SVGInlineTextBox.cpp [plain text]
#include "config.h"
#include "SVGInlineTextBox.h"
#include "FontCache.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "InlineFlowBox.h"
#include "PointerEventsHitRules.h"
#include "RenderBlock.h"
#include "RenderInline.h"
#include "RenderSVGResourceSolidColor.h"
#include "RenderView.h"
#include "SVGRenderingContext.h"
#include "SVGResourcesCache.h"
#include "SVGRootInlineBox.h"
#include "SVGTextRunRenderingContext.h"
namespace WebCore {
struct ExpectedSVGInlineTextBoxSize : public InlineTextBox {
float float1;
uint32_t bitfields : 5;
void* pointer;
Vector<SVGTextFragment> vector;
};
COMPILE_ASSERT(sizeof(SVGInlineTextBox) == sizeof(ExpectedSVGInlineTextBoxSize), SVGInlineTextBox_is_not_of_expected_size);
SVGInlineTextBox::SVGInlineTextBox(RenderSVGInlineText& renderer)
: InlineTextBox(renderer)
, m_logicalHeight(0)
, m_paintingResourceMode(ApplyToDefaultMode)
, m_startsNewTextChunk(false)
, m_paintingResource(0)
{
}
void SVGInlineTextBox::dirtyOwnLineBoxes()
{
InlineTextBox::dirtyLineBoxes();
clearTextFragments();
}
void SVGInlineTextBox::dirtyLineBoxes()
{
dirtyOwnLineBoxes();
for (InlineTextBox* nextBox = nextTextBox(); nextBox; nextBox = nextBox->nextTextBox())
nextBox->dirtyOwnLineBoxes();
}
int SVGInlineTextBox::offsetForPosition(float, bool) const
{
ASSERT_NOT_REACHED();
return 0;
}
int SVGInlineTextBox::offsetForPositionInFragment(const SVGTextFragment& fragment, float position, bool includePartialGlyphs) const
{
float scalingFactor = renderer().scalingFactor();
ASSERT(scalingFactor);
TextRun textRun = constructTextRun(&renderer().style(), fragment);
AffineTransform fragmentTransform;
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
textRun.setHorizontalGlyphStretch(narrowPrecisionToFloat(fragmentTransform.xScale()));
return fragment.characterOffset - start() + renderer().scaledFont().offsetForPosition(textRun, position * scalingFactor, includePartialGlyphs);
}
float SVGInlineTextBox::positionForOffset(int) const
{
ASSERT_NOT_REACHED();
return 0;
}
FloatRect SVGInlineTextBox::selectionRectForTextFragment(const SVGTextFragment& fragment, int startPosition, int endPosition, RenderStyle* style) const
{
ASSERT_WITH_SECURITY_IMPLICATION(startPosition < endPosition);
ASSERT(style);
FontCachePurgePreventer fontCachePurgePreventer;
float scalingFactor = renderer().scalingFactor();
ASSERT(scalingFactor);
const Font& scaledFont = renderer().scaledFont();
const FontMetrics& scaledFontMetrics = scaledFont.fontMetrics();
FloatPoint textOrigin(fragment.x, fragment.y);
if (scalingFactor != 1)
textOrigin.scale(scalingFactor, scalingFactor);
textOrigin.move(0, -scaledFontMetrics.floatAscent());
LayoutRect selectionRect = LayoutRect(textOrigin, LayoutSize(0, fragment.height * scalingFactor));
TextRun run = constructTextRun(style, fragment);
scaledFont.adjustSelectionRectForText(run, selectionRect, startPosition, endPosition);
FloatRect snappedSelectionRect = directionalPixelSnappedForPainting(selectionRect, renderer().document().deviceScaleFactor(), run.ltr());
if (scalingFactor == 1)
return snappedSelectionRect;
snappedSelectionRect.scale(1 / scalingFactor);
return snappedSelectionRect;
}
LayoutRect SVGInlineTextBox::localSelectionRect(int startPosition, int endPosition) const
{
int boxStart = start();
startPosition = std::max(startPosition - boxStart, 0);
endPosition = std::min(endPosition - boxStart, static_cast<int>(len()));
if (startPosition >= endPosition)
return LayoutRect();
RenderStyle& style = renderer().style();
AffineTransform fragmentTransform;
FloatRect selectionRect;
int fragmentStartPosition = 0;
int fragmentEndPosition = 0;
unsigned textFragmentsSize = m_textFragments.size();
for (unsigned i = 0; i < textFragmentsSize; ++i) {
const SVGTextFragment& fragment = m_textFragments.at(i);
fragmentStartPosition = startPosition;
fragmentEndPosition = endPosition;
if (!mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
continue;
FloatRect fragmentRect = selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, &style);
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
fragmentRect = fragmentTransform.mapRect(fragmentRect);
selectionRect.unite(fragmentRect);
}
return enclosingIntRect(selectionRect);
}
static inline bool textShouldBePainted(const RenderSVGInlineText& textRenderer)
{
return textRenderer.scaledFont().pixelSize();
}
void SVGInlineTextBox::paintSelectionBackground(PaintInfo& paintInfo)
{
ASSERT(paintInfo.shouldPaintWithinRoot(renderer()));
ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
ASSERT(truncation() == cNoTruncation);
if (renderer().style().visibility() != VISIBLE)
return;
RenderObject& parentRenderer = parent()->renderer();
ASSERT(!parentRenderer.document().printing());
bool paintSelectedTextOnly = paintInfo.phase == PaintPhaseSelection;
bool hasSelection = selectionState() != RenderObject::SelectionNone;
if (!hasSelection || paintSelectedTextOnly)
return;
Color backgroundColor = renderer().selectionBackgroundColor();
if (!backgroundColor.isValid() || !backgroundColor.alpha())
return;
if (!textShouldBePainted(renderer()))
return;
RenderStyle& style = parentRenderer.style();
RenderStyle* selectionStyle = &style;
if (hasSelection) {
selectionStyle = parentRenderer.getCachedPseudoStyle(SELECTION);
if (!selectionStyle)
selectionStyle = &style;
}
int startPosition, endPosition;
selectionStartEnd(startPosition, endPosition);
int fragmentStartPosition = 0;
int fragmentEndPosition = 0;
AffineTransform fragmentTransform;
unsigned textFragmentsSize = m_textFragments.size();
for (unsigned i = 0; i < textFragmentsSize; ++i) {
SVGTextFragment& fragment = m_textFragments.at(i);
ASSERT(!m_paintingResource);
fragmentStartPosition = startPosition;
fragmentEndPosition = endPosition;
if (!mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
continue;
GraphicsContextStateSaver stateSaver(*paintInfo.context);
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
paintInfo.context->concatCTM(fragmentTransform);
paintInfo.context->setFillColor(backgroundColor, style.colorSpace());
paintInfo.context->fillRect(selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, &style), backgroundColor, style.colorSpace());
m_paintingResourceMode = ApplyToDefaultMode;
}
ASSERT(!m_paintingResource);
}
void SVGInlineTextBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit, LayoutUnit)
{
ASSERT(paintInfo.shouldPaintWithinRoot(renderer()));
ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
ASSERT(truncation() == cNoTruncation);
if (renderer().style().visibility() != VISIBLE)
return;
RenderObject& parentRenderer = parent()->renderer();
bool paintSelectedTextOnly = paintInfo.phase == PaintPhaseSelection;
bool hasSelection = !parentRenderer.document().printing() && selectionState() != RenderObject::SelectionNone;
if (!hasSelection && paintSelectedTextOnly)
return;
if (!textShouldBePainted(renderer()))
return;
RenderStyle& style = parentRenderer.style();
const SVGRenderStyle& svgStyle = style.svgStyle();
bool hasFill = svgStyle.hasFill();
bool hasVisibleStroke = svgStyle.hasVisibleStroke();
RenderStyle* selectionStyle = &style;
if (hasSelection) {
selectionStyle = parentRenderer.getCachedPseudoStyle(SELECTION);
if (selectionStyle) {
const SVGRenderStyle& svgSelectionStyle = selectionStyle->svgStyle();
if (!hasFill)
hasFill = svgSelectionStyle.hasFill();
if (!hasVisibleStroke)
hasVisibleStroke = svgSelectionStyle.hasVisibleStroke();
} else
selectionStyle = &style;
}
if (renderer().view().frameView().paintBehavior() & PaintBehaviorRenderingSVGMask) {
hasFill = true;
hasVisibleStroke = false;
}
AffineTransform fragmentTransform;
unsigned textFragmentsSize = m_textFragments.size();
for (unsigned i = 0; i < textFragmentsSize; ++i) {
SVGTextFragment& fragment = m_textFragments.at(i);
ASSERT(!m_paintingResource);
GraphicsContextStateSaver stateSaver(*paintInfo.context);
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
paintInfo.context->concatCTM(fragmentTransform);
int decorations = style.textDecorationsInEffect();
if (decorations & TextDecorationUnderline)
paintDecoration(paintInfo.context, TextDecorationUnderline, fragment);
if (decorations & TextDecorationOverline)
paintDecoration(paintInfo.context, TextDecorationOverline, fragment);
Vector<PaintType> paintOrder = style.svgStyle().paintTypesForPaintOrder();
for (unsigned i = 0; i < paintOrder.size(); ++i) {
switch (paintOrder.at(i)) {
case PaintTypeFill:
if (!hasFill)
continue;
m_paintingResourceMode = ApplyToFillMode | ApplyToTextMode;
paintText(paintInfo.context, &style, selectionStyle, fragment, hasSelection, paintSelectedTextOnly);
break;
case PaintTypeStroke:
if (!hasVisibleStroke)
continue;
m_paintingResourceMode = ApplyToStrokeMode | ApplyToTextMode;
paintText(paintInfo.context, &style, selectionStyle, fragment, hasSelection, paintSelectedTextOnly);
break;
case PaintTypeMarkers:
continue;
}
}
if (decorations & TextDecorationLineThrough)
paintDecoration(paintInfo.context, TextDecorationLineThrough, fragment);
m_paintingResourceMode = ApplyToDefaultMode;
}
if (renderer().style().hasOutline() && parentRenderer.isRenderInline())
toRenderInline(parentRenderer).paintOutline(paintInfo, paintOffset);
ASSERT(!m_paintingResource);
}
bool SVGInlineTextBox::acquirePaintingResource(GraphicsContext*& context, float scalingFactor, RenderBoxModelObject& renderer, RenderStyle* style)
{
ASSERT(scalingFactor);
ASSERT(style);
ASSERT(m_paintingResourceMode != ApplyToDefaultMode);
Color fallbackColor;
if (m_paintingResourceMode & ApplyToFillMode)
m_paintingResource = RenderSVGResource::fillPaintingResource(renderer, *style, fallbackColor);
else if (m_paintingResourceMode & ApplyToStrokeMode)
m_paintingResource = RenderSVGResource::strokePaintingResource(renderer, *style, fallbackColor);
else {
ASSERT_NOT_REACHED();
}
if (!m_paintingResource)
return false;
if (!m_paintingResource->applyResource(renderer, *style, context, m_paintingResourceMode)) {
if (fallbackColor.isValid()) {
RenderSVGResourceSolidColor* fallbackResource = RenderSVGResource::sharedSolidPaintingResource();
fallbackResource->setColor(fallbackColor);
m_paintingResource = fallbackResource;
m_paintingResource->applyResource(renderer, *style, context, m_paintingResourceMode);
}
}
if (scalingFactor != 1 && m_paintingResourceMode & ApplyToStrokeMode)
context->setStrokeThickness(context->strokeThickness() * scalingFactor);
return true;
}
void SVGInlineTextBox::releasePaintingResource(GraphicsContext*& context, const Path* path)
{
ASSERT(m_paintingResource);
m_paintingResource->postApplyResource(parent()->renderer(), context, m_paintingResourceMode, path, 0);
m_paintingResource = nullptr;
}
bool SVGInlineTextBox::prepareGraphicsContextForTextPainting(GraphicsContext*& context, float scalingFactor, TextRun& textRun, RenderStyle* style)
{
bool acquiredResource = acquirePaintingResource(context, scalingFactor, parent()->renderer(), style);
if (!acquiredResource)
return false;
#if ENABLE(SVG_FONTS)
TextRun::RenderingContext* renderingContext = textRun.renderingContext();
if (renderingContext)
static_cast<SVGTextRunRenderingContext*>(renderingContext)->setActivePaintingResource(m_paintingResource);
#endif
return true;
}
void SVGInlineTextBox::restoreGraphicsContextAfterTextPainting(GraphicsContext*& context, TextRun& textRun)
{
releasePaintingResource(context, 0);
#if ENABLE(SVG_FONTS)
TextRun::RenderingContext* renderingContext = textRun.renderingContext();
if (renderingContext)
static_cast<SVGTextRunRenderingContext*>(renderingContext)->setActivePaintingResource(0);
#else
UNUSED_PARAM(textRun);
#endif
}
TextRun SVGInlineTextBox::constructTextRun(RenderStyle* style, const SVGTextFragment& fragment) const
{
ASSERT(style);
TextRun run(StringView(renderer().text()).substring(fragment.characterOffset, fragment.length)
, 0
, 0
, TextRun::AllowTrailingExpansion
, direction()
, dirOverride() || style->rtlOrdering() == VisualOrder );
if (style->font().primaryFont()->isSVGFont())
run.setRenderingContext(SVGTextRunRenderingContext::create(renderer()));
run.disableRoundingHacks();
run.disableSpacing();
run.setCharactersLength(renderer().textLength() - fragment.characterOffset);
ASSERT(run.charactersLength() >= run.length());
return run;
}
bool SVGInlineTextBox::mapStartEndPositionsIntoFragmentCoordinates(const SVGTextFragment& fragment, int& startPosition, int& endPosition) const
{
if (startPosition >= endPosition)
return false;
int offset = static_cast<int>(fragment.characterOffset) - start();
int length = static_cast<int>(fragment.length);
if (startPosition >= offset + length || endPosition <= offset)
return false;
if (startPosition < offset)
startPosition = 0;
else
startPosition -= offset;
if (endPosition > offset + length)
endPosition = length;
else {
ASSERT(endPosition >= offset);
endPosition -= offset;
}
ASSERT_WITH_SECURITY_IMPLICATION(startPosition < endPosition);
return true;
}
static inline float positionOffsetForDecoration(TextDecoration decoration, const FontMetrics& fontMetrics, float thickness)
{
if (decoration == TextDecorationUnderline)
return fontMetrics.floatAscent() + thickness * 1.5f;
if (decoration == TextDecorationOverline)
return thickness;
if (decoration == TextDecorationLineThrough)
return fontMetrics.floatAscent() * 5 / 8.0f;
ASSERT_NOT_REACHED();
return 0.0f;
}
static inline float thicknessForDecoration(TextDecoration, const Font& font)
{
return font.size() / 20.0f;
}
static inline RenderBoxModelObject& findRendererDefininingTextDecoration(InlineFlowBox* parentBox)
{
RenderBoxModelObject* renderer = nullptr;
while (parentBox) {
renderer = &parentBox->renderer();
if (renderer->style().textDecoration() != TextDecorationNone)
break;
parentBox = parentBox->parent();
}
ASSERT(renderer);
return *renderer;
}
void SVGInlineTextBox::paintDecoration(GraphicsContext* context, TextDecoration decoration, const SVGTextFragment& fragment)
{
if (renderer().style().textDecorationsInEffect() == TextDecorationNone)
return;
auto& decorationRenderer = findRendererDefininingTextDecoration(parent());
const RenderStyle& decorationStyle = decorationRenderer.style();
if (decorationStyle.visibility() == HIDDEN)
return;
const SVGRenderStyle& svgDecorationStyle = decorationStyle.svgStyle();
bool hasDecorationFill = svgDecorationStyle.hasFill();
bool hasVisibleDecorationStroke = svgDecorationStyle.hasVisibleStroke();
if (hasDecorationFill) {
m_paintingResourceMode = ApplyToFillMode;
paintDecorationWithStyle(context, decoration, fragment, decorationRenderer);
}
if (hasVisibleDecorationStroke) {
m_paintingResourceMode = ApplyToStrokeMode;
paintDecorationWithStyle(context, decoration, fragment, decorationRenderer);
}
}
void SVGInlineTextBox::paintDecorationWithStyle(GraphicsContext* context, TextDecoration decoration, const SVGTextFragment& fragment, RenderBoxModelObject& decorationRenderer)
{
ASSERT(!m_paintingResource);
ASSERT(m_paintingResourceMode != ApplyToDefaultMode);
RenderStyle& decorationStyle = decorationRenderer.style();
float scalingFactor = 1;
Font scaledFont;
RenderSVGInlineText::computeNewScaledFontForStyle(decorationRenderer, decorationStyle, scalingFactor, scaledFont);
ASSERT(scalingFactor);
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.fontMetrics();
GraphicsContextStateSaver stateSaver(*context);
if (scalingFactor != 1) {
width *= scalingFactor;
decorationOrigin.scale(scalingFactor, scalingFactor);
context->scale(FloatSize(1 / scalingFactor, 1 / scalingFactor));
}
decorationOrigin.move(0, -scaledFontMetrics.floatAscent() + positionOffsetForDecoration(decoration, scaledFontMetrics, thickness));
Path path;
path.addRect(FloatRect(decorationOrigin, FloatSize(width, thickness)));
if (acquirePaintingResource(context, scalingFactor, decorationRenderer, &decorationStyle))
releasePaintingResource(context, &path);
}
void SVGInlineTextBox::paintTextWithShadows(GraphicsContext* context, RenderStyle* style, TextRun& textRun, const SVGTextFragment& fragment, int startPosition, int endPosition)
{
float scalingFactor = renderer().scalingFactor();
ASSERT(scalingFactor);
const Font& 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, scalingFactor);
textSize.scale(scalingFactor);
}
FloatRect shadowRect(FloatPoint(textOrigin.x(), textOrigin.y() - scaledFont.fontMetrics().floatAscent()), textSize);
do {
if (!prepareGraphicsContextForTextPainting(context, scalingFactor, textRun, style))
break;
FloatSize extraOffset;
if (shadow)
extraOffset = applyShadowToGraphicsContext(context, shadow, shadowRect, false , true , true );
context->save();
context->scale(FloatSize(1 / scalingFactor, 1 / scalingFactor));
scaledFont.drawText(context, textRun, textOrigin + extraOffset, startPosition, endPosition);
context->restore();
if (shadow) {
if (shadow->next())
context->restore();
else
context->clearShadow();
}
restoreGraphicsContextAfterTextPainting(context, textRun);
if (!shadow)
break;
shadow = shadow->next();
} while (shadow);
}
void SVGInlineTextBox::paintText(GraphicsContext* context, RenderStyle* style, RenderStyle* selectionStyle, const SVGTextFragment& fragment, bool hasSelection, bool paintSelectedTextOnly)
{
ASSERT(style);
ASSERT(selectionStyle);
int startPosition = 0;
int endPosition = 0;
if (hasSelection) {
selectionStartEnd(startPosition, endPosition);
hasSelection = mapStartEndPositionsIntoFragmentCoordinates(fragment, startPosition, endPosition);
}
TextRun textRun = constructTextRun(style, fragment);
if (!hasSelection || startPosition >= endPosition) {
paintTextWithShadows(context, style, textRun, fragment, 0, fragment.length);
return;
}
if (startPosition > 0 && !paintSelectedTextOnly)
paintTextWithShadows(context, style, textRun, fragment, 0, startPosition);
if (style != selectionStyle)
SVGResourcesCache::clientStyleChanged(parent()->renderer(), StyleDifferenceRepaint, *selectionStyle);
TextRun selectionTextRun = constructTextRun(selectionStyle, fragment);
paintTextWithShadows(context, selectionStyle, textRun, fragment, startPosition, endPosition);
if (style != selectionStyle)
SVGResourcesCache::clientStyleChanged(parent()->renderer(), StyleDifferenceRepaint, *style);
if (endPosition < static_cast<int>(fragment.length) && !paintSelectedTextOnly)
paintTextWithShadows(context, style, textRun, fragment, endPosition, fragment.length);
}
FloatRect SVGInlineTextBox::calculateBoundaries() const
{
FloatRect textRect;
float scalingFactor = renderer().scalingFactor();
ASSERT(scalingFactor);
float baseline = renderer().scaledFont().fontMetrics().floatAscent() / scalingFactor;
AffineTransform fragmentTransform;
unsigned textFragmentsSize = m_textFragments.size();
for (unsigned i = 0; i < textFragmentsSize; ++i) {
const SVGTextFragment& fragment = m_textFragments.at(i);
FloatRect fragmentRect(fragment.x, fragment.y - baseline, fragment.width, fragment.height);
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
fragmentRect = fragmentTransform.mapRect(fragmentRect);
textRect.unite(fragmentRect);
}
return textRect;
}
bool SVGInlineTextBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit, LayoutUnit)
{
ASSERT(!isLineBreak());
PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_TEXT_HITTESTING, request, renderer().style().pointerEvents());
bool isVisible = renderer().style().visibility() == VISIBLE;
if (isVisible || !hitRules.requireVisible) {
if ((hitRules.canHitStroke && (renderer().style().svgStyle().hasStroke() || !hitRules.requireStroke))
|| (hitRules.canHitFill && (renderer().style().svgStyle().hasFill() || !hitRules.requireFill))) {
FloatPoint boxOrigin(x(), y());
boxOrigin.moveBy(accumulatedOffset);
FloatRect rect(boxOrigin, size());
if (locationInContainer.intersects(rect)) {
renderer().updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset));
if (!result.addNodeToRectBasedTestResult(&renderer().textNode(), request, locationInContainer, rect))
return true;
}
}
}
return false;
}
}