1/*
2 * Copyright (C) Research In Motion Limited 2010-2012. 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#ifndef SVGTextLayoutEngine_h
21#define SVGTextLayoutEngine_h
22
23#if ENABLE(SVG)
24#include "Path.h"
25#include "SVGTextChunkBuilder.h"
26#include "SVGTextFragment.h"
27#include "SVGTextLayoutAttributes.h"
28#include "SVGTextMetrics.h"
29#include <wtf/Vector.h>
30
31namespace WebCore {
32
33class RenderObject;
34class RenderStyle;
35class RenderSVGInlineText;
36class SVGElement;
37class SVGInlineTextBox;
38class SVGRenderStyle;
39
40// SVGTextLayoutEngine performs the second layout phase for SVG text.
41//
42// The InlineBox tree was created, containing the text chunk information, necessary to apply
43// certain SVG specific text layout properties (text-length adjustments and text-anchor).
44// The second layout phase uses the SVGTextLayoutAttributes stored in the individual
45// RenderSVGInlineText renderers to compute the final positions for each character
46// which are stored in the SVGInlineTextBox objects.
47
48class SVGTextLayoutEngine {
49    WTF_MAKE_NONCOPYABLE(SVGTextLayoutEngine);
50public:
51    SVGTextLayoutEngine(Vector<SVGTextLayoutAttributes*>&);
52
53    Vector<SVGTextLayoutAttributes*>& layoutAttributes() { return m_layoutAttributes; }
54    SVGTextChunkBuilder& chunkLayoutBuilder() { return m_chunkLayoutBuilder; }
55
56    void beginTextPathLayout(RenderObject*, SVGTextLayoutEngine& lineLayout);
57    void endTextPathLayout();
58
59    void layoutInlineTextBox(SVGInlineTextBox*);
60    void finishLayout();
61
62private:
63    void updateCharacerPositionIfNeeded(float& x, float& y);
64    void updateCurrentTextPosition(float x, float y, float glyphAdvance);
65    void updateRelativePositionAdjustmentsIfNeeded(float dx, float dy);
66
67    void recordTextFragment(SVGInlineTextBox*, Vector<SVGTextMetrics>&);
68    bool parentDefinesTextLength(RenderObject*) const;
69
70    void layoutTextOnLineOrPath(SVGInlineTextBox*, RenderSVGInlineText*, const RenderStyle*);
71    void finalizeTransformMatrices(Vector<SVGInlineTextBox*>&);
72
73    bool currentLogicalCharacterAttributes(SVGTextLayoutAttributes*&);
74    bool currentLogicalCharacterMetrics(SVGTextLayoutAttributes*&, SVGTextMetrics&);
75    bool currentVisualCharacterMetrics(SVGInlineTextBox*, Vector<SVGTextMetrics>&, SVGTextMetrics&);
76
77    void advanceToNextLogicalCharacter(const SVGTextMetrics&);
78    void advanceToNextVisualCharacter(const SVGTextMetrics&);
79
80private:
81    Vector<SVGTextLayoutAttributes*>& m_layoutAttributes;
82
83    Vector<SVGInlineTextBox*> m_lineLayoutBoxes;
84    Vector<SVGInlineTextBox*> m_pathLayoutBoxes;
85    SVGTextChunkBuilder m_chunkLayoutBuilder;
86
87    SVGTextFragment m_currentTextFragment;
88    unsigned m_layoutAttributesPosition;
89    unsigned m_logicalCharacterOffset;
90    unsigned m_logicalMetricsListOffset;
91    unsigned m_visualCharacterOffset;
92    unsigned m_visualMetricsListOffset;
93    float m_x;
94    float m_y;
95    float m_dx;
96    float m_dy;
97    bool m_isVerticalText;
98    bool m_inPathLayout;
99
100    // Text on path layout
101    Path m_textPath;
102    float m_textPathLength;
103    float m_textPathStartOffset;
104    float m_textPathCurrentOffset;
105    float m_textPathSpacing;
106    float m_textPathScaling;
107};
108
109} // namespace WebCore
110
111#endif // ENABLE(SVG)
112#endif
113