text-shaper 0.1.1 → 0.1.3

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.
Files changed (77) hide show
  1. package/README.md +1 -0
  2. package/dist/aat/state-machine.d.ts +18 -6
  3. package/dist/buffer/glyph-buffer.d.ts +35 -1
  4. package/dist/buffer/unicode-buffer.d.ts +2 -0
  5. package/dist/fluent/bitmap-builder.d.ts +146 -0
  6. package/dist/fluent/index.d.ts +102 -0
  7. package/dist/fluent/path-builder.d.ts +230 -0
  8. package/dist/fluent/pipe.d.ts +200 -0
  9. package/dist/fluent/types.d.ts +93 -0
  10. package/dist/font/face.d.ts +2 -0
  11. package/dist/font/font.d.ts +14 -0
  12. package/dist/font/tables/cff.d.ts +3 -1
  13. package/dist/font/tables/colr.d.ts +4 -1
  14. package/dist/font/tables/fvar.d.ts +3 -1
  15. package/dist/font/tables/glyf.d.ts +13 -0
  16. package/dist/font/tables/gpos.d.ts +9 -1
  17. package/dist/font/tables/gsub.d.ts +11 -0
  18. package/dist/font/tables/gvar.d.ts +10 -1
  19. package/dist/font/tables/head.d.ts +5 -0
  20. package/dist/font/tables/hhea.d.ts +5 -0
  21. package/dist/font/tables/loca.d.ts +3 -0
  22. package/dist/font/tables/maxp.d.ts +5 -0
  23. package/dist/font/tables/name.d.ts +5 -0
  24. package/dist/font/tables/os2.d.ts +5 -0
  25. package/dist/font/tables/post.d.ts +5 -0
  26. package/dist/index.d.ts +12 -5
  27. package/dist/index.js +12 -10
  28. package/dist/index.js.map +104 -93
  29. package/dist/layout/justify.d.ts +15 -2
  30. package/dist/layout/structures/class-def.d.ts +61 -8
  31. package/dist/layout/structures/coverage.d.ts +57 -10
  32. package/dist/layout/structures/device.d.ts +26 -5
  33. package/dist/layout/structures/layout-common.d.ts +18 -3
  34. package/dist/layout/structures/set-digest.d.ts +59 -0
  35. package/dist/perf/benchmark-runner-eksh2b26.js +0 -0
  36. package/dist/perf/benchmark-runner.html +517 -0
  37. package/dist/perf/comparison-tests.js +22676 -0
  38. package/dist/perf/comparison-tests.js.map +111 -0
  39. package/dist/perf/cpu/cpu-perf.js +21920 -0
  40. package/dist/perf/cpu/cpu-perf.js.map +108 -0
  41. package/dist/perf/gpu/webgl-perf.js +470 -0
  42. package/dist/perf/gpu/webgl-perf.js.map +11 -0
  43. package/dist/perf/gpu/webgpu-perf.js +442 -0
  44. package/dist/perf/gpu/webgpu-perf.js.map +11 -0
  45. package/dist/perf/index.html +258 -0
  46. package/dist/perf/module-loader.js +18 -0
  47. package/dist/perf/utils/perf-utils.js +170 -0
  48. package/dist/perf/utils/perf-utils.js.map +10 -0
  49. package/dist/raster/asymmetric-stroke.d.ts +61 -0
  50. package/dist/raster/bitmap-utils.d.ts +124 -0
  51. package/dist/raster/blur.d.ts +11 -0
  52. package/dist/raster/cascade-blur.d.ts +44 -0
  53. package/dist/raster/cell.d.ts +24 -0
  54. package/dist/raster/fixed-point.d.ts +1 -1
  55. package/dist/raster/gradient.d.ts +15 -0
  56. package/dist/raster/gray-raster.d.ts +7 -1
  57. package/dist/raster/lcd-filter.d.ts +15 -0
  58. package/dist/raster/msdf.d.ts +135 -0
  59. package/dist/raster/outline-decompose.d.ts +16 -17
  60. package/dist/raster/rasterize.d.ts +19 -2
  61. package/dist/raster/sdf.d.ts +3 -0
  62. package/dist/raster/stroker.d.ts +3 -0
  63. package/dist/raster/types.d.ts +24 -0
  64. package/dist/render/outline-transform.d.ts +169 -0
  65. package/dist/render/path.d.ts +56 -1
  66. package/dist/shaper/complex/arabic.d.ts +1 -0
  67. package/dist/shaper/shape-plan.d.ts +11 -8
  68. package/dist/shaper/shaper.d.ts +61 -3
  69. package/dist/unicode/bidi/brackets.d.ts +15 -0
  70. package/dist/unicode/bidi/char-types.d.ts +4 -0
  71. package/dist/unicode/bidi/embedding-levels.d.ts +3 -0
  72. package/dist/unicode/bidi/mirroring.d.ts +10 -0
  73. package/dist/unicode/bidi/reordering.d.ts +15 -0
  74. package/dist/unicode/normalize.d.ts +23 -0
  75. package/dist/unicode/script.d.ts +17 -0
  76. package/dist/unicode/segmentation.d.ts +18 -0
  77. package/package.json +12 -3
