OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
| 8 #include <cmath> |
8 #include "SkRRect.h" | 9 #include "SkRRect.h" |
9 #include "SkMatrix.h" | 10 #include "SkMatrix.h" |
| 11 #include "SkScaleToSides.h" |
10 | 12 |
11 /////////////////////////////////////////////////////////////////////////////// | 13 /////////////////////////////////////////////////////////////////////////////// |
12 | 14 |
13 void SkRRect::setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) { | 15 void SkRRect::setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) { |
14 fRect = rect; | 16 fRect = rect; |
15 fRect.sort(); | 17 fRect.sort(); |
16 | 18 |
17 if (fRect.isEmpty() || !fRect.isFinite()) { | 19 if (fRect.isEmpty() || !fRect.isFinite()) { |
18 this->setEmpty(); | 20 this->setEmpty(); |
19 return; | 21 return; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 } | 104 } |
103 | 105 |
104 fRadii[kUpperLeft_Corner].set(leftRad, topRad); | 106 fRadii[kUpperLeft_Corner].set(leftRad, topRad); |
105 fRadii[kUpperRight_Corner].set(rightRad, topRad); | 107 fRadii[kUpperRight_Corner].set(rightRad, topRad); |
106 fRadii[kLowerRight_Corner].set(rightRad, bottomRad); | 108 fRadii[kLowerRight_Corner].set(rightRad, bottomRad); |
107 fRadii[kLowerLeft_Corner].set(leftRad, bottomRad); | 109 fRadii[kLowerLeft_Corner].set(leftRad, bottomRad); |
108 | 110 |
109 SkDEBUGCODE(this->validate();) | 111 SkDEBUGCODE(this->validate();) |
110 } | 112 } |
111 | 113 |
112 /* | |
113 * TODO: clean this guy up and possibly add to SkScalar.h | |
114 */ | |
115 static inline SkScalar SkScalarDecULP(SkScalar value) { | |
116 #if SK_SCALAR_IS_FLOAT | |
117 return SkBits2Float(SkFloat2Bits(value) - 1); | |
118 #else | |
119 #error "need impl for doubles" | |
120 #endif | |
121 } | |
122 | |
123 /** | |
124 * We need all combinations of predicates to be true to have a "safe" radius va
lue. | |
125 */ | |
126 static SkScalar clamp_radius_check_predicates(SkScalar rad, SkScalar min, SkScal
ar max) { | |
127 SkASSERT(min < max); | |
128 if (rad > max - min || min + rad > max || max - rad < min) { | |
129 rad = SkScalarDecULP(rad); | |
130 } | |
131 return rad; | |
132 } | |
133 | |
134 // These parameters intentionally double. Apropos crbug.com/463920, if one of th
e | 114 // These parameters intentionally double. Apropos crbug.com/463920, if one of th
e |
135 // radii is huge while the other is small, single precision math can completely | 115 // radii is huge while the other is small, single precision math can completely |
136 // miss the fact that a scale is required. | 116 // miss the fact that a scale is required. |
137 static double compute_min_scale(double rad1, double rad2, double limit, double c
urMin) { | 117 static double compute_min_scale(double rad1, double rad2, double limit, double c
urMin) { |
138 if ((rad1 + rad2) > limit) { | 118 if ((rad1 + rad2) > limit) { |
139 return SkTMin(curMin, limit / (rad1 + rad2)); | 119 return SkTMin(curMin, limit / (rad1 + rad2)); |
140 } | 120 } |
141 return curMin; | 121 return curMin; |
142 } | 122 } |
143 | 123 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 // that to scale down _all_ the radii. This algorithm is from the | 163 // that to scale down _all_ the radii. This algorithm is from the |
184 // W3 spec (http://www.w3.org/TR/css3-background/) section 5.5 - Overlapping | 164 // W3 spec (http://www.w3.org/TR/css3-background/) section 5.5 - Overlapping |
185 // Curves: | 165 // Curves: |
186 // "Let f = min(Li/Si), where i is one of { top, right, bottom, left }, | 166 // "Let f = min(Li/Si), where i is one of { top, right, bottom, left }, |
187 // Si is the sum of the two corresponding radii of the corners on side i, | 167 // Si is the sum of the two corresponding radii of the corners on side i, |
188 // and Ltop = Lbottom = the width of the box, | 168 // and Ltop = Lbottom = the width of the box, |
189 // and Lleft = Lright = the height of the box. | 169 // and Lleft = Lright = the height of the box. |
190 // If f < 1, then all corner radii are reduced by multiplying them by f." | 170 // If f < 1, then all corner radii are reduced by multiplying them by f." |
191 double scale = 1.0; | 171 double scale = 1.0; |
192 | 172 |
193 scale = compute_min_scale(fRadii[0].fX, fRadii[1].fX, fRect.width(), scale)
; | 173 // The sides of the rectangle may be larger than a float. |
194 scale = compute_min_scale(fRadii[1].fY, fRadii[2].fY, fRect.height(), scale)
; | 174 double width = (double)fRect.fRight - (double)fRect.fLeft; |
195 scale = compute_min_scale(fRadii[2].fX, fRadii[3].fX, fRect.width(), scale)
; | 175 double height = (double)fRect.fBottom - (double)fRect.fTop; |
196 scale = compute_min_scale(fRadii[3].fY, fRadii[0].fY, fRect.height(), scale)
; | 176 scale = compute_min_scale(fRadii[0].fX, fRadii[1].fX, width, scale); |
| 177 scale = compute_min_scale(fRadii[1].fY, fRadii[2].fY, height, scale); |
| 178 scale = compute_min_scale(fRadii[2].fX, fRadii[3].fX, width, scale); |
| 179 scale = compute_min_scale(fRadii[3].fY, fRadii[0].fY, height, scale); |
197 | 180 |
198 if (scale < 1.0) { | 181 if (scale < 1.0) { |
199 for (int i = 0; i < 4; ++i) { | 182 ScaleToSides::AdjustRadii(width, scale, &fRadii[0].fX, &fRadii[1].fX); |
200 fRadii[i].fX *= scale; | 183 ScaleToSides::AdjustRadii(height, scale, &fRadii[1].fY, &fRadii[2].fY); |
201 fRadii[i].fY *= scale; | 184 ScaleToSides::AdjustRadii(width, scale, &fRadii[2].fX, &fRadii[3].fX); |
202 } | 185 ScaleToSides::AdjustRadii(height, scale, &fRadii[3].fY, &fRadii[0].fY); |
203 } | 186 } |
204 | 187 |
205 // https://bug.skia.org/3239 -- its possible that we can hit the following i
nconsistency: | |
206 // rad == bounds.bottom - bounds.top | |
207 // bounds.bottom - radius < bounds.top | |
208 // YIKES | |
209 // We need to detect and "fix" this now, otherwise we can have the following
wackiness: | |
210 // path.addRRect(rrect); | |
211 // rrect.rect() != path.getBounds() | |
212 for (int i = 0; i < 4; ++i) { | |
213 fRadii[i].fX = clamp_radius_check_predicates(fRadii[i].fX, fRect.fLeft,
fRect.fRight); | |
214 fRadii[i].fY = clamp_radius_check_predicates(fRadii[i].fY, fRect.fTop, f
Rect.fBottom); | |
215 } | |
216 // At this point we're either oval, simple, or complex (not empty or rect). | 188 // At this point we're either oval, simple, or complex (not empty or rect). |
217 this->computeType(); | 189 this->computeType(); |
218 | 190 |
219 SkDEBUGCODE(this->validate();) | 191 SkDEBUGCODE(this->validate();) |
220 } | 192 } |
221 | 193 |
222 // This method determines if a point known to be inside the RRect's bounds is | 194 // This method determines if a point known to be inside the RRect's bounds is |
223 // inside all the corners. | 195 // inside all the corners. |
224 bool SkRRect::checkCornerContainment(SkScalar x, SkScalar y) const { | 196 bool SkRRect::checkCornerContainment(SkScalar x, SkScalar y) const { |
225 SkPoint canonicalPt; // (x,y) translated to one of the quadrants | 197 SkPoint canonicalPt; // (x,y) translated to one of the quadrants |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 } | 564 } |
593 | 565 |
594 for (int i = 0; i < 4; ++i) { | 566 for (int i = 0; i < 4; ++i) { |
595 validate_radius_check_predicates(fRadii[i].fX, fRect.fLeft, fRect.fRight
); | 567 validate_radius_check_predicates(fRadii[i].fX, fRect.fLeft, fRect.fRight
); |
596 validate_radius_check_predicates(fRadii[i].fY, fRect.fTop, fRect.fBottom
); | 568 validate_radius_check_predicates(fRadii[i].fY, fRect.fTop, fRect.fBottom
); |
597 } | 569 } |
598 } | 570 } |
599 #endif // SK_DEBUG | 571 #endif // SK_DEBUG |
600 | 572 |
601 /////////////////////////////////////////////////////////////////////////////// | 573 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |