@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.
- package/CHANGELOG.md +40 -0
- package/README.md +45 -11
- 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/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.map +1 -1
- package/dist/footage/pipeline.js +13 -8
- 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/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- 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 +22 -0
- package/dist/og/photo-cover.d.ts.map +1 -1
- package/dist/og/photo-cover.js +27 -9
- 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/paid.ts +3 -3
- package/src/footage/pipeline.ts +12 -7
- package/src/footage/render.ts +7 -3
- package/src/index.ts +26 -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 +14 -0
- package/src/og/photo-cover.ts +46 -11
package/src/index.ts
CHANGED
|
@@ -165,11 +165,21 @@ export {
|
|
|
165
165
|
OG_TEMPLATE_IDS,
|
|
166
166
|
PHOTO_COVER_LAYOUTS,
|
|
167
167
|
getOgPalette,
|
|
168
|
+
FONT_USES,
|
|
169
|
+
findMissingCharacters,
|
|
168
170
|
loadOgFonts,
|
|
171
|
+
readFontFileInfo,
|
|
169
172
|
renderConfiguredOgImage,
|
|
170
173
|
renderOgImage,
|
|
174
|
+
isPointUnderCoverText,
|
|
171
175
|
renderPhotoCover,
|
|
172
176
|
type ConfiguredOgImageOptions,
|
|
177
|
+
type FontFileFormat,
|
|
178
|
+
type FontFileInfo,
|
|
179
|
+
type FontFileWeight,
|
|
180
|
+
type FontUnusableReason,
|
|
181
|
+
type FontUse,
|
|
182
|
+
type FontUseVerdict,
|
|
173
183
|
type OgBrand,
|
|
174
184
|
type OgImageInput,
|
|
175
185
|
type OgResult,
|
|
@@ -210,8 +220,13 @@ export {
|
|
|
210
220
|
CAPTION_PRESETS,
|
|
211
221
|
CAPTION_STYLES,
|
|
212
222
|
DEFAULT_EDIT,
|
|
223
|
+
DEFAULT_END_CARD_SECONDS,
|
|
224
|
+
DEFAULT_LOGO_END_CARD_FADE_SECONDS,
|
|
225
|
+
DEFAULT_LOGO_END_CARD_SECONDS,
|
|
213
226
|
getDefaultFillers,
|
|
214
227
|
getDeclickLevel,
|
|
228
|
+
getEndCardSeconds,
|
|
229
|
+
getEndCardTexts,
|
|
215
230
|
getFootageEdit,
|
|
216
231
|
type CaptionPreset,
|
|
217
232
|
type CaptionStyle,
|
|
@@ -222,28 +237,33 @@ export {
|
|
|
222
237
|
type CutTransition,
|
|
223
238
|
type JumpTransition,
|
|
224
239
|
type FootageClip,
|
|
240
|
+
type FootageEndCard,
|
|
225
241
|
type FootageFilm,
|
|
226
242
|
type FootageMusic,
|
|
243
|
+
type FullFootageEndCard,
|
|
244
|
+
type LogoFootageEndCard,
|
|
227
245
|
type FootageSpeech,
|
|
228
246
|
type FootageTransitions,
|
|
229
247
|
type MusicSource,
|
|
230
248
|
} from "./footage/film.js";
|
|
231
249
|
export {
|
|
232
|
-
|
|
233
|
-
|
|
250
|
+
DECLICK_AR_CONTEXT,
|
|
251
|
+
DECLICK_AR_ORDER,
|
|
252
|
+
DECLICK_CROSSFADE,
|
|
234
253
|
DECLICK_LEVELS,
|
|
235
254
|
DECLICK_MAX_IMPULSE,
|
|
236
255
|
DECLICK_SETTINGS,
|
|
237
|
-
|
|
256
|
+
DECLICK_WORD_GUARD,
|
|
238
257
|
buildDeclickDecodeArgs,
|
|
239
258
|
buildDeclickEncodeArgs,
|
|
240
259
|
declickSamples,
|
|
241
260
|
declickSpeech,
|
|
242
261
|
describeDeclick,
|
|
243
262
|
findImpulses,
|
|
244
|
-
|
|
245
|
-
|
|
263
|
+
getArPolynomial,
|
|
264
|
+
getRepairZones,
|
|
246
265
|
getRunningMedian,
|
|
266
|
+
repairSpan,
|
|
247
267
|
type ActiveDeclickLevel,
|
|
248
268
|
type DeclickLevel,
|
|
249
269
|
type DeclickReport,
|
|
@@ -401,6 +421,7 @@ export {
|
|
|
401
421
|
composeFootage,
|
|
402
422
|
getFootageFilmSeconds,
|
|
403
423
|
getFootageSfxCues,
|
|
424
|
+
getLogoCardBox,
|
|
404
425
|
getSegmentScales,
|
|
405
426
|
type FootageComposeAssets,
|
|
406
427
|
type FootageComposeInput,
|
package/src/og/character-map.ts
CHANGED
|
@@ -1,76 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { OutOfBounds, readFontTables, readInt16, readUint16, readUint32 } from "./font-tables.js";
|
|
3
2
|
import { err, ok, type OgResult } from "./result.js";
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* The characters a font file maps to a glyph, read from its `cmap` table the way Satori's font
|
|
7
6
|
* parser (opentype.js) reads it: the last Unicode subtable of the table (platform 0 encodings 0-4,
|
|
8
7
|
* platform 3 encodings 0, 1 and 10), in format 4 or 12. Glyph 0 (`.notdef`) means "not in the font".
|
|
9
|
-
*
|
|
8
|
+
* TrueType, OpenType, WOFF and WOFF2 files are read (`readFontTables`), though Satori itself draws no WOFF2. Every read
|
|
9
|
+
* is bounds-checked, so a broken file is an error value, never an exception.
|
|
10
10
|
*/
|
|
11
11
|
export interface CharacterMap {
|
|
12
12
|
has(codePoint: number): boolean;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
const WOFF_SIGNATURE = 0x774f4646; // wOFF
|
|
17
|
-
const WOFF2_SIGNATURE = 0x774f4632; // wOF2
|
|
18
|
-
const COLLECTION_SIGNATURE = 0x74746366; // ttcf
|
|
19
|
-
|
|
20
|
-
class OutOfBounds extends Error {}
|
|
21
|
-
|
|
22
|
-
function readUint16(data: Buffer, offset: number): number {
|
|
23
|
-
if (offset < 0 || offset + 2 > data.length) throw new OutOfBounds();
|
|
24
|
-
return data.readUInt16BE(offset);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function readInt16(data: Buffer, offset: number): number {
|
|
28
|
-
if (offset < 0 || offset + 2 > data.length) throw new OutOfBounds();
|
|
29
|
-
return data.readInt16BE(offset);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function readUint32(data: Buffer, offset: number): number {
|
|
33
|
-
if (offset < 0 || offset + 4 > data.length) throw new OutOfBounds();
|
|
34
|
-
return data.readUInt32BE(offset);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function slice(data: Buffer, offset: number, length: number): Buffer {
|
|
38
|
-
if (offset < 0 || length < 0 || offset + length > data.length) throw new OutOfBounds();
|
|
39
|
-
return data.subarray(offset, offset + length);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const CMAP_TAG = 0x636d6170; // cmap
|
|
43
|
-
|
|
44
|
-
/** The raw `cmap` table of an sfnt (`.ttf`, `.otf`) or WOFF file, or a reason it has none. */
|
|
15
|
+
/** The raw `cmap` table of a font file, or a reason it has none. */
|
|
45
16
|
function findCmapTable(data: Buffer): OgResult<Buffer> {
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const record = 12 + index * 16;
|
|
51
|
-
if (readUint32(data, record) === CMAP_TAG) return ok(slice(data, readUint32(data, record + 8), readUint32(data, record + 12)));
|
|
52
|
-
}
|
|
53
|
-
return err("the font has no cmap table");
|
|
54
|
-
}
|
|
55
|
-
if (signature === WOFF_SIGNATURE) {
|
|
56
|
-
const count = readUint16(data, 12);
|
|
57
|
-
for (let index = 0; index < count; index++) {
|
|
58
|
-
const entry = 44 + index * 20;
|
|
59
|
-
if (readUint32(data, entry) !== CMAP_TAG) continue;
|
|
60
|
-
const stored = slice(data, readUint32(data, entry + 4), readUint32(data, entry + 8));
|
|
61
|
-
const length = readUint32(data, entry + 12);
|
|
62
|
-
if (stored.length >= length) return ok(stored);
|
|
63
|
-
try {
|
|
64
|
-
return ok(inflateSync(stored));
|
|
65
|
-
} catch {
|
|
66
|
-
return err("the font's cmap table does not decompress");
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return err("the font has no cmap table");
|
|
70
|
-
}
|
|
71
|
-
if (signature === WOFF2_SIGNATURE) return err("WOFF2 is not read");
|
|
72
|
-
if (signature === COLLECTION_SIGNATURE) return err("font collections are not read");
|
|
73
|
-
return err("the file is not a TrueType, OpenType or WOFF font");
|
|
17
|
+
const tables = readFontTables(data);
|
|
18
|
+
if (!tables.ok) return tables;
|
|
19
|
+
const table = tables.value.get("cmap");
|
|
20
|
+
return table === undefined ? err("the font has no cmap table") : ok(table);
|
|
74
21
|
}
|
|
75
22
|
|
|
76
23
|
function isUnicodeEncoding(platform: number, encoding: number): boolean {
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { readCharacterMap } from "./character-map.js";
|
|
2
|
+
import { OutOfBounds, readFontTables, readInt32, readUint16, slice, type FontFileFormat, type FontTables } from "./font-tables.js";
|
|
3
|
+
import { OG_FONT_WEIGHTS } from "./fonts.js";
|
|
4
|
+
import { err, ok, type OgResult } from "./result.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What an uploaded font file is and where the kit can use it, so an app stores it as a `BrandFont` file
|
|
8
|
+
* (`family`, `files[{ path, weight, style }]`) and refuses it at upload with a reason instead of at render:
|
|
9
|
+
* - `captions`: the film's captions, drawn by the browser render, which takes `.ttf`, `.otf`, `.woff` and `.woff2`;
|
|
10
|
+
* - `draft`: the footage draft's captions, drawn by libass (FreeType), which reads `.ttf` and `.otf` and matches the
|
|
11
|
+
* family the file names itself;
|
|
12
|
+
* - `cover`: OG images and the photo cover, drawn by Satori, which reads static `.ttf`, `.otf` and `.woff` files at
|
|
13
|
+
* weights 100-900 in steps of 100 (the rules `loadOgFonts` applies).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const FONT_USES = ["captions", "draft", "cover"] as const;
|
|
17
|
+
|
|
18
|
+
export type FontUse = (typeof FONT_USES)[number];
|
|
19
|
+
|
|
20
|
+
/** Why a file does not work for a use; a code, so an app words it in its own language. */
|
|
21
|
+
export type FontUnusableReason = "format" | "variable" | "weight" | "character-map" | "family-name";
|
|
22
|
+
|
|
23
|
+
export type FontUseVerdict = { isUsable: true } | { isUsable: false; reason: FontUnusableReason; detail: string };
|
|
24
|
+
|
|
25
|
+
export type FontFileWeight = { kind: "static"; value: number } | { kind: "variable"; min: number; max: number; default: number };
|
|
26
|
+
|
|
27
|
+
export interface FontFileInfo {
|
|
28
|
+
/** The typographic family (name ID 16), else the family (name ID 1); null when the file names none. */
|
|
29
|
+
family: string | null;
|
|
30
|
+
format: FontFileFormat;
|
|
31
|
+
weight: FontFileWeight;
|
|
32
|
+
/** The weight as `BrandFont` files and CSS write it: `400`, or a variable range `100 900`. */
|
|
33
|
+
cssWeight: string;
|
|
34
|
+
style: "normal" | "italic";
|
|
35
|
+
usableFor: Record<FontUse, FontUseVerdict>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const FAMILY_NAME_ID = 1;
|
|
39
|
+
const TYPOGRAPHIC_FAMILY_NAME_ID = 16;
|
|
40
|
+
/** `usWeightClass` when a file has no `OS/2` table: regular, as browsers assume. */
|
|
41
|
+
const DEFAULT_WEIGHT = 400;
|
|
42
|
+
const WEIGHT_AXIS = "wght";
|
|
43
|
+
|
|
44
|
+
const USABLE: FontUseVerdict = { isUsable: true };
|
|
45
|
+
|
|
46
|
+
function unusable(reason: FontUnusableReason, detail: string): FontUseVerdict {
|
|
47
|
+
return { isUsable: false, reason, detail };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function decodeName(bytes: Buffer, platform: number): string {
|
|
51
|
+
if (platform === 1) return bytes.toString("latin1");
|
|
52
|
+
let text = "";
|
|
53
|
+
for (let index = 0; index + 1 < bytes.length; index += 2) text += String.fromCharCode(bytes.readUInt16BE(index));
|
|
54
|
+
return text;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A name record's text: the Windows Unicode record first, US English first among those, else the Macintosh one. */
|
|
58
|
+
function findName(table: Buffer, nameId: number): string | null {
|
|
59
|
+
const count = readUint16(table, 2);
|
|
60
|
+
const strings = readUint16(table, 4);
|
|
61
|
+
let best: { rank: number; name: string } | null = null;
|
|
62
|
+
for (let index = 0; index < count; index++) {
|
|
63
|
+
const record = 6 + index * 12;
|
|
64
|
+
const platform = readUint16(table, record);
|
|
65
|
+
if (readUint16(table, record + 6) !== nameId || (platform !== 3 && platform !== 1)) continue;
|
|
66
|
+
const language = readUint16(table, record + 4);
|
|
67
|
+
const text = decodeName(slice(table, strings + readUint16(table, record + 10), readUint16(table, record + 8)), platform).trim();
|
|
68
|
+
const rank = platform === 3 ? (language === 0x0409 ? 0 : 1) : 2;
|
|
69
|
+
if (text.length > 0 && (best === null || rank < best.rank)) best = { rank, name: text };
|
|
70
|
+
}
|
|
71
|
+
return best?.name ?? null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function readFamily(tables: FontTables): string | null {
|
|
75
|
+
const table = tables.get("name");
|
|
76
|
+
if (table === undefined) return null;
|
|
77
|
+
return findName(table, TYPOGRAPHIC_FAMILY_NAME_ID) ?? findName(table, FAMILY_NAME_ID);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A 16.16 fixed-point number, rounded to a whole weight. */
|
|
81
|
+
function readFixedWeight(table: Buffer, offset: number): number {
|
|
82
|
+
return Math.round(readInt32(table, offset) / 65536);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function readWeight(tables: FontTables): FontFileWeight {
|
|
86
|
+
const fvar = tables.get("fvar");
|
|
87
|
+
if (fvar !== undefined) {
|
|
88
|
+
const axesOffset = readUint16(fvar, 4);
|
|
89
|
+
const axisCount = readUint16(fvar, 8);
|
|
90
|
+
const axisSize = readUint16(fvar, 10);
|
|
91
|
+
for (let index = 0; index < axisCount; index++) {
|
|
92
|
+
const axis = axesOffset + index * axisSize;
|
|
93
|
+
if (slice(fvar, axis, 4).toString("latin1") !== WEIGHT_AXIS) continue;
|
|
94
|
+
return { kind: "variable", min: readFixedWeight(fvar, axis + 4), default: readFixedWeight(fvar, axis + 8), max: readFixedWeight(fvar, axis + 12) };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const os2 = tables.get("OS/2");
|
|
98
|
+
return { kind: "static", value: os2 === undefined ? DEFAULT_WEIGHT : readUint16(os2, 4) };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function readStyle(tables: FontTables): "normal" | "italic" {
|
|
102
|
+
const os2 = tables.get("OS/2");
|
|
103
|
+
if (os2 !== undefined && (readUint16(os2, 62) & 0x1) !== 0) return "italic";
|
|
104
|
+
const head = tables.get("head");
|
|
105
|
+
if (head !== undefined && (readUint16(head, 44) & 0x2) !== 0) return "italic";
|
|
106
|
+
return "normal";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function toCssWeight(weight: FontFileWeight): string {
|
|
110
|
+
return weight.kind === "static" ? String(weight.value) : `${weight.min} ${weight.max}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function judgeDraft(format: FontFileFormat, family: string | null): FontUseVerdict {
|
|
114
|
+
if (format !== "ttf" && format !== "otf") return unusable("format", `the footage draft (libass) reads .ttf and .otf files, not .${format}`);
|
|
115
|
+
// libass matches the family of name ID 1; a file that names a typographic family names a family too in practice.
|
|
116
|
+
return family === null ? unusable("family-name", "the font names no family, so the footage draft cannot match it") : USABLE;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function judgeCover(data: Buffer, format: FontFileFormat, weight: FontFileWeight): FontUseVerdict {
|
|
120
|
+
if (format === "woff2") return unusable("format", "OG images (Satori) read .ttf, .otf and .woff files, not .woff2");
|
|
121
|
+
if (weight.kind === "variable") return unusable("variable", `OG images (Satori) need a static file, and this one is variable (${weight.min}-${weight.max})`);
|
|
122
|
+
if (!OG_FONT_WEIGHTS.some((ogWeight) => ogWeight === weight.value)) {
|
|
123
|
+
return unusable("weight", `OG images (Satori) need a weight from 100 to 900 in steps of 100, and this file is ${weight.value}`);
|
|
124
|
+
}
|
|
125
|
+
const characters = readCharacterMap(data);
|
|
126
|
+
return characters.ok ? USABLE : unusable("character-map", `OG images (Satori) cannot read its character map: ${characters.error}`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Reads an uploaded font file's family, weight, style and format, and whether captions, the footage draft and OG images
|
|
131
|
+
* (the photo cover included) can use it. A broken or unknown file is an error value, never an exception.
|
|
132
|
+
*/
|
|
133
|
+
export function readFontFileInfo(data: Buffer): OgResult<FontFileInfo> {
|
|
134
|
+
const tables = readFontTables(data);
|
|
135
|
+
if (!tables.ok) return err(`Reading the font file: ${tables.error}.`);
|
|
136
|
+
try {
|
|
137
|
+
const { format } = tables.value;
|
|
138
|
+
const family = readFamily(tables.value);
|
|
139
|
+
const weight = readWeight(tables.value);
|
|
140
|
+
return ok({
|
|
141
|
+
family,
|
|
142
|
+
format,
|
|
143
|
+
weight,
|
|
144
|
+
cssWeight: toCssWeight(weight),
|
|
145
|
+
style: readStyle(tables.value),
|
|
146
|
+
usableFor: { captions: USABLE, draft: judgeDraft(format, family), cover: judgeCover(data, format, weight) },
|
|
147
|
+
});
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error instanceof OutOfBounds) return err("Reading the font file: a table of the font is truncated or malformed.");
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Characters that need no glyph: whitespace, control and format characters, variation selectors. */
|
|
155
|
+
const INVISIBLE_PATTERN = /^[\p{White_Space}\p{Cc}\p{Cf}\u{FE00}-\u{FE0F}\u{E0100}-\u{E01EF}]$/u;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The characters of `characters` the font file maps to no glyph, unique and in order (whitespace and other invisible
|
|
159
|
+
* characters are skipped), e.g. the Polish letters an app requires of a brand font. An error value when the file's
|
|
160
|
+
* character map cannot be read.
|
|
161
|
+
*/
|
|
162
|
+
export function findMissingCharacters(data: Buffer, characters: string): OgResult<string[]> {
|
|
163
|
+
const map = readCharacterMap(data);
|
|
164
|
+
if (!map.ok) return err(`Checking the font file's characters: ${map.error}.`);
|
|
165
|
+
const missing: string[] = [];
|
|
166
|
+
for (const character of characters) {
|
|
167
|
+
if (INVISIBLE_PATTERN.test(character) || missing.includes(character)) continue;
|
|
168
|
+
const codePoint = character.codePointAt(0);
|
|
169
|
+
if (codePoint !== undefined && !map.value.has(codePoint)) missing.push(character);
|
|
170
|
+
}
|
|
171
|
+
return ok(missing);
|
|
172
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { brotliDecompressSync, inflateSync } from "node:zlib";
|
|
2
|
+
|
|
3
|
+
import { err, ok, type OgResult } from "./result.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The tables of a font file, by tag, for the formats an app may upload: TrueType and OpenType (sfnt), WOFF (each table
|
|
7
|
+
* zlib-compressed on its own) and WOFF2 (all tables in one Brotli stream). WOFF2 may transform `glyf`, `loca` and
|
|
8
|
+
* `hmtx`; those come back as stored, which is fine for the tables read here (`cmap`, `name`, `OS/2`, `head`, `fvar`
|
|
9
|
+
* are never transformed). Every read is bounds-checked, so a broken file is an error value, never an exception.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type FontFileFormat = "ttf" | "otf" | "woff" | "woff2";
|
|
13
|
+
|
|
14
|
+
export interface FontTables {
|
|
15
|
+
format: FontFileFormat;
|
|
16
|
+
/** The format of the font inside a WOFF or WOFF2 wrapper (`ttf` or `otf`); the format itself for an sfnt. */
|
|
17
|
+
flavor: "ttf" | "otf";
|
|
18
|
+
get(tag: string): Buffer | undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const TRUETYPE_SIGNATURES = new Set([0x00010000, 0x74727565 /* true */]);
|
|
22
|
+
const OPENTYPE_SIGNATURE = 0x4f54544f; // OTTO
|
|
23
|
+
const WOFF_SIGNATURE = 0x774f4646; // wOFF
|
|
24
|
+
const WOFF2_SIGNATURE = 0x774f4632; // wOF2
|
|
25
|
+
const COLLECTION_SIGNATURE = 0x74746366; // ttcf
|
|
26
|
+
|
|
27
|
+
/** More tables than any real font has; a larger count is a broken or hostile file. */
|
|
28
|
+
const MAX_TABLES = 512;
|
|
29
|
+
/** The largest decompressed WOFF2 font read (a CJK font is about 20 MB). */
|
|
30
|
+
const MAX_WOFF2_SIZE = 64 * 1024 * 1024;
|
|
31
|
+
|
|
32
|
+
/** The WOFF2 known-table tags, by their 6-bit index (WOFF2 spec, 5.1). */
|
|
33
|
+
const WOFF2_KNOWN_TAGS = [
|
|
34
|
+
"cmap", "head", "hhea", "hmtx", "maxp", "name", "OS/2", "post", "cvt ", "fpgm", "glyf", "loca", "prep", "CFF ", "VORG",
|
|
35
|
+
"EBDT", "EBLC", "gasp", "hdmx", "kern", "LTSH", "PCLT", "VDMX", "vhea", "vmtx", "BASE", "GDEF", "GPOS", "GSUB", "EBSC",
|
|
36
|
+
"JSTF", "MATH", "CBDT", "CBLC", "COLR", "CPAL", "SVG ", "sbix", "acnt", "avar", "bdat", "bloc", "bsln", "cvar", "fdsc",
|
|
37
|
+
"feat", "fmtx", "fvar", "gvar", "hsty", "just", "lcar", "mort", "morx", "opbd", "prop", "trak", "Zapf", "Silf", "Glat",
|
|
38
|
+
"Gloc", "Feat", "Sill",
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
export class OutOfBounds extends Error {}
|
|
42
|
+
|
|
43
|
+
export function readUint8(data: Buffer, offset: number): number {
|
|
44
|
+
if (offset < 0 || offset + 1 > data.length) throw new OutOfBounds();
|
|
45
|
+
return data.readUInt8(offset);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function readUint16(data: Buffer, offset: number): number {
|
|
49
|
+
if (offset < 0 || offset + 2 > data.length) throw new OutOfBounds();
|
|
50
|
+
return data.readUInt16BE(offset);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function readInt16(data: Buffer, offset: number): number {
|
|
54
|
+
if (offset < 0 || offset + 2 > data.length) throw new OutOfBounds();
|
|
55
|
+
return data.readInt16BE(offset);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function readUint32(data: Buffer, offset: number): number {
|
|
59
|
+
if (offset < 0 || offset + 4 > data.length) throw new OutOfBounds();
|
|
60
|
+
return data.readUInt32BE(offset);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function readInt32(data: Buffer, offset: number): number {
|
|
64
|
+
if (offset < 0 || offset + 4 > data.length) throw new OutOfBounds();
|
|
65
|
+
return data.readInt32BE(offset);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function slice(data: Buffer, offset: number, length: number): Buffer {
|
|
69
|
+
if (offset < 0 || length < 0 || offset + length > data.length) throw new OutOfBounds();
|
|
70
|
+
return data.subarray(offset, offset + length);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function readTag(data: Buffer, offset: number): string {
|
|
74
|
+
return slice(data, offset, 4).toString("latin1");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function toTables(format: FontFileFormat, flavor: "ttf" | "otf", tables: Map<string, Buffer>): FontTables {
|
|
78
|
+
return { format, flavor, get: (tag) => tables.get(tag) };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function getFlavor(signature: number): "ttf" | "otf" | null {
|
|
82
|
+
if (TRUETYPE_SIGNATURES.has(signature)) return "ttf";
|
|
83
|
+
if (signature === OPENTYPE_SIGNATURE) return "otf";
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function readTableCount(data: Buffer, offset: number): OgResult<number> {
|
|
88
|
+
const count = readUint16(data, offset);
|
|
89
|
+
return count > MAX_TABLES ? err(`the font lists ${count} tables`) : ok(count);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function readSfnt(data: Buffer, flavor: "ttf" | "otf"): OgResult<FontTables> {
|
|
93
|
+
const count = readTableCount(data, 4);
|
|
94
|
+
if (!count.ok) return count;
|
|
95
|
+
const tables = new Map<string, Buffer>();
|
|
96
|
+
for (let index = 0; index < count.value; index++) {
|
|
97
|
+
const record = 12 + index * 16;
|
|
98
|
+
tables.set(readTag(data, record), slice(data, readUint32(data, record + 8), readUint32(data, record + 12)));
|
|
99
|
+
}
|
|
100
|
+
return ok(toTables(flavor, flavor, tables));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function readWoff(data: Buffer): OgResult<FontTables> {
|
|
104
|
+
const flavor = getFlavor(readUint32(data, 4));
|
|
105
|
+
if (flavor === null) return err("the WOFF file wraps neither a TrueType nor an OpenType font");
|
|
106
|
+
const count = readTableCount(data, 12);
|
|
107
|
+
if (!count.ok) return count;
|
|
108
|
+
const tables = new Map<string, Buffer>();
|
|
109
|
+
for (let index = 0; index < count.value; index++) {
|
|
110
|
+
const entry = 44 + index * 20;
|
|
111
|
+
const tag = readTag(data, entry);
|
|
112
|
+
const stored = slice(data, readUint32(data, entry + 4), readUint32(data, entry + 8));
|
|
113
|
+
const length = readUint32(data, entry + 12);
|
|
114
|
+
if (stored.length >= length) {
|
|
115
|
+
tables.set(tag, stored);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
tables.set(tag, inflateSync(stored, { maxOutputLength: Math.max(length, 1) }));
|
|
120
|
+
} catch {
|
|
121
|
+
return err(`the font's ${tag.trim()} table does not decompress`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return ok(toTables("woff", flavor, tables));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A WOFF2 UIntBase128 at `offset`: the value and the offset after it, or null when it is malformed. */
|
|
128
|
+
function readBase128(data: Buffer, offset: number): { value: number; next: number } | null {
|
|
129
|
+
let value = 0;
|
|
130
|
+
for (let index = 0; index < 5; index++) {
|
|
131
|
+
const byte = readUint8(data, offset + index);
|
|
132
|
+
if (index === 0 && byte === 0x80) return null;
|
|
133
|
+
if (value > 0x01ffffff) return null;
|
|
134
|
+
value = value * 128 + (byte & 0x7f);
|
|
135
|
+
if ((byte & 0x80) === 0) return { value, next: offset + index + 1 };
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface Woff2Entry {
|
|
141
|
+
tag: string;
|
|
142
|
+
length: number;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function readWoff2Directory(data: Buffer, count: number): OgResult<{ entries: Woff2Entry[]; end: number }> {
|
|
146
|
+
const entries: Woff2Entry[] = [];
|
|
147
|
+
let offset = 48;
|
|
148
|
+
for (let index = 0; index < count; index++) {
|
|
149
|
+
const flags = readUint8(data, offset);
|
|
150
|
+
offset += 1;
|
|
151
|
+
const known = flags & 0x3f;
|
|
152
|
+
let tag: string;
|
|
153
|
+
if (known === 0x3f) {
|
|
154
|
+
tag = readTag(data, offset);
|
|
155
|
+
offset += 4;
|
|
156
|
+
} else {
|
|
157
|
+
const name = WOFF2_KNOWN_TAGS[known];
|
|
158
|
+
if (name === undefined) return err(`the WOFF2 table directory names an unknown table index ${known}`);
|
|
159
|
+
tag = name;
|
|
160
|
+
}
|
|
161
|
+
const original = readBase128(data, offset);
|
|
162
|
+
if (original === null) return err("the WOFF2 table directory is malformed");
|
|
163
|
+
offset = original.next;
|
|
164
|
+
const version = flags >> 6;
|
|
165
|
+
const isTransformed = tag === "glyf" || tag === "loca" ? version === 0 : version !== 0;
|
|
166
|
+
let length = original.value;
|
|
167
|
+
if (isTransformed) {
|
|
168
|
+
const transformed = readBase128(data, offset);
|
|
169
|
+
if (transformed === null) return err("the WOFF2 table directory is malformed");
|
|
170
|
+
offset = transformed.next;
|
|
171
|
+
length = transformed.value;
|
|
172
|
+
}
|
|
173
|
+
entries.push({ tag, length });
|
|
174
|
+
}
|
|
175
|
+
return ok({ entries, end: offset });
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function readWoff2(data: Buffer): OgResult<FontTables> {
|
|
179
|
+
const flavorSignature = readUint32(data, 4);
|
|
180
|
+
if (flavorSignature === COLLECTION_SIGNATURE) return err("font collections are not read");
|
|
181
|
+
const flavor = getFlavor(flavorSignature);
|
|
182
|
+
if (flavor === null) return err("the WOFF2 file wraps neither a TrueType nor an OpenType font");
|
|
183
|
+
const count = readTableCount(data, 12);
|
|
184
|
+
if (!count.ok) return count;
|
|
185
|
+
const totalSfntSize = readUint32(data, 16);
|
|
186
|
+
const compressedSize = readUint32(data, 20);
|
|
187
|
+
const directory = readWoff2Directory(data, count.value);
|
|
188
|
+
if (!directory.ok) return directory;
|
|
189
|
+
const compressed = slice(data, directory.value.end, compressedSize);
|
|
190
|
+
const expected = directory.value.entries.reduce((sum, entry) => sum + entry.length, 0);
|
|
191
|
+
if (expected > MAX_WOFF2_SIZE || totalSfntSize > MAX_WOFF2_SIZE) return err("the WOFF2 font is larger than 64 MB unpacked");
|
|
192
|
+
let stream: Buffer;
|
|
193
|
+
try {
|
|
194
|
+
stream = brotliDecompressSync(compressed, { maxOutputLength: Math.max(expected, 1) });
|
|
195
|
+
} catch {
|
|
196
|
+
return err("the WOFF2 font data does not decompress");
|
|
197
|
+
}
|
|
198
|
+
const tables = new Map<string, Buffer>();
|
|
199
|
+
let offset = 0;
|
|
200
|
+
for (const entry of directory.value.entries) {
|
|
201
|
+
tables.set(entry.tag, slice(stream, offset, entry.length));
|
|
202
|
+
offset += entry.length;
|
|
203
|
+
}
|
|
204
|
+
return ok(toTables("woff2", flavor, tables));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Reads a font file's tables; the error says why it cannot be read. */
|
|
208
|
+
export function readFontTables(data: Buffer): OgResult<FontTables> {
|
|
209
|
+
try {
|
|
210
|
+
const signature = readUint32(data, 0);
|
|
211
|
+
const flavor = getFlavor(signature);
|
|
212
|
+
if (flavor !== null) return readSfnt(data, flavor);
|
|
213
|
+
if (signature === WOFF_SIGNATURE) return readWoff(data);
|
|
214
|
+
if (signature === WOFF2_SIGNATURE) return readWoff2(data);
|
|
215
|
+
if (signature === COLLECTION_SIGNATURE) return err("font collections are not read");
|
|
216
|
+
return err("the file is not a TrueType, OpenType, WOFF or WOFF2 font");
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (error instanceof OutOfBounds) return err("the font file is truncated or malformed");
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
}
|
package/src/og/index.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
// `@softure-ai/marketing-kit/og`: OG images from templates and data, for the CLI and for a thin Next
|
|
2
2
|
// route. Nothing here loads Playwright, so a route can import it.
|
|
3
3
|
export { loadCharacterMap, readCharacterMap, type CharacterMap } from "./character-map.js";
|
|
4
|
+
export {
|
|
5
|
+
FONT_USES,
|
|
6
|
+
findMissingCharacters,
|
|
7
|
+
readFontFileInfo,
|
|
8
|
+
type FontFileInfo,
|
|
9
|
+
type FontFileWeight,
|
|
10
|
+
type FontUnusableReason,
|
|
11
|
+
type FontUse,
|
|
12
|
+
type FontUseVerdict,
|
|
13
|
+
} from "./font-file.js";
|
|
14
|
+
export type { FontFileFormat } from "./font-tables.js";
|
|
4
15
|
export { describeCharacter, findMissingGlyphs, selectSatoriFont, type MissingGlyphs } from "./glyphs.js";
|
|
5
16
|
export { h, listNodes, type OgChild, type OgNode, type OgStyle } from "./element.js";
|
|
6
17
|
export { OG_FONT_KINDS, OG_FONT_WEIGHTS, loadOgFonts, pickWeight, toFontFamilyCss, type OgFontFamily, type OgFontKind, type OgFontWeight, type OgFonts, type ReadFontFile, type SatoriFont } from "./fonts.js";
|
|
@@ -27,8 +38,11 @@ export {
|
|
|
27
38
|
PHOTO_COVER_SIZE,
|
|
28
39
|
getCoverBand,
|
|
29
40
|
getCoverCaptionSize,
|
|
41
|
+
isPointUnderCoverText,
|
|
30
42
|
renderPhotoCover,
|
|
31
43
|
type CoverBand,
|
|
44
|
+
type CoverBox,
|
|
45
|
+
type CoverPoint,
|
|
32
46
|
type PhotoCover,
|
|
33
47
|
type PhotoCoverInput,
|
|
34
48
|
type PhotoCoverLayout,
|