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

Unified Diff: Source/core/rendering/svg/SVGInlineTextBox.cpp

Issue 627773002: Move painting code from SVGInlineTextBox to SVGInlineTextBoxPainter. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix debug build. Created 6 years, 2 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
« no previous file with comments | « Source/core/rendering/svg/SVGInlineTextBox.h ('k') | Source/core/rendering/svg/SVGRootInlineBox.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/rendering/svg/SVGInlineTextBox.cpp
diff --git a/Source/core/rendering/svg/SVGInlineTextBox.cpp b/Source/core/rendering/svg/SVGInlineTextBox.cpp
index 07c1c3191a7b7042e57585162d5f6a9a9273129d..a8f048d7939aa7c6d34fb4dea6a9bad5dc5f5c26 100644
--- a/Source/core/rendering/svg/SVGInlineTextBox.cpp
+++ b/Source/core/rendering/svg/SVGInlineTextBox.cpp
@@ -26,20 +26,16 @@
#include "core/dom/RenderedDocumentMarker.h"
#include "core/editing/Editor.h"
#include "core/frame/LocalFrame.h"
-#include "core/paint/InlinePainter.h"
-#include "core/paint/InlineTextBoxPainter.h"
+#include "core/paint/SVGInlineTextBoxPainter.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/InlineFlowBox.h"
#include "core/rendering/PaintInfo.h"
#include "core/rendering/PointerEventsHitRules.h"
#include "core/rendering/RenderInline.h"
#include "core/rendering/RenderTheme.h"
-#include "core/rendering/style/ShadowList.h"
#include "core/rendering/svg/RenderSVGInlineText.h"
#include "core/rendering/svg/RenderSVGResource.h"
#include "core/rendering/svg/RenderSVGResourceSolidColor.h"
-#include "core/rendering/svg/SVGRenderSupport.h"
-#include "core/rendering/svg/SVGResourcesCache.h"
#include "core/rendering/svg/SVGTextRunRenderingContext.h"
#include "platform/FloatConversion.h"
#include "platform/fonts/FontCache.h"
@@ -176,217 +172,10 @@ LayoutRect SVGInlineTextBox::localSelectionRect(int startPosition, int endPositi
return enclosingIntRect(selectionRect);
}
-static inline bool textShouldBePainted(RenderSVGInlineText& textRenderer)
-{
- // Font::pixelSize(), returns FontDescription::computedPixelSize(), which returns "int(x + 0.5)".
- // If the absolute font size on screen is below x=0.5, don't render anything.
- return textRenderer.scaledFont().fontDescription().computedPixelSize();
-}
-
-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());
-
- // Determine whether or not we're selected.
- bool paintSelectedTextOnly = paintInfo.phase == PaintPhaseSelection;
- bool hasSelection = selectionState() != RenderObject::SelectionNone;
- if (!hasSelection || paintSelectedTextOnly)
- return;
-
- Color backgroundColor = renderer().selectionBackgroundColor();
- if (!backgroundColor.alpha())
- return;
-
- RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
- if (!textShouldBePainted(textRenderer))
- return;
-
- RenderStyle* style = parentRenderer.style();
- ASSERT(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);
-
- 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);
- paintInfo.context->fillRect(selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style), backgroundColor);
- }
-}
-
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;
-
- // Note: We're explicitely not supporting composition & custom underlines and custom highlighters - unlike InlineTextBox.
- // If we ever need that for SVG, it's very easy to refactor and reuse the code.
-
- RenderObject& parentRenderer = parent()->renderer();
-
- bool paintSelectedTextOnly = paintInfo.phase == PaintPhaseSelection;
- bool hasSelection = !parentRenderer.document().printing() && selectionState() != RenderObject::SelectionNone;
- if (!hasSelection && paintSelectedTextOnly)
- return;
-
- RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
- if (!textShouldBePainted(textRenderer))
- return;
-
- RenderStyle* style = parentRenderer.style();
- ASSERT(style);
-
- InlineTextBoxPainter(*this).paintDocumentMarkers(paintInfo.context, paintOffset, style, textRenderer.scaledFont(), true);
-
- 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 (SVGRenderSupport::isRenderingClipPathAsMaskImage(textRenderer)) {
- hasFill = true;
- hasVisibleStroke = false;
- }
-
- AffineTransform fragmentTransform;
- unsigned textFragmentsSize = m_textFragments.size();
- for (unsigned i = 0; i < textFragmentsSize; ++i) {
- SVGTextFragment& fragment = m_textFragments.at(i);
-
- GraphicsContextStateSaver stateSaver(*paintInfo.context, false);
- fragment.buildFragmentTransform(fragmentTransform);
- if (!fragmentTransform.isIdentity()) {
- stateSaver.save();
- 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.
- unsigned decorations = style->textDecorationsInEffect();
- if (decorations & TextDecorationUnderline)
- paintDecoration(paintInfo.context, TextDecorationUnderline, fragment);
- if (decorations & TextDecorationOverline)
- paintDecoration(paintInfo.context, TextDecorationOverline, fragment);
-
- for (int i = 0; i < 3; i++) {
- switch (svgStyle.paintOrderType(i)) {
- case PT_FILL:
- // Fill text
- if (hasFill) {
- paintText(paintInfo.context, style, selectionStyle, fragment,
- ApplyToFillMode | ApplyToTextMode, hasSelection, paintSelectedTextOnly);
- }
- break;
- case PT_STROKE:
- // Stroke text
- if (hasVisibleStroke) {
- paintText(paintInfo.context, style, selectionStyle, fragment,
- ApplyToStrokeMode | ApplyToTextMode, hasSelection, paintSelectedTextOnly);
- }
- break;
- case PT_MARKERS:
- // Markers don't apply to text
- break;
- default:
- ASSERT_NOT_REACHED();
- break;
- }
- }
-
- // 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 & TextDecorationLineThrough)
- paintDecoration(paintInfo.context, TextDecorationLineThrough, fragment);
- }
-
- // finally, paint the outline if any
- if (style->hasOutline() && parentRenderer.isRenderInline())
- InlinePainter(toRenderInline(parentRenderer)).paintOutline(paintInfo, paintOffset);
-}
-
-class PaintingResourceScope {
-public:
- PaintingResourceScope(RenderObject& renderer)
- : m_renderer(renderer)
- , m_paintingResource(0)
- {
- }
- ~PaintingResourceScope() { ASSERT(!m_paintingResource); }
-
- bool acquirePaintingResource(GraphicsContext*&, RenderStyle*, RenderSVGResourceModeFlags);
- void releasePaintingResource(GraphicsContext*&);
-
-private:
- RenderObject& m_renderer;
- RenderSVGResource* m_paintingResource;
-};
-
-bool PaintingResourceScope::acquirePaintingResource(GraphicsContext*& context, RenderStyle* style, RenderSVGResourceModeFlags resourceModeFlags)
-{
- ASSERT(style);
- RenderSVGResourceMode resourceMode = static_cast<RenderSVGResourceMode>(resourceModeFlags & (ApplyToFillMode | ApplyToStrokeMode));
- ASSERT(resourceMode == ApplyToFillMode || resourceMode == ApplyToStrokeMode);
-
- bool hasFallback = false;
- m_paintingResource = RenderSVGResource::requestPaintingResource(resourceMode, &m_renderer, style, hasFallback);
- if (!m_paintingResource)
- return false;
-
- if (!m_paintingResource->applyResource(&m_renderer, style, context, resourceModeFlags)) {
- if (hasFallback) {
- m_paintingResource = RenderSVGResource::sharedSolidPaintingResource();
- m_paintingResource->applyResource(&m_renderer, style, context, resourceModeFlags);
- }
- }
- return true;
-}
-
-void PaintingResourceScope::releasePaintingResource(GraphicsContext*& context)
-{
- ASSERT(m_paintingResource);
- m_paintingResource->postApplyResource(context);
- m_paintingResource = 0;
+ SVGInlineTextBoxPainter(*this).paint(paintInfo, paintOffset);
}
TextRun SVGInlineTextBox::constructTextRun(RenderStyle* style, const SVGTextFragment& fragment) const
@@ -453,262 +242,14 @@ bool SVGInlineTextBox::mapStartEndPositionsIntoFragmentCoordinates(const SVGText
return true;
}
-// Offset from the baseline for |decoration|. Positive offsets are above the baseline.
-static inline float baselineOffsetForDecoration(TextDecoration 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/Presto.
- if (decoration == TextDecorationUnderline)
- return -thickness * 1.5f;
- if (decoration == TextDecorationOverline)
- return fontMetrics.floatAscent() - thickness;
- if (decoration == TextDecorationLineThrough)
- return fontMetrics.floatAscent() * 3 / 8.0f;
-
- ASSERT_NOT_REACHED();
- return 0.0f;
-}
-
-static inline float thicknessForDecoration(TextDecoration, const Font& font)
-{
- // FIXME: For SVG Fonts we need to use the attributes defined in the <font-face> if specified.
- // Compatible with Batik/Presto
- return font.fontDescription().computedSize() / 20.0f;
-}
-
-static inline RenderObject* findRenderObjectDefininingTextDecoration(InlineFlowBox* parentBox)
-{
- // Lookup first render object in parent hierarchy which has text-decoration set.
- RenderObject* renderer = 0;
- while (parentBox) {
- renderer = &parentBox->renderer();
-
- if (renderer->style() && 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;
-
- // Find out which render style defined the text-decoration, as its fill/stroke properties have to be used for drawing instead of ours.
- RenderObject* decorationRenderer = findRenderObjectDefininingTextDecoration(parent());
- RenderStyle* decorationStyle = decorationRenderer->style();
- ASSERT(decorationStyle);
-
- if (decorationStyle->visibility() == HIDDEN)
- return;
-
- const SVGRenderStyle& svgDecorationStyle = decorationStyle->svgStyle();
-
- for (int i = 0; i < 3; i++) {
- switch (svgDecorationStyle.paintOrderType(i)) {
- case PT_FILL:
- if (svgDecorationStyle.hasFill())
- paintDecorationWithStyle(context, decoration, fragment, decorationRenderer, ApplyToFillMode);
- break;
- case PT_STROKE:
- if (svgDecorationStyle.hasVisibleStroke())
- paintDecorationWithStyle(context, decoration, fragment, decorationRenderer, ApplyToStrokeMode);
- break;
- case PT_MARKERS:
- break;
- default:
- ASSERT_NOT_REACHED();
- }
- }
-}
-
-void SVGInlineTextBox::paintDecorationWithStyle(GraphicsContext* context, TextDecoration decoration,
- const SVGTextFragment& fragment, RenderObject* decorationRenderer, RenderSVGResourceModeFlags resourceMode)
-{
- RenderStyle* decorationStyle = decorationRenderer->style();
- ASSERT(decorationStyle);
-
- 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;
-
- float decorationOffset = baselineOffsetForDecoration(decoration, scaledFont.fontMetrics(), thickness);
- FloatPoint decorationOrigin(fragment.x, fragment.y - decorationOffset / scalingFactor);
-
- Path path;
- path.addRect(FloatRect(decorationOrigin, FloatSize(fragment.width, thickness / scalingFactor)));
-
- PaintingResourceScope resourceScope(*decorationRenderer);
- if (resourceScope.acquirePaintingResource(context, decorationStyle, resourceMode)) {
- SVGRenderSupport::fillOrStrokePath(context, resourceMode, path);
- resourceScope.releasePaintingResource(context);
- }
-}
-
-void SVGInlineTextBox::paintTextWithShadows(GraphicsContext* context, RenderStyle* style,
- TextRun& textRun, const SVGTextFragment& fragment, int startPosition, int endPosition,
- RenderSVGResourceModeFlags resourceMode)
-{
- RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
-
- float scalingFactor = textRenderer.scalingFactor();
- ASSERT(scalingFactor);
-
- const Font& scaledFont = textRenderer.scaledFont();
- const ShadowList* shadowList = style->textShadow();
-
- // Text shadows are disabled when printing. http://crbug.com/258321
- bool hasShadow = shadowList && !context->printing();
-
- FloatPoint textOrigin(fragment.x, fragment.y);
- FloatSize textSize(fragment.width, fragment.height);
-
- if (scalingFactor != 1) {
- textOrigin.scale(scalingFactor, scalingFactor);
- textSize.scale(scalingFactor);
- context->save();
- context->scale(1 / scalingFactor, 1 / scalingFactor);
- }
-
- if (hasShadow)
- context->setDrawLooper(shadowList->createDrawLooper(DrawLooperBuilder::ShadowRespectsAlpha));
-
- PaintingResourceScope resourceScope(parent()->renderer());
- if (resourceScope.acquirePaintingResource(context, style, resourceMode)) {
- if (scalingFactor != 1 && resourceMode & ApplyToStrokeMode)
- context->setStrokeThickness(context->strokeThickness() * scalingFactor);
-
- TextRunPaintInfo textRunPaintInfo(textRun);
- textRunPaintInfo.from = startPosition;
- textRunPaintInfo.to = endPosition;
-
- float baseline = scaledFont.fontMetrics().floatAscent();
- textRunPaintInfo.bounds = FloatRect(textOrigin.x(), textOrigin.y() - baseline,
- textSize.width(), textSize.height());
-
- scaledFont.drawText(context, textRunPaintInfo, textOrigin);
- resourceScope.releasePaintingResource(context);
- }
-
- if (scalingFactor != 1)
- context->restore();
- else if (hasShadow)
- context->clearShadow();
-}
-
-void SVGInlineTextBox::paintText(GraphicsContext* context, RenderStyle* style,
- RenderStyle* selectionStyle, const SVGTextFragment& fragment,
- RenderSVGResourceModeFlags resourceMode, bool hasSelection, bool paintSelectedTextOnly)
-{
- ASSERT(style);
- ASSERT(selectionStyle);
-
- int startPosition = 0;
- int endPosition = 0;
- if (hasSelection) {
- selectionStartEnd(startPosition, endPosition);
- hasSelection = mapStartEndPositionsIntoFragmentCoordinates(fragment, startPosition, endPosition);
- }
-
- // Fast path if there is no selection, just draw the whole chunk part using the regular style
- TextRun textRun = constructTextRun(style, fragment);
- if (!hasSelection || startPosition >= endPosition) {
- paintTextWithShadows(context, style, textRun, fragment, 0, fragment.length, resourceMode);
- return;
- }
-
- // Eventually draw text using regular style until the start position of the selection
- if (startPosition > 0 && !paintSelectedTextOnly)
- paintTextWithShadows(context, style, textRun, fragment, 0, startPosition, resourceMode);
-
- // Draw text using selection style from the start to the end position of the selection
- if (style != selectionStyle) {
- StyleDifference diff;
- diff.setNeedsPaintInvalidationObject();
- SVGResourcesCache::clientStyleChanged(&parent()->renderer(), diff, selectionStyle);
- }
-
- paintTextWithShadows(context, selectionStyle, textRun, fragment, startPosition, endPosition, resourceMode);
-
- if (style != selectionStyle) {
- StyleDifference diff;
- diff.setNeedsPaintInvalidationObject();
- SVGResourcesCache::clientStyleChanged(&parent()->renderer(), diff, style);
- }
-
- // Eventually draw text using regular style from the end position of the selection to the end of the current chunk part
- if (endPosition < static_cast<int>(fragment.length) && !paintSelectedTextOnly)
- paintTextWithShadows(context, style, textRun, fragment, endPosition, fragment.length, resourceMode);
-}
-
void SVGInlineTextBox::paintDocumentMarker(GraphicsContext*, const FloatPoint&, DocumentMarker*, RenderStyle*, const Font&, bool)
{
// SVG does not have support for generic document markers (e.g., spellchecking, etc).
}
-void SVGInlineTextBox::paintTextMatchMarker(GraphicsContext* context, const FloatPoint&, DocumentMarker* marker, RenderStyle* style, const Font& font)
+void SVGInlineTextBox::paintTextMatchMarker(GraphicsContext* context, const FloatPoint& point, DocumentMarker* marker, RenderStyle* style, const Font& font)
{
- // SVG is only interested in the TextMatch markers.
- if (marker->type() != DocumentMarker::TextMatch)
- return;
-
- RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
-
- FloatRect markerRect;
- AffineTransform fragmentTransform;
- for (InlineTextBox* box = textRenderer.firstTextBox(); box; box = box->nextTextBox()) {
- if (!box->isSVGInlineTextBox())
- continue;
-
- SVGInlineTextBox* textBox = toSVGInlineTextBox(box);
-
- int markerStartPosition = std::max<int>(marker->startOffset() - textBox->start(), 0);
- int markerEndPosition = std::min<int>(marker->endOffset() - textBox->start(), textBox->len());
-
- if (markerStartPosition >= markerEndPosition)
- continue;
-
- const Vector<SVGTextFragment>& fragments = textBox->textFragments();
- unsigned textFragmentsSize = fragments.size();
- for (unsigned i = 0; i < textFragmentsSize; ++i) {
- const SVGTextFragment& fragment = fragments.at(i);
-
- int fragmentStartPosition = markerStartPosition;
- int fragmentEndPosition = markerEndPosition;
- if (!textBox->mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
- continue;
-
- FloatRect fragmentRect = textBox->selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style);
- fragment.buildFragmentTransform(fragmentTransform);
-
- // Draw the marker highlight.
- if (renderer().frame()->editor().markedTextMatchesAreHighlighted()) {
- Color color = marker->activeMatch() ?
- RenderTheme::theme().platformActiveTextSearchHighlightColor() :
- RenderTheme::theme().platformInactiveTextSearchHighlightColor();
- GraphicsContextStateSaver stateSaver(*context);
- if (!fragmentTransform.isIdentity())
- context->concatCTM(fragmentTransform);
- context->setFillColor(color);
- context->fillRect(fragmentRect, color);
- }
-
- fragmentRect = fragmentTransform.mapRect(fragmentRect);
- markerRect.unite(fragmentRect);
- }
- }
-
- toRenderedDocumentMarker(marker)->setRenderedRect(textRenderer.localToAbsoluteQuad(markerRect).enclosingBoundingBox());
+ SVGInlineTextBoxPainter(*this).paintTextMatchMarker(context, point, marker, style, font);
}
FloatRect SVGInlineTextBox::calculateBoundaries() const
« no previous file with comments | « Source/core/rendering/svg/SVGInlineTextBox.h ('k') | Source/core/rendering/svg/SVGRootInlineBox.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698