lorid

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

commit 0b57e0123cb28e2146b7c4c27e33ed0e4ae6fd65
parent e8c7549835417ffaa6d2288580e9a1747a8c8829
Author: nibo <nibo@relim.de>
Date:   Thu, 16 Jan 2025 17:38:51 +0100

Add dots between toc title and page number

That look awesome

Diffstat:
MMakefile | 2+-
Mout_pdf.c | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mout_pdf.h | 1+
3 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,7 +4,7 @@ MANPREFIX = ${PREFIX}/share/man COLOR = 0 VARS = -DVERSION=\"${VERSION}\" -DCOLOR=${COLOR} -DPREFIX=\"${PREFIX}\" CFLAGS = -pedantic -Wall -Wextra -LDFLAGS = -lpdfio -ltoml -lfontconfig -lgrapheme +LDFLAGS = -lpdfio -ltoml -lfontconfig -lgrapheme -lm SRC = util.c config.c chordpro.c chord_diagram.c out_pdf.c lorid.c compile: diff --git a/out_pdf.c b/out_pdf.c @@ -7,6 +7,7 @@ #include <errno.h> #include <fontconfig/fontconfig.h> #include <grapheme.h> +#include <math.h> #include "chordpro.h" #include "config.h" #include "out_pdf.h" @@ -1460,7 +1461,6 @@ pdf_texts_add_lyrics( int i ) { - printf("pdf_texts_add_lyrics '%s'\n", item->u.text->text); struct PDFText ***texts; struct SpaceNeeded **sp; double width; @@ -1752,18 +1752,34 @@ pdf_toc_page_count( return page + 1; } +static const char * +toc_dots_create(double available_width, double dot_width) +{ + int dot_count; + static char dots[1024]; + memset(&dots, 0, sizeof(dots)); + dot_count = (int)floor(available_width / dot_width); + if (dot_count > 1023) { + return NULL; + } + memset(&dots, (int)'.', dot_count); + dots[dot_count+1] = 0; + return dots; +} + static bool pdf_texts_add_toc_entry( struct PDFContext *ctx, struct TocEntry *entry, struct ChoStyle *style, double max_title_width, - int toc_page_count + int toc_page_count, + double dot_width ) { struct PDFText ***texts; texts = &ctx->content->pages[ctx->page]->texts; - double width, page_no_x; + double width, page_no_x, end_of_title_x, width_between_title_and_page_no, available_dots_width; int index, line_count; char tmp[strlen(entry->title)+1]; char page_no[11+1]; @@ -1810,6 +1826,7 @@ pdf_texts_add_toc_entry( pdf_page_close_then_add(ctx); texts = &ctx->content->pages[ctx->page]->texts; } + end_of_title_x = width; *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); (*texts)[ctx->text]->text = strdup(t); @@ -1826,6 +1843,22 @@ pdf_texts_add_toc_entry( } page_no_x = MEDIABOX_WIDTH - MARGIN_HORIZONTAL - width; + width_between_title_and_page_no = page_no_x - end_of_title_x; + available_dots_width = width_between_title_and_page_no - TOC_DOTS_GAP_WIDTH*2; + const char *dots = toc_dots_create(available_dots_width, dot_width); + if (!dots) { + LOG_DEBUG("toc_dots_create failed."); + return false; + } + + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); + (*texts)[ctx->text] = pdf_text_new(); + (*texts)[ctx->text]->text = strdup(dots); + (*texts)[ctx->text]->style = cho_style_copy(style); + (*texts)[ctx->text]->x = end_of_title_x + TOC_DOTS_GAP_WIDTH; + (*texts)[ctx->text]->y = ctx->y; + ctx->text++; + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); (*texts)[ctx->text]->text = strdup(page_no); @@ -1851,6 +1884,7 @@ pdf_texts_add_toc_entry( ctx->content->pages[ctx->page] = pdf_page_new(); texts = &ctx->content->pages[ctx->page]->texts; } + end_of_title_x = width; *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); (*texts)[ctx->text]->text = strdup(entry->title); @@ -1872,6 +1906,22 @@ pdf_texts_add_toc_entry( } page_no_x = MEDIABOX_WIDTH - MARGIN_HORIZONTAL - width; + width_between_title_and_page_no = page_no_x - end_of_title_x; + available_dots_width = width_between_title_and_page_no - TOC_DOTS_GAP_WIDTH*2; + const char *dots = toc_dots_create(available_dots_width, dot_width); + if (!dots) { + LOG_DEBUG("toc_dots_create failed."); + return false; + } + + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); + (*texts)[ctx->text] = pdf_text_new(); + (*texts)[ctx->text]->text = strdup(dots); + (*texts)[ctx->text]->style = cho_style_copy(style); + (*texts)[ctx->text]->x = end_of_title_x + TOC_DOTS_GAP_WIDTH; + (*texts)[ctx->text]->y = ctx->y; + ctx->text++; + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); (*texts)[ctx->text]->text = strdup(page_no); @@ -1896,7 +1946,7 @@ pdf_toc_create( struct Config *config ) { - double max_title_width; + double max_title_width, dot_width; int toc_page_count; struct PDFContext ctx; struct PDFText ***texts; @@ -1924,8 +1974,13 @@ pdf_toc_create( return false; } cho_style_free(toc_title_style); */ + dot_width = text_width(".", toc_style); + if (dot_width == ERROR) { + LOG_DEBUG("text_width failed."); + return false; + } for (; *toc; toc++) { - if (!pdf_texts_add_toc_entry(&ctx, *toc, toc_style, max_title_width, toc_page_count)) { + if (!pdf_texts_add_toc_entry(&ctx, *toc, toc_style, max_title_width, toc_page_count, dot_width)) { LOG_DEBUG("pdf_texts_add_toc_entry failed."); return false; } diff --git a/out_pdf.h b/out_pdf.h @@ -10,6 +10,7 @@ #define LINE_WIDTH MEDIABOX_WIDTH - MARGIN_HORIZONTAL * 2 #define MIN_CHORD_GAP_WIDTH 5.0 #define SECTION_GAP_WIDTH 10.0 +#define TOC_DOTS_GAP_WIDTH 10.0 #define PATH_MAX 4096