11#include <utils/gp_vec.h>
12#include <utils/gp_vec_str.h>
28 return variant_names[id];
36 if (!strcmp(name, variant_names[i])) {
73 int size,
int ascent,
int descent,
int advance)
78 family && *family ? family :
"New");
85 meta->
em = ascent + descent;
151 for (i = 0; i < gp_vec_len(self->
glyphs); i++)
154 gp_vec_free(self->
glyphs);
170 gp_vec_free(self->
blocks);
208static size_t glyph_lookup(
struct gpf_variant *self, uint32_t code,
int *found)
210 size_t l = 0, r = gp_vec_len(self->
glyphs);
215 size_t m = l + (r - l) / 2;
234 size_t idx = glyph_lookup(self,
code, &found);
239 return &self->
glyphs[idx];
246 size_t idx = glyph_lookup(self,
code, &found);
249 return &self->
glyphs[idx];
252 glyphs = gp_vec_ins(self->
glyphs, idx, 1);
259 return &self->
glyphs[idx];
262static int code_cmp(
const void *a,
const void *b)
264 uint32_t ca = *(
const uint32_t *)a, cb = *(
const uint32_t *)b;
266 return ca < cb ? -1 : ca > cb;
319 uint32_t *vec = gp_vec_new(0,
sizeof(uint32_t));
333 if (!GP_VEC_APPEND(vec, variant->
glyphs[j].
code)) {
340 cnt = gp_vec_len(vec);
342 qsort(vec, cnt,
sizeof(uint32_t), code_cmp);
344 for (j = cnt; j > 1; j--) {
345 if (vec[j-1] == vec[j-2])
346 vec = gp_vec_del(vec, j-1, 1);
356 for (i = 0; i < gp_vec_len(self->
blocks); i++) {
382 for (i = 0; i < gp_vec_len(self->
blocks); i++) {
387 blocks = gp_vec_ins(self->
blocks, i, 1);
403 if (!isdigit((
unsigned char)
years[0]))
404 return "the years do not start with a year";
406 for (i = 0;
years[i]; i++) {
407 if (!isdigit((
unsigned char)
years[i]) &&
years[i] !=
'-' &&
409 return "the years are not years";
413 return "the author needs a name";
416 return "the email is not an email";
419 strlen(
name) >=
sizeof(author.
name) ||
421 return "the author is too long";
439 *authors = gp_vec_new(0,
sizeof(
struct gpf_author));
444 if (!GP_VEC_APPEND(*authors, author))
452 if (idx >= gp_vec_len(*authors))
455 *authors = gp_vec_del(*authors, idx, 1);
460 size_t len = gp_vec_len(authors);
467 memcpy(dup, authors, len *
sizeof(
struct gpf_author));
476 if (gp_vec_len(a) != gp_vec_len(b))
479 for (i = 0; i < gp_vec_len(a); i++) {
492 size_t idx = glyph_lookup(self, code, &found);
502static unsigned int variant_clear(
struct gpf_variant *self,
503 uint32_t min, uint32_t max)
505 size_t i, first = 0, cnt = 0;
507 for (i = 0; i < gp_vec_len(self->
glyphs); i++) {
530 unsigned int i, cnt = 0;
542 if (code >= min && code <= max)
553 unsigned int i, cnt = 0;
557 cnt += variant_clear(self->
variants[i], min, max);
567 for (i = 0; i < gp_vec_len(self->
blocks); i++) {
578 uint32_t code = self->
code;
581 memset(self, 0,
sizeof(
struct gpf_glyph));
586 unsigned int w,
unsigned int h,
587 int bearing_x,
int bearing_y)
590 int min_x = w, max_x = -1, min_y = h, max_y = -1;
594 for (y = 0; y < h; y++) {
595 for (x = 0; x < w; x++) {
596 if (!bits[y * w + x])
622 nw = max_x - min_x + 1;
623 nh = max_y - min_y + 1;
625 nbits = malloc(nw * nh);
629 for (y = 0; y < nh; y++) {
630 for (x = 0; x < nw; x++)
631 nbits[y * nw + x] = !!bits[(y + min_y) * w + x + min_x];
637 self->
bearing_x = bearing_x + (int)min_x;
638 self->
bearing_y = bearing_y - (int)min_y;
The in-memory font model.
struct gpf_author * gpf_authors_dup(const struct gpf_author *authors)
Copies a gp_vec of authors.
void gpf_glyph_clear(struct gpf_glyph *self)
Frees the ink and zeroes the glyph, keeping only the code.
void gpf_family_meta_guess(struct gpf_family_meta *meta, const char *family, int size, int ascent, int descent, int advance)
Fills in metadata for a font that is only being started.
int gpf_family_meta_same(const struct gpf_family_meta *a, const struct gpf_family_meta *b)
Compares two sets of font metadata.
struct gpf_glyph * gpf_variant_glyph_add(struct gpf_variant *self, uint32_t code)
Adds a glyph to a variant.
void gpf_font_free(struct gpf_font *self)
Frees a font with all its variants and glyphs.
const char * gpf_author_invalid(const char *years, const char *name, const char *email)
Checks the fields of an author.
unsigned int gpf_font_block_clear(struct gpf_font *self, uint32_t min, uint32_t max)
Deletes every glyph in a range, in all variants.
unsigned int gpf_font_range_glyphs(struct gpf_font *self, uint32_t min, uint32_t max)
Counts glyph entries in a range, in all variants.
void gpf_font_clear_modified(struct gpf_font *self)
Clears the modified flag of all glyphs in all variants.
void gpf_authors_del(struct gpf_author **authors, size_t idx)
Removes an author from a gp_vec of authors.
int gpf_font_block_declared(struct gpf_font *self, uint32_t min)
Checks whether a block is declared.
void gpf_variant_glyph_del(struct gpf_variant *self, uint32_t code)
Removes a glyph from a variant.
int gpf_authors_same(const struct gpf_author *a, const struct gpf_author *b)
Compares two gp_vecs of authors, NULL being the same as empty.
struct gpf_variant * gpf_font_variant(struct gpf_font *self, enum gpf_variant_id id, int create)
Returns a font variant.
static size_t gpf_variant_glyphs(const struct gpf_variant *self)
Returns the number of glyph entries in a variant.
gpf_variant_id
Font face variant.
@ GPF_BOLD_MONO
Bold monospace glyphs.
@ GPF_NO_PARENT
What gpf_variant_parent() returns for the base variant.
@ GPF_VARIANTS
Number of font variants.
@ GPF_MONO
Monospace glyphs, the base variant.
@ GPF_REGULAR
Proportional glyphs, "regular" on disk.
struct gpf_font * gpf_font_new(void)
Allocates an empty font.
struct gpf_glyph * gpf_variant_glyph(struct gpf_variant *self, uint32_t code)
Looks up a glyph in a variant.
uint32_t * gpf_font_codes(struct gpf_font *self)
Returns every codepoint the font mentions, in any variant.
int gpf_font_block_add(struct gpf_font *self, uint32_t min, uint32_t max, const char *name)
Declares a unicode block.
void gpf_font_ink_extent(struct gpf_font *self, int *top, int *bottom)
Returns how far the ink of the whole font reaches.
int gpf_authors_add(struct gpf_author **authors, const char *years, const char *name, const char *email)
Appends an author to a gp_vec of authors.
int gpf_glyph_set_bits(struct gpf_glyph *self, const uint8_t *bits, unsigned int w, unsigned int h, int bearing_x, int bearing_y)
Stores the glyph ink.
struct gpf_font * gpf_font_create(const struct gpf_family_meta *meta, int advance)
Creates a new font.
void gpf_font_block_del(struct gpf_font *self, uint32_t min)
Removes a block declaration.
A font author, one author line of the family file.
char years[32]
Copyright years, e.g. "2024", "2019-2024" or "2019,2023".
char email[64]
The author's email, without the angle brackets.
char name[64]
The author's name.
A declared unicode block.
char name[64]
The block name.
uint32_t max
The last codepoint of the block, inclusive.
uint32_t min
The first codepoint of the block.
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.
struct gpf_block_decl * blocks
Declared unicode blocks.
char * comments
Font comments.
int bearing_y
Glyph bearing y.
unsigned int height
Glyph ink height.
unsigned int width
Glyph ink width.
uint8_t modified
Glyph was modified flag.
int bearing_x
Glyph bearing x.
uint32_t code
Unicode code point.
int advance
Advance for all glyphs.
int derive_embolden
Glyphs not drawn here are emboldened from the parent.
enum gpf_variant_id id
Font variant e.g. GPF_REGULAR, GPF_MONO, etc.
int spacing_mono
Every glyph has the same advance.
struct gpf_glyph * glyphs
Glyphs.
char * comments
Comments above the first glyph block.