gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
canvas.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#include <stdlib.h>
7
8#include <gfxprim.h>
9
10#include "edit.h"
11#include "gui.h"
12#include "canvas.h"
13
14#define MARGIN 2
15/* columns of room around the cell, for ink that reaches outside of it */
16#define SLACK 2
17
18struct canvas {
19 /* the drawn range in glyph coordinates */
20 int left, right, top, bottom;
21 int zoom;
22 /* where the origin and the baseline land in the pixmap */
23 int ox, oy;
24 /* the room left of and above the grid, where it is numbered */
25 int rule_w, rule_h;
26};
27
28/* the widest number the rulers show, and the position has two of them */
29#define RULE_CHARS 3
30
31static int glyph_advance(void)
32{
33 struct gpf_resolved res;
34 int advance;
35
36 if (gpf_resolve(gui.font, gui.variant, gui.code, &res)) {
37 struct gpf_variant *variant = gui.font->variants[gui.variant];
38
39 if (variant && variant->advance)
40 return variant->advance;
41
42 return gui.font->meta.em ? gui.font->meta.em : 8;
43 }
44
45 advance = res.glyph.advance;
46
47 gpf_resolved_clear(&res);
48
49 return advance;
50}
51
52static void geometry(struct canvas *self, gp_pixmap *p)
53{
54 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
55 struct gpf_font *font = gui.font;
56 int zx, zy;
57
58 self->top = font->meta.ascent + SLACK;
59 self->bottom = -font->meta.descent - SLACK;
60 self->left = -SLACK;
61 self->right = glyph_advance() + SLACK;
62
63 self->rule_w = gp_text_max_width(ctx->font_mono, RULE_CHARS) + 2 * MARGIN;
64 self->rule_h = gp_text_height(ctx->font_mono) + 2 * MARGIN;
65
66 zx = ((int)p->w - self->rule_w - MARGIN) /
67 GPF_MAX(1, self->right - self->left);
68 zy = ((int)p->h - self->rule_h - MARGIN) /
69 GPF_MAX(1, self->top - self->bottom);
70
71 /*
72 * The canvas starts at whatever multiplier fits the widget and the
73 * spinner takes it from there, so that a dense display can be dialed
74 * up without the glyph starting out microscopic.
75 */
76 if (!gui.canvas_zoom)
77 gpf_gui_set_canvas_zoom(GPF_MAX(1, GPF_MIN(zx, zy)));
78
79 self->zoom = gui.canvas_zoom;
80
81 /* centred in what the rulers leave */
82 self->ox = self->rule_w +
83 ((int)p->w - self->rule_w -
84 (self->right - self->left) * self->zoom) / 2
85 - self->left * self->zoom;
86 self->oy = self->rule_h +
87 ((int)p->h - self->rule_h -
88 (self->top - self->bottom) * self->zoom) / 2
89 + self->top * self->zoom;
90}
91
92/* a column to a pixmap x */
93static int cx(const struct canvas *self, int col)
94{
95 return self->ox + col * self->zoom;
96}
97
98/* a height above the baseline to a pixmap y */
99static int cy(const struct canvas *self, int height)
100{
101 return self->oy - height * self->zoom;
102}
103
104/*
105 * A pixmap position to a glyph pixel. Returns non-zero when it is outside of
106 * the drawn region.
107 */
108static int hit(const struct canvas *self, int x, int y, int *col, int *height)
109{
110 int dx = x - cx(self, self->left);
111 int dy = y - cy(self, self->top);
112
113 if (dx < 0 || dy < 0)
114 return 1;
115
116 *col = self->left + dx / self->zoom;
117 *height = self->top - dy / self->zoom;
118
119 if (*col >= self->right || *height <= self->bottom)
120 return 1;
121
122 return 0;
123}
124
125/*
126 * The canvas is drawn on gpf_gui_back_color() and the grid in the other one of
127 * the foreground and the background colors.
128 */
129static gp_pixel grid_color(const gp_widget_render_ctx *ctx)
130{
131 if (gui.canvas && gui.canvas->focused)
132 return gp_widgets_color(ctx, GP_WIDGETS_COL_BG);
133
134 return gp_widgets_color(ctx, GP_WIDGETS_COL_FG);
135}
136
137static void draw_grid(const struct canvas *self, gp_pixmap *p,
138 const gp_widget_render_ctx *ctx)
139{
140 gp_pixel col = grid_color(ctx);
141 int i;
142
143 if (self->zoom < 4)
144 return;
145
146 for (i = self->left; i <= self->right; i++)
147 gp_vline_xyy(p, cx(self, i), cy(self, self->top), cy(self, self->bottom), col);
148
149 for (i = self->bottom; i <= self->top; i++)
150 gp_hline_xxy(p, cx(self, self->left), cx(self, self->right), cy(self, i), col);
151}
152
153static void draw_guides(const struct canvas *self, gp_pixmap *p,
154 const gp_widget_render_ctx *ctx)
155{
156 struct gpf_font *font = gui.font;
157 gp_pixel base = gp_widgets_color(ctx, GP_WIDGETS_COL_BLUE);
158 gp_pixel adv = gp_widgets_color(ctx, GP_WIDGETS_COL_GREEN);
159 /*
160 * The pixel grid is drawn in the secondary colour now, so the metric
161 * references need one of their own or they disappear into it.
162 */
163 gp_pixel hint = gp_widgets_color(ctx, GP_WIDGETS_COL_RED);
164 int x0 = cx(self, self->left), x1 = cx(self, self->right);
165 int y0 = cy(self, self->top), y1 = cy(self, self->bottom);
166
167 if (font->meta.x_height)
168 gp_hline_xxy(p, x0, x1, cy(self, font->meta.x_height), hint);
169
170 if (font->meta.cap_height)
171 gp_hline_xxy(p, x0, x1, cy(self, font->meta.cap_height), hint);
172
173 gp_hline_xxy(p, x0, x1, cy(self, font->meta.ascent), hint);
174 gp_hline_xxy(p, x0, x1, cy(self, -font->meta.descent), hint);
175
176 /* the baseline and the origin */
177 gp_hline_xxy(p, x0, x1, cy(self, 0), base);
178 gp_vline_xyy(p, cx(self, 0), y0, y1, base);
179
180 gp_vline_xyy(p, cx(self, self->right - SLACK), y0, y1, adv);
181}
182
183static void draw_ink(const struct canvas *self, gp_pixmap *p,
184 const gp_widget_render_ctx *ctx,
185 const struct gpf_resolved *res)
186{
187 const struct gpf_glyph *glyph = &res->glyph;
188 gp_pixel col = gp_widgets_color(ctx, GP_WIDGETS_COL_TEXT);
189 unsigned int x, y;
190
191 for (y = 0; y < glyph->height; y++) {
192 for (x = 0; x < glyph->width; x++) {
193 if (!gpf_glyph_pixel(glyph, x, y))
194 continue;
195
196 gp_fill_rect_xywh(p, cx(self, glyph->bearing_x + x),
197 cy(self, glyph->bearing_y - y),
198 self->zoom, self->zoom, col);
199 }
200 }
201}
202
203/*
204 * The pixel the keyboard edits, drawn as a frame so that it is visible on ink
205 * and on background alike. The mouse moves it too, there is one cursor.
206 */
207static void draw_cursor(const struct canvas *self, gp_pixmap *p,
208 const gp_widget_render_ctx *ctx)
209{
210 gp_pixel col = gp_widgets_color(ctx, GP_WIDGETS_COL_ALERT);
211 int x = cx(self, gui.cur_col), y = cy(self, gui.cur_height);
212
213 if (gui.cur_col < self->left || gui.cur_col >= self->right)
214 return;
215
216 if (gui.cur_height <= self->bottom || gui.cur_height > self->top)
217 return;
218
219 gp_rect_xywh(p, x, y, self->zoom, self->zoom, col);
220}
221
222/*
223 * The cursor position in the glyph coordinates, the column right of the origin
224 * and the height above the baseline, the same numbers the bearings are in.
225 * Drawn in the corner where the rulers meet, padded to a constant width so
226 * that the comma stays in place as the cursor moves. Returns where it ends.
227 */
228static int draw_pos(gp_pixmap *p, const gp_widget_render_ctx *ctx)
229{
230 return MARGIN + gp_print(p, ctx->font_mono, MARGIN, MARGIN,
231 GP_ALIGN_RIGHT | GP_VALIGN_BELOW,
232 gp_widgets_color(ctx, GP_WIDGETS_COL_TEXT),
233 gpf_gui_back_color(gui.canvas), "%*i,%-*i",
234 RULE_CHARS, gui.cur_col,
235 RULE_CHARS, gui.cur_height);
236}
237
238/*
239 * Every how many pixels a ruler is numbered: all of them when the numbers fit,
240 * every second, fifth, tenth... when they do not. Zero is always numbered.
241 */
242static int rule_step(int zoom, int size)
243{
244 static const int steps[] = {1, 2, 5, 10, 20, 50, 100};
245 unsigned int i;
246
247 for (i = 0; i < GP_ARRAY_SIZE(steps); i++) {
248 if (steps[i] * zoom >= size)
249 return steps[i];
250 }
251
252 return steps[GP_ARRAY_SIZE(steps) - 1];
253}
254
255/*
256 * The columns numbered above the grid and the heights left of it, in the same
257 * coordinates as the position. A column number that would run into the
258 * position is left out.
259 */
260static void draw_rulers(const struct canvas *self, gp_pixmap *p,
261 const gp_widget_render_ctx *ctx, int pos_end)
262{
263 const gp_text_style *font = ctx->font_mono;
264 gp_pixel fg = gp_widgets_color(ctx, GP_WIDGETS_COL_TEXT);
265 gp_pixel bg = gpf_gui_back_color(gui.canvas);
266 int num_w = gp_text_max_width(font, RULE_CHARS);
267 int step, i;
268
269 step = rule_step(self->zoom, num_w + MARGIN);
270
271 for (i = self->left; i < self->right; i++) {
272 int x = cx(self, i) + self->zoom / 2;
273
274 if (i % step)
275 continue;
276
277 if (x - num_w / 2 < pos_end + MARGIN &&
278 cy(self, self->top) - MARGIN < self->rule_h)
279 continue;
280
281 gp_print(p, font, x, cy(self, self->top) - MARGIN,
282 GP_ALIGN_CENTER | GP_VALIGN_ABOVE, fg, bg, "%i", i);
283 }
284
285 step = rule_step(self->zoom, gp_text_height(font));
286
287 for (i = self->bottom + 1; i <= self->top; i++) {
288 if (i % step)
289 continue;
290
291 gp_print(p, font, cx(self, self->left) - MARGIN,
292 cy(self, i) + self->zoom / 2,
293 GP_ALIGN_LEFT | GP_VALIGN_CENTER, fg, bg, "%i", i);
294 }
295}
296
297void gpf_canvas_draw(gp_widget *self)
298{
299 gp_pixmap *p = gp_widget_pixmap_get(self);
300 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
301 struct gpf_resolved res;
302 struct canvas canvas;
303 int have;
304
305 if (!p)
306 return;
307
308 gp_fill(p, gpf_gui_back_color(gui.canvas));
309
310 /* no font open, an empty canvas is the whole of it */
311 if (!gui.font)
312 return;
313
314 geometry(&canvas, p);
315
316 draw_grid(&canvas, p, ctx);
317 draw_guides(&canvas, p, ctx);
318
319 have = !gpf_resolve(gui.font, gui.variant, gui.code, &res);
320
321 if (have) {
322 draw_ink(&canvas, p, ctx, &res);
323 gpf_resolved_clear(&res);
324 }
325
326 draw_cursor(&canvas, p, ctx);
327 draw_rulers(&canvas, p, ctx, draw_pos(p, ctx));
328}
329
330/*
331 * Painting goes through gpf_gui_pixel_set(), which is also what copies the
332 * pixel into the accented letters when the update box is ticked.
333 */
334static void paint(int col, int height, int set)
335{
336 gui.cur_col = col;
337 gui.cur_height = height;
338
339 gpf_gui_pixel_set(col, height, set);
340}
341
342/* what a press near one of the metric lines grabs */
343enum drag {
344 DRAG_NONE = 0,
345 DRAG_ADVANCE,
346 DRAG_ORIGIN,
347};
348
349/*
350 * The advance and the origin are dragged, which is how the sidebearings are
351 * edited. The band is narrow so that drawing next to either line still draws.
352 */
353static enum drag drag_at(const struct canvas *canvas, int x)
354{
355 int band = GPF_MAX(3, canvas->zoom / 4);
356 int advance = canvas->right - SLACK;
357 struct gpf_variant *variant = gui.font->variants[gui.variant];
358
359 if (abs(x - cx(canvas, 0)) <= band)
360 return DRAG_ORIGIN;
361
362 /* in a monospace variant the advance belongs to the variant */
363 if (variant && variant->spacing_mono)
364 return DRAG_NONE;
365
366 if (abs(x - cx(canvas, advance)) <= band)
367 return DRAG_ADVANCE;
368
369 return DRAG_NONE;
370}
371
372static void drag_to(int col)
373{
374 struct gpf_glyph *glyph = gpf_gui_edit_glyph();
375
376 if (!glyph)
377 return;
378
379 switch (gui.drag) {
380 case DRAG_ADVANCE:
381 if (col < 0)
382 col = 0;
383
384 if (glyph->advance == col)
385 return;
386
387 glyph->advance = col;
388 break;
389 case DRAG_ORIGIN:
390 if (col == gui.drag_col)
391 return;
392
393 /* moving the origin right puts the ink left of it */
394 gpf_glyph_shift(glyph, gui.drag_col - col, 0);
395 gui.drag_col = col;
396 break;
397 default:
398 return;
399 }
400
402}
403
404static int press(int x, int y)
405{
406 gp_pixmap *p = gp_widget_pixmap_get(gui.canvas);
407 struct canvas canvas;
408 struct gpf_resolved res;
409 int col, height, set;
410
411 if (!p)
412 return 0;
413
414 geometry(&canvas, p);
415
416 if (hit(&canvas, x, y, &col, &height))
417 return 0;
418
419 gui.drag = drag_at(&canvas, x);
420
421 if (gui.drag) {
422 gui.drag_col = col;
423 gui.in_stroke = 1;
424
426
427 return 1;
428 }
429
430 /*
431 * A press toggles the pixel and the rest of the drag paints that same
432 * value, or a diagonal drag would flip pixels back off behind itself.
433 */
434 set = 1;
435
436 if (!gpf_resolve(gui.font, gui.variant, gui.code, &res)) {
437 set = !gpf_glyph_pixel_get(&res.glyph, col, height);
438 gpf_resolved_clear(&res);
439 }
440
442
443 gui.in_stroke = 1;
444 gui.paint_val = set;
445
446 paint(col, height, set);
447
448 return 1;
449}
450
451static int motion(int x, int y)
452{
453 gp_pixmap *p = gp_widget_pixmap_get(gui.canvas);
454 struct canvas canvas;
455 int col, height;
456
457 if (!p || !gui.in_stroke)
458 return 0;
459
460 geometry(&canvas, p);
461
462 if (hit(&canvas, x, y, &col, &height))
463 return 0;
464
465 if (gui.drag) {
466 drag_to(col);
467 return 1;
468 }
469
470 paint(col, height, gui.paint_val);
471
472 return 1;
473}
474
475/*
476 * The keyboard cursor follows the mouse, so that there is only one cursor and
477 * the space toggles the pixel the mouse points at.
478 */
479static int hover(int x, int y)
480{
481 gp_pixmap *p = gp_widget_pixmap_get(gui.canvas);
482 struct canvas canvas;
483 int col, height;
484
485 if (!p)
486 return 0;
487
488 geometry(&canvas, p);
489
490 if (hit(&canvas, x, y, &col, &height))
491 return 0;
492
493 if (col == gui.cur_col && height == gui.cur_height)
494 return 1;
495
496 gui.cur_col = col;
497 gui.cur_height = height;
498
499 gpf_canvas_draw(gui.canvas);
500 gp_widget_redraw(gui.canvas);
501
502 return 1;
503}
504
505static int release(void)
506{
507 if (!gui.in_stroke)
508 return 0;
509
510 gui.in_stroke = 0;
511 gui.drag = DRAG_NONE;
512
514
515 return 1;
516}
517
518static int toggle_cursor(void)
519{
520 struct gpf_resolved res;
521 int set = 1;
522
523 if (!gpf_resolve(gui.font, gui.variant, gui.code, &res)) {
524 set = !gpf_glyph_pixel_get(&res.glyph, gui.cur_col,
525 gui.cur_height);
526 gpf_resolved_clear(&res);
527 }
528
530
531 paint(gui.cur_col, gui.cur_height, set);
532
534
535 return 1;
536}
537
538static int move_cursor(int dx, int dy)
539{
540 gp_pixmap *p = gp_widget_pixmap_get(gui.canvas);
541 struct canvas canvas;
542
543 if (!p)
544 return 0;
545
546 geometry(&canvas, p);
547
548 gui.cur_col = GPF_MAX(canvas.left, GPF_MIN(canvas.right - 1,
549 gui.cur_col + dx));
550 gui.cur_height = GPF_MAX(canvas.bottom + 1,
551 GPF_MIN(canvas.top, gui.cur_height + dy));
552
553 gpf_canvas_draw(gui.canvas);
554 gp_widget_redraw(gui.canvas);
555
556 return 1;
557}
558
559/*
560 * Shift+arrows move the ink, which is how the bearings are edited, or the
561 * accent of a composed glyph.
562 */
563static int shift_ink(int dx, int dy)
564{
565 gpf_gui_shift(dx, dy);
566
567 return 1;
568}
569
570static int zoom(int wheel)
571{
572 gpf_gui_set_canvas_zoom(gui.canvas_zoom + wheel);
573 gpf_canvas_draw(gui.canvas);
574 gp_widget_redraw(gui.canvas);
575
576 return 1;
577}
578
579int gpf_canvas_input(gp_event *ev)
580{
581 int shift, alt;
582
583 if (!gui.font)
584 return 0;
585
586 switch (ev->type) {
587 case GP_EV_KEY:
588 shift = gp_ev_any_key_pressed(ev, GP_KEY_LEFT_SHIFT,
589 GP_KEY_RIGHT_SHIFT);
590
591 alt = gp_ev_any_key_pressed(ev, GP_KEY_LEFT_ALT,
592 GP_KEY_RIGHT_ALT);
593
594 if (ev->code == GP_EV_KEY_UP) {
595 if (ev->key.key == GP_BTN_LEFT ||
596 ev->key.key == GP_BTN_TOUCH)
597 return release();
598
599 return 0;
600 }
601
602 if (ev->code != GP_EV_KEY_DOWN)
603 return 0;
604
605 int inc = alt ? 5 : 1;
606
607 switch (ev->key.key) {
608 case GP_BTN_LEFT:
609 case GP_BTN_TOUCH:
610 return press(ev->st->cursor_x, ev->st->cursor_y);
611 case GP_KEY_SPACE:
612 return toggle_cursor();
613 case GP_KEY_LEFT:
614 return shift ? shift_ink(-inc, 0) : move_cursor(-inc, 0);
615 case GP_KEY_RIGHT:
616 return shift ? shift_ink(inc, 0) : move_cursor(inc, 0);
617 case GP_KEY_UP:
618 return shift ? shift_ink(0, inc) : move_cursor(0, inc);
619 case GP_KEY_DOWN:
620 return shift ? shift_ink(0, -inc) : move_cursor(0, -inc);
621 default:
622 return 0;
623 }
624 case GP_EV_REL:
625 switch (ev->code) {
626 case GP_EV_REL_POS:
627 if (gui.in_stroke)
628 return motion(ev->st->cursor_x, ev->st->cursor_y);
629
630 return hover(ev->st->cursor_x, ev->st->cursor_y);
631 case GP_EV_REL_WHEEL:
632 return zoom(ev->val);
633 break;
634 }
635 break;
636 }
637
638 return 0;
639}
The glyph canvas.
Glyph editing.
static int gpf_glyph_pixel(const struct gpf_glyph *self, unsigned int x, unsigned int y)
Returns a glyph ink pixel.
Definition font.h:623
#define GPF_MAX(a, b)
Returns the larger of two values, evaluates them twice.
Definition font.h:29
#define GPF_MIN(a, b)
Returns the smaller of two values, evaluates them twice.
Definition font.h:27
The editor state and the functions everything in the GUI goes through.
void gpf_gui_edit_begin(void)
Starts an edit, one undo entry.
Definition gpforge.c:664
struct gpf_glyph * gpf_gui_edit_glyph(void)
The glyph entry to edit in the current variant.
Definition gpforge.c:448
void gpf_gui_edit_commit(void)
Commits the edit started by gpf_gui_edit_begin() and repaints.
Definition gpforge.c:676
void gpf_gui_edited(void)
The font changed.
Definition gpforge.c:732
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
int descent
Font descent.
Definition font.h:241
int cap_height
Capital letter height.
Definition font.h:250
int x_height
The x-height.
Definition font.h:248
int ascent
Font ascent.
Definition font.h:239
A font family.
Definition font.h:274
struct gpf_family_meta meta
Font meta data.
Definition font.h:276
A glyph.
Definition font.h:80
int bearing_y
Glyph bearing y.
Definition font.h:128
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
int bearing_x
Glyph bearing x.
Definition font.h:122
A resolved glyph and where it was drawn from.
Definition resolve.h:36
struct gpf_glyph glyph
Freshly allocated glyph with copied/generated ink.
Definition resolve.h:42
A font variant.
Definition font.h:170
int advance
Advance for all glyphs.
Definition font.h:182
int spacing_mono
Every glyph has the same advance.
Definition font.h:174