• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2013 Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //    * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //    * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //    * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "config.h"
30 #include "platform/graphics/StrokeData.h"
31 #include "wtf/OwnPtr.h"
32 #include "wtf/PassOwnPtr.h"
33 
34 namespace WebCore {
35 
36 static const int dashRatio = 3; // Ratio of the length of a dash to its width.
37 
setLineDash(const DashArray & dashes,float dashOffset)38 void StrokeData::setLineDash(const DashArray& dashes, float dashOffset)
39 {
40     // FIXME: This is lifted directly off SkiaSupport, lines 49-74
41     // so it is not guaranteed to work correctly.
42     size_t dashLength = dashes.size();
43     if (!dashLength) {
44         // If no dash is set, revert to solid stroke
45         // FIXME: do we need to set NoStroke in some cases?
46         m_style = SolidStroke;
47         m_dash.clear();
48         return;
49     }
50 
51     size_t count = !(dashLength % 2) ? dashLength : dashLength * 2;
52     OwnPtr<SkScalar[]> intervals = adoptArrayPtr(new SkScalar[count]);
53 
54     for (unsigned i = 0; i < count; i++)
55         intervals[i] = dashes[i % dashLength];
56 
57     m_dash = adoptRef(SkDashPathEffect::Create(intervals.get(), count, dashOffset));
58 }
59 
setupPaint(SkPaint * paint,int length) const60 void StrokeData::setupPaint(SkPaint* paint, int length) const
61 {
62     paint->setStyle(SkPaint::kStroke_Style);
63     paint->setStrokeWidth(SkFloatToScalar(m_thickness));
64     paint->setStrokeCap(m_lineCap);
65     paint->setStrokeJoin(m_lineJoin);
66     paint->setStrokeMiter(SkFloatToScalar(m_miterLimit));
67 
68     setupPaintDashPathEffect(paint, length);
69 }
70 
setupPaintDashPathEffect(SkPaint * paint,int length) const71 void StrokeData::setupPaintDashPathEffect(SkPaint* paint, int length) const
72 {
73     float width = m_thickness;
74     if (m_dash) {
75         paint->setPathEffect(m_dash.get());
76     } else {
77         switch (m_style) {
78         case NoStroke:
79         case SolidStroke:
80         case DoubleStroke:
81         case WavyStroke: // FIXME: https://code.google.com/p/chromium/issues/detail?id=229574
82             paint->setPathEffect(0);
83             return;
84         case DashedStroke:
85             width = dashRatio * width;
86             // Fall through.
87         case DottedStroke:
88             // Truncate the width, since we don't want fuzzy dots or dashes.
89             int dashLength = static_cast<int>(width);
90             // Subtract off the endcaps, since they're rendered separately.
91             int distance = length - 2 * static_cast<int>(m_thickness);
92             int phase = 1;
93             if (dashLength > 1) {
94                 // Determine how many dashes or dots we should have.
95                 int numDashes = distance / dashLength;
96                 int remainder = distance % dashLength;
97                 // Adjust the phase to center the dashes within the line.
98                 if (numDashes % 2) {
99                     // Odd: shift right a full dash, minus half the remainder.
100                     phase = dashLength - remainder / 2;
101                 } else {
102                     // Even: shift right half a dash, minus half the remainder.
103                     phase = (dashLength - remainder) / 2;
104                 }
105             }
106             SkScalar dashLengthSk = SkIntToScalar(dashLength);
107             SkScalar intervals[2] = { dashLengthSk, dashLengthSk };
108             RefPtr<SkDashPathEffect> pathEffect = adoptRef(SkDashPathEffect::Create(intervals, 2, SkIntToScalar(phase)));
109             paint->setPathEffect(pathEffect.get());
110         }
111     }
112 }
113 
114 } // namespace
115