Debian Codegraph @

packages/datagrid/src/graphicscontext.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2019, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10 import { IDisposable } from '@lumino/disposable';
11
12 /**
13 * A thin caching wrapper around a 2D canvas rendering context.
14 *
15 * #### Notes
16 * This class is mostly a transparent wrapper around a canvas rendering
17 * context which improves performance when writing context state.
18 *
19 * For best performance, avoid reading state from the `gc`. Writes are
20 * cached based on the previously written value.
21 *
22 * Unless otherwise specified, the API and semantics of this class are
23 * identical to the builtin 2D canvas rendering context:
24 * https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
25 *
26 * The wrapped canvas context should not be manipulated externally
27 * until the wrapping `GraphicsContext` object is disposed.
28 */
29 export class GraphicsContext implements IDisposable {
30 /**
31 * Create a new graphics context object.
32 *
33 * @param context - The 2D canvas rendering context to wrap.
34 */
35 constructor(context: CanvasRenderingContext2D) {
36 this._context = context;
37 this._state = Private.State.create(context);
38 }
39
40 dispose(): void {
41 // Bail if the gc is already disposed.
42 if (this._disposed) {
43 return;
44 }
45
46 // Mark the gc as disposed.
47 this._disposed = true;
48
49 // Pop any unrestored saves.
50 while (this._state.next) {
51 this._state = this._state.next;
52 this._context.restore();
53 }
54 }
55
56 get isDisposed(): boolean {
57 return this._disposed;
58 }
59
60 get fillStyle(): string | CanvasGradient | CanvasPattern {
61 return this._context.fillStyle;
62 }
63
64 set fillStyle(value: string | CanvasGradient | CanvasPattern) {
65 if (this._state.fillStyle !== value) {
66 this._state.fillStyle = value;
67 this._context.fillStyle = value;
68 }
69 }
70
71 get strokeStyle(): string | CanvasGradient | CanvasPattern {
72 return this._context.strokeStyle;
73 }
74
75 set strokeStyle(value: string | CanvasGradient | CanvasPattern) {
76 if (this._state.strokeStyle !== value) {
77 this._state.strokeStyle = value;
78 this._context.strokeStyle = value;
79 }
80 }
81
82 get font(): string {
83 return this._context.font;
84 }
85
86 set font(value: string) {
87 if (this._state.font !== value) {
88 this._state.font = value;
89 this._context.font = value;
90 }
91 }
92
93 get textAlign(): CanvasTextAlign {
94 return this._context.textAlign;
95 }
96
97 set textAlign(value: CanvasTextAlign) {
98 if (this._state.textAlign !== value) {
99 this._state.textAlign = value;
100 this._context.textAlign = value;
101 }
102 }
103
104 get textBaseline(): CanvasTextBaseline {
105 return this._context.textBaseline;
106 }
107
108 set textBaseline(value: CanvasTextBaseline) {
109 if (this._state.textBaseline !== value) {
110 this._state.textBaseline = value;
111 this._context.textBaseline = value;
112 }
113 }
114
115 get lineCap(): CanvasLineCap {
116 return this._context.lineCap;
117 }
118
119 set lineCap(value: CanvasLineCap) {
120 if (this._state.lineCap !== value) {
121 this._state.lineCap = value;
122 this._context.lineCap = value;
123 }
124 }
125
126 get lineDashOffset(): number {
127 return this._context.lineDashOffset;
128 }
129
130 set lineDashOffset(value: number) {
131 if (this._state.lineDashOffset !== value) {
132 this._state.lineDashOffset = value;
133 this._context.lineDashOffset = value;
134 }
135 }
136
137 get lineJoin(): CanvasLineJoin {
138 return this._context.lineJoin;
139 }
140
141 set lineJoin(value: CanvasLineJoin) {
142 if (this._state.lineJoin !== value) {
143 this._state.lineJoin = value;
144 this._context.lineJoin = value;
145 }
146 }
147
148 get lineWidth(): number {
149 return this._context.lineWidth;
150 }
151
152 set lineWidth(value: number) {
153 if (this._state.lineWidth !== value) {
154 this._state.lineWidth = value;
155 this._context.lineWidth = value;
156 }
157 }
158
159 get miterLimit(): number {
160 return this._context.miterLimit;
161 }
162
163 set miterLimit(value: number) {
164 if (this._state.miterLimit !== value) {
165 this._state.miterLimit = value;
166 this._context.miterLimit = value;
167 }
168 }
169
170 get shadowBlur(): number {
171 return this._context.shadowBlur;
172 }
173
174 set shadowBlur(value: number) {
175 if (this._state.shadowBlur !== value) {
176 this._state.shadowBlur = value;
177 this._context.shadowBlur = value;
178 }
179 }
180
181 get shadowColor(): string {
182 return this._context.shadowColor;
183 }
184
185 set shadowColor(value: string) {
186 if (this._state.shadowColor !== value) {
187 this._state.shadowColor = value;
188 this._context.shadowColor = value;
189 }
190 }
191
192 get shadowOffsetX(): number {
193 return this._context.shadowOffsetX;
194 }
195
196 set shadowOffsetX(value: number) {
197 if (this._state.shadowOffsetX !== value) {
198 this._state.shadowOffsetX = value;
199 this._context.shadowOffsetX = value;
200 }
201 }
202
203 get shadowOffsetY(): number {
204 return this._context.shadowOffsetY;
205 }
206
207 set shadowOffsetY(value: number) {
208 if (this._state.shadowOffsetY !== value) {
209 this._state.shadowOffsetY = value;
210 this._context.shadowOffsetY = value;
211 }
212 }
213
214 get imageSmoothingEnabled(): boolean {
215 return this._context.imageSmoothingEnabled;
216 }
217
218 set imageSmoothingEnabled(value: boolean) {
219 if (this._state.imageSmoothingEnabled !== value) {
220 this._state.imageSmoothingEnabled = value;
221 this._context.imageSmoothingEnabled = value;
222 }
223 }
224
225 get globalAlpha(): number {
226 return this._context.globalAlpha;
227 }
228
229 set globalAlpha(value: number) {
230 if (this._state.globalAlpha !== value) {
231 this._state.globalAlpha = value;
232 this._context.globalAlpha = value;
233 }
234 }
235
236 get globalCompositeOperation(): GlobalCompositeOperation {
237 return this._context.globalCompositeOperation;
238 }
239
240 set globalCompositeOperation(value: GlobalCompositeOperation) {
241 if (this._state.globalCompositeOperation !== value) {
242 this._state.globalCompositeOperation = value;
243 this._context.globalCompositeOperation = value;
244 }
245 }
246
247 getLineDash(): number[] {
248 return this._context.getLineDash();
249 }
250
251 setLineDash(segments: number[]): void {
252 this._context.setLineDash(segments);
253 }
254
255 rotate(angle: number): void {
256 this._context.rotate(angle);
257 }
258
259 scale(x: number, y: number): void {
260 this._context.scale(x, y);
261 }
262
263 transform(
264 m11: number,
265 m12: number,
266 m21: number,
267 m22: number,
268 dx: number,
269 dy: number
270 ): void {
271 this._context.transform(m11, m12, m21, m22, dx, dy);
272 }
273
274 translate(x: number, y: number): void {
275 this._context.translate(x, y);
276 }
277
278 setTransform(
279 m11: number,
280 m12: number,
281 m21: number,
282 m22: number,
283 dx: number,
284 dy: number
285 ): void {
286 this._context.setTransform(m11, m12, m21, m22, dx, dy);
287 }
288
289 save(): void {
290 // Clone an push the current state to the stack.
291 this._state = Private.State.push(this._state);
292
293 // Save the wrapped context state.
294 this._context.save();
295 }
296
297 restore(): void {
298 // Bail if there is no state to restore.
299 if (!this._state.next) {
300 return;
301 }
302
303 // Pop the saved state from the stack.
304 this._state = Private.State.pop(this._state);
305
306 // Restore the wrapped context state.
307 this._context.restore();
308 }
309
310 beginPath(): void {
311 return this._context.beginPath();
312 }
313
314 closePath(): void {
315 this._context.closePath();
316 }
317
318 isPointInPath(x: number, y: number, fillRule?: CanvasFillRule): boolean {
319 let result: boolean;
320 if (arguments.length === 2) {
321 result = this._context.isPointInPath(x, y);
322 } else {
323 result = this._context.isPointInPath(x, y, fillRule);
324 }
325 return result;
326 }
327
328 arc(
329 x: number,
330 y: number,
331 radius: number,
332 startAngle: number,
333 endAngle: number,
334 anticlockwise?: boolean
335 ): void {
336 if (arguments.length === 5) {
337 this._context.arc(x, y, radius, startAngle, endAngle);
338 } else {
339 this._context.arc(x, y, radius, startAngle, endAngle, anticlockwise);
340 }
341 }
342
343 arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void {
344 this._context.arcTo(x1, y1, x2, y2, radius);
345 }
346
347 bezierCurveTo(
348 cp1x: number,
349 cp1y: number,
350 cp2x: number,
351 cp2y: number,
352 x: number,
353 y: number
354 ): void {
355 this._context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
356 }
357
358 ellipse(
359 x: number,
360 y: number,
361 radiusX: number,
362 radiusY: number,
363 rotation: number,
364 startAngle: number,
365 endAngle: number,
366 anticlockwise?: boolean
367 ): void {
368 if (arguments.length === 7) {
369 this._context.ellipse(
370 x,
371 y,
372 radiusX,
373 radiusY,
374 rotation,
375 startAngle,
376 endAngle
377 );
378 } else {
379 this._context.ellipse(
380 x,
381 y,
382 radiusX,
383 radiusY,
384 rotation,
385 startAngle,
386 endAngle,
387 anticlockwise
388 );
389 }
390 }
391
392 lineTo(x: number, y: number): void {
393 this._context.lineTo(x, y);
394 }
395
396 moveTo(x: number, y: number): void {
397 this._context.moveTo(x, y);
398 }
399
400 quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void {
401 this._context.quadraticCurveTo(cpx, cpy, x, y);
402 }
403
404 rect(x: number, y: number, w: number, h: number): void {
405 this._context.rect(x, y, w, h);
406 }
407
408 clip(fillRule?: CanvasFillRule): void {
409 if (arguments.length === 0) {
410 this._context.clip();
411 } else {
412 this._context.clip(fillRule);
413 }
414 }
415
416 fill(fillRule?: CanvasFillRule): void {
417 if (arguments.length === 0) {
418 this._context.fill();
419 } else {
420 this._context.fill(fillRule);
421 }
422 }
423
424 stroke(): void {
425 this._context.stroke();
426 }
427
428 clearRect(x: number, y: number, w: number, h: number): void {
429 return this._context.clearRect(x, y, w, h);
430 }
431
432 fillRect(x: number, y: number, w: number, h: number): void {
433 this._context.fillRect(x, y, w, h);
434 }
435
436 fillText(text: string, x: number, y: number, maxWidth?: number): void {
437 if (arguments.length === 3) {
438 this._context.fillText(text, x, y);
439 } else {
440 this._context.fillText(text, x, y, maxWidth);
441 }
442 }
443
444 strokeRect(x: number, y: number, w: number, h: number): void {
445 this._context.strokeRect(x, y, w, h);
446 }
447
448 strokeText(text: string, x: number, y: number, maxWidth?: number): void {
449 if (arguments.length === 3) {
450 this._context.strokeText(text, x, y);
451 } else {
452 this._context.strokeText(text, x, y, maxWidth);
453 }
454 }
455
456 measureText(text: string): TextMetrics {
457 return this._context.measureText(text);
458 }
459
460 createLinearGradient(
461 x0: number,
462 y0: number,
463 x1: number,
464 y1: number
465 ): CanvasGradient {
466 return this._context.createLinearGradient(x0, y0, x1, y1);
467 }
468
469 createRadialGradient(
470 x0: number,
471 y0: number,
472 r0: number,
473 x1: number,
474 y1: number,
475 r1: number
476 ): CanvasGradient {
477 return this._context.createRadialGradient(x0, y0, r0, x1, y1, r1);
478 }
479
480 createPattern(
481 image: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement,
482 repetition: string
483 ): CanvasPattern | null {
484 return this._context.createPattern(image, repetition);
485 }
486
487 createImageData(imageData: ImageData): ImageData;
488 createImageData(sw: number, sh: number): ImageData;
489 createImageData(): ImageData {
490 // eslint-disable-next-line prefer-spread, prefer-rest-params
491 return this._context.createImageData.apply(this._context, arguments);
492 }
493
494 getImageData(sx: number, sy: number, sw: number, sh: number): ImageData {
495 return this._context.getImageData(sx, sy, sw, sh);
496 }
497
498 putImageData(imagedata: ImageData, dx: number, dy: number): void;
499 putImageData(
500 imagedata: ImageData,
501 dx: number,
502 dy: number,
503 dirtyX: number,
504 dirtyY: number,
505 dirtyWidth: number,
506 dirtyHeight: number
507 ): void;
508 putImageData(): void {
509 // eslint-disable-next-line prefer-spread, prefer-rest-params
510 this._context.putImageData.apply(this._context, arguments);
511 }
512
513 drawImage(
514 image:
515 | HTMLImageElement
516 | HTMLCanvasElement
517 | HTMLVideoElement
518 | ImageBitmap,
519 dstX: number,
520 dstY: number
521 ): void;
522 drawImage(
523 image:
524 | HTMLImageElement
525 | HTMLCanvasElement
526 | HTMLVideoElement
527 | ImageBitmap,
528 dstX: number,
529 dstY: number,
530 dstW: number,
531 dstH: number
532 ): void;
533 drawImage(
534 image:
535 | HTMLImageElement
536 | HTMLCanvasElement
537 | HTMLVideoElement
538 | ImageBitmap,
539 srcX: number,
540 srcY: number,
541 srcW: number,
542 srcH: number,
543 dstX: number,
544 dstY: number,
545 dstW: number,
546 dstH: number
547 ): void;
548 drawImage(): void {
549 // eslint-disable-next-line prefer-spread, prefer-rest-params
550 this._context.drawImage.apply(this._context, arguments);
551 }
552
553 drawFocusIfNeeded(element: Element): void {
554 this._context.drawFocusIfNeeded(element);
555 }
556
557 private _disposed = false;
558 private _state: Private.State;
559 private _context: CanvasRenderingContext2D;
560 }
561
562 /**
563 * The namespace for the module implementation details.
564 */
565 namespace Private {
566 /**
567 * The index of next valid pool object.
568 */
569 let pi = -1;
570
571 /**
572 * A state object allocation pool.
573 */
574 const pool: State[] = [];
575
576 /**
577 * An object which holds the state for a gc.
578 */
579 export class State {
580 /**
581 * Create a gc state object from a 2D canvas context.
582 */
583 static create(context: CanvasRenderingContext2D): State {
584 let state = pi < 0 ? new State() : pool[pi--];
585 state.next = null;
586 state.fillStyle = context.fillStyle;
587 state.font = context.font;
588 state.globalAlpha = context.globalAlpha;
589 state.globalCompositeOperation = context.globalCompositeOperation;
590 state.imageSmoothingEnabled = context.imageSmoothingEnabled;
591 state.lineCap = context.lineCap;
592 state.lineDashOffset = context.lineDashOffset;
593 state.lineJoin = context.lineJoin;
594 state.lineWidth = context.lineWidth;
595 state.miterLimit = context.miterLimit;
596 state.shadowBlur = context.shadowBlur;
597 state.shadowColor = context.shadowColor;
598 state.shadowOffsetX = context.shadowOffsetX;
599 state.shadowOffsetY = context.shadowOffsetY;
600 state.strokeStyle = context.strokeStyle;
601 state.textAlign = context.textAlign;
602 state.textBaseline = context.textBaseline;
603 return state;
604 }
605
606 /**
607 * Clone an existing gc state object and add it to the state stack.
608 */
609 static push(other: State): State {
610 let state = pi < 0 ? new State() : pool[pi--];
611 state.next = other;
612 state.fillStyle = other.fillStyle;
613 state.font = other.font;
614 state.globalAlpha = other.globalAlpha;
615 state.globalCompositeOperation = other.globalCompositeOperation;
616 state.imageSmoothingEnabled = other.imageSmoothingEnabled;
617 state.lineCap = other.lineCap;
618 state.lineDashOffset = other.lineDashOffset;
619 state.lineJoin = other.lineJoin;
620 state.lineWidth = other.lineWidth;
621 state.miterLimit = other.miterLimit;
622 state.shadowBlur = other.shadowBlur;
623 state.shadowColor = other.shadowColor;
624 state.shadowOffsetX = other.shadowOffsetX;
625 state.shadowOffsetY = other.shadowOffsetY;
626 state.strokeStyle = other.strokeStyle;
627 state.textAlign = other.textAlign;
628 state.textBaseline = other.textBaseline;
629 return state;
630 }
631
632 /**
633 * Pop the next state object and return the current to the pool
634 */
635 static pop(state: State): State {
636 state.fillStyle = '';
637 state.strokeStyle = '';
638 pool[++pi] = state;
639 return state.next!;
640 }
641
642 next: State | null;
643
644 fillStyle: string | CanvasGradient | CanvasPattern;
645 font: string;
646 globalAlpha: number;
647 globalCompositeOperation: string;
648 imageSmoothingEnabled: boolean;
649 lineCap: string;
650 lineDashOffset: number;
651 lineJoin: string;
652 lineWidth: number;
653 miterLimit: number;
654 shadowBlur: number;
655 shadowColor: string;
656 shadowOffsetX: number;
657 shadowOffsetY: number;
658 strokeStyle: string | CanvasGradient | CanvasPattern;
659 textAlign: string;
660 textBaseline: string;
661 }
662 }