@@ -2,6 +2,7 @@ import type { GlyphBuffer } from "../buffer/glyph-buffer.ts";
2
2
  import type { Font } from "../font/font.ts";
3
3
  import type { Contour } from "../font/tables/glyf.ts";
4
4
  import type { GlyphId } from "../types.ts";
5
+ import { type Matrix2D, type Matrix3x3 } from "./outline-transform.ts";
5
6
  /**
6
7
  * Path command types for glyph rendering
7
8
  */
@@ -63,11 +64,12 @@ export interface GlyphPath {
63
64
  */
64
65
  export declare function contourToPath(contour: Contour): PathCommand[];
65
66
  /**
66
- * Get path commands for a glyph
67
+ * Get cached glyph path, computing and caching if not already cached
67
68
  */
68
69
  export declare function getGlyphPath(font: Font, glyphId: GlyphId): GlyphPath | null;
69
70
  /**
70
71
  * Convert path commands to SVG path data string
72
+ * Uses Math.round for ~40% faster string generation
71
73
  */
72
74
  export declare function pathToSVG(path: GlyphPath, options?: {
73
75
  flipY?: boolean;
@@ -125,6 +127,10 @@ export declare function renderShapedText(ctx: CanvasRenderingContext2D, font: Fo
125
127
  strokeWidth?: number;
126
128
  lineCap?: CanvasLineCap;
127
129
  lineJoin?: CanvasLineJoin;
130
+ /** 2D affine matrix to apply to glyph coordinates */
131
+ matrix?: Matrix2D;
132
+ /** 3D perspective matrix to apply to glyph coordinates (takes precedence over matrix) */
133
+ matrix3D?: Matrix3x3;
128
134
  }): void;
129
135
  /**
130
136
  * Generate SVG for shaped text
@@ -136,6 +142,12 @@ export declare function shapedTextToSVG(font: Font, glyphs: ShapedGlyph[], optio
136
142
  strokeWidth?: number;
137
143
  lineCap?: "butt" | "round" | "square";
138
144
  lineJoin?: "miter" | "round" | "bevel";
145
+ /** 2D affine matrix to apply to glyph coordinates */
146
+ matrix?: Matrix2D;
147
+ /** 3D perspective matrix to apply to glyph coordinates (takes precedence over matrix) */
148
+ matrix3D?: Matrix3x3;
149
+ /** If true, use native SVG transform attribute instead of transforming path data (2D only) */
150
+ useNativeTransform?: boolean;
139
151
  }): string;
