2
0

utils.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.HTMLResult = void 0;
  36. const dimConverters = {
  37. pt: x => x,
  38. cm: x => x / 2.54 * 72,
  39. mm: x => x / (10 * 2.54) * 72,
  40. in: x => x * 72,
  41. px: x => x
  42. };
  43. const measurementPattern = /([+-]?[0-9]+\.?[0-9]*)(.*)/;
  44. function getInteger({
  45. data,
  46. defaultValue,
  47. validate
  48. }) {
  49. if (!data) {
  50. return defaultValue;
  51. }
  52. data = data.trim();
  53. const n = parseInt(data, 10);
  54. if (!isNaN(n) && validate(n)) {
  55. return n;
  56. }
  57. return defaultValue;
  58. }
  59. function getFloat({
  60. data,
  61. defaultValue,
  62. validate
  63. }) {
  64. if (!data) {
  65. return defaultValue;
  66. }
  67. data = data.trim();
  68. const n = parseFloat(data);
  69. if (!isNaN(n) && validate(n)) {
  70. return n;
  71. }
  72. return defaultValue;
  73. }
  74. function getKeyword({
  75. data,
  76. defaultValue,
  77. validate
  78. }) {
  79. if (!data) {
  80. return defaultValue;
  81. }
  82. data = data.trim();
  83. if (validate(data)) {
  84. return data;
  85. }
  86. return defaultValue;
  87. }
  88. function getStringOption(data, options) {
  89. return getKeyword({
  90. data,
  91. defaultValue: options[0],
  92. validate: k => options.includes(k)
  93. });
  94. }
  95. function getMeasurement(str, def = "0") {
  96. def = def || "0";
  97. if (!str) {
  98. return getMeasurement(def);
  99. }
  100. const match = str.trim().match(measurementPattern);
  101. if (!match) {
  102. return getMeasurement(def);
  103. }
  104. const [, valueStr, unit] = match;
  105. const value = parseFloat(valueStr);
  106. if (isNaN(value)) {
  107. return getMeasurement(def);
  108. }
  109. if (value === 0) {
  110. return 0;
  111. }
  112. const conv = dimConverters[unit];
  113. if (conv) {
  114. return conv(value);
  115. }
  116. return value;
  117. }
  118. function getRatio(data) {
  119. if (!data) {
  120. return {
  121. num: 1,
  122. den: 1
  123. };
  124. }
  125. const ratio = data.trim().split(/\s*:\s*/).map(x => parseFloat(x)).filter(x => !isNaN(x));
  126. if (ratio.length === 1) {
  127. ratio.push(1);
  128. }
  129. if (ratio.length === 0) {
  130. return {
  131. num: 1,
  132. den: 1
  133. };
  134. }
  135. const [num, den] = ratio;
  136. return {
  137. num,
  138. den
  139. };
  140. }
  141. function getRelevant(data) {
  142. if (!data) {
  143. return [];
  144. }
  145. return data.trim().split(/\s+/).map(e => {
  146. return {
  147. excluded: e[0] === "-",
  148. viewname: e.substring(1)
  149. };
  150. });
  151. }
  152. function getColor(data, def = [0, 0, 0]) {
  153. let [r, g, b] = def;
  154. if (!data) {
  155. return {
  156. r,
  157. g,
  158. b
  159. };
  160. }
  161. 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);
  162. if (color.length < 3) {
  163. return {
  164. r,
  165. g,
  166. b
  167. };
  168. }
  169. [r, g, b] = color;
  170. return {
  171. r,
  172. g,
  173. b
  174. };
  175. }
  176. function getBBox(data) {
  177. const def = -1;
  178. if (!data) {
  179. return {
  180. x: def,
  181. y: def,
  182. width: def,
  183. height: def
  184. };
  185. }
  186. const bbox = data.trim().split(/\s*,\s*/).map(m => getMeasurement(m, "-1"));
  187. if (bbox.length < 4 || bbox[2] < 0 || bbox[3] < 0) {
  188. return {
  189. x: def,
  190. y: def,
  191. width: def,
  192. height: def
  193. };
  194. }
  195. const [x, y, width, height] = bbox;
  196. return {
  197. x,
  198. y,
  199. width,
  200. height
  201. };
  202. }
  203. class HTMLResult {
  204. constructor(success, html, bbox) {
  205. this.success = success;
  206. this.html = html;
  207. this.bbox = bbox;
  208. }
  209. static success(html, bbox = null) {
  210. return new HTMLResult(true, html, bbox);
  211. }
  212. }
  213. exports.HTMLResult = HTMLResult;
  214. HTMLResult.FAILURE = new HTMLResult(false, null, null);
  215. HTMLResult.EMPTY = new HTMLResult(true, null, null);