utils.js 4.0 KB

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