mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
5651c11803
- design_book takes a brief (energy, decoration, collages, theme), a cover archetype and a cover lockup, and pages.overrides that give single pages of the plan another beat or layout. Directives written before parse as they did, and a design asked for none of it is composed exactly as before. - The engine composes the seven cover archetypes from the book's own photos, words, theme faces and colours, lockups, shapes and stickers, varied by the seed: spine band, poster pop, cut-out, illustrated postcard, grid and badge, minimal serif and stamp collage. A title the directives give a display tier is held to it, and the notes say when a band, a badge or a stamp holds it smaller or a cover leaves a layout out. - A lively or loud book turns two in five hero and grid spreads into collages, script overlays, badge grids, stamp rows or two-tone splits, the first of them always, turns and frames the prints of its other pages, sets its cover title larger and takes one or two, or three to six, stickers of its theme's motifs a spread, never over words, a photo's detail or the fold. A calm book asked for collages gets them and nothing else. - Every turn the engine gives a print or a sticker stays within what the brief's energy takes, a calm book's within its theme's own, so a design is never judged against its own brief. - New checks name a flat cover, a page without a lead, colours or faces off the theme and, for a design made with a brief, a book livelier or calmer than it asked, on the pages the design made and on a page revised in the draft. Page edits answer with the art checks too, and the assistant's cards name them in every language. - list_book_assets lists the shapes, faces, lockups, themes and patterns beside the stickers, and the Studio's shape groups and photo frames are one list the assistant reads too. - The Studio prompt gains an index of themes, archetypes and faces and JSON recipes of the elements to add and style for each cover and three lively spreads, and a turn that designs the book may preview sixteen pages, still three images a request; a viewer is told of none of it.
173 lines
7.4 KiB
TypeScript
173 lines
7.4 KiB
TypeScript
import type { AutoEntry, AutoPhoto } from '../auto/auto-types';
|
|
import { applyBookOps } from '../book-ops';
|
|
import type { BookOpsContext } from '../book-ops-core';
|
|
import type { BookFinding } from '../book-ops.schema';
|
|
import type { BookDocument, BookSpread } from '../book.schema';
|
|
import type { BookDesignOverrideBeat, BookDesignPageOverride } from './design-brief.schema';
|
|
import type { ComposeCtx } from './design-compose';
|
|
import { composeLively } from './design-lively-pacing';
|
|
import type { BookDesignPlanRow } from './design-merge';
|
|
import type { PlannedSpread } from './design-pacing';
|
|
import { composeDuo, composeGrid, composeHero, composeTrio, type Composed } from './design-spreads';
|
|
import { decorateSpread } from './design-stickers';
|
|
|
|
/**
|
|
* Pages of a design given another beat or another layout by the directives
|
|
* (`pages.overrides`), after the design is made: the page keeps its place,
|
|
* its id, its chapter and its photos, and is composed again as the beat asks
|
|
* from those photos and its entry's words, or laid out on the template by the
|
|
* page edit's own `layout` operation, so a layout the engine learns there is
|
|
* one an override may name. Then it is themed and repaired like every other
|
|
* designed page.
|
|
*
|
|
* An override that cannot be honoured is said in the notes and the page stays
|
|
* as designed: a page the design keeps as it was, a single page, one that
|
|
* sets a story (no recomposed beat sets one), or too few photos for the beat.
|
|
*/
|
|
|
|
interface OverrideEnv {
|
|
c: ComposeCtx;
|
|
entries: readonly AutoEntry[];
|
|
/** The engine's own finish for a designed page: themed, repaired, its weights real. */
|
|
finish: (spread: BookSpread) => { spread: BookSpread; findings: BookFinding[] };
|
|
ops: BookOpsContext;
|
|
}
|
|
|
|
/** The photos a beat needs at least. */
|
|
const LEAST: Readonly<Record<BookDesignOverrideBeat, number>> = {
|
|
collage: 1,
|
|
'script-overlay': 1,
|
|
'grid-badge': 2,
|
|
'stamp-row': 1,
|
|
'two-tone': 1,
|
|
hero: 1,
|
|
duo: 2,
|
|
trio: 3,
|
|
grid: 4,
|
|
};
|
|
|
|
/** The beats that need the page's entry for their words. */
|
|
const NEEDS_ENTRY: ReadonlySet<BookDesignOverrideBeat> = new Set(['script-overlay', 'two-tone', 'hero']);
|
|
|
|
/** The photos of a page as the composers take them, in paint order. */
|
|
function photosOf(c: ComposeCtx, spread: BookSpread): AutoPhoto[] {
|
|
return spread.elements.flatMap((e) => {
|
|
if (e.kind !== 'photo' || e.photoId == null) return [];
|
|
const info = c.infos.get(e.photoId);
|
|
return [{ photoId: e.photoId, width: info?.width ?? null, height: info?.height ?? null }];
|
|
});
|
|
}
|
|
|
|
/** Whether a page sets an entry's story, which none of the beats an override names sets again. */
|
|
const setsStory = (spread: BookSpread) =>
|
|
spread.elements.some((e) => e.kind === 'text' && e.binding?.source === 'entry.story');
|
|
|
|
/** The page's elements composed as a classic photo beat, or null when the beat needs what the page lacks. */
|
|
function classic(
|
|
c: ComposeCtx,
|
|
beat: BookDesignOverrideBeat,
|
|
plan: PlannedSpread,
|
|
hand: 'left' | 'right',
|
|
): Composed | null {
|
|
const entryId = plan.entry?.id ?? null;
|
|
const [first] = plan.photos;
|
|
if (beat === 'hero') return plan.entry && first ? composeHero(c, plan.entry, first, hand) : null;
|
|
if (beat === 'duo') return composeDuo(c, plan.photos, hand, entryId);
|
|
if (beat === 'trio') return composeTrio(c, plan.photos, hand, entryId);
|
|
return beat === 'grid' ? composeGrid(c, plan.photos.slice(0, 6), hand, entryId) : null;
|
|
}
|
|
|
|
/** A page composed again as `beat`, or why it cannot be. */
|
|
function recomposed(
|
|
env: OverrideEnv,
|
|
spread: BookSpread,
|
|
beat: BookDesignOverrideBeat,
|
|
index: number,
|
|
): BookSpread | string {
|
|
const { c } = env;
|
|
if (setsStory(spread)) return `page ${index} sets a story, which a ${beat} page does not, so it stays as designed`;
|
|
const photos = photosOf(c, spread);
|
|
if (photos.length < LEAST[beat]) return `page ${index} holds ${photos.length} photos, too few for a ${beat} page`;
|
|
const entry = env.entries.find((e) => e.id === spread.entryId) ?? null;
|
|
if (NEEDS_ENTRY.has(beat) && !entry)
|
|
return `page ${index} belongs to no entry, so a ${beat} page has no words for it`;
|
|
const chapter = { key: `o${index}`, label: null, entries: entry ? [entry] : [], photos, stations: false, burst: [] };
|
|
const plan: PlannedSpread = { beat, chapter, entry, photos };
|
|
const hand = index % 2 === 0 ? 'left' : 'right';
|
|
const made = composeLively(c, plan, hand, index) ?? classic(c, beat, plan, hand);
|
|
if (!made) return `page ${index} could not be composed as a ${beat} page`;
|
|
const page: BookSpread = { ...spread, elements: made.elements, groups: undefined };
|
|
return decorateSpread(c, page, `deco:o${index}`);
|
|
}
|
|
|
|
/** A page laid out on a template by the `layout` operation, or why it cannot be. */
|
|
function laidOut(
|
|
env: OverrideEnv,
|
|
doc: BookDocument,
|
|
spread: BookSpread,
|
|
template: string,
|
|
index: number,
|
|
): BookSpread | string {
|
|
const spreads = doc.spreads.map((s) => (s.id === spread.id ? spread : s));
|
|
const done = applyBookOps({ ...doc, spreads }, { spreadId: spread.id, ops: [{ op: 'layout', template }] }, env.ops);
|
|
if (done.ok === false) return `page ${index} cannot take the layout ${template}: ${done.error.message}`;
|
|
const laid = done.doc.spreads.find((s) => s.id === spread.id) ?? spread;
|
|
// A layout mints ids of its own; the design's counter mints them again, so one journey designs one book.
|
|
const elements = laid.elements.map((e) => ({ ...e, id: env.c.newId(e.kind.charAt(0)) }));
|
|
return { ...laid, elements };
|
|
}
|
|
|
|
const count = (findings: readonly BookFinding[], severity: BookFinding['severity']) =>
|
|
findings.filter((f) => f.severity === severity).length;
|
|
|
|
/** The photos a page shows, as a plan row names them. */
|
|
const photoIdsOf = (spread: BookSpread) =>
|
|
spread.elements.flatMap((e) => (e.kind === 'photo' && e.photoId != null ? [e.photoId] : []));
|
|
|
|
/** The design with its overridden pages composed again, its plan following them, and what could not be done. */
|
|
export function applyPageOverrides(
|
|
doc: BookDocument,
|
|
plan: readonly BookDesignPlanRow[],
|
|
overrides: readonly BookDesignPageOverride[],
|
|
env: OverrideEnv,
|
|
): { doc: BookDocument; plan: BookDesignPlanRow[]; notes: string[] } {
|
|
const notes: string[] = [];
|
|
const spreads = [...doc.spreads];
|
|
const rows = [...plan];
|
|
for (const o of overrides) {
|
|
const spread = spreads[o.index];
|
|
const at = rows.findIndex((r) => r.index === o.index && r.action !== 'drop');
|
|
const row = rows[at];
|
|
if (!spread || !row || spread.role !== 'inner') {
|
|
notes.push(`page ${o.index} is not a double page of the design, so its override is left out`);
|
|
continue;
|
|
}
|
|
if (row.action === 'keep') {
|
|
notes.push(`page ${o.index} is kept as it was, so its override is left out`);
|
|
continue;
|
|
}
|
|
let next = spread;
|
|
let beat = row.beat;
|
|
if (o.beat) {
|
|
const made = recomposed(env, next, o.beat, o.index);
|
|
if (typeof made === 'string') notes.push(made);
|
|
else [next, beat] = [made, o.beat];
|
|
}
|
|
if (o.template) {
|
|
const laid = laidOut(env, { ...doc, spreads }, next, o.template, o.index);
|
|
if (typeof laid === 'string') notes.push(laid);
|
|
else next = laid;
|
|
}
|
|
if (next === spread) continue;
|
|
const finished = env.finish(next);
|
|
spreads[o.index] = finished.spread;
|
|
rows[at] = {
|
|
...row,
|
|
beat,
|
|
photoIds: photoIdsOf(finished.spread),
|
|
findings: { severe: count(finished.findings, 'severe'), warn: count(finished.findings, 'warn') },
|
|
};
|
|
}
|
|
return { doc: { ...doc, spreads }, plan: rows, notes };
|
|
}
|