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#include "core/rendering/svg/SVGTextChunk.h"
23
24#include "core/rendering/svg/SVGInlineTextBox.h"
25#include "core/rendering/svg/SVGTextFragment.h"
26
27namespace blink {
28
29SVGTextChunk::SVGTextChunk(unsigned chunkStyle, float desiredTextLength)
30    : m_chunkStyle(chunkStyle)
31    , m_desiredTextLength(desiredTextLength)
32{
33}
34
35void SVGTextChunk::calculateLength(float& length, unsigned& characters) const
36{
37    SVGTextFragment* lastFragment = 0;
38
39    unsigned boxCount = m_boxes.size();
40    for (unsigned boxPosition = 0; boxPosition < boxCount; ++boxPosition) {
41        SVGInlineTextBox* textBox = m_boxes.at(boxPosition);
42        Vector<SVGTextFragment>& fragments = textBox->textFragments();
43
44        unsigned size = fragments.size();
45        if (!size)
46            continue;
47
48        for (unsigned i = 0; i < size; ++i) {
49            SVGTextFragment& fragment = fragments.at(i);
50            characters += fragment.length;
51
52            if (m_chunkStyle & VerticalText)
53                length += fragment.height;
54            else
55                length += fragment.width;
56
57            if (!lastFragment) {
58                lastFragment = &fragment;
59                continue;
60            }
61
62            // Resepect gap between chunks.
63            if (m_chunkStyle & VerticalText)
64                 length += fragment.y - (lastFragment->y + lastFragment->height);
65            else
66                 length += fragment.x - (lastFragment->x + lastFragment->width);
67
68            lastFragment = &fragment;
69        }
70    }
71}
72
73float SVGTextChunk::calculateTextAnchorShift(float length) const
74{
75    if (m_chunkStyle & MiddleAnchor)
76        return -length / 2;
77    if (m_chunkStyle & EndAnchor)
78        return m_chunkStyle & RightToLeftText ? 0 : -length;
79    return m_chunkStyle & RightToLeftText ? -length : 0;
80}
81
82} // namespace blink
83