1/*
2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 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#pragma once
23
24#include "SVGExternalResourcesRequired.h"
25#include "SVGGraphicsElement.h"
26
27namespace WebCore {
28
29struct DOMPointInit;
30
31enum SVGLengthAdjustType {
32 SVGLengthAdjustUnknown,
33 SVGLengthAdjustSpacing,
34 SVGLengthAdjustSpacingAndGlyphs
35};
36
37template<> struct SVGPropertyTraits<SVGLengthAdjustType> {
38 static unsigned highestEnumValue() { return SVGLengthAdjustSpacingAndGlyphs; }
39
40 static String toString(SVGLengthAdjustType type)
41 {
42 switch (type) {
43 case SVGLengthAdjustUnknown:
44 return emptyString();
45 case SVGLengthAdjustSpacing:
46 return "spacing"_s;
47 case SVGLengthAdjustSpacingAndGlyphs:
48 return "spacingAndGlyphs"_s;
49 }
50
51 ASSERT_NOT_REACHED();
52 return emptyString();
53 }
54
55 static SVGLengthAdjustType fromString(const String& value)
56 {
57 if (value == "spacingAndGlyphs")
58 return SVGLengthAdjustSpacingAndGlyphs;
59 if (value == "spacing")
60 return SVGLengthAdjustSpacing;
61 return SVGLengthAdjustUnknown;
62 }
63};
64
65class SVGTextContentElement : public SVGGraphicsElement, public SVGExternalResourcesRequired {
66 WTF_MAKE_ISO_ALLOCATED(SVGTextContentElement);
67public:
68 enum {
69 LENGTHADJUST_UNKNOWN = SVGLengthAdjustUnknown,
70 LENGTHADJUST_SPACING = SVGLengthAdjustSpacing,
71 LENGTHADJUST_SPACINGANDGLYPHS = SVGLengthAdjustSpacingAndGlyphs
72 };
73
74 unsigned getNumberOfChars();
75 float getComputedTextLength();
76 ExceptionOr<float> getSubStringLength(unsigned charnum, unsigned nchars);
77 ExceptionOr<Ref<SVGPoint>> getStartPositionOfChar(unsigned charnum);
78 ExceptionOr<Ref<SVGPoint>> getEndPositionOfChar(unsigned charnum);
79 ExceptionOr<Ref<SVGRect>> getExtentOfChar(unsigned charnum);
80 ExceptionOr<float> getRotationOfChar(unsigned charnum);
81 int getCharNumAtPosition(DOMPointInit&&);
82 ExceptionOr<void> selectSubString(unsigned charnum, unsigned nchars);
83
84 static SVGTextContentElement* elementFromRenderer(RenderObject*);
85
86 using PropertyRegistry = SVGPropertyOwnerRegistry<SVGTextContentElement, SVGGraphicsElement, SVGExternalResourcesRequired>;
87
88 const SVGLengthValue& specifiedTextLength() const { return m_specifiedTextLength; }
89 const SVGLengthValue& textLength() const { return m_textLength->currentValue(); }
90 SVGLengthAdjustType lengthAdjust() const { return m_lengthAdjust->currentValue<SVGLengthAdjustType>(); }
91
92 SVGAnimatedLength& textLengthAnimated();
93 SVGAnimatedEnumeration& lengthAdjustAnimated() { return m_lengthAdjust; }
94
95protected:
96 SVGTextContentElement(const QualifiedName&, Document&);
97
98 bool isValid() const override { return SVGTests::isValid(); }
99
100 void parseAttribute(const QualifiedName&, const AtomicString&) override;
101 bool isPresentationAttribute(const QualifiedName&) const override;
102 void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStyleProperties&) override;
103 void svgAttributeChanged(const QualifiedName&) override;
104
105 bool selfHasRelativeLengths() const override;
106
107private:
108 bool isTextContent() const final { return true; }
109
110 Ref<SVGAnimatedLength> m_textLength { SVGAnimatedLength::create(this, LengthModeOther) };
111 Ref<SVGAnimatedEnumeration> m_lengthAdjust { SVGAnimatedEnumeration::create(this, SVGLengthAdjustSpacing) };
112 SVGLengthValue m_specifiedTextLength { LengthModeOther };
113};
114
115} // namespace WebCore
116
117SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::SVGTextContentElement)
118 static bool isType(const WebCore::SVGElement& element) { return element.isTextContent(); }
119 static bool isType(const WebCore::Node& node) { return is<WebCore::SVGElement>(node) && isType(downcast<WebCore::SVGElement>(node)); }
120SPECIALIZE_TYPE_TRAITS_END()
121