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