gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
undo.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#include <string.h>
8
9#include <core/gp_common.h>
10#include <utils/gp_vec.h>
11
12#include "undo.h"
13
14struct entry {
15 enum gpf_variant_id id;
16 uint32_t code;
17 /* entries of one group are taken back together, 0 is no group */
18 unsigned int group;
19 /* a snapshot that is not present means the glyph was not there */
20 int has_before, has_after;
21 struct gpf_glyph before, after;
22 /* a family entry instead, the before and the after, NULL for a glyph */
23 struct gpf_family_meta *meta;
24 /* and the authors, gp_vecs, before and after too */
25 struct gpf_author *authors[2];
26};
27
28struct gpf_undo {
29 struct entry *entries;
30 /* how many entries are applied, everything above is redo */
31 size_t pos;
32 /* the pos the file was written at, NO_SAVED when that is gone */
33 size_t saved;
34
35 /* the group being recorded, and the last one handed out */
36 unsigned int group;
37 unsigned int groups;
38
39 /* the pending begin() */
40 int in_edit;
41 enum gpf_variant_id id;
42 uint32_t code;
43 int has_before;
44 struct gpf_glyph before;
45};
46
47#define NO_SAVED ((size_t)-1)
48
49struct gpf_undo *gpf_undo_new(void)
50{
51 struct gpf_undo *self = calloc(1, sizeof(struct gpf_undo));
52
53 if (!self)
54 return NULL;
55
56 self->entries = gp_vec_new(0, sizeof(struct entry));
57 if (!self->entries) {
58 free(self);
59 return NULL;
60 }
61
62 return self;
63}
64
65void gpf_undo_group_begin(struct gpf_undo *self)
66{
67 self->group = ++self->groups;
68}
69
70void gpf_undo_group_end(struct gpf_undo *self)
71{
72 self->group = 0;
73}
74
75static void entry_clear(struct entry *self)
76{
77 free(self->before.bits);
78 free(self->after.bits);
79 free(self->meta);
80 gp_vec_free(self->authors[0]);
81 gp_vec_free(self->authors[1]);
82 memset(self, 0, sizeof(*self));
83}
84
85void gpf_undo_free(struct gpf_undo *self)
86{
87 size_t i;
88
89 if (!self)
90 return;
91
92 for (i = 0; i < gp_vec_len(self->entries); i++)
93 entry_clear(&self->entries[i]);
94
95 gp_vec_free(self->entries);
96 free(self->before.bits);
97 free(self);
98}
99
100static int glyph_snapshot(struct gpf_glyph *dst, const struct gpf_glyph *src)
101{
102 *dst = *src;
103 dst->bits = NULL;
104
105 if (!src->width || !src->height)
106 return 0;
107
108 dst->bits = malloc((size_t)src->width * src->height);
109 if (!dst->bits)
110 return 1;
111
112 memcpy(dst->bits, src->bits, (size_t)src->width * src->height);
113
114 return 0;
115}
116
117static int glyph_same(const struct gpf_glyph *a, const struct gpf_glyph *b)
118{
119 if (a->kind != b->kind || a->advance != b->advance ||
120 a->has_advance != b->has_advance || a->has_bearing != b->has_bearing)
121 return 0;
122
123 if (a->bearing_x != b->bearing_x || a->bearing_y != b->bearing_y)
124 return 0;
125
126 if (a->has_shift != b->has_shift || a->shift_x != b->shift_x ||
127 a->shift_y != b->shift_y)
128 return 0;
129
130 if (a->width != b->width || a->height != b->height)
131 return 0;
132
133 if (a->base != b->base || a->accent != b->accent ||
134 a->dx != b->dx || a->dy != b->dy)
135 return 0;
136
137 if (!a->width)
138 return 1;
139
140 return !memcmp(a->bits, b->bits, (size_t)a->width * a->height);
141}
142
143static struct gpf_glyph *glyph_get(struct gpf_font *font,
144 enum gpf_variant_id id, uint32_t code)
145{
146 struct gpf_variant *variant = font->variants[id];
147
148 if (!variant)
149 return NULL;
150
151 return gpf_variant_glyph(variant, code);
152}
153
154int gpf_undo_begin(struct gpf_undo *self, struct gpf_font *font,
155 enum gpf_variant_id id, uint32_t code)
156{
157 struct gpf_glyph *glyph = glyph_get(font, id, code);
158
159 free(self->before.bits);
160 memset(&self->before, 0, sizeof(self->before));
161
162 self->in_edit = 1;
163 self->id = id;
164 self->code = code;
165 self->has_before = !!glyph;
166
167 if (!glyph)
168 return 0;
169
170 return glyph_snapshot(&self->before, glyph);
171}
172
173int gpf_undo_retarget(struct gpf_undo *self, struct gpf_font *font,
174 enum gpf_variant_id id, uint32_t code)
175{
176 if (self->in_edit && self->id == id && self->code == code)
177 return 0;
178
179 if (self->in_edit)
180 gpf_undo_commit(self, font);
181
182 return gpf_undo_begin(self, font, id, code);
183}
184
185/*
186 * Appends an entry, which the journal then owns: it is freed here when it
187 * cannot be added.
188 */
189static int push(struct gpf_undo *self, struct entry *entry)
190{
191 struct entry *entries;
192 size_t i;
193
194 /* the redo thrown away may be what the file has */
195 if (self->saved != NO_SAVED && self->saved > self->pos)
196 self->saved = NO_SAVED;
197
198 /* an edit after an undo throws the redo away */
199 for (i = self->pos; i < gp_vec_len(self->entries); i++)
200 entry_clear(&self->entries[i]);
201
202 entries = gp_vec_resize(self->entries, self->pos);
203 if (!entries) {
204 entry_clear(entry);
205 return 1;
206 }
207
208 self->entries = entries;
209
210 if (!GP_VEC_APPEND(self->entries, *entry)) {
211 entry_clear(entry);
212 return 1;
213 }
214
215 self->pos = gp_vec_len(self->entries);
216
217 return 0;
218}
219
220void gpf_undo_commit(struct gpf_undo *self, struct gpf_font *font)
221{
222 struct gpf_glyph *glyph;
223 struct entry entry = {.id = self->id, .code = self->code,
224 .group = self->group};
225
226 if (!self->in_edit)
227 return;
228
229 self->in_edit = 0;
230
231 glyph = glyph_get(font, self->id, self->code);
232
233 /* nothing happened, do not clutter the journal with it */
234 if (!glyph && !self->has_before)
235 return;
236
237 /* the editor marks the glyph on the first touch, take that back */
238 if (glyph && self->has_before && glyph_same(glyph, &self->before)) {
239 glyph->modified = self->before.modified;
240 return;
241 }
242
243 entry.has_before = self->has_before;
244 entry.has_after = !!glyph;
245
246 /*
247 * Something did change, and the file does not have it: the journal is
248 * where every edit passes, so this is where the glyph is marked.
249 */
250 if (glyph)
251 glyph->modified = 1;
252
253 if (self->has_before) {
254 entry.before = self->before;
255 memset(&self->before, 0, sizeof(self->before));
256 }
257
258 if (glyph && glyph_snapshot(&entry.after, glyph)) {
259 entry_clear(&entry);
260 return;
261 }
262
263 push(self, &entry);
264}
265
266/*
267 * The font keeps a copy of its own, so that the entry can be applied again.
268 */
269static int authors_set(struct gpf_font *font, const struct gpf_author *authors)
270{
271 struct gpf_author *dup = gpf_authors_dup(authors);
272
273 if (!dup)
274 return 1;
275
276 gp_vec_free(font->authors);
277 font->authors = dup;
278
279 return 0;
280}
281
282int gpf_undo_family(struct gpf_undo *self, struct gpf_font *font,
283 const struct gpf_family_meta *meta,
284 const struct gpf_author *authors)
285{
286 struct entry entry = {.group = self->group};
287
288 if (gpf_family_meta_same(&font->meta, meta) &&
289 gpf_authors_same(font->authors, authors))
290 return 0;
291
292 entry.meta = malloc(2 * sizeof(*entry.meta));
293 entry.authors[0] = gpf_authors_dup(font->authors);
294 entry.authors[1] = gpf_authors_dup(authors);
295
296 if (!entry.meta || !entry.authors[0] || !entry.authors[1] ||
297 authors_set(font, authors)) {
298 entry_clear(&entry);
299 return 1;
300 }
301
302 entry.meta[0] = font->meta;
303 entry.meta[1] = *meta;
304
305 font->meta = *meta;
306
307 return push(self, &entry);
308}
309
310/*
311 * Puts a snapshot back, or removes the glyph when there was none.
312 */
313static int restore(struct gpf_font *font, enum gpf_variant_id id, uint32_t code,
314 int has, const struct gpf_glyph *snapshot)
315{
316 struct gpf_variant *variant;
317 struct gpf_glyph *glyph;
318
319 if (!has) {
320 /* only this variant, the others are none of our business */
321 if (font->variants[id])
323
324 return 0;
325 }
326
327 variant = gpf_font_variant(font, id, 1);
328 if (!variant)
329 return 1;
330
331 glyph = gpf_variant_glyph_add(variant, code);
332 if (!glyph)
333 return 1;
334
335 free(glyph->bits);
336
337 if (glyph_snapshot(glyph, snapshot))
338 return 1;
339
340 return 0;
341}
342
343/*
344 * Whether a glyph differs from the file: it does when an entry between the
345 * current position and the one the file was written at touched it.
346 */
347static int glyph_modified(struct gpf_undo *self, enum gpf_variant_id id,
348 uint32_t code)
349{
350 size_t i, from, to;
351
352 if (self->saved == NO_SAVED)
353 return 1;
354
355 from = GP_MIN(self->pos, self->saved);
356 to = GP_MAX(self->pos, self->saved);
357
358 for (i = from; i < to; i++) {
359 const struct entry *entry = &self->entries[i];
360
361 if (!entry->meta && entry->id == id && entry->code == code)
362 return 1;
363 }
364
365 return 0;
366}
367
368/*
369 * The snapshots carry whatever the flag was when they were taken, which says
370 * nothing about the file, so it is worked out again for the entries applied.
371 */
372static void mark_modified(struct gpf_undo *self, struct gpf_font *font,
373 size_t from, size_t to)
374{
375 size_t i;
376
377 for (i = from; i < to; i++) {
378 const struct entry *entry = &self->entries[i];
379 struct gpf_glyph *glyph;
380
381 if (entry->meta)
382 continue;
383
384 glyph = glyph_get(font, entry->id, entry->code);
385 if (glyph)
386 glyph->modified = glyph_modified(self, entry->id, entry->code);
387 }
388}
389
390/*
391 * Puts one side of an entry back. A glyph outranks the family in what is
392 * reported, since it is the thing the editor has to go to.
393 */
394static void apply(struct gpf_font *font, const struct entry *entry, int redo,
395 enum gpf_undo_what *what, enum gpf_variant_id *id,
396 uint32_t *code)
397{
398 if (entry->meta) {
399 font->meta = entry->meta[redo];
400 authors_set(font, entry->authors[redo]);
401
402 if (*what == GPF_UNDO_NONE)
403 *what = GPF_UNDO_FAMILY;
404
405 return;
406 }
407
408 if (redo) {
409 restore(font, entry->id, entry->code, entry->has_after,
410 &entry->after);
411 } else {
412 restore(font, entry->id, entry->code, entry->has_before,
413 &entry->before);
414 }
415
416 *id = entry->id;
417 *code = entry->code;
418 *what = GPF_UNDO_GLYPH;
419}
420
421enum gpf_undo_what gpf_undo(struct gpf_undo *self, struct gpf_font *font,
422 enum gpf_variant_id *id, uint32_t *code)
423{
424 enum gpf_undo_what what = GPF_UNDO_NONE;
425 struct entry *entry;
426 unsigned int group;
427 size_t end = self->pos;
428
429 if (!self->pos)
430 return GPF_UNDO_NONE;
431
432 entry = &self->entries[--self->pos];
433 group = entry->group;
434
435 apply(font, entry, 0, &what, id, code);
436
437 /* the rest of the group goes with it */
438 while (group && self->pos && self->entries[self->pos-1].group == group) {
439 entry = &self->entries[--self->pos];
440 apply(font, entry, 0, &what, id, code);
441 }
442
443 mark_modified(self, font, self->pos, end);
444
445 return what;
446}
447
448enum gpf_undo_what gpf_redo(struct gpf_undo *self, struct gpf_font *font,
449 enum gpf_variant_id *id, uint32_t *code)
450{
451 enum gpf_undo_what what = GPF_UNDO_NONE;
452 struct entry *entry;
453 unsigned int group;
454 size_t start = self->pos;
455
456 if (self->pos >= gp_vec_len(self->entries))
457 return GPF_UNDO_NONE;
458
459 entry = &self->entries[self->pos++];
460 group = entry->group;
461
462 apply(font, entry, 1, &what, id, code);
463
464 while (group && self->pos < gp_vec_len(self->entries) &&
465 self->entries[self->pos].group == group) {
466 entry = &self->entries[self->pos++];
467 apply(font, entry, 1, &what, id, code);
468 }
469
470 mark_modified(self, font, start, self->pos);
471
472 return what;
473}
474
475void gpf_undo_saved(struct gpf_undo *self)
476{
477 self->saved = self->pos;
478}
479
480int gpf_undo_is_saved(struct gpf_undo *self)
481{
482 return self->saved == self->pos;
483}
484
485int gpf_undo_can_undo(struct gpf_undo *self)
486{
487 return self && self->pos;
488}
489
490int gpf_undo_can_redo(struct gpf_undo *self)
491{
492 return self && self->pos < gp_vec_len(self->entries);
493}
struct gpf_author * gpf_authors_dup(const struct gpf_author *authors)
Copies a gp_vec of authors.
Definition font.c:458
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_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
gpf_variant_id
Font face variant.
Definition font.h:34
struct gpf_glyph * gpf_variant_glyph(struct gpf_variant *self, uint32_t code)
Looks up a glyph in a variant.
Definition font.c:231
A font author, one author line of the family file.
Definition font.h:218
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_variant * variants[GPF_VARIANTS]
Variants indexed by gpf_variant_id, NULL when missing.
Definition font.h:301
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 shift_x
How far the inherited ink is moved, positive right and up.
Definition font.h:164
int dx
Accent offset, positive right and up, compose only.
Definition font.h:156
uint8_t has_shift
The entry moves the inherited ink, GPF_GLYPH_METRICS only.
Definition font.h:101
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
A font variant.
Definition font.h:170
enum gpf_variant_id id
Font variant e.g. GPF_REGULAR, GPF_MONO, etc.
Definition font.h:172
The undo journal.
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