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 <memory>
30
31 #include "third_party/blink/renderer/platform/graphics/paint/paint_flags.h"
32 #include "third_party/blink/renderer/platform/graphics/stroke_data.h"
33 #include "third_party/skia/include/effects/SkDashPathEffect.h"
34
35 namespace blink {
36
SetLineDash(const DashArray & dashes,float dash_offset)37 void StrokeData::SetLineDash(const DashArray& dashes, float dash_offset) {
38 // FIXME: This is lifted directly off SkiaSupport, lines 49-74
39 // so it is not guaranteed to work correctly.
40 size_t dash_length = dashes.size();
41 if (!dash_length) {
42 // If no dash is set, revert to solid stroke
43 // FIXME: do we need to set NoStroke in some cases?
44 style_ = kSolidStroke;
45 dash_.reset();
46 return;
47 }
48
49 size_t count = !(dash_length % 2) ? dash_length : dash_length * 2;
50 auto intervals = std::make_unique<SkScalar[]>(count);
51
52 for (unsigned i = 0; i < count; i++)
53 intervals[i] = dashes[i % dash_length];
54
55 dash_ = SkDashPathEffect::Make(intervals.get(), count, dash_offset);
56 }
57
SetupPaint(PaintFlags * flags,const int length,const int dash_thickness) const58 void StrokeData::SetupPaint(PaintFlags* flags,
59 const int length,
60 const int dash_thickness) const {
61 flags->setStyle(PaintFlags::kStroke_Style);
62 flags->setStrokeWidth(SkFloatToScalar(thickness_));
63 flags->setStrokeCap(line_cap_);
64 flags->setStrokeJoin(line_join_);
65 flags->setStrokeMiter(SkFloatToScalar(miter_limit_));
66
67 SetupPaintDashPathEffect(flags, length, dash_thickness);
68 }
69
SetupPaintDashPathEffect(PaintFlags * flags,const int length,const int dash_thickness) const70 void StrokeData::SetupPaintDashPathEffect(PaintFlags* flags,
71 const int length,
72 const int dash_thickness) const {
73 int dash_width = dash_thickness ? dash_thickness : thickness_;
74 if (dash_) {
75 flags->setPathEffect(dash_);
76 } else if (StrokeIsDashed(dash_width, style_)) {
77 float dash_length = dash_width;
78 float gap_length = dash_length;
79 if (style_ == kDashedStroke) {
80 dash_length *= StrokeData::DashLengthRatio(dash_width);
81 gap_length *= StrokeData::DashGapRatio(dash_width);
82 }
83 if (length <= dash_length * 2) {
84 // No space for dashes
85 flags->setPathEffect(nullptr);
86 } else if (length <= 2 * dash_length + gap_length) {
87 // Exactly 2 dashes proportionally sized
88 float multiplier = length / (2 * dash_length + gap_length);
89 SkScalar intervals[2] = {dash_length * multiplier,
90 gap_length * multiplier};
91 flags->setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
92 } else {
93 float gap = gap_length;
94 if (style_ == kDashedStroke)
95 gap = SelectBestDashGap(length, dash_length, gap_length);
96 SkScalar intervals[2] = {dash_length, gap};
97 flags->setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
98 }
99 } else if (style_ == kDottedStroke) {
100 flags->setStrokeCap((PaintFlags::Cap)kRoundCap);
101 // Adjust the width to get equal dot spacing as much as possible.
102 float per_dot_length = dash_width * 2;
103 if (length < per_dot_length) {
104 // Not enoguh space for 2 dots. Just draw 1 by giving a gap that is
105 // bigger than the length.
106 SkScalar intervals[2] = {0, per_dot_length};
107 flags->setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
108 return;
109 }
110
111 // Epsilon ensures that we get a whole dot at the end of the line,
112 // even if that dot is a little inside the true endpoint. Without it
113 // we can drop the end dot due to rounding along the line.
114 static const float kEpsilon = 1.0e-2f;
115 float gap = SelectBestDashGap(length, dash_width, dash_width);
116 SkScalar intervals[2] = {0, gap + dash_width - kEpsilon};
117 flags->setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
118 } else {
119 flags->setPathEffect(nullptr);
120 }
121 }
122
StrokeIsDashed(float width,StrokeStyle style)123 bool StrokeData::StrokeIsDashed(float width, StrokeStyle style) {
124 return style == kDashedStroke || (style == kDottedStroke && width <= 3);
125 }
126
SelectBestDashGap(float stroke_length,float dash_length,float gap_length)127 float StrokeData::SelectBestDashGap(float stroke_length,
128 float dash_length,
129 float gap_length) {
130 // Determine what number of dashes gives the minimum deviation from
131 // gapLength between dashes. Set the gap to that width.
132 float min_num_dashes =
133 floorf((stroke_length + gap_length) / (dash_length + gap_length));
134 float max_num_dashes = min_num_dashes + 1;
135 float min_gap =
136 (stroke_length - min_num_dashes * dash_length) / (min_num_dashes - 1);
137 float max_gap =
138 (stroke_length - max_num_dashes * dash_length) / (max_num_dashes - 1);
139 return (max_gap <= 0) ||
140 (fabs(min_gap - gap_length) < fabs(max_gap - gap_length))
141 ? min_gap
142 : max_gap;
143 }
144
145 } // namespace blink
146