gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
font.h File Reference

The in-memory font model. More...

#include <stdint.h>
#include <stddef.h>
#include <utils/gp_vec.h>

Go to the source code of this file.

Data Structures

struct  gpf_glyph
 A glyph. More...
struct  gpf_variant
 A font variant. More...
struct  gpf_block_decl
 A declared unicode block. More...
struct  gpf_author
 A font author, one author line of the family file. More...
struct  gpf_family_meta
 Font family meta data. More...
struct  gpf_font
 A font family. More...

Macros

#define GPF_NAME_MAX   64
 Size of the name buffers, including the terminating zero.
#define GPF_YEARS_MAX   32
 Size of the author years buffer, including the terminating zero.
#define GPF_MIN(a, b)
 Returns the smaller of two values, evaluates them twice.
#define GPF_MAX(a, b)
 Returns the larger of two values, evaluates them twice.

Enumerations

enum  gpf_variant_id {
  GPF_MONO , GPF_REGULAR , GPF_BOLD_MONO , GPF_BOLD ,
  GPF_VARIANTS , GPF_NO_PARENT = GPF_VARIANTS
}
 Font face variant. More...
enum  gpf_glyph_kind { GPF_GLYPH_INK , GPF_GLYPH_METRICS , GPF_GLYPH_COMPOSE }
 A glyph entry type. More...

Functions

const char * gpf_variant_name (enum gpf_variant_id id)
 Returns the name of a variant.
int gpf_variant_by_name (const char *name, enum gpf_variant_id *id)
 Looks up a variant by its on-disk name.
enum gpf_variant_id gpf_variant_parent (enum gpf_variant_id id)
 Returns the variant a variant derives its missing glyphs from.
struct gpf_font * gpf_font_new (void)
 Allocates an empty font.
void gpf_font_free (struct gpf_font *self)
 Frees a font with all its variants and glyphs.
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.
struct gpf_font * gpf_font_create (const struct gpf_family_meta *meta, int advance)
 Creates a new font.
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_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.
uint32_t * gpf_font_codes (struct gpf_font *self)
 Returns every codepoint the font mentions, in any variant.
void gpf_font_clear_modified (struct gpf_font *self)
 Clears the modified flag of all glyphs in all variants.
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_font_block_add (struct gpf_font *self, uint32_t min, uint32_t max, const char *name)
 Declares a unicode block.
void gpf_font_block_del (struct gpf_font *self, uint32_t min)
 Removes a block declaration.
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.
int gpf_font_block_declared (struct gpf_font *self, uint32_t min)
 Checks whether a block is declared.
const char * gpf_author_invalid (const char *years, const char *name, const char *email)
 Checks the fields of an author.
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.
void gpf_authors_del (struct gpf_author **authors, size_t idx)
 Removes an author from a gp_vec of authors.
struct gpf_author * gpf_authors_dup (const struct gpf_author *authors)
 Copies a gp_vec of authors.
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_glyph * gpf_variant_glyph (struct gpf_variant *self, uint32_t code)
 Looks up a glyph in a variant.
struct gpf_glyph * gpf_variant_glyph_add (struct gpf_variant *self, uint32_t code)
 Adds a glyph to a variant.
void gpf_variant_glyph_del (struct gpf_variant *self, uint32_t code)
 Removes a glyph from a variant.
void gpf_glyph_clear (struct gpf_glyph *self)
 Frees the ink and zeroes the glyph, keeping only the code.
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.
static int gpf_glyph_pixel (const struct gpf_glyph *self, unsigned int x, unsigned int y)
 Returns a glyph ink pixel.

Detailed Description

The in-memory font model.

A font is a family of four variants: mono, regular, bold-mono and bold. Each variant holds a sparse array of glyphs sorted by unicode codepoint, which matches the on-disk order so that saving reshuffles nothing.

Definition in file font.h.

Enumeration Type Documentation

◆ gpf_glyph_kind

A glyph entry type.

What a glyph entry stored in a variant carries: the ink itself, numbers only with the ink derived from the parent, or a composition.

Enumerator
GPF_GLYPH_INK 

Glyph with ink.

The complete glyph is stored as ink. The advance and both bearings are valid.

GPF_GLYPH_METRICS 

Glyph with metrics.

Metrics are stored on disk, the ink comes from the parent.

GPF_GLYPH_COMPOSE 

Glyph is composed from base + accent.

Ink is base + accent, metrics come from the base.

Definition at line 55 of file font.h.

◆ gpf_variant_id

Font face variant.

Enumerator
GPF_MONO 

Monospace glyphs, the base variant.

GPF_REGULAR 

Proportional glyphs, "regular" on disk.

GPF_BOLD_MONO 

Bold monospace glyphs.

GPF_BOLD 

