Skip to content

Commit

Permalink
Merge pull request #1073 from osm-americana/zlw-banner-map
Browse files Browse the repository at this point in the history
Allow ShieldJSON to specify a map of banners on a base shield instead of requiring separate entries
  • Loading branch information
ZeLonewolf authored Sep 11, 2024
2 parents f9a8682 + 2b48f32 commit 88c72ad
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 264 deletions.
5 changes: 5 additions & 0 deletions shieldlib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ You should create one definition entry for each network. The entry key must matc
}
},
"banners": ["ALT"],
"bannerMap": {
"ThisNetwork:Truck": ["TRK"],
"ThisNetwork:Truck:Bypass": ["TRK", "BYP"]
},
"bannerTextColor": "#000",
"bannerTextHaloColor": "#FFF",
"textLayout": {
Expand Down Expand Up @@ -176,6 +180,7 @@ You should create one definition entry for each network. The entry key must matc

![Bannered routes near Downington, PA](https://wiki.openstreetmap.org/w/images/f/f8/Downington_bannered_routes_Americana.png)

- **`bannerMap`**: a map of network-to-banner arrays listing networks that should have the same style but with specified banners.
- **`bannerTextColor`**: specify the color of the banner text.
- **`bannerTextHaloColor`**: specify the color of the banner knockout halo.
- **`textLayout`**: specify how text should be inscribed within the padded bounds of the shield. The text will be drawn at the maximum size allowed by this constraint. See the [text layout functions](#text-layout-functions) section for text layout options.
Expand Down
25 changes: 25 additions & 0 deletions shieldlib/src/shield_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
RouteDefinition,
RouteParser,
ShapeBlankParams,
ShieldDefinition,
ShieldDefinitions,
ShieldOptions,
ShieldSpecification,
Expand Down Expand Up @@ -104,6 +105,30 @@ export class AbstractShieldRenderer {
/** Specify which shields to draw and with what graphics */
protected setShields(shieldSpec: ShieldSpecification) {
this._renderContext.options = shieldSpec.options;

// Unpack any banner maps and compose them as separate network entries in-memory
for (const [key, shieldDef] of Object.entries(shieldSpec.networks)) {
if (!shieldDef) {
continue; // Skip if shieldDef is null or undefined
}

// Check to see if it has a bannerMap
if (shieldDef.bannerMap) {
// If it does, loop through each entry in the bannerMap
for (const [bannerKey, banners] of Object.entries(
shieldDef.bannerMap
)) {
// Make a copy of the ShieldDefinition and attach the banners
const banneredShieldDef: ShieldDefinition = {
...shieldDef,
banners: banners,
};
// Insert this modified ShieldDefinition into the global network list
shieldSpec.networks[bannerKey] = banneredShieldDef;
}
}
}

this._renderContext.shieldDef = shieldSpec.networks;
this._fontSpec = "1em " + shieldSpec.options.shieldFont;
console.log("ShieldJSON loaded");
Expand Down
4 changes: 4 additions & 0 deletions shieldlib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export interface ShieldDefinitionBase {
textLayout?: TextLayout;
/** Banners to be drawn above a shield */
banners?: string[];
/** Map of additional networks that are bannered variants of this one */
bannerMap?: {
[key: string]: string[];
};
/** If true, no next should be drawn on this shield */
notext?: boolean;
/** Maximum size of shield text */
Expand Down
Loading

0 comments on commit 88c72ad

Please sign in to comment.