@unmade/text-renderer 1.1.0 → 1.1.1

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 (67) hide show
  1. package/dist/__vite-browser-external-CijFC1_R.js +7 -0
  2. package/dist/baselines/index.d.ts +40 -0
  3. package/dist/ce-adapter/index.cjs +1 -1
  4. package/dist/ce-adapter/index.js +154 -152
  5. package/dist/chunk-3b4jIN3o.js +13 -0
  6. package/dist/constants.d.ts +5 -0
  7. package/dist/glyph/svg-glyph-converter.d.ts +42 -0
  8. package/dist/helpers/bbox.d.ts +33 -0
  9. package/dist/helpers/matrix.d.ts +8 -0
  10. package/dist/helpers/point.d.ts +17 -0
  11. package/dist/helpers/xml.d.ts +1 -0
  12. package/dist/index.d.ts +16 -249
  13. package/dist/index.js +1543 -2004
  14. package/dist/loading/createLoadedFont.d.ts +53 -0
  15. package/dist/loading/index.d.ts +4 -0
  16. package/dist/measuring/conversion.d.ts +2 -0
  17. package/dist/measuring/index.d.ts +28 -0
  18. package/dist/node/baselines/index.d.ts +40 -0
  19. package/dist/node/ce-adapter/ce-types.d.ts +171 -0
  20. package/dist/node/ce-adapter/extractTextPlacements.d.ts +11 -0
  21. package/dist/node/ce-adapter/index.d.ts +12 -0
  22. package/dist/node/ce-adapter/mapStateToRendererOptions.d.ts +13 -0
  23. package/dist/node/ce-adapter/types.d.ts +56 -0
  24. package/dist/node/constants.d.ts +5 -0
  25. package/dist/node/glyph/svg-glyph-converter.d.ts +42 -0
  26. package/dist/node/helpers/bbox.d.ts +33 -0
  27. package/dist/node/helpers/matrix.d.ts +8 -0
  28. package/dist/node/helpers/point.d.ts +17 -0
  29. package/dist/node/helpers/xml.d.ts +1 -0
  30. package/dist/node/index.d.ts +16 -249
  31. package/dist/node/index.js +821 -945
  32. package/dist/node/loading/createLoadedFont.d.ts +53 -0
  33. package/dist/node/loading/index.d.ts +4 -0
  34. package/dist/node/measuring/conversion.d.ts +2 -0
  35. package/dist/node/measuring/index.d.ts +28 -0
  36. package/dist/node/renderText.d.ts +5 -0
  37. package/dist/node/rendering/index.d.ts +42 -0
  38. package/dist/node/svg/index.d.ts +11 -0
  39. package/dist/node/types.d.ts +72 -0
  40. package/dist/renderText.d.ts +5 -0
  41. package/dist/rendering/index.d.ts +42 -0
  42. package/dist/svg/index.d.ts +11 -0
  43. package/dist/types.d.ts +72 -0
  44. package/dist/worker/baselines/index.d.ts +40 -0
  45. package/dist/worker/ce-adapter/ce-types.d.ts +171 -0
  46. package/dist/worker/ce-adapter/extractTextPlacements.d.ts +11 -0
  47. package/dist/worker/ce-adapter/index.d.ts +12 -0
  48. package/dist/worker/ce-adapter/mapStateToRendererOptions.d.ts +13 -0
  49. package/dist/worker/ce-adapter/types.d.ts +56 -0
  50. package/dist/worker/constants.d.ts +5 -0
  51. package/dist/worker/glyph/svg-glyph-converter.d.ts +42 -0
  52. package/dist/worker/helpers/bbox.d.ts +33 -0
  53. package/dist/worker/helpers/matrix.d.ts +8 -0
  54. package/dist/worker/helpers/point.d.ts +17 -0
  55. package/dist/worker/helpers/xml.d.ts +1 -0
  56. package/dist/worker/index.d.ts +16 -249
  57. package/dist/worker/index.js +8002 -9726
  58. package/dist/worker/loading/createLoadedFont.d.ts +53 -0
  59. package/dist/worker/loading/index.d.ts +4 -0
  60. package/dist/worker/measuring/conversion.d.ts +2 -0
  61. package/dist/worker/measuring/index.d.ts +28 -0
  62. package/dist/worker/renderText.d.ts +5 -0
  63. package/dist/worker/rendering/index.d.ts +42 -0
  64. package/dist/worker/svg/index.d.ts +11 -0
  65. package/dist/worker/types.d.ts +72 -0
  66. package/package.json +11 -10
  67. package/dist/__vite-browser-external-l0sNRNKZ.js +0 -1
