diff options
| author | Anders Betts <anders.betts@gmail.com> | 2026-09-22 09:04:27 +0200 |
|---|---|---|
| committer | Anders Betts <anders.betts@gmail.com> | 2026-09-22 09:04:27 +0200 |
| commit | 227d0422da0062410c85529bc999d7e509d9de78 (patch) | |
| tree | 23102d1fb049b610ec154e7c136e3d92d941bd6d /src/pdf.c | |
| parent | 8ceb1340a9030bb1030be61003dc65b444732b9e (diff) | |
| download | bokf-0.1.63.tar.gz bokf-0.1.63.zip | |
documents: org name in the invoice and payslip headers, document_header_colorv0.1.63
Diffstat (limited to 'src/pdf.c')
| -rw-r--r-- | src/pdf.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -353,6 +353,42 @@ double pdf_font_ascent(const char *font, double size) return size > 0 ? size * PDF_ASCENT / 1000.0 : 0.0; } +void pdf_text_fit(struct pdf *p, double x, double baseline, const char *font, + double size, double min_size, double max_w, const char *rgb, + const char *utf8) +{ + char *truncated = NULL; + const char *text = utf8; + double w; + + if (!utf8 || !*utf8 || max_w <= 0) + return; + w = pdf_text_width(font, size, utf8); + if (w > max_w) { + size = size * max_w / w; + if (size < min_size) + size = min_size; + w = pdf_text_width(font, size, utf8); + } + if (w > max_w) { + size_t n = strlen(utf8); + + truncated = xmalloc(n + 4); + memcpy(truncated, utf8, n); + while (n > 0) { + memcpy(truncated + n, "...", 4); + if (pdf_text_width(font, size, truncated) <= max_w) + break; + n--; + while (n > 0 && ((unsigned char)truncated[n] & 0xC0) == 0x80) + n--; + } + text = truncated; + } + pdf_text(p, x, baseline, font, size, rgb, text); + free(truncated); +} + static int parse_num(const char **sp, double *out) { const char *s = *sp; |
