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"
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 return textFrame
->GetSubStringLength(this, charnum
, nchars
, rv
);
138 already_AddRefed
<nsISVGPoint
> SVGTextContentElement::GetStartPositionOfChar(
139 uint32_t charnum
, ErrorResult
& rv
) {
140 SVGTextFrame
* textFrame
= GetSVGTextFrame();
142 rv
.ThrowInvalidStateError("No layout information available for SVG text");
146 return textFrame
->GetStartPositionOfChar(this, charnum
, rv
);
149 already_AddRefed
<nsISVGPoint
> SVGTextContentElement::GetEndPositionOfChar(
150 uint32_t charnum
, ErrorResult
& rv
) {
151 SVGTextFrame
* textFrame
= GetSVGTextFrame();
153 rv
.ThrowInvalidStateError("No layout information available for SVG text");
157 return textFrame
->GetEndPositionOfChar(this, charnum
, rv
);
160 already_AddRefed
<SVGRect
> SVGTextContentElement::GetExtentOfChar(
161 uint32_t charnum
, ErrorResult
& rv
) {
162 SVGTextFrame
* textFrame
= GetSVGTextFrame();
165 rv
.ThrowInvalidStateError("No layout information available for SVG text");
169 return textFrame
->GetExtentOfChar(this, charnum
, rv
);
172 float SVGTextContentElement::GetRotationOfChar(uint32_t charnum
,
174 SVGTextFrame
* textFrame
= GetSVGTextFrame();
177 rv
.ThrowInvalidStateError("No layout information available for SVG text");
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;
191 } // namespace mozilla