1 | /* |
2 | * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org> |
3 | * Copyright (C) 2004, 2005, 2006, 2007, 2008 Rob Buis <buis@kde.org> |
4 | * Copyright (C) 2018-2019 Apple Inc. 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 "SVGTextContentElement.h" |
24 | |
25 | #include "CSSPropertyNames.h" |
26 | #include "CSSValueKeywords.h" |
27 | #include "DOMPoint.h" |
28 | #include "Frame.h" |
29 | #include "FrameSelection.h" |
30 | #include "RenderObject.h" |
31 | #include "RenderSVGResource.h" |
32 | #include "RenderSVGText.h" |
33 | #include "SVGNames.h" |
34 | #include "SVGPoint.h" |
35 | #include "SVGRect.h" |
36 | #include "SVGTextQuery.h" |
37 | #include "XMLNames.h" |
38 | #include <wtf/IsoMallocInlines.h> |
39 | #include <wtf/NeverDestroyed.h> |
40 | |
41 | namespace WebCore { |
42 | |
43 | WTF_MAKE_ISO_ALLOCATED_IMPL(SVGTextContentElement); |
44 | |
45 | SVGTextContentElement::SVGTextContentElement(const QualifiedName& tagName, Document& document) |
46 | : SVGGraphicsElement(tagName, document) |
47 | , SVGExternalResourcesRequired(this) |
48 | { |
49 | static std::once_flag onceFlag; |
50 | std::call_once(onceFlag, [] { |
51 | PropertyRegistry::registerProperty<SVGNames::textLengthAttr, &SVGTextContentElement::m_textLength>(); |
52 | PropertyRegistry::registerProperty<SVGNames::lengthAdjustAttr, SVGLengthAdjustType, &SVGTextContentElement::m_lengthAdjust>(); |
53 | }); |
54 | } |
55 | |
56 | unsigned SVGTextContentElement::getNumberOfChars() |
57 | { |
58 | document().updateLayoutIgnorePendingStylesheets(); |
59 | return SVGTextQuery(renderer()).numberOfCharacters(); |
60 | } |
61 | |
62 | float SVGTextContentElement::getComputedTextLength() |
63 | { |
64 | document().updateLayoutIgnorePendingStylesheets(); |
65 | return SVGTextQuery(renderer()).textLength(); |
66 | } |
67 | |
68 | ExceptionOr<float> SVGTextContentElement::getSubStringLength(unsigned charnum, unsigned nchars) |
69 | { |
70 | unsigned numberOfChars = getNumberOfChars(); |
71 | if (charnum >= numberOfChars) |
72 | return Exception { IndexSizeError }; |
73 | |
74 | nchars = std::min(nchars, numberOfChars - charnum); |
75 | return SVGTextQuery(renderer()).subStringLength(charnum, nchars); |
76 | } |
77 | |
78 | ExceptionOr<Ref<SVGPoint>> SVGTextContentElement::getStartPositionOfChar(unsigned charnum) |
79 | { |
80 | if (charnum > getNumberOfChars()) |
81 | return Exception { IndexSizeError }; |
82 | |
83 | return SVGPoint::create(SVGTextQuery(renderer()).startPositionOfCharacter(charnum)); |
84 | } |
85 | |
86 | ExceptionOr<Ref<SVGPoint>> SVGTextContentElement::getEndPositionOfChar(unsigned charnum) |
87 | { |
88 | if (charnum > getNumberOfChars()) |
89 | return Exception { IndexSizeError }; |
90 | |
91 | return SVGPoint::create(SVGTextQuery(renderer()).endPositionOfCharacter(charnum)); |
92 | } |
93 | |
94 | ExceptionOr<Ref<SVGRect>> SVGTextContentElement::getExtentOfChar(unsigned charnum) |
95 | { |
96 | if (charnum > getNumberOfChars()) |
97 | return Exception { IndexSizeError }; |
98 | |
99 | return SVGRect::create(SVGTextQuery(renderer()).extentOfCharacter(charnum)); |
100 | } |
101 | |
102 | ExceptionOr<float> SVGTextContentElement::getRotationOfChar(unsigned charnum) |
103 | { |
104 | if (charnum > getNumberOfChars()) |
105 | return Exception { IndexSizeError }; |
106 | |
107 | return SVGTextQuery(renderer()).rotationOfCharacter(charnum); |
108 | } |
109 | |
110 | int SVGTextContentElement::getCharNumAtPosition(DOMPointInit&& pointInit) |
111 | { |
112 | document().updateLayoutIgnorePendingStylesheets(); |
113 | FloatPoint transformPoint {static_cast<float>(pointInit.x), static_cast<float>(pointInit.y)}; |
114 | return SVGTextQuery(renderer()).characterNumberAtPosition(transformPoint); |
115 | } |
116 | |
117 | ExceptionOr<void> SVGTextContentElement::selectSubString(unsigned charnum, unsigned nchars) |
118 | { |
119 | unsigned numberOfChars = getNumberOfChars(); |
120 | if (charnum >= numberOfChars) |
121 | return Exception { IndexSizeError }; |
122 | |
123 | nchars = std::min(nchars, numberOfChars - charnum); |
124 | |
125 | ASSERT(document().frame()); |
126 | |
127 | FrameSelection& selection = document().frame()->selection(); |
128 | |
129 | // Find selection start |
130 | VisiblePosition start(firstPositionInNode(const_cast<SVGTextContentElement*>(this))); |
131 | for (unsigned i = 0; i < charnum; ++i) |
132 | start = start.next(); |
133 | |
134 | // Find selection end |
135 | VisiblePosition end(start); |
136 | for (unsigned i = 0; i < nchars; ++i) |
137 | end = end.next(); |
138 | |
139 | selection.setSelection(VisibleSelection(start, end)); |
140 | |
141 | return { }; |
142 | } |
143 | |
144 | bool SVGTextContentElement::isPresentationAttribute(const QualifiedName& name) const |
145 | { |
146 | if (name.matches(XMLNames::spaceAttr)) |
147 | return true; |
148 | return SVGGraphicsElement::isPresentationAttribute(name); |
149 | } |
150 | |
151 | void SVGTextContentElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStyleProperties& style) |
152 | { |
153 | if (name.matches(XMLNames::spaceAttr)) { |
154 | if (value == "preserve" ) |
155 | addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePre); |
156 | else |
157 | addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValueNowrap); |
158 | return; |
159 | } |
160 | |
161 | SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style); |
162 | } |
163 | |
164 | void SVGTextContentElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
165 | { |
166 | SVGParsingError parseError = NoError; |
167 | |
168 | if (name == SVGNames::lengthAdjustAttr) { |
169 | auto propertyValue = SVGPropertyTraits<SVGLengthAdjustType>::fromString(value); |
170 | if (propertyValue > 0) |
171 | m_lengthAdjust->setBaseValInternal<SVGLengthAdjustType>(propertyValue); |
172 | } else if (name == SVGNames::textLengthAttr) |
173 | m_textLength->setBaseValInternal(SVGLengthValue::construct(LengthModeOther, value, parseError, ForbidNegativeLengths)); |
174 | |
175 | reportAttributeParsingError(parseError, name, value); |
176 | |
177 | SVGGraphicsElement::parseAttribute(name, value); |
178 | SVGExternalResourcesRequired::parseAttribute(name, value); |
179 | } |
180 | |
181 | void SVGTextContentElement::svgAttributeChanged(const QualifiedName& attrName) |
182 | { |
183 | if (PropertyRegistry::isKnownAttribute(attrName)) { |
184 | if (attrName == SVGNames::textLengthAttr) |
185 | m_specifiedTextLength = m_textLength->baseVal()->value(); |
186 | |
187 | if (auto renderer = this->renderer()) { |
188 | InstanceInvalidationGuard guard(*this); |
189 | RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer); |
190 | } |
191 | return; |
192 | } |
193 | |
194 | SVGGraphicsElement::svgAttributeChanged(attrName); |
195 | SVGExternalResourcesRequired::svgAttributeChanged(attrName); |
196 | } |
197 | |
198 | SVGAnimatedLength& SVGTextContentElement::textLengthAnimated() |
199 | { |
200 | static NeverDestroyed<SVGLengthValue> defaultTextLength(LengthModeOther); |
201 | if (m_textLength->baseVal()->value() == defaultTextLength) |
202 | m_textLength->baseVal()->value().newValueSpecifiedUnits(LengthTypeNumber, getComputedTextLength()); |
203 | return m_textLength; |
204 | } |
205 | |
206 | bool SVGTextContentElement::selfHasRelativeLengths() const |
207 | { |
208 | // Any element of the <text> subtree is advertized as using relative lengths. |
209 | // On any window size change, we have to relayout the text subtree, as the |
210 | // effective 'on-screen' font size may change. |
211 | return true; |
212 | } |
213 | |
214 | SVGTextContentElement* SVGTextContentElement::elementFromRenderer(RenderObject* renderer) |
215 | { |
216 | if (!renderer) |
217 | return nullptr; |
218 | |
219 | if (!renderer->isSVGText() && !renderer->isSVGInline()) |
220 | return nullptr; |
221 | |
222 | SVGElement* element = downcast<SVGElement>(renderer->node()); |
223 | ASSERT(element); |
224 | |
225 | if (!is<SVGTextContentElement>(element)) |
226 | return nullptr; |
227 | |
228 | return downcast<SVGTextContentElement>(element); |
229 | } |
230 | |
231 | } |
232 | |