@softure-ai/marketing-kit 0.17.0 → 0.18.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +45 -11
  3. package/dist/config/actions-schema.d.ts +14 -14
  4. package/dist/config/config.d.ts.map +1 -1
  5. package/dist/config/config.js +11 -1
  6. package/dist/config/config.js.map +1 -1
  7. package/dist/config/schema.d.ts +78 -70
  8. package/dist/config/schema.d.ts.map +1 -1
  9. package/dist/config/schema.js +25 -8
  10. package/dist/config/schema.js.map +1 -1
  11. package/dist/config/shot-steps.d.ts +16 -16
  12. package/dist/footage/compose.d.ts +17 -3
  13. package/dist/footage/compose.d.ts.map +1 -1
  14. package/dist/footage/compose.js +61 -17
  15. package/dist/footage/compose.js.map +1 -1
  16. package/dist/footage/declick.d.ts +49 -41
  17. package/dist/footage/declick.d.ts.map +1 -1
  18. package/dist/footage/declick.js +181 -92
  19. package/dist/footage/declick.js.map +1 -1
  20. package/dist/footage/film.d.ts +45 -2
  21. package/dist/footage/film.d.ts.map +1 -1
  22. package/dist/footage/film.js +23 -0
  23. package/dist/footage/film.js.map +1 -1
  24. package/dist/footage/paid.d.ts +1 -1
  25. package/dist/footage/paid.d.ts.map +1 -1
  26. package/dist/footage/paid.js +3 -2
  27. package/dist/footage/paid.js.map +1 -1
  28. package/dist/footage/pipeline.d.ts.map +1 -1
  29. package/dist/footage/pipeline.js +13 -8
  30. package/dist/footage/pipeline.js.map +1 -1
  31. package/dist/footage/render.d.ts +5 -1
  32. package/dist/footage/render.d.ts.map +1 -1
  33. package/dist/footage/render.js +6 -2
  34. package/dist/footage/render.js.map +1 -1
  35. package/dist/index.d.ts +4 -4
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +4 -4
  38. package/dist/index.js.map +1 -1
  39. package/dist/og/character-map.d.ts +2 -1
  40. package/dist/og/character-map.d.ts.map +1 -1
  41. package/dist/og/character-map.js +7 -63
  42. package/dist/og/character-map.js.map +1 -1
  43. package/dist/og/font-file.d.ts +53 -0
  44. package/dist/og/font-file.d.ts.map +1 -0
  45. package/dist/og/font-file.js +153 -0
  46. package/dist/og/font-file.js.map +1 -0
  47. package/dist/og/font-tables.d.ts +25 -0
  48. package/dist/og/font-tables.d.ts.map +1 -0
  49. package/dist/og/font-tables.js +212 -0
  50. package/dist/og/font-tables.js.map +1 -0
  51. package/dist/og/index.d.ts +3 -1
  52. package/dist/og/index.d.ts.map +1 -1
  53. package/dist/og/index.js +2 -1
  54. package/dist/og/index.js.map +1 -1
  55. package/dist/og/photo-cover.d.ts +22 -0
  56. package/dist/og/photo-cover.d.ts.map +1 -1
  57. package/dist/og/photo-cover.js +27 -9
  58. package/dist/og/photo-cover.js.map +1 -1
  59. package/package.json +1 -1
  60. package/schema/marketing.schema.json +76 -23
  61. package/src/config/config.ts +10 -2
  62. package/src/config/schema.ts +24 -5
  63. package/src/footage/compose.ts +72 -17
  64. package/src/footage/declick.ts +172 -97
  65. package/src/footage/film.ts +54 -2
  66. package/src/footage/paid.ts +3 -3
  67. package/src/footage/pipeline.ts +12 -7
  68. package/src/footage/render.ts +7 -3
  69. package/src/index.ts +26 -5
  70. package/src/og/character-map.ts +8 -61
  71. package/src/og/font-file.ts +172 -0
  72. package/src/og/font-tables.ts +221 -0
  73. package/src/og/index.ts +14 -0
  74. package/src/og/photo-cover.ts +46 -11
