static void pdf_load_embedded_font(pdf_document *xref, pdf_font_desc *fontdesc, char *fontname, pdf_obj *stmref) { fz_buffer *buf; fz_context *ctx = xref->ctx; fz_try(ctx) { buf = pdf_load_stream(xref, pdf_to_num(stmref), pdf_to_gen(stmref)); } fz_catch(ctx) { fz_throw(ctx, "cannot load font stream (%d %d R)", pdf_to_num(stmref), pdf_to_gen(stmref)); } fz_try(ctx) { fontdesc->font = fz_new_font_from_memory(ctx, fontname, buf->data, buf->len, 0, 1); } fz_catch(ctx) { fz_drop_buffer(ctx, buf); fz_throw(ctx, "cannot load embedded font (%d %d R)", pdf_to_num(stmref), pdf_to_gen(stmref)); } fontdesc->size += buf->len; /* save the buffer so we can free it later */ fontdesc->font->ft_data = buf->data; fontdesc->font->ft_size = buf->len; fz_free(ctx, buf); /* only free the fz_buffer struct, not the contained data */ fontdesc->is_embedded = 1; }
static void pdf_load_builtin_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname, int has_descriptor) { FT_Face face; const char *clean_name = clean_font_name(fontname); fontdesc->font = fz_load_system_font(ctx, fontname, 0, 0, !has_descriptor); if (!fontdesc->font) { unsigned char *data; unsigned int len; data = pdf_lookup_builtin_font(clean_name, &len); if (!data) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find builtin font: '%s'", fontname); fontdesc->font = fz_new_font_from_memory(ctx, fontname, data, len, 0, 1); } if (!strcmp(clean_name, "Symbol") || !strcmp(clean_name, "ZapfDingbats")) fontdesc->flags |= PDF_FD_SYMBOLIC; face = fontdesc->font->ft_face; fontdesc->ascent = 1000.0f * face->ascender / face->units_per_EM; fontdesc->descent = 1000.0f * face->descender / face->units_per_EM; }
static void pdf_load_substitute_cjk_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname, int ros, int serif) { unsigned char *data; unsigned int len; data = pdf_lookup_substitute_cjk_font(ros, serif, &len); if (!data) fz_throw(ctx, "cannot find builtin CJK font"); /* a glyph bbox cache is too big for droid sans fallback (51k glyphs!) */ fontdesc->font = fz_new_font_from_memory(ctx, fontname, data, len, 0, 0); fontdesc->font->ft_substitute = 1; }
static void pdf_load_substitute_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname, int mono, int serif, int bold, int italic) { unsigned char *data; unsigned int len; data = pdf_lookup_substitute_font(mono, serif, bold, italic, &len); if (!data) fz_throw(ctx, "cannot find substitute font"); fontdesc->font = fz_new_font_from_memory(ctx, fontname, data, len, 0, 1); fontdesc->font->ft_substitute = 1; fontdesc->font->ft_bold = bold && !ft_is_bold(fontdesc->font->ft_face); fontdesc->font->ft_italic = italic && !ft_is_italic(fontdesc->font->ft_face); }
static void pdf_load_builtin_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname) { unsigned char *data; unsigned int len; data = pdf_lookup_builtin_font(fontname, &len); if (!data) fz_throw(ctx, "cannot find builtin font: '%s'", fontname); fontdesc->font = fz_new_font_from_memory(ctx, data, len, 0, 1); /* RJW: "cannot load freetype font from memory" */ if (!strcmp(fontname, "Symbol") || !strcmp(fontname, "ZapfDingbats")) fontdesc->flags |= PDF_FD_SYMBOLIC; }
static void pdf_load_builtin_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname) { unsigned char *data; unsigned int len; fontname = clean_font_name(fontname); data = pdf_lookup_builtin_font(fontname, &len); if (!data) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find builtin font: '%s'", fontname); fontdesc->font = fz_new_font_from_memory(ctx, fontname, data, len, 0, 1); if (!strcmp(fontname, "Symbol") || !strcmp(fontname, "ZapfDingbats")) fontdesc->flags |= PDF_FD_SYMBOLIC; }
static void pdf_load_substitute_cjk_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname, int ros, int serif) { fontdesc->font = fz_load_system_cjk_font(ctx, fontname, ros, serif); if (!fontdesc->font) { unsigned char *data; unsigned int len; int index; data = pdf_lookup_substitute_cjk_font(ros, serif, fontdesc->wmode, &len, &index); if (!data) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find builtin CJK font"); /* A glyph bbox cache is too big for CJK fonts. */ fontdesc->font = fz_new_font_from_memory(ctx, fontname, data, len, index, 0); } fontdesc->font->ft_substitute = 1; }
void ui_init_fonts(fz_context *ctx, float pixelsize) { const char *data; int size; glGenTextures(1, &g_cache_tex); glBindTexture(GL_TEXTURE_2D, g_cache_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g_cache_w, g_cache_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL); clear_font_cache(); data = fz_lookup_builtin_font(ctx, "Charis SIL", 0, 0, &size); if (!data) data = fz_lookup_builtin_font(ctx, "Times", 0, 0, &size); g_font = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0); g_font_size = pixelsize; }
fz_font * fz_load_html_font(fz_context *ctx, fz_html_font_set *set, const char *family, const char *variant, const char *style, const char *weight) { unsigned char *data; unsigned int size; int is_mono = !strcmp(family, "monospace"); int is_sans = !strcmp(family, "sans-serif"); int is_bold = !strcmp(weight, "bold") || !strcmp(weight, "bolder") || atoi(weight) > 400; int is_italic = !strcmp(style, "italic") || !strcmp(style, "oblique"); int idx = is_mono * 8 + is_sans * 4 + is_bold * 2 + is_italic; if (!set->fonts[idx]) { data = pdf_lookup_builtin_font(ctx, font_names[idx], &size); if (!data) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load html font: %s", font_names[idx]); set->fonts[idx] = fz_new_font_from_memory(ctx, font_names[idx], data, size, 0, 1); } return set->fonts[idx]; }
int pdfportfolio_main(int argc, char **argv) { char *password = ""; char *outfile = NULL; char *outopts = "compress"; char *infile; int exit_code = 0; int do_save = 0; int has_old_file = 0; int c; while ((c = fz_getopt(argc, argv, "p:o:O:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'o': outfile = fz_optarg; break; case 'O': outopts = fz_optarg; break; default: usage(); break; } } if (fz_optind == argc) usage(); infile = argv[fz_optind++]; if (!outfile) outfile = infile; ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } if (fz_file_exists(ctx, infile)) { doc = pdf_open_document(ctx, infile); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); has_old_file = 1; } else { doc = pdf_create_document(ctx); /* add a blank page */ { const char *template = "BT /Tm 16 Tf 50 434 TD (This is a portfolio document.) Tj ET\n"; const unsigned char *data; int size; fz_font *font; pdf_obj *font_obj, *page_obj; pdf_obj *resources; fz_buffer *contents; fz_rect mediabox = { 0, 0, 400, 500 }; data = fz_lookup_base14_font(ctx, "Times-Roman", &size); font = fz_new_font_from_memory(ctx, "Times-Roman", data, size, 0, 0); font_obj = pdf_add_simple_font(ctx, doc, font, PDF_SIMPLE_ENCODING_LATIN); fz_drop_font(ctx, font); resources = pdf_add_new_dict(ctx, doc, 1); pdf_dict_putp_drop(ctx, resources, "Font/Tm", font_obj); contents = fz_new_buffer_from_shared_data(ctx, (const unsigned char *)template, strlen(template));