diff --git a/pdfgen.c b/pdfgen.c index c7d258e..cc7f471 100644 --- a/pdfgen.c +++ b/pdfgen.c @@ -1160,7 +1160,17 @@ int pdf_set_font(struct pdf_doc *pdf, const char *font) int pdf_set_font_ttf(struct pdf_doc *pdf, const char *path) { - FILE *fp; + FILE *fp = fopen(path, "rb"); + if (!fp) + return pdf_set_err(pdf, -errno, "Unable to open font file '%s': %s", + path, strerror(errno)); + int err = pdf_set_font_ttf_file(pdf, fp, path); + fclose(fp); + return err; +} + +int pdf_set_font_ttf_file(struct pdf_doc *pdf, FILE *fp, const char *path) +{ uint8_t *font_data; size_t font_data_len; struct pdf_object *font_obj, *descriptor_obj, *stream_obj, *cid_obj; @@ -1180,30 +1190,21 @@ int pdf_set_font_ttf(struct pdf_doc *pdf, const char *path) char font_name[64]; struct stat st; - fp = fopen(path, "rb"); - if (!fp) - return pdf_set_err(pdf, -errno, "Unable to open font file '%s': %s", - path, strerror(errno)); - if (fstat(fileno(fp), &st) < 0) { - fclose(fp); return pdf_set_err(pdf, -errno, "Unable to stat font file '%s': %s", path, strerror(errno)); } font_data_len = (size_t)st.st_size; font_data = (uint8_t *)malloc(font_data_len); if (!font_data) { - fclose(fp); return pdf_set_err(pdf, -ENOMEM, "Unable to allocate %zu bytes for font '%s'", font_data_len, path); } if (fread(font_data, 1, font_data_len, fp) != font_data_len) { free(font_data); - fclose(fp); return pdf_set_err(pdf, -EIO, "Unable to read font file '%s'", path); } - fclose(fp); if (font_data_len < 12) { free(font_data); diff --git a/pdfgen.h b/pdfgen.h index 78a500a..7e42cd5 100644 --- a/pdfgen.h +++ b/pdfgen.h @@ -342,6 +342,17 @@ int pdf_set_font(struct pdf_doc *pdf, const char *font); */ int pdf_set_font_ttf(struct pdf_doc *pdf, const char *path); +/** + * Sets the font to use for text objects by loading a TrueType font file from + * a FILE pointer. + * + * @param pdf PDF document to update font on + * @param fp TTF font file pointer (must be readable and seekable) + * @param path Name of font for error messages and for internal PDF reference + * @return < 0 on failure, 0 on success + */ +int pdf_set_font_ttf_file(struct pdf_doc *pdf, FILE *fp, const char *path); + /** * Calculate the width of a given string in the current font * @param pdf PDF document