lorid

convert chordpro to pdf
git clone git://git.relim.de/lorid.git
Log | Files | Refs | README | LICENSE

commit 21fabeb3250c5aafc8e7e77a57ee50b520cb5a13
parent 2aa159ea45aee41133fd3b97e11d4091a1d96a29
Author: nibo <nibo@relim.de>
Date:   Thu, 13 Mar 2025 19:17:27 +0100

Prevent load a font file multiple times

Also take the label font into account
in `fonts_get_all()`

Closes #11

Diffstat:
Msrc/out_pdf.c | 58+++++++++++++++++++++++++++++++++++++++++++---------------
Msrc/util.c | 15+++++++++++++++
Msrc/util.h | 3++-
3 files changed, 60 insertions(+), 16 deletions(-)

diff --git a/src/out_pdf.c b/src/out_pdf.c @@ -190,6 +190,13 @@ fonts_get_all(struct ChoSong **songs, struct Config *config) } } for (se = (*so)->sections; *se; se++) { + if ((*se)->label && (*se)->label->style->font->name) { + font = cho_font_copy((*se)->label->style->font); + added = fonts_add_if_not_in(&fonts, font); + if (!added) { + cho_font_free(font); + } + } for (li = (*se)->lines; *li; li++) { for (above = (*li)->text_above; *above; above++) { if ((*above)->is_chord) { @@ -370,7 +377,9 @@ static bool pdf_load_fonts(struct Font **needed_fonts, struct Config *config) { char *fontpath; + char **fontpaths = NULL; const char *name; + int index; struct Font **f; struct Obj *fnt; for (f = needed_fonts; *f; f++) { @@ -378,13 +387,19 @@ pdf_load_fonts(struct Font **needed_fonts, struct Config *config) fontpath = filepath_resolve_tilde((*f)->name); fnt = obj_new(); fnt->name = fnt_name_create(*f); - fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, fontpath, true); - if (!fnt->value) { - LOG_DEBUG("pdfioFileCreateFontObjFromFile failed."); - return false; + index = strs_get_index_if_in(fontpaths, fontpath); + if (index == -1) { + fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, fontpath, true); + if (!fnt->value) { + LOG_DEBUG("pdfioFileCreateFontObjFromFile failed."); + return false; + } + strs_add(&fontpaths, fontpath); + util_log(NULL, 0, LOG_INFO, "Loaded font from '%s'.", (*f)->name); + } else { + fnt->value = g_fonts[index]->value; } free(fontpath); - util_log(NULL, 0, LOG_INFO, "Loaded font from '%s'.", (*f)->name); // cho_font_print(*f); objs_add_obj(&g_fonts, fnt); } else @@ -402,12 +417,18 @@ pdf_load_fonts(struct Font **needed_fonts, struct Config *config) if (fontpath) { fnt = obj_new(); fnt->name = fnt_name_create(*f); - fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, fontpath, true); - if (!fnt->value) { - LOG_DEBUG("pdfioFileCreateFontObjFromFile failed."); - return false; + index = strs_get_index_if_in(fontpaths, fontpath); + if (index == -1) { + fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, fontpath, true); + if (!fnt->value) { + LOG_DEBUG("pdfioFileCreateFontObjFromFile failed."); + return false; + } + strs_add(&fontpaths, fontpath); + util_log(NULL, 0, LOG_INFO, "Loaded font from '%s'.", fontpath); + } else { + fnt->value = g_fonts[index]->value; } - util_log(NULL, 0, LOG_INFO, "Loaded font from '%s'.", fontpath); // cho_font_print(*f); objs_add_obj(&g_fonts, fnt); free(fontpath); @@ -416,12 +437,18 @@ pdf_load_fonts(struct Font **needed_fonts, struct Config *config) if (fontpath) { fnt = obj_new(); fnt->name = fnt_name_create(*f); - fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, fontpath, true); - if (!fnt->value) { - LOG_DEBUG("pdfioFileCreateFontObjFromFile failed."); - return false; + index = strs_get_index_if_in(fontpaths, fontpath); + if (index == -1) { + fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, fontpath, true); + if (!fnt->value) { + LOG_DEBUG("pdfioFileCreateFontObjFromFile failed."); + return false; + } + strs_add(&fontpaths, fontpath); + util_log(NULL, 0, LOG_INFO, "Loaded font from '%s'.", fontpath); + } else { + fnt->value = g_fonts[index]->value; } - util_log(NULL, 0, LOG_INFO, "Loaded font from '%s'.", fontpath); // cho_font_print(*f); objs_add_obj(&g_fonts, fnt); free(fontpath); @@ -439,6 +466,7 @@ pdf_load_fonts(struct Font **needed_fonts, struct Config *config) return false; } } + strs_free(fontpaths); return true; } diff --git a/src/util.c b/src/util.c @@ -294,6 +294,21 @@ strs_add(char ***strs, const char *str) (*strs)[i+1] = NULL; } +int +strs_get_index_if_in(char **strs, const char *str) +{ + if (!strs) { + return -1; + } + int i; + for (i = 0; strs[i]; i++) { + if (!strcmp(strs[i], str)) { + return i; + } + } + return -1; +} + void strs_free(char **strs) { diff --git a/src/util.h b/src/util.h @@ -40,9 +40,10 @@ bool str_starts_with(const char *str, const char *part); char *str_normalize(const char *str); char *str_trim(const char *str); char *str_remove_leading_whitespace(const char *str); -void strs_free(char **strs); bool strs_has(char **strs, const char *str); void strs_add(char ***strs, const char *str); +int strs_get_index_if_in(char **strs, const char *str); +void strs_free(char **strs); int str_compare(const char *a, const char *b); long str_to_number(const char *str);