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
|
/*
* Copyright (c) 2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "SVGTextBoxPainter.h"
#include "GraphicsContext.h"
#include "GraphicsContextStateSaver.h"
#include "LegacyRenderSVGResourceSolidColor.h"
#include "RenderInline.h"
#include "RenderSVGInlineText.h"
#include "RenderSVGText.h"
#include "SVGInlineTextBox.h"
#include "SVGPaintServerHandling.h"
#include "SVGRenderStyle.h"
#include "SVGResourcesCache.h"
#include "SVGTextFragment.h"
#include "TextPainter.h"
namespace WebCore {
LegacySVGTextBoxPainter::LegacySVGTextBoxPainter(const SVGInlineTextBox& textBox, PaintInfo& paintInfo, const LayoutPoint& paintOffset)
: SVGTextBoxPainter(InlineIterator::BoxLegacyPath { &textBox }, paintInfo, paintOffset)
{
}
ModernSVGTextBoxPainter::ModernSVGTextBoxPainter(const LayoutIntegration::InlineContent& inlineContent, size_t boxIndex, PaintInfo& paintInfo, const LayoutPoint& paintOffset)
: SVGTextBoxPainter(InlineIterator::BoxModernPath { inlineContent, boxIndex }, paintInfo, paintOffset)
{
}
template<typename TextBoxPath>
SVGTextBoxPainter<TextBoxPath>::SVGTextBoxPainter(TextBoxPath&& textBox, PaintInfo& paintInfo, const LayoutPoint& paintOffset)
: m_textBox(WTFMove(textBox))
, m_renderer(downcast<RenderSVGInlineText>(m_textBox.renderer()))
, m_paintInfo(paintInfo)
, m_selectableRange(m_textBox.selectableRange())
, m_paintOffset(paintOffset)
, m_haveSelection(!m_renderer.document().printing() && m_renderer.view().selection().highlightStateForTextBox(m_renderer, m_selectableRange) != RenderObject::HighlightState::None)
{
}
template<typename TextBoxPath>
InlineIterator::SVGTextBoxIterator SVGTextBoxPainter<TextBoxPath>::textBoxIterator() const
{
return { m_textBox };
}
template<typename TextBoxPath>
const RenderBoxModelObject& SVGTextBoxPainter<TextBoxPath>::parentRenderer() const
{
return textBoxIterator()->parentInlineBox()->renderer();
}
FloatRect selectionRectForTextFragment(const RenderSVGInlineText& renderer, TextDirection direction, const SVGTextFragment& fragment, unsigned startPosition, unsigned endPosition, const RenderStyle& style)
{
ASSERT(startPosition < endPosition);
float scalingFactor = renderer.scalingFactor();
ASSERT(scalingFactor);
const FontCascade& scaledFont = renderer.scaledFont();
const FontMetrics& scaledFontMetrics = scaledFont.metricsOfPrimaryFont();
FloatPoint textOrigin(fragment.x, fragment.y);
if (scalingFactor != 1)
textOrigin.scale(scalingFactor);
textOrigin.move(0, -scaledFontMetrics.ascent());
LayoutRect selectionRect { textOrigin, LayoutSize(0, LayoutUnit(fragment.height * scalingFactor)) };
TextRun run = constructTextRun(renderer.text(), direction, style, fragment);
scaledFont.adjustSelectionRectForText(renderer.canUseSimplifiedTextMeasuring().value_or(false), run, selectionRect, startPosition, endPosition);
FloatRect snappedSelectionRect = snapRectToDevicePixelsWithWritingDirection(selectionRect, renderer.document().deviceScaleFactor(), run.ltr());
if (scalingFactor == 1)
return snappedSelectionRect;
snappedSelectionRect.scale(1 / scalingFactor);
return snappedSelectionRect;
}
static inline bool textShouldBePainted(const RenderSVGInlineText& textRenderer)
{
return textRenderer.scaledFont().size() >= 0.5;
}
template<typename TextBoxPath>
void SVGTextBoxPainter<TextBoxPath>::paintSelectionBackground()
{
ASSERT(m_paintInfo.shouldPaintWithinRoot(renderer()));
ASSERT(m_paintInfo.phase == PaintPhase::Foreground || m_paintInfo.phase == PaintPhase::Selection);
if (renderer().style().usedVisibility() != Visibility::Visible)
return;
auto& parentRenderer = this->parentRenderer();
ASSERT(!parentRenderer.document().printing());
// Determine whether or not we're selected.
bool paintSelectedTextOnly = m_paintInfo.phase == PaintPhase::Selection;
if (!m_haveSelection || paintSelectedTextOnly)
return;
Color backgroundColor = renderer().selectionBackgroundColor();
if (!backgroundColor.isVisible())
return;
if (!textShouldBePainted(renderer()))
return;
auto& style = parentRenderer.style();
auto [startPosition, endPosition] = selectionStartEnd();
unsigned fragmentStartPosition = 0;
unsigned fragmentEndPosition = 0;
AffineTransform fragmentTransform;
for (auto& fragment : textBoxIterator()->textFragments()) {
ASSERT(!m_legacyPaintingResource);
fragmentStartPosition = startPosition;
fragmentEndPosition = endPosition;
if (!mapStartEndPositionsIntoFragmentCoordinates(m_textBox.start(), fragment, fragmentStartPosition, fragmentEndPosition))
continue;
GraphicsContextStateSaver stateSaver(m_paintInfo.context());
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
m_paintInfo.context().concatCTM(fragmentTransform);
m_paintInfo.context().setFillColor(backgroundColor);
m_paintInfo.context().fillRect(selectionRectForTextFragment(renderer(), m_textBox.direction(), fragment, fragmentStartPosition, fragmentEndPosition, style), backgroundColor);
m_paintingResourceMode = { };
}
ASSERT(!m_legacyPaintingResource);
}
template<typename TextBoxPath>
void SVGTextBoxPainter<TextBoxPath>::paint()
{
ASSERT(m_paintInfo.shouldPaintWithinRoot(renderer()));
ASSERT(m_paintInfo.phase == PaintPhase::Foreground || m_paintInfo.phase == PaintPhase::Selection);
if (renderer().style().usedVisibility() != Visibility::Visible)
return;
// Note: We're explicitly not supporting composition & custom underlines and custom highlighters - unlike LegacyInlineTextBox.
// If we ever need that for SVG, it's very easy to refactor and reuse the code.
auto& parentRenderer = this->parentRenderer();
bool paintSelectedTextOnly = m_paintInfo.phase == PaintPhase::Selection;
bool shouldPaintSelectionHighlight = !(m_paintInfo.paintBehavior.contains(PaintBehavior::SkipSelectionHighlight));
bool hasSelection = !parentRenderer.document().printing() && m_haveSelection;
if (!hasSelection && paintSelectedTextOnly)
return;
if (!textShouldBePainted(renderer()))
return;
auto& style = parentRenderer.style();
const SVGRenderStyle& svgStyle = style.svgStyle();
bool hasFill = svgStyle.hasFill();
bool hasVisibleStroke = style.hasVisibleStroke();
const RenderStyle* selectionStyle = &style;
if (hasSelection && shouldPaintSelectionHighlight) {
selectionStyle = parentRenderer.getCachedPseudoStyle({ PseudoId::Selection });
if (selectionStyle) {
if (!hasFill)
hasFill = selectionStyle->svgStyle().hasFill();
if (!hasVisibleStroke)
hasVisibleStroke = selectionStyle->hasVisibleStroke();
} else
selectionStyle = &style;
}
if (renderer().view().frameView().paintBehavior().contains(PaintBehavior::RenderingSVGClipOrMask)) {
hasFill = true;
hasVisibleStroke = false;
}
AffineTransform fragmentTransform;
for (auto& fragment : textBoxIterator()->textFragments()) {
ASSERT(!m_legacyPaintingResource);
GraphicsContextStateSaver stateSaver(m_paintInfo.context());
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
m_paintInfo.context().concatCTM(fragmentTransform);
// Spec: All text decorations except line-through should be drawn before the text is filled and stroked; thus, the text is rendered on top of these decorations.
auto decorations = style.textDecorationsInEffect();
if (decorations & TextDecorationLine::Underline)
paintDecoration(TextDecorationLine::Underline, fragment);
if (decorations & TextDecorationLine::Overline)
paintDecoration(TextDecorationLine::Overline, fragment);
for (auto type : RenderStyle::paintTypesForPaintOrder(style.paintOrder())) {
switch (type) {
case PaintType::Fill:
if (!hasFill)
continue;
m_paintingResourceMode = { RenderSVGResourceMode::ApplyToFill, RenderSVGResourceMode::ApplyToText };
ASSERT(selectionStyle);
paintText(style, *selectionStyle, fragment, hasSelection, paintSelectedTextOnly);
break;
case PaintType::Stroke:
if (!hasVisibleStroke)
continue;
m_paintingResourceMode = { RenderSVGResourceMode::ApplyToStroke, RenderSVGResourceMode::ApplyToText };
ASSERT(selectionStyle);
paintText(style, *selectionStyle, fragment, hasSelection, paintSelectedTextOnly);
break;
case PaintType::Markers:
continue;
}
}
// Spec: Line-through should be drawn after the text is filled and stroked; thus, the line-through is rendered on top of the text.
if (decorations & TextDecorationLine::LineThrough)
paintDecoration(TextDecorationLine::LineThrough, fragment);
m_paintingResourceMode = { };
}
// Finally, paint the outline if any.
if (renderer().style().hasOutline()) {
if (CheckedPtr renderInline = dynamicDowncast<RenderInline>(parentRenderer))
renderInline->paintOutline(m_paintInfo, m_paintOffset);
}
ASSERT(!m_legacyPaintingResource);
}
template<typename TextBoxPath>
bool SVGTextBoxPainter<TextBoxPath>::acquirePaintingResource(SVGPaintServerHandling& paintServerHandling, float scalingFactor, const RenderBoxModelObject& renderer, const RenderStyle& style)
{
ASSERT(scalingFactor);
ASSERT(!paintingResourceMode().isEmpty());
ASSERT(renderer.document().settings().layerBasedSVGEngineEnabled());
auto& context = paintServerHandling.context();
if (paintingResourceMode().contains(RenderSVGResourceMode::ApplyToFill)) {
if (!paintServerHandling.preparePaintOperation<SVGPaintServerHandling::Operation::Fill>(parentRenderer(), style))
return false;
context.save();
context.setTextDrawingMode(TextDrawingMode::Fill);
} else if (paintingResourceMode().contains(RenderSVGResourceMode::ApplyToStroke)) {
if (!paintServerHandling.preparePaintOperation<SVGPaintServerHandling::Operation::Stroke>(parentRenderer(), style))
return false;
context.save();
context.setTextDrawingMode(TextDrawingMode::Stroke);
if (style.svgStyle().vectorEffect() == VectorEffect::NonScalingStroke) {
if (style.fontDescription().textRenderingMode() == TextRenderingMode::GeometricPrecision)
scalingFactor = 1.0 / RenderSVGInlineText::computeScalingFactorForRenderer(renderer);
else
scalingFactor = 1.0;
if (auto zoomFactor = renderer.style().usedZoom(); zoomFactor != 1.0)
scalingFactor *= zoomFactor;
if (auto deviceScaleFactor = renderer.document().deviceScaleFactor(); deviceScaleFactor != 1.0)
scalingFactor *= deviceScaleFactor;
}
if (scalingFactor != 1.0)
context.setStrokeThickness(context.strokeThickness() * scalingFactor);
}
if (context.fillGradient() || context.strokeGradient() || context.fillPattern() || context.strokePattern()) {
context.beginTransparencyLayer(1);
context.setCompositeOperation(CompositeOperator::SourceOver);
}
return true;
}
template<typename TextBoxPath>
void SVGTextBoxPainter<TextBoxPath>::releasePaintingResource(SVGPaintServerHandling& paintServerHandling)
{
auto& context = paintServerHandling.context();
if (context.fillGradient() || context.strokeGradient() || context.fillPattern() || context.strokePattern())
context.endTransparencyLayer();
context.restore();
}
template<typename TextBoxPath>
bool SVGTextBoxPainter<TextBoxPath>::acquireLegacyPaintingResource(GraphicsContext*& context, float scalingFactor, RenderBoxModelObject& renderer, const RenderStyle& style)
{
ASSERT(scalingFactor);
ASSERT(paintingResourceMode().containsAny({ RenderSVGResourceMode::ApplyToFill, RenderSVGResourceMode::ApplyToStroke }));
Color fallbackColor;
if (paintingResourceMode().contains(RenderSVGResourceMode::ApplyToFill))
m_legacyPaintingResource = LegacyRenderSVGResource::fillPaintingResource(renderer, style, fallbackColor);
else if (paintingResourceMode().contains(RenderSVGResourceMode::ApplyToStroke))
m_legacyPaintingResource = LegacyRenderSVGResource::strokePaintingResource(renderer, style, fallbackColor);
if (!m_legacyPaintingResource)
return false;
if (!resourceWasApplied(m_legacyPaintingResource->applyResource(renderer, style, context, paintingResourceMode()))) {
if (!fallbackColor.isValid()) {
m_legacyPaintingResource = nullptr;
return false;
}
auto* fallbackResource = LegacyRenderSVGResource::sharedSolidPaintingResource();
fallbackResource->setColor(fallbackColor);
m_legacyPaintingResource = fallbackResource;
if (!resourceWasApplied(m_legacyPaintingResource->applyResource(renderer, style, context, paintingResourceMode()))) {
m_legacyPaintingResource = nullptr;
return false;
}
}
if (paintingResourceMode().contains(RenderSVGResourceMode::ApplyToStroke)) {
if (style.svgStyle().vectorEffect() == VectorEffect::NonScalingStroke) {
if (style.fontDescription().textRenderingMode() == TextRenderingMode::GeometricPrecision)
scalingFactor = 1.0 / RenderSVGInlineText::computeScalingFactorForRenderer(renderer);
else
scalingFactor = 1.0;
if (auto zoomFactor = renderer.style().usedZoom(); zoomFactor != 1.0)
scalingFactor *= zoomFactor;
if (auto deviceScaleFactor = renderer.document().deviceScaleFactor(); deviceScaleFactor != 1.0)
scalingFactor *= deviceScaleFactor;
}
if (scalingFactor != 1.0)
context->setStrokeThickness(context->strokeThickness() * scalingFactor);
}
return true;
}
template<typename TextBoxPath>
void SVGTextBoxPainter<TextBoxPath>::releaseLegacyPaintingResource(GraphicsContext*& context, const Path* path)
{
ASSERT(m_legacyPaintingResource);
m_legacyPaintingResource->postApplyResource(const_cast<RenderBoxModelObject&>(parentRenderer()), context, paintingResourceMode(), path, /*LegacyRenderSVGShape*/ nullptr);
m_legacyPaintingResource = nullptr;
}
bool mapStartEndPositionsIntoFragmentCoordinates(unsigned textBoxStart, const SVGTextFragment& fragment, unsigned& startPosition, unsigned& endPosition)
{
unsigned startFragment = fragment.characterOffset - textBoxStart;
unsigned endFragment = startFragment + fragment.length;
// Find intersection between the intervals: [startFragment..endFragment) and [startPosition..endPosition)
startPosition = std::max(startFragment, startPosition);
endPosition = std::min(endFragment, endPosition);
if (startPosition >= endPosition)
return false;
startPosition -= startFragment;
endPosition -= startFragment;
return true;
}
static inline float positionOffsetForDecoration(OptionSet<TextDecorationLine> decoration, const FontMetrics& fontMetrics, float thickness)
{
// FIXME: For SVG Fonts we need to use the attributes defined in the <font-face> if specified.
// Compatible with Batik/Opera.
const float ascent = fontMetrics.ascent();
if (decoration == TextDecorationLine::Underline)
return ascent + thickness * 1.5f;
if (decoration == TextDecorationLine::Overline)
return thickness;
if (decoration == TextDecorationLine::LineThrough)
return ascent * 5 / 8.0f;
ASSERT_NOT_REACHED();
return 0.0f;
}
static inline float thicknessForDecoration(OptionSet<TextDecorationLine>, const FontCascade& font)
{
// FIXME: For SVG Fonts we need to use the attributes defined in the <font-face> if specified.
// Compatible with Batik/Opera
return font.size() / 20.0f;
}
template<typename TextBoxPath>
void SVGTextBoxPainter<TextBoxPath>::paintDecoration(OptionSet<TextDecorationLine> decoration, const SVGTextFragment& fragment)
{
if (renderer().style().textDecorationsInEffect().isEmpty())
return;
// Find out which render style defined the text-decoration, as its fill/stroke properties have to be used for drawing instead of ours.
auto decorationRenderer = [&] {
auto parentBox = textBoxIterator()->parentInlineBox();
// Lookup first render object in parent hierarchy which has text-decoration set.
CheckedPtr<const RenderBoxModelObject> renderer = nullptr;
while (parentBox) {
renderer = &parentBox->renderer();
if (!renderer->style().textDecorationLine().isEmpty())
break;
parentBox = parentBox->parentInlineBox();
}
return renderer;
}();
ASSERT(decorationRenderer);
const RenderStyle& decorationStyle = decorationRenderer->style();
if (decorationStyle.usedVisibility() == Visibility::Hidden)
return;
const SVGRenderStyle& svgDecorationStyle = decorationStyle.svgStyle();
for (auto type : RenderStyle::paintTypesForPaintOrder(renderer().style().paintOrder())) {
switch (type) {
case PaintType::Fill:
if (svgDecorationStyle.hasFill()) {
m_paintingResourceMode = RenderSVGResourceMode::ApplyToFill;
paintDecorationWithStyle(decoration, fragment, *decorationRenderer);
}
break;
case PaintType::Stroke:
if (decorationStyle.hasVisibleStroke()) {
m_paintingResourceMode = RenderSVGResourceMode::ApplyToStroke;
paintDecorationWithStyle(decoration, fragment, *decorationRenderer);
}
break;
case PaintType::Markers:
break;
default:
ASSERT_NOT_REACHED();
}
}
}
template<typename TextBoxPath>
void SVGTextBoxPainter<TextBoxPath>::paintDecorationWithStyle(OptionSet<TextDecorationLine> decoration, const SVGTextFragment& fragment, const RenderBoxModelObject& decorationRenderer)
{
ASSERT(!m_legacyPaintingResource);
ASSERT(!paintingResourceMode().isEmpty());
auto& context = m_paintInfo.context();
auto& decorationStyle = decorationRenderer.style();
float scalingFactor = 1;
FontCascade scaledFont;
RenderSVGInlineText::computeNewScaledFontForStyle(decorationRenderer, decorationStyle, scalingFactor, scaledFont);
ASSERT(scalingFactor);
// The initial y value refers to overline position.
float thickness = thicknessForDecoration(decoration, scaledFont);
if (fragment.width <= 0 && thickness <= 0)
return;
FloatPoint decorationOrigin(fragment.x, fragment.y);
float width = fragment.width;
const FontMetrics& scaledFontMetrics = scaledFont.metricsOfPrimaryFont();
GraphicsContextStateSaver stateSaver(context);
if (scalingFactor != 1) {
width *= scalingFactor;
decorationOrigin.scale(scalingFactor);
context.scale(1 / scalingFactor);
}
decorationOrigin.move(0, -scaledFontMetrics.ascent() + positionOffsetForDecoration(decoration, scaledFontMetrics, thickness));
Path path;
path.addRect(FloatRect(decorationOrigin, FloatSize(width, thickness)));
if (decorationRenderer.document().settings().layerBasedSVGEngineEnabled()) {
SVGPaintServerHandling paintServerHandling { context };
if (acquirePaintingResource(paintServerHandling, scalingFactor, decorationRenderer, decorationStyle)) {
if (paintingResourceMode().contains(RenderSVGResourceMode::ApplyToFill))
context.fillPath(path);
else if (paintingResourceMode().contains(RenderSVGResourceMode::ApplyToStroke))
context.strokePath(path);
releasePaintingResource(paintServerHandling);
}
return;
}
GraphicsContext* usedContext = &context;
if (acquireLegacyPaintingResource(usedContext, scalingFactor, const_cast<RenderBoxModelObject&>(decorationRenderer), decorationStyle))
releaseLegacyPaintingResource(usedContext, &path);
}
template<typename TextBoxPath>
void SVGTextBoxPainter<TextBoxPath>::paintTextWithShadows(const RenderStyle& style, TextRun& textRun, const SVGTextFragment& fragment, unsigned startPosition, unsigned endPosition)
{
auto& context = m_paintInfo.context();
float scalingFactor = renderer().scalingFactor();
ASSERT(scalingFactor);
const FontCascade& scaledFont = renderer().scaledFont();
const ShadowData* shadow = style.textShadow();
FloatPoint textOrigin(fragment.x, fragment.y);
FloatSize textSize(fragment.width, fragment.height);
if (scalingFactor != 1) {
textOrigin.scale(scalingFactor);
textSize.scale(scalingFactor);
}
FloatRect shadowRect(FloatPoint(textOrigin.x(), textOrigin.y() - scaledFont.metricsOfPrimaryFont().ascent()), textSize);
GraphicsContext* usedContext = &context;
SVGPaintServerHandling paintServerHandling { context };
auto prepareGraphicsContext = [&]() -> bool {
if (renderer().document().settings().layerBasedSVGEngineEnabled())
return acquirePaintingResource(paintServerHandling, scalingFactor, parentRenderer(), style);
return acquireLegacyPaintingResource(usedContext, scalingFactor, const_cast<RenderBoxModelObject&>(parentRenderer()), style);
};
auto restoreGraphicsContext = [&]() {
if (renderer().document().settings().layerBasedSVGEngineEnabled()) {
releasePaintingResource(paintServerHandling);
return;
}
releaseLegacyPaintingResource(usedContext, /* path */nullptr);
};
do {
if (!prepareGraphicsContext())
break;
{
// Optimized code path to support gradient/pattern fill/stroke on text without using temporary ImageBuffers / masking.
RefPtr<Gradient> gradient;
RefPtr<Pattern> pattern;
if (renderer().document().settings().layerBasedSVGEngineEnabled()) {
auto* textRootBlock = RenderSVGText::locateRenderSVGTextAncestor(renderer());
ASSERT(textRootBlock);
AffineTransform gradientSpaceTransform;
if (paintingResourceMode().contains(RenderSVGResourceMode::ApplyToFill)) {
gradient = usedContext->fillGradient();
gradientSpaceTransform = usedContext->fillGradientSpaceTransform();
pattern = usedContext->fillPattern();
} else {
gradient = usedContext->strokeGradient();
gradientSpaceTransform = usedContext->strokeGradientSpaceTransform();
pattern = usedContext->strokePattern();
}
ASSERT(!(gradient && pattern));
if (gradient || pattern) {
if (gradient)
usedContext->fillRect(textRootBlock->repaintRectInLocalCoordinates(), *gradient, gradientSpaceTransform);
else {
// FIXME: should be fillRect(const FloatRect&, Pattern&) on GraphicsContext.
GraphicsContextStateSaver stateSaver(*usedContext);
if (usedContext->strokePattern())
usedContext->setFillPattern(*usedContext->strokePattern());
usedContext->fillRect(textRootBlock->repaintRectInLocalCoordinates());
}
usedContext->setCompositeOperation(CompositeOperator::DestinationIn);
usedContext->beginTransparencyLayer(1);
}
}
ShadowApplier shadowApplier(style, *usedContext, shadow, nullptr, shadowRect);
if (!shadowApplier.didSaveContext())
usedContext->save();
usedContext->scale(1 / scalingFactor);
scaledFont.drawText(*usedContext, textRun, textOrigin + shadowApplier.extraOffset(), startPosition, endPosition);
if (!shadowApplier.didSaveContext())
usedContext->restore();
if (gradient || pattern)
usedContext->endTransparencyLayer();
}
restoreGraphicsContext();
if (!shadow)
break;
shadow = shadow->next();
} while (shadow);
}
template<typename TextBoxPath>
void SVGTextBoxPainter<TextBoxPath>::paintText(const RenderStyle& style, const RenderStyle& selectionStyle, const SVGTextFragment& fragment, bool hasSelection, bool paintSelectedTextOnly)
{
unsigned startPosition = 0;
unsigned endPosition = 0;
if (hasSelection) {
std::tie(startPosition, endPosition) = selectionStartEnd();
hasSelection = mapStartEndPositionsIntoFragmentCoordinates(m_textBox.start(), fragment, startPosition, endPosition);
}
// Fast path if there is no selection, just draw the whole chunk part using the regular style
TextRun textRun = constructTextRun(renderer().text(), m_textBox.direction(), style, fragment);
if (!hasSelection || startPosition >= endPosition) {
paintTextWithShadows(style, textRun, fragment, 0, fragment.length);
return;
}
// Eventually draw text using regular style until the start position of the selection
if (startPosition > 0 && !paintSelectedTextOnly)
paintTextWithShadows(style, textRun, fragment, 0, startPosition);
// Draw text using selection style from the start to the end position of the selection
{
SVGResourcesCache::SetStyleForScope temporaryStyleChange(const_cast<RenderBoxModelObject&>(parentRenderer()), style, selectionStyle);
paintTextWithShadows(selectionStyle, textRun, fragment, startPosition, endPosition);
}
// Eventually draw text using regular style from the end position of the selection to the end of the current chunk part
if (endPosition < fragment.length && !paintSelectedTextOnly)
paintTextWithShadows(style, textRun, fragment, endPosition, fragment.length);
}
TextRun constructTextRun(StringView text, TextDirection direction, const RenderStyle& style, const SVGTextFragment& fragment)
{
TextRun run(text.substring(fragment.characterOffset, fragment.length),
0, /* xPos, only relevant with allowTabs=true */
0, /* padding, only relevant for justified text, not relevant for SVG */
ExpansionBehavior::allowRightOnly(),
direction,
style.rtlOrdering() == Order::Visual /* directionalOverride */);
// We handle letter & word spacing ourselves.
run.disableSpacing();
return run;
}
template<typename TextBoxPath>
std::pair<unsigned, unsigned> SVGTextBoxPainter<TextBoxPath>::selectionStartEnd() const
{
return m_renderer.view().selection().rangeForTextBox(m_renderer, m_selectableRange);
}
template class SVGTextBoxPainter<InlineIterator::BoxModernPath>;
template class SVGTextBoxPainter<InlineIterator::BoxLegacyPath>;
}
|