1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
/**
* Copyright (C) 2007 Rob Buis <buis@kde.org>
* (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "SVGInlineTextBox.h"
#if ENABLE(SVG)
#include "FloatConversion.h"
#include "GraphicsContext.h"
#include "InlineFlowBox.h"
#include "RenderBlock.h"
#include "RenderSVGInlineText.h"
#include "RenderSVGResource.h"
#include "RenderSVGResourceSolidColor.h"
#include "SVGRootInlineBox.h"
using namespace std;
namespace WebCore {
SVGInlineTextBox::SVGInlineTextBox(RenderObject* object)
: InlineTextBox(object)
, m_logicalHeight(0)
, m_paintingResourceMode(ApplyToDefaultMode)
, m_startsNewTextChunk(false)
, m_paintingResource(0)
{
}
int SVGInlineTextBox::offsetForPosition(int, bool) const
{
// SVG doesn't use the standard offset <-> position selection system, as it's not suitable for SVGs complex needs.
// vertical text selection, inline boxes spanning multiple lines (contrary to HTML, etc.)
ASSERT_NOT_REACHED();
return 0;
}
int SVGInlineTextBox::offsetForPositionInFragment(const SVGTextFragment& fragment, float position, bool includePartialGlyphs) const
{
RenderText* textRenderer = this->textRenderer();
ASSERT(textRenderer);
RenderStyle* style = textRenderer->style();
ASSERT(style);
TextRun textRun(constructTextRun(style, fragment));
// Eventually handle lengthAdjust="spacingAndGlyphs".
// FIXME: Handle vertical text.
if (!fragment.transform.isIdentity())
textRun.setHorizontalGlyphStretch(narrowPrecisionToFloat(fragment.transform.xScale()));
return fragment.positionListOffset - start() + style->font().offsetForPosition(textRun, position, includePartialGlyphs);
}
int SVGInlineTextBox::positionForOffset(int) const
{
// SVG doesn't use the offset <-> position selection system.
ASSERT_NOT_REACHED();
return 0;
}
FloatRect SVGInlineTextBox::selectionRectForTextFragment(const SVGTextFragment& fragment, int startPosition, int endPosition, RenderStyle* style)
{
ASSERT(startPosition < endPosition);
const Font& font = style->font();
FloatPoint textOrigin(fragment.x, fragment.y - font.ascent());
return font.selectionRectForText(constructTextRun(style, fragment), textOrigin, fragment.height, startPosition, endPosition);
}
IntRect SVGInlineTextBox::selectionRect(int, int, int startPosition, int endPosition)
{
int boxStart = start();
startPosition = max(startPosition - boxStart, 0);
endPosition = min(endPosition - boxStart, static_cast<int>(len()));
if (startPosition >= endPosition)
return IntRect();
RenderText* text = textRenderer();
ASSERT(text);
RenderStyle* style = text->style();
ASSERT(style);
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);
if (!fragment.transform.isIdentity())
fragmentRect = fragment.transform.mapRect(fragmentRect);
selectionRect.unite(fragmentRect);
}
return enclosingIntRect(selectionRect);
}
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);
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.isValid() || !backgroundColor.alpha())
return;
RenderStyle* style = parentRenderer->style();
ASSERT(style);
const SVGRenderStyle* svgStyle = style->svgStyle();
ASSERT(svgStyle);
bool hasFill = svgStyle->hasFill();
bool hasStroke = svgStyle->hasStroke();
RenderStyle* selectionStyle = style;
if (hasSelection) {
selectionStyle = parentRenderer->getCachedPseudoStyle(SELECTION);
if (selectionStyle) {
const SVGRenderStyle* svgSelectionStyle = selectionStyle->svgStyle();
ASSERT(svgSelectionStyle);
if (!hasFill)
hasFill = svgSelectionStyle->hasFill();
if (!hasStroke)
hasStroke = svgSelectionStyle->hasStroke();
} else
selectionStyle = style;
}
int startPosition, endPosition;
selectionStartEnd(startPosition, endPosition);
int fragmentStartPosition = 0;
int fragmentEndPosition = 0;
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;
paintInfo.context->save();
if (!fragment.transform.isIdentity())
paintInfo.context->concatCTM(fragment.transform);
paintInfo.context->setFillColor(backgroundColor, style->colorSpace());
paintInfo.context->fillRect(selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style), backgroundColor, style->colorSpace());
m_paintingResourceMode = ApplyToDefaultMode;
paintInfo.context->restore();
}
ASSERT(!m_paintingResource);
}
void SVGInlineTextBox::paint(PaintInfo& paintInfo, int, int)
{
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();
ASSERT(parentRenderer);
bool paintSelectedTextOnly = paintInfo.phase == PaintPhaseSelection;
bool hasSelection = !parentRenderer->document()->printing() && selectionState() != RenderObject::SelectionNone;
if (!hasSelection && paintSelectedTextOnly)
return;
RenderStyle* style = parentRenderer->style();
ASSERT(style);
const SVGRenderStyle* svgStyle = style->svgStyle();
ASSERT(svgStyle);
bool hasFill = svgStyle->hasFill();
bool hasStroke = svgStyle->hasStroke();
RenderStyle* selectionStyle = style;
if (hasSelection) {
selectionStyle = parentRenderer->getCachedPseudoStyle(SELECTION);
if (selectionStyle) {
const SVGRenderStyle* svgSelectionStyle = selectionStyle->svgStyle();
ASSERT(svgSelectionStyle);
if (!hasFill)
hasFill = svgSelectionStyle->hasFill();
if (!hasStroke)
hasStroke = svgSelectionStyle->hasStroke();
} else
selectionStyle = style;
}
unsigned textFragmentsSize = m_textFragments.size();
for (unsigned i = 0; i < textFragmentsSize; ++i) {
SVGTextFragment& fragment = m_textFragments.at(i);
ASSERT(!m_paintingResource);
paintInfo.context->save();
if (!fragment.transform.isIdentity())
paintInfo.context->concatCTM(fragment.transform);
// 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.
int decorations = style->textDecorationsInEffect();
if (decorations & UNDERLINE)
paintDecoration(paintInfo.context, UNDERLINE, fragment);
if (decorations & OVERLINE)
paintDecoration(paintInfo.context, OVERLINE, fragment);
// Fill text
if (hasFill) {
m_paintingResourceMode = ApplyToFillMode | ApplyToTextMode;
paintText(paintInfo.context, style, selectionStyle, fragment, hasSelection, paintSelectedTextOnly);
}
// Stroke text
if (hasStroke) {
m_paintingResourceMode = ApplyToStrokeMode | ApplyToTextMode;
paintText(paintInfo.context, style, selectionStyle, fragment, hasSelection, paintSelectedTextOnly);
}
// 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 & LINE_THROUGH)
paintDecoration(paintInfo.context, LINE_THROUGH, fragment);
m_paintingResourceMode = ApplyToDefaultMode;
paintInfo.context->restore();
}
ASSERT(!m_paintingResource);
}
bool SVGInlineTextBox::acquirePaintingResource(GraphicsContext*& context, RenderObject* renderer, RenderStyle* style)
{
ASSERT(renderer);
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 {
// We're either called for stroking or filling.
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);
}
}
return true;
}
void SVGInlineTextBox::releasePaintingResource(GraphicsContext*& context)
{
ASSERT(m_paintingResource);
RenderObject* parentRenderer = parent()->renderer();
ASSERT(parentRenderer);
m_paintingResource->postApplyResource(parentRenderer, context, m_paintingResourceMode);
m_paintingResource = 0;
}
bool SVGInlineTextBox::prepareGraphicsContextForTextPainting(GraphicsContext*& context, TextRun& textRun, RenderStyle* style)
{
bool acquiredResource = acquirePaintingResource(context, parent()->renderer(), style);
#if ENABLE(SVG_FONTS)
// SVG Fonts need access to the painting resource used to draw the current text chunk.
if (acquiredResource)
textRun.setActivePaintingResource(m_paintingResource);
#endif
return acquiredResource;
}
void SVGInlineTextBox::restoreGraphicsContextAfterTextPainting(GraphicsContext*& context, TextRun& textRun)
{
releasePaintingResource(context);
#if ENABLE(SVG_FONTS)
textRun.setActivePaintingResource(0);
#endif
}
TextRun SVGInlineTextBox::constructTextRun(RenderStyle* style, const SVGTextFragment& fragment) const
{
ASSERT(style);
ASSERT(textRenderer());
RenderText* text = textRenderer();
ASSERT(text);
TextRun run(text->characters() + fragment.positionListOffset
, fragment.length
, false /* allowTabs */
, 0 /* xPos, only relevant with allowTabs=true */
, 0 /* padding, only relevant for justified text, not relevant for SVG */
, direction() == RTL
, m_dirOverride || style->visuallyOrdered() /* directionalOverride */);
#if ENABLE(SVG_FONTS)
RenderObject* parentRenderer = parent()->renderer();
ASSERT(parentRenderer);
run.setReferencingRenderObject(parentRenderer);
#endif
// Disable any word/character rounding.
run.disableRoundingHacks();
// We handle letter & word spacing ourselves.
run.disableSpacing();
return run;
}
bool SVGInlineTextBox::mapStartEndPositionsIntoFragmentCoordinates(const SVGTextFragment& fragment, int& startPosition, int& endPosition) const
{
if (startPosition >= endPosition)
return false;
int offset = static_cast<int>(fragment.positionListOffset) - 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(startPosition < endPosition);
return true;
}
static inline float positionOffsetForDecoration(ETextDecoration decoration, const Font& font, float thickness)
{
// FIXME: For SVG Fonts we need to use the attributes defined in the <font-face> if specified.
// Compatible with Batik/Opera.
if (decoration == UNDERLINE)
return font.ascent() + thickness * 1.5f;
if (decoration == OVERLINE)
return thickness;
if (decoration == LINE_THROUGH)
return font.ascent() * 5.0f / 8.0f;
ASSERT_NOT_REACHED();
return 0.0f;
}
static inline float thicknessForDecoration(ETextDecoration, const Font& font)
{
// FIXME: For SVG Fonts we need to use the attributes defined in the <font-face> if specified.
// Compatible with Batik/Opera
return font.size() / 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() != TDNONE)
break;
parentBox = parentBox->parent();
}
ASSERT(renderer);
return renderer;
}
void SVGInlineTextBox::paintDecoration(GraphicsContext* context, ETextDecoration decoration, const SVGTextFragment& fragment)
{
if (textRenderer()->style()->textDecorationsInEffect() == TDNONE)
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();
ASSERT(svgDecorationStyle);
bool hasDecorationFill = svgDecorationStyle->hasFill();
bool hasDecorationStroke = svgDecorationStyle->hasStroke();
if (hasDecorationFill) {
m_paintingResourceMode = ApplyToFillMode;
paintDecorationWithStyle(context, decoration, fragment, decorationRenderer);
}
if (hasDecorationStroke) {
m_paintingResourceMode = ApplyToStrokeMode;
paintDecorationWithStyle(context, decoration, fragment, decorationRenderer);
}
}
void SVGInlineTextBox::paintDecorationWithStyle(GraphicsContext* context, ETextDecoration decoration, const SVGTextFragment& fragment, RenderObject* decorationRenderer)
{
ASSERT(!m_paintingResource);
ASSERT(m_paintingResourceMode != ApplyToDefaultMode);
RenderStyle* decorationStyle = decorationRenderer->style();
ASSERT(decorationStyle);
const Font& font = decorationStyle->font();
// The initial y value refers to overline position.
float thickness = thicknessForDecoration(decoration, font);
if (fragment.width <= 0 && thickness <= 0)
return;
float y = fragment.y - font.ascent() + positionOffsetForDecoration(decoration, font, thickness);
Path path;
path.addRect(FloatRect(fragment.x, y, fragment.width, thickness));
context->save();
context->beginPath();
context->addPath(path);
if (acquirePaintingResource(context, decorationRenderer, decorationStyle))
releasePaintingResource(context);
context->restore();
}
void SVGInlineTextBox::paintTextWithShadows(GraphicsContext* context, RenderStyle* style, TextRun& textRun, const SVGTextFragment& fragment, int startPosition, int endPosition)
{
const Font& font = style->font();
const ShadowData* shadow = style->textShadow();
FloatPoint textOrigin(fragment.x, fragment.y);
FloatRect shadowRect(FloatPoint(textOrigin.x(), textOrigin.y() - font.ascent()), FloatSize(fragment.width, fragment.height));
do {
if (!prepareGraphicsContextForTextPainting(context, textRun, style))
break;
FloatSize extraOffset;
if (shadow)
extraOffset = applyShadowToGraphicsContext(context, shadow, shadowRect, false /* stroked */, true /* opaque */, true /* horizontal */);
font.drawText(context, textRun, textOrigin + extraOffset, startPosition, endPosition);
restoreGraphicsContextAfterTextPainting(context, textRun);
if (!shadow)
break;
if (shadow->next())
context->restore();
else
context->clearShadow();
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);
}
// 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);
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);
// Draw text using selection style from the start to the end position of the selection
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);
// 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);
}
IntRect SVGInlineTextBox::calculateBoundaries() const
{
FloatRect textRect;
RenderText* textRenderer = this->textRenderer();
ASSERT(textRenderer);
RenderStyle* style = textRenderer->style();
ASSERT(style);
int baseline = baselinePosition(AlphabeticBaseline);
int heightDifference = baseline - style->font().ascent();
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 + heightDifference);
if (!fragment.transform.isIdentity())
fragmentRect = fragment.transform.mapRect(fragmentRect);
textRect.unite(fragmentRect);
}
return enclosingIntRect(textRect);
}
} // namespace WebCore
#endif
|