SVGPathSegUtils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #ifndef MOZILLA_SVGPATHSEGUTILS_H__
  6. #define MOZILLA_SVGPATHSEGUTILS_H__
  7. #include "mozilla/ArrayUtils.h"
  8. #include "mozilla/gfx/Point.h"
  9. #include "nsDebug.h"
  10. namespace mozilla {
  11. // Path Segment Types
  12. static const unsigned short PATHSEG_UNKNOWN = 0;
  13. static const unsigned short PATHSEG_CLOSEPATH = 1;
  14. static const unsigned short PATHSEG_MOVETO_ABS = 2;
  15. static const unsigned short PATHSEG_MOVETO_REL = 3;
  16. static const unsigned short PATHSEG_LINETO_ABS = 4;
  17. static const unsigned short PATHSEG_LINETO_REL = 5;
  18. static const unsigned short PATHSEG_CURVETO_CUBIC_ABS = 6;
  19. static const unsigned short PATHSEG_CURVETO_CUBIC_REL = 7;
  20. static const unsigned short PATHSEG_CURVETO_QUADRATIC_ABS = 8;
  21. static const unsigned short PATHSEG_CURVETO_QUADRATIC_REL = 9;
  22. static const unsigned short PATHSEG_ARC_ABS = 10;
  23. static const unsigned short PATHSEG_ARC_REL = 11;
  24. static const unsigned short PATHSEG_LINETO_HORIZONTAL_ABS = 12;
  25. static const unsigned short PATHSEG_LINETO_HORIZONTAL_REL = 13;
  26. static const unsigned short PATHSEG_LINETO_VERTICAL_ABS = 14;
  27. static const unsigned short PATHSEG_LINETO_VERTICAL_REL = 15;
  28. static const unsigned short PATHSEG_CURVETO_CUBIC_SMOOTH_ABS = 16;
  29. static const unsigned short PATHSEG_CURVETO_CUBIC_SMOOTH_REL = 17;
  30. static const unsigned short PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS = 18;
  31. static const unsigned short PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL = 19;
  32. #define NS_SVG_PATH_SEG_MAX_ARGS 7
  33. #define NS_SVG_PATH_SEG_FIRST_VALID_TYPE mozilla::PATHSEG_CLOSEPATH
  34. #define NS_SVG_PATH_SEG_LAST_VALID_TYPE mozilla::PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL
  35. #define NS_SVG_PATH_SEG_TYPE_COUNT (NS_SVG_PATH_SEG_LAST_VALID_TYPE + 1)
  36. /**
  37. * Code that works with path segments can use an instance of this class to
  38. * store/provide information about the start of the current subpath and the
  39. * last path segment (if any).
  40. */
  41. struct SVGPathTraversalState
  42. {
  43. typedef gfx::Point Point;
  44. enum TraversalMode {
  45. eUpdateAll,
  46. eUpdateOnlyStartAndCurrentPos
  47. };
  48. SVGPathTraversalState()
  49. : start(0.0, 0.0)
  50. , pos(0.0, 0.0)
  51. , cp1(0.0, 0.0)
  52. , cp2(0.0, 0.0)
  53. , length(0.0)
  54. , mode(eUpdateAll)
  55. {}
  56. bool ShouldUpdateLengthAndControlPoints() { return mode == eUpdateAll; }
  57. Point start; // start point of current sub path (reset each moveto)
  58. Point pos; // current position (end point of previous segment)
  59. Point cp1; // quadratic control point - if the previous segment was a
  60. // quadratic bezier curve then this is set to the absolute
  61. // position of its control point, otherwise its set to pos
  62. Point cp2; // cubic control point - if the previous segment was a cubic
  63. // bezier curve then this is set to the absolute position of
  64. // its second control point, otherwise it's set to pos
  65. float length; // accumulated path length
  66. TraversalMode mode; // indicates what to track while traversing a path
  67. };
  68. /**
  69. * This class is just a collection of static methods - it doesn't have any data
  70. * members, and it's not possible to create instances of this class. This class
  71. * exists purely as a convenient place to gather together a bunch of methods
  72. * related to manipulating and answering questions about path segments.
  73. * Internally we represent path segments purely as an array of floats. See the
  74. * comment documenting SVGPathData for more info on that.
  75. *
  76. * The DOM wrapper classes for encoded path segments (data contained in
  77. * instances of SVGPathData) is DOMSVGPathSeg and its sub-classes. Note that
  78. * there are multiple different DOM classes for path segs - one for each of the
  79. * 19 SVG 1.1 segment types.
  80. */
  81. class SVGPathSegUtils
  82. {
  83. private:
  84. SVGPathSegUtils(){} // private to prevent instances
  85. public:
  86. static void GetValueAsString(const float *aSeg, nsAString& aValue);
  87. /**
  88. * Encode a segment type enum to a float.
  89. *
  90. * At some point in the future we will likely want to encode other
  91. * information into the float, such as whether the command was explicit or
  92. * not. For now all this method does is save on int to float runtime
  93. * conversion by requiring uint32_t and float to be of the same size so we
  94. * can simply do a bitwise uint32_t<->float copy.
  95. */
  96. static float EncodeType(uint32_t aType) {
  97. static_assert(sizeof(uint32_t) == sizeof(float), "sizeof uint32_t and float must be the same");
  98. MOZ_ASSERT(IsValidType(aType), "Seg type not recognized");
  99. return *(reinterpret_cast<float*>(&aType));
  100. }
  101. static uint32_t DecodeType(float aType) {
  102. static_assert(sizeof(uint32_t) == sizeof(float), "sizeof uint32_t and float must be the same");
  103. uint32_t type = *(reinterpret_cast<uint32_t*>(&aType));
  104. MOZ_ASSERT(IsValidType(type), "Seg type not recognized");
  105. return type;
  106. }
  107. static char16_t GetPathSegTypeAsLetter(uint32_t aType) {
  108. MOZ_ASSERT(IsValidType(aType), "Seg type not recognized");
  109. static const char16_t table[] = {
  110. char16_t('x'), // 0 == PATHSEG_UNKNOWN
  111. char16_t('z'), // 1 == PATHSEG_CLOSEPATH
  112. char16_t('M'), // 2 == PATHSEG_MOVETO_ABS
  113. char16_t('m'), // 3 == PATHSEG_MOVETO_REL
  114. char16_t('L'), // 4 == PATHSEG_LINETO_ABS
  115. char16_t('l'), // 5 == PATHSEG_LINETO_REL
  116. char16_t('C'), // 6 == PATHSEG_CURVETO_CUBIC_ABS
  117. char16_t('c'), // 7 == PATHSEG_CURVETO_CUBIC_REL
  118. char16_t('Q'), // 8 == PATHSEG_CURVETO_QUADRATIC_ABS
  119. char16_t('q'), // 9 == PATHSEG_CURVETO_QUADRATIC_REL
  120. char16_t('A'), // 10 == PATHSEG_ARC_ABS
  121. char16_t('a'), // 11 == PATHSEG_ARC_REL
  122. char16_t('H'), // 12 == PATHSEG_LINETO_HORIZONTAL_ABS
  123. char16_t('h'), // 13 == PATHSEG_LINETO_HORIZONTAL_REL
  124. char16_t('V'), // 14 == PATHSEG_LINETO_VERTICAL_ABS
  125. char16_t('v'), // 15 == PATHSEG_LINETO_VERTICAL_REL
  126. char16_t('S'), // 16 == PATHSEG_CURVETO_CUBIC_SMOOTH_ABS
  127. char16_t('s'), // 17 == PATHSEG_CURVETO_CUBIC_SMOOTH_REL
  128. char16_t('T'), // 18 == PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS
  129. char16_t('t') // 19 == PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL
  130. };
  131. static_assert(MOZ_ARRAY_LENGTH(table) == NS_SVG_PATH_SEG_TYPE_COUNT, "Unexpected table size");
  132. return table[aType];
  133. }
  134. static uint32_t ArgCountForType(uint32_t aType) {
  135. MOZ_ASSERT(IsValidType(aType), "Seg type not recognized");
  136. static const uint8_t table[] = {
  137. 0, // 0 == PATHSEG_UNKNOWN
  138. 0, // 1 == PATHSEG_CLOSEPATH
  139. 2, // 2 == PATHSEG_MOVETO_ABS
  140. 2, // 3 == PATHSEG_MOVETO_REL
  141. 2, // 4 == PATHSEG_LINETO_ABS
  142. 2, // 5 == PATHSEG_LINETO_REL
  143. 6, // 6 == PATHSEG_CURVETO_CUBIC_ABS
  144. 6, // 7 == PATHSEG_CURVETO_CUBIC_REL
  145. 4, // 8 == PATHSEG_CURVETO_QUADRATIC_ABS
  146. 4, // 9 == PATHSEG_CURVETO_QUADRATIC_REL
  147. 7, // 10 == PATHSEG_ARC_ABS
  148. 7, // 11 == PATHSEG_ARC_REL
  149. 1, // 12 == PATHSEG_LINETO_HORIZONTAL_ABS
  150. 1, // 13 == PATHSEG_LINETO_HORIZONTAL_REL
  151. 1, // 14 == PATHSEG_LINETO_VERTICAL_ABS
  152. 1, // 15 == PATHSEG_LINETO_VERTICAL_REL
  153. 4, // 16 == PATHSEG_CURVETO_CUBIC_SMOOTH_ABS
  154. 4, // 17 == PATHSEG_CURVETO_CUBIC_SMOOTH_REL
  155. 2, // 18 == PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS
  156. 2 // 19 == PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL
  157. };
  158. static_assert(MOZ_ARRAY_LENGTH(table) == NS_SVG_PATH_SEG_TYPE_COUNT, "Unexpected table size");
  159. return table[aType];
  160. }
  161. /**
  162. * Convenience so that callers can pass a float containing an encoded type
  163. * and have it decoded implicitly.
  164. */
  165. static uint32_t ArgCountForType(float aType) {
  166. return ArgCountForType(DecodeType(aType));
  167. }
  168. static bool IsValidType(uint32_t aType) {
  169. return aType >= NS_SVG_PATH_SEG_FIRST_VALID_TYPE &&
  170. aType <= NS_SVG_PATH_SEG_LAST_VALID_TYPE;
  171. }
  172. static bool IsCubicType(uint32_t aType) {
  173. return aType == PATHSEG_CURVETO_CUBIC_REL ||
  174. aType == PATHSEG_CURVETO_CUBIC_ABS ||
  175. aType == PATHSEG_CURVETO_CUBIC_SMOOTH_REL ||
  176. aType == PATHSEG_CURVETO_CUBIC_SMOOTH_ABS;
  177. }
  178. static bool IsQuadraticType(uint32_t aType) {
  179. return aType == PATHSEG_CURVETO_QUADRATIC_REL ||
  180. aType == PATHSEG_CURVETO_QUADRATIC_ABS ||
  181. aType == PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL ||
  182. aType == PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS;
  183. }
  184. static bool IsArcType(uint32_t aType) {
  185. return aType == PATHSEG_ARC_ABS ||
  186. aType == PATHSEG_ARC_REL;
  187. }
  188. static bool IsRelativeOrAbsoluteType(uint32_t aType) {
  189. MOZ_ASSERT(IsValidType(aType), "Seg type not recognized");
  190. // When adding a new path segment type, ensure that the returned condition
  191. // below is still correct.
  192. static_assert(NS_SVG_PATH_SEG_LAST_VALID_TYPE == PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL,
  193. "Unexpected type");
  194. return aType >= PATHSEG_MOVETO_ABS;
  195. }
  196. static bool IsRelativeType(uint32_t aType) {
  197. MOZ_ASSERT
  198. (IsRelativeOrAbsoluteType(aType),
  199. "IsRelativeType called with segment type that does not come in relative and absolute forms");
  200. // When adding a new path segment type, ensure that the returned condition
  201. // below is still correct.
  202. static_assert(NS_SVG_PATH_SEG_LAST_VALID_TYPE == PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL,
  203. "Unexpected type");
  204. return aType & 1;
  205. }
  206. static uint32_t RelativeVersionOfType(uint32_t aType) {
  207. MOZ_ASSERT
  208. (IsRelativeOrAbsoluteType(aType),
  209. "RelativeVersionOfType called with segment type that does not come in relative and absolute forms");
  210. // When adding a new path segment type, ensure that the returned condition
  211. // below is still correct.
  212. static_assert(NS_SVG_PATH_SEG_LAST_VALID_TYPE == PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL,
  213. "Unexpected type");
  214. return aType | 1;
  215. }
  216. static uint32_t SameTypeModuloRelativeness(uint32_t aType1, uint32_t aType2) {
  217. if (!IsRelativeOrAbsoluteType(aType1)) {
  218. return aType1 == aType2;
  219. }
  220. return RelativeVersionOfType(aType1) == RelativeVersionOfType(aType2);
  221. }
  222. /**
  223. * Traverse the given path segment and update the SVGPathTraversalState
  224. * object.
  225. */
  226. static void TraversePathSegment(const float* aData,
  227. SVGPathTraversalState& aState);
  228. };
  229. } // namespace mozilla
  230. #endif // MOZILLA_SVGPATHSEGUTILS_H__