initial import
[vuplus_webkit] / Source / WebCore / rendering / svg / RenderSVGInlineText.cpp
1 /*
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.
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include "config.h"
25
26 #if ENABLE(SVG)
27 #include "RenderSVGInlineText.h"
28
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"
36 #include "Settings.h"
37 #include "SVGImageBufferTools.h"
38 #include "SVGInlineTextBox.h"
39 #include "SVGRootInlineBox.h"
40 #include "VisiblePosition.h"
41
42 namespace WebCore {
43
44 static PassRefPtr<StringImpl> applySVGWhitespaceRules(PassRefPtr<StringImpl> string, bool preserveWhiteSpace)
45 {
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();
55     }
56
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();
66 }
67
68 RenderSVGInlineText::RenderSVGInlineText(Node* n, PassRefPtr<StringImpl> string)
69     : RenderText(n, applySVGWhitespaceRules(string, false))
70     , m_scalingFactor(1)
71 {
72 }
73
74 void RenderSVGInlineText::willBeDestroyed()
75 {
76     if (RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(this))
77         textRenderer->setNeedsPositioningValuesUpdate();
78
79     RenderText::willBeDestroyed();
80 }
81
82 void RenderSVGInlineText::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
83 {
84     RenderText::styleDidChange(diff, oldStyle);
85
86     if (diff == StyleDifferenceLayout) {
87         // The text metrics may be influenced by style changes.
88         if (RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(this))
89             textRenderer->setNeedsPositioningValuesUpdate();
90
91         updateScaledFont();
92     }
93
94     const RenderStyle* newStyle = style();
95     if (!newStyle || newStyle->whiteSpace() != PRE)
96         return;
97
98     if (!oldStyle || oldStyle->whiteSpace() != PRE)
99         setText(applySVGWhitespaceRules(originalText(), true), true);
100 }
101
102 InlineTextBox* RenderSVGInlineText::createTextBox()
103 {
104     InlineTextBox* box = new (renderArena()) SVGInlineTextBox(this);
105     box->setHasVirtualLogicalHeight();
106     return box;
107 }
108
109 IntRect RenderSVGInlineText::localCaretRect(InlineBox* box, int caretOffset, int*)
110 {
111     if (!box->isInlineTextBox())
112         return IntRect();
113
114     InlineTextBox* textBox = static_cast<InlineTextBox*>(box);
115     if (static_cast<unsigned>(caretOffset) < textBox->start() || static_cast<unsigned>(caretOffset) > textBox->start() + textBox->len())
116         return IntRect();
117
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());
123     }
124
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());
128 }
129
130 LayoutRect RenderSVGInlineText::linesBoundingBox() const
131 {
132     LayoutRect boundingBox;
133     for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
134         boundingBox.unite(box->calculateBoundaries());
135     return boundingBox;
136 }
137
138 bool RenderSVGInlineText::characterStartsNewTextChunk(int position) const
139 {
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()));
144
145     // Each <textPath> element starts a new text chunk, regardless of any x/y values.
146     if (!position && parent()->isSVGTextPath() && !previousSibling())
147         return true;
148
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);
153
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();
158         }
159
160         currentPosition += metrics.length();
161         if (currentPosition > position)
162             break;
163     }
164
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).
169     return false;
170 }
171
172 VisiblePosition RenderSVGInlineText::positionForPoint(const LayoutPoint& point)
173 {
174     if (!firstTextBox() || !textLength())
175         return createVisiblePosition(0, DOWNSTREAM);
176
177     float baseline = m_scaledFont.fontMetrics().floatAscent();
178
179     RenderBlock* containingBlock = this->containingBlock();
180     ASSERT(containingBlock);
181
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());
185
186     float closestDistance = std::numeric_limits<float>::max();
187     float closestDistancePosition = 0;
188     const SVGTextFragment* closestDistanceFragment = 0;
189     SVGInlineTextBox* closestDistanceBox = 0;
190
191     AffineTransform fragmentTransform;
192     for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
193         if (!box->isSVGInlineTextBox())
194             continue;
195
196         SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(box);
197         Vector<SVGTextFragment>& fragments = textBox->textFragments();
198
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);
206
207             float distance = powf(fragmentRect.x() - absolutePoint.x(), 2) +
208                              powf(fragmentRect.y() + fragmentRect.height() / 2 - absolutePoint.y(), 2);
209
210             if (distance < closestDistance) {
211                 closestDistance = distance;
212                 closestDistanceBox = textBox;
213                 closestDistanceFragment = &fragment;
214                 closestDistancePosition = fragmentRect.x();
215             }
216         }
217     }
218
219     if (!closestDistanceFragment)
220         return createVisiblePosition(0, DOWNSTREAM);
221
222     int offset = closestDistanceBox->offsetForPositionInFragment(*closestDistanceFragment, absolutePoint.x() - closestDistancePosition, true);
223     return createVisiblePosition(offset + closestDistanceBox->start(), offset > 0 ? VP_UPSTREAM_IF_POSSIBLE : DOWNSTREAM);
224 }
225
226 void RenderSVGInlineText::updateScaledFont()
227 {
228     computeNewScaledFontForStyle(this, style(), m_scalingFactor, m_scaledFont);
229 }
230
231 void RenderSVGInlineText::computeNewScaledFontForStyle(RenderObject* renderer, const RenderStyle* style, float& scalingFactor, Font& scaledFont)
232 {
233     ASSERT(style);
234     ASSERT(renderer);
235
236     Document* document = renderer->document();
237     ASSERT(document);
238     
239     CSSStyleSelector* styleSelector = document->styleSelector();
240     ASSERT(styleSelector);
241
242     // Alter font-size to the right on-screen value to avoid scaling the glyphs themselves, except when GeometricPrecision is specified
243     AffineTransform ctm;
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) {
247         scalingFactor = 1;
248         scaledFont = style->font();
249         return;
250     }
251
252     FontDescription fontDescription(style->fontDescription());
253     fontDescription.setComputedSize(CSSStyleSelector::getComputedSizeFromSpecifiedSize(document, scalingFactor, fontDescription.isAbsoluteSize(), fontDescription.computedSize(), DoNotUseSmartMinimumForFontSize));
254
255     scaledFont = Font(fontDescription, 0, 0);
256     scaledFont.update(styleSelector->fontSelector());
257 }
258
259 }
260
261 #endif // ENABLE(SVG)