lorid

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

commit 1e75427cf32e69e0878e7f9dc5c01c68f46d7c1c
parent 5ff41eade19ad7e68885727f900de471a85d03ed
Author: nibo <nibo@relim.de>
Date:   Wed, 15 Jan 2025 09:46:20 +0100

Store width in PDFText to not measure it twice

Diffstat:
Mout_pdf.c | 53+++++++++++++++++++++++++++++------------------------
Mout_pdf.h | 1+
2 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/out_pdf.c b/out_pdf.c @@ -646,15 +646,9 @@ pdf_draw_line(pdfio_stream_t *stream, struct PDFText *text, double y, double wid static bool pdf_text_show(pdfio_stream_t *stream, struct PDFText *text) { - double red, green, blue, width; + double red, green, blue; bool unicode; - // TODO: Maybe store the width in PDFText!? // TODO: Store 'unicode' in PDFText too - width = text_width(text->text, text->style); - if (width == ERROR) { - LOG_DEBUG("text_width failed."); - return false; - } if (!pdf_font_set(stream, text->style->font)) { LOG_DEBUG("pdf_font_set failed."); return false; @@ -688,19 +682,19 @@ pdf_text_show(pdfio_stream_t *stream, struct PDFText *text) return false; } if (text->style->underline_style == LS_SINGLE) { - pdf_draw_line(stream, text, text->y, width, LL_UNDER); + pdf_draw_line(stream, text, text->y, text->width, LL_UNDER); } else if (text->style->underline_style == LS_DOUBLE) { - pdf_draw_line(stream, text, text->y, width, LL_UNDER); - pdf_draw_line(stream, text, text->y-1.5, width, LL_UNDER); + pdf_draw_line(stream, text, text->y, text->width, LL_UNDER); + pdf_draw_line(stream, text, text->y-1.5, text->width, LL_UNDER); } if (text->style->strikethrough) { - pdf_draw_line(stream, text, text->y, width, LL_STRIKETHROUGH); + pdf_draw_line(stream, text, text->y, text->width, LL_STRIKETHROUGH); } if (text->style->overline_style == LS_SINGLE) { - pdf_draw_line(stream, text, text->y, width, LL_OVER); + pdf_draw_line(stream, text, text->y, text->width, LL_OVER); } else if (text->style->overline_style == LS_DOUBLE) { - pdf_draw_line(stream, text, text->y, width, LL_OVER); - pdf_draw_line(stream, text, text->y+1.5, width, LL_OVER); + pdf_draw_line(stream, text, text->y, text->width, LL_OVER); + pdf_draw_line(stream, text, text->y+1.5, text->width, LL_OVER); } if (text->style->boxed) { red = text->style->boxed_color->red / 255.0; @@ -710,7 +704,7 @@ pdf_text_show(pdfio_stream_t *stream, struct PDFText *text) LOG_DEBUG("pdfioContentSetFillColorRGB failed."); return false; } - if (!pdfioContentPathRect(stream, text->x - 2.0, text->y - 2.0, width + 4.0, text->style->font->size * 0.8 + 4.0)) { + if (!pdfioContentPathRect(stream, text->x - 2.0, text->y - 2.0, text->width + 4.0, text->style->font->size * 0.8 + 4.0)) { LOG_DEBUG("pdfioContentPathRect failed."); return false; } @@ -1115,6 +1109,7 @@ pdf_text_new(void) t->style = NULL; t->x = 0.0; t->y = 0.0; + t->width = 0.0; return t; } @@ -1462,11 +1457,12 @@ pdf_texts_add_lyrics( (*texts)[ctx->text]->style = cho_style_copy(item->u.text->style); (*texts)[ctx->text]->x = ctx->x; (*texts)[ctx->text]->y = ctx->y; - width = text_width((*texts)[ctx->text]->text, item->u.text->style); + width = text_width(item->u.text->text, item->u.text->style); if (width == ERROR) { LOG_DEBUG("text_width failed."); return false; } + (*texts)[ctx->text]->width = width; if (item->u.text->style->href) { if (!annot_url_link_add(ctx, item->u.text->style, width)) { LOG_DEBUG("annot_url_link_add failed."); @@ -1504,6 +1500,7 @@ pdf_texts_add_lyrics( LOG_DEBUG("text_width failed."); return false; } + (*texts)[ctx->text]->width = width; if (item->u.text->style->href) { if (!annot_url_link_add(ctx, item->u.text->style, width)) { LOG_DEBUG("annot_url_link_add failed."); @@ -1530,6 +1527,7 @@ pdf_texts_add_lyrics( LOG_DEBUG("text_width failed."); return false; } + (*texts)[ctx->text]->width = width; if (item->u.text->style->href) { if (!annot_url_link_add(ctx, item->u.text->style, width)) { LOG_DEBUG("annot_url_link_add failed."); @@ -1595,6 +1593,7 @@ pdf_texts_add_text( (*texts)[ctx->text]->style = cho_style_copy(style); (*texts)[ctx->text]->x = ctx->x; (*texts)[ctx->text]->y = ctx->y; + (*texts)[ctx->text]->width = width; if (style->href) { if (!annot_url_link_add(ctx, style, width)) { LOG_DEBUG("annot_url_link_add failed."); @@ -1622,6 +1621,7 @@ pdf_texts_add_text( (*texts)[ctx->text]->style = cho_style_copy(style); (*texts)[ctx->text]->x = ctx->x; (*texts)[ctx->text]->y = ctx->y; + (*texts)[ctx->text]->width = width; if (style->href) { if (!annot_url_link_add(ctx, style, width)) { LOG_DEBUG("annot_url_link_add failed."); @@ -1638,6 +1638,7 @@ pdf_texts_add_text( (*texts)[ctx->text]->style = cho_style_copy(style); (*texts)[ctx->text]->x = ctx->x; (*texts)[ctx->text]->y = ctx->y; + (*texts)[ctx->text]->width = width; if (style->href) { if (!annot_url_link_add(ctx, style, width)) { LOG_DEBUG("annot_url_link_add failed."); @@ -1721,8 +1722,7 @@ pdf_texts_add_toc_entry( LOG_DEBUG("text_width failed."); return false; } - width += MARGIN_HORIZONTAL; - if (width > max_title_width) { + if (width+MARGIN_HORIZONTAL > max_title_width) { char *t = (char *)&tmp; line_count = 0; while (width > max_title_width) { @@ -1757,6 +1757,7 @@ pdf_texts_add_toc_entry( LOG_DEBUG("text_width failed."); return false; } + (*texts)[ctx->text]->width = width; } if (ctx->y < MARGIN_BOTTOM) { *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); @@ -1774,6 +1775,7 @@ pdf_texts_add_toc_entry( (*texts)[ctx->text]->style = cho_style_copy(style); (*texts)[ctx->text]->x = MARGIN_HORIZONTAL; (*texts)[ctx->text]->y = ctx->y; + (*texts)[ctx->text]->width = width; ctx->text++; sprintf((char *)&page_no, "%d", entry->page_index+1); width = text_width(page_no, style); @@ -1789,6 +1791,7 @@ pdf_texts_add_toc_entry( (*texts)[ctx->text]->style = cho_style_copy(style); (*texts)[ctx->text]->x = page_no_x; (*texts)[ctx->text]->y = ctx->y; + (*texts)[ctx->text]->width = width; line_count++; if (!annot_page_link_add(ctx, entry, toc_page_count, line_count, style->font->size)) { LOG_DEBUG("annot_page_link_add"); @@ -1813,6 +1816,7 @@ pdf_texts_add_toc_entry( (*texts)[ctx->text]->style = cho_style_copy(style); (*texts)[ctx->text]->x = MARGIN_HORIZONTAL; (*texts)[ctx->text]->y = ctx->y; + (*texts)[ctx->text]->width = width; ctx->text++; sprintf((char *)&page_no, "%d", entry->page_index+1); width = text_width(page_no, style); @@ -1828,6 +1832,7 @@ pdf_texts_add_toc_entry( (*texts)[ctx->text]->style = cho_style_copy(style); (*texts)[ctx->text]->x = page_no_x; (*texts)[ctx->text]->y = ctx->y; + (*texts)[ctx->text]->width = width; if (!annot_page_link_add(ctx, entry, toc_page_count, 1, style->font->size)) { LOG_DEBUG("annot_page_link_add"); return false; @@ -2066,13 +2071,13 @@ pdf_content_create( } (*texts)[ctx.text]->text = string; (*texts)[ctx.text]->style = style; + (*texts)[ctx.text]->width = text_width(string, style); + if ((*texts)[ctx.text]->width == ERROR) { + LOG_DEBUG("text_width failed."); + return false; + } if (style->href) { - width = text_width(string, style); - if (width == ERROR) { - LOG_DEBUG("text_width failed."); - return false; - } - if (!annot_url_link_add(&ctx, style, width)) { + if (!annot_url_link_add(&ctx, style, (*texts)[ctx.text]->width)) { LOG_DEBUG("annot_url_link_add failed."); return false; } diff --git a/out_pdf.h b/out_pdf.h @@ -39,6 +39,7 @@ struct PDFText { struct ChoStyle *style; double x; double y; + double width; }; struct PDFImage {