-// 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)
-{
- ASSERT(resourceMode != ApplyToDefaultMode);
-
- 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);
-}
-