-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfastchart_text.c
More file actions
380 lines (356 loc) · 14.8 KB
/
Copy pathfastchart_text.c
File metadata and controls
380 lines (356 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
+----------------------------------------------------------------------+
| Copyright (c) 2025-2026, Ilia Alshanetsky |
| Copyright (c) 2025-2026, Advanced Internet Designs Inc. |
+----------------------------------------------------------------------+
| This source file is subject to the BSD 3-Clause license that is |
| bundled with this package in the file LICENSE. |
+----------------------------------------------------------------------+
| Author: Ilia Alshanetsky <ilia@ilia.ws> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "fastchart_text.h"
#include "fastchart_target.h"
#include "fastchart_svg.h"
#include "fastchart_pdf.h"
/* Measure a UTF-8 byte range at the given point size + DPI using
* FreeType directly. text_len is the byte count (NOT codepoint count);
* the function does NOT require NUL termination, so callers can pass
* arbitrary slices of a larger buffer. out_w / out_h receive the
* pixel-space width and the ascender-to-descender height. Returns 0
* on success, -1 on FT failure. Width sums each glyph's advance.x
* (in 26.6 fixed-point pixels). Height is the face's bbox in pixel
* units at the configured size. */
static int fc_ft_measure(const char *font_path, double size_pt, int dpi,
const char *text, size_t text_len,
int *out_w, int *out_h)
{
if (!font_path || !text) return -1;
if (dpi <= 0) dpi = 96;
/* The per-thread cache owns the face; do not call FT_Done_Face. */
FT_Face face = fastchart_ft_face(font_path);
if (!face) return -1;
/* Size in 1/64 of a point at the given DPI. Always set: the
* cached face's size carries over from whatever the last caller
* configured, and a miss in the advance cache below loads a glyph
* that must read this size. */
int32_t size_64 = (int32_t)(size_pt * 64.0);
if (FT_Set_Char_Size(face, (FT_F26Dot6)size_64, 0, dpi, dpi)) {
return -1;
}
long total_w_64 = 0;
const unsigned char *p = (const unsigned char *)text;
const unsigned char *end = p + text_len;
uint32_t cp;
/* Shared walker: invalid bytes measure as U+FFFD with a real
* advance, matching what the SVG/PDF emitters will draw. The advance
* comes from the measure cache (keyed on the size set above), so a
* repeated glyph costs no FT_Load_Glyph. */
while ((p = fc_utf8_next_cp(p, end, &cp)) != NULL) {
int32_t adv;
if (fastchart_measured_advance(face, size_64, dpi, cp, &adv) != 0)
continue;
total_w_64 += adv;
}
int w = (int)((total_w_64 + 32) / 64);
/* Height: ascender + |descender| (both in font units, scale to px).
* units_per_EM is the design-space unit count; size_px is the
* pixel size at the current DPI. */
double size_px = size_pt * (double)dpi / 72.0;
int h;
if (face->units_per_EM > 0) {
h = (int)((face->ascender - face->descender) * size_px
/ face->units_per_EM + 0.5);
} else {
h = (int)(size_px + 0.5);
}
if (out_w) *out_w = w;
if (out_h) *out_h = h;
return 0;
}
/* Per-line vertical advance for multi-line strings. ascender +
* descender + 20% leading. Falls back to size*1.4 on FT failure. */
static int line_advance(int dpi, const char *font_path, double font_size)
{
int w, h;
if (fc_ft_measure(font_path, font_size, dpi, "Mg9j", 4, &w, &h) != 0) {
return (int)(font_size * 1.4 + 0.5);
}
if (h <= 0) return (int)(font_size * 1.4 + 0.5);
return h * 6 / 5; /* +20% leading */
}
/* Font loss between setter and draw degrades to missing text. Check face
* and size before flattening; size_px matches the emitters' rounding. */
static int fc_flatten_usable(const char *font_path, double size_px,
char *err_buf, size_t err_buf_n)
{
FT_Face face = fastchart_ft_face(font_path);
if (!face) {
if (err_buf && err_buf_n) {
snprintf(err_buf, err_buf_n, "font could not be loaded");
}
return -1;
}
FT_UInt pix = (FT_UInt)(size_px + 0.5);
if (pix < 1) pix = 1;
if (FT_Set_Pixel_Sizes(face, 0, pix)) {
if (err_buf && err_buf_n) {
snprintf(err_buf, err_buf_n, "font size setup failed");
}
return -1;
}
return 0;
}
/* Map fastchart_align to FASTCHART_TARGET_ALIGN_*. The numeric values
* happen to match today; the explicit translation keeps the layers
* decoupled if either enum is re-ordered. */
static int align_to_target(fastchart_align a)
{
switch (a) {
case FASTCHART_ALIGN_CENTER: return FASTCHART_TARGET_ALIGN_CENTER;
case FASTCHART_ALIGN_RIGHT: return FASTCHART_TARGET_ALIGN_RIGHT;
case FASTCHART_ALIGN_LEFT:
default: return FASTCHART_TARGET_ALIGN_LEFT;
}
}
int fastchart_text_draw(fastchart_target_t *t,
const char *font_path, double font_size,
int color, int x, int y,
fastchart_align align,
const char *text,
char *err_buf, size_t err_buf_n)
{
if (!font_path || !*font_path) {
if (err_buf && err_buf_n) snprintf(err_buf, err_buf_n, "no font path set");
return -1;
}
if (!text || !*text) return 0;
/* SVG path. text-anchor handles horizontal alignment so we don't
* pre-measure; pass the alignment hint through and emit one
* <text> per line. size_pt -> CSS px at 96 DPI baseline:
* px = pt * 4/3. 20% leading matches the measured line advance.
* Branch on the target's text mode: PATHS flattens to <path>
* glyphs via FreeType; NATIVE keeps <text>. */
char family[64];
/* The family name is consumed only by the NATIVE <text> branch;
* PATHS mode and every raster/PDF render pay nothing for it. Resolve
* lazily. The kind check short-circuits before reading the svg union
* member when the target is PDF. */
int native = (t->kind != FASTCHART_TARGET_PDF
&& t->u.svg.text_mode == FASTCHART_SVG_TEXT_NATIVE);
if (native)
fastchart_target_resolve_font_family(t, font_path, family,
sizeof(family));
double size_px = font_size * (4.0 / 3.0);
uint32_t rgba = fastchart_target_color_to_rgba(t, color);
int svg_align = align_to_target(align);
/* Same measured advance the measure path uses, so multi-line
* layout reservations match what gets drawn. */
double line_step =
(double)line_advance(fastchart_target_get_dpi(t), font_path,
font_size);
/* Flattening skips unusable fonts; NATIVE needs no FreeType. */
int flatten = 0;
#ifdef HAVE_FASTCHART_PDF
if (t->kind == FASTCHART_TARGET_PDF) flatten = 1;
else flatten = (t->u.svg.text_mode == FASTCHART_SVG_TEXT_PATHS);
#else
flatten = (t->u.svg.text_mode == FASTCHART_SVG_TEXT_PATHS);
#endif
if (flatten && fc_flatten_usable(font_path, size_px,
err_buf, err_buf_n) != 0) {
return 0;
}
/* Emit each line directly from the source pointer + byte length.
* fc_svg_emit_text and fc_svg_emit_text_as_path both accept a
* (text, len) pair, so no intermediate NUL-terminated copy is
* needed — that removes the old byte-truncating char buf[1024]
* which could split a multi-byte UTF-8 sequence mid-line and
* silently drop the tail of long single-line labels. */
double line_y = (double)y;
const char *p = text;
while (1) {
const char *end = strchr(p, '\n');
size_t len = end ? (size_t)(end - p) : strlen(p);
if (len > 0) {
#ifdef HAVE_FASTCHART_PDF
if (t->kind == FASTCHART_TARGET_PDF) {
fc_pdf_emit_text_as_path(t->u.pdf.state, (double)x, line_y,
font_path, size_px, rgba,
0.0, svg_align, p, len);
} else
#endif
if (t->u.svg.text_mode == FASTCHART_SVG_TEXT_PATHS) {
fc_svg_emit_text_as_path(t->u.svg.buf, (double)x, line_y,
font_path, size_px, rgba,
0.0, svg_align, p, len);
} else {
fc_svg_emit_text(t->u.svg.buf, (double)x, line_y,
family, size_px, rgba,
0.0, svg_align, p, len);
}
}
if (!end) break;
p = end + 1;
line_y += line_step;
}
return 0;
}
int fastchart_text_draw_rotated(fastchart_target_t *t,
const char *font_path, double font_size,
int color, int x, int y,
fastchart_align align, double angle_deg,
const char *text,
char *err_buf, size_t err_buf_n)
{
if (!font_path || !*font_path) {
if (err_buf && err_buf_n) snprintf(err_buf, err_buf_n, "no font path set");
return -1;
}
if (!text || !*text) return 0;
/* SVG path. fc_svg_emit_text handles rotation via transform=
* "rotate(angle, x, y)"; text-anchor handles alignment.
*
* Multi-line: split on \n and emit one rotated <text> per line.
* Each line's anchor is offset PERPENDICULAR to the rotation
* direction so the lines stack along the rotated y-axis. For
* rotation by angle θ (CCW), a vertical line-step of S becomes:
* delta_x = -S * sin(θ)
* delta_y = S * cos(θ)
* The unrotated path emitted one element with raw \n bytes in
* the body; SVG <text> collapses \n to whitespace at render
* time, so the multi-line label rendered as a single line —
* while the companion measure() function reports the multi-line
* height, so layout and rendering disagreed. */
char family[64];
/* Family name only feeds the NATIVE <text> branch; resolve lazily so
* PATHS mode and PDF/raster renders skip the cache scan + snprintfs. */
int native = (t->kind != FASTCHART_TARGET_PDF
&& t->u.svg.text_mode == FASTCHART_SVG_TEXT_NATIVE);
if (native)
fastchart_target_resolve_font_family(t, font_path, family,
sizeof(family));
double size_px = font_size * (4.0 / 3.0);
uint32_t rgba = fastchart_target_color_to_rgba(t, color);
int svg_align = align_to_target(align);
/* Same measured advance the measure path uses; see draw above. */
double line_step =
(double)line_advance(fastchart_target_get_dpi(t), font_path,
font_size);
/* Flattening skips unusable fonts. */
int flatten = 0;
#ifdef HAVE_FASTCHART_PDF
if (t->kind == FASTCHART_TARGET_PDF) flatten = 1;
else flatten = (t->u.svg.text_mode == FASTCHART_SVG_TEXT_PATHS);
#else
flatten = (t->u.svg.text_mode == FASTCHART_SVG_TEXT_PATHS);
#endif
if (flatten && fc_flatten_usable(font_path, size_px,
err_buf, err_buf_n) != 0) {
return 0;
}
double rad = angle_deg * M_PI / 180.0;
double sin_t = sin(rad);
double cos_t = cos(rad);
int line_no = 0;
const char *p = text;
while (1) {
const char *end = strchr(p, '\n');
size_t len = end ? (size_t)(end - p) : strlen(p);
if (len > 0) {
double offset = line_no * line_step;
double line_x = (double)x - offset * sin_t;
double line_y = (double)y + offset * cos_t;
#ifdef HAVE_FASTCHART_PDF
if (t->kind == FASTCHART_TARGET_PDF) {
fc_pdf_emit_text_as_path(t->u.pdf.state, line_x, line_y,
font_path, size_px, rgba,
angle_deg, svg_align, p, len);
} else
#endif
if (t->u.svg.text_mode == FASTCHART_SVG_TEXT_PATHS) {
fc_svg_emit_text_as_path(t->u.svg.buf, line_x, line_y,
font_path, size_px, rgba,
angle_deg, svg_align, p, len);
} else {
fc_svg_emit_text(t->u.svg.buf, line_x, line_y,
family, size_px, rgba,
angle_deg, svg_align, p, len);
}
}
if (!end) break;
p = end + 1;
line_no++;
}
return 0;
}
int fastchart_text_measure(fastchart_target_t *t,
const char *font_path, double font_size,
const char *text,
int *out_w, int *out_h,
char *err_buf, size_t err_buf_n)
{
if (!font_path || !*font_path) {
if (err_buf && err_buf_n) {
snprintf(err_buf, err_buf_n, "no font path set");
}
return -1;
}
if (!text || !*text) {
if (out_w) *out_w = 0;
if (out_h) *out_h = 0;
return 0;
}
int dpi = t ? fastchart_target_get_dpi(t) : 96;
/* Multi-line: width = max line width, height = first ascender +
* (n_lines - 1) * advance. Mirror the draw path's per-line
* stepping so layout reservations match what gets drawn. */
if (strchr(text, '\n')) {
int adv = line_advance(dpi, font_path, font_size);
int max_w = 0, n_lines = 0;
int first_h = 0;
const char *p = text;
while (1) {
const char *end = strchr(p, '\n');
size_t len = end ? (size_t)(end - p) : strlen(p);
n_lines++;
if (len > 0) {
int w = 0, h = 0;
if (fc_ft_measure(font_path, font_size, dpi, p, len, &w, &h) != 0) {
/* Mirror the single-line path: a measurement failure
* must surface, not be silently skipped with stale
* bounds (callers reserve layout from out_w/out_h). */
if (err_buf && err_buf_n) {
snprintf(err_buf, err_buf_n, "FreeType measure failed");
}
return -1;
}
if (w > max_w) max_w = w;
if (first_h == 0) first_h = h;
}
if (!end) break;
p = end + 1;
}
if (out_w) *out_w = max_w;
if (out_h) *out_h = first_h + (n_lines - 1) * adv;
return 0;
}
int w = 0, h = 0;
if (fc_ft_measure(font_path, font_size, dpi, text, strlen(text), &w, &h) != 0) {
if (err_buf && err_buf_n) {
snprintf(err_buf, err_buf_n, "FreeType measure failed");
}
return -1;
}
if (out_w) *out_w = w;
if (out_h) *out_h = h;
return 0;
}