forked from heremaps/harp.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemeLoader.ts
556 lines (507 loc) · 19.2 KB
/
ThemeLoader.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* Copyright (C) 2023- XYZ maps contributors
* Copyright (C) 2019-2021 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import "@xyzmaps/harp-fetch";
import { isJsonExpr } from "@xyzmaps/harp-datasource-protocol";
import {
Definitions,
getDefinitionValue,
getStyles,
ImageTexture,
isJsonExprReference,
isStylesDictionary,
Style,
Styles,
Theme
} from "@xyzmaps/harp-datasource-protocol/lib/Theme";
import {
composeUriResolvers,
ContextLogger,
getAppBaseUrl,
getOptionValue,
IContextLogger,
ISimpleChannel,
RelativeUriResolver,
UriResolver
} from "@xyzmaps/harp-utils";
import { SKY_CUBEMAP_FACE_COUNT, SkyCubemapFaceId } from "./SkyCubemapTexture";
/**
* @internal
*/
export const DEFAULT_MAX_THEME_INTHERITANCE_DEPTH = 4;
/**
* Options to customize {@link @xyzmaps/harp-datasource-protocol#Theme} loading process.
*
* @see {@link ThemeLoader.load}
*/
export interface ThemeLoadOptions {
/**
* Whether to resolve `ref` expressions in `definition` and `styles` elements.
*
* @default `false`, as datasources resolve definitions in [[StyleSetEvaluator]].
*/
resolveDefinitions?: boolean;
/**
* Resolve the URIs to resources like fonts, icons, ...
* If true, [[uriResolver]] will be used to resolve the URI
* @default true
*/
resolveResourceUris?: boolean;
/**
* Resolve the URIs of inherited themes (using `extends` feature).
* If true, [[uriResolver]] will be used to resolve the URI
* @default true
*/
resolveIncludeUris?: boolean;
/**
* An `AbortSignal` object instance; allows you to communicate with a loading process
* (including fetch requests) request and abort it if desired via an `AbortController`.
*
* Modeled after Web APIs `fetch`s `init.signal`.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortController
*/
signal?: AbortSignal;
/**
* Maximum recursion depth when resolving base themes
* through [{@link @xyzmaps/harp-datasource-protocol#Theme}s `extends` property.
*
* @default [[DEFAULT_MAX_THEME_INTHERITANCE_DEPTH]]
*/
maxInheritanceDepth?: number;
/**
* Custom logging channel on which diagnostics and warnings will be reported.
*
* If not specified, {@link ThemeLoader.load} will log to `console`.
*/
logger?: ISimpleChannel;
/**
* Resolve asset `URI`s referenced in `Theme` assets using this resolver.
*/
uriResolver?: UriResolver;
}
/**
* Loads and validates a theme from URL objects.
*/
export class ThemeLoader {
/**
* Loads a {@link @xyzmaps/harp-datasource-protocol#Theme} from a
* remote resource, provided as a URL that points to a
* JSON-encoded theme.
*
* By default, resolves following features of theme:
*
* - `extends` - loads and merges all inherited themes (see [[resolveBaseTheme]])
* - `ref` - resolves all `ref` instances to their values defined in `definitions` section
* of theme (see [[resolveThemeReferences]])
*
* Relative URIs of reference resources are resolved to full URL using the document's base URL
* (see [[resolveUrls]]).
*
* Custom URIs (of theme itself and of resources referenced by theme) may be resolved with by
* providing {@link @xyzmaps/harp-utils#UriResolver} using {@link ThemeLoadOptions.uriResolver}
* option.
*
* @param theme - {@link @xyzmaps/harp-datasource-protocol#Theme} instance or theme URL
* to the theme.
* @param options - Optional, a {@link ThemeLoadOptions} objects
* containing any custom settings for
* this load request.
*/
static async load(theme: string | Theme, options?: ThemeLoadOptions): Promise<Theme> {
options = options ?? {};
if (typeof theme === "string") {
const uriResolver = options.uriResolver;
const themeUrl = uriResolver !== undefined ? uriResolver.resolveUri(theme) : theme;
const response = await fetch(themeUrl, { signal: options.signal });
if (!response.ok) {
throw new Error(`ThemeLoader#load: cannot load theme: ${response.statusText}`);
}
theme = (await response.json()) as Theme;
theme.url = themeUrl;
theme = this.resolveUrls(theme, options);
} else if (theme.url === undefined) {
// assume that theme url is same as baseUrl
theme.url = getAppBaseUrl();
theme = this.resolveUrls(theme, options);
}
theme.styles = getStyles(theme.styles);
if (theme === null || theme === undefined) {
throw new Error("ThemeLoader#load: loaded resource is not valid JSON");
}
const resolveDefinitions = getOptionValue<boolean>(options.resolveDefinitions, false);
theme = await ThemeLoader.resolveBaseThemes(theme, options);
if (resolveDefinitions) {
const contextLoader = new ContextLogger(
options.logger ?? console,
`when processing Theme ${theme.url}:`
);
ThemeLoader.resolveThemeReferences(theme, contextLoader);
}
return theme;
}
/**
* Checks if `theme` instance is completely loaded, meaning that `extends` property is resolved.
*
* @param theme -
*/
static isThemeLoaded(theme: Theme): boolean {
//TODO: Removed isStylesDictionary check when {@link StylesDictionary} is
// fully deprecated
return theme.extends === undefined && !isStylesDictionary(theme.styles);
}
/**
* @deprecated Please use `ThemeLoader.load`
*
* Loads a {@link @xyzmaps/harp-datasource-protocol#Theme} from a remote resource,
* provided as a URL that points to a JSON-encoded
* theme.
*
* @param themeUrl - The URL to the theme.
*
*/
static async loadAsync(themeUrl: string): Promise<Theme> {
return await ThemeLoader.load(themeUrl);
}
/**
* Resolves all {@link @xyzmaps/harp-datasource-protocol#Theme}'s relatives URLs
* to full URL using the {@link @xyzmaps/harp-datasource-protocol#Theme}'s URL
* (see: https://www.w3.org/TR/WD-html40-970917/htmlweb.html#h-5.1.2).
*
* This method mutates original `theme` instance.
*
* @param theme - The {@link @xyzmaps/harp-datasource-protocol#Theme} to resolve.
*/
private static resolveUrls(theme: Theme, options?: ThemeLoadOptions): Theme {
// Ensure that all resources referenced in theme by relative URIs are in fact relative to
// theme.
if (theme.url === undefined) {
return theme;
}
const childUrlResolver = composeUriResolvers(
options?.uriResolver,
new RelativeUriResolver(theme.url)
);
const resolveIncludes = options === undefined || !(options.resolveIncludeUris === false);
if (theme.extends && resolveIncludes) {
theme.extends = (Array.isArray(theme.extends) ? theme.extends : [theme.extends]).map(
baseTheme => {
if (typeof baseTheme === "string") {
return childUrlResolver.resolveUri(baseTheme);
} else {
if (baseTheme.url !== undefined) {
return baseTheme;
} else {
baseTheme.url = theme.url;
return this.resolveUrls(baseTheme, options);
}
}
}
);
}
const resolveResources = options === undefined || !(options.resolveResourceUris === false);
if (resolveResources) {
ThemeLoader.resolveResources(theme, childUrlResolver);
}
return theme;
}
/**
* Expand all `ref` expressions in {@link @xyzmaps/harp-datasource-protocol#Theme}
* basing on `definitions`.
*
* @remarks
* This method mutates original `theme` instance.
*/
private static resolveThemeReferences(theme: Theme, contextLogger: IContextLogger): Theme {
if (theme.styles !== undefined) {
contextLogger.pushAttr("styles");
theme.styles = ThemeLoader.resolveStyles(
getStyles(theme.styles),
theme.definitions,
contextLogger
);
contextLogger.pop();
contextLogger.pop();
}
return theme;
}
/**
* Expand all `ref` in [[StyleSet]] basing on `definitions`.
*/
private static resolveStyles(
styles: Styles,
definitions: Definitions | undefined,
contextLogger: IContextLogger
): Styles {
const result: Styles = [];
for (let index = 0; index < styles.length; ++index) {
const currentStyle = styles[index];
contextLogger.pushIndex(index);
const resolvedStyle = ThemeLoader.resolveStyle(
currentStyle,
definitions,
contextLogger
);
if (resolvedStyle !== undefined) {
result.push(resolvedStyle);
} else {
contextLogger.warn("invalid style, ignored");
}
contextLogger.pop();
}
return result;
}
/**
* Expand all `ref` in [[Style]] instance basing on `definitions`.
*/
private static resolveStyle(
style: Style,
definitions: Definitions | undefined,
contextLogger: IContextLogger
): Style | undefined {
if (Array.isArray(style.when)) {
contextLogger.pushAttr("when");
const resolvedWhen = this.resolveExpressionReferences(
style.when,
definitions,
contextLogger
);
contextLogger.pop();
if (resolvedWhen === undefined) {
return undefined;
}
style.when = resolvedWhen;
}
if (style.attr !== undefined) {
const attr = style.attr as any;
contextLogger.pushAttr("attr");
for (const prop in attr) {
if (!attr.hasOwnProperty(prop)) {
continue;
}
const value = attr[prop];
if (!Array.isArray(value)) {
continue; // nothing to do
}
contextLogger.pushAttr(prop);
const resolvedValue = this.resolveExpressionReferences(
value,
definitions,
contextLogger
);
contextLogger.pop();
if (resolvedValue !== undefined) {
attr[prop] = resolvedValue;
} else {
delete attr[prop];
}
}
contextLogger.pop();
}
return style;
}
/**
* Resolve `[ref, ...]` in expressions.
*
* Returns `undefined` some reference was invalid (missing or wrong type).
*/
private static resolveExpressionReferences<T>(
value: T,
definitions: Definitions | undefined,
contextLogger: IContextLogger
): T | undefined {
let failed = false;
function resolveInternal(node: any) {
if (isJsonExprReference(node)) {
const defName = node[1];
const def = definitions && definitions[defName];
if (def === undefined) {
contextLogger.warn(`invalid reference '${defName}' - not found`);
failed = true;
return undefined;
}
if (isJsonExpr(def)) {
return def;
}
return getDefinitionValue(def);
} else if (Array.isArray(node)) {
const result = [...node];
for (let i = 1; i < result.length; ++i) {
result[i] = resolveInternal(result[i]);
}
return result;
} else {
return node;
}
}
const r = resolveInternal(value);
if (failed) {
return undefined;
}
return r;
}
/**
* Realize `extends` clause by merging `theme` with
* its base {@link @xyzmaps/harp-datasource-protocol#Theme}.
*
* @param theme - {@link @xyzmaps/harp-datasource-protocol#Theme} object
* @param options - Optional, a {@link ThemeLoadOptions} objects
* containing any custom settings for
* this load request.
*/
private static async resolveBaseThemes(
theme: Theme,
options?: ThemeLoadOptions
): Promise<Theme> {
options = options ?? {};
if (theme.extends === undefined) {
return theme;
}
const maxInheritanceDepth = getOptionValue(
options.maxInheritanceDepth,
DEFAULT_MAX_THEME_INTHERITANCE_DEPTH
);
if (maxInheritanceDepth <= 0) {
throw new Error(`maxInheritanceDepth reached when attempting to load base theme`);
}
const baseThemes = !Array.isArray(theme.extends) ? [theme.extends] : theme.extends;
delete theme.extends;
let baseThemesMerged: Theme = {};
for (const baseTheme of baseThemes) {
const actualBaseTheme = await ThemeLoader.load(baseTheme, {
...options,
resolveDefinitions: false,
maxInheritanceDepth: maxInheritanceDepth - 1
});
baseThemesMerged = ThemeLoader.mergeThemes(actualBaseTheme, baseThemesMerged);
}
return ThemeLoader.mergeThemes(theme, baseThemesMerged);
}
private static mergeThemes(theme: Theme, baseTheme: Theme): Theme {
const definitions = { ...baseTheme.definitions, ...theme.definitions };
let styles!: Styles;
const baseStyles = getStyles(baseTheme.styles);
const themeStyles = getStyles(theme.styles);
if (baseTheme.styles && theme.styles) {
const newStyles: Styles = [];
const styleIdMap = new Map<string, number>();
baseStyles.forEach(style => {
if (typeof style.id === "string") {
//multiple identical style.ids are not supported and will fall back to the
//first occurence
if (!styleIdMap.has(style.id)) {
styleIdMap.set(style.id, newStyles.length);
}
}
newStyles.push(style);
});
themeStyles.forEach(style => {
if (typeof style.extends === "string" && styleIdMap.has(style.extends)) {
// extends the existing style referenced by `style.extends`.
const baseStyleIndex = styleIdMap.get(style.extends)!;
const baseStyle = newStyles[baseStyleIndex];
newStyles[baseStyleIndex] = { ...baseStyle, ...style } as any;
newStyles[baseStyleIndex].extends = undefined;
return;
}
if (typeof style.id === "string" && styleIdMap.has(style.id)) {
// overrides the existing style with `id` equals to `style.id`.
const styleIndex = styleIdMap.get(style.id)!;
// only match if the two rules are from the same styleset
if (newStyles[styleIndex].styleSet === style.styleSet) {
newStyles[styleIndex] = style;
}
return;
}
newStyles.push(style);
});
styles = newStyles;
} else if (baseTheme.styles) {
styles = [...baseStyles];
} else if (theme.styles) {
styles = [...themeStyles];
}
return {
...baseTheme,
...theme,
// Due to nested structure of the images/textures it needs a
// deep merge with a duplicate exclusion.
...ThemeLoader.mergeImageTextures(theme, baseTheme),
definitions,
styles
};
}
private static mergeImageTextures(
theme: Theme,
baseTheme: Theme
): Pick<Theme, "images" | "imageTextures"> {
const images = { ...baseTheme.images, ...theme.images };
let imageTextures: ImageTexture[] = [];
if (!baseTheme.imageTextures && theme.imageTextures) {
imageTextures = theme.imageTextures;
} else if (baseTheme.imageTextures && !theme.imageTextures) {
imageTextures = baseTheme.imageTextures;
} else if (baseTheme.imageTextures && theme.imageTextures) {
imageTextures = theme.imageTextures.slice();
baseTheme.imageTextures.forEach(val => {
if (!imageTextures.find(({ name }) => name === val.name)) {
imageTextures.push(val);
}
});
}
return {
images,
imageTextures
};
}
private static resolveResources(theme: Theme, childUrlResolver: UriResolver) {
if (theme.sky && theme.sky.type === "cubemap") {
for (let i = 0; i < SKY_CUBEMAP_FACE_COUNT; ++i) {
const faceUrl: string | undefined = (theme.sky as any)[SkyCubemapFaceId[i]];
if (faceUrl !== undefined) {
(theme.sky as any)[SkyCubemapFaceId[i]] = childUrlResolver.resolveUri(faceUrl);
}
}
}
if (theme.images) {
for (const name of Object.keys(theme.images)) {
const image = theme.images[name];
image.url = childUrlResolver.resolveUri(image.url);
if (image.atlas !== undefined) {
image.atlas = childUrlResolver.resolveUri(image.atlas);
}
}
}
if (theme.fontCatalogs) {
for (const font of theme.fontCatalogs) {
font.url = childUrlResolver.resolveUri(font.url);
}
}
if (theme.poiTables) {
for (const poiTable of theme.poiTables) {
poiTable.url = childUrlResolver.resolveUri(poiTable.url);
}
}
if (theme.styles !== undefined) {
for (const style of getStyles(theme.styles)) {
if (!style.attr) {
continue;
}
["map", "normalMap", "displacementMap", "roughnessMap"].forEach(
texturePropertyName => {
const textureProperty = (style.attr! as any)[texturePropertyName];
if (textureProperty && typeof textureProperty === "string") {
(style.attr! as any)[texturePropertyName] = childUrlResolver.resolveUri(
textureProperty
);
}
}
);
}
}
}
}