@adecore/drawing 0.62.1 → 0.63.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/edits.js CHANGED
@@ -63,7 +63,6 @@ export const DrawingElementPatchSchema = z.strictObject({
63
63
  font: DrawingFontSchema.optional(),
64
64
  align: DrawingAlignSchema.optional()
65
65
  });
66
- /* Elements to add on top, changes by id and ids to take away, in one step. */
67
66
  export const DrawingEditSchema = z.strictObject({
68
67
  add: z.array(NewDrawingElementSchema).optional(),
69
68
  update: z.array(DrawingElementPatchSchema).optional(),
@@ -72,7 +71,6 @@ export const DrawingEditSchema = z.strictObject({
72
71
  function touched(edit) {
73
72
  return (edit.add?.length ?? 0) + (edit.update?.length ?? 0) + (edit.remove?.length ?? 0);
74
73
  }
75
- /* The problems of the ids an update or a remove names: unknown, locked, or named twice. */
76
74
  function idProblems(byId, edit) {
77
75
  const problems = [];
78
76
  const named = new Set();
@@ -93,7 +91,6 @@ function idProblems(byId, edit) {
93
91
  edit.remove?.forEach((id, i) => check(id, ['remove', i]));
94
92
  return problems;
95
93
  }
96
- /* The problems of fields a patch sets that its element's kind does not have. */
97
94
  function kindProblems(byId, edit) {
98
95
  const shared = new Set(Object.keys(placed));
99
96
  return (edit.update ?? []).flatMap((patch, i) => {
@@ -1,26 +1,11 @@
1
+ import { type Point, type Rect } from '@adecore/utils/geometry';
1
2
  import type { DrawingElement } from './protocol.ts';
2
- export interface Point {
3
- x: number;
4
- y: number;
5
- }
6
- export interface Rect {
7
- x: number;
8
- y: number;
9
- w: number;
10
- h: number;
11
- }
12
3
  export declare const RESIZE_HANDLES: readonly ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'];
13
4
  export type ResizeHandle = (typeof RESIZE_HANDLES)[number];
14
5
  export declare function boundsOf(element: Pick<DrawingElement, 'x' | 'y' | 'w' | 'h'>): Rect;
15
- export declare function centerOf(rect: Rect): Point;
16
- export declare function roundPoint(point: Point): Point;
17
- export declare function rotatePoint(point: Point, around: Point, angle: number): Point;
18
- export declare function unionOf(rects: readonly Rect[]): Rect | null;
19
6
  export declare function boundsOfElements(elements: readonly DrawingElement[]): Rect | null;
20
7
  export declare function drawnBoundsOf(element: DrawingElement): Rect;
21
8
  export declare function drawnBoundsOfElements(elements: readonly DrawingElement[]): Rect | null;
22
- export declare function intersects(a: Rect, b: Rect): boolean;
23
- export declare function rectFromPoints(from: Point, to: Point): Rect;
24
9
  export declare function absolutePoints(element: DrawingElement): Point[];
25
10
  export declare function isFilled(element: DrawingElement): boolean;
26
11
  export declare function hitsElement(element: DrawingElement, point: Point, tolerance: number): boolean;
package/dist/geometry.js CHANGED
@@ -1,44 +1,12 @@
1
+ import { centerOf, intersects, rotatePoint, unionOf } from '@adecore/utils/geometry';
1
2
  export const RESIZE_HANDLES = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'];
2
3
  export function boundsOf(element) {
3
4
  return { x: element.x, y: element.y, w: element.w, h: element.h };
4
5
  }
5
- export function centerOf(rect) {
6
- return { x: rect.x + rect.w / 2, y: rect.y + rect.h / 2 };
7
- }
8
- /* A point on the whole pixel, for a layout that lines its boxes and its lines up on a pixel grid. */
9
- export function roundPoint(point) {
10
- return { x: Math.round(point.x), y: Math.round(point.y) };
11
- }
12
- export function rotatePoint(point, around, angle) {
13
- if (angle === 0) {
14
- return point;
15
- }
16
- const cos = Math.cos(angle);
17
- const sin = Math.sin(angle);
18
- const dx = point.x - around.x;
19
- const dy = point.y - around.y;
20
- return { x: around.x + dx * cos - dy * sin, y: around.y + dx * sin + dy * cos };
21
- }
22
6
  /* A point in the element's own frame, so every hit test can pretend nothing is turned. */
23
7
  function toLocal(element, point) {
24
8
  return rotatePoint(point, centerOf(boundsOf(element)), -(element.angle ?? 0));
25
9
  }
26
- export function unionOf(rects) {
27
- if (rects.length === 0) {
28
- return null;
29
- }
30
- let left = Infinity;
31
- let top = Infinity;
32
- let right = -Infinity;
33
- let bottom = -Infinity;
34
- for (const rect of rects) {
35
- left = Math.min(left, rect.x, rect.x + rect.w);
36
- top = Math.min(top, rect.y, rect.y + rect.h);
37
- right = Math.max(right, rect.x, rect.x + rect.w);
38
- bottom = Math.max(bottom, rect.y, rect.y + rect.h);
39
- }
40
- return { x: left, y: top, w: right - left, h: bottom - top };
41
- }
42
10
  export function boundsOfElements(elements) {
43
11
  return unionOf(elements);
44
12
  }
@@ -68,17 +36,6 @@ export function drawnBoundsOf(element) {
68
36
  export function drawnBoundsOfElements(elements) {
69
37
  return unionOf(elements.map(drawnBoundsOf));
70
38
  }
71
- export function intersects(a, b) {
72
- return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
73
- }
74
- export function rectFromPoints(from, to) {
75
- return {
76
- x: Math.min(from.x, to.x),
77
- y: Math.min(from.y, to.y),
78
- w: Math.abs(to.x - from.x),
79
- h: Math.abs(to.y - from.y)
80
- };
81
- }
82
39
  function distanceToSegment(point, a, b) {
83
40
  const dx = b.x - a.x;
84
41
  const dy = b.y - a.y;
@@ -246,7 +203,6 @@ export function arrowHead(tip, from, size) {
246
203
  [tip, { x: tip.x - size * Math.cos(angle + spread), y: tip.y - size * Math.sin(angle + spread) }]
247
204
  ];
248
205
  }
249
- /* How long an arrow head is for a given stroke: heavier lines carry a bigger head. */
250
206
  export function arrowHeadSize(strokeWidth) {
251
207
  return 12 + strokeWidth * 4;
252
208
  }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- export { type Point, type Rect, RESIZE_HANDLES, type ResizeHandle, boundsOf, centerOf, roundPoint, rotatePoint, unionOf, boundsOfElements, intersects, rectFromPoints, elementAt, elementsIn, handlePoint, resizeRect, scaleElement } from './geometry.ts';
1
+ export { RESIZE_HANDLES, type ResizeHandle, boundsOf, boundsOfElements, elementAt, elementsIn, handlePoint, resizeRect, scaleElement } from './geometry.ts';
2
2
  export { DRAWING_EDIT_LIMITS, NewDrawingElementSchema, type NewDrawingElement, DrawingElementPatchSchema, type DrawingElementPatch, DrawingEditSchema, type DrawingEdit, type DrawingEditProblem, type DrawingEditOutcome, type DrawingEditMint, applyDrawingEdit } from './edits.ts';
3
3
  export { type ElementPath, pathsOfElement } from './paths.ts';
4
4
  export { type DrawingRebase, rebaseDrawingElements } from './rebase.ts';
5
5
  export { readingOrder } from './reading-order.ts';
6
+ export { type Scene, type SceneColor, type SceneElement, type ScenePath, type SceneText, drawingScene, sceneColor } from './scene.ts';
6
7
  export { type SvgOptions, DEFAULT_SVG_MARGIN, DEFAULT_PALETTE, DEFAULT_PAPER, DEFAULT_EDGE, escapeXml, toSvg } from './svg.ts';
7
8
  export { LINE_HEIGHT, DEFAULT_FONT_STACKS, type WrittenElement, NOTE_PADDING, writingFrameOf, type MeasureLine, approximateMeasure, linesOf, fontOf } from './text.ts';
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
- export { RESIZE_HANDLES, boundsOf, centerOf, roundPoint, rotatePoint, unionOf, boundsOfElements, intersects, rectFromPoints, elementAt, elementsIn, handlePoint, resizeRect, scaleElement } from './geometry.js';
1
+ export { RESIZE_HANDLES, boundsOf, boundsOfElements, elementAt, elementsIn, handlePoint, resizeRect, scaleElement } from './geometry.js';
2
2
  export { DRAWING_EDIT_LIMITS, NewDrawingElementSchema, DrawingElementPatchSchema, DrawingEditSchema, applyDrawingEdit } from './edits.js';
3
3
  export { pathsOfElement } from './paths.js';
4
4
  export { rebaseDrawingElements } from './rebase.js';
5
5
  export { readingOrder } from './reading-order.js';
6
+ export { drawingScene, sceneColor } from './scene.js';
6
7
  export { DEFAULT_SVG_MARGIN, DEFAULT_PALETTE, DEFAULT_PAPER, DEFAULT_EDGE, escapeXml, toSvg } from './svg.js';
7
8
  export { LINE_HEIGHT, DEFAULT_FONT_STACKS, NOTE_PADDING, writingFrameOf, approximateMeasure, linesOf, fontOf } from './text.js';
@@ -1,7 +1,7 @@
1
- import { absolutePoints, boundsOf, centerOf, hitsElement } from './geometry.js';
1
+ import { centerOf } from '@adecore/utils/geometry';
2
+ import { absolutePoints, boundsOf, hitsElement } from './geometry.js';
2
3
  /* Two texts within this many units of each other are on the same line and read left to right. */
3
4
  const ROW_TOLERANCE = 24;
4
- /* How far from an arrow's end a label may sit and still be what the arrow points at. */
5
5
  const LABEL_REACH = 48;
6
6
  function labelOf(element) {
7
7
  return element.kind === 'text' || element.kind === 'note' ? element.text.trim() || null : null;
package/dist/rebase.js CHANGED
@@ -14,7 +14,6 @@ function canonical(value) {
14
14
  function byId(elements) {
15
15
  return new Map(elements.map((element) => [element.id, { element, text: canonical(element) }]));
16
16
  }
17
- /* Whether a side stacked the elements it shares with the base in another order. */
18
17
  function restacked(base, side) {
19
18
  const shared = new Set(base.map((element) => element.id));
20
19
  const order = side.map((element) => element.id).filter((id) => shared.has(id));
@@ -0,0 +1,40 @@
1
+ import { type Rect } from '@adecore/utils/geometry';
2
+ import type { DrawingAlign, DrawingColor, DrawingDocument, DrawingFont } from './protocol.ts';
3
+ export interface SceneColor {
4
+ tone: DrawingColor;
5
+ palette: 'ink' | 'paper' | 'edge';
6
+ }
7
+ export interface ScenePath {
8
+ d: string;
9
+ stroke: SceneColor | null;
10
+ fill: SceneColor | null;
11
+ strokeWidth: number;
12
+ dash: number[] | null;
13
+ }
14
+ export interface SceneText {
15
+ text: string;
16
+ x: number;
17
+ y: number;
18
+ size: number;
19
+ bold: boolean;
20
+ align: DrawingAlign;
21
+ font: DrawingFont;
22
+ color: SceneColor;
23
+ }
24
+ export interface SceneElement {
25
+ id: string;
26
+ x: number;
27
+ y: number;
28
+ angle: number;
29
+ centerX: number;
30
+ centerY: number;
31
+ paths: ScenePath[];
32
+ text: SceneText[];
33
+ }
34
+ export interface Scene {
35
+ rev: number;
36
+ bounds: Rect;
37
+ elements: SceneElement[];
38
+ }
39
+ export declare function sceneColor(tone: DrawingColor, palette?: SceneColor['palette']): SceneColor;
40
+ export declare function drawingScene(document: Pick<DrawingDocument, 'rev' | 'elements'>): Scene;
package/dist/scene.js ADDED
@@ -0,0 +1,71 @@
1
+ import { centerOf, rotatePoint, unionOf } from '@adecore/utils/geometry';
2
+ import { pathsOfElement } from './paths.js';
3
+ import { approximateMeasure, fontOf, lineAnchorX, lineBaselineY, linesOf } from './text.js';
4
+ export function sceneColor(tone, palette = 'ink') {
5
+ return { tone, palette };
6
+ }
7
+ function sceneText(element) {
8
+ if (element.kind !== 'text' && element.kind !== 'note') {
9
+ return [];
10
+ }
11
+ const x = lineAnchorX(element);
12
+ return linesOf(element, approximateMeasure(element.size, element.font)).map((text, index) => ({
13
+ text,
14
+ x,
15
+ y: lineBaselineY(element, index),
16
+ size: element.size,
17
+ bold: false,
18
+ align: element.align ?? 'left',
19
+ font: fontOf(element.font),
20
+ color: sceneColor(element.stroke)
21
+ }));
22
+ }
23
+ function scenePaths(element) {
24
+ const note = element.kind === 'note';
25
+ return pathsOfElement(element).map((path) => {
26
+ if (path.role === 'stroke') {
27
+ return {
28
+ d: path.d,
29
+ stroke: note ? sceneColor(element.fillColor ?? element.stroke, 'edge') : sceneColor(element.stroke),
30
+ fill: null,
31
+ strokeWidth: path.strokeWidth,
32
+ dash: path.dash
33
+ };
34
+ }
35
+ const fill = path.role === 'ink' ? sceneColor(element.stroke) : sceneColor(element.fillColor ?? element.stroke, note ? 'paper' : 'ink');
36
+ const hachure = path.role === 'fill' && path.strokeWidth > 0 && element.fill === 'hachure';
37
+ return { d: path.d, stroke: hachure ? fill : null, fill: hachure ? null : fill, strokeWidth: path.strokeWidth, dash: path.dash };
38
+ });
39
+ }
40
+ function rotatedBounds(elements) {
41
+ const corners = elements.flatMap((element) => {
42
+ const center = centerOf(element);
43
+ return [
44
+ [0, 0],
45
+ [element.w, 0],
46
+ [0, element.h],
47
+ [element.w, element.h]
48
+ ].map(([dx, dy]) => {
49
+ const point = rotatePoint({ x: element.x + dx, y: element.y + dy }, center, element.angle ?? 0);
50
+ return { ...point, w: 0, h: 0 };
51
+ });
52
+ });
53
+ return unionOf(corners) ?? { x: 0, y: 0, w: 0, h: 0 };
54
+ }
55
+ /* The scene of a drawing: the paths of `pathsOfElement` and the lines of `linesOf` placed as `toSvg` places them, with the bounds of the rotated elements. */
56
+ export function drawingScene(document) {
57
+ return {
58
+ rev: document.rev,
59
+ bounds: rotatedBounds(document.elements),
60
+ elements: document.elements.map((element) => ({
61
+ id: element.id,
62
+ x: element.x,
63
+ y: element.y,
64
+ angle: element.angle ?? 0,
65
+ centerX: element.w / 2,
66
+ centerY: element.h / 2,
67
+ paths: scenePaths(element),
68
+ text: sceneText(element)
69
+ }))
70
+ };
71
+ }
package/dist/svg.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { drawnBoundsOfElements } from './geometry.js';
2
2
  import { pathsOfElement } from './paths.js';
3
- import { DEFAULT_FONT_STACKS, LINE_HEIGHT, approximateMeasure, fontOf, linesOf, writingFrameOf } from './text.js';
3
+ import { DEFAULT_FONT_STACKS, approximateMeasure, fontOf, lineAnchorX, lineBaselineY, linesOf } from './text.js';
4
4
  export const DEFAULT_SVG_MARGIN = 32;
5
5
  // Fallback colors for rendering without a host theme.
6
6
  export const DEFAULT_PALETTE = {
@@ -59,11 +59,10 @@ function textSvg(element, options) {
59
59
  const font = fontOf(element.font);
60
60
  const family = options.fonts?.[font] ?? DEFAULT_FONT_STACKS[font];
61
61
  const anchor = element.align === 'center' ? 'middle' : element.align === 'right' ? 'end' : 'start';
62
- const frame = writingFrameOf(element);
63
- const dx = frame.x + (element.align === 'center' ? frame.w / 2 : element.align === 'right' ? frame.w : 0);
62
+ const dx = lineAnchorX(element);
64
63
  const measure = options.measure?.(element) ?? approximateMeasure(element.size, element.font);
65
64
  const lines = linesOf(element, measure)
66
- .map((line, index) => `<tspan x="${round(dx)}" y="${round(frame.y + (index + 0.8) * element.size * LINE_HEIGHT)}">${escapeXml(line)}</tspan>`)
65
+ .map((line, index) => `<tspan x="${round(dx)}" y="${round(lineBaselineY(element, index))}">${escapeXml(line)}</tspan>`)
67
66
  .join('');
68
67
  return `<text font-family="${escapeXml(family)}" font-size="${element.size}" fill="${options.palette[element.stroke]}" text-anchor="${anchor}">${lines}</text>`;
69
68
  }
package/dist/text.d.ts CHANGED
@@ -11,6 +11,8 @@ export declare function writingFrameOf(element: WrittenElement): {
11
11
  y: number;
12
12
  w: number;
13
13
  };
14
+ export declare function lineAnchorX(element: WrittenElement): number;
15
+ export declare function lineBaselineY(element: WrittenElement, index: number): number;
14
16
  export type MeasureLine = (line: string) => number;
15
17
  export declare function approximateMeasure(size: number, font: DrawingFont | undefined): MeasureLine;
16
18
  export declare function wrapLines(text: string, maxWidth: number, measure: MeasureLine): string[];
package/dist/text.js CHANGED
@@ -17,6 +17,15 @@ export const NOTE_RADIUS = 8;
17
17
  export function writingFrameOf(element) {
18
18
  return element.kind === 'note' ? { x: NOTE_PADDING, y: NOTE_PADDING, w: Math.max(1, element.w - NOTE_PADDING * 2) } : { x: 0, y: 0, w: element.w };
19
19
  }
20
+ /* Where every line of an element is anchored along its width, inside the element: its left edge, middle or right edge by `align`. */
21
+ export function lineAnchorX(element) {
22
+ const frame = writingFrameOf(element);
23
+ return frame.x + (element.align === 'center' ? frame.w / 2 : element.align === 'right' ? frame.w : 0);
24
+ }
25
+ /* The baseline of the line at `index`, inside the element: 0.8 of its size below the top of its line. */
26
+ export function lineBaselineY(element, index) {
27
+ return writingFrameOf(element).y + (index + 0.8) * element.size * LINE_HEIGHT;
28
+ }
20
29
  /* A measure for where no font exists, such as on a server: most faces average a glyph a little over half its size wide. */
21
30
  export function approximateMeasure(size, font) {
22
31
  const glyph = fontOf(font) === 'mono' ? 0.6 : 0.55;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adecore/drawing",
3
- "version": "0.62.1",
3
+ "version": "0.63.0",
4
4
  "description": "Drawing schemas, geometry, deterministic paths, SVG export and reading order.",
5
5
  "license": "FSL-1.1-MIT",
6
6
  "author": "Bas Milius",
@@ -45,6 +45,7 @@
45
45
  "typescript": "^7.0.2"
46
46
  },
47
47
  "dependencies": {
48
+ "@adecore/utils": "0.63.0",
48
49
  "perfect-freehand": "^1.2.3",
49
50
  "roughjs": "^4.6.6",
50
51
  "zod": "^4.6.7"
package/src/edits.ts CHANGED
@@ -85,7 +85,6 @@ export const DrawingElementPatchSchema = z.strictObject({
85
85
  });
86
86
  export type DrawingElementPatch = z.infer<typeof DrawingElementPatchSchema>;
87
87
 
88
- /* Elements to add on top, changes by id and ids to take away, in one step. */
89
88
  export const DrawingEditSchema = z.strictObject({
90
89
  add: z.array(NewDrawingElementSchema).optional(),
91
90
  update: z.array(DrawingElementPatchSchema).optional(),
@@ -93,7 +92,6 @@ export const DrawingEditSchema = z.strictObject({
93
92
  });
94
93
  export type DrawingEdit = z.infer<typeof DrawingEditSchema>;
95
94
 
96
- /* Where an edit went wrong, as the path into the edit and what is wrong there. */
97
95
  export interface DrawingEditProblem {
98
96
  path: (string | number)[];
99
97
  message: string;
@@ -111,7 +109,6 @@ function touched(edit: DrawingEdit): number {
111
109
  return (edit.add?.length ?? 0) + (edit.update?.length ?? 0) + (edit.remove?.length ?? 0);
112
110
  }
113
111
 
114
- /* The problems of the ids an update or a remove names: unknown, locked, or named twice. */
115
112
  function idProblems(byId: ReadonlyMap<string, DrawingElement>, edit: DrawingEdit): DrawingEditProblem[] {
116
113
  const problems: DrawingEditProblem[] = [];
117
114
  const named = new Set<string>();
@@ -136,7 +133,6 @@ function idProblems(byId: ReadonlyMap<string, DrawingElement>, edit: DrawingEdit
136
133
  return problems;
137
134
  }
138
135
 
139
- /* The problems of fields a patch sets that its element's kind does not have. */
140
136
  function kindProblems(byId: ReadonlyMap<string, DrawingElement>, edit: DrawingEdit): DrawingEditProblem[] {
141
137
  const shared = new Set(Object.keys(placed));
142
138
 
package/src/geometry.ts CHANGED
@@ -1,17 +1,6 @@
1
+ import { centerOf, intersects, rotatePoint, unionOf, type Point, type Rect } from '@adecore/utils/geometry';
1
2
  import type { DrawingElement } from './protocol.ts';
2
3
 
3
- export interface Point {
4
- x: number;
5
- y: number;
6
- }
7
-
8
- export interface Rect {
9
- x: number;
10
- y: number;
11
- w: number;
12
- h: number;
13
- }
14
-
15
4
  export const RESIZE_HANDLES = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'] as const;
16
5
  export type ResizeHandle = (typeof RESIZE_HANDLES)[number];
17
6
 
@@ -19,53 +8,11 @@ export function boundsOf(element: Pick<DrawingElement, 'x' | 'y' | 'w' | 'h'>):
19
8
  return { x: element.x, y: element.y, w: element.w, h: element.h };
20
9
  }
21
10
 
22
- export function centerOf(rect: Rect): Point {
23
- return { x: rect.x + rect.w / 2, y: rect.y + rect.h / 2 };
24
- }
25
-
26
- /* A point on the whole pixel, for a layout that lines its boxes and its lines up on a pixel grid. */
27
- export function roundPoint(point: Point): Point {
28
- return { x: Math.round(point.x), y: Math.round(point.y) };
29
- }
30
-
31
- export function rotatePoint(point: Point, around: Point, angle: number): Point {
32
- if (angle === 0) {
33
- return point;
34
- }
35
-
36
- const cos = Math.cos(angle);
37
- const sin = Math.sin(angle);
38
- const dx = point.x - around.x;
39
- const dy = point.y - around.y;
40
-
41
- return { x: around.x + dx * cos - dy * sin, y: around.y + dx * sin + dy * cos };
42
- }
43
-
44
11
  /* A point in the element's own frame, so every hit test can pretend nothing is turned. */
45
12
  function toLocal(element: DrawingElement, point: Point): Point {
46
13
  return rotatePoint(point, centerOf(boundsOf(element)), -(element.angle ?? 0));
47
14
  }
48
15
 
49
- export function unionOf(rects: readonly Rect[]): Rect | null {
50
- if (rects.length === 0) {
51
- return null;
52
- }
53
-
54
- let left = Infinity;
55
- let top = Infinity;
56
- let right = -Infinity;
57
- let bottom = -Infinity;
58
-
59
- for (const rect of rects) {
60
- left = Math.min(left, rect.x, rect.x + rect.w);
61
- top = Math.min(top, rect.y, rect.y + rect.h);
62
- right = Math.max(right, rect.x, rect.x + rect.w);
63
- bottom = Math.max(bottom, rect.y, rect.y + rect.h);
64
- }
65
-
66
- return { x: left, y: top, w: right - left, h: bottom - top };
67
- }
68
-
69
16
  export function boundsOfElements(elements: readonly DrawingElement[]): Rect | null {
70
17
  return unionOf(elements);
71
18
  }
@@ -102,19 +49,6 @@ export function drawnBoundsOfElements(elements: readonly DrawingElement[]): Rect
102
49
  return unionOf(elements.map(drawnBoundsOf));
103
50
  }
104
51
 
105
- export function intersects(a: Rect, b: Rect): boolean {
106
- return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
107
- }
108
-
109
- export function rectFromPoints(from: Point, to: Point): Rect {
110
- return {
111
- x: Math.min(from.x, to.x),
112
- y: Math.min(from.y, to.y),
113
- w: Math.abs(to.x - from.x),
114
- h: Math.abs(to.y - from.y)
115
- };
116
- }
117
-
118
52
  function distanceToSegment(point: Point, a: Point, b: Point): number {
119
53
  const dx = b.x - a.x;
120
54
  const dy = b.y - a.y;
@@ -327,7 +261,6 @@ export function arrowHead(tip: Point, from: Point, size: number): [Point, Point]
327
261
  ];
328
262
  }
329
263
 
330
- /* How long an arrow head is for a given stroke: heavier lines carry a bigger head. */
331
264
  export function arrowHeadSize(strokeWidth: number): number {
332
265
  return 12 + strokeWidth * 4;
333
266
  }
package/src/index.ts CHANGED
@@ -1,22 +1,4 @@
1
- export {
2
- type Point,
3
- type Rect,
4
- RESIZE_HANDLES,
5
- type ResizeHandle,
6
- boundsOf,
7
- centerOf,
8
- roundPoint,
9
- rotatePoint,
10
- unionOf,
11
- boundsOfElements,
12
- intersects,
13
- rectFromPoints,
14
- elementAt,
15
- elementsIn,
16
- handlePoint,
17
- resizeRect,
18
- scaleElement
19
- } from './geometry.ts';
1
+ export { RESIZE_HANDLES, type ResizeHandle, boundsOf, boundsOfElements, elementAt, elementsIn, handlePoint, resizeRect, scaleElement } from './geometry.ts';
20
2
  export {
21
3
  DRAWING_EDIT_LIMITS,
22
4
  NewDrawingElementSchema,
@@ -33,6 +15,7 @@ export {
33
15
  export { type ElementPath, pathsOfElement } from './paths.ts';
34
16
  export { type DrawingRebase, rebaseDrawingElements } from './rebase.ts';
35
17
  export { readingOrder } from './reading-order.ts';
18
+ export { type Scene, type SceneColor, type SceneElement, type ScenePath, type SceneText, drawingScene, sceneColor } from './scene.ts';
36
19
  export { type SvgOptions, DEFAULT_SVG_MARGIN, DEFAULT_PALETTE, DEFAULT_PAPER, DEFAULT_EDGE, escapeXml, toSvg } from './svg.ts';
37
20
  export {
38
21
  LINE_HEIGHT,
package/src/paths.ts CHANGED
@@ -1,9 +1,10 @@
1
+ import type { Point } from '@adecore/utils/geometry';
1
2
  import { getStroke } from 'perfect-freehand';
2
3
  // Rough's unbundled modules use extensionless imports; its package entry also runs in Node.
3
4
  import rough from 'roughjs';
4
5
  import type { Options } from 'roughjs/bin/core.js';
5
6
  import type { RoughGenerator } from 'roughjs/bin/generator.js';
6
- import { arrowHead, arrowHeadSize, isFilled, type Point } from './geometry.ts';
7
+ import { arrowHead, arrowHeadSize, isFilled } from './geometry.ts';
7
8
  import type { DrawingElement } from './protocol.ts';
8
9
  import { NOTE_RADIUS } from './text.ts';
9
10
 
@@ -16,7 +17,6 @@ export interface ElementPath {
16
17
  /* `stroke` outlines in the stroke color, `fill` fills in the fill color, `ink` fills in the stroke color. */
17
18
  role: 'stroke' | 'fill' | 'ink';
18
19
  strokeWidth: number;
19
- /* Whole units on and off, or null for a solid line. */
20
20
  dash: number[] | null;
21
21
  }
22
22
 
@@ -1,10 +1,10 @@
1
- import { absolutePoints, boundsOf, centerOf, hitsElement, type Point } from './geometry.ts';
1
+ import { centerOf, type Point } from '@adecore/utils/geometry';
2
+ import { absolutePoints, boundsOf, hitsElement } from './geometry.ts';
2
3
  import type { DrawingElement } from './protocol.ts';
3
4
 
4
5
  /* Two texts within this many units of each other are on the same line and read left to right. */
5
6
  const ROW_TOLERANCE = 24;
6
7
 
7
- /* How far from an arrow's end a label may sit and still be what the arrow points at. */
8
8
  const LABEL_REACH = 48;
9
9
 
10
10
  function labelOf(element: DrawingElement): string | null {
package/src/rebase.ts CHANGED
@@ -23,7 +23,6 @@ function byId(elements: readonly DrawingElement[]): Map<string, { element: Drawi
23
23
  return new Map(elements.map((element) => [element.id, { element, text: canonical(element) }]));
24
24
  }
25
25
 
26
- /* Whether a side stacked the elements it shares with the base in another order. */
27
26
  function restacked(base: readonly DrawingElement[], side: readonly DrawingElement[]): boolean {
28
27
  const shared = new Set(base.map((element) => element.id));
29
28
  const order = side.map((element) => element.id).filter((id) => shared.has(id));
package/src/scene.ts ADDED
@@ -0,0 +1,133 @@
1
+ import { centerOf, rotatePoint, unionOf, type Rect } from '@adecore/utils/geometry';
2
+ import { pathsOfElement } from './paths.ts';
3
+ import type { DrawingAlign, DrawingColor, DrawingDocument, DrawingElement, DrawingFont } from './protocol.ts';
4
+ import { approximateMeasure, fontOf, lineAnchorX, lineBaselineY, linesOf } from './text.ts';
5
+
6
+ /* A palette name and which of the theme's palettes to read it from: `paper` and `edge` are the sheets of a note. */
7
+ export interface SceneColor {
8
+ tone: DrawingColor;
9
+ palette: 'ink' | 'paper' | 'edge';
10
+ }
11
+
12
+ export interface ScenePath {
13
+ d: string;
14
+ stroke: SceneColor | null;
15
+ fill: SceneColor | null;
16
+ strokeWidth: number;
17
+ dash: number[] | null;
18
+ }
19
+
20
+ export interface SceneText {
21
+ text: string;
22
+ x: number;
23
+ /* The baseline, in the element's local coordinate system. */
24
+ y: number;
25
+ size: number;
26
+ bold: boolean;
27
+ align: DrawingAlign;
28
+ font: DrawingFont;
29
+ color: SceneColor;
30
+ }
31
+
32
+ export interface SceneElement {
33
+ id: string;
34
+ x: number;
35
+ y: number;
36
+ /* Translate by x and y, then rotate by `angle` radians about (`centerX`, `centerY`) in the element's own frame. */
37
+ angle: number;
38
+ centerX: number;
39
+ centerY: number;
40
+ paths: ScenePath[];
41
+ text: SceneText[];
42
+ }
43
+
44
+ /*
45
+ * A document as a native painter draws it: paths and text lines with palette names, never colors, so it
46
+ * reads the same geometry as `toSvg`. Elements go back to front, each drawing its paths before its text.
47
+ */
48
+ export interface Scene {
49
+ rev: number;
50
+ bounds: Rect;
51
+ elements: SceneElement[];
52
+ }
53
+
54
+ export function sceneColor(tone: DrawingColor, palette: SceneColor['palette'] = 'ink'): SceneColor {
55
+ return { tone, palette };
56
+ }
57
+
58
+ function sceneText(element: DrawingElement): SceneText[] {
59
+ if (element.kind !== 'text' && element.kind !== 'note') {
60
+ return [];
61
+ }
62
+
63
+ const x = lineAnchorX(element);
64
+
65
+ return linesOf(element, approximateMeasure(element.size, element.font)).map((text, index) => ({
66
+ text,
67
+ x,
68
+ y: lineBaselineY(element, index),
69
+ size: element.size,
70
+ bold: false,
71
+ align: element.align ?? 'left',
72
+ font: fontOf(element.font),
73
+ color: sceneColor(element.stroke)
74
+ }));
75
+ }
76
+
77
+ function scenePaths(element: DrawingElement): ScenePath[] {
78
+ const note = element.kind === 'note';
79
+
80
+ return pathsOfElement(element).map((path) => {
81
+ if (path.role === 'stroke') {
82
+ return {
83
+ d: path.d,
84
+ stroke: note ? sceneColor(element.fillColor ?? element.stroke, 'edge') : sceneColor(element.stroke),
85
+ fill: null,
86
+ strokeWidth: path.strokeWidth,
87
+ dash: path.dash
88
+ };
89
+ }
90
+
91
+ const fill = path.role === 'ink' ? sceneColor(element.stroke) : sceneColor(element.fillColor ?? element.stroke, note ? 'paper' : 'ink');
92
+ const hachure = path.role === 'fill' && path.strokeWidth > 0 && element.fill === 'hachure';
93
+
94
+ return { d: path.d, stroke: hachure ? fill : null, fill: hachure ? null : fill, strokeWidth: path.strokeWidth, dash: path.dash };
95
+ });
96
+ }
97
+
98
+ function rotatedBounds(elements: readonly DrawingElement[]): Rect {
99
+ const corners = elements.flatMap((element) => {
100
+ const center = centerOf(element);
101
+
102
+ return [
103
+ [0, 0],
104
+ [element.w, 0],
105
+ [0, element.h],
106
+ [element.w, element.h]
107
+ ].map(([dx, dy]) => {
108
+ const point = rotatePoint({ x: element.x + dx!, y: element.y + dy! }, center, element.angle ?? 0);
109
+
110
+ return { ...point, w: 0, h: 0 };
111
+ });
112
+ });
113
+
114
+ return unionOf(corners) ?? { x: 0, y: 0, w: 0, h: 0 };
115
+ }
116
+
117
+ /* The scene of a drawing: the paths of `pathsOfElement` and the lines of `linesOf` placed as `toSvg` places them, with the bounds of the rotated elements. */
118
+ export function drawingScene(document: Pick<DrawingDocument, 'rev' | 'elements'>): Scene {
119
+ return {
120
+ rev: document.rev,
121
+ bounds: rotatedBounds(document.elements),
122
+ elements: document.elements.map((element) => ({
123
+ id: element.id,
124
+ x: element.x,
125
+ y: element.y,
126
+ angle: element.angle ?? 0,
127
+ centerX: element.w / 2,
128
+ centerY: element.h / 2,
129
+ paths: scenePaths(element),
130
+ text: sceneText(element)
131
+ }))
132
+ };
133
+ }
package/src/svg.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { drawnBoundsOfElements, type Rect } from './geometry.ts';
1
+ import type { Rect } from '@adecore/utils/geometry';
2
+ import { drawnBoundsOfElements } from './geometry.ts';
2
3
  import { pathsOfElement, type ElementPath } from './paths.ts';
3
4
  import type { DrawingColor, DrawingElement, DrawingFont } from './protocol.ts';
4
- import { DEFAULT_FONT_STACKS, LINE_HEIGHT, approximateMeasure, fontOf, linesOf, writingFrameOf, type MeasureLine, type WrittenElement } from './text.ts';
5
+ import { DEFAULT_FONT_STACKS, approximateMeasure, fontOf, lineAnchorX, lineBaselineY, linesOf, type MeasureLine, type WrittenElement } from './text.ts';
5
6
 
6
7
  export interface SvgOptions {
7
8
  /* What every palette name is in the theme this export is made in; without it the light theme's colors are used. */
8
9
  palette?: Record<DrawingColor, string>;
9
- /* The paper behind the drawing, or null for a transparent one. */
10
10
  background?: string | null;
11
11
  /* World units around the drawing, so nothing touches the edge. */
12
12
  margin?: number;
@@ -88,12 +88,11 @@ function textSvg(element: WrittenElement, options: PaintOptions): string {
88
88
  const font = fontOf(element.font);
89
89
  const family = options.fonts?.[font] ?? DEFAULT_FONT_STACKS[font];
90
90
  const anchor = element.align === 'center' ? 'middle' : element.align === 'right' ? 'end' : 'start';
91
- const frame = writingFrameOf(element);
92
- const dx = frame.x + (element.align === 'center' ? frame.w / 2 : element.align === 'right' ? frame.w : 0);
91
+ const dx = lineAnchorX(element);
93
92
  const measure = options.measure?.(element) ?? approximateMeasure(element.size, element.font);
94
93
 
95
94
  const lines = linesOf(element, measure)
96
- .map((line, index) => `<tspan x="${round(dx)}" y="${round(frame.y + (index + 0.8) * element.size * LINE_HEIGHT)}">${escapeXml(line)}</tspan>`)
95
+ .map((line, index) => `<tspan x="${round(dx)}" y="${round(lineBaselineY(element, index))}">${escapeXml(line)}</tspan>`)
97
96
  .join('');
98
97
 
99
98
  return `<text font-family="${escapeXml(family)}" font-size="${element.size}" fill="${options.palette[element.stroke]}" text-anchor="${anchor}">${lines}</text>`;
package/src/text.ts CHANGED
@@ -26,6 +26,18 @@ export function writingFrameOf(element: WrittenElement): { x: number; y: number;
26
26
  return element.kind === 'note' ? { x: NOTE_PADDING, y: NOTE_PADDING, w: Math.max(1, element.w - NOTE_PADDING * 2) } : { x: 0, y: 0, w: element.w };
27
27
  }
28
28
 
29
+ /* Where every line of an element is anchored along its width, inside the element: its left edge, middle or right edge by `align`. */
30
+ export function lineAnchorX(element: WrittenElement): number {
31
+ const frame = writingFrameOf(element);
32
+
33
+ return frame.x + (element.align === 'center' ? frame.w / 2 : element.align === 'right' ? frame.w : 0);
34
+ }
35
+
36
+ /* The baseline of the line at `index`, inside the element: 0.8 of its size below the top of its line. */
37
+ export function lineBaselineY(element: WrittenElement, index: number): number {
38
+ return writingFrameOf(element).y + (index + 0.8) * element.size * LINE_HEIGHT;
39
+ }
40
+
29
41
  export type MeasureLine = (line: string) => number;
30
42
 
31
43
  /* A measure for where no font exists, such as on a server: most faces average a glyph a little over half its size wide. */