-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
textrenderer.ts
981 lines (890 loc) · 27.1 KB
/
textrenderer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2019, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import { CellRenderer } from './cellrenderer';
import { GraphicsContext } from './graphicscontext';
/**
* A cell renderer which renders data values as text.
*/
export class TextRenderer extends CellRenderer {
/**
* Construct a new text renderer.
*
* @param options - The options for initializing the renderer.
*/
constructor(options: TextRenderer.IOptions = {}) {
super();
this.font = options.font || '12px sans-serif';
this.textColor = options.textColor || '#000000';
this.backgroundColor = options.backgroundColor || '';
this.verticalAlignment = options.verticalAlignment || 'center';
this.horizontalAlignment = options.horizontalAlignment || 'left';
this.format = options.format || TextRenderer.formatGeneric();
this.elideDirection = options.elideDirection || 'right';
this.wrapText = options.wrapText || false;
}
/**
* The CSS shorthand font for drawing the text.
*/
readonly font: CellRenderer.ConfigOption<string>;
/**
* The CSS color for drawing the text.
*/
readonly textColor: CellRenderer.ConfigOption<string>;
/**
* The CSS color for the cell background.
*/
readonly backgroundColor: CellRenderer.ConfigOption<string>;
/**
* The vertical alignment for the cell text.
*/
readonly verticalAlignment: CellRenderer.ConfigOption<TextRenderer.VerticalAlignment>;
/**
* The horizontal alignment for the cell text.
*/
readonly horizontalAlignment: CellRenderer.ConfigOption<TextRenderer.HorizontalAlignment>;
/**
* The format function for the cell value.
*/
readonly format: TextRenderer.FormatFunc;
/**
* Which side to draw the ellipsis.
*/
readonly elideDirection: CellRenderer.ConfigOption<TextRenderer.ElideDirection>;
/**
* Boolean flag for applying text wrapping.
*/
readonly wrapText: CellRenderer.ConfigOption<boolean>;
/**
* Paint the content for a cell.
*
* @param gc - The graphics context to use for drawing.
*
* @param config - The configuration data for the cell.
*/
paint(gc: GraphicsContext, config: CellRenderer.CellConfig): void {
this.drawBackground(gc, config);
this.drawText(gc, config);
}
/**
* Draw the background for the cell.
*
* @param gc - The graphics context to use for drawing.
*
* @param config - The configuration data for the cell.
*/
drawBackground(gc: GraphicsContext, config: CellRenderer.CellConfig): void {
// Resolve the background color for the cell.
let color = CellRenderer.resolveOption(this.backgroundColor, config);
// Bail if there is no background color to draw.
if (!color) {
return;
}
// Fill the cell with the background color.
gc.fillStyle = color;
gc.fillRect(config.x, config.y, config.width, config.height);
}
/**
* Draw the text for the cell.
*
* @param gc - The graphics context to use for drawing.
*
* @param config - The configuration data for the cell.
*/
drawText(gc: GraphicsContext, config: CellRenderer.CellConfig): void {
// Resolve the font for the cell.
let font = CellRenderer.resolveOption(this.font, config);
// Bail if there is no font to draw.
if (!font) {
return;
}
// Resolve the text color for the cell.
let color = CellRenderer.resolveOption(this.textColor, config);
// Bail if there is no text color to draw.
if (!color) {
return;
}
// Format the cell value to text.
let format = this.format;
let text = format(config);
// Bail if there is no text to draw.
if (!text) {
return;
}
// Resolve the vertical and horizontal alignment.
let vAlign = CellRenderer.resolveOption(this.verticalAlignment, config);
let hAlign = CellRenderer.resolveOption(this.horizontalAlignment, config);
// Resolve the elision direction
let elideDirection = CellRenderer.resolveOption(
this.elideDirection,
config
);
// Resolve the text wrapping flag
let wrapText = CellRenderer.resolveOption(this.wrapText, config);
// Compute the padded text box height for the specified alignment.
let boxHeight = config.height - (vAlign === 'center' ? 1 : 2);
// Bail if the text box has no effective size.
if (boxHeight <= 0) {
return;
}
// Compute the text height for the gc font.
let textHeight = TextRenderer.measureFontHeight(font);
// Set up the text position variables.
let textX: number;
let textY: number;
let boxWidth: number;
// Compute the Y position for the text.
switch (vAlign) {
case 'top':
textY = config.y + 2 + textHeight;
break;
case 'center':
textY = config.y + config.height / 2 + textHeight / 2;
break;
case 'bottom':
textY = config.y + config.height - 2;
break;
default:
throw 'unreachable';
}
// Compute the X position for the text.
switch (hAlign) {
case 'left':
textX = config.x + 8;
boxWidth = config.width - 14;
break;
case 'center':
textX = config.x + config.width / 2;
boxWidth = config.width;
break;
case 'right':
textX = config.x + config.width - 8;
boxWidth = config.width - 14;
break;
default:
throw 'unreachable';
}
// Clip the cell if the text is taller than the text box height.
if (textHeight > boxHeight) {
gc.beginPath();
gc.rect(config.x, config.y, config.width, config.height - 1);
gc.clip();
}
// Set the gc state.
gc.font = font;
gc.fillStyle = color;
gc.textAlign = hAlign;
gc.textBaseline = 'bottom';
// The current text width in pixels.
let textWidth = gc.measureText(text).width;
// Apply text wrapping if enabled.
if (wrapText && textWidth > boxWidth) {
// Make sure box clipping happens.
gc.beginPath();
gc.rect(config.x, config.y, config.width, config.height - 1);
gc.clip();
// Split column name to words based on
// whitespace preceding a word boundary.
// "Hello world" --> ["Hello ", "world"]
const wordsInColumn = text.split(/\s(?=\b)/);
// Y-coordinate offset for any additional lines
let curY = textY;
let textInCurrentLine = wordsInColumn.shift()!;
// Single word. Applying text wrap on word by splitting
// it into characters and fitting the maximum number of
// characters possible per line (box width).
if (wordsInColumn.length === 0) {
let curLineTextWidth = gc.measureText(textInCurrentLine).width;
while (curLineTextWidth > boxWidth && textInCurrentLine !== '') {
// Iterating from the end of the string until we find a
// substring (0,i) which has a width less than the box width.
for (let i = textInCurrentLine.length; i > 0; i--) {
const curSubString = textInCurrentLine.substring(0, i);
const curSubStringWidth = gc.measureText(curSubString).width;
if (curSubStringWidth < boxWidth || curSubString.length === 1) {
// Found a substring which has a width less than the current
// box width. Rendering that substring on the current line
// and setting the remainder of the parent string as the next
// string to iterate on for the next line.
const nextLineText = textInCurrentLine.substring(
i,
textInCurrentLine.length
);
textInCurrentLine = nextLineText;
curLineTextWidth = gc.measureText(textInCurrentLine).width;
gc.fillText(curSubString, textX, curY);
curY += textHeight;
// No need to continue iterating after we identified
// an index to break the string on.
break;
}
}
}
}
// Multiple words in column header. Fitting maximum
// number of words possible per line (box width).
else {
while (wordsInColumn.length !== 0) {
// Processing the next word in the queue.
const curWord = wordsInColumn.shift();
// Joining that word with the existing text for
// the current line.
const incrementedText = [textInCurrentLine, curWord].join(' ');
const incrementedTextWidth = gc.measureText(incrementedText).width;
if (incrementedTextWidth > boxWidth) {
// If the newly combined text has a width larger than
// the box width, we render the line before the current
// word was added. We set the current word as the next
// line.
gc.fillText(textInCurrentLine, textX, curY);
curY += textHeight;
textInCurrentLine = curWord!;
} else {
// The combined text hasd a width less than the box width. We
// set the the current line text to be the new combined text.
textInCurrentLine = incrementedText;
}
}
}
gc.fillText(textInCurrentLine!, textX, curY);
// Terminating the call here as we don't want
// to apply text eliding when wrapping is active.
return;
}
// Elide text that is too long
let elide = '\u2026';
// Compute elided text
if (elideDirection === 'right') {
while (textWidth > boxWidth && text.length > 1) {
if (text.length > 4 && textWidth >= 2 * boxWidth) {
// If text width is substantially bigger, take half the string
text = text.substring(0, text.length / 2 + 1) + elide;
} else {
// Otherwise incrementally remove the last character
text = text.substring(0, text.length - 2) + elide;
}
textWidth = gc.measureText(text).width;
}
} else {
while (textWidth > boxWidth && text.length > 1) {
if (text.length > 4 && textWidth >= 2 * boxWidth) {
// If text width is substantially bigger, take half the string
text = elide + text.substring(text.length / 2);
} else {
// Otherwise incrementally remove the last character
text = elide + text.substring(2);
}
textWidth = gc.measureText(text).width;
}
}
// Draw the text for the cell.
gc.fillText(text, textX, textY);
}
}
/**
* The namespace for the `TextRenderer` class statics.
*/
export namespace TextRenderer {
/**
* A type alias for the supported vertical alignment modes.
*/
export type VerticalAlignment = 'top' | 'center' | 'bottom';
/**
* A type alias for the supported horizontal alignment modes.
*/
export type HorizontalAlignment = 'left' | 'center' | 'right';
/**
* A type alias for the supported ellipsis sides.
*/
export type ElideDirection = 'left' | 'right';
/**
* An options object for initializing a text renderer.
*/
export interface IOptions {
/**
* The font for drawing the cell text.
*
* The default is `'12px sans-serif'`.
*/
font?: CellRenderer.ConfigOption<string>;
/**
* The color for the drawing the cell text.
*
* The default `'#000000'`.
*/
textColor?: CellRenderer.ConfigOption<string>;
/**
* The background color for the cells.
*
* The default is `''`.
*/
backgroundColor?: CellRenderer.ConfigOption<string>;
/**
* The vertical alignment for the cell text.
*
* The default is `'center'`.
*/
verticalAlignment?: CellRenderer.ConfigOption<VerticalAlignment>;
/**
* The horizontal alignment for the cell text.
*
* The default is `'left'`.
*/
horizontalAlignment?: CellRenderer.ConfigOption<HorizontalAlignment>;
/**
* The format function for the renderer.
*
* The default is `TextRenderer.formatGeneric()`.
*/
format?: FormatFunc;
/**
* The ellipsis direction for the cell text.
*
* The default is `'right'`.
*/
elideDirection?: CellRenderer.ConfigOption<ElideDirection>;
/**
* Whether or not to apply text wrapping.
*
* The default is `'false'`.
*/
wrapText?: CellRenderer.ConfigOption<boolean>;
}
/**
* A type alias for a format function.
*/
export type FormatFunc = CellRenderer.ConfigFunc<string>;
/**
* Create a generic text format function.
*
* @param options - The options for creating the format function.
*
* @returns A new generic text format function.
*
* #### Notes
* This formatter uses the builtin `String()` to coerce any value
* to a string.
*/
export function formatGeneric(
options: formatGeneric.IOptions = {}
): FormatFunc {
let missing = options.missing || '';
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
return String(value);
};
}
/**
* The namespace for the `formatGeneric` function statics.
*/
export namespace formatGeneric {
/**
* The options for creating a generic format function.
*/
export interface IOptions {
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create a fixed decimal format function.
*
* @param options - The options for creating the format function.
*
* @returns A new fixed decimal format function.
*
* #### Notes
* This formatter uses the builtin `Number()` and `toFixed()` to
* coerce values.
*
* The `formatIntlNumber()` formatter is more flexible, but slower.
*/
export function formatFixed(options: formatFixed.IOptions = {}): FormatFunc {
let digits = options.digits;
let missing = options.missing || '';
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
return Number(value).toFixed(digits);
};
}
/**
* The namespace for the `formatFixed` function statics.
*/
export namespace formatFixed {
/**
* The options for creating a fixed format function.
*/
export interface IOptions {
/**
* The number of digits to include after the decimal point.
*
* The default is determined by the user agent.
*/
digits?: number;
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create a significant figure format function.
*
* @param options - The options for creating the format function.
*
* @returns A new significant figure format function.
*
* #### Notes
* This formatter uses the builtin `Number()` and `toPrecision()`
* to coerce values.
*
* The `formatIntlNumber()` formatter is more flexible, but slower.
*/
export function formatPrecision(
options: formatPrecision.IOptions = {}
): FormatFunc {
let digits = options.digits;
let missing = options.missing || '';
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
return Number(value).toPrecision(digits);
};
}
/**
* The namespace for the `formatPrecision` function statics.
*/
export namespace formatPrecision {
/**
* The options for creating a precision format function.
*/
export interface IOptions {
/**
* The number of significant figures to include in the value.
*
* The default is determined by the user agent.
*/
digits?: number;
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create a scientific notation format function.
*
* @param options - The options for creating the format function.
*
* @returns A new scientific notation format function.
*
* #### Notes
* This formatter uses the builtin `Number()` and `toExponential()`
* to coerce values.
*
* The `formatIntlNumber()` formatter is more flexible, but slower.
*/
export function formatExponential(
options: formatExponential.IOptions = {}
): FormatFunc {
let digits = options.digits;
let missing = options.missing || '';
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
return Number(value).toExponential(digits);
};
}
/**
* The namespace for the `formatExponential` function statics.
*/
export namespace formatExponential {
/**
* The options for creating an exponential format function.
*/
export interface IOptions {
/**
* The number of digits to include after the decimal point.
*
* The default is determined by the user agent.
*/
digits?: number;
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create an international number format function.
*
* @param options - The options for creating the format function.
*
* @returns A new international number format function.
*
* #### Notes
* This formatter uses the builtin `Intl.NumberFormat` object to
* coerce values.
*
* This is the most flexible (but slowest) number formatter.
*/
export function formatIntlNumber(
options: formatIntlNumber.IOptions = {}
): FormatFunc {
let missing = options.missing || '';
let nft = new Intl.NumberFormat(options.locales, options.options);
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
return nft.format(value);
};
}
/**
* The namespace for the `formatIntlNumber` function statics.
*/
export namespace formatIntlNumber {
/**
* The options for creating an intl number format function.
*/
export interface IOptions {
/**
* The locales to pass to the `Intl.NumberFormat` constructor.
*
* The default is determined by the user agent.
*/
locales?: string | string[];
/**
* The options to pass to the `Intl.NumberFormat` constructor.
*
* The default is determined by the user agent.
*/
options?: Intl.NumberFormatOptions;
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create a date format function.
*
* @param options - The options for creating the format function.
*
* @returns A new date format function.
*
* #### Notes
* This formatter uses `Date.toDateString()` to format the values.
*
* If a value is not a `Date` object, `new Date(value)` is used to
* coerce the value to a date.
*
* The `formatIntlDateTime()` formatter is more flexible, but slower.
*/
export function formatDate(options: formatDate.IOptions = {}): FormatFunc {
let missing = options.missing || '';
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
if (value instanceof Date) {
return value.toDateString();
}
return new Date(value).toDateString();
};
}
/**
* The namespace for the `formatDate` function statics.
*/
export namespace formatDate {
/**
* The options for creating a date format function.
*/
export interface IOptions {
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create a time format function.
*
* @param options - The options for creating the format function.
*
* @returns A new time format function.
*
* #### Notes
* This formatter uses `Date.toTimeString()` to format the values.
*
* If a value is not a `Date` object, `new Date(value)` is used to
* coerce the value to a date.
*
* The `formatIntlDateTime()` formatter is more flexible, but slower.
*/
export function formatTime(options: formatTime.IOptions = {}): FormatFunc {
let missing = options.missing || '';
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
if (value instanceof Date) {
return value.toTimeString();
}
return new Date(value).toTimeString();
};
}
/**
* The namespace for the `formatTime` function statics.
*/
export namespace formatTime {
/**
* The options for creating a time format function.
*/
export interface IOptions {
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create an ISO datetime format function.
*
* @param options - The options for creating the format function.
*
* @returns A new ISO datetime format function.
*
* #### Notes
* This formatter uses `Date.toISOString()` to format the values.
*
* If a value is not a `Date` object, `new Date(value)` is used to
* coerce the value to a date.
*
* The `formatIntlDateTime()` formatter is more flexible, but slower.
*/
export function formatISODateTime(
options: formatISODateTime.IOptions = {}
): FormatFunc {
let missing = options.missing || '';
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
if (value instanceof Date) {
return value.toISOString();
}
return new Date(value).toISOString();
};
}
/**
* The namespace for the `formatISODateTime` function statics.
*/
export namespace formatISODateTime {
/**
* The options for creating an ISO datetime format function.
*/
export interface IOptions {
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create a UTC datetime format function.
*
* @param options - The options for creating the format function.
*
* @returns A new UTC datetime format function.
*
* #### Notes
* This formatter uses `Date.toUTCString()` to format the values.
*
* If a value is not a `Date` object, `new Date(value)` is used to
* coerce the value to a date.
*
* The `formatIntlDateTime()` formatter is more flexible, but slower.
*/
export function formatUTCDateTime(
options: formatUTCDateTime.IOptions = {}
): FormatFunc {
let missing = options.missing || '';
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
if (value instanceof Date) {
return value.toUTCString();
}
return new Date(value).toUTCString();
};
}
/**
* The namespace for the `formatUTCDateTime` function statics.
*/
export namespace formatUTCDateTime {
/**
* The options for creating a UTC datetime format function.
*/
export interface IOptions {
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Create an international datetime format function.
*
* @param options - The options for creating the format function.
*
* @returns A new international datetime format function.
*
* #### Notes
* This formatter uses the builtin `Intl.DateTimeFormat` object to
* coerce values.
*
* This is the most flexible (but slowest) datetime formatter.
*/
export function formatIntlDateTime(
options: formatIntlDateTime.IOptions = {}
): FormatFunc {
let missing = options.missing || '';
let dtf = new Intl.DateTimeFormat(options.locales, options.options);
return ({ value }) => {
if (value === null || value === undefined) {
return missing;
}
return dtf.format(value);
};
}
/**
* The namespace for the `formatIntlDateTime` function statics.
*/
export namespace formatIntlDateTime {
/**
* The options for creating an intl datetime format function.
*/
export interface IOptions {
/**
* The locales to pass to the `Intl.DateTimeFormat` constructor.
*
* The default is determined by the user agent.
*/
locales?: string | string[];
/**
* The options to pass to the `Intl.DateTimeFormat` constructor.
*
* The default is determined by the user agent.
*/
options?: Intl.DateTimeFormatOptions;
/**
* The text to use for a `null` or `undefined` data value.
*
* The default is `''`.
*/
missing?: string;
}
}
/**
* Measure the height of a font.
*
* @param font - The CSS font string of interest.
*
* @returns The height of the font bounding box.
*
* #### Notes
* This function uses a temporary DOM node to measure the text box
* height for the specified font. The first call for a given font
* will incur a DOM reflow, but the return value is cached, so any
* subsequent call for the same font will return the cached value.
*/
export function measureFontHeight(font: string): number {
// Look up the cached font height.
let height = Private.fontHeightCache[font];
// Return the cached font height if it exists.
if (height !== undefined) {
return height;
}
// Normalize the font.
Private.fontMeasurementGC.font = font;
let normFont = Private.fontMeasurementGC.font;
// Set the font on the measurement node.
Private.fontMeasurementNode.style.font = normFont;
// Add the measurement node to the document.
document.body.appendChild(Private.fontMeasurementNode);
// Measure the node height.
height = Private.fontMeasurementNode.offsetHeight;
// Remove the measurement node from the document.
document.body.removeChild(Private.fontMeasurementNode);
// Cache the measured height for the font and norm font.
Private.fontHeightCache[font] = height;
Private.fontHeightCache[normFont] = height;
// Return the measured height.
return height;
}
}
/**
* The namespace for the module implementation details.
*/
namespace Private {
/**
* A cache of measured font heights.
*/
export const fontHeightCache: { [font: string]: number } =
Object.create(null);
/**
* The DOM node used for font height measurement.
*/
export const fontMeasurementNode = (() => {
let node = document.createElement('div');
node.style.position = 'absolute';
node.style.top = '-99999px';
node.style.left = '-99999px';
node.style.visibility = 'hidden';
node.textContent = 'M';
return node;
})();
/**
* The GC used for font measurement.
*/
export const fontMeasurementGC = (() => {
let canvas = document.createElement('canvas');
canvas.width = 0;
canvas.height = 0;
return canvas.getContext('2d')!;
})();
}