@dloizides/poster-render 0.11.3 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -7,6 +7,17 @@ var BackgroundStyle = /* @__PURE__ */ ((BackgroundStyle2) => {
7
7
  return BackgroundStyle2;
8
8
  })(BackgroundStyle || {});
9
9
 
10
+ // src/enums/BodySection.ts
11
+ var BodySection = /* @__PURE__ */ ((BodySection2) => {
12
+ BodySection2["Venue"] = "venue";
13
+ BodySection2["Schedule"] = "schedule";
14
+ BodySection2["Items"] = "items";
15
+ BodySection2["Prices"] = "prices";
16
+ BodySection2["Includes"] = "includes";
17
+ BodySection2["Footer"] = "footer";
18
+ return BodySection2;
19
+ })(BodySection || {});
20
+
10
21
  // src/enums/FontSetId.ts
11
22
  var FontSetId = /* @__PURE__ */ ((FontSetId2) => {
12
23
  FontSetId2["Classic"] = "classic";
@@ -86,10 +97,6 @@ var RowIcon = /* @__PURE__ */ ((RowIcon2) => {
86
97
  // src/schema/lineStyleSchema.ts
87
98
  var LINE_KEY_PATTERN = "^(title|titleBottom|script|footer|includes[.]heading|includes[.]items[.][0-5]|venue[.][0-2]|schedule[.][0-5][.](label|time)|prices[.][0-3][.](amount|label)|items[.][0-7][.](date|price|schedule[.][0-5]))$";
88
99
  var MAX_FONT = 40;
89
- var MIN_SIZE_SCALE = 0.25;
90
- var MAX_SIZE_SCALE = 3;
91
- var MIN_WIDTH_SCALE = 0.5;
92
- var MAX_WIDTH_SCALE = 1.5;
93
100
  var MIN_SPACING = -0.1;
94
101
  var MAX_SPACING = 0.5;
95
102
  var MAX_TIES = 12;
@@ -100,8 +107,8 @@ var lineStyle = {
100
107
  additionalProperties: false,
101
108
  properties: {
102
109
  font: { type: "string", minLength: 1, maxLength: MAX_FONT, pattern: "^[A-Za-z0-9 ]+$" },
103
- sizeScale: { type: "number", minimum: MIN_SIZE_SCALE, maximum: MAX_SIZE_SCALE },
104
- widthScale: { type: "number", minimum: MIN_WIDTH_SCALE, maximum: MAX_WIDTH_SCALE },
110
+ sizeScale: { type: "number", minimum: 0 },
111
+ widthScale: { type: "number", minimum: 0 },
105
112
  color: { type: "string", pattern: "^#[0-9a-fA-F]{6}$" },
106
113
  weight: { type: "integer", enum: WEIGHTS },
107
114
  italic: { type: "boolean" },
@@ -270,6 +277,7 @@ var MIN_SCALE = 0.3;
270
277
  var MAX_HEADER_GAP = 200;
271
278
  var MIN_DATE_SCALE = 0.5;
272
279
  var MAX_DATE_SCALE = 2;
280
+ var MAX_SECTIONS = 6;
273
281
  var text = (maxLength) => ({ type: "string", minLength: 1, maxLength });
274
282
  var scheduleRow = {
275
283
  type: "object",
@@ -348,6 +356,7 @@ var flierDocSchema = {
348
356
  lineStyles: lineStylesSchema,
349
357
  lineTies: lineTiesSchema,
350
358
  rows: rowsSchema,
359
+ sectionOrder: { type: "array", maxItems: MAX_SECTIONS, items: { type: "string", enum: Object.values(BodySection) } },
351
360
  items: {
352
361
  type: "array",
353
362
  minItems: 1,
@@ -807,12 +816,25 @@ function styled(line, key, sc) {
807
816
  italic: style?.italic,
808
817
  spacing: style?.letterSpacing,
809
818
  widthScale: style?.widthScale,
819
+ fixedSize: style?.sizeScale === void 0 ? void 0 : true,
810
820
  align: style?.align,
811
821
  widthTie,
812
822
  sizeTie
813
823
  };
814
824
  }
815
825
 
826
+ // src/layout/sectionOrder.ts
827
+ var EVENT_ORDER = ["venue" /* Venue */, "schedule" /* Schedule */, "prices" /* Prices */, "includes" /* Includes */, "footer" /* Footer */];
828
+ var LIST_ORDER = ["venue" /* Venue */, "items" /* Items */, "prices" /* Prices */, "includes" /* Includes */, "footer" /* Footer */];
829
+ function defaultSectionOrder(layout) {
830
+ return [...layout === "list" /* List */ ? LIST_ORDER : EVENT_ORDER];
831
+ }
832
+ function sectionOrderOf(doc) {
833
+ const base2 = defaultSectionOrder(doc.layout);
834
+ const own = (doc.sectionOrder ?? []).filter((s, i, all) => base2.includes(s) && all.indexOf(s) === i);
835
+ return [...own, ...base2.filter((s) => !own.includes(s))];
836
+ }
837
+
816
838
  // src/layout/blockGroups.ts
817
839
  var BOLD = 700;
818
840
  var SEMI = 600;
@@ -911,28 +933,26 @@ var tailed = (line, key, headLength) => ({ ...line, tail: { key, at: line.text.l
911
933
  var priceAmount = (doc, line) => doc.rows?.priceGap === void 0 ? line : { ...line, gapAfter: doc.rows.priceGap };
912
934
  function eventGroups(doc, l, u, options) {
913
935
  const b = doc.blocks;
914
- const groups = [];
936
+ const rows = [];
937
+ const priced = [];
915
938
  const schedule2 = b.schedule ?? [];
916
939
  const prices = (options.withPrices === false ? void 0 : b.prices) ?? [];
917
940
  const lastRow = schedule2.length + prices.length - 1;
918
941
  const icons2 = doc.rows?.icons;
919
942
  schedule2.forEach((row, i) => {
920
943
  const lines = options.compact ? [tailed(l.strong(`${row.label} ${row.time}`, SIZE.scheduleLabel, `schedule.${i}.label`), `schedule.${i}.time`, row.label.length)] : scheduleLines(doc, l, row, i);
921
- groups.push({ lines, gapAfter: GAP.row * u, keepWithNext: i < schedule2.length - 1, ...options.marks(icons2?.schedule, i, i === lastRow) });
944
+ rows.push({ lines, gapAfter: GAP.row * u, keepWithNext: i < schedule2.length - 1, ...options.marks(icons2?.schedule, i, i === lastRow) });
922
945
  });
923
946
  prices.forEach((row, i) => {
924
947
  const lines = options.compact ? [tailed(l.strong(`${row.amount} ${row.label}`, SIZE.priceAmount, `prices.${i}.amount`), `prices.${i}.label`, row.amount.length)] : [priceAmount(doc, l.strong(row.amount, SIZE.priceAmount, `prices.${i}.amount`)), l.accent(row.label, SIZE.priceLabel * (doc.rows?.priceLabelScale ?? 1), `prices.${i}.label`)];
925
- groups.push({ lines, gapAfter: GAP.row * u, keepWithNext: i < prices.length - 1, ...options.marks(icons2?.prices, i, schedule2.length + i === lastRow) });
948
+ priced.push({ lines, gapAfter: GAP.row * u, keepWithNext: i < prices.length - 1, ...options.marks(icons2?.prices, i, schedule2.length + i === lastRow) });
926
949
  });
927
- return groups;
950
+ return [rows, priced];
928
951
  }
929
952
  function listGroups(doc, l, u, dateScale) {
930
953
  const groups = (doc.items ?? []).map((item, n) => itemGroup(item, n, l, { u, dateScale }));
931
954
  const prices = doc.blocks.prices?.map((row, i) => tailed(l.strong(`${row.amount} ${row.label}`, SIZE.listPrice, `prices.${i}.amount`), `prices.${i}.label`, row.amount.length)) ?? [];
932
- if (prices.length > 0) {
933
- groups.push({ lines: prices, gapAfter: GAP.row * u });
934
- }
935
- return groups;
955
+ return [groups, prices.length > 0 ? [{ lines: prices, gapAfter: GAP.row * u }] : []];
936
956
  }
937
957
  function flankPriceGroups(doc, p, u) {
938
958
  const l = makers(p, u, styleContext(doc));
@@ -941,28 +961,46 @@ function flankPriceGroups(doc, p, u) {
941
961
  function pinOf(look, u) {
942
962
  return { size: look?.pinSize === void 0 ? void 0 : look.pinSize * u, gap: look?.pinGap === void 0 ? void 0 : look.pinGap * u, shift: look?.pinShift === void 0 ? void 0 : look.pinShift * u, centre: look?.centred };
943
963
  }
964
+ function venueGroups(doc, p, u, l) {
965
+ const b = doc.blocks;
966
+ if (!b.venue) {
967
+ return [];
968
+ }
969
+ const size = doc.layout === "list" /* List */ ? SIZE.listVenue : SIZE.venue;
970
+ const look = doc.rows?.venue;
971
+ const line = look?.body === true ? l.bodyVenue : l.venue;
972
+ const venue = look?.oneLine === true ? [b.venue.join(" ")] : b.venue;
973
+ return [{ lines: venue.map((v, i) => line(v, size, `venue.${i}`)), gapAfter: GAP.venue * u, rule: dividerOf(doc, p, u) ?? { color: p.accent, height: GAP.rule * u }, ruleBelow: true, icon: { color: p.icon ?? p.accent, glyph: p.background, bare: look?.bare, ...pinOf(look, u) } }];
974
+ }
975
+ var bound = (s, next) => s === "venue" /* Venue */ && next === "schedule" /* Schedule */ || s === "includes" /* Includes */ && next === "footer" /* Footer */;
976
+ function joined(order, parts) {
977
+ const filled = order.filter((s) => parts[s].length > 0);
978
+ return filled.flatMap((s, n) => {
979
+ const groups = parts[s];
980
+ if (!bound(s, filled[n + 1])) {
981
+ return groups;
982
+ }
983
+ const attach = s === "includes" /* Includes */ ? { attachNext: true } : {};
984
+ return groups.map((g, i) => i === groups.length - 1 ? { ...g, keepWithNext: true, ...attach } : g);
985
+ });
986
+ }
944
987
  function bodyGroups(doc, p, u, options = {}) {
945
988
  const l = makers(p, u, styleContext(doc), doc.rows?.timeWeight);
946
989
  const b = doc.blocks;
947
- const groups = [];
948
990
  const isList = doc.layout === "list" /* List */;
949
- if (b.venue) {
950
- const size = isList ? SIZE.listVenue : SIZE.venue;
951
- const keepWithNext = !isList && (b.schedule?.length ?? 0) > 0;
952
- const look = doc.rows?.venue;
953
- const line = look?.body === true ? l.bodyVenue : l.venue;
954
- const venue = look?.oneLine === true ? [b.venue.join(" ")] : b.venue;
955
- groups.push({ lines: venue.map((v, i) => line(v, size, `venue.${i}`)), gapAfter: GAP.venue * u, rule: dividerOf(doc, p, u) ?? { color: p.accent, height: GAP.rule * u }, ruleBelow: true, icon: { color: p.icon ?? p.accent, glyph: p.background, bare: look?.bare, ...pinOf(look, u) }, keepWithNext });
956
- }
957
- groups.push(...isList ? listGroups(doc, l, u, options.dateScale ?? 1) : eventGroups(doc, l, u, { ...options, marks: rowMarks(doc, p, u) }));
958
- if (b.includes) {
959
- const lines = [l.accent(b.includes.heading, SIZE.small, "includes.heading"), ...b.includes.items.map((item, i) => l.item(item, SIZE.small, `includes.items.${i}`))].flatMap((line) => wrapped(line, options.wrap));
960
- groups.push({ lines, gapAfter: GAP.includes * u, uniform: true, keepWithNext: b.footer !== void 0, attachNext: b.footer !== void 0 });
961
- }
962
- if (b.footer) {
963
- groups.push({ lines: wrapped(l.soft(b.footer, SIZE.small, "footer"), options.wrap), gapAfter: 0 });
964
- }
965
- return groups;
991
+ const [rows, prices] = isList ? listGroups(doc, l, u, options.dateScale ?? 1) : eventGroups(doc, l, u, { ...options, marks: rowMarks(doc, p, u) });
992
+ const includeLines = b.includes ? [l.accent(b.includes.heading, SIZE.small, "includes.heading"), ...b.includes.items.map((item, i) => l.item(item, SIZE.small, `includes.items.${i}`))] : [];
993
+ const includes = includeLines.length > 0 ? [{ lines: includeLines.flatMap((line) => wrapped(line, options.wrap)), gapAfter: GAP.includes * u, uniform: true }] : [];
994
+ const footer = b.footer ? [{ lines: wrapped(l.soft(b.footer, SIZE.small, "footer"), options.wrap), gapAfter: 0 }] : [];
995
+ const parts = {
996
+ ["venue" /* Venue */]: venueGroups(doc, p, u, l),
997
+ ["schedule" /* Schedule */]: isList ? [] : rows,
998
+ ["items" /* Items */]: isList ? rows : [],
999
+ ["prices" /* Prices */]: prices,
1000
+ ["includes" /* Includes */]: includes,
1001
+ ["footer" /* Footer */]: footer
1002
+ };
1003
+ return joined(sectionOrderOf(doc), parts);
966
1004
  }
967
1005
 
968
1006
  // src/layout/lineRuns.ts
@@ -1015,16 +1053,19 @@ function groupBox(group, k, colW) {
1015
1053
  return { pad, d, iconIndent, avail: colW - 2 * pad - iconIndent };
1016
1054
  }
1017
1055
  function lineSize(line, width, k, avail) {
1056
+ if (line.fixedSize === true) {
1057
+ return line.size;
1058
+ }
1018
1059
  const indent = bulletIndent(line, k);
1019
1060
  return Math.max(0, Math.min(line.size * k, line.size * (avail - indent) / Math.max(width, 1)));
1020
1061
  }
1021
- function groupSizes(m, k, avail, spare) {
1022
- const own = m.group.lines.map((line, i) => lineSize(line, m.widths[i] ?? 1, k, Math.min(avail * (line.widthScale ?? 1), avail + spare)));
1062
+ function groupSizes(m, k, avail) {
1063
+ const own = m.group.lines.map((line, i) => lineSize(line, m.widths[i] ?? 1, k, avail * (line.widthScale ?? 1)));
1023
1064
  if (!m.group.uniform) {
1024
1065
  return own;
1025
1066
  }
1026
1067
  const ratio = Math.min(...m.group.lines.map((line, i) => (own[i] ?? 0) / line.size));
1027
- return m.group.lines.map((line) => line.size * ratio);
1068
+ return m.group.lines.map((line) => line.fixedSize === true ? line.size : line.size * ratio);
1028
1069
  }
1029
1070
  function tieMembers(all, k, colW, table, pick) {
1030
1071
  const byId = /* @__PURE__ */ new Map();
@@ -1033,7 +1074,7 @@ function tieMembers(all, k, colW, table, pick) {
1033
1074
  const sizes = table.get(m) ?? [];
1034
1075
  m.group.lines.forEach((line, i) => {
1035
1076
  const id = pick(line);
1036
- if (id !== void 0) {
1077
+ if (id !== void 0 && line.fixedSize !== true) {
1037
1078
  const member = { line, width: Math.max(m.widths[i] ?? 1, 1), cap: Math.max(0, Math.min(1, line.widthScale ?? 1) * avail - bulletIndent(line, k)), sizes, i };
1038
1079
  byId.set(id, [...byId.get(id) ?? [], member]);
1039
1080
  }
@@ -1054,8 +1095,8 @@ function sameWidths(list) {
1054
1095
  x.sizes[x.i] = target * x.line.size / x.width;
1055
1096
  });
1056
1097
  }
1057
- function sizeTable(all, k, colW, room = colW) {
1058
- const table = new Map(all.map((m) => [m, groupSizes(m, k, groupBox(m.group, k, colW).avail, Math.max(0, room - colW))]));
1098
+ function sizeTable(all, k, colW) {
1099
+ const table = new Map(all.map((m) => [m, groupSizes(m, k, groupBox(m.group, k, colW).avail)]));
1059
1100
  tieMembers(all, k, colW, table, (line) => line.sizeTie).forEach(sameSizes);
1060
1101
  tieMembers(all, k, colW, table, (line) => line.widthTie).forEach(sameWidths);
1061
1102
  return table;
@@ -1121,7 +1162,7 @@ function placeGroup(m, at, out) {
1121
1162
  const w = (m.widths[i] ?? 0) * size / line.size;
1122
1163
  const x = lineX(line, { col, pad, indent, w });
1123
1164
  const box = { x, y, w, h: size * LINE_HEIGHT };
1124
- out?.texts.push({ text: line.text, font: "", color: line.color, box, key: line.key, ...runsOf(line, box, m.cuts?.[i], size / line.size) });
1165
+ out?.texts.push({ text: line.text, font: "", color: line.color, box, key: line.key, ...line.fixedSize === true ? { fixed: true } : {}, ...runsOf(line, box, m.cuts?.[i], size / line.size) });
1125
1166
  if (line.bullet && out) {
1126
1167
  out.bullets.push({ x: col.x + pad + size * BULLET_RADIUS, y: y + size * LINE_HEIGHT / 2, r: size * BULLET_RADIUS, color: line.bullet });
1127
1168
  }
@@ -1187,11 +1228,11 @@ function splitColumns(groups, zone) {
1187
1228
  }
1188
1229
  cut = keepTogether(groups, heights, cut);
1189
1230
  return [
1190
- { col: { ...zone, w, room: w }, groups: groups.slice(0, cut) },
1191
- { col: { ...zone, x: zone.x + w + gap, w, room: w }, groups: groups.slice(cut) }
1231
+ { col: { ...zone, w }, groups: groups.slice(0, cut) },
1232
+ { col: { ...zone, x: zone.x + w + gap, w }, groups: groups.slice(cut) }
1192
1233
  ];
1193
1234
  }
1194
- var tableFor = (columns, k) => sizeTable(columns.flatMap((c) => c.groups), k, columns[0]?.col.w ?? 0, columns[0]?.col.room);
1235
+ var tableFor = (columns, k) => sizeTable(columns.flatMap((c) => c.groups), k, columns[0]?.col.w ?? 0);
1195
1236
  function largestK(columns, zoneH) {
1196
1237
  const fits = (k) => {
1197
1238
  const table = tableFor(columns, k);
@@ -1341,11 +1382,11 @@ function flowAll(ctx, input, base2, reflow) {
1341
1382
  const head = flow(ctx, headerGroups(doc, palette, u), zones.text, fonts);
1342
1383
  const flanks = geo.flanks ?? [];
1343
1384
  const tight = closeHeaderGap(geo, head, doc.tuning?.headerGap, u);
1344
- const body = flow(ctx, bodyGroups(doc, palette, u, { withPrices: flanks.length === 0, compact: reflow.compact, wrap: reflow.wrap, dateScale: doc.tuning?.itemDateScale }), { ...tight.body, room: Math.max(tight.body.w, input.width - Math.round(Math.min(input.width, input.height) * MARGIN2) - tight.body.x) }, fonts);
1385
+ const body = flow(ctx, bodyGroups(doc, palette, u, { withPrices: flanks.length === 0, compact: reflow.compact, wrap: reflow.wrap, dateScale: doc.tuning?.itemDateScale }), tight.body, fonts);
1345
1386
  const sides = flanks.length > 0 ? equalFlanks(ctx, flankPriceGroups(doc, palette, u), flanks, fonts) : [emptyFlow()];
1346
1387
  return { geo: tight, zones, head, body, sides };
1347
1388
  }
1348
- var smallest = (t) => Math.min(Infinity, ...[t.head, t.body, ...t.sides].flatMap((f) => f.texts.map((x) => x.box.h / LINE_HEIGHT3)));
1389
+ var smallest = (t) => Math.min(Infinity, ...[t.head, t.body, ...t.sides].flatMap((f) => f.texts.filter((x) => x.fixed !== true).map((x) => x.box.h / LINE_HEIGHT3)));
1349
1390
  function shiftHeader(geo, shift) {
1350
1391
  const { header, body } = geo;
1351
1392
  const stacked = header.y + header.h <= body.y && header.x < body.x + body.w && body.x < header.x + header.w;
@@ -1430,6 +1471,21 @@ function lineBoxesOf(texts) {
1430
1471
  return boxes;
1431
1472
  }
1432
1473
 
1474
+ // src/render/roleOrder.ts
1475
+ var INDEX = /^\d+$/;
1476
+ var roleOf = (key) => key.split(".").filter((part) => !INDEX.test(part)).join(".");
1477
+ function roleOrder(boxes) {
1478
+ const first = /* @__PURE__ */ new Map();
1479
+ Object.entries(boxes).forEach(([key, box]) => {
1480
+ const role = roleOf(key);
1481
+ const seen = first.get(role);
1482
+ if (box !== void 0 && (seen === void 0 || box.y < seen.y || box.y === seen.y && box.x < seen.x)) {
1483
+ first.set(role, box);
1484
+ }
1485
+ });
1486
+ return [...first.entries()].sort(([, a], [, b]) => a.y - b.y || a.x - b.x).map(([role]) => role);
1487
+ }
1488
+
1433
1489
  // src/render/overlap.ts
1434
1490
  var clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
1435
1491
  function placePicture(image, region, slot, fit) {
@@ -1681,18 +1737,21 @@ function render(input, assets, canvas) {
1681
1737
  if (layered) {
1682
1738
  flows.forEach((f) => drawMarks(ctx, f, radius));
1683
1739
  }
1740
+ const lineBoxes = lineBoxesOf(flows.flatMap((f) => f.texts));
1684
1741
  return {
1685
1742
  width,
1686
1743
  height,
1687
1744
  picture,
1688
1745
  pictureRegion: region,
1689
1746
  textBoxes: flows.flatMap((f) => f.texts.map((t) => t.box)),
1690
- lineBoxes: lineBoxesOf(flows.flatMap((f) => f.texts)),
1747
+ lineBoxes,
1691
1748
  cardBoxes: body.cards.map((c) => c.rect),
1692
1749
  iconBoxes: flows.flatMap((f) => f.icons.map((i) => i.rect)),
1693
1750
  ruleBoxes: flows.flatMap((f) => f.rules.map((r3) => r3.rect)),
1694
1751
  logo,
1695
- backdrop
1752
+ backdrop,
1753
+ sectionOrder: sectionOrderOf(doc),
1754
+ roleOrder: roleOrder(lineBoxes)
1696
1755
  };
1697
1756
  }
1698
1757
 
@@ -1786,6 +1845,6 @@ var kizombaWorkshop = {
1786
1845
  };
1787
1846
  var kizombaWorkshopDocs = [...WORKSHOPS.map(workshopDoc), kizombaWorkshop];
1788
1847
 
1789
- export { BackgroundStyle, FONT_SETS, FORMAT_SIZES, FlierDocError, FontSetId, FormatId, LAYOUT_CATALOGUE, LINE_KEY_PATTERN, LayoutId, LineAlign, LineTieKind, PALETTES, PaletteId, PictureAnchor, RowIcon, defaultLayoutFor, exportPng, flierDocSchema, kizombaWorkshop, kizombaWorkshopDocs, render, sameSize, sameWidth, validateFlierDoc };
1848
+ export { BackgroundStyle, BodySection, FONT_SETS, FORMAT_SIZES, FlierDocError, FontSetId, FormatId, LAYOUT_CATALOGUE, LINE_KEY_PATTERN, LayoutId, LineAlign, LineTieKind, PALETTES, PaletteId, PictureAnchor, RowIcon, defaultLayoutFor, defaultSectionOrder, exportPng, flierDocSchema, kizombaWorkshop, kizombaWorkshopDocs, render, roleOf, roleOrder, sameSize, sameWidth, sectionOrderOf, validateFlierDoc };
1790
1849
  //# sourceMappingURL=index.mjs.map
1791
1850
  //# sourceMappingURL=index.mjs.map