@softure-ai/marketing-kit 0.16.0 → 0.17.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 +25 -0
- package/README.md +27 -8
- 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/pipeline.d.ts +6 -0
- package/dist/footage/pipeline.d.ts.map +1 -1
- package/dist/footage/pipeline.js +6 -6
- package/dist/footage/pipeline.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 +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/og/index.d.ts +1 -1
- package/dist/og/index.d.ts.map +1 -1
- package/dist/og/index.js +1 -1
- package/dist/og/index.js.map +1 -1
- package/dist/og/photo-cover.d.ts +27 -8
- package/dist/og/photo-cover.d.ts.map +1 -1
- package/dist/og/photo-cover.js +191 -49
- package/dist/og/photo-cover.js.map +1 -1
- package/package.json +1 -1
- package/src/footage/isolate-ranges.ts +66 -11
- package/src/footage/pipeline.ts +13 -7
- package/src/footage/speech-ranges.ts +55 -3
- package/src/index.ts +8 -0
- package/src/og/index.ts +5 -0
- package/src/og/photo-cover.ts +219 -57
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;
|
|
@@ -101,6 +140,45 @@ export interface PhotoCover {
|
|
|
101
140
|
bytes: number;
|
|
102
141
|
}
|
|
103
142
|
|
|
143
|
+
/** The text a cover draws, by layout. */
|
|
144
|
+
type CoverText =
|
|
145
|
+
| { layout: "title-bottom" | "title-center" | "title-top"; caption: string }
|
|
146
|
+
| { layout: "kicker-title"; caption: string; kicker: string }
|
|
147
|
+
| { layout: "pill"; caption: string };
|
|
148
|
+
|
|
149
|
+
type CoverRegion = "lower half" | "upper half" | "middle third";
|
|
150
|
+
|
|
151
|
+
/** The part of the band each text layout keeps to. */
|
|
152
|
+
const LAYOUT_REGIONS: Record<CoverText["layout"], CoverRegion> = {
|
|
153
|
+
"title-bottom": "lower half",
|
|
154
|
+
"title-center": "middle third",
|
|
155
|
+
"title-top": "upper half",
|
|
156
|
+
"kicker-title": "lower half",
|
|
157
|
+
pill: "lower half",
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** The rows of `region` in the band: `top` included, `bottom` excluded. */
|
|
161
|
+
function getRegionRows(band: CoverBand, region: CoverRegion): { top: number; bottom: number } {
|
|
162
|
+
const bandBottom = band.top + band.height;
|
|
163
|
+
if (region === "lower half") return { top: band.top + Math.floor(band.height / 2), bottom: bandBottom };
|
|
164
|
+
if (region === "upper half") return { top: band.top, bottom: band.top + Math.floor(band.height / 2) };
|
|
165
|
+
return { top: band.top + Math.floor(band.height / 3), bottom: band.top + Math.floor((2 * band.height) / 3) };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The text the options ask for, or an error when the layout and the texts do not go together. */
|
|
169
|
+
function resolveCoverText(options: { layout?: PhotoCoverLayout | undefined; caption?: string | undefined; kicker?: string | undefined }): OgResult<CoverText | null> {
|
|
170
|
+
const { caption, kicker } = options;
|
|
171
|
+
const layout = options.layout ?? (caption === undefined ? "photo" : "title-bottom");
|
|
172
|
+
if (kicker !== undefined && layout !== "kicker-title") return err(`the kicker is drawn only by the layout "kicker-title", not "${layout}"; leave it out`);
|
|
173
|
+
if (layout === "photo") return caption === undefined ? ok(null) : err('the layout "photo" draws no text; leave the caption out or pick a text layout');
|
|
174
|
+
if (caption === undefined) return err(`the layout "${layout}" needs a caption`);
|
|
175
|
+
if (layout === "kicker-title") return kicker === undefined ? err('the layout "kicker-title" needs a kicker') : ok({ layout, caption, kicker });
|
|
176
|
+
if (layout === "pill" && caption.length > MAX_COVER_PILL_LENGTH) {
|
|
177
|
+
return err(`the pill label ${JSON.stringify(caption)} is longer than ${String(MAX_COVER_PILL_LENGTH)} characters`);
|
|
178
|
+
}
|
|
179
|
+
return ok({ layout, caption });
|
|
180
|
+
}
|
|
181
|
+
|
|
104
182
|
function describeCover(id: string | undefined): string {
|
|
105
183
|
return id === undefined ? "Photo cover" : `Photo cover "${id}"`;
|
|
106
184
|
}
|
|
@@ -124,28 +202,72 @@ function getLuminance(color: string): number {
|
|
|
124
202
|
return 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5);
|
|
125
203
|
}
|
|
126
204
|
|
|
127
|
-
/** The brand's light and dark colour
|
|
128
|
-
function getCoverInk(colors: BrandColors): { text: string; shade: string } {
|
|
205
|
+
/** The text colour and the scrim (and shadow) colour: the brand's light and dark colour, swapped for a `dark` tone. */
|
|
206
|
+
function getCoverInk(colors: BrandColors, tone: PhotoCoverTone): { text: string; shade: string } {
|
|
129
207
|
const foreground = toOpaqueHex(colors.foreground);
|
|
130
208
|
const background = toOpaqueHex(colors.background);
|
|
131
|
-
|
|
209
|
+
const [light, dark] = getLuminance(foreground) >= getLuminance(background) ? [foreground, background] : [background, foreground];
|
|
210
|
+
return tone === "light" ? { text: light, shade: dark } : { text: dark, shade: light };
|
|
132
211
|
}
|
|
133
212
|
|
|
134
|
-
interface
|
|
213
|
+
interface TextLayout {
|
|
135
214
|
size: readonly [number, number];
|
|
136
215
|
band: CoverBand;
|
|
137
|
-
|
|
216
|
+
text: CoverText;
|
|
138
217
|
fonts: OgFonts;
|
|
139
218
|
ink: { text: string; shade: string };
|
|
140
219
|
}
|
|
141
220
|
|
|
142
|
-
/**
|
|
143
|
-
function
|
|
221
|
+
/** 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. */
|
|
222
|
+
function getBlockPlacement(layout: TextLayout, scale: number): Record<string, number | string> {
|
|
223
|
+
const [, height] = layout.size;
|
|
224
|
+
const { band } = layout;
|
|
225
|
+
const region = LAYOUT_REGIONS[layout.text.layout];
|
|
226
|
+
if (region === "lower half") return { bottom: height - (band.top + band.height) + Math.round(CAPTION_BOTTOM_PADDING * scale) };
|
|
227
|
+
if (region === "upper half") return { top: band.top + Math.round(CAPTION_TOP_PADDING * scale) };
|
|
228
|
+
const rows = getRegionRows(band, region);
|
|
229
|
+
return { top: rows.top, height: rows.bottom - rows.top, justifyContent: "center" };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The text alone on a transparent frame, laid out by its layout and centred across the band. */
|
|
233
|
+
function buildTextTree(layout: TextLayout): OgNode {
|
|
144
234
|
const [width, height] = layout.size;
|
|
145
|
-
const { band, fonts, ink } = layout;
|
|
235
|
+
const { band, fonts, ink, text } = layout;
|
|
146
236
|
const scale = band.width / BAND_BASE_WIDTH;
|
|
147
237
|
const side = Math.round(CAPTION_SIDE_PADDING * scale);
|
|
148
|
-
const
|
|
238
|
+
const font = { fontFamily: toFontFamilyCss(fonts.heading), fontWeight: pickWeight(fonts.heading.weights, 700) };
|
|
239
|
+
const shadow = `0px ${String(Math.round(4 * scale))}px ${String(Math.round(24 * scale))}px ${withAlpha(ink.shade, 0xb3)}`;
|
|
240
|
+
const line = (content: string, style: Record<string, number | string>) =>
|
|
241
|
+
h("div", { style: { display: "flex", justifyContent: "center", width: "100%", textAlign: "center", wordBreak: "break-word", color: ink.text, ...font, ...style } }, content);
|
|
242
|
+
const children =
|
|
243
|
+
text.layout === "pill"
|
|
244
|
+
? [
|
|
245
|
+
h(
|
|
246
|
+
"div",
|
|
247
|
+
{
|
|
248
|
+
style: {
|
|
249
|
+
display: "flex",
|
|
250
|
+
maxWidth: "100%",
|
|
251
|
+
padding: `${String(Math.round(PILL_PADDING[0] * scale))}px ${String(Math.round(PILL_PADDING[1] * scale))}px`,
|
|
252
|
+
borderRadius: Math.round(PILL_SIZE * scale),
|
|
253
|
+
backgroundColor: withAlpha(ink.shade, PILL_ALPHA),
|
|
254
|
+
textAlign: "center",
|
|
255
|
+
wordBreak: "break-word",
|
|
256
|
+
color: ink.text,
|
|
257
|
+
fontSize: Math.round(PILL_SIZE * scale),
|
|
258
|
+
lineHeight: 1.15,
|
|
259
|
+
...font,
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
text.caption,
|
|
263
|
+
),
|
|
264
|
+
]
|
|
265
|
+
: [
|
|
266
|
+
...(text.layout === "kicker-title"
|
|
267
|
+
? [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 })]
|
|
268
|
+
: []),
|
|
269
|
+
line(text.caption, { fontSize: Math.round(getCoverCaptionSize(text.caption) * scale), lineHeight: 1.08, textShadow: shadow }),
|
|
270
|
+
];
|
|
149
271
|
return h(
|
|
150
272
|
"div",
|
|
151
273
|
{ style: { display: "flex", position: "relative", width, height } },
|
|
@@ -155,34 +277,36 @@ function buildCaptionTree(layout: CaptionLayout): OgNode {
|
|
|
155
277
|
style: {
|
|
156
278
|
position: "absolute",
|
|
157
279
|
display: "flex",
|
|
158
|
-
|
|
280
|
+
flexDirection: "column",
|
|
281
|
+
alignItems: "center",
|
|
159
282
|
left: band.left + side,
|
|
160
283
|
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)}`,
|
|
284
|
+
...getBlockPlacement(layout, scale),
|
|
170
285
|
},
|
|
171
286
|
},
|
|
172
|
-
|
|
287
|
+
...children,
|
|
173
288
|
),
|
|
174
289
|
);
|
|
175
290
|
}
|
|
176
291
|
|
|
177
|
-
|
|
292
|
+
/** How errors name the cover's text, e.g. `the kicker "New" and the caption "Lisbon"`. */
|
|
293
|
+
function describeText(text: CoverText): { name: string; isPlural: boolean } {
|
|
294
|
+
if (text.layout === "pill") return { name: `the pill label ${JSON.stringify(text.caption)}`, isPlural: false };
|
|
295
|
+
if (text.layout === "kicker-title") return { name: `the kicker ${JSON.stringify(text.kicker)} and the caption ${JSON.stringify(text.caption)}`, isPlural: true };
|
|
296
|
+
return { name: `the caption ${JSON.stringify(text.caption)}`, isPlural: false };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function checkTextGlyphs(tree: OgNode, fonts: OgFonts, text: CoverText): OgResult<null> {
|
|
178
300
|
const missing = findMissingGlyphs(tree, fonts.satoriFonts);
|
|
179
301
|
if (!missing.ok) return err(`reading ${missing.error}`);
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
302
|
+
const sentences = missing.value
|
|
303
|
+
.filter((entry) => entry.characters.length > 0)
|
|
304
|
+
.map((entry) => {
|
|
305
|
+
const role = text.layout === "pill" ? "pill label" : text.layout === "kicker-title" && entry.text === text.kicker ? "kicker" : "caption";
|
|
306
|
+
return `the ${role} ${JSON.stringify(entry.text)} has characters none of the loaded fonts can draw at its weight: ${entry.characters.map(describeCharacter).join(", ")}`;
|
|
307
|
+
});
|
|
308
|
+
if (sentences.length === 0) return ok(null);
|
|
309
|
+
return err(`${sentences.join("; ")}. Use font files that cover them`);
|
|
186
310
|
}
|
|
187
311
|
|
|
188
312
|
/** The rows of `pixels` (RGBA, `width` wide) that hold anything drawn, or null when nothing is. */
|
|
@@ -203,10 +327,10 @@ function findDrawnBox(pixels: Uint8Array, width: number, height: number): { top:
|
|
|
203
327
|
return bottom < 0 ? null : { top, bottom, left, right };
|
|
204
328
|
}
|
|
205
329
|
|
|
206
|
-
/** The
|
|
207
|
-
async function
|
|
208
|
-
const tree =
|
|
209
|
-
const glyphs =
|
|
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 }>> {
|
|
332
|
+
const tree = buildTextTree(layout);
|
|
333
|
+
const glyphs = checkTextGlyphs(tree, layout.fonts, layout.text);
|
|
210
334
|
if (!glyphs.ok) return glyphs;
|
|
211
335
|
const [width, height] = layout.size;
|
|
212
336
|
let svg: string;
|
|
@@ -214,31 +338,74 @@ async function layOutCaption(layout: CaptionLayout): Promise<OgResult<{ svg: str
|
|
|
214
338
|
// Satori is typed for React elements; it reads only `type` and `props`, which `OgNode` has.
|
|
215
339
|
svg = await satori(tree as unknown as Parameters<typeof satori>[0], { width, height, fonts: layout.fonts.satoriFonts });
|
|
216
340
|
} catch (error) {
|
|
217
|
-
return err(`laying out the
|
|
341
|
+
return err(`laying out the text failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
218
342
|
}
|
|
219
343
|
const pixels = new Resvg(svg, { fitTo: { mode: "original" }, font: { loadSystemFonts: false } }).render().pixels;
|
|
220
344
|
const box = findDrawnBox(pixels, width, height);
|
|
221
|
-
|
|
345
|
+
const { name, isPlural } = describeText(layout.text);
|
|
346
|
+
if (box === null) return err(`${name} ${isPlural ? "draw" : "draws"} nothing`);
|
|
222
347
|
const { band } = layout;
|
|
223
|
-
// The
|
|
224
|
-
const
|
|
348
|
+
// The text keeps to the part of the band its layout names, so the photo's subject elsewhere stays clear.
|
|
349
|
+
const region = LAYOUT_REGIONS[layout.text.layout];
|
|
350
|
+
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;
|
|
225
352
|
if (!isInside) {
|
|
226
|
-
return err(
|
|
353
|
+
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
|
+
}
|
|
355
|
+
return ok({ svg, top: box.top, bottom: box.bottom });
|
|
356
|
+
}
|
|
357
|
+
|
|
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). */
|
|
359
|
+
function buildScrim(input: { size: readonly [number, number]; band: CoverBand; layout: CoverText["layout"]; text: { top: number; bottom: number }; shade: string }): string {
|
|
360
|
+
const [width, height] = input.size;
|
|
361
|
+
const { band, shade } = input;
|
|
362
|
+
const region = LAYOUT_REGIONS[input.layout];
|
|
363
|
+
if (input.layout === "pill") return "";
|
|
364
|
+
const fade = Math.round(band.height / 5);
|
|
365
|
+
const alpha = (SCRIM_ALPHA / 255).toFixed(3);
|
|
366
|
+
const stop = (offset: number, opacity: string) => `<stop offset="${offset.toFixed(4)}" stop-color="${shade}" stop-opacity="${opacity}"/>`;
|
|
367
|
+
const gradient = (y1: number, y2: number, stops: string) =>
|
|
368
|
+
`<defs><linearGradient id="scrim" gradientUnits="userSpaceOnUse" x1="0" y1="${String(y1)}" x2="0" y2="${String(y2)}">${stops}</linearGradient></defs>`;
|
|
369
|
+
const rect = (top: number, bottom: number) => `<rect y="${String(top)}" width="${String(width)}" height="${String(bottom - top)}" fill="url(#scrim)"/>`;
|
|
370
|
+
if (region === "lower half") {
|
|
371
|
+
// Fades in over a fifth of the band above the text and holds from the band's lower edge down.
|
|
372
|
+
const start = Math.max(0, input.text.top - fade);
|
|
373
|
+
return gradient(start, band.top + band.height, stop(0, "0") + stop(1, alpha)) + rect(start, height);
|
|
374
|
+
}
|
|
375
|
+
if (region === "upper half") {
|
|
376
|
+
// Holds from the frame's top to the band's top and fades out over a fifth of the band below the text.
|
|
377
|
+
const end = Math.min(height, input.text.bottom + fade);
|
|
378
|
+
return gradient(band.top, end, stop(0, alpha) + stop(1, "0")) + rect(0, end);
|
|
227
379
|
}
|
|
228
|
-
|
|
380
|
+
// Fades in above the text, holds behind it and fades out below it.
|
|
381
|
+
const start = Math.max(0, input.text.top - fade);
|
|
382
|
+
const end = Math.min(height, input.text.bottom + fade);
|
|
383
|
+
const span = Math.max(1, end - start);
|
|
384
|
+
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
385
|
}
|
|
230
386
|
|
|
231
387
|
const toDataUri = (type: string, bytes: Buffer | string) => `data:${type};base64,${Buffer.from(bytes).toString("base64")}`;
|
|
232
388
|
|
|
233
|
-
/** Renders the cover: the photo cropped to `size` around the focus point, the
|
|
389
|
+
/** Renders the cover: the photo cropped to `size` around the focus point, the layout's text (if any) over a soft scrim. */
|
|
234
390
|
export async function renderPhotoCover(input: PhotoCoverInput): Promise<OgResult<PhotoCover>> {
|
|
235
391
|
const name = describeCover(input.id);
|
|
236
|
-
const parsed = coverOptionsSchema.safeParse({
|
|
392
|
+
const parsed = coverOptionsSchema.safeParse({
|
|
393
|
+
photo: input.photo,
|
|
394
|
+
size: input.size,
|
|
395
|
+
focus: input.focus,
|
|
396
|
+
layout: input.layout,
|
|
397
|
+
caption: input.caption,
|
|
398
|
+
kicker: input.kicker,
|
|
399
|
+
tone: input.tone,
|
|
400
|
+
quality: input.quality,
|
|
401
|
+
});
|
|
237
402
|
if (!parsed.success) {
|
|
238
403
|
const lines = parsed.error.issues.map((issue) => ` ${issue.path.length > 0 ? issue.path.join(".") : "input"}: ${issue.message}`);
|
|
239
404
|
return err(`${name}: the input is not valid:\n${lines.join("\n")}`);
|
|
240
405
|
}
|
|
241
406
|
const options = parsed.data;
|
|
407
|
+
const text = resolveCoverText(options);
|
|
408
|
+
if (!text.ok) return err(`${name}: ${text.error}.`);
|
|
242
409
|
const bytes = readPhoto(options.photo);
|
|
243
410
|
if (!bytes.ok) return err(`${name}: ${bytes.error}.`);
|
|
244
411
|
const info = readPhotoInfo(bytes.value);
|
|
@@ -247,9 +414,9 @@ export async function renderPhotoCover(input: PhotoCoverInput): Promise<OgResult
|
|
|
247
414
|
const size = options.size;
|
|
248
415
|
const [width, height] = size;
|
|
249
416
|
const band = getCoverBand(size);
|
|
250
|
-
const ink = getCoverInk(input.colors);
|
|
251
|
-
const
|
|
252
|
-
if (
|
|
417
|
+
const ink = getCoverInk(input.colors, options.tone ?? "light");
|
|
418
|
+
const drawn = text.value === null ? null : await layOutText({ size, band, text: text.value, fonts: input.fonts, ink });
|
|
419
|
+
if (drawn !== null && !drawn.ok) return err(`${name}: ${drawn.error}.`);
|
|
253
420
|
|
|
254
421
|
const crop = getCoverCrop(getOrientedSize(info.value), size, options.focus);
|
|
255
422
|
const matrix = getOrientationMatrix(info.value.orientation, info.value.width, info.value.height).join(" ");
|
|
@@ -258,15 +425,10 @@ export async function renderPhotoCover(input: PhotoCoverInput): Promise<OgResult
|
|
|
258
425
|
`<image href="${toDataUri(`image/${info.value.format}`, bytes.value)}" width="${String(info.value.width)}" height="${String(info.value.height)}" preserveAspectRatio="none" ` +
|
|
259
426
|
`transform="translate(${String(crop.x)} ${String(crop.y)}) scale(${String(crop.scale)}) matrix(${matrix})"/>`,
|
|
260
427
|
];
|
|
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));
|
|
428
|
+
if (drawn !== null && text.value !== null) {
|
|
265
429
|
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)}"/>`,
|
|
430
|
+
buildScrim({ size, band, layout: text.value.layout, text: drawn.value, shade: ink.shade }),
|
|
431
|
+
`<image href="${toDataUri("image/svg+xml", drawn.value.svg)}" width="${String(width)}" height="${String(height)}"/>`,
|
|
270
432
|
);
|
|
271
433
|
}
|
|
272
434
|
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>`;
|