Bold glyphs.

GPF_VARIANTS 

Number of font variants.

GPF_NO_PARENT 

What gpf_variant_parent() returns for the base variant.

Definition at line 34 of file font.h.

Function Documentation

◆ gpf_author_invalid()

const char * gpf_author_invalid ( const char * years,
const char * name,
const char * email )

Checks the fields of an author.

The years are digits, '-' for a range and ',' for a list, and start with a digit; the name is not empty; the email has an '@' and no whitespace or angle brackets; and all of them fit struct gpf_author.

Parameters
yearsCopyright years, e.g. "2019-2024".
nameThe author's name.
emailThe author's email, without the angle brackets.
Returns
NULL when the author is valid, what is wrong with it otherwise.

Definition at line 397 of file font.c.

References gpf_author::email, gpf_author::name, and gpf_author::years.

Referenced by gpf_authors_add().

◆ gpf_authors_add()

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.

Parameters
authorsA gp_vec of authors, allocated when it is NULL.
yearsCopyright years, e.g. "2019-2024".
nameThe author's name.
emailThe author's email, without the angle brackets.
Returns
Non-zero on an allocation failure or when gpf_author_invalid() refuses the author.

Definition at line 426 of file font.c.

References gpf_author::email, gpf_author_invalid(), gpf_author::name, and gpf_author::years.

◆ gpf_authors_del()

void gpf_authors_del ( struct gpf_author ** authors,
size_t idx )

Removes an author from a gp_vec of authors.

Parameters
authorsA gp_vec of authors.
idxThe index of the author to remove, out of range is a no-op.

Definition at line 450 of file font.c.

◆ gpf_authors_dup()

struct gpf_author * gpf_authors_dup ( const struct gpf_author * authors)

Copies a gp_vec of authors.

Parameters
authorsA gp_vec of authors, may be NULL.
Returns
A new gp_vec, empty for NULL, or NULL on an allocation failure.

Definition at line 458 of file font.c.

◆ gpf_authors_same()

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.

Parameters
aA gp_vec of authors, may be NULL.
bA gp_vec of authors, may be NULL.
Returns
Non-zero when they hold the same authors in the same order.

Definition at line 472 of file font.c.

References gpf_author::email, gpf_author::name, and gpf_author::years.

◆ gpf_family_meta_guess()

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.

Sets what is asked for and guesses the rest, so that nothing is zero; the guesses are to be overwritten as the font is drawn.

Parameters
metaThe metadata to fill in.
familyA family name, "New" is used when NULL or empty.
sizeFont size.
ascentFont ascent.
descentFont descent.
advanceThe mono advance, used as the ch_width.

Definition at line 72 of file font.c.

References gpf_family_meta::ascent, gpf_family_meta::cap_height, gpf_family_meta::ch_width, gpf_family_meta::default_glyph, gpf_family_meta::descent, gpf_family_meta::em, gpf_family_meta::family, gpf_font::meta, gpf_family_meta::overline_pos, gpf_family_meta::overline_thickness, gpf_family_meta::size, gpf_family_meta::strike_pos, gpf_family_meta::strike_thickness, gpf_family_meta::underline_pos, gpf_family_meta::underline_thickness, and gpf_family_meta::x_height.

◆ gpf_family_meta_same()

int gpf_family_meta_same ( const struct gpf_family_meta * a,
const struct gpf_family_meta * b )

◆ gpf_font_block_add()

int gpf_font_block_add ( struct gpf_font * self,
uint32_t min,
uint32_t max,
const char * name )

Declares a unicode block.

Declaring an already declared block is a no-op.

Parameters
selfA font.
minThe first codepoint of the block.
maxThe last codepoint of the block.
nameThe block name, may be NULL.
Returns
Zero on success, non-zero on an allocation failure.

Definition at line 364 of file font.c.

References gpf_font::blocks, gpf_font_block_declared(), gpf_block_decl::max, gpf_block_decl::min, and gpf_block_decl::name.

◆ gpf_font_block_clear()

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.

Parameters
selfA font.
minThe first codepoint of the range.
maxThe last codepoint of the range, inclusive.
Returns
The number of glyph entries deleted.

Definition at line 550 of file font.c.

References GPF_VARIANTS, and gpf_font::variants.

◆ gpf_font_block_declared()

int gpf_font_block_declared ( struct gpf_font * self,
uint32_t min )

Checks whether a block is declared.

Parameters
selfA font.
minThe first codepoint of the block.
Returns
Non-zero when a block starting at min is declared.

Definition at line 352 of file font.c.

References gpf_font::blocks, and gpf_block_decl::min.

Referenced by gpf_font_block_add().

◆ gpf_font_block_del()

void gpf_font_block_del ( struct gpf_font * self,
uint32_t min )

