tor-browser

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

SVGFEColorMatrixElement.cpp (5091B)


      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/SVGFEColorMatrixElement.h"
      8 
      9 #include "DOMSVGAnimatedNumberList.h"
     10 #include "mozilla/dom/BindContext.h"
     11 #include "mozilla/dom/Document.h"
     12 #include "mozilla/dom/SVGFEColorMatrixElementBinding.h"
     13 
     14 #define NUM_ENTRIES_IN_4x5_MATRIX 20
     15 
     16 NS_IMPL_NS_NEW_SVG_ELEMENT(FEColorMatrix)
     17 
     18 using namespace mozilla::gfx;
     19 
     20 namespace mozilla::dom {
     21 
     22 JSObject* SVGFEColorMatrixElement::WrapNode(JSContext* aCx,
     23                                            JS::Handle<JSObject*> aGivenProto) {
     24  return SVGFEColorMatrixElement_Binding::Wrap(aCx, this, aGivenProto);
     25 }
     26 
     27 SVGEnumMapping SVGFEColorMatrixElement::sTypeMap[] = {
     28    {nsGkAtoms::matrix, SVG_FECOLORMATRIX_TYPE_MATRIX},
     29    {nsGkAtoms::saturate, SVG_FECOLORMATRIX_TYPE_SATURATE},
     30    {nsGkAtoms::hueRotate, SVG_FECOLORMATRIX_TYPE_HUE_ROTATE},
     31    {nsGkAtoms::luminanceToAlpha, SVG_FECOLORMATRIX_TYPE_LUMINANCE_TO_ALPHA},
     32    {nullptr, 0}};
     33 
     34 SVGElement::EnumInfo SVGFEColorMatrixElement::sEnumInfo[1] = {
     35    {nsGkAtoms::type, sTypeMap, SVG_FECOLORMATRIX_TYPE_MATRIX}};
     36 
     37 SVGElement::StringInfo SVGFEColorMatrixElement::sStringInfo[2] = {
     38    {nsGkAtoms::result, kNameSpaceID_None, true},
     39    {nsGkAtoms::in, kNameSpaceID_None, true}};
     40 
     41 SVGElement::NumberListInfo SVGFEColorMatrixElement::sNumberListInfo[1] = {
     42    {nsGkAtoms::values}};
     43 
     44 //----------------------------------------------------------------------
     45 // nsINode methods
     46 
     47 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFEColorMatrixElement)
     48 
     49 //----------------------------------------------------------------------
     50 
     51 already_AddRefed<DOMSVGAnimatedString> SVGFEColorMatrixElement::In1() {
     52  return mStringAttributes[IN1].ToDOMAnimatedString(this);
     53 }
     54 
     55 already_AddRefed<DOMSVGAnimatedEnumeration> SVGFEColorMatrixElement::Type() {
     56  return mEnumAttributes[TYPE].ToDOMAnimatedEnum(this);
     57 }
     58 
     59 already_AddRefed<DOMSVGAnimatedNumberList> SVGFEColorMatrixElement::Values() {
     60  return DOMSVGAnimatedNumberList::GetDOMWrapper(&mNumberListAttributes[VALUES],
     61                                                 this, VALUES);
     62 }
     63 
     64 void SVGFEColorMatrixElement::GetSourceImageNames(
     65    nsTArray<SVGStringInfo>& aSources) {
     66  aSources.AppendElement(SVGStringInfo(&mStringAttributes[IN1], this));
     67 }
     68 
     69 FilterPrimitiveDescription SVGFEColorMatrixElement::GetPrimitiveDescription(
     70    SVGFilterInstance* aInstance, const IntRect& aFilterSubregion,
     71    const nsTArray<bool>& aInputsAreTainted,
     72    nsTArray<RefPtr<SourceSurface>>& aInputImages) {
     73  uint32_t type = mEnumAttributes[TYPE].GetAnimValue();
     74  const SVGNumberList& values = mNumberListAttributes[VALUES].GetAnimValue();
     75 
     76  ColorMatrixAttributes atts;
     77  if (!mNumberListAttributes[VALUES].IsExplicitlySet() &&
     78      (type == SVG_FECOLORMATRIX_TYPE_MATRIX ||
     79       type == SVG_FECOLORMATRIX_TYPE_SATURATE ||
     80       type == SVG_FECOLORMATRIX_TYPE_HUE_ROTATE)) {
     81    atts.mType = (uint32_t)SVG_FECOLORMATRIX_TYPE_MATRIX;
     82    static const auto identityMatrix = std::array{
     83        // clang-format off
     84        1, 0, 0, 0, 0,
     85        0, 1, 0, 0, 0,
     86        0, 0, 1, 0, 0,
     87        0, 0, 0, 1, 0
     88        // clang-format on
     89    };
     90    atts.mValues.AppendElements(Span(identityMatrix));
     91  } else {
     92    atts.mType = type;
     93    if (values.Length()) {
     94      atts.mValues.AppendElements(&values[0], values.Length());
     95    }
     96  }
     97 
     98  return FilterPrimitiveDescription(AsVariant(std::move(atts)));
     99 }
    100 
    101 bool SVGFEColorMatrixElement::AttributeAffectsRendering(
    102    int32_t aNameSpaceID, nsAtom* aAttribute) const {
    103  return SVGFEColorMatrixElementBase::AttributeAffectsRendering(aNameSpaceID,
    104                                                                aAttribute) ||
    105         (aNameSpaceID == kNameSpaceID_None &&
    106          (aAttribute == nsGkAtoms::in || aAttribute == nsGkAtoms::type ||
    107           aAttribute == nsGkAtoms::values));
    108 }
    109 
    110 nsresult SVGFEColorMatrixElement::BindToTree(BindContext& aCtx,
    111                                             nsINode& aParent) {
    112  if (aCtx.InComposedDoc()) {
    113    aCtx.OwnerDoc().SetUseCounter(eUseCounter_custom_feColorMatrix);
    114  }
    115 
    116  return SVGFEColorMatrixElementBase::BindToTree(aCtx, aParent);
    117 }
    118 
    119 //----------------------------------------------------------------------
    120 // SVGElement methods
    121 
    122 SVGElement::EnumAttributesInfo SVGFEColorMatrixElement::GetEnumInfo() {
    123  return EnumAttributesInfo(mEnumAttributes, sEnumInfo, std::size(sEnumInfo));
    124 }
    125 
    126 SVGElement::StringAttributesInfo SVGFEColorMatrixElement::GetStringInfo() {
    127  return StringAttributesInfo(mStringAttributes, sStringInfo,
    128                              std::size(sStringInfo));
    129 }
    130 
    131 SVGElement::NumberListAttributesInfo
    132 SVGFEColorMatrixElement::GetNumberListInfo() {
    133  return NumberListAttributesInfo(mNumberListAttributes, sNumberListInfo,
    134                                  std::size(sNumberListInfo));
    135 }
    136 
    137 }  // namespace mozilla::dom