Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1107)

Unified Diff: third_party/WebKit/Source/platform/graphics/StrokeData.cpp

Issue 2737063002: Improve dashed line drawing (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/graphics/StrokeData.cpp
diff --git a/third_party/WebKit/Source/platform/graphics/StrokeData.cpp b/third_party/WebKit/Source/platform/graphics/StrokeData.cpp
index cbdb47b677f27bed5c95c4cb058e56f8c8842681..0ec4f922d72fa68748e3e5cefac97e7b1f6920fb 100644
--- a/third_party/WebKit/Source/platform/graphics/StrokeData.cpp
+++ b/third_party/WebKit/Source/platform/graphics/StrokeData.cpp
@@ -34,8 +34,6 @@
namespace blink {
-static const int dashRatio = 3; // Ratio of the length of a dash to its width.
-
void StrokeData::setLineDash(const DashArray& dashes, float dashOffset) {
// FIXME: This is lifted directly off SkiaSupport, lines 49-74
// so it is not guaranteed to work correctly.
@@ -68,39 +66,49 @@ void StrokeData::setupPaint(PaintFlags* flags, int length) const {
}
void StrokeData::setupPaintDashPathEffect(PaintFlags* flags, int length) const {
+ static float epsilon = 1.0e-2f;
f(malita) 2017/03/08 16:51:56 nit: can we leave this close to its only user, and
Stephen Chennney 2017/03/08 17:33:26 Yes, I should revert that back to where it was bef
if (m_dash) {
flags->setPathEffect(m_dash);
} else if (strokeIsDashed(m_thickness, m_style)) {
- float width =
- m_style == DashedStroke ? dashRatio * m_thickness : m_thickness;
-
- // Truncate the width, since we don't want fuzzy dots or dashes.
- int dashLength = static_cast<int>(width);
- // Subtract off the endcaps, since they're rendered separately.
- int distance = length - 2 * static_cast<int>(m_thickness);
- int phase = 1;
- if (dashLength > 1) {
- // Determine how many dashes or dots we should have.
- int numDashes = distance / dashLength;
- int remainder = distance % dashLength;
- // Adjust the phase to center the dashes within the line.
- if (numDashes % 2) {
- // Odd: shift right a full dash, minus half the remainder.
- phase = dashLength - remainder / 2;
- } else {
- // Even: shift right half a dash, minus half the remainder.
- phase = (dashLength - remainder) / 2;
+ float dashLength = m_thickness;
+ float gapLength = dashLength;
+ if (m_style == DashedStroke) {
+ dashLength *= StrokeData::dashLengthRatio(m_thickness);
+ gapLength *= StrokeData::dashGapRatio(m_thickness);
+ }
+ float perDashLength = dashLength + gapLength;
+ // Account for modification to effective length in
+ // GraphicsContext::adjustLineToPixelBoundaries
+ length -= 2 * m_thickness;
+ if (length <= dashLength) {
+ // No space for dashes
+ flags->setPathEffect(0);
+ } else if (length <= 2 * dashLength + gapLength) {
+ // Exactly 2 dashes proportionally sized
+ float multiplier = length / (2 * dashLength + gapLength);
+ SkScalar intervals[2] = {dashLength * multiplier, gapLength * multiplier};
+ flags->setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
+ } else {
+ float gap = gapLength;
+ if (m_style == DashedStroke) {
+ // Determine what number of dashes gives the minimum deviation from
+ // idealGap between dashes. Set the gap to that width.
+ float minNumDashes = floorf((length + gapLength) / perDashLength);
+ float maxNumDashes = minNumDashes + 1;
+ float minGap =
+ (length - minNumDashes * dashLength) / (minNumDashes - 1);
+ float maxGap =
+ (length - maxNumDashes * dashLength) / (maxNumDashes - 1);
+ gap = fabs(minGap - gapLength) < fabs(maxGap - gapLength) ? minGap
+ : maxGap;
}
+ SkScalar intervals[2] = {dashLength, gap};
+ flags->setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
}
- SkScalar dashLengthSk = SkIntToScalar(dashLength);
- SkScalar intervals[2] = {dashLengthSk, dashLengthSk};
- flags->setPathEffect(
- SkDashPathEffect::Make(intervals, 2, SkIntToScalar(phase)));
} else if (m_style == DottedStroke) {
flags->setStrokeCap((PaintFlags::Cap)RoundCap);
// Adjust the width to get equal dot spacing as much as possible.
float perDotLength = m_thickness * 2;
- static float epsilon = 1.0e-2f;
if (length < perDotLength + m_thickness) {
// Exactly 2 dots with whatever space we can get
SkScalar intervals[2] = {0, length - m_thickness - epsilon};

Powered by Google App Engine
This is Rietveld 408576698