UNPKG

5.81 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.Characters = void 0;
7
8/**
9 * Base class for defining characters and their elements.
10 */
11class Characters extends Object {
12 /**
13 * Round values to specified decimal places.
14 */
15
16 /**
17 * The stroke size of the lines.
18 */
19
20 /**
21 * Character and their elements encode bits.
22 */
23
24 /**
25 * Character elements.
26 */
27
28 /**
29 * Margin values.
30 */
31
32 /**
33 * Characters constructor.
34 */
35 constructor() {
36 super();
37 this.precision = 1;
38 this.stroke = void 0;
39 this._characters = {
40 /* eslint-disable quote-props, key-spacing */
41 ' ': 0b00000000000000000000000000000000000000000,
42 'A': 0b00000000000000000000000000000000000000101,
43 'B': 0b00000000000000000000000000000000000001101,
44 'C': 0b00000000000000000000000000000000000010001,
45 'D': 0b00000000000000000000000000000001000010001,
46 'E': 0b00000000000000000000000000110000000000001,
47 'F': 0b00000000000000000000000000010000000000001,
48 'G': 0b00000000000000000000000100000001000000001,
49 'H': 0b00000000000000000000000000001100000001101,
50 'I': 0b00000000000000000000000000000001000000001,
51 'J': 0b00000000000000000000000000000001100000001,
52 'K': 0b00000000000000000001101000000000000000001,
53 'L': 0b00000000000000000000000000000001010000001,
54 'M': 0b00000000000000000000000000001100000000001,
55 'N': 0b00000000000000000000000001000000000000001,
56 'O': 0b00000000000000000000000000000000000000001,
57 'P': 0b00000000000000000000000000000001001000001,
58 'Q': 0b00000000000000000000000000000000010000001,
59 'R': 0b00000000000000000000000100000001001000001,
60 'S': 0b00000000000000000000000000000000101000001,
61 'T': 0b00000000000000000000010000000010000000001,
62 'U': 0b00000000000000000000000000000000000001001,
63 'V': 0b00000000000000000000000011000000000001001,
64 'W': 0b00000000000000000000000011000000000110001,
65 'X': 0b00000000000000000000000011000000000000001,
66 'Y': 0b00000000000000000000100001000000000000001,
67 'Z': 0b00000000000000000000000010000000000000001,
68 '0': 0b00000000000000000000000000000000000000011,
69 '1': 0b00000000000000000100000000000000000000011,
70 '2': 0b00000000000000001100000000000000000000011,
71 '3': 0b00000000000011000100000000000000000000011,
72 '4': 0b00000000000000111100000000000000000000011,
73 '5': 0b00000000111100000100000000000000000000011,
74 '6': 0b00000000000000000010000000000000000000011,
75 '7': 0b00000001000000000010000000000000000000011,
76 '8': 0b00000011000000000010000000000000000000011,
77 '9': 0b00001101000000000010000000000000000000011,
78 '.': 0b00000000000000000000000000000000000000010,
79 '-': 0b00110000000000000000000000000000000000000,
80 '\u00C6': 0b11000000000000000000000000000011000000001,
81 '\u00D8': 0b00000000000000000000000000100001000000001,
82 '\u00C5': 0b00000000000000000000000000100000000001001
83 /* eslint-enable quote-props, key-spacing */
84
85 };
86 this._elements = void 0;
87 this.margin = {
88 left: 0,
89 right: 0,
90 top: 0,
91 bottom: 0
92 };
93 }
94 /**
95 * Character width, with stroke.
96 *
97 * @returns Width size.
98 */
99
100
101 /**
102 * Character center X position, with margins.
103 *
104 * @returns X position.
105 */
106 get centerX() {
107 return this.margin.left + (this.width - (this.margin.left + this.margin.right)) * 0.5;
108 }
109 /**
110 * Character center Y position, with margins.
111 *
112 * @returns Y position.
113 */
114
115
116 get centerY() {
117 return this.margin.top + (this.height - (this.margin.top + this.margin.bottom)) * 0.5;
118 }
119 /**
120 * Total width, with margin, rounded up to the next pixel.
121 *
122 * @returns Total width.
123 */
124
125
126 get width() {
127 return Math.ceil(this.margin.left + this.characterWidth + this.margin.right);
128 }
129 /**
130 * Total height, with margin, rounded up to the next pixel.
131 *
132 * @returns Total height.
133 */
134
135
136 get height() {
137 return Math.ceil(this.margin.top + this.characterHeight + this.margin.bottom);
138 }
139 /**
140 * Round value to the configured precision.
141 *
142 * @param n Value to be rounded.
143 * @returns Rounded value.
144 */
145
146
147 round(n) {
148 const x = Math.pow(10, this.precision);
149 return x ? Math.floor(n * x) / x : n;
150 }
151 /**
152 * Get character encode bits.
153 *
154 * @param character The character to encode.
155 * @returns Encode bits or null.
156 */
157
158
159 getCharacter(character) {
160 const c = character.toUpperCase();
161 const {
162 _characters
163 } = this;
164 return Object.prototype.hasOwnProperty.call(_characters, c) ? _characters[c] : null;
165 }
166 /**
167 * Get character elements for encode bits.
168 * Each length will be 3 (circle), 4 (line), or >4 (path).
169 * Circle: `[r, cx, cy]`
170 * Line: `[x1, y1, x2, y2]`
171 * Path: `[x1, y1, x2, y2, ...]` (NaN ending closes path)
172 * Values are padded and made absolute.
173 *
174 * @param bits Encode bits.
175 * @returns Element coordinates or null.
176 */
177
178
179 getElements(bits) {
180 const {
181 _elements
182 } = this;
183 const c = [this.centerX, this.centerY];
184 const r = [];
185
186 for (let i = 0; bits >= 1; bits /= 2, i++) {
187 // eslint-disable-next-line no-bitwise
188 if (bits & 1) {
189 const e = _elements[i];
190 r.push(e.map((v, i) => this.round(i === 2 && e.length === 3 ? v : v + c[i % 2])));
191 }
192 }
193
194 return r;
195 }
196 /**
197 * Get character elements for a character.
198 *
199 * @param character The character to encode.
200 * @returns Element coordinates or null.
201 */
202
203
204 getCharacterElements(character) {
205 const bits = this.getCharacter(character);
206 return bits === null ? null : this.getElements(bits);
207 }
208
209}
210
211exports.Characters = Characters;
212//# sourceMappingURL=characters.js.map