1 | /**
|
2 | * Sutton SignWriting TrueType Font Module v1.3.2 (https://github.com/sutton-signwriting/font-ttf)
|
3 | * Author: Steve Slevinski (https://SteveSlevinski.me)
|
4 | * index.js is released under the MIT License.
|
5 | */
|
6 |
|
7 | (function (global, factory) {
|
8 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
9 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
10 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.ssw = global.ssw || {}, global.ssw.ttf = {})));
|
11 | })(this, (function (exports) { 'use strict';
|
12 |
|
13 | /**
|
14 | * Function that appends font-face CSS for the Sutton SignWriting fonts for system installed fonts, relative directory fonts, or content delivery network
|
15 | * @function font.cssAppend
|
16 | * @param {string} dir - an optional relative directory for font location
|
17 | * @example
|
18 | * font.cssAppend('./font/')
|
19 | */
|
20 | const cssAppend = function (dir = '') {
|
21 | const id = "SgnwFontCss";
|
22 |
|
23 | if (!document.getElementById(id)) {
|
24 | const style = document.createElement('style');
|
25 | style.setAttribute("id", "SgnwFontCss");
|
26 | style.appendChild(document.createTextNode(`
|
27 | @font-face {
|
28 | font-family: "SuttonSignWritingLine";
|
29 | src:
|
30 | local('SuttonSignWritingLine'),
|
31 | ${dir ? `url('${dir}SuttonSignWritingLine.ttf') format('truetype'),` : ""}
|
32 | url('https://cdn.jsdelivr.net/npm/@sutton-signwriting/font-ttf@1.0.0/font/SuttonSignWritingLine.ttf') format('truetype');
|
33 | }
|
34 | @font-face {
|
35 | font-family: "SuttonSignWritingFill";
|
36 | src:
|
37 | local('SuttonSignWritingFill'),
|
38 | ${dir ? `url('${dir}SuttonSignWritingFill.ttf') format('truetype'),` : ""}
|
39 | url('https://cdn.jsdelivr.net/npm/@sutton-signwriting/font-ttf@1.0.0/font/SuttonSignWritingFill.ttf') format('truetype');
|
40 | }
|
41 | @font-face {
|
42 | font-family: "SuttonSignWritingOneD";
|
43 | src:
|
44 | local('SuttonSignWritingOneD'),
|
45 | ${dir ? `url('${dir}SuttonSignWritingOneD.ttf') format('truetype'),` : ""}
|
46 | url('https://cdn.jsdelivr.net/npm/@sutton-signwriting/font-ttf@1.0.0/font/SuttonSignWritingOneD.ttf') format('truetype');
|
47 | }
|
48 | `));
|
49 | document.head.appendChild(style);
|
50 | }
|
51 | };
|
52 |
|
53 | let sizes = {};
|
54 | const zoom = 2;
|
55 | const bound = 76 * zoom;
|
56 | let context;
|
57 | /**
|
58 | * Function that returns the size of a symbol using an id
|
59 | * @function font.symbolSize
|
60 | * @param {number} id - a 16-bit number of a symbol
|
61 | * @example
|
62 | * font.symbolSize(1)
|
63 | *
|
64 | * return [15,30]
|
65 | */
|
66 |
|
67 | const symbolSize$2 = function (id) {
|
68 | if (id in sizes) {
|
69 | return [...sizes[id]];
|
70 | }
|
71 |
|
72 | if (!context) {
|
73 | const canvaser = document.createElement("canvas");
|
74 | canvaser.width = bound;
|
75 | canvaser.height = bound;
|
76 | context = canvaser.getContext("2d");
|
77 | }
|
78 |
|
79 | context.clearRect(0, 0, bound, bound);
|
80 | context.font = 30 * zoom + "px 'SuttonSignWritingLine'";
|
81 | context.fillText(String.fromCodePoint(id + 0xF0000), 0, 0);
|
82 | const imgData = context.getImageData(0, 0, bound, bound).data;
|
83 | let w, h, i, s;
|
84 |
|
85 | wloop: for (w = bound - 1; w >= 0; w--) {
|
86 | for (h = 0; h < bound; h += 1) {
|
87 | for (s = 0; s < 4; s += 1) {
|
88 | i = w * 4 + h * 4 * bound + s;
|
89 |
|
90 | if (imgData[i]) {
|
91 | break wloop;
|
92 | }
|
93 | }
|
94 | }
|
95 | }
|
96 |
|
97 | var width = w;
|
98 |
|
99 | hloop: for (h = bound - 1; h >= 0; h--) {
|
100 | for (w = 0; w < width; w += 1) {
|
101 | for (s = 0; s < 4; s += 1) {
|
102 | i = w * 4 + h * 4 * bound + s;
|
103 |
|
104 | if (imgData[i]) {
|
105 | break hloop;
|
106 | }
|
107 | }
|
108 | }
|
109 | }
|
110 |
|
111 | var height = h + 1;
|
112 | width = Math.ceil(width / zoom);
|
113 | height = Math.ceil(height / zoom); // Rounding error in chrome. Manual fixes.
|
114 |
|
115 | if (14394 == id) {
|
116 | width = 19;
|
117 | }
|
118 |
|
119 | if ([10468, 10480, 10496, 10512, 10500, 10532, 10548, 10862, 10878, 10894, 11058, 11074, 11476, 11488, 11492, 11504, 11508, 11520, 10516, 10910, 10926, 11042, 11082, 10942].includes(id)) {
|
120 | width = 20;
|
121 | }
|
122 |
|
123 | if (31921 == id) {
|
124 | width = 22;
|
125 | }
|
126 |
|
127 | if (38460 == id) {
|
128 | width = 23;
|
129 | }
|
130 |
|
131 | if ([20164, 20212].includes(id)) {
|
132 | width = 25;
|
133 | }
|
134 |
|
135 | if (31894 == id) {
|
136 | width = 28;
|
137 | }
|
138 |
|
139 | if (46698 == id) {
|
140 | width = 29;
|
141 | }
|
142 |
|
143 | if (29606 == id) {
|
144 | width = 30;
|
145 | }
|
146 |
|
147 | if (44855 == id) {
|
148 | width = 40;
|
149 | }
|
150 |
|
151 | if (32667 == id) {
|
152 | width = 50;
|
153 | }
|
154 |
|
155 | if ([11088, 11474, 11490, 11506].includes(id)) {
|
156 | height = 20;
|
157 | }
|
158 |
|
159 | if (6285 == id) {
|
160 | height = 21;
|
161 | }
|
162 |
|
163 | if (40804 == id) {
|
164 | height = 31;
|
165 | }
|
166 |
|
167 | if (41475 == id) {
|
168 | height = 36;
|
169 | } // Error in chrome. Manual fix.
|
170 | // if (width==0 && height==0) {
|
171 |
|
172 |
|
173 | if (width == 0 && height == 0) {
|
174 | const sizefix = {
|
175 | 9: [15, 30],
|
176 | 10: [21, 30],
|
177 | 11: [30, 15],
|
178 | 12: [30, 21],
|
179 | 13: [15, 30],
|
180 | 14: [21, 30]
|
181 | };
|
182 |
|
183 | if (id in sizefix) {
|
184 | width = sizefix[id][0];
|
185 | height = sizefix[id][1];
|
186 | }
|
187 | }
|
188 |
|
189 | if (width == 0 && height == 0) {
|
190 | return undefined;
|
191 | }
|
192 |
|
193 | sizes[id] = [width, height];
|
194 | return [width, height];
|
195 | };
|
196 |
|
197 | /**
|
198 | * Function that returns a plane 15 character for a symbol line using an id
|
199 | * @function font.symbolLine
|
200 | * @param {number} id - a 16-bit number of a symbol
|
201 | * @example
|
202 | * font.symbolLine(1)
|
203 | *
|
204 | * return ''
|
205 | */
|
206 | const symbolLine$2 = function (id) {
|
207 | return String.fromCodePoint(id + 0xF0000);
|
208 | };
|
209 | /**
|
210 | * Function that returns a plane 16 character for a symbol fill using an id
|
211 | * @function font.symbolFill
|
212 | * @param {number} id - a 16-bit number of a symbol
|
213 | * @example
|
214 | * font.symbolFill(1)
|
215 | *
|
216 | * return ''
|
217 | */
|
218 |
|
219 |
|
220 | const symbolFill$2 = function (id) {
|
221 | return String.fromCodePoint(id + 0x100000);
|
222 | };
|
223 | /**
|
224 | * Function that creates two text elements for a symbol using an id
|
225 | * @function font.symbolText
|
226 | * @param {number} id - a 16-bit number of a symbol
|
227 | * @example
|
228 | * font.symbolText(1)
|
229 | *
|
230 | * return ` <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
231 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>`
|
232 | */
|
233 |
|
234 |
|
235 | const symbolText$2 = function (id) {
|
236 | return ` <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;">${symbolFill$2(id)}</text>
|
237 | <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;">${symbolLine$2(id)}</text>`;
|
238 | };
|
239 |
|
240 | /**
|
241 | * Function that executes a callback function once the Sutton SignWriiting Line and Fill fonts are ready to use
|
242 | * @function font.cssLoaded
|
243 | * @param {function} callback - a callback function to execute when fonts are ready
|
244 | * @example
|
245 | * const callback = () => {
|
246 | * console.log("Sutton SignWriting Line and Fill fonts are ready to use")
|
247 | * }
|
248 | *
|
249 | * font.cssLoaded( callback )
|
250 | */
|
251 |
|
252 | const cssLoaded = function (callback) {
|
253 | let lineReady = false;
|
254 | let fillReady = false;
|
255 | cssLoadedLine(() => {
|
256 | lineReady = true;
|
257 | });
|
258 | cssLoadedFill(() => {
|
259 | fillReady = true;
|
260 | });
|
261 | const cssCheck = setInterval(function () {
|
262 | if (lineReady && fillReady) {
|
263 | clearInterval(cssCheck);
|
264 | callback();
|
265 | }
|
266 | }, 100);
|
267 | };
|
268 | /**
|
269 | * Function that executes a callback function once the Sutton SignWriiting Line font is ready to use
|
270 | * @function font.cssLoadedLine
|
271 | * @param {function} callback - a callback function to execute when line font is ready
|
272 | * @example
|
273 | * const callback = () => {
|
274 | * console.log("Sutton SignWriting Line font is ready to use")
|
275 | * }
|
276 | *
|
277 | * font.cssLoadedLine( callback )
|
278 | */
|
279 |
|
280 |
|
281 | const cssLoadedLine = function (callback) {
|
282 | if (!symbolSize$2(1)) {
|
283 | const cssCheck = setInterval(function () {
|
284 | if (symbolSize$2(1)) {
|
285 | clearInterval(cssCheck);
|
286 | callback();
|
287 | }
|
288 | }, 100);
|
289 | } else {
|
290 | callback();
|
291 | }
|
292 | };
|
293 | /**
|
294 | * Function that executes a callback function once the Sutton SignWriiting Fill font is ready to use
|
295 | * @function font.cssLoadedFill
|
296 | * @param {function} callback - a callback function to execute when fill font is ready
|
297 | * @example
|
298 | * const callback = () => {
|
299 | * console.log("Sutton SignWriting Fill font is ready to use")
|
300 | * }
|
301 | *
|
302 | * font.cssLoadedFill( callback )
|
303 | */
|
304 |
|
305 |
|
306 | const cssLoadedFill = function (callback) {
|
307 | const fillReady = function () {
|
308 | const canvaser = document.createElement("canvas");
|
309 | canvaser.width = 15;
|
310 | canvaser.height = 30;
|
311 | const context = canvaser.getContext("2d");
|
312 | context.font = "30px 'SuttonSignWritingFill'";
|
313 | context.fillText(symbolFill$2(1), 0, 0);
|
314 | const imgData = context.getImageData(0, 0, 15, 30).data;
|
315 | return !imgData.every(item => item === 0);
|
316 | };
|
317 |
|
318 | if (!fillReady()) {
|
319 | const cssCheck = setInterval(function () {
|
320 | if (fillReady()) {
|
321 | clearInterval(cssCheck);
|
322 | callback();
|
323 | }
|
324 | }, 100);
|
325 | } else {
|
326 | callback();
|
327 | }
|
328 | };
|
329 |
|
330 | /** The font module contains functions for handing the TrueType fonts.
|
331 | * @module font
|
332 | */
|
333 |
|
334 | var index$2 = /*#__PURE__*/Object.freeze({
|
335 | __proto__: null,
|
336 | cssAppend: cssAppend,
|
337 | cssLoaded: cssLoaded,
|
338 | cssLoadedLine: cssLoadedLine,
|
339 | cssLoadedFill: cssLoadedFill,
|
340 | symbolSize: symbolSize$2,
|
341 | symbolLine: symbolLine$2,
|
342 | symbolFill: symbolFill$2,
|
343 | symbolText: symbolText$2
|
344 | });
|
345 |
|
346 | /**
|
347 | * Sutton SignWriting Core Module v1.4.2 (https://github.com/sutton-signwriting/core)
|
348 | * Author: Steve Slevinski (https://SteveSlevinski.me)
|
349 | * convert.mjs is released under the MIT License.
|
350 | */
|
351 | /**
|
352 | * Function to convert a number to an SWU number character
|
353 | * @function convert.num2swu
|
354 | * @param {number} num - Integer value for number
|
355 | * @returns {string} SWU number character
|
356 | * @example
|
357 | * convert.num2swu(500)
|
358 | *
|
359 | * return '𝤆'
|
360 | */
|
361 |
|
362 |
|
363 | const num2swu = num => String.fromCodePoint(0x1D80C + parseInt(num) - 250);
|
364 | /**
|
365 | * Function to convert an array of x,y integers to two SWU number characters
|
366 | * @function convert.coord2swu
|
367 | * @param {number[]} coord - Array of x,y integers
|
368 | * @returns {string} Two SWU number character
|
369 | * @example
|
370 | * convert.coord2swu([500, 500])
|
371 | *
|
372 | * return '𝤆𝤆'
|
373 | */
|
374 |
|
375 |
|
376 | const coord2swu = coord => coord.map(num => num2swu(num)).join('');
|
377 | /**
|
378 | * Function to convert an SWU symbol character to a code point on plane 4
|
379 | * @function convert.swu2code
|
380 | * @param {string} swuSym - SWU symbol character
|
381 | * @returns {number} Code point on plane 4
|
382 | * @example
|
383 | * convert.swu2code('')
|
384 | *
|
385 | * return 0x40001
|
386 | */
|
387 |
|
388 |
|
389 | const swu2code$1 = swuSym => parseInt(swuSym.codePointAt(0));
|
390 | /**
|
391 | * Function to convert an SWU symbol character to a 16-bit ID
|
392 | * @function convert.swu2id
|
393 | * @param {string} swuSym - SWU symbol character
|
394 | * @returns {number} 16-bit ID
|
395 | * @example
|
396 | * convert.swu2id('')
|
397 | *
|
398 | * return 1
|
399 | */
|
400 |
|
401 |
|
402 | const swu2id = swuSym => swu2code$1(swuSym) - 0x40000;
|
403 | /**
|
404 | * Function to convert an FSW symbol key to a 16-bit ID
|
405 | * @function convert.key2id
|
406 | * @param {string} key - FSW symbol key
|
407 | * @returns {number} 16-bit ID
|
408 | * @example
|
409 | * convert.key2id('S10000')
|
410 | *
|
411 | * return 1
|
412 | */
|
413 |
|
414 |
|
415 | const key2id = key => 1 + (parseInt(key.slice(1, 4), 16) - 256) * 96 + parseInt(key.slice(4, 5), 16) * 16 + parseInt(key.slice(5, 6), 16);
|
416 | /* support ongoing development on https://patreon.com/signwriting */
|
417 |
|
418 | /**
|
419 | * Function that returns the size of a symbol using an FSW symbol key
|
420 | * @function fsw.symbolSize
|
421 | * @param {string} fsw - an FSW symbol key
|
422 | * @example
|
423 | * fsw.symbolSize("S10000")
|
424 | *
|
425 | * return [15,30]
|
426 | */
|
427 |
|
428 | const symbolSize$1 = function (fsw) {
|
429 | return symbolSize$2(key2id(fsw));
|
430 | };
|
431 |
|
432 | /**
|
433 | * Function that returns a plane 15 character for a symbol line using an FSW symbol key
|
434 | * @function fsw.symbolLine
|
435 | * @param {string} fsw - an FSW symbol key
|
436 | * @example
|
437 | * fsw.symbolLine('S10000')
|
438 | *
|
439 | * return ''
|
440 | */
|
441 |
|
442 | const symbolLine$1 = function (fsw) {
|
443 | return symbolLine$2(key2id(fsw));
|
444 | };
|
445 | /**
|
446 | * Function that returns a plane 16 character for a symbol fill using an FSW symbol key
|
447 | * @function fsw.symbolFill
|
448 | * @param {string} fsw - an FSW symbol key
|
449 | * @example
|
450 | * font.symbolFill('S10000')
|
451 | *
|
452 | * return ''
|
453 | */
|
454 |
|
455 |
|
456 | const symbolFill$1 = function (fsw) {
|
457 | return symbolFill$2(key2id(fsw));
|
458 | };
|
459 | /**
|
460 | * Function that creates two text elements for a symbol using an FSW symbol key
|
461 | * @function fsw.symbolText
|
462 | * @param {string} fsw - an FSW symbol key
|
463 | * @example
|
464 | * fsw.symbolText('S10000')
|
465 | *
|
466 | * return ` <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
467 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>`
|
468 | */
|
469 |
|
470 |
|
471 | const symbolText$1 = function (fsw) {
|
472 | return symbolText$2(key2id(fsw));
|
473 | };
|
474 |
|
475 | /**
|
476 | * Sutton SignWriting Core Module v1.4.2 (https://github.com/sutton-signwriting/core)
|
477 | * Author: Steve Slevinski (https://SteveSlevinski.me)
|
478 | * style.mjs is released under the MIT License.
|
479 | */
|
480 |
|
481 | /**
|
482 | * Object of regular expressions for style strings
|
483 | *
|
484 | * @alias style.re
|
485 | * @type {object}
|
486 | * @property {string} colorize - regular expression for colorize section
|
487 | * @property {string} colorhex - regular expression for color hex values with 3 or 6 characters
|
488 | * @property {string} colorname - regular expression for css color name
|
489 | * @property {string} padding - regular expression for padding section
|
490 | * @property {string} zoom - regular expression for zoom section
|
491 | * @property {string} classbase - regular expression for class name definition
|
492 | * @property {string} id - regular expression for id definition
|
493 | * @property {string} colorbase - regular expression for color hex or color name
|
494 | * @property {string} color - regular expression for single color entry
|
495 | * @property {string} colors - regular expression for double color entry
|
496 | * @property {string} background - regular expression for background section
|
497 | * @property {string} detail - regular expression for color details for line and optional fill
|
498 | * @property {string} detailsym - regular expression for color details for individual symbols
|
499 | * @property {string} classes - regular expression for one or more class names
|
500 | * @property {string} full - full regular expression for style string
|
501 | */
|
502 | let re$3 = {
|
503 | 'colorize': 'C',
|
504 | 'colorhex': '(?:[0-9a-fA-F]{3}){1,2}',
|
505 | 'colorname': '[a-zA-Z]+',
|
506 | 'padding': 'P[0-9]{2}',
|
507 | 'zoom': 'Z(?:[0-9]+(?:\\.[0-9]+)?|x)',
|
508 | 'classbase': '-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}',
|
509 | 'id': '[a-zA-Z][_a-zA-Z0-9-]{0,100}'
|
510 | };
|
511 | re$3.colorbase = `(?:${re$3.colorhex}|${re$3.colorname})`;
|
512 | re$3.color = `_${re$3.colorbase}_`;
|
513 | re$3.colors = `_${re$3.colorbase}(?:,${re$3.colorbase})?_`;
|
514 | re$3.background = `G${re$3.color}`;
|
515 | re$3.detail = `D${re$3.colors}`;
|
516 | re$3.detailsym = `D[0-9]{2}${re$3.colors}`;
|
517 | re$3.classes = `${re$3.classbase}(?: ${re$3.classbase})*`;
|
518 | re$3.full = `-(${re$3.colorize})?(${re$3.padding})?(${re$3.background})?(${re$3.detail})?(${re$3.zoom})?(?:-((?:${re$3.detailsym})*))?(?:-(${re$3.classes})?!(?:(${re$3.id})!)?)?`;
|
519 |
|
520 | const prefixColor$2 = color => {
|
521 | const regex = new RegExp(`^${re$3.colorhex}$`);
|
522 | return (regex.test(color) ? '#' : '') + color;
|
523 | };
|
524 |
|
525 | const definedProps$2 = obj => Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== undefined));
|
526 | /**
|
527 | * Function to parse style string to object
|
528 | * @function style.parse
|
529 | * @param {string} styleString - a style string
|
530 | * @returns {StyleObject} elements of style string
|
531 | * @example
|
532 | * style.parse('-CP10G_blue_D_red,Cyan_')
|
533 | *
|
534 | * return {
|
535 | * 'colorize': true,
|
536 | * 'padding': 10,
|
537 | * 'background': 'blue',
|
538 | * 'detail': ['red', 'Cyan']
|
539 | * }
|
540 | */
|
541 |
|
542 |
|
543 | const parse$3 = styleString => {
|
544 | const regex = `^${re$3.full}`;
|
545 | const m = (typeof styleString === 'string' ? styleString.match(new RegExp(regex)) : []) || [];
|
546 | return definedProps$2({
|
547 | 'colorize': !m[1] ? undefined : !!m[1],
|
548 | 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
|
549 | 'background': !m[3] ? undefined : prefixColor$2(m[3].slice(2, -1)),
|
550 | 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor$2),
|
551 | 'zoom': !m[5] ? undefined : m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1)),
|
552 | 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re$3.detailsym, 'g')).map(val => {
|
553 | const parts = val.split('_');
|
554 | const detail = parts[1].split(',').map(prefixColor$2);
|
555 | return {
|
556 | 'index': parseInt(parts[0].slice(1)),
|
557 | 'detail': detail
|
558 | };
|
559 | }),
|
560 | 'classes': !m[7] ? undefined : m[7],
|
561 | 'id': !m[8] ? undefined : m[8]
|
562 | });
|
563 | };
|
564 | /**
|
565 | * Function to compose style string from object
|
566 | * @function style.compose
|
567 | * @param {StyleObject} styleObject - an object of style options
|
568 | * @returns {string} style string
|
569 | * @example
|
570 | * style.compose({
|
571 | * 'colorize': true,
|
572 | * 'padding': 10,
|
573 | * 'background': 'blue',
|
574 | * 'detail': ['red', 'Cyan'],
|
575 | * 'zoom': 1.1,
|
576 | * 'detailsym': [
|
577 | * {
|
578 | * 'index': 1,
|
579 | * 'detail': ['#ff00ff']
|
580 | * },
|
581 | * {
|
582 | * 'index': 2,
|
583 | * 'detail': ['yellow', 'green']
|
584 | * }
|
585 | * ],
|
586 | * 'classes': 'primary blinking',
|
587 | * 'id': 'cursor'
|
588 | * })
|
589 | *
|
590 | * return '-CP10G_blue_D_red,Cyan_Z1.1-D01_ff00ff_D02_yellow,green_-primary blinking!cursor!'
|
591 | */
|
592 |
|
593 |
|
594 | const compose = styleObject => {
|
595 | if (typeof styleObject !== 'object' || styleObject === null) return undefined; // three sections
|
596 |
|
597 | let style1 = '-';
|
598 | style1 += !styleObject.colorize ? '' : 'C';
|
599 | const padding = parseInt(styleObject.padding);
|
600 | style1 += !padding || padding <= 0 || padding > 99 ? '' : 'P' + (padding > 9 ? padding : '0' + padding);
|
601 | const background = !styleObject.background || !(typeof styleObject.background === 'string') ? undefined : styleObject.background.match(re$3.colorbase)[0];
|
602 | style1 += !background ? '' : 'G_' + background + '_';
|
603 | const detail1 = !styleObject.detail || !styleObject.detail[0] || !(typeof styleObject.detail[0] === 'string') ? undefined : styleObject.detail[0].match(re$3.colorbase)[0];
|
604 | const detail2 = !styleObject.detail || !styleObject.detail[1] || !(typeof styleObject.detail[1] === 'string') ? undefined : styleObject.detail[1].match(re$3.colorbase)[0];
|
605 |
|
606 | if (detail1) {
|
607 | style1 += 'D_' + detail1;
|
608 |
|
609 | if (detail2) {
|
610 | style1 += ',' + detail2;
|
611 | }
|
612 |
|
613 | style1 += '_';
|
614 | }
|
615 |
|
616 | const zoom = styleObject.zoom === 'x' ? 'x' : parseFloat(styleObject.zoom);
|
617 | style1 += !zoom || zoom <= 0 ? '' : 'Z' + zoom;
|
618 | let style2 = '';
|
619 | const detailsym = !styleObject.detailsym || !Array.isArray(styleObject.detailsym) ? [] : styleObject.detailsym.map(styleObject => {
|
620 | const index = parseInt(styleObject.index);
|
621 | if (!index || index <= 0 || index > 99) return '';
|
622 | let style = 'D' + (index > 9 ? index : '0' + index);
|
623 | const detail1 = !styleObject.detail || !styleObject.detail[0] ? undefined : styleObject.detail[0].match(re$3.colorbase)[0];
|
624 | const detail2 = !styleObject.detail || !styleObject.detail[1] ? undefined : styleObject.detail[1].match(re$3.colorbase)[0];
|
625 |
|
626 | if (detail1) {
|
627 | style += '_' + detail1;
|
628 |
|
629 | if (detail2) {
|
630 | style += ',' + detail2;
|
631 | }
|
632 |
|
633 | style += '_';
|
634 | }
|
635 |
|
636 | return style;
|
637 | });
|
638 | style2 += detailsym.join('');
|
639 | let style3 = '';
|
640 | const classes = !styleObject.classes || !(typeof styleObject.classes === 'string') ? undefined : styleObject.classes.match(re$3.classes)[0];
|
641 | style3 += !classes ? '' : classes;
|
642 | const id = !styleObject.id || !(typeof styleObject.id === 'string') ? undefined : styleObject.id.match(re$3.id)[0];
|
643 | style3 += classes || id ? '!' : '';
|
644 | style3 += !id ? '' : id + '!';
|
645 | return style1 + (style2 || style3 ? '-' + style2 : '') + (style3 ? '-' + style3 : '');
|
646 | };
|
647 | /* support ongoing development on https://patreon.com/signwriting */
|
648 |
|
649 | /**
|
650 | * Sutton SignWriting Core Module v1.4.2 (https://github.com/sutton-signwriting/core)
|
651 | * Author: Steve Slevinski (https://SteveSlevinski.me)
|
652 | * fsw.mjs is released under the MIT License.
|
653 | */
|
654 |
|
655 | /**
|
656 | * Object of regular expressions for FSW strings
|
657 | *
|
658 | * @alias fsw.re
|
659 | * @property {string} symbol - regular expressions for a symbol
|
660 | * @property {string} coord - regular expressions for a coordinate
|
661 | * @property {string} sort - regular expressions for the sorting marker
|
662 | * @property {string} box - regular expression for a signbox marker
|
663 | * @property {string} prefix - regular expression for a sorting marker followed by one or more symbols
|
664 | * @property {string} spatial - regular expression for a symbol followed by a coordinate
|
665 | * @property {string} signbox - regular expression for a signbox marker, max coordinate and zero or more spatial symbols
|
666 | * @property {string} sign - regular expression for an optional prefix followed by a signbox
|
667 | * @property {string} sortable - regular expression for a mandatory prefix followed by a signbox
|
668 | */
|
669 | let re$1$1 = {
|
670 | 'symbol': 'S[123][0-9a-f]{2}[0-5][0-9a-f]',
|
671 | 'coord': '[0-9]{3}x[0-9]{3}',
|
672 | 'sort': 'A',
|
673 | 'box': '[BLMR]'
|
674 | };
|
675 | re$1$1.prefix = `(?:${re$1$1.sort}(?:${re$1$1.symbol})+)`;
|
676 | re$1$1.spatial = `${re$1$1.symbol}${re$1$1.coord}`;
|
677 | re$1$1.signbox = `${re$1$1.box}${re$1$1.coord}(?:${re$1$1.spatial})*`;
|
678 | re$1$1.sign = `${re$1$1.prefix}?${re$1$1.signbox}`;
|
679 | re$1$1.sortable = `${re$1$1.prefix}${re$1$1.signbox}`;
|
680 | /**
|
681 | * Object of regular expressions for style strings
|
682 | *
|
683 | * @alias style.re
|
684 | * @type {object}
|
685 | * @property {string} colorize - regular expression for colorize section
|
686 | * @property {string} colorhex - regular expression for color hex values with 3 or 6 characters
|
687 | * @property {string} colorname - regular expression for css color name
|
688 | * @property {string} padding - regular expression for padding section
|
689 | * @property {string} zoom - regular expression for zoom section
|
690 | * @property {string} classbase - regular expression for class name definition
|
691 | * @property {string} id - regular expression for id definition
|
692 | * @property {string} colorbase - regular expression for color hex or color name
|
693 | * @property {string} color - regular expression for single color entry
|
694 | * @property {string} colors - regular expression for double color entry
|
695 | * @property {string} background - regular expression for background section
|
696 | * @property {string} detail - regular expression for color details for line and optional fill
|
697 | * @property {string} detailsym - regular expression for color details for individual symbols
|
698 | * @property {string} classes - regular expression for one or more class names
|
699 | * @property {string} full - full regular expression for style string
|
700 | */
|
701 |
|
702 | let re$2 = {
|
703 | 'colorize': 'C',
|
704 | 'colorhex': '(?:[0-9a-fA-F]{3}){1,2}',
|
705 | 'colorname': '[a-zA-Z]+',
|
706 | 'padding': 'P[0-9]{2}',
|
707 | 'zoom': 'Z(?:[0-9]+(?:\\.[0-9]+)?|x)',
|
708 | 'classbase': '-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}',
|
709 | 'id': '[a-zA-Z][_a-zA-Z0-9-]{0,100}'
|
710 | };
|
711 | re$2.colorbase = `(?:${re$2.colorhex}|${re$2.colorname})`;
|
712 | re$2.color = `_${re$2.colorbase}_`;
|
713 | re$2.colors = `_${re$2.colorbase}(?:,${re$2.colorbase})?_`;
|
714 | re$2.background = `G${re$2.color}`;
|
715 | re$2.detail = `D${re$2.colors}`;
|
716 | re$2.detailsym = `D[0-9]{2}${re$2.colors}`;
|
717 | re$2.classes = `${re$2.classbase}(?: ${re$2.classbase})*`;
|
718 | re$2.full = `-(${re$2.colorize})?(${re$2.padding})?(${re$2.background})?(${re$2.detail})?(${re$2.zoom})?(?:-((?:${re$2.detailsym})*))?(?:-(${re$2.classes})?!(?:(${re$2.id})!)?)?`;
|
719 |
|
720 | const prefixColor$1 = color => {
|
721 | const regex = new RegExp(`^${re$2.colorhex}$`);
|
722 | return (regex.test(color) ? '#' : '') + color;
|
723 | };
|
724 |
|
725 | const definedProps$1 = obj => Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== undefined));
|
726 | /**
|
727 | * Function to parse style string to object
|
728 | * @function style.parse
|
729 | * @param {string} styleString - a style string
|
730 | * @returns {StyleObject} elements of style string
|
731 | * @example
|
732 | * style.parse('-CP10G_blue_D_red,Cyan_')
|
733 | *
|
734 | * return {
|
735 | * 'colorize': true,
|
736 | * 'padding': 10,
|
737 | * 'background': 'blue',
|
738 | * 'detail': ['red', 'Cyan']
|
739 | * }
|
740 | */
|
741 |
|
742 |
|
743 | const parse$1$1 = styleString => {
|
744 | const regex = `^${re$2.full}`;
|
745 | const m = (typeof styleString === 'string' ? styleString.match(new RegExp(regex)) : []) || [];
|
746 | return definedProps$1({
|
747 | 'colorize': !m[1] ? undefined : !!m[1],
|
748 | 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
|
749 | 'background': !m[3] ? undefined : prefixColor$1(m[3].slice(2, -1)),
|
750 | 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor$1),
|
751 | 'zoom': !m[5] ? undefined : m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1)),
|
752 | 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re$2.detailsym, 'g')).map(val => {
|
753 | const parts = val.split('_');
|
754 | const detail = parts[1].split(',').map(prefixColor$1);
|
755 | return {
|
756 | 'index': parseInt(parts[0].slice(1)),
|
757 | 'detail': detail
|
758 | };
|
759 | }),
|
760 | 'classes': !m[7] ? undefined : m[7],
|
761 | 'id': !m[8] ? undefined : m[8]
|
762 | });
|
763 | };
|
764 | /** The convert module contains functions to convert between Formal SignWriitng in ASCII (FSW) and SignWriting in Unicode (SWU) characters, along with other types of data.
|
765 | * [Characters set definitions](https://tools.ietf.org/id/draft-slevinski-formal-signwriting-09.html#name-characters)
|
766 | * @module convert
|
767 | */
|
768 |
|
769 | /**
|
770 | * Function to convert an FSW coordinate string to an array of x,y integers
|
771 | * @function convert.fsw2coord
|
772 | * @param {string} fswCoord - An FSW coordinate string
|
773 | * @returns {number[]} Array of x,y integers
|
774 | * @example
|
775 | * convert.fsw2coord('500x500')
|
776 | *
|
777 | * return [500, 500]
|
778 | */
|
779 |
|
780 |
|
781 | const fsw2coord = fswCoord => fswCoord.split('x').map(num => parseInt(num));
|
782 |
|
783 | const parse$2 = {
|
784 | /**
|
785 | * Function to parse an fsw symbol with optional coordinate and style string
|
786 | * @function fsw.parse.symbol
|
787 | * @param {string} fswSym - an fsw symbol
|
788 | * @returns {object} elements of fsw symbol
|
789 | * @example
|
790 | * fsw.parse.symbol('S10000500x500-C')
|
791 | *
|
792 | * return {
|
793 | * 'symbol': 'S10000',
|
794 | * 'coord': [500, 500],
|
795 | * 'style': '-C'
|
796 | * }
|
797 | */
|
798 | symbol: fswSym => {
|
799 | const regex = `^(${re$1$1.symbol})(${re$1$1.coord})?(${re$2.full})?`;
|
800 | const symbol = typeof fswSym === 'string' ? fswSym.match(new RegExp(regex)) : undefined;
|
801 | return {
|
802 | 'symbol': symbol ? symbol[1] : undefined,
|
803 | 'coord': symbol && symbol[2] ? fsw2coord(symbol[2]) : undefined,
|
804 | 'style': symbol ? symbol[3] : undefined
|
805 | };
|
806 | },
|
807 |
|
808 | /**
|
809 | * Function to parse an fsw sign with style string
|
810 | * @function fsw.parse.sign
|
811 | * @param {string} fswSign - an fsw sign
|
812 | * @returns {object} elements of fsw sign
|
813 | * @example
|
814 | * fsw.parse.sign('AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475-C')
|
815 | *
|
816 | * return {
|
817 | * sequence: ['S10011', 'S10019', 'S2e704', 'S2e748'],
|
818 | * box: 'M',
|
819 | * max: [525, 535],
|
820 | * spatials: [
|
821 | * {
|
822 | * symbol: 'S2e748',
|
823 | * coord: [483, 510]
|
824 | * },
|
825 | * {
|
826 | * symbol: 'S10011',
|
827 | * coord: [501, 466]
|
828 | * },
|
829 | * {
|
830 | * symbol: 'S2e704',
|
831 | * coord: [510, 500]
|
832 | * },
|
833 | * {
|
834 | * symbol: 'S10019',
|
835 | * coord: [476, 475]
|
836 | * }
|
837 | * ],
|
838 | * style: '-C'
|
839 | * }
|
840 | */
|
841 | sign: fswSign => {
|
842 | const regex = `^(${re$1$1.prefix})?(${re$1$1.signbox})(${re$2.full})?`;
|
843 | const sign = typeof fswSign === 'string' ? fswSign.match(new RegExp(regex)) : undefined;
|
844 |
|
845 | if (sign) {
|
846 | return {
|
847 | 'sequence': sign[1] ? sign[1].slice(1).match(/.{6}/g) : undefined,
|
848 | 'box': sign[2][0],
|
849 | 'max': fsw2coord(sign[2].slice(1, 8)),
|
850 | 'spatials': sign[2].length < 9 ? undefined : sign[2].slice(8).match(/(.{13})/g).map(m => {
|
851 | return {
|
852 | symbol: m.slice(0, 6),
|
853 | coord: [parseInt(m.slice(6, 9)), parseInt(m.slice(10, 13))]
|
854 | };
|
855 | }),
|
856 | 'style': sign[3]
|
857 | };
|
858 | } else {
|
859 | return {};
|
860 | }
|
861 | },
|
862 |
|
863 | /**
|
864 | * Function to parse an fsw text
|
865 | * @function fsw.parse.text
|
866 | * @param {string} fswText - an fsw text
|
867 | * @returns {array} fsw signs and punctuations
|
868 | * @example
|
869 | * fsw.parse.text('AS14c20S27106M518x529S14c20481x471S27106503x489 AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468 S38800464x496')
|
870 | *
|
871 | * return [
|
872 | * 'AS14c20S27106M518x529S14c20481x471S27106503x489',
|
873 | * 'AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468',
|
874 | * 'S38800464x496'
|
875 | * ]
|
876 | */
|
877 | text: fswText => {
|
878 | if (typeof fswText !== 'string') return [];
|
879 | const regex = `(${re$1$1.sign}(${re$2.full})?|${re$1$1.spatial}(${re$2.full})?)`;
|
880 | const matches = fswText.match(new RegExp(regex, 'g'));
|
881 | return matches ? [...matches] : [];
|
882 | }
|
883 | };
|
884 | /**
|
885 | * Function to gather sizing information about an fsw sign or symbol
|
886 | * @function fsw.info
|
887 | * @param {string} fsw - an fsw sign or symbol
|
888 | * @returns {object} information about the fsw string
|
889 | * @example
|
890 | * fsw.info('AS14c20S27106L518x529S14c20481x471S27106503x489-P10Z2')
|
891 | *
|
892 | * return {
|
893 | * minX: 481,
|
894 | * minY: 471,
|
895 | * width: 37,
|
896 | * height: 58,
|
897 | * zoom: 2,
|
898 | * padding: 10,
|
899 | * segment: 'sign',
|
900 | * lane: -1
|
901 | * }
|
902 | */
|
903 |
|
904 | const info$1 = fsw => {
|
905 | let lanes = {
|
906 | "B": 0,
|
907 | "L": -1,
|
908 | "M": 0,
|
909 | "R": 1
|
910 | };
|
911 | let parsed = parse$2.sign(fsw);
|
912 | let width, height, segment, x1, x2, y1, y2, lane;
|
913 |
|
914 | if (parsed.spatials) {
|
915 | x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
|
916 | x2 = parsed.max[0];
|
917 | width = x2 - x1;
|
918 | y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
|
919 | y2 = parsed.max[1];
|
920 | height = y2 - y1;
|
921 | segment = 'sign';
|
922 | lane = parsed.box;
|
923 | } else {
|
924 | parsed = parse$2.symbol(fsw);
|
925 | lane = "M";
|
926 |
|
927 | if (parsed.coord) {
|
928 | x1 = parsed.coord[0];
|
929 | width = (500 - x1) * 2;
|
930 | y1 = parsed.coord[1];
|
931 | height = (500 - y1) * 2;
|
932 | segment = 'symbol';
|
933 | } else {
|
934 | x1 = 490;
|
935 | width = 20;
|
936 | y1 = 490;
|
937 | height = 20;
|
938 | segment = 'none';
|
939 | }
|
940 | }
|
941 |
|
942 | let style = parse$1$1(parsed.style);
|
943 | let zoom = style.zoom || 1;
|
944 | let padding = style.padding || 0;
|
945 | return {
|
946 | minX: x1,
|
947 | minY: y1,
|
948 | width: width,
|
949 | height: height,
|
950 | segment: segment,
|
951 | lane: lanes[lane],
|
952 | padding: padding,
|
953 | zoom: zoom
|
954 | };
|
955 | };
|
956 |
|
957 | const columnDefaults$1 = {
|
958 | 'height': 500,
|
959 | 'width': 150,
|
960 | 'offset': 50,
|
961 | 'pad': 20,
|
962 | 'margin': 5,
|
963 | 'dynamic': false,
|
964 | 'background': undefined,
|
965 | 'punctuation': {
|
966 | 'spacing': true,
|
967 | 'pad': 30,
|
968 | 'pull': true
|
969 | },
|
970 | 'style': {
|
971 | 'detail': ['black', 'white'],
|
972 | 'zoom': 1
|
973 | }
|
974 | };
|
975 | /**
|
976 | * Function to an object of column options with default values
|
977 | *
|
978 | * @function fsw.columnDefaultsMerge
|
979 | * @param {ColumnOptions} options - object of column options
|
980 | * @returns {ColumnOptions} object of column options merged with column defaults
|
981 | * @example
|
982 | * fsw.columnDefaultsMerge({height: 500,width:150})
|
983 | *
|
984 | * return {
|
985 | * "height": 500,
|
986 | * "width": 150,
|
987 | * "offset": 50,
|
988 | * "pad": 20,
|
989 | * "margin": 5,
|
990 | * "dynamic": false,
|
991 | * "punctuation": {
|
992 | * "spacing": true,
|
993 | * "pad": 30,
|
994 | * "pull": true
|
995 | * },
|
996 | * "style": {
|
997 | * "detail": [
|
998 | * "black",
|
999 | * "white"
|
1000 | * ],
|
1001 | * "zoom": 1
|
1002 | * }
|
1003 | * }
|
1004 | */
|
1005 |
|
1006 | const columnDefaultsMerge$1 = options => {
|
1007 | if (typeof options !== 'object') options = {};
|
1008 | return { ...columnDefaults$1,
|
1009 | ...options,
|
1010 | punctuation: { ...columnDefaults$1.punctuation,
|
1011 | ...options.punctuation
|
1012 | },
|
1013 | style: { ...columnDefaults$1.style,
|
1014 | ...options.style
|
1015 | }
|
1016 | };
|
1017 | };
|
1018 | /**
|
1019 | * Function to transform an FSW text to an array of columns
|
1020 | *
|
1021 | * @function fsw.columns
|
1022 | * @param {string} fswText - FSW text of signs and punctuation
|
1023 | * @param {ColumnOptions} options - object of column options
|
1024 | * @returns {{options:ColumnOptions,widths:number[],columns:ColumnData}} object of column options, widths array, and column data
|
1025 | * @example
|
1026 | * fsw.columns('AS14c20S27106M518x529S14c20481x471S27106503x489 AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468 S38800464x496', {height: 500,width:150})
|
1027 | *
|
1028 | * return {
|
1029 | * "options": {
|
1030 | * "height": 500,
|
1031 | * "width": 150,
|
1032 | * "offset": 50,
|
1033 | * "pad": 20,
|
1034 | * "margin": 5,
|
1035 | * "dynamic": false,
|
1036 | * "punctuation": {
|
1037 | * "spacing": true,
|
1038 | * "pad": 30,
|
1039 | * "pull": true
|
1040 | * },
|
1041 | * "style": {
|
1042 | * "detail": [
|
1043 | * "black",
|
1044 | * "white"
|
1045 | * ],
|
1046 | * "zoom": 1
|
1047 | * }
|
1048 | * },
|
1049 | * "widths": [
|
1050 | * 150
|
1051 | * ],
|
1052 | * "columns": [
|
1053 | * [
|
1054 | * {
|
1055 | * "x": 56,
|
1056 | * "y": 20,
|
1057 | * "minX": 481,
|
1058 | * "minY": 471,
|
1059 | * "width": 37,
|
1060 | * "height": 58,
|
1061 | * "lane": 0,
|
1062 | * "padding": 0,
|
1063 | * "segment": "sign",
|
1064 | * "text": "AS14c20S27106M518x529S14c20481x471S27106503x489",
|
1065 | * "zoom": 1
|
1066 | * },
|
1067 | * {
|
1068 | * "x": 57,
|
1069 | * "y": 118,
|
1070 | * "minX": 482,
|
1071 | * "minY": 468,
|
1072 | * "width": 36,
|
1073 | * "height": 65,
|
1074 | * "lane": 0,
|
1075 | * "padding": 0,
|
1076 | * "segment": "sign",
|
1077 | * "text": "AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468",
|
1078 | * "zoom": 1
|
1079 | * },
|
1080 | * {
|
1081 | * "x": 39,
|
1082 | * "y": 203,
|
1083 | * "minX": 464,
|
1084 | * "minY": 496,
|
1085 | * "width": 72,
|
1086 | * "height": 8,
|
1087 | * "lane": 0,
|
1088 | * "padding": 0,
|
1089 | * "segment": "symbol",
|
1090 | * "text": "S38800464x496",
|
1091 | * "zoom": 1
|
1092 | * }
|
1093 | * ]
|
1094 | * ]
|
1095 | * }
|
1096 | */
|
1097 |
|
1098 |
|
1099 | const columns$1 = (fswText, options) => {
|
1100 | if (typeof fswText !== 'string') return {};
|
1101 | const values = columnDefaultsMerge$1(options);
|
1102 | let input = parse$2.text(fswText);
|
1103 | let cursor = 0;
|
1104 | let cols = [];
|
1105 | let col = [];
|
1106 | let plus = 0;
|
1107 | let center = parseInt(values.width / 2);
|
1108 | let maxHeight = values.height - values.margin;
|
1109 | let pullable = true;
|
1110 | let finalize = false;
|
1111 |
|
1112 | for (let val of input) {
|
1113 | let informed = info$1(val);
|
1114 | cursor += plus;
|
1115 |
|
1116 | if (values.punctuation.spacing) {
|
1117 | cursor += informed.segment == 'sign' ? values.pad : 0;
|
1118 | } else {
|
1119 | cursor += values.pad;
|
1120 | }
|
1121 |
|
1122 | finalize = cursor + informed.height > maxHeight;
|
1123 |
|
1124 | if (finalize && informed.segment == 'symbol' && values.punctuation.pull && pullable) {
|
1125 | finalize = false;
|
1126 | pullable = false;
|
1127 | }
|
1128 |
|
1129 | if (col.length == 0) {
|
1130 | finalize = false;
|
1131 | }
|
1132 |
|
1133 | if (finalize) {
|
1134 | cursor = values.pad;
|
1135 | cols.push(col);
|
1136 | col = [];
|
1137 | pullable = true;
|
1138 | }
|
1139 |
|
1140 | col.push(Object.assign(informed, {
|
1141 | x: center + values.offset * informed.lane - (500 - informed.minX) * informed.zoom * values.style.zoom,
|
1142 | y: cursor,
|
1143 | text: val
|
1144 | }));
|
1145 | cursor += informed.height * informed.zoom * values.style.zoom;
|
1146 |
|
1147 | if (values.punctuation.spacing) {
|
1148 | plus = informed.segment == 'sign' ? values.pad : values.punctuation.pad;
|
1149 | } else {
|
1150 | plus = values.pad;
|
1151 | }
|
1152 | }
|
1153 |
|
1154 | if (col.length) {
|
1155 | cols.push(col);
|
1156 | } // over height issue when pulling punctuation
|
1157 |
|
1158 |
|
1159 | if (values.punctuation.pull) {
|
1160 | for (let col of cols) {
|
1161 | let last = col[col.length - 1];
|
1162 | let diff = last.y + last.height - (values.height - values.margin);
|
1163 |
|
1164 | if (diff > 0) {
|
1165 | let adj = parseInt(diff / col.length) + 1;
|
1166 |
|
1167 | for (let i in col) {
|
1168 | col[i].y -= adj * i + adj;
|
1169 | }
|
1170 | }
|
1171 | }
|
1172 | } // contract, expand, adjust
|
1173 |
|
1174 |
|
1175 | let widths = [];
|
1176 |
|
1177 | for (let col of cols) {
|
1178 | let min = [center - values.offset - values.pad];
|
1179 | let max = [center + values.offset + values.pad];
|
1180 |
|
1181 | for (let item of col) {
|
1182 | min.push(item.x - values.pad);
|
1183 | max.push(item.x + item.width + values.pad);
|
1184 | }
|
1185 |
|
1186 | min = Math.min(...min);
|
1187 | max = Math.max(...max);
|
1188 | let width = values.width;
|
1189 | let adj = 0;
|
1190 |
|
1191 | if (!values.dynamic) {
|
1192 | adj = center - parseInt((min + max) / 2);
|
1193 | } else {
|
1194 | width = max - min;
|
1195 | adj = -min;
|
1196 | }
|
1197 |
|
1198 | for (let item of col) {
|
1199 | item.x += adj;
|
1200 | }
|
1201 |
|
1202 | widths.push(width);
|
1203 | }
|
1204 |
|
1205 | return {
|
1206 | 'options': values,
|
1207 | 'widths': widths,
|
1208 | 'columns': cols
|
1209 | };
|
1210 | };
|
1211 | /**
|
1212 | * Array of numbers for categories of symbols: hand, movement, dynamics, head, trunk & limb, location, and punctuation.
|
1213 | * @alias fsw.category
|
1214 | * @type {array}
|
1215 | */
|
1216 |
|
1217 | const category$1 = [0x100, 0x205, 0x2f7, 0x2ff, 0x36d, 0x37f, 0x387];
|
1218 | /**
|
1219 | * Object of symbol ranges with starting and ending numbers.
|
1220 | *
|
1221 | * { all, writing, hand, movement, dynamic, head, hcenter, vcenter, trunk, limb, location, punctuation }
|
1222 | * @alias fsw.ranges
|
1223 | * @type {object}
|
1224 | */
|
1225 |
|
1226 | const ranges$1 = {
|
1227 | 'all': [0x100, 0x38b],
|
1228 | 'writing': [0x100, 0x37e],
|
1229 | 'hand': [0x100, 0x204],
|
1230 | 'movement': [0x205, 0x2f6],
|
1231 | 'dynamic': [0x2f7, 0x2fe],
|
1232 | 'head': [0x2ff, 0x36c],
|
1233 | 'hcenter': [0x2ff, 0x36c],
|
1234 | 'vcenter': [0x2ff, 0x375],
|
1235 | 'trunk': [0x36d, 0x375],
|
1236 | 'limb': [0x376, 0x37e],
|
1237 | 'location': [0x37f, 0x386],
|
1238 | 'punctuation': [0x387, 0x38b]
|
1239 | };
|
1240 | /**
|
1241 | * Array of colors associated with the seven symbol categories.
|
1242 | * @alias fsw.colors
|
1243 | * @type {array}
|
1244 | */
|
1245 |
|
1246 |
|
1247 | const colors$1 = ['#0000CC', '#CC0000', '#FF0099', '#006600', '#000000', '#884411', '#FF9900'];
|
1248 | /**
|
1249 | * Function that returns the standardized color for a symbol.
|
1250 | * @function fsw.colorize
|
1251 | * @param {string} key - an FSW symbol key
|
1252 | * @returns {string} name of standardized color for symbol
|
1253 | * @example
|
1254 | * fsw.colorize('S10000')
|
1255 | *
|
1256 | * return '#0000CC'
|
1257 | */
|
1258 |
|
1259 | const colorize$1 = key => {
|
1260 | const parsed = parse$2.symbol(key);
|
1261 | let color = '#000000';
|
1262 |
|
1263 | if (parsed.symbol) {
|
1264 | const dec = parseInt(parsed.symbol.slice(1, 4), 16);
|
1265 | const index = category$1.findIndex(val => val > dec);
|
1266 | color = colors$1[index < 0 ? 6 : index - 1];
|
1267 | }
|
1268 |
|
1269 | return color;
|
1270 | };
|
1271 | /* support ongoing development on https://patreon.com/signwriting */
|
1272 |
|
1273 | /**
|
1274 | * Function that creates an SVG image from an FSW symbol key with an optional style string
|
1275 | * @function fsw.symbolSvgBody
|
1276 | * @param {string} fswSym - an FSW symbol key with optional style string
|
1277 | * @example
|
1278 | * fsw.symbolSvgBody('S10000')
|
1279 | *
|
1280 | * return `<text font-size="0">S10000</text>
|
1281 | * <g transform="translate(500,500)">
|
1282 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1283 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1284 | * </g>`
|
1285 | */
|
1286 |
|
1287 | const symbolSvgBody$1 = fswSym => {
|
1288 | const parsed = parse$2.symbol(fswSym);
|
1289 | const blank = '';
|
1290 | if (!parsed.symbol) return blank;
|
1291 | let styling = parse$3(parsed.style);
|
1292 | let x1, y1, x2, y2;
|
1293 |
|
1294 | if (parsed.coord) {
|
1295 | x1 = parsed.coord[0];
|
1296 | y1 = parsed.coord[1];
|
1297 | x2 = 500 + (500 - x1);
|
1298 | y2 = 500 + (500 - y1);
|
1299 | } else {
|
1300 | let size = symbolSize$1(parsed.symbol);
|
1301 | if (!size) return blank;
|
1302 | x1 = 500 - parseInt((size[0] + 1) / 2);
|
1303 | y1 = 500 - parseInt((size[1] + 1) / 2);
|
1304 | x2 = 500 + (500 - x1);
|
1305 | y2 = 500 + (500 - y1);
|
1306 | }
|
1307 |
|
1308 | let symSvg = symbolText$1(parsed.symbol);
|
1309 | symSvg = ` <g transform="translate(${x1},${y1})">
|
1310 | ${symSvg}
|
1311 | </g>`;
|
1312 | let line;
|
1313 |
|
1314 | if (styling.colorize) {
|
1315 | line = colorize$1(parsed.symbol);
|
1316 | } else if (styling.detail) {
|
1317 | line = styling.detail[0];
|
1318 | }
|
1319 |
|
1320 | if (line) {
|
1321 | symSvg = symSvg.replace(/class="sym-line" fill="black"/, `class="sym-line" fill="${line}"`);
|
1322 | }
|
1323 |
|
1324 | let fill = styling.detail && styling.detail[1];
|
1325 |
|
1326 | if (fill) {
|
1327 | symSvg = symSvg.replace(/class="sym-fill" fill="white"/, `class="sym-fill" fill="${fill}"`);
|
1328 | }
|
1329 |
|
1330 | let background = '';
|
1331 |
|
1332 | if (styling.padding) {
|
1333 | x1 -= styling.padding;
|
1334 | y1 -= styling.padding;
|
1335 | x2 += styling.padding;
|
1336 | y2 += styling.padding;
|
1337 | }
|
1338 |
|
1339 | if (styling.background) {
|
1340 | background = `\n <rect x="${x1}" y="${y1}" width="${x2 - x1}" height="${y2 - y1}" style="fill:${styling.background};" />`;
|
1341 | }
|
1342 |
|
1343 | return ` <text font-size="0">${fswSym}</text>${background}
|
1344 | ${symSvg}`;
|
1345 | };
|
1346 | /**
|
1347 | * Function that creates an SVG image from an FSW symbol key with an optional style string
|
1348 | * @function fsw.symbolSvg
|
1349 | * @param {string} fswSym - an FSW symbol key with optional style string
|
1350 | * @example
|
1351 | * fsw.symbolSvg('S10000')
|
1352 | *
|
1353 | * return `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="15" height="30" viewBox="500 500 15 30">
|
1354 | * <text font-size="0">S10000</text>
|
1355 | * <g transform="translate(500,500)">
|
1356 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1357 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1358 | * </g>
|
1359 | * </svg>`
|
1360 | */
|
1361 |
|
1362 |
|
1363 | const symbolSvg$1 = fswSym => {
|
1364 | const parsed = parse$2.symbol(fswSym);
|
1365 | const blank = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>';
|
1366 | if (!parsed.symbol) return blank;
|
1367 | let styling = parse$3(parsed.style);
|
1368 | let x1, y1, x2, y2;
|
1369 |
|
1370 | if (parsed.coord) {
|
1371 | x1 = parsed.coord[0];
|
1372 | y1 = parsed.coord[1];
|
1373 | x2 = 500 + (500 - x1);
|
1374 | y2 = 500 + (500 - y1);
|
1375 | } else {
|
1376 | let size = symbolSize$1(parsed.symbol);
|
1377 | if (!size) return blank;
|
1378 | x1 = parseInt(500 - size[0] / 2);
|
1379 | y1 = parseInt(500 - size[1] / 2);
|
1380 | x2 = x1 + size[0];
|
1381 | y2 = y1 + size[1];
|
1382 | }
|
1383 |
|
1384 | let classes = '';
|
1385 |
|
1386 | if (styling.classes) {
|
1387 | classes = ` class="${styling.classes}"`;
|
1388 | }
|
1389 |
|
1390 | let id = '';
|
1391 |
|
1392 | if (styling.id) {
|
1393 | id = ` id="${styling.id}"`;
|
1394 | }
|
1395 |
|
1396 | if (styling.padding) {
|
1397 | x1 -= styling.padding;
|
1398 | y1 -= styling.padding;
|
1399 | x2 += styling.padding;
|
1400 | y2 += styling.padding;
|
1401 | }
|
1402 |
|
1403 | let sizing = '';
|
1404 |
|
1405 | if (styling.zoom != 'x') {
|
1406 | sizing = ` width="${(x2 - x1) * (styling.zoom ? styling.zoom : 1)}" height="${(y2 - y1) * (styling.zoom ? styling.zoom : 1)}"`;
|
1407 | }
|
1408 |
|
1409 | return `<svg${classes}${id} version="1.1" xmlns="http://www.w3.org/2000/svg"${sizing} viewBox="${x1} ${y1} ${x2 - x1} ${y2 - y1}">
|
1410 | ${symbolSvgBody$1(fswSym)}
|
1411 | </svg>`;
|
1412 | };
|
1413 |
|
1414 | const symbolCanvas$1 = function (fswSym) {
|
1415 | const parsed = parse$2.symbol(fswSym);
|
1416 |
|
1417 | if (parsed.symbol) {
|
1418 | let size = symbolSize$1(parsed.symbol);
|
1419 |
|
1420 | if (size) {
|
1421 | const canvas = document.createElement('canvas');
|
1422 | const context = canvas.getContext('2d');
|
1423 | let styling = parse$3(parsed.style);
|
1424 | let line = 'black';
|
1425 |
|
1426 | if (styling.colorize) {
|
1427 | line = colorize$1(parsed.symbol);
|
1428 | } else if (styling.detail) {
|
1429 | line = styling.detail[0];
|
1430 | }
|
1431 |
|
1432 | let fill = styling.detail && styling.detail[1] || 'white';
|
1433 | let x1 = 500;
|
1434 | let x2 = x1 + size[0];
|
1435 | let y1 = 500;
|
1436 | let y2 = y1 + size[1];
|
1437 |
|
1438 | if (styling.padding) {
|
1439 | x1 -= styling.padding;
|
1440 | y1 -= styling.padding;
|
1441 | x2 += styling.padding;
|
1442 | y2 += styling.padding;
|
1443 | }
|
1444 |
|
1445 | let sizing = 1;
|
1446 |
|
1447 | if (styling.zoom != 'x') {
|
1448 | sizing = styling.zoom;
|
1449 | }
|
1450 |
|
1451 | let w = (x2 - x1) * sizing;
|
1452 | let h = (y2 - y1) * sizing;
|
1453 | canvas.width = w ? w : 1;
|
1454 | canvas.height = h ? h : 1;
|
1455 |
|
1456 | if (styling.background) {
|
1457 | context.rect(0, 0, w, h);
|
1458 | context.fillStyle = styling.background;
|
1459 | context.fill();
|
1460 | }
|
1461 |
|
1462 | context.font = 30 * sizing + "px 'SuttonSignWritingFill'";
|
1463 | context.fillStyle = fill;
|
1464 | context.fillText(symbolFill$1(parsed.symbol), (500 - x1) * sizing, (500 - y1) * sizing);
|
1465 | context.font = 30 * sizing + "px 'SuttonSignWritingLine'";
|
1466 | context.fillStyle = line;
|
1467 | context.fillText(symbolLine$1(parsed.symbol), (500 - x1) * sizing, (500 - y1) * sizing);
|
1468 | return canvas;
|
1469 | }
|
1470 | }
|
1471 | };
|
1472 | /**
|
1473 | * Function that creates a PNG data url from an FSW symbol key with an optional style string
|
1474 | * @function fsw.symbolPng
|
1475 | * @param {string} fswSym - an FSW symbol key with optional style string
|
1476 | * @example
|
1477 | * fsw.symbolPng('S10000')
|
1478 | *
|
1479 | * return 'data:image/png;base64,iVBORw...'
|
1480 | */
|
1481 |
|
1482 |
|
1483 | const symbolPng$1 = fswSym => {
|
1484 | const canvas = symbolCanvas$1(fswSym);
|
1485 | const png = canvas.toDataURL("image/png");
|
1486 | canvas.remove();
|
1487 | return png;
|
1488 | };
|
1489 |
|
1490 | const blank$1 = null;
|
1491 | /**
|
1492 | * Function that normalizes a symbol with a minimum coordinate for a center of 500,500
|
1493 | * @function fsw.symbolNormalize
|
1494 | * @param {string} fswSym - an FSW symbol key with optional coordinate and style string
|
1495 | * @example
|
1496 | * fsw.symbolNormalize('S10000-CP10G_green_Z2')
|
1497 | *
|
1498 | * return 'S10000493x485-CP10G_green_Z2'
|
1499 | */
|
1500 |
|
1501 | const symbolNormalize$1 = fswSym => {
|
1502 | const parsed = parse$2.symbol(fswSym);
|
1503 |
|
1504 | if (parsed.symbol) {
|
1505 | let size = symbolSize$1(parsed.symbol);
|
1506 |
|
1507 | if (size) {
|
1508 | return `${parsed.symbol}${500 - parseInt((size[0] + 1) / 2)}x${500 - parseInt((size[1] + 1) / 2)}${parsed.style || ''}`;
|
1509 | }
|
1510 | } else {
|
1511 | return blank$1;
|
1512 | }
|
1513 | };
|
1514 |
|
1515 | /**
|
1516 | * Function that creates an SVG image from an FSW sign with an optional style string
|
1517 | * @function fsw.signSvgBody
|
1518 | * @param {string} fswSign - an FSW sign with optional style string
|
1519 | * @example
|
1520 | * fsw.signSvgBody('M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475')
|
1521 | *
|
1522 | * return `<text font-size="0">M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475</text>
|
1523 | * <g transform="translate(483,510)">
|
1524 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1525 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1526 | * </g>
|
1527 | * <g transform="translate(501,466)">
|
1528 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1529 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1530 | * </g>
|
1531 | * <g transform="translate(510,500)">
|
1532 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1533 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1534 | * </g>
|
1535 | * <g transform="translate(476,475)">
|
1536 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1537 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1538 | * </g>`
|
1539 | */
|
1540 |
|
1541 | const signSvgBody$1 = fswSign => {
|
1542 | let parsed = parse$2.sign(fswSign);
|
1543 | const blank = '';
|
1544 |
|
1545 | if (parsed.spatials) {
|
1546 | let styling = parse$3(parsed.style);
|
1547 |
|
1548 | if (styling.detailsym) {
|
1549 | styling.detailsym.forEach(sym => {
|
1550 | if (parsed.spatials[sym.index - 1]) {
|
1551 | parsed.spatials[sym.index - 1].detail = sym.detail;
|
1552 | }
|
1553 | });
|
1554 | }
|
1555 |
|
1556 | let x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
|
1557 | let y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
|
1558 | let x2 = parsed.max[0];
|
1559 | let y2 = parsed.max[1];
|
1560 | let background = '';
|
1561 |
|
1562 | if (styling.padding) {
|
1563 | x1 -= styling.padding;
|
1564 | y1 -= styling.padding;
|
1565 | x2 += styling.padding;
|
1566 | y2 += styling.padding;
|
1567 | }
|
1568 |
|
1569 | if (styling.background) {
|
1570 | background = `\n <rect x="${x1}" y="${y1}" width="${x2 - x1}" height="${y2 - y1}" style="fill:${styling.background};" />`;
|
1571 | }
|
1572 |
|
1573 | let svg = ` <text font-size="0">${fswSign}</text>${background}`;
|
1574 | const line = styling.detail && styling.detail[0];
|
1575 | const fill = styling.detail && styling.detail[1];
|
1576 | svg += '\n' + parsed.spatials.map(spatial => {
|
1577 | let svg = symbolText$1(spatial.symbol);
|
1578 | let symLine = line;
|
1579 |
|
1580 | if (spatial.detail) {
|
1581 | symLine = spatial.detail[0];
|
1582 | } else if (styling.colorize) {
|
1583 | symLine = colorize$1(spatial.symbol);
|
1584 | }
|
1585 |
|
1586 | if (symLine) {
|
1587 | svg = svg.replace(/class="sym-line" fill="black"/, `class="sym-line" fill="${symLine}"`);
|
1588 | }
|
1589 |
|
1590 | let symFill = fill;
|
1591 |
|
1592 | if (spatial.detail && spatial.detail[1]) {
|
1593 | symFill = spatial.detail[1];
|
1594 | }
|
1595 |
|
1596 | if (symFill) {
|
1597 | svg = svg.replace(/class="sym-fill" fill="white"/, `class="sym-fill" fill="${symFill}"`);
|
1598 | }
|
1599 |
|
1600 | return ` <g transform="translate(${spatial.coord[0]},${spatial.coord[1]})">
|
1601 | ${svg}
|
1602 | </g>`;
|
1603 | }).join('\n');
|
1604 | return svg;
|
1605 | }
|
1606 |
|
1607 | return blank;
|
1608 | };
|
1609 | /**
|
1610 | * Function that creates an SVG image from an FSW sign with an optional style string
|
1611 | * @function fsw.signSvg
|
1612 | * @param {string} fswSign - an FSW sign with optional style string
|
1613 | * @example
|
1614 | * fsw.signSvg('M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475')
|
1615 | *
|
1616 | * return `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="49" height="69" viewBox="476 466 49 69">
|
1617 | * <text font-size="0">M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475</text>
|
1618 | * <g transform="translate(483,510)">
|
1619 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1620 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1621 | * </g>
|
1622 | * <g transform="translate(501,466)">
|
1623 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1624 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1625 | * </g>
|
1626 | * <g transform="translate(510,500)">
|
1627 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1628 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1629 | * </g>
|
1630 | * <g transform="translate(476,475)">
|
1631 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1632 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1633 | * </g>
|
1634 | * </svg>`
|
1635 | */
|
1636 |
|
1637 |
|
1638 | const signSvg$1 = fswSign => {
|
1639 | let parsed = parse$2.sign(fswSign);
|
1640 | const blank = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>';
|
1641 |
|
1642 | if (parsed.spatials) {
|
1643 | let styling = parse$3(parsed.style);
|
1644 | let x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
|
1645 | let y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
|
1646 | let x2 = parsed.max[0];
|
1647 | let y2 = parsed.max[1];
|
1648 | let classes = '';
|
1649 |
|
1650 | if (styling.classes) {
|
1651 | classes = ` class="${styling.classes}"`;
|
1652 | }
|
1653 |
|
1654 | let id = '';
|
1655 |
|
1656 | if (styling.id) {
|
1657 | id = ` id="${styling.id}"`;
|
1658 | }
|
1659 |
|
1660 | if (styling.padding) {
|
1661 | x1 -= styling.padding;
|
1662 | y1 -= styling.padding;
|
1663 | x2 += styling.padding;
|
1664 | y2 += styling.padding;
|
1665 | }
|
1666 |
|
1667 | let sizing = '';
|
1668 |
|
1669 | if (styling.zoom != 'x') {
|
1670 | sizing = ` width="${(x2 - x1) * (styling.zoom ? styling.zoom : 1)}" height="${(y2 - y1) * (styling.zoom ? styling.zoom : 1)}"`;
|
1671 | }
|
1672 |
|
1673 | let svg = `<svg${classes}${id} version="1.1" xmlns="http://www.w3.org/2000/svg"${sizing} viewBox="${x1} ${y1} ${x2 - x1} ${y2 - y1}">
|
1674 | `;
|
1675 | svg += signSvgBody$1(fswSign);
|
1676 | svg += '\n</svg>';
|
1677 | return svg;
|
1678 | }
|
1679 |
|
1680 | return blank;
|
1681 | };
|
1682 |
|
1683 | const signCanvas$1 = function (fswSign) {
|
1684 | const parsed = parse$2.sign(fswSign);
|
1685 |
|
1686 | if (parsed.spatials) {
|
1687 | const canvas = document.createElement('canvas');
|
1688 | const context = canvas.getContext('2d');
|
1689 | let styling = parse$3(parsed.style);
|
1690 |
|
1691 | if (styling.detailsym) {
|
1692 | styling.detailsym.forEach(sym => {
|
1693 | if (parsed.spatials[sym.index - 1]) {
|
1694 | parsed.spatials[sym.index - 1].detail = sym.detail;
|
1695 | }
|
1696 | });
|
1697 | }
|
1698 |
|
1699 | let x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
|
1700 | let y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
|
1701 | let x2 = parsed.max[0];
|
1702 | let y2 = parsed.max[1];
|
1703 |
|
1704 | if (styling.padding) {
|
1705 | x1 -= styling.padding;
|
1706 | y1 -= styling.padding;
|
1707 | x2 += styling.padding;
|
1708 | y2 += styling.padding;
|
1709 | }
|
1710 |
|
1711 | let sizing = 1;
|
1712 |
|
1713 | if (styling.zoom != 'x') {
|
1714 | sizing = styling.zoom;
|
1715 | }
|
1716 |
|
1717 | let w = (x2 - x1) * sizing;
|
1718 | let h = (y2 - y1) * sizing;
|
1719 | canvas.width = w ? w : 1;
|
1720 | canvas.height = h ? h : 1;
|
1721 |
|
1722 | if (styling.background) {
|
1723 | context.rect(0, 0, w, h);
|
1724 | context.fillStyle = styling.background;
|
1725 | context.fill();
|
1726 | }
|
1727 |
|
1728 | const line = styling.detail && styling.detail[0] || "black";
|
1729 | const fill = styling.detail && styling.detail[1] || "white";
|
1730 | parsed.spatials.forEach(spatial => {
|
1731 | let symLine = line;
|
1732 |
|
1733 | if (spatial.detail) {
|
1734 | symLine = spatial.detail[0];
|
1735 | } else if (styling.colorize) {
|
1736 | symLine = colorize$1(spatial.symbol);
|
1737 | }
|
1738 |
|
1739 | let symFill = fill;
|
1740 |
|
1741 | if (spatial.detail && spatial.detail[1]) {
|
1742 | symFill = spatial.detail[1];
|
1743 | }
|
1744 |
|
1745 | context.font = 30 * sizing + "px 'SuttonSignWritingFill'";
|
1746 | context.fillStyle = symFill;
|
1747 | context.fillText(symbolFill$1(spatial.symbol), (spatial.coord[0] - x1) * sizing, (spatial.coord[1] - y1) * sizing);
|
1748 | context.font = 30 * sizing + "px 'SuttonSignWritingLine'";
|
1749 | context.fillStyle = symLine;
|
1750 | context.fillText(symbolLine$1(spatial.symbol), (spatial.coord[0] - x1) * sizing, (spatial.coord[1] - y1) * sizing);
|
1751 | });
|
1752 | return canvas;
|
1753 | }
|
1754 | };
|
1755 | /**
|
1756 | * Function that creates a PNG data url from an FSW sign with an optional style string
|
1757 | * @function fsw.signPng
|
1758 | * @param {string} fswSign - an FSW sign with optional style string
|
1759 | * @example
|
1760 | * fsw.signPng('M525x535S2e748483x510S10011501x466S20544510x500S10019476x475')
|
1761 | *
|
1762 | * return 'data:image/png;base64,iVBORw...'
|
1763 | */
|
1764 |
|
1765 |
|
1766 | const signPng$1 = fswSign => {
|
1767 | const canvas = signCanvas$1(fswSign);
|
1768 | const png = canvas.toDataURL("image/png");
|
1769 | canvas.remove();
|
1770 | return png;
|
1771 | };
|
1772 |
|
1773 | /**
|
1774 | * Function that normalizes an FSW sign for a center of 500,500
|
1775 | * @function fsw.signNormalize
|
1776 | * @param {string} fswSign - an FSW sign with optional style string
|
1777 | * @example
|
1778 | * fsw.signNormalize('M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475')
|
1779 | *
|
1780 | * return 'M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475'
|
1781 | */
|
1782 |
|
1783 | const signNormalize$1 = fswSign => {
|
1784 | const parsed = parse$2.sign(fswSign);
|
1785 |
|
1786 | if (parsed.spatials) {
|
1787 | const symbolsizes = parsed.spatials.reduce((output, spatial) => {
|
1788 | const size = symbolSize$1(spatial.symbol);
|
1789 | output[spatial.symbol] = {
|
1790 | width: size[0],
|
1791 | height: size[1]
|
1792 | };
|
1793 | return output;
|
1794 | }, {});
|
1795 |
|
1796 | const bbox = symbols => {
|
1797 | const x1 = Math.min(...symbols.map(spatial => spatial.coord[0]));
|
1798 | const y1 = Math.min(...symbols.map(spatial => spatial.coord[1]));
|
1799 | const x2 = Math.max(...symbols.map(spatial => spatial.coord[0] + parseInt(symbolsizes[spatial.symbol].width)));
|
1800 | const y2 = Math.max(...symbols.map(spatial => spatial.coord[1] + parseInt(symbolsizes[spatial.symbol].height)));
|
1801 | return {
|
1802 | x1: x1,
|
1803 | y1: y1,
|
1804 | x2: x2,
|
1805 | y2: y2
|
1806 | };
|
1807 | };
|
1808 |
|
1809 | const hrange = ranges$1['hcenter'];
|
1810 | const hsyms = parsed.spatials.filter(spatial => {
|
1811 | const dec = parseInt(spatial.symbol.slice(1, 4), 16);
|
1812 | return hrange[0] <= dec && hrange[1] >= dec;
|
1813 | });
|
1814 | const vrange = ranges$1['vcenter'];
|
1815 | const vsyms = parsed.spatials.filter(spatial => {
|
1816 | const dec = parseInt(spatial.symbol.slice(1, 4), 16);
|
1817 | return vrange[0] <= dec && vrange[1] >= dec;
|
1818 | });
|
1819 | let abox = bbox(parsed.spatials);
|
1820 | let max = [abox.x2, abox.y2];
|
1821 |
|
1822 | if (hsyms.length) {
|
1823 | const hbox = bbox(hsyms);
|
1824 | abox.x1 = hbox.x1;
|
1825 | abox.x2 = hbox.x2;
|
1826 | }
|
1827 |
|
1828 | if (vsyms.length) {
|
1829 | const vbox = bbox(vsyms);
|
1830 | abox.y1 = vbox.y1;
|
1831 | abox.y2 = vbox.y2;
|
1832 | }
|
1833 |
|
1834 | const offset = [parseInt((abox.x2 + abox.x1) / 2) - 500, parseInt((abox.y2 + abox.y1) / 2) - 500];
|
1835 | const fswout = (parsed.sequence ? 'A' + parsed.sequence.join('') : '') + parsed.box + (max[0] - offset[0]) + 'x' + (max[1] - offset[1]) + parsed.spatials.map(spatial => spatial.symbol + (spatial.coord[0] - offset[0]) + 'x' + (spatial.coord[1] - offset[1])).join('') + (parsed.style || '');
|
1836 | return fswout;
|
1837 | }
|
1838 | };
|
1839 |
|
1840 | /**
|
1841 | * Function that creates an SVG image for a column of FSW
|
1842 | * @function fsw.columnSvg
|
1843 | * @param {ColumnData} fswColumn - an array of objects with information about FSW signs and punctuation
|
1844 | * @param {ColumnOptions} options - an object of column options
|
1845 | * @returns {string} column svg
|
1846 | * @example
|
1847 | * fsw.columnSvg([
|
1848 | * {
|
1849 | * "x": 56,
|
1850 | * "y": 20,
|
1851 | * "minX": 481,
|
1852 | * "minY": 471,
|
1853 | * "width": 37,
|
1854 | * "height": 58,
|
1855 | * "lane": 0,
|
1856 | * "padding": 0,
|
1857 | * "segment": "sign",
|
1858 | * "text": "AS14c20S27106M518x529S14c20481x471S27106503x489",
|
1859 | * "zoom": 1
|
1860 | * },
|
1861 | * {
|
1862 | * "x": 57,
|
1863 | * "y": 118,
|
1864 | * "minX": 482,
|
1865 | * "minY": 468,
|
1866 | * "width": 36,
|
1867 | * "height": 65,
|
1868 | * "lane": 0,
|
1869 | * "padding": 0,
|
1870 | * "segment": "sign",
|
1871 | * "text": "AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468",
|
1872 | * "zoom": 1
|
1873 | * },
|
1874 | * {
|
1875 | * "x": 39,
|
1876 | * "y": 203,
|
1877 | * "minX": 464,
|
1878 | * "minY": 496,
|
1879 | * "width": 72,
|
1880 | * "height": 8,
|
1881 | * "lane": 0,
|
1882 | * "padding": 0,
|
1883 | * "segment": "symbol",
|
1884 | * "text": "S38800464x496",
|
1885 | * "zoom": 1
|
1886 | * }
|
1887 | * ],
|
1888 | * {
|
1889 | * "height": 250,
|
1890 | * "width": 150,
|
1891 | * })
|
1892 | *
|
1893 | * return `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="150" height="250" viewBox="0 0 150 250">
|
1894 | * <g transform="translate(56,20) scale(1) translate(-481,-471) ">
|
1895 | * <text font-size="0">AS14c20S27106M518x529S14c20481x471S27106503x489-D_black,white_Z1</text>
|
1896 | * <g transform="translate(481,471)">
|
1897 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1898 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1899 | * </g>
|
1900 | * <g transform="translate(503,489)">
|
1901 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1902 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1903 | * </g>
|
1904 | * </g>
|
1905 | * <g transform="translate(57,118) scale(1) translate(-482,-468) ">
|
1906 | * <text font-size="0">AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468-D_black,white_Z1</text>
|
1907 | * <g transform="translate(489,515)">
|
1908 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1909 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1910 | * </g>
|
1911 | * <g transform="translate(482,490)">
|
1912 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1913 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1914 | * </g>
|
1915 | * <g transform="translate(508,496)">
|
1916 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1917 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1918 | * </g>
|
1919 | * <g transform="translate(500,468)">
|
1920 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1921 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1922 | * </g>
|
1923 | * </g>
|
1924 | * <g transform="translate(39,203) scale(1) translate(-464,-496) ">
|
1925 | * <text font-size="0">S38800464x496-D_black,white_Z1</text>
|
1926 | * <g transform="translate(464,496)">
|
1927 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1928 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1929 | * </g>
|
1930 | * </g>
|
1931 | * </svg>`
|
1932 | */
|
1933 |
|
1934 | const columnSvg$1 = (fswColumn, options) => {
|
1935 |
|
1936 | if (typeof options !== 'object') options = {};
|
1937 | const values = Object.assign(columnDefaults$1, options);
|
1938 | let x1 = 0;
|
1939 | let y1 = 0;
|
1940 | let x2 = values.width;
|
1941 | let y2 = values.height;
|
1942 | let background = '';
|
1943 |
|
1944 | if (values.background) {
|
1945 | background = `\n <rect x="${x1}" y="${y1}" width="${x2 - x1}" height="${y2 - y1}" style="fill:${values.background};" />`;
|
1946 | }
|
1947 |
|
1948 | let sizing = ` width="${values.width}" height="${values.height}"`;
|
1949 | let svg = `<svg version="1.1" xmlns="http://www.w3.org/2000/svg"${sizing} viewBox="${x1} ${y1} ${x2 - x1} ${y2 - y1}">
|
1950 | ${background}`;
|
1951 | svg += fswColumn.map(item => {
|
1952 | const dash = item.text.indexOf('-');
|
1953 |
|
1954 | if (dash > 0) {
|
1955 | const itemStyle = item.text.substring(dash);
|
1956 | const newStyle = { ...values.style,
|
1957 | ...parse$3(itemStyle)
|
1958 | };
|
1959 | item.text = item.text.replace(itemStyle, compose(newStyle));
|
1960 | } else {
|
1961 | item.text += compose(values.style);
|
1962 | }
|
1963 |
|
1964 | item.zoom = item.zoom * values.style.zoom;
|
1965 | return '<g transform="translate(' + item.x + ',' + item.y + ') scale(' + item.zoom + ') translate(' + -item.minX + ',' + -item.minY + ') ">' + (item.segment == "sign" ? signSvgBody$1(item.text) : symbolSvgBody$1(item.text)) + '</g>';
|
1966 | }).join('\n');
|
1967 | svg += '\n</svg>';
|
1968 | return svg;
|
1969 | };
|
1970 |
|
1971 | /**
|
1972 | * Function that creates an array of SVG column images for an FSW text
|
1973 | * @function fsw.columnsSvg
|
1974 | * @param {string} fswText - a text of FSW signs and punctuation
|
1975 | * @param {ColumnOptions} options - an object of column options
|
1976 | * @returns {string[]} array of SVG columns
|
1977 | * @example
|
1978 | * fsw.columnsSvg('AS14c20S27106M518x529S14c20481x471S27106503x489 AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468 S38800464x496',{
|
1979 | * "height": 250,
|
1980 | * "width": 150,
|
1981 | * })
|
1982 | *
|
1983 | * return [`<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="150" height="250" viewBox="0 0 150 250">
|
1984 | * <g transform="translate(56,20) scale(1) translate(-481,-471) ">
|
1985 | * <text font-size="0">AS14c20S27106M518x529S14c20481x471S27106503x489-D_black,white_Z1</text>
|
1986 | * <g transform="translate(481,471)">
|
1987 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1988 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1989 | * </g>
|
1990 | * <g transform="translate(503,489)">
|
1991 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1992 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
1993 | * </g>
|
1994 | * </g>
|
1995 | * <g transform="translate(57,118) scale(1) translate(-482,-468) ">
|
1996 | * <text font-size="0">AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468-D_black,white_Z1</text>
|
1997 | * <g transform="translate(489,515)">
|
1998 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
1999 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
2000 | * </g>
|
2001 | * <g transform="translate(482,490)">
|
2002 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
2003 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
2004 | * </g>
|
2005 | * <g transform="translate(508,496)">
|
2006 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
2007 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
2008 | * </g>
|
2009 | * <g transform="translate(500,468)">
|
2010 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
2011 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
2012 | * </g>
|
2013 | * </g>
|
2014 | * <g transform="translate(39,203) scale(1) translate(-464,-496) ">
|
2015 | * <text font-size="0">S38800464x496-D_black,white_Z1</text>
|
2016 | * <g transform="translate(464,496)">
|
2017 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
2018 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
2019 | * </g>
|
2020 | * </g>
|
2021 | * </svg>`]
|
2022 | */
|
2023 |
|
2024 | const columnsSvg$1 = function (fswText, options) {
|
2025 | if (typeof options !== 'object') options = {};
|
2026 | let values = columns$1(fswText, options);
|
2027 | let cols = values.columns.map((col, i) => {
|
2028 | return columnSvg$1(col, { ...values.options,
|
2029 | ...{
|
2030 | width: values.widths[i]
|
2031 | }
|
2032 | });
|
2033 | });
|
2034 | return cols;
|
2035 | };
|
2036 |
|
2037 | const columnCanvas$1 = function (fswColumn, options) {
|
2038 | if (typeof options !== 'object') options = {};
|
2039 | const values = Object.assign(columnDefaults$1, options);
|
2040 | const canvas = document.createElement('canvas');
|
2041 | canvas.width = values.width;
|
2042 | canvas.height = values.height;
|
2043 | const context = canvas.getContext('2d');
|
2044 |
|
2045 | if (values.background) {
|
2046 | context.rect(0, 0, values.width, values.height);
|
2047 | context.fillStyle = values.background;
|
2048 | context.fill();
|
2049 | }
|
2050 |
|
2051 | fswColumn.map(item => {
|
2052 | const dash = item.text.indexOf('-');
|
2053 |
|
2054 | if (dash > 0) {
|
2055 | const itemStyle = item.text.substring(dash);
|
2056 | const newStyle = { ...values.style,
|
2057 | ...parse$3(itemStyle)
|
2058 | };
|
2059 | item.text = item.text.replace(itemStyle, compose(newStyle));
|
2060 | } else {
|
2061 | item.text += compose(values.style);
|
2062 | }
|
2063 |
|
2064 | item.zoom = item.zoom * values.style.zoom;
|
2065 | let parsed = {};
|
2066 |
|
2067 | if (item.segment == "sign") {
|
2068 | parsed = parse$2.sign(item.text);
|
2069 | } else {
|
2070 | let sym = parse$2.symbol(item.text);
|
2071 | parsed.style = sym.style;
|
2072 | parsed.spatials = [sym];
|
2073 | }
|
2074 |
|
2075 | let styling = parse$3(parsed.style);
|
2076 |
|
2077 | if (styling.background) {
|
2078 | context.fillStyle = styling.background;
|
2079 | context.fillRect(item.x - styling.padding * item.zoom, item.y - styling.padding * item.zoom, (item.width + styling.padding * 2) * item.zoom, (item.height + styling.padding * 2) * item.zoom);
|
2080 | }
|
2081 |
|
2082 | if (styling.detailsym) {
|
2083 | styling.detailsym.forEach(sym => {
|
2084 | if (parsed.spatials[sym.index - 1]) {
|
2085 | parsed.spatials[sym.index - 1].detail = sym.detail;
|
2086 | }
|
2087 | });
|
2088 | }
|
2089 |
|
2090 | const line = styling.detail && styling.detail[0] || "black";
|
2091 | const fill = styling.detail && styling.detail[1] || "white";
|
2092 | parsed.spatials.forEach(spatial => {
|
2093 | let symLine = line;
|
2094 |
|
2095 | if (spatial.detail) {
|
2096 | symLine = spatial.detail[0];
|
2097 | } else if (styling.colorize) {
|
2098 | symLine = colorize$1(spatial.symbol);
|
2099 | }
|
2100 |
|
2101 | let symFill = fill;
|
2102 |
|
2103 | if (spatial.detail && spatial.detail[1]) {
|
2104 | symFill = spatial.detail[1];
|
2105 | }
|
2106 |
|
2107 | context.font = 30 * item.zoom + "px 'SuttonSignWritingFill'";
|
2108 | context.fillStyle = symFill;
|
2109 | context.fillText(symbolFill$1(spatial.symbol), item.x + (spatial.coord[0] - item.minX) * item.zoom, item.y + (spatial.coord[1] - item.minY) * item.zoom);
|
2110 | context.font = 30 * item.zoom + "px 'SuttonSignWritingLine'";
|
2111 | context.fillStyle = symLine;
|
2112 | context.fillText(symbolLine$1(spatial.symbol), item.x + (spatial.coord[0] - item.minX) * item.zoom, item.y + (spatial.coord[1] - item.minY) * item.zoom);
|
2113 | });
|
2114 | });
|
2115 | return canvas;
|
2116 | };
|
2117 | /**
|
2118 | * Function that creates a PNG data url for a column of FSW
|
2119 | * @function fsw.columnPng
|
2120 | * @param {ColumnData} fswColumn - an array of objects with information about FSW signs and punctuation
|
2121 | * @param {ColumnOptions} options - an object of column options
|
2122 | * @returns {string} column png data url
|
2123 | * @example
|
2124 | * fsw.columnPng([
|
2125 | * {
|
2126 | * "x": 56,
|
2127 | * "y": 20,
|
2128 | * "minX": 481,
|
2129 | * "minY": 471,
|
2130 | * "width": 37,
|
2131 | * "height": 58,
|
2132 | * "lane": 0,
|
2133 | * "padding": 0,
|
2134 | * "segment": "sign",
|
2135 | * "text": "AS14c20S27106M518x529S14c20481x471S27106503x489",
|
2136 | * "zoom": 1
|
2137 | * },
|
2138 | * {
|
2139 | * "x": 57,
|
2140 | * "y": 118,
|
2141 | * "minX": 482,
|
2142 | * "minY": 468,
|
2143 | * "width": 36,
|
2144 | * "height": 65,
|
2145 | * "lane": 0,
|
2146 | * "padding": 0,
|
2147 | * "segment": "sign",
|
2148 | * "text": "AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468",
|
2149 | * "zoom": 1
|
2150 | * },
|
2151 | * {
|
2152 | * "x": 39,
|
2153 | * "y": 203,
|
2154 | * "minX": 464,
|
2155 | * "minY": 496,
|
2156 | * "width": 72,
|
2157 | * "height": 8,
|
2158 | * "lane": 0,
|
2159 | * "padding": 0,
|
2160 | * "segment": "symbol",
|
2161 | * "text": "S38800464x496",
|
2162 | * "zoom": 1
|
2163 | * }
|
2164 | * ],
|
2165 | * {
|
2166 | * "height": 250,
|
2167 | * "width": 150,
|
2168 | * })
|
2169 | *
|
2170 | * return 'data:image/png;base64,iVBORw...'
|
2171 | */
|
2172 |
|
2173 |
|
2174 | const columnPng$1 = (fswColumn, options) => {
|
2175 | const canvas = columnCanvas$1(fswColumn, options);
|
2176 | const png = canvas.toDataURL("image/png");
|
2177 | canvas.remove();
|
2178 | return png;
|
2179 | };
|
2180 |
|
2181 | /**
|
2182 | * Function that creates an array of PNG data urls for an FSW text
|
2183 | * @function fsw.columnsPng
|
2184 | * @param {string} fswText - a text of FSW signs and punctuation
|
2185 | * @param {ColumnOptions} options - an object of column options
|
2186 | * @returns {string[]} array of PNG data urls
|
2187 | * @example
|
2188 | * fsw.columnsPng('AS14c20S27106M518x529S14c20481x471S27106503x489 AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468 S38800464x496',{
|
2189 | * "height": 250,
|
2190 | * "width": 150,
|
2191 | * })
|
2192 | *
|
2193 | * return ['data:image/png;base64,iVBORw...']
|
2194 | */
|
2195 |
|
2196 | const columnsPng$1 = function (fswText, options) {
|
2197 | if (typeof options !== 'object') options = {};
|
2198 | let values = columns$1(fswText, options);
|
2199 | let cols = values.columns.map((col, i) => {
|
2200 | return columnPng$1(col, { ...values.options,
|
2201 | ...{
|
2202 | width: values.widths[i]
|
2203 | }
|
2204 | });
|
2205 | });
|
2206 | return cols;
|
2207 | };
|
2208 |
|
2209 | /** The fsw module contains functions for handling Formal SignWriitng in ASCII (FSW) characters.
|
2210 | * [FSW characters definition](https://tools.ietf.org/id/draft-slevinski-formal-signwriting-09.html#name-formal-signwriting-in-ascii)
|
2211 | * @module fsw
|
2212 | */
|
2213 |
|
2214 | var index$1 = /*#__PURE__*/Object.freeze({
|
2215 | __proto__: null,
|
2216 | symbolSize: symbolSize$1,
|
2217 | symbolLine: symbolLine$1,
|
2218 | symbolFill: symbolFill$1,
|
2219 | symbolText: symbolText$1,
|
2220 | symbolSvgBody: symbolSvgBody$1,
|
2221 | symbolSvg: symbolSvg$1,
|
2222 | symbolPng: symbolPng$1,
|
2223 | symbolNormalize: symbolNormalize$1,
|
2224 | signSvgBody: signSvgBody$1,
|
2225 | signSvg: signSvg$1,
|
2226 | signPng: signPng$1,
|
2227 | signNormalize: signNormalize$1,
|
2228 | columnSvg: columnSvg$1,
|
2229 | columnsSvg: columnsSvg$1,
|
2230 | columnPng: columnPng$1,
|
2231 | columnsPng: columnsPng$1
|
2232 | });
|
2233 |
|
2234 | /**
|
2235 | * Function that returns the size of a symbol using an SWU symbol character
|
2236 | * @function swu.symbolSize
|
2237 | * @param {string} swu - an SWU symbol character
|
2238 | * @example
|
2239 | * swu.symbolSize("")
|
2240 | *
|
2241 | * return [15,30]
|
2242 | */
|
2243 |
|
2244 | const symbolSize = function (swu) {
|
2245 | return symbolSize$2(swu2id(swu));
|
2246 | };
|
2247 |
|
2248 | /**
|
2249 | * Function that returns a plane 15 character for a symbol line using an SWU symbol character
|
2250 | * @function swu.symbolLine
|
2251 | * @param {string} swu - an SWU symbol character
|
2252 | * @example
|
2253 | * swu.symbolLine('')
|
2254 | *
|
2255 | * return ''
|
2256 | */
|
2257 |
|
2258 | const symbolLine = function (swu) {
|
2259 | return symbolLine$2(swu2id(swu));
|
2260 | };
|
2261 | /**
|
2262 | * Function that returns a plane 165 character for a symbol fill using an SWU symbol character
|
2263 | * @function swu.symbolFill
|
2264 | * @param {string} swu - an SWU symbol character
|
2265 | * @example
|
2266 | * swu.symbolFill('')
|
2267 | *
|
2268 | * return ''
|
2269 | */
|
2270 |
|
2271 |
|
2272 | const symbolFill = function (swu) {
|
2273 | return symbolFill$2(swu2id(swu));
|
2274 | };
|
2275 | /**
|
2276 | * Function that creates two text elements for a symbol using an SWU symbol character
|
2277 | * @function swu.symbolText
|
2278 | * @param {string} swu - an SWU symbol character
|
2279 | * @example
|
2280 | * swu.symbolText('')
|
2281 | *
|
2282 | * return ` <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
2283 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>`
|
2284 | */
|
2285 |
|
2286 |
|
2287 | const symbolText = function (swu) {
|
2288 | return symbolText$2(swu2id(swu));
|
2289 | };
|
2290 |
|
2291 | /**
|
2292 | * Sutton SignWriting Core Module v1.4.2 (https://github.com/sutton-signwriting/core)
|
2293 | * Author: Steve Slevinski (https://SteveSlevinski.me)
|
2294 | * swu.mjs is released under the MIT License.
|
2295 | */
|
2296 |
|
2297 | /**
|
2298 | * Object of regular expressions for SWU strings in UTF-16
|
2299 | *
|
2300 | * @alias swu.re
|
2301 | * @property {string} symbol - regular expressions for a symbol
|
2302 | * @property {string} coord - regular expressions for a coordinate
|
2303 | * @property {string} sort - regular expressions for the sorting marker
|
2304 | * @property {string} box - regular expression for a signbox marker
|
2305 | * @property {string} prefix - regular expression for a sorting marker followed by one or more symbols
|
2306 | * @property {string} spatial - regular expression for a symbol followed by a coordinate
|
2307 | * @property {string} signbox - regular expression for a signbox marker, max coordinate and zero or more spatial symbols
|
2308 | * @property {string} sign - regular expression for an optional prefix followed by a signbox
|
2309 | * @property {string} sortable - regular expression for a mandatory prefix followed by a signbox
|
2310 | */
|
2311 | let re$1 = {
|
2312 | 'symbol': '(?:(?:\uD8C0[\uDC01-\uDFFF])|(?:[\uD8C1-\uD8FC][\uDC00-\uDFFF])|(?:\uD8FD[\uDC00-\uDC80]))',
|
2313 | 'coord': '(?:\uD836[\uDC0C-\uDDFF]){2}',
|
2314 | 'sort': '\uD836\uDC00',
|
2315 | 'box': '\uD836[\uDC01-\uDC04]'
|
2316 | };
|
2317 | re$1.prefix = `(?:${re$1.sort}(?:${re$1.symbol})+)`;
|
2318 | re$1.spatial = `${re$1.symbol}${re$1.coord}`;
|
2319 | re$1.signbox = `${re$1.box}${re$1.coord}(?:${re$1.spatial})*`;
|
2320 | re$1.sign = `${re$1.prefix}?${re$1.signbox}`;
|
2321 | re$1.sortable = `${re$1.prefix}${re$1.signbox}`;
|
2322 | /**
|
2323 | * Object of regular expressions for style strings
|
2324 | *
|
2325 | * @alias style.re
|
2326 | * @type {object}
|
2327 | * @property {string} colorize - regular expression for colorize section
|
2328 | * @property {string} colorhex - regular expression for color hex values with 3 or 6 characters
|
2329 | * @property {string} colorname - regular expression for css color name
|
2330 | * @property {string} padding - regular expression for padding section
|
2331 | * @property {string} zoom - regular expression for zoom section
|
2332 | * @property {string} classbase - regular expression for class name definition
|
2333 | * @property {string} id - regular expression for id definition
|
2334 | * @property {string} colorbase - regular expression for color hex or color name
|
2335 | * @property {string} color - regular expression for single color entry
|
2336 | * @property {string} colors - regular expression for double color entry
|
2337 | * @property {string} background - regular expression for background section
|
2338 | * @property {string} detail - regular expression for color details for line and optional fill
|
2339 | * @property {string} detailsym - regular expression for color details for individual symbols
|
2340 | * @property {string} classes - regular expression for one or more class names
|
2341 | * @property {string} full - full regular expression for style string
|
2342 | */
|
2343 |
|
2344 | let re = {
|
2345 | 'colorize': 'C',
|
2346 | 'colorhex': '(?:[0-9a-fA-F]{3}){1,2}',
|
2347 | 'colorname': '[a-zA-Z]+',
|
2348 | 'padding': 'P[0-9]{2}',
|
2349 | 'zoom': 'Z(?:[0-9]+(?:\\.[0-9]+)?|x)',
|
2350 | 'classbase': '-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}',
|
2351 | 'id': '[a-zA-Z][_a-zA-Z0-9-]{0,100}'
|
2352 | };
|
2353 | re.colorbase = `(?:${re.colorhex}|${re.colorname})`;
|
2354 | re.color = `_${re.colorbase}_`;
|
2355 | re.colors = `_${re.colorbase}(?:,${re.colorbase})?_`;
|
2356 | re.background = `G${re.color}`;
|
2357 | re.detail = `D${re.colors}`;
|
2358 | re.detailsym = `D[0-9]{2}${re.colors}`;
|
2359 | re.classes = `${re.classbase}(?: ${re.classbase})*`;
|
2360 | re.full = `-(${re.colorize})?(${re.padding})?(${re.background})?(${re.detail})?(${re.zoom})?(?:-((?:${re.detailsym})*))?(?:-(${re.classes})?!(?:(${re.id})!)?)?`;
|
2361 |
|
2362 | const prefixColor = color => {
|
2363 | const regex = new RegExp(`^${re.colorhex}$`);
|
2364 | return (regex.test(color) ? '#' : '') + color;
|
2365 | };
|
2366 |
|
2367 | const definedProps = obj => Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== undefined));
|
2368 | /**
|
2369 | * Function to parse style string to object
|
2370 | * @function style.parse
|
2371 | * @param {string} styleString - a style string
|
2372 | * @returns {StyleObject} elements of style string
|
2373 | * @example
|
2374 | * style.parse('-CP10G_blue_D_red,Cyan_')
|
2375 | *
|
2376 | * return {
|
2377 | * 'colorize': true,
|
2378 | * 'padding': 10,
|
2379 | * 'background': 'blue',
|
2380 | * 'detail': ['red', 'Cyan']
|
2381 | * }
|
2382 | */
|
2383 |
|
2384 |
|
2385 | const parse$1 = styleString => {
|
2386 | const regex = `^${re.full}`;
|
2387 | const m = (typeof styleString === 'string' ? styleString.match(new RegExp(regex)) : []) || [];
|
2388 | return definedProps({
|
2389 | 'colorize': !m[1] ? undefined : !!m[1],
|
2390 | 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
|
2391 | 'background': !m[3] ? undefined : prefixColor(m[3].slice(2, -1)),
|
2392 | 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor),
|
2393 | 'zoom': !m[5] ? undefined : m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1)),
|
2394 | 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re.detailsym, 'g')).map(val => {
|
2395 | const parts = val.split('_');
|
2396 | const detail = parts[1].split(',').map(prefixColor);
|
2397 | return {
|
2398 | 'index': parseInt(parts[0].slice(1)),
|
2399 | 'detail': detail
|
2400 | };
|
2401 | }),
|
2402 | 'classes': !m[7] ? undefined : m[7],
|
2403 | 'id': !m[8] ? undefined : m[8]
|
2404 | });
|
2405 | };
|
2406 | /** The convert module contains functions to convert between Formal SignWriitng in ASCII (FSW) and SignWriting in Unicode (SWU) characters, along with other types of data.
|
2407 | * [Characters set definitions](https://tools.ietf.org/id/draft-slevinski-formal-signwriting-09.html#name-characters)
|
2408 | * @module convert
|
2409 | */
|
2410 |
|
2411 | /**
|
2412 | * Function to convert an SWU number character to an integer
|
2413 | * @function convert.swu2num
|
2414 | * @param {string} swuNum - SWU number character
|
2415 | * @returns {number} Integer value for number
|
2416 | * @example
|
2417 | * convert.swu2num('𝤆')
|
2418 | *
|
2419 | * return 500
|
2420 | */
|
2421 |
|
2422 |
|
2423 | const swu2num = swuNum => parseInt(swuNum.codePointAt(0)) - 0x1D80C + 250;
|
2424 | /**
|
2425 | * Function to convert two SWU number characters to an array of x,y integers
|
2426 | * @function convert.swu2coord
|
2427 | * @param {string} swuCoord - Two SWU number character
|
2428 | * @returns {number[]} Array of x,y integers
|
2429 | * @example
|
2430 | * convert.swu2coord('𝤆𝤆')
|
2431 | *
|
2432 | * return [500, 500]
|
2433 | */
|
2434 |
|
2435 |
|
2436 | const swu2coord = swuCoord => [swu2num(swuCoord.slice(0, 2)), swu2num(swuCoord.slice(2, 4))];
|
2437 | /**
|
2438 | * Function to convert an SWU symbol character to a code point on plane 4
|
2439 | * @function convert.swu2code
|
2440 | * @param {string} swuSym - SWU symbol character
|
2441 | * @returns {number} Code point on plane 4
|
2442 | * @example
|
2443 | * convert.swu2code('')
|
2444 | *
|
2445 | * return 0x40001
|
2446 | */
|
2447 |
|
2448 |
|
2449 | const swu2code = swuSym => parseInt(swuSym.codePointAt(0));
|
2450 |
|
2451 | const parse = {
|
2452 | /**
|
2453 | * Function to parse an swu symbol with optional coordinate and style string
|
2454 | * @function swu.parse.symbol
|
2455 | * @param {string} swuSym - an swu symbol
|
2456 | * @returns {object} elements of swu symbol
|
2457 | * @example
|
2458 | * swu.parse.symbol('𝤆𝤆-C')
|
2459 | *
|
2460 | * return {
|
2461 | * 'symbol': '',
|
2462 | * 'coord': [500, 500],
|
2463 | * 'style': '-C'
|
2464 | * }
|
2465 | */
|
2466 | symbol: swuSym => {
|
2467 | const regex = `^(${re$1.symbol})(${re$1.coord})?(${re.full})?`;
|
2468 | const symbol = typeof swuSym === 'string' ? swuSym.match(new RegExp(regex)) : undefined;
|
2469 | return {
|
2470 | 'symbol': symbol ? symbol[1] : undefined,
|
2471 | 'coord': symbol && symbol[2] ? swu2coord(symbol[2]) : undefined,
|
2472 | 'style': symbol ? symbol[3] : undefined
|
2473 | };
|
2474 | },
|
2475 |
|
2476 | /**
|
2477 | * Function to parse an swu sign with style string
|
2478 | * @function swu.parse.sign
|
2479 | * @param {string} swuSign - an swu sign
|
2480 | * @returns {object} elements of swu sign
|
2481 | * @example
|
2482 | * swu.parse.sign('𝠀𝠃𝤟𝤩𝣵𝤐𝤇𝣤𝤐𝤆𝣮𝣭-C')
|
2483 | *
|
2484 | * return {
|
2485 | * sequence: ['','','',''],
|
2486 | * box: '𝠃',
|
2487 | * max: [525, 535],
|
2488 | * spatials: [
|
2489 | * {
|
2490 | * symbol: '',
|
2491 | * coord: [483, 510]
|
2492 | * },
|
2493 | * {
|
2494 | * symbol: '',
|
2495 | * coord: [501, 466]
|
2496 | * },
|
2497 | * {
|
2498 | * symbol: '',
|
2499 | * coord: [510, 500]
|
2500 | * },
|
2501 | * {
|
2502 | * symbol: '',
|
2503 | * coord: [476, 475]
|
2504 | * }
|
2505 | * ],
|
2506 | * style: '-C'
|
2507 | * }
|
2508 | */
|
2509 | sign: swuSign => {
|
2510 | const regex = `^(${re$1.prefix})?(${re$1.signbox})(${re.full})?`;
|
2511 | const sign = typeof swuSign === 'string' ? swuSign.match(new RegExp(regex)) : undefined;
|
2512 |
|
2513 | if (sign) {
|
2514 | return {
|
2515 | 'sequence': sign[1] ? sign[1].slice(2).match(/.{2}/g) : undefined,
|
2516 | 'box': sign[2].slice(0, 2),
|
2517 | 'max': swu2coord(sign[2].slice(2, 6)),
|
2518 | 'spatials': sign[2].length < 7 ? undefined : sign[2].slice(6).match(/(.{6})/g).map(m => {
|
2519 | return {
|
2520 | symbol: m.slice(0, 2),
|
2521 | coord: swu2coord(m.slice(2))
|
2522 | };
|
2523 | }),
|
2524 | 'style': sign[3]
|
2525 | };
|
2526 | } else {
|
2527 | return {};
|
2528 | }
|
2529 | },
|
2530 |
|
2531 | /**
|
2532 | * Function to parse an swu text
|
2533 | * @function swu.parse.text
|
2534 | * @param {string} swuText - an swu text
|
2535 | * @returns {array} swu signs and punctuations
|
2536 | * @example
|
2537 | * swu.parse.text('𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻 𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦 𝣢𝤂')
|
2538 | *
|
2539 | * return [
|
2540 | * '𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻',
|
2541 | * '𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦',
|
2542 | * '𝣢𝤂'
|
2543 | * ]
|
2544 | */
|
2545 | text: swuText => {
|
2546 | if (typeof swuText !== 'string') return [];
|
2547 | const regex = `(${re$1.sign}(${re.full})?|${re$1.spatial}(${re.full})?)`;
|
2548 | const matches = swuText.match(new RegExp(regex, 'g'));
|
2549 | return matches ? [...matches] : [];
|
2550 | }
|
2551 | };
|
2552 | /**
|
2553 | * Function to gather sizing information about an swu sign or symbol
|
2554 | * @function swu.info
|
2555 | * @param {string} swu - an swu sign or symbol
|
2556 | * @returns {object} information about the swu string
|
2557 | * @example
|
2558 | * swu.info('𝠀𝠂𝤘𝤣𝣳𝣩𝤉𝣻-P10Z2')
|
2559 | *
|
2560 | * return {
|
2561 | * minX: 481,
|
2562 | * minY: 471,
|
2563 | * width: 37,
|
2564 | * height: 58,
|
2565 | * segment: 'sign',
|
2566 | * lane: -1
|
2567 | * padding: 10,
|
2568 | * zoom: 2
|
2569 | * }
|
2570 | */
|
2571 |
|
2572 | const info = swu => {
|
2573 | let lanes = {
|
2574 | '𝠁': 0,
|
2575 | '𝠂': -1,
|
2576 | '𝠃': 0,
|
2577 | '𝠄': 1
|
2578 | };
|
2579 | let parsed = parse.sign(swu);
|
2580 | let width, height, segment, x1, x2, y1, y2, lane;
|
2581 |
|
2582 | if (parsed.spatials) {
|
2583 | x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
|
2584 | x2 = parsed.max[0];
|
2585 | width = x2 - x1;
|
2586 | y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
|
2587 | y2 = parsed.max[1];
|
2588 | height = y2 - y1;
|
2589 | segment = 'sign';
|
2590 | lane = parsed.box;
|
2591 | } else {
|
2592 | parsed = parse.symbol(swu);
|
2593 | lane = "𝠃";
|
2594 |
|
2595 | if (parsed.coord) {
|
2596 | x1 = parsed.coord[0];
|
2597 | width = (500 - x1) * 2;
|
2598 | y1 = parsed.coord[1];
|
2599 | height = (500 - y1) * 2;
|
2600 | segment = 'symbol';
|
2601 | } else {
|
2602 | x1 = 490;
|
2603 | width = 20;
|
2604 | y1 = 490;
|
2605 | height = 20;
|
2606 | segment = 'none';
|
2607 | }
|
2608 | }
|
2609 |
|
2610 | let style = parse$1(parsed.style);
|
2611 | let zoom = style.zoom || 1;
|
2612 | let padding = style.padding || 0;
|
2613 | return {
|
2614 | minX: x1,
|
2615 | minY: y1,
|
2616 | width: width,
|
2617 | height: height,
|
2618 | segment: segment,
|
2619 | lane: lanes[lane],
|
2620 | padding: padding,
|
2621 | zoom: zoom
|
2622 | };
|
2623 | };
|
2624 |
|
2625 | const columnDefaults = {
|
2626 | 'height': 500,
|
2627 | 'width': 150,
|
2628 | 'offset': 50,
|
2629 | 'pad': 20,
|
2630 | 'margin': 5,
|
2631 | 'dynamic': false,
|
2632 | 'background': undefined,
|
2633 | 'punctuation': {
|
2634 | 'spacing': true,
|
2635 | 'pad': 30,
|
2636 | 'pull': true
|
2637 | },
|
2638 | 'style': {
|
2639 | 'detail': ['black', 'white'],
|
2640 | 'zoom': 1
|
2641 | }
|
2642 | };
|
2643 | /**
|
2644 | * Function to an object of column options with default values
|
2645 | *
|
2646 | * @function swu.columnDefaultsMerge
|
2647 | * @param {ColumnOptions} options - object of column options
|
2648 | * @returns {ColumnOptions} object of column options merged with column defaults
|
2649 | * @example
|
2650 | * swu.columnDefaultsMerge({height: 500,width:150})
|
2651 | *
|
2652 | * return {
|
2653 | * "height": 500,
|
2654 | * "width": 150,
|
2655 | * "offset": 50,
|
2656 | * "pad": 20,
|
2657 | * "margin": 5,
|
2658 | * "dynamic": false,
|
2659 | * "punctuation": {
|
2660 | * "spacing": true,
|
2661 | * "pad": 30,
|
2662 | * "pull": true
|
2663 | * },
|
2664 | * "style": {
|
2665 | * "detail": [
|
2666 | * "black",
|
2667 | * "white"
|
2668 | * ],
|
2669 | * "zoom": 1
|
2670 | * }
|
2671 | * }
|
2672 | */
|
2673 |
|
2674 | const columnDefaultsMerge = options => {
|
2675 | if (typeof options !== 'object') options = {};
|
2676 | return { ...columnDefaults,
|
2677 | ...options,
|
2678 | punctuation: { ...columnDefaults.punctuation,
|
2679 | ...options.punctuation
|
2680 | },
|
2681 | style: { ...columnDefaults.style,
|
2682 | ...options.style
|
2683 | }
|
2684 | };
|
2685 | };
|
2686 | /**
|
2687 | * Function to transform an SWU text to an array of columns
|
2688 | *
|
2689 | * @function swu.columns
|
2690 | * @param {string} swuText - SWU text of signs and punctuation
|
2691 | * @param {ColumnOptions} options - object of column options
|
2692 | * @returns {{options:ColumnOptions,widths:number[],columns:ColumnData}} object of column options, widths array, and column data
|
2693 | * @example
|
2694 | * swu.columns('𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻 𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦 𝣢𝤂', {height: 500,width:150})
|
2695 | *
|
2696 | * return {
|
2697 | * "options": {
|
2698 | * "height": 500,
|
2699 | * "width": 150,
|
2700 | * "offset": 50,
|
2701 | * "pad": 20,
|
2702 | * "margin": 5,
|
2703 | * "dynamic": false,
|
2704 | * "punctuation": {
|
2705 | * "spacing": true,
|
2706 | * "pad": 30,
|
2707 | * "pull": true
|
2708 | * },
|
2709 | * "style": {
|
2710 | * "detail": [
|
2711 | * "black",
|
2712 | * "white"
|
2713 | * ],
|
2714 | * "zoom": 1
|
2715 | * }
|
2716 | * },
|
2717 | * "widths": [
|
2718 | * 150
|
2719 | * ],
|
2720 | * "columns": [
|
2721 | * [
|
2722 | * {
|
2723 | * "x": 56,
|
2724 | * "y": 20,
|
2725 | * "minX": 481,
|
2726 | * "minY": 471,
|
2727 | * "width": 37,
|
2728 | * "height": 58,
|
2729 | * "lane": 0,
|
2730 | * "padding": 0,
|
2731 | * "segment": "sign",
|
2732 | * "text": "𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻",
|
2733 | * "zoom": 1
|
2734 | * },
|
2735 | * {
|
2736 | * "x": 57,
|
2737 | * "y": 118,
|
2738 | * "minX": 482,
|
2739 | * "minY": 468,
|
2740 | * "width": 36,
|
2741 | * "height": 65,
|
2742 | * "lane": 0,
|
2743 | * "padding": 0,
|
2744 | * "segment": "sign",
|
2745 | * "text": "𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦",
|
2746 | * "zoom": 1
|
2747 | * },
|
2748 | * {
|
2749 | * "x": 39,
|
2750 | * "y": 203,
|
2751 | * "minX": 464,
|
2752 | * "minY": 496,
|
2753 | * "width": 72,
|
2754 | * "height": 8,
|
2755 | * "lane": 0,
|
2756 | * "padding": 0,
|
2757 | * "segment": "symbol",
|
2758 | * "text": "𝣢𝤂",
|
2759 | * "zoom": 1
|
2760 | * }
|
2761 | * ]
|
2762 | * ]
|
2763 | * }
|
2764 | */
|
2765 |
|
2766 |
|
2767 | const columns = (swuText, options) => {
|
2768 | if (typeof swuText !== 'string') return {};
|
2769 | const values = columnDefaultsMerge(options);
|
2770 | let input = parse.text(swuText);
|
2771 | let cursor = 0;
|
2772 | let cols = [];
|
2773 | let col = [];
|
2774 | let plus = 0;
|
2775 | let center = parseInt(values.width / 2);
|
2776 | let maxHeight = values.height - values.margin;
|
2777 | let pullable = true;
|
2778 | let finalize = false;
|
2779 |
|
2780 | for (let val of input) {
|
2781 | let informed = info(val);
|
2782 | cursor += plus;
|
2783 |
|
2784 | if (values.punctuation.spacing) {
|
2785 | cursor += informed.segment == 'sign' ? values.pad : 0;
|
2786 | } else {
|
2787 | cursor += values.pad;
|
2788 | }
|
2789 |
|
2790 | finalize = cursor + informed.height > maxHeight;
|
2791 |
|
2792 | if (finalize && informed.segment == 'symbol' && values.punctuation.pull && pullable) {
|
2793 | finalize = false;
|
2794 | pullable = false;
|
2795 | }
|
2796 |
|
2797 | if (col.length == 0) {
|
2798 | finalize = false;
|
2799 | }
|
2800 |
|
2801 | if (finalize) {
|
2802 | cursor = values.pad;
|
2803 | cols.push(col);
|
2804 | col = [];
|
2805 | pullable = true;
|
2806 | }
|
2807 |
|
2808 | col.push(Object.assign(informed, {
|
2809 | x: center + values.offset * informed.lane - (500 - informed.minX) * informed.zoom * values.style.zoom,
|
2810 | y: cursor,
|
2811 | text: val
|
2812 | }));
|
2813 | cursor += informed.height * informed.zoom * values.style.zoom;
|
2814 |
|
2815 | if (values.punctuation.spacing) {
|
2816 | plus = informed.segment == 'sign' ? values.pad : values.punctuation.pad;
|
2817 | } else {
|
2818 | plus = values.pad;
|
2819 | }
|
2820 | }
|
2821 |
|
2822 | if (col.length) {
|
2823 | cols.push(col);
|
2824 | } // over height issue when pulling punctuation
|
2825 |
|
2826 |
|
2827 | if (values.punctuation.pull) {
|
2828 | for (let col of cols) {
|
2829 | let last = col[col.length - 1];
|
2830 | let diff = last.y + last.height - (values.height - values.margin);
|
2831 |
|
2832 | if (diff > 0) {
|
2833 | let adj = parseInt(diff / col.length) + 1;
|
2834 |
|
2835 | for (let i in col) {
|
2836 | col[i].y -= adj * i + adj;
|
2837 | }
|
2838 | }
|
2839 | }
|
2840 | } // contract, expand, adjust
|
2841 |
|
2842 |
|
2843 | let widths = [];
|
2844 |
|
2845 | for (let col of cols) {
|
2846 | let min = [center - values.offset - values.pad];
|
2847 | let max = [center + values.offset + values.pad];
|
2848 |
|
2849 | for (let item of col) {
|
2850 | min.push(item.x - values.pad);
|
2851 | max.push(item.x + item.width + values.pad);
|
2852 | }
|
2853 |
|
2854 | min = Math.min(...min);
|
2855 | max = Math.max(...max);
|
2856 | let width = values.width;
|
2857 | let adj = 0;
|
2858 |
|
2859 | if (!values.dynamic) {
|
2860 | adj = center - parseInt((min + max) / 2);
|
2861 | } else {
|
2862 | width = max - min;
|
2863 | adj = -min;
|
2864 | }
|
2865 |
|
2866 | for (let item of col) {
|
2867 | item.x += adj;
|
2868 | }
|
2869 |
|
2870 | widths.push(width);
|
2871 | }
|
2872 |
|
2873 | return {
|
2874 | 'options': values,
|
2875 | 'widths': widths,
|
2876 | 'columns': cols
|
2877 | };
|
2878 | };
|
2879 | /**
|
2880 | * Array of plane 4 code points for categories of symbols: hand, movement, dynamics, head, trunk & limb, location, and punctuation.
|
2881 | * @alias swu.category
|
2882 | * @type {array}
|
2883 | */
|
2884 |
|
2885 | const category = [0x40001, 0x461e1, 0x4bca1, 0x4bfa1, 0x4e8e1, 0x4efa1, 0x4f2a1];
|
2886 | /**
|
2887 | * Object of symbol ranges with starting and ending code points on plane 4.
|
2888 | *
|
2889 | * { all, writing, hand, movement, dynamic, head, hcenter, vcenter, trunk, limb, location, punctuation }
|
2890 | * @alias swu.ranges
|
2891 | * @type {object}
|
2892 | */
|
2893 |
|
2894 | const ranges = {
|
2895 | 'all': [0x40001, 0x4f480],
|
2896 | 'writing': [0x40001, 0x4efa0],
|
2897 | 'hand': [0x40001, 0x461e0],
|
2898 | 'movement': [0x461e1, 0x4bca0],
|
2899 | 'dynamic': [0x4bca1, 0x4bfa0],
|
2900 | 'head': [0x4bfa1, 0x4e8e0],
|
2901 | 'hcenter': [0x4bfa1, 0x4e8e0],
|
2902 | 'vcenter': [0x4bfa1, 0x4ec40],
|
2903 | 'trunk': [0x4e8e1, 0x4ec40],
|
2904 | 'limb': [0x4ec41, 0x4efa0],
|
2905 | 'location': [0x4efa1, 0x4f2a0],
|
2906 | 'punctuation': [0x4f2a1, 0x4f480]
|
2907 | };
|
2908 | /**
|
2909 | * Array of colors associated with the seven symbol categories.
|
2910 | * @alias swu.colors
|
2911 | * @type {array}
|
2912 | */
|
2913 |
|
2914 |
|
2915 | const colors = ['#0000CC', '#CC0000', '#FF0099', '#006600', '#000000', '#884411', '#FF9900'];
|
2916 | /**
|
2917 | * Function that returns the standardized color for a symbol.
|
2918 | * @function swu.colorize
|
2919 | * @param {string} swuSym - an SWU symbol character
|
2920 | * @returns {string} name of standardized color for symbol
|
2921 | * @example
|
2922 | * swu.colorize('')
|
2923 | *
|
2924 | * return '#0000CC'
|
2925 | */
|
2926 |
|
2927 | const colorize = swuSym => {
|
2928 | const parsed = parse.symbol(swuSym);
|
2929 | let color = '#000000';
|
2930 |
|
2931 | if (parsed.symbol) {
|
2932 | const code = swu2code(parsed.symbol);
|
2933 | const index = category.findIndex(val => val > code);
|
2934 | color = colors[index < 0 ? 6 : index - 1];
|
2935 | }
|
2936 |
|
2937 | return color;
|
2938 | };
|
2939 | /* support ongoing development on https://patreon.com/signwriting */
|
2940 |
|
2941 | /**
|
2942 | * Function that creates an SVG image from an SWU symbol key with an optional style string
|
2943 | * @function swu.symbolSvgBody
|
2944 | * @param {string} swuSym - an SWU symbol key with optional style string
|
2945 | * @example
|
2946 | * swu.symbolSvgBody('S10000')
|
2947 | *
|
2948 | * return `<text font-size="0">S10000</text>
|
2949 | * <g transform="translate(500,500)">
|
2950 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
2951 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
2952 | * </g>`
|
2953 | */
|
2954 |
|
2955 | const symbolSvgBody = swuSym => {
|
2956 | const parsed = parse.symbol(swuSym);
|
2957 | const blank = '';
|
2958 | if (!parsed.symbol) return blank;
|
2959 | let styling = parse$3(parsed.style);
|
2960 | let x1, y1, x2, y2;
|
2961 |
|
2962 | if (parsed.coord) {
|
2963 | x1 = parsed.coord[0];
|
2964 | y1 = parsed.coord[1];
|
2965 | x2 = 500 + (500 - x1);
|
2966 | y2 = 500 + (500 - y1);
|
2967 | } else {
|
2968 | let size = symbolSize(parsed.symbol);
|
2969 | if (!size) return blank;
|
2970 | x1 = 500 - parseInt((size[0] + 1) / 2);
|
2971 | y1 = 500 - parseInt((size[1] + 1) / 2);
|
2972 | x2 = 500 + (500 - x1);
|
2973 | y2 = 500 + (500 - y1);
|
2974 | }
|
2975 |
|
2976 | let symSvg = symbolText(parsed.symbol);
|
2977 | symSvg = ` <g transform="translate(${x1},${y1})">
|
2978 | ${symSvg}
|
2979 | </g>`;
|
2980 | let line;
|
2981 |
|
2982 | if (styling.colorize) {
|
2983 | line = colorize(parsed.symbol);
|
2984 | } else if (styling.detail) {
|
2985 | line = styling.detail[0];
|
2986 | }
|
2987 |
|
2988 | if (line) {
|
2989 | symSvg = symSvg.replace(/class="sym-line" fill="black"/, `class="sym-line" fill="${line}"`);
|
2990 | }
|
2991 |
|
2992 | let fill = styling.detail && styling.detail[1];
|
2993 |
|
2994 | if (fill) {
|
2995 | symSvg = symSvg.replace(/class="sym-fill" fill="white"/, `class="sym-fill" fill="${fill}"`);
|
2996 | }
|
2997 |
|
2998 | let background = '';
|
2999 |
|
3000 | if (styling.padding) {
|
3001 | x1 -= styling.padding;
|
3002 | y1 -= styling.padding;
|
3003 | x2 += styling.padding;
|
3004 | y2 += styling.padding;
|
3005 | }
|
3006 |
|
3007 | if (styling.background) {
|
3008 | background = `\n <rect x="${x1}" y="${y1}" width="${x2 - x1}" height="${y2 - y1}" style="fill:${styling.background};" />`;
|
3009 | }
|
3010 |
|
3011 | return ` <text font-size="0">${swuSym}</text>${background}
|
3012 | ${symSvg}`;
|
3013 | };
|
3014 | /**
|
3015 | * Function that creates an SVG image from an SWU symbol key with an optional style string
|
3016 | * @function swu.symbolSvg
|
3017 | * @param {string} swuSym - an SWU symbol key with optional style string
|
3018 | * @example
|
3019 | * swu.symbolSvg('S10000')
|
3020 | *
|
3021 | * return `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="15" height="30" viewBox="500 500 15 30">
|
3022 | * <text font-size="0">S10000</text>
|
3023 | * <g transform="translate(500,500)">
|
3024 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3025 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3026 | * </g>
|
3027 | * </svg>`
|
3028 | */
|
3029 |
|
3030 |
|
3031 | const symbolSvg = swuSym => {
|
3032 | const parsed = parse.symbol(swuSym);
|
3033 | const blank = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>';
|
3034 | if (!parsed.symbol) return blank;
|
3035 | let styling = parse$3(parsed.style);
|
3036 | let x1, y1, x2, y2;
|
3037 |
|
3038 | if (parsed.coord) {
|
3039 | x1 = parsed.coord[0];
|
3040 | y1 = parsed.coord[1];
|
3041 | x2 = 500 + (500 - x1);
|
3042 | y2 = 500 + (500 - y1);
|
3043 | } else {
|
3044 | let size = symbolSize(parsed.symbol);
|
3045 | if (!size) return blank;
|
3046 | x1 = parseInt(500 - size[0] / 2);
|
3047 | y1 = parseInt(500 - size[1] / 2);
|
3048 | x2 = x1 + size[0];
|
3049 | y2 = y1 + size[1];
|
3050 | }
|
3051 |
|
3052 | let classes = '';
|
3053 |
|
3054 | if (styling.classes) {
|
3055 | classes = ` class="${styling.classes}"`;
|
3056 | }
|
3057 |
|
3058 | let id = '';
|
3059 |
|
3060 | if (styling.id) {
|
3061 | id = ` id="${styling.id}"`;
|
3062 | }
|
3063 |
|
3064 | if (styling.padding) {
|
3065 | x1 -= styling.padding;
|
3066 | y1 -= styling.padding;
|
3067 | x2 += styling.padding;
|
3068 | y2 += styling.padding;
|
3069 | }
|
3070 |
|
3071 | let sizing = '';
|
3072 |
|
3073 | if (styling.zoom != 'x') {
|
3074 | sizing = ` width="${(x2 - x1) * (styling.zoom ? styling.zoom : 1)}" height="${(y2 - y1) * (styling.zoom ? styling.zoom : 1)}"`;
|
3075 | }
|
3076 |
|
3077 | return `<svg${classes}${id} version="1.1" xmlns="http://www.w3.org/2000/svg"${sizing} viewBox="${x1} ${y1} ${x2 - x1} ${y2 - y1}">
|
3078 | ${symbolSvgBody(swuSym)}
|
3079 | </svg>`;
|
3080 | };
|
3081 |
|
3082 | const symbolCanvas = function (swuSym) {
|
3083 | const parsed = parse.symbol(swuSym);
|
3084 |
|
3085 | if (parsed.symbol) {
|
3086 | let size = symbolSize(parsed.symbol);
|
3087 |
|
3088 | if (size) {
|
3089 | const canvas = document.createElement('canvas');
|
3090 | const context = canvas.getContext('2d');
|
3091 | let styling = parse$3(parsed.style);
|
3092 | let line = 'black';
|
3093 |
|
3094 | if (styling.colorize) {
|
3095 | line = colorize(parsed.symbol);
|
3096 | } else if (styling.detail) {
|
3097 | line = styling.detail[0];
|
3098 | }
|
3099 |
|
3100 | let fill = styling.detail && styling.detail[1] || 'white';
|
3101 | let x1 = 500;
|
3102 | let x2 = x1 + size[0];
|
3103 | let y1 = 500;
|
3104 | let y2 = y1 + size[1];
|
3105 |
|
3106 | if (styling.padding) {
|
3107 | x1 -= styling.padding;
|
3108 | y1 -= styling.padding;
|
3109 | x2 += styling.padding;
|
3110 | y2 += styling.padding;
|
3111 | }
|
3112 |
|
3113 | let sizing = 1;
|
3114 |
|
3115 | if (styling.zoom != 'x') {
|
3116 | sizing = styling.zoom;
|
3117 | }
|
3118 |
|
3119 | let w = (x2 - x1) * sizing;
|
3120 | let h = (y2 - y1) * sizing;
|
3121 | canvas.width = w ? w : 1;
|
3122 | canvas.height = h ? h : 1;
|
3123 |
|
3124 | if (styling.background) {
|
3125 | context.rect(0, 0, w, h);
|
3126 | context.fillStyle = styling.background;
|
3127 | context.fill();
|
3128 | }
|
3129 |
|
3130 | context.font = 30 * sizing + "px 'SuttonSignWritingFill'";
|
3131 | context.fillStyle = fill;
|
3132 | context.fillText(symbolFill(parsed.symbol), (500 - x1) * sizing, (500 - y1) * sizing);
|
3133 | context.font = 30 * sizing + "px 'SuttonSignWritingLine'";
|
3134 | context.fillStyle = line;
|
3135 | context.fillText(symbolLine(parsed.symbol), (500 - x1) * sizing, (500 - y1) * sizing);
|
3136 | return canvas;
|
3137 | }
|
3138 | }
|
3139 | };
|
3140 | /**
|
3141 | * Function that creates a PNG data url from an SWU symbol character with an optional style string
|
3142 | * @function swu.symbolPng
|
3143 | * @param {string} swuSym - an SWU symbol character with optional style string
|
3144 | * @example
|
3145 | * swu.symbolPng('-CP10G_green_Z2')
|
3146 | *
|
3147 | * return 'data:image/png;base64,iVBORw...'
|
3148 | */
|
3149 |
|
3150 |
|
3151 | const symbolPng = swuSym => {
|
3152 | const canvas = symbolCanvas(swuSym);
|
3153 | const png = canvas.toDataURL("image/png");
|
3154 | canvas.remove();
|
3155 | return png;
|
3156 | };
|
3157 |
|
3158 | const blank = null;
|
3159 | /**
|
3160 | * Function that normalizes a symbol with a minimum coordinate for a center of 500,500
|
3161 | * @function swu.symbolNormalize
|
3162 | * @param {string} swuSym - an SWU symbol character with optional coordinate and style string
|
3163 | * @example
|
3164 | * swu.symbolNormalize('')
|
3165 | *
|
3166 | * return '𝣿𝣷'
|
3167 | */
|
3168 |
|
3169 | const symbolNormalize = swuSym => {
|
3170 | const parsed = parse.symbol(swuSym);
|
3171 |
|
3172 | if (parsed.symbol) {
|
3173 | let size = symbolSize(parsed.symbol);
|
3174 |
|
3175 | if (size) {
|
3176 | return `${parsed.symbol}${coord2swu([500 - parseInt((size[0] + 1) / 2), 500 - parseInt((size[1] + 1) / 2)])}${parsed.style || ''}`;
|
3177 | }
|
3178 | } else {
|
3179 | return blank;
|
3180 | }
|
3181 | };
|
3182 |
|
3183 | /**
|
3184 | * Function that creates an SVG image from an SWU sign with an optional style string
|
3185 | * @function swu.signSvgBody
|
3186 | * @param {string} swuSign - an SWU sign with optional style string
|
3187 | * @example
|
3188 | * swu.signSvgBody('M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475')
|
3189 | *
|
3190 | * return `<text font-size="0">M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475</text>
|
3191 | * <g transform="translate(483,510)">
|
3192 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3193 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3194 | * </g>
|
3195 | * <g transform="translate(501,466)">
|
3196 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3197 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3198 | * </g>
|
3199 | * <g transform="translate(510,500)">
|
3200 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3201 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3202 | * </g>
|
3203 | * <g transform="translate(476,475)">
|
3204 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3205 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3206 | * </g>`
|
3207 | */
|
3208 |
|
3209 | const signSvgBody = swuSign => {
|
3210 | let parsed = parse.sign(swuSign);
|
3211 | const blank = '';
|
3212 |
|
3213 | if (parsed.spatials) {
|
3214 | let styling = parse$3(parsed.style);
|
3215 |
|
3216 | if (styling.detailsym) {
|
3217 | styling.detailsym.forEach(sym => {
|
3218 | if (parsed.spatials[sym.index - 1]) {
|
3219 | parsed.spatials[sym.index - 1].detail = sym.detail;
|
3220 | }
|
3221 | });
|
3222 | }
|
3223 |
|
3224 | let x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
|
3225 | let y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
|
3226 | let x2 = parsed.max[0];
|
3227 | let y2 = parsed.max[1];
|
3228 | let background = '';
|
3229 |
|
3230 | if (styling.padding) {
|
3231 | x1 -= styling.padding;
|
3232 | y1 -= styling.padding;
|
3233 | x2 += styling.padding;
|
3234 | y2 += styling.padding;
|
3235 | }
|
3236 |
|
3237 | if (styling.background) {
|
3238 | background = `\n <rect x="${x1}" y="${y1}" width="${x2 - x1}" height="${y2 - y1}" style="fill:${styling.background};" />`;
|
3239 | }
|
3240 |
|
3241 | let svg = ` <text font-size="0">${swuSign}</text>${background}`;
|
3242 | const line = styling.detail && styling.detail[0];
|
3243 | const fill = styling.detail && styling.detail[1];
|
3244 | svg += '\n' + parsed.spatials.map(spatial => {
|
3245 | let svg = symbolText(spatial.symbol);
|
3246 | let symLine = line;
|
3247 |
|
3248 | if (spatial.detail) {
|
3249 | symLine = spatial.detail[0];
|
3250 | } else if (styling.colorize) {
|
3251 | symLine = colorize(spatial.symbol);
|
3252 | }
|
3253 |
|
3254 | if (symLine) {
|
3255 | svg = svg.replace(/class="sym-line" fill="black"/, `class="sym-line" fill="${symLine}"`);
|
3256 | }
|
3257 |
|
3258 | let symFill = fill;
|
3259 |
|
3260 | if (spatial.detail && spatial.detail[1]) {
|
3261 | symFill = spatial.detail[1];
|
3262 | }
|
3263 |
|
3264 | if (symFill) {
|
3265 | svg = svg.replace(/class="sym-fill" fill="white"/, `class="sym-fill" fill="${symFill}"`);
|
3266 | }
|
3267 |
|
3268 | return ` <g transform="translate(${spatial.coord[0]},${spatial.coord[1]})">
|
3269 | ${svg}
|
3270 | </g>`;
|
3271 | }).join('\n');
|
3272 | return svg;
|
3273 | }
|
3274 |
|
3275 | return blank;
|
3276 | };
|
3277 | /**
|
3278 | * Function that creates an SVG image from an SWU sign with an optional style string
|
3279 | * @function swu.signSvg
|
3280 | * @param {string} swuSign - an SWU sign with optional style string
|
3281 | * @example
|
3282 | * swu.signSvg('M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475')
|
3283 | *
|
3284 | * return `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="49" height="69" viewBox="476 466 49 69">
|
3285 | * <text font-size="0">M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475</text>
|
3286 | * <g transform="translate(483,510)">
|
3287 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3288 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3289 | * </g>
|
3290 | * <g transform="translate(501,466)">
|
3291 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3292 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3293 | * </g>
|
3294 | * <g transform="translate(510,500)">
|
3295 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3296 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3297 | * </g>
|
3298 | * <g transform="translate(476,475)">
|
3299 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3300 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3301 | * </g>
|
3302 | * </svg>`
|
3303 | */
|
3304 |
|
3305 |
|
3306 | const signSvg = swuSign => {
|
3307 | let parsed = parse.sign(swuSign);
|
3308 | const blank = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>';
|
3309 |
|
3310 | if (parsed.spatials) {
|
3311 | let styling = parse$3(parsed.style);
|
3312 | let x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
|
3313 | let y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
|
3314 | let x2 = parsed.max[0];
|
3315 | let y2 = parsed.max[1];
|
3316 | let classes = '';
|
3317 |
|
3318 | if (styling.classes) {
|
3319 | classes = ` class="${styling.classes}"`;
|
3320 | }
|
3321 |
|
3322 | let id = '';
|
3323 |
|
3324 | if (styling.id) {
|
3325 | id = ` id="${styling.id}"`;
|
3326 | }
|
3327 |
|
3328 | if (styling.padding) {
|
3329 | x1 -= styling.padding;
|
3330 | y1 -= styling.padding;
|
3331 | x2 += styling.padding;
|
3332 | y2 += styling.padding;
|
3333 | }
|
3334 |
|
3335 | let sizing = '';
|
3336 |
|
3337 | if (styling.zoom != 'x') {
|
3338 | sizing = ` width="${(x2 - x1) * (styling.zoom ? styling.zoom : 1)}" height="${(y2 - y1) * (styling.zoom ? styling.zoom : 1)}"`;
|
3339 | }
|
3340 |
|
3341 | let svg = `<svg${classes}${id} version="1.1" xmlns="http://www.w3.org/2000/svg"${sizing} viewBox="${x1} ${y1} ${x2 - x1} ${y2 - y1}">
|
3342 | `;
|
3343 | svg += signSvgBody(swuSign);
|
3344 | svg += '\n</svg>';
|
3345 | return svg;
|
3346 | }
|
3347 |
|
3348 | return blank;
|
3349 | };
|
3350 |
|
3351 | const signCanvas = function (swuSign) {
|
3352 | const parsed = parse.sign(swuSign);
|
3353 |
|
3354 | if (parsed.spatials) {
|
3355 | const canvas = document.createElement('canvas');
|
3356 | const context = canvas.getContext('2d');
|
3357 | let styling = parse$3(parsed.style);
|
3358 |
|
3359 | if (styling.detailsym) {
|
3360 | styling.detailsym.forEach(sym => {
|
3361 | if (parsed.spatials[sym.index - 1]) {
|
3362 | parsed.spatials[sym.index - 1].detail = sym.detail;
|
3363 | }
|
3364 | });
|
3365 | }
|
3366 |
|
3367 | let x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
|
3368 | let y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
|
3369 | let x2 = parsed.max[0];
|
3370 | let y2 = parsed.max[1];
|
3371 |
|
3372 | if (styling.padding) {
|
3373 | x1 -= styling.padding;
|
3374 | y1 -= styling.padding;
|
3375 | x2 += styling.padding;
|
3376 | y2 += styling.padding;
|
3377 | }
|
3378 |
|
3379 | let sizing = 1;
|
3380 |
|
3381 | if (styling.zoom != 'x') {
|
3382 | sizing = styling.zoom;
|
3383 | }
|
3384 |
|
3385 | let w = (x2 - x1) * sizing;
|
3386 | let h = (y2 - y1) * sizing;
|
3387 | canvas.width = w ? w : 1;
|
3388 | canvas.height = h ? h : 1;
|
3389 |
|
3390 | if (styling.background) {
|
3391 | context.rect(0, 0, w, h);
|
3392 | context.fillStyle = styling.background;
|
3393 | context.fill();
|
3394 | }
|
3395 |
|
3396 | const line = styling.detail && styling.detail[0] || "black";
|
3397 | const fill = styling.detail && styling.detail[1] || "white";
|
3398 | parsed.spatials.forEach(spatial => {
|
3399 | let symLine = line;
|
3400 |
|
3401 | if (spatial.detail) {
|
3402 | symLine = spatial.detail[0];
|
3403 | } else if (styling.colorize) {
|
3404 | symLine = colorize(spatial.symbol);
|
3405 | }
|
3406 |
|
3407 | let symFill = fill;
|
3408 |
|
3409 | if (spatial.detail && spatial.detail[1]) {
|
3410 | symFill = spatial.detail[1];
|
3411 | }
|
3412 |
|
3413 | context.font = 30 * sizing + "px 'SuttonSignWritingFill'";
|
3414 | context.fillStyle = symFill;
|
3415 | context.fillText(symbolFill(spatial.symbol), (spatial.coord[0] - x1) * sizing, (spatial.coord[1] - y1) * sizing);
|
3416 | context.font = 30 * sizing + "px 'SuttonSignWritingLine'";
|
3417 | context.fillStyle = symLine;
|
3418 | context.fillText(symbolLine(spatial.symbol), (spatial.coord[0] - x1) * sizing, (spatial.coord[1] - y1) * sizing);
|
3419 | });
|
3420 | return canvas;
|
3421 | }
|
3422 | };
|
3423 | /**
|
3424 | * Function that creates a PNG data url from an SWU sign with an optional style string
|
3425 | * @function swu.signPng
|
3426 | * @param {string} swuSign - an SWU sign with optional style string
|
3427 | * @example
|
3428 | * swu.signPng('𝠀𝠃𝤟𝤩𝣵𝤐𝤇𝣤𝤐𝤆𝣮𝣭')
|
3429 | *
|
3430 | * return 'data:image/png;base64,iVBORw...'
|
3431 | */
|
3432 |
|
3433 |
|
3434 | const signPng = swuSign => {
|
3435 | const canvas = signCanvas(swuSign);
|
3436 | const png = canvas.toDataURL("image/png");
|
3437 | canvas.remove();
|
3438 | return png;
|
3439 | };
|
3440 |
|
3441 | /**
|
3442 | * Function that normalizes an SWU sign for a center of 500,500
|
3443 | * @function swu.signNormalize
|
3444 | * @param {string} swuSign - an SWU sign with optional style string
|
3445 | * @example
|
3446 | * swu.signNormalize('𝠀𝠃𝤟𝤩𝣵𝤐𝤇𝣤𝤐𝤆𝣮𝣭')
|
3447 | *
|
3448 | * return '𝠀𝠃𝤟𝤩𝣵𝤐𝤇𝣤𝤐𝤆𝣮𝣭'
|
3449 | */
|
3450 |
|
3451 | const signNormalize = swuSign => {
|
3452 | const parsed = parse.sign(swuSign);
|
3453 |
|
3454 | if (parsed.spatials) {
|
3455 | const symbolsizes = parsed.spatials.reduce((output, spatial) => {
|
3456 | const size = symbolSize(spatial.symbol);
|
3457 | output[spatial.symbol] = {
|
3458 | width: size[0],
|
3459 | height: size[1]
|
3460 | };
|
3461 | return output;
|
3462 | }, {});
|
3463 |
|
3464 | const bbox = symbols => {
|
3465 | const x1 = Math.min(...symbols.map(spatial => spatial.coord[0]));
|
3466 | const y1 = Math.min(...symbols.map(spatial => spatial.coord[1]));
|
3467 | const x2 = Math.max(...symbols.map(spatial => spatial.coord[0] + parseInt(symbolsizes[spatial.symbol].width)));
|
3468 | const y2 = Math.max(...symbols.map(spatial => spatial.coord[1] + parseInt(symbolsizes[spatial.symbol].height)));
|
3469 | return {
|
3470 | x1: x1,
|
3471 | y1: y1,
|
3472 | x2: x2,
|
3473 | y2: y2
|
3474 | };
|
3475 | };
|
3476 |
|
3477 | const hrange = ranges['hcenter'];
|
3478 | const hsyms = parsed.spatials.filter(spatial => {
|
3479 | const dec = parseInt(spatial.symbol.slice(1, 4), 16);
|
3480 | return hrange[0] <= dec && hrange[1] >= dec;
|
3481 | });
|
3482 | const vrange = ranges['vcenter'];
|
3483 | const vsyms = parsed.spatials.filter(spatial => {
|
3484 | const dec = parseInt(spatial.symbol.slice(1, 4), 16);
|
3485 | return vrange[0] <= dec && vrange[1] >= dec;
|
3486 | });
|
3487 | let abox = bbox(parsed.spatials);
|
3488 | let max = [abox.x2, abox.y2];
|
3489 |
|
3490 | if (hsyms.length) {
|
3491 | const hbox = bbox(hsyms);
|
3492 | abox.x1 = hbox.x1;
|
3493 | abox.x2 = hbox.x2;
|
3494 | }
|
3495 |
|
3496 | if (vsyms.length) {
|
3497 | const vbox = bbox(vsyms);
|
3498 | abox.y1 = vbox.y1;
|
3499 | abox.y2 = vbox.y2;
|
3500 | }
|
3501 |
|
3502 | const offset = [parseInt((abox.x2 + abox.x1) / 2) - 500, parseInt((abox.y2 + abox.y1) / 2) - 500];
|
3503 | const swuout = (parsed.sequence ? '𝠀' + parsed.sequence.join('') : '') + parsed.box + coord2swu([max[0] - offset[0], max[1] - offset[1]]) + parsed.spatials.map(spatial => spatial.symbol + coord2swu([spatial.coord[0] - offset[0], spatial.coord[1] - offset[1]])).join('') + (parsed.style || '');
|
3504 | return swuout;
|
3505 | }
|
3506 | };
|
3507 |
|
3508 | /**
|
3509 | * Function that creates an SVG image for a column of SWU
|
3510 | * @function swu.columnSvg
|
3511 | * @param {ColumnData} swuColumn - an array of objects with information about FSW signs and punctuation
|
3512 | * @param {ColumnOptions} options - an object of column options
|
3513 | * @returns {string} column svg
|
3514 | * @example
|
3515 | * swu.columnSvg([
|
3516 | * {
|
3517 | * "x": 56,
|
3518 | * "y": 20,
|
3519 | * "minX": 481,
|
3520 | * "minY": 471,
|
3521 | * "width": 37,
|
3522 | * "height": 58,
|
3523 | * "lane": 0,
|
3524 | * "padding": 0,
|
3525 | * "segment": "sign",
|
3526 | * "text": "𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻",
|
3527 | * "zoom": 1
|
3528 | * },
|
3529 | * {
|
3530 | * "x": 57,
|
3531 | * "y": 118,
|
3532 | * "minX": 482,
|
3533 | * "minY": 468,
|
3534 | * "width": 36,
|
3535 | * "height": 65,
|
3536 | * "lane": 0,
|
3537 | * "padding": 0,
|
3538 | * "segment": "sign",
|
3539 | * "text": "𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦",
|
3540 | * "zoom": 1
|
3541 | * },
|
3542 | * {
|
3543 | * "x": 39,
|
3544 | * "y": 203,
|
3545 | * "minX": 464,
|
3546 | * "minY": 496,
|
3547 | * "width": 72,
|
3548 | * "height": 8,
|
3549 | * "lane": 0,
|
3550 | * "padding": 0,
|
3551 | * "segment": "symbol",
|
3552 | * "text": "𝣢𝤂",
|
3553 | * "zoom": 1
|
3554 | * }
|
3555 | * ],
|
3556 | * {
|
3557 | * "height": 250,
|
3558 | * "width": 150,
|
3559 | * })
|
3560 | *
|
3561 | * return `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="150" height="250" viewBox="0 0 150 250">
|
3562 | * <g transform="translate(56,20) scale(1) translate(-481,-471) ">
|
3563 | * <text font-size="0">𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻-D_black,white_Z1</text>
|
3564 | * <g transform="translate(481,471)">
|
3565 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3566 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3567 | * </g>
|
3568 | * <g transform="translate(503,489)">
|
3569 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3570 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3571 | * </g>
|
3572 | * </g>
|
3573 | * <g transform="translate(57,118) scale(1) translate(-482,-468) ">
|
3574 | * <text font-size="0">𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦-D_black,white_Z1</text>
|
3575 | * <g transform="translate(489,515)">
|
3576 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3577 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3578 | * </g>
|
3579 | * <g transform="translate(482,490)">
|
3580 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3581 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3582 | * </g>
|
3583 | * <g transform="translate(508,496)">
|
3584 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3585 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3586 | * </g>
|
3587 | * <g transform="translate(500,468)">
|
3588 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3589 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3590 | * </g>
|
3591 | * </g>
|
3592 | * <g transform="translate(39,203) scale(1) translate(-464,-496) ">
|
3593 | * <text font-size="0">𝣢𝤂-D_black,white_Z1</text>
|
3594 | * <g transform="translate(464,496)">
|
3595 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3596 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3597 | * </g>
|
3598 | * </g>
|
3599 | * </svg>`
|
3600 | */
|
3601 |
|
3602 | const columnSvg = (swuColumn, options) => {
|
3603 |
|
3604 | if (typeof options !== 'object') options = {};
|
3605 | const values = Object.assign(columnDefaults, options);
|
3606 | let x1 = 0;
|
3607 | let y1 = 0;
|
3608 | let x2 = values.width;
|
3609 | let y2 = values.height;
|
3610 | let background = '';
|
3611 |
|
3612 | if (values.background) {
|
3613 | background = `\n <rect x="${x1}" y="${y1}" width="${x2 - x1}" height="${y2 - y1}" style="fill:${values.background};" />`;
|
3614 | }
|
3615 |
|
3616 | let sizing = ` width="${values.width}" height="${values.height}"`;
|
3617 | let svg = `<svg version="1.1" xmlns="http://www.w3.org/2000/svg"${sizing} viewBox="${x1} ${y1} ${x2 - x1} ${y2 - y1}">
|
3618 | <text font-size="0">${x1}</text>${background}`;
|
3619 | svg += swuColumn.map(item => {
|
3620 | const dash = item.text.indexOf('-');
|
3621 |
|
3622 | if (dash > 0) {
|
3623 | const itemStyle = item.text.substring(dash);
|
3624 | const newStyle = { ...values.style,
|
3625 | ...parse$3(itemStyle)
|
3626 | };
|
3627 | item.text = item.text.replace(itemStyle, compose(newStyle));
|
3628 | } else {
|
3629 | item.text += compose(values.style);
|
3630 | }
|
3631 |
|
3632 | item.zoom = item.zoom * values.style.zoom;
|
3633 | return '<g transform="translate(' + item.x + ',' + item.y + ') scale(' + item.zoom + ') translate(' + -item.minX + ',' + -item.minY + ') ">' + (item.segment == "sign" ? signSvgBody(item.text) : symbolSvgBody(item.text)) + '</g>';
|
3634 | }).join('\n');
|
3635 | svg += '\n</svg>';
|
3636 | return svg;
|
3637 | };
|
3638 |
|
3639 | /**
|
3640 | * Function that creates an array of SVG column images for an SWU text
|
3641 | * @function swu.columnsSvg
|
3642 | * @param {string} swuText - a text of SWU signs and punctuation
|
3643 | * @param {ColumnOptions} options - an object of column options
|
3644 | * @returns {string[]} array of SVG columns
|
3645 | * @example
|
3646 | * swu.columnsSvg('𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻 𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦 𝣢𝤂',{
|
3647 | * "height": 250,
|
3648 | * "width": 150,
|
3649 | * })
|
3650 | *
|
3651 | * return [`<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="150" height="250" viewBox="0 0 150 250">
|
3652 | * <g transform="translate(56,20) scale(1) translate(-481,-471) ">
|
3653 | * <text font-size="0">𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻-D_black,white_Z1</text>
|
3654 | * <g transform="translate(481,471)">
|
3655 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3656 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3657 | * </g>
|
3658 | * <g transform="translate(503,489)">
|
3659 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3660 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3661 | * </g>
|
3662 | * </g>
|
3663 | * <g transform="translate(57,118) scale(1) translate(-482,-468) ">
|
3664 | * <text font-size="0">𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦-D_black,white_Z1</text>
|
3665 | * <g transform="translate(489,515)">
|
3666 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3667 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3668 | * </g>
|
3669 | * <g transform="translate(482,490)">
|
3670 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3671 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3672 | * </g>
|
3673 | * <g transform="translate(508,496)">
|
3674 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3675 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3676 | * </g>
|
3677 | * <g transform="translate(500,468)">
|
3678 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3679 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3680 | * </g>
|
3681 | * </g>
|
3682 | * <g transform="translate(39,203) scale(1) translate(-464,-496) ">
|
3683 | * <text font-size="0">𝣢𝤂-D_black,white_Z1</text>
|
3684 | * <g transform="translate(464,496)">
|
3685 | * <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;"></text>
|
3686 | * <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;"></text>
|
3687 | * </g>
|
3688 | * </g>
|
3689 | * </svg>`]
|
3690 | */
|
3691 |
|
3692 | const columnsSvg = function (swuText, options) {
|
3693 | if (typeof options !== 'object') options = {};
|
3694 | let values = columns(swuText, options);
|
3695 | let cols = values.columns.map((col, i) => {
|
3696 | return columnSvg(col, { ...values.options,
|
3697 | ...{
|
3698 | width: values.widths[i]
|
3699 | }
|
3700 | });
|
3701 | });
|
3702 | return cols;
|
3703 | };
|
3704 |
|
3705 | const columnCanvas = function (swuColumn, options) {
|
3706 | if (typeof options !== 'object') options = {};
|
3707 | const values = Object.assign(columnDefaults, options);
|
3708 | const canvas = document.createElement('canvas');
|
3709 | canvas.width = values.width;
|
3710 | canvas.height = values.height;
|
3711 | const context = canvas.getContext('2d');
|
3712 |
|
3713 | if (values.background) {
|
3714 | context.rect(0, 0, values.width, values.height);
|
3715 | context.fillStyle = values.background;
|
3716 | context.fill();
|
3717 | }
|
3718 |
|
3719 | swuColumn.map(item => {
|
3720 | const dash = item.text.indexOf('-');
|
3721 |
|
3722 | if (dash > 0) {
|
3723 | const itemStyle = item.text.substring(dash);
|
3724 | const newStyle = { ...values.style,
|
3725 | ...parse$3(itemStyle)
|
3726 | };
|
3727 | item.text = item.text.replace(itemStyle, compose(newStyle));
|
3728 | } else {
|
3729 | item.text += compose(values.style);
|
3730 | }
|
3731 |
|
3732 | item.zoom = item.zoom * values.style.zoom;
|
3733 | let parsed = {};
|
3734 |
|
3735 | if (item.segment == "sign") {
|
3736 | parsed = parse.sign(item.text);
|
3737 | } else {
|
3738 | let sym = parse.symbol(item.text);
|
3739 | parsed.style = sym.style;
|
3740 | parsed.spatials = [sym];
|
3741 | }
|
3742 |
|
3743 | let styling = parse$3(parsed.style);
|
3744 |
|
3745 | if (styling.background) {
|
3746 | context.fillStyle = styling.background;
|
3747 | context.fillRect(item.x - styling.padding * item.zoom, item.y - styling.padding * item.zoom, (item.width + styling.padding * 2) * item.zoom, (item.height + styling.padding * 2) * item.zoom);
|
3748 | }
|
3749 |
|
3750 | if (styling.detailsym) {
|
3751 | styling.detailsym.forEach(sym => {
|
3752 | if (parsed.spatials[sym.index - 1]) {
|
3753 | parsed.spatials[sym.index - 1].detail = sym.detail;
|
3754 | }
|
3755 | });
|
3756 | }
|
3757 |
|
3758 | const line = styling.detail && styling.detail[0] || "black";
|
3759 | const fill = styling.detail && styling.detail[1] || "white";
|
3760 | parsed.spatials.forEach(spatial => {
|
3761 | let symLine = line;
|
3762 |
|
3763 | if (spatial.detail) {
|
3764 | symLine = spatial.detail[0];
|
3765 | } else if (styling.colorize) {
|
3766 | symLine = colorize(spatial.symbol);
|
3767 | }
|
3768 |
|
3769 | let symFill = fill;
|
3770 |
|
3771 | if (spatial.detail && spatial.detail[1]) {
|
3772 | symFill = spatial.detail[1];
|
3773 | }
|
3774 |
|
3775 | context.font = 30 * item.zoom + "px 'SuttonSignWritingFill'";
|
3776 | context.fillStyle = symFill;
|
3777 | context.fillText(symbolFill(spatial.symbol), item.x + (spatial.coord[0] - item.minX) * item.zoom, item.y + (spatial.coord[1] - item.minY) * item.zoom);
|
3778 | context.font = 30 * item.zoom + "px 'SuttonSignWritingLine'";
|
3779 | context.fillStyle = symLine;
|
3780 | context.fillText(symbolLine(spatial.symbol), item.x + (spatial.coord[0] - item.minX) * item.zoom, item.y + (spatial.coord[1] - item.minY) * item.zoom);
|
3781 | });
|
3782 | });
|
3783 | return canvas;
|
3784 | };
|
3785 | /**
|
3786 | * Function that creates a PNG data url for a column of SWU
|
3787 | * @function swu.columnPng
|
3788 | * @param {ColumnData} swuColumn - an array of SWU signs and punctuation with coordinates
|
3789 | * @param {ColumnOptions} options - an object of column options
|
3790 | * @returns {string} column png data url
|
3791 | * @example
|
3792 | * swu.columnPng([
|
3793 | * {
|
3794 | * "x": 56,
|
3795 | * "y": 20,
|
3796 | * "minX": 481,
|
3797 | * "minY": 471,
|
3798 | * "width": 37,
|
3799 | * "height": 58,
|
3800 | * "lane": 0,
|
3801 | * "padding": 0,
|
3802 | * "segment": "sign",
|
3803 | * "text": "𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻",
|
3804 | * "zoom": 1
|
3805 | * },
|
3806 | * {
|
3807 | * "x": 57,
|
3808 | * "y": 118,
|
3809 | * "minX": 482,
|
3810 | * "minY": 468,
|
3811 | * "width": 36,
|
3812 | * "height": 65,
|
3813 | * "lane": 0,
|
3814 | * "padding": 0,
|
3815 | * "segment": "sign",
|
3816 | * "text": "𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦",
|
3817 | * "zoom": 1
|
3818 | * },
|
3819 | * {
|
3820 | * "x": 39,
|
3821 | * "y": 203,
|
3822 | * "minX": 464,
|
3823 | * "minY": 496,
|
3824 | * "width": 72,
|
3825 | * "height": 8,
|
3826 | * "lane": 0,
|
3827 | * "padding": 0,
|
3828 | * "segment": "symbol",
|
3829 | * "text": "𝣢𝤂",
|
3830 | * "zoom": 1
|
3831 | * }
|
3832 | * ],
|
3833 | * {
|
3834 | * "height": 250,
|
3835 | * "width": 150,
|
3836 | * })
|
3837 | *
|
3838 | * return 'data:image/png;base64,iVBORw...'
|
3839 | */
|
3840 |
|
3841 |
|
3842 | const columnPng = (swuColumn, options) => {
|
3843 | const canvas = columnCanvas(swuColumn, options);
|
3844 | const png = canvas.toDataURL("image/png");
|
3845 | canvas.remove();
|
3846 | return png;
|
3847 | };
|
3848 |
|
3849 | /**
|
3850 | * Function that creates an SVG image for a column of SWU
|
3851 | * @function swu.columnsPng
|
3852 | * @param {string} swuText - an array of SWU signs and punctuation with coordinates
|
3853 | * @param {ColumnOptions} options - an object of column options
|
3854 | * @returns {string[]} array of PNG data urls
|
3855 | * @example
|
3856 | * swu.columnsPng('𝠀𝠃𝤘𝤣𝣳𝣩𝤉𝣻 𝠀𝠃𝤘𝤧𝣻𝤕𝣴𝣼𝤎𝤂𝤆𝣦 𝣢𝤂',{
|
3857 | * "height": 250,
|
3858 | * "width": 150,
|
3859 | * })
|
3860 | *
|
3861 | * return ['data:image/png;base64,iVBORw...']
|
3862 | */
|
3863 |
|
3864 | const columnsPng = function (swuText, options) {
|
3865 | if (typeof options !== 'object') options = {};
|
3866 | let values = columns(swuText, options);
|
3867 | let cols = values.columns.map((col, i) => {
|
3868 | return columnPng(col, { ...values.options,
|
3869 | ...{
|
3870 | width: values.widths[i]
|
3871 | }
|
3872 | });
|
3873 | });
|
3874 | return cols;
|
3875 | };
|
3876 |
|
3877 | /** The swu module contains functions for handling SignWriting in Unicode (SWU) characters.
|
3878 | * [SWU characters definition](https://tools.ietf.org/id/draft-slevinski-formal-signwriting-09.html#name-signwriting-in-unicode-swu)
|
3879 | * @module swu
|
3880 | */
|
3881 |
|
3882 | var index = /*#__PURE__*/Object.freeze({
|
3883 | __proto__: null,
|
3884 | symbolSize: symbolSize,
|
3885 | symbolLine: symbolLine,
|
3886 | symbolFill: symbolFill,
|
3887 | symbolText: symbolText,
|
3888 | symbolSvgBody: symbolSvgBody,
|
3889 | symbolSvg: symbolSvg,
|
3890 | symbolPng: symbolPng,
|
3891 | symbolNormalize: symbolNormalize,
|
3892 | signSvgBody: signSvgBody,
|
3893 | signSvg: signSvg,
|
3894 | signPng: signPng,
|
3895 | signNormalize: signNormalize,
|
3896 | columnSvg: columnSvg,
|
3897 | columnsSvg: columnsSvg,
|
3898 | columnPng: columnPng,
|
3899 | columnsPng: columnsPng
|
3900 | });
|
3901 |
|
3902 | exports.font = index$2;
|
3903 | exports.fsw = index$1;
|
3904 | exports.swu = index;
|
3905 |
|
3906 | Object.defineProperty(exports, '__esModule', { value: true });
|
3907 |
|
3908 | }));
|
3909 |
|
3910 | /* support ongoing development on https://patreon.com/signwriting */
|