= new Map();
+
/** @hidden */
private _renderContext: ShieldRenderingContext;
private _shieldDefCallbacks = [];
@@ -84,11 +105,31 @@ export class AbstractShieldRenderer {
protected setShields(shieldSpec: ShieldSpecification) {
this._renderContext.options = shieldSpec.options;
this._renderContext.shieldDef = shieldSpec.networks;
+ this._fontSpec = "1em " + shieldSpec.options.shieldFont;
+ if (this._map) {
+ this.reloadShieldsOnFontLoad();
+ }
this._shieldDefCallbacks.forEach((callback) =>
callback(shieldSpec.networks)
);
}
+ private onFontsLoaded() {
+ this._fontsLoaded = true;
+ if (this._preFontImageCache.size == 0) {
+ return;
+ }
+ console.log("Re-processing shields with loaded fonts");
+
+ // Loop through each previously-loaded shield and re-render it
+ for (let [id, routeDef] of this._preFontImageCache.entries()) {
+ missingIconLoader(this._renderContext, routeDef, id, true);
+ }
+
+ this._preFontImageCache.clear();
+ this._map.redraw();
+ }
+
/** Get the shield definitions */
public getShieldDefinitions(): ShieldDefinitions {
return this._renderContext.shieldDef;
@@ -123,12 +164,24 @@ export class AbstractShieldRenderer {
}
/** Set which MaplibreGL map to handle shields for */
- public renderOnMaplibreGL(map: Map): AbstractShieldRenderer {
+ public renderOnMaplibreGL(map: MapLibre): AbstractShieldRenderer {
+ this._map = map;
+ if (this._fontSpec) {
+ this.reloadShieldsOnFontLoad();
+ }
this.renderOnRepository(new MaplibreGLSpriteRepository(map));
map.on("styleimagemissing", this.getStyleImageMissingHandler());
return this;
}
+ private reloadShieldsOnFontLoad(): void {
+ if (!this._fontsLoaded && !document.fonts.check(this._fontSpec)) {
+ document.fonts.load(this._fontSpec).then(() => this.onFontsLoaded());
+ } else {
+ this._fontsLoaded = true;
+ }
+ }
+
/** Set a callback that fires when shield definitions are loaded */
public onShieldDefLoad(
callback: (shields: ShieldDefinitions) => void
@@ -166,7 +219,10 @@ export class AbstractShieldRenderer {
return;
}
if (routeDef) {
- missingIconLoader(this._renderContext, routeDef, e.id);
+ if (!this._fontsLoaded && routeDef.ref) {
+ this._preFontImageCache.set(e.id, routeDef);
+ }
+ missingIconLoader(this._renderContext, routeDef, e.id, false);
}
} catch (err) {
console.error(`Exception while loading image ‘${e?.id}’:\n`, err);
diff --git a/shieldlib/src/types.d.ts b/shieldlib/src/types.d.ts
index a81c725ad..cb78e4ad2 100644
--- a/shieldlib/src/types.d.ts
+++ b/shieldlib/src/types.d.ts
@@ -23,7 +23,12 @@ export interface SpriteProducer {
getSprite(spriteID: string): StyleImage;
}
export interface SpriteConsumer {
- putSprite(spriteID: string, image: ImageData, pixelRatio: number): void;
+ putSprite(
+ spriteID: string,
+ image: ImageData,
+ pixelRatio: number,
+ update: boolean
+ ): void;
}
export type SpriteRepository = SpriteProducer & SpriteConsumer;
export interface ShieldDefinitions {
diff --git a/shieldlib/src/types.ts b/shieldlib/src/types.ts
index 63ae4ff1d..5ba35c4c9 100644
--- a/shieldlib/src/types.ts
+++ b/shieldlib/src/types.ts
@@ -1,4 +1,4 @@
-import { StyleImage } from "maplibre-gl";
+import { StyleImage, StyleImageMetadata } from "maplibre-gl";
/** Defines the set of routes that a shield applies to */
export interface RouteDefinition {
@@ -135,7 +135,12 @@ export interface SpriteProducer {
/** Store a sprite graphic based on an ID */
export interface SpriteConsumer {
- putSprite(spriteID: string, image: ImageData, pixelRatio: number): void;
+ putSprite(
+ spriteID: string,
+ image: ImageData,
+ options: StyleImageMetadata,
+ update: boolean
+ ): void;
}
/** Respository that can store and retrieve sprite graphics */
diff --git a/shieldlib/tsconfig.json b/shieldlib/tsconfig.json
index 1204f7fe6..3446656ce 100644
--- a/shieldlib/tsconfig.json
+++ b/shieldlib/tsconfig.json
@@ -6,10 +6,6 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
- "ts-node": {
- "esm": true,
- "experimentalSpecifierResolution": "node"
- },
"exclude": ["node_modules", "**/*.json"],
"include": ["src/**/*.ts", "scripts/**.ts", "src/shield.js", "src/shield.js"]
}
diff --git a/src/bare_map.html b/src/bare_map.html
index 0aa072f6b..4be225275 100644
--- a/src/bare_map.html
+++ b/src/bare_map.html
@@ -27,13 +27,6 @@
-
- Invisible text so the font will load early
-