1/*
2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "SVGTextChunkBuilder.h"
23
24#include "SVGElement.h"
25#include "SVGInlineTextBox.h"
26#include "SVGLengthContext.h"
27#include "SVGTextFragment.h"
28
29namespace WebCore {
30
31SVGTextChunkBuilder::SVGTextChunkBuilder()
32{
33}
34
35unsigned SVGTextChunkBuilder::totalCharacters() const
36{
37 unsigned characters = 0;
38 for (const auto& chunk : m_textChunks)
39 characters += chunk.totalCharacters();
40 return characters;
41}
42
43float SVGTextChunkBuilder::totalLength() const
44{
45 float length = 0;
46 for (const auto& chunk : m_textChunks)
47 length += chunk.totalLength();
48 return length;
49}
50
51float SVGTextChunkBuilder::totalAnchorShift() const
52{
53 float anchorShift = 0;
54 for (const auto& chunk : m_textChunks)
55 anchorShift += chunk.totalAnchorShift();
56 return anchorShift;
57}
58
59AffineTransform SVGTextChunkBuilder::transformationForTextBox(SVGInlineTextBox* textBox) const
60{
61 auto it = m_textBoxTransformations.find(textBox);
62 return it == m_textBoxTransformations.end() ? AffineTransform() : it->value;
63}
64
65void SVGTextChunkBuilder::buildTextChunks(const Vector<SVGInlineTextBox*>& lineLayoutBoxes)
66{
67 if (lineLayoutBoxes.isEmpty())
68 return;
69
70 unsigned limit = lineLayoutBoxes.size();
71 unsigned first = limit;
72
73 for (unsigned i = 0; i < limit; ++i) {
74 if (!lineLayoutBoxes[i]->startsNewTextChunk())
75 continue;
76
77 if (first == limit)
78 first = i;
79 else {
80 ASSERT_WITH_SECURITY_IMPLICATION(first != i);
81 m_textChunks.append(SVGTextChunk(lineLayoutBoxes, first, i));
82 first = i;
83 }
84 }
85
86 if (first != limit)
87 m_textChunks.append(SVGTextChunk(lineLayoutBoxes, first, limit));
88}
89
90void SVGTextChunkBuilder::layoutTextChunks(const Vector<SVGInlineTextBox*>& lineLayoutBoxes)
91{
92 buildTextChunks(lineLayoutBoxes);
93 if (m_textChunks.isEmpty())
94 return;
95
96 for (const auto& chunk : m_textChunks)
97 chunk.layout(m_textBoxTransformations);
98
99 m_textChunks.clear();
100}
101
102}
103