-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlayers.js
More file actions
327 lines (302 loc) · 12.1 KB
/
Copy pathlayers.js
File metadata and controls
327 lines (302 loc) · 12.1 KB
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
// Per-year PDF structure profiles.
//
// The BRC placement map is redrawn every year and the artists rename (and
// restructure) the PDF's optional-content groups as they go. Everything the
// rest of the app sees is expressed in the 2025 vocabulary — the four canonical
// layer names below — and this file is the only place that knows how a given
// year's PDF spells them.
//
// 2025 vs 2026, the differences that matter:
// - Layer names changed wholesale ('Camp Outlines' -> 'Listed Theme Camp
// outlines' + 'Support camps', 'Camp Names' -> 'Listed Camp Names', ...).
// - 2026 has no live text at all: every label was converted to outlines, so
// the "Camp Names" layer arrives as ~15k glyph-shaped paths instead of text
// runs. See ocr.js, which reconstructs text items from those glyphs.
// - Callouts changed from a stroked two-point leader line to filled artwork:
// a dot marking the camp plus an outlined (capsule-shaped) leader.
export const CANONICAL = {
BASE_MAP: 'Base Map Things',
CAMP_OUTLINES: 'Camp Outlines',
CAMP_NAMES: 'Camp Names',
CALLOUTS: 'Listed Name Callouts',
};
const PROFILES = {
2025: {
// source OCG name -> canonical layer name
layerMap: {
'Base Map Things': CANONICAL.BASE_MAP,
'Camp Outlines': CANONICAL.CAMP_OUTLINES,
'Camp Names': CANONICAL.CAMP_NAMES,
'Listed Name Callouts': CANONICAL.CALLOUTS,
},
textStyle: 'live', // camp names are real text runs
calloutStyle: 'lines', // leaders are a single moveTo + lineTo
outlineStyle: 'stroke', // camp outlines are stroked paths: one ring per camp
},
2026: {
layerMap: {
'Base Map Things': CANONICAL.BASE_MAP,
'Listed Theme Camp outlines': CANONICAL.CAMP_OUTLINES,
'Support camps': CANONICAL.CAMP_OUTLINES,
'Listed Camp Names': CANONICAL.CAMP_NAMES,
'Unlisted Camp Names': CANONICAL.CAMP_NAMES,
Callouts: CANONICAL.CALLOUTS,
// Deliberately excluded: 'Layer 21' (white knockout copies of the name
// glyphs — 31k duplicate paths) and 'Unlisted Camps' (empty in this file).
},
textStyle: 'outlined', // camp names are vector outlines; OCR them
calloutStyle: 'outlined',// leaders are filled capsules with a dot at the camp end
outlineStyle: 'filled', // camp outlines are filled ribbons: two rings per camp
},
};
export function getLayerProfile(year) {
const profile = PROFILES[year];
if (!profile) {
const known = Object.keys(PROFILES).join(', ');
throw new Error(`No PDF layer profile for year ${year} (known years: ${known}). Add one to layers.js.`);
}
return { year, ...profile };
}
// The OCG names to pull out of the PDF for this year.
export function sourceLayerNames(profile) {
return Object.keys(profile.layerMap);
}
// Rewrite extracted items' `layer` from the year's own names to the canonical
// ones, dropping anything the profile doesn't map.
export function toCanonicalLayers(items, profile) {
const out = [];
for (const item of items) {
const canonical = profile.layerMap[item.layer];
if (!canonical) continue;
out.push({ ...item, layer: canonical });
}
return out;
}
// ---------------------------------------------------------------------------
// Callouts
// A path that spans (nearly) the whole page is the artboard clip rectangle that
// every layer carries; it is never real artwork.
export function isPageRect(bbox, width, height) {
return (bbox[2] - bbox[0]) >= width * 0.99 && (bbox[3] - bbox[1]) >= height * 0.99;
}
export function cmdsBbox(cmds) {
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
for (const c of cmds) {
for (let i = 1; i < c.length; i += 2) {
const x = c[i], y = c[i + 1];
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
}
}
return [x0, y0, x1, y1];
}
export function splitSubpaths(cmds) {
const subs = [];
let cur = null;
for (const c of cmds) {
if (c[0] === 'M') { if (cur) subs.push(cur); cur = [c]; }
else if (cur) cur.push(c);
}
if (cur) subs.push(cur);
return subs;
}
// Sample a path's curves into plain polylines, one per subpath.
export function flattenCmds(cmds, samples = 6) {
const rings = [];
let ring = null, cur = [0, 0], start = [0, 0];
const bez = (p0, p1, p2, p3) => {
for (let i = 1; i <= samples; i++) {
const t = i / samples, mt = 1 - t;
ring.push([
mt*mt*mt*p0[0] + 3*mt*mt*t*p1[0] + 3*mt*t*t*p2[0] + t*t*t*p3[0],
mt*mt*mt*p0[1] + 3*mt*mt*t*p1[1] + 3*mt*t*t*p2[1] + t*t*t*p3[1],
]);
}
};
for (const c of cmds) {
if (c[0] === 'M') {
if (ring && ring.length > 1) rings.push(ring);
ring = [[c[1], c[2]]]; cur = [c[1], c[2]]; start = cur;
} else if (!ring) {
continue;
} else if (c[0] === 'L') {
ring.push([c[1], c[2]]); cur = [c[1], c[2]];
} else if (c[0] === 'C') {
bez(cur, [c[1], c[2]], [c[3], c[4]], [c[5], c[6]]); cur = [c[5], c[6]];
} else if (c[0] === 'V') {
bez(cur, cur, [c[1], c[2]], [c[3], c[4]]); cur = [c[3], c[4]];
} else if (c[0] === 'Y') {
bez(cur, [c[1], c[2]], [c[3], c[4]], [c[3], c[4]]); cur = [c[3], c[4]];
} else if (c[0] === 'Z') {
ring.push([start[0], start[1]]); cur = start;
}
}
if (ring && ring.length > 1) rings.push(ring);
return rings;
}
function polygonArea(pts) {
let s = 0;
for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) {
s += pts[j][0] * pts[i][1] - pts[i][0] * pts[j][1];
}
return Math.abs(s) / 2;
}
function nearestOnPolyline(x, y, pts) {
let best = null, bestD2 = Infinity;
for (let i = 1; i < pts.length; i++) {
const [ax, ay] = pts[i - 1], [bx, by] = pts[i];
const dx = bx - ax, dy = by - ay;
const len2 = dx * dx + dy * dy;
let t = len2 > 0 ? ((x - ax) * dx + (y - ay) * dy) / len2 : 0;
t = Math.max(0, Math.min(1, t));
const px = ax + t * dx, py = ay + t * dy;
const d2 = (x - px) * (x - px) + (y - py) * (y - py);
if (d2 < bestD2) { bestD2 = d2; best = [px, py]; }
}
return best;
}
// ---------------------------------------------------------------------------
// Camp outlines
// A camp drawn with a stroke and then converted to outlines becomes a filled
// ribbon: two concentric rings about half a stroke width — nearly 8 feet on the
// 2026 map — either side of the line the artist actually drew. The rest of the
// app wants that line, so pull each ring's points back to the middle. Working
// off the outer ring's own commands keeps the curves as curves, which is what
// the client's frontage detection needs to recognise a camp fronting a circular
// street.
function ribbonToCentreline(cmds) {
const subs = splitSubpaths(cmds);
if (subs.length < 2) return openRibbonToCentreline(cmds);
const flat = subs.map(s => flattenCmds(s, 6)[0] || []);
const areas = flat.map(polygonArea);
let outer = 0;
for (let i = 1; i < areas.length; i++) if (areas[i] > areas[outer]) outer = i;
let inner = -1;
for (let i = 0; i < areas.length; i++) {
if (i !== outer && (inner < 0 || areas[i] > areas[inner])) inner = i;
}
const innerPts = flat[inner];
if (!innerPts || innerPts.length < 2) return cmds;
const out = [];
for (const c of subs[outer]) {
if (c[0] === 'Z') { out.push(['Z']); continue; }
const moved = [c[0]];
for (let i = 1; i < c.length; i += 2) {
const p = nearestOnPolyline(c[i], c[i + 1], innerPts);
moved.push((c[i] + p[0]) / 2, (c[i + 1] + p[1]) / 2);
}
out.push(moved);
}
if (out.length && out[out.length - 1][0] !== 'Z') out.push(['Z']);
return out;
}
// One ring only means the artist's outline was never closed: the fill traces
// out along one side of the stroke, around the cap and back down the other.
// Fold it in half to recover the line, and leave it open so the client still
// flags it for the user to close.
function openRibbonToCentreline(cmds) {
const pts = flattenCmds(cmds, 6)[0];
if (!pts || pts.length < 6) return cmds;
const bbox = cmdsBbox(cmds);
const boxArea = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1]);
// A solid shape fills its box; a there-and-back ribbon barely covers a tenth.
if (!boxArea || polygonArea(pts) / boxArea > 0.3) return cmds;
const half = Math.floor(pts.length / 2);
const side = pts.slice(0, half);
const back = pts.slice(half).reverse();
const line = side.map((p, i) => {
const q = back[Math.min(back.length - 1, Math.round(i * (back.length - 1) / Math.max(1, side.length - 1)))];
return [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2];
});
return [['M', line[0][0], line[0][1]], ...line.slice(1).map(p => ['L', p[0], p[1]])];
}
export function normalizeCampOutlines(paths, profile, pageWidth, pageHeight) {
if (profile.outlineStyle !== 'filled') return paths;
let ribbons = 0, open = 0;
const out = paths.map(p => {
if (p.layer !== CANONICAL.CAMP_OUTLINES) return p;
const bbox = cmdsBbox(p.cmds);
if (isPageRect(bbox, pageWidth, pageHeight)) return p;
const cmds = ribbonToCentreline(p.cmds);
if (cmds === p.cmds) return p;
if (cmds[cmds.length - 1][0] === 'Z') ribbons++; else open++;
return { ...p, cmds };
});
console.log(` Camp outlines: ${ribbons} filled ribbons reduced to centrelines, ${open} left open for review`);
return out;
}
function cmdsPoints(cmds) {
const pts = [];
for (const c of cmds) {
for (let i = 1; i < c.length; i += 2) pts.push([c[i], c[i + 1]]);
}
return pts;
}
// The two farthest-apart points of a shape. For the thin capsule that 2026 uses
// to draw a leader line, these are its two ends (off the centreline by half the
// stroke width — under a PDF unit, versus camps tens of units across).
function farthestPair(pts) {
let best = null, bestD2 = -1;
for (let i = 0; i < pts.length; i++) {
for (let j = i + 1; j < pts.length; j++) {
const dx = pts[i][0] - pts[j][0], dy = pts[i][1] - pts[j][1];
const d2 = dx * dx + dy * dy;
if (d2 > bestD2) { bestD2 = d2; best = [pts[i], pts[j]]; }
}
}
return best;
}
// Convert a year's callout artwork into the canonical two-point form the client
// consumes: cmds = [['M', anchorX, anchorY], ['L', labelX, labelY]] where the M
// end sits inside the camp and the L end points at the camp's label.
export function normalizeCallouts(paths, profile, pageWidth, pageHeight) {
// 2025 already draws leaders this way, and the client picks the two-point
// ones out for itself, so hand the layer over untouched.
if (profile.calloutStyle === 'lines') return paths;
// 2026: filled artwork. Markers (a dot, sometimes with a ring around it) are
// all-curve shapes; leaders are longer shapes containing straight segments.
const markers = [];
const leaders = [];
for (const p of paths) {
if (p.layer !== CANONICAL.CALLOUTS) continue;
const bbox = cmdsBbox(p.cmds);
if (isPageRect(bbox, pageWidth, pageHeight)) continue;
const hasLine = p.cmds.some(c => c[0] === 'L');
const w = bbox[2] - bbox[0], h = bbox[3] - bbox[1];
if (!hasLine) {
markers.push([(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2]);
} else if (Math.max(w, h) > 3) {
leaders.push(p);
}
}
const converted = [];
let oriented = 0;
for (const p of leaders) {
const ends = farthestPair(cmdsPoints(p.cmds));
if (!ends) continue;
// The dot marks the camp end, so the endpoint nearer a marker is the anchor.
let dNear = [Infinity, Infinity];
for (const [i, e] of ends.entries()) {
for (const m of markers) {
const d = Math.hypot(e[0] - m[0], e[1] - m[1]);
if (d < dNear[i]) dNear[i] = d;
}
}
const anchorIdx = dNear[0] <= dNear[1] ? 0 : 1;
if (isFinite(dNear[anchorIdx])) oriented++;
const a = ends[anchorIdx], b = ends[1 - anchorIdx];
converted.push({ layer: p.layer, cmds: [['M', a[0], a[1]], ['L', b[0], b[1]]] });
}
console.log(` Callouts: ${converted.length} leaders from ${leaders.length} shapes, ` +
`${markers.length} markers (${oriented} oriented by marker)`);
// Drop the dots and the leader artwork, keeping the rest of the layer order.
const out = [];
let placed = false;
for (const p of paths) {
if (p.layer !== CANONICAL.CALLOUTS) { out.push(p); continue; }
if (!placed) { out.push(...converted); placed = true; }
}
return out;
}