// Pens, colours and page templates for the notebook editor, and the way a // pointer's path becomes a stroke the tablet understands. import { Pen, PenColor, RM_PALETTE, makeLine, type Line, type PaintSegment } from '../../lib/browser.js'; import type { PenKind, PenPrefs, Thickness } from './model.ts'; export type RawPoint = { x: number; y: number; pressure: number; t: number; tiltX: number; tiltY: number }; /** The pens, with the tool id the tablet uses for each and the point width that drives its rendering formula. */ export const PENS: Record number; icon: string }> = { fineliner: { label: 'Fineliner', tool: Pen.FINELINER_2, width: () => 8, icon: 'icon-pencil' }, ballpoint: { label: 'Ballpoint', tool: Pen.BALLPOINT_2, width: (t) => 6 + 2 * t, icon: 'icon-pencil' }, marker: { label: 'Marker', tool: Pen.MARKER_2, width: (t) => 14 + 6 * t, icon: 'icon-pencil' }, pencil: { label: 'Pencil', tool: Pen.PENCIL_2, width: () => 8, icon: 'icon-pencil' }, mechanical: { label: 'Mechanical pencil', tool: Pen.MECHANICAL_PENCIL_2, width: () => 8, icon: 'icon-pencil' }, brush: { label: 'Paintbrush', tool: Pen.PAINTBRUSH_2, width: (t) => 8 + 4 * t, icon: 'icon-palette' }, calligraphy: { label: 'Calligraphy', tool: Pen.CALIGRAPHY, width: (t) => 6 + 4 * t, icon: 'icon-type' }, highlighter: { label: 'Highlighter', tool: Pen.HIGHLIGHTER_2, width: () => 120, icon: 'icon-heading' }, shader: { label: 'Shader', tool: Pen.SHADER, width: () => 40, icon: 'icon-sun' }, }; export const PEN_KINDS = Object.keys(PENS) as PenKind[]; const css = (id: number) => { const [r, g, b] = RM_PALETTE.get(id) ?? [0, 0, 0]; return `rgb(${r},${g},${b})`; }; export const INK_COLORS: { id: number; label: string; css: string }[] = [ { id: PenColor.BLACK, label: 'Black', css: css(PenColor.BLACK) }, { id: PenColor.GRAY, label: 'Grey', css: css(PenColor.GRAY) }, { id: PenColor.WHITE, label: 'White', css: css(PenColor.WHITE) }, { id: PenColor.BLUE, label: 'Blue', css: css(PenColor.BLUE) }, { id: PenColor.RED, label: 'Red', css: css(PenColor.RED) }, { id: PenColor.GREEN_2, label: 'Green', css: css(PenColor.GREEN_2) }, { id: PenColor.YELLOW_2, label: 'Yellow', css: css(PenColor.YELLOW_2) }, { id: PenColor.CYAN, label: 'Cyan', css: css(PenColor.CYAN) }, { id: PenColor.MAGENTA, label: 'Magenta', css: css(PenColor.MAGENTA) }, ]; export const HIGHLIGHT_COLORS: { id: number; label: string; css: string }[] = [ { id: PenColor.YELLOW, label: 'Yellow', css: css(PenColor.YELLOW) }, { id: PenColor.GREEN_2, label: 'Green', css: css(PenColor.GREEN_2) }, { id: PenColor.CYAN, label: 'Cyan', css: css(PenColor.CYAN) }, { id: PenColor.MAGENTA, label: 'Magenta', css: css(PenColor.MAGENTA) }, { id: PenColor.PINK, label: 'Pink', css: css(PenColor.PINK) }, ]; export const THICKNESSES: { value: Thickness; label: string }[] = [ { value: 1, label: 'Thin' }, { value: 2, label: 'Medium' }, { value: 3, label: 'Thick' }, ]; export const DEFAULT_PEN: PenPrefs = { kind: 'fineliner', color: PenColor.BLACK, thickness: 2 }; export function colorsFor(kind: PenKind) { return kind === 'highlighter' ? HIGHLIGHT_COLORS : INK_COLORS; } /** A pointer's path as the tablet would have recorded it: pressure, speed and tilt feed the pens' rendering. */ export function buildLine(prefs: PenPrefs, points: RawPoint[]): Line { const pen = PENS[prefs.kind]; const width = pen.width(prefs.thickness); const pts = points.map((p, i) => { const prev = points[i - 1]; const dist = prev ? Math.hypot(p.x - prev.x, p.y - prev.y) : 0; const dt = prev ? Math.max(1, p.t - prev.t) : 1; const tilt = (Math.hypot(p.tiltX, p.tiltY) * Math.PI) / 180; return { x: p.x, y: p.y, pressure: Math.round(Math.min(1, Math.max(0.05, p.pressure)) * 255), speed: Math.round((dist / dt) * 10), direction: Math.round((tilt * 255) / (2 * Math.PI)), width, }; }); return makeLine({ tool: pen.tool, color: prefs.color, thickness: prefs.thickness, points: pts }); } /** Thin out a pointer path: points closer than `min` page units to the last kept one are dropped. */ export function simplify(points: RawPoint[], min = 1.5): RawPoint[] { const out: RawPoint[] = []; for (const p of points) { const last = out[out.length - 1]; if (!last || Math.hypot(p.x - last.x, p.y - last.y) >= min) out.push(p); } const last = points[points.length - 1]; if (last && out[out.length - 1] !== last) out.push(last); return out; } function distToSegment(px: number, py: number, [ax, ay]: [number, number], [bx, by]: [number, number]): number { const dx = bx - ax, dy = by - ay; const len2 = dx * dx + dy * dy; const t = len2 ? Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / len2)) : 0; return Math.hypot(px - (ax + t * dx), py - (ay + t * dy)); } /** The keys of the strokes within `radius` page units of a point. */ export function strokesNear(segments: PaintSegment[], x: number, y: number, radius: number): Set { const hits = new Set(); for (const s of segments) { if (!s.item || hits.has(s.item)) continue; const r = radius + s.width / 2; const pts = s.points; if (pts.length === 1) { const p = pts[0]!; if (Math.hypot(p[0] - x, p[1] - y) <= r) hits.add(s.item); continue; } for (let i = 1; i < pts.length; i++) { if (distToSegment(x, y, pts[i - 1]!, pts[i]!) <= r) { hits.add(s.item); break; } } } return hits; } /** The tablet's page templates this editor can draw; others show as blank here (the tablet still draws them). */ export type TemplateSpec = { kind: 'blank' } | { kind: 'lines'; step: number; margin?: number } | { kind: 'grid'; step: number } | { kind: 'dots'; step: number }; export const TEMPLATES: { name: string; label: string; spec: TemplateSpec }[] = [ { name: 'Blank', label: 'Blank', spec: { kind: 'blank' } }, { name: 'P Lines small', label: 'Lines, small', spec: { kind: 'lines', step: 38 } }, { name: 'P Lines medium', label: 'Lines, medium', spec: { kind: 'lines', step: 52 } }, { name: 'P Lines large', label: 'Lines, large', spec: { kind: 'lines', step: 76 } }, { name: 'P Margin medium', label: 'Lines with margin', spec: { kind: 'lines', step: 52, margin: 160 } }, { name: 'P Grid small', label: 'Grid, small', spec: { kind: 'grid', step: 32 } }, { name: 'P Grid medium', label: 'Grid, medium', spec: { kind: 'grid', step: 52 } }, { name: 'P Grid large', label: 'Grid, large', spec: { kind: 'grid', step: 76 } }, { name: 'P Dots S', label: 'Dots, small', spec: { kind: 'dots', step: 32 } }, { name: 'P Dots large', label: 'Dots, large', spec: { kind: 'dots', step: 64 } }, ]; export function templateSpec(name: string | null | undefined): TemplateSpec { if (!name) return { kind: 'blank' }; const t = TEMPLATES.find((x) => x.name === name); if (t) return t.spec; const n = name.toLowerCase(); if (/lined|lines|ruled/.test(n)) return { kind: 'lines', step: /small/.test(n) ? 38 : /large/.test(n) ? 76 : 52 }; if (/grid|graph|squared/.test(n)) return { kind: 'grid', step: /small/.test(n) ? 32 : /large/.test(n) ? 76 : 52 }; if (/dot/.test(n)) return { kind: 'dots', step: /large|\bl\b/.test(n) ? 64 : 32 }; return { kind: 'blank' }; }