lorid

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

commit 44872e99ca2c9ed95c5f10c3bb93463adf9bd530
parent 81f6bffc0f2af3efa8b2eb3291e3ec1ab41a22d4
Author: nibo <nibo@relim.de>
Date:   Tue, 31 Dec 2024 19:56:47 +0100

Improve various things

Diffstat:
MMakefile | 34+++++++++++++++++++---------------
Mchord_diagram.c | 4++++
Mchordpro.c | 7++++++-
Mconfig.h | 6++++++
Mout_pdf.c | 51+++++++++++++++++++++++++++------------------------
5 files changed, 62 insertions(+), 40 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,16 +1,17 @@ VERSION = 0.1.0 PREFIX = /usr/local MANPREFIX = ${PREFIX}/share/man -CFLAGS = -pedantic -Wall -Wextra -DVERSION=\"${VERSION}\" +COLOR = 0 +VARS = -DVERSION=\"${VERSION}\" -DCOLOR=${COLOR} +DEBUG_VARS = -DDEBUG -DPREFIX=\"${PREFIX}\" +CFLAGS = -pedantic -Wall -Wextra LDFLAGS = -lpdfio -ltoml -lfontconfig SRC = util.c fontconfig.c config.c chordpro.c chord_diagram.c out_pdf.c lorid.c -COLOR = 0 - -all: - $(CC) ${CFLAGS} -DCOLOR=${COLOR} -O2 ${SRC} -o lorid ${LDFLAGS} +compile: + $(CC) ${CFLAGS} ${VARS} -O2 ${SRC} -o lorid ${LDFLAGS} debug: - $(CC) ${CFLAGS} -DCOLOR=${COLOR} -DDEBUG -g ${SRC} -o lorid ${LDFLAGS} + $(CC) ${CFLAGS} ${VARS} ${DEBUG_VARS} -g ${SRC} -o lorid ${LDFLAGS} clean: rm *.o rm lorid @@ -20,19 +21,22 @@ fontconfig: lib: $(CC) -fpic -c util.c fontconfig.c config.c chordpro.c out_pdf.c $(CC) -shared *.o ${LDFLAGS} -o liblorid.so.${VERSION} -install: all lib +install: compile mkdir -p ${PREFIX}/bin cp lorid ${PREFIX}/bin - mkdir -p ${PREFIX}/lib - cp liblorid.so.${VERSION} ${PREFIX}/lib - ln -f -s ${PREFIX}/lib/liblorid.so.${VERSION} ${PREFIX}/lib/liblorid.so - mkdir -p ${PREFIX}/include - cp lorid.h ${PREFIX}/include + # mkdir -p ${PREFIX}/lib + # cp liblorid.so.${VERSION} ${PREFIX}/lib + # ln -f -s ${PREFIX}/lib/liblorid.so.${VERSION} ${PREFIX}/lib/liblorid.so + # mkdir -p ${PREFIX}/include + # cp lorid.h ${PREFIX}/include mkdir -p ${MANPREFIX}/man1 cp lorid.1 ${MANPREFIX}/man1/lorid.1 + mkdir -p ${PREFIX}/share/lorid/ + cp ./misc/ChordProSymbols.ttf ${PREFIX}/share/lorid/ uninstall: rm ${PREFIX}/bin/lorid - rm ${PREFIX}/lib/liblorid.so* - rm ${PREFIX}/include/lorid.h + # rm ${PREFIX}/lib/liblorid.so* + # rm ${PREFIX}/include/lorid.h rm ${MANPREFIX}/man1/lorid.1 -.PHONY: all debug clean fontconfig parser lib install uninstall + rm ${PREFIX}/share/lorid/ChordProSymbols.ttf +.PHONY: compile debug clean fontconfig parser lib install uninstall diff --git a/chord_diagram.c b/chord_diagram.c @@ -455,6 +455,10 @@ string_diagram_draw( } else { base_pos_x = x - field_width - field_width / 3; } + if (!pdfioContentSetTextCharacterSpacing(stream, -1.5)) { + fprintf(stderr, "pdfioContentSetTextCharacterSpacing failed.\n"); + return false; + } if (!pdfioContentSetTextFont(stream, "chord-diagram-regular-font", field_width*1.15)) { fprintf(stderr, "pdfioContentSetTextFont failed.\n"); return false; diff --git a/chordpro.c b/chordpro.c @@ -2891,7 +2891,12 @@ cho_chord_diagram_parse(const char *str) diagram->u.kd->keys[f] = (int8_t)l; } } - if (current_content == CDC_STRING && fret_count != finger_count) { + if ( + current_content == CDC_STRING && + fret_count > 0 && + finger_count > 0 && + fret_count != finger_count + ) { cho_log(LOG_ERR, "The number of frets (%d) and fingers (%d) in the chord diagram must be equal.", fret_count, finger_count); return NULL; } diff --git a/config.h b/config.h @@ -1,6 +1,12 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ +#ifdef DEBUG +#define SYMBOLS_FILEPATH "./misc/ChordProSymbols.ttf" +#else +#define SYMBOLS_FILEPATH PREFIX"/share/lorid/ChordProSymbols.ttf" +#endif /* DEBUG */ + struct OutputStyle { char *name; struct ChoStyle *style; diff --git a/out_pdf.c b/out_pdf.c @@ -186,7 +186,7 @@ out_pdf_filename_generate_from_songs(struct ChoSong **songs) } static char * -out_pdf_filename_create(struct ChoSong **songs, const char *cho_filepath, const char *out) +out_pdf_filepath_create(struct ChoSong **songs, const char *cho_filepath, const char *out) { char *pdf_filepath = NULL; char *pdf_filename; @@ -868,19 +868,13 @@ static bool out_pdf_load_chord_diagram_fonts(void) { struct Obj *fnt; - char *regular_font_filepath = fontconfig_fontpath_find_regular_font(); - if (!regular_font_filepath) { - LOG_DEBUG("fontconfig_fontpath_find_regular_font failed."); - return false; - } fnt = obj_new(); fnt->name = strdup("chord-diagram-regular-font"); - fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, regular_font_filepath, true); - free(regular_font_filepath); + fnt->value = pdfioFileCreateFontObjFromBase(g_pdf_file, "Helvetica"); objs_add_obj(&g_fonts, fnt); fnt = obj_new(); fnt->name = strdup("chord-diagram-symbols"); - fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, "./misc/ChordProSymbols.ttf", true); + fnt->value = pdfioFileCreateFontObjFromFile(g_pdf_file, SYMBOLS_FILEPATH, true); objs_add_obj(&g_fonts, fnt); return true; } @@ -2276,7 +2270,7 @@ out_pdf_create( pdfio_rect_t media_box_a4 = { 0.0, 0.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT }; pdfio_rect_t crop_box = { 0.0, 0.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT }; int f = 0; - char *dirpath, *fontpath, *pdf_filename; + char *dirpath, *fontpath, *pdf_filepath; memset(&g_current_font_name, 0, sizeof(g_current_font_name)); memset(&g_cho_dirpath, 0, PATH_MAX); @@ -2288,15 +2282,19 @@ out_pdf_create( } strcpy((char *)&g_cho_dirpath, dirpath); free(dirpath); - pdf_filename = out_pdf_filename_create(songs, cho_filepath, output_folder_or_file); - if (!pdf_filename) { - LOG_DEBUG("out_pdf_filename_create failed."); + pdf_filepath = out_pdf_filepath_create(songs, cho_filepath, output_folder_or_file); + if (!pdf_filepath) { + LOG_DEBUG("out_pdf_filepath_create failed."); + return NULL; + } + g_pdf_file = pdfioFileCreate(pdf_filepath, "2.0", &media_box_a4, &crop_box, NULL, NULL); + if (!g_pdf_file) { + LOG_DEBUG("pdfioFileCreate failed."); return NULL; } - g_pdf_file = pdfioFileCreate(pdf_filename, "2.0", &media_box_a4, &crop_box, NULL, NULL); if (!out_pdf_set_title(g_pdf_file, songs)) { LOG_DEBUG("out_pdf_set_title failed."); - return NULL; + goto CLEAN; } needed_fonts = out_pdf_font_get_all(songs, config); while (needed_fonts[f] != NULL) { @@ -2318,7 +2316,7 @@ out_pdf_create( } else { util_log(LOG_ERR, "Didn't find font file for following font:"); cho_font_print(needed_fonts[f]); - return NULL; + goto CLEAN; } } f++; @@ -2327,37 +2325,37 @@ out_pdf_create( if (config->output->diagram->show) { if (!out_pdf_load_chord_diagram_fonts()) { LOG_DEBUG("out_pdf_load_chord_diagram_fonts failed."); - return NULL; + goto CLEAN; } } if (!out_pdf_load_images(&img_objs, g_pdf_file, songs)) { LOG_DEBUG("out_pdf_load_images failed."); - return NULL; + goto CLEAN; } if (!pdf_content_create(&pdf_content, songs, config, img_objs)) { LOG_DEBUG("pdf_content_create failed."); - return NULL; + goto CLEAN; } if (config->output->toc->show) { if (!pdf_toc_create(&toc_content, pdf_content, config)) { LOG_DEBUG("pdf_toc_create failed."); - return false; + goto CLEAN; } if (!pdf_toc_render(toc_content, g_pdf_file)) { LOG_DEBUG("pdf_toc_render failed."); - return false; + goto CLEAN; } } if (!pdf_content_render(pdf_content, g_pdf_file)) { LOG_DEBUG("pdf_content_render failed."); - return NULL; + goto CLEAN; } objs_free(img_objs); pdf_content_free(toc_content); pdf_content_free(pdf_content); if (!pdfioFileClose(g_pdf_file)) { LOG_DEBUG("pdfioFileClose failed."); - return NULL; + goto CLEAN; } g_pdf_file = NULL; objs_free(g_fonts); @@ -2365,5 +2363,10 @@ out_pdf_create( g_current_font_size = 0.0; g_current_font_obj = NULL; g_current_page_index = 0; - return pdf_filename; + return pdf_filepath; + CLEAN: + if (unlink(pdf_filepath)) { + LOG_DEBUG("unlink failed."); + } + return NULL; }