@dloizides/poster-render 0.11.1 → 0.11.3

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.js CHANGED
@@ -901,7 +901,7 @@ function halves(text2) {
901
901
  }
902
902
  function wrapped(line, wrap) {
903
903
  const parts = wrap ? halves(line.text) : [line.text];
904
- return parts.map((text2, i) => i === 0 ? { ...line, text: text2 } : { ...line, text: text2, bullet: void 0, hang: line.bullet !== void 0 });
904
+ return parts.length === 1 ? [line] : parts.map((text2, i) => i === 0 ? { ...line, text: text2, tail: void 0 } : { ...line, text: text2, tail: void 0, bullet: void 0, hang: line.bullet !== void 0 });
905
905
  }
906
906
  function scheduleLines(doc, l, row, i) {
907
907
  const r3 = doc.rows;
@@ -909,6 +909,7 @@ function scheduleLines(doc, l, row, i) {
909
909
  const tied = r3?.labelScale === void 0 ? label : { ...label, sizeTie: label.sizeTie ?? LABEL_TIE };
910
910
  return [tied, l.time(row.time, SIZE.scheduleTime * (r3?.timeScale ?? 1), `schedule.${i}.time`)];
911
911
  }
912
+ var tailed = (line, key, headLength) => ({ ...line, tail: { key, at: line.text.length - line.text.slice(headLength).trimStart().length } });
912
913
  var priceAmount = (doc, line) => doc.rows?.priceGap === void 0 ? line : { ...line, gapAfter: doc.rows.priceGap };
913
914
  function eventGroups(doc, l, u, options) {
914
915
  const b = doc.blocks;
@@ -918,18 +919,18 @@ function eventGroups(doc, l, u, options) {
918
919
  const lastRow = schedule2.length + prices.length - 1;
919
920
  const icons2 = doc.rows?.icons;
920
921
  schedule2.forEach((row, i) => {
921
- const lines = options.compact ? [l.strong(`${row.label} ${row.time}`, SIZE.scheduleLabel, `schedule.${i}.label`)] : scheduleLines(doc, l, row, i);
922
+ 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);
922
923
  groups.push({ lines, gapAfter: GAP.row * u, keepWithNext: i < schedule2.length - 1, ...options.marks(icons2?.schedule, i, i === lastRow) });
923
924
  });
924
925
  prices.forEach((row, i) => {
925
- const lines = options.compact ? [l.strong(`${row.amount} ${row.label}`, SIZE.priceAmount, `prices.${i}.amount`)] : [priceAmount(doc, l.strong(row.amount, SIZE.priceAmount, `prices.${i}.amount`)), l.accent(row.label, SIZE.priceLabel * (doc.rows?.priceLabelScale ?? 1), `prices.${i}.label`)];
926
+ 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`)];
926
927
  groups.push({ lines, gapAfter: GAP.row * u, keepWithNext: i < prices.length - 1, ...options.marks(icons2?.prices, i, schedule2.length + i === lastRow) });
927
928
  });
928
929
  return groups;
929
930
  }
930
931
  function listGroups(doc, l, u, dateScale) {
931
932
  const groups = (doc.items ?? []).map((item, n) => itemGroup(item, n, l, { u, dateScale }));
932
- const prices = doc.blocks.prices?.map((row, i) => l.strong(`${row.amount} ${row.label}`, SIZE.listPrice, `prices.${i}.amount`)) ?? [];
933
+ 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)) ?? [];
933
934
  if (prices.length > 0) {
934
935
  groups.push({ lines: prices, gapAfter: GAP.row * u });
935
936
  }
@@ -966,6 +967,34 @@ function bodyGroups(doc, p, u, options = {}) {
966
967
  return groups;
967
968
  }
968
969
 
970
+ // src/layout/lineRuns.ts
971
+ var NO_SPACING = "0px";
972
+ function widthOf(ctx, line, fonts, text2) {
973
+ ctx.font = fontOf(line, line.size, fonts);
974
+ if (line.spacing === void 0) {
975
+ return ctx.measureText(text2).width;
976
+ }
977
+ ctx.letterSpacing = `${line.spacing * line.size}px`;
978
+ const width = ctx.measureText(text2).width;
979
+ ctx.letterSpacing = NO_SPACING;
980
+ return width;
981
+ }
982
+ var measure = (ctx, line, fonts) => widthOf(ctx, line, fonts, line.text);
983
+ function measureCut(ctx, line, fonts) {
984
+ if (line.tail === void 0) {
985
+ return void 0;
986
+ }
987
+ const head = line.text.slice(0, line.tail.at);
988
+ return [widthOf(ctx, line, fonts, head.trimEnd()), widthOf(ctx, line, fonts, head)];
989
+ }
990
+ function runsOf(line, box, cut, scale) {
991
+ if (line.tail === void 0 || line.key === void 0 || cut === void 0) {
992
+ return {};
993
+ }
994
+ const start = cut[1] * scale;
995
+ return { runs: [{ key: line.key, box: { ...box, w: cut[0] * scale } }, { key: line.tail.key, box: { ...box, x: box.x + start, w: Math.max(0, box.w - start) } }] };
996
+ }
997
+
969
998
  // src/layout/lineSizes.ts
970
999
  var LINE_HEIGHT = 1.2;
971
1000
  var BULLET_INDENT = 1.1;
@@ -1044,7 +1073,6 @@ var TWO_COLUMN_MIN = 0.75;
1044
1073
  var TWO_COLUMN_GAIN = 1.15;
1045
1074
  var MIN_K = 0.02;
1046
1075
  var STEPS = 22;
1047
- var NO_SPACING = "0px";
1048
1076
  var fontOf = (line, size, fonts) => {
1049
1077
  const family = line.family === void 0 ? fonts[line.role] : `"${line.family}", ${fonts[line.role]}`;
1050
1078
  return `${line.italic === true ? "italic " : ""}${line.weight} ${size}px ${family}`;
@@ -1094,7 +1122,8 @@ function placeGroup(m, at, out) {
1094
1122
  const indent = (indented(line) ? line.size * k * BULLET_INDENT : 0) + iconIndent;
1095
1123
  const w = (m.widths[i] ?? 0) * size / line.size;
1096
1124
  const x = lineX(line, { col, pad, indent, w });
1097
- out?.texts.push({ text: line.text, font: "", color: line.color, box: { x, y, w, h: size * LINE_HEIGHT }, key: line.key });
1125
+ const box = { x, y, w, h: size * LINE_HEIGHT };
1126
+ out?.texts.push({ text: line.text, font: "", color: line.color, box, key: line.key, ...runsOf(line, box, m.cuts?.[i], size / line.size) });
1098
1127
  if (line.bullet && out) {
1099
1128
  out.bullets.push({ x: col.x + pad + size * BULLET_RADIUS, y: y + size * LINE_HEIGHT / 2, r: size * BULLET_RADIUS, color: line.bullet });
1100
1129
  }
@@ -1196,20 +1225,11 @@ function bestColumns(measured, zone) {
1196
1225
  const forced = zone.w / Math.max(zone.h, 1) > WIDE;
1197
1226
  return forced || k2 > k1 * TWO_COLUMN_GAIN ? { columns: split, k: k2 } : { columns: single, k: k1 };
1198
1227
  }
1199
- function measure(ctx, line, fonts) {
1200
- ctx.font = fontOf(line, line.size, fonts);
1201
- if (line.spacing === void 0) {
1202
- return ctx.measureText(line.text).width;
1203
- }
1204
- ctx.letterSpacing = `${line.spacing * line.size}px`;
1205
- const width = ctx.measureText(line.text).width;
1206
- ctx.letterSpacing = NO_SPACING;
1207
- return width;
1208
- }
1209
1228
  function flow(ctx, groups, zone, fonts) {
1210
1229
  const measured = groups.map((group) => ({
1211
1230
  group,
1212
- widths: group.lines.map((line) => measure(ctx, line, fonts))
1231
+ widths: group.lines.map((line) => measure(ctx, line, fonts)),
1232
+ cuts: group.lines.map((line) => measureCut(ctx, line, fonts))
1213
1233
  }));
1214
1234
  const { columns, k: lo } = bestColumns(measured, zone);
1215
1235
  const out = emptyFlow();
@@ -1323,7 +1343,7 @@ function flowAll(ctx, input, base2, reflow) {
1323
1343
  const head = flow(ctx, headerGroups(doc, palette, u), zones.text, fonts);
1324
1344
  const flanks = geo.flanks ?? [];
1325
1345
  const tight = closeHeaderGap(geo, head, doc.tuning?.headerGap, u);
1326
- 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, base2.body.w) }, fonts);
1346
+ 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);
1327
1347
  const sides = flanks.length > 0 ? equalFlanks(ctx, flankPriceGroups(doc, palette, u), flanks, fonts) : [emptyFlow()];
1328
1348
  return { geo: tight, zones, head, body, sides };
1329
1349
  }
@@ -1393,6 +1413,25 @@ function fitToFloor(ctx, input) {
1393
1413
  return best ?? flowAll(ctx, input, base2, { compact: false, wrap: false});
1394
1414
  }
1395
1415
 
1416
+ // src/render/lineBoxesOf.ts
1417
+ function union(a, b) {
1418
+ if (a === void 0) {
1419
+ return { ...b };
1420
+ }
1421
+ const x = Math.min(a.x, b.x);
1422
+ const y = Math.min(a.y, b.y);
1423
+ return { x, y, w: Math.max(a.x + a.w, b.x + b.w) - x, h: Math.max(a.y + a.h, b.y + b.h) - y };
1424
+ }
1425
+ function lineBoxesOf(texts) {
1426
+ const boxes = {};
1427
+ texts.forEach(({ key, box, runs }) => {
1428
+ (runs ?? (key === void 0 ? [] : [{ key, box }])).forEach((r3) => {
1429
+ boxes[r3.key] = union(boxes[r3.key], r3.box);
1430
+ });
1431
+ });
1432
+ return boxes;
1433
+ }
1434
+
1396
1435
  // src/render/overlap.ts
1397
1436
  var clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
1398
1437
  function placePicture(image, region, slot, fit) {
@@ -1615,23 +1654,6 @@ function drawLogo(ctx, doc, assets, fitted) {
1615
1654
  }
1616
1655
  return logo;
1617
1656
  }
1618
- function lineBoxesOf(texts) {
1619
- const boxes = {};
1620
- texts.forEach(({ key, box }) => {
1621
- if (key !== void 0) {
1622
- boxes[key] = union(boxes[key], box);
1623
- }
1624
- });
1625
- return boxes;
1626
- }
1627
- function union(a, b) {
1628
- if (a === void 0) {
1629
- return { ...b };
1630
- }
1631
- const x = Math.min(a.x, b.x);
1632
- const y = Math.min(a.y, b.y);
1633
- return { x, y, w: Math.max(a.x + a.w, b.x + b.w) - x, h: Math.max(a.y + a.h, b.y + b.h) - y };
1634
- }
1635
1657
  function render(input, assets, canvas) {
1636
1658
  const doc = checked(input, assets);
1637
1659
  const { width, height } = FORMAT_SIZES[doc.format];