1 // Copyright (C) 2013 Google Inc. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
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
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.
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.
30 #include "platform/graphics/StrokeData.h"
31 #include "wtf/OwnPtr.h"
32 #include "wtf/PassOwnPtr.h"
36 static const int dashRatio
= 3; // Ratio of the length of a dash to its width.
38 void StrokeData::setLineDash(const DashArray
& dashes
, float dashOffset
)
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();
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
;
51 size_t count
= !(dashLength
% 2) ? dashLength
: dashLength
* 2;
52 OwnPtr
<SkScalar
[]> intervals
= adoptArrayPtr(new SkScalar
[count
]);
54 for (unsigned i
= 0; i
< count
; i
++)
55 intervals
[i
] = dashes
[i
% dashLength
];
57 m_dash
= adoptRef(SkDashPathEffect::Create(intervals
.get(), count
, dashOffset
));
60 void StrokeData::setupPaint(SkPaint
* paint
, int length
) const
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
));
68 setupPaintDashPathEffect(paint
, length
);
71 void StrokeData::setupPaintDashPathEffect(SkPaint
* paint
, int length
) const
73 float width
= m_thickness
;
75 paint
->setPathEffect(m_dash
.get());
81 case WavyStroke
: // FIXME: https://code.google.com/p/chromium/issues/detail?id=229574
82 paint
->setPathEffect(0);
85 width
= dashRatio
* width
;
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
);
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.
99 // Odd: shift right a full dash, minus half the remainder.
100 phase
= dashLength
- remainder
/ 2;
102 // Even: shift right half a dash, minus half the remainder.
103 phase
= (dashLength
- remainder
) / 2;
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());