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.
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.
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.
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.
23 #include "core/rendering/svg/SVGInlineTextBox.h"
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"
42 struct ExpectedSVGInlineTextBoxSize : public InlineTextBox {
44 uint32_t bitfields : 1;
45 Vector<SVGTextFragment> vector;
48 COMPILE_ASSERT(sizeof(SVGInlineTextBox) == sizeof(ExpectedSVGInlineTextBoxSize), SVGInlineTextBox_is_not_of_expected_size);
50 SVGInlineTextBox::SVGInlineTextBox(RenderObject& object, int start, unsigned short length)
51 : InlineTextBox(object, start, length)
53 , m_startsNewTextChunk(false)
57 void SVGInlineTextBox::dirtyLineBoxes()
59 InlineTextBox::dirtyLineBoxes();
61 // Clear the now stale text fragments
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();
68 nextBox->dirtyLineBoxes();
71 int SVGInlineTextBox::offsetForPosition(float, bool) const
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.)
79 int SVGInlineTextBox::offsetForPositionInFragment(const SVGTextFragment& fragment, float position, bool includePartialGlyphs) const
81 RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
83 float scalingFactor = textRenderer.scalingFactor();
84 ASSERT(scalingFactor);
86 RenderStyle* style = textRenderer.style();
89 TextRun textRun = constructTextRun(style, fragment);
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()));
98 return fragment.characterOffset - start() + textRenderer.scaledFont().offsetForPosition(textRun, position * scalingFactor, includePartialGlyphs);
101 float SVGInlineTextBox::positionForOffset(int) const
103 // SVG doesn't use the offset <-> position selection system.
104 ASSERT_NOT_REACHED();
108 FloatRect SVGInlineTextBox::selectionRectForTextFragment(const SVGTextFragment& fragment, int startPosition, int endPosition, RenderStyle* style)
110 ASSERT(startPosition < endPosition);
113 FontCachePurgePreventer fontCachePurgePreventer;
115 RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
117 float scalingFactor = textRenderer.scalingFactor();
118 ASSERT(scalingFactor);
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);
126 textOrigin.move(0, -scaledFontMetrics.floatAscent());
128 FloatRect selectionRect = scaledFont.selectionRectForText(constructTextRun(style, fragment), textOrigin, fragment.height * scalingFactor, startPosition, endPosition);
129 if (scalingFactor == 1)
130 return selectionRect;
132 selectionRect.scale(1 / scalingFactor);
133 return selectionRect;
136 LayoutRect SVGInlineTextBox::localSelectionRect(int startPosition, int endPosition)
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)
144 RenderStyle* style = renderer().style();
147 AffineTransform fragmentTransform;
148 FloatRect selectionRect;
149 int fragmentStartPosition = 0;
150 int fragmentEndPosition = 0;
152 unsigned textFragmentsSize = m_textFragments.size();
153 for (unsigned i = 0; i < textFragmentsSize; ++i) {
154 const SVGTextFragment& fragment = m_textFragments.at(i);
156 fragmentStartPosition = startPosition;
157 fragmentEndPosition = endPosition;
158 if (!mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
161 FloatRect fragmentRect = selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style);
162 fragment.buildFragmentTransform(fragmentTransform);
163 fragmentRect = fragmentTransform.mapRect(fragmentRect);
165 selectionRect.unite(fragmentRect);
168 return enclosingIntRect(selectionRect);
171 void SVGInlineTextBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit, LayoutUnit)
174 SVGInlineTextBoxPainter(*this).paint(paintInfo, paintOffset);
177 TextRun SVGInlineTextBox::constructTextRun(RenderStyle* style, const SVGTextFragment& fragment) const
181 RenderText* text = &renderer();
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());
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
193 , dirOverride() || style->rtlOrdering() == VisualOrder /* directionalOverride */);
195 if (fragment.length) {
197 run.setText(text->characters8() + fragment.characterOffset, fragment.length);
199 run.setText(text->characters16() + fragment.characterOffset, fragment.length);
202 // We handle letter & word spacing ourselves.
203 run.disableSpacing();
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());
211 bool SVGInlineTextBox::mapStartEndPositionsIntoFragmentCoordinates(const SVGTextFragment& fragment, int& startPosition, int& endPosition) const
213 if (startPosition >= endPosition)
216 int offset = static_cast<int>(fragment.characterOffset) - start();
217 int length = static_cast<int>(fragment.length);
219 if (startPosition >= offset + length || endPosition <= offset)
222 if (startPosition < offset)
225 startPosition -= offset;
227 if (endPosition > offset + length)
228 endPosition = length;
230 ASSERT(endPosition >= offset);
231 endPosition -= offset;
234 ASSERT(startPosition < endPosition);
238 void SVGInlineTextBox::paintDocumentMarker(GraphicsContext*, const FloatPoint&, DocumentMarker*, RenderStyle*, const Font&, bool)
240 // SVG does not have support for generic document markers (e.g., spellchecking, etc).
243 void SVGInlineTextBox::paintTextMatchMarker(GraphicsContext* context, const FloatPoint& point, DocumentMarker* marker, RenderStyle* style, const Font& font)
245 SVGInlineTextBoxPainter(*this).paintTextMatchMarker(context, point, marker, style, font);
248 FloatRect SVGInlineTextBox::calculateBoundaries() const
252 RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
254 float scalingFactor = textRenderer.scalingFactor();
255 ASSERT(scalingFactor);
257 float baseline = textRenderer.scaledFont().fontMetrics().floatAscent() / scalingFactor;
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);
267 textRect.unite(fragmentRect);
273 bool SVGInlineTextBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit, LayoutUnit)
275 // FIXME: integrate with InlineTextBox::nodeAtPoint better.
276 ASSERT(!isLineBreak());
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))