1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
* (C) 2005, 2006 Samuel Weinig (sam.weinig@gmail.com)
* Copyright (C) 2005-2025 Apple Inc. All rights reserved.
* Copyright (C) 2010 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "OutlinePainter.h"
#include "BorderEdge.h"
#include "BorderPainter.h"
#include "BorderShape.h"
#include "ContainerNodeInlines.h"
#include "FloatPointGraph.h"
#include "FloatRoundedRect.h"
#include "GeometryUtilities.h"
#include "GraphicsContext.h"
#include "HTMLNames.h"
#include "HTMLSelectElement.h"
#include "InlineIteratorInlineBox.h"
#include "InlineIteratorLineBox.h"
#include "LegacyRenderSVGModelObject.h"
#include "PaintInfo.h"
#include "PathUtilities.h"
#include "RenderBlockFlow.h"
#include "RenderChildIterator.h"
#include "RenderInline.h"
#include "RenderListBox.h"
#include "RenderObjectDocument.h"
#include "RenderStyle+GettersInlines.h"
#include "RenderSVGModelObject.h"
#include "RenderTheme.h"
#include "StyleBorderRadius.h"
#include "StylePrimitiveNumericTypes+Evaluation.h"
namespace WebCore {
OutlinePainter::OutlinePainter(const PaintInfo& paintInfo)
: m_paintInfo(paintInfo)
{
}
static float deviceScaleFactor(const RenderElement& renderer)
{
return renderer.protectedDocument()->deviceScaleFactor();
}
void OutlinePainter::paintOutline(const RenderElement& renderer, const LayoutRect& paintRect) const
{
ASSERT(renderer.hasOutline());
CheckedRef styleToUse = renderer.style();
auto hasThemedFocusRing = renderer.theme().supportsFocusRing(renderer, styleToUse.get());
// Only paint the focus ring by hand if the theme isn't able to draw it.
if (styleToUse->outlineStyle() == OutlineStyle::Auto && !hasThemedFocusRing) {
LayoutRect paintRectToUse { paintRect };
if (CheckedPtr box = dynamicDowncast<RenderBox>(renderer))
paintRectToUse = renderer.theme().adjustedPaintRect(*box, paintRectToUse);
CheckedPtr paintContainer = m_paintInfo.paintContainer;
auto focusRingRects = collectFocusRingRects(renderer, paintRectToUse.location(), paintContainer.get());
paintFocusRing(renderer, focusRingRects);
return;
}
if (renderer.hasOutlineAnnotation() && !hasThemedFocusRing)
addPDFURLAnnotationForLink(renderer, paintRect.location());
auto borderStyle = toBorderStyle(styleToUse->outlineStyle());
if (!borderStyle || *borderStyle == BorderStyle::None)
return;
auto outlineWidth = Style::evaluate<LayoutUnit>(styleToUse->usedOutlineWidth(), Style::ZoomNeeded { });
auto outlineOffset = Style::evaluate<LayoutUnit>(styleToUse->usedOutlineOffset(), Style::ZoomNeeded { });
auto outerRect = paintRect;
outerRect.inflate(outlineOffset + outlineWidth);
// FIXME: This prevents outlines from painting inside the object http://webkit.org/b/12042.
if (outerRect.isEmpty())
return;
auto hasBorderRadius = styleToUse->hasBorderRadius();
auto closedEdges = RectEdges<bool> { true };
auto outlineEdgeWidths = RectEdges<LayoutUnit> { outlineWidth };
auto outlineShape = BorderShape::shapeForOutsetRect(styleToUse.get(), paintRect, outerRect, outlineEdgeWidths, closedEdges);
auto bleedAvoidance = BleedAvoidance::ShrinkBackground;
auto appliedClipAlready = false;
auto edges = borderEdgesForOutline(styleToUse, *borderStyle, deviceScaleFactor(renderer));
auto haveAllSolidEdges = BorderPainter::decorationHasAllSolidEdges(edges);
BorderPainter { renderer, m_paintInfo }.paintSides(outlineShape, {
hasBorderRadius ? std::make_optional(styleToUse->borderRadii()) : std::nullopt,
edges,
haveAllSolidEdges,
outlineShape.outerShapeIsRectangular(),
outlineShape.innerShapeIsRectangular(),
bleedAvoidance,
closedEdges,
appliedClipAlready,
});
}
void OutlinePainter::paintOutline(const RenderInline& renderer, const LayoutPoint& paintOffset) const
{
ASSERT(renderer.hasOutline());
CheckedRef styleToUse = renderer.style();
if (styleToUse->outlineStyle() == OutlineStyle::Auto) {
CheckedPtr paintContainer = m_paintInfo.paintContainer;
auto focusRingRects = collectFocusRingRects(renderer, paintOffset, paintContainer.get());
paintFocusRing(renderer, focusRingRects);
return;
}
if (renderer.hasOutlineAnnotation())
addPDFURLAnnotationForLink(renderer, paintOffset);
if (m_paintInfo.context().paintingDisabled() || !styleToUse->hasOutline())
return;
if (!renderer.containingBlock()) {
ASSERT_NOT_REACHED();
return;
}
auto isHorizontalWritingMode = renderer.isHorizontalWritingMode();
CheckedRef containingBlock = *renderer.containingBlock();
auto isFlipped = containingBlock->writingMode().isBlockFlipped();
Vector<LayoutRect> rects;
for (auto box = InlineIterator::lineLeftmostInlineBoxFor(renderer); box; box.traverseInlineBoxLineRightward()) {
auto lineBox = box->lineBox();
auto logicalTop = std::max(lineBox->contentLogicalTop(), box->logicalTop());
auto logicalBottom = std::min(lineBox->contentLogicalBottom(), box->logicalBottom());
auto enclosingVisualRect = FloatRect { box->logicalLeftIgnoringInlineDirection(), logicalTop, box->logicalWidth(), logicalBottom - logicalTop };
if (!isHorizontalWritingMode)
enclosingVisualRect = enclosingVisualRect.transposedRect();
if (isFlipped)
containingBlock->flipForWritingMode(enclosingVisualRect);
rects.append(LayoutRect { enclosingVisualRect });
}
paintOutlineWithLineRects(renderer, paintOffset, rects);
}
void OutlinePainter::paintOutlineWithLineRects(const RenderInline& renderer, const LayoutPoint& paintOffset, const Vector<LayoutRect>& lineRects) const
{
if (lineRects.size() == 1) {
auto adjustedPaintRect = lineRects[0];
adjustedPaintRect.moveBy(paintOffset);
paintOutline(renderer, adjustedPaintRect);
return;
}
auto styleToUse = CheckedRef { renderer.style() };
auto outlineOffset = Style::evaluate<float>(styleToUse->usedOutlineOffset(), Style::ZoomNeeded { });
auto outlineWidth = Style::evaluate<float>(styleToUse->usedOutlineWidth(), Style::ZoomNeeded { });
auto deviceScaleFactor = WebCore::deviceScaleFactor(renderer);
Vector<FloatRect> pixelSnappedRects;
for (size_t index = 0; index < lineRects.size(); ++index) {
auto rect = lineRects[index];
rect.moveBy(paintOffset);
rect.inflate(outlineOffset + outlineWidth / 2);
pixelSnappedRects.append(snapRectToDevicePixels(rect, deviceScaleFactor));
}
auto path = pathWithShrinkWrappedRects(pixelSnappedRects, styleToUse->border().radii, outlineOffset, styleToUse->writingMode(), deviceScaleFactor);
if (path.isEmpty()) {
// Disjoint line spanning inline boxes.
for (auto rect : lineRects) {
rect.moveBy(paintOffset);
paintOutline(renderer, rect);
}
return;
}
auto& graphicsContext = m_paintInfo.context();
auto outlineColor = styleToUse->visitedDependentOutlineColorApplyingColorFilter();
auto useTransparencyLayer = !outlineColor.isOpaque();
if (useTransparencyLayer) {
graphicsContext.beginTransparencyLayer(outlineColor.alphaAsFloat());
outlineColor = outlineColor.opaqueColor();
}
graphicsContext.setStrokeColor(outlineColor);
graphicsContext.setStrokeThickness(outlineWidth);
graphicsContext.setStrokeStyle(StrokeStyle::SolidStroke);
graphicsContext.strokePath(path);
if (useTransparencyLayer)
graphicsContext.endTransparencyLayer();
}
static bool usePlatformFocusRingColorForOutlineStyleAuto()
{
#if PLATFORM(COCOA) || PLATFORM(GTK) || PLATFORM(WPE)
return true;
#else
return false;
#endif
}
static bool useShrinkWrappedFocusRingForOutlineStyleAuto()
{
#if PLATFORM(COCOA) || PLATFORM(GTK) || PLATFORM(WPE)
return true;
#else
return false;
#endif
}
static void drawFocusRing(GraphicsContext& context, const Path& path, const RenderStyle& style, const Color& color)
{
context.drawFocusRing(path, Style::evaluate<float>(style.usedOutlineWidth(), Style::ZoomNeeded { }), color);
}
static void drawFocusRing(GraphicsContext& context, Vector<FloatRect> rects, const RenderStyle& style, const Color& color)
{
#if PLATFORM(MAC)
context.drawFocusRing(rects, 0, Style::evaluate<float>(style.usedOutlineWidth(), Style::ZoomNeeded { }), color);
#else
context.drawFocusRing(rects, Style::evaluate<float>(style.usedOutlineOffset(), Style::ZoomNeeded { }), Style::evaluate<float>(style.usedOutlineWidth(), Style::ZoomNeeded { }), color);
#endif
}
void OutlinePainter::paintFocusRing(const RenderElement& renderer, const Vector<LayoutRect>& focusRingRects) const
{
CheckedRef style = renderer.style();
ASSERT(style->outlineStyle() == OutlineStyle::Auto);
auto deviceScaleFactor = WebCore::deviceScaleFactor(renderer);
auto outlineOffset = Style::evaluate<float>(style->usedOutlineOffset(), Style::ZoomNeeded { });
Vector<FloatRect> pixelSnappedFocusRingRects;
for (auto rect : focusRingRects) {
rect.inflate(outlineOffset);
pixelSnappedFocusRingRects.append(snapRectToDevicePixels(rect, deviceScaleFactor));
}
auto styleOptions = renderer.styleColorOptions();
styleOptions.add(StyleColorOptions::UseSystemAppearance);
auto focusRingColor = usePlatformFocusRingColorForOutlineStyleAuto() ? RenderTheme::singleton().focusRingColor(styleOptions) : style->visitedDependentOutlineColorApplyingColorFilter();
if (useShrinkWrappedFocusRingForOutlineStyleAuto() && style->hasBorderRadius()) {
auto path = pathWithShrinkWrappedRects(pixelSnappedFocusRingRects, style->border().radii, outlineOffset, style->writingMode(), deviceScaleFactor);
if (path.isEmpty()) {
for (auto rect : pixelSnappedFocusRingRects)
path.addRect(rect);
}
drawFocusRing(m_paintInfo.context(), path, style.get(), focusRingColor);
} else
drawFocusRing(m_paintInfo.context(), pixelSnappedFocusRingRects, style.get(), focusRingColor);
}
Vector<LayoutRect> OutlinePainter::collectFocusRingRects(const RenderElement& renderer, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer)
{
Vector<LayoutRect> rects;
collectFocusRingRects(renderer, rects, additionalOffset, paintContainer);
return rects;
}
static void appendIfNotEmpty(Vector<LayoutRect>& rects, LayoutRect&& rect)
{
if (rect.isEmpty())
return;
rects.append(rect);
}
void OutlinePainter::collectFocusRingRects(const RenderElement& renderer, Vector<LayoutRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer)
{
if (CheckedPtr svgRenderer = dynamicDowncast<RenderSVGModelObject>(renderer)) {
svgRenderer->addFocusRingRects(rects, additionalOffset, paintContainer);
return;
}
if (CheckedPtr svgRenderer = dynamicDowncast<LegacyRenderSVGModelObject>(renderer)) {
svgRenderer->addFocusRingRects(rects, additionalOffset, paintContainer);
return;
}
if (CheckedPtr renderInline = dynamicDowncast<RenderInline>(renderer)) {
collectFocusRingRectsForInline(*renderInline, rects, additionalOffset, paintContainer);
return;
}
if (CheckedPtr listBox = dynamicDowncast<RenderListBox>(renderer)) {
if (collectFocusRingRectsForListBox(*listBox, rects, additionalOffset, paintContainer))
return;
}
if (CheckedPtr block = dynamicDowncast<RenderBlock>(renderer)) {
if (collectFocusRingRectsForBlock(*block, rects, additionalOffset, paintContainer))
return;
}
if (CheckedPtr box = dynamicDowncast<RenderBox>(renderer))
appendIfNotEmpty(rects, { additionalOffset, box->size() });
}
bool OutlinePainter::collectFocusRingRectsForListBox(const RenderListBox& renderer, Vector<LayoutRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*)
{
if (!renderer.selectElement().allowsNonContiguousSelection())
return false;
CheckedRef selectElement = renderer.selectElement();
// Focus the last selected item.
int selectedItem = selectElement->activeSelectionEndListIndex();
if (selectedItem >= 0) {
rects.append(snappedIntRect(renderer.itemBoundingBoxRect(additionalOffset, selectedItem)));
return true;
}
// No selected items, find the first non-disabled item.
int indexOfFirstEnabledOption = 0;
for (auto& item : selectElement->listItems()) {
if (is<HTMLOptionElement>(item.get()) && !item->isDisabledFormControl()) {
selectElement->setActiveSelectionEndIndex(indexOfFirstEnabledOption);
rects.append(renderer.itemBoundingBoxRect(additionalOffset, indexOfFirstEnabledOption));
return true;
}
indexOfFirstEnabledOption++;
}
return true;
}
void OutlinePainter::collectFocusRingRectsForInline(const RenderInline& renderer, Vector<LayoutRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer)
{
renderer.collectLineBoxRects(rects, additionalOffset);
for (CheckedRef child : childrenOfType<RenderBoxModelObject>(renderer)) {
if (child->isRenderListMarker())
continue;
FloatPoint pos(additionalOffset);
// FIXME: This doesn't work correctly with transforms.
if (child->hasLayer())
pos = child->localToContainerPoint(FloatPoint(), paintContainer);
else if (auto* box = dynamicDowncast<RenderBox>(child.get()))
pos.move(box->locationOffset());
collectFocusRingRects(child, rects, flooredIntPoint(pos), paintContainer);
}
if (CheckedPtr continuation = renderer.continuation()) {
if (CheckedPtr inlineRenderer = dynamicDowncast<RenderInline>(*continuation))
collectFocusRingRectsForInline(*inlineRenderer, rects, flooredLayoutPoint(LayoutPoint(additionalOffset + continuation->containingBlock()->location() - renderer.containingBlock()->location())), paintContainer);
else
collectFocusRingRects(*continuation, rects, flooredLayoutPoint(LayoutPoint(additionalOffset + downcast<RenderBox>(*continuation).location() - renderer.containingBlock()->location())), paintContainer);
}
}
bool OutlinePainter::collectFocusRingRectsForBlock(const RenderBlock& renderer, Vector<LayoutRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer)
{
if (renderer.isRenderTextControl())
return false;
// For blocks inside inlines, we include margins so that we run right up to the inline boxes
// above and below us (thus getting merged with them to form a single irregular shape).
CheckedPtr inlineContinuation = renderer.inlineContinuation();
if (inlineContinuation) {
// FIXME: This check really isn't accurate.
bool nextInlineHasLineBox = inlineContinuation->firstLegacyInlineBox();
// FIXME: This is wrong. The principal renderer may not be the continuation preceding this block.
// FIXME: This is wrong for block-flows that are horizontal.
// https://bugs.webkit.org/show_bug.cgi?id=46781
bool prevInlineHasLineBox = downcast<RenderInline>(*inlineContinuation->element()->renderer()).firstLegacyInlineBox();
auto topMargin = prevInlineHasLineBox ? renderer.collapsedMarginBefore() : 0_lu;
auto bottomMargin = nextInlineHasLineBox ? renderer.collapsedMarginAfter() : 0_lu;
LayoutRect rect(additionalOffset.x(), additionalOffset.y() - topMargin, renderer.width(), renderer.height() + topMargin + bottomMargin);
appendIfNotEmpty(rects, WTF::move(rect));
} else if (renderer.width() && renderer.height())
rects.append(LayoutRect(additionalOffset, renderer.size()));
if (!renderer.hasNonVisibleOverflow() && !renderer.hasControlClip()) {
if (renderer.childrenInline() && is<RenderBlockFlow>(renderer))
collectFocusRingRectsForInlineChildren(downcast<RenderBlockFlow>(renderer), rects, additionalOffset, paintContainer);
for (CheckedRef box : childrenOfType<RenderBox>(renderer))
collectFocusRingRectsForChildBox(box, rects, additionalOffset, paintContainer);
}
if (inlineContinuation)
collectFocusRingRects(*inlineContinuation, rects, flooredLayoutPoint(LayoutPoint(additionalOffset + inlineContinuation->containingBlock()->location() - renderer.location())), paintContainer);
return true;
}
void OutlinePainter::collectFocusRingRectsForChildBox(const RenderBox& box, Vector<LayoutRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer)
{
if (box.isRenderListMarker() || box.isOutOfFlowPositioned())
return;
FloatPoint pos;
// FIXME: This doesn't work correctly with transforms.
if (box.layer())
pos = box.localToContainerPoint(FloatPoint(), paintContainer);
else
pos = FloatPoint(additionalOffset.x() + box.x(), additionalOffset.y() + box.y());
collectFocusRingRects(box, rects, flooredLayoutPoint(pos), paintContainer);
}
void OutlinePainter::collectFocusRingRectsForInlineChildren(const RenderBlockFlow& renderer, Vector<LayoutRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer)
{
ASSERT(renderer.childrenInline());
bool hasBlockContent = false;
for (auto box = InlineIterator::firstRootInlineBoxFor(renderer); box; box.traverseInlineBoxLineRightward()) {
auto lineBox = box->lineBox();
if (lineBox->hasBlockContent()) {
hasBlockContent = true;
continue;
}
// FIXME: This is mixing physical and logical coordinates.
auto unflippedVisualRect = box->visualRectIgnoringBlockDirection();
auto top = std::max(lineBox->contentLogicalTop(), unflippedVisualRect.y());
auto bottom = std::min(lineBox->contentLogicalBottom(), unflippedVisualRect.maxY());
auto rect = LayoutRect { LayoutUnit { additionalOffset.x() + unflippedVisualRect.x() }
, additionalOffset.y() + top
, LayoutUnit { unflippedVisualRect.width() }
, bottom - top };
appendIfNotEmpty(rects, WTF::move(rect));
}
if (hasBlockContent) {
for (auto line = InlineIterator::firstLineBoxFor(renderer); line; line.traverseNext()) {
auto blockLevelBox = line->blockLevelBox();
if (!blockLevelBox)
continue;
CheckedRef renderBox = downcast<RenderBox>(blockLevelBox->renderer());
collectFocusRingRectsForChildBox(renderBox, rects, additionalOffset, paintContainer);
}
}
}
static std::pair<FloatPoint, FloatPoint> startAndEndPointsForCorner(const FloatPointGraph::Edge& fromEdge, const FloatPointGraph::Edge& toEdge, const FloatSize& radius)
{
FloatSize fromEdgeVector = *fromEdge.second - *fromEdge.first;
FloatSize toEdgeVector = *toEdge.second - *toEdge.first;
FloatPoint fromEdgeNorm = toFloatPoint(fromEdgeVector);
fromEdgeNorm.normalize();
FloatSize fromOffset = FloatSize(radius.width() * fromEdgeNorm.x(), radius.height() * fromEdgeNorm.y());
FloatPoint startPoint = *fromEdge.second - fromOffset;
FloatPoint toEdgeNorm = toFloatPoint(toEdgeVector);
toEdgeNorm.normalize();
FloatSize toOffset = FloatSize(radius.width() * toEdgeNorm.x(), radius.height() * toEdgeNorm.y());
FloatPoint endPoint = *toEdge.first + toOffset;
return std::make_pair(startPoint, endPoint);
}
enum class CornerType : uint8_t { TopLeft, TopRight, BottomRight, BottomLeft, Other };
static CornerType cornerType(const FloatPointGraph::Edge& fromEdge, const FloatPointGraph::Edge& toEdge)
{
auto fromEdgeVector = *fromEdge.second - *fromEdge.first;
auto toEdgeVector = *toEdge.second - *toEdge.first;
if (fromEdgeVector.height() < 0 && toEdgeVector.width() > 0)
return CornerType::TopLeft;
if (fromEdgeVector.width() > 0 && toEdgeVector.height() > 0)
return CornerType::TopRight;
if (fromEdgeVector.height() > 0 && toEdgeVector.width() < 0)
return CornerType::BottomRight;
if (fromEdgeVector.width() < 0 && toEdgeVector.height() < 0)
return CornerType::BottomLeft;
return CornerType::Other;
}
static CornerType cornerTypeForMultiline(const FloatPointGraph::Edge& fromEdge, const FloatPointGraph::Edge& toEdge, const Vector<FloatPoint>& corners)
{
auto corner = cornerType(fromEdge, toEdge);
if (corner == CornerType::TopLeft && corners.at(0) == *fromEdge.second)
return corner;
if (corner == CornerType::TopRight && corners.at(1) == *fromEdge.second)
return corner;
if (corner == CornerType::BottomRight && corners.at(2) == *fromEdge.second)
return corner;
if (corner == CornerType::BottomLeft && corners.at(3) == *fromEdge.second)
return corner;
return CornerType::Other;
}
static std::pair<FloatPoint, FloatPoint> controlPointsForBezierCurve(CornerType cornerType, const FloatPointGraph::Edge& fromEdge, const FloatPointGraph::Edge& toEdge, const FloatSize& radius)
{
FloatPoint cp1;
FloatPoint cp2;
switch (cornerType) {
case CornerType::TopLeft: {
cp1 = FloatPoint(fromEdge.second->x(), fromEdge.second->y() + radius.height() * Path::circleControlPoint());
cp2 = FloatPoint(toEdge.first->x() + radius.width() * Path::circleControlPoint(), toEdge.first->y());
break;
}
case CornerType::TopRight: {
cp1 = FloatPoint(fromEdge.second->x() - radius.width() * Path::circleControlPoint(), fromEdge.second->y());
cp2 = FloatPoint(toEdge.first->x(), toEdge.first->y() + radius.height() * Path::circleControlPoint());
break;
}
case CornerType::BottomRight: {
cp1 = FloatPoint(fromEdge.second->x(), fromEdge.second->y() - radius.height() * Path::circleControlPoint());
cp2 = FloatPoint(toEdge.first->x() - radius.width() * Path::circleControlPoint(), toEdge.first->y());
break;
}
case CornerType::BottomLeft: {
cp1 = FloatPoint(fromEdge.second->x() + radius.width() * Path::circleControlPoint(), fromEdge.second->y());
cp2 = FloatPoint(toEdge.first->x(), toEdge.first->y() - radius.height() * Path::circleControlPoint());
break;
}
case CornerType::Other: {
ASSERT_NOT_REACHED();
break;
}
}
return std::make_pair(cp1, cp2);
}
static CornerRadii adjustedRadiiForHuggingCurve(const CornerRadii& inputRadii, float outlineOffset)
{
// This adjusts the radius so that it follows the border curve even when offset is present.
auto adjustedRadius = [outlineOffset](const FloatSize& radius) {
FloatSize expandSize;
if (radius.width() > outlineOffset)
expandSize.setWidth(std::min(outlineOffset, radius.width() - outlineOffset));
if (radius.height() > outlineOffset)
expandSize.setHeight(std::min(outlineOffset, radius.height() - outlineOffset));
FloatSize adjustedRadius = radius;
adjustedRadius.expand(expandSize.width(), expandSize.height());
// Do not go to negative radius.
return adjustedRadius.expandedTo(FloatSize(0, 0));
};
return {
adjustedRadius(inputRadii.topLeft()),
adjustedRadius(inputRadii.topRight()),
adjustedRadius(inputRadii.bottomLeft()),
adjustedRadius(inputRadii.bottomRight()),
};
}
static std::optional<FloatRect> rectFromPolygon(const FloatPointGraph::Polygon& poly)
{
if (poly.size() != 4)
return std::optional<FloatRect>();
std::optional<FloatPoint> topLeft;
std::optional<FloatPoint> bottomRight;
for (unsigned i = 0; i < poly.size(); ++i) {
const auto& toEdge = poly[i];
const auto& fromEdge = (i > 0) ? poly[i - 1] : poly[poly.size() - 1];
auto corner = cornerType(fromEdge, toEdge);
if (corner == CornerType::TopLeft) {
ASSERT(!topLeft);
topLeft = *fromEdge.second;
} else if (corner == CornerType::BottomRight) {
ASSERT(!bottomRight);
bottomRight = *fromEdge.second;
}
}
if (!topLeft || !bottomRight)
return std::optional<FloatRect>();
return FloatRect(topLeft.value(), bottomRight.value());
}
Path OutlinePainter::pathWithShrinkWrappedRects(const Vector<FloatRect>& rects, const Style::BorderRadius& radii, float outlineOffset, WritingMode writingMode, float deviceScaleFactor)
{
auto roundedRect = [radii, outlineOffset, deviceScaleFactor](const FloatRect& rect) {
auto adjustedRadii = adjustedRadiiForHuggingCurve(Style::evaluate<CornerRadii>(radii, rect.size(), Style::ZoomNeeded { }), outlineOffset);
adjustedRadii.scale(calcBorderRadiiConstraintScaleFor(rect, adjustedRadii));
LayoutRoundedRect roundedRect(
LayoutRect(rect),
LayoutRoundedRect::Radii(
LayoutSize(adjustedRadii.topLeft()),
LayoutSize(adjustedRadii.topRight()),
LayoutSize(adjustedRadii.bottomLeft()),
LayoutSize(adjustedRadii.bottomRight())
)
);
Path path;
path.addRoundedRect(roundedRect.pixelSnappedRoundedRectForPainting(deviceScaleFactor));
return path;
};
if (rects.size() == 1)
return roundedRect(rects.at(0));
auto [graph, polys] = FloatPointGraph::polygonsForRect(rects);
// Fall back to corner painting with no radius for empty and disjoint rectangles.
if (!polys.size() || polys.size() > 1)
return Path();
const auto& poly = polys.at(0);
// Fast path when poly has one rect only.
std::optional<FloatRect> rect = rectFromPolygon(poly);
if (rect)
return roundedRect(rect.value());
Path path;
// Multiline outline needs to match multiline border painting. Only first and last lines are getting rounded borders.
auto isLeftToRight = writingMode.isBidiLTR();
auto firstLineRect = isLeftToRight ? rects.at(0) : rects.at(rects.size() - 1);
auto lastLineRect = isLeftToRight ? rects.at(rects.size() - 1) : rects.at(0);
// Adjust radius so that it matches the box border.
auto firstLineRadii = Style::evaluate<CornerRadii>(radii, firstLineRect.size(), Style::ZoomNeeded { });
auto lastLineRadii = Style::evaluate<CornerRadii>(radii, lastLineRect.size(), Style::ZoomNeeded { });
firstLineRadii.scale(calcBorderRadiiConstraintScaleFor(firstLineRect, firstLineRadii));
lastLineRadii.scale(calcBorderRadiiConstraintScaleFor(lastLineRect, lastLineRadii));
// physical topLeft/topRight/bottomRight/bottomLeft
auto isHorizontal = writingMode.isHorizontal();
auto corners = Vector<FloatPoint>::from(
firstLineRect.minXMinYCorner(),
isHorizontal ? lastLineRect.maxXMinYCorner() : firstLineRect.maxXMinYCorner(),
lastLineRect.maxXMaxYCorner(),
isHorizontal ? firstLineRect.minXMaxYCorner() : lastLineRect.minXMaxYCorner()
);
for (unsigned i = 0; i < poly.size(); ++i) {
auto moveOrAddLineTo = [i, &path](const FloatPoint& startPoint) {
if (!i)
path.moveTo(startPoint);
else
path.addLineTo(startPoint);
};
const auto& toEdge = poly[i];
const auto& fromEdge = (i > 0) ? poly[i - 1] : poly[poly.size() - 1];
FloatSize radius;
auto corner = cornerTypeForMultiline(fromEdge, toEdge, corners);
switch (corner) {
case CornerType::TopLeft:
radius = firstLineRadii.topLeft();
break;
case CornerType::TopRight:
radius = lastLineRadii.topRight();
break;
case CornerType::BottomRight:
radius = lastLineRadii.bottomRight();
break;
case CornerType::BottomLeft:
radius = firstLineRadii.bottomLeft();
break;
case CornerType::Other:
// Do not apply border radius on corners that normal border painting skips. (multiline content)
moveOrAddLineTo(*fromEdge.second);
continue;
}
auto [startPoint, endPoint] = startAndEndPointsForCorner(fromEdge, toEdge, radius);
moveOrAddLineTo(startPoint);
auto [cp1, cp2] = controlPointsForBezierCurve(corner, fromEdge, toEdge, radius);
path.addBezierCurveTo(cp1, cp2, endPoint);
}
path.closeSubpath();
return path;
}
void OutlinePainter::addPDFURLAnnotationForLink(const RenderElement& renderer, const LayoutPoint& paintOffset) const
{
Ref element = *renderer.element();
ASSERT(element->isLink());
CheckedPtr paintContainer = m_paintInfo.paintContainer;
auto focusRingRects = collectFocusRingRects(renderer, paintOffset, paintContainer.get());
LayoutRect urlRect = unionRect(focusRingRects);
if (urlRect.isEmpty())
return;
auto href = element->getAttribute(HTMLNames::hrefAttr);
if (href.isNull())
return;
if (m_paintInfo.context().supportsInternalLinks()) {
String outAnchorName;
RefPtr linkTarget = element->findAnchorElementForLink(outAnchorName);
if (linkTarget) {
m_paintInfo.context().setDestinationForRect(outAnchorName, urlRect);
return;
}
}
m_paintInfo.context().setURLForRect(element->protectedDocument()->completeURL(href), urlRect);
}
} // namespace WebCore
|