gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
preview.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#include <stdlib.h>
7
8#include <gfxprim.h>
9
10#include "unicode_blocks.h"
11#include "compose.h"
12#include "gui.h"
13#include "preview.h"
14#include "cell.h"
15
16/*
17 * Sidebearings are judged in context, never on a single glyph, so the second
18 * line puts the glyph between reference glyphs.
19 */
20static void spacing_str(char *buf, size_t len)
21{
22 char ch[5];
23
24 ch[gp_to_utf8(gui.code, ch)] = 0;
25
26 snprintf(buf, len, "non%snon HOH%sHOH", ch, ch);
27}
28
29/*
30 * A block with no sample text of its own is previewed with its own glyphs,
31 * which is all a block of symbols can be shown as anyway.
32 */
33static const char *block_sample(char *buf, size_t len)
34{
35 const struct gpf_ucode_block *block = gpf_gui_block();
36 struct gpf_resolved res;
37 const char *sample;
38 uint32_t code;
39 size_t pos = 0;
40
41 if (!block)
42 return "";
43
44 sample = gpf_ucode_block_sample(block->name);
45 if (sample)
46 return sample;
47
48 for (code = block->min; code <= block->max; code++) {
49 if (pos + 5 >= len)
50 break;
51
52 if (gpf_resolve(gui.font, gui.variant, code, &res))
53 continue;
54
56
57 pos += gp_to_utf8(code, buf + pos);
58 buf[pos++] = ' ';
59 }
60
61 buf[pos] = 0;
62
63 return buf;
64}
65
66/*
67 * The line the user typed, drawn under the two the preview picks for itself.
68 * It is read off the widget at every paint rather than kept anywhere: the
69 * widget is where it lives, and the preview is recomputed either way.
70 */
71static const char *custom_line(void)
72{
73 const char *text;
74
75 if (!gui.preview_text)
76 return NULL;
77
78 text = gp_widget_tbox_text(gui.preview_text);
79
80 if (!text || !text[0])
81 return NULL;
82
83 return text;
84}
85
86/*
87 * The underline, the strikethrough and the overline, where the font says and
88 * at the size the line is drawn at: gp_text_decor_pos() and
89 * gp_text_decor_thickness() scale the face's metric by the style, which is
90 * what they are for. The position is the top of the rule, counted up from the
91 * baseline.
92 */
93static void draw_rule(gp_pixmap *p, const gp_text_style *style, int x,
94 int baseline, unsigned int w,
95 enum gp_text_decor_type type, gp_pixel fg)
96{
97 gp_fill_rect_xywh(p, x, baseline - gp_text_decor_pos(style, type),
98 w, gp_text_decor_thickness(style, type), fg);
99}
100
101static void draw_rules(gp_pixmap *p, const gp_text_style *style, int x, int y,
102 const char *str, gp_pixel fg)
103{
104 int baseline = y + (int)gp_text_ascent(style);
105 unsigned int w = gp_text_wbbox(style, str);
106
107 if (gui.rule_underline) {
108 draw_rule(p, style, x, baseline, w, GP_TEXT_DECOR_UNDERLINE,
109 fg);
110 }
111
112 if (gui.rule_strike) {
113 draw_rule(p, style, x, baseline, w,
114 GP_TEXT_DECOR_STRIKETHROUGH, fg);
115 }
116
117 if (gui.rule_overline)
118 draw_rule(p, style, x, baseline, w, GP_TEXT_DECOR_OVERLINE, fg);
119}
120
121/* the space between the zooms of a line, and between the lines */
122#define ZOOM_GAP 2
123#define LINE_GAP 6
124
125#define MAX_ZOOM 3
126
127/*
128 * A line of the preview, drawn once at each zoom its mask has a bit for, bit
129 * zero being 1x.
130 */
131struct preview_line {
132 const char *str;
133 unsigned int zooms;
134};
135
136static int lines_height(const gp_font_face *face,
137 const struct preview_line *lines, unsigned int cnt)
138{
139 unsigned int i, z;
140 int h = 2;
141
142 for (i = 0; i < cnt; i++) {
143 for (z = 1; z <= MAX_ZOOM; z++) {
144 if (lines[i].zooms & (1u << (z - 1)))
145 h += z * gp_font_height(face) + ZOOM_GAP;
146 }
147
148 h += LINE_GAP - ZOOM_GAP;
149 }
150
151 /* nothing follows the last line */
152 return h - LINE_GAP;
153}
154
155/*
156 * Every line at 1x and 2x, and at 3x as well when there is room for all of
157 * them. A preview too short for all the 2x lines drops them one at a time:
158 * the context line keeps its 2x the longest, since spacing is what is judged
159 * at a zoom, then the typed line, then the sample.
160 */
161static void lines_fit(const gp_font_face *face, struct preview_line *lines,
162 unsigned int cnt, int h)
163{
164 static const unsigned int prio[] = {1, 2, 0};
165 unsigned int i;
166
167 for (i = 0; i < GP_ARRAY_SIZE(prio); i++) {
168 if (prio[i] >= cnt)
169 continue;
170
171 lines[prio[i]].zooms |= 2;
172
173 if (lines_height(face, lines, cnt) > h)
174 lines[prio[i]].zooms &= ~2u;
175 }
176
177 for (i = 0; i < cnt; i++) {
178 if (!(lines[i].zooms & 2))
179 return;
180 }
181
182 for (i = 0; i < cnt; i++)
183 lines[i].zooms |= 4;
184
185 if (lines_height(face, lines, cnt) <= h)
186 return;
187
188 for (i = 0; i < cnt; i++)
189 lines[i].zooms &= ~4u;
190}
191
192void gpf_preview_draw(gp_widget *self)
193{
194 gp_pixmap *p = gp_widget_pixmap_get(self);
195 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
196 const gp_font_face *face = gpf_gui_face(gui.variant);
197 gp_text_style style = {};
198 struct preview_line lines[3];
199 const char *custom = custom_line();
200 char sample[256], spacing[64];
201 unsigned int i, z, cnt = 0;
202 gp_pixel fg, bg;
203 int y = 2;
204
205 if (!p)
206 return;
207
208 fg = gp_widgets_color(ctx, GP_WIDGETS_COL_TEXT);
209 bg = gp_widgets_color(ctx, GP_WIDGETS_COL_FG);
210
211 gp_fill(p, bg);
212
213 if (!face)
214 return;
215
216 style.font = face;
217
218 spacing_str(spacing, sizeof(spacing));
219
220 /*
221 * The sample follows the block being worked on, the context line the
222 * glyph, and the typed line is whatever the user wants to look at.
223 */
224 lines[cnt++] = (struct preview_line){
225 .str = block_sample(sample, sizeof(sample)), .zooms = 1};
226 lines[cnt++] = (struct preview_line){.str = spacing, .zooms = 1};
227
228 if (custom)
229 lines[cnt++] = (struct preview_line){.str = custom, .zooms = 1};
230
231 lines_fit(face, lines, cnt, p->h);
232
233 for (i = 0; i < cnt; i++) {
234 for (z = 1; z <= MAX_ZOOM; z++) {
235 if (!(lines[i].zooms & (1u << (z - 1))))
236 continue;
237
238 style.pixel_xmul = z;
239 style.pixel_ymul = z;
240
241 gp_text(p, &style, 2, y, GP_ALIGN_RIGHT | GP_VALIGN_BELOW,
242 fg, bg, lines[i].str);
243
244 draw_rules(p, &style, 2, y, lines[i].str, fg);
245
246 y += gp_text_height(&style) + ZOOM_GAP;
247 }
248
249 y += LINE_GAP - ZOOM_GAP;
250 }
251}
252
253/*
254 * The same glyph in all four variants, and the variant selector: the whole
255 * format is derivation, so the payoff of an edit is what happens downstream,
256 * and clicking the variant you want to look at is the natural way to switch.
257 */
258static const char *short_names[GPF_VARIANTS] = {
259 [GPF_MONO] = "mono",
260 [GPF_REGULAR] = "reg",
261 [GPF_BOLD_MONO] = "bmono",
262 [GPF_BOLD] = "bold",
263};
264
265/*
266 * The label has to survive a bigger UI font, a TrueType one included, so it
267 * falls back to a short name and then to nothing rather than running into the
268 * next cell.
269 */
270static const char *fit_label(const gp_widget_render_ctx *ctx,
271 enum gpf_variant_id id, unsigned int cell_w)
272{
273 const char *name = gpf_variant_name(id);
274
275 if (gp_text_wbbox(ctx->font, name) <= cell_w)
276 return name;
277
278 name = short_names[id];
279
280 if (gp_text_wbbox(ctx->font, name) <= cell_w)
281 return name;
282
283 return NULL;
284}
285
286static void draw_variant(gp_pixmap *p, const gp_widget_render_ctx *ctx,
287 enum gpf_variant_id id, unsigned int cell_w)
288{
289 struct gpf_resolved res;
290 unsigned int label_h = gp_text_height(ctx->font);
291 unsigned int line_h = gui.font->meta.ascent + gui.font->meta.descent;
292 unsigned int gx, gy, zoom;
293 const char *label;
294 int x = id * cell_w;
295 int ox, oy;
296 gp_pixel bg, fg;
297
298 bg = id == gui.variant ? gp_widgets_color(ctx, GP_WIDGETS_COL_SELECT)
299 : gpf_gui_back_color(gui.strip);
300 fg = gp_widgets_color(ctx, GP_WIDGETS_COL_TEXT);
301
302 gp_fill_rect_xywh(p, x, 0, cell_w, p->h, bg);
303
304 label = fit_label(ctx, id, cell_w - 2);
305
306 if (label) {
307 /*
308 * The text background has to be the colour that is actually
309 * under it, or an antialiased font fringes against the wrong
310 * one on the selected cell.
311 */
312 gp_text(p, ctx->font, x + cell_w/2, 0,
313 GP_ALIGN_CENTER | GP_VALIGN_BELOW, fg, bg, label);
314 }
315
316 if (!line_h)
317 return;
318
319 zoom = GPF_MAX(1u, (p->h - label_h - 2) / line_h);
320
321 if (gpf_resolve(gui.font, id, gui.code, &res))
322 return;
323
324 oy = label_h + 2 + zoom * gui.font->meta.ascent;
325 ox = x + ((int)cell_w - zoom * res.glyph.advance) / 2;
326
327 for (gy = 0; gy < res.glyph.height; gy++) {
328 for (gx = 0; gx < res.glyph.width; gx++) {
329 if (!gpf_glyph_pixel(&res.glyph, gx, gy))
330 continue;
331
332 gp_fill_rect_xywh(p,
333 ox + zoom * (res.glyph.bearing_x + (int)gx),
334 oy - zoom * (res.glyph.bearing_y - (int)gy),
335 zoom, zoom, fg);
336 }
337 }
338
339 gpf_resolved_clear(&res);
340}
341
342void gpf_strip_draw(gp_widget *self)
343{
344 gp_pixmap *p = gp_widget_pixmap_get(self);
345 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
346 unsigned int i, cell_w;
347
348 if (!p)
349 return;
350
351 gp_fill(p, gpf_gui_back_color(self));
352
353 if (!gui.font)
354 return;
355
356 cell_w = GPF_MAX(1u, p->w / GPF_VARIANTS);
357
358 for (i = 0; i < GPF_VARIANTS; i++)
359 draw_variant(p, ctx, i, cell_w);
360}
361
362/*
363 * Everything composed from the glyph being edited, in the same cells as the
364 * glyph table and at its zoom: fixing a base letter is worth doing because of
365 * what it does to these, so they are on the screen while it is being done, and
366 * clicking one goes there.
367 */
368/* the longest list the strip holds: a caron goes on thirty-five letters */
369#define GPF_DEPS_MAX 256
370
371/*
372 * What the strip shows: the accented forms of the selected letter, or of the
373 * letter it is an accented form of — so selecting `i` and selecting `í` show
374 * the same list. Picking one out of the strip then leaves the strip alone,
375 * with the glyph you picked marked in it.
376 *
377 * A mark is not a letter with accented forms, it is the accent itself, and
378 * selecting one lists the letters that wear it instead — drawn with the mark
379 * as it is being drawn now, since editing a caron is editing every letter that
380 * has one and this is where that is seen.
381 *
382 * Returns non-zero when the list is an accent's letters, which is what is
383 * drawn composed rather than as the font has it.
384 */
385static int deps_codes(uint32_t *codes, unsigned int max, unsigned int *cnt)
386{
387 *cnt = gpf_decompose_by_accent(gui.code, codes, max);
388
389 if (*cnt)
390 return 1;
391
392 *cnt = gpf_decompose_accented(gpf_decompose_base(gui.code), codes, max);
393
394 return 0;
395}
396
397/*
398 * The letter as it would come out with this accent on it: the base the font
399 * has, plus the ink being edited, where gpf_accent_offset() puts it — which is
400 * where clicking an empty slot would draw it. Nothing is stored; the compose
401 * view shows what drawing it now would give.
402 */
403static int compose_preview(uint32_t code, const struct gpf_glyph *accent,
404 struct gpf_glyph *out)
405{
406 const struct gpf_decomposition *decomp = gpf_decompose(code);
407 struct gpf_resolved base;
408 int dx, dy, ret;
409
410 if (!decomp)
411 return 1;
412
413 if (gpf_draw_base(gui.font, gui.variant, decomp, &base))
414 return 1;
415
416 gpf_accent_offset(gui.font, &base.glyph, accent, decomp->place,
417 &dx, &dy);
418
419 ret = gpf_compose(out, &base.glyph, accent, dx, dy);
420
421 gpf_resolved_clear(&base);
422
423 return ret;
424}
425
426static int deps_grid(gp_pixmap *p, struct gpf_cell *cell, unsigned int *cols,
427 unsigned int *rows)
428{
429 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
430
431 if (!p || !gui.font)
432 return 1;
433
434 gpf_cell_init(cell, ctx, p->h);
435
436 cell->bg = gpf_gui_back_color(gui.deps);
437
438 *cols = GPF_MAX(1u, p->w / cell->w);
439 *rows = GPF_MAX(1u, p->h / cell->h);
440
441 gpf_cell_center(cell, p, *cols, *rows);
442
443 return 0;
444}
445
446/*
447 * The list, and where in it the strip is looking. The whole list is fetched,
448 * since what fits is rarely all of it — a caron goes on thirty-five letters —
449 * and the offset is clamped here, so that the drawing and the clicking cannot
450 * disagree about which cell is which.
451 */
452static int deps_list(uint32_t *codes, unsigned int max, unsigned int *cnt,
453 unsigned int visible)
454{
455 int compose = deps_codes(codes, max, cnt);
456 uint32_t anchor = compose ? gui.code : gpf_decompose_base(gui.code);
457
458 /* another letter is another list, and it starts at its beginning */
459 if (anchor != gui.deps_anchor) {
460 gui.deps_anchor = anchor;
461 gui.deps_off = 0;
462 }
463
464 if (*cnt <= visible)
465 gui.deps_off = 0;
466 else if (gui.deps_off > *cnt - visible)
467 gui.deps_off = *cnt - visible;
468
469 return compose;
470}
471
472/*
473 * A wheel step is one cell. The strip is a list of letters and not a page of
474 * codepoints like the table, so it is nudged along rather than turned over;
475 * scrolling is browsing and leaves the selection where it is.
476 */
477static int deps_scroll(int delta)
478{
479 gp_pixmap *p = gp_widget_pixmap_get(gui.deps);
480 uint32_t codes[GPF_DEPS_MAX];
481 struct gpf_cell cell;
482 unsigned int cols, rows, cnt, visible;
483 long off;
484
485 if (deps_grid(p, &cell, &cols, &rows))
486 return 0;
487
488 visible = cols * rows;
489
490 deps_list(codes, GPF_DEPS_MAX, &cnt, visible);
491
492 if (cnt <= visible)
493 return 0;
494
495 off = (long)gui.deps_off + delta;
496
497 off = GPF_MAX(0l, GPF_MIN(off, (long)(cnt - visible)));
498
499 if ((unsigned int)off == gui.deps_off)
500 return 0;
501
502 gui.deps_off = off;
503
504 gpf_deps_draw(gui.deps);
505 gp_widget_redraw(gui.deps);
506
507 return 1;
508}
509
510void gpf_deps_draw(gp_widget *self)
511{
512 gp_pixmap *p = gp_widget_pixmap_get(self);
513 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
514 uint32_t codes[GPF_DEPS_MAX];
515 struct gpf_cell cell;
516 struct gpf_resolved accent;
517 unsigned int cols, rows, cnt, shown, i;
518 int compose;
519
520 if (p)
521 gp_fill(p, gp_widgets_color(ctx, GP_WIDGETS_COL_BG));
522
523 if (deps_grid(p, &cell, &cols, &rows))
524 return;
525
526 compose = deps_list(codes, GPF_DEPS_MAX, &cnt, cols * rows);
527
528 shown = GPF_MIN(cnt - gui.deps_off, cols * rows);
529
530 /*
531 * The mark itself, which is the ink the letters are drawn with here.
532 * A mark the variant has no glyph for has nothing to put on them, so
533 * the letters are shown as the font has them instead.
534 */
535 if (compose && gpf_resolve(gui.font, gui.variant, gui.code, &accent))
536 compose = 0;
537
538 for (i = 0; i < shown; i++) {
539 uint32_t code = codes[gui.deps_off + i];
540 int x = (i % cols) * cell.w;
541 int y = (i / cols) * cell.h;
542 struct gpf_glyph glyph = {};
543
544 if (compose && !compose_preview(code, &accent.glyph, &glyph)) {
545 gpf_cell_draw_glyph(p, ctx, &cell, x, y, code, &glyph);
546 free(glyph.bits);
547 continue;
548 }
549
550 gpf_cell_draw(p, ctx, &cell, x, y, code);
551 }
552
553 if (compose)
554 gpf_resolved_clear(&accent);
555
556 if (shown)
557 gpf_cell_grid(p, ctx, &cell, cols, (shown + cols - 1) / cols);
558}
559
560int gpf_deps_input(gp_event *ev)
561{
562 gp_pixmap *p = gp_widget_pixmap_get(gui.deps);
563 uint32_t codes[GPF_DEPS_MAX];
564 struct gpf_cell cell;
565 struct gpf_resolved res;
566 unsigned int cols, rows, cnt, idx;
567 int cell_idx;
568
569 if (!gui.font)
570 return 0;
571
572 if (ev->type == GP_EV_REL && ev->code == GP_EV_REL_WHEEL)
573 return deps_scroll(ev->val < 0 ? 1 : -1);
574
575 if (ev->type != GP_EV_KEY || ev->code != GP_EV_KEY_DOWN)
576 return 0;
577
578 if (ev->key.key != GP_BTN_LEFT && ev->key.key != GP_BTN_TOUCH)
579 return 0;
580
581 if (deps_grid(p, &cell, &cols, &rows))
582 return 0;
583
584 deps_list(codes, GPF_DEPS_MAX, &cnt, cols * rows);
585
586 cell_idx = gpf_cell_at(&cell, cols, rows, ev->st->cursor_x,
587 ev->st->cursor_y);
588 if (cell_idx < 0)
589 return 0;
590
591 idx = cell_idx;
592
593 if (idx >= GPF_MIN(cnt - gui.deps_off, cols * rows))
594 return 0;
595
596 idx += gui.deps_off;
597
598 /* the table keeps its page, it is not what was clicked */
599 gpf_gui_select_keep(codes[idx]);
600
601 /*
602 * An empty slot is a letter the font has not got, and clicking it is
603 * how it gets one: the glyph is created from the base and the accent.
604 */
605 if (gpf_resolve(gui.font, gui.variant, codes[idx], &res))
606 gpf_gui_create_glyph(codes[idx]);
607 else
608 gpf_resolved_clear(&res);
609
610 return 1;
611}
612
613int gpf_strip_input(gp_event *ev)
614{
615 gp_pixmap *p = gp_widget_pixmap_get(gui.strip);
616 unsigned int cell_w, id;
617
618 if (!gui.font || !p)
619 return 0;
620
621 if (ev->type == GP_EV_REL && ev->code == GP_EV_REL_WHEEL) {
622 gpf_gui_move_variant(ev->val);
623 return 1;
624 }
625
626 if (ev->type != GP_EV_KEY || ev->code != GP_EV_KEY_DOWN)
627 return 0;
628
629 switch (ev->key.key) {
630 case GP_BTN_LEFT:
631 case GP_BTN_TOUCH:
632 cell_w = GPF_MAX(1u, p->w / GPF_VARIANTS);
633 id = ev->st->cursor_x / cell_w;
634 if (id >= GPF_VARIANTS)
635 return 0;
636
637 gpf_gui_set_variant(id);
638
639 return 1;
640 break;
641 case GP_KEY_LEFT:
642 gpf_gui_move_variant(-1);
643 break;
644 case GP_KEY_RIGHT:
645 gpf_gui_move_variant(1);
646 break;
647 case GP_KEY_HOME:
648 gpf_gui_set_variant(0);
649 break;
650 case GP_KEY_END:
651 gpf_gui_set_variant(GPF_VARIANTS-1);
652 break;
653 }
654
655 return 0;
656}
A glyph cell rendering.
Accented letters: the decomposition table and accent composition.
const struct gpf_decomposition * gpf_decompose(uint32_t code)
Looks up the decomposition of an accented letter.
Definition compose.c:357
static int gpf_glyph_pixel(const struct gpf_glyph *self, unsigned int x, unsigned int y)
Returns a glyph ink pixel.
Definition font.h:623
#define GPF_MAX(a, b)
Returns the larger of two values, evaluates them twice.
Definition font.h:29
gpf_variant_id
Font face variant.
Definition font.h:34
@ GPF_BOLD_MONO
Bold monospace glyphs.
Definition font.h:40
@ GPF_VARIANTS
Number of font variants.
Definition font.h:44
@ GPF_MONO
Monospace glyphs, the base variant.
Definition font.h:36
@ GPF_BOLD
Bold glyphs.
Definition font.h:42
@ GPF_REGULAR
Proportional glyphs, "regular" on disk.
Definition font.h:38
#define GPF_MIN(a, b)
Returns the smaller of two values, evaluates them twice.
Definition font.h:27
The editor state and the functions everything in the GUI goes through.
const struct gpf_ucode_block * gpf_gui_block(void)
The block being browsed.
Definition gpforge.c:78
The preview, the variant strip and the accented letters strip.
void gpf_resolved_clear(struct gpf_resolved *self)
Clears the resolved glyph.
Definition resolve.c:30
int gpf_resolve(struct gpf_font *font, enum gpf_variant_id id, uint32_t code, struct gpf_resolved *res)
Resolves a glyph variant metric and ink.
Definition resolve.c:521
A glyph cell geometry.
Definition cell.h:29
gp_pixel bg
The color the cells are drawn on, the selected one is highlighted instead.
Definition cell.h:57
unsigned int w
The cell size in pixels.
Definition cell.h:31
A glyph decomposition.
Definition compose.h:40
enum gpf_accent_place place
Where the accent is placed in the glyph rectangle.
Definition compose.h:69
uint32_t code
A unicode code point of the accented letter.
Definition compose.h:42
A glyph.
Definition font.h:80
uint8_t * bits
Glyph ink.
Definition font.h:145
uint32_t accent
Accent to be combined with the base, GPF_GLYPH_COMPOSE only.
Definition font.h:154
uint32_t code
Unicode code point.
Definition font.h:84
A resolved glyph and where it was drawn from.
Definition resolve.h:36
A unicode block description.
The BMP part of unicode block table and the blocks of a font.