UNPKG

7.86 kBJavaScriptView Raw
1/**
2* Sutton SignWriting Core Module v1.2.0 (https://github.com/sutton-signwriting/core)
3* Author: Steve Slevinski (https://SteveSlevinski.me)
4* style.mjs is released under the MIT License.
5*/
6
7/**
8 * Object of regular expressions for style strings
9 *
10 * { colorize, colorhex, colorname, padding, zoom, zoomsym, classbase, id, colorbase, color, colors, background, detail, detailsym, classes, full }
11 * @alias style.re
12 * @type {object}
13 */
14let re = {
15 'colorize': 'C',
16 'colorhex': '(?:[0-9a-fA-F]{3}){1,2}',
17 'colorname': '[a-zA-Z]+',
18 'padding': 'P[0-9]{2}',
19 'zoom': 'Z(?:[0-9]+(?:\\.[0-9]+)?|x)',
20 'zoomsym': 'Z[0-9]{2},[0-9]+(?:\\.[0-9]+)?(?:,[0-9]{3}x[0-9]{3})?',
21 'classbase': '-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}',
22 'id': '[a-zA-Z][_a-zA-Z0-9-]{0,100}'
23};
24re.colorbase = `(?:${re.colorhex}|${re.colorname})`;
25re.color = `_${re.colorbase}_`;
26re.colors = `_${re.colorbase}(?:,${re.colorbase})?_`;
27re.background = `G${re.color}`;
28re.detail = `D${re.colors}`;
29re.detailsym = `D[0-9]{2}${re.colors}`;
30re.classes = `${re.classbase}(?: ${re.classbase})*`;
31re.full = `-(${re.colorize})?(${re.padding})?(${re.background})?(${re.detail})?(${re.zoom})?(?:-((?:${re.detailsym})*)((?:${re.zoomsym})*))?(?:-(${re.classes})?!(?:(${re.id})!)?)?`;
32
33const prefixColor = color => {
34 const regex = new RegExp(`^${re.colorhex}$`);
35 return (regex.test(color) ? '#' : '') + color;
36};
37/**
38 * Function to parse style string to object
39 * @function style.parse
40 * @param {string} styleString - a style string
41 * @returns {object} elements of style string
42 * @example
43 * style.parse('-CP10G_blue_D_red,Cyan_')
44 *
45 * return {
46 * 'colorize': true,
47 * 'padding': 10,
48 * 'background': 'blue',
49 * 'detail': ['red', 'Cyan']
50 * }
51 */
52
53
54const parse = styleString => {
55 const regex = `^${re.full}`;
56 const m = (typeof styleString === 'string' ? styleString.match(new RegExp(regex)) : []) || [];
57 return {
58 'colorize': !m[1] ? undefined : !!m[1],
59 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
60 'background': !m[3] ? undefined : prefixColor(m[3].slice(2, -1)),
61 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor),
62 'zoom': !m[5] ? undefined : m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1)),
63 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re.detailsym, 'g')).map(val => {
64 const parts = val.split('_');
65 const detail = parts[1].split(',').map(prefixColor);
66 return {
67 'index': parseInt(parts[0].slice(1)),
68 'detail': detail
69 };
70 }),
71 'zoomsym': !m[7] ? undefined : m[7].match(new RegExp(re.zoomsym, 'g')).map(val => {
72 const parts = val.split(',');
73 return {
74 'index': parseInt(parts[0].slice(1)),
75 'zoom': parseFloat(parts[1]),
76 'offset': !parts[2] ? undefined : parts[2].split('x').map(val => parseInt(val) - 500)
77 };
78 }),
79 'classes': !m[8] ? undefined : m[8],
80 'id': !m[9] ? undefined : m[9]
81 };
82};
83
84/**
85 * Function to compose style string from object
86 * @function style.compose
87 * @param {object} styleObject - an object of style options
88 * @param {boolean} styleObject.colorize - boolean to use standardized colors for symbol groups
89 * @param {number} styleObject.padding - integer value for padding around symbol or sign
90 * @param {string} styleObject.background - css name or hex color for background
91 * @param {string[]} styleObject.detail - css name or hex color for line and optional fill
92 * @param {number} styleObject.zoom - decimal value for zoom level
93 * @param {{index:number,detail:string[]}[]} styleObject.detailsym - array of symbol indexes and detail color array
94 * @param {{index:number,zoom:number,offset:number[]}[]} styleObject.zoomsym - array of symbol indexes and zoom levels with optional x,y offset
95 * @param {string} styleObject.classes - list of class names separated with spaces used for SVG
96 * @param {string} styleObject.id - id name used for SVG
97 * @returns {string} style string
98 * @example
99 * style.compose({
100 * 'colorize': true,
101 * 'padding': 10,
102 * 'background': 'blue',
103 * 'detail': ['red', 'Cyan'],
104 * 'zoom': 1.1,
105 * 'detailsym': [
106 * {
107 * 'index': 1,
108 * 'detail': ['#ff00ff']
109 * },
110 * {
111 * 'index': 2,
112 * 'detail': ['yellow', 'green']
113 * }
114 * ],
115 * 'zoomsym': [
116 * {
117 * 'index': 1,
118 * 'zoom': 10,
119 * 'offset': [0, 0]
120 * },
121 * {
122 * 'index': 2,
123 * 'zoom': 5.5
124 * }
125 * ],
126 * 'classes': 'primary blinking',
127 * 'id': 'cursor'
128 * })
129 *
130 * return '-CP10G_blue_D_red,Cyan_Z1.1-D01_ff00ff_D02_yellow,green_Z01,10,500x500Z02,5.5-primary blinking!cursor!'
131 */
132
133const compose = styleObject => {
134 if (typeof styleObject !== 'object' || styleObject === null) return undefined; // three sections
135
136 let style1 = '-';
137 style1 += !styleObject.colorize ? '' : 'C';
138 const padding = parseInt(styleObject.padding);
139 style1 += !padding || padding <= 0 || padding > 99 ? '' : 'P' + (padding > 9 ? padding : '0' + padding);
140 const background = !styleObject.background || !(typeof styleObject.background === 'string') ? undefined : styleObject.background.match(re.colorbase)[0];
141 style1 += !background ? '' : 'G_' + background + '_';
142 const detail1 = !styleObject.detail || !styleObject.detail[0] || !(typeof styleObject.detail[0] === 'string') ? undefined : styleObject.detail[0].match(re.colorbase)[0];
143 const detail2 = !styleObject.detail || !styleObject.detail[1] || !(typeof styleObject.detail[1] === 'string') ? undefined : styleObject.detail[1].match(re.colorbase)[0];
144
145 if (detail1) {
146 style1 += 'D_' + detail1;
147
148 if (detail2) {
149 style1 += ',' + detail2;
150 }
151
152 style1 += '_';
153 }
154
155 const zoom = styleObject.zoom === 'x' ? 'x' : parseFloat(styleObject.zoom);
156 style1 += !zoom || zoom <= 0 ? '' : 'Z' + zoom;
157 let style2 = '';
158 const detailsym = !styleObject.detailsym || !Array.isArray(styleObject.detailsym) ? [] : styleObject.detailsym.map(styleObject => {
159 const index = parseInt(styleObject.index);
160 if (!index || index <= 0 || index > 99) return '';
161 let style = 'D' + (index > 9 ? index : '0' + index);
162 const detail1 = !styleObject.detail || !styleObject.detail[0] ? undefined : styleObject.detail[0].match(re.colorbase)[0];
163 const detail2 = !styleObject.detail || !styleObject.detail[1] ? undefined : styleObject.detail[1].match(re.colorbase)[0];
164
165 if (detail1) {
166 style += '_' + detail1;
167
168 if (detail2) {
169 style += ',' + detail2;
170 }
171
172 style += '_';
173 }
174
175 return style;
176 });
177 style2 += detailsym.join('');
178 const zoomsym = !styleObject.zoomsym || !Array.isArray(styleObject.zoomsym) ? [] : styleObject.zoomsym.map(styleObject => {
179 const index = parseInt(styleObject.index);
180 if (!index || index <= 0 || index > 99) return '';
181 let style = 'Z' + (index > 9 ? index : '0' + index);
182 const zoom = parseFloat(styleObject.zoom);
183 style += !zoom || zoom <= 0 ? '' : ',' + zoom;
184
185 if (styleObject.offset && 0 in styleObject.offset && 1 in styleObject.offset) {
186 const x = parseInt(styleObject.offset[0]) + 500;
187 const y = parseInt(styleObject.offset[1]) + 500;
188
189 if (x >= 250 && x < 750 && y >= 250 && y < 750) {
190 style += ',' + x + 'x' + y;
191 }
192 }
193
194 return style;
195 });
196 style2 += zoomsym.join('');
197 let style3 = '';
198 const classes = !styleObject.classes || !(typeof styleObject.classes === 'string') ? undefined : styleObject.classes.match(re.classes)[0];
199 style3 += !classes ? '' : classes;
200 const id = !styleObject.id || !(typeof styleObject.id === 'string') ? undefined : styleObject.id.match(re.id)[0];
201 style3 += classes || id ? '!' : '';
202 style3 += !id ? '' : id + '!';
203 return style1 + (style2 || style3 ? '-' + style2 : '') + (style3 ? '-' + style3 : '');
204};
205
206export { compose, parse, re };
207
208/* support ongoing development on https://patreon.com/signwriting */