lengths.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.px = exports.emRounded = exports.em = exports.percent = exports.length2em = exports.MATHSPACE = exports.RELUNITS = exports.UNITS = exports.BIGDIMEN = void 0;
  4. exports.BIGDIMEN = 1000000;
  5. exports.UNITS = {
  6. px: 1,
  7. 'in': 96,
  8. cm: 96 / 2.54,
  9. mm: 96 / 25.4
  10. };
  11. exports.RELUNITS = {
  12. em: 1,
  13. ex: .431,
  14. pt: 1 / 10,
  15. pc: 12 / 10,
  16. mu: 1 / 18
  17. };
  18. exports.MATHSPACE = {
  19. veryverythinmathspace: 1 / 18,
  20. verythinmathspace: 2 / 18,
  21. thinmathspace: 3 / 18,
  22. mediummathspace: 4 / 18,
  23. thickmathspace: 5 / 18,
  24. verythickmathspace: 6 / 18,
  25. veryverythickmathspace: 7 / 18,
  26. negativeveryverythinmathspace: -1 / 18,
  27. negativeverythinmathspace: -2 / 18,
  28. negativethinmathspace: -3 / 18,
  29. negativemediummathspace: -4 / 18,
  30. negativethickmathspace: -5 / 18,
  31. negativeverythickmathspace: -6 / 18,
  32. negativeveryverythickmathspace: -7 / 18,
  33. thin: .04,
  34. medium: .06,
  35. thick: .1,
  36. normal: 1,
  37. big: 2,
  38. small: 1 / Math.sqrt(2),
  39. infinity: exports.BIGDIMEN
  40. };
  41. function length2em(length, size, scale, em) {
  42. if (size === void 0) { size = 0; }
  43. if (scale === void 0) { scale = 1; }
  44. if (em === void 0) { em = 16; }
  45. if (typeof length !== 'string') {
  46. length = String(length);
  47. }
  48. if (length === '' || length == null) {
  49. return size;
  50. }
  51. if (exports.MATHSPACE[length]) {
  52. return exports.MATHSPACE[length];
  53. }
  54. var match = length.match(/^\s*([-+]?(?:\.\d+|\d+(?:\.\d*)?))?(pt|em|ex|mu|px|pc|in|mm|cm|%)?/);
  55. if (!match) {
  56. return size;
  57. }
  58. var m = parseFloat(match[1] || '1'), unit = match[2];
  59. if (exports.UNITS.hasOwnProperty(unit)) {
  60. return m * exports.UNITS[unit] / em / scale;
  61. }
  62. if (exports.RELUNITS.hasOwnProperty(unit)) {
  63. return m * exports.RELUNITS[unit];
  64. }
  65. if (unit === '%') {
  66. return m / 100 * size;
  67. }
  68. return m * size;
  69. }
  70. exports.length2em = length2em;
  71. function percent(m) {
  72. return (100 * m).toFixed(1).replace(/\.?0+$/, '') + '%';
  73. }
  74. exports.percent = percent;
  75. function em(m) {
  76. if (Math.abs(m) < .001)
  77. return '0';
  78. return (m.toFixed(3).replace(/\.?0+$/, '')) + 'em';
  79. }
  80. exports.em = em;
  81. function emRounded(m, em) {
  82. if (em === void 0) { em = 16; }
  83. m = (Math.round(m * em) + .05) / em;
  84. if (Math.abs(m) < .001)
  85. return '0em';
  86. return m.toFixed(3).replace(/\.?0+$/, '') + 'em';
  87. }
  88. exports.emRounded = emRounded;
  89. function px(m, M, em) {
  90. if (M === void 0) { M = -exports.BIGDIMEN; }
  91. if (em === void 0) { em = 16; }
  92. m *= em;
  93. if (M && m < M)
  94. m = M;
  95. if (Math.abs(m) < .1)
  96. return '0';
  97. return m.toFixed(1).replace(/\.0$/, '') + 'px';
  98. }
  99. exports.px = px;
  100. //# sourceMappingURL=lengths.js.map