Bug 1635304 [wpt PR 23392] - Remove erroneous named properties object test, a=testonly
[gecko.git] / dom / svg / SVGTextContentElement.cpp
blob31ab87a19baa90121b093de66889c2c1ce9116f1
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/SVGTextContentElement.h"
9 #include "mozilla/dom/SVGLengthBinding.h"
10 #include "mozilla/dom/SVGTextContentElementBinding.h"
11 #include "mozilla/dom/SVGRect.h"
12 #include "nsBidiUtils.h"
13 #include "nsISVGPoint.h"
14 #include "nsTextFragment.h"
15 #include "nsTextFrameUtils.h"
16 #include "nsTextNode.h"
17 #include "SVGTextFrame.h"
19 namespace mozilla {
20 namespace dom {
22 using namespace SVGTextContentElement_Binding;
24 SVGEnumMapping SVGTextContentElement::sLengthAdjustMap[] = {
25 {nsGkAtoms::spacing, LENGTHADJUST_SPACING},
26 {nsGkAtoms::spacingAndGlyphs, LENGTHADJUST_SPACINGANDGLYPHS},
27 {nullptr, 0}};
29 SVGElement::EnumInfo SVGTextContentElement::sEnumInfo[1] = {
30 {nsGkAtoms::lengthAdjust, sLengthAdjustMap, LENGTHADJUST_SPACING}};
32 SVGElement::LengthInfo SVGTextContentElement::sLengthInfo[1] = {
33 {nsGkAtoms::textLength, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
34 SVGContentUtils::XY}};
36 SVGTextFrame* SVGTextContentElement::GetSVGTextFrame() {
37 nsIFrame* frame = GetPrimaryFrame(FlushType::Layout);
38 nsIFrame* textFrame =
39 nsLayoutUtils::GetClosestFrameOfType(frame, LayoutFrameType::SVGText);
40 return static_cast<SVGTextFrame*>(textFrame);
43 SVGTextFrame*
44 SVGTextContentElement::GetSVGTextFrameForNonLayoutDependentQuery() {
45 nsIFrame* frame = GetPrimaryFrame(FlushType::Frames);
46 nsIFrame* textFrame =
47 nsLayoutUtils::GetClosestFrameOfType(frame, LayoutFrameType::SVGText);
48 return static_cast<SVGTextFrame*>(textFrame);
51 already_AddRefed<DOMSVGAnimatedLength> SVGTextContentElement::TextLength() {
52 return LengthAttributes()[TEXTLENGTH].ToDOMAnimatedLength(this);
55 already_AddRefed<DOMSVGAnimatedEnumeration>
56 SVGTextContentElement::LengthAdjust() {
57 return EnumAttributes()[LENGTHADJUST].ToDOMAnimatedEnum(this);
60 //----------------------------------------------------------------------
62 template <typename T>
63 static bool FragmentHasSkippableCharacter(const T* aBuffer, uint32_t aLength) {
64 for (uint32_t i = 0; i < aLength; i++) {
65 if (nsTextFrameUtils::IsSkippableCharacterForTransformText(aBuffer[i])) {
66 return true;
69 return false;
72 Maybe<int32_t> SVGTextContentElement::GetNonLayoutDependentNumberOfChars() {
73 SVGTextFrame* frame = GetSVGTextFrameForNonLayoutDependentQuery();
74 if (!frame || frame != GetPrimaryFrame()) {
75 // Only support this fast path on <text>, not child <tspan>s, etc.
76 return Some(0);
79 uint32_t num = 0;
81 for (nsINode* n = Element::GetFirstChild(); n; n = n->GetNextSibling()) {
82 if (!n->IsText()) {
83 return Nothing();
86 const nsTextFragment* text = &n->AsText()->TextFragment();
87 uint32_t length = text->GetLength();
89 if (text->Is2b()) {
90 if (FragmentHasSkippableCharacter(text->Get2b(), length)) {
91 return Nothing();
93 } else {
94 auto buffer = reinterpret_cast<const uint8_t*>(text->Get1b());
95 if (FragmentHasSkippableCharacter(buffer, length)) {
96 return Nothing();
100 num += length;
103 return Some(num);
106 int32_t SVGTextContentElement::GetNumberOfChars() {
107 Maybe<int32_t> num = GetNonLayoutDependentNumberOfChars();
108 if (num) {
109 return *num;
112 SVGTextFrame* textFrame = GetSVGTextFrame();
113 return textFrame ? textFrame->GetNumberOfChars(this) : 0;
116 float SVGTextContentElement::GetComputedTextLength() {
117 SVGTextFrame* textFrame = GetSVGTextFrame();
118 return textFrame ? textFrame->GetComputedTextLength(this) : 0.0f;
121 void SVGTextContentElement::SelectSubString(uint32_t charnum, uint32_t nchars,
122 ErrorResult& rv) {
123 SVGTextFrame* textFrame = GetSVGTextFrame();
124 if (!textFrame) return;
126 textFrame->SelectSubString(this, charnum, nchars, rv);
129 float SVGTextContentElement::GetSubStringLength(uint32_t charnum,
130 uint32_t nchars,
131 ErrorResult& rv) {
132 SVGTextFrame* textFrame = GetSVGTextFrameForNonLayoutDependentQuery();
133 if (!textFrame) return 0.0f;
135 return textFrame->GetSubStringLength(this, charnum, nchars, rv);
138 already_AddRefed<nsISVGPoint> SVGTextContentElement::GetStartPositionOfChar(
139 uint32_t charnum, ErrorResult& rv) {
140 SVGTextFrame* textFrame = GetSVGTextFrame();
141 if (!textFrame) {
142 rv.ThrowInvalidStateError("No layout information available for SVG text");
143 return nullptr;
146 return textFrame->GetStartPositionOfChar(this, charnum, rv);
149 already_AddRefed<nsISVGPoint> SVGTextContentElement::GetEndPositionOfChar(
150 uint32_t charnum, ErrorResult& rv) {
151 SVGTextFrame* textFrame = GetSVGTextFrame();
152 if (!textFrame) {
153 rv.ThrowInvalidStateError("No layout information available for SVG text");
154 return nullptr;
157 return textFrame->GetEndPositionOfChar(this, charnum, rv);
160 already_AddRefed<SVGRect> SVGTextContentElement::GetExtentOfChar(
161 uint32_t charnum, ErrorResult& rv) {
162 SVGTextFrame* textFrame = GetSVGTextFrame();
164 if (!textFrame) {
165 rv.ThrowInvalidStateError("No layout information available for SVG text");
166 return nullptr;
169 return textFrame->GetExtentOfChar(this, charnum, rv);
172 float SVGTextContentElement::GetRotationOfChar(uint32_t charnum,
173 ErrorResult& rv) {
174 SVGTextFrame* textFrame = GetSVGTextFrame();
176 if (!textFrame) {
177 rv.ThrowInvalidStateError("No layout information available for SVG text");
178 return 0.0f;
181 return textFrame->GetRotationOfChar(this, charnum, rv);
184 int32_t SVGTextContentElement::GetCharNumAtPosition(
185 const DOMPointInit& aPoint) {
186 SVGTextFrame* textFrame = GetSVGTextFrame();
187 return textFrame ? textFrame->GetCharNumAtPosition(this, aPoint) : -1;
190 } // namespace dom
191 } // namespace mozilla