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