@kvndy/glyph-metrics 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +99 -2
- package/dist/glyph-metrics.d.ts +2 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.js +125 -0
- package/dist/text-layout.d.ts +4 -0
- package/dist/types.d.ts +24 -0
- package/package.json +30 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Doughty
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,100 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kvndy/glyph-metrics
|
|
2
2
|
|
|
3
|
-
|
|
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/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
function readGlyphMetrics(glyph) {
|
|
2
|
+
const { fontWeight, fontSize, fontFamily, lineHeight } = getComputedStyle(glyph);
|
|
3
|
+
return {
|
|
4
|
+
weight: fontWeight,
|
|
5
|
+
size: fontSize,
|
|
6
|
+
family: fontFamily,
|
|
7
|
+
full: `${fontWeight} ${fontSize} ${fontFamily}`,
|
|
8
|
+
height: parseFloat(lineHeight),
|
|
9
|
+
char: glyph.getBoundingClientRect().width,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function findLineAtOffset(lines, offset) {
|
|
14
|
+
let first = 0;
|
|
15
|
+
let last = lines.length - 1;
|
|
16
|
+
while (first < last) {
|
|
17
|
+
const middle = Math.ceil((first + last) / 2);
|
|
18
|
+
if (lines[middle].charStart <= offset)
|
|
19
|
+
first = middle;
|
|
20
|
+
else
|
|
21
|
+
last = middle - 1;
|
|
22
|
+
}
|
|
23
|
+
return lines[first];
|
|
24
|
+
}
|
|
25
|
+
function calculateVisibleLineRange(viewport, lineCount, lineHeight, paddingTop) {
|
|
26
|
+
if (lineCount === 0) {
|
|
27
|
+
return {
|
|
28
|
+
firstLine: 0,
|
|
29
|
+
lastLine: 0
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const scrollTop = viewport.scrollTop;
|
|
33
|
+
const actualTop = scrollTop - paddingTop;
|
|
34
|
+
const clientHeight = viewport.clientHeight;
|
|
35
|
+
const scrollBottom = scrollTop + clientHeight;
|
|
36
|
+
const firstIndex = Math.min(lineCount - 1, Math.max(0, Math.floor(actualTop / lineHeight)));
|
|
37
|
+
const lastIndex = Math.min(// inclusive means first and last line are visible, which is what you'd expect from firstLine and lastLine
|
|
38
|
+
lineCount - 1, Math.max(firstIndex, Math.floor(scrollBottom / lineHeight)) // INCLUSIVE
|
|
39
|
+
);
|
|
40
|
+
const result = {
|
|
41
|
+
firstLine: firstIndex,
|
|
42
|
+
lastLine: lastIndex // INCLUSIVE
|
|
43
|
+
};
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
function performGlyphLayout(measure, textLength, { char: charWidth, height: lineHeight }) {
|
|
47
|
+
const range = measure.ownerDocument.createRange();
|
|
48
|
+
const lines = [];
|
|
49
|
+
let documentOffset = 0;
|
|
50
|
+
let origin;
|
|
51
|
+
const rowAtTop = (top) => Math.round((top - origin) / lineHeight);
|
|
52
|
+
for (const node of measure.childNodes) {
|
|
53
|
+
if (!(node instanceof Text))
|
|
54
|
+
continue;
|
|
55
|
+
range.selectNodeContents(node);
|
|
56
|
+
const rects = range.getClientRects();
|
|
57
|
+
if (rects.length)
|
|
58
|
+
origin ??= rects[0].top;
|
|
59
|
+
const rowWidths = new Map();
|
|
60
|
+
for (const rect of rects) {
|
|
61
|
+
const row = rowAtTop(rect.top);
|
|
62
|
+
rowWidths.set(row, (rowWidths.get(row) ?? 0) + rect.width);
|
|
63
|
+
}
|
|
64
|
+
const rowAtOffset = (offset) => {
|
|
65
|
+
range.setStart(node, offset);
|
|
66
|
+
range.setEnd(node, offset + 1);
|
|
67
|
+
const rect = range.getClientRects()[0];
|
|
68
|
+
return rect ? rowAtTop(rect.top) : Number.POSITIVE_INFINITY;
|
|
69
|
+
};
|
|
70
|
+
const end = Math.min(node.length, textLength - documentOffset);
|
|
71
|
+
let start = 0;
|
|
72
|
+
while (start < end) {
|
|
73
|
+
const row = rowAtOffset(start);
|
|
74
|
+
if (!Number.isFinite(row))
|
|
75
|
+
break;
|
|
76
|
+
const width = rowWidths.get(row) ?? 0;
|
|
77
|
+
const estimate = Math.min(end, start + Math.max(1, Math.round(width / charWidth)));
|
|
78
|
+
const rowEnd = findRowEnd(start, end, estimate, offset => rowAtOffset(offset) <= row);
|
|
79
|
+
lines.push({
|
|
80
|
+
charStart: documentOffset + start,
|
|
81
|
+
charEnd: documentOffset + rowEnd,
|
|
82
|
+
y: row * lineHeight,
|
|
83
|
+
debug_width: width,
|
|
84
|
+
});
|
|
85
|
+
start = rowEnd;
|
|
86
|
+
}
|
|
87
|
+
// The appended newline supplies a measurable blank row after a final
|
|
88
|
+
// document newline. It also supplies the caret row for an empty document.
|
|
89
|
+
if (end < node.length) {
|
|
90
|
+
if (end === 0 && node.data[0] === "\n" && rects.length) {
|
|
91
|
+
lines.push({ charStart: textLength, charEnd: textLength, y: rowAtTop(rects[0].top) * lineHeight, debug_width: 0 });
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
documentOffset += node.length;
|
|
96
|
+
}
|
|
97
|
+
return lines;
|
|
98
|
+
}
|
|
99
|
+
function findRowEnd(start, end, estimate, belongsToRow) {
|
|
100
|
+
let low = start + 1;
|
|
101
|
+
let high = estimate;
|
|
102
|
+
// Hanging spaces can contribute a partial advance, and newlines can
|
|
103
|
+
// contribute none. The estimated offset still needs to match the DOM row.
|
|
104
|
+
if (estimate < end && belongsToRow(estimate)) {
|
|
105
|
+
low = estimate + 1;
|
|
106
|
+
high = low;
|
|
107
|
+
for (let distance = 1; high < end && belongsToRow(high); distance *= 2) {
|
|
108
|
+
low = high + 1;
|
|
109
|
+
high = Math.min(end, high + distance);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (estimate === low || belongsToRow(estimate - 1)) {
|
|
113
|
+
return estimate;
|
|
114
|
+
}
|
|
115
|
+
while (low < high) {
|
|
116
|
+
const middle = Math.floor((low + high) / 2);
|
|
117
|
+
if (belongsToRow(middle))
|
|
118
|
+
low = middle + 1;
|
|
119
|
+
else
|
|
120
|
+
high = middle;
|
|
121
|
+
}
|
|
122
|
+
return low;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export { calculateVisibleLineRange, findLineAtOffset, performGlyphLayout, readGlyphMetrics };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { LineDescriptor, LineRange, GlyphMetricsFontInfo } from "./types.js";
|
|
2
|
+
export declare function findLineAtOffset(lines: LineDescriptor[], offset: number): LineDescriptor;
|
|
3
|
+
export declare function calculateVisibleLineRange(viewport: HTMLElement, lineCount: number, lineHeight: number, paddingTop: number): LineRange;
|
|
4
|
+
export declare function performGlyphLayout(measure: HTMLElement, textLength: number, { char: charWidth, height: lineHeight }: GlyphMetricsFontInfo): LineDescriptor[];
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface GlyphMetricsFontInfo {
|
|
2
|
+
weight: string;
|
|
3
|
+
size: string;
|
|
4
|
+
family: string;
|
|
5
|
+
full: string;
|
|
6
|
+
height: number;
|
|
7
|
+
char: number;
|
|
8
|
+
}
|
|
9
|
+
export interface LineDescriptor {
|
|
10
|
+
charStart: number;
|
|
11
|
+
charEnd: number;
|
|
12
|
+
y: number;
|
|
13
|
+
debug_width: number;
|
|
14
|
+
}
|
|
15
|
+
export interface VisibleRange {
|
|
16
|
+
firstLine: LineDescriptor;
|
|
17
|
+
firstIndex: number;
|
|
18
|
+
lastLine: LineDescriptor;
|
|
19
|
+
lastIndex: number;
|
|
20
|
+
}
|
|
21
|
+
export interface LineRange {
|
|
22
|
+
firstLine: number;
|
|
23
|
+
lastLine: number;
|
|
24
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@kvndy/glyph-metrics",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "DOM monospace glyph measurement",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Kevin Doughty",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://gitlab.com/kevindoughty/attributed-string.git",
|
|
11
|
+
"directory": "glyph-metrics"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"main": "./dist/main.js",
|
|
17
|
+
"types": "./dist/main.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/main.d.ts",
|
|
21
|
+
"import": "./dist/main.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc && rollup -c"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"rollup": "^4.0.0",
|
|
29
|
+
"typescript": "^7.0.2"
|
|
30
|
+
}
|
|
31
|
+
}
|