gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
cell.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#include <utils/gp_utf.h>
7
8#include "format.h"
9#include "resolve.h"
10#include "gui.h"
11#include "cell.h"
12
13void gpf_cell_init(struct gpf_cell *self, const gp_widget_render_ctx *ctx,
14 unsigned int max_h)
15{
16 struct gpf_font *font = gui.font;
17 unsigned int glyph_w = font->meta.em ? (unsigned int)font->meta.em : 8;
18 unsigned int label_w = gp_text_wbbox(ctx->font_mono, "0000");
19 unsigned int line_h, fixed;
20 int top, bottom;
21
22 /* the box is what the ink needs, which is not always ascent + descent */
23 gpf_font_ink_extent(font, &top, &bottom);
24
25 self->top = top;
26
27 line_h = GPF_MAX(1, top - bottom + 1);
28
29 /*
30 * The theme's padding, above and below both of the lines that carry a
31 * letter. A letter drawn against the line above it is hard to read,
32 * and the accent of an `À` reaches above the ascent its font reports —
33 * the padding is more than the descent, so it covers that as well.
34 */
35 self->pad = ctx->padd;
36
37 self->bg = gp_widgets_color(ctx, GP_WIDGETS_COL_FG);
38
39 self->label_h = gp_text_height(ctx->font_mono);
40
41 /* the band the reference character gets: its height and the padding */
42 self->ref_h = gp_text_height(ctx->font) + 2 * self->pad;
43
44 self->zoom = GPF_MAX(1u, gui.zoom);
45
46 fixed = self->label_h + self->ref_h + 2 * self->pad;
47
48 if (max_h > fixed) {
49 unsigned int fits = (max_h - fixed) / line_h;
50
51 self->zoom = GPF_MAX(1u, GPF_MIN(self->zoom, fits));
52 }
53
54 self->w = GPF_MAX(self->zoom * glyph_w, label_w) + 2 * self->pad;
55 self->h = self->zoom * line_h + fixed;
56
57 self->x0 = 0;
58 self->y0 = 0;
59}
60
61void gpf_cell_center(struct gpf_cell *self, const gp_pixmap *p,
62 unsigned int cols, unsigned int rows)
63{
64 /* a grid larger than the pixmap keeps its first cell in view */
65 self->x0 = GPF_MAX(0, ((int)p->w - (int)(cols * self->w)) / 2);
66 self->y0 = GPF_MAX(0, ((int)p->h - (int)(rows * self->h)) / 2);
67}
68
69int gpf_cell_at(const struct gpf_cell *self, unsigned int cols,
70 unsigned int rows, gp_coord x, gp_coord y)
71{
72 x -= self->x0;
73 y -= self->y0;
74
75 if (x < 0 || y < 0)
76 return -1;
77
78 if ((unsigned int)x / self->w >= cols ||
79 (unsigned int)y / self->h >= rows)
80 return -1;
81
82 return (y / self->h) * cols + x / self->w;
83}
84
85/*
86 * The codepoint is the status line of a cell: red when the glyph has been
87 * edited and not yet written, and inverted when this variant carries an entry
88 * for it rather than taking it from a parent. Both are answers to "what have
89 * I actually touched", which is what the table is read for.
90 */
91static void label_colors(const gp_widget_render_ctx *ctx, uint32_t code,
92 int have, gp_pixel *bg, gp_pixel *fg)
93{
94 struct gpf_variant *variant = gui.font->variants[gui.variant];
95 struct gpf_glyph *glyph = variant ? gpf_variant_glyph(variant, code)
96 : NULL;
97
98 if (gpf_gui_modified(code)) {
99 *bg = gp_widgets_color(ctx, GP_WIDGETS_COL_ALERT);
100 *fg = gp_widgets_color(ctx, GP_WIDGETS_COL_FG);
101 return;
102 }
103
104 if (!have || !glyph)
105 return;
106
107 *bg = gp_widgets_color(ctx, GP_WIDGETS_COL_TEXT);
108 *fg = gp_widgets_color(ctx, GP_WIDGETS_COL_FG);
109}
110
111/*
112 * Whether the widget font has this character, rather than the '?' it hands out
113 * for everything it has not got: a table of question marks says nothing, and
114 * the line is there to say what the letter should look like.
115 */
116static int widget_font_has(const gp_text_style *style, uint32_t code)
117{
118 if (code == '?')
119 return 1;
120
121 return gp_glyph_get(style->font, code) != gp_glyph_get(style->font, '?');
122}
123
124/*
125 * One cell: the codepoint, the reference character, the glyph. `have` says
126 * whether the font has the codepoint, which is what the label is about, and
127 * `glyph` is the ink to draw, which is not always the same thing — the compose
128 * view draws a letter the font has not got.
129 */
130static void draw_cell(gp_pixmap *p, const gp_widget_render_ctx *ctx,
131 const struct gpf_cell *self, int x, int y, uint32_t code,
132 const struct gpf_glyph *glyph, int have)
133{
134 /* the line between the reference character and the glyph */
135 int split = y + (int)self->label_h + (int)self->ref_h;
136 int oy = split + (int)self->pad + (int)self->zoom * self->top;
137 unsigned int gx, gy;
138 gp_pixel bg, ink, label_bg, label_fg;
139 char buf[8];
140 int ox;
141
142 bg = code == gui.code ? gp_widgets_color(ctx, GP_WIDGETS_COL_SELECT)
143 : self->bg;
144
145 ink = gp_widgets_color(ctx, GP_WIDGETS_COL_TEXT);
146
147 gp_fill_rect_xywh(p, x, y, self->w, self->h, bg);
148
149 label_bg = self->bg;
150 label_fg = have ? ink : gp_widgets_color(ctx, GP_WIDGETS_COL_DISABLED);
151
152 label_colors(ctx, code, have, &label_bg, &label_fg);
153
154 gp_fill_rect_xywh(p, x, y, self->w, self->label_h, label_bg);
155
156 snprintf(buf, sizeof(buf), "%04X", code);
157
158 /* only the codepoint says whether the glyph is there at all */
159 gp_text(p, ctx->font_mono, x + self->w/2, y,
160 GP_ALIGN_CENTER | GP_VALIGN_BELOW, label_fg, label_bg, buf);
161
162 /*
163 * And under it the same character as the widget font draws it, which
164 * is what the letter is supposed to look like — the only thing in the
165 * editor that does not come from the font being edited.
166 */
167 if (gpf_code_printable(code) && widget_font_has(ctx->font, code)) {
168 char ch[5];
169
170 ch[gp_to_utf8(code, ch)] = 0;
171
172 gp_text(p, ctx->font, x + self->w/2,
173 y + (int)self->label_h + (int)self->pad,
174 GP_ALIGN_CENTER | GP_VALIGN_BELOW,
175 gp_widgets_color(ctx, GP_WIDGETS_COL_DISABLED), bg, ch);
176 }
177
178 /* the line the glyph starts under, so that the two are told apart */
179 gp_hline_xxy(p, x, x + (int)self->w - 1, split,
180 gp_widgets_color(ctx, GP_WIDGETS_COL_DISABLED));
181
182 if (!glyph)
183 return;
184
185 /* the glyph sits in the middle of the cell, on its advance */
186 ox = x + ((int)self->w - (int)self->zoom * glyph->advance) / 2;
187
188 for (gy = 0; gy < glyph->height; gy++) {
189 for (gx = 0; gx < glyph->width; gx++) {
190 int px = ox + (int)self->zoom *
191 (glyph->bearing_x + (int)gx);
192 int py = oy - (int)self->zoom *
193 (glyph->bearing_y - (int)gy);
194
195 if (!gpf_glyph_pixel(glyph, gx, gy))
196 continue;
197
198 /*
199 * An accent that reaches above the ascent, or ink
200 * wider than the advance, belongs to this glyph and
201 * not to the cell next to it. The cell is the box it
202 * gets.
203 */
204 if (py <= split || py + (int)self->zoom > y + (int)self->h)
205 continue;
206
207 if (px < x || px + (int)self->zoom > x + (int)self->w)
208 continue;
209
210 gp_fill_rect_xywh(p, px, py, self->zoom, self->zoom, ink);
211 }
212 }
213}
214
215void gpf_cell_draw(gp_pixmap *p, const gp_widget_render_ctx *ctx,
216 const struct gpf_cell *self, int x, int y, uint32_t code)
217{
218 struct gpf_resolved res;
219 int have = !gpf_resolve(gui.font, gui.variant, code, &res);
220
221 draw_cell(p, ctx, self, self->x0 + x, self->y0 + y, code,
222 have ? &res.glyph : NULL, have);
223
224 if (have)
225 gpf_resolved_clear(&res);
226}
227
228void gpf_cell_draw_glyph(gp_pixmap *p, const gp_widget_render_ctx *ctx,
229 const struct gpf_cell *self, int x, int y,
230 uint32_t code, const struct gpf_glyph *glyph)
231{
232 struct gpf_resolved res;
233 int have = !gpf_resolve(gui.font, gui.variant, code, &res);
234
235 if (have)
236 gpf_resolved_clear(&res);
237
238 draw_cell(p, ctx, self, self->x0 + x, self->y0 + y, code, glyph, have);
239}
240
241void gpf_cell_grid(gp_pixmap *p, const gp_widget_render_ctx *ctx,
242 const struct gpf_cell *self, unsigned int cols,
243 unsigned int rows)
244{
245 gp_pixel col = gp_widgets_color(ctx, GP_WIDGETS_COL_DISABLED);
246 unsigned int w = cols * self->w;
247 unsigned int h = rows * self->h;
248 unsigned int i;
249
250 int x0 = self->x0, y0 = self->y0;
251
252 for (i = 0; i <= cols; i++)
253 gp_vline_xyy(p, x0 + i * self->w, y0, y0 + h, col);
254
255 for (i = 0; i <= rows; i++)
256 gp_hline_xxy(p, x0, x0 + w, y0 + i * self->h, col);
257}
A glyph cell rendering.
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
struct gpf_glyph * gpf_variant_glyph(struct gpf_variant *self, uint32_t code)
Looks up a glyph in a variant.
Definition font.c:231
void gpf_font_ink_extent(struct gpf_font *self, int *top, int *bottom)
Returns how far the ink of the whole font reaches.
Definition font.c:285
#define GPF_MIN(a, b)
Returns the smaller of two values, evaluates them twice.
Definition font.h:27
Native font format.
The editor state and the functions everything in the GUI goes through.
The glyph variant lattice.
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 glyph cell geometry.
Definition cell.h:29
gp_pixel bg
The color the cells are drawn on, the selected one is highlighted instead.
Definition cell.h:57
unsigned int label_h
The height of the codepoint line at the top.
Definition cell.h:33
unsigned int w
The cell size in pixels.
Definition cell.h:31
int x0
Where the grid starts in the pixmap, see gpf_cell_center().
Definition cell.h:49
unsigned int pad
The theme's padding, above the glyph and below it.
Definition cell.h:42
int top
How far the font's ink reaches above the baseline, at least the ascent.
Definition cell.h:40
unsigned int zoom
The pixel multiplier, the table's, or less when there is no room.
Definition cell.h:47
unsigned int ref_h
The line under it, where the widget font draws the character.
Definition cell.h:35
int em
Em size.
Definition font.h:246
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
uint32_t code
Unicode code point.
Definition font.h:84
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