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 "SVGTextLayoutAttributes.h"
24
25#include <stdio.h>
26#include <wtf/text/CString.h>
27
28namespace WebCore {
29
30SVGTextLayoutAttributes::SVGTextLayoutAttributes(RenderSVGInlineText* context)
31    : m_context(context)
32{
33}
34
35void SVGTextLayoutAttributes::reserveCapacity(unsigned length)
36{
37    m_positioningLists.xValues.reserveCapacity(length);
38    m_positioningLists.yValues.reserveCapacity(length);
39    m_positioningLists.dxValues.reserveCapacity(length);
40    m_positioningLists.dyValues.reserveCapacity(length);
41    m_positioningLists.rotateValues.reserveCapacity(length);
42    m_textMetricsValues.reserveCapacity(length);
43}
44
45void SVGTextLayoutAttributes::PositioningLists::fillWithEmptyValues(unsigned length)
46{
47    xValues.fill(SVGTextLayoutAttributes::emptyValue(), length);
48    yValues.fill(SVGTextLayoutAttributes::emptyValue(), length);
49    dxValues.fill(SVGTextLayoutAttributes::emptyValue(), length);
50    dyValues.fill(SVGTextLayoutAttributes::emptyValue(), length);
51    rotateValues.fill(SVGTextLayoutAttributes::emptyValue(), length);
52}
53
54void SVGTextLayoutAttributes::PositioningLists::appendEmptyValues()
55{
56    xValues.append(SVGTextLayoutAttributes::emptyValue());
57    yValues.append(SVGTextLayoutAttributes::emptyValue());
58    dxValues.append(SVGTextLayoutAttributes::emptyValue());
59    dyValues.append(SVGTextLayoutAttributes::emptyValue());
60    rotateValues.append(SVGTextLayoutAttributes::emptyValue());
61}
62
63static inline float safeValueAtPosition(const Vector<float>& values, unsigned position)
64{
65    return position < values.size() ? values[position] : SVGTextLayoutAttributes::emptyValue();
66}
67
68void SVGTextLayoutAttributes::PositioningLists::appendValuesFromPosition(const PositioningLists& source, unsigned position)
69{
70    xValues.append(safeValueAtPosition(source.xValues, position));
71    yValues.append(safeValueAtPosition(source.yValues, position));
72    dxValues.append(safeValueAtPosition(source.dxValues, position));
73    dyValues.append(safeValueAtPosition(source.dyValues, position));
74    rotateValues.append(safeValueAtPosition(source.rotateValues, position));
75}
76
77float SVGTextLayoutAttributes::emptyValue()
78{
79    static float s_emptyValue = std::numeric_limits<float>::max() - 1;
80    return s_emptyValue;
81}
82
83static inline void dumpLayoutVector(const Vector<float>& values)
84{
85    if (values.isEmpty()) {
86        fprintf(stderr, "empty");
87        return;
88    }
89
90    unsigned size = values.size();
91    for (unsigned i = 0; i < size; ++i) {
92        float value = values.at(i);
93        if (value == SVGTextLayoutAttributes::emptyValue())
94            fprintf(stderr, "x ");
95        else
96            fprintf(stderr, "%lf ", value);
97    }
98}
99
100void SVGTextLayoutAttributes::dump() const
101{
102    fprintf(stderr, "context: %p\n", m_context);
103
104    fprintf(stderr, "x values: ");
105    dumpLayoutVector(m_positioningLists.xValues);
106    fprintf(stderr, "\n");
107
108    fprintf(stderr, "y values: ");
109    dumpLayoutVector(m_positioningLists.yValues);
110    fprintf(stderr, "\n");
111
112    fprintf(stderr, "dx values: ");
113    dumpLayoutVector(m_positioningLists.dxValues);
114    fprintf(stderr, "\n");
115
116    fprintf(stderr, "dy values: ");
117    dumpLayoutVector(m_positioningLists.dyValues);
118    fprintf(stderr, "\n");
119
120    fprintf(stderr, "rotate values: ");
121    dumpLayoutVector(m_positioningLists.rotateValues);
122    fprintf(stderr, "\n");
123
124    fprintf(stderr, "character data values:\n");
125    unsigned textMetricsSize = m_textMetricsValues.size();
126    for (unsigned i = 0; i < textMetricsSize; ++i) {
127        const SVGTextMetrics& metrics = m_textMetricsValues.at(i);
128        fprintf(stderr, "| {length=%i, glyphName='%s', unicodeString='%s', width=%lf, height=%lf}\n",
129                metrics.length(), metrics.glyph().name.utf8().data(), metrics.glyph().unicodeString.utf8().data(), metrics.width(), metrics.height());
130    }
131    fprintf(stderr, "\n");
132}
133
134}
135
136#endif // ENABLE(SVG)
137