lorid

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

commit 720b8437e1bebbfe503f3fb5b21ec66e878ea8ee
parent 2a79e50c9047656f6b6dd8de5f95f8a77d84a35c
Author: nibo <nibo@relim.de>
Date:   Fri, 10 Jan 2025 10:30:31 +0100

Remove struct OutputStyle

Diffstat:
Mchordpro.c | 14++++++--------
Mconfig.c | 156++++++++++++++++++++++++++++++--------------------------------------------------
Mconfig.h | 8+-------
Mout_pdf.c | 47++++++++++++++++++++++-------------------------
4 files changed, 87 insertions(+), 138 deletions(-)

diff --git a/chordpro.c b/chordpro.c @@ -871,20 +871,17 @@ cho_style_copy(struct ChoStyle *style) struct ChoStyle * cho_style_new_from_config(enum SongFragmentType ftype) { - struct OutputStyle *style; - style = config_output_style_get(g_config->output->styles, ftype); - if (!style) { - printf("fragment type '%d'\n", ftype); - LOG_DEBUG("config_output_style_get failed."); - return NULL; - } - return cho_style_copy(style->style); + return cho_style_copy(g_config->output->styles[ftype]); } struct ChoStyle * cho_style_new_default(void) { struct ChoStyle *style = cho_style_new_from_config(g_current_ftype); + if (!style) { + LOG_DEBUG("cho_style_new_from_config failed."); + return NULL; + } cho_style_apply_default(g_current_ftype, style); return style; } @@ -3704,6 +3701,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) lines = &songs[so]->sections[se]->lines; (*lines)[li]->items = emalloc(sizeof(struct ChoLineItem *)); (*lines)[li]->items[ly] = cho_line_item_new(); + songs[so]->present_fragments[SF_TOC] = config->output->toc->show; struct Tag **tags = NULL; struct ChoStyle *tag_style; struct StyleProperty sprop; diff --git a/config.c b/config.c @@ -167,34 +167,6 @@ config_song_fragment_type_parse(const char *str) return SF_EMPTY; } -static struct OutputStyle * -config_output_style_new(enum SongFragmentType type) -{ - struct OutputStyle *item = emalloc(sizeof(struct OutputStyle)); - item->type = type; - item->style = cho_style_new(); - return item; -} - -static void -config_output_style_free(struct OutputStyle *item) -{ - cho_style_free(item->style); - free(item); -} - -struct OutputStyle * -config_output_style_get(struct OutputStyle **styles, enum SongFragmentType type) -{ - struct OutputStyle **os; - for (os = styles; *os; os++) { - if ((*os)->type == type) { - return *os; - } - } - return NULL; -} - static enum NamingSystem config_naming_system_parse(const char *str) { @@ -427,63 +399,51 @@ config_load_default(void) config->output->diagram->show = true; config->output->diagram->instrument = INS_GUITAR; config->output->system = NS_COMMON; - config->output->styles = emalloc(14 * sizeof(struct OutputStyle *)); - - config->output->styles[0] = config_output_style_new(SF_CHORD); - config->output->styles[0]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[0]->style->font->weight = FW_BOLD; - - config->output->styles[1] = config_output_style_new(SF_ANNOT); - config->output->styles[1]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[1]->style->font->style = FS_ITALIC; - - config->output->styles[2] = config_output_style_new(SF_CHORUS); - config->output->styles[2]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - - // Footer - - config->output->styles[3] = config_output_style_new(SF_GRID); - config->output->styles[3]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[3]->style->font->weight = FW_BOLD; - - config->output->styles[4] = config_output_style_new(SF_TAB); - config->output->styles[4]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - - config->output->styles[5] = config_output_style_new(SF_TOC); - config->output->styles[5]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[5]->style->font->size = 12.0; - - config->output->styles[6] = config_output_style_new(SF_TEXT); - config->output->styles[6]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - - config->output->styles[7] = config_output_style_new(SF_TITLE); - config->output->styles[7]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[7]->style->font->weight = FW_BOLD; - config->output->styles[7]->style->font->size = 18.0; - - config->output->styles[8] = config_output_style_new(SF_SUBTITLE); - config->output->styles[8]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[8]->style->font->size = 12.0; - - config->output->styles[9] = config_output_style_new(SF_LABEL); - config->output->styles[9]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[9]->style->font->style = FS_ITALIC; - - config->output->styles[10] = config_output_style_new(SF_COMMENT); - config->output->styles[10]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[10]->style->background_color->red = 228; - config->output->styles[10]->style->background_color->green = 228; - config->output->styles[10]->style->background_color->blue = 228; - - config->output->styles[11] = config_output_style_new(SF_COMMENT_ITALIC); - config->output->styles[11]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[11]->style->font->style = FS_ITALIC; - - config->output->styles[12] = config_output_style_new(SF_COMMENT_BOX); - config->output->styles[12]->style->font->name = strdup(DEFAULT_FONT_FAMILY); - config->output->styles[12]->style->boxed = true; - - config->output->styles[13] = NULL; + config->output->styles = emalloc(SF_LENGTH * sizeof(struct ChoStyle *)); + + config->output->styles[SF_EMPTY] = NULL; + config->output->styles[SF_CHORD] = cho_style_new(); + config->output->styles[SF_CHORD]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_CHORD]->font->weight = FW_BOLD; + config->output->styles[SF_ANNOT] = cho_style_new(); + config->output->styles[SF_ANNOT]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_ANNOT]->font->style = FS_ITALIC; + config->output->styles[SF_CHORUS] = cho_style_new(); + config->output->styles[SF_CHORUS]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_FOOTER] = cho_style_new(); + config->output->styles[SF_GRID] = cho_style_new(); + config->output->styles[SF_GRID]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_GRID]->font->weight = FW_BOLD; + config->output->styles[SF_TAB] = cho_style_new(); + config->output->styles[SF_TAB]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_TOC] = cho_style_new(); + config->output->styles[SF_TOC]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_TOC]->font->size = 12.0; + config->output->styles[SF_TEXT] = cho_style_new(); + config->output->styles[SF_TEXT]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_TITLE] = cho_style_new(); + config->output->styles[SF_TITLE]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_TITLE]->font->weight = FW_BOLD; + config->output->styles[SF_TITLE]->font->size = 18.0; + config->output->styles[SF_SUBTITLE] = cho_style_new(); + config->output->styles[SF_SUBTITLE]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_SUBTITLE]->font->size = 12.0; + config->output->styles[SF_LABEL] = cho_style_new(); + config->output->styles[SF_LABEL]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_LABEL]->font->style = FS_ITALIC; + config->output->styles[SF_COMMENT] = cho_style_new(); + config->output->styles[SF_COMMENT]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_COMMENT]->background_color->red = 228; + config->output->styles[SF_COMMENT]->background_color->green = 228; + config->output->styles[SF_COMMENT]->background_color->blue = 228; + config->output->styles[SF_COMMENT_ITALIC] = cho_style_new(); + config->output->styles[SF_COMMENT_ITALIC]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_COMMENT_ITALIC]->font->style = FS_ITALIC; + config->output->styles[SF_COMMENT_BOX] = cho_style_new(); + config->output->styles[SF_COMMENT_BOX]->font->name = strdup(DEFAULT_FONT_FAMILY); + config->output->styles[SF_COMMENT_BOX]->boxed = true; + + // config->output->styles[15] = NULL; config->output->notes = config_notes_new_default(NS_COMMON); config->parser = emalloc(sizeof(struct ConfigParser)); config->parser->chords = emalloc(sizeof(struct ConfigChords)); @@ -524,9 +484,10 @@ config_print_default(void) printf("label = \"Chorus\"\n"); printf("quote = false\n\n"); printf("[output.styles]\n"); - struct OutputStyle **os; - for (os = config->output->styles; *os; os++) { - config_output_style_print_as_toml(*os); + int i; + for (i = 1; i<SF_LENGTH; i++) { + printf("[output.styles.%s]\n\n", song_fragment_types[i]); + cho_style_print_as_toml(config->output->styles[i], song_fragment_types[i]); } printf("[parser]\n"); printf("[parser.chords]\n"); @@ -809,10 +770,11 @@ config_load(const char *filepath) } toml_table_t *styles = toml_table_table(output, "styles"); if (styles) { + // bool has_chorus = has_chorus_style(styles); int unused; const char *key_name; toml_table_t *key; - struct OutputStyle *item; + struct ChoStyle *style; enum SongFragmentType ftype; int i; for (i = 0; i<toml_table_len(styles); i++) { @@ -821,12 +783,10 @@ config_load(const char *filepath) if (ftype != SF_EMPTY) { key = toml_table_table(styles, key_name); if (key) { - item = config_output_style_get(config->output->styles, ftype); - if (item) { - if (!config_load_style(item->style, key, key_name)) { - LOG_DEBUG("config_load_style failed."); - return NULL; - } + style = config->output->styles[ftype]; + if (!config_load_style(style, key, key_name)) { + LOG_DEBUG("config_load_style failed."); + return NULL; } } } @@ -884,13 +844,13 @@ config_load(const char *filepath) void config_free(struct Config *config) { - struct OutputStyle **os; free(config->output->toc->title); free(config->output->toc); free(config->output->chorus->label); free(config->output->chorus); - for (os = config->output->styles; *os; os++) { - config_output_style_free(*os); + int i; + for (i = 0; i<SF_LENGTH; i++) { + cho_style_free(config->output->styles[i]); } free(config->output->styles); config_notes_free(config->output->notes); diff --git a/config.h b/config.h @@ -8,11 +8,6 @@ #endif /* DEBUG */ #define DEFAULT_FONT_FAMILY "Open Sans" -struct OutputStyle { - enum SongFragmentType type; - struct ChoStyle *style; -}; - enum NamingSystem { NS_COMMON, NS_GERMAN, @@ -78,7 +73,7 @@ struct ConfigOutput { struct ConfigChorus *chorus; struct ConfigChordDiagram *diagram; enum NamingSystem system; - struct OutputStyle **styles; + struct ChoStyle **styles; struct Note **notes; }; @@ -90,6 +85,5 @@ struct Config { struct Config *config_load(const char *filepath); void config_free(struct Config *config); void config_print_default(void); -struct OutputStyle *config_output_style_get(struct OutputStyle **styles, enum SongFragmentType type); #endif /* _CONFIG_H_ */ diff --git a/out_pdf.c b/out_pdf.c @@ -175,24 +175,24 @@ fonts_get_all(struct ChoSong **songs, struct Config *config) { struct Font **fonts = NULL; struct Font *font; - bool added; - int i; - for (i = 0; config->output->styles[i]; i++) { - font = cho_font_copy(config->output->styles[i]->style->font); - added = fonts_add_if_not_in(&fonts, font); - if (!added) { - cho_font_free(font); - } - } struct ChoSong **so; struct ChoSection **se; struct ChoLine **li; struct ChoLineItemAbove **above; struct ChoLineItem **it; struct ChoStyle *style; + bool added; + int i; for (so = songs; *so; so++) { - // TODO: Take care of SF_TOC and SF_FOOTER - int i; + for (i = 0; i < SF_LENGTH; i++) { + if ((*so)->present_fragments[i]) { + font = cho_font_copy(config->output->styles[i]->font); + added = fonts_add_if_not_in(&fonts, font); + if (!added) { + cho_font_free(font); + } + } + } const char *name; for (i = 0; i<SF_LENGTH; i++) { switch (i) { @@ -1893,6 +1893,8 @@ pdf_toc_create( int toc_page_count; struct PDFContext ctx; struct PDFText ***texts; + struct ChoStyle *toc_style; + struct TocEntry **toc; ctx.text = 0; ctx.page = 0; ctx.x = MARGIN_HORIZONTAL; @@ -1901,16 +1903,15 @@ pdf_toc_create( ctx.content->pages = emalloc(sizeof(struct PDFPage *)); ctx.content->pages[ctx.page] = pdf_page_new(); texts = &ctx.content->pages[ctx.page]->texts; - struct OutputStyle *toc_style = config_output_style_get(config->output->styles, SF_TOC); - struct TocEntry **toc; + toc_style = config->output->styles[SF_TOC]; toc = pdf_content->toc; max_title_width = LINE_WIDTH * 0.85; - toc_page_count = pdf_toc_page_count(toc, toc_style->style, max_title_width); + toc_page_count = pdf_toc_page_count(toc, toc_style, max_title_width); if (toc_page_count == -1) { LOG_DEBUG("pdf_toc_page_count failed."); return false; } - /* struct ChoStyle *toc_title_style = cho_style_copy(toc_style->style); + /* struct ChoStyle *toc_title_style = cho_style_copy(toc_style); toc_title_style->font->size = 18.0; // toc_title_style->font->weight = FW_BOLD; if (!pdf_texts_add_text(&ctx, config->output->toc->title, toc_title_style, A_CENTER)) { @@ -1919,19 +1920,19 @@ pdf_toc_create( } cho_style_free(toc_title_style); */ for (; *toc; toc++) { - if (!pdf_texts_add_toc_entry(&ctx, *toc, toc_style->style, max_title_width, toc_page_count)) { + if (!pdf_texts_add_toc_entry(&ctx, *toc, toc_style, max_title_width, toc_page_count)) { LOG_DEBUG("pdf_texts_add_toc_entry failed."); return false; } // TODO: create (multi)line with title, dots and the page number /* space_for_dots = LINE_WIDTH; space_for_dots -= width; - width = text_width((*toc)->title, toc_style->style); + width = text_width((*toc)->title, toc_style); space_for_dots -= width; printf("width for dots '%.1f'\n", space_for_dots); char line[4096]; sprintf((char *)&line, "%s ........ %d", (*toc)->title, (*toc)->page_index+1); - if (!pdf_texts_add_text(&ctx, (char *)&line, toc_style->style, A_LEFT)) { + if (!pdf_texts_add_text(&ctx, (char *)&line, toc_style, A_LEFT)) { LOG_DEBUG("pdf_texts_add_text failed."); return false; } */ @@ -2033,13 +2034,9 @@ pdf_content_create( used because the subtitle style can only be manipulated from the config file */ - struct OutputStyle *output_style; - output_style = config_output_style_get(config->output->styles, SF_SUBTITLE); - if (!output_style) { - LOG_DEBUG("config_output_style_get failed."); - return NULL; - } - if (!pdf_texts_add_text(&ctx, (*m)->value, output_style->style, A_CENTER)) { + struct ChoStyle *output_style; + output_style = config->output->styles[SF_SUBTITLE]; + if (!pdf_texts_add_text(&ctx, (*m)->value, output_style, A_CENTER)) { LOG_DEBUG("pdf_texts_add_text failed."); return false; }