lorid

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

commit ac6aa5ef429f432b0fe60276943a785d189d5224
parent 3f2020b310d4278761ed5335015c81fd2e3275cf
Author: nibo <nibo@relim.de>
Date:   Sat, 11 Jan 2025 10:05:40 +0100

Set text style as default for lyrics in config file

Diffstat:
Mchordpro.c | 14++++++++++----
Mchordpro.h | 1+
Mconfig.c | 158++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mconfig.h | 38+++++++++++++++++++++++++++++++-------
Mout_pdf.c | 52----------------------------------------------------
5 files changed, 191 insertions(+), 72 deletions(-)

diff --git a/chordpro.c b/chordpro.c @@ -358,7 +358,7 @@ is_whitespace(char c) return false; } -struct RGBColor * +static struct RGBColor * cho_rgbcolor_new(uint8_t red, uint8_t green, uint8_t blue) { struct RGBColor *color = emalloc(sizeof(struct RGBColor)); @@ -368,7 +368,7 @@ cho_rgbcolor_new(uint8_t red, uint8_t green, uint8_t blue) return color; } -struct RGBColor * +static struct RGBColor * cho_rgbcolor_copy(struct RGBColor *color) { struct RGBColor *copy = emalloc(sizeof(struct RGBColor)); @@ -378,7 +378,7 @@ cho_rgbcolor_copy(struct RGBColor *color) return copy; } -const char * +static const char * cho_rgbcolor_to_string(struct RGBColor *color) { static char str[8]; @@ -387,7 +387,7 @@ cho_rgbcolor_to_string(struct RGBColor *color) return (const char *)&str; } -struct RGBColor * +static struct RGBColor * cho_rgbcolor_parse(const char *str) { struct RGBColor *color = emalloc(sizeof(struct RGBColor)); @@ -548,6 +548,12 @@ cho_color_parse(const char *str) return color; } +inline struct RGBColor * +cho_color_copy(struct RGBColor *color) +{ + return cho_rgbcolor_copy(color); +} + struct Font * cho_font_new(void) { diff --git a/chordpro.h b/chordpro.h @@ -354,6 +354,7 @@ struct Font *cho_style_font_desc_parse(const char *str); void cho_style_print_as_toml(struct ChoStyle *style, const char *section); struct RGBColor *cho_color_parse(const char *str); +struct RGBColor *cho_color_copy(struct RGBColor *color); enum LineStyle cho_linestyle_parse(const char *str); // const char *cho_image_name_create(struct ChoImage *image, const char *dirname); diff --git a/config.c b/config.c @@ -490,7 +490,7 @@ config_print_default(void) } static bool -config_load_font(struct Font *font, toml_table_t *table, const char *key_name) +config_load_font(struct Font *font, toml_table_t *table, const char *key_name, struct ChoStylePresence *presence) { enum FontFamily family; enum FontStyle style; @@ -498,11 +498,13 @@ config_load_font(struct Font *font, toml_table_t *table, const char *key_name) toml_value_t value; value = toml_table_string(table, "name"); if (value.ok) { + presence->font.name = true; free(font->name); font->name = value.u.s; } value = toml_table_string(table, "family"); if (value.ok) { + presence->font.family = true; family = cho_font_family_parse(value.u.s); if (family != FF_EMPTY) { font->family = family; @@ -514,6 +516,7 @@ config_load_font(struct Font *font, toml_table_t *table, const char *key_name) } value = toml_table_string(table, "style"); if (value.ok) { + presence->font.style = true; style = cho_font_style_parse(value.u.s); if (style != FS_EMPTY) { font->style = style; @@ -525,6 +528,7 @@ config_load_font(struct Font *font, toml_table_t *table, const char *key_name) } value = toml_table_string(table, "weight"); if (value.ok) { + presence->font.weight = true; weight = cho_font_weight_parse(value.u.s); if (weight != FW_EMPTY) { font->weight = weight; @@ -536,26 +540,33 @@ config_load_font(struct Font *font, toml_table_t *table, const char *key_name) } value = toml_table_int(table, "size"); if (value.ok) { + presence->font.size = true; font->size = value.u.i; } return true; } static bool -config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_name) +config_load_style( + struct ChoStyle *style, + toml_table_t *table, + const char *key_name, + struct ChoStylePresence *presence +) { toml_value_t value; struct RGBColor *color; enum LineStyle line_style; toml_table_t *font_section = toml_table_table(table, "font"); if (font_section) { - if (!config_load_font(style->font, font_section, key_name)) { + if (!config_load_font(style->font, font_section, key_name, presence)) { LOG_DEBUG("config_load_font failed."); return false; } } value = toml_table_string(table, "foreground_color"); if (value.ok) { + presence->foreground_color = true; color = cho_color_parse(value.u.s); if (color) { free(style->foreground_color); @@ -568,6 +579,7 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n } value = toml_table_string(table, "background_color"); if (value.ok) { + presence->background_color = true; color = cho_color_parse(value.u.s); if (color) { free(style->background_color); @@ -580,6 +592,7 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n } value = toml_table_string(table, "underline_style"); if (value.ok) { + presence->underline_style = true; line_style = cho_linestyle_parse(value.u.s); if (line_style != LS_EMPTY) { style->underline_style = line_style; @@ -591,6 +604,7 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n } value = toml_table_string(table, "underline_color"); if (value.ok) { + presence->underline_color = true; color = cho_color_parse(value.u.s); if (color) { free(style->underline_color); @@ -603,6 +617,7 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n } value = toml_table_string(table, "overline_style"); if (value.ok) { + presence->overline_style = true; line_style = cho_linestyle_parse(value.u.s); if (line_style != LS_EMPTY) { style->overline_style = line_style; @@ -614,6 +629,7 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n } value = toml_table_string(table, "overline_color"); if (value.ok) { + presence->overline_color = true; color = cho_color_parse(value.u.s); if (color) { free(style->overline_color); @@ -626,10 +642,12 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n } value = toml_table_bool(table, "strikethrough"); if (value.ok) { + presence->strikethrough = true; style->strikethrough = value.u.b; } value = toml_table_string(table, "strikethrough_color"); if (value.ok) { + presence->strikethrough_color = true; color = cho_color_parse(value.u.s); if (color) { free(style->strikethrough_color); @@ -642,10 +660,12 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n } value = toml_table_bool(table, "boxed"); if (value.ok) { + presence->boxed = true; style->boxed = value.u.b; } value = toml_table_string(table, "boxed_color"); if (value.ok) { + presence->boxed_color = true; color = cho_color_parse(value.u.s); if (color) { free(style->boxed_color); @@ -658,15 +678,135 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n } value = toml_table_double(table, "rise"); if (value.ok) { + presence->rise = true; style->rise = value.u.d; } value = toml_table_string(table, "href"); if (value.ok) { + presence->href = true; style->href = value.u.s; } return true; } +static void +presence_print(const char *name, struct ChoStylePresence *presence) +{ + printf("---- BEGIN PRESENCE ----\n"); + printf("style '%s'\n", name); + printf("font.name %d\n", presence->font.name); + printf("font.family %d\n", presence->font.family); + printf("font.style %d\n", presence->font.style); + printf("font.weight %d\n", presence->font.weight); + printf("font.size %d\n", presence->font.size); + printf("foreground_color %d\n", presence->foreground_color); + printf("background_color %d\n", presence->background_color); + printf("underline_style %d\n", presence->underline_style); + printf("underline_color %d\n", presence->underline_color); + printf("overline_style %d\n", presence->overline_style); + printf("overline_color %d\n", presence->overline_color); + printf("strikethrough %d\n", presence->strikethrough); + printf("strikethrough_color %d\n", presence->strikethrough_color); + printf("boxed %d\n", presence->boxed); + printf("boxed_color %d\n", presence->boxed_color); + printf("rise %d\n", presence->rise); + printf("href %d\n", presence->href); + printf("---- END PRESENCE ------\n"); +} + +static void +set_text_style( + struct ChoStylePresence *text_presence, + struct ChoStyle *text_style, + struct ChoStylePresence *presence, + struct ChoStyle *style +) +{ + + if (!presence->font.name && + text_presence->font.name) { + free(style->font->name); + style->font->name = strdup(text_style->font->name); + } + if (!presence->font.family && text_presence->font.family) { + style->font->family = text_style->font->family; + } + if (!presence->font.style && text_presence->font.style) { + style->font->style = text_style->font->style; + } + if (!presence->font.weight && text_presence->font.weight) { + style->font->weight = text_style->font->weight; + } + if (!presence->font.size && text_presence->font.size) { + style->font->size = text_style->font->size; + } + if (!presence->foreground_color && text_presence->foreground_color) { + free(style->foreground_color); + style->foreground_color = cho_color_copy(text_style->foreground_color); + } + if (!presence->background_color && text_presence->background_color) { + free(style->background_color); + style->background_color = cho_color_copy(text_style->background_color); + } + if (!presence->underline_style && text_presence->underline_style) { + style->underline_style = text_style->underline_style; + } + if (!presence->underline_color && text_presence->underline_color) { + free(style->underline_color); + style->underline_color = cho_color_copy(text_style->underline_color); + } + if (!presence->overline_style && text_presence->overline_style) { + style->overline_style = text_style->overline_style; + } + if (!presence->overline_color && text_presence->overline_color) { + free(style->overline_color); + style->overline_color = cho_color_copy(text_style->overline_color); + } + if (!presence->strikethrough && text_presence->strikethrough) { + style->strikethrough = text_style->strikethrough; + } + if (!presence->strikethrough_color && text_presence->strikethrough_color) { + free(style->strikethrough_color); + style->strikethrough_color = cho_color_copy(text_style->strikethrough_color); + } + if (!presence->boxed && text_presence->boxed) { + style->boxed = text_style->boxed; + } + if (!presence->boxed_color && text_presence->boxed_color) { + free(style->boxed_color); + style->boxed_color = cho_color_copy(text_style->boxed_color); + } + if (!presence->rise && text_presence->rise) { + style->rise = text_style->rise; + } + if (!presence->href && text_presence->href) { + free(style->href); + style->href = strdup(text_style->href); + } +} + +static void +lyrics_set_text_style_as_default( + struct ChoStylePresence presences[], + struct ChoStyle **styles +) +{ + struct ChoStyle *style, *text_style; + struct ChoStylePresence *presence, *text_presence; + enum SongFragmentType lyric_types[] = { + SF_CHORUS, SF_COMMENT, + SF_COMMENT_ITALIC, SF_COMMENT_BOX + }; + text_presence = &presences[SF_TEXT]; + text_style = styles[SF_TEXT]; + size_t i; + for (i = 0; i<LENGTH(lyric_types); i++) { + presence = &presences[lyric_types[i]]; + style = styles[lyric_types[i]]; + set_text_style(text_presence, text_style, presence, style); + } +} + struct Config * config_load(const char *filepath) { @@ -763,13 +903,12 @@ 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; + int i, unused; const char *key_name; - toml_table_t *key; - struct ChoStyle *style; enum SongFragmentType ftype; - int i; + struct ChoStyle *style; + struct ChoStylePresence presences[SF_LENGTH] = {0}; + toml_table_t *key; for (i = 0; i<toml_table_len(styles); i++) { key_name = toml_table_key(styles, i, &unused); ftype = config_song_fragment_type_parse(key_name); @@ -777,13 +916,14 @@ config_load(const char *filepath) key = toml_table_table(styles, key_name); if (key) { style = config->output->styles[ftype]; - if (!config_load_style(style, key, key_name)) { + if (!config_load_style(style, key, key_name, &presences[ftype])) { LOG_DEBUG("config_load_style failed."); return NULL; } } } } + lyrics_set_text_style_as_default(presences, config->output->styles); } } toml_table_t *parser = toml_table_table(table, "parser"); diff --git a/config.h b/config.h @@ -23,12 +23,6 @@ enum ParseMode { PM_RELAXED }; -struct Note { - char *note; - char *sharp; - char *flat; -}; - enum NoteType { NT_NOTE, NT_SHARP, @@ -43,6 +37,36 @@ enum Instrument { INS_UNKNOWN }; +struct FontPresence { + bool name; + bool family; + bool style; + bool weight; + bool size; +}; + +struct ChoStylePresence { + struct FontPresence font; + bool foreground_color; + bool background_color; + bool underline_style; + bool underline_color; + bool overline_style; + bool overline_color; + bool strikethrough; + bool strikethrough_color; + bool boxed; + bool boxed_color; + bool rise; + bool href; +}; + +struct Note { + char *note; + char *sharp; + char *flat; +}; + struct ConfigChords { enum NamingSystem system; enum ParseMode mode; @@ -69,8 +93,8 @@ struct ConfigToc { }; struct ConfigOutput { - struct ConfigToc *toc; struct ConfigChorus *chorus; + struct ConfigToc *toc; struct ConfigChordDiagram *diagram; enum NamingSystem system; struct ChoStyle **styles; diff --git a/out_pdf.c b/out_pdf.c @@ -193,58 +193,6 @@ fonts_get_all(struct ChoSong **songs, struct Config *config) } } } - const char *name; - for (i = 0; i<SF_LENGTH; i++) { - switch (i) { - case SF_EMPTY: - name = "SF_EMPTY"; - break; - case SF_CHORD: - name = "SF_CHORD"; - break; - case SF_ANNOT: - name = "SF_ANNOT"; - break; - case SF_CHORUS: - name = "SF_CHORUS"; - break; - case SF_FOOTER: - name = "SF_FOOTER"; - break; - case SF_GRID: - name = "SF_GRID"; - break; - case SF_TAB: - name = "SF_TAB"; - break; - case SF_TOC: - name = "SF_TOC"; - break; - case SF_TEXT: - name = "SF_TEXT"; - break; - case SF_TITLE: - name = "SF_TITLE"; - break; - case SF_SUBTITLE: - name = "SF_SUBTITLE"; - break; - case SF_LABEL: - name = "SF_LABEL"; - break; - case SF_COMMENT: - name = "SF_COMMENT"; - break; - case SF_COMMENT_ITALIC: - name = "SF_COMMENT_ITALIC"; - break; - case SF_COMMENT_BOX: - name = "SF_COMMENT_BOX"; - break; - } - printf("%s = %d\n", name, (*so)->present_fragments[i]); - } - printf("\n"); for (se = (*so)->sections; *se; se++) { for (li = (*se)->lines; *li; li++) { for (above = (*li)->text_above; *above; above++) {