140
152
  /**
141
153
  * Convert GlyphBuffer output to ShapedGlyph array
@@ -182,3 +194,46 @@ export declare function createPath2D(path: GlyphPath, options?: {
182
194
  offsetX?: number;
183
195
  offsetY?: number;
184
196
  }): Path2D;
197
+ /**
198
+ * Render path to canvas with 2D affine matrix transformation applied to coordinates
199
+ * The matrix transforms each point before drawing
200
+ */
201
+ export declare function pathToCanvasWithMatrix(ctx: CanvasRenderingContext2D | Path2D, path: GlyphPath, matrix: Matrix2D, options?: {
202
+ flipY?: boolean;
203
+ }): void;
204
+ /**
205
+ * Convert path to SVG with 2D affine matrix transformation applied to coordinates
206
+ */
207
+ export declare function pathToSVGWithMatrix(path: GlyphPath, matrix: Matrix2D, options?: {
208
+ flipY?: boolean;
209
+ }): string;
210
+ /**
211
+ * Render path to canvas with 3D perspective matrix transformation
212
+ * Uses homogeneous coordinates for perspective projection
213
+ */
214
+ export declare function pathToCanvasWithMatrix3D(ctx: CanvasRenderingContext2D | Path2D, path: GlyphPath, matrix: Matrix3x3, options?: {
215
+ flipY?: boolean;
216
+ }): void;
217
+ /**
218
+ * Convert path to SVG with 3D perspective matrix transformation
219
+ * Uses homogeneous coordinates for perspective projection
220
+ */
221
+ export declare function pathToSVGWithMatrix3D(path: GlyphPath, matrix: Matrix3x3, options?: {
222
+ flipY?: boolean;
223
+ }): string;
224
+ /**
225
+ * Apply 2D affine matrix to canvas context using native transform
226
+ * Use ctx.save() before and ctx.restore() after to preserve context state
227
+ */
228
+ export declare function applyMatrixToContext(ctx: CanvasRenderingContext2D, matrix: Matrix2D): void;
229
+ /**
230
+ * Convert 2D affine matrix to SVG transform attribute string
231
+ * Returns a string like "matrix(a, b, c, d, tx, ty)"
232
+ */
233
+ export declare function matrixToSVGTransform(matrix: Matrix2D): string;
234
+ /**
235
+ * Direct SVG serialization with transform applied in single pass
236
+ * Avoids creating intermediate PathCommand arrays
237
+ * Uses Math.round for ~40% faster string generation
238
+ */
239
+ export declare function pathToSVGDirect(path: GlyphPath, scale: number, offsetX: number, offsetY: number): string;
@@ -56,5 +56,6 @@ export declare function analyzeJoining(infos: GlyphInfo[]): JoiningAction[];
56
56
  export declare function getFeatureForAction(action: JoiningAction): string | null;
57
57
  /**
58
58
  * Set the feature mask for each glyph based on joining analysis
59
+ * Inlined for performance - avoids intermediate array allocation
59
60
  */
60
61
  export declare function setupArabicMasks(infos: GlyphInfo[]): void;
@@ -7,21 +7,24 @@ export interface ShapeFeature {
7
7
  tag: Tag;
8
8
  enabled: boolean;
9
9
  }
10
+ /** Lookup entry with index */
11
+ export interface LookupEntry<T> {
12
+ index: number;
13
+ lookup: T;
14
+ }
10
15
  /** Collected lookups for shaping */
11
16
  export interface ShapePlan {
12
17
  script: Tag;
13
18
  language: Tag | null;
14
19
  direction: "ltr" | "rtl";
15
20
  /** GSUB lookups to apply, in order */
16
- gsubLookups: Array<{
17
- index: number;
18
- lookup: AnyGsubLookup;
19
- }>;
21
+ gsubLookups: LookupEntry<AnyGsubLookup>[];
20
22
  /** GPOS lookups to apply, in order */
21
- gposLookups: Array<{
22
- index: number;
23
- lookup: AnyGposLookup;
24
- }>;
23
+ gposLookups: LookupEntry<AnyGposLookup>[];
24
+ /** Fast O(1) lookup by index for nested GSUB lookups */
25
+ gsubLookupMap: Map<number, LookupEntry<AnyGsubLookup>>;
26
+ /** Fast O(1) lookup by index for nested GPOS lookups */
27
+ gposLookupMap: Map<number, LookupEntry<AnyGposLookup>>;
25
28
  }
