utils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.getBBox = getBBox;
  27. exports.getColor = getColor;
  28. exports.getFloat = getFloat;
  29. exports.getInteger = getInteger;
  30. exports.getKeyword = getKeyword;
  31. exports.getMeasurement = getMeasurement;
  32. exports.getRatio = getRatio;
  33. exports.getRelevant = getRelevant;
  34. exports.getStringOption = getStringOption;
  35. exports.stripQuotes = stripQuotes;
  36. exports.HTMLResult = void 0;
  37. var _util = require("../../shared/util.js");
  38. const dimConverters = {
  39. pt: x => x,
  40. cm: x => x / 2.54 * 72,
  41. mm: x => x / (10 * 2.54) * 72,
  42. in: x => x * 72,
  43. px: x => x
  44. };
  45. const measurementPattern = /([+-]?[0-9]+\.?[0-9]*)(.*)/;
  46. function stripQuotes(str) {
  47. if (str.startsWith("'") || str.startsWith('"')) {
  48. return str.slice(1, str.length - 1);
  49. }
  50. return str;
  51. }
  52. function getInteger({
  53. data,
  54. defaultValue,
  55. validate
  56. }) {
  57. if (!data) {
  58. return defaultValue;
  59. }
  60. data = data.trim();
  61. const n = parseInt(data, 10);
  62. if (!isNaN(n) && validate(n)) {
  63. return n;
  64. }
  65. return defaultValue;
  66. }
  67. function getFloat({
  68. data,
  69. defaultValue,
  70. validate
  71. }) {
  72. if (!data) {
  73. return defaultValue;
  74. }
  75. data = data.trim();
  76. const n = parseFloat(data);
  77. if (!isNaN(n) && validate(n)) {
  78. return n;
  79. }
  80. return defaultValue;
  81. }
  82. function getKeyword({
  83. data,
  84. defaultValue,
  85. validate
  86. }) {
  87. if (!data) {
  88. return defaultValue;
  89. }
  90. data = data.trim();
  91. if (validate(data)) {
  92. return data;
  93. }
  94. return defaultValue;
  95. }
  96. function getStringOption(data, options) {
  97. return getKeyword({
  98. data,
  99. defaultValue: options[0],
  100. validate: k => options.includes(k)
  101. });
  102. }
  103. function getMeasurement(str, def = "0") {
  104. def = def || "0";
  105. if (!str) {
  106. return getMeasurement(def);
  107. }
  108. const match = str.trim().match(measurementPattern);
  109. if (!match) {
  110. return getMeasurement(def);
  111. }
  112. const [, valueStr, unit] = match;
  113. const value = parseFloat(valueStr);
  114. if (isNaN(value)) {
  115. return getMeasurement(def);
  116. }
  117. if (value === 0) {
  118. return 0;
  119. }
  120. const conv = dimConverters[unit];
  121. if (conv) {
  122. return conv(value);
  123. }
  124. return value;
  125. }
  126. function getRatio(data) {
  127. if (!data) {
  128. return {
  129. num: 1,
  130. den: 1
  131. };
  132. }
  133. const ratio = data.trim().split(/\s*:\s*/).map(x => parseFloat(x)).filter(x => !isNaN(x));
  134. if (ratio.length === 1) {
  135. ratio.push(1);
  136. }
  137. if (ratio.length === 0) {
  138. return {
  139. num: 1,
  140. den: 1
  141. };
  142. }
  143. const [num, den] = ratio;
  144. return {
  145. num,
  146. den
  147. };
  148. }
  149. function getRelevant(data) {
  150. if (!data) {
  151. return [];
  152. }
  153. return data.trim().split(/\s+/).map(e => {
  154. return {
  155. excluded: e[0] === "-",
  156. viewname: e.substring(1)
  157. };
  158. });
  159. }
  160. function getColor(data, def = [0, 0, 0]) {
  161. let [r, g, b] = def;
  162. if (!data) {
  163. return {
  164. r,
  165. g,
  166. b
  167. };
  168. }
  169. const color = data.trim().split(/\s*,\s*/).map(c => Math.min(Math.max(0, parseInt(c.trim(), 10)), 255)).map(c => isNaN(c) ? 0 : c);
  170. if (color.length < 3) {
  171. return {
  172. r,
  173. g,
  174. b
  175. };
  176. }
  177. [r, g, b] = color;
  178. return {
  179. r,
  180. g,
  181. b
  182. };
  183. }
  184. function getBBox(data) {
  185. const def = -1;
  186. if (!data) {
  187. return {
  188. x: def,
  189. y: def,
  190. width: def,
  191. height: def
  192. };
  193. }
  194. const bbox = data.trim().split(/\s*,\s*/).map(m => getMeasurement(m, "-1"));
  195. if (bbox.length < 4 || bbox[2] < 0 || bbox[3] < 0) {
  196. return {
  197. x: def,
  198. y: def,
  199. width: def,
  200. height: def
  201. };
  202. }
  203. const [x, y, width, height] = bbox;
  204. return {
  205. x,
  206. y,
  207. width,
  208. height
  209. };
  210. }
  211. class HTMLResult {
  212. static get FAILURE() {
  213. return (0, _util.shadow)(this, "FAILURE", new HTMLResult(false, null, null, null));
  214. }
  215. static get EMPTY() {
  216. return (0, _util.shadow)(this, "EMPTY", new HTMLResult(true, null, null, null));
  217. }
  218. constructor(success, html, bbox, breakNode) {
  219. this.success = success;
  220. this.html = html;
  221. this.bbox = bbox;
  222. this.breakNode = breakNode;
  223. }
  224. isBreak() {
  225. return !!this.breakNode;
  226. }
  227. static breakNode(node) {
  228. return new HTMLResult(false, null, null, node);
  229. }
  230. static success(html, bbox = null) {
  231. return new HTMLResult(true, html, bbox, null);
  232. }
  233. }
  234. exports.HTMLResult = HTMLResult;