feat(app): editor Annotations tab, core-decided placement, repair rules

This commit is contained in:
bchanot committed 2026-11-04 13:49:41 +01:00
1 parent 92c595caa1
commit 26097852ff
37 files changed
+5001 -6

No files matched your search

+147
View File
@@ -0,0 +1,147 @@
/**
* Proposal arithmetic for a new point (plan « Default point and the frame », C26). The
* page only PROPOSES values; the core decides every one of them on the candidate
* document. Nothing here copies a core rule (no visibility constant, no preview size,
* no twin radius, no edge tolerance).
*/
export interface Frame {
x: number
y: number
w: number
h: number
}
export interface XY {
x: number
y: number
}
export interface CircleShape {
cx: number
cy: number
r: number
}
export type PointProposal =
| { kind: 'outside' }
| { kind: 'circle'; circle: CircleShape; shrunk: boolean }
| { kind: 'corrected'; circle: CircleShape }
/** Float noise of `v × 100` must not move a value across a hundredth. */
const EPSILON = 1e-9
export const round2 = (value: number): number => Math.round(value * 100) / 100
const floor2 = (value: number): number =>
Math.floor(value * 100 + EPSILON) / 100
const ceil2 = (value: number): number => Math.ceil(value * 100 - EPSILON) / 100
/**
* F from the composed root's `viewBox` attribute TEXT (the property `baseVal` is held in
* f32). Null when it is not four finite numbers or the span is not positive: the caller
* then has no frame, never the fallback `0 0 100 100`.
*/
export function parseFrame(viewBox: string | null): Frame | null {
if (viewBox === null) return null
const parts = viewBox
.trim()
.split(/[\s,]+/)
.map(Number)
if (parts.length !== 4 || !parts.every(Number.isFinite)) return null
const [x, y, w, h] = parts
return w > 0 && h > 0 ? { x, y, w, h } : null
}
/** r0 = min(12 + 0.03 × max(w, h), min(w, h) / 2), floored to 0.01. */
export function defaultRadius(frame: Frame): number {
const wanted = 12 + 0.03 * Math.max(frame.w, frame.h)
return floor2(Math.min(wanted, Math.min(frame.w, frame.h) / 2))
}
/** Where a keyboard-created point sits: the centre of F, which fits by construction. */
export function centreCircle(frame: Frame): CircleShape {
return {
cx: round2(frame.x + frame.w / 2),
cy: round2(frame.y + frame.h / 2),
r: defaultRadius(frame),
}
}
function distanceToEdge(frame: Frame, at: XY): number {
return Math.min(
at.x - frame.x,
frame.x + frame.w - at.x,
at.y - frame.y,
frame.y + frame.h - at.y,
)
}
/** One axis of candidate C: clamped inward on the hundredth grid, else exactly. */
function clampAxis(value: number, low: number, high: number): number {
const inwardLow = ceil2(low)
const inwardHigh = floor2(high)
if (inwardLow <= inwardHigh) {
return Math.min(Math.max(value, inwardLow), inwardHigh)
}
return Math.min(Math.max(value, low), high)
}
/**
* Candidate C: radius r0, the centre clamped per axis into [x + r0, x + w − r0] and
* [y + r0, y + h − r0], rounded inward to 0.01. When that rounding leaves an empty
* interval (r0 = min(w, h) / 2 with an origin off the grid) the exact clamped value is
* used: the core accepts any decimals.
*/
export function correctedCircle(frame: Frame, at: XY): CircleShape {
const r = defaultRadius(frame)
return {
cx: clampAxis(round2(at.x), frame.x + r, frame.x + frame.w - r),
cy: clampAxis(round2(at.y), frame.y + r, frame.y + frame.h - r),
r,
}
}
/**
* The proposal at a click (c rounded to 0.01, d its distance to the nearest edge of F):
* A (c, r0) when d ≥ r0; B (c, floor₀.₀₁(d)) when that radius is at least 0.01, else C
* at once; nothing outside F (the edge is inclusive).
*/
export function proposePoint(frame: Frame, click: XY): PointProposal {
if (distanceToEdge(frame, click) < 0) return { kind: 'outside' }
const at = { x: round2(click.x), y: round2(click.y) }
// A click inside F whose rounding lands outside it has no room: straight to C.
const distance = Math.max(distanceToEdge(frame, at), 0)
const r0 = defaultRadius(frame)
if (distance >= r0) {
return {
kind: 'circle',
circle: { cx: at.x, cy: at.y, r: r0 },
shrunk: false,
}
}
const smaller = floor2(distance)
if (smaller < 0.01) {
return { kind: 'corrected', circle: correctedCircle(frame, at) }
}
return {
kind: 'circle',
circle: { cx: at.x, cy: at.y, r: smaller },
shrunk: true,
}
}
/** The three vertices of a keyboard-created zone or link: 10 % of the smaller side. */
export function centreTriangle(frame: Frame): [number, number][] {
const side = 0.1 * Math.min(frame.w, frame.h)
const cx = frame.x + frame.w / 2
const cy = frame.y + frame.h / 2
const corners: [number, number][] = [
[cx, cy - side / 2],
[cx + side / 2, cy + side / 2],
[cx - side / 2, cy + side / 2],
]
return corners.map(([x, y]) => [round2(x), round2(y)])
}
export const sameCircle = (a: CircleShape, b: CircleShape): boolean =>
a.cx === b.cx && a.cy === b.cy && a.r === b.r