Index: trunk/src/font.c =================================================================== --- trunk/src/font.c (revision 7016) +++ trunk/src/font.c (revision 7017) @@ -29,6 +29,7 @@ #include "config.h" #include +#include #include "font.h" #include "board.h" @@ -37,6 +38,7 @@ #include "plug_io.h" #include "paths.h" #include "compat_nls.h" +#include "compat_misc.h" #define STEP_SYMBOLLINE 10 @@ -185,7 +187,6 @@ return (line); } - pcb_font_t *pcb_font(pcb_board_t *pcb, pcb_font_id_t id, int fallback) { if (id == 0) @@ -194,14 +195,56 @@ return &pcb->fontkit.dflt; /* temporary, compatibility solution */ } +static void hash_setup(pcb_fontkit_t *fk) +{ + if (fk->hash_inited) + return; + + htip_init(&fk->fonts, longhash, longkeyeq); + fk->hash_inited = 1; +} + +pcb_font_t *pcb_new_font(pcb_fontkit_t *fk, pcb_font_id_t id, const char *name) +{ + pcb_font_t *f; + + if (id == 0) + return NULL; + + hash_setup(fk); + + /* do not attempt to overwrite/reuse existing font of the same ID, rather report error */ + f = htip_get(&fk->fonts, id); + if (f != NULL) + return NULL; + + f = calloc(sizeof(pcb_font_t), 1); + htip_set(&fk->fonts, id, f); + f->name = pcb_strdup(name); + f->id = id; + + return f; +} + + static void pcb_font_free(pcb_font_t *f) { int i; for (i = 0; i <= PCB_MAX_FONTPOSITION; i++) free(f->Symbol[i].Line); + free(f->name); + f->name = NULL; + f->id = -1; } void pcb_fontkit_free(pcb_fontkit_t *fk) { pcb_font_free(&fk->dflt); + if (fk->hash_inited) { + htip_entry_t *e; + for (e = htip_first(&fk->fonts); e; e = htip_next(&fk->fonts, e)) + pcb_font_free(e->value); + htip_uninit(&fk->fonts); + fk->hash_inited = 0; + } } Index: trunk/src/font.h =================================================================== --- trunk/src/font.h (revision 7016) +++ trunk/src/font.h (revision 7017) @@ -42,7 +42,7 @@ pcb_coord_t Width, Height, Delta; /* size of cell, distance to next symbol */ } pcb_symbol_t; -typedef int pcb_font_id_t; /* a font is referenced by a pcb_board_t:pcb_font_id_t pair */ +typedef long int pcb_font_id_t; /* a font is referenced by a pcb_board_t:pcb_font_id_t pair */ struct pcb_font_s { /* complete set of symbols */ pcb_coord_t MaxHeight, MaxWidth; /* maximum cell width and height */ @@ -54,8 +54,8 @@ struct pcb_fontkit_s { /* a set of unrelated fonts */ pcb_font_t dflt; /* default, fallback font, also the sysfont */ - pcb_bool valid; htip_t fonts; + pcb_bool valid, hash_inited; }; /* Look up font. If not found: Return NULL (fallback=0), or return the @@ -70,6 +70,7 @@ /*** font kit handling ***/ void pcb_fontkit_free(pcb_fontkit_t *fk); +pcb_font_t *pcb_new_font(pcb_fontkit_t *fk, pcb_font_id_t id, const char *name); #endif