Repository navigation
Expand file tree
/
Copy pathbdffont.ts
More file actions
238 lines (216 loc) · 7.56 KB
/
Copy pathbdffont.ts
File metadata and controls
238 lines (216 loc) · 7.56 KB
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
import { knownFolders } from "@nativescript/core";
import { getStringSetting, onSettingsStoreChanged } from "~/native/settings-store";
export type Glyph = {
encoding: number;
dwidthX: number;
bbxWidth: number;
bbxHeight: number;
bbxX: number;
bbxY: number;
bitmapRows: number[];
};
export class BdfFont {
readonly ascent: number;
readonly descent: number;
readonly lineHeight: number;
readonly defaultChar: number;
/**
* Font consulted for code points this font has no glyph for. Used so a
* partial-coverage face (e.g. TerminusV, which only ships ISO-8859-1) can
* borrow glyphs from a full-coverage face of the same pixel height. The
* fallback's glyphs are drawn with *this* font's ascent, so the two must
* share a baseline (ascent/descent) for the result to line up.
*/
private readonly fallback?: BdfFont;
private readonly glyphs: Map<number, Glyph>;
constructor(opts: {
ascent: number;
descent: number;
defaultChar: number;
glyphs: Map<number, Glyph>;
fallback?: BdfFont;
}) {
this.ascent = opts.ascent;
this.descent = opts.descent;
this.lineHeight = opts.ascent + opts.descent;
this.defaultChar = opts.defaultChar;
this.glyphs = opts.glyphs;
this.fallback = opts.fallback;
}
static parse(source: string, opts: { fallback?: BdfFont; minEncoding?: number } = {}): BdfFont {
const minEncoding = opts.minEncoding ?? 0;
const lines = source.replace(/\r/g, "").split("\n");
let ascent = 10;
let descent = 2;
let defaultChar = 63;
const glyphs = new Map<number, Glyph>();
for (let i = 0; i < lines.length; i++) {
const line = lines[i]!;
if (line.startsWith("FONT_ASCENT ")) {
ascent = parseInt(line.slice("FONT_ASCENT ".length), 10);
continue;
}
if (line.startsWith("FONT_DESCENT ")) {
descent = parseInt(line.slice("FONT_DESCENT ".length), 10);
continue;
}
if (line.startsWith("DEFAULT_CHAR ")) {
defaultChar = parseInt(line.slice("DEFAULT_CHAR ".length), 10);
continue;
}
if (!line.startsWith("STARTCHAR ")) {
continue;
}
let encoding = -1;
let dwidthX = 0;
let bbxWidth = 0;
let bbxHeight = 0;
let bbxX = 0;
let bbxY = 0;
const bitmapRows: number[] = [];
for (i = i + 1; i < lines.length; i++) {
const inner = lines[i]!;
if (inner.startsWith("ENCODING ")) {
encoding = parseInt(inner.slice("ENCODING ".length), 10);
continue;
}
if (inner.startsWith("DWIDTH ")) {
dwidthX = parseInt(inner.slice("DWIDTH ".length).split(/\s+/)[0]!, 10);
continue;
}
if (inner.startsWith("BBX ")) {
const [w, h, x, y] = inner
.slice("BBX ".length)
.split(/\s+/)
.map((value) => parseInt(value, 10));
bbxWidth = w!;
bbxHeight = h!;
bbxX = x!;
bbxY = y!;
continue;
}
if (inner === "BITMAP") {
for (let row = 0; row < bbxHeight && i + 1 < lines.length; row++) {
const bitmapLine = lines[++i]!;
bitmapRows.push(parseInt(bitmapLine || "0", 16) || 0);
}
continue;
}
if (inner === "ENDCHAR") {
if (encoding >= minEncoding) {
glyphs.set(encoding, {
encoding,
dwidthX: dwidthX || bbxWidth,
bbxWidth,
bbxHeight,
bbxX,
bbxY,
bitmapRows,
});
}
break;
}
}
}
return new BdfFont({ ascent, descent, defaultChar, glyphs, fallback: opts.fallback });
}
/** Exact lookup for a single code point, with no default-char substitution. */
private getExactGlyph(codePoint: number): Glyph | undefined {
return this.glyphs.get(codePoint) ?? this.fallback?.getExactGlyph(codePoint);
}
getGlyph(codePoint: number): Glyph | undefined {
return (
this.getExactGlyph(codePoint) ??
this.glyphs.get(this.defaultChar) ??
this.glyphs.get(63) ??
this.fallback?.getGlyph(codePoint)
);
}
hasGlyph(codePoint: number): boolean {
return this.glyphs.has(codePoint) || (this.fallback?.hasGlyph(codePoint) ?? false);
}
measureText(text: string): number {
let width = 0;
for (const char of text) {
width += this.getGlyph(char.codePointAt(0) ?? 32)?.dwidthX ?? 0;
}
return width;
}
}
const embeddedFonts = {
"terminus12": "fonts/terminus/ter-u12n.bdf",
"terminus16": "fonts/terminus/ter-u16n.bdf",
"terminus24": "fonts/terminus/ter-u24n.bdf",
"terminus32": "fonts/terminus/ter-u32n.bdf",
// TerminusV is a proportional (variable-width) Terminus derivative that only
// covers ISO-8859-1. At 12px it shares Terminus's baseline, so we fall back
// to Terminus 12 for any code point outside Latin-1. minEncoding 32 drops
// TerminusV's private low-encoding drawing glyphs so control code points fall
// through to Terminus too.
"terminusv12": "fonts/terminusv/terv12n.bdf",
} as const;
type EmbeddedFontName = keyof typeof embeddedFonts;
const fontFallbacks: Partial<Record<EmbeddedFontName, EmbeddedFontName>> = {
terminusv12: "terminus12",
};
const fontMinEncoding: Partial<Record<EmbeddedFontName, number>> = {
terminusv12: 32,
};
const cachedEmbeddedFonts = new Map<EmbeddedFontName, BdfFont>();
function loadEmbeddedFont(name: EmbeddedFontName): BdfFont {
const cached = cachedEmbeddedFonts.get(name);
if (cached) {
return cached;
}
const file = knownFolders.currentApp().getFile(embeddedFonts[name]);
const text = file.readTextSync();
const fallbackName = fontFallbacks[name];
const font = BdfFont.parse(text, {
fallback: fallbackName ? loadEmbeddedFont(fallbackName) : undefined,
minEncoding: fontMinEncoding[name],
});
cachedEmbeddedFonts.set(name, font);
return font;
}
export function getFont(font: EmbeddedFontName): BdfFont {
return loadEmbeddedFont(font);
}
// User-selectable UI typeface. Only the small (12px) font has an alternative,
// since TerminusV ships no larger sizes; medium/large stay Terminus. The
// storage key and values are re-used by the settings UI in dashboard-settings.
export const UI_FONT_SETTING_KEY = "display.uiFont";
export const UI_FONT_VALUES = ["terminus", "terminusv"] as const;
export type UiFontChoice = (typeof UI_FONT_VALUES)[number];
const SMALL_FONT_FOR_CHOICE: Record<UiFontChoice, EmbeddedFontName> = {
terminus: "terminus12",
terminusv: "terminusv12",
};
// Reading the setting hits a JNI bridge, and getDefaultSmallFont() is called
// once per menu item per paint, so cache the resolved choice and invalidate it
// when the setting changes (in any isolate).
let cachedSmallFontChoice: UiFontChoice | null = null;
let smallFontListenerInstalled = false;
function currentSmallFontName(): EmbeddedFontName {
if (!smallFontListenerInstalled) {
onSettingsStoreChanged((key) => {
if (key === UI_FONT_SETTING_KEY) cachedSmallFontChoice = null;
});
smallFontListenerInstalled = true;
}
if (cachedSmallFontChoice === null) {
const raw = getStringSetting(UI_FONT_SETTING_KEY, "terminus");
cachedSmallFontChoice = (UI_FONT_VALUES as readonly string[]).includes(raw)
? (raw as UiFontChoice)
: "terminus";
}
return SMALL_FONT_FOR_CHOICE[cachedSmallFontChoice];
}
export function getDefaultSmallFont(): BdfFont {
return getFont(currentSmallFontName());
}
export function getDefaultMediumFont(): BdfFont {
return getFont("terminus16");
}
export function getDefaultLargeFont(): BdfFont {
return getFont("terminus24");
}