import type { CSSProperties } from 'react' import type { BookElement, BookIconElement, BookPageSetup, BookPhotoElement, BookShapeElement, BookSpread, } from '@trek/shared' import { photoSrc } from './bookRender' import { fontStack } from './bookFonts' import { iconComponent } from './iconLibrary' import { HOLED_SHAPES, SHAPE_PATHS, scalePath, unitPath } from './shapes' import { TravelElementView } from './TravelElements' import { PageNumbers } from './PageNumbers' /** * One spread, drawn. * * This component is the whole reason Studio renders in DOM rather than on a * canvas: the *same* tree is what the print renderer will run in headless * Chromium. Edit mode adds handles and outlines on top; print mode is this and * nothing else. There is no second renderer to drift against. * * Everything is positioned in millimetres. CSS maps mm onto the PDF with a fixed * ratio, so what you see at `scale(0.4)` is the same box model the printer gets * at 1:1 — not an approximation of it. */ function frameStyle(el: BookElement): CSSProperties { return { position: 'absolute', left: `${el.frame.x}mm`, top: `${el.frame.y}mm`, width: `${el.frame.w}mm`, height: `${el.frame.h}mm`, opacity: el.opacity, transform: el.rotation ? `rotate(${el.rotation}deg)` : undefined, } } /** Two decimals of a millimetre, matching what the document itself stores. */ const round2 = (n: number) => Math.round(n * 100) / 100 /** #rrggbb plus an alpha, for the fades a cover panel needs. */ function hexToRgba(hex: string, alpha: number): string { const n = Number.parseInt(hex.slice(1), 16) return `rgba(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}, ${alpha})` } const FILTERS: Record = { none: undefined, bw: 'grayscale(1) contrast(1.05)', warm: 'saturate(1.1) sepia(0.16)', cool: 'saturate(1.05) hue-rotate(-8deg) brightness(1.02)', // Faded, the way a print left in the light goes: lifted blacks, less colour. fade: 'saturate(0.72) contrast(0.88) brightness(1.08)', contrast: 'contrast(1.22) saturate(1.06)', } /** * How much of the frame the decoration around a picture eats, as a fraction of * the shorter side. * * A Polaroid's chin is the reason this is a table rather than one number: the * bottom border is roughly three times the others, and getting that ratio wrong * is the difference between a Polaroid and a photo with a white edge. */ const FRAME_INSET: Record = { none: { pad: 0, bottom: 0 }, polaroid: { pad: 0.055, bottom: 0.17 }, white: { pad: 0.035, bottom: 0.035 }, shadow: { pad: 0, bottom: 0 }, film: { pad: 0.075, bottom: 0.075 }, tape: { pad: 0, bottom: 0 }, } /** * A shape, drawn. * * Rectangles and ellipses stay `
`s — a box with a corner radius is exactly * what CSS is good at, and those two are what every document written before the * shape library existed contains. Everything else is an SVG path. * * The viewBox is in millimetres and the element is that many millimetres wide, * so one user unit is one millimetre and a stroke width means the same thing on * every side. The path is drawn into a box inset by half the stroke, which puts * the whole stroke inside the frame the editor snapped — the same thing * `box-sizing: border-box` does for the div case, and the reason a snapped edge * and a drawn edge line up. */ function ShapeView({ el }: { el: BookShapeElement }) { const fill = el.fill ?? 'transparent' const gradientId = `g-${el.id}` const gradient = el.gradient !== 'none' && el.fill if (el.shape === 'rect' || el.shape === 'ellipse') { const background = !gradient ? fill : `linear-gradient(${el.gradient === 'up' ? 'to top' : 'to bottom'},` + ` ${hexToRgba(el.fill!, 0)} 0%,` + ` ${hexToRgba(el.fill!, 0.55)} 46%,` + ` ${hexToRgba(el.fill!, 1)} 100%)` return (
) } const sw = el.stroke ? el.strokeWidth : 0 const w = Math.max(0.01, el.frame.w - sw) const h = Math.max(0.01, el.frame.h - sw) const d = scalePath(SHAPE_PATHS[el.shape], w, h) // Proportional to the stroke, so a dashed outline keeps its rhythm whether it // is a hairline on a caption rule or a 2mm band around a cover panel. const dash = el.strokeStyle === 'dashed' ? `${sw * 3} ${sw * 2}` : el.strokeStyle === 'dotted' ? `${sw * 0.01} ${sw * 2}` : undefined return (
{gradient && ( )}
) } /** * A picture — possibly cut to a shape, possibly sitting in something. * * The mask and the decoration are independent on purpose: a heart in a Polaroid * is a silly thing to want and there is no reason to stop anyone wanting it, and * keeping them separate means neither has to know about the other. */ function PhotoView({ el, big, print, dropLabel }: { el: BookPhotoElement; big: boolean; print: boolean; dropLabel: string }) { const deco = FRAME_INSET[el.frameStyle] ?? FRAME_INSET.none const side = Math.min(el.frame.w, el.frame.h) // Rounded to a hundredth of a millimetre — the same precision the document // stores. Without it a fraction of the frame lands in the CSS as // `10.200000000000001mm`, which is not wrong but is float noise in a // stylesheet that a person may well end up reading. const pad = round2(deco.pad * side) const bottom = round2(deco.bottom * side) const clipId = `c-${el.id}` const clipped = el.mask && el.mask !== 'rect' /* * An empty frame is a template's promise: it says where a picture goes before * anyone has chosen which — and it says it *in the frame it will wear*. * * This used to render on its own, ignoring frameStyle entirely, so a Polaroid * placeholder was an ordinary dashed rectangle until a photograph landed in * it and the chin appeared from nowhere. The two branches have been folded * together: the surround is drawn once, and only what goes inside it differs. */ const empty = el.photoId == null // In the printed book an unfilled frame is nothing at all — a hatch and an // instruction on a page someone paid to have bound would be a defect. if (empty && print) return null // The label is sized in millimetres so it scales with the zoom exactly as the // page does, and it steps aside on a frame too small to hold it. const labelSize = Math.max(2.4, Math.min(4.6, side * 0.085)) const roomy = side - pad * 2 > 22 && el.frame.w - pad * 2 > 34 const hatch = (
{roomy && dropLabel && ( {dropLabel} )}
) const picture = (
{empty ? hatch : ( )}
) return (
{clipped && ( )} {picture} {/* Sprocket holes. Drawn as a repeating gradient rather than as elements so the count follows the width instead of being fixed at a size that only looks right on one frame. */} {el.frameStyle === 'film' && [0, 1].map(row => (
))} {/* Two strips of tape, held at an angle across opposite corners. */} {/* Two strips, one over each top corner, each centred *on* its corner so it overhangs the picture evenly on both sides — which is what a piece of tape does. Offsetting by a fraction of the strip instead left them sitting beside the corners rather than across them. */} {el.frameStyle === 'tape' && ([-1, 1] as const).map(dir => { const tapeW = side * 0.26 const tapeH = side * 0.075 return (
) })}
) } /** * A lucide drawing, at the size the frame gives it. * * Kept proportional rather than stretched to the box. A shape is scaled by * moving its points and looks deliberate at any ratio; a stroked icon does not * — a stretched compass reads as a mistake — so it sits centred in whatever * rectangle it was given, which is also what the handles then appear to do. * * The stroke follows the drawing rather than staying a fixed width, because an * icon set 60mm across with a hairline is a diagram, not a mark. `lineWidth` is * the weight against lucide's own 24-unit grid, exactly as the icons are drawn. */ function IconView({ el }: { el: BookIconElement }) { const Icon = iconComponent(el.name) return (
) } export function ElementView({ el, big, print = false, dropLabel = '', hiddenPhotoIds, }: { el: BookElement; big: boolean; print?: boolean; dropLabel?: string /** Photos drawn as empty frames instead of themselves. */ hiddenPhotoIds?: ReadonlySet }) { if (el.kind === 'photo') { // A photo left out is drawn as its empty frame, kept even in print, so the // page still shows where a picture sits without showing the picture. if (el.photoId != null && hiddenPhotoIds?.has(el.photoId)) { return } return } if (el.kind === 'shape') return // Above the catch-all below, which routes everything that is not text into // the travel elements and draws null for anything they do not know. if (el.kind === 'icon') return if (el.kind !== 'text') { // map, stats, countries, badge — the elements drawn from the journey's own // figures. They live in their own file because they are a different kind of // drawing: type and vector graphics laid out from data, rather than one box // with one property set. return } return (
{el.text}
) } /** * The sheet. `mode="print"` is exactly what the renderer will produce; the * editor draws the same thing and layers its chrome above it. */ export function SpreadView({ spread, page, folios = [], big = false, showGuides = false, print = false, dropLabel = '', hiddenPhotoIds, }: { spread: BookSpread page: BookPageSetup /** * The page numbers this spread carries, from foliosOf. Only PageNumbers * needs them — a page number is the one thing on the page that is a * function of position in the book rather than of the spread itself, and * the caller is the one holding the book. */ folios?: readonly number[] big?: boolean showGuides?: boolean /** The print renderer passes this: no guides, no placeholders, no chrome. */ print?: boolean dropLabel?: string /** * Photos to leave out, drawn as empty frames: a page drawn for the * assistant shows only the asking user's own photos (journey-studio.md 3.4). */ hiddenPhotoIds?: ReadonlySet }) { const isSingle = spread.role !== 'inner' const w = isSingle ? page.pageWidth : page.pageWidth * 2 return (
{spread.elements.map(el => ( ))} {showGuides && ( <> {/* Safe area, per page: on a spread the inner margin belongs to the gutter, so one box around the whole sheet would be a lie. */} {(isSingle ? [0] : [0, page.pageWidth]).map(offset => (
))}
)}
) } /** * The fold down the middle of an open book. * * Preview chrome, not content — which is why it lives outside `SpreadView`. A * printed book has a physical crease; a *printed* shadow down the gutter would * be a defect. So the editor and the page thumbnails draw this, and the renderer * never sees it. * * Two layers, because that is what makes paper read as curving rather than as a * grey stripe: the shadow ramps into the spine and darkens hard at the crease, * and just outside it a pale band lifts, the way the sheet catches light as it * comes back up out of the binding. */ export function SpreadFold({ page, scaled }: { page: BookPageSetup; scaled: number }) { // Wide on purpose: the lift has to ramp over a long distance to read as // paper curving. A narrow band reads as two painted stripes instead. const width = 52 * scaled const left = page.pageWidth * scaled - width / 2 return (
{/* The crease itself. Sub-pixel at small zoom, which is right — you should not see a hard line on a page shown at 15%. */}
) }