@xterm/addon-webgl 0.20.0-beta.2 → 0.20.0-beta.200

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,740 @@
1
+ /**
2
+ * Copyright (c) 2021 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ import { throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
7
+ import { customGlyphDefinitions } from './CustomGlyphDefinitions';
8
+ import { CustomGlyphDefinitionType, CustomGlyphScaleType, CustomGlyphVectorType, type CustomGlyphDefinitionPart, type CustomGlyphPathDrawFunctionDefinition, type CustomGlyphPatternDefinition, type ICustomGlyphSolidOctantBlockVector, type ICustomGlyphVectorShape } from './Types';
9
+
10
+ /**
11
+ * Try drawing a custom block element or box drawing character, returning whether it was
12
+ * successfully drawn.
13
+ */
14
+ export function tryDrawCustomGlyph(
15
+ ctx: CanvasRenderingContext2D,
16
+ c: string,
17
+ xOffset: number,
18
+ yOffset: number,
19
+ deviceCellWidth: number,
20
+ deviceCellHeight: number,
21
+ deviceCharWidth: number,
22
+ deviceCharHeight: number,
23
+ fontSize: number,
24
+ devicePixelRatio: number,
25
+ backgroundColor?: string,
26
+ variantOffset: number = 0
27
+ ): boolean {
28
+ const unifiedCharDefinition = customGlyphDefinitions[c];
29
+ if (unifiedCharDefinition) {
30
+ // Normalize to array for uniform handling
31
+ const parts = Array.isArray(unifiedCharDefinition) ? unifiedCharDefinition : [unifiedCharDefinition];
32
+ for (const part of parts) {
33
+ drawDefinitionPart(ctx, part, xOffset, yOffset, deviceCellWidth, deviceCellHeight, deviceCharWidth, deviceCharHeight, fontSize, devicePixelRatio, backgroundColor, variantOffset);
34
+ }
35
+ return true;
36
+ }
37
+
38
+ return false;
39
+ }
40
+
41
+ function drawDefinitionPart(
42
+ ctx: CanvasRenderingContext2D,
43
+ part: CustomGlyphDefinitionPart,
44
+ xOffset: number,
45
+ yOffset: number,
46
+ deviceCellWidth: number,
47
+ deviceCellHeight: number,
48
+ deviceCharWidth: number,
49
+ deviceCharHeight: number,
50
+ fontSize: number,
51
+ devicePixelRatio: number,
52
+ backgroundColor?: string,
53
+ variantOffset: number = 0
54
+ ): void {
55
+ // Handle scaleType - adjust dimensions and offset when scaling to character area
56
+ let drawWidth = deviceCellWidth;
57
+ let drawHeight = deviceCellHeight;
58
+ let drawXOffset = xOffset;
59
+ let drawYOffset = yOffset;
60
+ if (part.scaleType === CustomGlyphScaleType.CHAR) {
61
+ drawWidth = deviceCharWidth;
62
+ drawHeight = deviceCharHeight;
63
+ // Center the character within the cell
64
+ drawXOffset = xOffset + (deviceCellWidth - deviceCharWidth) / 2;
65
+ drawYOffset = yOffset + (deviceCellHeight - deviceCharHeight) / 2;
66
+ }
67
+
68
+ // Handle clipPath generically for any definition type
69
+ if (part.clipPath) {
70
+ ctx.save();
71
+ applyClipPath(ctx, part.clipPath, drawXOffset, drawYOffset, drawWidth, drawHeight);
72
+ }
73
+
74
+ switch (part.type) {
75
+ case CustomGlyphDefinitionType.SOLID_OCTANT_BLOCK_VECTOR:
76
+ drawBlockVectorChar(ctx, part.data, drawXOffset, drawYOffset, drawWidth, drawHeight);
77
+ break;
78
+ case CustomGlyphDefinitionType.BLOCK_PATTERN:
79
+ drawPatternChar(ctx, part.data, drawXOffset, drawYOffset, drawWidth, drawHeight, variantOffset);
80
+ break;
81
+ case CustomGlyphDefinitionType.PATH_FUNCTION:
82
+ drawPathFunctionCharacter(ctx, part.data, drawXOffset, drawYOffset, drawWidth, drawHeight, devicePixelRatio, part.strokeWidth);
83
+ break;
84
+ case CustomGlyphDefinitionType.PATH:
85
+ drawPathDefinitionCharacter(ctx, part.data, drawXOffset, drawYOffset, drawWidth, drawHeight, devicePixelRatio, part.strokeWidth);
86
+ break;
87
+ case CustomGlyphDefinitionType.PATH_NEGATIVE:
88
+ drawPathNegativeDefinitionCharacter(ctx, part.data, drawXOffset, drawYOffset, drawWidth, drawHeight, devicePixelRatio, backgroundColor);
89
+ break;
90
+ case CustomGlyphDefinitionType.VECTOR_SHAPE:
91
+ drawVectorShape(ctx, part.data, drawXOffset, drawYOffset, drawWidth, drawHeight, fontSize, devicePixelRatio);
92
+ break;
93
+ case CustomGlyphDefinitionType.BRAILLE:
94
+ drawBrailleCharacter(ctx, part.data, drawXOffset, drawYOffset, drawWidth, drawHeight);
95
+ break;
96
+ }
97
+
98
+ if (part.clipPath) {
99
+ ctx.restore();
100
+ }
101
+ }
102
+
103
+ function drawBlockVectorChar(
104
+ ctx: CanvasRenderingContext2D,
105
+ charDefinition: ICustomGlyphSolidOctantBlockVector[],
106
+ xOffset: number,
107
+ yOffset: number,
108
+ deviceCellWidth: number,
109
+ deviceCellHeight: number
110
+ ): void {
111
+ for (let i = 0; i < charDefinition.length; i++) {
112
+ const box = charDefinition[i];
113
+ const xEighth = deviceCellWidth / 8;
114
+ const yEighth = deviceCellHeight / 8;
115
+ ctx.fillRect(
116
+ xOffset + box.x * xEighth,
117
+ yOffset + box.y * yEighth,
118
+ box.w * xEighth,
119
+ box.h * yEighth
120
+ );
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Braille dot positions in octant coordinates (x, y for center of each dot area)
126
+ * Columns: left=1-2, right=5-6 (leaving 0 and 7 as margins, 3-4 as gap)
127
+ * Rows: 0-1, 2-3, 4-5, 6-7 for the 4 rows
128
+ */
129
+ const brailleDotPositions = new Uint8Array([
130
+ 1, 0, // dot 1 - bit 0
131
+ 1, 2, // dot 2 - bit 1
132
+ 1, 4, // dot 3 - bit 2
133
+ 5, 0, // dot 4 - bit 3
134
+ 5, 2, // dot 5 - bit 4
135
+ 5, 4, // dot 6 - bit 5
136
+ 1, 6, // dot 7 - bit 6
137
+ 5, 6, // dot 8 - bit 7
138
+ ]);
139
+
140
+ /**
141
+ * Draws a braille pattern
142
+ */
143
+ function drawBrailleCharacter(
144
+ ctx: CanvasRenderingContext2D,
145
+ pattern: number,
146
+ xOffset: number,
147
+ yOffset: number,
148
+ deviceCellWidth: number,
149
+ deviceCellHeight: number
150
+ ): void {
151
+ const xEighth = deviceCellWidth / 8;
152
+ const paddingY = deviceCellHeight * 0.1;
153
+ const usableHeight = deviceCellHeight * 0.8;
154
+ const yEighth = usableHeight / 8;
155
+ const radius = Math.min(xEighth, yEighth);
156
+
157
+ for (let bit = 0; bit < 8; bit++) {
158
+ if (pattern & (1 << bit)) {
159
+ const x = brailleDotPositions[bit * 2];
160
+ const y = brailleDotPositions[bit * 2 + 1];
161
+ const cx = xOffset + (x + 1) * xEighth;
162
+ const cy = yOffset + paddingY + (y + 1) * yEighth;
163
+ ctx.beginPath();
164
+ ctx.arc(cx, cy, radius, 0, Math.PI * 2);
165
+ ctx.fill();
166
+ }
167
+ }
168
+ }
169
+
170
+ function drawPathDefinitionCharacter(
171
+ ctx: CanvasRenderingContext2D,
172
+ charDefinition: CustomGlyphPathDrawFunctionDefinition | string,
173
+ xOffset: number,
174
+ yOffset: number,
175
+ deviceCellWidth: number,
176
+ deviceCellHeight: number,
177
+ devicePixelRatio: number,
178
+ strokeWidth?: number
179
+ ): void {
180
+ const instructions = typeof charDefinition === 'string' ? charDefinition : charDefinition(0, 0);
181
+ ctx.beginPath();
182
+ let currentX = 0;
183
+ let currentY = 0;
184
+ let lastControlX = 0;
185
+ let lastControlY = 0;
186
+ let lastCommand = '';
187
+ for (const instruction of instructions.split(' ')) {
188
+ const type = instruction[0];
189
+ const args: string[] = instruction.substring(1).split(',');
190
+ if (type === 'Z') {
191
+ ctx.closePath();
192
+ lastCommand = type;
193
+ continue;
194
+ }
195
+ if (type === 'V') {
196
+ const y = yOffset + parseFloat(args[0]) * deviceCellHeight;
197
+ ctx.lineTo(currentX, y);
198
+ currentY = y;
199
+ lastControlX = currentX;
200
+ lastControlY = currentY;
201
+ lastCommand = type;
202
+ continue;
203
+ }
204
+ if (type === 'H') {
205
+ const x = xOffset + parseFloat(args[0]) * deviceCellWidth;
206
+ ctx.lineTo(x, currentY);
207
+ currentX = x;
208
+ lastControlX = currentX;
209
+ lastControlY = currentY;
210
+ lastCommand = type;
211
+ continue;
212
+ }
213
+ if (!args[0] || !args[1]) {
214
+ continue;
215
+ }
216
+ if (type === 'A') {
217
+ // SVG arc: A rx,ry,xAxisRotation,largeArcFlag,sweepFlag,x,y
218
+ const rx = parseFloat(args[0]) * deviceCellWidth;
219
+ const ry = parseFloat(args[1]) * deviceCellHeight;
220
+ const xAxisRotation = parseFloat(args[2]) * Math.PI / 180;
221
+ const largeArcFlag = parseInt(args[3]);
222
+ const sweepFlag = parseInt(args[4]);
223
+ const x = xOffset + parseFloat(args[5]) * deviceCellWidth;
224
+ const y = yOffset + parseFloat(args[6]) * deviceCellHeight;
225
+ drawSvgArc(ctx, currentX, currentY, rx, ry, xAxisRotation, largeArcFlag, sweepFlag, x, y);
226
+ currentX = x;
227
+ currentY = y;
228
+ continue;
229
+ }
230
+ const translatedArgs = args.map((e, i) => {
231
+ const val = parseFloat(e);
232
+ return i % 2 === 0
233
+ ? xOffset + val * deviceCellWidth
234
+ : yOffset + val * deviceCellHeight;
235
+ });
236
+ if (type === 'M') {
237
+ ctx.moveTo(translatedArgs[0], translatedArgs[1]);
238
+ currentX = translatedArgs[0];
239
+ currentY = translatedArgs[1];
240
+ lastControlX = currentX;
241
+ lastControlY = currentY;
242
+ } else if (type === 'L') {
243
+ ctx.lineTo(translatedArgs[0], translatedArgs[1]);
244
+ currentX = translatedArgs[0];
245
+ currentY = translatedArgs[1];
246
+ lastControlX = currentX;
247
+ lastControlY = currentY;
248
+ } else if (type === 'Q') {
249
+ ctx.quadraticCurveTo(translatedArgs[0], translatedArgs[1], translatedArgs[2], translatedArgs[3]);
250
+ lastControlX = translatedArgs[0];
251
+ lastControlY = translatedArgs[1];
252
+ currentX = translatedArgs[2];
253
+ currentY = translatedArgs[3];
254
+ } else if (type === 'T') {
255
+ // T uses reflection of last control point if previous command was Q or T
256
+ let cpX: number;
257
+ let cpY: number;
258
+ if (lastCommand === 'Q' || lastCommand === 'T') {
259
+ cpX = 2 * currentX - lastControlX;
260
+ cpY = 2 * currentY - lastControlY;
261
+ } else {
262
+ cpX = currentX;
263
+ cpY = currentY;
264
+ }
265
+ ctx.quadraticCurveTo(cpX, cpY, translatedArgs[0], translatedArgs[1]);
266
+ lastControlX = cpX;
267
+ lastControlY = cpY;
268
+ currentX = translatedArgs[0];
269
+ currentY = translatedArgs[1];
270
+ } else if (type === 'C') {
271
+ ctx.bezierCurveTo(translatedArgs[0], translatedArgs[1], translatedArgs[2], translatedArgs[3], translatedArgs[4], translatedArgs[5]);
272
+ lastControlX = translatedArgs[2];
273
+ lastControlY = translatedArgs[3];
274
+ currentX = translatedArgs[4];
275
+ currentY = translatedArgs[5];
276
+ }
277
+ lastCommand = type;
278
+ }
279
+ if (strokeWidth !== undefined) {
280
+ ctx.strokeStyle = ctx.fillStyle;
281
+ ctx.lineWidth = devicePixelRatio * strokeWidth;
282
+ ctx.stroke();
283
+ } else {
284
+ ctx.fill();
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Converts SVG arc parameters to canvas arc/ellipse calls.
290
+ * Based on the SVG spec's endpoint to center parameterization conversion.
291
+ */
292
+ function drawSvgArc(
293
+ ctx: CanvasRenderingContext2D,
294
+ x1: number, y1: number,
295
+ rx: number, ry: number,
296
+ phi: number,
297
+ largeArcFlag: number,
298
+ sweepFlag: number,
299
+ x2: number, y2: number
300
+ ): void {
301
+ // Handle degenerate cases
302
+ if (rx === 0 || ry === 0) {
303
+ ctx.lineTo(x2, y2);
304
+ return;
305
+ }
306
+
307
+ rx = Math.abs(rx);
308
+ ry = Math.abs(ry);
309
+
310
+ const cosPhi = Math.cos(phi);
311
+ const sinPhi = Math.sin(phi);
312
+
313
+ // Step 1: Compute (x1', y1')
314
+ const dx = (x1 - x2) / 2;
315
+ const dy = (y1 - y2) / 2;
316
+ const x1p = cosPhi * dx + sinPhi * dy;
317
+ const y1p = -sinPhi * dx + cosPhi * dy;
318
+
319
+ // Step 2: Compute (cx', cy')
320
+ let rxSq = rx * rx;
321
+ let rySq = ry * ry;
322
+ const x1pSq = x1p * x1p;
323
+ const y1pSq = y1p * y1p;
324
+
325
+ // Correct radii if necessary
326
+ const lambda = x1pSq / rxSq + y1pSq / rySq;
327
+ if (lambda > 1) {
328
+ const lambdaSqrt = Math.sqrt(lambda);
329
+ rx *= lambdaSqrt;
330
+ ry *= lambdaSqrt;
331
+ rxSq = rx * rx;
332
+ rySq = ry * ry;
333
+ }
334
+
335
+ let sq = (rxSq * rySq - rxSq * y1pSq - rySq * x1pSq) / (rxSq * y1pSq + rySq * x1pSq);
336
+ if (sq < 0) sq = 0;
337
+ const coef = (largeArcFlag === sweepFlag ? -1 : 1) * Math.sqrt(sq);
338
+ const cxp = coef * (rx * y1p / ry);
339
+ const cyp = coef * -(ry * x1p / rx);
340
+
341
+ // Step 3: Compute (cx, cy) from (cx', cy')
342
+ const cx = cosPhi * cxp - sinPhi * cyp + (x1 + x2) / 2;
343
+ const cy = sinPhi * cxp + cosPhi * cyp + (y1 + y2) / 2;
344
+
345
+ // Step 4: Compute angles
346
+ const ux = (x1p - cxp) / rx;
347
+ const uy = (y1p - cyp) / ry;
348
+ const vx = (-x1p - cxp) / rx;
349
+ const vy = (-y1p - cyp) / ry;
350
+
351
+ const startAngle = Math.atan2(uy, ux);
352
+ let dTheta = Math.atan2(vy, vx) - startAngle;
353
+
354
+ if (sweepFlag === 0 && dTheta > 0) {
355
+ dTheta -= 2 * Math.PI;
356
+ } else if (sweepFlag === 1 && dTheta < 0) {
357
+ dTheta += 2 * Math.PI;
358
+ }
359
+
360
+ const endAngle = startAngle + dTheta;
361
+
362
+ ctx.ellipse(cx, cy, rx, ry, phi, startAngle, endAngle, sweepFlag === 0);
363
+ }
364
+
365
+ /**
366
+ * Draws a "negative" path where the background color is used to draw the shape on top of a
367
+ * foreground-filled cell. This creates the appearance of a cutout without using actual
368
+ * transparency, which allows SPAA (subpixel anti-aliasing) to work correctly.
369
+ *
370
+ * @param ctx The canvas rendering context (fillStyle should be set to foreground color)
371
+ * @param charDefinition The vector shape definition for the negative shape
372
+ * @param xOffset The x offset to draw at
373
+ * @param yOffset The y offset to draw at
374
+ * @param deviceCellWidth The width of the cell in device pixels
375
+ * @param deviceCellHeight The height of the cell in device pixels
376
+ * @param devicePixelRatio The device pixel ratio
377
+ * @param backgroundColor The background color to use for the "cutout" portion
378
+ */
379
+ function drawPathNegativeDefinitionCharacter(
380
+ ctx: CanvasRenderingContext2D,
381
+ charDefinition: ICustomGlyphVectorShape,
382
+ xOffset: number,
383
+ yOffset: number,
384
+ deviceCellWidth: number,
385
+ deviceCellHeight: number,
386
+ devicePixelRatio: number,
387
+ backgroundColor?: string
388
+ ): void {
389
+ ctx.save();
390
+
391
+ // First, fill the entire cell with foreground color
392
+ ctx.fillRect(xOffset, yOffset, deviceCellWidth, deviceCellHeight);
393
+
394
+ // Then draw the "negative" shape with the background color
395
+ if (backgroundColor) {
396
+ ctx.fillStyle = backgroundColor;
397
+ ctx.strokeStyle = backgroundColor;
398
+ }
399
+
400
+ ctx.lineWidth = devicePixelRatio;
401
+ ctx.lineCap = 'square';
402
+ ctx.beginPath();
403
+ for (const instruction of charDefinition.d.split(' ')) {
404
+ const type = instruction[0];
405
+ const args: string[] = instruction.substring(1).split(',');
406
+ if (!args[0] || !args[1]) {
407
+ if (type === 'Z') {
408
+ ctx.closePath();
409
+ }
410
+ continue;
411
+ }
412
+ const translatedArgs = args.map((e, i) => {
413
+ const val = parseFloat(e);
414
+ return i % 2 === 0
415
+ ? xOffset + val * deviceCellWidth
416
+ : yOffset + val * deviceCellHeight;
417
+ });
418
+ if (type === 'M') {
419
+ ctx.moveTo(translatedArgs[0], translatedArgs[1]);
420
+ } else if (type === 'L') {
421
+ ctx.lineTo(translatedArgs[0], translatedArgs[1]);
422
+ }
423
+ }
424
+
425
+ if (charDefinition.type === CustomGlyphVectorType.STROKE) {
426
+ ctx.stroke();
427
+ } else {
428
+ ctx.fill();
429
+ }
430
+
431
+ ctx.restore();
432
+ }
433
+
434
+ const cachedPatterns: Map<CustomGlyphPatternDefinition, Map</* fillStyle */string, CanvasPattern>> = new Map();
435
+
436
+ function drawPatternChar(
437
+ ctx: CanvasRenderingContext2D,
438
+ charDefinition: number[][],
439
+ xOffset: number,
440
+ yOffset: number,
441
+ deviceCellWidth: number,
442
+ deviceCellHeight: number,
443
+ variantOffset: number = 0
444
+ ): void {
445
+ let patternSet = cachedPatterns.get(charDefinition);
446
+ if (!patternSet) {
447
+ patternSet = new Map();
448
+ cachedPatterns.set(charDefinition, patternSet);
449
+ }
450
+ const fillStyle = ctx.fillStyle;
451
+ if (typeof fillStyle !== 'string') {
452
+ throw new Error(`Unexpected fillStyle type "${fillStyle}"`);
453
+ }
454
+ let pattern = patternSet.get(fillStyle);
455
+ if (!pattern) {
456
+ const width = charDefinition[0].length;
457
+ const height = charDefinition.length;
458
+ const tmpCanvas = ctx.canvas.ownerDocument.createElement('canvas');
459
+ tmpCanvas.width = width;
460
+ tmpCanvas.height = height;
461
+ const tmpCtx = throwIfFalsy(tmpCanvas.getContext('2d'));
462
+ const imageData = new ImageData(width, height);
463
+
464
+ // Extract rgba from fillStyle
465
+ let r: number;
466
+ let g: number;
467
+ let b: number;
468
+ let a: number;
469
+ if (fillStyle.startsWith('#')) {
470
+ r = parseInt(fillStyle.slice(1, 3), 16);
471
+ g = parseInt(fillStyle.slice(3, 5), 16);
472
+ b = parseInt(fillStyle.slice(5, 7), 16);
473
+ a = fillStyle.length > 7 && parseInt(fillStyle.slice(7, 9), 16) || 1;
474
+ } else if (fillStyle.startsWith('rgba')) {
475
+ ([r, g, b, a] = fillStyle.substring(5, fillStyle.length - 1).split(',').map(e => parseFloat(e)));
476
+ } else {
477
+ throw new Error(`Unexpected fillStyle color format "${fillStyle}" when drawing pattern glyph`);
478
+ }
479
+
480
+ for (let y = 0; y < height; y++) {
481
+ for (let x = 0; x < width; x++) {
482
+ imageData.data[(y * width + x) * 4 ] = r;
483
+ imageData.data[(y * width + x) * 4 + 1] = g;
484
+ imageData.data[(y * width + x) * 4 + 2] = b;
485
+ imageData.data[(y * width + x) * 4 + 3] = charDefinition[y][x] * (a * 255);
486
+ }
487
+ }
488
+ tmpCtx.putImageData(imageData, 0, 0);
489
+ pattern = throwIfFalsy(ctx.createPattern(tmpCanvas, null));
490
+ patternSet.set(fillStyle, pattern);
491
+ }
492
+ // Apply pattern offset to ensure seamless tiling across cells when cell dimensions are odd.
493
+ // variantOffset encodes: bit 1 = x pixel shift, bit 0 = y pixel shift.
494
+ const dx = (variantOffset >> 1) & 1;
495
+ const dy = variantOffset & 1;
496
+ if (dx !== 0 || dy !== 0) {
497
+ pattern.setTransform(new DOMMatrix().translateSelf(-dx, -dy));
498
+ } else {
499
+ pattern.setTransform(new DOMMatrix());
500
+ }
501
+ ctx.fillStyle = pattern;
502
+ ctx.fillRect(xOffset, yOffset, deviceCellWidth, deviceCellHeight);
503
+ }
504
+
505
+ function drawPathFunctionCharacter(
506
+ ctx: CanvasRenderingContext2D,
507
+ charDefinition: string | ((xp: number, yp: number) => string),
508
+ xOffset: number,
509
+ yOffset: number,
510
+ deviceCellWidth: number,
511
+ deviceCellHeight: number,
512
+ devicePixelRatio: number,
513
+ strokeWidth?: number
514
+ ): void {
515
+ ctx.save();
516
+ ctx.beginPath();
517
+ ctx.rect(xOffset, yOffset, deviceCellWidth, deviceCellHeight);
518
+ ctx.clip();
519
+
520
+ ctx.beginPath();
521
+ let actualInstructions: string;
522
+ if (typeof charDefinition === 'function') {
523
+ const xp = .15;
524
+ const yp = .15 / deviceCellHeight * deviceCellWidth;
525
+ actualInstructions = charDefinition(xp, yp);
526
+ } else {
527
+ actualInstructions = charDefinition;
528
+ }
529
+ const state: ISvgPathState = { currentX: 0, currentY: 0, lastControlX: 0, lastControlY: 0, lastCommand: '' };
530
+ for (const instruction of actualInstructions.split(' ')) {
531
+ const type = instruction[0];
532
+ if (type === 'Z') {
533
+ ctx.closePath();
534
+ state.lastCommand = type;
535
+ continue;
536
+ }
537
+ const f = svgToCanvasInstructionMap[type];
538
+ if (!f) {
539
+ console.error(`Could not find drawing instructions for "${type}"`);
540
+ continue;
541
+ }
542
+ const args: string[] = instruction.substring(1).split(',');
543
+ if (!args[0] || !args[1]) {
544
+ continue;
545
+ }
546
+ f(ctx, translateArgs(args, deviceCellWidth, deviceCellHeight, xOffset, yOffset, true, devicePixelRatio, 0, 0, false), state);
547
+ state.lastCommand = type;
548
+ }
549
+ if (strokeWidth !== undefined) {
550
+ ctx.strokeStyle = ctx.fillStyle;
551
+ ctx.lineWidth = devicePixelRatio * strokeWidth;
552
+ ctx.stroke();
553
+ } else {
554
+ ctx.fill();
555
+ }
556
+ ctx.closePath();
557
+ ctx.restore();
558
+ }
559
+
560
+ /**
561
+ * Applies a clip path to the canvas context from SVG-like path instructions.
562
+ */
563
+ function applyClipPath(
564
+ ctx: CanvasRenderingContext2D,
565
+ clipPath: string,
566
+ xOffset: number,
567
+ yOffset: number,
568
+ deviceCellWidth: number,
569
+ deviceCellHeight: number
570
+ ): void {
571
+ ctx.beginPath();
572
+ for (const instruction of clipPath.split(' ')) {
573
+ const type = instruction[0];
574
+ if (type === 'Z') {
575
+ ctx.closePath();
576
+ continue;
577
+ }
578
+ const args: string[] = instruction.substring(1).split(',');
579
+ if (!args[0] || !args[1]) {
580
+ continue;
581
+ }
582
+ const x = xOffset + parseFloat(args[0]) * deviceCellWidth;
583
+ const y = yOffset + parseFloat(args[1]) * deviceCellHeight;
584
+ if (type === 'M') {
585
+ ctx.moveTo(x, y);
586
+ } else if (type === 'L') {
587
+ ctx.lineTo(x, y);
588
+ }
589
+ }
590
+ ctx.clip();
591
+ }
592
+
593
+ function drawVectorShape(
594
+ ctx: CanvasRenderingContext2D,
595
+ charDefinition: ICustomGlyphVectorShape,
596
+ xOffset: number,
597
+ yOffset: number,
598
+ deviceCellWidth: number,
599
+ deviceCellHeight: number,
600
+ fontSize: number,
601
+ devicePixelRatio: number
602
+ ): void {
603
+ // Clip the cell to make sure drawing doesn't occur beyond bounds
604
+ const clipRegion = new Path2D();
605
+ clipRegion.rect(xOffset, yOffset, deviceCellWidth, deviceCellHeight);
606
+ ctx.clip(clipRegion);
607
+
608
+ ctx.beginPath();
609
+ // Scale the stroke with DPR and font size
610
+ const cssLineWidth = fontSize / 12;
611
+ ctx.lineWidth = devicePixelRatio * cssLineWidth;
612
+ const state: ISvgPathState = { currentX: 0, currentY: 0, lastControlX: 0, lastControlY: 0, lastCommand: '' };
613
+ for (const instruction of charDefinition.d.split(' ')) {
614
+ const type = instruction[0];
615
+ if (type === 'Z') {
616
+ ctx.closePath();
617
+ state.lastCommand = type;
618
+ continue;
619
+ }
620
+ const f = svgToCanvasInstructionMap[type];
621
+ if (!f) {
622
+ console.error(`Could not find drawing instructions for "${type}"`);
623
+ continue;
624
+ }
625
+ const args: string[] = instruction.substring(1).split(',');
626
+ if (!args[0] || !args[1]) {
627
+ continue;
628
+ }
629
+ f(ctx, translateArgs(
630
+ args,
631
+ deviceCellWidth,
632
+ deviceCellHeight,
633
+ xOffset,
634
+ yOffset,
635
+ false,
636
+ devicePixelRatio,
637
+ (charDefinition.leftPadding ?? 0) * (cssLineWidth / 2),
638
+ (charDefinition.rightPadding ?? 0) * (cssLineWidth / 2)
639
+ ), state);
640
+ state.lastCommand = type;
641
+ }
642
+ if (charDefinition.type === CustomGlyphVectorType.STROKE) {
643
+ ctx.strokeStyle = ctx.fillStyle;
644
+ ctx.stroke();
645
+ } else {
646
+ ctx.fill();
647
+ }
648
+ ctx.closePath();
649
+ }
650
+
651
+ function clamp(value: number, max: number, min: number = 0): number {
652
+ return Math.max(Math.min(value, max), min);
653
+ }
654
+
655
+ interface ISvgPathState {
656
+ currentX: number;
657
+ currentY: number;
658
+ lastControlX: number;
659
+ lastControlY: number;
660
+ lastCommand: string;
661
+ }
662
+
663
+ const svgToCanvasInstructionMap: { [index: string]: (ctx: CanvasRenderingContext2D, args: number[], state: ISvgPathState) => void } = {
664
+ 'C': (ctx, args, state) => {
665
+ ctx.bezierCurveTo(args[0], args[1], args[2], args[3], args[4], args[5]);
666
+ state.lastControlX = args[2];
667
+ state.lastControlY = args[3];
668
+ state.currentX = args[4];
669
+ state.currentY = args[5];
670
+ },
671
+ 'L': (ctx, args, state) => {
672
+ ctx.lineTo(args[0], args[1]);
673
+ state.lastControlX = state.currentX = args[0];
674
+ state.lastControlY = state.currentY = args[1];
675
+ },
676
+ 'M': (ctx, args, state) => {
677
+ ctx.moveTo(args[0], args[1]);
678
+ state.lastControlX = state.currentX = args[0];
679
+ state.lastControlY = state.currentY = args[1];
680
+ },
681
+ 'Q': (ctx, args, state) => {
682
+ ctx.quadraticCurveTo(args[0], args[1], args[2], args[3]);
683
+ state.lastControlX = args[0];
684
+ state.lastControlY = args[1];
685
+ state.currentX = args[2];
686
+ state.currentY = args[3];
687
+ },
688
+ 'T': (ctx, args, state) => {
689
+ let cpX: number;
690
+ let cpY: number;
691
+ if (state.lastCommand === 'Q' || state.lastCommand === 'T') {
692
+ cpX = 2 * state.currentX - state.lastControlX;
693
+ cpY = 2 * state.currentY - state.lastControlY;
694
+ } else {
695
+ cpX = state.currentX;
696
+ cpY = state.currentY;
697
+ }
698
+ ctx.quadraticCurveTo(cpX, cpY, args[0], args[1]);
699
+ state.lastControlX = cpX;
700
+ state.lastControlY = cpY;
701
+ state.currentX = args[0];
702
+ state.currentY = args[1];
703
+ }
704
+ };
705
+
706
+ function translateArgs(args: string[], cellWidth: number, cellHeight: number, xOffset: number, yOffset: number, doClamp: boolean, devicePixelRatio: number, leftPadding: number = 0, rightPadding: number = 0, clampToCell: boolean = true): number[] {
707
+ const result = args.map(e => parseFloat(e) || parseInt(e));
708
+
709
+ if (result.length < 2) {
710
+ throw new Error('Too few arguments for instruction');
711
+ }
712
+
713
+ for (let x = 0; x < result.length; x += 2) {
714
+ // Translate from 0-1 to 0-cellWidth
715
+ result[x] *= cellWidth - (leftPadding * devicePixelRatio) - (rightPadding * devicePixelRatio);
716
+ // Round to the nearest 0.5 to ensure a crisp line at 100% devicePixelRatio, and optionally
717
+ // clamp to the cell bounds.
718
+ if (doClamp && result[x] !== 0) {
719
+ const rounded = Math.round(result[x] + 0.5) - 0.5;
720
+ result[x] = clampToCell ? clamp(rounded, cellWidth, 0) : rounded;
721
+ }
722
+ // Apply the cell's offset (ie. x*cellWidth)
723
+ result[x] += xOffset + (leftPadding * devicePixelRatio);
724
+ }
725
+
726
+ for (let y = 1; y < result.length; y += 2) {
727
+ // Translate from 0-1 to 0-cellHeight
728
+ result[y] *= cellHeight;
729
+ // Round to the nearest 0.5 to ensure a crisp line at 100% devicePixelRatio, and optionally
730
+ // clamp to the cell bounds.
731
+ if (doClamp && result[y] !== 0) {
732
+ const rounded = Math.round(result[y] + 0.5) - 0.5;
733
+ result[y] = clampToCell ? clamp(rounded, cellHeight, 0) : rounded;
734
+ }
735
+ // Apply the cell's offset (ie. x*cellHeight)
736
+ result[y] += yOffset;
737
+ }
738
+
739
+ return result;
740
+ }