@softure-ai/marketing-kit 0.16.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.
- package/CHANGELOG.md +65 -0
- package/README.md +72 -19
- package/dist/config/actions-schema.d.ts +14 -14
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +11 -1
- package/dist/config/config.js.map +1 -1
- package/dist/config/schema.d.ts +78 -70
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +25 -8
- package/dist/config/schema.js.map +1 -1
- package/dist/config/shot-steps.d.ts +16 -16
- package/dist/footage/compose.d.ts +17 -3
- package/dist/footage/compose.d.ts.map +1 -1
- package/dist/footage/compose.js +61 -17
- package/dist/footage/compose.js.map +1 -1
- package/dist/footage/declick.d.ts +49 -41
- package/dist/footage/declick.d.ts.map +1 -1
- package/dist/footage/declick.js +181 -92
- package/dist/footage/declick.js.map +1 -1
- package/dist/footage/film.d.ts +45 -2
- package/dist/footage/film.d.ts.map +1 -1
- package/dist/footage/film.js +23 -0
- package/dist/footage/film.js.map +1 -1
- package/dist/footage/isolate-ranges.d.ts +10 -1
- package/dist/footage/isolate-ranges.d.ts.map +1 -1
- package/dist/footage/isolate-ranges.js +56 -10
- package/dist/footage/isolate-ranges.js.map +1 -1
- package/dist/footage/paid.d.ts +1 -1
- package/dist/footage/paid.d.ts.map +1 -1
- package/dist/footage/paid.js +3 -2
- package/dist/footage/paid.js.map +1 -1
- package/dist/footage/pipeline.d.ts +6 -0
- package/dist/footage/pipeline.d.ts.map +1 -1
- package/dist/footage/pipeline.js +19 -14
- package/dist/footage/pipeline.js.map +1 -1
- package/dist/footage/render.d.ts +5 -1
- package/dist/footage/render.d.ts.map +1 -1
- package/dist/footage/render.js +6 -2
- package/dist/footage/render.js.map +1 -1
- package/dist/footage/speech-ranges.d.ts +28 -1
- package/dist/footage/speech-ranges.d.ts.map +1 -1
- package/dist/footage/speech-ranges.js +47 -2
- package/dist/footage/speech-ranges.js.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/og/character-map.d.ts +2 -1
- package/dist/og/character-map.d.ts.map +1 -1
- package/dist/og/character-map.js +7 -63
- package/dist/og/character-map.js.map +1 -1
- package/dist/og/font-file.d.ts +53 -0
- package/dist/og/font-file.d.ts.map +1 -0
- package/dist/og/font-file.js +153 -0
- package/dist/og/font-file.js.map +1 -0
- package/dist/og/font-tables.d.ts +25 -0
- package/dist/og/font-tables.d.ts.map +1 -0
- package/dist/og/font-tables.js +212 -0
- package/dist/og/font-tables.js.map +1 -0
- package/dist/og/index.d.ts +3 -1
- package/dist/og/index.d.ts.map +1 -1
- package/dist/og/index.js +2 -1
- package/dist/og/index.js.map +1 -1
- package/dist/og/photo-cover.d.ts +49 -8
- package/dist/og/photo-cover.d.ts.map +1 -1
- package/dist/og/photo-cover.js +213 -53
- package/dist/og/photo-cover.js.map +1 -1
- package/package.json +1 -1
- package/schema/marketing.schema.json +76 -23
- package/src/config/config.ts +10 -2
- package/src/config/schema.ts +24 -5
- package/src/footage/compose.ts +72 -17
- package/src/footage/declick.ts +172 -97
- package/src/footage/film.ts +54 -2
- package/src/footage/isolate-ranges.ts +66 -11
- package/src/footage/paid.ts +3 -3
- package/src/footage/pipeline.ts +25 -14
- package/src/footage/render.ts +7 -3
- package/src/footage/speech-ranges.ts +55 -3
- package/src/index.ts +34 -5
- package/src/og/character-map.ts +8 -61
- package/src/og/font-file.ts +172 -0
- package/src/og/font-tables.ts +221 -0
- package/src/og/index.ts +19 -0
- package/src/og/photo-cover.ts +259 -62
package/src/og/photo-cover.ts
CHANGED
|
@@ -15,10 +15,10 @@ import { err, ok, type OgResult } from "./result.js";
|
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* A publish-ready cover for a vertical video from a photo the owner picks: the photo scaled to cover the frame and
|
|
18
|
-
* cropped around a focus point (EXIF orientation applied), and
|
|
19
|
-
*
|
|
20
|
-
* grid. Satori
|
|
21
|
-
*
|
|
18
|
+
* cropped around a focus point (EXIF orientation applied), and optional text in the brand's display font laid out by
|
|
19
|
+
* one of `PHOTO_COVER_LAYOUTS`, kept inside the centre 4:5 band the platforms keep when they crop the cover for the
|
|
20
|
+
* profile grid. Satori lays the text out, resvg draws the whole cover, `jpeg-js` encodes it: no network, no system
|
|
21
|
+
* fonts.
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
/** A Reels, Shorts and TikTok cover. */
|
|
@@ -29,10 +29,35 @@ export const PHOTO_COVER_QUALITY = 90;
|
|
|
29
29
|
/** The longest caption accepted before layout; whether a shorter one fits is measured on the drawn caption. */
|
|
30
30
|
export const MAX_COVER_CAPTION_LENGTH = 80;
|
|
31
31
|
|
|
32
|
+
/** The longest kicker (the small line above the title in `kicker-title`). */
|
|
33
|
+
export const MAX_COVER_KICKER_LENGTH = 40;
|
|
34
|
+
|
|
35
|
+
/** The longest label of the `pill` layout (the caption set in a rounded box). */
|
|
36
|
+
export const MAX_COVER_PILL_LENGTH = 32;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Where the cover's text goes. `photo`: no text. `title-bottom`, `title-center`, `title-top`: the caption in the lower
|
|
40
|
+
* half, the middle third or the upper half of the band. `kicker-title`: a small kicker line above the caption, in the
|
|
41
|
+
* lower half. `pill`: the caption as a short label in a rounded box, in the lower half.
|
|
42
|
+
*/
|
|
43
|
+
export const PHOTO_COVER_LAYOUTS = ["photo", "title-bottom", "title-center", "title-top", "kicker-title", "pill"] as const;
|
|
44
|
+
|
|
45
|
+
export type PhotoCoverLayout = (typeof PHOTO_COVER_LAYOUTS)[number];
|
|
46
|
+
|
|
47
|
+
/** `light`: light text over a dark scrim. `dark`: dark text over a light scrim. */
|
|
48
|
+
export type PhotoCoverTone = "light" | "dark";
|
|
49
|
+
|
|
32
50
|
/** Sizes in this file are written for a 1080 px wide band and scaled with its width. */
|
|
33
51
|
const BAND_BASE_WIDTH = 1080;
|
|
34
52
|
const CAPTION_SIDE_PADDING = 80;
|
|
35
53
|
const CAPTION_BOTTOM_PADDING = 120;
|
|
54
|
+
const CAPTION_TOP_PADDING = 120;
|
|
55
|
+
const KICKER_SIZE = 44;
|
|
56
|
+
const KICKER_GAP = 20;
|
|
57
|
+
const PILL_SIZE = 56;
|
|
58
|
+
const PILL_PADDING: readonly [number, number] = [22, 48];
|
|
59
|
+
/** The pill box's opacity: the box is the pill's scrim. */
|
|
60
|
+
const PILL_ALPHA = 0xd9;
|
|
36
61
|
/** A pixel whose alpha is above this counts as drawn when the caption is measured (the shadow's faint fringe does not). */
|
|
37
62
|
const DRAWN_ALPHA = 16;
|
|
38
63
|
/** The scrim's opacity at the band's lower edge and below it. */
|
|
@@ -65,12 +90,20 @@ const coverOptionsSchema = z.object({
|
|
|
65
90
|
photo: z.union([z.string().min(1), z.instanceof(Buffer)]),
|
|
66
91
|
size: z.tuple([z.number().int().min(64).max(4096), z.number().int().min(64).max(4096)]).default([...PHOTO_COVER_SIZE] as [number, number]),
|
|
67
92
|
focus: z.object({ x: z.number().min(0).max(1), y: z.number().min(0).max(1) }).default({ x: 0.5, y: 0.5 }),
|
|
93
|
+
layout: z.enum(PHOTO_COVER_LAYOUTS).optional(),
|
|
68
94
|
caption: z
|
|
69
95
|
.string()
|
|
70
96
|
.trim()
|
|
71
97
|
.min(1, "the caption is empty; leave it out for a cover without one")
|
|
72
98
|
.max(MAX_COVER_CAPTION_LENGTH, `the caption is longer than ${String(MAX_COVER_CAPTION_LENGTH)} characters`)
|
|
73
99
|
.optional(),
|
|
100
|
+
kicker: z
|
|
101
|
+
.string()
|
|
102
|
+
.trim()
|
|
103
|
+
.min(1, "the kicker is empty")
|
|
104
|
+
.max(MAX_COVER_KICKER_LENGTH, `the kicker is longer than ${String(MAX_COVER_KICKER_LENGTH)} characters`)
|
|
105
|
+
.optional(),
|
|
106
|
+
tone: z.enum(["light", "dark"]).optional(),
|
|
74
107
|
quality: z.number().int().min(1).max(100).default(PHOTO_COVER_QUALITY),
|
|
75
108
|
});
|
|
76
109
|
|
|
@@ -81,11 +114,17 @@ export interface PhotoCoverInput {
|
|
|
81
114
|
size?: readonly [number, number];
|
|
82
115
|
/** The point of the upright photo to keep in view, as fractions of its width and height; the centre by default. */
|
|
83
116
|
focus?: FocusPoint;
|
|
84
|
-
/**
|
|
117
|
+
/** Where the text goes (`PHOTO_COVER_LAYOUTS`); `title-bottom` with a caption and `photo` without one by default. */
|
|
118
|
+
layout?: PhotoCoverLayout;
|
|
119
|
+
/** The title (2 to 6 words fit best), or the `pill` label; required by every layout but `photo`, refused by it. */
|
|
85
120
|
caption?: string;
|
|
86
|
-
/** The
|
|
121
|
+
/** The small line above the title; required by `kicker-title` and refused by the other layouts. */
|
|
122
|
+
kicker?: string;
|
|
123
|
+
/** `light` text over a dark scrim, or `dark` text over a light one; `light` by default. */
|
|
124
|
+
tone?: PhotoCoverTone;
|
|
125
|
+
/** The light colour is the lighter of `foreground` and `background`, the dark colour the darker. */
|
|
87
126
|
colors: BrandColors;
|
|
88
|
-
/** The brand's fonts (`loadOgFonts`); the
|
|
127
|
+
/** The brand's fonts (`loadOgFonts`); the text is set in the heading (display) family. */
|
|
89
128
|
fonts: OgFonts;
|
|
90
129
|
/** JPEG quality, 1-100; 90 by default. */
|
|
91
130
|
quality?: number;
|
|
@@ -93,12 +132,69 @@ export interface PhotoCoverInput {
|
|
|
93
132
|
id?: string;
|
|
94
133
|
}
|
|
95
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
|
+
|
|
96
149
|
export interface PhotoCover {
|
|
97
150
|
/** A baseline JPEG of exactly `width` × `height`, without an ICC profile (sRGB). */
|
|
98
151
|
jpeg: Buffer;
|
|
99
152
|
width: number;
|
|
100
153
|
height: number;
|
|
101
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;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The text a cover draws, by layout. */
|
|
162
|
+
type CoverText =
|
|
163
|
+
| { layout: "title-bottom" | "title-center" | "title-top"; caption: string }
|
|
164
|
+
| { layout: "kicker-title"; caption: string; kicker: string }
|
|
165
|
+
| { layout: "pill"; caption: string };
|
|
166
|
+
|
|
167
|
+
type CoverRegion = "lower half" | "upper half" | "middle third";
|
|
168
|
+
|
|
169
|
+
/** The part of the band each text layout keeps to. */
|
|
170
|
+
const LAYOUT_REGIONS: Record<CoverText["layout"], CoverRegion> = {
|
|
171
|
+
"title-bottom": "lower half",
|
|
172
|
+
"title-center": "middle third",
|
|
173
|
+
"title-top": "upper half",
|
|
174
|
+
"kicker-title": "lower half",
|
|
175
|
+
pill: "lower half",
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** The rows of `region` in the band: `top` included, `bottom` excluded. */
|
|
179
|
+
function getRegionRows(band: CoverBand, region: CoverRegion): { top: number; bottom: number } {
|
|
180
|
+
const bandBottom = band.top + band.height;
|
|
181
|
+
if (region === "lower half") return { top: band.top + Math.floor(band.height / 2), bottom: bandBottom };
|
|
182
|
+
if (region === "upper half") return { top: band.top, bottom: band.top + Math.floor(band.height / 2) };
|
|
183
|
+
return { top: band.top + Math.floor(band.height / 3), bottom: band.top + Math.floor((2 * band.height) / 3) };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The text the options ask for, or an error when the layout and the texts do not go together. */
|
|
187
|
+
function resolveCoverText(options: { layout?: PhotoCoverLayout | undefined; caption?: string | undefined; kicker?: string | undefined }): OgResult<CoverText | null> {
|
|
188
|
+
const { caption, kicker } = options;
|
|
189
|
+
const layout = options.layout ?? (caption === undefined ? "photo" : "title-bottom");
|
|
190
|
+
if (kicker !== undefined && layout !== "kicker-title") return err(`the kicker is drawn only by the layout "kicker-title", not "${layout}"; leave it out`);
|
|
191
|
+
if (layout === "photo") return caption === undefined ? ok(null) : err('the layout "photo" draws no text; leave the caption out or pick a text layout');
|
|
192
|
+
if (caption === undefined) return err(`the layout "${layout}" needs a caption`);
|
|
193
|
+
if (layout === "kicker-title") return kicker === undefined ? err('the layout "kicker-title" needs a kicker') : ok({ layout, caption, kicker });
|
|
194
|
+
if (layout === "pill" && caption.length > MAX_COVER_PILL_LENGTH) {
|
|
195
|
+
return err(`the pill label ${JSON.stringify(caption)} is longer than ${String(MAX_COVER_PILL_LENGTH)} characters`);
|
|
196
|
+
}
|
|
197
|
+
return ok({ layout, caption });
|
|
102
198
|
}
|
|
103
199
|
|
|
104
200
|
function describeCover(id: string | undefined): string {
|
|
@@ -124,28 +220,72 @@ function getLuminance(color: string): number {
|
|
|
124
220
|
return 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5);
|
|
125
221
|
}
|
|
126
222
|
|
|
127
|
-
/** The brand's light and dark colour
|
|
128
|
-
function getCoverInk(colors: BrandColors): { text: string; shade: string } {
|
|
223
|
+
/** The text colour and the scrim (and shadow) colour: the brand's light and dark colour, swapped for a `dark` tone. */
|
|
224
|
+
function getCoverInk(colors: BrandColors, tone: PhotoCoverTone): { text: string; shade: string } {
|
|
129
225
|
const foreground = toOpaqueHex(colors.foreground);
|
|
130
226
|
const background = toOpaqueHex(colors.background);
|
|
131
|
-
|
|
227
|
+
const [light, dark] = getLuminance(foreground) >= getLuminance(background) ? [foreground, background] : [background, foreground];
|
|
228
|
+
return tone === "light" ? { text: light, shade: dark } : { text: dark, shade: light };
|
|
132
229
|
}
|
|
133
230
|
|
|
134
|
-
interface
|
|
231
|
+
interface TextLayout {
|
|
135
232
|
size: readonly [number, number];
|
|
136
233
|
band: CoverBand;
|
|
137
|
-
|
|
234
|
+
text: CoverText;
|
|
138
235
|
fonts: OgFonts;
|
|
139
236
|
ink: { text: string; shade: string };
|
|
140
237
|
}
|
|
141
238
|
|
|
142
|
-
/**
|
|
143
|
-
function
|
|
239
|
+
/** Where the text block sits: its foot above the band's lower edge, its head below the band's top, or centred in the middle third. */
|
|
240
|
+
function getBlockPlacement(layout: TextLayout, scale: number): Record<string, number | string> {
|
|
241
|
+
const [, height] = layout.size;
|
|
242
|
+
const { band } = layout;
|
|
243
|
+
const region = LAYOUT_REGIONS[layout.text.layout];
|
|
244
|
+
if (region === "lower half") return { bottom: height - (band.top + band.height) + Math.round(CAPTION_BOTTOM_PADDING * scale) };
|
|
245
|
+
if (region === "upper half") return { top: band.top + Math.round(CAPTION_TOP_PADDING * scale) };
|
|
246
|
+
const rows = getRegionRows(band, region);
|
|
247
|
+
return { top: rows.top, height: rows.bottom - rows.top, justifyContent: "center" };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The text alone on a transparent frame, laid out by its layout and centred across the band. */
|
|
251
|
+
function buildTextTree(layout: TextLayout): OgNode {
|
|
144
252
|
const [width, height] = layout.size;
|
|
145
|
-
const { band, fonts, ink } = layout;
|
|
253
|
+
const { band, fonts, ink, text } = layout;
|
|
146
254
|
const scale = band.width / BAND_BASE_WIDTH;
|
|
147
255
|
const side = Math.round(CAPTION_SIDE_PADDING * scale);
|
|
148
|
-
const
|
|
256
|
+
const font = { fontFamily: toFontFamilyCss(fonts.heading), fontWeight: pickWeight(fonts.heading.weights, 700) };
|
|
257
|
+
const shadow = `0px ${String(Math.round(4 * scale))}px ${String(Math.round(24 * scale))}px ${withAlpha(ink.shade, 0xb3)}`;
|
|
258
|
+
const line = (content: string, style: Record<string, number | string>) =>
|
|
259
|
+
h("div", { style: { display: "flex", justifyContent: "center", width: "100%", textAlign: "center", wordBreak: "break-word", color: ink.text, ...font, ...style } }, content);
|
|
260
|
+
const children =
|
|
261
|
+
text.layout === "pill"
|
|
262
|
+
? [
|
|
263
|
+
h(
|
|
264
|
+
"div",
|
|
265
|
+
{
|
|
266
|
+
style: {
|
|
267
|
+
display: "flex",
|
|
268
|
+
maxWidth: "100%",
|
|
269
|
+
padding: `${String(Math.round(PILL_PADDING[0] * scale))}px ${String(Math.round(PILL_PADDING[1] * scale))}px`,
|
|
270
|
+
borderRadius: Math.round(PILL_SIZE * scale),
|
|
271
|
+
backgroundColor: withAlpha(ink.shade, PILL_ALPHA),
|
|
272
|
+
textAlign: "center",
|
|
273
|
+
wordBreak: "break-word",
|
|
274
|
+
color: ink.text,
|
|
275
|
+
fontSize: Math.round(PILL_SIZE * scale),
|
|
276
|
+
lineHeight: 1.15,
|
|
277
|
+
...font,
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
text.caption,
|
|
281
|
+
),
|
|
282
|
+
]
|
|
283
|
+
: [
|
|
284
|
+
...(text.layout === "kicker-title"
|
|
285
|
+
? [line(text.kicker, { fontSize: Math.round(KICKER_SIZE * scale), lineHeight: 1.2, letterSpacing: Math.round(3 * scale), marginBottom: Math.round(KICKER_GAP * scale), textShadow: shadow })]
|
|
286
|
+
: []),
|
|
287
|
+
line(text.caption, { fontSize: Math.round(getCoverCaptionSize(text.caption) * scale), lineHeight: 1.08, textShadow: shadow }),
|
|
288
|
+
];
|
|
149
289
|
return h(
|
|
150
290
|
"div",
|
|
151
291
|
{ style: { display: "flex", position: "relative", width, height } },
|
|
@@ -155,38 +295,40 @@ function buildCaptionTree(layout: CaptionLayout): OgNode {
|
|
|
155
295
|
style: {
|
|
156
296
|
position: "absolute",
|
|
157
297
|
display: "flex",
|
|
158
|
-
|
|
298
|
+
flexDirection: "column",
|
|
299
|
+
alignItems: "center",
|
|
159
300
|
left: band.left + side,
|
|
160
301
|
width: band.width - 2 * side,
|
|
161
|
-
|
|
162
|
-
fontFamily: toFontFamilyCss(fonts.heading),
|
|
163
|
-
fontSize,
|
|
164
|
-
fontWeight: pickWeight(fonts.heading.weights, 700),
|
|
165
|
-
lineHeight: 1.08,
|
|
166
|
-
textAlign: "center",
|
|
167
|
-
wordBreak: "break-word",
|
|
168
|
-
color: ink.text,
|
|
169
|
-
textShadow: `0px ${String(Math.round(4 * scale))}px ${String(Math.round(24 * scale))}px ${withAlpha(ink.shade, 0xb3)}`,
|
|
302
|
+
...getBlockPlacement(layout, scale),
|
|
170
303
|
},
|
|
171
304
|
},
|
|
172
|
-
|
|
305
|
+
...children,
|
|
173
306
|
),
|
|
174
307
|
);
|
|
175
308
|
}
|
|
176
309
|
|
|
177
|
-
|
|
310
|
+
/** How errors name the cover's text, e.g. `the kicker "New" and the caption "Lisbon"`. */
|
|
311
|
+
function describeText(text: CoverText): { name: string; isPlural: boolean } {
|
|
312
|
+
if (text.layout === "pill") return { name: `the pill label ${JSON.stringify(text.caption)}`, isPlural: false };
|
|
313
|
+
if (text.layout === "kicker-title") return { name: `the kicker ${JSON.stringify(text.kicker)} and the caption ${JSON.stringify(text.caption)}`, isPlural: true };
|
|
314
|
+
return { name: `the caption ${JSON.stringify(text.caption)}`, isPlural: false };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function checkTextGlyphs(tree: OgNode, fonts: OgFonts, text: CoverText): OgResult<null> {
|
|
178
318
|
const missing = findMissingGlyphs(tree, fonts.satoriFonts);
|
|
179
319
|
if (!missing.ok) return err(`reading ${missing.error}`);
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
320
|
+
const sentences = missing.value
|
|
321
|
+
.filter((entry) => entry.characters.length > 0)
|
|
322
|
+
.map((entry) => {
|
|
323
|
+
const role = text.layout === "pill" ? "pill label" : text.layout === "kicker-title" && entry.text === text.kicker ? "kicker" : "caption";
|
|
324
|
+
return `the ${role} ${JSON.stringify(entry.text)} has characters none of the loaded fonts can draw at its weight: ${entry.characters.map(describeCharacter).join(", ")}`;
|
|
325
|
+
});
|
|
326
|
+
if (sentences.length === 0) return ok(null);
|
|
327
|
+
return err(`${sentences.join("; ")}. Use font files that cover them`);
|
|
186
328
|
}
|
|
187
329
|
|
|
188
|
-
/** The
|
|
189
|
-
function findDrawnBox(pixels: Uint8Array, width: number, height: number):
|
|
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 {
|
|
190
332
|
let top = height;
|
|
191
333
|
let bottom = -1;
|
|
192
334
|
let left = width;
|
|
@@ -200,13 +342,13 @@ function findDrawnBox(pixels: Uint8Array, width: number, height: number): { top:
|
|
|
200
342
|
right = Math.max(right, x);
|
|
201
343
|
}
|
|
202
344
|
}
|
|
203
|
-
return bottom < 0 ? null : {
|
|
345
|
+
return bottom < 0 ? null : { left, top, right: right + 1, bottom: bottom + 1 };
|
|
204
346
|
}
|
|
205
347
|
|
|
206
|
-
/** The
|
|
207
|
-
async function
|
|
208
|
-
const tree =
|
|
209
|
-
const glyphs =
|
|
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 }>> {
|
|
350
|
+
const tree = buildTextTree(layout);
|
|
351
|
+
const glyphs = checkTextGlyphs(tree, layout.fonts, layout.text);
|
|
210
352
|
if (!glyphs.ok) return glyphs;
|
|
211
353
|
const [width, height] = layout.size;
|
|
212
354
|
let svg: string;
|
|
@@ -214,31 +356,77 @@ async function layOutCaption(layout: CaptionLayout): Promise<OgResult<{ svg: str
|
|
|
214
356
|
// Satori is typed for React elements; it reads only `type` and `props`, which `OgNode` has.
|
|
215
357
|
svg = await satori(tree as unknown as Parameters<typeof satori>[0], { width, height, fonts: layout.fonts.satoriFonts });
|
|
216
358
|
} catch (error) {
|
|
217
|
-
return err(`laying out the
|
|
359
|
+
return err(`laying out the text failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
218
360
|
}
|
|
219
361
|
const pixels = new Resvg(svg, { fitTo: { mode: "original" }, font: { loadSystemFonts: false } }).render().pixels;
|
|
220
362
|
const box = findDrawnBox(pixels, width, height);
|
|
221
|
-
|
|
363
|
+
const { name, isPlural } = describeText(layout.text);
|
|
364
|
+
if (box === null) return err(`${name} ${isPlural ? "draw" : "draws"} nothing`);
|
|
222
365
|
const { band } = layout;
|
|
223
|
-
// The
|
|
224
|
-
const
|
|
366
|
+
// The text keeps to the part of the band its layout names, so the photo's subject elsewhere stays clear.
|
|
367
|
+
const region = LAYOUT_REGIONS[layout.text.layout];
|
|
368
|
+
const rows = getRegionRows(band, region);
|
|
369
|
+
const isInside = box.top >= rows.top && box.bottom <= rows.bottom && box.left >= band.left && box.right <= band.left + band.width;
|
|
225
370
|
if (!isInside) {
|
|
226
|
-
return err(
|
|
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"}`);
|
|
227
372
|
}
|
|
228
|
-
return ok({ svg,
|
|
373
|
+
return ok({ svg, box });
|
|
374
|
+
}
|
|
375
|
+
|
|
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
|
+
*/
|
|
380
|
+
function buildScrim(input: { size: readonly [number, number]; band: CoverBand; layout: CoverText["layout"]; text: { top: number; bottom: number }; shade: string }): string {
|
|
381
|
+
const [width, height] = input.size;
|
|
382
|
+
const { band, shade } = input;
|
|
383
|
+
const region = LAYOUT_REGIONS[input.layout];
|
|
384
|
+
if (input.layout === "pill") return "";
|
|
385
|
+
const fade = Math.round(band.height / 5);
|
|
386
|
+
const alpha = (SCRIM_ALPHA / 255).toFixed(3);
|
|
387
|
+
const stop = (offset: number, opacity: string) => `<stop offset="${offset.toFixed(4)}" stop-color="${shade}" stop-opacity="${opacity}"/>`;
|
|
388
|
+
const gradient = (y1: number, y2: number, stops: string) =>
|
|
389
|
+
`<defs><linearGradient id="scrim" gradientUnits="userSpaceOnUse" x1="0" y1="${String(y1)}" x2="0" y2="${String(y2)}">${stops}</linearGradient></defs>`;
|
|
390
|
+
const rect = (top: number, bottom: number) => `<rect y="${String(top)}" width="${String(width)}" height="${String(bottom - top)}" fill="url(#scrim)"/>`;
|
|
391
|
+
if (region === "lower half") {
|
|
392
|
+
// Fades in over a fifth of the band above the text and holds from the band's lower edge down.
|
|
393
|
+
const start = Math.max(0, input.text.top - fade);
|
|
394
|
+
return gradient(start, band.top + band.height, stop(0, "0") + stop(1, alpha)) + rect(start, height);
|
|
395
|
+
}
|
|
396
|
+
if (region === "upper half") {
|
|
397
|
+
// Holds from the frame's top to the band's top and fades out over a fifth of the band below the text.
|
|
398
|
+
const end = Math.min(height, input.text.bottom + fade);
|
|
399
|
+
return gradient(band.top, end, stop(0, alpha) + stop(1, "0")) + rect(0, end);
|
|
400
|
+
}
|
|
401
|
+
// Fades in above the text, holds behind it and fades out below it.
|
|
402
|
+
const start = Math.max(0, input.text.top - fade);
|
|
403
|
+
const end = Math.min(height, input.text.bottom + fade);
|
|
404
|
+
const span = Math.max(1, end - start);
|
|
405
|
+
return gradient(start, end, stop(0, "0") + stop((input.text.top - start) / span, alpha) + stop((input.text.bottom - start) / span, alpha) + stop(1, "0")) + rect(start, end);
|
|
229
406
|
}
|
|
230
407
|
|
|
231
408
|
const toDataUri = (type: string, bytes: Buffer | string) => `data:${type};base64,${Buffer.from(bytes).toString("base64")}`;
|
|
232
409
|
|
|
233
|
-
/** Renders the cover: the photo cropped to `size` around the focus point, the
|
|
410
|
+
/** Renders the cover: the photo cropped to `size` around the focus point, the layout's text (if any) over a soft scrim. */
|
|
234
411
|
export async function renderPhotoCover(input: PhotoCoverInput): Promise<OgResult<PhotoCover>> {
|
|
235
412
|
const name = describeCover(input.id);
|
|
236
|
-
const parsed = coverOptionsSchema.safeParse({
|
|
413
|
+
const parsed = coverOptionsSchema.safeParse({
|
|
414
|
+
photo: input.photo,
|
|
415
|
+
size: input.size,
|
|
416
|
+
focus: input.focus,
|
|
417
|
+
layout: input.layout,
|
|
418
|
+
caption: input.caption,
|
|
419
|
+
kicker: input.kicker,
|
|
420
|
+
tone: input.tone,
|
|
421
|
+
quality: input.quality,
|
|
422
|
+
});
|
|
237
423
|
if (!parsed.success) {
|
|
238
424
|
const lines = parsed.error.issues.map((issue) => ` ${issue.path.length > 0 ? issue.path.join(".") : "input"}: ${issue.message}`);
|
|
239
425
|
return err(`${name}: the input is not valid:\n${lines.join("\n")}`);
|
|
240
426
|
}
|
|
241
427
|
const options = parsed.data;
|
|
428
|
+
const text = resolveCoverText(options);
|
|
429
|
+
if (!text.ok) return err(`${name}: ${text.error}.`);
|
|
242
430
|
const bytes = readPhoto(options.photo);
|
|
243
431
|
if (!bytes.ok) return err(`${name}: ${bytes.error}.`);
|
|
244
432
|
const info = readPhotoInfo(bytes.value);
|
|
@@ -247,34 +435,43 @@ export async function renderPhotoCover(input: PhotoCoverInput): Promise<OgResult
|
|
|
247
435
|
const size = options.size;
|
|
248
436
|
const [width, height] = size;
|
|
249
437
|
const band = getCoverBand(size);
|
|
250
|
-
const ink = getCoverInk(input.colors);
|
|
251
|
-
const
|
|
252
|
-
if (
|
|
438
|
+
const ink = getCoverInk(input.colors, options.tone ?? "light");
|
|
439
|
+
const drawn = text.value === null ? null : await layOutText({ size, band, text: text.value, fonts: input.fonts, ink });
|
|
440
|
+
if (drawn !== null && !drawn.ok) return err(`${name}: ${drawn.error}.`);
|
|
253
441
|
|
|
254
|
-
const
|
|
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 };
|
|
255
445
|
const matrix = getOrientationMatrix(info.value.orientation, info.value.width, info.value.height).join(" ");
|
|
256
446
|
const layers = [
|
|
257
447
|
`<rect width="${String(width)}" height="${String(height)}" fill="${ink.shade}"/>`,
|
|
258
448
|
`<image href="${toDataUri(`image/${info.value.format}`, bytes.value)}" width="${String(info.value.width)}" height="${String(info.value.height)}" preserveAspectRatio="none" ` +
|
|
259
449
|
`transform="translate(${String(crop.x)} ${String(crop.y)}) scale(${String(crop.scale)}) matrix(${matrix})"/>`,
|
|
260
450
|
];
|
|
261
|
-
if (
|
|
262
|
-
// The scrim fades in over a fifth of the band above the caption and holds from the band's lower edge down.
|
|
263
|
-
const bandBottom = band.top + band.height;
|
|
264
|
-
const start = Math.max(0, caption.value.top - Math.round(band.height / 5));
|
|
451
|
+
if (drawn !== null && text.value !== null) {
|
|
265
452
|
layers.push(
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
`<rect y="${String(start)}" width="${String(width)}" height="${String(height - start)}" fill="url(#scrim)"/>`,
|
|
269
|
-
`<image href="${toDataUri("image/svg+xml", caption.value.svg)}" width="${String(width)}" height="${String(height)}"/>`,
|
|
453
|
+
buildScrim({ size, band, layout: text.value.layout, text: { top: drawn.value.box.top, bottom: drawn.value.box.bottom - 1 }, shade: ink.shade }),
|
|
454
|
+
`<image href="${toDataUri("image/svg+xml", drawn.value.svg)}" width="${String(width)}" height="${String(height)}"/>`,
|
|
270
455
|
);
|
|
271
456
|
}
|
|
272
457
|
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${String(width)}" height="${String(height)}" viewBox="0 0 ${String(width)} ${String(height)}">${layers.join("")}</svg>`;
|
|
273
458
|
try {
|
|
274
459
|
const image = new Resvg(svg, { fitTo: { mode: "original" }, font: { loadSystemFonts: false } }).render();
|
|
275
460
|
const encoded = jpeg.encode({ data: Buffer.from(image.pixels), width: image.width, height: image.height }, options.quality).data;
|
|
276
|
-
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 });
|
|
277
462
|
} catch (error) {
|
|
278
463
|
return err(`${name}: drawing the cover failed: ${error instanceof Error ? error.message : String(error)}.`);
|
|
279
464
|
}
|
|
280
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
|
+
}
|