@unmade/text-renderer 0.1.2 → 0.1.4

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.
@@ -1,6 +1,15 @@
1
1
  import { default as default_2 } from 'opentype.js';
2
2
  import { SVGGlyphData } from '@unmade/svg-glyphs';
3
3
 
4
+ export declare type BaselineConfig = {
5
+ type: 'flat';
6
+ } | {
7
+ type: 'curved';
8
+ } | {
9
+ type: 'custom';
10
+ path: string;
11
+ };
12
+
4
13
  export declare type BaselineType = 'flat' | 'curved';
5
14
 
6
15
  export declare type BoxDimensions = {
@@ -11,52 +20,120 @@ export declare type BoxDimensions = {
11
20
  physicalUnits: MeasurementUnits;
12
21
  };
13
22
 
23
+ export declare interface ColourInfo {
24
+ readonly hex: string;
25
+ readonly reference?: string;
26
+ }
27
+
28
+ /** Maps original hex colour → replacement ColourInfo. */
29
+ export declare type ColourMap = Readonly<Record<string, ColourInfo>>;
30
+
14
31
  export declare const DEFAULT_OUTLINE_FACTOR = 0.03125;
15
32
 
33
+ export declare const DEFAULT_SPACING: TextSpacing;
34
+
16
35
  /**
17
- * Generate a path command to curve text and align that text to the top of the box
18
- * We use two quadratic bezier curves[1] - one from the bottom left to the peak of the path, and a second, mirrored, from the peak to the end of the path
19
- * Using two curves allows us to explicitly define the peak of the curve, which will be at the horizontal centre of the box, far enough down from the top of the box that there's space for the text
20
- * SVG Quadratic curves require two sets of coordinates - a control point and end point, with the start point being the end point of the previous command
21
- * [1] https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths#B%C3%A9zier_Curves
36
+ * Generate a path command to curve text and align that text to the top of the box.
37
+ * Uses two quadratic bezier curves — one from the bottom left to the peak, and a
38
+ * second mirrored from the peak to the end.
22
39
  *
23
- * Curved text is not currently supported for multiline text so this does not handle multiline parameters
40
+ * Curved text is not currently supported for multiline text.
24
41
  *
25
- * @param boxWidth - the width of the box
26
- * @param boxHeight - the height of the box
27
- * @param textBaselineYPosition - the distance from the top of rendered text to the baseline
28
- * @param textHeight - the height of the text being rendered
29
- * @param verticalAlignment - the vertical alignment of the text
30
42
  * @returns an SVG path command to draw the curve
31
43
  */
32
- export declare const generateCurvedBaseline: (boxDimensions: BoxDimensions, textBaselineYPosition: number, textHeight: number, verticalAlignment: VerticalTextAlignmentValues) => string;
44
+ export declare const generateCurvedBaseline: ({ boxDimensions, baseline, textHeight, verticalAlignment, }: GenerateCurvedBaselineOptions) => string;
45
+
46
+ export declare interface GenerateCurvedBaselineOptions {
47
+ boxDimensions: BoxDimensions;
48
+ baseline: number;
49
+ textHeight: number;
50
+ verticalAlignment: VerticalTextAlignmentValues;
51
+ }
33
52
 
34
53
  /**
35
54
  * Generate a path as a baseline for straight text
36
55
  * This can be called multiple times for multiline text by incrementing the lineNumber parameter
37
56
  *
38
- * @param boxDimensions - dimensions of the box into which the text will be rendered
39
- * @param totalHeightOfAllTextLines - combined measured height of all lines of text
40
- * @param textBaselineYPosition - the y position of the measured baseline for this specific line of text
41
- * @param heightOfThisTextLine - the height of this specific line of text
42
- * @param verticalAlignment - vertical alignment of the text within the box
43
- * @param lineNumber - the line number of this text line (starting from 0)
44
- * @returns an SVG path command to draw the curve
57
+ * @returns an SVG path command to draw the baseline
45
58
  */
46
- export declare const generateFlatBaseline: (boxDimensions: BoxDimensions, totalHeightOfAllTextLines: number, textBaselineYPosition: number, heightOfThisTextLine: number, verticalAlignment?: VerticalTextAlignmentValues, lineNumber?: number) => string;
59
+ export declare const generateFlatBaseline: ({ boxDimensions, totalTextHeight, baseline, lineHeight, verticalAlignment, lineNumber, }: GenerateFlatBaselineOptions) => string;
60
+
61
+ export declare interface GenerateFlatBaselineOptions {
62
+ boxDimensions: BoxDimensions;
63
+ totalTextHeight: number;
64
+ baseline: number;
65
+ lineHeight: number;
66
+ verticalAlignment?: VerticalTextAlignmentValues;
67
+ lineNumber?: number;
68
+ }
47
69
 
48
70
  export declare const getFontCache: () => Map<any, any>;
49
71
 
50
- export declare const getFontSizeFromPhysicalSize: (font: LoadedFont, boxDimensions: BoxDimensions, physicalFontSize: PhysicalSize, textOutlineFactor?: number) => number;
72
+ /**
73
+ * Get the unique colours used by a font's SVG glyphs.
74
+ *
75
+ * @param font - A loaded font with svgGlyphs populated
76
+ * @returns Array of unique hex colour strings found in the font's SVG glyphs
77
+ */
78
+ export declare function getFontColours(font: LoadedFont): string[];
79
+
80
+ export declare const getFontSizeFromPhysicalSize: ({ font, boxDimensions, physicalSize, spacing, }: GetFontSizeFromPhysicalSizeOptions) => number;
51
81
 
52
- export declare const getRenderedText: (options: RenderTextOptions) => Promise<string>;
82
+ export declare interface GetFontSizeFromPhysicalSizeOptions {
83
+ font: LoadedFont;
84
+ boxDimensions: BoxDimensions;
85
+ physicalSize: PhysicalSize;
86
+ spacing?: TextSpacing;
87
+ }
53
88
 
54
- export declare const getTextMetricsAtFontSize: (font: LoadedFont, text: string, fontSize: number, outlineFactor?: number) => {
89
+ /**
90
+ * Async text renderer. Loads font from URL, then delegates to renderText.
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
+ boxDimensions: BoxDimensions;
100
+ spacing?: TextSpacing;
101
+ verticalAlignment?: VerticalTextAlignmentValues;
102
+ baseline?: BaselineType | BaselineConfig;
103
+ horizontalAlignment?: HorizontalTextAlignmentValues;
104
+ fillColour?: ColourInfo;
105
+ strokeColour?: ColourInfo;
106
+ colourMap?: ColourMap;
107
+ }
108
+
109
+ export declare interface GetTextAsPathDataOptions {
110
+ font: LoadedFont;
111
+ baselinePath: string;
112
+ text: string;
113
+ fontSize: number;
114
+ spacing?: TextSpacing;
115
+ horizontalAlignment?: HorizontalTextAlignmentValues;
116
+ colourMap?: ColourMap;
117
+ }
118
+
119
+ export declare const getTextMetricsAtFontSize: ({ font, text, fontSize, spacing, }: GetTextMetricsAtFontSizeOptions) => {
55
120
  width: number;
56
121
  height: number;
57
122
  baseline: number;
58
123
  };
59
124
 
125
+ export declare interface GetTextMetricsAtFontSizeOptions {
126
+ font: LoadedFont;
127
+ text: string;
128
+ fontSize: number;
129
+ spacing?: TextSpacing;
130
+ }
131
+
132
+ export declare interface GlyphFill {
133
+ readonly pathData: string;
134
+ readonly colour: ColourInfo;
135
+ }
136
+
60
137
  export declare type HorizontalTextAlignmentValues = 'left' | 'centre' | 'right' | 'distributed';
61
138
 
62
139
  export declare const LINE_HEIGHT = 1.1;
@@ -71,21 +148,54 @@ export declare const loadOpentypeFont: (fontUrl: string, fetchSVGGlyphs?: boolea
71
148
 
72
149
  export declare type MeasurementUnits = 'mm' | 'in';
73
150
 
151
+ /**
152
+ * Normalise a baseline option (string shorthand or object) into a BaselineConfig.
153
+ */
154
+ export declare const normaliseBaselineOption: (baseline: BaselineType | BaselineConfig | undefined) => BaselineConfig;
155
+
74
156
  export declare type PhysicalSize = [height: number, units: MeasurementUnits];
75
157
 
158
+ /**
159
+ * Synchronous text renderer. Requires a pre-loaded font.
160
+ */
161
+ export declare const renderText: (options: RenderTextOptions) => string;
162
+
163
+ /** Options for synchronous renderText (font already loaded). */
76
164
  export declare interface RenderTextOptions {
77
165
  text: string;
78
- fontUrl: string;
166
+ font: LoadedFont;
79
167
  physicalSize: PhysicalSize;
80
168
  boxDimensions: BoxDimensions;
81
- outlineFactor?: number;
169
+ spacing?: TextSpacing;
82
170
  verticalAlignment?: VerticalTextAlignmentValues;
83
- baseline?: BaselineType;
171
+ baseline?: BaselineType | BaselineConfig;
84
172
  horizontalAlignment?: HorizontalTextAlignmentValues;
85
- fillColour?: string;
86
- strokeColour?: string;
173
+ fillColour?: ColourInfo;
174
+ strokeColour?: ColourInfo;
175
+ colourMap?: ColourMap;
176
+ }
177
+
178
+ export declare interface SvgGlyphEntry {
179
+ content: string;
180
+ viewBox: string;
181
+ x: number;
182
+ y: number;
183
+ width: number;
184
+ height: number;
185
+ glyphIndex: number;
186
+ }
187
+
188
+ export declare interface TextSpacing {
189
+ readonly outlineWidth: number;
190
+ readonly letterSpacing: number;
191
+ readonly letterSpacingOutline: number;
87
192
  }
88
193
 
194
+ /**
195
+ * Validate a custom baseline SVG path string.
196
+ */
197
+ export declare const validateCustomBaselinePath: (path: string) => void;
198
+
89
199
  export declare type VerticalTextAlignmentValues = 'centre' | 'top' | 'bottom';
90
200
 
91
201
  export { }