convertTransform.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. 'use strict';
  2. /**
  3. * @typedef {import('../lib/types').XastElement} XastElement
  4. */
  5. const { cleanupOutData } = require('../lib/svgo/tools.js');
  6. const {
  7. transform2js,
  8. transformsMultiply,
  9. matrixToTransform,
  10. } = require('./_transforms.js');
  11. exports.type = 'visitor';
  12. exports.name = 'convertTransform';
  13. exports.active = true;
  14. exports.description = 'collapses multiple transformations and optimizes it';
  15. /**
  16. * Convert matrices to the short aliases,
  17. * convert long translate, scale or rotate transform notations to the shorts ones,
  18. * convert transforms to the matrices and multiply them all into one,
  19. * remove useless transforms.
  20. *
  21. * @see https://www.w3.org/TR/SVG11/coords.html#TransformMatrixDefined
  22. *
  23. * @author Kir Belevich
  24. *
  25. * @type {import('../lib/types').Plugin<{
  26. * convertToShorts?: boolean,
  27. * degPrecision?: number,
  28. * floatPrecision?: number,
  29. * transformPrecision?: number,
  30. * matrixToTransform?: boolean,
  31. * shortTranslate?: boolean,
  32. * shortScale?: boolean,
  33. * shortRotate?: boolean,
  34. * removeUseless?: boolean,
  35. * collapseIntoOne?: boolean,
  36. * leadingZero?: boolean,
  37. * negativeExtraSpace?: boolean,
  38. * }>}
  39. */
  40. exports.fn = (_root, params) => {
  41. const {
  42. convertToShorts = true,
  43. // degPrecision = 3, // transformPrecision (or matrix precision) - 2 by default
  44. degPrecision,
  45. floatPrecision = 3,
  46. transformPrecision = 5,
  47. matrixToTransform = true,
  48. shortTranslate = true,
  49. shortScale = true,
  50. shortRotate = true,
  51. removeUseless = true,
  52. collapseIntoOne = true,
  53. leadingZero = true,
  54. negativeExtraSpace = false,
  55. } = params;
  56. const newParams = {
  57. convertToShorts,
  58. degPrecision,
  59. floatPrecision,
  60. transformPrecision,
  61. matrixToTransform,
  62. shortTranslate,
  63. shortScale,
  64. shortRotate,
  65. removeUseless,
  66. collapseIntoOne,
  67. leadingZero,
  68. negativeExtraSpace,
  69. };
  70. return {
  71. element: {
  72. enter: (node) => {
  73. // transform
  74. if (node.attributes.transform != null) {
  75. convertTransform(node, 'transform', newParams);
  76. }
  77. // gradientTransform
  78. if (node.attributes.gradientTransform != null) {
  79. convertTransform(node, 'gradientTransform', newParams);
  80. }
  81. // patternTransform
  82. if (node.attributes.patternTransform != null) {
  83. convertTransform(node, 'patternTransform', newParams);
  84. }
  85. },
  86. },
  87. };
  88. };
  89. /**
  90. * @typedef {{
  91. * convertToShorts: boolean,
  92. * degPrecision?: number,
  93. * floatPrecision: number,
  94. * transformPrecision: number,
  95. * matrixToTransform: boolean,
  96. * shortTranslate: boolean,
  97. * shortScale: boolean,
  98. * shortRotate: boolean,
  99. * removeUseless: boolean,
  100. * collapseIntoOne: boolean,
  101. * leadingZero: boolean,
  102. * negativeExtraSpace: boolean,
  103. * }} TransformParams
  104. */
  105. /**
  106. * @typedef {{ name: string, data: Array<number> }} TransformItem
  107. */
  108. /**
  109. * Main function.
  110. *
  111. * @type {(item: XastElement, attrName: string, params: TransformParams) => void}
  112. */
  113. const convertTransform = (item, attrName, params) => {
  114. let data = transform2js(item.attributes[attrName]);
  115. params = definePrecision(data, params);
  116. if (params.collapseIntoOne && data.length > 1) {
  117. data = [transformsMultiply(data)];
  118. }
  119. if (params.convertToShorts) {
  120. data = convertToShorts(data, params);
  121. } else {
  122. data.forEach((item) => roundTransform(item, params));
  123. }
  124. if (params.removeUseless) {
  125. data = removeUseless(data);
  126. }
  127. if (data.length) {
  128. item.attributes[attrName] = js2transform(data, params);
  129. } else {
  130. delete item.attributes[attrName];
  131. }
  132. };
  133. /**
  134. * Defines precision to work with certain parts.
  135. * transformPrecision - for scale and four first matrix parameters (needs a better precision due to multiplying),
  136. * floatPrecision - for translate including two last matrix and rotate parameters,
  137. * degPrecision - for rotate and skew. By default it's equal to (rougly)
  138. * transformPrecision - 2 or floatPrecision whichever is lower. Can be set in params.
  139. *
  140. * @type {(data: Array<TransformItem>, params: TransformParams) => TransformParams}
  141. *
  142. * clone params so it don't affect other elements transformations.
  143. */
  144. const definePrecision = (data, { ...newParams }) => {
  145. const matrixData = [];
  146. for (const item of data) {
  147. if (item.name == 'matrix') {
  148. matrixData.push(...item.data.slice(0, 4));
  149. }
  150. }
  151. let significantDigits = newParams.transformPrecision;
  152. // Limit transform precision with matrix one. Calculating with larger precision doesn't add any value.
  153. if (matrixData.length) {
  154. newParams.transformPrecision = Math.min(
  155. newParams.transformPrecision,
  156. Math.max.apply(Math, matrixData.map(floatDigits)) ||
  157. newParams.transformPrecision
  158. );
  159. significantDigits = Math.max.apply(
  160. Math,
  161. matrixData.map(
  162. (n) => n.toString().replace(/\D+/g, '').length // Number of digits in a number. 123.45 → 5
  163. )
  164. );
  165. }
  166. // No sense in angle precision more then number of significant digits in matrix.
  167. if (newParams.degPrecision == null) {
  168. newParams.degPrecision = Math.max(
  169. 0,
  170. Math.min(newParams.floatPrecision, significantDigits - 2)
  171. );
  172. }
  173. return newParams;
  174. };
  175. /**
  176. * @type {(data: Array<number>, params: TransformParams) => Array<number>}
  177. */
  178. const degRound = (data, params) => {
  179. if (
  180. params.degPrecision != null &&
  181. params.degPrecision >= 1 &&
  182. params.floatPrecision < 20
  183. ) {
  184. return smartRound(params.degPrecision, data);
  185. } else {
  186. return round(data);
  187. }
  188. };
  189. /**
  190. * @type {(data: Array<number>, params: TransformParams) => Array<number>}
  191. */
  192. const floatRound = (data, params) => {
  193. if (params.floatPrecision >= 1 && params.floatPrecision < 20) {
  194. return smartRound(params.floatPrecision, data);
  195. } else {
  196. return round(data);
  197. }
  198. };
  199. /**
  200. * @type {(data: Array<number>, params: TransformParams) => Array<number>}
  201. */
  202. const transformRound = (data, params) => {
  203. if (params.transformPrecision >= 1 && params.floatPrecision < 20) {
  204. return smartRound(params.transformPrecision, data);
  205. } else {
  206. return round(data);
  207. }
  208. };
  209. /**
  210. * Returns number of digits after the point. 0.125 → 3
  211. *
  212. * @type {(n: number) => number}
  213. */
  214. const floatDigits = (n) => {
  215. const str = n.toString();
  216. return str.slice(str.indexOf('.')).length - 1;
  217. };
  218. /**
  219. * Convert transforms to the shorthand alternatives.
  220. *
  221. * @type {(transforms: Array<TransformItem>, params: TransformParams) => Array<TransformItem>}
  222. */
  223. const convertToShorts = (transforms, params) => {
  224. for (var i = 0; i < transforms.length; i++) {
  225. var transform = transforms[i];
  226. // convert matrix to the short aliases
  227. if (params.matrixToTransform && transform.name === 'matrix') {
  228. var decomposed = matrixToTransform(transform, params);
  229. if (
  230. js2transform(decomposed, params).length <=
  231. js2transform([transform], params).length
  232. ) {
  233. transforms.splice(i, 1, ...decomposed);
  234. }
  235. transform = transforms[i];
  236. }
  237. // fixed-point numbers
  238. // 12.754997 → 12.755
  239. roundTransform(transform, params);
  240. // convert long translate transform notation to the shorts one
  241. // translate(10 0) → translate(10)
  242. if (
  243. params.shortTranslate &&
  244. transform.name === 'translate' &&
  245. transform.data.length === 2 &&
  246. !transform.data[1]
  247. ) {
  248. transform.data.pop();
  249. }
  250. // convert long scale transform notation to the shorts one
  251. // scale(2 2) → scale(2)
  252. if (
  253. params.shortScale &&
  254. transform.name === 'scale' &&
  255. transform.data.length === 2 &&
  256. transform.data[0] === transform.data[1]
  257. ) {
  258. transform.data.pop();
  259. }
  260. // convert long rotate transform notation to the short one
  261. // translate(cx cy) rotate(a) translate(-cx -cy) → rotate(a cx cy)
  262. if (
  263. params.shortRotate &&
  264. transforms[i - 2] &&
  265. transforms[i - 2].name === 'translate' &&
  266. transforms[i - 1].name === 'rotate' &&
  267. transforms[i].name === 'translate' &&
  268. transforms[i - 2].data[0] === -transforms[i].data[0] &&
  269. transforms[i - 2].data[1] === -transforms[i].data[1]
  270. ) {
  271. transforms.splice(i - 2, 3, {
  272. name: 'rotate',
  273. data: [
  274. transforms[i - 1].data[0],
  275. transforms[i - 2].data[0],
  276. transforms[i - 2].data[1],
  277. ],
  278. });
  279. // splice compensation
  280. i -= 2;
  281. }
  282. }
  283. return transforms;
  284. };
  285. /**
  286. * Remove useless transforms.
  287. *
  288. * @type {(trasforms: Array<TransformItem>) => Array<TransformItem>}
  289. */
  290. const removeUseless = (transforms) => {
  291. return transforms.filter((transform) => {
  292. // translate(0), rotate(0[, cx, cy]), skewX(0), skewY(0)
  293. if (
  294. (['translate', 'rotate', 'skewX', 'skewY'].indexOf(transform.name) > -1 &&
  295. (transform.data.length == 1 || transform.name == 'rotate') &&
  296. !transform.data[0]) ||
  297. // translate(0, 0)
  298. (transform.name == 'translate' &&
  299. !transform.data[0] &&
  300. !transform.data[1]) ||
  301. // scale(1)
  302. (transform.name == 'scale' &&
  303. transform.data[0] == 1 &&
  304. (transform.data.length < 2 || transform.data[1] == 1)) ||
  305. // matrix(1 0 0 1 0 0)
  306. (transform.name == 'matrix' &&
  307. transform.data[0] == 1 &&
  308. transform.data[3] == 1 &&
  309. !(
  310. transform.data[1] ||
  311. transform.data[2] ||
  312. transform.data[4] ||
  313. transform.data[5]
  314. ))
  315. ) {
  316. return false;
  317. }
  318. return true;
  319. });
  320. };
  321. /**
  322. * Convert transforms JS representation to string.
  323. *
  324. * @type {(transformJS: Array<TransformItem>, params: TransformParams) => string}
  325. */
  326. const js2transform = (transformJS, params) => {
  327. var transformString = '';
  328. // collect output value string
  329. transformJS.forEach((transform) => {
  330. roundTransform(transform, params);
  331. transformString +=
  332. (transformString && ' ') +
  333. transform.name +
  334. '(' +
  335. cleanupOutData(transform.data, params) +
  336. ')';
  337. });
  338. return transformString;
  339. };
  340. /**
  341. * @type {(transform: TransformItem, params: TransformParams) => TransformItem}
  342. */
  343. const roundTransform = (transform, params) => {
  344. switch (transform.name) {
  345. case 'translate':
  346. transform.data = floatRound(transform.data, params);
  347. break;
  348. case 'rotate':
  349. transform.data = [
  350. ...degRound(transform.data.slice(0, 1), params),
  351. ...floatRound(transform.data.slice(1), params),
  352. ];
  353. break;
  354. case 'skewX':
  355. case 'skewY':
  356. transform.data = degRound(transform.data, params);
  357. break;
  358. case 'scale':
  359. transform.data = transformRound(transform.data, params);
  360. break;
  361. case 'matrix':
  362. transform.data = [
  363. ...transformRound(transform.data.slice(0, 4), params),
  364. ...floatRound(transform.data.slice(4), params),
  365. ];
  366. break;
  367. }
  368. return transform;
  369. };
  370. /**
  371. * Rounds numbers in array.
  372. *
  373. * @type {(data: Array<number>) => Array<number>}
  374. */
  375. const round = (data) => {
  376. return data.map(Math.round);
  377. };
  378. /**
  379. * Decrease accuracy of floating-point numbers
  380. * in transforms keeping a specified number of decimals.
  381. * Smart rounds values like 2.349 to 2.35.
  382. *
  383. * @type {(precision: number, data: Array<number>) => Array<number>}
  384. */
  385. const smartRound = (precision, data) => {
  386. for (
  387. var i = data.length,
  388. tolerance = +Math.pow(0.1, precision).toFixed(precision);
  389. i--;
  390. ) {
  391. if (Number(data[i].toFixed(precision)) !== data[i]) {
  392. var rounded = +data[i].toFixed(precision - 1);
  393. data[i] =
  394. +Math.abs(rounded - data[i]).toFixed(precision + 1) >= tolerance
  395. ? +data[i].toFixed(precision)
  396. : rounded;
  397. }
  398. }
  399. return data;
  400. };