gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
gpforge.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5/*
6 * gpforge, a bitmap font editor.
7 */
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <ctype.h>
13#include <errno.h>
14#include <sys/stat.h>
15
16#include <gfxprim.h>
17
18#include "format.h"
19#include "format_bdf.h"
20#include "format_c.h"
21#include "edit.h"
22#include "compose.h"
23#include "lint.h"
24#include "gui.h"
25#include "canvas.h"
26#include "browser.h"
27#include "preview.h"
28#include "dialog_block.h"
29#include "dialog_family.h"
30#include "dialog_range.h"
31#include "dialog_save.h"
32
33struct gpf_gui gui;
34
35gp_app_info app_info = {
36 .name = "gpforge",
37 .desc = "A bitmap font editor",
38 .version = "0.1",
39 .license = "GPL-2.0-or-later",
40 .authors = (gp_app_info_author []) {
41 {.name = "Cyril Hrubis", .email = "metan@ucw.cz", .years = "2026"},
42 {}
43 }
44};
45
46const gp_font_face *gpf_gui_face(enum gpf_variant_id id)
47{
48 if (!gui.font || id >= GPF_VARIANTS)
49 return NULL;
50
51 if (!gui.faces[id])
52 gui.faces[id] = gpf_face_build(gui.font, id);
53
54 return gui.faces[id];
55}
56
58{
59 unsigned int i;
60
61 for (i = 0; i < GPF_VARIANTS; i++) {
62 gpf_face_free(gui.faces[i]);
63 gui.faces[i] = NULL;
64 }
65}
66
68{
69 if (!gui.font)
70 return NULL;
71
72 if (!gui.block_list)
73 gui.block_list = gpf_blocks_build(gui.font);
74
75 return gui.block_list;
76}
77
78const struct gpf_ucode_block *gpf_gui_block(void)
79{
80 const struct gpf_ucode_block *blocks = gpf_gui_blocks();
81
82 if (!blocks || gui.block >= gp_vec_len(blocks))
83 return NULL;
84
85 return &blocks[gui.block];
86}
87
88/*
89 * The metric spinners show the resolved glyph and edit it. They are the same
90 * edit as dragging the lines on the canvas, for when the number is what you
91 * know.
92 */
93static void update_metrics(void)
94{
95 struct gpf_variant *variant = gui.font ? gui.font->variants[gui.variant]
96 : NULL;
97 struct gpf_resolved res;
98 int advance = 0, bearing_x = 0, bearing_y = 0;
99
100 if (gui.font && !gpf_resolve(gui.font, gui.variant, gui.code, &res)) {
101 advance = res.glyph.advance;
102 bearing_x = res.glyph.bearing_x;
103 bearing_y = res.glyph.bearing_y;
104
105 gpf_resolved_clear(&res);
106 }
107
108 if (gui.advance_widget) {
109 gp_widget_int_val_set(gui.advance_widget, advance);
110
111 /* in a monospace variant the advance belongs to the variant */
112 gp_widget_disabled_set(gui.advance_widget,
113 variant && variant->spacing_mono);
114 }
115
116 if (gui.bearing_x_widget)
117 gp_widget_int_val_set(gui.bearing_x_widget, bearing_x);
118
119 if (gui.bearing_y_widget)
120 gp_widget_int_val_set(gui.bearing_y_widget, bearing_y);
121}
122
123/*
124 * How many errors and warnings the whole font has. Recomputed at every
125 * redraw that is not in the middle of a stroke, like everything else the lint
126 * says: a whole font is a few milliseconds, and a kept count would be wrong
127 * the moment a pixel moves.
128 */
129static void lint_count_set(gp_widget *label, const char *text)
130{
131 /* a label sized to its text relayouts on every set, so only on change */
132 if (label && strcmp(gp_widget_label_get(label), text))
133 gp_widget_label_set(label, text);
134}
135
136static void update_lint_count(void)
137{
138 unsigned int errors, warnings;
139 char buf[16];
140
141 if (!gui.font) {
142 lint_count_set(gui.lint_errors, "");
143 lint_count_set(gui.lint_warnings, "");
144 return;
145 }
146
147 gpf_lint_count(gui.font, &errors, &warnings);
148
149 snprintf(buf, sizeof(buf), "%u", errors);
150 lint_count_set(gui.lint_errors, buf);
151
152 snprintf(buf, sizeof(buf), "%u", warnings);
153 lint_count_set(gui.lint_warnings, buf);
154}
155
156static void set_lint_label(struct gpf_lint_finding *finding)
157{
158 if (!gui.lint_label)
159 return;
160
161 if (!finding) {
162 gp_widget_label_set(gui.lint_label, "");
163 gp_widget_label_colors_set(gui.lint_label, GP_WIDGETS_COL_TEXT, GP_WIDGETS_COL_BG);
164 return;
165 }
166
167 gp_widget_label_printf(gui.lint_label, " U+%04X %s: %s",
168 gui.code,
169 gpf_lint_severity_name(finding->sev),
170 finding->msg);
171
172 switch (finding->sev) {
173 case GPF_LINT_ERROR:
174 gp_widget_label_colors_set(gui.lint_label, GP_WIDGETS_COL_TEXT, GP_WIDGETS_COL_ALERT);
175 break;
176 case GPF_LINT_WARN:
177 gp_widget_label_colors_set(gui.lint_label, GP_WIDGETS_COL_TEXT, GP_WIDGETS_COL_WARN);
178 break;
179 }
180
181}
182
183static void update_status(void)
184{
185 struct gpf_resolved res;
186 const char *dirty = gui.dirty ? "*" : " ";
187 struct gpf_lint_finding finding;
188 char ch[5] = "";
189
190 if (!gui.status)
191 return;
192
193 if (!gui.font) {
194 update_metrics();
195 set_lint_label(NULL);
196 gp_widget_label_set(gui.status, "no font open");
197 return;
198 }
199
200 if (gpf_code_printable(gui.code))
201 ch[gp_to_utf8(gui.code, ch)] = 0;
202
203 update_metrics();
204
205 /* what is wrong with this glyph, on a line of its own so it can be read */
206 if (gui.lint_label) {
207 if (gpf_lint_glyph(gui.font, gui.variant, gui.code, &finding))
208 set_lint_label(NULL);
209 else
210 set_lint_label(&finding);
211 }
212
213 if (gpf_resolve(gui.font, gui.variant, gui.code, &res)) {
214 gp_widget_label_printf(gui.status, "%sU+%04X '%s' not in the font",
215 dirty, gui.code, ch);
216 return;
217 }
218
219 gp_widget_label_printf(gui.status, "%sU+%04X '%s' %s ink %ux%u",
220 dirty, gui.code, ch, gpf_provenance_name(res.prov),
221 res.glyph.width, res.glyph.height);
222
223 gpf_resolved_clear(&res);
224}
225
226/*
227 * The font being edited, or that there is none.
228 */
229static void update_family(void)
230{
231 if (!gui.family_label)
232 return;
233
234 if (!gui.font) {
235 gp_widget_label_set(gui.family_label, "no font");
236 return;
237 }
238
239 gp_widget_label_printf(gui.family_label, "%s %i",
240 gui.font->meta.family, gui.font->meta.size);
241}
242
243static void button_enable(gp_widget *self)
244{
245 if (self)
246 gp_widget_disabled_set(self, 0);
247}
248
249/*
250 * Disables a button, handing the focus over first when it is the one that has
251 * it.
252 *
253 * A widget disabled while focused keeps the focus: the library refuses to
254 * focus *out* of a disabled widget, so the flag stays set, the container goes
255 * on routing clicks to it and the click that should have gone elsewhere is
256 * swallowed. That is what broke edit, undo, redo, undo — the last undo was
257 * delivered to the redo button, which had just disabled itself. The focus
258 * goes to the button taking over, or to the canvas, which is where the editing
259 * is anyway.
260 */
261static void button_disable(gp_widget *self, gp_widget *next)
262{
263 if (!self || gp_widget_disabled_get(self))
264 return;
265
266 if (self->focused) {
267 if (next && !gp_widget_disabled_get(next))
268 gp_widget_focus_set(next);
269 else if (gui.canvas)
270 gp_widget_focus_set(gui.canvas);
271 }
272
273 gp_widget_disabled_set(self, 1);
274}
275
276/*
277 * The journal is what the undo and redo buttons are: nothing to take back on a
278 * font just opened, nothing to put back until something has been taken. Both
279 * are asked of the journal rather than tracked, since it is the thing that
280 * knows — and a disabled button that is already disabled costs nothing.
281 *
282 * Enabling comes first: the button that is about to be disabled hands its
283 * focus to the other one, which has to be usable by the time it is offered.
284 */
285static void update_undo(void)
286{
287 int undo = gpf_undo_can_undo(gui.undo);
288 int redo = gpf_undo_can_redo(gui.undo);
289
290 if (undo)
291 button_enable(gui.undo_button);
292
293 if (redo)
294 button_enable(gui.redo_button);
295
296 if (!undo)
297 button_disable(gui.undo_button, gui.redo_button);
298
299 if (!redo)
300 button_disable(gui.redo_button, gui.undo_button);
301}
302
303/*
304 * There is nothing to export when there is no font open. The font properties
305 * stay enabled: with no font they are how one is started.
306 */
307static void update_export(void)
308{
309 if (gui.font)
310 button_enable(gui.export_button);
311 else
312 button_disable(gui.export_button, gui.family_button);
313}
314
315/*
316 * The codepoint a block starts at, shown in hex next to the selector. Typing
317 * into it is how you get to a block, or a glyph, without spinning to it.
318 */
319static void update_block_start(void)
320{
321 const struct gpf_ucode_block *block = gpf_gui_block();
322
323 if (!gui.block_start || !block)
324 return;
325
326 gp_widget_tbox_printf(gui.block_start, "%04X", block->min);
327}
328
329/*
330 * Rebuilds the block list, keeping the position: the block that was being
331 * browsed stays selected, on the same page, if it is still on the list.
332 *
333 * The list is made from which glyphs the font has, so it is rebuilt when a
334 * glyph is added or removed, not on every edit.
335 */
336static void rebuild_blocks(void)
337{
338 const struct gpf_ucode_block *block = gpf_gui_block();
339 uint32_t min = block ? block->min : 0;
340 const struct gpf_ucode_block *blocks;
341 size_t i;
342
343 gp_vec_free(gui.block_list);
344 gui.block_list = NULL;
345
346 blocks = gpf_gui_blocks();
347 if (!blocks)
348 return;
349
350 gui.block = 0;
351
352 for (i = 0; i < gp_vec_len(blocks); i++) {
353 if (blocks[i].min <= min)
354 gui.block = i;
355 }
356
357 if (blocks[gui.block].min != min)
358 gui.browser_page = 0;
359
360 /* the coverage counts are in the selector */
361 if (gui.blocks)
362 gp_widget_redraw(gui.blocks);
363
364 update_block_start();
365}
366
367/*
368 * The glyph entry to edit in the current variant. Editing a glyph that is
369 * inherited, emboldened or composed materializes it here first, which is what
370 * creating an overlay is; a glyph the font does not have starts empty with the
371 * variant's advance.
372 */
373static struct gpf_glyph *edit_code(enum gpf_variant_id id, uint32_t code)
374{
375 struct gpf_variant *variant;
376 struct gpf_resolved res;
377 struct gpf_glyph *glyph;
378
379 if (!gui.font)
380 return NULL;
381
382 variant = gpf_font_variant(gui.font, id, 1);
383
384 if (!variant)
385 return NULL;
386
387 /*
388 * The journal follows the glyph that is about to change, which is not
389 * always the selected one: a pixel on a composed letter lands on one
390 * of its parts, and it is the part that is edited, marked as changed
391 * and taken back by undo.
392 */
393 gpf_undo_retarget(gui.undo, gui.font, id, code);
394
395 glyph = gpf_variant_glyph(variant, code);
396
397 if (glyph && glyph->kind == GPF_GLYPH_INK) {
398 /*
399 * Marked here and not when the journal entry is committed: the
400 * entry of the glyph being edited is still pending when the
401 * screen is painted, so a strip of letters edited in one go
402 * went red one cell at a time, the last of them a whole edit
403 * behind.
404 */
405 glyph->modified = 1;
406 return glyph;
407 }
408
409 if (gpf_resolve(gui.font, id, code, &res)) {
410 memset(&res, 0, sizeof(res));
411 res.glyph.advance = variant->advance ? variant->advance
412 : gui.font->meta.em;
413 }
414
415 glyph = gpf_variant_glyph_add(variant, code);
416 if (!glyph) {
417 gpf_resolved_clear(&res);
418 return NULL;
419 }
420
421 free(glyph->bits);
422
423 glyph->bits = res.glyph.bits;
424 glyph->width = res.glyph.width;
425 glyph->height = res.glyph.height;
426 glyph->advance = res.glyph.advance;
427 glyph->bearing_x = res.glyph.bearing_x;
428 glyph->bearing_y = res.glyph.bearing_y;
429 glyph->kind = GPF_GLYPH_INK;
430 glyph->has_advance = 1;
431 glyph->has_bearing = 1;
432
433 /* the ink was taken over, not copied */
434 res.glyph.bits = NULL;
435
436 rebuild_blocks();
437
438 glyph->modified = 1;
439
440 return glyph;
441}
442
443struct gpf_glyph *gpf_gui_edit_code(uint32_t code)
444{
445 return edit_code(gui.variant, code);
446}
447
449{
450 return gpf_gui_edit_code(gui.code);
451}
452
453/*
454 * The accented letters that follow the glyph being edited: the ones the font
455 * has, when the update box is ticked. Editing `Á` does not drag `À` along —
456 * only the letter they are both accented forms of does.
457 */
458unsigned int gpf_gui_accented(uint32_t *codes, unsigned int max)
459{
460 uint32_t all[256];
461 unsigned int i, n, cnt = 0;
462
463 if (!gui.update || !gui.font)
464 return 0;
465
466 n = gpf_decompose_accented(gui.code, all, GP_ARRAY_SIZE(all));
467
468 for (i = 0; i < n && cnt < max; i++) {
469 struct gpf_resolved res;
470
471 /* a letter the font has not got is not drawn by editing another */
472 if (gpf_resolve(gui.font, gui.variant, all[i], &res))
473 continue;
474
475 gpf_resolved_clear(&res);
476
477 codes[cnt++] = all[i];
478 }
479
480 return cnt;
481}
482
483/*
484 * A pixel of the glyph being edited, and the same pixel of every accented
485 * letter that follows it. A carbon copy: the letters keep their own accents,
486 * and the part of them that is the letter is the part that changes.
487 */
488void gpf_gui_pixel_set(int col, int height, int set)
489{
490 struct gpf_glyph *glyph = gpf_gui_edit_glyph();
491 uint32_t codes[64];
492 unsigned int i, n;
493
494 if (!glyph || gpf_glyph_pixel_set(glyph, col, height, set))
495 return;
496
497 n = gpf_gui_accented(codes, GP_ARRAY_SIZE(codes));
498
499 for (i = 0; i < n; i++) {
500 struct gpf_glyph *dep = gpf_gui_edit_code(codes[i]);
501
502 if (dep)
503 gpf_glyph_pixel_set(dep, col, height, set);
504 }
505
507}
508
509static int glyph_eq(const struct gpf_glyph *a, const struct gpf_glyph *b)
510{
511 if (a->width != b->width || a->height != b->height ||
512 a->advance != b->advance || a->bearing_x != b->bearing_x ||
513 a->bearing_y != b->bearing_y)
514 return 0;
515
516 if (!a->width || !a->height)
517 return 1;
518
519 return !memcmp(a->bits, b->bits, (size_t)a->width * a->height);
520}
521
522/*
523 * Whether what the variant would inherit is already the letter composed from
524 * its own parts, in which case there is nothing to store.
525 */
526static int inherits_same(enum gpf_variant_id id,
527 const struct gpf_decomposition *decomp,
528 const struct gpf_glyph *base,
529 const struct gpf_glyph *accent, int dx, int dy)
530{
531 struct gpf_glyph letter = {0};
532 struct gpf_resolved res;
533 int ret;
534
535 if (gpf_resolve(gui.font, id, decomp->code, &res))
536 return 0;
537
538 ret = !gpf_compose(&letter, base, accent, dx, dy) &&
539 glyph_eq(&letter, &res.glyph);
540
541 free(letter.bits);
542 gpf_resolved_clear(&res);
543
544 return ret;
545}
546
547/*
548 * The parent's ink in its own place is stored as the numbers block saving
549 * would turn it into, bold tells an ink entry in regular from a numbers one.
550 */
551static void store_as_saved(enum gpf_variant_id id, struct gpf_glyph *glyph)
552{
553 struct gpf_glyph numbers;
554
555 if (gpf_entry_form(gui.font, id, glyph, &numbers) != GPF_ENTRY_METRICS)
556 return;
557
558 free(glyph->bits);
559 numbers.code = glyph->code;
560 numbers.modified = 1;
561 *glyph = numbers;
562}
563
564/*
565 * The letter is drawn once, here, as ink: the base with the accent this
566 * font draws on it. From then on it is an ordinary glyph — nothing composes
567 * it again, and editing it edits it.
568 */
569static void create_glyph(enum gpf_variant_id id,
570 const struct gpf_decomposition *decomp, int derived)
571{
572 struct gpf_resolved base = {0};
573 struct gpf_glyph accent = {0};
574 struct gpf_glyph *glyph;
575 int dx = 0, dy = 0;
576
577 /* an accent above an i is drawn on the dotless letter */
578 if (gpf_draw_base(gui.font, id, decomp, &base))
579 return;
580
581 if (!gpf_accent_ink(gui.font, id, decomp, &accent)) {
582 gpf_accent_offset(gui.font, &base.glyph, &accent, decomp->place,
583 &dx, &dy);
584 }
585
586 if (derived &&
587 inherits_same(id, decomp, &base.glyph, &accent, dx, dy))
588 goto out;
589
590 glyph = edit_code(id, decomp->code);
591
592 /* with no accent to draw this copies the base, which gpf_compose() does */
593 if (glyph && !gpf_compose(glyph, &base.glyph, &accent, dx, dy) && derived)
594 store_as_saved(id, glyph);
595out:
596 gpf_resolved_clear(&base);
597 free(accent.bits);
598}
599
600static int derives_from(enum gpf_variant_id id, enum gpf_variant_id ancestor)
601{
602 while ((id = gpf_variant_parent(id)) != GPF_NO_PARENT) {
603 if (id == ancestor)
604 return 1;
605 }
606
607 return 0;
608}
609
610/*
611 * A variant that has its own entry for the base letter, ink or only numbers,
612 * draws the accented one from it too. Left to inherit, the letter would be
613 * the one drawn here, with none of the variant's base in it — not even where
614 * it sits. A variant that derives the base whole derives the letter the same
615 * way.
616 */
617static int has_own_base(enum gpf_variant_id id,
618 const struct gpf_decomposition *decomp)
619{
620 struct gpf_variant *variant = gui.font->variants[id];
621 struct gpf_glyph *entry;
622
623 if (!variant)
624 return 0;
625
626 /* a letter already drawn there is not drawn over */
627 entry = gpf_variant_glyph(variant, decomp->code);
628 if (entry && entry->kind != GPF_GLYPH_METRICS)
629 return 0;
630
631 /* with no dotless ı the variant's i is what it is drawn from */
632 return gpf_variant_glyph(variant, decomp->draw_base) ||
633 gpf_variant_glyph(variant, decomp->base_code);
634}
635
636/*
637 * The empty slots of the accented letters strip are the letters the font has
638 * not got, and clicking one is how a font gets them: which letters it covers
639 * is a decision, made one letter at a time.
640 */
641void gpf_gui_create_glyph(uint32_t code)
642{
643 const struct gpf_decomposition *decomp = gpf_decompose(code);
644 unsigned int id;
645
646 if (!gui.font || !decomp)
647 return;
648
650
651 create_glyph(gui.variant, decomp, 0);
652
653 /* parents come before their children, so each builds on what it inherits */
654 for (id = 0; id < GPF_VARIANTS; id++) {
655 if (derives_from(id, gui.variant) && has_own_base(id, decomp))
656 create_glyph(id, decomp, 1);
657 }
658
661}
662
663
665{
666 /* with no font open there is nothing to edit and nothing to journal */
667 if (!gui.font)
668 return;
669
670 /* an edit and whatever it drags along with it are one entry */
671 gpf_undo_group_begin(gui.undo);
672
673 gpf_undo_begin(gui.undo, gui.font, gui.variant, gui.code);
674}
675
677{
678 if (!gui.font)
679 return;
680
681 gpf_undo_commit(gui.undo, gui.font);
682
683 gpf_undo_group_end(gui.undo);
684
685 /*
686 * Which glyphs count as changed is decided by the journal, and the
687 * journal only knows at the commit — so the last word on the red
688 * codepoints is said here, after the strokes have stopped.
689 */
691}
692
693/*
694 * The font has changed, or it has just been written. The save button is the
695 * one thing that says which, so it follows this and nothing else.
696 */
697void gpf_gui_set_dirty(int dirty)
698{
699 gui.dirty = dirty;
700
701 if (dirty)
702 button_enable(gui.save_button);
703 else
704 button_disable(gui.save_button, gui.export_button);
705}
706
707/*
708 * Whether the glyph has changed since the font was written. The entry that
709 * says so is the one the ink comes from, which in a variant that inherits it
710 * is the parent's.
711 */
712int gpf_gui_modified(uint32_t code)
713{
714 struct gpf_resolved res;
715 struct gpf_variant *variant;
716 struct gpf_glyph *entry;
717
718 if (!gui.font)
719 return 0;
720
721 if (gpf_resolve(gui.font, gui.variant, code, &res))
722 return 0;
723
724 variant = gui.font->variants[res.ink_from];
725 entry = variant ? gpf_variant_glyph(variant, code) : NULL;
726
727 gpf_resolved_clear(&res);
728
729 return entry && entry->modified;
730}
731
733{
734 gpf_gui_set_dirty(1);
735
736 /* the compiled faces have the old glyph in them */
738
740}
741
742static void redraw(gp_widget *widget, void (*draw)(gp_widget *self))
743{
744 if (!widget)
745 return;
746
747 draw(widget);
748 gp_widget_redraw(widget);
749}
750
751/*
752 * While a stroke is being drawn only the canvas and the accented letters are
753 * repainted — that is what is being watched, and the letters have all been
754 * changed by then, in one go, before this is called.
755 *
756 * The glyph table, the variant strip and the sample text wait for the end of
757 * the stroke: they are not where the pixel is going, and the table paints a
758 * hundred cells while the sample text compiles the whole font into a face.
759 * Doing that for every pixel of a drag is what makes the drag feel like it is
760 * catching up with the mouse.
761 */
763{
764 /*
765 * Before the stroke test: the update copies commit as they go, so what
766 * the journal holds changes in the middle of a drag as well.
767 */
768 update_undo();
769
770 redraw(gui.canvas, gpf_canvas_draw);
771 redraw(gui.deps, gpf_deps_draw);
772
773 if (gui.in_stroke)
774 return;
775
776 redraw(gui.browser, gpf_browser_draw);
777 redraw(gui.strip, gpf_strip_draw);
778 redraw(gui.preview, gpf_preview_draw);
779
780 update_status();
781 update_lint_count();
782}
783
784/*
785 * Remembers the selection for the block, so that switching back to the block
786 * with the selector puts it back. A glyph picked out of the accented letters
787 * strip is often in another block, 'Č' picked while browsing 'C', and it is
788 * remembered for the block being browsed, next to the glyph the table has
789 * selected.
790 */
791static void remember_sel(uint32_t code, int follow)
792{
793 const struct gpf_ucode_block *blocks = gpf_gui_blocks();
794 struct gpf_block_sel sel;
795 size_t i;
796 int idx;
797
798 if (!blocks)
799 return;
800
801 idx = follow ? gpf_blocks_find(blocks, code) : (int)gui.block;
802 if (idx < 0 || (size_t)idx >= gp_vec_len(blocks))
803 return;
804
805 sel.min = blocks[idx].min;
806 sel.code = code;
807
808 /* a strip pick outside of the block keeps the table selection */
809 if (code >= blocks[idx].min && code <= blocks[idx].max)
810 sel.table = code;
811 else
812 sel.table = blocks[idx].min;
813
814 for (i = 0; i < gp_vec_len(gui.block_sels); i++) {
815 struct gpf_block_sel *cur = &gui.block_sels[i];
816
817 if (cur->min != sel.min)
818 continue;
819
820 if (sel.table != code)
821 sel.table = cur->table;
822
823 *cur = sel;
824 return;
825 }
826
827 if (!gui.block_sels)
828 gui.block_sels = gp_vec_new(0, sizeof(struct gpf_block_sel));
829
830 if (gui.block_sels)
831 GP_VEC_APPEND(gui.block_sels, sel);
832}
833
834/*
835 * Selects what was selected in a block last, or its first codepoint when
836 * nothing was. A remembered glyph that is out of the block, which was
837 * redefined since, does not count.
838 */
839static void restore_sel(const struct gpf_ucode_block *block)
840{
841 size_t i;
842
843 for (i = 0; i < gp_vec_len(gui.block_sels); i++) {
844 struct gpf_block_sel sel = gui.block_sels[i];
845
846 if (sel.min != block->min || sel.table > block->max)
847 continue;
848
850
851 if (sel.code != sel.table)
853
854 return;
855 }
856
857 gpf_gui_select(block->min);
858}
859
860static void select_code(uint32_t code, int follow)
861{
862 gui.code = code;
863
864 remember_sel(code, follow);
865
866 if (follow)
868
870}
871
872void gpf_gui_select(uint32_t code)
873{
874 select_code(code, 1);
875}
876
878{
879 select_code(code, 0);
880}
881
882void gpf_gui_set_block(unsigned int block)
883{
884 gui.block = block;
885 gui.browser_page = 0;
886
887 /* the selector shows the block, whoever changed it */
888 if (gui.blocks)
889 gp_widget_redraw(gui.blocks);
890
891 update_block_start();
892
894}
895
896/*
897 * Deletes every glyph in a range, in every variant, as one undo entry: it is
898 * one action to the user however many glyphs it touches.
899 */
900static void delete_range(uint32_t min, uint32_t max)
901{
902 unsigned int i;
903 uint32_t code;
904
905 gpf_undo_group_begin(gui.undo);
906
907 for (i = 0; i < GPF_VARIANTS; i++) {
908 struct gpf_variant *variant = gui.font->variants[i];
909
910 if (!variant)
911 continue;
912
913 for (code = min; code <= max; code++) {
914 if (!gpf_variant_glyph(variant, code))
915 continue;
916
917 gpf_undo_begin(gui.undo, gui.font, i, code);
918
919 gpf_variant_glyph_del(variant, code);
920
921 gpf_undo_commit(gui.undo, gui.font);
922 }
923 }
924
925 gpf_undo_group_end(gui.undo);
926
927 gpf_gui_set_dirty(1);
928
929 /* the compiled faces have the deleted glyphs in them */
931
932 rebuild_blocks();
933
935}
936
937/*
938 * The block selector shows where in the table we are, so it follows the
939 * paging. The ops read gui.block on every render, so a redraw is all it
940 * takes to show a new value.
941 */
942void gpf_gui_sync_block(unsigned int block)
943{
944 if (block == gui.block)
945 return;
946
947 gui.block = block;
948
949 if (gui.blocks)
950 gp_widget_redraw(gui.blocks);
951
952 update_block_start();
953
954 redraw(gui.preview, gpf_preview_draw);
955}
956
957void gpf_gui_set_variant(enum gpf_variant_id id)
958{
959 if (gui.variant == id)
960 return;
961
962 gui.variant = id;
963
965}
966
967void gpf_gui_move_variant(int offset)
968{
969 int new_variant = gui.variant;
970
971 new_variant += offset;
972
973 if (new_variant < 0)
974 new_variant = 0;
975
976 if (new_variant >= GPF_VARIANTS)
977 new_variant = GPF_VARIANTS-1;
978
979 gpf_gui_set_variant(new_variant);
980}
981
982void gpf_pixmap_resize(gp_widget_event *ev)
983{
984 gp_widget *self = ev->self;
985 gp_pixmap *pixmap = gp_pixmap_alloc(self->w, self->h, ev->ctx->pixel_type);
986
987 gp_pixmap_free(gp_widget_pixmap_set(self, pixmap));
988}
989
990static int pixmap_event(gp_widget_event *ev, void (*draw)(gp_widget *self))
991{
992 switch (ev->type) {
993 case GP_WIDGET_EVENT_RESIZE:
994 gpf_pixmap_resize(ev);
995 break;
996 case GP_WIDGET_EVENT_COLOR_SCHEME:
997 break;
998 /* the library redraws the widget right after this */
999 case GP_WIDGET_EVENT_FOCUS:
1000 break;
1001 default:
1002 return 0;
1003 }
1004
1005 draw(ev->self);
1006
1007 return 0;
1008}
1009
1010static int canvas_on_event(gp_widget_event *ev)
1011{
1012 if (ev->type == GP_WIDGET_EVENT_INPUT)
1013 return gpf_canvas_input(ev->input_ev);
1014
1015 return pixmap_event(ev, gpf_canvas_draw);
1016}
1017
1018static int update_on_event(gp_widget_event *ev)
1019{
1020 if (ev->type != GP_WIDGET_EVENT_WIDGET)
1021 return 0;
1022
1023 gui.update = gp_widget_bool_get(ev->self);
1024
1025 return 0;
1026}
1027
1028static int deps_on_event(gp_widget_event *ev)
1029{
1030 if (ev->type == GP_WIDGET_EVENT_INPUT)
1031 return gpf_deps_input(ev->input_ev);
1032
1033 return pixmap_event(ev, gpf_deps_draw);
1034}
1035
1036static int strip_on_event(gp_widget_event *ev)
1037{
1038 if (ev->type == GP_WIDGET_EVENT_INPUT)
1039 return gpf_strip_input(ev->input_ev);
1040
1041 return pixmap_event(ev, gpf_strip_draw);
1042}
1043
1044static int preview_on_event(gp_widget_event *ev)
1045{
1046 return pixmap_event(ev, gpf_preview_draw);
1047}
1048
1049/*
1050 * The preview repaints as the line is typed — that is what it is for, and the
1051 * face it draws with is already compiled, so a keystroke costs a repaint of
1052 * one pixmap and nothing else.
1053 */
1054static int preview_text_on_event(gp_widget_event *ev)
1055{
1056 if (ev->type == GP_WIDGET_EVENT_WIDGET)
1057 redraw(gui.preview, gpf_preview_draw);
1058
1059 return 0;
1060}
1061
1062/*
1063 * The three rules the preview draws over its text. They are a property of
1064 * the preview and not of the font, so they live here and not in the model.
1065 */
1066static int rule_on_event(gp_widget_event *ev, int *flag)
1067{
1068 if (ev->type != GP_WIDGET_EVENT_WIDGET)
1069 return 0;
1070
1071 *flag = gp_widget_bool_get(ev->self);
1072
1073 redraw(gui.preview, gpf_preview_draw);
1074
1075 return 0;
1076}
1077
1078static int rule_under_on_event(gp_widget_event *ev)
1079{
1080 return rule_on_event(ev, &gui.rule_underline);
1081}
1082
1083static int rule_strike_on_event(gp_widget_event *ev)
1084{
1085 return rule_on_event(ev, &gui.rule_strike);
1086}
1087
1088static int rule_over_on_event(gp_widget_event *ev)
1089{
1090 return rule_on_event(ev, &gui.rule_overline);
1091}
1092
1093static int browser_on_event(gp_widget_event *ev)
1094{
1095 if (ev->type == GP_WIDGET_EVENT_INPUT)
1096 return gpf_browser_input(ev->input_ev);
1097
1098 /*
1099 * The page is a number of pages into the block, and a resize changes
1100 * how many cells a page holds, so the old number is somewhere else
1101 * now — past the end of the block, even. Turn to the selection.
1102 */
1103 if (ev->type == GP_WIDGET_EVENT_RESIZE)
1104 gui.browser_follow = 1;
1105
1106 return pixmap_event(ev, gpf_browser_draw);
1107}
1108
1109/*
1110 * Jumps to a codepoint typed in hex: to the glyph if the block list has it,
1111 * and to the nearest block start otherwise.
1112 */
1113static int block_start_on_event(gp_widget_event *ev)
1114{
1115 const struct gpf_ucode_block *blocks = gpf_gui_blocks();
1116 unsigned long code;
1117 size_t i, found = 0;
1118 char *end;
1119
1120 switch (ev->type) {
1121 case GP_WIDGET_EVENT_NEW:
1122 gp_widget_tbox_filter_set(ev->self, GP_TBOX_FILTER_HEX);
1123 gp_widget_tbox_clear_on_input(ev->self);
1124 return 0;
1125 case GP_WIDGET_EVENT_WIDGET:
1126 if (ev->sub_type != GP_WIDGET_TBOX_TRIGGER)
1127 return 0;
1128 break;
1129 default:
1130 return 0;
1131 }
1132
1133 if (!blocks || !gp_vec_len(blocks))
1134 return 0;
1135
1136 code = strtoul(gp_widget_tbox_text(ev->self), &end, 16);
1137
1138 if (*end)
1139 goto out;
1140
1141 for (i = 0; i < gp_vec_len(blocks); i++) {
1142 if (blocks[i].min <= code)
1143 found = i;
1144 }
1145
1146 gpf_gui_set_block(found);
1147
1148 if (code >= blocks[found].min && code <= blocks[found].max)
1149 gpf_gui_select(code);
1150out:
1151 /* whatever was typed, the field goes back to showing the block */
1152 update_block_start();
1153
1154 return 0;
1155}
1156
1157/*
1158 * The pixel multiplier the glyphs are drawn with, so that the table stays
1159 * readable on a dense display.
1160 */
1161gp_pixel gpf_gui_back_color(gp_widget *self)
1162{
1163 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
1164
1165 if (self && self->focused)
1166 return gp_widgets_color(ctx, GP_WIDGETS_COL_FG);
1167
1168 return gp_widgets_color(ctx, GP_WIDGETS_COL_BG);
1169}
1170
1171/*
1172 * The canvas multiplier is fitted to the widget once and then it is the
1173 * user's, which is why it is set from the drawing as well as from the spinner.
1174 */
1175void gpf_gui_set_canvas_zoom(unsigned int zoom)
1176{
1177 gui.canvas_zoom = GPF_MAX(1u, zoom);
1178
1179 if (gui.canvas_zoom_widget) {
1180 gp_widget_int_val_set(gui.canvas_zoom_widget,
1181 gui.canvas_zoom);
1182 }
1183}
1184
1185static int canvas_zoom_on_event(gp_widget_event *ev)
1186{
1187 if (ev->type != GP_WIDGET_EVENT_WIDGET)
1188 return 0;
1189
1190 gui.canvas_zoom = GPF_MAX((int64_t)1, gp_widget_int_val_get(ev->self));
1191
1192 redraw(gui.canvas, gpf_canvas_draw);
1193
1194 return 0;
1195}
1196
1197static int advance_on_event(gp_widget_event *ev)
1198{
1199 struct gpf_glyph *glyph;
1200
1201 if (ev->type != GP_WIDGET_EVENT_WIDGET)
1202 return 0;
1203
1205
1206 glyph = gpf_gui_edit_glyph();
1207 if (!glyph)
1208 return 0;
1209
1210 glyph->advance = gp_widget_int_val_get(ev->self);
1211
1214
1215 return 0;
1216}
1217
1218/*
1219 * A bearing is where the ink sits, so setting one moves the ink.
1220 */
1221static int bearing_on_event(gp_widget_event *ev, int horiz)
1222{
1223 struct gpf_glyph *glyph;
1224 int val;
1225
1226 if (ev->type != GP_WIDGET_EVENT_WIDGET)
1227 return 0;
1228
1229 val = gp_widget_int_val_get(ev->self);
1230
1232
1233 glyph = gpf_gui_edit_glyph();
1234 if (!glyph)
1235 return 0;
1236
1237 if (horiz)
1238 gpf_glyph_shift(glyph, val - glyph->bearing_x, 0);
1239 else
1240 gpf_glyph_shift(glyph, 0, val - glyph->bearing_y);
1241
1244
1245 return 0;
1246}
1247
1248static int bearing_x_on_event(gp_widget_event *ev)
1249{
1250 return bearing_on_event(ev, 1);
1251}
1252
1253static int bearing_y_on_event(gp_widget_event *ev)
1254{
1255 return bearing_on_event(ev, 0);
1256}
1257
1258static int zoom_on_event(gp_widget_event *ev)
1259{
1260 if (ev->type != GP_WIDGET_EVENT_WIDGET)
1261 return 0;
1262
1263 gui.zoom = GPF_MAX((int64_t)1, gp_widget_int_val_get(ev->self));
1264
1265 /* the page holds a different number of cells now */
1266 gui.browser_follow = 1;
1267
1269
1270 return 0;
1271}
1272
1273static int blocks_on_event(gp_widget_event *ev)
1274{
1275 const struct gpf_ucode_block *block;
1276
1277 if (ev->type != GP_WIDGET_EVENT_WIDGET)
1278 return 0;
1279
1280 gpf_gui_set_block(gp_widget_choice_sel_get(ev->self));
1281
1282 /* back to where we were in the block, or to its start */
1283 block = gpf_gui_block();
1284 if (block)
1285 restore_sel(block);
1286
1287 return 0;
1288}
1289
1290/*
1291 * The block list carries a coverage count, which is also the answer to
1292 * whether a block is complete enough to export.
1293 */
1294/*
1295 * A part of a block, rather than all of it.
1296 */
1297static int glyph_range_del_on_event(gp_widget_event *ev)
1298{
1299 const struct gpf_ucode_block *block = gpf_gui_block();
1300 uint32_t min, max;
1301 unsigned int cnt;
1302
1303 if (ev->type != GP_WIDGET_EVENT_WIDGET || !block)
1304 return 0;
1305
1306 min = block->min;
1307 max = block->max;
1308
1309 /*
1310 * The dialog is the confirmation — its button says Delete — and a
1311 * second one would have to be opened from inside this one, which the
1312 * widget library does not survive.
1313 */
1314 if (gpf_dialog_range("Delete glyphs", &min, &max))
1315 return 0;
1316
1317 cnt = gpf_font_range_glyphs(gui.font, min, max);
1318
1319 if (!cnt)
1320 return 0;
1321
1322 delete_range(min, max);
1323
1324 return 0;
1325}
1326
1327/*
1328 * The letter in every variant, not just the one being edited: the glyph
1329 * delete button drops the overlay of one variant and the letter falls back to
1330 * what the others have, this is what gets rid of it.
1331 */
1332static int glyph_clear_on_event(gp_widget_event *ev)
1333{
1334 if (ev->type != GP_WIDGET_EVENT_WIDGET || !gui.font)
1335 return 0;
1336
1337 if (!gpf_font_range_glyphs(gui.font, gui.code, gui.code))
1338 return 0;
1339
1340 delete_range(gui.code, gui.code);
1341
1342 return 0;
1343}
1344
1345static const char *blocks_get_choice(gp_widget *self, size_t idx)
1346{
1347 static char bufs[4][64];
1348 static unsigned int cur;
1349 char *buf = bufs[cur++ % 4];
1350 const struct gpf_ucode_block *blocks = gpf_gui_blocks();
1351
1352 (void)self;
1353
1354 if (!blocks || idx >= gp_vec_len(blocks))
1355 return "";
1356
1357 snprintf(buf, 64, "%s %u/%u", blocks[idx].name, blocks[idx].coverage,
1358 gpf_block_glyphs(&blocks[idx]));
1359
1360 return buf;
1361}
1362
1363static size_t blocks_get(gp_widget *self, enum gp_widget_choice_op op)
1364{
1365 (void)self;
1366
1367 switch (op) {
1368 case GP_WIDGET_CHOICE_OP_SEL:
1369 return gui.block;
1370 case GP_WIDGET_CHOICE_OP_CNT:
1371 return gp_vec_len(gpf_gui_blocks());
1372 }
1373
1374 return 0;
1375}
1376
1377static void blocks_set(gp_widget *self, size_t sel)
1378{
1379 (void)self;
1380
1381 gui.block = sel;
1382}
1383
1384static const gp_widget_choice_ops blocks_ops = {
1385 .get_choice = blocks_get_choice,
1386 .get = blocks_get,
1387 .set = blocks_set,
1388};
1389
1390static const gp_widget_choice_desc blocks_desc = {
1391 .ops = &blocks_ops,
1392};
1393
1394/*
1395 * Adding a block is picking one the font does not have and declaring it: the
1396 * declaration is what keeps it on the list while it is still empty, which is
1397 * what starting a new script looks like.
1398 */
1399static int block_add_on_event(gp_widget_event *ev)
1400{
1401 const struct gpf_ucode_block *picked;
1402 char name[GPF_NAME_MAX];
1403 uint32_t min, max;
1404 int idx;
1405
1406 if (ev->type != GP_WIDGET_EVENT_WIDGET)
1407 return 0;
1408
1409 if (!gui.font)
1410 return 0;
1411
1412 picked = gpf_dialog_block_add();
1413 if (!picked)
1414 return 0;
1415
1416 /* the pick points into a list that the rebuild below throws away */
1417 min = picked->min;
1418 max = picked->max;
1419 snprintf(name, sizeof(name), "%s", picked->name);
1420
1421 if (gpf_font_block_add(gui.font, min, max, name))
1422 return 0;
1423
1424 rebuild_blocks();
1425
1427
1428 if (idx >= 0)
1429 gpf_gui_set_block(idx);
1430
1431 return 0;
1432}
1433
1434/*
1435 * Removing a block removes its glyphs with it, which is the only thing "remove
1436 * this block" can mean for a block that has any: the block is on the list
1437 * because of them.
1438 */
1439static int block_del_on_event(gp_widget_event *ev)
1440{
1441 const struct gpf_ucode_block *block = gpf_gui_block();
1442 uint32_t min, max;
1443
1444 if (ev->type != GP_WIDGET_EVENT_WIDGET || !block)
1445 return 0;
1446
1447 min = block->min;
1448 max = block->max;
1449
1450 if (block->coverage) {
1451 if (gp_dialog_msg_printf_run(GP_DIALOG_MSG_QUESTION,
1452 "Remove block",
1453 "Delete all %u glyphs in %s?",
1454 block->coverage,
1455 block->name) != GP_DIALOG_YES)
1456 return 0;
1457
1458 delete_range(min, max);
1459 }
1460
1461 gpf_font_block_del(gui.font, min);
1462
1463 rebuild_blocks();
1464
1465 if (gui.code >= min && gui.code <= max) {
1466 const struct gpf_ucode_block *cur = gpf_gui_block();
1467
1468 if (cur)
1469 gpf_gui_select(cur->min);
1470 }
1471
1473
1474 return 0;
1475}
1476
1477static int is_dir(const char *path)
1478{
1479 struct stat st;
1480
1481 if (stat(path, &st))
1482 return 0;
1483
1484 return S_ISDIR(st.st_mode);
1485}
1486
1487static struct gpf_font *load(const char *path, char *err, size_t err_len)
1488{
1489 if (is_dir(path))
1490 return gpf_font_read(path, err, err_len);
1491
1492 return gpf_bdf_read(path, err, err_len);
1493}
1494
1495/*
1496 * Puts a font in the editor, throwing away everything that was derived from
1497 * the previous one. The undo journal goes with it: it holds glyphs of a font
1498 * that is no longer here.
1499 */
1500static void font_set(struct gpf_font *font, const char *path, char *save_path)
1501{
1503
1504 gp_vec_free(gui.block_list);
1505 gui.block_list = NULL;
1506
1507 gpf_font_free(gui.font);
1508 gpf_undo_free(gui.undo);
1509 free(gui.save_path);
1510
1511 gui.font = font;
1512 gui.path = path;
1513 gui.save_path = save_path;
1514 gui.undo = gpf_undo_new();
1515
1516 gpf_gui_set_dirty(0);
1517
1518 /* the selections were in the blocks of the previous font */
1519 gp_vec_free(gui.block_sels);
1520 gui.block_sels = NULL;
1521
1522 gui.variant = GPF_MONO;
1523 gui.code = 'A';
1524 gui.block = 0;
1525 gui.browser_page = 0;
1526 gui.browser_follow = 1;
1527 gui.canvas_zoom = 0;
1528
1529 if (gui.blocks)
1530 gp_widget_redraw(gui.blocks);
1531
1532 update_family();
1533
1534 /*
1535 * The block selector was laid out for whatever font was open before,
1536 * or for none at all, and its width comes from the longest block name
1537 * in it. Ask for the resize or the first block opened into an empty
1538 * editor is shown clipped.
1539 */
1540 if (gui.blocks)
1541 gp_widget_resize(gui.blocks);
1542
1543 update_block_start();
1544 update_export();
1546}
1547
1548static void save(void);
1549
1550/*
1551 * Editing with no way back is not worth a keystroke, so anything that would
1552 * lose the journal asks first — and the answer to unsaved work is usually to
1553 * save it, which is why this is not a yes or no question.
1554 */
1555static int confirm_discard(const char *title)
1556{
1557 if (!gui.dirty)
1558 return 1;
1559
1560 switch (gpf_dialog_save(title)) {
1561 case GPF_SAVE_SAVE:
1562 save();
1563 /* a save that was cancelled cancels what asked for it */
1564 return !gui.dirty;
1565 case GPF_SAVE_DISCARD:
1566 return 1;
1567 default:
1568 return 0;
1569 }
1570}
1571
1572static void open_font(void)
1573{
1574 gp_dialog_file_opts opts = {
1575 .flags = GP_DIALOG_OPEN_FILE | GP_DIALOG_OPEN_DIR,
1576 };
1577 char err[GPF_ERR_MAX] = "";
1578 struct gpf_font *font;
1579 gp_dialog *dialog;
1580 const char *picked;
1581 char *path;
1582
1583 if (!confirm_discard("Open"))
1584 return;
1585
1586 dialog = gp_dialog_file_open_new(NULL, &opts);
1587 if (!dialog)
1588 return;
1589
1590 if (gp_dialog_run(dialog) != GP_WIDGET_DIALOG_PATH) {
1591 gp_dialog_free(dialog);
1592 return;
1593 }
1594
1595 picked = gp_dialog_file_path(dialog);
1596 path = picked ? strdup(picked) : NULL;
1597
1598 gp_dialog_free(dialog);
1599
1600 if (!path)
1601 return;
1602
1603 font = load(path, err, sizeof(err));
1604 if (!font) {
1605 gp_dialog_msg_printf_run(GP_DIALOG_MSG_ERR, "Open", "%s", err);
1606 free(path);
1607 return;
1608 }
1609
1610 /* a directory is its own save target, a BDF is not */
1611 font_set(font, path, is_dir(path) ? strdup(path) : NULL);
1612}
1613
1614/*
1615 * A new font is started from the font properties dialog: the properties are
1616 * the ones it starts from and accepting them creates it. It has no directory
1617 * yet, so the first save asks for one.
1618 */
1619static void create_font(void)
1620{
1621 struct gpf_family_meta meta;
1622 struct gpf_author *authors = NULL;
1623 struct gpf_font *font;
1624 int advance = 8;
1625
1626 gpf_family_meta_guess(&meta, NULL, 16, 12, 4, advance);
1627
1628 if (gpf_dialog_family(&meta, &authors, &advance)) {
1629 gp_vec_free(authors);
1630 return;
1631 }
1632
1633 font = gpf_font_create(&meta, advance);
1634 if (!font) {
1635 gp_vec_free(authors);
1636 return;
1637 }
1638
1639 font->authors = authors;
1640
1641 font_set(font, "", NULL);
1642}
1643
1644static void new_font(void)
1645{
1646 if (!confirm_discard("New font"))
1647 return;
1648
1649 create_font();
1650}
1651
1652static void quit(void)
1653{
1654 if (!confirm_discard("Quit"))
1655 return;
1656
1657 /*
1658 * Asked and answered — the exit path asks again otherwise, since it
1659 * cannot tell this from the window being closed behind our back.
1660 */
1661 gpf_gui_set_dirty(0);
1662
1663 gp_widgets_exit(0);
1664}
1665
1666/*
1667 * A dialog opened while the window is being closed has no event coming: the
1668 * dialog loop waits before it draws, and the click that closed the window went
1669 * to the window manager. One timer that does nothing is enough to make the
1670 * loop come round once and paint.
1671 */
1672static uint32_t wake_up(gp_timer *self)
1673{
1674 (void)self;
1675
1676 return GP_TIMER_STOP;
1677}
1678
1679static GP_TIMER_DECLARE(wake_timer, 10, 0, "wake", wake_up, NULL);
1680
1681/*
1682 * The window manager's close button and the quit key are handled inside the
1683 * widget library, which exits without asking the application whether it may:
1684 * the FREE event arrives once the decision is made, with the backend still up.
1685 * So this is the last chance to offer to save the work, and there is nothing
1686 * left to cancel with — the dialog would be lying if it offered it.
1687 */
1688static void save_on_exit(void)
1689{
1690 static int asked;
1691
1692 if (!gui.dirty || asked)
1693 return;
1694
1695 asked = 1;
1696
1697 gp_app_timer_start(&wake_timer);
1698
1699 if (gp_dialog_msg_run(GP_DIALOG_MSG_QUESTION, "Closing",
1700 "The font has unsaved changes, save them?")
1701 == GP_DIALOG_YES)
1702 save();
1703}
1704
1705/*
1706 * Puts the editor where an undone edit happened, so that taking something back
1707 * shows what came back.
1708 */
1709static void goto_edit(enum gpf_variant_id id, uint32_t code)
1710{
1711 int block;
1712
1713 gui.variant = id;
1714
1715 /* the glyph may have come back, or gone, and its block with it */
1716 rebuild_blocks();
1717
1718 block = gpf_blocks_find(gpf_gui_blocks(), code);
1719
1720 if (block >= 0)
1721 gpf_gui_set_block(block);
1722
1724 gpf_gui_select(code);
1725}
1726
1727/*
1728 * The family metadata changed. Every painter reads the metrics from the font
1729 * at each paint, so the faces are all that has to be thrown away — but the
1730 * table cells are sized from the ascent and the descent, so a page holds a
1731 * different number of them and the table has to turn to the selection again.
1732 */
1733static void family_changed(void)
1734{
1735 update_family();
1736
1737 gui.browser_follow = 1;
1738
1740}
1741
1742static int undo_redo(int redo)
1743{
1744 enum gpf_undo_what what;
1745 enum gpf_variant_id id;
1746 uint32_t code;
1747
1748 if (!gui.font)
1749 return 0;
1750
1751 if (redo)
1752 what = gpf_redo(gui.undo, gui.font, &id, &code);
1753 else
1754 what = gpf_undo(gui.undo, gui.font, &id, &code);
1755
1756 switch (what) {
1757 case GPF_UNDO_NONE:
1758 return 0;
1759 case GPF_UNDO_GLYPH:
1760 goto_edit(id, code);
1761 break;
1762 /* the metrics are nowhere in particular, so the editor stays put */
1763 case GPF_UNDO_FAMILY:
1764 family_changed();
1765 break;
1766 }
1767
1768 /* back where the file was written there is nothing left to save */
1769 gpf_gui_set_dirty(!gpf_undo_is_saved(gui.undo));
1770
1771 return 1;
1772}
1773
1774/*
1775 * The family metadata and the authors, edited in a dialog as a copy and set as
1776 * one entry of the journal.
1777 */
1778static void edit_family(void)
1779{
1780 struct gpf_family_meta meta;
1781 struct gpf_author *authors;
1782
1783 /* with no font open there is nothing to lose, so nothing is asked */
1784 if (!gui.font) {
1785 create_font();
1786 return;
1787 }
1788
1789 meta = gui.font->meta;
1790
1791 authors = gpf_authors_dup(gui.font->authors);
1792 if (!authors)
1793 return;
1794
1795 if (gpf_dialog_family(&meta, &authors, NULL))
1796 goto out;
1797
1798 if (gpf_family_meta_same(&meta, &gui.font->meta) &&
1799 gpf_authors_same(authors, gui.font->authors))
1800 goto out;
1801
1802 if (gpf_undo_family(gui.undo, gui.font, &meta, authors))
1803 goto out;
1804
1805 family_changed();
1806out:
1807 gp_vec_free(authors);
1808}
1809
1810static void save(void)
1811{
1812 char err[GPF_ERR_MAX] = "";
1813 const char *path;
1814
1815 if (!gui.font)
1816 return;
1817
1818 path = gui.save_path ? gui.save_path : gui.path;
1819
1820 /*
1821 * A font imported from a BDF has no directory of its own yet, so the
1822 * first save asks for one.
1823 */
1824 if (!is_dir(path)) {
1825 gp_dialog *dialog = gp_dialog_file_save_new(NULL, NULL);
1826 const char *picked;
1827
1828 if (!dialog)
1829 return;
1830
1831 if (gp_dialog_run(dialog) != GP_WIDGET_DIALOG_PATH) {
1832 gp_dialog_free(dialog);
1833 return;
1834 }
1835
1836 picked = gp_dialog_file_path(dialog);
1837
1838 free(gui.save_path);
1839 gui.save_path = picked ? strdup(picked) : NULL;
1840
1841 gp_dialog_free(dialog);
1842
1843 if (!gui.save_path)
1844 return;
1845
1846 path = gui.save_path;
1847 }
1848
1849 if (gpf_font_write(gui.font, path, err, sizeof(err))) {
1850 gp_dialog_msg_printf_run(GP_DIALOG_MSG_ERR, "Save", "%s", err);
1851 return;
1852 }
1853
1854 /* the file has the smallest form, so the editor should show it too */
1855 gpf_font_simplify(gui.font);
1856
1857 /* the overlays it dropped may have been the only glyphs in a block */
1858 rebuild_blocks();
1859
1860 gpf_font_clear_modified(gui.font);
1861 gpf_undo_saved(gui.undo);
1862
1863 gpf_gui_set_dirty(0);
1864
1866}
1867
1868/*
1869 * The C export names the symbols and the family after the file it is written
1870 * to, the way `gpforge-cli c` takes them as arguments: haxor-narrow-18.c is
1871 * the family gfxprim looks up as "haxor-narrow-18", and its symbols are
1872 * font_family_haxor_narrow_18 and friends.
1873 */
1874static void export_names(const char *path, char *name, size_t name_len,
1875 char *id, size_t id_len)
1876{
1877 const char *base = strrchr(path, '/');
1878 const char *dot;
1879 size_t i;
1880
1881 base = base ? base + 1 : path;
1882 dot = strrchr(base, '.');
1883
1884 snprintf(name, name_len, "%.*s",
1885 (int)(dot ? (size_t)(dot - base) : strlen(base)), base);
1886
1887 snprintf(id, id_len, "%s", name);
1888
1889 /* the id is a C identifier, which a family name is not */
1890 for (i = 0; id[i]; i++) {
1891 if (!isalnum((unsigned char)id[i]))
1892 id[i] = '_';
1893 }
1894}
1895
1896/*
1897 * Export, the one thing the editor writes that is not the font: the gfxprim C
1898 * font, or a BDF for what still wants one. Which of the two is the name the
1899 * file is saved under — `.c` is the C font and anything else is a BDF — and
1900 * not a second question, because a handler that runs two dialogs comes back
1901 * with the layout in pieces. That is also why a name the rule cannot answer
1902 * does not exist: refusing one would be a second dialog for a typo.
1903 *
1904 * A BDF holds one face, and the face it holds is the variant being edited:
1905 * the variant strip is where that is chosen, here as everywhere else. The C
1906 * font is the whole family and asks nothing.
1907 */
1908static void export_font(void)
1909{
1910 char err[GPF_ERR_MAX] = "";
1911 gp_dialog *dialog;
1912 const char *picked, *base, *ext;
1913 char *path;
1914 FILE *f;
1915 int ret;
1916
1917 if (!gui.font)
1918 return;
1919
1920 dialog = gp_dialog_file_save_new(NULL, NULL);
1921 if (!dialog)
1922 return;
1923
1924 if (gp_dialog_run(dialog) != GP_WIDGET_DIALOG_PATH) {
1925 gp_dialog_free(dialog);
1926 return;
1927 }
1928
1929 picked = gp_dialog_file_path(dialog);
1930 path = picked ? strdup(picked) : NULL;
1931
1932 gp_dialog_free(dialog);
1933
1934 if (!path)
1935 return;
1936
1937 /* a dot in a directory name is not an extension */
1938 base = strrchr(path, '/');
1939 ext = strrchr(base ? base + 1 : path, '.');
1940
1941 f = fopen(path, "w");
1942 if (!f) {
1943 gp_dialog_msg_printf_run(GP_DIALOG_MSG_ERR, "Export", "%s: %s",
1944 path, strerror(errno));
1945 free(path);
1946 return;
1947 }
1948
1949 if (ext && !strcmp(ext, ".c")) {
1950 char name[GPF_NAME_MAX], id[GPF_NAME_MAX];
1951
1952 export_names(path, name, sizeof(name), id, sizeof(id));
1953
1954 ret = gpf_export_c(gui.font, id, name, f, err, sizeof(err));
1955 } else {
1956 ret = gpf_bdf_write(gui.font, gui.variant, f, err, sizeof(err));
1957 }
1958
1959 if (fclose(f) && !ret) {
1960 snprintf(err, sizeof(err), "%s: %s", path, strerror(errno));
1961 ret = 1;
1962 }
1963
1964 if (ret)
1965 gp_dialog_msg_printf_run(GP_DIALOG_MSG_ERR, "Export", "%s", err);
1966
1967 free(path);
1968}
1969
1970/*
1971 * Reverting drops the entry in this variant, which is what an overlay is: the
1972 * glyph goes back to whatever the lattice resolves it to.
1973 */
1974static void revert(void)
1975{
1976 struct gpf_variant *variant;
1977
1978 if (!gui.font)
1979 return;
1980
1981 variant = gui.font->variants[gui.variant];
1982
1983 if (!variant || !gpf_variant_glyph(variant, gui.code))
1984 return;
1985
1987
1988 gpf_variant_glyph_del(variant, gui.code);
1989
1990 rebuild_blocks();
1991
1994}
1995
1996void gpf_gui_shift(int dx, int dy)
1997{
1998 struct gpf_glyph *glyph;
1999 uint32_t codes[64];
2000 unsigned int i, n;
2001
2002 if (!gui.font)
2003 return;
2004
2006
2007 glyph = gpf_gui_edit_glyph();
2008
2009 if (!glyph) {
2011 return;
2012 }
2013
2014 gpf_glyph_shift(glyph, dx, dy);
2015
2016 /*
2017 * The letter moved, so its accented forms move with it — the accent
2018 * sits on the letter and goes where it goes.
2019 */
2020 n = gpf_gui_accented(codes, GP_ARRAY_SIZE(codes));
2021
2022 for (i = 0; i < n; i++) {
2023 struct gpf_glyph *dep = gpf_gui_edit_code(codes[i]);
2024
2025 if (dep)
2026 gpf_glyph_shift(dep, dx, dy);
2027 }
2028
2031}
2032
2033/*
2034 * The selected glyph as the variant resolves it, so an inherited or a derived
2035 * glyph is copied as it is seen. A glyph the font has not got is nothing to
2036 * copy, and the clipboard is left as it was.
2037 */
2038static void copy(void)
2039{
2040 struct gpf_resolved res;
2041 struct gpf_glyph *clip = &gui.clipboard;
2042
2043 if (!gui.font || gpf_resolve(gui.font, gui.variant, gui.code, &res))
2044 return;
2045
2046 if (gpf_glyph_set_bits(clip, res.glyph.bits, res.glyph.width,
2047 res.glyph.height, res.glyph.bearing_x,
2048 res.glyph.bearing_y)) {
2049 gpf_resolved_clear(&res);
2050 return;
2051 }
2052
2053 clip->advance = res.glyph.advance;
2054 gui.clipboard_full = 1;
2055
2056 gpf_resolved_clear(&res);
2057
2058 button_enable(gui.paste_button);
2059}
2060
2061/*
2062 * Replaces the selected glyph with the copied one, as one undo entry.
2063 *
2064 * The accented forms do not follow even with the update box ticked: a whole
2065 * glyph pasted into them would take their accents away. A monospace variant
2066 * keeps its advance, which is not a glyph property there.
2067 */
2068static void paste(void)
2069{
2070 const struct gpf_glyph *clip = &gui.clipboard;
2071 struct gpf_glyph *glyph;
2072 int mono;
2073
2074 if (!gui.font || !gui.clipboard_full)
2075 return;
2076
2077 mono = gui.font->variants[gui.variant] &&
2078 gui.font->variants[gui.variant]->spacing_mono;
2079
2081
2082 glyph = gpf_gui_edit_glyph();
2083
2084 if (glyph && !gpf_glyph_set_bits(glyph, clip->bits, clip->width,
2085 clip->height, clip->bearing_x,
2086 clip->bearing_y)) {
2087 if (!mono)
2088 glyph->advance = clip->advance;
2089
2091 }
2092
2094}
2095
2096/*
2097 * The next glyph the lint has something to say about, in whatever variant it
2098 * is in: the report is a list you walk rather than a list you read, and one
2099 * button is the whole of it. The findings are recomputed on every press, so
2100 * a glyph that has just been fixed is not offered again.
2101 */
2102static void lint_jump(void)
2103{
2104 struct gpf_lint_finding *findings;
2105 size_t i, next = 0;
2106 int found = 0;
2107
2108 if (!gui.font)
2109 return;
2110
2111 findings = gpf_lint(gui.font);
2112 if (!findings)
2113 return;
2114
2115 for (i = 0; i < gp_vec_len(findings); i++) {
2116 if (findings[i].id < gui.variant)
2117 continue;
2118
2119 if (findings[i].id == gui.variant && findings[i].code <= gui.code)
2120 continue;
2121
2122 next = i;
2123 found = 1;
2124 break;
2125 }
2126
2127 /* past the last one, round to the first */
2128 if (!found && gp_vec_len(findings))
2129 found = 1;
2130
2131 if (!found) {
2132 if (gui.lint_label)
2133 gp_widget_label_set(gui.lint_label,
2134 "nothing for the lint to say");
2135
2136 gp_vec_free(findings);
2137 return;
2138 }
2139
2140 gui.variant = findings[next].id;
2141
2142 gpf_gui_select(findings[next].code);
2143
2144 gp_vec_free(findings);
2145}
2146
2147static void glyph_command(int key)
2148{
2149 struct gpf_glyph *glyph;
2150 int mono;
2151
2152 if (!gui.font)
2153 return;
2154
2155 mono = gui.font->variants[gui.variant] &&
2156 gui.font->variants[gui.variant]->spacing_mono;
2157
2158 if ((key == GP_KEY_LEFT_BRACE || key == GP_KEY_RIGHT_BRACE) && mono) {
2159 gp_dialog_msg_run(GP_DIALOG_MSG_INFO, "Advance",
2160 "A monospace variant has one advance for the "
2161 "whole font, it is not a glyph property.");
2162 return;
2163 }
2164
2166
2167 glyph = gpf_gui_edit_glyph();
2168 if (!glyph)
2169 return;
2170
2171 switch (key) {
2172 case GP_KEY_DELETE:
2173 gpf_glyph_clear_ink(glyph);
2174 break;
2175 case GP_KEY_X:
2176 gpf_glyph_flip_h(glyph);
2177 break;
2178 case GP_KEY_Y:
2179 gpf_glyph_flip_v(glyph);
2180 break;
2181 case GP_KEY_LEFT_BRACE:
2182 if (glyph->advance > 0)
2183 glyph->advance--;
2184 break;
2185 case GP_KEY_RIGHT_BRACE:
2186 glyph->advance++;
2187 break;
2188 }
2189
2192}
2193
2194/*
2195 * Input the widgets did not take, which is where the editor's own keys live.
2196 */
2197static int app_on_event(gp_widget_event *ev)
2198{
2199 gp_event *input;
2200 int ctrl, shift;
2201
2202 if (ev->type == GP_WIDGET_EVENT_FREE) {
2203 save_on_exit();
2204 return 0;
2205 }
2206
2207 if (ev->type != GP_WIDGET_EVENT_INPUT)
2208 return 0;
2209
2210 input = ev->input_ev;
2211
2212 if (input->type != GP_EV_KEY || input->code != GP_EV_KEY_DOWN)
2213 return 0;
2214
2215 ctrl = gp_ev_any_key_pressed(input, GP_KEY_LEFT_CTRL, GP_KEY_RIGHT_CTRL);
2216 shift = gp_ev_any_key_pressed(input, GP_KEY_LEFT_SHIFT, GP_KEY_RIGHT_SHIFT);
2217
2218 if (ctrl) {
2219 switch (input->key.key) {
2220 case GP_KEY_Z:
2221 return undo_redo(shift);
2222 case GP_KEY_S:
2223 save();
2224 return 1;
2225 case GP_KEY_O:
2226 open_font();
2227 return 1;
2228 case GP_KEY_P:
2229 edit_family();
2230 return 1;
2231 case GP_KEY_N:
2232 new_font();
2233 return 1;
2234 case GP_KEY_Q:
2235 quit();
2236 return 1;
2237 case GP_KEY_C:
2238 copy();
2239 return 1;
2240 case GP_KEY_V:
2241 paste();
2242 return 1;
2243 default:
2244 return 0;
2245 }
2246 }
2247
2248 switch (input->key.key) {
2249 case GP_KEY_L:
2250 lint_jump();
2251 return 1;
2252 case GP_KEY_V:
2253 revert();
2254 return 1;
2255 case GP_KEY_DELETE:
2256 case GP_KEY_X:
2257 case GP_KEY_Y:
2258 case GP_KEY_LEFT_BRACE:
2259 case GP_KEY_RIGHT_BRACE:
2260 glyph_command(input->key.key);
2261 return 1;
2262 default:
2263 return 0;
2264 }
2265}
2266
2267static int lint_on_event(gp_widget_event *ev)
2268{
2269 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2270 lint_jump();
2271
2272 return 0;
2273}
2274
2275static int open_on_event(gp_widget_event *ev)
2276{
2277 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2278 open_font();
2279
2280 return 0;
2281}
2282
2283static int save_on_event(gp_widget_event *ev)
2284{
2285 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2286 save();
2287
2288 return 0;
2289}
2290
2291static int export_on_event(gp_widget_event *ev)
2292{
2293 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2294 export_font();
2295
2296 return 0;
2297}
2298
2299static int new_on_event(gp_widget_event *ev)
2300{
2301 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2302 new_font();
2303
2304 return 0;
2305}
2306
2307static int family_on_event(gp_widget_event *ev)
2308{
2309 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2310 edit_family();
2311
2312 return 0;
2313}
2314
2315/*
2316 * The buttons under the canvas. Everything they do has a key as well, they
2317 * are there so that the keys can be discovered.
2318 */
2319static int undo_on_event(gp_widget_event *ev)
2320{
2321 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2322 undo_redo(0);
2323
2324 return 0;
2325}
2326
2327static int redo_on_event(gp_widget_event *ev)
2328{
2329 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2330 undo_redo(1);
2331
2332 return 0;
2333}
2334
2335static int glyph_del_on_event(gp_widget_event *ev)
2336{
2337 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2338 revert();
2339
2340 return 0;
2341}
2342
2343static int copy_on_event(gp_widget_event *ev)
2344{
2345 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2346 copy();
2347
2348 return 0;
2349}
2350
2351static int paste_on_event(gp_widget_event *ev)
2352{
2353 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2354 paste();
2355
2356 return 0;
2357}
2358
2359static int move_left_on_event(gp_widget_event *ev)
2360{
2361 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2362 gpf_gui_shift(-1, 0);
2363
2364 return 0;
2365}
2366
2367static int move_right_on_event(gp_widget_event *ev)
2368{
2369 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2370 gpf_gui_shift(1, 0);
2371
2372 return 0;
2373}
2374
2375static int move_up_on_event(gp_widget_event *ev)
2376{
2377 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2378 gpf_gui_shift(0, 1);
2379
2380 return 0;
2381}
2382
2383static int move_down_on_event(gp_widget_event *ev)
2384{
2385 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2386 gpf_gui_shift(0, -1);
2387
2388 return 0;
2389}
2390
2391static int flip_h_on_event(gp_widget_event *ev)
2392{
2393 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2394 glyph_command(GP_KEY_X);
2395
2396 return 0;
2397}
2398
2399static int flip_v_on_event(gp_widget_event *ev)
2400{
2401 if (ev->type == GP_WIDGET_EVENT_WIDGET)
2402 glyph_command(GP_KEY_Y);
2403
2404 return 0;
2405}
2406
2407static const gp_widget_json_addr app_callbacks[] = {
2408 {.id = "advance_on_event", .on_event = advance_on_event},
2409 {.id = "bearing_x_on_event", .on_event = bearing_x_on_event},
2410 {.id = "bearing_y_on_event", .on_event = bearing_y_on_event},
2411 {.id = "block_add_on_event", .on_event = block_add_on_event},
2412 {.id = "block_del_on_event", .on_event = block_del_on_event},
2413 {.id = "block_start_on_event", .on_event = block_start_on_event},
2414 {.id = "blocks_desc", .addr = (void *)&blocks_desc},
2415 {.id = "blocks_on_event", .on_event = blocks_on_event},
2416 {.id = "browser_on_event", .on_event = browser_on_event},
2417 {.id = "canvas_on_event", .on_event = canvas_on_event},
2418 {.id = "canvas_zoom_on_event", .on_event = canvas_zoom_on_event},
2419 {.id = "copy_on_event", .on_event = copy_on_event},
2420 {.id = "deps_on_event", .on_event = deps_on_event},
2421 {.id = "export_on_event", .on_event = export_on_event},
2422 {.id = "family_on_event", .on_event = family_on_event},
2423 {.id = "flip_h_on_event", .on_event = flip_h_on_event},
2424 {.id = "flip_v_on_event", .on_event = flip_v_on_event},
2425 {.id = "glyph_clear_on_event", .on_event = glyph_clear_on_event},
2426 {.id = "glyph_del_on_event", .on_event = glyph_del_on_event},
2427 {.id = "glyph_range_del_on_event", .on_event = glyph_range_del_on_event},
2428 {.id = "lint_on_event", .on_event = lint_on_event},
2429 {.id = "move_down_on_event", .on_event = move_down_on_event},
2430 {.id = "move_left_on_event", .on_event = move_left_on_event},
2431 {.id = "move_right_on_event", .on_event = move_right_on_event},
2432 {.id = "move_up_on_event", .on_event = move_up_on_event},
2433 {.id = "new_on_event", .on_event = new_on_event},
2434 {.id = "open_on_event", .on_event = open_on_event},
2435 {.id = "paste_on_event", .on_event = paste_on_event},
2436 {.id = "preview_on_event", .on_event = preview_on_event},
2437 {.id = "preview_text_on_event", .on_event = preview_text_on_event},
2438 {.id = "redo_on_event", .on_event = redo_on_event},
2439 {.id = "rule_over_on_event", .on_event = rule_over_on_event},
2440 {.id = "rule_strike_on_event", .on_event = rule_strike_on_event},
2441 {.id = "rule_under_on_event", .on_event = rule_under_on_event},
2442 {.id = "save_on_event", .on_event = save_on_event},
2443 {.id = "strip_on_event", .on_event = strip_on_event},
2444 {.id = "undo_on_event", .on_event = undo_on_event},
2445 {.id = "update_on_event", .on_event = update_on_event},
2446 {.id = "zoom_on_event", .on_event = zoom_on_event},
2447 {}
2448};
2449
2450static const gp_widget_json_callbacks callbacks = {
2451 .addrs = app_callbacks,
2452};
2453
2454static gp_widget *widget_by_uid(const char *uid, enum gp_widget_type type)
2455{
2456 gp_widget *widget = gp_widget_by_uid(gui.uids, uid, type);
2457
2458 if (!widget)
2459 fprintf(stderr, "gpforge: the layout has no '%s'\n", uid);
2460
2461 return widget;
2462}
2463
2464int main(int argc, char *argv[])
2465{
2466 char err[GPF_ERR_MAX] = "";
2467 gp_widget *layout;
2468
2469 gp_widgets_getopt(&argc, &argv);
2470
2471 /* starting with nothing open is a way to start: open or new comes next */
2472 gui.path = argv[0];
2473
2474 if (gui.path) {
2475 gui.font = load(gui.path, err, sizeof(err));
2476 if (!gui.font) {
2477 fprintf(stderr, "%s\n", err);
2478 return 1;
2479 }
2480 }
2481
2482 gui.code = 'A';
2483 gui.zoom = 2;
2484 gui.browser_follow = 1;
2485
2486 gui.undo = gpf_undo_new();
2487 if (!gui.undo) {
2488 fprintf(stderr, "gpforge: out of memory\n");
2489 return 1;
2490 }
2491
2492 gp_app_on_event_set(app_on_event);
2493 gp_app_event_unmask(GP_WIDGET_EVENT_INPUT);
2494
2495 layout = gp_app_layout_load2("gpforge", &callbacks, &gui.uids);
2496 if (!layout) {
2497 fprintf(stderr, "gpforge: cannot load the layout\n");
2498 return 1;
2499 }
2500
2501 gui.canvas = widget_by_uid("canvas", GP_WIDGET_PIXMAP);
2502 gui.browser = widget_by_uid("browser", GP_WIDGET_PIXMAP);
2503 gui.preview = widget_by_uid("preview", GP_WIDGET_PIXMAP);
2504 gui.strip = widget_by_uid("strip", GP_WIDGET_PIXMAP);
2505 gui.deps = widget_by_uid("deps", GP_WIDGET_PIXMAP);
2506 gui.status = widget_by_uid("status", GP_WIDGET_LABEL);
2507 gui.blocks = widget_by_uid("blocks", GP_WIDGET_SPINBUTTON);
2508 gui.block_start = widget_by_uid("block_start", GP_WIDGET_TBOX);
2509 gui.preview_text = widget_by_uid("preview_text", GP_WIDGET_TBOX);
2510 gui.canvas_zoom_widget = widget_by_uid("canvas_zoom", GP_WIDGET_SPINNER);
2511 gui.lint_label = widget_by_uid("lint", GP_WIDGET_LABEL);
2512 gui.lint_errors = widget_by_uid("lint_errors", GP_WIDGET_LABEL);
2513 gui.lint_warnings = widget_by_uid("lint_warnings", GP_WIDGET_LABEL);
2514 gui.save_button = widget_by_uid("save", GP_WIDGET_BUTTON);
2515 gui.export_button = widget_by_uid("export", GP_WIDGET_BUTTON);
2516 gui.family_button = widget_by_uid("family_button", GP_WIDGET_BUTTON);
2517 gui.undo_button = widget_by_uid("undo", GP_WIDGET_BUTTON);
2518 gui.redo_button = widget_by_uid("redo", GP_WIDGET_BUTTON);
2519 gui.paste_button = widget_by_uid("paste", GP_WIDGET_BUTTON);
2520 gui.advance_widget = widget_by_uid("advance", GP_WIDGET_SPINNER);
2521 gui.bearing_x_widget = widget_by_uid("bearing_x", GP_WIDGET_SPINNER);
2522 gui.bearing_y_widget = widget_by_uid("bearing_y", GP_WIDGET_SPINNER);
2523
2524 gp_widget_events_unmask(gui.canvas, GP_WIDGET_EVENT_RESIZE |
2525 GP_WIDGET_EVENT_COLOR_SCHEME |
2526 GP_WIDGET_EVENT_INPUT |
2527 GP_WIDGET_EVENT_FOCUS);
2528 gp_widget_events_unmask(gui.strip, GP_WIDGET_EVENT_RESIZE |
2529 GP_WIDGET_EVENT_COLOR_SCHEME |
2530 GP_WIDGET_EVENT_INPUT |
2531 GP_WIDGET_EVENT_FOCUS);
2532 gp_widget_events_unmask(gui.deps, GP_WIDGET_EVENT_RESIZE |
2533 GP_WIDGET_EVENT_COLOR_SCHEME |
2534 GP_WIDGET_EVENT_INPUT |
2535 GP_WIDGET_EVENT_FOCUS);
2536 gp_widget_events_unmask(gui.preview, GP_WIDGET_EVENT_RESIZE |
2537 GP_WIDGET_EVENT_COLOR_SCHEME);
2538 gp_widget_events_unmask(gui.browser, GP_WIDGET_EVENT_RESIZE |
2539 GP_WIDGET_EVENT_COLOR_SCHEME |
2540 GP_WIDGET_EVENT_INPUT |
2541 GP_WIDGET_EVENT_FOCUS);
2542
2543 gui.family_label = gp_widget_by_uid(gui.uids, "family", GP_WIDGET_LABEL);
2544
2545 update_family();
2546
2547 update_status();
2548 update_lint_count();
2549 update_block_start();
2550
2551 /* nothing has been edited yet, so there is nothing to save */
2552 gpf_gui_set_dirty(0);
2553
2554 update_export();
2555 update_undo();
2556
2557 /* nothing has been copied yet */
2558 button_disable(gui.paste_button, NULL);
2559
2560 gp_widgets_main_loop(layout, NULL, 0, NULL);
2561
2562 return 0;
2563}
The glyph table.
void gpf_browser_show_sel(void)
Turns the table to the selected glyph.
Definition browser.c:46
The glyph canvas.
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
The block picker dialog.
const struct gpf_ucode_block * gpf_dialog_block_add(void)
Runs the block picker.
The font properties dialog.
The codepoint range dialog.
The unsaved changes dialog.
@ GPF_SAVE_SAVE
Definition dialog_save.h:24
@ GPF_SAVE_DISCARD
Definition dialog_save.h:26
Glyph editing.
struct gpf_author * gpf_authors_dup(const struct gpf_author *authors)
Copies a gp_vec of authors.
Definition font.c:458
void gpf_family_meta_guess(struct gpf_family_meta *meta, const char *family, int size, int ascent, int descent, int advance)
Fills in metadata for a font that is only being started.
Definition font.c:72
int gpf_family_meta_same(const struct gpf_family_meta *a, const struct gpf_family_meta *b)
Compares two sets of font metadata.
Definition font.c:122
struct gpf_glyph * gpf_variant_glyph_add(struct gpf_variant *self, uint32_t code)
Adds a glyph to a variant.
Definition font.c:242
void gpf_font_free(struct gpf_font *self)
Frees a font with all its variants and glyphs.
Definition font.c:159
enum gpf_variant_id gpf_variant_parent(enum gpf_variant_id id)
Returns the variant a variant derives its missing glyphs from.
Definition font.c:45
#define GPF_NAME_MAX
Size of the name buffers, including the terminating zero.
Definition font.h:22
void gpf_font_clear_modified(struct gpf_font *self)
Clears the modified flag of all glyphs in all variants.
Definition font.c:269
void gpf_variant_glyph_del(struct gpf_variant *self, uint32_t code)
Removes a glyph from a variant.
Definition font.c:489
struct gpf_variant * gpf_font_variant(struct gpf_font *self, enum gpf_variant_id id, int create)
Returns a font variant.
Definition font.c:175
#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_NO_PARENT
What gpf_variant_parent() returns for the base variant.
Definition font.h:46
@ GPF_VARIANTS
Number of font variants.
Definition font.h:44
@ GPF_MONO
Monospace glyphs, the base variant.
Definition font.h:36
struct gpf_glyph * gpf_variant_glyph(struct gpf_variant *self, uint32_t code)
Looks up a glyph in a variant.
Definition font.c:231
@ GPF_GLYPH_INK
Glyph with ink.
Definition font.h:62
@ GPF_GLYPH_METRICS
Glyph with metrics.
Definition font.h:68
int gpf_glyph_set_bits(struct gpf_glyph *self, const uint8_t *bits, unsigned int w, unsigned int h, int bearing_x, int bearing_y)
Stores the glyph ink.
Definition font.c:585
struct gpf_font * gpf_font_create(const struct gpf_family_meta *meta, int advance)
Creates a new font.
Definition font.c:100
Native font format.
BDF import, and BDF export.
The gfxprim C font export.
The editor state and the functions everything in the GUI goes through.
void gpf_gui_redraw(void)
Repaints everything that shows the font.
Definition gpforge.c:762
void gpf_gui_select_keep(uint32_t code)
Selects a glyph without moving the table to it.
Definition gpforge.c:877
void gpf_gui_edit_begin(void)
Starts an edit, one undo entry.
Definition gpforge.c:664
const struct gpf_ucode_block * gpf_gui_block(void)
The block being browsed.
Definition gpforge.c:78
void gpf_gui_set_block(unsigned int block)
Selects a block and turns the table to its first page.
Definition gpforge.c:882
struct gpf_glyph * gpf_gui_edit_glyph(void)
The glyph entry to edit in the current variant.
Definition gpforge.c:448
void gpf_gui_select(uint32_t code)
Selects a glyph and turns the table to it.
Definition gpforge.c:872
void gpf_gui_invalidate(void)
Throws the compiled faces away.
Definition gpforge.c:57
void gpf_gui_edit_commit(void)
Commits the edit started by gpf_gui_edit_begin() and repaints.
Definition gpforge.c:676
const gp_font_face * gpf_gui_face(enum gpf_variant_id id)
The resolved variant compiled into a gfxprim face.
Definition gpforge.c:46
void gpf_gui_edited(void)
The font changed.
Definition gpforge.c:732
const struct gpf_ucode_block * gpf_gui_blocks(void)
The block list, built on demand.
Definition gpforge.c:67
The lint checks.
int gpf_lint_glyph(struct gpf_font *font, enum gpf_variant_id id, uint32_t code, struct gpf_lint_finding *out)
Runs a linter for a single glyph.
Definition lint.c:232
struct gpf_lint_finding * gpf_lint(struct gpf_font *font)
Runs linter on a font.
Definition lint.c:244
@ GPF_LINT_WARN
Probably a slip. May be deliberate.
Definition lint.h:25
@ GPF_LINT_ERROR
The font renders wrong.
Definition lint.h:23
The preview, the variant strip and the accented letters strip.
gpf_entry_form
The smallest form an entry can be written in on save.
Definition resolve.h:101
@ GPF_ENTRY_METRICS
The ink is the parent's, only numbers differ.
Definition resolve.h:105
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 font author, one author line of the family file.
Definition font.h:218
char name[64]
The author's name.
Definition font.h:222
The glyph selected last in a block, to go back to when the block is switched to again.
Definition gui.h:33
uint32_t min
The block, by its first codepoint: indices move on a rebuild.
Definition gui.h:35
uint32_t code
The glyph selected, the table one or one picked from the accented letters strip, which may be out of ...
Definition gui.h:42
uint32_t table
The glyph selected in its table.
Definition gui.h:37
A glyph decomposition.
Definition compose.h:40
uint32_t draw_base
What a new one is drawn from.
Definition compose.h:51
enum gpf_accent_place place
Where the accent is placed in the glyph rectangle.
Definition compose.h:69
uint32_t base_code
The letter this is an accented form of, e.g. 'i' for 'í'.
Definition compose.h:44
uint32_t code
A unicode code point of the accented letter.
Definition compose.h:42
Font family meta data.
Definition font.h:230
A font family.
Definition font.h:274
struct gpf_author * authors
Font authors.
Definition font.h:298
struct gpf_family_meta meta
Font meta data.
Definition font.h:276
A glyph.
Definition font.h:80
uint8_t kind
enum gpf_glyph_kind
Definition font.h:88
uint8_t * bits
Glyph ink.
Definition font.h:145
int bearing_y
Glyph bearing y.
Definition font.h:128
uint8_t has_advance
The entry carries an advance.
Definition font.h:97
int dx
Accent offset, positive right and up, compose only.
Definition font.h:156
uint32_t base
Base glyph, GPF_GLYPH_COMPOSE only.
Definition font.h:150
uint32_t accent
Accent to be combined with the base, GPF_GLYPH_COMPOSE only.
Definition font.h:154
uint8_t has_bearing
The entry carries both bearings, see has_advance.
Definition font.h:99
unsigned int height
Glyph ink height.
Definition font.h:137
unsigned int width
Glyph ink width.
Definition font.h:133
int advance
Glyph advance.
Definition font.h:116
uint8_t modified
Glyph was modified flag.
Definition font.h:108
int bearing_x
Glyph bearing x.
Definition font.h:122
uint32_t code
Unicode code point.
Definition font.h:84
The editor state.
Definition gui.h:48
gp_font_face * faces[GPF_VARIANTS]
The compiled faces, built lazily, one per variant.
Definition gui.h:163
A linter finding.
Definition lint.h:29
uint32_t code
A unicode code point the finding is for.
Definition lint.h:33
enum gpf_lint_severity sev
Severity.
Definition lint.h:35
enum gpf_variant_id id
Variant of the glyph the finding is for.
Definition lint.h:31
char msg[72]
Linter message.
Definition lint.h:37
A resolved glyph and where it was drawn from.
Definition resolve.h:36
enum gpf_variant_id ink_from
The variant the ink came from.
Definition resolve.h:46
struct gpf_glyph glyph
Freshly allocated glyph with copied/generated ink.
Definition resolve.h:42
A unicode block description.
const char * name
Human readable name of the block.
unsigned int coverage
Number of glyphs the font has in this block.
uint32_t max
Last code point in the block.
uint32_t min
First code point in the block.
A font variant.
Definition font.h:170
int advance
Advance for all glyphs.
Definition font.h:182
enum gpf_variant_id id
Font variant e.g. GPF_REGULAR, GPF_MONO, etc.
Definition font.h:172
int spacing_mono
Every glyph has the same advance.
Definition font.h:174
struct gpf_undo * gpf_undo_new(void)
Creates a new undo.
Definition undo.c:49
gpf_undo_what
Undo type.
Definition undo.h:131
@ GPF_UNDO_FAMILY
The family metadata or the authors changed.
Definition undo.h:141
@ GPF_UNDO_GLYPH
A glyph changed.
Definition undo.h:139
@ GPF_UNDO_NONE
The journal had nothing at that end.
Definition undo.h:133
int gpf_blocks_find(const struct gpf_ucode_block *blocks, uint32_t code)
Looks up a block by a codepoint.
struct gpf_ucode_block * gpf_blocks_build(struct gpf_font *font)
Builds a list of blocks for a font.
static uint32_t gpf_block_glyphs(const struct gpf_ucode_block *block)
Counts number of codepoints in a unicode block.