Removes a block declaration.

Does not touch any glyphs: a block that has glyphs stays on the font's list regardless.

Parameters
selfA font.
minThe first codepoint of the block, an undeclared one is a no-op.

Definition at line 563 of file font.c.

References gpf_font::blocks, and gpf_block_decl::min.

◆ gpf_font_clear_modified()

void gpf_font_clear_modified ( struct gpf_font * self)

Clears the modified flag of all glyphs in all variants.

Forgets which glyphs were edited, which is what writing the font makes true.

Parameters
selfA font.

Definition at line 269 of file font.c.

References gpf_variant::glyphs, gpf_variant_glyphs(), GPF_VARIANTS, gpf_glyph::modified, and gpf_font::variants.

◆ gpf_font_codes()

uint32_t * gpf_font_codes ( struct gpf_font * self)

Returns every codepoint the font mentions, in any variant.

Parameters
selfA font.
Returns
A sorted gp_vec of unique codepoints, free it with gp_vec_free(), NULL on an allocation failure.

Definition at line 317 of file font.c.

References gpf_glyph::code, gpf_variant::glyphs, gpf_variant_glyphs(), GPF_VARIANTS, and gpf_font::variants.

Referenced by gpf_face_build().

◆ gpf_font_create()

struct gpf_font * gpf_font_create ( const struct gpf_family_meta * meta,
int advance )

Creates a new font.

The font has the metadata and an empty mono variant of the advance, the other variants appear when something is drawn in them.

Parameters
metaFont metadata, copied.
advanceThe advance of the mono variant.
Returns
A new font or NULL on an allocation failure.

Definition at line 100 of file font.c.

References gpf_variant::advance, gpf_font_free(), gpf_font_new(), gpf_font_variant(), GPF_MONO, and gpf_font::meta.

◆ gpf_font_free()

void gpf_font_free ( struct gpf_font * self)

Frees a font with all its variants and glyphs.

Parameters
selfA font, may be NULL.

Definition at line 159 of file font.c.

References gpf_font::authors, gpf_font::blocks, gpf_font::comments, GPF_VARIANTS, and gpf_font::variants.

Referenced by gpf_font_create(), and gpf_font_read().

◆ gpf_font_ink_extent()

void gpf_font_ink_extent ( struct gpf_font * self,
int * top,
int * bottom )

Returns how far the ink of the whole font reaches.

This is not always the ascent and the descent: an accent that does not fit over a capital sticks out of the box, and a cell that is drawn as tall as the box would cut it off. The result is never smaller than the box.

Parameters
selfA font.
topSet to the highest ink row, positive up from the baseline.
bottomSet to the lowest ink row, positive up from the baseline.

Definition at line 285 of file font.c.

References gpf_family_meta::ascent, gpf_glyph::bearing_y, gpf_family_meta::descent, gpf_variant::glyphs, gpf_variant_glyphs(), GPF_VARIANTS, gpf_glyph::height, gpf_font::meta, and gpf_font::variants.

Referenced by gpf_cell_init().

◆ gpf_font_new()

struct gpf_font * gpf_font_new ( void )

Allocates an empty font.

The font has no variants and zeroed metadata apart from the line thicknesses, which are set to one.

Returns
A new font or NULL on an allocation failure.

Definition at line 58 of file font.c.

References gpf_font::meta, gpf_family_meta::overline_thickness, gpf_family_meta::strike_thickness, and gpf_family_meta::underline_thickness.

Referenced by gpf_bdf_read(), gpf_font_create(), and gpf_font_read().

◆ gpf_font_range_glyphs()

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.

Parameters
selfA font.
minThe first codepoint of the range.
maxThe last codepoint of the range, inclusive.
Returns
The number of glyph entries in the range.

Definition at line 527 of file font.c.

References gpf_glyph::code, gpf_variant::glyphs, gpf_variant_glyphs(), GPF_VARIANTS, and gpf_font::variants.

◆ gpf_font_variant()

struct gpf_variant * gpf_font_variant ( struct gpf_font * self,
enum gpf_variant_id id,
int create )

Returns a font variant.

A newly allocated variant is empty, has spacing_mono set for the mono variants and derive_embolden set for the bold ones.

Parameters
selfA font.
idA variant id.
createAllocate the variant when it is missing.
Returns
The variant, NULL when it is missing and create is not set, on an invalid id or on an allocation failure.

Definition at line 175 of file font.c.

References gpf_variant::derive_embolden, gpf_variant::glyphs, GPF_BOLD, GPF_BOLD_MONO, GPF_MONO, GPF_VARIANTS, gpf_variant::id, gpf_variant::spacing_mono, and gpf_font::variants.

Referenced by gpf_font_create().

◆ gpf_glyph_clear()

