lorid

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

commit 27fe59799de8ec440007987e0da7eb51dc01b5cd
parent fe1d1a07ca7fb9716f59ffd8d3b4899aa6785972
Author: nibo <nibo@relim.de>
Date:   Fri,  3 Jan 2025 19:44:08 +0100

Remove unnecessary function name prefixes

If I use a function from another file I find
it nice if that function name has a prefix
that I directly know where it's coming from
but if the function is static that prefix is
not needed.

In chordpro.c I leave most of the prefixes
cause it just looks better I think. Not consequent.
hmm..

Diffstat:
Mout_pdf.c | 183+++++++++++++++++++++++++++++++++++++------------------------------------------
Mutil.c | 35++++++++++++++++++++++++++---------
Mutil.h | 3++-
3 files changed, 113 insertions(+), 108 deletions(-)

diff --git a/out_pdf.c b/out_pdf.c @@ -32,6 +32,40 @@ out_pdf_fnt_obj_get_by_name(const char *name) return NULL; } +static char * +fnt_name_create(struct Font *font) +{ + char *name = str_normalize(font->name); + const char *family = cho_font_family_to_config_string(font->family); + const char *style = cho_font_style_to_config_string(font->style); + const char *weight = cho_font_weight_to_config_string(font->weight); + size_t len = strlen(name) + strlen(family) + strlen(style) + strlen(weight); + int n = 0; + int i; + char *fnt_name = emalloc((len + 4) * sizeof(char)); + for (i = 0; name[i]; i++, n++) { + fnt_name[n] = name[i]; + } + fnt_name[n] = '-'; + n++; + for (i = 0; family[i]; i++, n++) { + fnt_name[n] = family[i]; + } + fnt_name[n] = '-'; + n++; + for (i = 0; style[i]; i++, n++) { + fnt_name[n] = style[i]; + } + fnt_name[n] = '-'; + n++; + for (i = 0; weight[i]; i++, n++) { + fnt_name[n] = weight[i]; + } + fnt_name[n] = 0; + free(name); + return fnt_name; +} + static bool fonts_add_if_not_in(struct Font ***array, struct Font *font) { @@ -150,21 +184,6 @@ fonts_get_all(struct ChoSong **songs, struct Config *config) return fonts; } -static bool -file_extension_is_ttc(const char *filepath) -{ - int mark = -1; - int i; - for (i = 0; filepath[i]; i++) { - if (filepath[i] == '.') { - mark = i; - } - } - if (!strcmp(&filepath[mark+1], "ttc")) - return true; - return false; -} - static char * fontpath_find(struct Font *font, enum FontType font_type) { @@ -222,7 +241,7 @@ fontpath_find(struct Font *font, enum FontType font_type) FcChar8 *file; for (int i=0; i<set->nfont; i++) { if (FcPatternGetString(set->fonts[i], FC_FILE, 0, &file) == FcResultMatch) { - if (!file_extension_is_ttc((const char *)file)) { + if (!file_extension_equals((const char *)file, "ttc")) { filepath = strdup((const char *)file); break; } @@ -233,8 +252,9 @@ fontpath_find(struct Font *font, enum FontType font_type) FcFontSetDestroy(set); return filepath; } + static char * -out_pdf_filename_generate_from_songs(struct ChoSong **songs) +pdf_filename_generate_from_songs(struct ChoSong **songs) { char *filename; char *normalized_title; @@ -258,7 +278,7 @@ out_pdf_filename_generate_from_songs(struct ChoSong **songs) } static char * -out_pdf_filepath_create(struct ChoSong **songs, const char *cho_filepath, const char *out) +pdf_filepath_create(struct ChoSong **songs, const char *cho_filepath, const char *out) { char *pdf_filepath = NULL; char *pdf_filename; @@ -268,9 +288,9 @@ out_pdf_filepath_create(struct ChoSong **songs, const char *cho_filepath, const pdf_filepath = file_extension_replace_or_add(cho_filepath, "pdf"); pdf_filename = filepath_basename(pdf_filepath); } else { - pdf_filename = out_pdf_filename_generate_from_songs(songs); + pdf_filename = pdf_filename_generate_from_songs(songs); if (!pdf_filename) { - LOG_DEBUG("out_pdf_filename_generate_from_songs failed."); + LOG_DEBUG("pdf_filename_generate_from_songs failed."); return NULL; } } @@ -317,48 +337,11 @@ out_pdf_filepath_create(struct ChoSong **songs, const char *cho_filepath, const } } -static char * -out_pdf_fnt_name_create(struct Font *font) -{ - char *name = str_normalize(font->name); - const char *family = cho_font_family_to_config_string(font->family); - const char *style = cho_font_style_to_config_string(font->style); - const char *weight = cho_font_weight_to_config_string(font->weight); - int n = 0; - int i; - char *fnt_name = emalloc((strlen(name) + strlen(family) + strlen(style) + strlen(weight) + 4) * sizeof(char)); - for (i = 0; name[i]; i++) { - fnt_name[n] = name[i]; - n++; - } - fnt_name[n] = '-'; - n++; - for (i = 0; family[i]; i++) { - fnt_name[n] = family[i]; - n++; - } - fnt_name[n] = '-'; - n++; - for (i = 0; style[i]; i++) { - fnt_name[n] = style[i]; - n++; - } - fnt_name[n] = '-'; - n++; - for (i = 0; weight[i]; i++) { - fnt_name[n] = weight[i]; - n++; - } - fnt_name[n] = 0; - free(name); - return fnt_name; -} - static bool -out_pdf_font_set(pdfio_stream_t *stream, struct Font *font) +pdf_font_set(pdfio_stream_t *stream, struct Font *font) { static int page_index = 0; - char *name = out_pdf_fnt_name_create(font); + char *name = fnt_name_create(font); if ( !strcmp(name, g_current_font_name) && font->size == g_current_font_size && @@ -387,7 +370,7 @@ out_pdf_font_set(pdfio_stream_t *stream, struct Font *font) static double text_width(const char *text, struct ChoStyle *style) { - char *name = out_pdf_fnt_name_create(style->font); + char *name = fnt_name_create(style->font); pdfio_obj_t *font_obj = out_pdf_fnt_obj_get_by_name(name); if (!font_obj) { LOG_DEBUG("out_pdf_fnt_obj_get_by_name failed."); @@ -400,9 +383,9 @@ text_width(const char *text, struct ChoStyle *style) static double text_above_width(struct ChoLineItemAbove *above) { - char *name; double width; if (above->is_chord) { + char *name; name = cho_chord_name_generate(above->u.chord); width = text_width(name, above->u.chord->style); if (width == ERROR) { @@ -464,7 +447,7 @@ text_find_fitting( } static bool -out_pdf_draw_line(pdfio_stream_t *stream, struct PDFText *text, double y, double width, enum LineLocation line_location) +pdf_draw_line(pdfio_stream_t *stream, struct PDFText *text, double y, double width, enum LineLocation line_location) { double red, green, blue; switch (line_location) { @@ -507,7 +490,7 @@ out_pdf_draw_line(pdfio_stream_t *stream, struct PDFText *text, double y, double } static bool -out_pdf_text_show(pdfio_stream_t *stream, struct PDFText *text) +pdf_text_show(pdfio_stream_t *stream, struct PDFText *text) { double red, green, blue, width; // TODO: Maybe store the width in PDFText!? @@ -516,8 +499,8 @@ out_pdf_text_show(pdfio_stream_t *stream, struct PDFText *text) LOG_DEBUG("text_width failed."); return false; } - if (!out_pdf_font_set(stream, text->style->font)) { - LOG_DEBUG("out_pdf_font_set failed."); + if (!pdf_font_set(stream, text->style->font)) { + LOG_DEBUG("pdf_font_set failed."); return false; } red = text->style->foreground_color->red / 255.0; @@ -548,19 +531,19 @@ out_pdf_text_show(pdfio_stream_t *stream, struct PDFText *text) return false; } if (text->style->underline_style == LS_SINGLE) { - out_pdf_draw_line(stream, text, text->y, width, LL_UNDER); + pdf_draw_line(stream, text, text->y, width, LL_UNDER); } else if (text->style->underline_style == LS_DOUBLE) { - out_pdf_draw_line(stream, text, text->y, width, LL_UNDER); - out_pdf_draw_line(stream, text, text->y-1.5, width, LL_UNDER); + pdf_draw_line(stream, text, text->y, width, LL_UNDER); + pdf_draw_line(stream, text, text->y-1.5, width, LL_UNDER); } if (text->style->strikethrough) { - out_pdf_draw_line(stream, text, text->y, width, LL_STRIKETHROUGH); + pdf_draw_line(stream, text, text->y, width, LL_STRIKETHROUGH); } if (text->style->overline_style == LS_SINGLE) { - out_pdf_draw_line(stream, text, text->y, width, LL_OVER); + pdf_draw_line(stream, text, text->y, width, LL_OVER); } else if (text->style->overline_style == LS_DOUBLE) { - out_pdf_draw_line(stream, text, text->y, width, LL_OVER); - out_pdf_draw_line(stream, text, text->y+1.5, width, LL_OVER); + pdf_draw_line(stream, text, text->y, width, LL_OVER); + pdf_draw_line(stream, text, text->y+1.5, width, LL_OVER); } if (text->style->boxed) { red = text->style->boxed_color->red / 255.0; @@ -674,7 +657,7 @@ annot_url_link_add(struct PDFContext *ctx, struct ChoStyle *style, double width) } static bool -out_pdf_set_title(pdfio_file_t *pdf, struct ChoSong **songs) +pdf_set_title(pdfio_file_t *pdf, struct ChoSong **songs) { // INFO: Set pdf title only if a single song exist if (songs[0] && !songs[1]) { @@ -690,7 +673,7 @@ out_pdf_set_title(pdfio_file_t *pdf, struct ChoSong **songs) } static pdfio_stream_t * -out_pdf_page_create(pdfio_file_t *pdf, struct PDFImage **imgs, pdfio_array_t *annots) +pdf_page_create(pdfio_file_t *pdf, struct PDFImage **imgs, pdfio_array_t *annots) { pdfio_dict_t *page_dict; pdfio_stream_t *page_stream; @@ -703,7 +686,7 @@ out_pdf_page_create(pdfio_file_t *pdf, struct PDFImage **imgs, pdfio_array_t *an } if (!pdfioDictSetArray(page_dict, "Annots", annots)) { LOG_DEBUG("pdfioDictSetArray failed."); - return false; + return NULL; } if (imgs) { for (f = 0; imgs[f]; f++) { @@ -856,7 +839,7 @@ image_name(struct ChoImage *image) } static bool -out_pdf_load_images(struct Obj ***images, pdfio_file_t *file, struct ChoSong **songs) +pdf_load_images(struct Obj ***images, pdfio_file_t *file, struct ChoSong **songs) { struct ChoSong **s; struct ChoSection **se; @@ -907,7 +890,7 @@ out_pdf_load_images(struct Obj ***images, pdfio_file_t *file, struct ChoSong **s } static bool -out_pdf_load_chord_diagram_fonts(void) +pdf_load_chord_diagram_fonts(void) { struct Obj *fnt; fnt = obj_new(); @@ -922,7 +905,7 @@ out_pdf_load_chord_diagram_fonts(void) } static bool -out_pdf_get_chords(struct ChoSong *song, struct ChoChord ***chords) +pdf_get_chords(struct ChoSong *song, struct ChoChord ***chords) { struct ChoSection **se; struct ChoLine **li; @@ -980,7 +963,7 @@ line_width_until_text_above( for (i = 0; items[i] && i <= save_i; i++) { if (items[i]->is_text) { text = items[i]->u.text; - name = out_pdf_fnt_name_create(text->style->font); + name = fnt_name_create(text->style->font); font_obj = out_pdf_fnt_obj_get_by_name(name); if (!font_obj) { LOG_DEBUG("out_pdf_fnt_obj_get_by_name failed."); @@ -1873,8 +1856,8 @@ pdf_content_create( // printf("song '%d' start on page '%d'.\n", s, ctx.page); if (config->output->diagram->show) { struct ChoChord **chords = NULL; - if (!out_pdf_get_chords(songs[s], &chords)) { - LOG_DEBUG("out_pdf_get_chords failed."); + if (!pdf_get_chords(songs[s], &chords)) { + LOG_DEBUG("pdf_get_chords failed."); return false; } if (chords) { @@ -2217,10 +2200,14 @@ pdf_toc_render(struct PDFContent *content, pdfio_file_t *file) int p; for (p = 0; pages[p]; p++) { g_current_page_index = p; - stream = out_pdf_page_create(file, NULL, pages[p]->annots); + stream = pdf_page_create(file, NULL, pages[p]->annots); + if (!stream) { + LOG_DEBUG("pdf_page_create failed."); + return false; + } for (texts = pages[p]->texts; *texts; texts++) { - if (!out_pdf_text_show(stream, *texts)) { - LOG_DEBUG("out_pdf_text_show failed."); + if (!pdf_text_show(stream, *texts)) { + LOG_DEBUG("pdf_text_show failed."); return false; } } @@ -2243,14 +2230,14 @@ pdf_content_render(struct PDFContent *content, pdfio_file_t *file) pages = content->pages; for (p = 0; pages[p]; p++) { g_current_page_index = p; - stream = out_pdf_page_create(file, pages[p]->images, pages[p]->annots); + stream = pdf_page_create(file, pages[p]->images, pages[p]->annots); if (!stream) { - LOG_DEBUG("out_pdf_page_create failed."); + LOG_DEBUG("pdf_page_create failed."); return false; } for (texts = pages[p]->texts; *texts; texts++) { - if (!out_pdf_text_show(stream, *texts)) { - LOG_DEBUG("out_pdf_text_show failed."); + if (!pdf_text_show(stream, *texts)) { + LOG_DEBUG("pdf_text_show failed."); return false; } } @@ -2319,9 +2306,9 @@ out_pdf_create( } strcpy((char *)&g_cho_dirpath, dirpath); free(dirpath); - pdf_filepath = out_pdf_filepath_create(songs, cho_filepath, output_folder_or_file); + pdf_filepath = pdf_filepath_create(songs, cho_filepath, output_folder_or_file); if (!pdf_filepath) { - LOG_DEBUG("out_pdf_filepath_create failed."); + LOG_DEBUG("pdf_filepath_create failed."); return NULL; } g_pdf_file = pdfioFileCreate(pdf_filepath, "2.0", &media_box_a4, &crop_box, NULL, NULL); @@ -2329,8 +2316,8 @@ out_pdf_create( LOG_DEBUG("pdfioFileCreate failed."); return NULL; } - if (!out_pdf_set_title(g_pdf_file, songs)) { - LOG_DEBUG("out_pdf_set_title failed."); + if (!pdf_set_title(g_pdf_file, songs)) { + LOG_DEBUG("pdf_set_title failed."); goto CLEAN; } int f; @@ -2339,7 +2326,7 @@ out_pdf_create( fontpath = fontpath_find(needed_fonts[f], FT_TTF); if (fontpath) { fnt = obj_new(); - fnt->name = out_pdf_fnt_name_create(needed_fonts[f]); + fnt->name = fnt_name_create(needed_fonts[f]); fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, fontpath, true); objs_add_obj(&g_fonts, fnt); free(fontpath); @@ -2347,7 +2334,7 @@ out_pdf_create( fontpath = fontpath_find(needed_fonts[f], FT_OTF); if (fontpath) { fnt = obj_new(); - fnt->name = out_pdf_fnt_name_create(needed_fonts[f]); + fnt->name = fnt_name_create(needed_fonts[f]); fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, fontpath, true); objs_add_obj(&g_fonts, fnt); free(fontpath); @@ -2360,13 +2347,13 @@ out_pdf_create( } cho_fonts_free(needed_fonts); if (config->output->diagram->show) { - if (!out_pdf_load_chord_diagram_fonts()) { - LOG_DEBUG("out_pdf_load_chord_diagram_fonts failed."); + if (!pdf_load_chord_diagram_fonts()) { + LOG_DEBUG("pdf_load_chord_diagram_fonts failed."); goto CLEAN; } } - if (!out_pdf_load_images(&img_objs, g_pdf_file, songs)) { - LOG_DEBUG("out_pdf_load_images failed."); + if (!pdf_load_images(&img_objs, g_pdf_file, songs)) { + LOG_DEBUG("pdf_load_images failed."); goto CLEAN; } if (!pdf_content_create(&pdf_content, songs, config, img_objs)) { diff --git a/util.c b/util.c @@ -296,23 +296,23 @@ file_read(const char *filepath) } char * -file_extension_replace_or_add(const char *old, const char *extension) +file_extension_replace_or_add(const char *filepath, const char *extension) { size_t extension_len = strlen(extension); char *new = NULL; int mark = -1; int i, k; - int old_len; - for (i = 0; old[i]; i++) { - if (old[i] == '.') { + int path_len; + for (i = 0; filepath[i]; i++) { + if (filepath[i] == '.') { mark = i; } } if (mark == -1) { - old_len = (int)strlen(old); - new = emalloc((old_len+2+extension_len) * sizeof(char)); - for (i = 0; i < old_len; i++) { - new[i] = old[i]; + path_len = (int)strlen(filepath); + new = emalloc((path_len+2+extension_len) * sizeof(char)); + for (i = 0; i < path_len; i++) { + new[i] = filepath[i]; } new[i] = '.'; i++; @@ -323,7 +323,7 @@ file_extension_replace_or_add(const char *old, const char *extension) } else { new = emalloc((mark+2+extension_len) * sizeof(char)); for (i = 0; i <= mark; i++) { - new[i] = old[i]; + new[i] = filepath[i]; } for (k = 0; extension[k]; k++, i++) { new[i] = extension[k]; @@ -333,6 +333,23 @@ file_extension_replace_or_add(const char *old, const char *extension) return new; } +bool +file_extension_equals(const char *filepath, const char *extension) +{ + int mark = -1; + int i; + for (i = strlen(filepath)-1; i >= 0; i--) { + if (filepath[i] == '.') { + mark = i; + break; + } + } + if (!strcmp(&filepath[mark+1], extension)) { + return true; + } + return false; +} + char * filepath_add_ending_slash_if_missing(const char *path) { diff --git a/util.h b/util.h @@ -54,7 +54,8 @@ long str_to_number(const char *str); enum FileType file_type(const char *path); char *file_read(const char *filepath); -char *file_extension_replace_or_add(const char *old, const char *extension); +char *file_extension_replace_or_add(const char *filepath, const char *extension); +bool file_extension_equals(const char *filepath, const char *extension); char *filepath_add_ending_slash_if_missing(const char *path); char *filepath_basename(const char *path);