-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathglyph_manager.js
161 lines (137 loc) · 5.27 KB
/
glyph_manager.js
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
// @flow
import loadGlyphRange from '../style/load_glyph_range';
import TinySDF from '@mapbox/tiny-sdf';
import isChar from '../util/is_char_in_unicode_block';
import { asyncAll } from '../util/util';
import { AlphaImage } from '../util/image';
import type {StyleGlyph} from '../style/style_glyph';
import type {RequestTransformFunction} from '../ui/map';
import type {Callback} from '../types/callback';
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]: Array<Callback<{[number]: StyleGlyph | null}>>},
tinySDF?: TinySDF
};
class GlyphManager {
requestTransform: RequestTransformFunction;
localIdeographFontFamily: ?string;
entries: {[string]: Entry};
url: ?string;
// exposed as statics to enable stubbing in unit tests
static loadGlyphRange: typeof loadGlyphRange;
static TinySDF: Class<TinySDF>;
constructor(requestTransform: RequestTransformFunction, localIdeographFontFamily: ?string) {
this.requestTransform = requestTransform;
this.localIdeographFontFamily = localIdeographFontFamily;
this.entries = {};
}
setURL(url: ?string) {
this.url = url;
}
getGlyphs(glyphs: {[stack: string]: Array<number>}, callback: Callback<{[stack: string]: {[id: number]: ?StyleGlyph}}>) {
const all = [];
for (const stack in glyphs) {
for (const id of glyphs[stack]) {
all.push({stack, id});
}
}
asyncAll(all, ({stack, id}, callback: Callback<{stack: string, id: number, glyph: ?StyleGlyph}>) => {
let entry = this.entries[stack];
if (!entry) {
entry = this.entries[stack] = {
glyphs: {},
requests: {}
};
}
let glyph = entry.glyphs[id];
if (glyph !== undefined) {
callback(null, {stack, id, glyph});
return;
}
glyph = this._tinySDF(entry, stack, id);
if (glyph) {
callback(null, {stack, id, glyph});
return;
}
const range = Math.floor(id / 256);
if (range * 256 > 65535) {
callback(new Error('glyphs > 65535 not supported'));
return;
}
let requests = entry.requests[range];
if (!requests) {
requests = entry.requests[range] = [];
GlyphManager.loadGlyphRange(stack, range, (this.url: any), this.requestTransform,
(err, response: ?{[number]: StyleGlyph | null}) => {
if (response) {
for (const id in response) {
entry.glyphs[+id] = response[+id];
}
}
for (const cb of requests) {
cb(err, response);
}
delete entry.requests[range];
});
}
requests.push((err, result: ?{[number]: StyleGlyph | null}) => {
if (err) {
callback(err);
} else if (result) {
callback(null, {stack, id, glyph: result[id] || null});
}
});
}, (err, glyphs: ?Array<{stack: string, id: number, glyph: ?StyleGlyph}>) => {
if (err) {
callback(err);
} else if (glyphs) {
const result = {};
for (const {stack, id, glyph} of glyphs) {
// Clone the glyph so that our own copy of its ArrayBuffer doesn't get transferred.
(result[stack] || (result[stack] = {}))[id] = glyph && {
id: glyph.id,
bitmap: glyph.bitmap.clone(),
metrics: glyph.metrics
};
}
callback(null, result);
}
});
}
_tinySDF(entry: Entry, stack: string, id: number): ?StyleGlyph {
const family = this.localIdeographFontFamily;
if (!family) {
return;
}
if (!isChar['CJK Unified Ideographs'](id) && !isChar['Hangul Syllables'](id)) { // eslint-disable-line new-cap
return;
}
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(24, 3, 8, .25, family, fontWeight);
}
return {
id,
bitmap: new AlphaImage({width: 30, height: 30}, tinySDF.draw(String.fromCharCode(id))),
metrics: {
width: 24,
height: 24,
left: 0,
top: -8,
advance: 24
}
};
}
}
GlyphManager.loadGlyphRange = loadGlyphRange;
GlyphManager.TinySDF = TinySDF;
export default GlyphManager;