2 * Copyright (C) 2006 Oliver Hunt <ojh16@student.canterbury.ac.nz>
3 * Copyright (C) 2006 Apple Computer Inc.
4 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
5 * Copyright (C) 2008 Rob Buis <buis@kde.org>
6 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
27 #include "RenderSVGInlineText.h"
29 #include "CSSFontSelector.h"
30 #include "CSSStyleSelector.h"
31 #include "FloatConversion.h"
32 #include "FloatQuad.h"
33 #include "RenderBlock.h"
34 #include "RenderSVGRoot.h"
35 #include "RenderSVGText.h"
37 #include "SVGImageBufferTools.h"
38 #include "SVGInlineTextBox.h"
39 #include "SVGRootInlineBox.h"
40 #include "VisiblePosition.h"
44 static PassRefPtr<StringImpl> applySVGWhitespaceRules(PassRefPtr<StringImpl> string, bool preserveWhiteSpace)
46 if (preserveWhiteSpace) {
47 // Spec: When xml:space="preserve", the SVG user agent will do the following using a
48 // copy of the original character data content. It will convert all newline and tab
49 // characters into space characters. Then, it will draw all space characters, including
50 // leading, trailing and multiple contiguous space characters.
51 RefPtr<StringImpl> newString = string->replace('\t', ' ');
52 newString = newString->replace('\n', ' ');
53 newString = newString->replace('\r', ' ');
54 return newString.release();
57 // Spec: When xml:space="default", the SVG user agent will do the following using a
58 // copy of the original character data content. First, it will remove all newline
59 // characters. Then it will convert all tab characters into space characters.
60 // Then, it will strip off all leading and trailing space characters.
61 // Then, all contiguous space characters will be consolidated.
62 RefPtr<StringImpl> newString = string->replace('\n', StringImpl::empty());
63 newString = newString->replace('\r', StringImpl::empty());
64 newString = newString->replace('\t', ' ');
65 return newString.release();
68 RenderSVGInlineText::RenderSVGInlineText(Node* n, PassRefPtr<StringImpl> string)
69 : RenderText(n, applySVGWhitespaceRules(string, false))
74 void RenderSVGInlineText::willBeDestroyed()
76 if (RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(this))
77 textRenderer->setNeedsPositioningValuesUpdate();
79 RenderText::willBeDestroyed();
82 void RenderSVGInlineText::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
84 RenderText::styleDidChange(diff, oldStyle);
86 if (diff == StyleDifferenceLayout) {
87 // The text metrics may be influenced by style changes.
88 if (RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(this))
89 textRenderer->setNeedsPositioningValuesUpdate();
94 const RenderStyle* newStyle = style();
95 if (!newStyle || newStyle->whiteSpace() != PRE)
98 if (!oldStyle || oldStyle->whiteSpace() != PRE)
99 setText(applySVGWhitespaceRules(originalText(), true), true);
102 InlineTextBox* RenderSVGInlineText::createTextBox()
104 InlineTextBox* box = new (renderArena()) SVGInlineTextBox(this);
105 box->setHasVirtualLogicalHeight();
109 IntRect RenderSVGInlineText::localCaretRect(InlineBox* box, int caretOffset, int*)
111 if (!box->isInlineTextBox())
114 InlineTextBox* textBox = static_cast<InlineTextBox*>(box);
115 if (static_cast<unsigned>(caretOffset) < textBox->start() || static_cast<unsigned>(caretOffset) > textBox->start() + textBox->len())
118 // Use the edge of the selection rect to determine the caret rect.
119 if (static_cast<unsigned>(caretOffset) < textBox->start() + textBox->len()) {
120 IntRect rect = textBox->localSelectionRect(caretOffset, caretOffset + 1);
121 int x = box->isLeftToRightDirection() ? rect.x() : rect.maxX();
122 return IntRect(x, rect.y(), caretWidth, rect.height());
125 IntRect rect = textBox->localSelectionRect(caretOffset - 1, caretOffset);
126 int x = box->isLeftToRightDirection() ? rect.maxX() : rect.x();
127 return IntRect(x, rect.y(), caretWidth, rect.height());
130 LayoutRect RenderSVGInlineText::linesBoundingBox() const
132 LayoutRect boundingBox;
133 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
134 boundingBox.unite(box->calculateBoundaries());
138 bool RenderSVGInlineText::characterStartsNewTextChunk(int position) const
140 ASSERT(m_attributes.xValues().size() == textLength());
141 ASSERT(m_attributes.yValues().size() == textLength());
142 ASSERT(position >= 0);
143 ASSERT(position < static_cast<int>(textLength()));
145 // Each <textPath> element starts a new text chunk, regardless of any x/y values.
146 if (!position && parent()->isSVGTextPath() && !previousSibling())
149 int currentPosition = 0;
150 unsigned size = m_attributes.textMetricsValues().size();
151 for (unsigned i = 0; i < size; ++i) {
152 const SVGTextMetrics& metrics = m_attributes.textMetricsValues().at(i);
154 // We found the desired character.
155 if (currentPosition == position) {
156 return m_attributes.xValues().at(position) != SVGTextLayoutAttributes::emptyValue()
157 || m_attributes.yValues().at(position) != SVGTextLayoutAttributes::emptyValue();
160 currentPosition += metrics.length();
161 if (currentPosition > position)
165 // The desired position is available in the x/y list, but not in the character data values list.
166 // That means the previous character data described a single glyph, consisting of multiple unicode characters.
167 // The consequence is that the desired character does not define a new absolute x/y position, even if present in the x/y test.
168 // This code is tested by svg/W3C-SVG-1.1/text-text-06-t.svg (and described in detail, why this influences chunk detection).
172 VisiblePosition RenderSVGInlineText::positionForPoint(const LayoutPoint& point)
174 if (!firstTextBox() || !textLength())
175 return createVisiblePosition(0, DOWNSTREAM);
177 float baseline = m_scaledFont.fontMetrics().floatAscent();
179 RenderBlock* containingBlock = this->containingBlock();
180 ASSERT(containingBlock);
182 // Map local point to absolute point, as the character origins stored in the text fragments use absolute coordinates.
183 FloatPoint absolutePoint(point);
184 absolutePoint.moveBy(containingBlock->location());
186 float closestDistance = std::numeric_limits<float>::max();
187 float closestDistancePosition = 0;
188 const SVGTextFragment* closestDistanceFragment = 0;
189 SVGInlineTextBox* closestDistanceBox = 0;
191 AffineTransform fragmentTransform;
192 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
193 if (!box->isSVGInlineTextBox())
196 SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(box);
197 Vector<SVGTextFragment>& fragments = textBox->textFragments();
199 unsigned textFragmentsSize = fragments.size();
200 for (unsigned i = 0; i < textFragmentsSize; ++i) {
201 const SVGTextFragment& fragment = fragments.at(i);
202 FloatRect fragmentRect(fragment.x, fragment.y - baseline, fragment.width, fragment.height);
203 fragment.buildFragmentTransform(fragmentTransform);
204 if (!fragmentTransform.isIdentity())
205 fragmentRect = fragmentTransform.mapRect(fragmentRect);
207 float distance = powf(fragmentRect.x() - absolutePoint.x(), 2) +
208 powf(fragmentRect.y() + fragmentRect.height() / 2 - absolutePoint.y(), 2);
210 if (distance < closestDistance) {
211 closestDistance = distance;
212 closestDistanceBox = textBox;
213 closestDistanceFragment = &fragment;
214 closestDistancePosition = fragmentRect.x();
219 if (!closestDistanceFragment)
220 return createVisiblePosition(0, DOWNSTREAM);
222 int offset = closestDistanceBox->offsetForPositionInFragment(*closestDistanceFragment, absolutePoint.x() - closestDistancePosition, true);
223 return createVisiblePosition(offset + closestDistanceBox->start(), offset > 0 ? VP_UPSTREAM_IF_POSSIBLE : DOWNSTREAM);
226 void RenderSVGInlineText::updateScaledFont()
228 computeNewScaledFontForStyle(this, style(), m_scalingFactor, m_scaledFont);
231 void RenderSVGInlineText::computeNewScaledFontForStyle(RenderObject* renderer, const RenderStyle* style, float& scalingFactor, Font& scaledFont)
236 Document* document = renderer->document();
239 CSSStyleSelector* styleSelector = document->styleSelector();
240 ASSERT(styleSelector);
242 // Alter font-size to the right on-screen value to avoid scaling the glyphs themselves, except when GeometricPrecision is specified
244 SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(renderer, ctm);
245 scalingFactor = narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
246 if (scalingFactor == 1 || !scalingFactor || style->fontDescription().textRenderingMode() == GeometricPrecision) {
248 scaledFont = style->font();
252 FontDescription fontDescription(style->fontDescription());
253 fontDescription.setComputedSize(CSSStyleSelector::getComputedSizeFromSpecifiedSize(document, scalingFactor, fontDescription.isAbsoluteSize(), fontDescription.computedSize(), DoNotUseSmartMinimumForFontSize));
255 scaledFont = Font(fontDescription, 0, 0);
256 scaledFont.update(styleSelector->fontSelector());
261 #endif // ENABLE(SVG)