@kvndy/glyph-metrics 0.0.1 → 0.0.2

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.
Files changed (2) hide show
  1. package/README.md +100 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # @kvndy/glyph-metrics
2
+
3
+ ## Usage
4
+
5
+ ```sh
6
+ npm install --save-exact @kvndy/glyph-metrics@0.0.1
7
+ ```
8
+
9
+ ```ts
10
+ import {
11
+ calculateVisibleLineRange,
12
+ findLineAtOffset,
13
+ performGlyphLayout,
14
+ readGlyphMetrics,
15
+ } from "@kvndy/glyph-metrics";
16
+
17
+ const text = "A soft-wrapped line of text\nAnother line";
18
+ const viewport = document.createElement("div");
19
+ Object.assign(viewport.style, {
20
+ width: "240px",
21
+ height: "96px",
22
+ overflow: "auto",
23
+ font: "16px/24px monospace",
24
+ fontVariantLigatures: "none",
25
+ });
26
+
27
+ const glyph = document.createElement("span");
28
+ glyph.textContent = "M";
29
+ Object.assign(glyph.style, { position: "absolute", visibility: "hidden" });
30
+
31
+ const measure = document.createElement("div");
32
+ Object.assign(measure.style, { whiteSpace: "pre-wrap", overflowWrap: "break-word" });
33
+ for (const line of (text + "\n ").match(/[^\n]*\n|[^\n]+$/g) ?? []) {
34
+ measure.appendChild(document.createTextNode(line));
35
+ }
36
+
37
+ viewport.append(glyph, measure);
38
+ document.body.append(viewport);
39
+
40
+ const metrics = readGlyphMetrics(glyph);
41
+ const lines = performGlyphLayout(measure, text.length, metrics);
42
+ const visible = calculateVisibleLineRange(viewport, lines.length, metrics.height, 0);
43
+ const firstLine = lines.length ? findLineAtOffset(lines, 0) : undefined;
44
+
45
+ // Call viewport.remove() when you no longer need the measurement elements.
46
+ ```
47
+
48
+ Give the probe and measurement div matching monospace typography and a fixed
49
+ line height. Measure after attaching both to the DOM. Use one direct text node
50
+ per logical line, including its newline. The example appends `"\n "` to make a
51
+ trailing blank row measurable and passes the original document length.
52
+
53
+ ## Type definitions
54
+
55
+ ```ts
56
+ export interface GlyphMetricsFontInfo {
57
+ weight: string;
58
+ size: string;
59
+ family: string;
60
+ full: string;
61
+ height: number;
62
+ char: number;
63
+ }
64
+
65
+ export interface LineDescriptor {
66
+ charStart: number;
67
+ charEnd: number;
68
+ y: number;
69
+ debug_width: number;
70
+ }
71
+
72
+ export interface LineRange {
73
+ firstLine: number;
74
+ lastLine: number;
75
+ }
76
+
77
+ export declare function readGlyphMetrics(glyph: HTMLElement): GlyphMetricsFontInfo;
78
+
79
+ export declare function performGlyphLayout(
80
+ measure: HTMLElement,
81
+ textLength: number,
82
+ fontInfo: GlyphMetricsFontInfo,
83
+ ): LineDescriptor[];
84
+
85
+ export declare function findLineAtOffset(
86
+ lines: LineDescriptor[],
87
+ offset: number,
88
+ ): LineDescriptor;
89
+
90
+ export declare function calculateVisibleLineRange(
91
+ viewport: HTMLElement,
92
+ lineCount: number,
93
+ lineHeight: number,
94
+ paddingTop: number,
95
+ ): LineRange;
96
+ ```
97
+
98
+ Character offsets are UTF-16 code units and `charEnd` is exclusive.
99
+ `LineRange` uses inclusive row indices. Call `findLineAtOffset` with at least
100
+ one row.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kvndy/glyph-metrics",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "DOM monospace glyph measurement",
5
5
  "license": "MIT",
6
6
  "author": "Kevin Doughty",