20 int left, right, top, bottom;
31static int glyph_advance(
void)
36 if (
gpf_resolve(gui.font, gui.variant, gui.code, &res)) {
37 struct gpf_variant *variant = gui.font->variants[gui.variant];
39 if (variant && variant->
advance)
42 return gui.font->meta.em ? gui.font->meta.em : 8;
47 gpf_resolved_clear(&res);
52static void geometry(
struct canvas *self, gp_pixmap *p)
54 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
61 self->right = glyph_advance() + SLACK;
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;
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);
79 self->zoom = gui.canvas_zoom;
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;
93static int cx(
const struct canvas *self,
int col)
95 return self->ox + col * self->zoom;
99static int cy(
const struct canvas *self,
int height)
101 return self->oy - height * self->zoom;
108static int hit(
const struct canvas *self,
int x,
int y,
int *col,
int *height)
110 int dx = x - cx(self, self->left);
111 int dy = y - cy(self, self->top);
113 if (dx < 0 || dy < 0)
116 *col = self->left + dx / self->zoom;
117 *height = self->top - dy / self->zoom;
119 if (*col >= self->right || *height <= self->bottom)
129static gp_pixel grid_color(
const gp_widget_render_ctx *ctx)
131 if (gui.canvas && gui.canvas->focused)
132 return gp_widgets_color(ctx, GP_WIDGETS_COL_BG);
134 return gp_widgets_color(ctx, GP_WIDGETS_COL_FG);
137static void draw_grid(
const struct canvas *self, gp_pixmap *p,
138 const gp_widget_render_ctx *ctx)
140 gp_pixel col = grid_color(ctx);
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);
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);
153static void draw_guides(
const struct canvas *self, gp_pixmap *p,
154 const gp_widget_render_ctx *ctx)
157 gp_pixel base = gp_widgets_color(ctx, GP_WIDGETS_COL_BLUE);
158 gp_pixel adv = gp_widgets_color(ctx, GP_WIDGETS_COL_GREEN);
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);
168 gp_hline_xxy(p, x0, x1, cy(self, font->
meta.
x_height), hint);
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);
177 gp_hline_xxy(p, x0, x1, cy(self, 0), base);
178 gp_vline_xyy(p, cx(self, 0), y0, y1, base);
180 gp_vline_xyy(p, cx(self, self->right - SLACK), y0, y1, adv);
183static void draw_ink(
const struct canvas *self, gp_pixmap *p,
184 const gp_widget_render_ctx *ctx,
188 gp_pixel col = gp_widgets_color(ctx, GP_WIDGETS_COL_TEXT);
191 for (y = 0; y < glyph->
height; y++) {
192 for (x = 0; x < glyph->
width; x++) {
196 gp_fill_rect_xywh(p, cx(self, glyph->
bearing_x + x),
198 self->zoom, self->zoom, col);
207static void draw_cursor(
const struct canvas *self, gp_pixmap *p,
208 const gp_widget_render_ctx *ctx)
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);
213 if (gui.cur_col < self->left || gui.cur_col >= self->right)
216 if (gui.cur_height <= self->bottom || gui.cur_height > self->top)
219 gp_rect_xywh(p, x, y, self->zoom, self->zoom, col);
228static int draw_pos(gp_pixmap *p,
const gp_widget_render_ctx *ctx)
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);
242static int rule_step(
int zoom,
int size)
244 static const int steps[] = {1, 2, 5, 10, 20, 50, 100};
247 for (i = 0; i < GP_ARRAY_SIZE(steps); i++) {
248 if (steps[i] * zoom >= size)
252 return steps[GP_ARRAY_SIZE(steps) - 1];
260static void draw_rulers(
const struct canvas *self, gp_pixmap *p,
261 const gp_widget_render_ctx *ctx,
int pos_end)
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);
269 step = rule_step(self->zoom, num_w + MARGIN);
271 for (i = self->left; i < self->right; i++) {
272 int x = cx(self, i) + self->zoom / 2;
277 if (x - num_w / 2 < pos_end + MARGIN &&
278 cy(self, self->top) - MARGIN < self->rule_h)
281 gp_print(p, font, x, cy(self, self->top) - MARGIN,
282 GP_ALIGN_CENTER | GP_VALIGN_ABOVE, fg, bg,
"%i", i);
285 step = rule_step(self->zoom, gp_text_height(font));
287 for (i = self->bottom + 1; i <= self->top; i++) {
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);
297void gpf_canvas_draw(gp_widget *self)
299 gp_pixmap *p = gp_widget_pixmap_get(self);
300 const gp_widget_render_ctx *ctx = gp_widgets_render_ctx();
302 struct canvas canvas;
308 gp_fill(p, gpf_gui_back_color(gui.canvas));
314 geometry(&canvas, p);
316 draw_grid(&canvas, p, ctx);
317 draw_guides(&canvas, p, ctx);
319 have = !gpf_resolve(gui.font, gui.variant, gui.code, &res);
322 draw_ink(&canvas, p, ctx, &res);
323 gpf_resolved_clear(&res);
326 draw_cursor(&canvas, p, ctx);
327 draw_rulers(&canvas, p, ctx, draw_pos(p, ctx));
334static void paint(
int col,
int height,
int set)
337 gui.cur_height = height;
339 gpf_gui_pixel_set(col, height, set);
353static enum drag drag_at(
const struct canvas *canvas,
int x)
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];
359 if (abs(x - cx(canvas, 0)) <= band)
366 if (abs(x - cx(canvas,
advance)) <= band)
372static void drag_to(
int col)
390 if (col == gui.drag_col)
394 gpf_glyph_shift(glyph, gui.drag_col - col, 0);
404static int press(
int x,
int y)
406 gp_pixmap *p = gp_widget_pixmap_get(gui.canvas);
407 struct canvas canvas;
409 int col, height, set;
414 geometry(&canvas, p);
416 if (hit(&canvas, x, y, &col, &height))
419 gui.drag = drag_at(&canvas, x);
436 if (!
gpf_resolve(gui.font, gui.variant, gui.code, &res)) {
437 set = !gpf_glyph_pixel_get(&res.
glyph, col, height);
446 paint(col, height, set);
451static int motion(
int x,
int y)
453 gp_pixmap *p = gp_widget_pixmap_get(gui.canvas);
454 struct canvas canvas;
457 if (!p || !gui.in_stroke)
460 geometry(&canvas, p);
462 if (hit(&canvas, x, y, &col, &height))
470 paint(col, height, gui.paint_val);
479static int hover(
int x,
int y)
481 gp_pixmap *p = gp_widget_pixmap_get(gui.canvas);
482 struct canvas canvas;
488 geometry(&canvas, p);
490 if (hit(&canvas, x, y, &col, &height))
493 if (col == gui.cur_col && height == gui.cur_height)
497 gui.cur_height = height;
499 gpf_canvas_draw(gui.canvas);
500 gp_widget_redraw(gui.canvas);
505static int release(
void)
511 gui.drag = DRAG_NONE;
518static int toggle_cursor(
void)
523 if (!
gpf_resolve(gui.font, gui.variant, gui.code, &res)) {
524 set = !gpf_glyph_pixel_get(&res.
glyph, gui.cur_col,
531 paint(gui.cur_col, gui.cur_height, set);
538static int move_cursor(
int dx,
int dy)
540 gp_pixmap *p = gp_widget_pixmap_get(gui.canvas);
541 struct canvas canvas;
546 geometry(&canvas, p);
550 gui.cur_height =
GPF_MAX(canvas.bottom + 1,
551 GPF_MIN(canvas.top, gui.cur_height + dy));
553 gpf_canvas_draw(gui.canvas);
554 gp_widget_redraw(gui.canvas);
563static int shift_ink(
int dx,
int dy)
565 gpf_gui_shift(dx, dy);
570static int zoom(
int wheel)
572 gpf_gui_set_canvas_zoom(gui.canvas_zoom + wheel);
573 gpf_canvas_draw(gui.canvas);
574 gp_widget_redraw(gui.canvas);
579int gpf_canvas_input(gp_event *ev)
588 shift = gp_ev_any_key_pressed(ev, GP_KEY_LEFT_SHIFT,
591 alt = gp_ev_any_key_pressed(ev, GP_KEY_LEFT_ALT,
594 if (ev->code == GP_EV_KEY_UP) {
595 if (ev->key.key == GP_BTN_LEFT ||
596 ev->key.key == GP_BTN_TOUCH)
602 if (ev->code != GP_EV_KEY_DOWN)
605 int inc = alt ? 5 : 1;
607 switch (ev->key.key) {
610 return press(ev->st->cursor_x, ev->st->cursor_y);
612 return toggle_cursor();
614 return shift ? shift_ink(-inc, 0) : move_cursor(-inc, 0);
616 return shift ? shift_ink(inc, 0) : move_cursor(inc, 0);
618 return shift ? shift_ink(0, inc) : move_cursor(0, inc);
620 return shift ? shift_ink(0, -inc) : move_cursor(0, -inc);
628 return motion(ev->st->cursor_x, ev->st->cursor_y);
630 return hover(ev->st->cursor_x, ev->st->cursor_y);
631 case GP_EV_REL_WHEEL:
632 return zoom(ev->val);
static int gpf_glyph_pixel(const struct gpf_glyph *self, unsigned int x, unsigned int y)
Returns a glyph ink pixel.
#define GPF_MAX(a, b)
Returns the larger of two values, evaluates them twice.
#define GPF_MIN(a, b)
Returns the smaller of two values, evaluates them twice.
The editor state and the functions everything in the GUI goes through.
void gpf_gui_edit_begin(void)
Starts an edit, one undo entry.
struct gpf_glyph * gpf_gui_edit_glyph(void)
The glyph entry to edit in the current variant.
void gpf_gui_edit_commit(void)
Commits the edit started by gpf_gui_edit_begin() and repaints.
void gpf_gui_edited(void)
The font changed.
void gpf_resolved_clear(struct gpf_resolved *self)
Clears the resolved glyph.
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.
struct gpf_family_meta meta
Font meta data.
int bearing_y
Glyph bearing y.
unsigned int height
Glyph ink height.
unsigned int width
Glyph ink width.
int advance
Glyph advance.
int bearing_x
Glyph bearing x.
A resolved glyph and where it was drawn from.
struct gpf_glyph glyph
Freshly allocated glyph with copied/generated ink.
int advance
Advance for all glyphs.
int spacing_mono
Every glyph has the same advance.