/
shield.js
364 lines (309 loc) · 9.37 KB
/
shield.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
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
"use strict";
import * as ShieldText from "./shield_text.js";
import * as ShieldDraw from "./shield_canvas_draw.js";
import * as Gfx from "./screen_gfx.js";
import {
drawBanners,
drawBannerHalos,
getBannerCount,
} from "./shield_banner.js";
function compoundShieldSize(r, dimension, bannerCount) {
return {
width: dimension.width,
height:
dimension.height +
bannerCount * r.px(r.options.bannerHeight + r.options.bannerPadding),
};
}
export function isValidRef(ref) {
return ref !== null && ref.length !== 0 && ref.length <= 6;
}
/**
* Retrieve the shield blank that goes with a particular route. If there are
* multiple shields for a route (different widths), it picks the best shield.
*
* @param {*} r - render context
* @param {*} shieldDef - shield definition for this route
* @param {*} routeDef - route tagging from OSM
* @returns shield blank or null if no shield exists
*/
function getRasterShieldBlank(r, shieldDef, routeDef) {
var shieldArtwork = null;
var textLayout;
var bannerCount = 0;
var bounds;
if (Array.isArray(shieldDef.spriteBlank)) {
for (var i = 0; i < shieldDef.spriteBlank.length; i++) {
shieldArtwork = r.spriteRepo.getSprite(shieldDef.spriteBlank[i]);
bounds = compoundShieldSize(r, shieldArtwork.data, bannerCount);
textLayout = ShieldText.layoutShieldTextFromDef(
r,
routeDef.ref,
shieldDef,
bounds
);
if (textLayout.fontPx > r.px(Gfx.fontSizeThreshold)) {
break;
}
}
} else {
shieldArtwork = r.spriteRepo.getSprite(shieldDef.spriteBlank);
}
return shieldArtwork;
}
function textColor(shieldDef) {
if (shieldDef != null && typeof shieldDef.textColor != "undefined") {
return shieldDef.textColor;
}
return "black";
}
function getDrawFunc(shieldDef) {
if (typeof shieldDef.shapeBlank != "undefined") {
return (r, ctx, ref) =>
ShieldDraw.draw(
r,
shieldDef.shapeBlank.drawFunc,
ctx,
shieldDef.shapeBlank.params,
ref
);
}
return ShieldDraw.blank;
}
function drawShield(r, ctx, shieldDef, routeDef) {
let bannerCount = getBannerCount(shieldDef);
let yOffset = bannerCount * r.px(r.options.bannerHeight);
//Shift canvas to draw shield below banner
ctx.save();
ctx.translate(0, yOffset);
let drawFunc = getDrawFunc(shieldDef);
drawFunc(r, ctx, routeDef.ref);
ctx.restore();
}
function getDrawHeight(r, shieldDef) {
if (typeof shieldDef.shapeBlank != "undefined") {
return ShieldDraw.shapeHeight(r, shieldDef.shapeBlank.drawFunc);
}
return r.shieldSize();
}
function drawShieldText(r, ctx, shieldDef, routeDef) {
var bannerCount = getBannerCount(shieldDef);
var shieldBounds = null;
var shieldArtwork = getRasterShieldBlank(r, shieldDef, routeDef);
let yOffset = bannerCount * r.px(r.options.bannerHeight);
if (shieldArtwork == null) {
ctx.translate(0, yOffset);
let drawFunc = getDrawFunc(shieldDef);
drawFunc(r, ctx, routeDef.ref);
ctx.translate(0, -yOffset);
shieldBounds = {
width: ctx.canvas.width,
height: getDrawHeight(r, shieldDef),
};
} else {
shieldBounds = {
width: shieldArtwork.data.width,
height: shieldArtwork.data.height,
};
}
if (shieldDef.notext) {
//If the shield definition says not to draw a ref, ignore ref
return ctx;
}
//The ref is valid and we're supposed to draw it
var textLayout = ShieldText.layoutShieldTextFromDef(
r,
routeDef.ref,
shieldDef,
shieldBounds
);
textLayout.yBaseline += bannerCount * r.px(r.options.bannerHeight);
if (typeof r.options.SHIELD_TEXT_HALO_COLOR_OVERRIDE !== "undefined") {
ctx.strokeStyle = options.SHIELD_TEXT_HALO_COLOR_OVERRIDE;
ShieldText.drawShieldHaloText(r, ctx, routeDef.ref, textLayout);
} else if (shieldDef.textHaloColor) {
ctx.strokeStyle = shieldDef.textHaloColor;
ShieldText.drawShieldHaloText(r, ctx, routeDef.ref, textLayout);
}
ctx.fillStyle = textColor(shieldDef);
ShieldText.renderShieldText(r, ctx, routeDef.ref, textLayout);
if (r.options.SHIELD_TEXT_BBOX_COLOR) {
ctx.strokeStyle = r.options.SHIELD_TEXT_BBOX_COLOR; //TODO move to debugOptions
ctx.lineWidth = r.px(1);
ctx.strokeRect(
r.px(shieldDef.padding.left - 0.5),
bannerCount * r.px(r.options.bannerHeight) +
r.px(shieldDef.padding.top - 0.5),
shieldBounds.width -
r.px(shieldDef.padding.left + shieldDef.padding.right - 1),
shieldBounds.height -
r.px(shieldDef.padding.top + shieldDef.padding.bottom - 1)
);
}
return ctx;
}
export function missingIconLoader(r, routeDef, spriteID) {
let ctx = generateShieldCtx(r, routeDef);
if (ctx == null) {
// Want to return null here, but that gives a corrupted display. See #243
console.warn("Didn't produce a shield for", JSON.stringify(routeDef));
ctx = r.gfxFactory.createGraphics({ width: 1, height: 1 });
}
storeSprite(r, spriteID, ctx);
}
function storeSprite(r, id, ctx) {
const imgData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
r.spriteRepo.putSprite(
id,
{
width: ctx.canvas.width,
height: ctx.canvas.height,
data: imgData.data,
},
r.px(1)
);
}
export function storeNoShield(r, id) {
storeSprite(r, id, r.emptySprite());
}
function refForDefs(routeDef, shieldDef) {
// Handle special case for manually-applied abbreviations
if (
shieldDef.refsByName &&
routeDef.name &&
shieldDef.refsByName[routeDef.name]
) {
return shieldDef.refsByName[routeDef.name];
}
return routeDef.ref;
}
function getShieldDef(shields, routeDef) {
var shieldDef = shields[routeDef.network];
if (routeDef == null) {
return null;
}
if (shieldDef == null) {
// Default to plain black text with halo and no background shield
console.debug("Generic shield for", JSON.stringify(routeDef));
return isValidRef(routeDef.ref) ? shields.default : null;
}
var ref = refForDefs(routeDef, shieldDef);
if (shieldDef.overrideByRef) {
shieldDef = {
...shieldDef,
...shieldDef.overrideByRef[ref],
};
}
if (shieldDef.overrideByName) {
shieldDef = {
...shieldDef,
...shieldDef.overrideByName[routeDef.name || ""],
};
}
//Special case where there's a defined fallback shield when no ref is tagged
//Example: PA Turnpike
if (!isValidRef(ref) && "noref" in shieldDef) {
shieldDef = shieldDef.noref;
// noref implies notext
shieldDef.notext = true;
}
//Determine whether a route without a ref gets drawn
if (
!isValidRef(ref) &&
!shieldDef.notext &&
!(shieldDef.refsByName && routeDef.name)
) {
return null;
}
return shieldDef;
}
/**
* Reformats an alphanumeric ref as Roman numerals, preserving any alphabetic
* suffix.
*/
export function romanizeRef(ref) {
let number = parseInt(ref, 10);
if (isNaN(number)) {
return ref;
}
let roman =
"M".repeat(number / 1000) +
"D".repeat((number % 1000) / 500) +
"C".repeat((number % 500) / 100) +
"L".repeat((number % 100) / 50) +
"X".repeat((number % 50) / 10) +
"V".repeat((number % 10) / 5) +
"I".repeat(number % 5);
roman = roman
.replace("DCCCC", "CM")
.replace("CCCC", "CD")
.replace("LXXXX", "XC")
.replace("XXXX", "XL")
.replace("VIIII", "IX")
.replace("IIII", "IV");
return roman + ref.slice(number.toString().length);
}
function getDrawnShieldBounds(r, shieldDef, ref) {
let width = Math.max(
r.shieldSize(),
ShieldDraw.computeWidth(
r,
shieldDef.shapeBlank.params,
ref,
shieldDef.shapeBlank.drawFunc
)
);
let height = ShieldDraw.shapeHeight(r, shieldDef.shapeBlank.drawFunc);
return { width, height };
}
export function generateShieldCtx(r, routeDef) {
let shieldDef = getShieldDef(r.shieldDef, routeDef);
if (shieldDef == null) {
return null;
}
routeDef.ref = refForDefs(routeDef, shieldDef);
//Determine overall shield+banner dimensions
let bannerCount = getBannerCount(shieldDef);
let sourceSprite = getRasterShieldBlank(r, shieldDef, routeDef);
let width = r.shieldSize();
let height = r.shieldSize();
if (sourceSprite == null) {
if (typeof shieldDef.shapeBlank != "undefined") {
let bounds = getDrawnShieldBounds(r, shieldDef, routeDef.ref);
width = bounds.width;
height = bounds.height;
}
} else {
width = sourceSprite.data.width;
height = sourceSprite.data.height;
}
let bannerHeight = bannerCount * r.px(r.options.bannerHeight);
height += bannerHeight;
//Generate empty canvas sized to the graphic
let ctx = r.gfxFactory.createGraphics({ width, height });
// Convert numbering systems. Normally alternative numbering systems should be
// tagged directly in ref=*, but some shields use different numbering systems
// for aesthetic reasons only.
if (routeDef.ref && shieldDef.numberingSystem === "roman") {
routeDef.ref = romanizeRef(routeDef.ref);
}
// Add the halo around modifier plaque text
drawBannerHalos(r, ctx, shieldDef);
if (sourceSprite == null) {
drawShield(r, ctx, shieldDef, routeDef);
} else {
Gfx.transposeImageData(
ctx,
sourceSprite,
bannerHeight,
shieldDef.verticalReflect,
shieldDef.colorLighten,
shieldDef.colorDarken
);
}
// Draw the shield text
drawShieldText(r, ctx, shieldDef, routeDef);
// Add modifier plaque text
drawBanners(r, ctx, shieldDef);
return ctx;
}