initial import
[vuplus_webkit] / Source / WebCore / rendering / svg / SVGInlineTextBox.cpp
1 /**
2  * Copyright (C) 2007 Rob Buis <buis@kde.org>
3  * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
4  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23 #include "SVGInlineTextBox.h"
24
25 #if ENABLE(SVG)
26 #include "FloatConversion.h"
27 #include "FontCache.h"
28 #include "GraphicsContext.h"
29 #include "HitTestResult.h"
30 #include "InlineFlowBox.h"
31 #include "PointerEventsHitRules.h"
32 #include "RenderBlock.h"
33 #include "RenderSVGInlineText.h"
34 #include "RenderSVGResource.h"
35 #include "RenderSVGResourceSolidColor.h"
36 #include "SVGImageBufferTools.h"
37 #include "SVGRootInlineBox.h"
38 #include "SVGTextRunRenderingContext.h"
39
40 using namespace std;
41
42 namespace WebCore {
43
44 SVGInlineTextBox::SVGInlineTextBox(RenderObject* object)
45     : InlineTextBox(object)
46     , m_logicalHeight(0)
47     , m_paintingResourceMode(ApplyToDefaultMode)
48     , m_startsNewTextChunk(false)
49     , m_paintingResource(0)
50 {
51 }
52
53 int SVGInlineTextBox::offsetForPosition(float, bool) const
54 {
55     // SVG doesn't use the standard offset <-> position selection system, as it's not suitable for SVGs complex needs.
56     // vertical text selection, inline boxes spanning multiple lines (contrary to HTML, etc.)
57     ASSERT_NOT_REACHED();
58     return 0;
59 }
60
61 int SVGInlineTextBox::offsetForPositionInFragment(const SVGTextFragment& fragment, float position, bool includePartialGlyphs) const
62 {
63     RenderSVGInlineText* textRenderer = toRenderSVGInlineText(this->textRenderer());
64     ASSERT(textRenderer);
65
66     float scalingFactor = textRenderer->scalingFactor();
67     ASSERT(scalingFactor);
68
69     RenderStyle* style = textRenderer->style();
70     ASSERT(style);
71
72     TextRun textRun = constructTextRun(style, fragment);
73
74     // Eventually handle lengthAdjust="spacingAndGlyphs".
75     // FIXME: Handle vertical text.
76     AffineTransform fragmentTransform;
77     fragment.buildFragmentTransform(fragmentTransform);
78     if (!fragmentTransform.isIdentity())
79         textRun.setHorizontalGlyphStretch(narrowPrecisionToFloat(fragmentTransform.xScale()));
80
81     return fragment.characterOffset - start() + textRenderer->scaledFont().offsetForPosition(textRun, position * scalingFactor, includePartialGlyphs);
82 }
83
84 float SVGInlineTextBox::positionForOffset(int) const
85 {
86     // SVG doesn't use the offset <-> position selection system. 
87     ASSERT_NOT_REACHED();
88     return 0;
89 }
90
91 FloatRect SVGInlineTextBox::selectionRectForTextFragment(const SVGTextFragment& fragment, int startPosition, int endPosition, RenderStyle* style)
92 {
93     ASSERT(startPosition < endPosition);
94     ASSERT(style);
95
96     FontCachePurgePreventer fontCachePurgePreventer;
97
98     RenderSVGInlineText* textRenderer = toRenderSVGInlineText(this->textRenderer());
99     ASSERT(textRenderer);
100
101     float scalingFactor = textRenderer->scalingFactor();
102     ASSERT(scalingFactor);
103
104     const Font& scaledFont = textRenderer->scaledFont();
105     const FontMetrics& scaledFontMetrics = scaledFont.fontMetrics();
106     FloatPoint textOrigin(fragment.x, fragment.y);
107     if (scalingFactor != 1)
108         textOrigin.scale(scalingFactor, scalingFactor);
109
110     textOrigin.move(0, -scaledFontMetrics.floatAscent());
111
112     FloatRect selectionRect = scaledFont.selectionRectForText(constructTextRun(style, fragment), textOrigin, fragment.height * scalingFactor, startPosition, endPosition);
113     if (scalingFactor == 1)
114         return selectionRect;
115
116     selectionRect.scale(1 / scalingFactor);
117     return selectionRect;
118 }
119
120 IntRect SVGInlineTextBox::localSelectionRect(int startPosition, int endPosition)
121 {
122     int boxStart = start();
123     startPosition = max(startPosition - boxStart, 0);
124     endPosition = min(endPosition - boxStart, static_cast<int>(len()));
125     if (startPosition >= endPosition)
126         return IntRect();
127
128     RenderText* text = textRenderer();
129     ASSERT(text);
130
131     RenderStyle* style = text->style();
132     ASSERT(style);
133
134     AffineTransform fragmentTransform;
135     FloatRect selectionRect;
136     int fragmentStartPosition = 0;
137     int fragmentEndPosition = 0;
138
139     unsigned textFragmentsSize = m_textFragments.size();
140     for (unsigned i = 0; i < textFragmentsSize; ++i) {
141         const SVGTextFragment& fragment = m_textFragments.at(i);
142
143         fragmentStartPosition = startPosition;
144         fragmentEndPosition = endPosition;
145         if (!mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
146             continue;
147
148         FloatRect fragmentRect = selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style);
149         fragment.buildFragmentTransform(fragmentTransform);
150         if (!fragmentTransform.isIdentity())
151             fragmentRect = fragmentTransform.mapRect(fragmentRect);
152
153         selectionRect.unite(fragmentRect);
154     }
155
156     return enclosingIntRect(selectionRect);
157 }
158
159 static inline bool textShouldBePainted(RenderSVGInlineText* textRenderer)
160 {
161     // Font::pixelSize(), returns FontDescription::computedPixelSize(), which returns "int(x + 0.5)".
162     // If the absolute font size on screen is below x=0.5, don't render anything.
163     return textRenderer->scaledFont().pixelSize();
164 }
165
166 void SVGInlineTextBox::paintSelectionBackground(PaintInfo& paintInfo)
167 {
168     ASSERT(paintInfo.shouldPaintWithinRoot(renderer()));
169     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
170     ASSERT(truncation() == cNoTruncation);
171
172     if (renderer()->style()->visibility() != VISIBLE)
173         return;
174
175     RenderObject* parentRenderer = parent()->renderer();
176     ASSERT(parentRenderer);
177     ASSERT(!parentRenderer->document()->printing());
178
179     // Determine whether or not we're selected.
180     bool paintSelectedTextOnly = paintInfo.phase == PaintPhaseSelection;
181     bool hasSelection = selectionState() != RenderObject::SelectionNone;
182     if (!hasSelection || paintSelectedTextOnly)
183         return;
184
185     Color backgroundColor = renderer()->selectionBackgroundColor();
186     if (!backgroundColor.isValid() || !backgroundColor.alpha())
187         return;
188
189     RenderSVGInlineText* textRenderer = toRenderSVGInlineText(this->textRenderer());
190     ASSERT(textRenderer);
191     if (!textShouldBePainted(textRenderer))
192         return;
193
194     RenderStyle* style = parentRenderer->style();
195     ASSERT(style);
196
197     const SVGRenderStyle* svgStyle = style->svgStyle();
198     ASSERT(svgStyle);
199
200     bool hasFill = svgStyle->hasFill();
201     bool hasStroke = svgStyle->hasStroke();
202
203     RenderStyle* selectionStyle = style;
204     if (hasSelection) {
205         selectionStyle = parentRenderer->getCachedPseudoStyle(SELECTION);
206         if (selectionStyle) {
207             const SVGRenderStyle* svgSelectionStyle = selectionStyle->svgStyle();
208             ASSERT(svgSelectionStyle);
209
210             if (!hasFill)
211                 hasFill = svgSelectionStyle->hasFill();
212             if (!hasStroke)
213                 hasStroke = svgSelectionStyle->hasStroke();
214         } else
215             selectionStyle = style;
216     }
217
218     int startPosition, endPosition;
219     selectionStartEnd(startPosition, endPosition);
220
221     int fragmentStartPosition = 0;
222     int fragmentEndPosition = 0;
223     AffineTransform fragmentTransform;
224     unsigned textFragmentsSize = m_textFragments.size();
225     for (unsigned i = 0; i < textFragmentsSize; ++i) {
226         SVGTextFragment& fragment = m_textFragments.at(i);
227         ASSERT(!m_paintingResource);
228
229         fragmentStartPosition = startPosition;
230         fragmentEndPosition = endPosition;
231         if (!mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
232             continue;
233
234         GraphicsContextStateSaver stateSaver(*paintInfo.context);
235         fragment.buildFragmentTransform(fragmentTransform);
236         if (!fragmentTransform.isIdentity())
237             paintInfo.context->concatCTM(fragmentTransform);
238
239         paintInfo.context->setFillColor(backgroundColor, style->colorSpace());
240         paintInfo.context->fillRect(selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style), backgroundColor, style->colorSpace());
241
242         m_paintingResourceMode = ApplyToDefaultMode;
243     }
244
245     ASSERT(!m_paintingResource);
246 }
247
248 void SVGInlineTextBox::paint(PaintInfo& paintInfo, const LayoutPoint&, LayoutUnit, LayoutUnit)
249 {
250     ASSERT(paintInfo.shouldPaintWithinRoot(renderer()));
251     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
252     ASSERT(truncation() == cNoTruncation);
253
254     if (renderer()->style()->visibility() != VISIBLE)
255         return;
256
257     // Note: We're explicitely not supporting composition & custom underlines and custom highlighters - unlike InlineTextBox.
258     // If we ever need that for SVG, it's very easy to refactor and reuse the code.
259
260     RenderObject* parentRenderer = parent()->renderer();
261     ASSERT(parentRenderer);
262
263     bool paintSelectedTextOnly = paintInfo.phase == PaintPhaseSelection;
264     bool hasSelection = !parentRenderer->document()->printing() && selectionState() != RenderObject::SelectionNone;
265     if (!hasSelection && paintSelectedTextOnly)
266         return;
267
268     RenderSVGInlineText* textRenderer = toRenderSVGInlineText(this->textRenderer());
269     ASSERT(textRenderer);
270     if (!textShouldBePainted(textRenderer))
271         return;
272
273     RenderStyle* style = parentRenderer->style();
274     ASSERT(style);
275
276     const SVGRenderStyle* svgStyle = style->svgStyle();
277     ASSERT(svgStyle);
278
279     bool hasFill = svgStyle->hasFill();
280     bool hasStroke = svgStyle->hasStroke();
281
282     RenderStyle* selectionStyle = style;
283     if (hasSelection) {
284         selectionStyle = parentRenderer->getCachedPseudoStyle(SELECTION);
285         if (selectionStyle) {
286             const SVGRenderStyle* svgSelectionStyle = selectionStyle->svgStyle();
287             ASSERT(svgSelectionStyle);
288
289             if (!hasFill)
290                 hasFill = svgSelectionStyle->hasFill();
291             if (!hasStroke)
292                 hasStroke = svgSelectionStyle->hasStroke();
293         } else
294             selectionStyle = style;
295     }
296
297     AffineTransform fragmentTransform;
298     unsigned textFragmentsSize = m_textFragments.size();
299     for (unsigned i = 0; i < textFragmentsSize; ++i) {
300         SVGTextFragment& fragment = m_textFragments.at(i);
301         ASSERT(!m_paintingResource);
302
303         GraphicsContextStateSaver stateSaver(*paintInfo.context);
304         fragment.buildFragmentTransform(fragmentTransform);
305         if (!fragmentTransform.isIdentity())
306             paintInfo.context->concatCTM(fragmentTransform);
307
308         // 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.
309         int decorations = style->textDecorationsInEffect();
310         if (decorations & UNDERLINE)
311             paintDecoration(paintInfo.context, UNDERLINE, fragment);
312         if (decorations & OVERLINE)
313             paintDecoration(paintInfo.context, OVERLINE, fragment);
314
315         // Fill text
316         if (hasFill) {
317             m_paintingResourceMode = ApplyToFillMode | ApplyToTextMode;
318             paintText(paintInfo.context, style, selectionStyle, fragment, hasSelection, paintSelectedTextOnly);
319         }
320
321         // Stroke text
322         if (hasStroke) {
323             m_paintingResourceMode = ApplyToStrokeMode | ApplyToTextMode;
324             paintText(paintInfo.context, style, selectionStyle, fragment, hasSelection, paintSelectedTextOnly);
325         }
326
327         // Spec: Line-through should be drawn after the text is filled and stroked; thus, the line-through is rendered on top of the text.
328         if (decorations & LINE_THROUGH)
329             paintDecoration(paintInfo.context, LINE_THROUGH, fragment);
330
331         m_paintingResourceMode = ApplyToDefaultMode;
332     }
333
334     ASSERT(!m_paintingResource);
335 }
336
337 bool SVGInlineTextBox::acquirePaintingResource(GraphicsContext*& context, float scalingFactor, RenderObject* renderer, RenderStyle* style)
338 {
339     ASSERT(scalingFactor);
340     ASSERT(renderer);
341     ASSERT(style);
342     ASSERT(m_paintingResourceMode != ApplyToDefaultMode);
343
344     Color fallbackColor;
345     if (m_paintingResourceMode & ApplyToFillMode)
346         m_paintingResource = RenderSVGResource::fillPaintingResource(renderer, style, fallbackColor);
347     else if (m_paintingResourceMode & ApplyToStrokeMode)
348         m_paintingResource = RenderSVGResource::strokePaintingResource(renderer, style, fallbackColor);
349     else {
350         // We're either called for stroking or filling.
351         ASSERT_NOT_REACHED();
352     }
353
354     if (!m_paintingResource)
355         return false;
356
357     if (!m_paintingResource->applyResource(renderer, style, context, m_paintingResourceMode)) {
358         if (fallbackColor.isValid()) {
359             RenderSVGResourceSolidColor* fallbackResource = RenderSVGResource::sharedSolidPaintingResource();
360             fallbackResource->setColor(fallbackColor);
361
362             m_paintingResource = fallbackResource;
363             m_paintingResource->applyResource(renderer, style, context, m_paintingResourceMode);
364         }
365     }
366
367     if (scalingFactor != 1 && m_paintingResourceMode & ApplyToStrokeMode)
368         context->setStrokeThickness(context->strokeThickness() * scalingFactor);
369
370     return true;
371 }
372
373 void SVGInlineTextBox::releasePaintingResource(GraphicsContext*& context, const Path* path)
374 {
375     ASSERT(m_paintingResource);
376
377     RenderObject* parentRenderer = parent()->renderer();
378     ASSERT(parentRenderer);
379
380     m_paintingResource->postApplyResource(parentRenderer, context, m_paintingResourceMode, path);
381     m_paintingResource = 0;
382 }
383
384 bool SVGInlineTextBox::prepareGraphicsContextForTextPainting(GraphicsContext*& context, float scalingFactor, TextRun& textRun, RenderStyle* style)
385 {
386     bool acquiredResource = acquirePaintingResource(context, scalingFactor, parent()->renderer(), style);
387     if (!acquiredResource)
388         return false;
389
390 #if ENABLE(SVG_FONTS)
391     // SVG Fonts need access to the painting resource used to draw the current text chunk.
392     TextRun::RenderingContext* renderingContext = textRun.renderingContext();
393     if (renderingContext)
394         static_cast<SVGTextRunRenderingContext*>(renderingContext)->setActivePaintingResource(m_paintingResource);
395 #endif
396
397     return true;
398 }
399
400 void SVGInlineTextBox::restoreGraphicsContextAfterTextPainting(GraphicsContext*& context, TextRun& textRun)
401 {
402     releasePaintingResource(context, /* path */0);
403
404 #if ENABLE(SVG_FONTS)
405     TextRun::RenderingContext* renderingContext = textRun.renderingContext();
406     if (renderingContext)
407         static_cast<SVGTextRunRenderingContext*>(renderingContext)->setActivePaintingResource(0);
408 #else
409     UNUSED_PARAM(textRun);
410 #endif
411 }
412
413 TextRun SVGInlineTextBox::constructTextRun(RenderStyle* style, const SVGTextFragment& fragment) const
414 {
415     ASSERT(style);
416     ASSERT(textRenderer());
417
418     RenderText* text = textRenderer();
419     ASSERT(text);
420
421     TextRun run(text->characters() + fragment.characterOffset
422                 , fragment.length
423                 , false /* allowTabs */
424                 , 0 /* xPos, only relevant with allowTabs=true */
425                 , 0 /* padding, only relevant for justified text, not relevant for SVG */
426                 , TextRun::AllowTrailingExpansion
427                 , direction()
428                 , m_dirOverride || style->rtlOrdering() == VisualOrder /* directionalOverride */);
429
430     if (textRunNeedsRenderingContext(style->font()))
431         run.setRenderingContext(SVGTextRunRenderingContext::create(text));
432
433     run.disableRoundingHacks();
434
435     // We handle letter & word spacing ourselves.
436     run.disableSpacing();
437
438     // Propagate the maximum length of the characters buffer to the TextRun, even when we're only processing a substring.
439     run.setCharactersLength(text->textLength() - fragment.characterOffset);
440     ASSERT(run.charactersLength() >= run.length());
441     return run;
442 }
443
444 bool SVGInlineTextBox::mapStartEndPositionsIntoFragmentCoordinates(const SVGTextFragment& fragment, int& startPosition, int& endPosition) const
445 {
446     if (startPosition >= endPosition)
447         return false;
448
449     int offset = static_cast<int>(fragment.characterOffset) - start();
450     int length = static_cast<int>(fragment.length);
451
452     if (startPosition >= offset + length || endPosition <= offset)
453         return false;
454
455     if (startPosition < offset)
456         startPosition = 0;
457     else
458         startPosition -= offset;
459
460     if (endPosition > offset + length)
461         endPosition = length;
462     else {
463         ASSERT(endPosition >= offset);
464         endPosition -= offset;
465     }
466
467     ASSERT(startPosition < endPosition);
468     return true;
469 }
470
471 static inline float positionOffsetForDecoration(ETextDecoration decoration, const FontMetrics& fontMetrics, float thickness)
472 {
473     // FIXME: For SVG Fonts we need to use the attributes defined in the <font-face> if specified.
474     // Compatible with Batik/Opera.
475     if (decoration == UNDERLINE)
476         return fontMetrics.floatAscent() + thickness * 1.5f;
477     if (decoration == OVERLINE)
478         return thickness;
479     if (decoration == LINE_THROUGH)
480         return fontMetrics.floatAscent() * 5 / 8.0f;
481
482     ASSERT_NOT_REACHED();
483     return 0.0f;
484 }
485
486 static inline float thicknessForDecoration(ETextDecoration, const Font& font)
487 {
488     // FIXME: For SVG Fonts we need to use the attributes defined in the <font-face> if specified.
489     // Compatible with Batik/Opera
490     return font.size() / 20.0f;
491 }
492
493 static inline RenderObject* findRenderObjectDefininingTextDecoration(InlineFlowBox* parentBox)
494 {
495     // Lookup first render object in parent hierarchy which has text-decoration set.
496     RenderObject* renderer = 0;
497     while (parentBox) {
498         renderer = parentBox->renderer();
499
500         if (renderer->style() && renderer->style()->textDecoration() != TDNONE)
501             break;
502
503         parentBox = parentBox->parent();
504     }
505
506     ASSERT(renderer);
507     return renderer;
508 }
509
510 void SVGInlineTextBox::paintDecoration(GraphicsContext* context, ETextDecoration decoration, const SVGTextFragment& fragment)
511 {
512     if (textRenderer()->style()->textDecorationsInEffect() == TDNONE)
513         return;
514
515     // Find out which render style defined the text-decoration, as its fill/stroke properties have to be used for drawing instead of ours.
516     RenderObject* decorationRenderer = findRenderObjectDefininingTextDecoration(parent());
517     RenderStyle* decorationStyle = decorationRenderer->style();
518     ASSERT(decorationStyle);
519
520     if (decorationStyle->visibility() == HIDDEN)
521         return;
522
523     const SVGRenderStyle* svgDecorationStyle = decorationStyle->svgStyle();
524     ASSERT(svgDecorationStyle);
525
526     bool hasDecorationFill = svgDecorationStyle->hasFill();
527     bool hasDecorationStroke = svgDecorationStyle->hasStroke();
528
529     if (hasDecorationFill) {
530         m_paintingResourceMode = ApplyToFillMode;
531         paintDecorationWithStyle(context, decoration, fragment, decorationRenderer);
532     }
533
534     if (hasDecorationStroke) {
535         m_paintingResourceMode = ApplyToStrokeMode;
536         paintDecorationWithStyle(context, decoration, fragment, decorationRenderer);
537     }
538 }
539
540 static inline void normalizeTransform(AffineTransform& transform)
541 {
542     // Obtain consistent numerical results for the AffineTransform on both 32/64bit platforms.
543     // Tested with SnowLeopard on Core Duo vs. Core 2 Duo.
544     static const float s_floatEpsilon = std::numeric_limits<float>::epsilon();
545
546     if (fabs(transform.a() - 1) <= s_floatEpsilon)
547         transform.setA(1);
548     else if (fabs(transform.a() + 1) <= s_floatEpsilon)
549         transform.setA(-1);
550
551     if (fabs(transform.d() - 1) <= s_floatEpsilon)
552         transform.setD(1);
553     else if (fabs(transform.d() + 1) <= s_floatEpsilon)
554         transform.setD(-1);
555
556     if (fabs(transform.e()) <= s_floatEpsilon)
557         transform.setE(0);
558
559     if (fabs(transform.f()) <= s_floatEpsilon)
560         transform.setF(0);
561 }
562
563 void SVGInlineTextBox::paintDecorationWithStyle(GraphicsContext* context, ETextDecoration decoration, const SVGTextFragment& fragment, RenderObject* decorationRenderer)
564 {
565     ASSERT(!m_paintingResource);
566     ASSERT(m_paintingResourceMode != ApplyToDefaultMode);
567
568     RenderStyle* decorationStyle = decorationRenderer->style();
569     ASSERT(decorationStyle);
570
571     float scalingFactor = 1;
572     Font scaledFont;
573     RenderSVGInlineText::computeNewScaledFontForStyle(decorationRenderer, decorationStyle, scalingFactor, scaledFont);
574     ASSERT(scalingFactor);
575
576     // The initial y value refers to overline position.
577     float thickness = thicknessForDecoration(decoration, scaledFont);
578
579     if (fragment.width <= 0 && thickness <= 0)
580         return;
581
582     FloatPoint decorationOrigin(fragment.x, fragment.y);
583     float width = fragment.width;
584     const FontMetrics& scaledFontMetrics = scaledFont.fontMetrics();
585
586     GraphicsContextStateSaver stateSaver(*context);
587     if (scalingFactor != 1) {
588         width *= scalingFactor;
589         decorationOrigin.scale(scalingFactor, scalingFactor);
590
591         AffineTransform newTransform = context->getCTM();
592         newTransform.scale(1 / scalingFactor);
593         normalizeTransform(newTransform);
594
595         context->setCTM(newTransform);
596     }
597
598     decorationOrigin.move(0, -scaledFontMetrics.floatAscent() + positionOffsetForDecoration(decoration, scaledFontMetrics, thickness));
599
600     Path path;
601     path.addRect(FloatRect(decorationOrigin, FloatSize(width, thickness)));
602
603     if (acquirePaintingResource(context, scalingFactor, decorationRenderer, decorationStyle))
604         releasePaintingResource(context, &path);
605 }
606
607 void SVGInlineTextBox::paintTextWithShadows(GraphicsContext* context, RenderStyle* style, TextRun& textRun, const SVGTextFragment& fragment, int startPosition, int endPosition)
608 {
609     RenderSVGInlineText* textRenderer = toRenderSVGInlineText(this->textRenderer());
610     ASSERT(textRenderer);
611
612     float scalingFactor = textRenderer->scalingFactor();
613     ASSERT(scalingFactor);
614
615     const Font& scaledFont = textRenderer->scaledFont();
616     const ShadowData* shadow = style->textShadow();
617
618     FloatPoint textOrigin(fragment.x, fragment.y);
619     FloatSize textSize(fragment.width, fragment.height);
620
621     if (scalingFactor != 1) {
622         textOrigin.scale(scalingFactor, scalingFactor);
623         textSize.scale(scalingFactor);
624     }
625
626     FloatRect shadowRect(FloatPoint(textOrigin.x(), textOrigin.y() - scaledFont.fontMetrics().floatAscent()), textSize);
627
628     do {
629         if (!prepareGraphicsContextForTextPainting(context, scalingFactor, textRun, style))
630             break;
631
632         FloatSize extraOffset;
633         if (shadow)
634             extraOffset = applyShadowToGraphicsContext(context, shadow, shadowRect, false /* stroked */, true /* opaque */, true /* horizontal */);
635
636         AffineTransform originalTransform;
637         if (scalingFactor != 1) {
638             originalTransform = context->getCTM();
639
640             AffineTransform newTransform = originalTransform;
641             newTransform.scale(1 / scalingFactor);
642             normalizeTransform(newTransform);
643
644             context->setCTM(newTransform);
645         }
646
647         scaledFont.drawText(context, textRun, textOrigin + extraOffset, startPosition, endPosition);
648
649         if (scalingFactor != 1)
650             context->setCTM(originalTransform);
651
652         restoreGraphicsContextAfterTextPainting(context, textRun);
653
654         if (!shadow)
655             break;
656
657         if (shadow->next())
658             context->restore();
659         else
660             context->clearShadow();
661
662         shadow = shadow->next();
663     } while (shadow);
664 }
665
666 void SVGInlineTextBox::paintText(GraphicsContext* context, RenderStyle* style, RenderStyle* selectionStyle, const SVGTextFragment& fragment, bool hasSelection, bool paintSelectedTextOnly)
667 {
668     ASSERT(style);
669     ASSERT(selectionStyle);
670
671     int startPosition = 0;
672     int endPosition = 0;
673     if (hasSelection) {
674         selectionStartEnd(startPosition, endPosition);
675         hasSelection = mapStartEndPositionsIntoFragmentCoordinates(fragment, startPosition, endPosition);
676     }
677
678     // Fast path if there is no selection, just draw the whole chunk part using the regular style
679     TextRun textRun = constructTextRun(style, fragment);
680     if (!hasSelection || startPosition >= endPosition) {
681         paintTextWithShadows(context, style, textRun, fragment, 0, fragment.length);
682         return;
683     }
684
685     // Eventually draw text using regular style until the start position of the selection
686     if (startPosition > 0 && !paintSelectedTextOnly)
687         paintTextWithShadows(context, style, textRun, fragment, 0, startPosition);
688
689     // Draw text using selection style from the start to the end position of the selection
690     if (style != selectionStyle)
691         SVGResourcesCache::clientStyleChanged(parent()->renderer(), StyleDifferenceRepaint, selectionStyle);
692
693     TextRun selectionTextRun = constructTextRun(selectionStyle, fragment);
694     paintTextWithShadows(context, selectionStyle, textRun, fragment, startPosition, endPosition);
695
696     if (style != selectionStyle)
697         SVGResourcesCache::clientStyleChanged(parent()->renderer(), StyleDifferenceRepaint, style);
698
699     // Eventually draw text using regular style from the end position of the selection to the end of the current chunk part
700     if (endPosition < static_cast<int>(fragment.length) && !paintSelectedTextOnly)
701         paintTextWithShadows(context, style, textRun, fragment, endPosition, fragment.length);
702 }
703
704 IntRect SVGInlineTextBox::calculateBoundaries() const
705 {
706     FloatRect textRect;
707
708     RenderSVGInlineText* textRenderer = toRenderSVGInlineText(this->textRenderer());
709     ASSERT(textRenderer);
710
711     float scalingFactor = textRenderer->scalingFactor();
712     ASSERT(scalingFactor);
713
714     float baseline = textRenderer->scaledFont().fontMetrics().floatAscent() / scalingFactor;
715
716     AffineTransform fragmentTransform;
717     unsigned textFragmentsSize = m_textFragments.size();
718     for (unsigned i = 0; i < textFragmentsSize; ++i) {
719         const SVGTextFragment& fragment = m_textFragments.at(i);
720         FloatRect fragmentRect(fragment.x, fragment.y - baseline, fragment.width, fragment.height);
721         fragment.buildFragmentTransform(fragmentTransform);
722         if (!fragmentTransform.isIdentity())
723             fragmentRect = fragmentTransform.mapRect(fragmentRect);
724
725         textRect.unite(fragmentRect);
726     }
727
728     return enclosingIntRect(textRect);
729 }
730
731 bool SVGInlineTextBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit, LayoutUnit)
732 {
733     // FIXME: integrate with InlineTextBox::nodeAtPoint better.
734     ASSERT(!isLineBreak());
735
736     PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_TEXT_HITTESTING, request, renderer()->style()->pointerEvents());
737     bool isVisible = renderer()->style()->visibility() == VISIBLE;
738     if (isVisible || !hitRules.requireVisible) {
739         if ((hitRules.canHitStroke && (renderer()->style()->svgStyle()->hasStroke() || !hitRules.requireStroke))
740             || (hitRules.canHitFill && (renderer()->style()->svgStyle()->hasFill() || !hitRules.requireFill))) {
741             FloatPoint boxOrigin(x(), y());
742             boxOrigin.moveBy(accumulatedOffset);
743             FloatRect rect(boxOrigin, size());
744             if (rect.intersects(result.rectForPoint(pointInContainer))) {
745                 renderer()->updateHitTestResult(result, pointInContainer - toLayoutSize(accumulatedOffset));
746                 if (!result.addNodeToRectBasedTestResult(renderer()->node(), pointInContainer, rect))
747                     return true;
748              }
749         }
750     }
751     return false;
752 }
753
754 } // namespace WebCore
755
756 #endif