26
29
  /** Get or create a cached shape plan */
27
30
  export declare function getOrCreateShapePlan(font: Font, script: string, language: string | null, direction: "ltr" | "rtl", userFeatures?: ShapeFeature[], axisCoords?: number[] | null): ShapePlan;
@@ -3,16 +3,74 @@ import type { UnicodeBuffer } from "../buffer/unicode-buffer.ts";
3
3
  import { Face } from "../font/face.ts";
4
4
  import type { Font } from "../font/font.ts";
5
5
  import { type ShapeFeature } from "./shape-plan.ts";
6
+ /**
7
+ * Options for controlling text shaping behavior.
8
+ */
6
9
  export interface ShapeOptions {
10
+ /** ISO 15924 script tag (e.g., "arab", "deva", "latn"). Defaults to buffer.script or "latn" */
7
11
  script?: string;
12
+ /** BCP 47 language tag (e.g., "en", "ar-SA", "hi-IN"). Defaults to buffer.language or null */
8
13
  language?: string | null;
14
+ /** Text direction: "ltr" (left-to-right) or "rtl" (right-to-left). Defaults to "ltr" */
9
15
  direction?: "ltr" | "rtl";
16
+ /** Array of OpenType features to apply (e.g., [{tag: "liga", value: 1}, {tag: "kern", value: 0}]) */
10
17
  features?: ShapeFeature[];
11
18
  }
12
- /** Font or Face - accepted by shape function */
19
+ /**
20
+ * Union type accepting either Font or Face instances.
21
+ * Use Face for variable fonts to provide axis coordinates for feature variations.
22
+ * Use Font for static fonts or when default axis values are acceptable.
23
+ */
13
24
  export type FontLike = Font | Face;
14
25
  /**
15
- * Shape text using OpenType features.
16
- * Accepts Font or Face (for variable fonts).
26
+ * Return a GlyphBuffer to the pool for reuse.
27
+ * Call this when done with a buffer from shape() to reduce allocations.
28
+ * The pool has a maximum size, so buffers beyond that limit will be discarded.
29
+ *
30
+ * @param buffer - The GlyphBuffer to return to the pool
31
+ */
32
+ export declare function releaseBuffer(buffer: GlyphBuffer): void;
33
+ /**
34
+ * Shape text using OpenType features and complex script processing.
35
+ *
36
+ * Text shaping is the process of converting Unicode text into positioned glyphs for rendering.
37
+ * This involves:
38
+ * - Mapping characters to glyphs via the font's cmap table
39
+ * - Applying OpenType GSUB substitutions (ligatures, contextual forms, etc.)
40
+ * - Positioning glyphs via GPOS or fallback kerning/mark positioning
41
+ * - Complex script analysis (Arabic joining, Indic reordering, etc.)
42
+ * - Applying OpenType features (liga, kern, calt, etc.)
43
+ *
44
+ * The function returns a pooled GlyphBuffer for efficiency. Call releaseBuffer() when done
45
+ * to return it to the pool for reuse.
46
+ *
47
+ * @param fontLike - Font or Face instance (Face for variable fonts with axis coordinates)
48
+ * @param buffer - UnicodeBuffer containing the input text and metadata (script, language, direction)
49
+ * @param options - Optional shaping parameters
50
+ * @param options.script - ISO 15924 script tag (e.g., "arab", "deva"), defaults to buffer.script or "latn"
51
+ * @param options.language - BCP 47 language tag (e.g., "en", "ar-SA"), defaults to buffer.language or null
52
+ * @param options.direction - Text direction "ltr" or "rtl", defaults to "ltr"
53
+ * @param options.features - Array of OpenType features to enable/disable (e.g., [{tag: "liga", value: 1}])
54
+ * @returns GlyphBuffer containing shaped glyphs with positions and metadata
17
55
  */
