Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / svg / SVGTextContentElement.cpp
blob4015bdf0173361f52bdaf10372e77156e8ab81d1
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},
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 Nothing();
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 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
140 // that for us.
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();
154 if (!textFrame) {
155 rv.ThrowInvalidStateError("No layout information available for SVG text");
156 return nullptr;
159 return textFrame->GetStartPositionOfChar(this, charnum, rv);
162 already_AddRefed<DOMSVGPoint> SVGTextContentElement::GetEndPositionOfChar(
163 uint32_t charnum, ErrorResult& rv) {
164 SVGTextFrame* textFrame = GetSVGTextFrame();
165 if (!textFrame) {
166 rv.ThrowInvalidStateError("No layout information available for SVG text");
167 return nullptr;
170 return textFrame->GetEndPositionOfChar(this, charnum, rv);
173 already_AddRefed<SVGRect> SVGTextContentElement::GetExtentOfChar(
174 uint32_t charnum, ErrorResult& rv) {
175 SVGTextFrame* textFrame = GetSVGTextFrame();
177 if (!textFrame) {
178 rv.ThrowInvalidStateError("No layout information available for SVG text");
179 return nullptr;
182 return textFrame->GetExtentOfChar(this, charnum, rv);
185 float SVGTextContentElement::GetRotationOfChar(uint32_t charnum,
186 ErrorResult& rv) {
187 SVGTextFrame* textFrame = GetSVGTextFrame();
189 if (!textFrame) {
190 rv.ThrowInvalidStateError("No layout information available for SVG text");
191 return 0.0f;
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