summaryrefslogtreecommitdiffstats
path: root/WebCore/rendering/svg/SVGTextLayoutBuilder.cpp
blob: 0b3a75214b018874ccbec934459065cbe66e559c (plain)
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
/*
 * 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"

#if ENABLE(SVG)
#include "SVGTextLayoutBuilder.h"

#include "RenderSVGInlineText.h"
#include "RenderSVGText.h"
#include "SVGTextLayoutUtilities.h"
#include "SVGTextPositioningElement.h"

// Set to a value > 0 to dump the layout vectors
#define DUMP_LAYOUT_VECTORS 0

namespace WebCore {

SVGTextLayoutBuilder::SVGTextLayoutBuilder()
{
}

void SVGTextLayoutBuilder::buildLayoutAttributesForTextSubtree(RenderSVGText* textRoot)
{
    ASSERT(textRoot);
    m_scopes.clear();

    // Build layout scopes.
    unsigned atCharacter = 0;
    buildLayoutScopes(textRoot, atCharacter);

    if (!atCharacter)
        return;

    // Add outermost scope, after text length is known.
    LayoutScope scope;
    buildLayoutScope(scope, textRoot, 0, atCharacter);
    m_scopes.prepend(scope);

    // Build layout information respecting scope of attribute values.
    buildLayoutAttributesFromScopes();

    atCharacter = 0;
    propagateLayoutAttributes(textRoot, atCharacter);
}

static inline void copyToDestinationVector(Vector<float>& destination, unsigned destinationStartOffset, Vector<float>& source, unsigned sourceStartOffset, unsigned length)
{
    ASSERT(destinationStartOffset + length <= destination.size());

    Vector<float>::iterator sourceBegin = source.begin() + sourceStartOffset;
    std::copy(sourceBegin, sourceBegin + length, destination.begin() + destinationStartOffset);
}

static inline void copyToDestinationVectorIfSourceRangeIsNotEmpty(Vector<float>& destination, unsigned destinationStartOffset, Vector<float>& source, unsigned sourceStartOffset, unsigned length)
{
    bool rangeEmpty = true;

    unsigned size = sourceStartOffset + length;
    for (unsigned i = sourceStartOffset; i < size; ++i) {
        if (source.at(i) == SVGTextLayoutAttributes::emptyValue())
            continue;
        rangeEmpty = false;
        break;
    }

    if (rangeEmpty)
        return;

    destination.resize(length);
    copyToDestinationVector(destination, destinationStartOffset, source, sourceStartOffset, length);
}

void SVGTextLayoutBuilder::propagateLayoutAttributes(RenderObject* start, unsigned& atCharacter)
{
    for (RenderObject* child = start->firstChild(); child; child = child->nextSibling()) { 
        if (!child->isSVGInlineText()) {
            if (child->isSVGInline())
                propagateLayoutAttributes(child, atCharacter);
            continue;
        }

        RenderSVGInlineText* text = static_cast<RenderSVGInlineText*>(child);
        unsigned textLength = text->textLength();

        // Build layout attributes for a single RenderSVGInlineText renderer.
        SVGTextLayoutAttributes attributes;

        // The x value list should always be as large as the text length.
        // Any values that are empty will be filled in by the actual text layout process later,
        // as we need to be able to query the x/y position for every character through SVG DOM.
        attributes.xValues().resize(textLength);
        copyToDestinationVector(attributes.xValues(), 0, m_attributes.xValues(), atCharacter, textLength);

        // Same for the y value list.
        attributes.yValues().resize(textLength);
        copyToDestinationVector(attributes.yValues(), 0, m_attributes.yValues(), atCharacter, textLength);

        // The dx/dy/rotate value lists may be empty.
        copyToDestinationVectorIfSourceRangeIsNotEmpty(attributes.dxValues(), 0, m_attributes.dxValues(), atCharacter, textLength);
        copyToDestinationVectorIfSourceRangeIsNotEmpty(attributes.dyValues(), 0, m_attributes.dyValues(), atCharacter, textLength);
        copyToDestinationVectorIfSourceRangeIsNotEmpty(attributes.rotateValues(), 0, m_attributes.rotateValues(), atCharacter, textLength);

        // Build CharacterData, which will be used to detect ligatures, holds kerning pairs (glyph name, unicode string) and character metrics.
        measureCharacters(text, attributes);

#if DUMP_LAYOUT_VECTORS > 0
        fprintf(stderr, "Dumping layout vector for RenderSVGInlineText, renderer=%p, node=%p\n", text, text->node());
        attributes.dump();
#endif

        text->storeLayoutAttributes(attributes);
        atCharacter += text->textLength();
    }
}

void SVGTextLayoutBuilder::buildLayoutScopes(RenderObject* start, unsigned& atCharacter)
{
    for (RenderObject* child = start->firstChild(); child; child = child->nextSibling()) { 
        if (child->isSVGInlineText()) {
            atCharacter += toRenderText(child)->textLength();
            continue;
        }

        if (!child->isSVGInline())
            continue;

        unsigned textContentStart = atCharacter;
        buildLayoutScopes(child, atCharacter);

        LayoutScope scope;
        buildLayoutScope(scope, child, textContentStart, atCharacter - textContentStart);
        m_scopes.append(scope);
    }
}

static inline void fillDestinationVectorWithLastSourceValue(Vector<float>& destination, unsigned destinationStartOffset, Vector<float>& source, unsigned length)
{
    if (source.isEmpty())
        return;

    float lastValue = source.last();

    unsigned rotateValuesSize = source.size();
    for (unsigned i = rotateValuesSize; i < length; ++i) {
        ASSERT(i + destinationStartOffset < destination.size());
        destination.at(i + destinationStartOffset) = lastValue;
    }
}

void SVGTextLayoutBuilder::buildLayoutAttributesFromScopes()
{
    ASSERT(!m_scopes.isEmpty());

    unsigned totalLength = m_scopes.first().textContentLength;
    if (!totalLength)
        return;

    m_attributes.fillWithEmptyValues(totalLength);

    // Build final list of x/y/dx/dy/rotate values for each character stores in the <text> subtree.
    for (unsigned atScope = 0; atScope < m_scopes.size(); ++atScope) {
        LayoutScope& scope = m_scopes.at(atScope);
        SVGTextLayoutAttributes& attributes = scope.attributes;

        copyToDestinationVector(m_attributes.xValues(), scope.textContentStart, attributes.xValues(), 0, attributes.xValues().size());
        copyToDestinationVector(m_attributes.yValues(), scope.textContentStart, attributes.yValues(), 0, attributes.yValues().size());
        copyToDestinationVector(m_attributes.dxValues(), scope.textContentStart, attributes.dxValues(), 0, attributes.dxValues().size());
        copyToDestinationVector(m_attributes.dyValues(), scope.textContentStart, attributes.dyValues(), 0, attributes.dyValues().size());
        copyToDestinationVector(m_attributes.rotateValues(), scope.textContentStart, attributes.rotateValues(), 0, attributes.rotateValues().size());

        // In horizontal (vertical) writing modes, the last y (x) value in the scope is the default y (x) value for all following characters, unless explicitely overriden.
        if (scope.isVerticalWritingMode)
            fillDestinationVectorWithLastSourceValue(m_attributes.xValues(), scope.textContentStart, attributes.xValues(), scope.textContentLength);
        else
            fillDestinationVectorWithLastSourceValue(m_attributes.yValues(), scope.textContentStart, attributes.yValues(), scope.textContentLength);

        // The last rotation value in the scope is the default rotation for all following character, unless explicitely overriden.
        fillDestinationVectorWithLastSourceValue(m_attributes.rotateValues(), scope.textContentStart, attributes.rotateValues(), scope.textContentLength);
    }
}

void SVGTextLayoutBuilder::measureCharacters(RenderSVGInlineText* text, SVGTextLayoutAttributes& attributes)
{
    ASSERT(text);
    ASSERT(text->style());
    const Font& font = text->style()->font();
    const UChar* characters = text->characters();
    int length = text->textLength();

    TextRun run(0, 0);
    run.disableSpacing();
    run.disableRoundingHacks();

    int charsConsumed = 0;
    for (int position = 0; position < length; position += charsConsumed) {
        run.setText(characters + position, 1);
        int extraCharsAvailable = length - position - 1;

        SVGTextLayoutAttributes::CharacterData characterData;
        characterData.width = font.floatWidth(run, extraCharsAvailable, characterData.spansCharacters, characterData.glyphName);
        characterData.height = font.height();
        characterData.unicodeString = String(characters + position, characterData.spansCharacters);
        attributes.characterDataValues().append(characterData);

        charsConsumed = characterData.spansCharacters;
    }
}

static inline void extractFloatValuesFromSVGLengthList(SVGElement* lengthContext, SVGLengthList* list, Vector<float>& floatValues, int textContentLength)
{
    ASSERT(lengthContext);
    ASSERT(list);
    ASSERT(textContentLength >= 0);

    ExceptionCode ec = 0;
    int length = list->numberOfItems();
    if (length > textContentLength)
        length = textContentLength;

    for (int i = 0; i < length; ++i) {
        SVGLength length(list->getItem(i, ec));
        ASSERT(!ec);
        floatValues.append(length.value(lengthContext));
    }
}

static inline void extractFloatValuesFromSVGNumberList(SVGNumberList* list, Vector<float>& floatValues, int textContentLength)
{
    ASSERT(list);
    ASSERT(textContentLength >= 0);

    ExceptionCode ec = 0;
    int length = list->numberOfItems();
    if (length > textContentLength)
        length = textContentLength;

    for (int i = 0; i < length; ++i) {
        float length(list->getItem(i, ec));
        ASSERT(!ec);
        floatValues.append(length);
    }
}

static inline SVGTextPositioningElement* svgTextPositioningElementForInlineRenderer(RenderObject* renderer)
{
    ASSERT(renderer);
    ASSERT(renderer->isSVGText() || renderer->isSVGInline());

    Node* node = renderer->node();
    ASSERT(node);
    ASSERT(node->isSVGElement());

    if (!node->hasTagName(SVGNames::textTag)
     && !node->hasTagName(SVGNames::tspanTag)
#if ENABLE(SVG_FONTS)
     && !node->hasTagName(SVGNames::altGlyphTag)
#endif
     && !node->hasTagName(SVGNames::trefTag))
        return 0;

    return static_cast<SVGTextPositioningElement*>(node);
}

void SVGTextLayoutBuilder::buildLayoutScope(LayoutScope& scope, RenderObject* renderer, unsigned textContentStart, unsigned textContentLength)
{
    ASSERT(renderer);
    ASSERT(renderer->style());

    scope.isVerticalWritingMode = isVerticalWritingMode(renderer->style()->svgStyle());
    scope.textContentStart = textContentStart;
    scope.textContentLength = textContentLength;

    SVGTextPositioningElement* element = svgTextPositioningElementForInlineRenderer(renderer);
    if (!element)
        return;

    SVGTextLayoutAttributes& attributes = scope.attributes;
    extractFloatValuesFromSVGLengthList(element, element->x(), attributes.xValues(), textContentLength);
    extractFloatValuesFromSVGLengthList(element, element->y(), attributes.yValues(), textContentLength);
    extractFloatValuesFromSVGLengthList(element, element->dx(), attributes.dxValues(), textContentLength);
    extractFloatValuesFromSVGLengthList(element, element->dy(), attributes.dyValues(), textContentLength);
    extractFloatValuesFromSVGNumberList(element->rotate(), attributes.rotateValues(), textContentLength);
}

}

#endif // ENABLE(SVG)