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 "DOMSVGPoint.h"
14 #include "nsLayoutUtils.h"
15 #include "nsTextFragment.h"
16 #include "nsTextFrameUtils.h"
17 #include "nsTextNode.h"
18 #include "SVGTextFrame.h"
20 namespace mozilla::dom
{
22 using namespace SVGTextContentElement_Binding
;
24 SVGEnumMapping
SVGTextContentElement::sLengthAdjustMap
[] = {
25 {nsGkAtoms::spacing
, LENGTHADJUST_SPACING
},
26 {nsGkAtoms::spacingAndGlyphs
, LENGTHADJUST_SPACINGANDGLYPHS
},
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
);
39 nsLayoutUtils::GetClosestFrameOfType(frame
, LayoutFrameType::SVGText
);
40 return static_cast<SVGTextFrame
*>(textFrame
);
44 SVGTextContentElement::GetSVGTextFrameForNonLayoutDependentQuery() {
45 nsIFrame
* frame
= GetPrimaryFrame(FlushType::Frames
);
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 //----------------------------------------------------------------------
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
])) {
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.
81 for (nsINode
* n
= Element::GetFirstChild(); n
; n
= n
->GetNextSibling()) {
86 const nsTextFragment
* text
= &n
->AsText()->TextFragment();
87 uint32_t length
= text
->GetLength();
90 if (FragmentHasSkippableCharacter(text
->Get2b(), length
)) {
94 auto buffer
= reinterpret_cast<const uint8_t*>(text
->Get1b());
95 if (FragmentHasSkippableCharacter(buffer
, length
)) {
106 int32_t SVGTextContentElement::GetNumberOfChars() {
107 Maybe
<int32_t> num
= GetNonLayoutDependentNumberOfChars();
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
,
123 SVGTextFrame
* textFrame
= GetSVGTextFrame();
124 if (!textFrame
) return;
126 textFrame
->SelectSubString(this, charnum
, nchars
, rv
);
129 float SVGTextContentElement::GetSubStringLength(uint32_t charnum
,
132 SVGTextFrame
* textFrame
= GetSVGTextFrameForNonLayoutDependentQuery();
133 if (!textFrame
) return 0.0f
;
135 if (!textFrame
->RequiresSlowFallbackForSubStringLength()) {
136 return textFrame
->GetSubStringLengthFastPath(this, charnum
, nchars
, rv
);
138 // We need to make sure that we've been reflowed before using the slow
139 // fallback path as it may affect glyph positioning. GetSVGTextFrame will do
141 // XXX perf: It may be possible to limit reflow to just calling ReflowSVG,
142 // but we would still need to resort to full reflow for percentage
143 // positioning attributes. For now we just do a full reflow regardless
144 // since the cases that would cause us to be called are relatively uncommon.
145 textFrame
= GetSVGTextFrame();
146 if (!textFrame
) return 0.0f
;
148 return textFrame
->GetSubStringLengthSlowFallback(this, charnum
, nchars
, rv
);
151 already_AddRefed
<DOMSVGPoint
> SVGTextContentElement::GetStartPositionOfChar(
152 uint32_t charnum
, ErrorResult
& rv
) {
153 SVGTextFrame
* textFrame
= GetSVGTextFrame();
155 rv
.ThrowInvalidStateError("No layout information available for SVG text");
159 return textFrame
->GetStartPositionOfChar(this, charnum
, rv
);
162 already_AddRefed
<DOMSVGPoint
> SVGTextContentElement::GetEndPositionOfChar(
163 uint32_t charnum
, ErrorResult
& rv
) {
164 SVGTextFrame
* textFrame
= GetSVGTextFrame();
166 rv
.ThrowInvalidStateError("No layout information available for SVG text");
170 return textFrame
->GetEndPositionOfChar(this, charnum
, rv
);
173 already_AddRefed
<SVGRect
> SVGTextContentElement::GetExtentOfChar(
174 uint32_t charnum
, ErrorResult
& rv
) {
175 SVGTextFrame
* textFrame
= GetSVGTextFrame();
178 rv
.ThrowInvalidStateError("No layout information available for SVG text");
182 return textFrame
->GetExtentOfChar(this, charnum
, rv
);
185 float SVGTextContentElement::GetRotationOfChar(uint32_t charnum
,
187 SVGTextFrame
* textFrame
= GetSVGTextFrame();
190 rv
.ThrowInvalidStateError("No layout information available for SVG text");
194 return textFrame
->GetRotationOfChar(this, charnum
, rv
);
197 int32_t SVGTextContentElement::GetCharNumAtPosition(
198 const DOMPointInit
& aPoint
) {
199 SVGTextFrame
* textFrame
= GetSVGTextFrame();
200 return textFrame
? textFrame
->GetCharNumAtPosition(this, aPoint
) : -1;
203 } // namespace mozilla::dom