From dd3220b0035fa52448ffa781112358bca9343858 Mon Sep 17 00:00:00 2001 From: Andre Renaud Date: Mon, 23 Feb 2026 22:07:47 +1300 Subject: [PATCH] Change font case to canonical version --- pdfgen.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pdfgen.c b/pdfgen.c index 182f154..7f86b32 100644 --- a/pdfgen.c +++ b/pdfgen.c @@ -208,6 +208,15 @@ static const char png_chunk_palette[] = "PLTE"; static const char png_chunk_data[] = "IDAT"; static const char png_chunk_end[] = "IEND"; +// PDF standard fonts +static const char *valid_fonts[] = { + "Times-Roman", "Times-Bold", + "Times-Italic", "Times-BoldItalic", + "Helvetica", "Helvetica-Bold", + "Helvetica-Oblique", "Helvetica-BoldOblique", + "Courier", "Courier-Bold", + "Courier-Oblique", "Courier-BoldOblique"}; + typedef struct pdf_object pdf_object; enum { @@ -828,6 +837,22 @@ int pdf_set_font(struct pdf_doc *pdf, const char *font) { struct pdf_object *obj; int last_index = 0; + bool found = false; + + // PDF requires one of the 14 standard fonts to be used, so check that + // the font name is valid and canonicalise it to the correct case if it is + for (size_t i = 0; i < sizeof(valid_fonts) / sizeof(valid_fonts[0]); + i++) { + if (strcasecmp(font, valid_fonts[i]) == 0) { + font = valid_fonts[i]; + found = true; + break; + } + } + + if (!found) { + return pdf_set_err(pdf, -EINVAL, "Invalid font name '%s'", font); + } /* See if we've used this font before */ for (obj = pdf_find_first_object(pdf, OBJ_font); obj; obj = obj->next) {