void gpf_glyph_clear ( struct gpf_glyph * self)

Frees the ink and zeroes the glyph, keeping only the code.

Parameters
selfA glyph.

Definition at line 576 of file font.c.

References gpf_glyph::bits, and gpf_glyph::code.

◆ gpf_glyph_pixel()

int gpf_glyph_pixel ( const struct gpf_glyph * self,
unsigned int x,
unsigned int y )
inlinestatic

Returns a glyph ink pixel.

Parameters
selfA glyph.
xThe column in the trimmed ink.
yThe row in the trimmed ink.
Returns
Non-zero when the pixel is inked, zero outside of the ink.

Definition at line 623 of file font.h.

References gpf_glyph::bits, gpf_glyph::height, and gpf_glyph::width.

Referenced by gpf_compose_remainder().

◆ gpf_glyph_set_bits()

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.

Trims empty rows and columns and adjusts the bearings. An empty bitmap produces an empty glyph with zero bearings.

Parameters
selfA glyph.
bitsThe ink, one byte per pixel, w * h, row major, copied.
wThe bitmap width.
hThe bitmap height.
bearing_xThe bearing x of the bitmap left column.
bearing_yThe bearing y of the bitmap top row.
Returns
Zero on success, non-zero on an allocation failure.

Definition at line 585 of file font.c.

References gpf_glyph::bearing_x, gpf_glyph::bearing_y, gpf_glyph::bits, gpf_glyph::height, and gpf_glyph::width.

◆ gpf_variant_by_name()

int gpf_variant_by_name ( const char * name,
enum gpf_variant_id * id )

Looks up a variant by its on-disk name.

Parameters
nameA variant name, e.g. "regular".
idSet to the variant id on success.
Returns
Zero on success, non-zero for an unknown name.

Definition at line 31 of file font.c.

References GPF_VARIANTS.

◆ gpf_variant_glyph()

struct gpf_glyph * gpf_variant_glyph ( struct gpf_variant * self,
uint32_t code )

Looks up a glyph in a variant.

Parameters
selfA font variant.
codeA unicode codepoint.
Returns
The glyph or NULL when the variant does not have it. The pointer is invalidated by adding or removing glyphs.

Definition at line 231 of file font.c.

References gpf_glyph::code, and gpf_variant::glyphs.

Referenced by gpf_gui_modified(), and gpf_resolve_derived().

◆ gpf_variant_glyph_add()

struct gpf_glyph * gpf_variant_glyph_add ( struct gpf_variant * self,
uint32_t code )

Adds a glyph to a variant.

The new glyph is zeroed apart from the code, i.e. an empty GPF_GLYPH_INK.

Parameters
selfA font variant.
codeA unicode codepoint.
Returns
The new glyph, the existing one when the variant already has it, or NULL on an allocation failure. The pointer is invalidated by adding or removing glyphs.

Definition at line 242 of file font.c.

References gpf_glyph::code, and gpf_variant::glyphs.

◆ gpf_variant_glyph_del()

void gpf_variant_glyph_del ( struct gpf_variant * self,
uint32_t code )

Removes a glyph from a variant.

Reverting an overlay is exactly this.

Parameters
selfA font variant.
codeA unicode codepoint, a missing one is a no-op.

Definition at line 489 of file font.c.

References gpf_glyph::bits, and gpf_variant::glyphs.

Referenced by gpf_font_simplify().

◆ gpf_variant_glyphs()

size_t gpf_variant_glyphs ( const struct gpf_variant * self)
inlinestatic

Returns the number of glyph entries in a variant.

Parameters
selfA font variant.
Returns
The length of the glyphs array.

Definition at line 411 of file font.h.

References gpf_variant::glyphs.

Referenced by gpf_font_clear_modified(), gpf_font_codes(), gpf_font_ink_extent(), gpf_font_range_glyphs(), and gpf_font_simplify().

◆ gpf_variant_name()

const char * gpf_variant_name ( enum gpf_variant_id id)

Returns the name of a variant.

Parameters
idA variant id.
Returns
The on-disk name, e.g. "bold-mono", NULL for an invalid id.

Definition at line 23 of file font.c.

References GPF_VARIANTS.

◆ gpf_variant_parent()

enum gpf_variant_id gpf_variant_parent ( enum gpf_variant_id id)

Returns the variant a variant derives its missing glyphs from.

Regular and bold-mono derive from mono, bold derives from regular.

Parameters
idA variant id.
Returns
The parent variant id, GPF_NO_PARENT for mono.

Definition at line 45 of file font.c.

References GPF_BOLD, GPF_BOLD_MONO, GPF_MONO, GPF_NO_PARENT, and GPF_REGULAR.

Referenced by gpf_entry_form(), and gpf_font_simplify().