@@ -132,12 +132,30 @@ export interface PhotoCoverInput {
132
132
  id?: string;
133
133
  }
134
134
 
135
+ /** A box on the cover, in pixels: `left` and `top` included, `right` and `bottom` excluded. */
136
+ export interface CoverBox {
137
+ left: number;
138
+ top: number;
139
+ right: number;
140
+ bottom: number;
141
+ }
142
+
143
+ /** A point on the cover, in pixels from its top left corner. */
144
+ export interface CoverPoint {
145
+ x: number;
146
+ y: number;
147
+ }
148
+
135
149
  export interface PhotoCover {
136
150
  /** A baseline JPEG of exactly `width` × `height`, without an ICC profile (sRGB). */
137
151
  jpeg: Buffer;
138
152
  width: number;
139
153
  height: number;
140
154
  bytes: number;
155
+ /** The drawn text's bounds on the cover, its shadow included (the box the fit check measured); null for layout `photo`. */
156
+ textBox: CoverBox | null;
157
+ /** Where the focus point landed on the cover after the crop; off-centre when the photo's edge stops the shift. */
158
+ focusPoint: CoverPoint;
141
159
  }
142
160
 
143
161
  /** The text a cover draws, by layout. */
@@ -309,8 +327,8 @@ function checkTextGlyphs(tree: OgNode, fonts: OgFonts, text: CoverText): OgResul
309
327
  return err(`${sentences.join("; ")}. Use font files that cover them`);
310
328
  }
311
329
 
312
- /** The rows of `pixels` (RGBA, `width` wide) that hold anything drawn, or null when nothing is. */
313
- function findDrawnBox(pixels: Uint8Array, width: number, height: number): { top: number; bottom: number; left: number; right: number } | null {
330
+ /** The box of `pixels` (RGBA, `width` wide) that holds anything drawn (`right` and `bottom` excluded), or null when nothing is. */
331
+ function findDrawnBox(pixels: Uint8Array, width: number, height: number): CoverBox | null {
314
332
  let top = height;
315
333
  let bottom = -1;
316
334
  let left = width;
@@ -324,11 +342,11 @@ function findDrawnBox(pixels: Uint8Array, width: number, height: number): { top:
324
342
  right = Math.max(right, x);
325
343
  }
326
344
  }
327
- return bottom < 0 ? null : { top, bottom, left, right };
345
+ return bottom < 0 ? null : { left, top, right: right + 1, bottom: bottom + 1 };
328
346
  }
329
347
 
