msdfgen-ts 0.1.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.
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Pure scalar math utilities — no allocations, no Vec2.
3
+ *
4
+ * These are the arithmetic helpers used throughout the hot paths in msdf/.
5
+ * Keep all functions pure (no side effects, no allocation).
6
+ *
7
+ * Ported from core/arithmetics.hpp and core/equation-solver.cpp.
8
+ * msdfgen © Viktor Chlumský — MIT licence.
9
+ */
10
+ /**
11
+ * 2-D dot product.
12
+ * @param ax First vector x.
13
+ * @param ay First vector y.
14
+ * @param bx Second vector x.
15
+ * @param by Second vector y.
16
+ * @returns Dot product a·b.
17
+ */
18
+ export declare function dot(ax: number, ay: number, bx: number, by: number): number;
19
+ /**
20
+ * 2-D cross product (scalar z-component of the 3-D cross product).
21
+ * @param ax First vector x.
22
+ * @param ay First vector y.
23
+ * @param bx Second vector x.
24
+ * @param by Second vector y.
25
+ * @returns Cross product a×b.
26
+ */
27
+ export declare function cross(ax: number, ay: number, bx: number, by: number): number;
28
+ /**
29
+ * Linear interpolation.
30
+ * port of core/arithmetics.hpp: mix
31
+ * @param a Start value.
32
+ * @param b End value.
33
+ * @param t Weight (0 = a, 1 = b).
34
+ * @returns (1-t)*a + t*b.
35
+ */
36
+ export declare function mix(a: number, b: number, t: number): number;
37
+ /**
38
+ * Returns the sign of n: 1 for positive, -1 for negative, 0 for zero.
39
+ * port of core/arithmetics.hpp: sign
40
+ * @param n Input value.
41
+ * @returns Sign of n.
42
+ */
43
+ export declare function sign(n: number): number;
44
+ /**
45
+ * Returns 1 for non-negative values and -1 for negative values.
46
+ * port of core/arithmetics.hpp: nonZeroSign
47
+ * @param n Input value.
48
+ * @returns 1 if n >= 0, -1 if n < 0.
49
+ */
50
+ export declare function nonZeroSign(n: number): number;
51
+ /**
52
+ * Clamps n to [a, b].
53
+ * port of core/arithmetics.hpp: clamp (3-argument form)
54
+ * @param n Value to clamp.
55
+ * @param a Lower bound.
56
+ * @param b Upper bound.
57
+ * @returns Clamped value.
58
+ */
59
+ export declare function clamp(n: number, a: number, b: number): number;
60
+ /**
61
+ * Returns the median of three values (the middle value).
62
+ * port of core/arithmetics.hpp: median
63
+ * @param a First value.
64
+ * @param b Second value.
65
+ * @param c Third value.
66
+ * @returns The median of a, b, c.
67
+ */
68
+ export declare function median3(a: number, b: number, c: number): number;
69
+ /**
70
+ * Returns the length of a 2-D vector.
71
+ * @param x Vector x component.
72
+ * @param y Vector y component.
73
+ * @returns Vector length.
74
+ */
75
+ export declare function length2(x: number, y: number): number;
76
+ /**
77
+ * Solves ax² + bx + c = 0, writes up to 2 real roots into x[0], x[1].
78
+ * Returns the number of real roots (0, 1, or 2).
79
+ * Returns -1 when the equation is identically 0 (infinite solutions).
80
+ * port of core/equation-solver.cpp: solveQuadratic
81
+ *
82
+ * @param x Output array (length >= 2).
83
+ * @param a Coefficient of x².
84
+ * @param b Coefficient of x.
85
+ * @param c Constant term.
86
+ * @returns Number of real solutions.
87
+ */
88
+ export declare function solveQuadratic(x: number[], a: number, b: number, c: number): number;
89
+ /**
90
+ * Solves ax³ + bx² + cx + d = 0, writes up to 3 real roots into x[0..2].
91
+ * Returns the number of real roots.
92
+ * port of core/equation-solver.cpp: solveCubic
93
+ *
94
+ * @param x Output array (length >= 3).
95
+ * @param a Coefficient of x³.
96
+ * @param b Coefficient of x².
97
+ * @param c Coefficient of x.
98
+ * @param d Constant term.
99
+ * @returns Number of real solutions.
100
+ */
101
+ export declare function solveCubic(x: number[], a: number, b: number, c: number, d: number): number;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Edge coloring for multi-channel SDF.
3
+ *
4
+ * Assigns a color bitmask to each edge segment so that adjacent segments share
5
+ * at most one channel. This is the prerequisite for MSDF generation.
6
+ *
7
+ * Only `edgeColoringSimple` is ported (the algorithm used by the CLI by
8
+ * default). The ink-trap and distance-based variants are out of scope.
9
+ *
10
+ * port of core/edge-coloring.cpp: edgeColoringSimple (and helpers)
11
+ *
12
+ * msdfgen © Viktor Chlumský — MIT licence.
13
+ */
14
+ import { type Shape } from "../shape/shape.js";
15
+ import { LINEAR, QUADRATIC } from "../shape/segments.js";
16
+ /** port of core/EdgeColor.h: EdgeColor enum */
17
+ export declare const BLACK: 0;
18
+ export declare const RED: 1;
19
+ export declare const GREEN: 2;
20
+ export declare const YELLOW: 3;
21
+ export declare const BLUE: 4;
22
+ export declare const MAGENTA: 5;
23
+ export declare const CYAN: 6;
24
+ export declare const WHITE: 7;
25
+ /**
26
+ * Assigns edge colors to all segments of the shape using the "simple" strategy.
27
+ *
28
+ * @param shape Em-normalized, normalized shape (mutated in place).
29
+ * @param angleThreshold Maximum angle (radians) to be considered a corner
30
+ * (e.g. 3 ≈ 172°). Values below π/2 are treated as the external angle.
31
+ * @param seed PRNG seed for color selection (default 0).
32
+ *
33
+ * port of core/edge-coloring.cpp: edgeColoringSimple
34
+ */
35
+ export declare function edgeColoringSimple(shape: Shape, angleThreshold: number, seed?: bigint): void;
36
+ export { LINEAR, QUADRATIC };
@@ -0,0 +1,61 @@
1
+ /**
2
+ * MSDF post-processing: scanline sign correction and error correction.
3
+ *
4
+ * Ports the two post-processing steps that the msdfgen CLI applies when
5
+ * `-scanline` is used:
6
+ *
7
+ * 1. `distanceSignCorrection` (multiDistanceSignCorrection in rasterization.cpp):
8
+ * Flips all three channels of texels whose median is on the wrong side of 0.5
9
+ * relative to the scanline fill test.
10
+ *
11
+ * 2. `msdfErrorCorrection` / `MSDFErrorCorrection` (MSDFErrorCorrection.cpp):
12
+ * Detects interpolation artifacts and sets their channels to the median.
13
+ *
14
+ * With `-scanline`, the CLI uses:
15
+ * - errorCorrection.mode = EDGE_PRIORITY
16
+ * - errorCorrection.distanceCheckMode = DO_NOT_CHECK_DISTANCE
17
+ * so the sequence is: protectCorners → protectEdges → findErrors → apply.
18
+ *
19
+ * msdfgen © Viktor Chlumský — MIT licence.
20
+ */
21
+ import { type Shape } from "../shape/shape.js";
22
+ /**
23
+ * Corrects the signs of MSDF channels via a scanline fill test.
24
+ *
25
+ * Texels whose median is on the wrong side of 0.5 relative to the scanline fill
26
+ * have all three channels flipped: `ch = 1 - ch`.
27
+ * Ambiguous texels (median exactly 0.5) are resolved by looking at neighbours.
28
+ *
29
+ * port of core/rasterization.cpp: multiDistanceSignCorrection (template N=3)
30
+ *
31
+ * @param msdf Float32Array of length width*height*3, mutated in place.
32
+ * @param shape Normalized shape (y-up).
33
+ * @param width Bitmap width.
34
+ * @param height Bitmap height.
35
+ * @param scale Projection scale.
36
+ * @param tx Projection translate x.
37
+ * @param ty Projection translate y.
38
+ */
39
+ export declare function distanceSignCorrection(msdf: Float32Array, shape: Shape, width: number, height: number, scale: number, tx: number, ty: number): void;
40
+ /**
41
+ * Marks corners as protected, detects MSDF artifacts and fixes them.
42
+ *
43
+ * Exactly matches the `-scanline` pipeline (mode=EDGE_PRIORITY,
44
+ * distanceCheckMode=DO_NOT_CHECK_DISTANCE):
45
+ * 1. protectCorners(shape)
46
+ * 2. protectEdges(msdf)
47
+ * 3. findErrors(msdf) [base classifier only — no shape distance check]
48
+ * 4. apply(msdf) [errored texels → median]
49
+ *
50
+ * port of core/MSDFErrorCorrection.cpp: MSDFErrorCorrection + msdf-error-correction.cpp
51
+ *
52
+ * @param msdf Float32Array of length width*height*3, mutated in place.
53
+ * @param shape Normalized, coloured shape.
54
+ * @param width Bitmap width.
55
+ * @param height Bitmap height.
56
+ * @param scale Projection scale.
57
+ * @param tx Projection translate x.
58
+ * @param ty Projection translate y.
59
+ * @param pxrange Distance range in pixels.
60
+ */
61
+ export declare function msdfErrorCorrection(msdf: Float32Array, shape: Shape, width: number, height: number, scale: number, tx: number, ty: number, pxrange: number): void;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Multi-channel SDF (MSDF) generation.
3
+ *
4
+ * Ports OverlappingContourCombiner<MultiDistanceSelector> from:
5
+ * core/edge-selectors.cpp — MultiDistanceSelector, PerpendicularDistanceSelectorBase
6
+ * core/contour-combiners.cpp — OverlappingContourCombiner
7
+ * core/msdfgen.cpp — generateDistanceField, DistancePixelConversion<MultiDistance>
8
+ *
9
+ * Output is a flat Float32Array, 3 channels per pixel, y-up (row 0 = bottom).
10
+ * The caller must apply distanceSignCorrection and msdfErrorCorrection afterwards
11
+ * (see pipeline in msdf-error-correction.cpp when -scanline is used).
12
+ *
13
+ * Performance rules (CLAUDE.md): hot loop uses only local number variables,
14
+ * no new/closures/object literals. Module-scope scratch arrays for direction/
15
+ * point queries.
16
+ *
17
+ * msdfgen © Viktor Chlumský — MIT licence.
18
+ */
19
+ import { type Shape } from "../shape/shape.js";
20
+ /**
21
+ * Generates a 3-channel MSDF into `out`.
22
+ *
23
+ * The shape must have edge colors assigned (call `edgeColoringSimple` first)
24
+ * and be em-normalized + normalized.
25
+ *
26
+ * After calling this function, apply `distanceSignCorrection` then
27
+ * `msdfErrorCorrection` to match the reference binary's `-scanline` output.
28
+ *
29
+ * @param shape Coloured, normalized shape (y-up font coordinates).
30
+ * @param width Output width in texels.
31
+ * @param height Output height in texels.
32
+ * @param scale Uniform projection scale (msdfgen `-scale`).
33
+ * @param tx Projection translation x (msdfgen `-translate` x).
34
+ * @param ty Projection translation y (msdfgen `-translate` y).
35
+ * @param pxrange Distance range in pixels (msdfgen `-pxrange`).
36
+ * @param out Float32Array of length `width * height * 3`.
37
+ * Layout: (y * width + x) * 3 + ch, y-up (row 0 = bottom).
38
+ *
39
+ * port of core/msdfgen.cpp: generateMSDF via
40
+ * generateDistanceField<OverlappingContourCombiner<MultiDistanceSelector>>
41
+ */
42
+ export declare function generateMSDF(shape: Shape, width: number, height: number, scale: number, tx: number, ty: number, pxrange: number, out: Float32Array): void;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Single-channel (true) signed distance field generation.
3
+ *
4
+ * Ported from core/msdfgen.cpp: generateSDF / generateDistanceField
5
+ * (OverlappingContourCombiner<TrueDistanceSelector>) plus the CLI's scanline
6
+ * sign correction (core/rasterization.cpp: distanceSignCorrection).
7
+ *
8
+ * The binary is built without Skia (NO_PREPROCESS), so overlapSupport=true and
9
+ * the OverlappingContourCombiner is always used. It tracks per-contour nearest
10
+ * distances and applies winding-aware combination to handle overlapping contours
11
+ * correctly.
12
+ *
13
+ * The shape must already be em-normalized and normalized (see
14
+ * shape/normalize.ts). Output is a flat Float32Array of length width*height,
15
+ * stored y-up (row 0 = bottom) to match msdfgen's FL32 files.
16
+ *
17
+ * msdfgen © Viktor Chlumský — MIT licence.
18
+ */
19
+ import { type Shape } from "../shape/shape.js";
20
+ import { type FillRule } from "../shape/scanline.js";
21
+ /**
22
+ * Parameters for {@link generateSDF}. Mirror msdfgen's projection + range:
23
+ * a pixel `(x,y)` maps to shape space via
24
+ * `((x+0.5)/scale - tx, (y+0.5)/scale - ty)`, and a signed distance `d`
25
+ * (shape units) maps to the stored value `d/rangeWidth + 0.5`, where
26
+ * `rangeWidth = pxrange/scale`.
27
+ */
28
+ export interface SdfParams {
29
+ /** Output width in texels. */
30
+ width: number;
31
+ /** Output height in texels. */
32
+ height: number;
33
+ /** Uniform projection scale (msdfgen `-scale`). */
34
+ scale: number;
35
+ /** Projection translation x (msdfgen `-translate` x). */
36
+ tx: number;
37
+ /** Projection translation y (msdfgen `-translate` y). */
38
+ ty: number;
39
+ /** Distance range in pixels (msdfgen `-pxrange`). */
40
+ pxrange: number;
41
+ /** Fill rule for the scanline sign-correction pass. Default FILL_NONZERO. */
42
+ fillRule?: FillRule;
43
+ /** Whether to run the scanline sign-correction pass. Default true. */
44
+ scanline?: boolean;
45
+ }
46
+ /**
47
+ * Generates a single-channel true SDF into `out`.
48
+ *
49
+ * @param shape Em-normalized, normalized shape (y-up font coordinates).
50
+ * @param params Projection + range parameters.
51
+ * @param out Destination Float32Array of length width*height (mutated).
52
+ */
53
+ export declare function generateSDF(shape: Shape, params: SdfParams, out: Float32Array): void;
@@ -0,0 +1,7 @@
1
+ import { type EdgeSegment } from "./segments.js";
2
+ /**
3
+ * A closed contour — an ordered sequence of edge segments.
4
+ * The endpoint of segment[i] must equal the start of segment[i+1];
5
+ * the endpoint of the last segment must equal the start of the first.
6
+ */
7
+ export type Contour = EdgeSegment[];
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Shape normalization and em-normalization.
3
+ *
4
+ * This module owns the *single named boundary* at which font units are divided
5
+ * by unitsPerEm (FONT_SCALING_EM_NORMALIZED in msdfgen terms). No other
6
+ * source file may divide by unitsPerEm — import `emNormalizeShape` here.
7
+ *
8
+ * Ported from core/Shape.cpp: Shape::normalize(), and
9
+ * core/convergent-curve-ordering.cpp: convergentCurveOrdering().
10
+ * msdfgen © Viktor Chlumský — MIT licence.
11
+ */
12
+ import { type Shape } from "./shape.js";
13
+ /**
14
+ * Divides all control-point coordinates by `unitsPerEm`, converting font
15
+ * units to the normalised em-square coordinate system used by msdfgen.
16
+ *
17
+ * This is the **single named em-normalisation step** (FONT_SCALING_EM_NORMALIZED).
18
+ * No other file may perform this division by unitsPerEm.
19
+ *
20
+ * @param shape Shape in raw font units (mutated in-place).
21
+ * @param unitsPerEm Design units per em (from Font.metrics.unitsPerEm).
22
+ */
23
+ export declare function emNormalizeShape(shape: Shape, unitsPerEm: number): void;
24
+ /**
25
+ * Normalizes the shape geometry for distance field generation (mutates in-place).
26
+ *
27
+ * Steps (in order):
28
+ * 1. Remove zero-length segments (start == end) — these would produce NaN in
29
+ * distance code and are an artefact of some TrueType outlines.
30
+ * 2. Split single-edge contours into three sub-segments (splitInThirds).
31
+ * 3. Deconverge convergent junctions (near-anti-parallel adjacent edges).
32
+ *
33
+ * Must be called AFTER `emNormalizeShape`.
34
+ * port of core/Shape.cpp: Shape::normalize()
35
+ *
36
+ * @param shape Shape to normalize (mutated in-place).
37
+ */
38
+ export declare function normalizeShape(shape: Shape): void;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Scanline fill testing for distance-field sign correction.
3
+ *
4
+ * Ported from core/Scanline.cpp and core/Shape.cpp: Shape::scanline.
5
+ * msdfgen © Viktor Chlumský — MIT licence.
6
+ */
7
+ import { type Shape } from "./shape.js";
8
+ /** Fill-rule tags — match C++ msdfgen's FillRule enum. */
9
+ export declare const FILL_NONZERO: 0;
10
+ export declare const FILL_ODD: 1;
11
+ export declare const FILL_POSITIVE: 2;
12
+ export declare const FILL_NEGATIVE: 3;
13
+ export type FillRule = typeof FILL_NONZERO | typeof FILL_ODD | typeof FILL_POSITIVE | typeof FILL_NEGATIVE;
14
+ /**
15
+ * Interprets an accumulated winding number under a fill rule.
16
+ * port of core/Scanline.cpp: interpretFillRule
17
+ *
18
+ * @param intersections Accumulated signed crossing count at a point.
19
+ * @param fillRule Fill rule to apply.
20
+ * @returns True when the point is considered filled.
21
+ */
22
+ export declare function interpretFillRule(intersections: number, fillRule: FillRule): boolean;
23
+ /** A single scanline crossing: x-coordinate and running winding direction. */
24
+ interface Intersection {
25
+ x: number;
26
+ direction: number;
27
+ }
28
+ /**
29
+ * A horizontal scanline through a shape, storing sorted edge crossings with
30
+ * prefix-summed winding directions for O(log n) fill queries.
31
+ * port of core/Scanline.cpp: Scanline
32
+ */
33
+ export declare class Scanline {
34
+ private _intersections;
35
+ private _lastIndex;
36
+ /**
37
+ * Replaces the crossings, sorting by x and prefix-summing the directions.
38
+ * port of core/Scanline.cpp: Scanline::setIntersections + preprocess
39
+ *
40
+ * @param intersections Unsorted crossings (ownership transferred; mutated).
41
+ */
42
+ setIntersections(intersections: Intersection[]): void;
43
+ /**
44
+ * Finds the index of the last crossing at or before x (or -1).
45
+ * port of core/Scanline.cpp: Scanline::moveTo
46
+ * @param x Query x-coordinate.
47
+ * @returns Index into the sorted crossings, or -1 when x precedes all.
48
+ */
49
+ private _moveTo;
50
+ /**
51
+ * Returns the accumulated winding number at x.
52
+ * port of core/Scanline.cpp: Scanline::sumIntersections
53
+ * @param x Query x-coordinate.
54
+ * @returns Signed winding number.
55
+ */
56
+ sumIntersections(x: number): number;
57
+ /**
58
+ * Tests whether the point at x is filled under the given fill rule.
59
+ * port of core/Scanline.cpp: Scanline::filled
60
+ * @param x Query x-coordinate.
61
+ * @param fillRule Fill rule to apply.
62
+ * @returns True when filled.
63
+ */
64
+ filled(x: number, fillRule: FillRule): boolean;
65
+ }
66
+ /**
67
+ * Fills `line` with the crossings of `shape` at height `y`.
68
+ * port of core/Shape.cpp: Shape::scanline
69
+ *
70
+ * @param shape Shape to intersect (contours of EdgeSegment).
71
+ * @param y Scanline height (shape space).
72
+ * @param line Scanline to populate (its crossings are replaced).
73
+ */
74
+ export declare function computeShapeScanline(shape: Shape, y: number, line: Scanline): void;
75
+ export {};
@@ -0,0 +1,148 @@
1
+ /**
2
+ * EdgeSegment — the single monomorphic segment class used throughout the library.
3
+ *
4
+ * Performance contract (never violate — hot path):
5
+ * - One class, no subclasses. Dispatch via `switch (seg.type)`.
6
+ * - All fields are plain numbers, initialised in constructor order.
7
+ * - Unused fields are set to 0; CUBIC fields are always allocated but ignored for
8
+ * LINEAR / QUADRATIC segments.
9
+ * - Never add conditional fields; always initialise every field in the same order.
10
+ */
11
+ /** Segment type tags — match C++ msdfgen's EdgeType enum. */
12
+ export declare const LINEAR: 0;
13
+ export declare const QUADRATIC: 1;
14
+ export declare const CUBIC: 2;
15
+ export type SegmentType = typeof LINEAR | typeof QUADRATIC | typeof CUBIC;
16
+ /**
17
+ * Reusable output for {@link EdgeSegment.signedDistance}. Allocated once per
18
+ * generator call (or module scope) and passed in — the hot per-pixel loop must
19
+ * never allocate. Mirrors C++ msdfgen's `SignedDistance` plus the by-reference
20
+ * `param` out-value returned from `EdgeSegment::signedDistance`.
21
+ */
22
+ export interface SignedDistanceResult {
23
+ /** Signed distance (sign from edge winding). */
24
+ distance: number;
25
+ /** Alignment term, used to break ties between edges (`|cosθ|` at endpoints). */
26
+ dot: number;
27
+ /** Nearest-point parameter t (may lie outside [0,1] for endpoint regions). */
28
+ param: number;
29
+ }
30
+ /**
31
+ * Compares two signed distances the way msdfgen's `SignedDistance::operator<`
32
+ * does: smaller absolute distance wins; ties broken by the smaller `dot`.
33
+ * port of core/SignedDistance.hpp: operator<
34
+ *
35
+ * @param aDist Candidate distance.
36
+ * @param aDot Candidate dot.
37
+ * @param bDist Incumbent distance.
38
+ * @param bDot Incumbent dot.
39
+ * @returns True when (aDist,aDot) is closer than (bDist,bDot).
40
+ */
41
+ export declare function signedDistanceLess(aDist: number, aDot: number, bDist: number, bDot: number): boolean;
42
+ /**
43
+ * A single curve segment within a contour.
44
+ *
45
+ * Field layout (matches C++ msdfgen EdgeSegment conventions):
46
+ * LINEAR — p0=start, p1=end, p2=p3=0
47
+ * QUADRATIC — p0=start, p1=control, p2=end, p3=0
48
+ * CUBIC — p0=start, p1=ctrl1, p2=ctrl2, p3=end
49
+ */
50
+ export declare class EdgeSegment {
51
+ /** @type {SegmentType} */
52
+ type: SegmentType;
53
+ /** Start x (always on-curve). */
54
+ p0x: number;
55
+ /** Start y (always on-curve). */
56
+ p0y: number;
57
+ /** LINEAR: end x. QUADRATIC: control x. CUBIC: first control x. */
58
+ p1x: number;
59
+ /** LINEAR: end y. QUADRATIC: control y. CUBIC: first control y. */
60
+ p1y: number;
61
+ /** QUADRATIC/CUBIC: second control or end x. LINEAR: 0. */
62
+ p2x: number;
63
+ /** QUADRATIC/CUBIC: second control or end y. LINEAR: 0. */
64
+ p2y: number;
65
+ /** CUBIC: end x. Others: 0. */
66
+ p3x: number;
67
+ /** CUBIC: end y. Others: 0. */
68
+ p3y: number;
69
+ /**
70
+ * Edge color bitmask for MSDF channel assignment.
71
+ * port of core/EdgeColor.h: EdgeColor (BLACK=0, RED=1, GREEN=2, YELLOW=3, BLUE=4, MAGENTA=5, CYAN=6, WHITE=7)
72
+ */
73
+ color: number;
74
+ /**
75
+ * @param type Segment type (LINEAR=0, QUADRATIC=1, CUBIC=2).
76
+ * @param p0x Start x.
77
+ * @param p0y Start y.
78
+ * @param p1x LINEAR: end x. QUADRATIC: control x. CUBIC: first control x.
79
+ * @param p1y LINEAR: end y. QUADRATIC: control y. CUBIC: first control y.
80
+ * @param p2x QUADRATIC: end x. CUBIC: second control x. LINEAR: 0.
81
+ * @param p2y QUADRATIC: end y. CUBIC: second control y. LINEAR: 0.
82
+ * @param p3x CUBIC: end x. Others: 0.
83
+ * @param p3y CUBIC: end y. Others: 0.
84
+ */
85
+ constructor(type: SegmentType, p0x: number, p0y: number, p1x: number, p1y: number, p2x: number, p2y: number, p3x: number, p3y: number);
86
+ /** Endpoint x (the on-curve destination of this segment). */
87
+ endX(): number;
88
+ /** Endpoint y. */
89
+ endY(): number;
90
+ /**
91
+ * Returns the point on the segment at parameter t ∈ [0,1].
92
+ * port of core/edge-segments.cpp: LinearSegment::point, QuadraticSegment::point, CubicSegment::point
93
+ *
94
+ * @param param Parameter t.
95
+ * @param out Two-element array [x, y] to write into (reuse to avoid allocation).
96
+ */
97
+ point(param: number, out: number[]): void;
98
+ /**
99
+ * Returns the (unnormalized) direction tangent at parameter t.
100
+ * port of core/edge-segments.cpp: LinearSegment::direction, QuadraticSegment::direction, CubicSegment::direction
101
+ *
102
+ * @param param Parameter t.
103
+ * @param out Two-element array [dx, dy] to write into.
104
+ */
105
+ direction(param: number, out: number[]): void;
106
+ /**
107
+ * Splits the segment into thirds. Each of the 3 returned segments covers
108
+ * [0,1/3], [1/3,2/3], [2/3,1] of the original parameter range.
109
+ * port of core/edge-segments.cpp: LinearSegment::splitInThirds, QuadraticSegment::splitInThirds, CubicSegment::splitInThirds
110
+ *
111
+ * @returns Tuple [part0, part1, part2].
112
+ */
113
+ splitInThirds(): [EdgeSegment, EdgeSegment, EdgeSegment];
114
+ /**
115
+ * Computes the signed distance from `origin` to this segment, writing the
116
+ * result (distance, alignment dot, nearest parameter) into `out`.
117
+ * port of core/edge-segments.cpp:
118
+ * LinearSegment / QuadraticSegment / CubicSegment ::signedDistance
119
+ *
120
+ * The sign follows the edge winding via `nonZeroSign(crossProduct(...))`.
121
+ * Hot path: uses only local number variables and the module-scope root buffer.
122
+ *
123
+ * @param ox Query point x (shape space).
124
+ * @param oy Query point y (shape space).
125
+ * @param out Reusable result object (mutated).
126
+ */
127
+ signedDistance(ox: number, oy: number, out: SignedDistanceResult): void;
128
+ /**
129
+ * Computes the x-coordinates where a horizontal scanline at height `y`
130
+ * crosses this segment, together with the vertical crossing direction.
131
+ * port of core/edge-segments.cpp:
132
+ * LinearSegment / QuadraticSegment / CubicSegment ::scanlineIntersections
133
+ *
134
+ * @param y Scanline height (shape space).
135
+ * @param xOut Output array (length >= 3) — crossing x-coordinates.
136
+ * @param dyOut Output array (length >= 3) — crossing directions (±1).
137
+ * @returns Number of crossings written (0..3).
138
+ */
139
+ scanlineIntersections(y: number, xOut: number[], dyOut: number[]): number;
140
+ /**
141
+ * Converts a QUADRATIC segment to a degree-elevated CUBIC equivalent.
142
+ * port of core/edge-segments.cpp: QuadraticSegment::convertToCubic
143
+ *
144
+ * Only valid when `this.type === QUADRATIC`.
145
+ * @returns A new CUBIC EdgeSegment with identical geometry.
146
+ */
147
+ convertToCubic(): EdgeSegment;
148
+ }
@@ -0,0 +1,12 @@
1
+ import { type Contour } from "./contour.js";
2
+ /**
3
+ * A glyph outline — a collection of closed contours.
4
+ *
5
+ * `inverseYAxis`: when true, the y-axis points down (screen space).
6
+ * TrueType font coordinates are y-up; this flag is flipped at the atlas
7
+ * boundary, in exactly one place, when blitting to a texture.
8
+ */
9
+ export interface Shape {
10
+ contours: Contour[];
11
+ inverseYAxis: boolean;
12
+ }