Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / StrokeData.cpp
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
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(new SkDashPathEffect(intervals.get(), count, dashOffset));
58 }
59
60 float StrokeData::setupPaint(SkPaint* paint, int length) const
61 {
62     float width = m_thickness;
63
64     paint->setStyle(SkPaint::kStroke_Style);
65     paint->setStrokeWidth(SkFloatToScalar(width));
66     paint->setStrokeCap(m_lineCap);
67     paint->setStrokeJoin(m_lineJoin);
68     paint->setStrokeMiter(SkFloatToScalar(m_miterLimit));
69
70     if (m_dash) {
71         paint->setPathEffect(m_dash.get());
72     } else {
73         switch (m_style) {
74         case NoStroke:
75         case SolidStroke:
76         case DoubleStroke:
77         case WavyStroke: // FIXME: https://code.google.com/p/chromium/issues/detail?id=229574
78             break;
79         case DashedStroke:
80             width = dashRatio * width;
81             // Fall through.
82         case DottedStroke:
83             // Truncate the width, since we don't want fuzzy dots or dashes.
84             int dashLength = static_cast<int>(width);
85             // Subtract off the endcaps, since they're rendered separately.
86             int distance = length - 2 * static_cast<int>(m_thickness);
87             int phase = 1;
88             if (dashLength > 1) {
89                 // Determine how many dashes or dots we should have.
90                 int numDashes = distance / dashLength;
91                 int remainder = distance % dashLength;
92                 // Adjust the phase to center the dashes within the line.
93                 if (numDashes % 2) {
94                     // Odd: shift right a full dash, minus half the remainder.
95                     phase = dashLength - remainder / 2;
96                 } else {
97                     // Even: shift right half a dash, minus half the remainder.
98                     phase = (dashLength - remainder) / 2;
99                 }
100             }
101             SkScalar dashLengthSk = SkIntToScalar(dashLength);
102             SkScalar intervals[2] = { dashLengthSk, dashLengthSk };
103             RefPtr<SkDashPathEffect> pathEffect = adoptRef(new SkDashPathEffect(intervals, 2, SkIntToScalar(phase)));
104             paint->setPathEffect(pathEffect.get());
105         }
106     }
107
108     return width;
109 }
110
111 } // namespace