330
- /** The text as SVG and the rows it draws, or an error when it does not fit its part of the band. */
331
- async function layOutText(layout: TextLayout): Promise<OgResult<{ svg: string; top: number; bottom: number }>> {
348
+ /** The text as SVG and the box it draws, or an error when it does not fit its part of the band. */
349
+ async function layOutText(layout: TextLayout): Promise<OgResult<{ svg: string; box: CoverBox }>> {
332
350
  const tree = buildTextTree(layout);
333
351
  const glyphs = checkTextGlyphs(tree, layout.fonts, layout.text);
334
352
  if (!glyphs.ok) return glyphs;
@@ -348,14 +366,17 @@ async function layOutText(layout: TextLayout): Promise<OgResult<{ svg: string; t
348
366
  // The text keeps to the part of the band its layout names, so the photo's subject elsewhere stays clear.
349
367
  const region = LAYOUT_REGIONS[layout.text.layout];
350
368
  const rows = getRegionRows(band, region);
351
- const isInside = box.top >= rows.top && box.bottom < rows.bottom && box.left >= band.left && box.right < band.left + band.width;
369
+ const isInside = box.top >= rows.top && box.bottom <= rows.bottom && box.left >= band.left && box.right <= band.left + band.width;
352
370
  if (!isInside) {
353
371
  return err(`${name} ${isPlural ? "do" : "does"} not fit the ${region} of the centre 4:5 band (${String(band.width)}×${String(band.height)}); shorten ${isPlural ? "them" : "it"}`);
354
372
  }
355
- return ok({ svg, top: box.top, bottom: box.bottom });
373
+ return ok({ svg, box });
356
374
  }
357
375
 
358
- /** The scrim behind the text, as SVG: a gradient of the shade that follows the text's position; none for a pill (its box is its scrim). */
376
+ /**
377
+ * The scrim behind the text, as SVG: a gradient of the shade that follows the text's position; none for a pill (its
378
+ * box is its scrim). `text.top` is the text's first drawn row and `text.bottom` its last.
379
+ */
359
380
  function buildScrim(input: { size: readonly [number, number]; band: CoverBand; layout: CoverText["layout"]; text: { top: number; bottom: number }; shade: string }): string {
360
381
  const [width, height] = input.size;
361
382
  const { band, shade } = input;
@@ -418,7 +439,9 @@ export async function renderPhotoCover(input: PhotoCoverInput): Promise<OgResult
418
439
  const drawn = text.value === null ? null : await layOutText({ size, band, text: text.value, fonts: input.fonts, ink });
419
440
  if (drawn !== null && !drawn.ok) return err(`${name}: ${drawn.error}.`);
420
441
 
421
- const crop = getCoverCrop(getOrientedSize(info.value), size, options.focus);
442
+ const oriented = getOrientedSize(info.value);
443
+ const crop = getCoverCrop(oriented, size, options.focus);
444
+ const focusPoint = { x: crop.x + options.focus.x * oriented.width * crop.scale, y: crop.y + options.focus.y * oriented.height * crop.scale };
422
445
  const matrix = getOrientationMatrix(info.value.orientation, info.value.width, info.value.height).join(" ");
423
446
  const layers = [
424
447
  `<rect width="${String(width)}" height="${String(height)}" fill="${ink.shade}"/>`,
@@ -427,7 +450,7 @@ export async function renderPhotoCover(input: PhotoCoverInput): Promise<OgResult
427
450
  ];
428
451
  if (drawn !== null && text.value !== null) {
429
452
  layers.push(
430
- buildScrim({ size, band, layout: text.value.layout, text: drawn.value, shade: ink.shade }),
453
+ buildScrim({ size, band, layout: text.value.layout, text: { top: drawn.value.box.top, bottom: drawn.value.box.bottom - 1 }, shade: ink.shade }),
431
454
  `<image href="${toDataUri("image/svg+xml", drawn.value.svg)}" width="${String(width)}" height="${String(height)}"/>`,
432
455
  );
433
456
  }
@@ -435,8 +458,20 @@ export async function renderPhotoCover(input: PhotoCoverInput): Promise<OgResult
435
458
  try {
436
459
  const image = new Resvg(svg, { fitTo: { mode: "original" }, font: { loadSystemFonts: false } }).render();
437
460
  const encoded = jpeg.encode({ data: Buffer.from(image.pixels), width: image.width, height: image.height }, options.quality).data;
438
- return ok({ jpeg: encoded, width: image.width, height: image.height, bytes: encoded.length });
461
+ return ok({ jpeg: encoded, width: image.width, height: image.height, bytes: encoded.length, textBox: drawn === null ? null : { ...drawn.value.box }, focusPoint });
439
462
  } catch (error) {
440
463
  return err(`${name}: drawing the cover failed: ${error instanceof Error ? error.message : String(error)}.`);
441
464
  }
442
465
  }
466
+
467
+ /**
468
+ * Whether `point` lies under the cover's text: inside its `textBox` grown by `marginPx` on every side (0 by default).
469
+ * A cover without text (layout `photo`) covers no point. Use it with `focusPoint` to warn when the text hides the
470
+ * photo's subject.
471
+ */
472
+ export function isPointUnderCoverText(cover: Pick<PhotoCover, "textBox">, point: CoverPoint, marginPx = 0): boolean {
473
+ if (!Number.isFinite(marginPx) || marginPx < 0) throw new RangeError(`isPointUnderCoverText: the margin must be a finite number of pixels, 0 or more, not ${String(marginPx)}`);
474
+ const box = cover.textBox;
475
+ if (box === null) return false;
476
+ return point.x >= box.left - marginPx && point.x < box.right + marginPx && point.y >= box.top - marginPx && point.y < box.bottom + marginPx;
477
+ }