Skip to content

packages/text/src/lib/bounds.ts

Read as Markdown

This is the source snapshot used to build these API details. View this revision on GitHub.

Back to reference

1 import type { TextGeometryResult } from '../index.js';
2 import type { PathGeometry } from "@pibbl/core";
3 /**
4  * Axis-aligned bounds of visible glyph outlines in logical output coordinates.
5  *
6  * @see {@link TextGeometryResult}
7  */
8 export interface InkBounds {
9   /**
10    * Horizontal coordinate or displacement in the containing coordinate system. See
11    * {@link InkBounds}.
12    */
13   readonly x: number;
14   /**
15    * Vertical coordinate or displacement in the containing coordinate system. See {@link InkBounds}
16    * .
17    */
18   readonly y: number;
19   /**
20    * Horizontal extent in the units of the containing geometry or surface. See {@link InkBounds}.
21    */
22   readonly width: number;
23   /** Vertical extent in the units of the containing geometry or surface. See {@link InkBounds}. */
24   readonly height: number;
25 }
26 /** Curve extrema, not control-point bounds. One linear traversal; no flattening. */
27 export function inkBounds(path: PathGeometry): Readonly<InkBounds> | null {
28   let minX = Infinity,
29     minY = Infinity,
30     maxX = -Infinity,
31     maxY = -Infinity;
32   let x = 0,
33     y = 0,
34     startX = 0,
35     startY = 0;
36   const include = (a: number, b: number) => {
37     minX = Math.min(minX, a);
38     maxX = Math.max(maxX, a);
39     minY = Math.min(minY, b);
40     maxY = Math.max(maxY, b);
41   };
42   const lerp = (a: number, b: number, t: number) => a * (1 - t) + b * t;
43   const quadratic = (a: number, b: number, c: number, t: number) =>
44     lerp(lerp(a, b, t), lerp(b, c, t), t);
45   const cubic = (a: number, b: number, c: number, d: number, t: number) =>
46     lerp(quadratic(a, b, c, t), quadratic(b, c, d, t), t);
47   for (const segment of path) {
48     if (segment.type === "move") {
49       x = startX = segment.x;
50       y = startY = segment.y;
51       continue;
52     }
53     if (segment.type === "close") {
54       if (x !== startX || y !== startY) {
55         include(x, y);
56         include(startX, startY);
57       }
58       x = startX;
59       y = startY;
60       continue;
61     }
62     if (segment.type === "arc")
63       throw new Error("Font outlines cannot contain analytic arc commands.");
64     include(x, y);
65     include(segment.x, segment.y);
66     if (segment.type === "quadratic") {
67       const root = (a: number, b: number, c: number) => {
68         const scale = Math.max(Math.abs(a), Math.abs(b), Math.abs(c), 1);
69         a /= scale;
70         b /= scale;
71         c /= scale;
72         return (a - b) / (a - 2 * b + c);
73       };
74       for (let axis = 0; axis < 2; axis++) {
75         const t = axis === 0 ? root(x, segment.cpx, segment.x) : root(y, segment.cpy, segment.y);
76         if (t > 0 && t < 1)
77           include(
78             quadratic(x, segment.cpx, segment.x, t),
79             quadratic(y, segment.cpy, segment.y, t),
80           );
81       }
82     } else if (segment.type === "cubic") {
83       for (let axis = 0; axis < 2; axis++) {
84         let p0 = axis === 0 ? x : y;
85         let p1 = axis === 0 ? segment.cp1x : segment.cp1y;
86         let p2 = axis === 0 ? segment.cp2x : segment.cp2y;
87         let p3 = axis === 0 ? segment.x : segment.y;
88         // Normalize first to keep coefficient arithmetic finite for large coordinates.
89         const scale = Math.max(
90       Math.abs(p0),
91       Math.abs(p1),
92       Math.abs(p2),
93       Math.abs(p3),
94       1,
95     );
96         p0 /= scale;
97         p1 /= scale;
98         p2 /= scale;
99         p3 /= scale;
100         const a = -p0 + 3 * p1 - 3 * p2 + p3;
101         const b = 2 * (p0 - 2 * p1 + p2),
102       c = p1 - p0;
103         let firstRoot: number, secondRoot = NaN;
104         if (a === 0) {
105           if (b === 0) continue;
106           firstRoot = -c / b;
107         } else {
108           const discriminant = b * b - 4 * a * c;
109           if (discriminant < 0) continue;
110           const q = -0.5 * (b + (b < 0 ? -1 : 1) * Math.sqrt(discriminant));
111           firstRoot = q === 0 ? -b / (2 * a) : q / a;
112           if (q !== 0) secondRoot = c / q;
113         }
114         for (let rootIndex = 0; rootIndex < 2; rootIndex++) {
115           const t = rootIndex === 0 ? firstRoot : secondRoot;
116           if (t > 0 && t < 1)
117           include(
118             cubic(x, segment.cp1x, segment.cp2x, segment.x, t),
119             cubic(y, segment.cp1y, segment.cp2y, segment.y, t),
120           );
121         }
122       }
123     }
124     x = segment.x;
125     y = segment.y;
126   }
127   if (minX === Infinity) return null;
128   const result = { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
129   if (!Object.values(result).every(Number.isFinite))
130     throw new RangeError("Outline bounds overflow.");
131   return Object.freeze(result);
132 }
133 

Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.