Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / SVGInlineTextBox.cpp
1 /**
2  * Copyright (C) 2007 Rob Buis <buis@kde.org>
3  * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
4  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23 #include "core/rendering/svg/SVGInlineTextBox.h"
24
25 #include "core/dom/DocumentMarkerController.h"
26 #include "core/dom/RenderedDocumentMarker.h"
27 #include "core/editing/Editor.h"
28 #include "core/frame/LocalFrame.h"
29 #include "core/paint/SVGInlineTextBoxPainter.h"
30 #include "core/rendering/HitTestResult.h"
31 #include "core/rendering/InlineFlowBox.h"
32 #include "core/rendering/PaintInfo.h"
33 #include "core/rendering/PointerEventsHitRules.h"
34 #include "core/rendering/RenderInline.h"
35 #include "core/rendering/RenderTheme.h"
36 #include "core/rendering/svg/RenderSVGInlineText.h"
37 #include "platform/FloatConversion.h"
38 #include "platform/fonts/FontCache.h"
39
40 namespace blink {
41
42 struct ExpectedSVGInlineTextBoxSize : public InlineTextBox {
43     float float1;
44     uint32_t bitfields : 1;
45     Vector<SVGTextFragment> vector;
46 };
47
48 COMPILE_ASSERT(sizeof(SVGInlineTextBox) == sizeof(ExpectedSVGInlineTextBoxSize), SVGInlineTextBox_is_not_of_expected_size);
49
50 SVGInlineTextBox::SVGInlineTextBox(RenderObject& object, int start, unsigned short length)
51     : InlineTextBox(object, start, length)
52     , m_logicalHeight(0)
53     , m_startsNewTextChunk(false)
54 {
55 }
56
57 void SVGInlineTextBox::dirtyLineBoxes()
58 {
59     InlineTextBox::dirtyLineBoxes();
60
61     // Clear the now stale text fragments
62     clearTextFragments();
63
64     // And clear any following text fragments as the text on which they
65     // depend may now no longer exist, or glyph positions may be wrong
66     InlineTextBox* nextBox = nextTextBox();
67     if (nextBox)
68         nextBox->dirtyLineBoxes();
69 }
70
71 int SVGInlineTextBox::offsetForPosition(float, bool) const
72 {
73     // SVG doesn't use the standard offset <-> position selection system, as it's not suitable for SVGs complex needs.
74     // vertical text selection, inline boxes spanning multiple lines (contrary to HTML, etc.)
75     ASSERT_NOT_REACHED();
76     return 0;
77 }
78
79 int SVGInlineTextBox::offsetForPositionInFragment(const SVGTextFragment& fragment, float position, bool includePartialGlyphs) const
80 {
81     RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
82
83     float scalingFactor = textRenderer.scalingFactor();
84     ASSERT(scalingFactor);
85
86     RenderStyle* style = textRenderer.style();
87     ASSERT(style);
88
89     TextRun textRun = constructTextRun(style, fragment);
90
91     // Eventually handle lengthAdjust="spacingAndGlyphs".
92     // FIXME: Handle vertical text.
93     AffineTransform fragmentTransform;
94     fragment.buildFragmentTransform(fragmentTransform);
95     if (!fragmentTransform.isIdentity())
96         textRun.setHorizontalGlyphStretch(narrowPrecisionToFloat(fragmentTransform.xScale()));
97
98     return fragment.characterOffset - start() + textRenderer.scaledFont().offsetForPosition(textRun, position * scalingFactor, includePartialGlyphs);
99 }
100
101 float SVGInlineTextBox::positionForOffset(int) const
102 {
103     // SVG doesn't use the offset <-> position selection system.
104     ASSERT_NOT_REACHED();
105     return 0;
106 }
107
108 FloatRect SVGInlineTextBox::selectionRectForTextFragment(const SVGTextFragment& fragment, int startPosition, int endPosition, RenderStyle* style)
109 {
110     ASSERT(startPosition < endPosition);
111     ASSERT(style);
112
113     FontCachePurgePreventer fontCachePurgePreventer;
114
115     RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
116
117     float scalingFactor = textRenderer.scalingFactor();
118     ASSERT(scalingFactor);
119
120     const Font& scaledFont = textRenderer.scaledFont();
121     const FontMetrics& scaledFontMetrics = scaledFont.fontMetrics();
122     FloatPoint textOrigin(fragment.x, fragment.y);
123     if (scalingFactor != 1)
124         textOrigin.scale(scalingFactor, scalingFactor);
125
126     textOrigin.move(0, -scaledFontMetrics.floatAscent());
127
128     FloatRect selectionRect = scaledFont.selectionRectForText(constructTextRun(style, fragment), textOrigin, fragment.height * scalingFactor, startPosition, endPosition);
129     if (scalingFactor == 1)
130         return selectionRect;
131
132     selectionRect.scale(1 / scalingFactor);
133     return selectionRect;
134 }
135
136 LayoutRect SVGInlineTextBox::localSelectionRect(int startPosition, int endPosition)
137 {
138     int boxStart = start();
139     startPosition = std::max(startPosition - boxStart, 0);
140     endPosition = std::min(endPosition - boxStart, static_cast<int>(len()));
141     if (startPosition >= endPosition)
142         return LayoutRect();
143
144     RenderStyle* style = renderer().style();
145     ASSERT(style);
146
147     AffineTransform fragmentTransform;
148     FloatRect selectionRect;
149     int fragmentStartPosition = 0;
150     int fragmentEndPosition = 0;
151
152     unsigned textFragmentsSize = m_textFragments.size();
153     for (unsigned i = 0; i < textFragmentsSize; ++i) {
154         const SVGTextFragment& fragment = m_textFragments.at(i);
155
156         fragmentStartPosition = startPosition;
157         fragmentEndPosition = endPosition;
158         if (!mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
159             continue;
160
161         FloatRect fragmentRect = selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style);
162         fragment.buildFragmentTransform(fragmentTransform);
163         fragmentRect = fragmentTransform.mapRect(fragmentRect);
164
165         selectionRect.unite(fragmentRect);
166     }
167
168     return enclosingIntRect(selectionRect);
169 }
170
171 void SVGInlineTextBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit, LayoutUnit)
172 {
173
174     SVGInlineTextBoxPainter(*this).paint(paintInfo, paintOffset);
175 }
176
177 TextRun SVGInlineTextBox::constructTextRun(RenderStyle* style, const SVGTextFragment& fragment) const
178 {
179     ASSERT(style);
180
181     RenderText* text = &renderer();
182
183     // FIXME(crbug.com/264211): This should not be necessary but can occur if we
184     //                          layout during layout. Remove this when 264211 is fixed.
185     RELEASE_ASSERT(!text->needsLayout());
186
187     TextRun run(static_cast<const LChar*>(0) // characters, will be set below if non-zero.
188                 , 0 // length, will be set below if non-zero.
189                 , 0 // xPos, only relevant with allowTabs=true
190                 , 0 // padding, only relevant for justified text, not relevant for SVG
191                 , TextRun::AllowTrailingExpansion
192                 , direction()
193                 , dirOverride() || style->rtlOrdering() == VisualOrder /* directionalOverride */);
194
195     if (fragment.length) {
196         if (text->is8Bit())
197             run.setText(text->characters8() + fragment.characterOffset, fragment.length);
198         else
199             run.setText(text->characters16() + fragment.characterOffset, fragment.length);
200     }
201
202     // We handle letter & word spacing ourselves.
203     run.disableSpacing();
204
205     // Propagate the maximum length of the characters buffer to the TextRun, even when we're only processing a substring.
206     run.setCharactersLength(text->textLength() - fragment.characterOffset);
207     ASSERT(run.charactersLength() >= run.length());
208     return run;
209 }
210
211 bool SVGInlineTextBox::mapStartEndPositionsIntoFragmentCoordinates(const SVGTextFragment& fragment, int& startPosition, int& endPosition) const
212 {
213     if (startPosition >= endPosition)
214         return false;
215
216     int offset = static_cast<int>(fragment.characterOffset) - start();
217     int length = static_cast<int>(fragment.length);
218
219     if (startPosition >= offset + length || endPosition <= offset)
220         return false;
221
222     if (startPosition < offset)
223         startPosition = 0;
224     else
225         startPosition -= offset;
226
227     if (endPosition > offset + length)
228         endPosition = length;
229     else {
230         ASSERT(endPosition >= offset);
231         endPosition -= offset;
232     }
233
234     ASSERT(startPosition < endPosition);
235     return true;
236 }
237
238 void SVGInlineTextBox::paintDocumentMarker(GraphicsContext*, const FloatPoint&, DocumentMarker*, RenderStyle*, const Font&, bool)
239 {
240     // SVG does not have support for generic document markers (e.g., spellchecking, etc).
241 }
242
243 void SVGInlineTextBox::paintTextMatchMarker(GraphicsContext* context, const FloatPoint& point, DocumentMarker* marker, RenderStyle* style, const Font& font)
244 {
245     SVGInlineTextBoxPainter(*this).paintTextMatchMarker(context, point, marker, style, font);
246 }
247
248 FloatRect SVGInlineTextBox::calculateBoundaries() const
249 {
250     FloatRect textRect;
251
252     RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
253
254     float scalingFactor = textRenderer.scalingFactor();
255     ASSERT(scalingFactor);
256
257     float baseline = textRenderer.scaledFont().fontMetrics().floatAscent() / scalingFactor;
258
259     AffineTransform fragmentTransform;
260     unsigned textFragmentsSize = m_textFragments.size();
261     for (unsigned i = 0; i < textFragmentsSize; ++i) {
262         const SVGTextFragment& fragment = m_textFragments.at(i);
263         FloatRect fragmentRect(fragment.x, fragment.y - baseline, fragment.width, fragment.height);
264         fragment.buildFragmentTransform(fragmentTransform);
265         fragmentRect = fragmentTransform.mapRect(fragmentRect);
266
267         textRect.unite(fragmentRect);
268     }
269
270     return textRect;
271 }
272
273 bool SVGInlineTextBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit, LayoutUnit)
274 {
275     // FIXME: integrate with InlineTextBox::nodeAtPoint better.
276     ASSERT(!isLineBreak());
277
278     PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_TEXT_HITTESTING, request, renderer().style()->pointerEvents());
279     bool isVisible = renderer().style()->visibility() == VISIBLE;
280     if (isVisible || !hitRules.requireVisible) {
281         if (hitRules.canHitBoundingBox
282             || (hitRules.canHitStroke && (renderer().style()->svgStyle().hasStroke() || !hitRules.requireStroke))
283             || (hitRules.canHitFill && (renderer().style()->svgStyle().hasFill() || !hitRules.requireFill))) {
284             FloatPoint boxOrigin(x(), y());
285             boxOrigin.moveBy(accumulatedOffset);
286             FloatRect rect(boxOrigin, size());
287             if (locationInContainer.intersects(rect)) {
288                 renderer().updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset));
289                 if (!result.addNodeToRectBasedTestResult(renderer().node(), request, locationInContainer, rect))
290                     return true;
291              }
292         }
293     }
294     return false;
295 }
296
297 } // namespace blink