9#include <core/gp_common.h>
10#include <utils/gp_vec.h>
20 int has_before, has_after;
21 struct gpf_glyph before, after;
23 struct gpf_family_meta *meta;
25 struct gpf_author *authors[2];
29 struct entry *entries;
44 struct gpf_glyph before;
47#define NO_SAVED ((size_t)-1)
51 struct gpf_undo *self = calloc(1,
sizeof(
struct gpf_undo));
56 self->entries = gp_vec_new(0,
sizeof(
struct entry));
65void gpf_undo_group_begin(
struct gpf_undo *self)
67 self->group = ++self->groups;
70void gpf_undo_group_end(
struct gpf_undo *self)
75static void entry_clear(
struct entry *self)
77 free(self->before.bits);
78 free(self->after.
bits);
80 gp_vec_free(self->authors[0]);
81 gp_vec_free(self->authors[1]);
82 memset(self, 0,
sizeof(*self));
85void gpf_undo_free(
struct gpf_undo *self)
92 for (i = 0; i < gp_vec_len(self->entries); i++)
93 entry_clear(&self->entries[i]);
95 gp_vec_free(self->entries);
96 free(self->before.
bits);
127 a->shift_y != b->shift_y)
134 a->
dx != b->
dx || a->dy != b->dy)
154int gpf_undo_begin(
struct gpf_undo *self,
struct gpf_font *font,
159 free(self->before.
bits);
160 memset(&self->before, 0,
sizeof(self->before));
165 self->has_before = !!glyph;
170 return glyph_snapshot(&self->before, glyph);
173int gpf_undo_retarget(
struct gpf_undo *self,
struct gpf_font *font,
176 if (self->in_edit && self->id ==
id && self->code ==
code)
180 gpf_undo_commit(self, font);
182 return gpf_undo_begin(self, font,
id,
code);
189static int push(
struct gpf_undo *self,
struct entry *entry)
191 struct entry *entries;
195 if (self->saved != NO_SAVED && self->saved > self->pos)
196 self->saved = NO_SAVED;
199 for (i = self->pos; i < gp_vec_len(self->entries); i++)
200 entry_clear(&self->entries[i]);
202 entries = gp_vec_resize(self->entries, self->pos);
208 self->entries = entries;
210 if (!GP_VEC_APPEND(self->entries, *entry)) {
215 self->pos = gp_vec_len(self->entries);
220void gpf_undo_commit(
struct gpf_undo *self,
struct gpf_font *font)
223 struct entry entry = {.id = self->id, .code = self->code,
224 .group = self->group};
231 glyph = glyph_get(font, self->id, self->code);
234 if (!glyph && !self->has_before)
238 if (glyph && self->has_before && glyph_same(glyph, &self->before)) {
243 entry.has_before = self->has_before;
244 entry.has_after = !!glyph;
253 if (self->has_before) {
254 entry.before = self->before;
255 memset(&self->before, 0,
sizeof(self->before));
258 if (glyph && glyph_snapshot(&entry.after, glyph)) {
282int gpf_undo_family(
struct gpf_undo *self,
struct gpf_font *font,
286 struct entry entry = {.group = self->group};
288 if (gpf_family_meta_same(&font->
meta, meta) &&
289 gpf_authors_same(font->
authors, authors))
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);
296 if (!entry.meta || !entry.authors[0] || !entry.authors[1] ||
297 authors_set(font, authors)) {
302 entry.meta[0] = font->
meta;
303 entry.meta[1] = *meta;
307 return push(self, &entry);
314 int has,
const struct gpf_glyph *snapshot)
337 if (glyph_snapshot(glyph, snapshot))
347static int glyph_modified(
struct gpf_undo *self,
enum gpf_variant_id id,
352 if (self->saved == NO_SAVED)
355 from = GP_MIN(self->pos, self->saved);
356 to = GP_MAX(self->pos, self->saved);
358 for (i = from; i < to; i++) {
359 const struct entry *entry = &self->entries[i];
361 if (!entry->meta && entry->id ==
id && entry->code == code)
372static void mark_modified(
struct gpf_undo *self,
struct gpf_font *font,
373 size_t from,
size_t to)
377 for (i = from; i < to; i++) {
378 const struct entry *entry = &self->entries[i];
384 glyph = glyph_get(font, entry->id, entry->code);
386 glyph->
modified = glyph_modified(self, entry->id, entry->code);
394static void apply(
struct gpf_font *font,
const struct entry *entry,
int redo,
399 font->
meta = entry->meta[redo];
400 authors_set(font, entry->authors[redo]);
409 restore(font, entry->id, entry->code, entry->has_after,
412 restore(font, entry->id, entry->code, entry->has_before,
427 size_t end = self->pos;
432 entry = &self->entries[--self->pos];
433 group = entry->group;
435 apply(font, entry, 0, &what,
id, code);
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);
443 mark_modified(self, font, self->pos, end);
454 size_t start = self->pos;
456 if (self->pos >= gp_vec_len(self->entries))
459 entry = &self->entries[self->pos++];
460 group = entry->group;
462 apply(font, entry, 1, &what,
id, code);
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);
470 mark_modified(self, font, start, self->pos);
475void gpf_undo_saved(
struct gpf_undo *self)
477 self->saved = self->pos;
480int gpf_undo_is_saved(
struct gpf_undo *self)
482 return self->saved == self->pos;
485int gpf_undo_can_undo(
struct gpf_undo *self)
487 return self && self->pos;
490int gpf_undo_can_redo(
struct gpf_undo *self)
492 return self && self->pos < gp_vec_len(self->entries);
struct gpf_author * gpf_authors_dup(const struct gpf_author *authors)
Copies a gp_vec of authors.
struct gpf_glyph * gpf_variant_glyph_add(struct gpf_variant *self, uint32_t code)
Adds a glyph to a variant.
void gpf_variant_glyph_del(struct gpf_variant *self, uint32_t code)
Removes a glyph from a variant.
struct gpf_variant * gpf_font_variant(struct gpf_font *self, enum gpf_variant_id id, int create)
Returns a font variant.
gpf_variant_id
Font face variant.
struct gpf_glyph * gpf_variant_glyph(struct gpf_variant *self, uint32_t code)
Looks up a glyph in a variant.
A font author, one author line of the family file.
struct gpf_author * authors
Font authors.
struct gpf_variant * variants[GPF_VARIANTS]
Variants indexed by gpf_variant_id, NULL when missing.
struct gpf_family_meta meta
Font meta data.
uint8_t kind
enum gpf_glyph_kind
int bearing_y
Glyph bearing y.
uint8_t has_advance
The entry carries an advance.
int shift_x
How far the inherited ink is moved, positive right and up.
int dx
Accent offset, positive right and up, compose only.
uint8_t has_shift
The entry moves the inherited ink, GPF_GLYPH_METRICS only.
uint32_t base
Base glyph, GPF_GLYPH_COMPOSE only.
uint32_t accent
Accent to be combined with the base, GPF_GLYPH_COMPOSE only.
uint8_t has_bearing
The entry carries both bearings, see has_advance.
unsigned int height
Glyph ink height.
unsigned int width
Glyph ink width.
int advance
Glyph advance.
uint8_t modified
Glyph was modified flag.
int bearing_x
Glyph bearing x.
uint32_t code
Unicode code point.
enum gpf_variant_id id
Font variant e.g. GPF_REGULAR, GPF_MONO, etc.
struct gpf_undo * gpf_undo_new(void)
Creates a new undo.
@ GPF_UNDO_FAMILY
The family metadata or the authors changed.
@ GPF_UNDO_GLYPH
A glyph changed.
@ GPF_UNDO_NONE
The journal had nothing at that end.