mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
ce9c94a77b
- Fifty more shapes: arches, paper cut-outs (leaves, coral, weed, organic circles, a wonky star and sun), stamps, a postmark, a rosette, tags and ticket stubs, sunbursts, ribbons and bands, speech bubbles, drawn arrows, hearts, stars, photo frames and ornaments (scroll, laurel, fleuron, corner). The broad ones are photo frames too, and each tile is drawn at its shape's own proportions, which a new shape is also placed at. - A cut edge on rectangles, circles, arches and the other broad shapes, painted or not, and on a photo's cut: zigzag, scallop, a postage stamp's perforation, wavy, torn and deckle, with a depth in millimetres. Cut in millimetres, so holes stay round on a wide frame. - A line element: two to 32 points dragged on the canvas (a handle pulled from the middle adds one, a double-click removes it), straight or curved, solid, dashed or dotted, a wave or a zigzag, a hand-drawn wobble, a head on either end and an ornament in the middle. A shelf of lines and dividers places them. - The assistant reaches all of it: add and style take the line (its points on the spread in mm) and the edges, describe_book_page says them, and two new findings say when a line is too thin to print and when an edge is set on a shape that takes none. A photo's mask is checked against the shape set rather than listed again, which keeps the page edit's schema smaller. - BOOK_SCHEMA_REV goes to 6.
104 lines
3.3 KiB
TypeScript
104 lines
3.3 KiB
TypeScript
import { bookLineFrame, bookLinePad, bookLineWithPoints, bookLinePoints } from './book-line';
|
|
import type { BookFrame, BookLineElement, BookPageSetup } from './book.schema';
|
|
|
|
/**
|
|
* What `add` and `style` do to a line, for the element operations
|
|
* (book-ops-elements.ts): the Studio's own new line, and a restyle that
|
|
* refits the frame when the points move or the ends grow a head.
|
|
*/
|
|
|
|
type Pt = readonly [number, number];
|
|
|
|
const LOOK_KEYS = [
|
|
'opacity',
|
|
'stroke',
|
|
'strokeWidth',
|
|
'dash',
|
|
'curve',
|
|
'squiggle',
|
|
'startCap',
|
|
'endCap',
|
|
'wobble',
|
|
'ornament',
|
|
] as const;
|
|
|
|
/** What a line op may carry: its look, and points on the spread in millimetres. */
|
|
interface BookLineSpec {
|
|
points?: readonly { x: number; y: number }[];
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
const tuples = (points: readonly { x: number; y: number }[]): Pt[] => points.map((p) => [p.x, p.y]);
|
|
|
|
/** The look fields of a spec set on a line, untouched where the spec leaves them out. */
|
|
function withLook(el: BookLineElement, spec: BookLineSpec): BookLineElement {
|
|
const next: Record<string, unknown> = { ...el };
|
|
for (const key of LOOK_KEYS) {
|
|
const value = spec[key];
|
|
if (value === undefined) continue;
|
|
next[key] = key === 'strokeWidth' && typeof value === 'number' ? Math.round(value * 100) / 100 : value;
|
|
}
|
|
return next as unknown as BookLineElement;
|
|
}
|
|
|
|
/**
|
|
* A line restyled. New points, a wider stroke or a new head each refit the
|
|
* frame, so the ends stay where they are drawn and the heads stay inside it.
|
|
*/
|
|
export function bookLineStyled(el: BookLineElement, spec: BookLineSpec): BookLineElement {
|
|
const styled = withLook(el, spec);
|
|
const reaches = spec.strokeWidth !== undefined || spec.startCap !== undefined || spec.endCap !== undefined;
|
|
const points = spec.points ? tuples(spec.points) : reaches ? bookLinePoints(el) : null;
|
|
return points ? bookLineWithPoints(styled, points) : styled;
|
|
}
|
|
|
|
/**
|
|
* A new line, as the Studio's Lines shelf places it: a rule across the middle
|
|
* of the given frame, or half a page wide across the right-hand page of a
|
|
* spread (the left-hand one is the fold's other side), unless points are named.
|
|
*/
|
|
export function bookNewLine(
|
|
spec: BookLineSpec,
|
|
opts: { id: string; frame: BookFrame | null; inner: boolean; page: BookPageSetup },
|
|
): BookLineElement {
|
|
const { page } = opts;
|
|
const origin = opts.inner ? page.pageWidth : 0;
|
|
const box = opts.frame ?? {
|
|
x: origin + page.pageWidth * 0.25,
|
|
y: page.pageHeight / 2,
|
|
w: page.pageWidth * 0.5,
|
|
h: 0,
|
|
};
|
|
const mid = box.y + box.h / 2;
|
|
const points: readonly Pt[] = spec.points
|
|
? tuples(spec.points)
|
|
: [
|
|
[box.x, mid],
|
|
[box.x + box.w, mid],
|
|
];
|
|
const base: BookLineElement = {
|
|
id: opts.id,
|
|
kind: 'line',
|
|
rotation: 0,
|
|
opacity: 1,
|
|
locked: false,
|
|
frame: { x: 0, y: 0, w: 1, h: 1 },
|
|
points: [
|
|
{ x: 0, y: 0.5 },
|
|
{ x: 1, y: 0.5 },
|
|
],
|
|
curve: 'straight',
|
|
stroke: '#141414',
|
|
strokeWidth: 0.5,
|
|
dash: 'solid',
|
|
squiggle: 'none',
|
|
startCap: 'none',
|
|
endCap: 'none',
|
|
wobble: 0,
|
|
ornament: 'none',
|
|
};
|
|
const look = withLook(base, spec);
|
|
const fit = bookLineFrame(points, { pad: bookLinePad(look.strokeWidth, [look.startCap, look.endCap]) });
|
|
return { ...look, frame: fit.frame, points: fit.points };
|
|
}
|