nsSVGLength2.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/ArrayUtils.h"
  6. #include "nsSVGLength2.h"
  7. #include "mozilla/dom/SVGAnimatedLength.h"
  8. #include "mozilla/dom/SVGSVGElement.h"
  9. #include "nsContentUtils.h" // NS_ENSURE_FINITE
  10. #include "nsIFrame.h"
  11. #include "nsSMILFloatType.h"
  12. #include "nsSMILValue.h"
  13. #include "nsSVGAttrTearoffTable.h"
  14. #include "nsSVGIntegrationUtils.h"
  15. #include "nsTextFormatter.h"
  16. #include "DOMSVGLength.h"
  17. #include "LayoutLogging.h"
  18. using namespace mozilla;
  19. using namespace mozilla::dom;
  20. static nsIAtom** const unitMap[] =
  21. {
  22. nullptr, /* SVG_LENGTHTYPE_UNKNOWN */
  23. nullptr, /* SVG_LENGTHTYPE_NUMBER */
  24. &nsGkAtoms::percentage,
  25. &nsGkAtoms::em,
  26. &nsGkAtoms::ex,
  27. &nsGkAtoms::px,
  28. &nsGkAtoms::cm,
  29. &nsGkAtoms::mm,
  30. &nsGkAtoms::in,
  31. &nsGkAtoms::pt,
  32. &nsGkAtoms::pc
  33. };
  34. static nsSVGAttrTearoffTable<nsSVGLength2, SVGAnimatedLength>
  35. sSVGAnimatedLengthTearoffTable;
  36. /* Helper functions */
  37. static bool
  38. IsValidUnitType(uint16_t unit)
  39. {
  40. if (unit > nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN &&
  41. unit <= nsIDOMSVGLength::SVG_LENGTHTYPE_PC)
  42. return true;
  43. return false;
  44. }
  45. static void
  46. GetUnitString(nsAString& unit, uint16_t unitType)
  47. {
  48. if (IsValidUnitType(unitType)) {
  49. if (unitMap[unitType]) {
  50. (*unitMap[unitType])->ToString(unit);
  51. }
  52. return;
  53. }
  54. NS_NOTREACHED("Unknown unit type");
  55. return;
  56. }
  57. static uint16_t
  58. GetUnitTypeForString(const nsAString& unitStr)
  59. {
  60. if (unitStr.IsEmpty())
  61. return nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER;
  62. nsIAtom *unitAtom = NS_GetStaticAtom(unitStr);
  63. if (unitAtom) {
  64. for (uint32_t i = 0 ; i < ArrayLength(unitMap) ; i++) {
  65. if (unitMap[i] && *unitMap[i] == unitAtom) {
  66. return i;
  67. }
  68. }
  69. }
  70. return nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN;
  71. }
  72. static void
  73. GetValueString(nsAString &aValueAsString, float aValue, uint16_t aUnitType)
  74. {
  75. char16_t buf[24];
  76. nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
  77. u"%g",
  78. (double)aValue);
  79. aValueAsString.Assign(buf);
  80. nsAutoString unitString;
  81. GetUnitString(unitString, aUnitType);
  82. aValueAsString.Append(unitString);
  83. }
  84. static bool
  85. GetValueFromString(const nsAString& aString,
  86. float& aValue,
  87. uint16_t* aUnitType)
  88. {
  89. RangedPtr<const char16_t> iter =
  90. SVGContentUtils::GetStartRangedPtr(aString);
  91. const RangedPtr<const char16_t> end =
  92. SVGContentUtils::GetEndRangedPtr(aString);
  93. if (!SVGContentUtils::ParseNumber(iter, end, aValue)) {
  94. return false;
  95. }
  96. const nsAString& units = Substring(iter.get(), end.get());
  97. *aUnitType = GetUnitTypeForString(units);
  98. return IsValidUnitType(*aUnitType);
  99. }
  100. static float GetMMPerPixel() { return MM_PER_INCH_FLOAT / 96; }
  101. static float
  102. FixAxisLength(float aLength)
  103. {
  104. if (aLength == 0.0f) {
  105. LAYOUT_WARNING("zero axis length");
  106. return 1e-20f;
  107. }
  108. return aLength;
  109. }
  110. SVGElementMetrics::SVGElementMetrics(nsSVGElement* aSVGElement,
  111. SVGSVGElement* aCtx)
  112. : mSVGElement(aSVGElement)
  113. , mCtx(aCtx)
  114. {
  115. }
  116. float
  117. SVGElementMetrics::GetEmLength() const
  118. {
  119. return SVGContentUtils::GetFontSize(mSVGElement);
  120. }
  121. float
  122. SVGElementMetrics::GetExLength() const
  123. {
  124. return SVGContentUtils::GetFontXHeight(mSVGElement);
  125. }
  126. float
  127. SVGElementMetrics::GetAxisLength(uint8_t aCtxType) const
  128. {
  129. if (!EnsureCtx()) {
  130. return 1;
  131. }
  132. return FixAxisLength(mCtx->GetLength(aCtxType));
  133. }
  134. bool
  135. SVGElementMetrics::EnsureCtx() const
  136. {
  137. if (!mCtx && mSVGElement) {
  138. mCtx = mSVGElement->GetCtx();
  139. if (!mCtx && mSVGElement->IsSVGElement(nsGkAtoms::svg)) {
  140. // mSVGElement must be the outer svg element
  141. mCtx = static_cast<SVGSVGElement*>(mSVGElement);
  142. }
  143. }
  144. return mCtx != nullptr;
  145. }
  146. NonSVGFrameUserSpaceMetrics::NonSVGFrameUserSpaceMetrics(nsIFrame* aFrame)
  147. : mFrame(aFrame)
  148. {
  149. }
  150. float
  151. NonSVGFrameUserSpaceMetrics::GetEmLength() const
  152. {
  153. return SVGContentUtils::GetFontSize(mFrame);
  154. }
  155. float
  156. NonSVGFrameUserSpaceMetrics::GetExLength() const
  157. {
  158. return SVGContentUtils::GetFontXHeight(mFrame);
  159. }
  160. gfx::Size
  161. NonSVGFrameUserSpaceMetrics::GetSize() const
  162. {
  163. return nsSVGIntegrationUtils::GetSVGCoordContextForNonSVGFrame(mFrame);
  164. }
  165. float
  166. UserSpaceMetricsWithSize::GetAxisLength(uint8_t aCtxType) const
  167. {
  168. gfx::Size size = GetSize();
  169. float length;
  170. switch (aCtxType) {
  171. case SVGContentUtils::X:
  172. length = size.width;
  173. break;
  174. case SVGContentUtils::Y:
  175. length = size.height;
  176. break;
  177. case SVGContentUtils::XY:
  178. length = SVGContentUtils::ComputeNormalizedHypotenuse(size.width, size.height);
  179. break;
  180. default:
  181. NS_NOTREACHED("Unknown axis type");
  182. length = 1;
  183. break;
  184. }
  185. return FixAxisLength(length);
  186. }
  187. float
  188. nsSVGLength2::GetUnitScaleFactor(nsSVGElement *aSVGElement,
  189. uint8_t aUnitType) const
  190. {
  191. return GetUnitScaleFactor(SVGElementMetrics(aSVGElement), aUnitType);
  192. }
  193. float
  194. nsSVGLength2::GetUnitScaleFactor(SVGSVGElement *aCtx, uint8_t aUnitType) const
  195. {
  196. return GetUnitScaleFactor(SVGElementMetrics(aCtx, aCtx), aUnitType);
  197. }
  198. float
  199. nsSVGLength2::GetUnitScaleFactor(nsIFrame *aFrame, uint8_t aUnitType) const
  200. {
  201. nsIContent* content = aFrame->GetContent();
  202. if (content->IsSVGElement()) {
  203. return GetUnitScaleFactor(SVGElementMetrics(static_cast<nsSVGElement*>(content)), aUnitType);
  204. }
  205. return GetUnitScaleFactor(NonSVGFrameUserSpaceMetrics(aFrame), aUnitType);
  206. }
  207. float
  208. nsSVGLength2::GetUnitScaleFactor(const UserSpaceMetrics& aMetrics, uint8_t aUnitType) const
  209. {
  210. switch (aUnitType) {
  211. case nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER:
  212. case nsIDOMSVGLength::SVG_LENGTHTYPE_PX:
  213. return 1;
  214. case nsIDOMSVGLength::SVG_LENGTHTYPE_MM:
  215. return GetMMPerPixel();
  216. case nsIDOMSVGLength::SVG_LENGTHTYPE_CM:
  217. return GetMMPerPixel() / 10.0f;
  218. case nsIDOMSVGLength::SVG_LENGTHTYPE_IN:
  219. return GetMMPerPixel() / MM_PER_INCH_FLOAT;
  220. case nsIDOMSVGLength::SVG_LENGTHTYPE_PT:
  221. return GetMMPerPixel() * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT;
  222. case nsIDOMSVGLength::SVG_LENGTHTYPE_PC:
  223. return GetMMPerPixel() * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT / 12.0f;
  224. case nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE:
  225. return 100.0f / aMetrics.GetAxisLength(mCtxType);
  226. case nsIDOMSVGLength::SVG_LENGTHTYPE_EMS:
  227. return 1 / aMetrics.GetEmLength();
  228. case nsIDOMSVGLength::SVG_LENGTHTYPE_EXS:
  229. return 1 / aMetrics.GetExLength();
  230. default:
  231. NS_NOTREACHED("Unknown unit type");
  232. return 0;
  233. }
  234. }
  235. void
  236. nsSVGLength2::SetBaseValueInSpecifiedUnits(float aValue,
  237. nsSVGElement *aSVGElement,
  238. bool aDoSetAttr)
  239. {
  240. if (mIsBaseSet && mBaseVal == aValue) {
  241. return;
  242. }
  243. nsAttrValue emptyOrOldValue;
  244. if (aDoSetAttr) {
  245. emptyOrOldValue = aSVGElement->WillChangeLength(mAttrEnum);
  246. }
  247. mBaseVal = aValue;
  248. mIsBaseSet = true;
  249. if (!mIsAnimated) {
  250. mAnimVal = mBaseVal;
  251. }
  252. else {
  253. aSVGElement->AnimationNeedsResample();
  254. }
  255. if (aDoSetAttr) {
  256. aSVGElement->DidChangeLength(mAttrEnum, emptyOrOldValue);
  257. }
  258. }
  259. nsresult
  260. nsSVGLength2::ConvertToSpecifiedUnits(uint16_t unitType,
  261. nsSVGElement *aSVGElement)
  262. {
  263. if (!IsValidUnitType(unitType))
  264. return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
  265. if (mIsBaseSet && mSpecifiedUnitType == uint8_t(unitType))
  266. return NS_OK;
  267. // Even though we're not changing the visual effect this length will have
  268. // on the document, we still need to send out notifications in case we have
  269. // mutation listeners, since the actual string value of the attribute will
  270. // change.
  271. nsAttrValue emptyOrOldValue = aSVGElement->WillChangeLength(mAttrEnum);
  272. float valueInUserUnits =
  273. mBaseVal / GetUnitScaleFactor(aSVGElement, mSpecifiedUnitType);
  274. mSpecifiedUnitType = uint8_t(unitType);
  275. // Setting aDoSetAttr to false here will ensure we don't call
  276. // Will/DidChangeAngle a second time (and dispatch duplicate notifications).
  277. SetBaseValue(valueInUserUnits, aSVGElement, false);
  278. aSVGElement->DidChangeLength(mAttrEnum, emptyOrOldValue);
  279. return NS_OK;
  280. }
  281. nsresult
  282. nsSVGLength2::NewValueSpecifiedUnits(uint16_t unitType,
  283. float valueInSpecifiedUnits,
  284. nsSVGElement *aSVGElement)
  285. {
  286. NS_ENSURE_FINITE(valueInSpecifiedUnits, NS_ERROR_ILLEGAL_VALUE);
  287. if (!IsValidUnitType(unitType))
  288. return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
  289. if (mIsBaseSet && mBaseVal == valueInSpecifiedUnits &&
  290. mSpecifiedUnitType == uint8_t(unitType)) {
  291. return NS_OK;
  292. }
  293. nsAttrValue emptyOrOldValue = aSVGElement->WillChangeLength(mAttrEnum);
  294. mBaseVal = valueInSpecifiedUnits;
  295. mIsBaseSet = true;
  296. mSpecifiedUnitType = uint8_t(unitType);
  297. if (!mIsAnimated) {
  298. mAnimVal = mBaseVal;
  299. }
  300. else {
  301. aSVGElement->AnimationNeedsResample();
  302. }
  303. aSVGElement->DidChangeLength(mAttrEnum, emptyOrOldValue);
  304. return NS_OK;
  305. }
  306. nsresult
  307. nsSVGLength2::ToDOMBaseVal(DOMSVGLength **aResult, nsSVGElement *aSVGElement)
  308. {
  309. RefPtr<DOMSVGLength> domBaseVal =
  310. DOMSVGLength::GetTearOff(this, aSVGElement, false);
  311. domBaseVal.forget(aResult);
  312. return NS_OK;
  313. }
  314. nsresult
  315. nsSVGLength2::ToDOMAnimVal(DOMSVGLength **aResult, nsSVGElement *aSVGElement)
  316. {
  317. RefPtr<DOMSVGLength> domAnimVal =
  318. DOMSVGLength::GetTearOff(this, aSVGElement, true);
  319. domAnimVal.forget(aResult);
  320. return NS_OK;
  321. }
  322. /* Implementation */
  323. nsresult
  324. nsSVGLength2::SetBaseValueString(const nsAString &aValueAsString,
  325. nsSVGElement *aSVGElement,
  326. bool aDoSetAttr)
  327. {
  328. float value;
  329. uint16_t unitType;
  330. if (!GetValueFromString(aValueAsString, value, &unitType)) {
  331. return NS_ERROR_DOM_SYNTAX_ERR;
  332. }
  333. if (mIsBaseSet && mBaseVal == float(value) &&
  334. mSpecifiedUnitType == uint8_t(unitType)) {
  335. return NS_OK;
  336. }
  337. nsAttrValue emptyOrOldValue;
  338. if (aDoSetAttr) {
  339. emptyOrOldValue = aSVGElement->WillChangeLength(mAttrEnum);
  340. }
  341. mBaseVal = value;
  342. mIsBaseSet = true;
  343. mSpecifiedUnitType = uint8_t(unitType);
  344. if (!mIsAnimated) {
  345. mAnimVal = mBaseVal;
  346. }
  347. else {
  348. aSVGElement->AnimationNeedsResample();
  349. }
  350. if (aDoSetAttr) {
  351. aSVGElement->DidChangeLength(mAttrEnum, emptyOrOldValue);
  352. }
  353. return NS_OK;
  354. }
  355. void
  356. nsSVGLength2::GetBaseValueString(nsAString & aValueAsString) const
  357. {
  358. GetValueString(aValueAsString, mBaseVal, mSpecifiedUnitType);
  359. }
  360. void
  361. nsSVGLength2::GetAnimValueString(nsAString & aValueAsString) const
  362. {
  363. GetValueString(aValueAsString, mAnimVal, mSpecifiedUnitType);
  364. }
  365. void
  366. nsSVGLength2::SetBaseValue(float aValue, nsSVGElement *aSVGElement,
  367. bool aDoSetAttr)
  368. {
  369. SetBaseValueInSpecifiedUnits(aValue * GetUnitScaleFactor(aSVGElement,
  370. mSpecifiedUnitType),
  371. aSVGElement, aDoSetAttr);
  372. }
  373. void
  374. nsSVGLength2::SetAnimValueInSpecifiedUnits(float aValue,
  375. nsSVGElement* aSVGElement)
  376. {
  377. if (mAnimVal == aValue && mIsAnimated) {
  378. return;
  379. }
  380. mAnimVal = aValue;
  381. mIsAnimated = true;
  382. aSVGElement->DidAnimateLength(mAttrEnum);
  383. }
  384. void
  385. nsSVGLength2::SetAnimValue(float aValue, nsSVGElement *aSVGElement)
  386. {
  387. SetAnimValueInSpecifiedUnits(aValue * GetUnitScaleFactor(aSVGElement,
  388. mSpecifiedUnitType),
  389. aSVGElement);
  390. }
  391. already_AddRefed<SVGAnimatedLength>
  392. nsSVGLength2::ToDOMAnimatedLength(nsSVGElement* aSVGElement)
  393. {
  394. RefPtr<SVGAnimatedLength> svgAnimatedLength =
  395. sSVGAnimatedLengthTearoffTable.GetTearoff(this);
  396. if (!svgAnimatedLength) {
  397. svgAnimatedLength = new SVGAnimatedLength(this, aSVGElement);
  398. sSVGAnimatedLengthTearoffTable.AddTearoff(this, svgAnimatedLength);
  399. }
  400. return svgAnimatedLength.forget();
  401. }
  402. SVGAnimatedLength::~SVGAnimatedLength()
  403. {
  404. sSVGAnimatedLengthTearoffTable.RemoveTearoff(mVal);
  405. }
  406. nsISMILAttr*
  407. nsSVGLength2::ToSMILAttr(nsSVGElement *aSVGElement)
  408. {
  409. return new SMILLength(this, aSVGElement);
  410. }
  411. nsresult
  412. nsSVGLength2::SMILLength::ValueFromString(const nsAString& aStr,
  413. const SVGAnimationElement* /*aSrcElement*/,
  414. nsSMILValue& aValue,
  415. bool& aPreventCachingOfSandwich) const
  416. {
  417. float value;
  418. uint16_t unitType;
  419. if (!GetValueFromString(aStr, value, &unitType)) {
  420. return NS_ERROR_DOM_SYNTAX_ERR;
  421. }
  422. nsSMILValue val(nsSMILFloatType::Singleton());
  423. val.mU.mDouble = value / mVal->GetUnitScaleFactor(mSVGElement, unitType);
  424. aValue = val;
  425. aPreventCachingOfSandwich =
  426. (unitType == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE ||
  427. unitType == nsIDOMSVGLength::SVG_LENGTHTYPE_EMS ||
  428. unitType == nsIDOMSVGLength::SVG_LENGTHTYPE_EXS);
  429. return NS_OK;
  430. }
  431. nsSMILValue
  432. nsSVGLength2::SMILLength::GetBaseValue() const
  433. {
  434. nsSMILValue val(nsSMILFloatType::Singleton());
  435. val.mU.mDouble = mVal->GetBaseValue(mSVGElement);
  436. return val;
  437. }
  438. void
  439. nsSVGLength2::SMILLength::ClearAnimValue()
  440. {
  441. if (mVal->mIsAnimated) {
  442. mVal->mIsAnimated = false;
  443. mVal->mAnimVal = mVal->mBaseVal;
  444. mSVGElement->DidAnimateLength(mVal->mAttrEnum);
  445. }
  446. }
  447. nsresult
  448. nsSVGLength2::SMILLength::SetAnimValue(const nsSMILValue& aValue)
  449. {
  450. NS_ASSERTION(aValue.mType == nsSMILFloatType::Singleton(),
  451. "Unexpected type to assign animated value");
  452. if (aValue.mType == nsSMILFloatType::Singleton()) {
  453. mVal->SetAnimValue(float(aValue.mU.mDouble), mSVGElement);
  454. }
  455. return NS_OK;
  456. }