static int showpage(fz_context *ctx, pdf_document *doc, fz_output *out, int page) { pdf_obj *pageref; int failed = 0; fz_write_printf(ctx, out, "<page pagenum=\"%d\">\n", page); fz_try(ctx) { pageref = pdf_lookup_page_obj(ctx, doc, page-1); if (!pageref) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot retrieve info from page %d", page); } fz_catch(ctx) { fz_write_printf(ctx, out, "Failed to gather information for page %d\n", page); failed = 1; } if (!failed) { failed |= showbox(ctx, out, pageref, "MediaBox", PDF_NAME(MediaBox)); failed |= showbox(ctx, out, pageref, "CropBox", PDF_NAME(CropBox)); failed |= showbox(ctx, out, pageref, "ArtBox", PDF_NAME(ArtBox)); failed |= showbox(ctx, out, pageref, "BleedBox", PDF_NAME(BleedBox)); failed |= showbox(ctx, out, pageref, "TrimBox", PDF_NAME(TrimBox)); failed |= shownum(ctx, out, pageref, "Rotate", PDF_NAME(Rotate)); failed |= shownum(ctx, out, pageref, "UserUnit", PDF_NAME(UserUnit)); } fz_write_printf(ctx, out, "</page>\n"); return failed; }
static void fz_edgebuffer_print_app(fz_context *ctx, fz_output *out, fz_edgebuffer * edgebuffer) { int i; int height = edgebuffer->super.clip.y1 - edgebuffer->super.clip.y0; fz_write_printf(ctx, out, "Edgebuffer %x\n", edgebuffer); fz_write_printf(ctx, out, "xmin=%x xmax=%x base=%x height=%x\n", edgebuffer->super.clip.x0, edgebuffer->super.clip.x1, edgebuffer->super.clip.y0, height); if (edgebuffer->table == NULL) return; for (i=0; i < height; i++) { int offset = edgebuffer->index[i]; int *row = &edgebuffer->table[offset]; int count = *row++; int count0 = count; fz_write_printf(ctx, out, "%x @ %x: %d =", i, offset, count); while (count-- > 0) { int l = *row++; int r = *row++; fz_write_printf(ctx, out, " %x:%x", l, r); } assert((count0 & 1) == 0); (void)count0; fz_write_printf(ctx, out, "\n"); } }
void fz_print_stext_block_as_html(fz_context *ctx, fz_output *out, fz_stext_block *block) { fz_stext_line *line; fz_stext_char *ch; int x, y; fz_font *font = NULL; float size = 0; int sup = 0; for (line = block->u.t.first_line; line; line = line->next) { x = line->bbox.x0; y = line->bbox.y0; fz_write_printf(ctx, out, "<p style=\"top:%dpt;left:%dpt;\">", y, x); font = NULL; for (ch = line->first_char; ch; ch = ch->next) { int ch_sup = detect_super_script(line, ch); if (ch->font != font || ch->size != size) { if (font) fz_print_style_end_html(ctx, out, font, size, sup); font = ch->font; size = ch->size; sup = ch_sup; fz_print_style_begin_html(ctx, out, font, size, sup); } switch (ch->c) { default: if (ch->c >= 32 && ch->c <= 127) fz_write_byte(ctx, out, ch->c); else fz_write_printf(ctx, out, "&#x%x;", ch->c); break; case '<': fz_write_string(ctx, out, "<"); break; case '>': fz_write_string(ctx, out, ">"); break; case '&': fz_write_string(ctx, out, "&"); break; case '"': fz_write_string(ctx, out, """); break; case '\'': fz_write_string(ctx, out, "'"); break; } } if (font) fz_print_style_end_html(ctx, out, font, size, sup); fz_write_string(ctx, out, "</p>\n"); } }
static void pdf_out_gs_begin(fz_context *ctx, pdf_processor *proc, const char *name, pdf_obj *extgstate) { fz_output *out = ((pdf_output_processor*)proc)->out; ((pdf_output_processor*)proc)->extgstate = 1; fz_write_printf(ctx, out, "/%s gs\n", name); }
static void pdf_out_ri(fz_context *ctx, pdf_processor *proc, const char *intent) { fz_output *out = ((pdf_output_processor*)proc)->out; if (!((pdf_output_processor*)proc)->extgstate) fz_write_printf(ctx, out, "/%s ri\n", intent); }
static void pdf_out_M(fz_context *ctx, pdf_processor *proc, float a) { fz_output *out = ((pdf_output_processor*)proc)->out; if (!((pdf_output_processor*)proc)->extgstate) fz_write_printf(ctx, out, "%g M\n", a); }
static void pdf_out_Tf(fz_context *ctx, pdf_processor *proc, const char *name, pdf_font_desc *font, float size) { fz_output *out = ((pdf_output_processor*)proc)->out; if (!((pdf_output_processor*)proc)->extgstate) fz_write_printf(ctx, out, "/%s %g Tf\n", name, size); }
static void pdf_out_J(fz_context *ctx, pdf_processor *proc, int linecap) { fz_output *out = ((pdf_output_processor*)proc)->out; if (!((pdf_output_processor*)proc)->extgstate) fz_write_printf(ctx, out, "%d J\n", linecap); }
static void pdf_out_i(fz_context *ctx, pdf_processor *proc, float flatness) { fz_output *out = ((pdf_output_processor*)proc)->out; if (!((pdf_output_processor*)proc)->extgstate) fz_write_printf(ctx, out, "%g i\n", flatness); }
static void pdf_out_w(fz_context *ctx, pdf_processor *proc, float linewidth) { fz_output *out = ((pdf_output_processor*)proc)->out; if (!((pdf_output_processor*)proc)->extgstate) fz_write_printf(ctx, out, "%g w\n", linewidth); }
static void pdf_out_d(fz_context *ctx, pdf_processor *proc, pdf_obj *array, float phase) { fz_output *out = ((pdf_output_processor*)proc)->out; if (!((pdf_output_processor*)proc)->extgstate) { pdf_print_obj(ctx, out, array, 1); fz_write_printf(ctx, out, " %g d\n", phase); } }
static void fz_print_stext_image_as_xhtml(fz_context *ctx, fz_output *out, fz_stext_block *block) { int w = block->bbox.x1 - block->bbox.x0; int h = block->bbox.y1 - block->bbox.y0; fz_write_printf(ctx, out, "<p><img width=\"%d\" height=\"%d\" src=\"data:", w, h); fz_write_image_as_data_uri(ctx, out, block->u.i.image); fz_write_string(ctx, out, "\"/></p>\n"); }
static void fz_print_stext_image_as_html(fz_context *ctx, fz_output *out, fz_stext_block *block) { int x = block->bbox.x0; int y = block->bbox.y0; int w = block->bbox.x1 - block->bbox.x0; int h = block->bbox.y1 - block->bbox.y0; fz_write_printf(ctx, out, "<img style=\"top:%dpt;left:%dpt;width:%dpt;height:%dpt\" src=\"data:", y, x, w, h); fz_write_image_as_data_uri(ctx, out, block->u.i.image); fz_write_string(ctx, out, "\">\n"); }
static void fz_edgebuffer_print(fz_context *ctx, fz_output *out, fz_edgebuffer * edgebuffer) { int i; int height = edgebuffer->super.clip.y1 - edgebuffer->super.clip.y0; fz_write_printf(ctx, out, "Edgebuffer %x\n", edgebuffer); fz_write_printf(ctx, out, "xmin=%x xmax=%x base=%x height=%x\n", edgebuffer->super.clip.x0, edgebuffer->super.clip.x1, edgebuffer->super.clip.y0, height); for (i=0; i < height; i++) { int offset = edgebuffer->index[i]; int *row = &edgebuffer->table[offset]; int count = *row++; assert ((count & 1) == 0); fz_write_printf(ctx, out, "%x @ %x: %d =", i, offset, count); while (count-- > 0) { int v = *row++; fz_write_printf(ctx, out, " %x:%d", v&~1, v&1); } fz_write_printf(ctx, out, "\n"); } }
/* * Write pixmap to PNM file (without alpha channel) */ static void pnm_write_header(fz_context *ctx, fz_band_writer *writer, const fz_colorspace *cs) { fz_output *out = writer->out; int w = writer->w; int h = writer->h; int n = writer->n; int alpha = writer->alpha; if (writer->s != 0) fz_throw(ctx, FZ_ERROR_GENERIC, "PNM writer cannot cope with spot colors"); n -= alpha; if (n != 1 && n != 3) fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or rgb to write as pnm"); if (n == 1) fz_write_printf(ctx, out, "P5\n"); if (n == 3) fz_write_printf(ctx, out, "P6\n"); fz_write_printf(ctx, out, "%d %d\n", w, h); fz_write_printf(ctx, out, "255\n"); }
static int pdfpages_pages(fz_context *ctx, fz_output *out, char *filename, char *password, char *argv[], int argc) { enum { NO_FILE_OPENED, NO_INFO_GATHERED, INFO_SHOWN } state; int argidx = 0; pdf_document *doc = NULL; int ret = 0; state = NO_FILE_OPENED; while (argidx < argc) { if (state == NO_FILE_OPENED || !fz_is_page_range(ctx, argv[argidx])) { if (state == NO_INFO_GATHERED) { showpages(ctx, doc, out, "1-N"); } pdf_drop_document(ctx, doc); filename = argv[argidx]; fz_write_printf(ctx, out, "%s:\n", filename); doc = pdf_open_document(ctx, filename); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", filename); state = NO_INFO_GATHERED; } else { ret |= showpages(ctx, doc, out, argv[argidx]); state = INFO_SHOWN; } argidx++; } if (state == NO_INFO_GATHERED) showpages(ctx, doc, out, "1-N"); pdf_drop_document(ctx, doc); return ret; }
static void fz_print_style_begin_html(fz_context *ctx, fz_output *out, fz_font *font, float size, int sup) { char family[80]; int is_bold = fz_font_is_bold(ctx, font); int is_italic = fz_font_is_italic(ctx, font); int is_serif = fz_font_is_serif(ctx, font); int is_mono = fz_font_is_monospaced(ctx, font); font_family_name(ctx, font, family, sizeof family, is_mono, is_serif); if (sup) fz_write_string(ctx, out, "<sup>"); if (is_mono) fz_write_string(ctx, out, "<tt>"); if (is_bold) fz_write_string(ctx, out, "<b>"); if (is_italic) fz_write_string(ctx, out, "<i>"); fz_write_printf(ctx, out, "<span style=\"font-family:%s;font-size:%gpt;\">", family, size); }
void fz_print_stext_page_as_html(fz_context *ctx, fz_output *out, fz_stext_page *page) { fz_stext_block *block; int w = page->mediabox.x1 - page->mediabox.x0; int h = page->mediabox.y1 - page->mediabox.y0; fz_write_printf(ctx, out, "<div style=\"width:%dpt;height:%dpt\">\n", w, h); for (block = page->first_block; block; block = block->next) { if (block->type == FZ_STEXT_BLOCK_IMAGE) fz_print_stext_image_as_html(ctx, out, block); else if (block->type == FZ_STEXT_BLOCK_TEXT) fz_print_stext_block_as_html(ctx, out, block); } fz_write_string(ctx, out, "</div>\n"); }
static int shownum(fz_context *ctx, fz_output *out, pdf_obj *page, char *text, pdf_obj *name) { pdf_obj *obj; int failed = 0; fz_try(ctx) { obj = pdf_dict_get(ctx, page, name); if (!pdf_is_number(ctx, obj)) break; fz_write_printf(ctx, out, "<%s v=\"%g\" />\n", text, pdf_to_real(ctx, obj)); } fz_catch(ctx) { failed = 1; } return failed; }
static int showbox(fz_context *ctx, fz_output *out, pdf_obj *page, char *text, pdf_obj *name) { fz_rect bbox; pdf_obj *obj; int failed = 0; fz_try(ctx) { obj = pdf_dict_get(ctx, page, name); if (!pdf_is_array(ctx, obj)) break; pdf_to_rect(ctx, obj, &bbox); fz_write_printf(ctx, out, "<%s l=\"%g\" b=\"%g\" r=\"%g\" t=\"%g\" />\n", text, bbox.x0, bbox.y0, bbox.x1, bbox.y1); } fz_catch(ctx) { failed = 1; } return failed; }
static void pdf_out_TL(fz_context *ctx, pdf_processor *proc, float leading) { fz_output *out = ((pdf_output_processor*)proc)->out; fz_write_printf(ctx, out, "%g TL\n", leading); }
static void pdf_out_Tr(fz_context *ctx, pdf_processor *proc, int render) { fz_output *out = ((pdf_output_processor*)proc)->out; fz_write_printf(ctx, out, "%d Tr\n", render); }
static void pdf_out_Tw(fz_context *ctx, pdf_processor *proc, float wordspace) { fz_output *out = ((pdf_output_processor*)proc)->out; fz_write_printf(ctx, out, "%g Tw\n", wordspace); }
static void pdf_out_Tz(fz_context *ctx, pdf_processor *proc, float scale) { fz_output *out = ((pdf_output_processor*)proc)->out; fz_write_printf(ctx, out, "%g Tz\n", scale); }
static void pdf_out_Ts(fz_context *ctx, pdf_processor *proc, float rise) { fz_output *out = ((pdf_output_processor*)proc)->out; fz_write_printf(ctx, out, "%g Ts\n", rise); }
void fz_print_stext_page_as_xml(fz_context *ctx, fz_output *out, fz_stext_page *page) { fz_stext_block *block; fz_stext_line *line; fz_stext_char *ch; fz_write_printf(ctx, out, "<page width=\"%g\" height=\"%g\">\n", page->mediabox.x1 - page->mediabox.x0, page->mediabox.y1 - page->mediabox.y0); for (block = page->first_block; block; block = block->next) { switch (block->type) { case FZ_STEXT_BLOCK_TEXT: fz_write_printf(ctx, out, "<block bbox=\"%g %g %g %g\">\n", block->bbox.x0, block->bbox.y0, block->bbox.x1, block->bbox.y1); for (line = block->u.t.first_line; line; line = line->next) { fz_font *font = NULL; float size = 0; const char *name = NULL; fz_rect rect; fz_write_printf(ctx, out, "<line bbox=\"%g %g %g %g\" wmode=\"%d\" dir=\"%g %g\">\n", line->bbox.x0, line->bbox.y0, line->bbox.x1, line->bbox.y1, line->wmode, line->dir.x, line->dir.y); for (ch = line->first_char; ch; ch = ch->next) { if (ch->font != font || ch->size != size) { if (font) fz_write_string(ctx, out, "</font>\n"); font = ch->font; size = ch->size; name = font_full_name(ctx, font); fz_write_printf(ctx, out, "<font name=\"%s\" size=\"%g\">\n", name, size); } fz_stext_char_bbox(ctx, &rect, line, ch); fz_write_printf(ctx, out, "<char bbox=\"%g %g %g %g\" x=\"%g\" y=\"%g\" c=\"", rect.x0, rect.y0, rect.x1, rect.y1, ch->origin.x, ch->origin.y); switch (ch->c) { case '<': fz_write_string(ctx, out, "<"); break; case '>': fz_write_string(ctx, out, ">"); break; case '&': fz_write_string(ctx, out, "&"); break; case '"': fz_write_string(ctx, out, """); break; case '\'': fz_write_string(ctx, out, "'"); break; default: if (ch->c >= 32 && ch->c <= 127) fz_write_printf(ctx, out, "%c", ch->c); else fz_write_printf(ctx, out, "&#x%x;", ch->c); break; } fz_write_string(ctx, out, "\"/>\n"); } if (font) fz_write_string(ctx, out, "</font>\n"); fz_write_string(ctx, out, "</line>\n"); } fz_write_string(ctx, out, "</block>\n"); break; case FZ_STEXT_BLOCK_IMAGE: fz_write_printf(ctx, out, "<image bbox=\"%g %g %g %g\" />\n", block->bbox.x0, block->bbox.y0, block->bbox.x1, block->bbox.y1); break; } } fz_write_string(ctx, out, "</page>\n"); }
static void pdf_out_TD(fz_context *ctx, pdf_processor *proc, float tx, float ty) { fz_output *out = ((pdf_output_processor*)proc)->out; fz_write_printf(ctx, out, "%g %g TD\n", tx, ty); }
static void pdf_out_Tm(fz_context *ctx, pdf_processor *proc, float a, float b, float c, float d, float e, float f) { fz_output *out = ((pdf_output_processor*)proc)->out; fz_write_printf(ctx, out, "%g %g %g %g %g %g Tm\n", a, b, c, d, e, f); }
static void pam_write_header(fz_context *ctx, fz_band_writer *writer, const fz_colorspace *cs) { fz_output *out = writer->out; int w = writer->w; int h = writer->h; int n = writer->n; int alpha = writer->alpha; if (writer->s != 0) fz_throw(ctx, FZ_ERROR_GENERIC, "PAM writer cannot cope with spot colors"); fz_write_printf(ctx, out, "P7\n"); fz_write_printf(ctx, out, "WIDTH %d\n", w); fz_write_printf(ctx, out, "HEIGHT %d\n", h); fz_write_printf(ctx, out, "DEPTH %d\n", n); fz_write_printf(ctx, out, "MAXVAL 255\n"); n -= alpha; if (n == 0 && alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE\n"); else if (n == 1 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE\n"); else if (n == 1 && alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE_ALPHA\n"); else if (n == 3 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE RGB\n"); else if (n == 3 && alpha) fz_write_printf(ctx, out, "TUPLTYPE RGB_ALPHA\n"); else if (n == 4 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE CMYK\n"); else if (n == 5) fz_write_printf(ctx, out, "TUPLTYPE CMYK_ALPHA\n"); fz_write_printf(ctx, out, "ENDHDR\n"); }
static void pdf_out_Tc(fz_context *ctx, pdf_processor *proc, float charspace) { fz_output *out = ((pdf_output_processor*)proc)->out; fz_write_printf(ctx, out, "%g Tc\n", charspace); }