From d447754cc9a6220df1b2f8395101b5d79adac9fe Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 10 Aug 2017 15:08:02 +0200 Subject: Add PWG document writer. Option parsing is not implemented yet. --- include/mupdf/fitz/writer.h | 9 +++- source/fitz/draw-device.c | 4 +- source/fitz/output-cbz.c | 2 - source/fitz/output-pwg.c | 100 ++++++++++++++++++++++++++++++++++++++++++++ source/fitz/stext-device.c | 6 +-- source/fitz/writer.c | 3 ++ source/tools/muconvert.c | 8 ++-- 7 files changed, 120 insertions(+), 12 deletions(-) diff --git a/include/mupdf/fitz/writer.h b/include/mupdf/fitz/writer.h index b5121707..3f534783 100644 --- a/include/mupdf/fitz/writer.h +++ b/include/mupdf/fitz/writer.h @@ -91,10 +91,14 @@ int fz_option_eq(const char *a, const char *b); */ fz_document_writer *fz_new_document_writer(fz_context *ctx, const char *path, const char *format, const char *options); -fz_document_writer *fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options); fz_document_writer *fz_new_pdf_writer(fz_context *ctx, const char *path, const char *options); fz_document_writer *fz_new_svg_writer(fz_context *ctx, const char *path, const char *options); + fz_document_writer *fz_new_text_writer(fz_context *ctx, const char *format, const char *path, const char *options); + +fz_document_writer *fz_new_pwg_writer(fz_context *ctx, const char *path, const char *options); + +fz_document_writer *fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options); fz_document_writer *fz_new_png_pixmap_writer(fz_context *ctx, const char *path, const char *options); fz_document_writer *fz_new_tga_pixmap_writer(fz_context *ctx, const char *path, const char *options); fz_document_writer *fz_new_pam_pixmap_writer(fz_context *ctx, const char *path, const char *options); @@ -142,8 +146,9 @@ void fz_drop_document_writer(fz_context *ctx, fz_document_writer *wri); fz_document_writer *fz_new_pixmap_writer(fz_context *ctx, const char *path, const char *options, const char *default_path, int n, void (*save)(fz_context *ctx, fz_pixmap *pix, const char *filename)); -extern const char *fz_cbz_write_options_usage; extern const char *fz_pdf_write_options_usage; extern const char *fz_svg_write_options_usage; +extern const char *fz_pwg_write_options_usage; + #endif diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c index ac6752bd..ce825eca 100644 --- a/source/fitz/draw-device.c +++ b/source/fitz/draw-device.c @@ -2507,7 +2507,7 @@ fz_bound_path_accurate(fz_context *ctx, fz_irect *bbox, const fz_irect *scissor, } const char *fz_draw_options_usage = - "Common raster format output options:\n" + "Raster output options:\n" "\trotate=N: rotate rendered pages N degrees counterclockwise\n" "\tresolution=N: set both X and Y resolution in pixels per inch\n" "\tx-resolution=N: X resolution of rendered pages in pixels per inch\n" @@ -2565,7 +2565,7 @@ fz_parse_draw_options(fz_context *ctx, fz_draw_options *opts, const char *args) opts->height = fz_atoi(val); if (fz_has_option(ctx, args, "colorspace", &val)) { - if (fz_option_eq(val, "gray") || fz_option_eq(val, "grey")) + if (fz_option_eq(val, "gray") || fz_option_eq(val, "grey") || fz_option_eq(val, "mono")) opts->colorspace = fz_device_gray(ctx); else if (fz_option_eq(val, "rgb")) opts->colorspace = fz_device_rgb(ctx); diff --git a/source/fitz/output-cbz.c b/source/fitz/output-cbz.c index 55c28e77..68a71fed 100644 --- a/source/fitz/output-cbz.c +++ b/source/fitz/output-cbz.c @@ -17,8 +17,6 @@ struct fz_cbz_writer_s fz_zip_writer *zip; }; -const char *fz_cbz_write_options_usage = ""; - static fz_device * cbz_begin_page(fz_context *ctx, fz_document_writer *wri_, const fz_rect *mediabox) { diff --git a/source/fitz/output-pwg.c b/source/fitz/output-pwg.c index 2cd79afe..7e2a4178 100644 --- a/source/fitz/output-pwg.c +++ b/source/fitz/output-pwg.c @@ -379,3 +379,103 @@ fz_band_writer *fz_new_pwg_band_writer(fz_context *ctx, fz_output *out, const fz return &writer->super; } + +/* High-level document writer interface */ + +const char *fz_pwg_write_options_usage = + "PWG output options:\n" + "\tcolorspace=mono: render 1-bit black and white bitmap\n" + "\n"; + +fz_pwg_options * +fz_parse_pwg_options(fz_context *ctx, fz_pwg_options *opts, const char *args) +{ + memset(opts, 0, sizeof *opts); + + return opts; +} + +typedef struct fz_pwg_writer_s fz_pwg_writer; + +struct fz_pwg_writer_s +{ + fz_document_writer super; + fz_draw_options draw; + fz_pwg_options pwg; + int mono; + fz_pixmap *pixmap; + fz_output *out; +}; + +static fz_device * +pwg_begin_page(fz_context *ctx, fz_document_writer *wri_, const fz_rect *mediabox) +{ + fz_pwg_writer *wri = (fz_pwg_writer*)wri_; + return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap); +} + +static void +pwg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) +{ + fz_pwg_writer *wri = (fz_pwg_writer*)wri_; + + fz_close_device(ctx, dev); + fz_drop_device(ctx, dev); + + if (wri->mono) + { + fz_bitmap *bitmap = fz_new_bitmap_from_pixmap(ctx, wri->pixmap, NULL); + fz_try(ctx) + fz_write_bitmap_as_pwg_page(ctx, wri->out, bitmap, &wri->pwg); + fz_always(ctx) + fz_drop_bitmap(ctx, bitmap); + fz_catch(ctx) + fz_rethrow(ctx); + } + else + { + fz_write_pixmap_as_pwg_page(ctx, wri->out, wri->pixmap, &wri->pwg); + } + + fz_drop_pixmap(ctx, wri->pixmap); + wri->pixmap = NULL; +} + +static void +pwg_close_writer(fz_context *ctx, fz_document_writer *wri_) +{ +} + +static void +pwg_drop_writer(fz_context *ctx, fz_document_writer *wri_) +{ + fz_pwg_writer *wri = (fz_pwg_writer*)wri_; + fz_drop_pixmap(ctx, wri->pixmap); + fz_drop_output(ctx, wri->out); +} + +fz_document_writer * +fz_new_pwg_writer(fz_context *ctx, const char *path, const char *options) +{ + fz_pwg_writer *wri = fz_new_derived_document_writer(ctx, fz_pwg_writer, pwg_begin_page, pwg_end_page, pwg_close_writer, pwg_drop_writer); + const char *val; + + fz_try(ctx) + { + fz_parse_draw_options(ctx, &wri->draw, options); + fz_parse_pwg_options(ctx, &wri->pwg, options); + if (fz_has_option(ctx, options, "colorspace", &val)) + if (fz_option_eq(val, "mono")) + wri->mono = 1; + wri->out = fz_new_output_with_path(ctx, path ? path : "out.pwg", 0); + fz_write_pwg_file_header(ctx, wri->out); + } + fz_catch(ctx) + { + fz_drop_output(ctx, wri->out); + fz_free(ctx, wri); + fz_rethrow(ctx); + } + + return (fz_document_writer*)wri; +} diff --git a/source/fitz/stext-device.c b/source/fitz/stext-device.c index 87009f03..d4d502b1 100644 --- a/source/fitz/stext-device.c +++ b/source/fitz/stext-device.c @@ -28,9 +28,9 @@ struct fz_stext_device_s }; const char *fz_stext_options_usage = - "Structured text output options:\n" - "\tpreserve-ligatures: do not expand all ligatures into constituent characters\n" - "\tpreserve-whitespace: do not convert all whitespace characters into spaces\n" + "Text output options:\n" + "\tpreserve-ligatures: do not expand ligatures into constituent characters\n" + "\tpreserve-whitespace: do not convert all whitespace into space characters\n" "\tpreserve-images: keep images in output\n" "\n"; diff --git a/source/fitz/writer.c b/source/fitz/writer.c index 4e436e08..f0e00256 100644 --- a/source/fitz/writer.c +++ b/source/fitz/writer.c @@ -140,6 +140,9 @@ fz_new_document_writer(fz_context *ctx, const char *path, const char *format, co if (!fz_strcasecmp(format, "pkm")) return fz_new_pkm_pixmap_writer(ctx, path, options); + if (!fz_strcasecmp(format, "pwg")) + return fz_new_pwg_writer(ctx, path, options); + if (!fz_strcasecmp(format, "txt") || !fz_strcasecmp(format, "text")) return fz_new_text_writer(ctx, "text", path, options); if (!fz_strcasecmp(format, "html")) diff --git a/source/tools/muconvert.c b/source/tools/muconvert.c index 99d5c774..f306664d 100644 --- a/source/tools/muconvert.c +++ b/source/tools/muconvert.c @@ -42,16 +42,18 @@ static void usage(void) "\n" "\t-o -\toutput file name (%%d for page number)\n" "\t-F -\toutput format (default inferred from output file name)\n" - "\t\t\tpng, pnm, pgm, ppm, pam, tga, pbm, pkm,\n" - "\t\t\tpdf, svg, cbz\n" + "\t\t\traster: cbz, png, pnm, pgm, ppm, pam, tga, pbm, pkm.\n" + "\t\t\tprint-raster: ps, pcl, pwg.\n" + "\t\t\tvector: pdf, svg.\n" + "\t\t\ttext: html, xhtml, text, stext.\n" "\t-O -\tcomma separated list of options for output format\n" "\n" "\tpages\tcomma separated list of page ranges (N=last page)\n" "\n" ); fputs(fz_draw_options_usage, stderr); + fputs(fz_pwg_write_options_usage, stderr); fputs(fz_stext_options_usage, stderr); - fputs(fz_cbz_write_options_usage, stderr); #if FZ_ENABLE_PDF fputs(fz_pdf_write_options_usage, stderr); #endif -- cgit v1.2.3