1/*
2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21
22#if ENABLE(SVG)
23#include "SVGTextChunk.h"
24
25#include "SVGInlineTextBox.h"
26#include "SVGTextFragment.h"
27
28namespace WebCore {
29
30SVGTextChunk::SVGTextChunk(unsigned chunkStyle, float desiredTextLength)
31    : m_chunkStyle(chunkStyle)
32    , m_desiredTextLength(desiredTextLength)
33{
34}
35
36void SVGTextChunk::calculateLength(float& length, unsigned& characters) const
37{
38    SVGTextFragment* lastFragment = 0;
39
40    unsigned boxCount = m_boxes.size();
41    for (unsigned boxPosition = 0; boxPosition < boxCount; ++boxPosition) {
42        SVGInlineTextBox* textBox = m_boxes.at(boxPosition);
43        Vector<SVGTextFragment>& fragments = textBox->textFragments();
44
45        unsigned size = fragments.size();
46        if (!size)
47            continue;
48
49        for (unsigned i = 0; i < size; ++i) {
50            SVGTextFragment& fragment = fragments.at(i);
51            characters += fragment.length;
52
53            if (m_chunkStyle & VerticalText)
54                length += fragment.height;
55            else
56                length += fragment.width;
57
58            if (!lastFragment) {
59                lastFragment = &fragment;
60                continue;
61            }
62
63            // Resepect gap between chunks.
64            if (m_chunkStyle & VerticalText)
65                 length += fragment.y - (lastFragment->y + lastFragment->height);
66            else
67                 length += fragment.x - (lastFragment->x + lastFragment->width);
68
69            lastFragment = &fragment;
70        }
71    }
72}
73
74float SVGTextChunk::calculateTextAnchorShift(float length) const
75{
76    if (m_chunkStyle & MiddleAnchor)
77        return -length / 2;
78    if (m_chunkStyle & EndAnchor)
79        return m_chunkStyle & RightToLeftText ? 0 : -length;
80    return m_chunkStyle & RightToLeftText ? -length : 0;
81}
82
83} // namespace WebCore
84
85#endif // ENABLE(SVG)
86