@@ -0,0 +1,42 @@
1
+ import { LoadedFont } from '../loading';
2
+ import { ColourInfo, ColourMap, GlyphFill } from '../types';
3
+ /**
4
+ * Get the unique colours used by a font's SVG glyphs.
5
+ *
6
+ * @param font - A loaded font with svgGlyphs populated
7
+ * @returns Array of unique hex colour strings found in the font's SVG glyphs
8
+ */
9
+ export declare function getFontColours(font: LoadedFont): string[];
10
+ /**
11
+ * Extract fill paths from SVG content with optional colour remapping.
12
+ *
13
+ * @param svgContent - Raw SVG string from the font's SVG table
14
+ * @param colourMap - Optional mapping of original colours to replacements
15
+ * @returns Array of GlyphFill objects
16
+ */
17
+ export declare function extractFillsFromSVG(svgContent: string, colourMap?: ColourMap): GlyphFill[];
18
+ /**
19
+ * Apply colour map to remap a colour value.
20
+ *
21
+ * @param colour - Original colour hex string
22
+ * @param colourMap - Optional colour replacement mapping
23
+ * @returns ColourInfo with hex and optional reference
24
+ */
25
+ export declare function applyColourMap(colour: string, colourMap?: ColourMap): ColourInfo;
26
+ /**
27
+ * Extract the inner content from a raw SVG glyph string.
28
+ * Strips the XML declaration and outer <svg> wrapper, returning the inner elements.
29
+ */
30
+ export declare function extractSvgGlyphContent(rawSvg: string): string;
31
+ /**
32
+ * Namespace all IDs in SVG content to prevent collisions when multiple
33
+ * glyphs are embedded in the same document. Prefixes all id attributes
34
+ * and updates all references (href, url(), clip-path, mask, filter).
35
+ */
36
+ export declare function namespaceSvgIds(svgContent: string, prefix: string): string;
37
+ /**
38
+ * Remap colour values in raw SVG content. Replaces fill, stroke,
39
+ * and stop-color attribute values according to the colour map.
40
+ * Also handles elements without explicit fill that default to black.
41
+ */
42
+ export declare function remapSvgColours(svgContent: string, colourMap: ColourMap): string;
@@ -0,0 +1,33 @@
1
+ export declare class Box {
2
+ corners: DOMPoint[];
3
+ x1: number;
4
+ y1: number;
5
+ x2: number;
6
+ y2: number;
7
+ _x3: number | undefined;
8
+ _y3: number | undefined;
9
+ _x4: number | undefined;
10
+ _y4: number | undefined;
11
+ get x3(): number;
12
+ get y3(): number;
13
+ get x4(): number;
14
+ get y4(): number;
15
+ constructor(x1: number, y1: number, x2: number, y2: number, x3?: number, y3?: number, x4?: number, y4?: number);
16
+ transform(matrix: DOMMatrix): void;
17
+ toPointsArray(): number[][];
18
+ getBBox(): BBox;
19
+ }
20
+ export declare class BBox extends Box {
21
+ get x(): number;
22
+ get y(): number;
23
+ get width(): number;
24
+ get height(): number;
25
+ add(box: Box): void;
26
+ addPoint(x: number, y: number): void;
27
+ serialize(): {
28
+ x: number;
29
+ y: number;
30
+ width: number;
31
+ height: number;
32
+ };
33
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Create a matrix with optional initial values
3
+ * Detects what style of matrix is supported by the browser
4
+ *
5
+ * @param {Array} initialValues - the initial values for the matrix
6
+ * @returns {DOMMatrix} the new matrix
7
+ */
8
+ export declare const createMatrix: (initialValues?: number[]) => DOMMatrix;
@@ -0,0 +1,17 @@
1
+ export declare class Point {
2
+ private x;
3
+ private y;
4
+ constructor(x: number, y: number);
5
+ get X(): number;
6
+ get Y(): number;
7
+ toSVGString(): string;
8
+ }
9
+ /**
10
+ * Rotate a point about a pivot point
11
+ *
12
+ * @param point - the point to rotate
13
+ * @param rotate - the amount in degrees
14
+ * @param pivotPoint - the point around which to rotate
15
+ * @returns the new point
16
+ */
17
+ export declare function rotatePoint(point: Point, rotate: number, pivotPoint?: Point): Point;
@@ -0,0 +1 @@
1
+ export declare function escapeXML(str: string): string;
@@ -1,249 +1,16 @@
1
- export declare type BaselineConfig = {
2
- type: 'flat';
3
- } | {
4
- type: 'curved';
5
- } | {
6
- type: 'custom';
7
- path: string;
8
- };
9
-
10
- export declare type BaselineType = 'flat' | 'curved';
11
-
12
- export declare type BoxDimensions = {
13
- width: number;
14
- height: number;
15
- physicalWidth: number;
16
- physicalHeight: number;
17
- physicalUnits: MeasurementUnits;
18
- };
19
-
20
- export declare interface ColourInfo {
21
- readonly hex: string;
22
- readonly reference?: string;
23
- }
24
-
25
- /** Maps original hex colour → replacement ColourInfo. */
26
- export declare type ColourMap = Readonly<Record<string, ColourInfo>>;
27
-
28
- export declare const DEFAULT_OUTLINE_FACTOR = 0.03125;
29
-
30
- export declare const DEFAULT_SPACING: TextSpacing;
31
-
32
- /**
33
- * Generate a path command to curve text and align that text to the top of the box.
34
- * Uses two quadratic bezier curves — one from the bottom left to the peak, and a
35
- * second mirrored from the peak to the end.
36
- *
37
- * Curved text is not currently supported for multiline text.
38
- *
39
- * @returns an SVG path command to draw the curve
40
- */
41
- export declare const generateCurvedBaseline: ({ boxDimensions, baseline, textHeight, verticalAlignment, }: GenerateCurvedBaselineOptions) => string;
42
-
43
- export declare interface GenerateCurvedBaselineOptions {
44
- boxDimensions: BoxDimensions;
45
- baseline: number;
46
- textHeight: number;
47
- verticalAlignment: VerticalTextAlignmentValues;
48
- }
49
-
50
- /**
51
- * Generate a path as a baseline for straight text
52
- * This can be called multiple times for multiline text by incrementing the lineNumber parameter
53
- *
54
- * @returns an SVG path command to draw the baseline
55
- */
56
- export declare const generateFlatBaseline: ({ boxDimensions, totalTextHeight, baseline, lineHeight, verticalAlignment, lineNumber, }: GenerateFlatBaselineOptions) => string;
57
-
58
- export declare interface GenerateFlatBaselineOptions {
59
- boxDimensions: BoxDimensions;
60
- totalTextHeight: number;
61
- baseline: number;
62
- lineHeight: number;
63
- verticalAlignment?: VerticalTextAlignmentValues;
64
- lineNumber?: number;
65
- }
66
-
67
- export declare const getFontCache: () => Map<string, LoadedFont>;
68
-
69
- /**
70
- * Get the unique colours used by a font's SVG glyphs.
71
- *
72
- * @param font - A loaded font with svgGlyphs populated
73
- * @returns Array of unique hex colour strings found in the font's SVG glyphs
74
- */
75
- export declare function getFontColours(font: LoadedFont): string[];
76
-
77
- export declare const getFontSizeFromPhysicalSize: ({ font, boxDimensions, physicalSize, spacing, }: GetFontSizeFromPhysicalSizeOptions) => number;
78
-
79
- export declare interface GetFontSizeFromPhysicalSizeOptions {
80
- font: LoadedFont;
81
- boxDimensions: BoxDimensions;
82
- physicalSize: PhysicalSize;
83
- spacing?: TextSpacing;
84
- }
85
-
86
- /**
87
- * Async text renderer. Loads font from URL, then delegates to renderText.
88
- *
89
- * If the calculated font size causes a TOO_WIDE error and fontSizeFallback is
90
- * provided, retries once using the fallback as an explicit fontSize override.
91
- */
92
- export declare const getRenderedText: (options: GetRenderedTextOptions) => Promise<string>;
93
-
94
- /** Options for async getRenderedText (loads font from URL). */
95
- export declare interface GetRenderedTextOptions {
96
- text: string;
97
- fontUrl: string;
98
- physicalSize: PhysicalSize;
99
- /**
100
- * Fallback font size (in pixels) to use if the calculated size causes a
101
- * TOO_WIDE error. Useful when the caller has an authoritative size (e.g.
102
- * from the CE) that avoids floating-point rounding issues.
103
- */
104
- fontSizeFallback?: number;
105
- boxDimensions: BoxDimensions;
106
- spacing?: TextSpacing;
107
- verticalAlignment?: VerticalTextAlignmentValues;
108
- baseline?: BaselineType | BaselineConfig;
109
- horizontalAlignment?: HorizontalTextAlignmentValues;
110
- fillColour?: ColourInfo;
111
- strokeColour?: ColourInfo;
112
- colourMap?: ColourMap;
113
- }
114
-
115
- export declare interface GetTextAsPathDataOptions {
116
- font: LoadedFont;
117
- baselinePath: string;
118
- text: string;
119
- fontSize: number;
120
- spacing?: TextSpacing;
121
- horizontalAlignment?: HorizontalTextAlignmentValues;
122
- colourMap?: ColourMap;
123
- }
124
-
125
- export declare const getTextMetricsAtFontSize: ({ font, text, fontSize, spacing, }: GetTextMetricsAtFontSizeOptions) => {
126
- width: number;
127
- height: number;
128
- baseline: number;
129
- };
130
-
131
- export declare interface GetTextMetricsAtFontSizeOptions {
132
- font: LoadedFont;
133
- text: string;
134
- fontSize: number;
135
- spacing?: TextSpacing;
136
- }
137
-
138
- export declare interface GlyphFill {
139
- readonly pathData: string;
140
- readonly colour: ColourInfo;
141
- }
142
-
143
- export declare type HorizontalTextAlignmentValues = 'left' | 'centre' | 'right' | 'distributed';
144
-
145
- export declare const LINE_HEIGHT = 1.1;
146
-
147
- export declare interface LoadedFont {
148
- unitsPerEm: number;
149
- names: {
150
- fontFamily: string;
151
- };
152
- svgGlyphs?: Map<number, SVGGlyphData>;
153
- charToGlyph(char: string): {
154
- index: number;
155
- advanceWidth: number;
156
- name: string;
157
- getBoundingBox(): {
158
- x1: number;
159
- y1: number;
160
- x2: number;
161
- y2: number;
162
- };
163
- };
164
- getPath(text: string, x: number, y: number, fontSize: number, options?: {
165
- drawSVG?: boolean;
166
- }): {
167
- getBoundingBox(): {
168
- x1: number;
169
- y1: number;
170
- x2: number;
171
- y2: number;
172
- };
173
- toPathData(decimals?: number): string;
174
- };
175
- getPaths(text: string, x: number, y: number, fontSize: number, options?: {
176
- drawSVG?: boolean;
177
- }): Array<{
178
- getBoundingBox(): {
179
- x1: number;
180
- y1: number;
181
- x2: number;
182
- y2: number;
183
- };
184
- toPathData(decimals?: number): string;
185
- }>;
186
- }
187
-
188
- export declare const loadFont: (fontUrl: string, fetchSVGGlyphs?: boolean) => Promise<LoadedFont>;
189
-
190
- export declare type MeasurementUnits = 'mm' | 'in';
191
-
192
- /**
193
- * Normalise a baseline option (string shorthand or object) into a BaselineConfig.
194
- */
195
- export declare const normaliseBaselineOption: (baseline: BaselineType | BaselineConfig | undefined) => BaselineConfig;
196
-
197
- export declare type PhysicalSize = [height: number, units: MeasurementUnits];
198
-
199
- /**
200
- * Synchronous text renderer. Requires a pre-loaded font.
201
- */
202
- export declare const renderText: (options: RenderTextOptions) => string;
203
-
204
- /** Options for synchronous renderText (font already loaded). */
205
- export declare interface RenderTextOptions {
206
- text: string;
207
- font: LoadedFont;
208
- physicalSize: PhysicalSize;
209
- /** When provided, bypasses the physicalSize → font size calculation. */
210
- fontSize?: number;
211
- boxDimensions: BoxDimensions;
212
- spacing?: TextSpacing;
213
- verticalAlignment?: VerticalTextAlignmentValues;
214
- baseline?: BaselineType | BaselineConfig;
215
- horizontalAlignment?: HorizontalTextAlignmentValues;
216
- fillColour?: ColourInfo;
217
- strokeColour?: ColourInfo;
218
- colourMap?: ColourMap;
219
- }
220
-
221
- declare interface SVGGlyphData {
222
- svg: string;
223
- viewbox: string;
224
- }
225
-
226
- export declare interface SvgGlyphEntry {
227
- content: string;
228
- viewBox: string;
229
- x: number;
230
- y: number;
231
- width: number;
232
- height: number;
233
- glyphIndex: number;
234
- }
235
-
236
- export declare interface TextSpacing {
237
- readonly outlineWidth: number;
238
- readonly letterSpacing: number;
239
- readonly letterSpacingOutline: number;
240
- }
241
-
242
- /**
243
- * Validate a custom baseline SVG path string.
244
- */
245
- export declare const validateCustomBaselinePath: (path: string) => void;
246
-
247
- export declare type VerticalTextAlignmentValues = 'centre' | 'top' | 'bottom';
248
-
249
- export { }
1
+ import { GetRenderedTextOptions } from './types';
2
+ export { type GenerateCurvedBaselineOptions, type GenerateFlatBaselineOptions, generateCurvedBaseline, generateFlatBaseline, normaliseBaselineOption, validateCustomBaselinePath, } from './baselines';
3
+ export { DEFAULT_OUTLINE_FACTOR, DEFAULT_SPACING, LINE_HEIGHT, } from './constants';
4
+ export { getFontColours } from './glyph/svg-glyph-converter';
5
+ export { getFontCache, type LoadedFont, loadFont } from './loading';
6
+ export { type GetFontSizeFromPhysicalSizeOptions, type GetTextMetricsAtFontSizeOptions, getFontSizeFromPhysicalSize, getTextMetricsAtFontSize, } from './measuring';
7
+ export type { GetTextAsPathDataOptions, SvgGlyphEntry } from './rendering';
8
+ export { renderText } from './renderText';
9
+ export type { BaselineConfig, BaselineType, BoxDimensions, ColourInfo, ColourMap, GetRenderedTextOptions, GlyphFill, HorizontalTextAlignmentValues, MeasurementUnits, PhysicalSize, RenderTextOptions, TextSpacing, VerticalTextAlignmentValues, } from './types';
10
+ /**
11
+ * Async text renderer. Loads font from URL, then delegates to renderText.
12
+ *
13
+ * If the calculated font size causes a TOO_WIDE error and fontSizeFallback is
14
+ * provided, retries once using the fallback as an explicit fontSize override.
15
+ */
16
+ export declare const getRenderedText: (options: GetRenderedTextOptions) => Promise<string>;