2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-01-06 18:14:43 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/SVGTextContentElement.h"
|
2017-01-17 08:45:43 +03:00
|
|
|
|
2018-02-02 16:21:33 +03:00
|
|
|
#include "mozilla/dom/SVGLengthBinding.h"
|
2018-02-18 18:53:13 +03:00
|
|
|
#include "mozilla/dom/SVGTextContentElementBinding.h"
|
2019-06-20 17:03:54 +03:00
|
|
|
#include "mozilla/dom/SVGRect.h"
|
2017-01-17 08:45:43 +03:00
|
|
|
#include "nsBidiUtils.h"
|
2013-01-06 18:14:43 +04:00
|
|
|
#include "nsISVGPoint.h"
|
2017-01-17 08:45:43 +03:00
|
|
|
#include "nsTextFragment.h"
|
|
|
|
#include "nsTextFrameUtils.h"
|
|
|
|
#include "nsTextNode.h"
|
2013-12-20 20:38:51 +04:00
|
|
|
#include "SVGTextFrame.h"
|
2013-01-06 18:14:43 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-06-26 00:20:54 +03:00
|
|
|
using namespace SVGTextContentElement_Binding;
|
2018-02-18 18:53:13 +03:00
|
|
|
|
2019-01-02 21:24:11 +03:00
|
|
|
SVGEnumMapping SVGTextContentElement::sLengthAdjustMap[] = {
|
2018-03-29 12:45:28 +03:00
|
|
|
{nsGkAtoms::spacing, LENGTHADJUST_SPACING},
|
|
|
|
{nsGkAtoms::spacingAndGlyphs, LENGTHADJUST_SPACINGANDGLYPHS},
|
2013-07-07 11:27:51 +04:00
|
|
|
{nullptr, 0}};
|
|
|
|
|
2018-12-21 11:58:14 +03:00
|
|
|
SVGElement::EnumInfo SVGTextContentElement::sEnumInfo[1] = {
|
2018-03-29 12:45:24 +03:00
|
|
|
{nsGkAtoms::lengthAdjust, sLengthAdjustMap, LENGTHADJUST_SPACING}};
|
2013-07-07 11:27:51 +04:00
|
|
|
|
2018-12-21 11:58:14 +03:00
|
|
|
SVGElement::LengthInfo SVGTextContentElement::sLengthInfo[1] = {
|
2018-03-29 12:45:24 +03:00
|
|
|
{nsGkAtoms::textLength, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
|
|
|
|
SVGContentUtils::XY}};
|
2013-07-07 11:27:51 +04:00
|
|
|
|
2013-02-11 10:22:17 +04:00
|
|
|
SVGTextFrame* SVGTextContentElement::GetSVGTextFrame() {
|
2017-01-05 10:31:56 +03:00
|
|
|
nsIFrame* frame = GetPrimaryFrame(FlushType::Layout);
|
2017-01-17 08:45:43 +03:00
|
|
|
nsIFrame* textFrame =
|
2017-05-01 20:32:52 +03:00
|
|
|
nsLayoutUtils::GetClosestFrameOfType(frame, LayoutFrameType::SVGText);
|
2017-01-17 08:45:43 +03:00
|
|
|
return static_cast<SVGTextFrame*>(textFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
SVGTextFrame*
|
|
|
|
SVGTextContentElement::GetSVGTextFrameForNonLayoutDependentQuery() {
|
|
|
|
nsIFrame* frame = GetPrimaryFrame(FlushType::Frames);
|
|
|
|
nsIFrame* textFrame =
|
2017-05-01 20:32:52 +03:00
|
|
|
nsLayoutUtils::GetClosestFrameOfType(frame, LayoutFrameType::SVGText);
|
2017-01-17 08:45:43 +03:00
|
|
|
return static_cast<SVGTextFrame*>(textFrame);
|
2013-02-11 10:22:17 +04:00
|
|
|
}
|
|
|
|
|
2019-03-19 03:01:03 +03:00
|
|
|
already_AddRefed<DOMSVGAnimatedLength> SVGTextContentElement::TextLength() {
|
2013-07-24 04:01:35 +04:00
|
|
|
return LengthAttributes()[TEXTLENGTH].ToDOMAnimatedLength(this);
|
2013-07-07 11:27:51 +04:00
|
|
|
}
|
|
|
|
|
2019-03-19 03:01:03 +03:00
|
|
|
already_AddRefed<DOMSVGAnimatedEnumeration>
|
|
|
|
SVGTextContentElement::LengthAdjust() {
|
2013-07-24 04:01:35 +04:00
|
|
|
return EnumAttributes()[LENGTHADJUST].ToDOMAnimatedEnum(this);
|
2013-07-07 11:27:51 +04:00
|
|
|
}
|
|
|
|
|
2013-01-06 18:14:43 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2017-01-17 08:45:43 +03:00
|
|
|
template <typename T>
|
|
|
|
static bool FragmentHasSkippableCharacter(const T* aBuffer, uint32_t aLength) {
|
|
|
|
for (uint32_t i = 0; i < aLength; i++) {
|
|
|
|
if (nsTextFrameUtils::IsSkippableCharacterForTransformText(aBuffer[i])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<int32_t> SVGTextContentElement::GetNonLayoutDependentNumberOfChars() {
|
|
|
|
SVGTextFrame* frame = GetSVGTextFrameForNonLayoutDependentQuery();
|
|
|
|
if (!frame || frame != GetPrimaryFrame()) {
|
|
|
|
// Only support this fast path on <text>, not child <tspan>s, etc.
|
|
|
|
return Some(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t num = 0;
|
|
|
|
|
|
|
|
for (nsINode* n = Element::GetFirstChild(); n; n = n->GetNextSibling()) {
|
2018-04-13 01:41:00 +03:00
|
|
|
if (!n->IsText()) {
|
2017-01-17 08:45:43 +03:00
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
2019-05-22 08:18:48 +03:00
|
|
|
const nsTextFragment* text = &n->AsText()->TextFragment();
|
2017-01-17 08:45:43 +03:00
|
|
|
uint32_t length = text->GetLength();
|
|
|
|
|
|
|
|
if (text->Is2b()) {
|
|
|
|
if (FragmentHasSkippableCharacter(text->Get2b(), length)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto buffer = reinterpret_cast<const uint8_t*>(text->Get1b());
|
|
|
|
if (FragmentHasSkippableCharacter(buffer, length)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
num += length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(num);
|
|
|
|
}
|
|
|
|
|
2013-01-06 18:14:43 +04:00
|
|
|
int32_t SVGTextContentElement::GetNumberOfChars() {
|
2017-01-17 08:45:43 +03:00
|
|
|
Maybe<int32_t> num = GetNonLayoutDependentNumberOfChars();
|
|
|
|
if (num) {
|
|
|
|
return *num;
|
|
|
|
}
|
|
|
|
|
2013-12-20 20:38:51 +04:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrame();
|
2013-11-18 18:29:53 +04:00
|
|
|
return textFrame ? textFrame->GetNumberOfChars(this) : 0;
|
2013-01-06 18:14:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
float SVGTextContentElement::GetComputedTextLength() {
|
2013-12-20 20:38:51 +04:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrame();
|
2013-11-18 18:29:53 +04:00
|
|
|
return textFrame ? textFrame->GetComputedTextLength(this) : 0.0f;
|
2013-01-06 18:14:43 +04:00
|
|
|
}
|
|
|
|
|
2013-04-01 13:43:38 +04:00
|
|
|
void SVGTextContentElement::SelectSubString(uint32_t charnum, uint32_t nchars,
|
|
|
|
ErrorResult& rv) {
|
2013-12-20 20:38:51 +04:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrame();
|
2013-11-18 18:29:53 +04:00
|
|
|
if (!textFrame) return;
|
|
|
|
|
2020-02-09 05:15:32 +03:00
|
|
|
textFrame->SelectSubString(this, charnum, nchars, rv);
|
2013-04-01 13:43:38 +04:00
|
|
|
}
|
|
|
|
|
2013-01-06 18:14:43 +04:00
|
|
|
float SVGTextContentElement::GetSubStringLength(uint32_t charnum,
|
|
|
|
uint32_t nchars,
|
|
|
|
ErrorResult& rv) {
|
2017-09-03 00:22:54 +03:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrameForNonLayoutDependentQuery();
|
2013-11-18 18:29:53 +04:00
|
|
|
if (!textFrame) return 0.0f;
|
2013-02-11 10:22:17 +04:00
|
|
|
|
2020-02-09 05:15:32 +03:00
|
|
|
return textFrame->GetSubStringLength(this, charnum, nchars, rv);
|
2013-01-06 18:14:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsISVGPoint> SVGTextContentElement::GetStartPositionOfChar(
|
|
|
|
uint32_t charnum, ErrorResult& rv) {
|
2013-12-20 20:38:51 +04:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrame();
|
2013-11-18 18:29:53 +04:00
|
|
|
if (!textFrame) {
|
2020-02-09 05:15:32 +03:00
|
|
|
rv.ThrowInvalidStateError("No layout information available for SVG text");
|
2013-11-18 18:29:53 +04:00
|
|
|
return nullptr;
|
2013-02-11 10:22:17 +04:00
|
|
|
}
|
2013-11-18 18:29:53 +04:00
|
|
|
|
2020-02-09 05:15:32 +03:00
|
|
|
return textFrame->GetStartPositionOfChar(this, charnum, rv);
|
2013-01-06 18:14:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsISVGPoint> SVGTextContentElement::GetEndPositionOfChar(
|
|
|
|
uint32_t charnum, ErrorResult& rv) {
|
2013-12-20 20:38:51 +04:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrame();
|
2013-11-18 18:29:53 +04:00
|
|
|
if (!textFrame) {
|
2020-02-09 05:15:32 +03:00
|
|
|
rv.ThrowInvalidStateError("No layout information available for SVG text");
|
2013-11-18 18:29:53 +04:00
|
|
|
return nullptr;
|
2013-02-11 10:22:17 +04:00
|
|
|
}
|
2013-11-18 18:29:53 +04:00
|
|
|
|
2020-02-09 05:15:32 +03:00
|
|
|
return textFrame->GetEndPositionOfChar(this, charnum, rv);
|
2013-01-06 18:14:43 +04:00
|
|
|
}
|
|
|
|
|
2019-06-20 17:03:54 +03:00
|
|
|
already_AddRefed<SVGRect> SVGTextContentElement::GetExtentOfChar(
|
2013-01-06 18:14:43 +04:00
|
|
|
uint32_t charnum, ErrorResult& rv) {
|
2013-12-20 20:38:51 +04:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrame();
|
2013-02-11 10:22:17 +04:00
|
|
|
|
2013-11-18 18:29:53 +04:00
|
|
|
if (!textFrame) {
|
2020-02-09 05:15:32 +03:00
|
|
|
rv.ThrowInvalidStateError("No layout information available for SVG text");
|
2013-11-18 18:29:53 +04:00
|
|
|
return nullptr;
|
2013-02-11 10:22:17 +04:00
|
|
|
}
|
2013-11-18 18:29:53 +04:00
|
|
|
|
2020-02-09 05:15:32 +03:00
|
|
|
return textFrame->GetExtentOfChar(this, charnum, rv);
|
2013-01-06 18:14:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
float SVGTextContentElement::GetRotationOfChar(uint32_t charnum,
|
|
|
|
ErrorResult& rv) {
|
2013-12-20 20:38:51 +04:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrame();
|
2013-01-06 18:14:43 +04:00
|
|
|
|
2013-11-18 18:29:53 +04:00
|
|
|
if (!textFrame) {
|
2020-02-09 05:15:32 +03:00
|
|
|
rv.ThrowInvalidStateError("No layout information available for SVG text");
|
2013-11-18 18:29:53 +04:00
|
|
|
return 0.0f;
|
2013-02-11 10:22:17 +04:00
|
|
|
}
|
2013-11-18 18:29:53 +04:00
|
|
|
|
2020-02-09 05:15:32 +03:00
|
|
|
return textFrame->GetRotationOfChar(this, charnum, rv);
|
2013-01-06 18:14:43 +04:00
|
|
|
}
|
|
|
|
|
2019-06-16 12:12:40 +03:00
|
|
|
int32_t SVGTextContentElement::GetCharNumAtPosition(
|
|
|
|
const DOMPointInit& aPoint) {
|
2013-12-20 20:38:51 +04:00
|
|
|
SVGTextFrame* textFrame = GetSVGTextFrame();
|
2019-06-16 12:12:40 +03:00
|
|
|
return textFrame ? textFrame->GetCharNumAtPosition(this, aPoint) : -1;
|
2013-01-06 18:14:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|