@@ -86,6 +86,108 @@ class Font {
86
86
return renderer . textBounds ( str , x , y , width , height ) ;
87
87
}
88
88
89
+ /**
90
+ * Returns a flat array of path commands that describe the outlines of a string of text.
91
+ *
92
+ * Each command is represented as an array of the form `[type, ...coords]`, where:
93
+ * - `type` is one of `'M'`, `'L'`, `'Q'`, `'C'`, or `'Z'`,
94
+ * - `coords` are the numeric values needed for that command.
95
+ *
96
+ * `'M'` indicates a "move to" (starting a new contour),
97
+ * `'L'` a line segment,
98
+ * `'Q'` a quadratic bezier,
99
+ * `'C'` a cubic bezier, and
100
+ * `'Z'` closes the current path.
101
+ *
102
+ * The first two parameters, `x` and `y`, specify the baseline origin for the text.
103
+ * Optionally, you can provide a `width` and `height` for text wrapping; if you don't need
104
+ * wrapping, you can omit them and directly pass `options` as the fourth parameter.
105
+ *
106
+ * @param {String } str The text to convert into path commands.
107
+ * @param {Number } x x‐coordinate of the text baseline.
108
+ * @param {Number } y y‐coordinate of the text baseline.
109
+ * @param {Number } [width] Optional width for text wrapping.
110
+ * @param {Number } [height] Optional height for text wrapping.
111
+ * @param {Object } [options] Configuration object for rendering text.
112
+ * @return {Array<Array> } A flat array of path commands.
113
+ *
114
+ * @example
115
+ * <div>
116
+ * <code>
117
+ * let font;
118
+ *
119
+ * async function setup() {
120
+ * font = await loadFont('assets/inconsolata.otf');
121
+ * createCanvas(200, 200);
122
+ * background(220);
123
+ * noLoop();
124
+ * }
125
+ *
126
+ * function draw() {
127
+ * background(220);
128
+ * stroke(0);
129
+ * noFill();
130
+ * textSize(60);
131
+ *
132
+ * // Get path commands for "Hello" (drawn at baseline x=50, y=100):
133
+ * const pathCommands = font.textToPaths('Hello', 30, 110);
134
+ *
135
+ * beginShape();
136
+ * for (let i = 0; i < pathCommands.length; i++) {
137
+ * const cmd = pathCommands[i];
138
+ * const type = cmd[0];
139
+ *
140
+ * switch (type) {
141
+ * case 'M': {
142
+ * // Move to (start a new contour)
143
+ * const x = cmd[1];
144
+ * const y = cmd[2];
145
+ * endContour(); // In case we were already drawing
146
+ * beginContour();
147
+ * vertex(x, y);
148
+ * break;
149
+ * }
150
+ * case 'L': {
151
+ * // Line to
152
+ * const x = cmd[1];
153
+ * const y = cmd[2];
154
+ * vertex(x, y);
155
+ * break;
156
+ * }
157
+ * case 'Q': {
158
+ * // Quadratic curve to: ['Q', cx, cy, x, y]
159
+ * // Simplified to just draw line to endpoint
160
+ * const x = cmd[3];
161
+ * const y = cmd[4];
162
+ * vertex(x, y);
163
+ * break;
164
+ * }
165
+ * case 'C': {
166
+ * // Cubic bezier to: ['C', x1, y1, x2, y2, x3, y3]
167
+ * // Simplified to just draw line to endpoint
168
+ * const x = cmd[5];
169
+ * const y = cmd[6];
170
+ * vertex(x, y);
171
+ * break;
172
+ * }
173
+ * case 'Z': {
174
+ * // Close path
175
+ * endContour(CLOSE);
176
+ * beginContour();
177
+ * break;
178
+ * }
179
+ * }
180
+ * }
181
+ * endContour();
182
+ * endShape();
183
+ *
184
+ * // Note: The letters will appear angular since we're only using lines
185
+ * // between points rather than the actual curves from the font outlines.
186
+ * }
187
+ * </code>
188
+ * </div>
189
+ */
190
+
89
191
textToPaths ( str , x , y , width , height , options ) {
90
192
91
193
( { width, height, options } = this . _parseArgs ( width , height , options ) ) ;
0 commit comments