gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
edit.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 "edit.h"
10
11int gpf_glyph_pixel_get(const struct gpf_glyph *self, int col, int height)
12{
13 int x = col - self->bearing_x;
14 int y = self->bearing_y - height;
15
16 if (x < 0 || y < 0 || x >= (int)self->width || y >= (int)self->height)
17 return 0;
18
19 return self->bits[y * self->width + x];
20}
21
22int gpf_glyph_pixel_set(struct gpf_glyph *self, int col, int height, int set)
23{
24 int left, right, top, bottom;
25 unsigned int w, h, x, y;
26 uint8_t *bits;
27 int ret;
28
29 if (gpf_glyph_pixel_get(self, col, height) == !!set)
30 return 0;
31
32 if (self->width) {
33 left = GPF_MIN(self->bearing_x, col);
34 right = GPF_MAX(self->bearing_x + (int)self->width, col + 1);
35 top = GPF_MAX(self->bearing_y, height);
36 bottom = GPF_MIN(self->bearing_y - (int)self->height, height - 1);
37 } else {
38 if (!set)
39 return 0;
40
41 left = col;
42 right = col + 1;
43 top = height;
44 bottom = height - 1;
45 }
46
47 w = right - left;
48 h = top - bottom;
49
50 bits = calloc(1, (size_t)w * h);
51 if (!bits)
52 return 1;
53
54 for (y = 0; y < self->height; y++) {
55 for (x = 0; x < self->width; x++) {
56 bits[(y + top - self->bearing_y) * w +
57 x + self->bearing_x - left] =
58 self->bits[y * self->width + x];
59 }
60 }
61
62 bits[(top - height) * w + col - left] = !!set;
63
64 /* set_bits() trims, which is how clearing shrinks the box back */
65 ret = gpf_glyph_set_bits(self, bits, w, h, left, top);
66
67 free(bits);
68
69 return ret;
70}
71
72void gpf_glyph_shift(struct gpf_glyph *self, int dx, int dy)
73{
74 if (!self->width)
75 return;
76
77 self->bearing_x += dx;
78 self->bearing_y += dy;
79}
80
81void gpf_glyph_flip_h(struct gpf_glyph *self)
82{
83 unsigned int x, y;
84
85 for (y = 0; y < self->height; y++) {
86 for (x = 0; x < self->width / 2; x++) {
87 uint8_t tmp = self->bits[y * self->width + x];
88
89 self->bits[y * self->width + x] =
90 self->bits[y * self->width + self->width - x - 1];
91 self->bits[y * self->width + self->width - x - 1] = tmp;
92 }
93 }
94
95 if (self->width)
96 self->bearing_x = self->advance - self->bearing_x - self->width;
97}
98
99void gpf_glyph_flip_v(struct gpf_glyph *self)
100{
101 unsigned int x, y;
102
103 for (y = 0; y < self->height / 2; y++) {
104 for (x = 0; x < self->width; x++) {
105 uint8_t tmp = self->bits[y * self->width + x];
106
107 self->bits[y * self->width + x] =
108 self->bits[(self->height - y - 1) * self->width + x];
109 self->bits[(self->height - y - 1) * self->width + x] = tmp;
110 }
111 }
112}
113
114void gpf_glyph_clear_ink(struct gpf_glyph *self)
115{
116 free(self->bits);
117
118 self->bits = NULL;
119 self->width = 0;
120 self->height = 0;
121 self->bearing_x = 0;
122 self->bearing_y = 0;
123}
Glyph editing.
#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
A glyph.
Definition font.h:80
uint8_t * bits
Glyph ink.
Definition font.h:145
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