UNPKG

1.08 kBPlain TextView Raw
1import {getArrayBuffer, ResourceType} from '../util/ajax';
2
3import parseGlyphPBF from './parse_glyph_pbf';
4
5import type {StyleGlyph} from './style_glyph';
6import type {RequestManager} from '../util/request_manager';
7import type {Callback} from '../types/callback';
8
9export default function loadGlyphRange(fontstack: string,
10 range: number,
11 urlTemplate: string,
12 requestManager: RequestManager,
13 callback: Callback<{
14 [_: number]: StyleGlyph | null;
15 }>) {
16 const begin = range * 256;
17 const end = begin + 255;
18
19 const request = requestManager.transformRequest(
20 urlTemplate.replace('{fontstack}', fontstack).replace('{range}', `${begin}-${end}`),
21 ResourceType.Glyphs
22 );
23
24 getArrayBuffer(request, (err?: Error | null, data?: ArrayBuffer | null) => {
25 if (err) {
26 callback(err);
27 } else if (data) {
28 const glyphs = {};
29
30 for (const glyph of parseGlyphPBF(data)) {
31 glyphs[glyph.id] = glyph;
32 }
33
34 callback(null, glyphs);
35 }
36 });
37}