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 "SVGGeometryProperty.h"
8 #include "SVGCircleElement.h"
9 #include "SVGEllipseElement.h"
10 #include "SVGForeignObjectElement.h"
11 #include "SVGImageElement.h"
12 #include "SVGRectElement.h"
13 #include "SVGUseElement.h"
14 #include "nsCSSValue.h"
15 
16 namespace mozilla {
17 namespace dom {
18 namespace SVGGeometryProperty {
19 
SpecifiedUnitTypeToCSSUnit(uint8_t aSpecifiedUnit)20 nsCSSUnit SpecifiedUnitTypeToCSSUnit(uint8_t aSpecifiedUnit) {
21   switch (aSpecifiedUnit) {
22     case SVGLength_Binding::SVG_LENGTHTYPE_NUMBER:
23     case SVGLength_Binding::SVG_LENGTHTYPE_PX:
24       return nsCSSUnit::eCSSUnit_Pixel;
25 
26     case SVGLength_Binding::SVG_LENGTHTYPE_MM:
27       return nsCSSUnit::eCSSUnit_Millimeter;
28 
29     case SVGLength_Binding::SVG_LENGTHTYPE_CM:
30       return nsCSSUnit::eCSSUnit_Centimeter;
31 
32     case SVGLength_Binding::SVG_LENGTHTYPE_IN:
33       return nsCSSUnit::eCSSUnit_Inch;
34 
35     case SVGLength_Binding::SVG_LENGTHTYPE_PT:
36       return nsCSSUnit::eCSSUnit_Point;
37 
38     case SVGLength_Binding::SVG_LENGTHTYPE_PC:
39       return nsCSSUnit::eCSSUnit_Pica;
40 
41     case SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE:
42       return nsCSSUnit::eCSSUnit_Percent;
43 
44     case SVGLength_Binding::SVG_LENGTHTYPE_EMS:
45       return nsCSSUnit::eCSSUnit_EM;
46 
47     case SVGLength_Binding::SVG_LENGTHTYPE_EXS:
48       return nsCSSUnit::eCSSUnit_XHeight;
49 
50     default:
51       MOZ_ASSERT_UNREACHABLE("Unknown unit type");
52       return nsCSSUnit::eCSSUnit_Pixel;
53   }
54 }
55 
AttrEnumToCSSPropId(const SVGElement * aElement,uint8_t aAttrEnum)56 nsCSSPropertyID AttrEnumToCSSPropId(const SVGElement* aElement,
57                                     uint8_t aAttrEnum) {
58   // This is a very trivial function only applied to a few elements,
59   // so we want to avoid making it virtual.
60   if (aElement->IsSVGElement(nsGkAtoms::rect)) {
61     return SVGRectElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
62   }
63   if (aElement->IsSVGElement(nsGkAtoms::circle)) {
64     return SVGCircleElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
65   }
66   if (aElement->IsSVGElement(nsGkAtoms::ellipse)) {
67     return SVGEllipseElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
68   }
69   if (aElement->IsSVGElement(nsGkAtoms::image)) {
70     return SVGImageElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
71   }
72   if (aElement->IsSVGElement(nsGkAtoms::foreignObject)) {
73     return SVGForeignObjectElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
74   }
75   if (aElement->IsSVGElement(nsGkAtoms::use)) {
76     return SVGUseElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
77   }
78   return eCSSProperty_UNKNOWN;
79 }
80 
IsNonNegativeGeometryProperty(nsCSSPropertyID aProp)81 bool IsNonNegativeGeometryProperty(nsCSSPropertyID aProp) {
82   return aProp == eCSSProperty_r || aProp == eCSSProperty_rx ||
83          aProp == eCSSProperty_ry || aProp == eCSSProperty_width ||
84          aProp == eCSSProperty_height;
85 }
86 
ElementMapsLengthsToStyle(SVGElement const * aElement)87 bool ElementMapsLengthsToStyle(SVGElement const* aElement) {
88   return aElement->IsAnyOfSVGElements(nsGkAtoms::rect, nsGkAtoms::circle,
89                                       nsGkAtoms::ellipse, nsGkAtoms::image,
90                                       nsGkAtoms::foreignObject, nsGkAtoms::use);
91 }
92 
93 }  // namespace SVGGeometryProperty
94 }  // namespace dom
95 }  // namespace mozilla
96