lorid

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

commit 7fcb2d8fa267c05dc576d30f9a9ef8b7b5192b2c
parent d966cc31da06a63942a70d9646ca5bff6cf3037c
Author: nibo <nibo@relim.de>
Date:   Fri, 26 Jul 2024 16:54:59 +0200

Fix memory leaks and correct DIN A4 dimensions

Diffstat:
Mout_pdf.c | 126+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mout_pdf.h | 6++----
2 files changed, 67 insertions(+), 65 deletions(-)

diff --git a/out_pdf.c b/out_pdf.c @@ -495,10 +495,10 @@ static void text_line_set_lineheight(struct TextLine *line, enum SongFragmentTyp } switch (ftype) { case SF_CHORD: - line->height = 8.0 + biggest_font_size; + line->height = 2.0 + biggest_font_size; break; default: - line->height = 2.0 + biggest_font_size; + line->height = 8.0 + biggest_font_size; } } @@ -587,60 +587,62 @@ static struct Text **text_create(struct ChoSong **songs, struct Config *config) } lines = songs[so]->sections[se]->lines; for (li = 0; lines[li]; li++, tl++) { - text[t]->lines = realloc(text[t]->lines, (tl+1) * sizeof(struct TextLine *)); - text[t]->lines[tl] = malloc(sizeof(struct TextLine)); - text[t]->lines[tl]->items = NULL; chords = songs[so]->sections[se]->lines[li]->chords; int chords_len = cho_chord_count(chords); - printable_item = config_printable_item_get(config->printable_items, "chord"); - if (!printable_item) { - fprintf(stderr, "config_printable_item_get failed.\n"); - return NULL; - } - double added_space = 0.0; - for (ch = 0; chords[ch]; ch++, tli++) { - text[t]->lines[tl]->items = realloc(text[t]->lines[tl]->items, (tli+1) * sizeof(struct TextLineItem *)); - text[t]->lines[tl]->items[tli] = malloc(sizeof(struct TextLineItem)); - if (tli == 0) { - text[t]->lines[tl]->items[tli]->x = PADDING + line_width_until_chord(lines[li], chords[ch], NULL); - } else { - text[t]->lines[tl]->items[tli]->x = PADDING + line_width_until_chord(lines[li], chords[ch], NULL) + added_space; + if (chords_len > 0) { + text[t]->lines = realloc(text[t]->lines, (tl+1) * sizeof(struct TextLine *)); + text[t]->lines[tl] = malloc(sizeof(struct TextLine)); + text[t]->lines[tl]->items = NULL; + printable_item = config_printable_item_get(config->printable_items, "chord"); + if (!printable_item) { + fprintf(stderr, "config_printable_item_get failed.\n"); + return NULL; } - if (add_space_to_next_chord) { - added_space += space.amount; - text[t]->lines[tl]->items[tli]->x += space.amount; - add_space_to_next_chord = false; - } - // TODO: implement chord style - text[t]->lines[tl]->items[tli]->style = cho_style_duplicate(printable_item->style); - text[t]->lines[tl]->items[tli]->text = strdup(chords[ch]->chord); - if (chords_len == ch+1) { - break; - } - if (!out_pdf_chord_is_enough_space( - songs[so]->sections[se]->lines[li], - chords[ch], - chords[ch+1], - printable_item->style->font, - &space - )) { - spaces = realloc(spaces, (sn+1) * sizeof(struct SpaceNeeded *)); - spaces[sn] = malloc(sizeof(struct SpaceNeeded)); - spaces[sn]->line_item_index = space.line_item_index; - spaces[sn]->text_index = space.text_index; - spaces[sn]->amount = space.amount; - sn++; - add_space_to_next_chord = true; + double added_space = 0.0; + for (ch = 0; chords[ch]; ch++, tli++) { + text[t]->lines[tl]->items = realloc(text[t]->lines[tl]->items, (tli+1) * sizeof(struct TextLineItem *)); + text[t]->lines[tl]->items[tli] = malloc(sizeof(struct TextLineItem)); + if (tli == 0) { + text[t]->lines[tl]->items[tli]->x = PADDING + line_width_until_chord(lines[li], chords[ch], NULL); + } else { + text[t]->lines[tl]->items[tli]->x = PADDING + line_width_until_chord(lines[li], chords[ch], NULL) + added_space; + } + if (add_space_to_next_chord) { + added_space += space.amount; + text[t]->lines[tl]->items[tli]->x += space.amount; + add_space_to_next_chord = false; + } + // TODO: implement chord style + text[t]->lines[tl]->items[tli]->style = cho_style_duplicate(printable_item->style); + text[t]->lines[tl]->items[tli]->text = strdup(chords[ch]->chord); + if (chords_len == ch+1) { + break; + } + if (!out_pdf_chord_is_enough_space( + songs[so]->sections[se]->lines[li], + chords[ch], + chords[ch+1], + printable_item->style->font, + &space + )) { + spaces = realloc(spaces, (sn+1) * sizeof(struct SpaceNeeded *)); + spaces[sn] = malloc(sizeof(struct SpaceNeeded)); + spaces[sn]->line_item_index = space.line_item_index; + spaces[sn]->text_index = space.text_index; + spaces[sn]->amount = space.amount; + sn++; + add_space_to_next_chord = true; + } } + spaces = realloc(spaces, (sn+1) * sizeof(struct SpaceNeeded *)); + spaces[sn] = NULL; + tli++; + text[t]->lines[tl]->items = realloc(text[t]->lines[tl]->items, (tli+1) * sizeof(struct TextLineItem *)); + text[t]->lines[tl]->items[tli] = NULL; + text_line_set_lineheight(text[t]->lines[tl], SF_CHORD); + tli = 0; + tl++; } - spaces = realloc(spaces, (sn+1) * sizeof(struct SpaceNeeded *)); - spaces[sn] = NULL; - tli++; - text[t]->lines[tl]->items = realloc(text[t]->lines[tl]->items, (tli+1) * sizeof(struct TextLineItem *)); - text[t]->lines[tl]->items[tli] = NULL; - text_line_set_lineheight(text[t]->lines[tl], SF_CHORD); - tli = 0; - tl++; text[t]->lines = realloc(text[t]->lines, (tl+1) * sizeof(struct TextLine *)); text[t]->lines[tl] = malloc(sizeof(struct TextLine)); text[t]->lines[tl]->items = NULL; @@ -660,7 +662,7 @@ static struct Text **text_create(struct ChoSong **songs, struct Config *config) } int tlii = 0; for (ii = 0; lines[li]->lyrics[ly]->text[ii] != 0; ii++) { - if ((sp = needs_space(spaces, ly, ii))) { + if (spaces && (sp = needs_space(spaces, ly, ii))) { text[t]->lines[tl]->items[tli]->text = realloc(text[t]->lines[tl]->items[tli]->text, (tlii+1) * sizeof(char)); text[t]->lines[tl]->items[tli]->text[tlii] = lines[li]->lyrics[ly]->text[ii]; tlii++; @@ -682,23 +684,25 @@ static struct Text **text_create(struct ChoSong **songs, struct Config *config) } text[t]->lines[tl]->items[tli]->text = realloc(text[t]->lines[tl]->items[tli]->text, (tlii+1) * sizeof(char)); text[t]->lines[tl]->items[tli]->text[tlii] = 0; + tli++; } - tli++; text[t]->lines[tl]->items = realloc(text[t]->lines[tl]->items, (tli+1) * sizeof(struct TextLineItem *)); text[t]->lines[tl]->items[tli] = NULL; text_line_set_lineheight(text[t]->lines[tl], SF_TEXT); tli = 0; - for (sn = 0; spaces[sn]; sn++) { - free(spaces[sn]); + if (spaces) { + for (sn = 0; spaces[sn]; sn++) { + free(spaces[sn]); + } + free(spaces); + spaces = NULL; + sn = 0; } - free(spaces); - spaces = NULL; - sn = 0; } - text[t]->lines = realloc(text[t]->lines, (tl+1) * sizeof(struct TextLine *)); - text[t]->lines[tl] = NULL; - tl = 0; } + text[t]->lines = realloc(text[t]->lines, (tl+1) * sizeof(struct TextLine *)); + text[t]->lines[tl] = NULL; + tl = 0; } text = realloc(text, (t+1) * sizeof(struct Text *)); text[t] = NULL; diff --git a/out_pdf.h b/out_pdf.h @@ -1,9 +1,7 @@ #include <pdfio.h> -#define MEDIABOX_HEIGHT 842.0 -#define MEDIABOX_WIDTH 595.0 -// #define PAGE_HEIGHT MEDIABOX_HEIGHT - 36.0 -// #define PAGE_WIDTH MEDIABOX_WIDTH - 36.0 +#define MEDIABOX_HEIGHT 878.0 +#define MEDIABOX_WIDTH 631.0 #define PADDING 80.0 #define LINE_LEN MEDIABOX_WIDTH - PADDING * 2