commit 36abc048f17df2f33f223ff4f21607c64c80e172
parent 0f329a46fd0e4ae40a91721fcf9d2fe01a8fea8b
Author: nibo <nibo@relim.de>
Date: Mon, 27 Jan 2025 21:02:27 +0100
Support ChoStyle background color
Diffstat:
| M | out_pdf.c | | | 72 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- |
1 file changed, 62 insertions(+), 10 deletions(-)
diff --git a/out_pdf.c b/out_pdf.c
@@ -630,7 +630,13 @@ text_find_fitting(
}
static bool
-pdf_draw_line(pdfio_stream_t *stream, struct PDFText *text, double y, double width, enum LineLocation line_location)
+pdf_draw_line(
+ pdfio_stream_t *stream,
+ struct PDFText *text,
+ double y,
+ double width,
+ enum LineLocation line_location
+)
{
double red, green, blue;
switch (line_location) {
@@ -673,6 +679,42 @@ pdf_draw_line(pdfio_stream_t *stream, struct PDFText *text, double y, double wid
}
static bool
+pdf_draw_rectangle(
+ pdfio_stream_t *stream,
+ struct PDFText *text
+)
+{
+ double height;
+ double red, green, blue;
+ red = text->style->background_color->red / 255.0;
+ green = text->style->background_color->green / 255.0;
+ blue = text->style->background_color->blue / 255.0;
+ if (!pdfioContentSetFillColorRGB(stream, red, green, blue)) {
+ fprintf(stderr, "pdfioContentSetFillColorRGB failed.\n");
+ return false;
+ }
+ height = (8.0 + text->style->font->size) - text->style->font->size * 0.8;
+ if (!pdfioContentPathRect(stream, text->x, text->y, text->width, height)) {
+ fprintf(stderr, "pdfioContentPathRect failed.\n");
+ return false;
+ }
+ if (!pdfioContentFill(stream, true)) {
+ fprintf(stderr, "pdfioContentFill failed.\n");
+ return false;
+ }
+ return true;
+}
+
+static bool
+is_purest_white(struct RGBColor *color)
+{
+ if (color->red == 255 && color->green == 255 && color->blue == 255) {
+ return true;
+ }
+ return false;
+}
+
+static bool
pdf_text_show(pdfio_stream_t *stream, struct PDFText *text)
{
double red, green, blue;
@@ -682,6 +724,12 @@ pdf_text_show(pdfio_stream_t *stream, struct PDFText *text)
return false;
}
unicode = !is_base_font(text->style->font);
+ if (!is_purest_white(text->style->background_color)) {
+ if (!pdf_draw_rectangle(stream, text)) {
+ LOG_DEBUG("draw_rectangle failed.");
+ return false;
+ }
+ }
red = text->style->foreground_color->red / 255.0;
green = text->style->foreground_color->green / 255.0;
blue = text->style->foreground_color->blue / 255.0;
@@ -1954,6 +2002,7 @@ pdf_toc_page_count(
static const char *
toc_dots_create(double available_width, double dot_width)
{
+ available_width -= MARGIN_HORIZONTAL;
int dot_count;
static char dots[1024];
memset(&dots, 0, sizeof(dots));
@@ -1988,11 +2037,10 @@ pdf_texts_add_toc_entry(
LOG_DEBUG("text_width failed.");
return false;
}
- width += MARGIN_HORIZONTAL;
- if (width > max_song_title_width) {
+ if (width+MARGIN_HORIZONTAL > max_song_title_width) {
char *t = (char *)&tmp;
line_count = 0;
- while (width > max_song_title_width) {
+ while (width+MARGIN_HORIZONTAL > max_song_title_width) {
if (ctx->y < MARGIN_BOTTOM) {
if (!pdf_page_close_then_add(ctx, NUS_ROMAN)) {
LOG_DEBUG("pdf_page_close_then_add failed.");
@@ -2014,15 +2062,19 @@ pdf_texts_add_toc_entry(
(*texts)[ctx->text]->y = ctx->y;
ctx->y -= 8.0 + style->font->size;
line_count++;
- t += index + 1;
width = text_width(t, style);
if (width == ERROR) {
LOG_DEBUG("text_width failed.");
return false;
}
(*texts)[ctx->text]->width = width;
+ t += index + 1;
+ width = text_width(t, style);
+ if (width == ERROR) {
+ LOG_DEBUG("text_width failed.");
+ return false;
+ }
ctx->text++;
- width += MARGIN_HORIZONTAL;
}
if (ctx->y < MARGIN_BOTTOM) {
if (!pdf_page_close_then_add(ctx, NUS_ROMAN)) {
@@ -2047,7 +2099,7 @@ pdf_texts_add_toc_entry(
return false;
}
- page_no_x = MEDIABOX_WIDTH - MARGIN_HORIZONTAL - width;
+ page_no_x = MEDIABOX_WIDTH - width - MARGIN_HORIZONTAL;
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);
@@ -2060,7 +2112,7 @@ pdf_texts_add_toc_entry(
(*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]->x = MARGIN_HORIZONTAL + end_of_title_x + TOC_DOTS_GAP_WIDTH;
(*texts)[ctx->text]->y = ctx->y;
ctx->text++;
@@ -2107,7 +2159,7 @@ pdf_texts_add_toc_entry(
return false;
}
- page_no_x = MEDIABOX_WIDTH - MARGIN_HORIZONTAL - width;
+ page_no_x = MEDIABOX_WIDTH - width - MARGIN_HORIZONTAL;
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);
@@ -2120,7 +2172,7 @@ pdf_texts_add_toc_entry(
(*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]->x = MARGIN_HORIZONTAL + end_of_title_x + TOC_DOTS_GAP_WIDTH;
(*texts)[ctx->text]->y = ctx->y;
ctx->text++;