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