18
56
  export declare function shape(fontLike: FontLike, buffer: UnicodeBuffer, options?: ShapeOptions): GlyphBuffer;
57
+ /**
58
+ * Shape text into an existing GlyphBuffer (zero-allocation hot path).
59
+ *
60
+ * This is a performance-optimized version of shape() that reuses an existing GlyphBuffer
61
+ * instead of allocating a new one. Use this for maximum performance when shaping repeatedly,
62
+ * such as in animation loops or batch text processing.
63
+ *
64
+ * The provided GlyphBuffer will be reset and filled with the shaped output. This avoids
65
+ * allocations from the buffer pool and gives you full control over buffer lifecycle.
66
+ *
67
+ * @param fontLike - Font or Face instance (Face for variable fonts with axis coordinates)
68
+ * @param buffer - UnicodeBuffer containing the input text and metadata (script, language, direction)
69
+ * @param glyphBuffer - Existing GlyphBuffer to fill with shaped output (will be reset)
70
+ * @param options - Optional shaping parameters
71
+ * @param options.script - ISO 15924 script tag (e.g., "arab", "deva"), defaults to buffer.script or "latn"
72
+ * @param options.language - BCP 47 language tag (e.g., "en", "ar-SA"), defaults to buffer.language or null
73
+ * @param options.direction - Text direction "ltr" or "rtl", defaults to "ltr"
74
+ * @param options.features - Array of OpenType features to enable/disable (e.g., [{tag: "liga", value: 1}])
75
+ */
76
+ export declare function shapeInto(fontLike: FontLike, buffer: UnicodeBuffer, glyphBuffer: GlyphBuffer, options?: ShapeOptions): void;
@@ -2,6 +2,21 @@
2
2
  * Bidi bracket pair functions
3
3
  * Port of bidi-js brackets.js
4
4
  */
5
+ /**
6
+ * Get the closing bracket character for an opening bracket
7
+ * @param char Opening bracket character
8
+ * @returns Corresponding closing bracket character, or null if not an opening bracket
9
+ */
5
10
  export declare function openingToClosingBracket(char: string): string | null;
11
+ /**
12
+ * Get the opening bracket character for a closing bracket
13
+ * @param char Closing bracket character
14
+ * @returns Corresponding opening bracket character, or null if not a closing bracket
15
+ */
6
16
  export declare function closingToOpeningBracket(char: string): string | null;
17
+ /**
18
+ * Get the canonical bracket character for a bracket
19
+ * @param char Bracket character
20
+ * @returns Canonical form of the bracket character, or null if not applicable
21
+ */
7
22
  export declare function getCanonicalBracket(char: string): string | null;
@@ -11,9 +11,13 @@ export declare const BN_LIKE_TYPES: number;
11
11
  export declare const TRAILING_TYPES: number;
12
12
  /**
13
13
  * Get the bidi character type for a character
14
+ * @param char Character to check
15
+ * @returns Bidi character type as a bitmask
14
16
  */
15
17
  export declare function getBidiCharType(char: string): number;
16
18
  /**
17
19
  * Get the name of a bidi character type
20
+ * @param char Character to check
21
+ * @returns Bidi character type name (e.g., "L", "R", "EN", "AN", etc.)
18
22
  */
19
23
  export declare function getBidiCharTypeName(char: string): string;
