tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SVGGraphicsElement.cpp (5750B)


      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/. */
      6 
      7 #include "mozilla/dom/SVGGraphicsElement.h"
      8 
      9 #include "mozilla/ISVGDisplayableFrame.h"
     10 #include "mozilla/SVGContentUtils.h"
     11 #include "mozilla/SVGTextFrame.h"
     12 #include "mozilla/SVGUtils.h"
     13 #include "mozilla/dom/BindContext.h"
     14 #include "mozilla/dom/Document.h"
     15 #include "mozilla/dom/SVGGraphicsElementBinding.h"
     16 #include "mozilla/dom/SVGMatrix.h"
     17 #include "mozilla/dom/SVGRect.h"
     18 #include "mozilla/dom/SVGSVGElement.h"
     19 #include "nsIContentInlines.h"
     20 #include "nsLayoutUtils.h"
     21 
     22 namespace mozilla::dom {
     23 
     24 //----------------------------------------------------------------------
     25 // nsISupports methods
     26 
     27 NS_IMPL_ADDREF_INHERITED(SVGGraphicsElement, SVGGraphicsElementBase)
     28 NS_IMPL_RELEASE_INHERITED(SVGGraphicsElement, SVGGraphicsElementBase)
     29 
     30 NS_INTERFACE_MAP_BEGIN(SVGGraphicsElement)
     31  NS_INTERFACE_MAP_ENTRY(mozilla::dom::SVGTests)
     32 NS_INTERFACE_MAP_END_INHERITING(SVGGraphicsElementBase)
     33 
     34 //----------------------------------------------------------------------
     35 // Implementation
     36 
     37 SVGGraphicsElement::SVGGraphicsElement(
     38    already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
     39    : SVGGraphicsElementBase(std::move(aNodeInfo)) {}
     40 
     41 static already_AddRefed<SVGRect> ZeroBBox(SVGGraphicsElement& aOwner) {
     42  return MakeAndAddRef<SVGRect>(&aOwner, gfx::Rect{0, 0, 0, 0});
     43 }
     44 
     45 already_AddRefed<SVGRect> SVGGraphicsElement::GetBBox(
     46    const SVGBoundingBoxOptions& aOptions) {
     47  nsIFrame* frame = GetPrimaryFrame(FlushType::Layout);
     48 
     49  if (!frame || frame->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
     50    return ZeroBBox(*this);
     51  }
     52  ISVGDisplayableFrame* svgframe = do_QueryFrame(frame);
     53 
     54  if (!svgframe) {
     55    if (!frame->IsInSVGTextSubtree()) {
     56      return ZeroBBox(*this);
     57    }
     58 
     59    // For <tspan>, <textPath>, the frame is an nsInlineFrame or
     60    // nsBlockFrame, |svgframe| will be a nullptr.
     61    // We implement their getBBox directly here instead of in
     62    // SVGUtils::GetBBox, because SVGUtils::GetBBox is more
     63    // or less used for other purpose elsewhere. e.g. gradient
     64    // code assumes GetBBox of <tspan> returns the bbox of the
     65    // outer <text>.
     66    // TODO: cleanup this sort of usecase of SVGUtils::GetBBox,
     67    // then move this code SVGUtils::GetBBox.
     68    SVGTextFrame* text =
     69        static_cast<SVGTextFrame*>(nsLayoutUtils::GetClosestFrameOfType(
     70            frame->GetParent(), LayoutFrameType::SVGText));
     71 
     72    if (text->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
     73      return ZeroBBox(*this);
     74    }
     75 
     76    gfxRect rec = text->TransformFrameRectFromTextChild(
     77        frame->GetRectRelativeToSelf(), frame);
     78 
     79    // Should also add the |x|, |y| of the SVGTextFrame itself, since
     80    // the result obtained by TransformFrameRectFromTextChild doesn't
     81    // include them.
     82    rec.x += float(text->GetPosition().x) / AppUnitsPerCSSPixel();
     83    rec.y += float(text->GetPosition().y) / AppUnitsPerCSSPixel();
     84 
     85    return do_AddRef(new SVGRect(this, ToRect(rec)));
     86  }
     87 
     88  if (!NS_SVGNewGetBBoxEnabled()) {
     89    return do_AddRef(new SVGRect(
     90        this, ToRect(SVGUtils::GetBBox(
     91                  frame, SVGUtils::eBBoxIncludeFillGeometry |
     92                             SVGUtils::eUseUserSpaceOfUseElement))));
     93  }
     94  uint32_t flags = 0;
     95  if (aOptions.mFill) {
     96    flags |= SVGUtils::eBBoxIncludeFillGeometry;
     97  }
     98  if (aOptions.mStroke) {
     99    flags |= SVGUtils::eBBoxIncludeStroke;
    100  }
    101  if (aOptions.mMarkers) {
    102    flags |= SVGUtils::eBBoxIncludeMarkers;
    103  }
    104  if (aOptions.mClipped) {
    105    flags |= SVGUtils::eBBoxIncludeClipped;
    106  }
    107  if (flags == 0) {
    108    return do_AddRef(new SVGRect(this, {}));
    109  }
    110  if (flags == SVGUtils::eBBoxIncludeMarkers ||
    111      flags == SVGUtils::eBBoxIncludeClipped) {
    112    flags |= SVGUtils::eBBoxIncludeFillGeometry;
    113  }
    114  flags |= SVGUtils::eUseUserSpaceOfUseElement;
    115  return do_AddRef(new SVGRect(this, ToRect(SVGUtils::GetBBox(frame, flags))));
    116 }
    117 
    118 already_AddRefed<SVGMatrix> SVGGraphicsElement::GetCTM() {
    119  if (auto* currentDoc = GetComposedDoc()) {
    120    // Flush all pending notifications so that our frames are up to date
    121    currentDoc->FlushPendingNotifications(FlushType::Layout);
    122  }
    123  gfx::Matrix m = SVGContentUtils::GetCTM(this);
    124  if (m.IsSingular()) {
    125    m = {};
    126  }
    127  return do_AddRef(new SVGMatrix(ThebesMatrix(m)));
    128 }
    129 
    130 already_AddRefed<SVGMatrix> SVGGraphicsElement::GetScreenCTM() {
    131  if (auto* currentDoc = GetComposedDoc()) {
    132    // Flush all pending notifications so that our frames are up to date
    133    currentDoc->FlushPendingNotifications(FlushType::Layout);
    134  }
    135  gfx::Matrix m = SVGContentUtils::GetScreenCTM(this);
    136  if (m.IsSingular()) {
    137    m = {};
    138  }
    139  return do_AddRef(new SVGMatrix(ThebesMatrix(m)));
    140 }
    141 
    142 bool SVGGraphicsElement::IsSVGFocusable(bool* aIsFocusable,
    143                                        int32_t* aTabIndex) {
    144  // XXXedgar, maybe we could factor out the common code for SVG, HTML and
    145  // MathML elements, see bug 1586011.
    146  if (!IsInComposedDoc() || IsInDesignMode()) {
    147    // In designMode documents we only allow focusing the document.
    148    *aTabIndex = -1;
    149    *aIsFocusable = false;
    150    return true;
    151  }
    152 
    153  *aTabIndex = TabIndex();
    154  // If a tabindex is specified at all, or the default tabindex is 0, we're
    155  // focusable
    156  *aIsFocusable = *aTabIndex >= 0 || GetTabIndexAttrValue().isSome();
    157  return false;
    158 }
    159 
    160 Focusable SVGGraphicsElement::IsFocusableWithoutStyle(IsFocusableFlags) {
    161  Focusable result;
    162  IsSVGFocusable(&result.mFocusable, &result.mTabIndex);
    163  return result;
    164 }
    165 
    166 }  // namespace mozilla::dom