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

Side by Side Diff: third_party/WebKit/Source/platform/graphics/GraphicsContext.cpp

Issue 2711983002: Paint dotted borders using circular dots (Closed)
Patch Set: Refactor isDashedStroke 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2003, 2004, 2005, 2006, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 bool isVerticalLine = (p1.x() == p2.x()); 494 bool isVerticalLine = (p1.x() == p2.x());
495 int width = roundf(strokeThickness()); 495 int width = roundf(strokeThickness());
496 496
497 // We know these are vertical or horizontal lines, so the length will just 497 // We know these are vertical or horizontal lines, so the length will just
498 // be the sum of the displacement component vectors give or take 1 - 498 // be the sum of the displacement component vectors give or take 1 -
499 // probably worth the speed up of no square root, which also won't be exact. 499 // probably worth the speed up of no square root, which also won't be exact.
500 FloatSize disp = p2 - p1; 500 FloatSize disp = p2 - p1;
501 int length = SkScalarRoundToInt(disp.width() + disp.height()); 501 int length = SkScalarRoundToInt(disp.width() + disp.height());
502 PaintFlags flags(immutableState()->strokeFlags(length)); 502 PaintFlags flags(immutableState()->strokeFlags(length));
503 503
504 if (getStrokeStyle() == DottedStroke || getStrokeStyle() == DashedStroke) { 504 if (StrokeData::strokeIsDashed(width, getStrokeStyle())) {
505 // Do a rect fill of our endpoints. This ensures we always have the 505 // Do a rect fill of our endpoints. This ensures we always have the
506 // appearance of being a border. We then draw the actual dotted/dashed 506 // appearance of being a border. We then draw the actual dotted/dashed
507 // line. 507 // line.
508 SkRect r1, r2; 508 SkRect r1, r2;
509 r1.set(p1.x(), p1.y(), p1.x() + width, p1.y() + width); 509 r1.set(p1.x(), p1.y(), p1.x() + width, p1.y() + width);
510 r2.set(p2.x(), p2.y(), p2.x() + width, p2.y() + width); 510 r2.set(p2.x(), p2.y(), p2.x() + width, p2.y() + width);
511 511
512 if (isVerticalLine) { 512 if (isVerticalLine) {
513 r1.offset(-width / 2, 0); 513 r1.offset(-width / 2, 0);
514 r2.offset(-width / 2, -width); 514 r2.offset(-width / 2, -width);
515 } else { 515 } else {
516 r1.offset(0, -width / 2); 516 r1.offset(0, -width / 2);
517 r2.offset(-width, -width / 2); 517 r2.offset(-width, -width / 2);
518 } 518 }
519 PaintFlags fillFlags; 519 PaintFlags fillFlags;
520 fillFlags.setColor(flags.getColor()); 520 fillFlags.setColor(flags.getColor());
521 drawRect(r1, fillFlags); 521 drawRect(r1, fillFlags);
522 drawRect(r2, fillFlags); 522 drawRect(r2, fillFlags);
523 } else if (getStrokeStyle() == DottedStroke) {
524 // We draw thick dotted lines with 0 length dash strokes and round endcaps,
525 // producing circles. The endcaps extend beyond the line's endpoints,
526 // so move the start and end in.
527 if (isVerticalLine) {
528 p1.setY(p1.y() + width / 2.f);
529 p2.setY(p2.y() - width / 2.f);
530 } else {
531 p1.setX(p1.x() + width / 2.f);
532 p2.setX(p2.x() - width / 2.f);
533 }
523 } 534 }
524 535
525 adjustLineToPixelBoundaries(p1, p2, width, penStyle); 536 adjustLineToPixelBoundaries(p1, p2, width, penStyle);
526 m_canvas->drawLine(p1.x(), p1.y(), p2.x(), p2.y(), flags); 537 m_canvas->drawLine(p1.x(), p1.y(), p2.x(), p2.y(), flags);
527 } 538 }
528 539
529 namespace { 540 namespace {
530 541
531 #if !OS(MACOSX) 542 #if !OS(MACOSX)
532 543
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 1302
1292 void GraphicsContext::adjustLineToPixelBoundaries(FloatPoint& p1, 1303 void GraphicsContext::adjustLineToPixelBoundaries(FloatPoint& p1,
1293 FloatPoint& p2, 1304 FloatPoint& p2,
1294 float strokeWidth, 1305 float strokeWidth,
1295 StrokeStyle penStyle) { 1306 StrokeStyle penStyle) {
1296 // For odd widths, we add in 0.5 to the appropriate x/y so that the float 1307 // For odd widths, we add in 0.5 to the appropriate x/y so that the float
1297 // arithmetic works out. For example, with a border width of 3, WebKit will 1308 // arithmetic works out. For example, with a border width of 3, WebKit will
1298 // pass us (y1+y2)/2, e.g., (50+53)/2 = 103/2 = 51 when we want 51.5. It is 1309 // pass us (y1+y2)/2, e.g., (50+53)/2 = 103/2 = 51 when we want 51.5. It is
1299 // always true that an even width gave us a perfect position, but an odd width 1310 // always true that an even width gave us a perfect position, but an odd width
1300 // gave us a position that is off by exactly 0.5. 1311 // gave us a position that is off by exactly 0.5.
1301 if (penStyle == DottedStroke || penStyle == DashedStroke) { 1312 if (StrokeData::strokeIsDashed(strokeWidth, penStyle)) {
1302 if (p1.x() == p2.x()) { 1313 if (p1.x() == p2.x()) {
1303 p1.setY(p1.y() + strokeWidth); 1314 p1.setY(p1.y() + strokeWidth);
1304 p2.setY(p2.y() - strokeWidth); 1315 p2.setY(p2.y() - strokeWidth);
1305 } else { 1316 } else {
1306 p1.setX(p1.x() + strokeWidth); 1317 p1.setX(p1.x() + strokeWidth);
1307 p2.setX(p2.x() - strokeWidth); 1318 p2.setX(p2.x() - strokeWidth);
1308 } 1319 }
1309 } 1320 }
1310 1321
1311 if (static_cast<int>(strokeWidth) % 2) { // odd 1322 if (static_cast<int>(strokeWidth) % 2) { // odd
(...skipping 24 matching lines...) Expand all
1336 break; 1347 break;
1337 default: 1348 default:
1338 NOTREACHED(); 1349 NOTREACHED();
1339 break; 1350 break;
1340 } 1351 }
1341 1352
1342 return nullptr; 1353 return nullptr;
1343 } 1354 }
1344 1355
1345 } // namespace blink 1356 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/paint/ObjectPainter.cpp ('k') | third_party/WebKit/Source/platform/graphics/StrokeData.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698