@@ -14,5 +14,8 @@ export interface EmbeddingLevelsResult {
14
14
  * This function applies the Bidirectional Algorithm to a string, returning the resolved embedding levels
15
15
  * in a single Uint8Array plus a list of objects holding each paragraph's start and end indices and resolved
16
16
  * base embedding level.
17
+ * @param string Text string to process
18
+ * @param baseDirection Base text direction: "ltr", "rtl", or "auto"
19
+ * @returns Object containing embedding levels array and paragraph information
17
20
  */
18
21
  export declare function getEmbeddingLevels(string: string, baseDirection?: "ltr" | "rtl" | "auto"): EmbeddingLevelsResult;
@@ -2,9 +2,19 @@
2
2
  * Bidi character mirroring
3
3
  * Port of bidi-js mirroring.js
4
4
  */
5
+ /**
6
+ * Get the mirrored version of a character for BiDi display
7
+ * @param char Character to mirror
8
+ * @returns Mirrored character, or null if character has no mirror
9
+ */
5
10
  export declare function getMirroredCharacter(char: string): string | null;
6
11
  /**
7
12
  * Given a string and its resolved embedding levels, build a map of indices to replacement chars
8
13
  * for any characters in right-to-left segments that have defined mirrored characters.
14
+ * @param string Text string to process
15
+ * @param embeddingLevels Resolved embedding levels from getEmbeddingLevels
16
+ * @param start Start index (defaults to 0)
17
+ * @param end End index (defaults to string length - 1)
18
+ * @returns Map of character indices to their mirrored replacements
9
19
  */
10
20
  export declare function getMirroredCharactersMap(string: string, embeddingLevels: Uint8Array, start?: number, end?: number): Map<number, string>;
@@ -6,13 +6,28 @@ import type { EmbeddingLevelsResult } from "./embedding-levels.ts";
6
6
  /**
7
7
  * Given a start and end denoting a single line within a string, and a set of precalculated
8
8
  * bidi embedding levels, produce a list of segments whose ordering should be flipped, in sequence.
9
+ * @param string Text string to process
10
+ * @param embeddingLevelsResult Result from getEmbeddingLevels
11
+ * @param start Start index (defaults to 0)
12
+ * @param end End index (defaults to string length - 1)
13
+ * @returns Array of segment ranges [start, end] to be reversed
9
14
  */
10
15
  export declare function getReorderSegments(string: string, embeddingLevelsResult: EmbeddingLevelsResult, start?: number, end?: number): Array<[number, number]>;
11
16
  /**
12
17
  * Get the reordered string with bidi segments reversed
18
+ * @param string Text string to reorder
19
+ * @param embedLevelsResult Result from getEmbeddingLevels
20
+ * @param start Start index (defaults to 0)
21
+ * @param end End index (defaults to string length - 1)
22
+ * @returns Reordered string with BiDi applied
13
23
  */
14
24
  export declare function getReorderedString(string: string, embedLevelsResult: EmbeddingLevelsResult, start?: number, end?: number): string;
15
25
  /**
16
26
  * Get an array with character indices in their new bidi order
27
+ * @param string Text string to process
28
+ * @param embedLevelsResult Result from getEmbeddingLevels
29
+ * @param start Start index (defaults to 0)
30
+ * @param end End index (defaults to string length - 1)
31
+ * @returns Array mapping new positions to original character indices
17
32
  */
18
33
  export declare function getReorderedIndices(string: string, embedLevelsResult: EmbeddingLevelsResult, start?: number, end?: number): number[];
@@ -15,22 +15,45 @@ export declare enum NormalizationMode {
15
15
  /**
16
16
  * Canonical Combining Class (ccc) for combining marks
17
17
  * Based on Unicode 15.0
18
+ *
19
+ * Optimized with early-exit for common non-combining ranges:
20
+ * - Basic Latin (0000-007F)
21
+ * - Latin-1 Supplement (0080-00FF)
22
+ * - Latin Extended-A/B (0100-024F)
23
+ * - IPA Extensions (0250-02AF)
24
+ * - Spacing Modifier Letters (02B0-02FF) - most are spacing
25
+ * - Greek and Coptic (0370-03FF) - base chars only
26
+ * - Cyrillic (0400-04FF)
27
+ * - Cyrillic Supplement (0500-052F)
28
+ * - CJK ranges (3000+)
29
+ * - Hangul (AC00-D7AF)
30
+ * @param cp Unicode codepoint to check
31
+ * @returns Canonical combining class (0 for non-combining characters, 1-255 for combining marks)
18
32
  */
19
33
  export declare function getCombiningClass(cp: number): number;
20
34
  /**
21
35
  * Reorder combining marks according to canonical combining class
36
+ * @param infos Array of glyph information objects (modified in place)
22
37
  */
23
38
  export declare function reorderMarks(infos: GlyphInfo[]): void;
24
39
  /**
25
40
  * Decompose a codepoint if it has a canonical decomposition
41
+ * @param cp Unicode codepoint to decompose
42
+ * @returns Array of decomposed codepoints, or null if character has no decomposition
26
43
  */
27
44
  export declare function decompose(cp: number): number[] | null;
28
45
  /**
29
46
  * Try to compose a base character with a combining mark
30
47
  * Returns the composed character or null if no composition exists
48
+ * @param base Base character codepoint
49
+ * @param combining Combining mark codepoint
50
+ * @returns Composed character codepoint, or null if no composition exists
31
51
  */
32
52
  export declare function tryCompose(base: number, combining: number): number | null;
33
53
  /**
34
54
  * Apply normalization to glyph infos
55
+ * @param infos Array of glyph information objects
56
+ * @param mode Normalization mode (None, Decompose, Compose, or Auto)
57
+ * @returns Normalized array of glyph information objects
35
58
  */
36
59
  export declare function normalize(infos: GlyphInfo[], mode: NormalizationMode): GlyphInfo[];
@@ -173,19 +173,28 @@ export declare enum Script {
173
173
  }
174
174
  /**
175
175
  * Get script for a codepoint using binary search
176
+ * @param cp Unicode codepoint to check
177
+ * @returns The script for the given codepoint
176
178
  */
177
179
  export declare function getScript(cp: number): Script;
178
180
  /**
179
181
  * Get script for a string (returns the dominant non-Common/Inherited script)
182
+ * @param text Text string to analyze
183
+ * @returns The dominant script found in the text (most frequent non-Common/Inherited script)
180
184
  */
181
185
  export declare function detectScript(text: string): Script;
182
186
  /**
183
187
  * Get all scripts present in text
188
+ * @param text Text string to analyze
189
+ * @returns Array of all scripts found in the text
184
190
  */
185
191
  export declare function getScripts(text: string): Script[];
186
192
  /**
187
193
  * Check if text contains only characters from a specific script
188
194
  * (Common and Inherited are allowed)
195
+ * @param text Text string to check
196
+ * @param script Script to check against
197
+ * @returns True if text contains only characters from the specified script (plus Common/Inherited)
189
198
  */
190
199
  export declare function isScript(text: string, script: Script): boolean;
191
200
  /**
@@ -199,17 +208,25 @@ export interface ScriptRun {
199
208
  }
200
209
  /**
201
210
  * Split text into script runs
211
+ * @param text Text string to split
212
+ * @returns Array of script runs, where each run is a contiguous sequence of characters with the same script
202
213
  */
203
214
  export declare function getScriptRuns(text: string): ScriptRun[];
204
215
  /**
205
216
  * Get OpenType script tag for a Unicode script
217
+ * @param script Unicode script to convert
218
+ * @returns OpenType script tag (4-character string like "latn", "arab", etc.)
206
219
  */
207
220
  export declare function getScriptTag(script: Script): string;
208
221
  /**
209
222
  * Check if a script requires complex shaping
223
+ * @param script Script to check
224
+ * @returns True if the script requires complex shaping (e.g., Arabic, Devanagari, Thai)
210
225
  */
211
226
  export declare function isComplexScript(script: Script): boolean;
212
227
  /**
213
228
  * Get script direction (LTR or RTL)
229
+ * @param script Script to check
230
+ * @returns Direction of the script: "ltr" for left-to-right or "rtl" for right-to-left
214
231
  */
215
232
  export declare function getScriptDirection(script: Script): "ltr" | "rtl";
@@ -50,10 +50,14 @@ export declare enum WordBreakProperty {
50
50
  }
51
51
  /**
52
52
  * Get grapheme break property for codepoint
53
+ * @param cp Unicode codepoint to check
54
+ * @returns Grapheme break property for the codepoint
53
55
  */
54
56
  export declare function getGraphemeBreakProperty(cp: number): GraphemeBreakProperty;
55
57
  /**
56
58
  * Get word break property for codepoint
59
+ * @param cp Unicode codepoint to check
60
+ * @returns Word break property for the codepoint
57
61
  */
58
62
  export declare function getWordBreakProperty(cp: number): WordBreakProperty;
59
63
  /**
@@ -67,6 +71,8 @@ export interface GraphemeBoundaries {
67
71
  }
68
72
  /**
69
73
  * Find grapheme cluster boundaries in codepoints
74
+ * @param codepoints Array of Unicode codepoints
75
+ * @returns Object containing boundary positions and grapheme break properties
70
76
  */
71
77
  export declare function findGraphemeBoundaries(codepoints: number[]): GraphemeBoundaries;
72
78
  /**
@@ -80,25 +86,37 @@ export interface WordBoundaries {
80
86
  }
81
87
  /**
82
88
  * Find word boundaries in codepoints
89
+ * @param codepoints Array of Unicode codepoints
90
+ * @returns Object containing boundary positions and word break properties
83
91
  */
84
92
  export declare function findWordBoundaries(codepoints: number[]): WordBoundaries;
85
93
  /**
86
94
  * Split text into grapheme clusters
95
+ * @param text Text string to split
96
+ * @returns Array of grapheme cluster strings
87
97
  */
88
98
  export declare function splitGraphemes(text: string): string[];
89
99
  /**
90
100
  * Split text into words
101
+ * @param text Text string to split
102
+ * @returns Array of word strings (whitespace-only segments are filtered out)
91
103
  */
92
104
  export declare function splitWords(text: string): string[];
93
105
  /**
94
106
  * Count grapheme clusters in text
107
+ * @param text Text string to analyze
108
+ * @returns Number of grapheme clusters in the text
95
109
  */
96
110
  export declare function countGraphemes(text: string): number;
97
111
  /**
98
112
  * Analyze grapheme boundaries for glyph infos
113
+ * @param infos Array of glyph information objects
114
+ * @returns Object containing boundary positions and grapheme break properties
99
115
  */
100
116
  export declare function analyzeGraphemesForGlyphs(infos: GlyphInfo[]): GraphemeBoundaries;
101
117
  /**
102
118
  * Analyze word boundaries for glyph infos
119
+ * @param infos Array of glyph information objects
120
+ * @returns Object containing boundary positions and word break properties
103
121
  */
104
122
  export declare function analyzeWordsForGlyphs(infos: GlyphInfo[]): WordBoundaries;
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "text-shaper",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "devDependencies": {
7
7
  "@biomejs/biome": "2.3.8",
8
8
  "@types/bun": "latest",
9
+ "@types/opentype.js": "^1.3.8",
10
+ "freetype2": "^2.1.0",
11
+ "harfbuzzjs": "^0.4.13",
12
+ "opentype.js": "^1.3.4",
9
13
  "vitepress": "^2.0.0-alpha.15",
10
14
  "vitepress-plugin-llms": "^1.9.3"
11
15
  },
@@ -43,6 +47,8 @@
43
47
  ],
44
48
  "license": "MIT",
45
49
  "scripts": {
50
+ "postinstall": "node scripts/patch-pkg-prebuilds.js",
51
+ "bench": "bun test ./bench/ --timeout 120000",
46
52
  "build": "bun build ./src/index.ts --outdir ./dist --target browser --minify --sourcemap=linked",
47
53
  "build:prod": "bun build ./src/index.ts --outdir ./dist --target browser --production --sourcemap=external",
48
54
  "build:analyze": "bun build ./src/index.ts --outdir ./dist --target browser --minify --metafile=./dist/meta.json",
@@ -56,5 +62,8 @@
56
62
  "docs:preview": "vitepress preview docs"
57
63
  },
58
64
  "type": "module",
59
- "types": "./dist/index.d.ts"
60
- }
65
+ "types": "./dist/index.d.ts",
66
+ "trustedDependencies": [
67
+ "freetype2"
68
+ ]
69
+ }