aboutsummaryrefslogtreecommitdiff
path: root/src/pdf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pdf.c')
-rw-r--r--src/pdf.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/pdf.c b/src/pdf.c
index c6ae876..5718ed7 100644
--- a/src/pdf.c
+++ b/src/pdf.c
@@ -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;