-
-
Notifications
You must be signed in to change notification settings - Fork 754
/
Copy pathglyph_manager.ts
198 lines (168 loc) · 6.78 KB
/
glyph_manager.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
import {loadGlyphRange} from '../style/load_glyph_range';
import TinySDF from '@mapbox/tiny-sdf';
import {AlphaImage} from '../util/image';
import type {StyleGlyph} from '../style/style_glyph';
import type {RequestManager} from '../util/request_manager';
import type {GetGlyphsResponse} from '../util/actor_messages';
type Entry = {
// null means we've requested the range, but the glyph wasn't included in the result.
glyphs: {
[id: number]: StyleGlyph | null;
};
requests: {
[range: number]: Promise<{[_: number]: StyleGlyph | null}>;
};
ranges: {
[range: number]: boolean | null;
};
tinySDF?: TinySDF;
};
export class GlyphManager {
requestManager: RequestManager;
localIdeographFontFamily: string | false;
entries: {[stack: string]: Entry};
url: string;
// exposed as statics to enable stubbing in unit tests
static loadGlyphRange = loadGlyphRange;
static TinySDF = TinySDF;
constructor(requestManager: RequestManager, localIdeographFontFamily?: string | false) {
this.requestManager = requestManager;
this.localIdeographFontFamily = localIdeographFontFamily;
this.entries = {};
}
setURL(url?: string | null) {
this.url = url;
}
async getGlyphs(glyphs: {[stack: string]: Array<number>}): Promise<GetGlyphsResponse> {
const glyphsPromises: Promise<{stack: string; id: number; glyph: StyleGlyph}>[] = [];
for (const stack in glyphs) {
for (const id of glyphs[stack]) {
glyphsPromises.push(this._getAndCacheGlyphsPromise(stack, id));
}
}
const updatedGlyphs = await Promise.all(glyphsPromises);
const result: GetGlyphsResponse = {};
for (const {stack, id, glyph} of updatedGlyphs) {
if (!result[stack]) {
result[stack] = {};
}
// Clone the glyph so that our own copy of its ArrayBuffer doesn't get transferred.
result[stack][id] = glyph && {
id: glyph.id,
bitmap: glyph.bitmap.clone(),
metrics: glyph.metrics
};
}
return result;
}
async _getAndCacheGlyphsPromise(stack: string, id: number): Promise<{stack: string; id: number; glyph: StyleGlyph}> {
let entry = this.entries[stack];
if (!entry) {
entry = this.entries[stack] = {
glyphs: {},
requests: {},
ranges: {}
};
}
let glyph = entry.glyphs[id];
if (glyph !== undefined) {
return {stack, id, glyph};
}
glyph = this._tinySDF(entry, stack, id);
if (glyph) {
entry.glyphs[id] = glyph;
return {stack, id, glyph};
}
const range = Math.floor(id / 256);
if (range * 256 > 65535) {
throw new Error('glyphs > 65535 not supported');
}
if (entry.ranges[range]) {
return {stack, id, glyph};
}
if (!this.url) {
throw new Error('glyphsUrl is not set');
}
if (!entry.requests[range]) {
const promise = GlyphManager.loadGlyphRange(stack, range, this.url, this.requestManager);
entry.requests[range] = promise;
}
const response = await entry.requests[range];
for (const id in response) {
if (!this._doesCharSupportLocalGlyph(+id)) {
entry.glyphs[+id] = response[+id];
}
}
entry.ranges[range] = true;
return {stack, id, glyph: response[id] || null};
}
_doesCharSupportLocalGlyph(id: number): boolean {
// The CJK Unified Ideographs blocks and Hangul Syllables blocks are
// spread across many glyph PBFs and are typically accessed very
// randomly. Preferring local rendering for these blocks reduces
// wasteful bandwidth consumption. For visual consistency within CJKV
// text, also include any other CJKV or siniform ideograph or hangul,
// hiragana, or katakana character.
return !!this.localIdeographFontFamily &&
/\p{Ideo}|\p{sc=Hang}|\p{sc=Hira}|\p{sc=Kana}/u.test(String.fromCodePoint(id));
}
_tinySDF(entry: Entry, stack: string, id: number): StyleGlyph {
const fontFamily = this.localIdeographFontFamily;
if (!fontFamily) {
return;
}
if (!this._doesCharSupportLocalGlyph(id)) {
return;
}
// Client-generated glyphs are rendered at 2x texture scale,
// because CJK glyphs are more detailed than others.
const textureScale = 2;
let tinySDF = entry.tinySDF;
if (!tinySDF) {
let fontWeight = '400';
if (/bold/i.test(stack)) {
fontWeight = '900';
} else if (/medium/i.test(stack)) {
fontWeight = '500';
} else if (/light/i.test(stack)) {
fontWeight = '200';
}
tinySDF = entry.tinySDF = new GlyphManager.TinySDF({
fontSize: 24 * textureScale,
buffer: 3 * textureScale,
radius: 8 * textureScale,
cutoff: 0.25,
fontFamily,
fontWeight
});
}
const char = tinySDF.draw(String.fromCharCode(id));
/**
* TinySDF's "top" is the distance from the alphabetic baseline to the top of the glyph.
* Server-generated fonts specify "top" relative to an origin above the em box (the origin
* comes from FreeType, but I'm unclear on exactly how it's derived)
* ref: https://github.com/mapbox/sdf-glyph-foundry
*
* Server fonts don't yet include baseline information, so we can't line up exactly with them
* (and they don't line up with each other)
* ref: https://github.com/mapbox/node-fontnik/pull/160
*
* To approximately align TinySDF glyphs with server-provided glyphs, we use this baseline adjustment
* factor calibrated to be in between DIN Pro and Arial Unicode (but closer to Arial Unicode)
*/
const topAdjustment = 27.5;
const leftAdjustment = 0.5;
return {
id,
bitmap: new AlphaImage({width: char.width || 30 * textureScale, height: char.height || 30 * textureScale}, char.data),
metrics: {
width: char.glyphWidth / textureScale || 24,
height: char.glyphHeight / textureScale || 24,
left: (char.glyphLeft / textureScale + leftAdjustment) || 0,
top: char.glyphTop / textureScale - topAdjustment || -8,
advance: char.glyphAdvance / textureScale || 24,
isDoubleResolution: true
}
};
}
}