diff --git a/.travis.yml b/.travis.yml index 57fd6ab06..80116883e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,9 +8,8 @@ cache: - "$HOME/Library/Caches/Homebrew" env: global: - - MUPDF='https://mupdf.com/downloads/mupdf-1.13.0-source.tar.gz' + - MUPDF='https://mupdf.com/downloads/archive/mupdf-1.14.0-source.tar.gz' - COPYFILE_DISABLE=1 - - TWINE_USERNAME=rk700 matrix: include: - os: linux @@ -42,19 +41,17 @@ install: - mkdir mupdf && wget -q $MUPDF -O - | tar zx -C mupdf --strip-components=1 - cp fitz/_mupdf_config.h mupdf/include/mupdf/fitz/config.h script: -- export CIBW_ENVIRONMENT='CFLAGS="-fPIC -std=gnu99"' - export CIBW_SKIP="cp33-* *i686" - export CIBW_TEST_COMMAND="python -c 'import fitz'" - cibuildwheel --output-dir wheelhouse deploy: provider: pages - github_token: - secure: aZCfaInTGLFs2Ik60w61g/wK7IR4sxK035H+YiKvIrwyufUVuafFlRlG9FLsxrhgYlh7KalNJciD9kdYCgQ3qt21lud9CviBgwNdFGF8LoUhzflr+MzOP51ESnHMgN3t51lN7Ui2RrFlsbST34O28tq8vyPitDS3AMXeuUSZzx8GpsFr/5wRvikd0q1QxF+KkeTp0iVRu4woV0LUNnDylEHRmHdvObuIXkQJunvsXW/Zpxzk8rREdW1/4mBqWV2H8ywk5nptgHfwcjmrpoPHrJOkgXYkXkONzXdC0OuL5SSHLFF4NPNNVdf6Rz4RoGsozI5rDvJ1lEEijz+1sEIQAvghprv9LgiSKs4ECnGtk6J65NCf1KT+3Sj7zcl9C7MmOdJFgnIeWGj5a34i+lGIph30ji3MiBwTnApMyaWHxF6+qshkmN5E+m4Y8XHVAS8gR3EEvLwPR15wmfRATHE2uc1y7p6yNeR95wS5xFqsrXjyZY0/9Ht29JQh/R1NZdLVNJD0/1hcaEhwMSTa1YGmDrUh+wGNjRlPgzS/y3nKG9vwFyPcO8sWaFVMBz1VyqT2ZYQq8Qg2Wtg3r0TvqntwN2ois9KRsr+j8yKGEXkTwZoi/6uL7sjl/NHUL5ps37s1h8GRMdhtQ4dz1s2x+E8kj5cZFGopFybAp44jmdz80wo= + github_token: $GITHUB_TOKEN keep-history: false skip_cleanup: true local-dir: wheelhouse repo: JorjMcKie/PyMuPDF-wheels target-branch: $TRAVIS_OS_NAME on: - repo: rk700/PyMuPDF + repo: JorjMcKie/py-mupdf diff --git a/README.md b/README.md index 47bfd0ceb..05bc65d48 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# PyMuPDF 1.13.20 +# PyMuPDF 1.14.0 ![logo](https://github.com/rk700/PyMuPDF/blob/master/demo/pymupdf.jpg) -Release date: September 10, 2018 +Release date: November 15, 2018 -**Travis-CI:** [![Build Status](https://travis-ci.org/rk700/PyMuPDF.svg?branch=master)](https://travis-ci.org/rk700/PyMuPDF) +**Travis-CI:** [![Build Status](https://travis-ci.org/JorjMcKie/py-mupdf.svg?branch=master)](https://travis-ci.org/JorjMcKie/py-mupdf) On **[PyPI](https://pypi.org/project/PyMuPDF)** since August 2016: [![](https://pepy.tech/badge/pymupdf)](https://pepy.tech/project/pymupdf) @@ -14,7 +14,7 @@ On **[PyPI](https://pypi.org/project/PyMuPDF)** since August 2016: [![](https:// # Introduction -This is **version 1.13.20 of PyMuPDF (formerly python-fitz)**, a Python binding with support for [MuPDF 1.13.0](http://mupdf.com/) - "a lightweight PDF, XPS, and E-book viewer". +This is **version 1.14.0 of PyMuPDF (formerly python-fitz)**, a Python binding with support for [MuPDF 1.14.0](http://mupdf.com/) - "a lightweight PDF, XPS, and E-book viewer". MuPDF can access files in PDF, XPS, OpenXPS, CBZ, EPUB and FB2 (e-books) formats, and it is known for its top performance and high rendering quality. @@ -32,14 +32,14 @@ The platform tag for Linux is `manylinux1_x86_64`, which makes these wheels usab On other operating systems you need to generate PyMuPDF yourself. Before you can do this, you must download and generate MuPDF. This process depends very much on your system. For most platforms, the MuPDF source contains prepared procedures for achieving this. -Be sure to download the official MuPDF source release from [here](https://mupdf.com/downloads). Do **not use** MuPDF's [GitHub repo](https://github.com/ArtifexSoftware/mupdf). It contains their current **development source**, which is **not compatible** with the published PyMuPDF version most of the time. +Be sure to download the official MuPDF source release from [here](https://mupdf.com/downloads). Do **not use** MuPDF's [GitHub repo](https://github.com/ArtifexSoftware/mupdf). It contains their current **development source**, which is **not compatible** with this PyMuPDF version most of the time. -Once this is done, adjust directories in ``setup.py`` and the rest should be as easy as running the usual ``python setup.py install``. +Once this is done, adjust directories in ``setup.py`` and run ``python setup.py install``. -The following sections contain some platform-specific comments. +The following sections contain further comments for some platforms. ## Ubuntu -One of our users (thanks to **@gileadslostson**!) has documented his MuPDF installation experience from sources in this [Wiki page](https://github.com/rk700/PyMuPDF/wiki/Experience-from-an-Ubuntu-installation). +Our users (thanks to **@gileadslostson** and **@jbarlow83**!) have documented their MuPDF installation experiences from sources in this [Wiki page](https://github.com/rk700/PyMuPDF/wiki/Experience-from-an-Ubuntu-installation). ## OSX First, install the MuPDF headers and libraries, which are provided by mupdf-tools: ``brew install mupdf-tools``. diff --git a/doc/PyMuPDF.pdf b/doc/PyMuPDF.pdf index cfdc08cda..e1ab0f8fb 100644 Binary files a/doc/PyMuPDF.pdf and b/doc/PyMuPDF.pdf differ diff --git a/doc/html.zip b/doc/html.zip index f77d5c7fb..5f6b00d8a 100644 Binary files a/doc/html.zip and b/doc/html.zip differ diff --git a/examples/posterize.py b/examples/posterize.py index 9d395ebe4..ddba399c2 100644 --- a/examples/posterize.py +++ b/examples/posterize.py @@ -10,7 +10,8 @@ Result ------- -A file "poster-input.pdf" with 4 output pages for every input page. +The file "poster-input.pdf" with 4 output pages for every input page: +top-lef, top-right, bottom-left, bottom-right part on separate pages. Notes ----- diff --git a/fitz/__init__.py b/fitz/__init__.py index 464630edb..21680e9d7 100644 --- a/fitz/__init__.py +++ b/fitz/__init__.py @@ -1,30 +1,6 @@ from __future__ import absolute_import from fitz.fitz import * - -class M_Identity(fitz.Matrix): - """Identity matrix [1, 0, 0, 1, 0, 0]""" - def __init__(self): - fitz.Matrix.__init__(self, 1.0, 1.0) - def __setattr__(self, name, value): - if name in "abcdef": - raise NotImplementedError("Identity is readonly") - else: - super(fitz.Matrix, self).__setattr__(name, value) - - def checkargs(*args): - raise NotImplementedError("Identity is readonly") - preRotate = checkargs - preShear = checkargs - preScale = checkargs - preTranslate = checkargs - concat = checkargs - invert = checkargs - def __repr__(self): - return "fitz.Identity" - -Identity = M_Identity() - -fitz.Identity = Identity +from fitz import _fitz # define the supported colorspaces for convenience fitz.csRGB = fitz.Colorspace(fitz.CS_RGB) @@ -34,6 +10,9 @@ def __repr__(self): csGRAY = fitz.csGRAY csCMYK = fitz.csCMYK +TOOLS = fitz.Tools() +fitz.TOOLS = TOOLS + import fitz.utils # copy functions to their respective fitz classes @@ -83,89 +62,17 @@ def __repr__(self): #------------------------------------------------------------------------------ fitz.Pixmap.writeImage = fitz.utils.writeImage -#------------------------------------------------------------------------------ -# Annot -#------------------------------------------------------------------------------ -fitz.Annot.updateImage = fitz.utils.updateImage - -#------------------------------------------------------------------------------ -# Matrix algebra -#------------------------------------------------------------------------------ -fitz.Matrix.__abs__ = fitz.utils.mat_abs -fitz.Matrix.__add__ = fitz.utils.mat_add -fitz.Matrix.__bool__ = fitz.utils.mat_true -fitz.Matrix.__contains__ = fitz.utils.mat_contains -fitz.Matrix.__div__ = fitz.utils.mat_div -fitz.Matrix.__eq__ = fitz.utils.mat_equ -fitz.Matrix.__invert__ = fitz.utils.mat_invert -fitz.Matrix.__mul__ = fitz.utils.mat_mult -fitz.Matrix.__neg__ = lambda x: fitz.Matrix(x)*-1 # unary "-" -fitz.Matrix.__nonzero__ = fitz.utils.mat_true -fitz.Matrix.__pos__ = lambda x: fitz.Matrix(x) # unary "+" -fitz.Matrix.__sub__ = fitz.utils.mat_sub -fitz.Matrix.__truediv__ = fitz.utils.mat_div - - #------------------------------------------------------------------------------ # Rect #------------------------------------------------------------------------------ fitz.Rect.getRectArea = fitz.utils.getRectArea fitz.Rect.getArea = fitz.utils.getRectArea -fitz.Rect.intersects = fitz.utils.intersects -# Rect algebra -fitz.Rect.__neg__ = lambda x: fitz.Rect(x)*-1 # unary "-" -fitz.Rect.__pos__ = lambda x: fitz.Rect(x) # unary "+" -fitz.Rect.__abs__ = lambda x: fitz.Rect.getArea(x) # abs -fitz.Rect.__or__ = fitz.utils.rect_or -fitz.Rect.__and__ = fitz.utils.rect_and -fitz.Rect.__add__ = fitz.utils.rect_add -fitz.Rect.__sub__ = fitz.utils.rect_sub -fitz.Rect.__mul__ = fitz.utils.rect_mul -fitz.Rect.__truediv__ = fitz.utils.rect_div -fitz.Rect.__div__ = fitz.utils.rect_div -fitz.Rect.__eq__ = fitz.utils.rect_equ -fitz.Rect.__nonzero__ = fitz.utils.rect_true -fitz.Rect.__bool__ = fitz.utils.rect_true -fitz.Rect.__contains__ = fitz.utils.rect_contains #------------------------------------------------------------------------------ # IRect #------------------------------------------------------------------------------ fitz.IRect.getRectArea = fitz.utils.getRectArea fitz.IRect.getArea = fitz.utils.getRectArea -fitz.IRect.intersects = fitz.utils.intersects -# IRect algebra -fitz.IRect.__neg__ = lambda x: fitz.IRect(x)*-1 # unary "-" -fitz.IRect.__pos__ = lambda x: fitz.IRect(x) # unary "+" -fitz.IRect.__abs__ = lambda x: fitz.IRect.getArea(x) # abs -fitz.IRect.__or__ = fitz.utils.rect_or -fitz.IRect.__and__ = fitz.utils.rect_and -fitz.IRect.__add__ = fitz.utils.rect_add -fitz.IRect.__sub__ = fitz.utils.rect_sub -fitz.IRect.__mul__ = fitz.utils.rect_mul -fitz.IRect.__truediv__ = fitz.utils.rect_div -fitz.IRect.__div__ = fitz.utils.rect_div -fitz.IRect.__eq__ = fitz.utils.rect_equ -fitz.IRect.__nonzero__ = fitz.utils.rect_true -fitz.IRect.__bool__ = fitz.utils.rect_true -fitz.IRect.__contains__ = fitz.utils.rect_contains - -#------------------------------------------------------------------------------ -# Point algebra -#------------------------------------------------------------------------------ -fitz.Point.__neg__ = lambda x: fitz.Point(x)*-1 # unary "-" -fitz.Point.__pos__ = lambda x: fitz.Point(x) # unary "+" -fitz.Point.__add__ = fitz.utils.point_add -fitz.Point.__sub__ = fitz.utils.point_sub -fitz.Point.__abs__ = fitz.utils.point_abs -fitz.Point.__mul__ = fitz.utils.point_mul -fitz.Point.__truediv__ = fitz.utils.point_div -fitz.Point.__div__ = fitz.utils.point_div -fitz.Point.__nonzero__ = fitz.utils.point_true -fitz.Point.__bool__ = fitz.utils.point_true -fitz.Point.__eq__ = fitz.utils.point_equ -fitz.Point.__contains__ = fitz.utils.point_contains - fitz.__doc__ = "PyMuPDF %s: Python bindings for the MuPDF %s library,\nbuilt on %s" \ % (fitz.VersionBind, fitz.VersionFitz, fitz.VersionDate) diff --git a/fitz/_error.c b/fitz/_error.c new file mode 100644 index 000000000..3b136aa74 --- /dev/null +++ b/fitz/_error.c @@ -0,0 +1,226 @@ +#include "mupdf/fitz.h" + +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +#ifndef NDEBUG +#define USE_OUTPUT_DEBUG_STRING +#include +#endif +#endif + +#ifdef __ANDROID__ +#define USE_ANDROID_LOG +#include +#endif + +/* Warning context */ + +void fz_var_imp(void *var) +{ + /* Do nothing */ +} + +void fz_flush_warnings(fz_context *ctx) +{ + if (ctx->warn->count > 1) + { + fz_write_printf(ctx, fz_stderr(ctx), "warning: ... repeated %d times ...\n", ctx->warn->count); + } + ctx->warn->message[0] = 0; + ctx->warn->count = 0; +} + +void fz_vwarn(fz_context *ctx, const char *fmt, va_list ap) +{ + char buf[sizeof ctx->warn->message]; + + fz_vsnprintf(buf, sizeof buf, fmt, ap); + buf[sizeof(buf) - 1] = 0; +#ifdef USE_OUTPUT_DEBUG_STRING + OutputDebugStringA(buf); + OutputDebugStringA("\n"); +#endif +#ifdef USE_ANDROID_LOG + __android_log_print(ANDROID_LOG_WARN, "libmupdf", "%s", buf); +#endif + + if (!strcmp(buf, ctx->warn->message)) + { + ctx->warn->count++; + } + else + { + fz_flush_warnings(ctx); + fz_write_printf(ctx, fz_stderr(ctx), "warning: %s\n", buf); + fz_strlcpy(ctx->warn->message, buf, sizeof ctx->warn->message); + ctx->warn->count = 1; + } +} + +void fz_warn(fz_context *ctx, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fz_vwarn(ctx, fmt, ap); + va_end(ap); +} + +/* Error context */ + +/* When we first setjmp, code is set to 0. Whenever we throw, we add 2 to + * this code. Whenever we enter the always block, we add 1. + * + * fz_push_try sets code to 0. + * If (fz_throw called within fz_try) + * fz_throw makes code = 2. + * If (no always block present) + * enter catch region with code = 2. OK. + * else + * fz_always entered as code < 3; Makes code = 3; + * if (fz_throw called within fz_always) + * fz_throw makes code = 5 + * fz_always is not reentered. + * catch region entered with code = 5. OK. + * else + * catch region entered with code = 3. OK + * else + * if (no always block present) + * catch region not entered as code = 0. OK. + * else + * fz_always entered as code < 3. makes code = 1 + * if (fz_throw called within fz_always) + * fz_throw makes code = 3; + * fz_always NOT entered as code >= 3 + * catch region entered with code = 3. OK. + * else + * catch region entered with code = 1. + */ + +FZ_NORETURN static void throw(fz_context *ctx) +{ + if (ctx->error->top >= ctx->error->stack) + { + ctx->error->top->code += 2; + fz_longjmp(ctx->error->top->buffer, 1); + } + else + { + fz_write_printf(ctx, fz_stderr(ctx), "uncaught error: %s\n", ctx->error->message); +#ifdef USE_OUTPUT_DEBUG_STRING + OutputDebugStringA("uncaught error: "); + OutputDebugStringA(ctx->error->message); + OutputDebugStringA("\n"); +#endif +#ifdef USE_ANDROID_LOG + __android_log_print(ANDROID_LOG_ERROR, "libmupdf", "(uncaught) %s", ctx->error->message); +#endif + exit(EXIT_FAILURE); + } +} + +/* Only called when we hit the bottom of the exception stack. + * Do the same as fz_throw, but don't actually throw. */ +static int fz_fake_throw(fz_context *ctx, int code, const char *fmt, ...) +{ + va_list args; + ctx->error->errcode = code; + va_start(args, fmt); + fz_vsnprintf(ctx->error->message, sizeof ctx->error->message, fmt, args); + ctx->error->message[sizeof(ctx->error->message) - 1] = 0; + va_end(args); + + if (code != FZ_ERROR_ABORT) + { + fz_flush_warnings(ctx); + fz_write_printf(ctx, fz_stderr(ctx), "error: %s\n", ctx->error->message); +#ifdef USE_OUTPUT_DEBUG_STRING + OutputDebugStringA("error: "); + OutputDebugStringA(ctx->error->message); + OutputDebugStringA("\n"); +#endif +#ifdef USE_ANDROID_LOG + __android_log_print(ANDROID_LOG_ERROR, "libmupdf", "%s", ctx->error->message); +#endif + } + + /* We need to arrive in the always/catch block as if throw + * had taken place. */ + ctx->error->top++; + ctx->error->top->code = 2; + return 0; +} + +int fz_push_try(fz_context *ctx) +{ + /* If we would overflow the exception stack, throw an exception instead + * of entering the try block. We assume that we always have room for + * 1 extra level on the stack here - i.e. we throw the error on us + * starting to use the last level. */ + if (ctx->error->top + 2 >= ctx->error->stack + nelem(ctx->error->stack)) + return fz_fake_throw(ctx, FZ_ERROR_GENERIC, "exception stack overflow!"); + + ctx->error->top++; + ctx->error->top->code = 0; + return 1; +} + +int fz_caught(fz_context *ctx) +{ + assert(ctx && ctx->error && ctx->error->errcode >= FZ_ERROR_NONE); + return ctx->error->errcode; +} + +const char *fz_caught_message(fz_context *ctx) +{ + assert(ctx && ctx->error && ctx->error->errcode >= FZ_ERROR_NONE); + return ctx->error->message; +} + +FZ_NORETURN void fz_vthrow(fz_context *ctx, int code, const char *fmt, va_list ap) +{ + ctx->error->errcode = code; + fz_vsnprintf(ctx->error->message, sizeof ctx->error->message, fmt, ap); + ctx->error->message[sizeof(ctx->error->message) - 1] = 0; + + if (code != FZ_ERROR_ABORT) + { + fz_flush_warnings(ctx); + fz_write_printf(ctx, fz_stderr(ctx), "error: %s\n", ctx->error->message); +#ifdef USE_OUTPUT_DEBUG_STRING + OutputDebugStringA("error: "); + OutputDebugStringA(ctx->error->message); + OutputDebugStringA("\n"); +#endif +#ifdef USE_ANDROID_LOG + __android_log_print(ANDROID_LOG_ERROR, "libmupdf", "%s", ctx->error->message); +#endif + } + + throw(ctx); +} + +FZ_NORETURN void fz_throw(fz_context *ctx, int code, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fz_vthrow(ctx, code, fmt, ap); + va_end(ap); +} + +FZ_NORETURN void fz_rethrow(fz_context *ctx) +{ + assert(ctx && ctx->error && ctx->error->errcode >= FZ_ERROR_NONE); + throw(ctx); +} + +void fz_rethrow_if(fz_context *ctx, int err) +{ + assert(ctx && ctx->error && ctx->error->errcode >= FZ_ERROR_NONE); + if (ctx->error->errcode == err) + fz_rethrow(ctx); +} diff --git a/fitz/_mupdf_config.h b/fitz/_mupdf_config.h index 93ada94e6..80140fca7 100644 --- a/fitz/_mupdf_config.h +++ b/fitz/_mupdf_config.h @@ -6,7 +6,7 @@ Enable the following for spot (and hence overprint/overprint simulation) capable rendering. This forces FZ_PLOTTERS_N on. */ -#define FZ_ENABLE_SPOT_RENDERING +#define FZ_ENABLE_SPOT_RENDERING 1 /* Choose which plotters we need. @@ -29,7 +29,6 @@ /* #define FZ_ENABLE_SVG 1 */ /* #define FZ_ENABLE_CBZ 1 */ /* #define FZ_ENABLE_IMG 1 */ -/* #define FZ_ENABLE_TIFF 1 */ /* #define FZ_ENABLE_HTML 1 */ /* #define FZ_ENABLE_EPUB 1 */ /* #define FZ_ENABLE_GPRF 1 */ @@ -60,7 +59,7 @@ #define TOFU // <=== PyMuPDF /* To skip the CJK font, enable: (this implicitly enables TOFU_CJK_EXT and TOFU_CJK_LANG) */ -// #define TOFU_CJK // <=== PyMuPDF +// #define TOFU_CJK /* To skip CJK Extension A, enable: (this implicitly enables TOFU_CJK_LANG) */ #define TOFU_CJK_EXT // <=== PyMuPDF @@ -136,10 +135,6 @@ #define FZ_ENABLE_IMG 1 #endif /* FZ_ENABLE_IMG */ -#ifndef FZ_ENABLE_TIFF -#define FZ_ENABLE_TIFF 1 -#endif /* FZ_ENABLE_TIFF */ - #ifndef FZ_ENABLE_HTML #define FZ_ENABLE_HTML 1 #endif /* FZ_ENABLE_HTML */ diff --git a/fitz/_pdf-device.c b/fitz/_pdf-device.c new file mode 100644 index 000000000..31a7a10f2 --- /dev/null +++ b/fitz/_pdf-device.c @@ -0,0 +1,1154 @@ +#include "mupdf/fitz.h" +#include "mupdf/pdf.h" + +#include +#include FT_FREETYPE_H +#include FT_ADVANCES_H + +#define ALLOWED_TEXT_POS_ERROR (0.001f) + +typedef struct pdf_device_s pdf_device; + +typedef struct gstate_s gstate; + +struct gstate_s +{ + /* The first few entries aren't really graphics state things, but + * they are recorded here as they are fundamentally intertwined with + * the push/pulling of the gstates. */ + fz_buffer *buf; + void (*on_pop)(fz_context*,pdf_device*,void *); + void *on_pop_arg; + + /* The graphics state proper */ + fz_matrix ctm; + fz_colorspace *colorspace[2]; + float color[2][4]; + float alpha[2]; + fz_stroke_state *stroke_state; + int font; + int text_rendering_mode; + int knockout; +}; + +/* The image digest information, object reference, as well as indirect reference + * ID are all stored in doc->resources->image, and so they are maintained + * through the life of the document not just this page level device. As we + * encounter images on a page, we will add to the hash table if they are not + * already present. When we have an image on a particular page, the resource + * dict will be updated with the proper indirect reference across the document. + * We do need to maintain some information as to what image resources we have + * already specified for this page which is the purpose of image_indices + */ + +typedef struct alpha_entry_s alpha_entry; + +struct alpha_entry_s +{ + float alpha; + int stroke; +}; + +typedef struct group_entry_s group_entry; + +struct group_entry_s +{ + int alpha; + int isolated; + int knockout; + fz_colorspace *colorspace; + pdf_obj *ref; +}; + +struct pdf_device_s +{ + fz_device super; + + pdf_document *doc; + pdf_obj *resources; + fz_buffer *buffer; + + int in_text; + + int num_forms; + int num_smasks; + + int num_gstates; + int max_gstates; + gstate *gstates; + + int num_imgs; + int max_imgs; + int *image_indices; + + int num_cid_fonts; + int max_cid_fonts; + fz_font **cid_fonts; + + int num_alphas; + int max_alphas; + alpha_entry *alphas; + + int num_groups; + int max_groups; + group_entry *groups; +}; + +#define CURRENT_GSTATE(pdev) (&(pdev)->gstates[(pdev)->num_gstates-1]) + +/* Helper functions */ +static void +pdf_dev_stroke_state(fz_context *ctx, pdf_device *pdev, const fz_stroke_state *stroke_state) +{ + gstate *gs = CURRENT_GSTATE(pdev); + + if (stroke_state == gs->stroke_state) + return; + if (gs->stroke_state && !memcmp(stroke_state, gs->stroke_state, sizeof(*stroke_state))) + return; + if (!gs->stroke_state || gs->stroke_state->linewidth != stroke_state->linewidth) + { + fz_append_printf(ctx, gs->buf, "%g w\n", stroke_state->linewidth); + } + if (!gs->stroke_state || gs->stroke_state->start_cap != stroke_state->start_cap) + { + int cap = stroke_state->start_cap; + /* FIXME: Triangle caps aren't supported in pdf */ + if (cap == FZ_LINECAP_TRIANGLE) + cap = FZ_LINECAP_BUTT; + fz_append_printf(ctx, gs->buf, "%d J\n", cap); + } + if (!gs->stroke_state || gs->stroke_state->linejoin != stroke_state->linejoin) + { + int join = stroke_state->linejoin; + if (join == FZ_LINEJOIN_MITER_XPS) + join = FZ_LINEJOIN_MITER; + fz_append_printf(ctx, gs->buf, "%d j\n", join); + } + if (!gs->stroke_state || gs->stroke_state->miterlimit != stroke_state->miterlimit) + { + fz_append_printf(ctx, gs->buf, "%g M\n", stroke_state->miterlimit); + } + if (gs->stroke_state == NULL && stroke_state->dash_len == 0) + {} + else if (!gs->stroke_state || gs->stroke_state->dash_phase != stroke_state->dash_phase || gs->stroke_state->dash_len != stroke_state->dash_len || + memcmp(gs->stroke_state->dash_list, stroke_state->dash_list, sizeof(float)*stroke_state->dash_len)) + { + int i; + if (stroke_state->dash_len == 0) + fz_append_byte(ctx, gs->buf, '['); + for (i = 0; i < stroke_state->dash_len; i++) + { + if (i > 0) + fz_append_byte(ctx, gs->buf, ' '); + fz_append_printf(ctx, gs->buf, "%g", stroke_state->dash_list[i]); + } + fz_append_printf(ctx, gs->buf, "]%g d\n", stroke_state->dash_phase); + } + fz_drop_stroke_state(ctx, gs->stroke_state); + gs->stroke_state = fz_keep_stroke_state(ctx, stroke_state); +} + +typedef struct +{ + fz_context *ctx; + fz_buffer *buf; +} pdf_dev_path_arg; + +static void +pdf_dev_path_moveto(fz_context *ctx, void *arg, float x, float y) +{ + fz_buffer *buf = (fz_buffer *)arg; + fz_append_printf(ctx, buf, "%g %g m\n", x, y); +} + +static void +pdf_dev_path_lineto(fz_context *ctx, void *arg, float x, float y) +{ + fz_buffer *buf = (fz_buffer *)arg; + fz_append_printf(ctx, buf, "%g %g l\n", x, y); +} + +static void +pdf_dev_path_curveto(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2, float x3, float y3) +{ + fz_buffer *buf = (fz_buffer *)arg; + fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", x1, y1, x2, y2, x3, y3); +} + +static void +pdf_dev_path_close(fz_context *ctx, void *arg) +{ + fz_buffer *buf = (fz_buffer *)arg; + fz_append_string(ctx, buf, "h\n"); +} + +static const fz_path_walker pdf_dev_path_proc = +{ + pdf_dev_path_moveto, + pdf_dev_path_lineto, + pdf_dev_path_curveto, + pdf_dev_path_close +}; + +static void +pdf_dev_path(fz_context *ctx, pdf_device *pdev, const fz_path *path) +{ + gstate *gs = CURRENT_GSTATE(pdev); + + fz_walk_path(ctx, path, &pdf_dev_path_proc, (void *)gs->buf); +} + +static void +pdf_dev_ctm(fz_context *ctx, pdf_device *pdev, fz_matrix ctm) +{ + fz_matrix inverse; + gstate *gs = CURRENT_GSTATE(pdev); + + if (memcmp(&gs->ctm, &ctm, sizeof(ctm)) == 0) + return; + inverse = fz_invert_matrix(gs->ctm); + inverse = fz_concat(ctm, inverse); + gs->ctm = ctm; + fz_append_printf(ctx, gs->buf, "%M cm\n", &inverse); +} + +static void +pdf_dev_color(fz_context *ctx, pdf_device *pdev, fz_colorspace *colorspace, const float *color, int stroke, const fz_color_params *color_params) +{ + int diff = 0; + int i; + int cspace = 0; + float rgb[FZ_MAX_COLORS]; + gstate *gs = CURRENT_GSTATE(pdev); + + if (colorspace == fz_device_gray(ctx)) + cspace = 1; + else if (colorspace == fz_device_rgb(ctx)) + cspace = 3; + else if (colorspace == fz_device_cmyk(ctx)) + cspace = 4; + + if (cspace == 0) + { + /* If it's an unknown colorspace, fallback to rgb */ + fz_convert_color(ctx, color_params, NULL, fz_device_rgb(ctx), rgb, colorspace, color); + color = rgb; + colorspace = fz_device_rgb(ctx); + cspace = 3; + } + + if (gs->colorspace[stroke] != colorspace) + { + gs->colorspace[stroke] = colorspace; + diff = 1; + } + + for (i=0; i < cspace; i++) + if (gs->color[stroke][i] != color[i]) + { + gs->color[stroke][i] = color[i]; + diff = 1; + } + + if (diff == 0) + return; + + switch (cspace + stroke*8) + { + case 1: + fz_append_printf(ctx, gs->buf, "%g g\n", color[0]); + break; + case 3: + fz_append_printf(ctx, gs->buf, "%g %g %g rg\n", color[0], color[1], color[2]); + break; + case 4: + fz_append_printf(ctx, gs->buf, "%g %g %g %g k\n", color[0], color[1], color[2], color[3]); + break; + case 1+8: + fz_append_printf(ctx, gs->buf, "%g G\n", color[0]); + break; + case 3+8: + fz_append_printf(ctx, gs->buf, "%g %g %g RG\n", color[0], color[1], color[2]); + break; + case 4+8: + fz_append_printf(ctx, gs->buf, "%g %g %g %g K\n", color[0], color[1], color[2], color[3]); + break; + } +} + +static void +pdf_dev_alpha(fz_context *ctx, pdf_device *pdev, float alpha, int stroke) +{ + int i; + pdf_document *doc = pdev->doc; + gstate *gs = CURRENT_GSTATE(pdev); + + /* If the alpha is unchanged, nothing to do */ + if (gs->alpha[stroke] == alpha) + return; + + /* Have we sent such an alpha before? */ + for (i = 0; i < pdev->num_alphas; i++) + if (pdev->alphas[i].alpha == alpha && pdev->alphas[i].stroke == stroke) + break; + + if (i == pdev->num_alphas) + { + pdf_obj *o, *ref; + + /* No. Need to make a new one */ + if (pdev->num_alphas == pdev->max_alphas) + { + int newmax = pdev->max_alphas * 2; + if (newmax == 0) + newmax = 4; + pdev->alphas = fz_resize_array(ctx, pdev->alphas, newmax, sizeof(*pdev->alphas)); + pdev->max_alphas = newmax; + } + pdev->alphas[i].alpha = alpha; + pdev->alphas[i].stroke = stroke; + + o = pdf_new_dict(ctx, doc, 1); + fz_try(ctx) + { + char text[32]; + pdf_dict_put_real(ctx, o, (stroke ? PDF_NAME(CA) : PDF_NAME(ca)), alpha); + fz_snprintf(text, sizeof(text), "ExtGState/Alp%d", i); + ref = pdf_add_object(ctx, doc, o); + pdf_dict_putp_drop(ctx, pdev->resources, text, ref); + } + fz_always(ctx) + { + pdf_drop_obj(ctx, o); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + pdev->num_alphas++; + } + fz_append_printf(ctx, gs->buf, "/Alp%d gs\n", i); +} + +static int +pdf_dev_add_font_res(fz_context *ctx, pdf_device *pdev, fz_font *font) +{ + pdf_obj *fres; + char text[32]; + int k; + int num; + + /* Check if we already had this one */ + for (k = 0; k < pdev->num_cid_fonts; k++) + if (pdev->cid_fonts[k] == font) + return k; + + /* This will add it to the xref if needed */ + fres = pdf_add_cid_font(ctx, pdev->doc, font); + + /* Not there so add to resources */ + fz_snprintf(text, sizeof(text), "Font/F%d", pdev->num_cid_fonts); + pdf_dict_putp_drop(ctx, pdev->resources, text, fres); + + /* And add index to our list for this page */ + if (pdev->num_cid_fonts == pdev->max_cid_fonts) + { + int newmax = pdev->max_cid_fonts * 2; + if (newmax == 0) + newmax = 4; + pdev->cid_fonts = fz_resize_array(ctx, pdev->cid_fonts, newmax, sizeof(*pdev->cid_fonts)); + pdev->max_cid_fonts = newmax; + } + num = pdev->num_cid_fonts++; + pdev->cid_fonts[num] = fz_keep_font(ctx, font); + return num; +} + +static void +pdf_dev_font(fz_context *ctx, pdf_device *pdev, fz_font *font) +{ + gstate *gs = CURRENT_GSTATE(pdev); + + /* If the font is unchanged, nothing to do */ + if (gs->font >= 0 && pdev->cid_fonts[gs->font] == font) + return; + + if (fz_font_t3_procs(ctx, font)) + fz_throw(ctx, FZ_ERROR_GENERIC, "pdf device does not support type 3 fonts"); + if (fz_font_flags(font)->ft_substitute) + fz_throw(ctx, FZ_ERROR_GENERIC, "pdf device does not support substitute fonts"); + if (!pdf_font_writing_supported(font)) + fz_throw(ctx, FZ_ERROR_GENERIC, "pdf device does not support font types found in this file"); + + gs->font = pdf_dev_add_font_res(ctx, pdev, font); + + fz_append_printf(ctx, gs->buf, "/F%d 1 Tf\n", gs->font); +} + +static void +pdf_dev_push_new_buf(fz_context *ctx, pdf_device *pdev, fz_buffer *buf, void (*on_pop)(fz_context*,pdf_device*,void*), void *on_pop_arg) +{ + if (pdev->num_gstates == pdev->max_gstates) + { + int newmax = pdev->max_gstates*2; + + pdev->gstates = fz_resize_array(ctx, pdev->gstates, newmax, sizeof(*pdev->gstates)); + pdev->max_gstates = newmax; + } + memcpy(&pdev->gstates[pdev->num_gstates], &pdev->gstates[pdev->num_gstates-1], sizeof(*pdev->gstates)); + fz_keep_stroke_state(ctx, pdev->gstates[pdev->num_gstates].stroke_state); + if (buf) + pdev->gstates[pdev->num_gstates].buf = buf; + else + fz_keep_buffer(ctx, pdev->gstates[pdev->num_gstates].buf); + pdev->gstates[pdev->num_gstates].on_pop = on_pop; + pdev->gstates[pdev->num_gstates].on_pop_arg = on_pop_arg; + fz_append_string(ctx, pdev->gstates[pdev->num_gstates].buf, "q\n"); + pdev->num_gstates++; +} + +static void +pdf_dev_push(fz_context *ctx, pdf_device *pdev) +{ + pdf_dev_push_new_buf(ctx, pdev, NULL, NULL, NULL); +} + +static void * +pdf_dev_pop(fz_context *ctx, pdf_device *pdev) +{ + gstate *gs = CURRENT_GSTATE(pdev); + void *arg = gs->on_pop_arg; + + fz_append_string(ctx, gs->buf, "Q\n"); + if (gs->on_pop) + gs->on_pop(ctx, pdev, arg); + pdev->num_gstates--; + fz_drop_stroke_state(ctx, pdev->gstates[pdev->num_gstates].stroke_state); + fz_drop_buffer(ctx, pdev->gstates[pdev->num_gstates].buf); + return arg; +} + +static void +pdf_dev_text_span(fz_context *ctx, pdf_device *pdev, fz_text_span *span) +{ + gstate *gs = CURRENT_GSTATE(pdev); + fz_matrix tm, inv_tm; + fz_point d; + float adv; + int dx, dy; + int i; + + if (span->len == 0) + return; + + tm = span->trm; + tm.e = span->items[0].x; + tm.f = span->items[0].y; + + inv_tm = fz_invert_matrix(tm); + + fz_append_printf(ctx, gs->buf, "%M Tm\n[<", &tm); + + for (i = 0; i < span->len; ++i) + { + fz_text_item *it = &span->items[i]; + if (it->gid < 0) + continue; + + /* transform difference from expected pen position into font units. */ + d.x = it->x - tm.e; + d.y = it->y - tm.f; + d = fz_transform_vector(d, inv_tm); + dx = (int)(d.x * 1000 + (d.x < 0 ? -0.5f : 0.5f)); + dy = (int)(d.y * 1000 + (d.y < 0 ? -0.5f : 0.5f)); + + tm.e = it->x; + tm.f = it->y; + + if (dx != 0 || dy != 0) + { + if (span->wmode == 0 && dy == 0) + fz_append_printf(ctx, gs->buf, ">%d<", -dx); + else if (span->wmode == 1 && dx == 0) + fz_append_printf(ctx, gs->buf, ">%d<", -dy); + else + fz_append_printf(ctx, gs->buf, ">]TJ\n%M Tm\n[<", &tm); + } + + if (fz_font_t3_procs(ctx, span->font)) + fz_append_printf(ctx, gs->buf, "%02x", it->gid); + else + fz_append_printf(ctx, gs->buf, "%04x", it->gid); + + adv = fz_advance_glyph(ctx, span->font, it->gid, span->wmode); + if (span->wmode == 0) + tm = fz_pre_translate(tm, adv, 0); + else + tm = fz_pre_translate(tm, 0, adv); + } + + fz_append_string(ctx, gs->buf, ">]TJ\n"); +} + +static void +pdf_dev_trm(fz_context *ctx, pdf_device *pdev, int trm) +{ + gstate *gs = CURRENT_GSTATE(pdev); + + if (gs->text_rendering_mode == trm) + return; + gs->text_rendering_mode = trm; + fz_append_printf(ctx, gs->buf, "%d Tr\n", trm); +} + +static void +pdf_dev_begin_text(fz_context *ctx, pdf_device *pdev, fz_matrix tm, int trm) +{ + pdf_dev_trm(ctx, pdev, trm); + if (!pdev->in_text) + { + gstate *gs = CURRENT_GSTATE(pdev); + fz_append_string(ctx, gs->buf, "BT\n"); + pdev->in_text = 1; + } +} + +static void +pdf_dev_end_text(fz_context *ctx, pdf_device *pdev) +{ + gstate *gs = CURRENT_GSTATE(pdev); + + if (!pdev->in_text) + return; + pdev->in_text = 0; + fz_append_string(ctx, gs->buf, "ET\n"); +} + +static int +pdf_dev_new_form(fz_context *ctx, pdf_obj **form_ref, pdf_device *pdev, fz_rect bbox, int isolated, int knockout, float alpha, fz_colorspace *colorspace) +{ + pdf_document *doc = pdev->doc; + int num; + pdf_obj *group_ref = NULL; + pdf_obj *group; + pdf_obj *form; + + *form_ref = NULL; + + /* Find (or make) a new group with the required options. */ + for(num = 0; num < pdev->num_groups; num++) + { + group_entry *g = &pdev->groups[num]; + if (g->isolated == isolated && g->knockout == knockout && g->alpha == alpha && g->colorspace == colorspace) + { + group_ref = pdev->groups[num].ref; + break; + } + } + + /* If we didn't find one, make one */ + if (num == pdev->num_groups) + { + if (pdev->num_groups == pdev->max_groups) + { + int newmax = pdev->max_groups * 2; + if (newmax == 0) + newmax = 4; + pdev->groups = fz_resize_array(ctx, pdev->groups, newmax, sizeof(*pdev->groups)); + pdev->max_groups = newmax; + } + pdev->num_groups++; + pdev->groups[num].isolated = isolated; + pdev->groups[num].knockout = knockout; + pdev->groups[num].alpha = alpha; + pdev->groups[num].colorspace = fz_keep_colorspace(ctx, colorspace); + pdev->groups[num].ref = NULL; + group = pdf_new_dict(ctx, doc, 5); + fz_try(ctx) + { + pdf_dict_put(ctx, group, PDF_NAME(Type), PDF_NAME(Group)); + pdf_dict_put(ctx, group, PDF_NAME(S), PDF_NAME(Transparency)); + pdf_dict_put_bool(ctx, group, PDF_NAME(K), knockout); + pdf_dict_put_bool(ctx, group, PDF_NAME(I), isolated); + switch (fz_colorspace_type(ctx, colorspace)) + { + case FZ_COLORSPACE_GRAY: + pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceGray)); + break; + case FZ_COLORSPACE_RGB: + pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceRGB)); + break; + case FZ_COLORSPACE_CMYK: + pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceCMYK)); + break; + default: + break; + } + group_ref = pdev->groups[num].ref = pdf_add_object(ctx, doc, group); + } + fz_always(ctx) + { + pdf_drop_obj(ctx, group); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + } + + /* Make us a new Forms object that points to that group, and change + * to writing into the buffer for that Forms object. */ + form = pdf_new_dict(ctx, doc, 4); + fz_try(ctx) + { + pdf_dict_put(ctx, form, PDF_NAME(Subtype), PDF_NAME(Form)); + pdf_dict_put(ctx, form, PDF_NAME(Group), group_ref); + pdf_dict_put_int(ctx, form, PDF_NAME(FormType), 1); + pdf_dict_put_rect(ctx, form, PDF_NAME(BBox), bbox); + *form_ref = pdf_add_object(ctx, doc, form); + } + fz_always(ctx) + { + pdf_drop_obj(ctx, form); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + + /* Insert the new form object into the resources */ + { + char text[32]; + num = pdev->num_forms++; + fz_snprintf(text, sizeof(text), "XObject/Fm%d", num); + pdf_dict_putp(ctx, pdev->resources, text, *form_ref); + } + + return num; +} + +/* Entry points */ + +static void +pdf_dev_fill_path(fz_context *ctx, fz_device *dev, const fz_path *path, int even_odd, fz_matrix ctm, + fz_colorspace *colorspace, const float *color, float alpha, const fz_color_params *color_params) +{ + pdf_device *pdev = (pdf_device*)dev; + gstate *gs = CURRENT_GSTATE(pdev); + + pdf_dev_end_text(ctx, pdev); + pdf_dev_alpha(ctx, pdev, alpha, 0); + pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params); + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_path(ctx, pdev, path); + fz_append_string(ctx, gs->buf, (even_odd ? "f*\n" : "f\n")); +} + +static void +pdf_dev_stroke_path(fz_context *ctx, fz_device *dev, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm, + fz_colorspace *colorspace, const float *color, float alpha, const fz_color_params *color_params) +{ + pdf_device *pdev = (pdf_device*)dev; + gstate *gs = CURRENT_GSTATE(pdev); + + pdf_dev_end_text(ctx, pdev); + pdf_dev_alpha(ctx, pdev, alpha, 1); + pdf_dev_color(ctx, pdev, colorspace, color, 1, color_params); + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_stroke_state(ctx, pdev, stroke); + pdf_dev_path(ctx, pdev, path); + fz_append_string(ctx, gs->buf, "S\n"); +} + +static void +pdf_dev_clip_path(fz_context *ctx, fz_device *dev, const fz_path *path, int even_odd, fz_matrix ctm, fz_rect scissor) +{ + pdf_device *pdev = (pdf_device*)dev; + gstate *gs; + + pdf_dev_end_text(ctx, pdev); + pdf_dev_push(ctx, pdev); + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_path(ctx, pdev, path); + gs = CURRENT_GSTATE(pdev); + fz_append_string(ctx, gs->buf, (even_odd ? "W* n\n" : "W n\n")); +} + +static void +pdf_dev_clip_stroke_path(fz_context *ctx, fz_device *dev, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm, fz_rect scissor) +{ + pdf_device *pdev = (pdf_device*)dev; + gstate *gs; + + pdf_dev_end_text(ctx, pdev); + pdf_dev_push(ctx, pdev); + /* FIXME: Need to push a group, select a pattern (or shading) here, + * stroke with the pattern/shading. Then move to defining that pattern + * with the next calls to the device interface until the next pop + * when we pop the group. */ + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_path(ctx, pdev, path); + gs = CURRENT_GSTATE(pdev); + fz_append_string(ctx, gs->buf, "W n\n"); +} + +static void +pdf_dev_fill_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm, + fz_colorspace *colorspace, const float *color, float alpha, const fz_color_params *color_params) +{ + pdf_device *pdev = (pdf_device*)dev; + fz_text_span *span; + + for (span = text->head; span; span = span->next) + { + fz_matrix trm = span->trm; + pdf_dev_begin_text(ctx, pdev, trm, 0); + pdf_dev_font(ctx, pdev, span->font); + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_alpha(ctx, pdev, alpha, 0); + pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params); + pdf_dev_text_span(ctx, pdev, span); + } +} + +static void +pdf_dev_stroke_text(fz_context *ctx, fz_device *dev, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm, + fz_colorspace *colorspace, const float *color, float alpha, const fz_color_params *color_params) +{ + pdf_device *pdev = (pdf_device*)dev; + fz_text_span *span; + + for (span = text->head; span; span = span->next) + { + pdf_dev_begin_text(ctx, pdev, span->trm, 1); + pdf_dev_font(ctx, pdev, span->font); + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_alpha(ctx, pdev, alpha, 1); + pdf_dev_color(ctx, pdev, colorspace, color, 1, color_params); + pdf_dev_text_span(ctx, pdev, span); + } +} + +static void +pdf_dev_clip_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm, fz_rect scissor) +{ + pdf_device *pdev = (pdf_device*)dev; + fz_text_span *span; + for (span = text->head; span; span = span->next) + { + pdf_dev_begin_text(ctx, pdev, span->trm, 0); + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_font(ctx, pdev, span->font); + pdf_dev_text_span(ctx, pdev, span); + } +} + +static void +pdf_dev_clip_stroke_text(fz_context *ctx, fz_device *dev, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm, fz_rect scissor) +{ + pdf_device *pdev = (pdf_device*)dev; + fz_text_span *span; + for (span = text->head; span; span = span->next) + { + pdf_dev_begin_text(ctx, pdev, span->trm, 0); + pdf_dev_font(ctx, pdev, span->font); + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_text_span(ctx, pdev, span); + } +} + +static void +pdf_dev_ignore_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm) +{ + pdf_device *pdev = (pdf_device*)dev; + fz_text_span *span; + for (span = text->head; span; span = span->next) + { + pdf_dev_begin_text(ctx, pdev, span->trm, 0); + pdf_dev_ctm(ctx, pdev, ctm); + pdf_dev_font(ctx, pdev, span->font); + pdf_dev_text_span(ctx, pdev, span); + } +} + +static void +pdf_dev_add_image_res(fz_context *ctx, fz_device *dev, pdf_obj *im_res) +{ + char text[32]; + pdf_device *pdev = (pdf_device*)dev; + int k; + int num; + + /* Check if we already had this one */ + for (k = 0; k < pdev->num_imgs; k++) + { + if (pdev->image_indices[k] == pdf_to_num(ctx, im_res)) + return; + } + + /* Not there so add to resources */ + fz_snprintf(text, sizeof(text), "XObject/Img%d", pdf_to_num(ctx, im_res)); + pdf_dict_putp(ctx, pdev->resources, text, im_res); + + /* And add index to our list for this page */ + if (pdev->num_imgs == pdev->max_imgs) + { + int newmax = pdev->max_imgs * 2; + if (newmax == 0) + newmax = 4; + pdev->image_indices = fz_resize_array(ctx, pdev->image_indices, newmax, sizeof(*pdev->image_indices)); + pdev->max_imgs = newmax; + } + num = pdev->num_imgs++; + pdev->image_indices[num] = pdf_to_num(ctx, im_res); +} + +static void +pdf_dev_fill_image(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, float alpha, const fz_color_params *color_params) +{ + pdf_device *pdev = (pdf_device*)dev; + pdf_obj *im_res; + gstate *gs = CURRENT_GSTATE(pdev); + + pdf_dev_end_text(ctx, pdev); + im_res = pdf_add_image(ctx, pdev->doc, image, 0); + if (im_res == NULL) + { + fz_warn(ctx, "pdf_add_image: problem adding image resource"); + return; + } + pdf_dev_alpha(ctx, pdev, alpha, 0); + + /* PDF images are upside down, so fiddle the ctm */ + ctm = fz_pre_scale(ctm, 1, -1); + ctm = fz_pre_translate(ctm, 0, -1); + pdf_dev_ctm(ctx, pdev, ctm); + fz_append_printf(ctx, gs->buf, "/Img%d Do\n", pdf_to_num(ctx, im_res)); + + /* Possibly add to page resources */ + pdf_dev_add_image_res(ctx, dev, im_res); + pdf_drop_obj(ctx, im_res); +} + +static void +pdf_dev_fill_shade(fz_context *ctx, fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha, const fz_color_params *color_params) +{ + pdf_device *pdev = (pdf_device*)dev; + + /* FIXME */ + pdf_dev_end_text(ctx, pdev); +} + +static void +pdf_dev_fill_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, + fz_colorspace *colorspace, const float *color, float alpha, const fz_color_params *color_params) +{ + pdf_device *pdev = (pdf_device*)dev; + pdf_obj *im_res = NULL; + gstate *gs = CURRENT_GSTATE(pdev); + + pdf_dev_end_text(ctx, pdev); + im_res = pdf_add_image(ctx, pdev->doc, image, 1); + if (im_res == NULL) + { + fz_warn(ctx, "pdf_add_image: problem adding image resource"); + return; + } + fz_append_string(ctx, gs->buf, "q\n"); + pdf_dev_alpha(ctx, pdev, alpha, 0); + pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params); + + /* PDF images are upside down, so fiddle the ctm */ + ctm = fz_pre_scale(ctm, 1, -1); + ctm = fz_pre_translate(ctm, 0, -1); + pdf_dev_ctm(ctx, pdev, ctm); + fz_append_printf(ctx, gs->buf, "/Img%d Do Q\n", pdf_to_num(ctx, im_res)); + + /* Possibly add to page resources */ + pdf_dev_add_image_res(ctx, dev, im_res); + pdf_drop_obj(ctx, im_res); +} + +static void +pdf_dev_clip_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, fz_rect scissor) +{ + pdf_device *pdev = (pdf_device*)dev; + + /* FIXME */ + pdf_dev_end_text(ctx, pdev); + pdf_dev_push(ctx, pdev); +} + +static void +pdf_dev_pop_clip(fz_context *ctx, fz_device *dev) +{ + pdf_device *pdev = (pdf_device*)dev; + + /* FIXME */ + pdf_dev_end_text(ctx, pdev); + pdf_dev_pop(ctx, pdev); +} + +static void +pdf_dev_begin_mask(fz_context *ctx, fz_device *dev, fz_rect bbox, int luminosity, fz_colorspace *colorspace, const float *color, const fz_color_params *color_params) +{ + pdf_device *pdev = (pdf_device*)dev; + pdf_document *doc = pdev->doc; + gstate *gs; + pdf_obj *smask = NULL; + pdf_obj *egs = NULL; + pdf_obj *egs_ref; + pdf_obj *form_ref; + pdf_obj *color_obj = NULL; + int i; + + fz_var(smask); + fz_var(egs); + fz_var(color_obj); + + pdf_dev_end_text(ctx, pdev); + + /* Make a new form to contain the contents of the softmask */ + pdf_dev_new_form(ctx, &form_ref, pdev, bbox, 0, 0, 1, colorspace); + + fz_try(ctx) + { + int n = fz_colorspace_n(ctx, colorspace); + smask = pdf_new_dict(ctx, doc, 4); + pdf_dict_put(ctx, smask, PDF_NAME(Type), PDF_NAME(Mask)); + pdf_dict_put(ctx, smask, PDF_NAME(S), (luminosity ? PDF_NAME(Luminosity) : PDF_NAME(Alpha))); + pdf_dict_put(ctx, smask, PDF_NAME(G), form_ref); + color_obj = pdf_new_array(ctx, doc, n); + for (i = 0; i < n; i++) + pdf_array_push_real(ctx, color_obj, color[i]); + pdf_dict_put_drop(ctx, smask, PDF_NAME(BC), color_obj); + color_obj = NULL; + + egs = pdf_new_dict(ctx, doc, 5); + pdf_dict_put(ctx, egs, PDF_NAME(Type), PDF_NAME(ExtGState)); + pdf_dict_put_drop(ctx, egs, PDF_NAME(SMask), pdf_add_object(ctx, doc, smask)); + + { + char text[32]; + fz_snprintf(text, sizeof(text), "ExtGState/SM%d", pdev->num_smasks++); + egs_ref = pdf_add_object(ctx, doc, egs); + pdf_dict_putp_drop(ctx, pdev->resources, text, egs_ref); + } + gs = CURRENT_GSTATE(pdev); + fz_append_printf(ctx, gs->buf, "/SM%d gs\n", pdev->num_smasks-1); + } + fz_always(ctx) + { + pdf_drop_obj(ctx, smask); + } + fz_catch(ctx) + { + pdf_drop_obj(ctx, form_ref); + pdf_drop_obj(ctx, color_obj); + fz_rethrow(ctx); + } + + /* Now, everything we get until the end_mask needs to go into a + * new buffer, which will be the stream contents for the form. */ + pdf_dev_push_new_buf(ctx, pdev, fz_new_buffer(ctx, 1024), NULL, form_ref); +} + +static void +pdf_dev_end_mask(fz_context *ctx, fz_device *dev) +{ + pdf_device *pdev = (pdf_device*)dev; + pdf_document *doc = pdev->doc; + gstate *gs = CURRENT_GSTATE(pdev); + fz_buffer *buf = fz_keep_buffer(ctx, gs->buf); + pdf_obj *form_ref = (pdf_obj *)gs->on_pop_arg; + + /* Here we do part of the pop, but not all of it. */ + pdf_dev_end_text(ctx, pdev); + fz_append_string(ctx, buf, "Q\n"); + pdf_update_stream(ctx, doc, form_ref, buf, 0); + fz_drop_buffer(ctx, buf); + gs->buf = fz_keep_buffer(ctx, gs[-1].buf); + gs->on_pop_arg = NULL; + pdf_drop_obj(ctx, form_ref); + fz_append_string(ctx, gs->buf, "q\n"); +} + +static void +pdf_dev_begin_group(fz_context *ctx, fz_device *dev, fz_rect bbox, fz_colorspace *cs, int isolated, int knockout, int blendmode, float alpha) +{ + pdf_device *pdev = (pdf_device*)dev; + pdf_document *doc = pdev->doc; + int num; + pdf_obj *form_ref; + gstate *gs; + + pdf_dev_end_text(ctx, pdev); + + num = pdf_dev_new_form(ctx, &form_ref, pdev, bbox, isolated, knockout, alpha, cs); + + /* Do we have an appropriate blending extgstate already? */ + { + char text[32]; + pdf_obj *obj; + fz_snprintf(text, sizeof(text), "ExtGState/BlendMode%d", blendmode); + obj = pdf_dict_getp(ctx, pdev->resources, text); + if (obj == NULL) + { + /* No, better make one */ + obj = pdf_new_dict(ctx, doc, 2); + pdf_dict_put(ctx, obj, PDF_NAME(Type), PDF_NAME(ExtGState)); + pdf_dict_put_name(ctx, obj, PDF_NAME(BM), fz_blendmode_name(blendmode)); + pdf_dict_putp_drop(ctx, pdev->resources, text, obj); + } + } + + /* Add the call to this group */ + gs = CURRENT_GSTATE(pdev); + fz_append_printf(ctx, gs->buf, "/BlendMode%d gs /Fm%d Do\n", blendmode, num); + + /* Now, everything we get until the end of group needs to go into a + * new buffer, which will be the stream contents for the form. */ + pdf_dev_push_new_buf(ctx, pdev, fz_new_buffer(ctx, 1024), NULL, form_ref); +} + +static void +pdf_dev_end_group(fz_context *ctx, fz_device *dev) +{ + pdf_device *pdev = (pdf_device*)dev; + pdf_document *doc = pdev->doc; + gstate *gs = CURRENT_GSTATE(pdev); + fz_buffer *buf = fz_keep_buffer(ctx, gs->buf); + pdf_obj *form_ref; + + pdf_dev_end_text(ctx, pdev); + form_ref = (pdf_obj *)pdf_dev_pop(ctx, pdev); + pdf_update_stream(ctx, doc, form_ref, buf, 0); + fz_drop_buffer(ctx, buf); + pdf_drop_obj(ctx, form_ref); +} + +static int +pdf_dev_begin_tile(fz_context *ctx, fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm, int id) +{ + pdf_device *pdev = (pdf_device*)dev; + + /* FIXME */ + pdf_dev_end_text(ctx, pdev); + return 0; +} + +static void +pdf_dev_end_tile(fz_context *ctx, fz_device *dev) +{ + pdf_device *pdev = (pdf_device*)dev; + + /* FIXME */ + pdf_dev_end_text(ctx, pdev); +} + +static void +pdf_dev_close_device(fz_context *ctx, fz_device *dev) +{ + pdf_device *pdev = (pdf_device*)dev; + pdf_dev_end_text(ctx, pdev); +} + +static void +pdf_dev_drop_device(fz_context *ctx, fz_device *dev) +{ + pdf_device *pdev = (pdf_device*)dev; + int i; + + for (i = pdev->num_gstates-1; i >= 0; i--) + fz_drop_stroke_state(ctx, pdev->gstates[i].stroke_state); + + for (i = pdev->num_cid_fonts-1; i >= 0; i--) + fz_drop_font(ctx, pdev->cid_fonts[i]); + + for (i = pdev->num_groups - 1; i >= 0; i--) + pdf_drop_obj(ctx, pdev->groups[i].ref); + + fz_drop_buffer(ctx, pdev->buffer); + pdf_drop_obj(ctx, pdev->resources); + fz_free(ctx, pdev->cid_fonts); + fz_free(ctx, pdev->image_indices); + fz_free(ctx, pdev->groups); + fz_free(ctx, pdev->alphas); + fz_free(ctx, pdev->gstates); +} + +fz_device *pdf_new_pdf_device(fz_context *ctx, pdf_document *doc, fz_matrix topctm, fz_rect mediabox, pdf_obj *resources, fz_buffer *buf) +{ + pdf_device *dev = fz_new_derived_device(ctx, pdf_device); + + dev->super.close_device = pdf_dev_close_device; + dev->super.drop_device = pdf_dev_drop_device; + + dev->super.fill_path = pdf_dev_fill_path; + dev->super.stroke_path = pdf_dev_stroke_path; + dev->super.clip_path = pdf_dev_clip_path; + dev->super.clip_stroke_path = pdf_dev_clip_stroke_path; + + dev->super.fill_text = pdf_dev_fill_text; + dev->super.stroke_text = pdf_dev_stroke_text; + dev->super.clip_text = pdf_dev_clip_text; + dev->super.clip_stroke_text = pdf_dev_clip_stroke_text; + dev->super.ignore_text = pdf_dev_ignore_text; + + dev->super.fill_shade = pdf_dev_fill_shade; + dev->super.fill_image = pdf_dev_fill_image; + dev->super.fill_image_mask = pdf_dev_fill_image_mask; + dev->super.clip_image_mask = pdf_dev_clip_image_mask; + + dev->super.pop_clip = pdf_dev_pop_clip; + + dev->super.begin_mask = pdf_dev_begin_mask; + dev->super.end_mask = pdf_dev_end_mask; + dev->super.begin_group = pdf_dev_begin_group; + dev->super.end_group = pdf_dev_end_group; + + dev->super.begin_tile = pdf_dev_begin_tile; + dev->super.end_tile = pdf_dev_end_tile; + + fz_try(ctx) + { + dev->buffer = fz_keep_buffer(ctx, buf); + if (!buf) + buf = fz_new_buffer(ctx, 256); + dev->doc = doc; + dev->resources = pdf_keep_obj(ctx, resources); + dev->gstates = fz_malloc_struct(ctx, gstate); + dev->gstates[0].buf = buf; + dev->gstates[0].ctm = fz_identity; // XXX + dev->gstates[0].colorspace[0] = fz_device_gray(ctx); + dev->gstates[0].colorspace[1] = fz_device_gray(ctx); + dev->gstates[0].color[0][0] = 1; + dev->gstates[0].color[1][0] = 1; + dev->gstates[0].alpha[0] = 1.0f; + dev->gstates[0].alpha[1] = 1.0f; + dev->gstates[0].font = -1; + dev->num_gstates = 1; + dev->max_gstates = 1; + + if (!fz_is_identity(topctm)) + fz_append_printf(ctx, buf, "%M cm\n", &topctm); + } + fz_catch(ctx) + { + if (dev->gstates && dev->buffer == NULL) + fz_drop_buffer(ctx, dev->gstates[0].buf); + fz_free(ctx, dev); + fz_rethrow(ctx); + } + + return (fz_device*)dev; +} + +fz_device *pdf_page_write(fz_context *ctx, pdf_document *doc, fz_rect mediabox, pdf_obj **presources, fz_buffer **pcontents) +{ + fz_matrix pagectm = { 1, 0, 0, -1, -mediabox.x0, mediabox.y1 }; + *presources = pdf_new_dict(ctx, doc, 0); + *pcontents = fz_new_buffer(ctx, 0); + return pdf_new_pdf_device(ctx, doc, pagectm, mediabox, *presources, *pcontents); +} diff --git a/fitz/fitz.i b/fitz/fitz.i index 6944a91b5..94a50447e 100644 --- a/fitz/fitz.i +++ b/fitz/fitz.i @@ -3,15 +3,15 @@ // SWIG macro: generate fitz exceptions //----------------------------------------------------------------------------- %define FITZEXCEPTION(meth, cond) - %exception meth - { - $action - if(cond) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } +%exception meth +{ + $action + if(cond) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } +} %enddef //----------------------------------------------------------------------------- @@ -50,29 +50,6 @@ self._annot_refs[id(val)] = val%} %enddef -%define ANNOTWRAP2(meth, doc) - FITZEXCEPTION(meth, !result) - %pythonprepend meth %{CheckParent(self)%} - %feature("autodoc", doc) meth; - %pythonappend meth %{ - if not val: return - val.thisown = True - val.parent = weakref.proxy(self) - self._annot_refs[id(val)] = val - - if val.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(val) - elif val.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(val) - elif val.type[0] in (ANNOT_LINE, ANNOT_POLYLINE, ANNOT_POLYGON): - ap = _make_line_AP(val) - else: - return val - r = Rect(0, 0, val.rect.width, val.rect.height) - val._checkAP(r, ap) - %} -%enddef - %feature("autodoc", "0"); %{ @@ -87,6 +64,7 @@ #define SWIG_FILE_WITH_INIT #define SWIG_PYTHON_2_UNICODE +#define JM_EPS 1E-5 // memory allocation macros #define JM_MEMORY 1 @@ -151,10 +129,38 @@ char *JM_Python_str_AsChar(PyObject *str); # endif } fz_register_document_handlers(gctx); + +//----------------------------------------------------------------------------- +// START redirect stdout/stderr +//----------------------------------------------------------------------------- + +JM_fitz_stdout = fz_new_output(gctx, 0, JM_fitz_stdout, JM_write_stdout, NULL, NULL); + +fz_set_stdout(gctx, JM_fitz_stdout); + +JM_fitz_stderr = fz_new_output(gctx, 0, JM_fitz_stderr, JM_write_stderr, NULL, NULL); + +fz_set_stderr(gctx, JM_fitz_stderr); + +if (JM_fitz_stderr && JM_fitz_stdout) + {;} +else + PySys_WriteStdout("error redirecting stdout/stderr!\n"); + +JM_error_log = PyList_New(0); +JM_output_log = PyList_New(0); + +//----------------------------------------------------------------------------- +// STOP redirect stdout/stderr +//----------------------------------------------------------------------------- %} %header %{ - fz_context *gctx; +fz_context *gctx; +int JM_UNIQUE_ID = 0; + +PyObject *fitz_stdout = NULL; +PyObject *fitz_stderr = NULL; struct DeviceWrapper { fz_device *device; @@ -169,9 +175,15 @@ struct DeviceWrapper { import weakref from binascii import hexlify import math +import platform +platform_bitness = platform.architecture()[0] +del platform %} %include version.i +%include helper-geo-c.i %include helper-other.i +%include helper-write-c.i +%include helper-geo-py.i %include helper-annot.i %include helper-stext.i %include helper-fields.i @@ -226,9 +238,7 @@ struct fz_document_s self.openErrCode = self._getGCTXerrcode() self.openErrMsg = self._getGCTXerrmsg() self.thisown = True - tools = Tools() - self._graft_id = tools.gen_id() - tools = None + self._graft_id = TOOLS.gen_id() if self.needsPass: self.isEncrypted = 1 else: # we won't init until doc is decrypted @@ -238,7 +248,7 @@ struct fz_document_s %} fz_document_s(const char *filename = NULL, PyObject *stream = NULL, - const char *filetype = NULL, struct fz_rect_s *rect = NULL, + const char *filetype = NULL, PyObject *rect = NULL, float width = 0, float height = 0, float fontsize = 11) { @@ -248,16 +258,16 @@ struct fz_document_s fz_stream *data = NULL; char *streamdata; float w = width, h = height; + fz_rect r = JM_rect_from_py(rect); + if (!(fz_is_empty_rect(r) && !fz_is_infinite_rect(r))) + { + w = r.x1 - r.x0; + h = r.y1 - r.y0; + } + size_t streamlen = JM_CharFromBytesOrArray(stream, &streamdata); fz_try(gctx) { - if (rect) - { - if (fz_is_empty_rect(rect) || fz_is_infinite_rect(rect)) - THROWMSG("rect must be finite and not empty"); - w = rect->x1 - rect->x0; - h = rect->y1 - rect->y0; - } if (streamlen > 0) { data = fz_open_memory(gctx, streamdata, streamlen); @@ -403,7 +413,7 @@ struct fz_document_s //------------------------------------------------------------- for (i = 0; i < n; i += 2) { - char *test = pdf_to_utf8(gctx, pdf_array_get(gctx, names, i)); + char *test = (char *) pdf_to_text_string(gctx, pdf_array_get(gctx, names, i)); if (!strcmp(test, name)) { pdf_array_delete(gctx, names, i + 1); @@ -436,32 +446,32 @@ struct fz_document_s PyObject *infodict = PyDict_New(); // name of file entry - name = pdf_to_utf8(gctx, pdf_portfolio_entry_name(gctx, pdf, n)); + name = (char *) pdf_to_text_string(gctx, pdf_portfolio_entry_name(gctx, pdf, n)); PyDict_SetItemString(infodict, "name", JM_UNICODE(name)); pdf_obj *o = pdf_portfolio_entry_obj(gctx, pdf, n); - name = pdf_to_utf8(gctx, pdf_dict_get(gctx, o, PDF_NAME_F)); + name = (char *) pdf_to_text_string(gctx, pdf_dict_get(gctx, o, PDF_NAME(F))); PyDict_SetItemString(infodict, "filename", JM_UNICODE(name)); - name = pdf_to_utf8(gctx, pdf_dict_get(gctx, o, PDF_NAME_UF)); + name = (char *) pdf_to_text_string(gctx, pdf_dict_get(gctx, o, PDF_NAME(UF))); PyDict_SetItemString(infodict, "ufilename", JM_UNICODE(name)); - name = pdf_to_utf8(gctx, pdf_dict_get(gctx, o, PDF_NAME_Desc)); + name = (char *) pdf_to_text_string(gctx, pdf_dict_get(gctx, o, PDF_NAME(Desc))); PyDict_SetItemString(infodict, "desc", JM_UNICODE(name)); int len = -1, DL = -1; - pdf_obj *ef = pdf_dict_get(gctx, o, PDF_NAME_EF); - o = pdf_dict_getl(gctx, ef, PDF_NAME_F, - PDF_NAME_Length, NULL); + pdf_obj *ef = pdf_dict_get(gctx, o, PDF_NAME(EF)); + o = pdf_dict_getl(gctx, ef, PDF_NAME(F), + PDF_NAME(Length), NULL); if (o) len = pdf_to_int(gctx, o); - o = pdf_dict_getl(gctx, ef, PDF_NAME_F, PDF_NAME_DL, NULL); + o = pdf_dict_getl(gctx, ef, PDF_NAME(F), PDF_NAME(DL), NULL); if (o) DL = pdf_to_int(gctx, o); else { - o = pdf_dict_getl(gctx, ef, PDF_NAME_F, PDF_NAME_Params, - PDF_NAME_Size, NULL); + o = pdf_dict_getl(gctx, ef, PDF_NAME(F), PDF_NAME(Params), + PDF_NAME(Size), NULL); if (o) DL = pdf_to_int(gctx, o); } @@ -486,8 +496,8 @@ struct fz_document_s if (n < 0) THROWMSG("entry not found"); pdf_obj *entry = pdf_portfolio_entry_obj(gctx, pdf, n); - pdf_obj *filespec = pdf_dict_getl(gctx, entry, PDF_NAME_EF, - PDF_NAME_F, NULL); + pdf_obj *filespec = pdf_dict_getl(gctx, entry, PDF_NAME(EF), + PDF_NAME(F), NULL); char *data = NULL; size_t len = JM_CharFromBytesOrArray(buffer, &data); @@ -497,18 +507,18 @@ struct fz_document_s res = fz_new_buffer_from_copied_data(gctx, data, len); JM_update_stream(gctx, pdf, filespec, res); // adjust /DL and /Size parameters - pdf_obj *l = pdf_new_int(gctx, NULL, (int64_t) len); - pdf_dict_put(gctx, filespec, PDF_NAME_DL, l); - pdf_dict_putl(gctx, filespec, l, PDF_NAME_Params, PDF_NAME_Size, NULL); + pdf_obj *l = pdf_new_int(gctx, (int64_t) len); + pdf_dict_put(gctx, filespec, PDF_NAME(DL), l); + pdf_dict_putl(gctx, filespec, l, PDF_NAME(Params), PDF_NAME(Size), NULL); } if (filename) - pdf_dict_put_text_string(gctx, entry, PDF_NAME_F, filename); + pdf_dict_put_text_string(gctx, entry, PDF_NAME(F), filename); if (ufilename) - pdf_dict_put_text_string(gctx, entry, PDF_NAME_UF, ufilename); + pdf_dict_put_text_string(gctx, entry, PDF_NAME(UF), ufilename); if (desc) - pdf_dict_put_text_string(gctx, entry, PDF_NAME_Desc, desc); + pdf_dict_put_text_string(gctx, entry, PDF_NAME(Desc), desc); } fz_always(gctx) fz_drop_buffer(gctx, res); @@ -596,18 +606,18 @@ struct fz_document_s entry = JM_find_embedded(gctx, Py_BuildValue("s", name), pdf); // (1) insert the real metadata pdf_obj *o = pdf_portfolio_entry_obj(gctx, pdf, entry); - pdf_dict_put_text_string(gctx, o, PDF_NAME_F, f); - pdf_dict_put_text_string(gctx, o, PDF_NAME_UF, uf); - pdf_dict_put_text_string(gctx, o, PDF_NAME_Desc, d); + pdf_dict_put_text_string(gctx, o, PDF_NAME(F), f); + pdf_dict_put_text_string(gctx, o, PDF_NAME(UF), uf); + pdf_dict_put_text_string(gctx, o, PDF_NAME(Desc), d); // (2) insert the real file contents - pdf_obj *filespec = pdf_dict_getl(gctx, o, PDF_NAME_EF, - PDF_NAME_F, NULL); + pdf_obj *filespec = pdf_dict_getl(gctx, o, PDF_NAME(EF), + PDF_NAME(F), NULL); data = fz_new_buffer_from_copied_data(gctx, buffdata, size); JM_update_stream(gctx, pdf, filespec, data); // finally update some size attributes - pdf_obj *l = pdf_new_int(gctx, NULL, (int64_t) size); - pdf_dict_put(gctx, filespec, PDF_NAME_DL, l); - pdf_dict_putl(gctx, filespec, l, PDF_NAME_Params, PDF_NAME_Size, NULL); + pdf_obj *l = pdf_new_int(gctx, (int64_t) size); + pdf_dict_put(gctx, filespec, PDF_NAME(DL), l); + pdf_dict_putl(gctx, filespec, l, PDF_NAME(Params), PDF_NAME(Size), NULL); } fz_always(gctx) { @@ -621,7 +631,7 @@ struct fz_document_s FITZEXCEPTION(convertToPDF, !result) CLOSECHECK(convertToPDF) - %feature("autodoc","Convert document to PDF selecting copy range and optional rotation. Output bytes object.") convertToPDF; + %feature("autodoc","Convert document to PDF selecting page range and optional rotation. Output bytes object.") convertToPDF; PyObject *convertToPDF(int from_page=0, int to_page=-1, int rotate=0) { PyObject *doc = NULL; @@ -629,7 +639,7 @@ struct fz_document_s { int fp = from_page, tp = to_page, srcCount = fz_count_pages(gctx, $self); if (pdf_specifics(gctx, $self)) - THROWMSG("document is PDF already"); + THROWMSG("use select+write or insertPDF for PDF docs instead"); if (fp < 0) fp = 0; if (fp > srcCount - 1) fp = srcCount - 1; if (tp < 0) tp = srcCount - 1; @@ -686,18 +696,17 @@ struct fz_document_s %pythonappend layout %{ self._reset_page_refs() self.initData()%} - PyObject *layout(struct fz_rect_s *rect = NULL, float width = 0, float height = 0, float fontsize = 11) + PyObject *layout(PyObject *rect = NULL, float width = 0, float height = 0, float fontsize = 11) { if (!fz_is_document_reflowable(gctx, $self)) return NONE; fz_try(gctx) { float w = width, h = height; - if (rect) + fz_rect r = JM_rect_from_py(rect); + if (!fz_is_empty_rect(r) && !fz_is_infinite_rect(r)) { - if (fz_is_empty_rect(rect) || fz_is_infinite_rect(rect)) - THROWMSG("rect must be finite and not empty"); - w = rect->x1 - rect->x0; - h = rect->y1 - rect->y0; + w = r.x1 - r.x0; + h = r.y1 - r.y0; } if (w <= 0.0f || h <= 0.0f) THROWMSG("invalid page size"); @@ -759,7 +768,7 @@ struct fz_document_s fz_try(gctx) { pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), - PDF_NAME_Root); + PDF_NAME(Root)); xref = pdf_to_num(gctx, root); } fz_catch(gctx) {;} @@ -774,6 +783,26 @@ struct fz_document_s else Py_RETURN_FALSE; } + CLOSECHECK0(_hasXrefStream) + %pythoncode%{@property%} + PyObject *_hasXrefStream() + { + pdf_document *pdf = pdf_specifics(gctx, $self); + if (!pdf) Py_RETURN_FALSE; + if (pdf->has_xref_streams) Py_RETURN_TRUE; + Py_RETURN_FALSE; + } + + CLOSECHECK0(_hasXrefOldStyle) + %pythoncode%{@property%} + PyObject *_hasXrefOldStyle() + { + pdf_document *pdf = pdf_specifics(gctx, $self); + if (!pdf) Py_RETURN_FALSE; + if (pdf->has_old_style_xrefs) Py_RETURN_TRUE; + Py_RETURN_FALSE; + } + CLOSECHECK0(isDirty) %pythoncode%{@property%} PyObject *isDirty() @@ -799,8 +828,8 @@ struct fz_document_s self.initData() self.thisown = True %} - int authenticate(const char *pass) { - return fz_authenticate_password(gctx, $self, pass); + int authenticate(char *password) { + return fz_authenticate_password(gctx, $self, (const char *) password); } //--------------------------------------------------------------------- // save(filename, ...) @@ -816,12 +845,15 @@ struct fz_document_s else: raise TypeError("filename must be a string") if filename == self.name and not incremental: - raise ValueError("save to original requires incremental") - if incremental and (self.name != filename or self.streamlen > 0): - raise ValueError("incremental save needs original file") + raise ValueError("save to original must be incremental") + if self.pageCount < 1: + raise ValueError("cannot save with zero pages") + if incremental: + if self.name != filename or self.streamlen > 0: + raise ValueError("incremental needs original file") %} - PyObject *save(char *filename, int garbage=0, int clean=0, int deflate=0, int incremental=0, int ascii=0, int expand=0, int linear=0, int pretty = 0) + PyObject *save(char *filename, int garbage=0, int clean=0, int deflate=0, int incremental=0, int ascii=0, int expand=0, int linear=0, int pretty = 0, int decrypt = 1) { int errors = 0; pdf_write_options opts = { 0 }; @@ -840,17 +872,12 @@ struct fz_document_s opts.errors = &errors; pdf_document *pdf = pdf_specifics(gctx, $self); fz_try(gctx) - { + { assert_PDF(pdf); - if (fz_count_pages(gctx, $self) < 1) - THROWMSG("document has no pages"); - if ((incremental) && (fz_needs_password(gctx, $self))) - THROWMSG("decrypted file - save to new"); - pdf_finish_edit(gctx, pdf); JM_embedded_clean(gctx, pdf); - pdf_save_document(gctx, pdf, filename, &opts); + JM_save_document(gctx, pdf, filename, &opts, decrypt); pdf->dirty = 0; - } + } fz_catch(gctx) return NULL; return NONE; } @@ -862,10 +889,13 @@ struct fz_document_s %feature("autodoc", "Write document to a bytes object.") write; %pythonprepend write %{ if self.isClosed or self.isEncrypted: - raise ValueError("operation illegal for closed / encrypted doc")%} + raise ValueError("operation illegal for closed / encrypted doc") + if self.pageCount < 1: + raise ValueError("cannot write with zero pages") + %} PyObject *write(int garbage=0, int clean=0, int deflate=0, - int ascii=0, int expand=0, int linear=0, int pretty = 0) + int ascii=0, int expand=0, int linear=0, int pretty = 0, int decrypt = 1) { PyObject *r; struct fz_buffer_s *res = NULL; @@ -892,12 +922,11 @@ struct fz_document_s { assert_PDF(pdf); if (fz_count_pages(gctx, $self) < 1) - THROWMSG("document has zero pages"); - pdf_finish_edit(gctx, pdf); + THROWMSG("cannot save with zero pages"); JM_embedded_clean(gctx, pdf); res = fz_new_buffer(gctx, 1024); out = fz_new_output_with_buffer(gctx, res); - pdf_write_document(gctx, pdf, out, &opts); + JM_write_document(gctx, pdf, out, &opts, decrypt); pdf->dirty = 0; r = JM_BinFromBuffer(gctx, res); } @@ -910,11 +939,11 @@ struct fz_document_s return r; } - //********************************************************************* + //--------------------------------------------------------------------- // Insert pages from a source PDF into this PDF. // For reconstructing the links (_do_links method), we must save the // insertion point (start_at) if it was specified as -1. - //********************************************************************* + //--------------------------------------------------------------------- FITZEXCEPTION(insertPDF, !result) %pythonprepend insertPDF %{if self.isClosed or self.isEncrypted: @@ -939,18 +968,20 @@ if links: pdf_document *pdfsrc = pdf_specifics(gctx, docsrc); int outCount = fz_count_pages(gctx, $self); int srcCount = fz_count_pages(gctx, docsrc); - int fp, tp, sa; + // local copies of page numbers - fp = from_page; - tp = to_page; - sa = start_at; - /* normalize page specifications */ - if (fp < 0) fp = 0; - if (fp > srcCount - 1) fp = srcCount - 1; - if (tp < 0) tp = srcCount - 1; - if (tp > srcCount - 1) tp = srcCount - 1; - if (sa < 0) sa = outCount; - if (sa > outCount) sa = outCount; + int fp = from_page, tp = to_page, sa = start_at; + + // normalize page numbers + fp = MAX(fp, 0); // -1 = first page + fp = MIN(fp, srcCount - 1); // but do not exceed last page + + if (tp < 0) tp = srcCount - 1; // -1 = last page + tp = MIN(tp, srcCount - 1); // but do not exceed last page + + if (sa < 0) sa = outCount; // -1 = behind last page + sa = MIN(sa, outCount); // but that is also the limit + fz_try(gctx) { if (!pdfout || !pdfsrc) THROWMSG("source or target not a PDF"); @@ -1003,9 +1034,8 @@ if links: contents = fz_new_buffer(gctx, 10); fz_append_string(gctx, contents, ""); fz_terminate_buffer(gctx, contents); - page_obj = pdf_add_page(gctx, pdf, &mediabox, 0, resources, contents); + page_obj = pdf_add_page(gctx, pdf, mediabox, 0, resources, contents); pdf_insert_page(gctx, pdf, pno , page_obj); - pdf_finish_edit(gctx, pdf); } fz_always(gctx) { @@ -1038,15 +1068,14 @@ if links: { assert_PDF(pdf); if (!PySequence_Check(pyliste)) - THROWMSG("expected a sequence"); + THROWMSG("sequence required"); if (PySequence_Size(pyliste) < 1) - THROWMSG("len(sequence) invalid"); + THROWMSG("invalid sequ. length"); // now call retainpages (code copy of fz_clean_file.c) globals glo = {0}; glo.ctx = gctx; glo.doc = pdf; retainpages(gctx, &glo, pyliste); - pdf_finish_edit(gctx, pdf); } fz_catch(gctx) return NULL; pdf->dirty = 1; @@ -1097,7 +1126,7 @@ if links: pdf_obj *o = pdf_load_object(gctx, pdf, xref); if (pdf_is_dict(gctx, o)) { - basefont = pdf_dict_get(gctx, o, PDF_NAME_BaseFont); + basefont = pdf_dict_get(gctx, o, PDF_NAME(BaseFont)); if (pdf_is_name(gctx, basefont)) { bfname = (char *) pdf_to_name(gctx, basefont); @@ -1189,7 +1218,7 @@ if links: if (n >= pageCount) THROWMSG("invalid page number(s)"); assert_PDF(pdf); pageref = pdf_lookup_page_obj(gctx, pdf, n); - rsrc = pdf_dict_get(gctx, pageref, PDF_NAME_Resources); + rsrc = pdf_dict_get(gctx, pageref, PDF_NAME(Resources)); if (!pageref || !rsrc) THROWMSG("cannot retrieve page info"); liste = PyList_New(0); JM_scan_resources(gctx, pdf, rsrc, liste, what); @@ -1222,14 +1251,14 @@ if links: fz_try(gctx) { obj = pdf_load_object(gctx, pdf, xref); - pdf_obj *type = pdf_dict_get(gctx, obj, PDF_NAME_Type); - pdf_obj *subtype = pdf_dict_get(gctx, obj, PDF_NAME_Subtype); - if(pdf_name_eq(gctx, type, PDF_NAME_Font) && + pdf_obj *type = pdf_dict_get(gctx, obj, PDF_NAME(Type)); + pdf_obj *subtype = pdf_dict_get(gctx, obj, PDF_NAME(Subtype)); + if(pdf_name_eq(gctx, type, PDF_NAME(Font)) && strncmp(pdf_to_name(gctx, subtype), "CIDFontType", 11) != 0) { - basefont = pdf_dict_get(gctx, obj, PDF_NAME_BaseFont); + basefont = pdf_dict_get(gctx, obj, PDF_NAME(BaseFont)); if (!basefont || pdf_is_null(gctx, basefont)) - bname = pdf_dict_get(gctx, obj, PDF_NAME_Name); + bname = pdf_dict_get(gctx, obj, PDF_NAME(Name)); else bname = basefont; ext = fontextension(gctx, pdf, xref); @@ -1239,7 +1268,7 @@ if links: bytes = JM_BinFromBuffer(gctx, buffer); fz_drop_buffer(gctx, buffer); } - fontname = JM_ASCIIFromChar(pdf_to_name(gctx, bname)); + fontname = (char *) JM_ASCIIFromChar((char *) pdf_to_name(gctx, bname)); tuple = Py_BuildValue("sssO", fontname, ext, @@ -1295,8 +1324,8 @@ if links: fz_try(gctx) { obj = pdf_new_indirect(gctx, pdf, xref, 0); - pdf_obj *subtype = pdf_dict_get(gctx, obj, PDF_NAME_Subtype); - if (pdf_name_eq(gctx, subtype, PDF_NAME_Image)) + pdf_obj *subtype = pdf_dict_get(gctx, obj, PDF_NAME(Subtype)); + if (pdf_name_eq(gctx, subtype, PDF_NAME(Image))) { image = pdf_load_image(gctx, pdf, obj); cbuf = fz_compressed_image_buffer(gctx, image); @@ -1304,13 +1333,13 @@ if links: // ensure returning a PNG for unsupported images ---------- if (type < FZ_IMAGE_BMP) type = FZ_IMAGE_UNKNOWN; - pdf_obj *o = pdf_dict_get(gctx, obj, PDF_NAME_SMask); + pdf_obj *o = pdf_dict_get(gctx, obj, PDF_NAME(SMask)); if (o) smask = pdf_to_num(gctx, o); - o = pdf_dict_get(gctx, obj, PDF_NAME_Width); + o = pdf_dict_get(gctx, obj, PDF_NAME(Width)); if (o) width = pdf_to_int(gctx, o); - o = pdf_dict_get(gctx, obj, PDF_NAME_Height); + o = pdf_dict_get(gctx, obj, PDF_NAME(Height)); if (o) height = pdf_to_int(gctx, o); n = fz_colorspace_n(gctx, image->colorspace); @@ -1389,34 +1418,33 @@ if links: { PyObject *xrefs = PyList_New(0); // create Python list - pdf_document *pdf = pdf_specifics(gctx, $self); // conv doc to pdf + pdf_document *pdf = pdf_specifics(gctx, $self); if (!pdf) return xrefs; // not a pdf + pdf_obj *root, *olroot, *first; + int xref_count, olroot_xref, i, xref; + // get the main root - root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root); + root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root)); // get the outline root - olroot = pdf_dict_get(gctx, root, PDF_NAME_Outlines); + olroot = pdf_dict_get(gctx, root, PDF_NAME(Outlines)); if (!olroot) return xrefs; // no outlines or some problem - int objcount, argc, i; - int *res; - objcount = 0; - argc = 0; - first = pdf_dict_get(gctx, olroot, PDF_NAME_First); // first outline - if (!first) return xrefs; - argc = countOutlines(gctx, first, argc); // get number of outlines - if (argc < 1) return xrefs; - res = JM_Alloc(int, (size_t) argc); // object number table - objcount = fillOLNumbers(gctx, res, first, objcount, argc); // fill table - pdf_dict_del(gctx, olroot, PDF_NAME_First); - pdf_dict_del(gctx, olroot, PDF_NAME_Last); - pdf_dict_del(gctx, olroot, PDF_NAME_Count); - - for (i = 0; i < objcount; i++) - { - pdf_delete_object(gctx, pdf, res[i]); // delete outline item - PyList_Append(xrefs, PyInt_FromLong((long) res[i])); - } - JM_Free(res); + + first = pdf_dict_get(gctx, olroot, PDF_NAME(First)); // first outline + + xrefs = JM_outline_xrefs(gctx, first, xrefs); + xref_count = (int) PyList_Size(xrefs); + + olroot_xref = pdf_to_num(gctx, olroot); // delete OL root + pdf_delete_object(gctx, pdf, olroot_xref); // delete OL root + pdf_dict_del(gctx, root, PDF_NAME(Outlines)); // delete OL root + + for (i = 0; i < xref_count; i++) + { + xref = (int) PyInt_AsLong(PyList_GetItem(xrefs, i)); + pdf_delete_object(gctx, pdf, xref); // delete outline item + } + PyList_Append(xrefs, Py_BuildValue("i", olroot_xref)); pdf->dirty = 1; return xrefs; } @@ -1435,10 +1463,10 @@ if links: int have_form = 0; // preset indicator fz_try(gctx) { - form = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root, PDF_NAME_AcroForm, NULL); + form = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root), PDF_NAME(AcroForm), NULL); if (form) // form obj exists { - fields = pdf_dict_get(gctx, form, PDF_NAME_Fields); + fields = pdf_dict_get(gctx, form, PDF_NAME(Fields)); if (fields && pdf_array_len(gctx, fields) > 0) have_form = 1; } } @@ -1460,7 +1488,7 @@ if links: PyObject *liste = PyList_New(0); fz_try(gctx) { - fonts = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root, PDF_NAME_AcroForm, PDF_NAME_DR, PDF_NAME_Font, NULL); + fonts = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(DR), PDF_NAME(Font), NULL); if (fonts && pdf_is_dict(gctx, fonts)) // fonts exist { int i, n = pdf_dict_len(gctx, fonts); @@ -1487,12 +1515,12 @@ if links: pdf_obj *fonts = NULL; fz_try(gctx) { - fonts = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root, - PDF_NAME_AcroForm, PDF_NAME_DR, PDF_NAME_Font, NULL); + fonts = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root), + PDF_NAME(AcroForm), PDF_NAME(DR), PDF_NAME(Font), NULL); if (!fonts || !pdf_is_dict(gctx, fonts)) THROWMSG("PDF has no form fonts yet"); - pdf_obj *k = pdf_new_name(gctx, pdf, (const char *) name); - pdf_obj *v = pdf_new_obj_from_str(gctx, pdf, font); + pdf_obj *k = pdf_new_name(gctx, (const char *) name); + pdf_obj *v = JM_pdf_obj_from_str(gctx, pdf, font); pdf_dict_put(gctx, fonts, k, v); } fz_catch(gctx) NULL; @@ -1512,16 +1540,16 @@ if links: pdf_obj *root, *olroot, *ind_obj; // get main root - root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root); + root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root)); // get outline root - olroot = pdf_dict_get(gctx, root, PDF_NAME_Outlines); + olroot = pdf_dict_get(gctx, root, PDF_NAME(Outlines)); if (olroot == NULL) { olroot = pdf_new_dict(gctx, pdf, 4); - pdf_dict_put(gctx, olroot, PDF_NAME_Type, PDF_NAME_Outlines); + pdf_dict_put(gctx, olroot, PDF_NAME(Type), PDF_NAME(Outlines)); ind_obj = pdf_add_object(gctx, pdf, olroot); - pdf_dict_put(gctx, root, PDF_NAME_Outlines, ind_obj); - olroot = pdf_dict_get(gctx, root, PDF_NAME_Outlines); + pdf_dict_put(gctx, root, PDF_NAME(Outlines), ind_obj); + olroot = pdf_dict_get(gctx, root, PDF_NAME(Outlines)); pdf_drop_obj(gctx, ind_obj); pdf->dirty = 1; } @@ -1566,7 +1594,7 @@ if links: fz_try(gctx) { assert_PDF(pdf); - pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root); + pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root)); if (!root) THROWMSG("could not load root object"); xml = pdf_dict_gets(gctx, root, "Metadata"); if (xml) xref = pdf_to_num(gctx, xml); @@ -1586,7 +1614,7 @@ if links: fz_try(gctx) { assert_PDF(pdf); - pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root); + pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root)); if (root) pdf_dict_dels(gctx, root, "Metadata"); } fz_catch(gctx) return NULL; @@ -1686,7 +1714,7 @@ if links: if (!INRANGE(xref, 1, xreflen-1)) THROWMSG("xref out of range"); // create new object with passed-in string - new_obj = pdf_new_obj_from_str(gctx, pdf, text); + new_obj = JM_pdf_obj_from_str(gctx, pdf, text); pdf_update_object(gctx, pdf, xref, new_obj); pdf_drop_obj(gctx, new_obj); if (page) refresh_link_table(gctx, pdf_page_from_fz_page(gctx, page)); @@ -1747,12 +1775,12 @@ if links: fz_try(gctx) { assert_PDF(pdf); // create new /Info object based on passed-in string - new_info = pdf_new_obj_from_str(gctx, pdf, text); + new_info = JM_pdf_obj_from_str(gctx, pdf, text); } fz_catch(gctx) return NULL; pdf->dirty = 1; // replace existing /Info object - info = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Info); + info = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Info)); if (info) { info_num = pdf_to_num(gctx, info); // get xref no of old info @@ -1763,7 +1791,7 @@ if links: // create new indirect object from /Info object new_info_ind = pdf_add_object(gctx, pdf, new_info); // put this in the trailer dictionary - pdf_dict_put_drop(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Info, new_info_ind); + pdf_dict_put_drop(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Info), new_info_ind); return NONE; } @@ -1932,23 +1960,23 @@ struct fz_page_s { //--------------------------------------------------------------------- // bound() //--------------------------------------------------------------------- - struct fz_rect_s *bound() { - fz_rect *rect = JM_Alloc(fz_rect, 1); - fz_bound_page(gctx, $self, rect); - return rect; + %pythonappend bound %{val = Rect(val)%} + PyObject *bound() { + fz_rect rect = fz_bound_page(gctx, $self); + return JM_py_from_rect(rect); } %pythoncode %{rect = property(bound, doc="page rectangle")%} //--------------------------------------------------------------------- // run() //--------------------------------------------------------------------- - FITZEXCEPTION(run, result) + FITZEXCEPTION(run, !result) PARENTCHECK(run) - int run(struct DeviceWrapper *dw, const struct fz_matrix_s *m) + PyObject *run(struct DeviceWrapper *dw, PyObject *m) { - fz_try(gctx) fz_run_page(gctx, $self, dw->device, m, NULL); - fz_catch(gctx) return 1; - return 0; + fz_try(gctx) fz_run_page(gctx, $self, dw->device, JM_matrix_from_py(m), NULL); + fz_catch(gctx) return NULL; + return NONE; } //--------------------------------------------------------------------- @@ -1957,24 +1985,21 @@ struct fz_page_s { FITZEXCEPTION(getSVGimage, !result) %feature("autodoc","Create an SVG image from the page as a string.") getSVGimage; PARENTCHECK(getSVGimage) - PyObject *getSVGimage(struct fz_matrix_s *matrix = NULL) + PyObject *getSVGimage(PyObject *matrix = NULL) { - fz_rect mediabox; - fz_bound_page(gctx, $self, &mediabox); + fz_rect mediabox = fz_bound_page(gctx, $self); fz_device *dev = NULL; fz_buffer *res = NULL; PyObject *text = NULL; - fz_matrix *ctm = matrix; - if (!matrix) ctm = (fz_matrix *) &fz_identity; - fz_rect tbounds; + fz_matrix ctm = JM_matrix_from_py(matrix); fz_cookie *cookie = NULL; fz_output *out = NULL; fz_separations *seps = NULL; fz_var(out); fz_var(dev); fz_var(res); - tbounds = mediabox; - fz_transform_rect(&tbounds, ctm); + fz_rect tbounds = mediabox; + tbounds = fz_transform_rect(tbounds, ctm); fz_try(gctx) { @@ -2001,15 +2026,15 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addLineAnnot //--------------------------------------------------------------------- - ANNOTWRAP2(addLineAnnot, "Add 'Line' annot for points p1 and p2.") - struct fz_annot_s *addLineAnnot(struct fz_point_s *p1, struct fz_point_s *p2) + ANNOTWRAP1(addLineAnnot, "Add 'Line' annot for points p1 and p2.") + struct fz_annot_s *addLineAnnot(PyObject *p1, PyObject *p2) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); pdf_annot *annot = NULL; float col[3] = {0, 0, 0}; float width = 1.0f; - fz_point a = {p1->x, p1->y}; - fz_point b = {p2->x, p2->y}; + fz_point a = JM_point_from_py(p1); + fz_point b = JM_point_from_py(p2); fz_rect r = {MIN(a.x, b.x), MIN(a.y, b.y), MAX(a.x, b.x), MAX(a.y, b.y)}; fz_var(annot); fz_try(gctx) @@ -2020,8 +2045,9 @@ struct fz_page_s { pdf_set_annot_line(gctx, annot, a, b); pdf_set_annot_border(gctx, annot, width); pdf_set_annot_color(gctx, annot, 3, col); - fz_expand_rect(&r, 3 * width); - pdf_set_annot_rect(gctx, annot, &r); + r = fz_expand_rect(r, 3 * width); + pdf_set_annot_rect(gctx, annot, r); + pdf_update_annot(gctx, annot); } fz_catch(gctx) return NULL; fz_annot *fzannot = (fz_annot *) annot; @@ -2032,12 +2058,11 @@ struct fz_page_s { // page addTextAnnot //--------------------------------------------------------------------- ANNOTWRAP1(addTextAnnot, "Add a 'sticky note' at position 'point'.") - struct fz_annot_s *addTextAnnot(struct fz_point_s *point, char *text) + struct fz_annot_s *addTextAnnot(PyObject *point, char *text) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); pdf_annot *annot = NULL; - char *name = "Note"; - fz_point pos = {point->x, point->y}; + fz_point pos = JM_point_from_py(point); fz_var(annot); fz_try(gctx) { @@ -2045,10 +2070,105 @@ struct fz_page_s { annot = pdf_create_annot(gctx, page, PDF_ANNOT_TEXT); pdf_set_text_annot_position(gctx, annot, pos); pdf_set_annot_contents(gctx, annot, text); - pdf_set_annot_icon_name(gctx, annot, name); - pdf_update_appearance(gctx, annot); + pdf_set_annot_icon_name(gctx, annot, "Note"); + pdf_update_annot(gctx, annot); + } + fz_catch(gctx) return NULL; + fz_annot *fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); + } + + //--------------------------------------------------------------------- + // page addInkAnnot + //--------------------------------------------------------------------- + ANNOTWRAP1(addInkAnnot, "Add a 'handwriting' as a list of list of point-likes. Each sublist forms an independent stroke.") + struct fz_annot_s *addInkAnnot(PyObject *list) + { + pdf_page *page = pdf_page_from_fz_page(gctx, $self); + pdf_annot *annot = NULL; + PyObject *p = NULL, *sublist = NULL; + pdf_obj *inklist = NULL, *stroke = NULL; + double x, y, height; + fz_rect prect; + fz_var(annot); + fz_try(gctx) + { + assert_PDF(page); + if (!PySequence_Check(list)) THROWMSG("arg must be a sequence"); + prect = pdf_bound_page(gctx, page); + height = prect.y1 - prect.y0; + annot = pdf_create_annot(gctx, page, PDF_ANNOT_INK); + Py_ssize_t i, j, n0 = PySequence_Size(list), n1; + inklist = pdf_new_array(gctx, annot->page->doc, n0); + for (j = 0; j < n0; j++) + { + sublist = PySequence_GetItem(list, j); + n1 = PySequence_Size(sublist); + stroke = pdf_new_array(gctx, annot->page->doc, 2 * n1); + for (i = 0; i < n1; i++) + { + p = PySequence_GetItem(sublist, i); + if (!PySequence_Check(p) || PySequence_Size(p) != 2) + THROWMSG("3rd level entries must be pairs of floats"); + x = PyFloat_AsDouble(PySequence_GetItem(p, 0)); + if (PyErr_Occurred()) + THROWMSG("invalid point coordinate"); + y = PyFloat_AsDouble(PySequence_GetItem(p, 1)); + if (PyErr_Occurred()) + THROWMSG("invalid point coordinate"); + Py_CLEAR(p); + pdf_array_push_real(gctx, stroke, x); + pdf_array_push_real(gctx, stroke, height - y); + } + pdf_array_push_drop(gctx, inklist, stroke); + stroke = NULL; + Py_CLEAR(sublist); + } + pdf_dict_put_drop(gctx, annot->obj, PDF_NAME(InkList), inklist); + inklist = NULL; pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, page); + pdf_update_annot(gctx, annot); + } + fz_catch(gctx) + { + Py_CLEAR(p); + Py_CLEAR(sublist); + return NULL; + } + + fz_annot *fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); + } + + //--------------------------------------------------------------------- + // page addStampAnnot + //--------------------------------------------------------------------- + ANNOTWRAP1(addStampAnnot, "Add a 'rubber stamp' in a rectangle.") + struct fz_annot_s *addStampAnnot(PyObject *rect, int stamp = 0) + { + pdf_page *page = pdf_page_from_fz_page(gctx, $self); + pdf_annot *annot = NULL; + pdf_obj *stamp_id[] = {PDF_NAME(Approved), PDF_NAME(AsIs), + PDF_NAME(Confidential), PDF_NAME(Departmental), + PDF_NAME(Experimental), PDF_NAME(Expired), + PDF_NAME(Final), PDF_NAME(ForComment), + PDF_NAME(ForPublicRelease), PDF_NAME(NotApproved), + PDF_NAME(NotForPublicRelease), PDF_NAME(Sold), + PDF_NAME(TopSecret), PDF_NAME(Draft)}; + int n = nelem(stamp_id); + pdf_obj *name = stamp_id[0]; + fz_var(annot); + fz_try(gctx) + { + assert_PDF(page); + if (INRANGE(stamp, 0, n-1)) + name = stamp_id[stamp]; + annot = pdf_create_annot(gctx, page, PDF_ANNOT_STAMP); + pdf_set_annot_rect(gctx, annot, JM_rect_from_py(rect)); + pdf_dict_put(gctx, annot->obj, PDF_NAME(Name), name); + pdf_set_annot_contents(gctx, annot, + pdf_dict_get_name(gctx, annot->obj, PDF_NAME(Name))); + pdf_update_annot(gctx, annot); } fz_catch(gctx) return NULL; fz_annot *fzannot = (fz_annot *) annot; @@ -2058,8 +2178,8 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addFileAnnot //--------------------------------------------------------------------- - ANNOTWRAP1(addFileAnnot, "Add a 'FileAttachment' annotation.") - struct fz_annot_s *addFileAnnot(struct fz_point_s *point, PyObject *buffer, char *filename, char *ufilename = NULL, char *desc = NULL) + ANNOTWRAP1(addFileAnnot, "Add a 'FileAttachment' annotation at location 'point'.") + struct fz_annot_s *addFileAnnot(PyObject *point, PyObject *buffer, char *filename, char *ufilename = NULL, char *desc = NULL) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); fz_annot *fzannot = NULL; @@ -2069,23 +2189,22 @@ struct fz_page_s { if (!desc) d = filename; size_t len = 0; fz_buffer *filebuf = NULL; - fz_rect r = {point->x, point->y, point->x + 20, point->y + 30}; + fz_point p = JM_point_from_py(point); + fz_rect r = {p.x, p.y, p.x + 20, p.y + 30}; fz_var(annot); fz_try(gctx) { assert_PDF(page); annot = pdf_create_annot(gctx, page, ANNOT_FILEATTACHMENT); - pdf_set_annot_rect(gctx, annot, &r); + pdf_set_annot_rect(gctx, annot, r); pdf_set_annot_icon_name(gctx, annot, "PushPin"); len = JM_CharFromBytesOrArray(buffer, &data); filebuf = fz_new_buffer_from_shared_data(gctx, data, len); pdf_obj *val = JM_embed_file(gctx, page->doc, filebuf, filename, uf, d); - pdf_dict_put(gctx, annot->obj, PDF_NAME_FS, val); - pdf_dict_put_text_string(gctx, annot->obj, PDF_NAME_Contents, filename); - JM_update_file_attachment_annot(gctx, page->doc, annot); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, page); + pdf_dict_put(gctx, annot->obj, PDF_NAME(FS), val); + pdf_dict_put_text_string(gctx, annot->obj, PDF_NAME(Contents), filename); + pdf_update_annot(gctx, annot); } fz_catch(gctx) return NULL; fzannot = (fz_annot *) annot; @@ -2095,16 +2214,17 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addStrikeoutAnnot //--------------------------------------------------------------------- - ANNOTWRAP1(addStrikeoutAnnot, "Strike out content in a rectangle.") - struct fz_annot_s *addStrikeoutAnnot(struct fz_rect_s *rect) + ANNOTWRAP1(addStrikeoutAnnot, "Strike out content in a rectangle or quadrilateral.") + struct fz_annot_s *addStrikeoutAnnot(PyObject *rect) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); fz_annot *annot = NULL; fz_var(annot); + fz_quad quad = JM_quad_from_py(rect); fz_try(gctx) { assert_PDF(page); - annot = JM_AnnotTextmarker(gctx, page, rect, PDF_ANNOT_STRIKE_OUT); + annot = JM_AnnotTextmarker(gctx, page, quad, PDF_ANNOT_STRIKE_OUT); } fz_catch(gctx) return NULL; return fz_keep_annot(gctx, annot); @@ -2113,16 +2233,36 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addUnderlineAnnot //--------------------------------------------------------------------- - ANNOTWRAP1(addUnderlineAnnot, "Underline content in a rectangle.") - struct fz_annot_s *addUnderlineAnnot(struct fz_rect_s *rect) + ANNOTWRAP1(addUnderlineAnnot, "Underline content in a rectangle or quadrilateral.") + struct fz_annot_s *addUnderlineAnnot(PyObject *rect) + { + pdf_page *page = pdf_page_from_fz_page(gctx, $self); + fz_annot *annot = NULL; + fz_var(annot); + fz_quad quad = JM_quad_from_py(rect); + fz_try(gctx) + { + assert_PDF(page); + annot = JM_AnnotTextmarker(gctx, page, quad, PDF_ANNOT_UNDERLINE); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, annot); + } + + //--------------------------------------------------------------------- + // page addSquigglyAnnot + //--------------------------------------------------------------------- + ANNOTWRAP1(addSquigglyAnnot, "Wavy underline content in a rectangle or quadrilateral.") + struct fz_annot_s *addSquigglyAnnot(PyObject *rect) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); fz_annot *annot = NULL; fz_var(annot); + fz_quad quad = JM_quad_from_py(rect); fz_try(gctx) { assert_PDF(page); - annot = JM_AnnotTextmarker(gctx, page, rect, PDF_ANNOT_UNDERLINE); + annot = JM_AnnotTextmarker(gctx, page, quad, PDF_ANNOT_SQUIGGLY); } fz_catch(gctx) return NULL; return fz_keep_annot(gctx, annot); @@ -2131,16 +2271,17 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addHighlightAnnot //--------------------------------------------------------------------- - ANNOTWRAP1(addHighlightAnnot, "Highlight content in a rectangle.") - struct fz_annot_s *addHighlightAnnot(struct fz_rect_s *rect) + ANNOTWRAP1(addHighlightAnnot, "Highlight content in a rectangle or quadrilateral.") + struct fz_annot_s *addHighlightAnnot(PyObject *rect) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); fz_annot *annot = NULL; fz_var(annot); + fz_quad quad = JM_quad_from_py(rect); fz_try(gctx) { assert_PDF(page); - annot = JM_AnnotTextmarker(gctx, page, rect, PDF_ANNOT_HIGHLIGHT); + annot = JM_AnnotTextmarker(gctx, page, quad, PDF_ANNOT_HIGHLIGHT); } fz_catch(gctx) return NULL; return fz_keep_annot(gctx, annot); @@ -2149,8 +2290,8 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addRectAnnot //--------------------------------------------------------------------- - ANNOTWRAP2(addRectAnnot, "Add a 'Rectangle' annotation.") - struct fz_annot_s *addRectAnnot(struct fz_rect_s *rect) + ANNOTWRAP1(addRectAnnot, "Add a 'Rectangle' annotation.") + struct fz_annot_s *addRectAnnot(PyObject *rect) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); fz_annot *fzannot = NULL; @@ -2167,8 +2308,8 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addCircleAnnot //--------------------------------------------------------------------- - ANNOTWRAP2(addCircleAnnot, "Add a 'Circle' annotation.") - struct fz_annot_s *addCircleAnnot(struct fz_rect_s *rect) + ANNOTWRAP1(addCircleAnnot, "Add a 'Circle' annotation.") + struct fz_annot_s *addCircleAnnot(PyObject *rect) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); fz_annot *fzannot = NULL; @@ -2185,7 +2326,7 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addPolylineAnnot //--------------------------------------------------------------------- - ANNOTWRAP2(addPolylineAnnot, "Add a 'Polyline' annotation for a sequence of points.") + ANNOTWRAP1(addPolylineAnnot, "Add a 'Polyline' annotation for a sequence of points.") struct fz_annot_s *addPolylineAnnot(PyObject *points) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); @@ -2203,7 +2344,7 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addPolygonAnnot //--------------------------------------------------------------------- - ANNOTWRAP2(addPolygonAnnot, "Add a 'Polygon' annotation for a sequence of points.") + ANNOTWRAP1(addPolygonAnnot, "Add a 'Polygon' annotation for a sequence of points.") struct fz_annot_s *addPolygonAnnot(PyObject *points) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); @@ -2221,34 +2362,32 @@ struct fz_page_s { //--------------------------------------------------------------------- // page addFreetextAnnot //--------------------------------------------------------------------- - ANNOTWRAP1(addFreetextAnnot, "Add a 'FreeText' annotation at position 'point'.") - struct fz_annot_s *addFreetextAnnot(struct fz_point_s *pos, char *text, float fontsize = 11, PyObject *color = NULL) + ANNOTWRAP1(addFreetextAnnot, "Add a 'FreeText' annotation in rectangle 'rect'.") + struct fz_annot_s *addFreetextAnnot(PyObject *rect, char *text, float fontsize = 12, char *fontname = NULL, PyObject *color = NULL, int rotate = 0) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); - float col[3] = {0, 0, 0}; - if (color && PySequence_Check(color) && PySequence_Size(color) == 3) - { - col[0] = (float) PyFloat_AsDouble(PySequence_GetItem(color, 0)); - col[1] = (float) PyFloat_AsDouble(PySequence_GetItem(color, 1)); - col[2] = (float) PyFloat_AsDouble(PySequence_GetItem(color, 2)); - } - char *fname = "Helvetica"; + float bcol[3] = {1.0f, 1.0f, 1.0f}; // border, box color: white + float col[4] = {0.0f, 0.0f, 0.0f, 0.0f}; // std. text color: black + int ncol = 3; + JM_color_FromSequence(color, &ncol, col); + fz_rect r = JM_rect_from_py(rect); pdf_annot *annot = NULL; - char *ascii = JM_ASCIIFromChar(text); - fz_var(ascii); fz_var(annot); fz_try(gctx) { assert_PDF(page); - pdf_document *pdf = page->doc; annot = pdf_create_annot(gctx, page, PDF_ANNOT_FREE_TEXT); - pdf_set_free_text_details(gctx, annot, pos, - ascii, fname, fontsize, col); - pdf_update_free_text_annot_appearance(gctx, pdf, annot); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, page); + pdf_set_annot_contents(gctx, annot, text); + pdf_set_annot_color(gctx, annot, 3, bcol); // set rect colors + pdf_dict_put_int(gctx, annot->obj, PDF_NAME(Rotate), rotate); + + pdf_set_text_annot_position(gctx, annot, fz_make_point(r.x0, r.y0)); + pdf_set_annot_rect(gctx, annot, r); + // insert the default appearance string + JM_make_annot_DA(gctx, annot, ncol, col, fontname, fontsize); + pdf_update_annot(gctx, annot); } - fz_always(gctx) JM_Free(ascii); + fz_always(gctx) {;} fz_catch(gctx) return NULL; fz_annot *fzannot = (fz_annot *) annot; return fz_keep_annot(gctx, fzannot); @@ -2359,22 +2498,23 @@ struct fz_page_s { //--------------------------------------------------------------------- FITZEXCEPTION(setCropBox, !result) PARENTCHECK(setCropBox) - PyObject *setCropBox(struct fz_rect_s *rect = NULL) + PyObject *setCropBox(PyObject *rect) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); fz_try(gctx) { assert_PDF(page); - if (!rect) THROWMSG("rect must be given"); - fz_rect mediabox = {0,0,0,0}; - fz_rect cropbox = {0,0,0,0}; - pdf_to_rect(gctx, pdf_dict_get(gctx, page->obj, PDF_NAME_MediaBox), &mediabox); - cropbox.x0 = rect->x0; - cropbox.y0 = mediabox.y1 - rect->y1; - cropbox.x1 = rect->x1; - cropbox.y1 = mediabox.y1 - rect->y0; - pdf_dict_put_drop(gctx, page->obj, PDF_NAME_CropBox, - pdf_new_rect(gctx, page->doc, &cropbox)); + fz_rect mediabox = pdf_bound_page(gctx, page); + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(MediaBox)); + if (o) mediabox = pdf_to_rect(gctx, o); + fz_rect cropbox = fz_empty_rect; + fz_rect r = JM_rect_from_py(rect); + cropbox.x0 = r.x0; + cropbox.y0 = mediabox.y1 - r.y1; + cropbox.x1 = r.x1; + cropbox.y1 = mediabox.y1 - r.y0; + pdf_dict_put_drop(gctx, page->obj, PDF_NAME(CropBox), + pdf_new_rect(gctx, page->doc, cropbox)); } fz_catch(gctx) return NULL; page->doc->dirty = 1; @@ -2385,11 +2525,16 @@ struct fz_page_s { // loadLinks() //--------------------------------------------------------------------- PARENTCHECK(loadLinks) - %pythonappend loadLinks -%{if val: - val.thisown = True - val.parent = weakref.proxy(self) # owning page object - self._annot_refs[id(val)] = val%} + %pythonappend loadLinks %{ + if val: + val.thisown = True + val.parent = weakref.proxy(self) # owning page object + self._annot_refs[id(val)] = val + if self.parent.isPDF: + val.xref = self._getLinkXrefs()[0] + else: + val.xref = 0 + %} struct fz_link_s *loadLinks() { fz_link *l = NULL; @@ -2441,7 +2586,7 @@ except: if (!page) return; // have no PDF int xref = (int) PyInt_AsLong(PyDict_GetItemString(linkdict, "xref")); if (xref < 1) return; // invalid xref - pdf_obj *annots = pdf_dict_get(gctx, page->obj, PDF_NAME_Annots); + pdf_obj *annots = pdf_dict_get(gctx, page->obj, PDF_NAME(Annots)); if (!annots) return; // have no annotations int len = pdf_array_len(gctx, annots); int i, oxref = 0; @@ -2453,7 +2598,7 @@ except: if (xref != oxref) return; // xref not in annotations pdf_array_delete(gctx, annots, i); // delete entry in annotations pdf_delete_object(gctx, page->doc, xref); // delete link object - pdf_dict_put(gctx, page->obj, PDF_NAME_Annots, annots); + pdf_dict_put(gctx, page->obj, PDF_NAME(Annots), annots); refresh_link_table(gctx, page); // reload link / annot tables page->doc->dirty = 1; return; @@ -2495,23 +2640,22 @@ fannot._erase() %pythoncode %{@property%} %feature("autodoc","Retrieve width, height of /MediaBox.") MediaBoxSize; %pythonappend MediaBoxSize %{ - if val == Point(0,0): + val = Point(val) + if not bool(val): r = self.rect val = Point(r.width, r.height) %} - struct fz_point_s *MediaBoxSize() + PyObject *MediaBoxSize() { - fz_point *p = JM_Alloc(fz_point, 1); - p->x = p->y = 0.0; + PyObject *p = JM_py_from_point(fz_make_point(0, 0)); pdf_page *page = pdf_page_from_fz_page(gctx, $self); if (!page) return p; - fz_rect r = {0,0,0,0}; - pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME_MediaBox); + fz_rect r = fz_empty_rect; + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(MediaBox)); if (!o) return p; - pdf_to_rect(gctx, o, &r); - p->x = r.x1 - r.x0; - p->y = r.y1 - r.y0; - return p; + + r = pdf_to_rect(gctx, o); + return JM_py_from_point(fz_make_point(r.x1 - r.x0, r.y1 - r.y0)); } //--------------------------------------------------------------------- @@ -2520,19 +2664,16 @@ fannot._erase() PARENTCHECK(CropBoxPosition) %pythoncode %{@property%} %feature("autodoc","Retrieve position of /CropBox. Return (0,0) for non-PDF, or no /CropBox.") CropBoxPosition; - struct fz_point_s *CropBoxPosition() + %pythonappend CropBoxPosition %{val = Point(val)%} + PyObject *CropBoxPosition() { - fz_point *p = JM_Alloc(fz_point, 1); - p->x = p->y = 0.0; + PyObject *p = JM_py_from_point(fz_make_point(0, 0)); pdf_page *page = pdf_page_from_fz_page(gctx, $self); if (!page) return p; // not a PDF - fz_rect cbox = {0,0,0,0}; - pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME_CropBox); + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(CropBox)); if (!o) return p; // no CropBox specified - pdf_to_rect(gctx, o, &cbox); - p->x = cbox.x0; - p->y = cbox.y0; - return p; + fz_rect cbox = pdf_to_rect(gctx, o); + return JM_py_from_point(fz_make_point(cbox.x0, cbox.y0));; } //--------------------------------------------------------------------- @@ -2545,7 +2686,7 @@ fannot._erase() { pdf_page *page = pdf_page_from_fz_page(gctx, $self); if (!page) return -1; - pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME_Rotate); + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(Rotate)); if (!o) return 0; return pdf_to_int(gctx, o); } @@ -2563,7 +2704,7 @@ fannot._erase() pdf_page *page = pdf_page_from_fz_page(gctx, $self); assert_PDF(page); if (rot % 90) THROWMSG("rotate not 90 * int"); - pdf_dict_put_int(gctx, page->obj, PDF_NAME_Rotate, (int64_t) rot); + pdf_dict_put_int(gctx, page->obj, PDF_NAME(Rotate), (int64_t) rot); page->doc->dirty = 1; } fz_catch(gctx) return NULL; @@ -2589,7 +2730,7 @@ fannot._erase() { assert_PDF(page); // make sure we have a PDF // get existing annots array - annots = pdf_dict_get(gctx, page->obj, PDF_NAME_Annots); + annots = pdf_dict_get(gctx, page->obj, PDF_NAME(Annots)); if (annots) { if (pdf_is_indirect(gctx, annots)) @@ -2619,7 +2760,7 @@ fannot._erase() txtpy = PySequence_ITEM(linklist, (Py_ssize_t) i); text = JM_Python_str_AsChar(txtpy); if (!text) THROWMSG("non-string linklist item"); - annot = pdf_new_obj_from_str(gctx, page->doc, text); + annot = JM_pdf_obj_from_str(gctx, page->doc, text); JM_Python_str_DelForPy3(text); ind_obj = pdf_add_object(gctx, page->doc, annot); pdf_array_push_drop(gctx, new_array, ind_obj); @@ -2637,7 +2778,7 @@ fannot._erase() } fz_try(gctx) { - pdf_dict_put_drop(gctx, page->obj, PDF_NAME_Annots, new_array); + pdf_dict_put_drop(gctx, page->obj, PDF_NAME(Annots), new_array); refresh_link_table(gctx, page); } fz_catch(gctx) return NULL; @@ -2645,18 +2786,18 @@ fannot._erase() return NONE; } - /*********************************************************************/ - // Page._getLinkXrefs - return list of link xref numbers. - /*********************************************************************/ - + //--------------------------------------------------------------------- + // Page._getLinkXrefs - get list of link xref numbers. + // Return None for non-PDF + //--------------------------------------------------------------------- PyObject *_getLinkXrefs() { pdf_obj *annots, *annots_arr, *link, *obj; int i, lcount; pdf_page *page = pdf_page_from_fz_page(gctx, $self); PyObject *linkxrefs = PyList_New(0); - if (!page) return linkxrefs; // empty list if not PDF - annots = pdf_dict_get(gctx, page->obj, PDF_NAME_Annots); + if (!page) return linkxrefs; // empty list for non-PDF + annots = pdf_dict_get(gctx, page->obj, PDF_NAME(Annots)); if (!annots) return linkxrefs; if (pdf_is_indirect(gctx, annots)) annots_arr = pdf_resolve_indirect(gctx, annots); @@ -2664,15 +2805,12 @@ fannot._erase() annots_arr = annots; lcount = pdf_array_len(gctx, annots_arr); for (i = 0; i < lcount; i++) - { - link = pdf_array_get(gctx, annots_arr, i); - obj = pdf_dict_get(gctx, link, PDF_NAME_Subtype); - if (pdf_name_eq(gctx, obj, PDF_NAME_Link)) - { - int xref = pdf_to_num(gctx, link); - PyList_Append(linkxrefs, PyInt_FromLong((long) xref)); - } - } + { + link = pdf_array_get(gctx, annots_arr, i); + obj = pdf_dict_get(gctx, link, PDF_NAME(Subtype)); + if (pdf_name_eq(gctx, obj, PDF_NAME(Link))) + PyList_Append(linkxrefs, Py_BuildValue("i", pdf_to_num(gctx, link))); + } return linkxrefs; } @@ -2703,18 +2841,21 @@ fannot._erase() // Show a PDF page //--------------------------------------------------------------------- FITZEXCEPTION(_showPDFpage, result<0) - int _showPDFpage(struct fz_rect_s *rect, struct fz_document_s *docsrc, int pno=0, int overlay=1, int keep_proportion=1, int reuse_xref=0, struct fz_rect_s *clip = NULL, struct pdf_graft_map_s *graftmap = NULL, char *_imgname = NULL) + int _showPDFpage(PyObject *rect, struct fz_document_s *docsrc, int pno=0, int overlay=1, int keep_proportion=1, int reuse_xref=0, PyObject *clip = NULL, struct pdf_graft_map_s *graftmap = NULL, char *_imgname = NULL) { int xref; xref = reuse_xref; pdf_obj *xobj1, *xobj2, *resources, *o; fz_buffer *res, *nres; - fz_rect mediabox = {0,0,0,0}; - fz_rect cropbox = {0,0,0,0}; + fz_rect mediabox; + fz_rect cropbox; + fz_rect rrect = JM_rect_from_py(rect); fz_try(gctx) { pdf_page *tpage = pdf_page_from_fz_page(gctx, $self); assert_PDF(tpage); + if (fz_is_infinite_rect(rrect) || fz_is_empty_rect(rrect)) + THROWMSG("rect must be finite and not empty"); pdf_obj *tpageref = tpage->obj; pdf_document *pdfout = tpage->doc; // target PDF pdf_document *pdfsrc = pdf_specifics(gctx, docsrc); @@ -2730,39 +2871,42 @@ fannot._erase() //------------------------------------------------------------- // Calculate /Matrix and /BBox of the referencing XObject //------------------------------------------------------------- - if (clip) - { // set cropbox if clip given - cropbox.x0 = clip->x0; - cropbox.y0 = mediabox.y1 - clip->y1; - cropbox.x1 = clip->x1; - cropbox.y1 = mediabox.y1 - clip->y0; + fz_rect rclip = JM_rect_from_py(clip); + if (!fz_is_infinite_rect(rclip)) // set cropbox if clip given + { + cropbox.x0 = rclip.x0; + cropbox.y0 = mediabox.y1 - rclip.y1; + cropbox.x1 = rclip.x1; + cropbox.y1 = mediabox.y1 - rclip.y0; } - fz_matrix mat = {1, 0, 0, 1, 0, 0}; - fz_rect prect = {0, 0, 0, 0}; - fz_rect r = {0, 0, 0, 0}; - fz_bound_page(gctx, $self, &prect); - o = pdf_dict_get(gctx, tpageref, PDF_NAME_CropBox); - pdf_to_rect(gctx, o, &r); + fz_matrix mat = fz_identity; + fz_rect prect = fz_bound_page(gctx, $self); + fz_rect r = fz_empty_rect; + o = pdf_dict_get(gctx, tpageref, PDF_NAME(CropBox)); + if (o) { + r = pdf_to_rect(gctx, o); prect.x0 = r.x0; prect.y0 = r.y0; } - o = pdf_dict_get(gctx, tpageref, PDF_NAME_MediaBox); - pdf_to_rect(gctx, o, &r); + o = pdf_dict_get(gctx, tpageref, PDF_NAME(MediaBox)); + if (o) { + r = pdf_to_rect(gctx, o); prect.x1 = r.x1; prect.y1 = r.y1; } - float W = rect->x1 - rect->x0; - float H = rect->y1 - rect->y0; + + float W = rrect.x1 - rrect.x0; + float H = rrect.y1 - rrect.y0; float fw = W / (cropbox.x1 - cropbox.x0); float fh = H / (cropbox.y1 - cropbox.y0); if ((fw < fh) && keep_proportion) // zoom factors in matrix fh = fw; - float X = rect->x0 + prect.x0 - fw*cropbox.x0; - float Y = prect.y1 - (rect->y1 + prect.y0 + fh*cropbox.y0); + float X = rrect.x0 + prect.x0 - fw*cropbox.x0; + float Y = prect.y1 - (rrect.y1 + prect.y0 + fh*cropbox.y0); mat.a = fw; mat.d = fh; mat.e = X; @@ -2771,19 +2915,19 @@ fannot._erase() //------------------------------------------------------------- // create referencing XObject (controls actual display) //------------------------------------------------------------- - xobj2 = pdf_new_xobject(gctx, pdfout, &cropbox, &mat); - // fill reference to xobj1 into its /Resources - o = pdf_xobject_resources(gctx, xobj2); - pdf_obj *subres = pdf_new_dict(gctx, pdfout, 10); - pdf_dict_put(gctx, o, PDF_NAME_XObject, subres); - pdf_dict_puts(gctx, subres, "fullpage", xobj1); - pdf_drop_obj(gctx, subres); + //------------------------------------------------------------- + pdf_obj *subres1 = pdf_new_dict(gctx, pdfout, 5); + pdf_dict_puts(gctx, subres1, "fullpage", xobj1); + pdf_obj *subres = pdf_new_dict(gctx, pdfout, 5); + pdf_dict_put_drop(gctx, subres, PDF_NAME(XObject), subres1); - // xobj2 invokes xobj1 via one statement res = fz_new_buffer(gctx, 50); fz_append_string(gctx, res, "/fullpage Do"); - pdf_update_xobject_contents(gctx, pdfout, xobj2, res); + + xobj2 = pdf_new_xobject(gctx, pdfout, cropbox, mat, subres, res); + + pdf_drop_obj(gctx, subres); fz_drop_buffer(gctx, res); //------------------------------------------------------------- @@ -2791,12 +2935,12 @@ fannot._erase() //------------------------------------------------------------- // 1. resources object //------------------------------------------------------------- - resources = pdf_dict_get(gctx, tpageref, PDF_NAME_Resources); - subres = pdf_dict_get(gctx, resources, PDF_NAME_XObject); + resources = pdf_dict_get(gctx, tpageref, PDF_NAME(Resources)); + subres = pdf_dict_get(gctx, resources, PDF_NAME(XObject)); if (!subres) // has no XObject dict yet: create one { subres = pdf_new_dict(gctx, pdfout, 10); - pdf_dict_put(gctx, resources, PDF_NAME_XObject, subres); + pdf_dict_put(gctx, resources, PDF_NAME(XObject), subres); } pdf_dict_puts(gctx, subres, _imgname, xobj2); @@ -2831,7 +2975,7 @@ fannot._erase() i += 1 _imgname = n + str(i)%} %feature("autodoc", "Insert a new image into a rectangle.") insertImage; - PyObject *insertImage(struct fz_rect_s *rect, const char *filename=NULL, struct fz_pixmap_s *pixmap = NULL, PyObject *stream = NULL, int overlay = 1, + PyObject *insertImage(PyObject *rect, const char *filename=NULL, struct fz_pixmap_s *pixmap = NULL, PyObject *stream = NULL, int overlay = 1, char *_imgname = NULL) { pdf_page *page = pdf_page_from_fz_page(gctx, $self); @@ -2859,41 +3003,44 @@ fannot._erase() assert_PDF(page); if (parm_count != 1) THROWMSG("need exactly one of filename, pixmap or stream"); - if (fz_is_empty_rect(rect) || fz_is_infinite_rect(rect)) + + fz_rect rect_py = JM_rect_from_py(rect); + if (fz_is_empty_rect(rect_py) || fz_is_infinite_rect(rect_py)) THROWMSG("rect must be finite and not empty"); + // calculate coordinates for image matrix - fz_rect prect = {0, 0, 0, 0}; // normal page rectangle - fz_bound_page(gctx, $self, &prect); // get page mediabox - fz_rect r = {0, 0, 0, 0}; // modify where necessary - pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME_CropBox); + fz_rect prect = fz_bound_page(gctx, $self); // page rectangle + + fz_rect r = fz_empty_rect; // modify where necessary + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(CropBox)); if (o) { // set top-left of page rect to new values - pdf_to_rect(gctx, o, &r); + r = pdf_to_rect(gctx, o); prect.x0 = r.x0; prect.y0 = r.y0; } - o = pdf_dict_get(gctx, page->obj, PDF_NAME_MediaBox); + o = pdf_dict_get(gctx, page->obj, PDF_NAME(MediaBox)); if (o) { // set bottom-right to new values - pdf_to_rect(gctx, o, &r); + r = pdf_to_rect(gctx, o); prect.x1 = r.x1; prect.y1 = r.y1; } // adjust rect.x0, rect.y0 by CropBox start - float X = rect->x0 + prect.x0; - float Y = prect.y1 - (rect->y1 + prect.y0); - float W = rect->x1 - rect->x0; - float H = rect->y1 - rect->y0; + float X = rect_py.x0 + prect.x0; + float Y = prect.y1 - (rect_py.y1 + prect.y0); + float W = rect_py.x1 - rect_py.x0; + float H = rect_py.y1 - rect_py.y0; pdf = page->doc; // get objects "Resources" & "XObject" - resources = pdf_dict_get(gctx, page->obj, PDF_NAME_Resources); - subres = pdf_dict_get(gctx, resources, PDF_NAME_XObject); + resources = pdf_dict_get(gctx, page->obj, PDF_NAME(Resources)); + subres = pdf_dict_get(gctx, resources, PDF_NAME(XObject)); if (!subres) // has no XObject yet, create one { subres = pdf_new_dict(gctx, pdf, 10); - pdf_dict_put_drop(gctx, resources, PDF_NAME_XObject, subres); + pdf_dict_put_drop(gctx, resources, PDF_NAME(XObject), subres); } // create the image @@ -2999,8 +3146,8 @@ fannot._erase() assert_PDF(page); pdf = page->doc; // get objects "Resources", "Resources/Font" - resources = pdf_dict_get(gctx, page->obj, PDF_NAME_Resources); - fonts = pdf_dict_get(gctx, resources, PDF_NAME_Font); + resources = pdf_dict_get(gctx, page->obj, PDF_NAME(Resources)); + fonts = pdf_dict_get(gctx, resources, PDF_NAME(Font)); int simple = 0; if (!fonts) // page has no fonts yet fonts = pdf_add_object_drop(gctx, pdf, pdf_new_dict(gctx, pdf, 1)); @@ -3053,7 +3200,7 @@ fannot._erase() fz_drop_font(gctx, font); // resources and fonts objects will contain named reference to font pdf_dict_puts(gctx, fonts, fontname, font_obj); - pdf_dict_put(gctx, resources, PDF_NAME_Font, fonts); + pdf_dict_put(gctx, resources, PDF_NAME(Font), fonts); } fz_catch(gctx) return NULL; pdf->dirty = 1; @@ -3075,7 +3222,7 @@ fannot._erase() { assert_PDF(page); list = PyList_New(0); - contents = pdf_dict_get(gctx, page->obj, PDF_NAME_Contents); + contents = pdf_dict_get(gctx, page->obj, PDF_NAME(Contents)); if (pdf_is_array(gctx, contents)) { for (i=0; i < pdf_array_len(gctx, contents); i++) { @@ -3113,7 +3260,7 @@ fannot._erase() contents = pdf_new_indirect(gctx, page->doc, xref, 0); if (!pdf_is_stream(gctx, contents)) THROWMSG("xref is not a stream"); - pdf_dict_put_drop(gctx, page->obj, PDF_NAME_Contents, contents); + pdf_dict_put_drop(gctx, page->obj, PDF_NAME(Contents), contents); } fz_catch(gctx) return NULL; page->doc->dirty = 1; @@ -3152,7 +3299,8 @@ fannot._erase() annot._erase() self._annot_refs.clear() - def _getXref(self): + @property + def xref(self): """Return PDF XREF number of page.""" CheckParent(self) return self.parent._getPageXref(self.number)[0] @@ -3197,516 +3345,70 @@ fannot._erase() }; %clearnodefaultctor; - -/* fz_rect */ -%rename(_fz_transform_rect) fz_transform_rect; -struct fz_rect_s *fz_transform_rect(struct fz_rect_s *restrict rect, const struct fz_matrix_s *restrict transform); -%rename(Rect) fz_rect_s; - -struct fz_rect_s +//----------------------------------------------------------------------------- +// Pixmap +//----------------------------------------------------------------------------- +%rename(Pixmap) fz_pixmap_s; +struct fz_pixmap_s { - float x0, y0, x1, y1; - fz_rect_s(); + int x, y, w, h, n; + int xres, yres; %extend { - ~fz_rect_s() { - DEBUGMSG1("rect"); - JM_Free($self); + ~fz_pixmap_s() { + DEBUGMSG1("pixmap"); + fz_drop_pixmap(gctx, $self); DEBUGMSG2; } - FITZEXCEPTION(fz_rect_s, !result) - fz_rect_s(const struct fz_rect_s *s) { - fz_rect *r = JM_Alloc(fz_rect, 1); - if (!s) - { - r->x0 = r->y0 = r->x1 = r->y1 = 0; - } - else *r = *s; - return r; - } - - fz_rect_s(const struct fz_point_s *lt, const struct fz_point_s *rb) { - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = lt->x; - r->y0 = lt->y; - r->x1 = rb->x; - r->y1 = rb->y; - return r; - } - - fz_rect_s(float x0, float y0, const struct fz_point_s *rb) { - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = x0; - r->y0 = y0; - r->x1 = rb->x; - r->y1 = rb->y; - return r; - } - - fz_rect_s(const struct fz_point_s *lt, float x1, float y1) { - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = lt->x; - r->y0 = lt->y; - r->x1 = x1; - r->y1 = y1; - return r; - } - - fz_rect_s(float x0, float y0, float x1, float y1) { - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = x0; - r->y0 = y0; - r->x1 = x1; - r->y1 = y1; - return r; + FITZEXCEPTION(fz_pixmap_s, !result) + //--------------------------------------------------------------------- + // create empty pixmap with colorspace and IRect + //--------------------------------------------------------------------- + fz_pixmap_s(struct fz_colorspace_s *cs, PyObject *bbox, int alpha = 0) + { + fz_pixmap *pm = NULL; + fz_separations *seps = NULL; + fz_try(gctx) + pm = fz_new_pixmap_with_bbox(gctx, cs, JM_irect_from_py(bbox), seps, alpha); + fz_catch(gctx) return NULL; + return pm; } - //-------------------------------------------------------------------- - // create Rect from Python list - //-------------------------------------------------------------------- - fz_rect_s(PyObject *list) + //--------------------------------------------------------------------- + // copy pixmap, converting colorspace + // New in v1.11: option to remove alpha + // Changed in v1.13: alpha = 0 does not work since at least v1.12 + //--------------------------------------------------------------------- + fz_pixmap_s(struct fz_colorspace_s *cs, struct fz_pixmap_s *spix) { - fz_rect *r = JM_Alloc(fz_rect, 1); + fz_pixmap *pm = NULL; fz_try(gctx) { - if (!PySequence_Check(list)) THROWMSG("expected a sequence"); - if (PySequence_Size(list) != 4) THROWMSG("len(sequence) invalid"); - r->x0 = (float) PyFloat_AsDouble(PySequence_GetItem(list, 0)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->y0 = (float) PyFloat_AsDouble(PySequence_GetItem(list, 1)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->x1 = (float) PyFloat_AsDouble(PySequence_GetItem(list, 2)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->y1 = (float) PyFloat_AsDouble(PySequence_GetItem(list, 3)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - } - fz_catch(gctx) - { - JM_Free(r); - return NULL; + if (!fz_pixmap_colorspace(gctx, spix)) + THROWMSG("cannot copy pixmap with NULL colorspace"); + pm = fz_convert_pixmap(gctx, spix, cs, NULL, NULL, NULL, 1); } - return r; - } - - %pythonappend round() %{val.thisown = True%} - %feature("autodoc","Create enclosing 'IRect'") round; - struct fz_irect_s *round() - { - fz_irect *irect = JM_Alloc(fz_irect, 1); - fz_rect rect = {MIN($self->x0, $self->x1), - MIN($self->y0, $self->y1), - MAX($self->x0, $self->x1), - MAX($self->y0, $self->y1)}; - fz_round_rect(irect, &rect); - return irect; - } - - %feature("autodoc","Enlarge to include a 'Point' p") includePoint; - struct fz_rect_s *includePoint(const struct fz_point_s *p) - { - if (fz_is_infinite_rect($self)) return $self; - return fz_include_point_in_rect($self, p); - } - - %feature("autodoc","Shrink to intersection with another 'Rect' r") intersect; - struct fz_rect_s *intersect(struct fz_rect_s *r) { - fz_intersect_rect($self, r); - return $self; + fz_catch(gctx) return NULL; + return pm; } - %feature("autodoc","Enlarge to include another 'Rect' r") includeRect; - struct fz_rect_s *includeRect(struct fz_rect_s *r) - { - fz_union_rect($self, r); - return $self; - } - - %feature("autodoc","Make rectangle finite") normalize; - struct fz_rect_s *normalize() + //--------------------------------------------------------------------- + // create pixmap as scaled copy of another one + //--------------------------------------------------------------------- + fz_pixmap_s(struct fz_pixmap_s *spix, float w, float h, PyObject *clip = NULL) { - float f; - if ($self->x1 < $self->x0) - { - f = $self->x1; - $self->x1 = $self->x0; - $self->x0 = f; - } - if ($self->y1 < $self->y0) + fz_pixmap *pm = NULL; + fz_try(gctx) { - f = $self->y1; - $self->y1 = $self->y0; - $self->y0 = f; + fz_irect bbox = JM_irect_from_py(clip); + if (!fz_is_infinite_irect(bbox)) + { + pm = fz_scale_pixmap(gctx, spix, spix->x, spix->y, w, h, &bbox); + } + else + pm = fz_scale_pixmap(gctx, spix, spix->x, spix->y, w, h, NULL); } - return $self; - } - - // check if Rect contains another Rect - %feature("autodoc","contains") contains; - PyObject *contains(struct fz_rect_s *rect) - { - if (fz_is_empty_rect(rect)) Py_RETURN_TRUE; - if (fz_is_empty_rect($self)) Py_RETURN_FALSE; - float l = MIN($self->x0, $self->x1); - float r = MAX($self->x0, $self->x1); - float t = MIN($self->y0, $self->y1); - float b = MAX($self->y0, $self->y1); - - return JM_BOOL(INRANGE(rect->x0, l, r) && - INRANGE(rect->x1, l, r) && - INRANGE(rect->y0, t, b) && - INRANGE(rect->y1, t, b)); - } - - // check if Rect contains another IRect - PyObject *contains(struct fz_irect_s *rect) - { - if (fz_is_empty_irect(rect)) Py_RETURN_TRUE; - if (fz_is_empty_rect($self)) Py_RETURN_FALSE; - float l = MIN($self->x0, $self->x1); - float r = MAX($self->x0, $self->x1); - float t = MIN($self->y0, $self->y1); - float b = MAX($self->y0, $self->y1); - - return JM_BOOL(INRANGE(rect->x0, l, r) && - INRANGE(rect->x1, l, r) && - INRANGE(rect->y0, t, b) && - INRANGE(rect->y1, t, b)); - } - - // check if Rect contains a Point - PyObject *contains(struct fz_point_s *p) - { - if (fz_is_empty_rect($self)) Py_RETURN_FALSE; - float l = MIN($self->x0, $self->x1); - float r = MAX($self->x0, $self->x1); - float t = MIN($self->y0, $self->y1); - float b = MAX($self->y0, $self->y1); - - return JM_BOOL(INRANGE(p->x, l, r) && - INRANGE(p->y, t, b)); - } - - %pythoncode %{@property%} - PyObject *isEmpty() - { - return JM_BOOL(fz_is_empty_rect($self)); - } - - %pythoncode %{@property%} - PyObject *isInfinite() - { - return JM_BOOL(fz_is_infinite_rect($self)); - } - - %pythoncode %{ - def transform(self, m): - """Transform rectangle with Matrix m.""" - _fitz._fz_transform_rect(self, m) - return self - - @property - def top_left(self): - """Return the rectangle's top-left point.""" - return Point(self.x0, self.y0) - - @property - def top_right(self): - """Return the rectangle's top-right point.""" - return Point(self.x1, self.y0) - - @property - def bottom_left(self): - """Return the rectangle's bottom-left point.""" - return Point(self.x0, self.y1) - - @property - def bottom_right(self): - """Return the rectangle's bottom-right point.""" - return Point(self.x1, self.y1) - - def __getitem__(self, i): - return (self.x0, self.y0, self.x1, self.y1)[i] - - def __setitem__(self, i, v): - if i == 0: self.x0 = v - elif i == 1: self.y0 = v - elif i == 2: self.x1 = v - elif i == 3: self.y1 = v - else: - raise IndexError("index out of range") - return - - def __len__(self): - return 4 - - def __repr__(self): - return "fitz.Rect" + str((self.x0, self.y0, self.x1, self.y1)) - - irect = property(round) - width = property(lambda self: self.x1-self.x0) - height = property(lambda self: self.y1-self.y0) - tl = top_left - tr = top_right - br = bottom_right - bl = bottom_left - %} - } -}; - - -/* fz_irect */ -%rename(IRect) fz_irect_s; -struct fz_irect_s -{ - int x0, y0; - int x1, y1; - fz_irect_s(); - %extend { - ~fz_irect_s() { - DEBUGMSG1("irect"); - JM_Free($self); - DEBUGMSG2; - } - FITZEXCEPTION(fz_irect_s, !result) - fz_irect_s(const struct fz_irect_s *s) { - fz_irect *r = JM_Alloc(fz_irect, 1); - if (!s) - { - r->x0 = r->y0 = r->x1 = r->y1 = 0; - } - else *r = *s; - return r; - } - - fz_irect_s(int x0, int y0, int x1, int y1) { - fz_irect *r = JM_Alloc(fz_irect, 1); - r->x0 = x0; - r->y0 = y0; - r->x1 = x1; - r->y1 = y1; - return r; - } - - //-------------------------------------------------------------------- - // create IRect from Python list - //-------------------------------------------------------------------- - fz_irect_s(PyObject *list) - { - fz_irect *r = JM_Alloc(fz_irect, 1); - fz_try(gctx) - { - if (!PySequence_Check(list)) THROWMSG("expected a sequence"); - if (PySequence_Size(list) != 4) THROWMSG("len(sequence) invalid"); - r->x0 = (int) PyInt_AsLong(PySequence_GetItem(list, 0)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->y0 = (int) PyInt_AsLong(PySequence_GetItem(list, 1)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->x1 = (int) PyInt_AsLong(PySequence_GetItem(list, 2)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->y1 = (int) PyInt_AsLong(PySequence_GetItem(list, 3)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - } - fz_catch(gctx) - { - JM_Free(r); - return NULL; - } - return r; - } - - %pythoncode %{@property%} - PyObject *isEmpty() - { - return JM_BOOL(fz_is_empty_irect($self)); - } - - %pythoncode %{@property%} - PyObject *isInfinite() - { - return JM_BOOL(fz_is_infinite_irect($self)); - } - - %feature("autodoc","Make rectangle finite") normalize; - struct fz_irect_s *normalize() - { - int f; - if ($self->x1 < $self->x0) - { - f = $self->x1; - $self->x1 = $self->x0; - $self->x0 = f; - } - if ($self->y1 < $self->y0) - { - f = $self->y1; - $self->y1 = $self->y0; - $self->y0 = f; - } - return $self; - } - - %feature("autodoc","contains") contains; - PyObject *contains(struct fz_irect_s *rect) - { - if (fz_is_empty_irect(rect)) Py_RETURN_TRUE; - if (fz_is_empty_irect($self)) Py_RETURN_FALSE; - int l = MIN($self->x0, $self->x1); - int r = MAX($self->x0, $self->x1); - int t = MIN($self->y0, $self->y1); - int b = MAX($self->y0, $self->y1); - - return JM_BOOL(INRANGE(rect->x0, l, r) && - INRANGE(rect->x1, l, r) && - INRANGE(rect->y0, t, b) && - INRANGE(rect->y1, t, b)); - } - - PyObject *contains(struct fz_rect_s *rect) - { - if (fz_is_empty_rect(rect)) Py_RETURN_TRUE; - if (fz_is_empty_irect($self)) Py_RETURN_FALSE; - float l = MIN($self->x0, $self->x1); - float r = MAX($self->x0, $self->x1); - float t = MIN($self->y0, $self->y1); - float b = MAX($self->y0, $self->y1); - - return JM_BOOL(INRANGE(rect->x0, l, r) && - INRANGE(rect->x1, l, r) && - INRANGE(rect->y0, t, b) && - INRANGE(rect->y1, t, b)); - } - - PyObject *contains(struct fz_point_s *p) - { - if (fz_is_empty_irect($self)) Py_RETURN_FALSE; - float l = MIN($self->x0, $self->x1); - float r = MAX($self->x0, $self->x1); - float t = MIN($self->y0, $self->y1); - float b = MAX($self->y0, $self->y1); - - return JM_BOOL(INRANGE(p->x, l, r) && - INRANGE(p->y, t, b)); - } - - struct fz_irect_s *translate(int xoff, int yoff) { - fz_translate_irect($self, xoff, yoff); - return $self; - } - - struct fz_irect_s *intersect(struct fz_irect_s *ir) { - fz_intersect_irect($self, ir); - return $self; - } - - %pythoncode %{ - def getRect(self): - return Rect(self.x0, self.y0, self.x1, self.y1) - - rect = property(getRect) - - @property - def top_left(self): - return Point(self.x0, self.y0) - - @property - def top_right(self): - return Point(self.x1, self.y0) - - @property - def bottom_left(self): - return Point(self.x0, self.y1) - - @property - def bottom_right(self): - return Point(self.x1, self.y1) - - def __getitem__(self, i): - return (self.x0, self.y0, self.x1, self.y1)[i] - - def __setitem__(self, i, v): - if i == 0: self.x0 = v - elif i == 1: self.y0 = v - elif i == 2: self.x1 = v - elif i == 3: self.y1 = v - else: - raise IndexError("index out of range") - return - - def __len__(self): - return 4 - - def __repr__(self): - if not type(self) is IRect: return - return "fitz.IRect" + str((self.x0, self.y0, self.x1, self.y1)) - - width = property(lambda self: self.x1-self.x0) - height = property(lambda self: self.y1-self.y0) - tl = top_left - tr = top_right - br = bottom_right - bl = bottom_left - - %} - } -}; - -//----------------------------------------------------------------------------- -// Pixmap -//----------------------------------------------------------------------------- -%rename(Pixmap) fz_pixmap_s; -struct fz_pixmap_s -{ - int x, y, w, h, n; - int xres, yres; - %extend { - ~fz_pixmap_s() { - DEBUGMSG1("pixmap"); - fz_drop_pixmap(gctx, $self); - DEBUGMSG2; - } - FITZEXCEPTION(fz_pixmap_s, !result) - //--------------------------------------------------------------------- - // create empty pixmap with colorspace and IRect - //--------------------------------------------------------------------- - fz_pixmap_s(struct fz_colorspace_s *cs, const struct fz_irect_s *bbox, int alpha = 0) - { - fz_pixmap *pm = NULL; - fz_separations *seps = NULL; - fz_try(gctx) - pm = fz_new_pixmap_with_bbox(gctx, cs, bbox, seps, alpha); - fz_catch(gctx) return NULL; - return pm; - } - - //--------------------------------------------------------------------- - // copy pixmap, converting colorspace - // New in v1.11: option to remove alpha - // Changed in 1.13: alpha = 0 does not work since at least 1.12 - //--------------------------------------------------------------------- - fz_pixmap_s(struct fz_colorspace_s *cs, struct fz_pixmap_s *spix) - { - fz_pixmap *pm = NULL; - fz_try(gctx) - { - if (!fz_pixmap_colorspace(gctx, spix)) - THROWMSG("cannot copy pixmap with NULL colorspace"); - pm = fz_convert_pixmap(gctx, spix, cs, NULL, NULL, NULL, 1); - } - fz_catch(gctx) return NULL; - return pm; - } - - //--------------------------------------------------------------------- - // create pixmap as scaled copy of another one - //--------------------------------------------------------------------- - fz_pixmap_s(struct fz_pixmap_s *spix, float w, float h, struct fz_irect_s *clip = NULL) - { - fz_pixmap *pm = NULL; - fz_try(gctx) - { - pm = fz_scale_pixmap(gctx, spix, spix->x, spix->y, w, h, clip); - } - fz_catch(gctx) return NULL; - return pm; + fz_catch(gctx) return NULL; + return pm; } //--------------------------------------------------------------------- @@ -3839,8 +3541,8 @@ struct fz_pixmap_s if (!INRANGE(xref, 1, xreflen-1)) THROWMSG("xref out of range"); ref = pdf_new_indirect(gctx, pdf, xref, 0); - type = pdf_dict_get(gctx, ref, PDF_NAME_Subtype); - if (!pdf_name_eq(gctx, type, PDF_NAME_Image)) + type = pdf_dict_get(gctx, ref, PDF_NAME(Subtype)); + if (!pdf_name_eq(gctx, type, PDF_NAME(Image))) THROWMSG("xref not an image"); img = pdf_load_image(gctx, pdf, ref); pix = fz_get_pixmap_from_image(gctx, img, NULL, NULL, NULL, NULL); @@ -3865,7 +3567,7 @@ struct fz_pixmap_s { if (factor < 1) { - PySys_WriteStdout("warning: ignoring shrink factor < 1\n"); + JM_Warning("ignoring shrink factor < 1"); return; } fz_subsample_pixmap(gctx, $self, factor); @@ -3878,7 +3580,7 @@ struct fz_pixmap_s { if (!fz_pixmap_colorspace(gctx, $self)) { - PySys_WriteStdout("warning: colorspace invalid for function\n"); + JM_Warning("colorspace invalid for function"); return; } fz_gamma_pixmap(gctx, $self, gamma); @@ -3915,22 +3617,22 @@ struct fz_pixmap_s //---------------------------------------------------------------------- // clear pixmap rectangle with value //---------------------------------------------------------------------- - void clearWith(int value, const struct fz_irect_s *bbox) + void clearWith(int value, PyObject *bbox) { - fz_clear_pixmap_rect_with_value(gctx, $self, value, bbox); + fz_clear_pixmap_rect_with_value(gctx, $self, value, JM_irect_from_py(bbox)); } //---------------------------------------------------------------------- // copy pixmaps //---------------------------------------------------------------------- FITZEXCEPTION(copyPixmap, !result) - PyObject *copyPixmap(struct fz_pixmap_s *src, const struct fz_irect_s *bbox) + PyObject *copyPixmap(struct fz_pixmap_s *src, PyObject *bbox) { fz_try(gctx) { if (!fz_pixmap_colorspace(gctx, src)) THROWMSG("cannot copy pixmap with NULL colorspace"); - fz_copy_pixmap_rect(gctx, $self, src, bbox, NULL); + fz_copy_pixmap_rect(gctx, $self, src, JM_irect_from_py(bbox), NULL); } fz_catch(gctx) return NULL; return NONE; @@ -3967,11 +3669,10 @@ struct fz_pixmap_s // return irect of pixmap //---------------------------------------------------------------------- %pythoncode %{@property%} - struct fz_irect_s *irect() + %pythonappend irect %{val = IRect(val)%} + PyObject *irect() { - fz_irect *r = JM_Alloc(fz_irect, 1); - r->x0 = r->y0 = r->x1 = r->y1 = 0; - return fz_pixmap_bbox(gctx, $self, r); + return JM_py_from_irect(fz_pixmap_bbox(gctx, $self)); } //---------------------------------------------------------------------- @@ -3997,18 +3698,18 @@ struct fz_pixmap_s int h = fz_pixmap_height(gctx, $self); int balen = w * h * (n+1); unsigned char *data = NULL; - size_t data_len = 0; + int data_len = 0; if (alphavalues) { - data_len = JM_CharFromBytesOrArray(alphavalues, &data); - if (data_len > 0 && data_len < w * h) + data_len = (int) JM_CharFromBytesOrArray(alphavalues, &data); + if (data_len && data_len < w * h) THROWMSG("not enough alpha values"); } int i = 0, k = 0; while (i < balen) { - if (data_len > 0) $self->samples[i+n] = data[k]; - else $self->samples[i+n] = 255; + if (data_len) $self->samples[i+n] = data[k]; + else $self->samples[i+n] = 255; i += n+1; k += 1; } @@ -4026,7 +3727,7 @@ struct fz_pixmap_s struct fz_buffer_s *res = NULL; fz_output *out = NULL; PyObject *r; - if (savealpha != -1) PySys_WriteStdout("warning: ignoring savealpha\n"); + if (savealpha != -1) JM_Warning("ignoring savealpha"); fz_try(gctx) { res = fz_new_buffer(gctx, 1024); out = fz_new_output_with_buffer(gctx, res); @@ -4048,7 +3749,7 @@ struct fz_pixmap_s FITZEXCEPTION(_writeIMG, !result) PyObject *_writeIMG(char *filename, int format, int savealpha=-1) { - if (savealpha != -1) PySys_WriteStdout("warning: ignoring savealpha\n"); + if (savealpha != -1) JM_Warning("ignoring savealpha"); fz_try(gctx) { switch(format) { @@ -4076,16 +3777,19 @@ struct fz_pixmap_s //---------------------------------------------------------------------- // invertIRect //---------------------------------------------------------------------- - void invertIRect(const struct fz_irect_s *irect = NULL) + void invertIRect(PyObject *irect = NULL) { if (!fz_pixmap_colorspace(gctx, $self)) { - PySys_WriteStdout("warning: ignored for stencil pixmap\n"); + JM_Warning("ignored for stencil pixmap"); return; } - - if (irect) fz_invert_pixmap_rect(gctx, $self, irect); - else fz_invert_pixmap(gctx, $self); + + fz_irect r = JM_irect_from_py(irect); + if (!fz_is_infinite_irect(r)) + fz_invert_pixmap_rect(gctx, $self, r); + else + fz_invert_pixmap(gctx, $self); } //---------------------------------------------------------------------- @@ -4194,16 +3898,17 @@ struct DeviceWrapper { %extend { FITZEXCEPTION(DeviceWrapper, !result) - DeviceWrapper(struct fz_pixmap_s *pm, struct fz_irect_s *clip) { + DeviceWrapper(struct fz_pixmap_s *pm, PyObject *clip) { struct DeviceWrapper *dw = NULL; fz_try(gctx) { dw = (struct DeviceWrapper *)calloc(1, sizeof(struct DeviceWrapper)); - if (!clip) - dw->device = fz_new_draw_device(gctx, &fz_identity, pm); + fz_irect bbox = JM_irect_from_py(clip); + if (fz_is_infinite_irect(bbox)) + dw->device = fz_new_draw_device(gctx, fz_identity, pm); else - dw->device = fz_new_draw_device_with_bbox(gctx, &fz_identity, pm, clip); + dw->device = fz_new_draw_device_with_bbox(gctx, fz_identity, pm, &bbox); } - fz_catch(gctx) return NULL; + fz_catch(gctx) return NULL; return dw; } DeviceWrapper(struct fz_display_list_s *dl) { @@ -4244,172 +3949,9 @@ struct DeviceWrapper } }; -//------------------------------------------------------------------------------ -// fz_matrix -//------------------------------------------------------------------------------ -%rename(_fz_pre_scale) fz_pre_scale; -%rename(_fz_pre_shear) fz_pre_shear; -%rename(_fz_pre_rotate) fz_pre_rotate; -struct fz_matrix_s *fz_pre_scale(struct fz_matrix_s *m, float sx, float sy); -struct fz_matrix_s *fz_pre_shear(struct fz_matrix_s *m, float sx, float sy); -struct fz_matrix_s *fz_pre_rotate(struct fz_matrix_s *m, float degree); -%rename(Matrix) fz_matrix_s; -struct fz_matrix_s -{ - float a, b, c, d, e, f; - fz_matrix_s(); - %extend { - FITZEXCEPTION(fz_matrix_s, !result) - ~fz_matrix_s() - { - DEBUGMSG1("matrix"); - JM_Free($self); - DEBUGMSG2; - } - - //-------------------------------------------------------------------- - // copy constructor - //-------------------------------------------------------------------- - fz_matrix_s(const struct fz_matrix_s* n) - { - fz_matrix *m = JM_Alloc(fz_matrix, 1); - if (!n) - { - m->a = m->b = m->c = m->d = m->e = m->f = 0; - } - else - { - *m = *n; - } - return m; - } - - //-------------------------------------------------------------------- - // create a scale/shear matrix, scale matrix by default - //-------------------------------------------------------------------- - fz_matrix_s(float sx, float sy, int shear=0) - { - fz_matrix *m = JM_Alloc(fz_matrix, 1); - if(shear) return fz_shear(m, sx, sy); - return fz_scale(m, sx, sy); - } - - //-------------------------------------------------------------------- - // create a matrix by its 6 components - //-------------------------------------------------------------------- - fz_matrix_s(float r, float s, float t, float u, float v, float w) - { - fz_matrix *m = JM_Alloc(fz_matrix, 1); - m->a = r; - m->b = s; - m->c = t; - m->d = u; - m->e = v; - m->f = w; - return m; - } - - //-------------------------------------------------------------------- - // create a rotation matrix - //-------------------------------------------------------------------- - fz_matrix_s(float degree) - { - fz_matrix *m = JM_Alloc(fz_matrix, 1); - return fz_rotate(m, degree); - } - - //-------------------------------------------------------------------- - // create matrix from Python sequence - //-------------------------------------------------------------------- - fz_matrix_s(PyObject *list) - { - fz_matrix *m = JM_Alloc(fz_matrix, 1); - fz_try(gctx) - { - if (!PySequence_Check(list)) THROWMSG("expected a sequence"); - if (PySequence_Size(list) != 6) THROWMSG("len(sequence) invalid"); - m->a = (float) PyFloat_AsDouble(PySequence_GetItem(list, 0)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->b = (float) PyFloat_AsDouble(PySequence_GetItem(list, 1)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->c = (float) PyFloat_AsDouble(PySequence_GetItem(list, 2)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->d = (float) PyFloat_AsDouble(PySequence_GetItem(list, 3)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->e = (float) PyFloat_AsDouble(PySequence_GetItem(list, 4)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->f = (float) PyFloat_AsDouble(PySequence_GetItem(list, 5)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - } - fz_catch(gctx) - { - JM_Free(m); - return NULL; - } - return m; - } - - //-------------------------------------------------------------------- - // invert a matrix - //-------------------------------------------------------------------- - int invert(const struct fz_matrix_s *m) - { - int rc = fz_try_invert_matrix($self, m); - return rc; - } - - struct fz_matrix_s *preTranslate(float sx, float sy) - { - fz_pre_translate($self, sx, sy); - return $self; - } - - //-------------------------------------------------------------------- - // multiply matrices - //-------------------------------------------------------------------- - struct fz_matrix_s *concat(struct fz_matrix_s *m1, struct fz_matrix_s *m2) { - fz_concat($self, m1, m2); - return $self; - } - - %pythoncode %{ - def preScale(self, sx, sy): - """preScale(Matrix self, float sx, float sy) -> Matrix self updated""" - _fitz._fz_pre_scale(self, sx, sy) - return self - def preShear(self, sx, sy): - """preShear(Matrix self, float sx, float sy) -> Matrix self updated""" - _fitz._fz_pre_shear(self, sx, sy) - return self - def preRotate(self, degree): - """preRotate(Matrix self, float degree) -> Matrix self updated""" - _fitz._fz_pre_rotate(self, degree) - return self - def __getitem__(self, i): - return (self.a, self.b, self.c, self.d, self.e, self.f)[i] - - def __setitem__(self, i, v): - if i == 0: self.a = v - elif i == 1: self.b = v - elif i == 2: self.c = v - elif i == 3: self.d = v - elif i == 4: self.e = v - elif i == 5: self.f = v - else: - raise IndexError("index out of range") - return - - def __len__(self): - return 6 - def __repr__(self): - return "fitz.Matrix(%s, %s, %s, %s, %s, %s)" % (self.a, self.b, self.c, self.d, self.e, self.f) - - %} - } -}; - - -/* fz_outline */ +//----------------------------------------------------------------------------- +// fz_outline +//----------------------------------------------------------------------------- %rename(Outline) fz_outline_s; %nodefaultctor; struct fz_outline_s { @@ -4488,127 +4030,6 @@ struct fz_outline_s { }; %clearnodefaultctor; -/* fz_point */ -%rename(_fz_transform_point) fz_transform_point; -struct fz_point_s *fz_transform_point(struct fz_point_s *restrict point, const struct fz_matrix_s *restrict transform); -%rename(Point) fz_point_s; -struct fz_point_s -{ - float x, y; - fz_point_s(); - %extend - { - FITZEXCEPTION(fz_point_s, !result) - fz_point_s(const struct fz_point_s *q) { - fz_point *p = JM_Alloc(fz_point, 1); - if (!q) - { - p->x = p->y = 0; - } - else *p = *q; - return p; - } - - fz_point_s(float x, float y) { - fz_point *p = JM_Alloc(fz_point, 1); - p->x = x; - p->y = y; - return p; - } - - //-------------------------------------------------------------------- - // create Point from Python list - //-------------------------------------------------------------------- - fz_point_s(PyObject *list) - { - fz_point *p = JM_Alloc(fz_point, 1); - fz_try(gctx) - { - if (!PySequence_Check(list)) THROWMSG("expected a sequence"); - if (PySequence_Size(list) != 2) THROWMSG("len(sequence) invalid"); - p->x = (float) PyFloat_AsDouble(PySequence_GetItem(list, 0)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - p->y = (float) PyFloat_AsDouble(PySequence_GetItem(list, 1)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - } - fz_catch(gctx) - { - JM_Free(p); - return NULL; - } - return p; - } - - %pythoncode %{ - def distance_to(self, *args): - """Return the distance to a rectangle or another point.""" - assert len(args) > 0, "at least one parameter must be given" - x = args[0] - if len(args) > 1: - unit = args[1] - else: - unit = "px" - u = {"px": (1.,1.), "in": (1.,72.), "cm": (2.54, 72.), "mm": (25.4, 72.)} - f = u[unit][0] / u[unit][1] - if type(x) is Point: - return abs(self - x) * f - - # from here on, x is a rectangle - # as a safeguard, make a finite copy of it - r = Rect(x.top_left, x.top_left) - r = r | x.bottom_right - if self in r: - return 0.0 - if self.x > r.x1: - if self.y >= r.y1: - return self.distance_to(r.bottom_right, unit) - elif self.y <= r.y0: - return self.distance_to(r.top_right, unit) - else: - return (self.x - r.x1) * f - elif r.x0 <= self.x <= r.x1: - if self.y >= r.y1: - return (self.y - r.y1) * f - else: - return (r.y0 - self.y) * f - else: - if self.y >= r.y1: - return self.distance_to(r.bottom_left, unit) - elif self.y <= r.y0: - return self.distance_to(r.top_left, unit) - else: - return (r.x0 - self.x) * f - - def transform(self, m): - _fitz._fz_transform_point(self, m) - return self - - def __setitem__(self, i, v): - if i == 0: - self.x = v - elif i == 1: - self.y = v - else: - raise IndexError("index out of range") - return - - def __getitem__(self, i): - return (self.x, self.y)[i] - - def __len__(self): - return 2 - - def __repr__(self): - return "fitz.Point" + str((self.x, self.y)) - %} - ~fz_point_s() - { - DEBUGMSG1("point"); - JM_Free($self); - DEBUGMSG2; - } - } -}; //----------------------------------------------------------------------------- // Annotation @@ -4740,10 +4161,11 @@ struct fz_annot_s PARENTCHECK(rect) %feature("autodoc","Rectangle containing the annot") rect; %pythoncode %{@property%} - struct fz_rect_s *rect() + %pythonappend rect %{val = Rect(val)%} + PyObject *rect() { - fz_rect *r = JM_Alloc(fz_rect, 1); - return fz_bound_annot(gctx, $self, r); + fz_rect r = fz_bound_annot(gctx, $self); + return JM_py_from_rect(r); } //--------------------------------------------------------------------- @@ -4751,7 +4173,8 @@ struct fz_annot_s //--------------------------------------------------------------------- PARENTCHECK(_getXref) %feature("autodoc","Xref number of annotation") _getXref; - int _getXref() + %pythoncode %{@property%} + int xref() { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); if(!annot) return 0; @@ -4762,7 +4185,6 @@ struct fz_annot_s // annotation get decompressed appearance stream source //--------------------------------------------------------------------- FITZEXCEPTION(_getAP, !result) - PARENTCHECK(_getAP) %feature("autodoc","Get contents source of a PDF annot") _getAP; PyObject *_getAP() { @@ -4772,8 +4194,8 @@ struct fz_annot_s if (!annot) return NONE; fz_try(gctx) { - pdf_obj *ap = pdf_dict_getl(gctx, annot->obj, PDF_NAME_AP, - PDF_NAME_N, NULL); + pdf_obj *ap = pdf_dict_getl(gctx, annot->obj, PDF_NAME(AP), + PDF_NAME(N), NULL); if (pdf_is_stream(gctx, ap)) res = pdf_load_stream(gctx, ap); if (res) r = JM_BinFromBuffer(gctx, res); @@ -4787,9 +4209,8 @@ struct fz_annot_s // annotation update /AP stream //--------------------------------------------------------------------- FITZEXCEPTION(_setAP, !result) - PARENTCHECK(_setAP) %feature("autodoc","Update contents source of a PDF annot") _setAP; - PyObject *_setAP(PyObject *ap) + PyObject *_setAP(PyObject *ap, int rect = 0) { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); fz_buffer *res = NULL; @@ -4797,8 +4218,8 @@ struct fz_annot_s fz_try(gctx) { assert_PDF(annot); - pdf_obj *apobj = pdf_dict_getl(gctx, annot->obj, PDF_NAME_AP, - PDF_NAME_N, NULL); + pdf_obj *apobj = pdf_dict_getl(gctx, annot->obj, PDF_NAME(AP), + PDF_NAME(N), NULL); if (!apobj) THROWMSG("annot has no /AP/N object"); if (!pdf_is_stream(gctx, apobj)) THROWMSG("/AP/N object is no stream"); @@ -4807,8 +4228,12 @@ struct fz_annot_s if (!c) THROWMSG("invalid /AP stream argument"); res = fz_new_buffer_from_copied_data(gctx, c, strlen(c)); JM_update_stream(gctx, annot->page->doc, apobj, res); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, annot->page); + if (rect) + { + fz_rect bbox = pdf_dict_get_rect(gctx, annot->obj, PDF_NAME(Rect)); + pdf_dict_put_rect(gctx, apobj, PDF_NAME(BBox), bbox); + annot->ap = NULL; + } } fz_always(gctx) fz_drop_buffer(gctx, res); @@ -4816,79 +4241,20 @@ struct fz_annot_s return NONE; } - //--------------------------------------------------------------------- - // annotation create or update /AP object - //--------------------------------------------------------------------- - FITZEXCEPTION(_checkAP, !result) - PARENTCHECK(_checkAP) - %feature("autodoc","Check and update /AP object of annot") _checkAP; - PyObject *_checkAP(struct fz_rect_s *rect, char *c) - { - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); - fz_try(gctx) - { - assert_PDF(annot); - JM_make_ap_object(gctx, $self, rect, c); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, annot->page); - } - fz_catch(gctx) return NULL; - return NONE; - } - //--------------------------------------------------------------------- // annotation set rectangle //--------------------------------------------------------------------- - void _setRect(struct fz_rect_s *rect) + void setRect(PyObject *rect) { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); if (!annot) return; fz_try(gctx) { - pdf_set_annot_rect(gctx, annot, rect); + pdf_set_annot_rect(gctx, annot, JM_rect_from_py(rect)); } fz_catch(gctx) {;} return; } - %pythoncode %{ - def setRect(self, rect): - """Change the annot's rectangle.""" - CheckParent(self) - if rect.isEmpty or rect.isInfinite: - raise ValueError("Rect must be finite and not empty.") - # only handle Circle, Square, Line, PolyLine and Polygon here - if self.type[0] not in range(2, 8): - self._setRect(rect) - return - - if self.type[0] == ANNOT_CIRCLE: - self._setRect(rect) - ap = _make_circle_AP(self) - self._checkAP(Rect(0, 0, rect.width, rect.height), ap) - return - - if self.type[0] == ANNOT_SQUARE: - self._setRect(rect) - ap = _make_rect_AP(self) - self._checkAP(Rect(0, 0, rect.width, rect.height), ap) - return - - orect = self.rect - m = Matrix(rect.width / orect.width, rect.height / orect.height) - - # now transform the points of the annot - ov = self.vertices - nv = [(Point(v) - orect.tl) * m + rect.tl for v in ov] # new points - r0 = Rect(nv[0], nv[0]) # recalculate new rectangle - for v in nv[1:]: - r0 |= v # enveloping all points - w = self.border["width"] * 3 # allow for add'l space - r0 += (-w, -w, w, w) # for line end symbols - self._setRect(r0) # this is the final rect - self._setVertices(nv) # put the points in annot - ap = _make_line_AP(self, nv, r0) - self._checkAP(Rect(0, 0, r0.width, r0.height), ap) - %} //--------------------------------------------------------------------- // annotation vertices (for "Line", "Polgon", "Ink", etc. @@ -4908,9 +4274,9 @@ struct fz_annot_s // Every pair of floats is one point, that needs to be separately // transformed with the page's transformation matrix. //---------------------------------------------------------------- - pdf_obj *o = pdf_dict_get(gctx, annot->obj, PDF_NAME_Vertices); - if (!o) o = pdf_dict_get(gctx, annot->obj, PDF_NAME_L); - if (!o) o = pdf_dict_get(gctx, annot->obj, PDF_NAME_QuadPoints); + pdf_obj *o = pdf_dict_get(gctx, annot->obj, PDF_NAME(Vertices)); + if (!o) o = pdf_dict_get(gctx, annot->obj, PDF_NAME(L)); + if (!o) o = pdf_dict_get(gctx, annot->obj, PDF_NAME(QuadPoints)); if (!o) o = pdf_dict_gets(gctx, annot->obj, "CL"); int i, j, n; fz_point point; // point object to work with @@ -4925,15 +4291,17 @@ struct fz_annot_s { point.x = pdf_to_real(gctx, pdf_array_get(gctx, o, i)); point.y = pdf_to_real(gctx, pdf_array_get(gctx, o, i+1)); - fz_transform_point(&point, &page_ctm); + fz_transform_point(point, page_ctm); PyList_Append(res, Py_BuildValue("ff", point.x, point.y)); } return res; } // nothing found so far - maybe an Ink annotation? - pdf_obj *il_o = pdf_dict_get(gctx, annot->obj, PDF_NAME_InkList); + pdf_obj *il_o = pdf_dict_get(gctx, annot->obj, PDF_NAME(InkList)); if (!il_o) return NONE; // no inkList res = PyList_New(0); // create result list + fz_rect prect = pdf_bound_page(gctx, annot->page); + double h = prect.y1 - prect.y0; n = pdf_array_len(gctx, il_o); for (i = 0; i < n; i++) { @@ -4944,8 +4312,7 @@ struct fz_annot_s { point.x = pdf_to_real(gctx, pdf_array_get(gctx, o, j)); point.y = pdf_to_real(gctx, pdf_array_get(gctx, o, j+1)); - fz_transform_point(&point, &page_ctm); - PyList_Append(list, Py_BuildValue("ff", point.x, point.y)); + PyList_Append(list, Py_BuildValue("ff", point.x, h - point.y)); } PyList_Append(res, list); Py_CLEAR(list); @@ -4953,53 +4320,6 @@ struct fz_annot_s return res; } - //--------------------------------------------------------------------- - // annotation set vertices - //--------------------------------------------------------------------- - FITZEXCEPTION(_setVertices, !result) - PARENTCHECK(_setVertices) - %feature("autodoc","Change the annot's vertices. Only for 'Line', 'PolyLine' and 'Polygon' types.") _setVertices; - PyObject *_setVertices(PyObject *vertices) - { - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); - if (!annot) return NONE; - int type = pdf_annot_type(gctx, annot); - if (!INRANGE(type, 3, 7)) return NONE; - if (INRANGE(type, 4, 5)) return NONE; // only handling 3 types - fz_try(gctx) - { - fz_point a, b; - PyObject *p = NULL; - if (type == PDF_ANNOT_LINE) - { - p = PySequence_ITEM(vertices, 0); - a.x = (float) PyFloat_AsDouble(PySequence_ITEM(p, 0)); - a.y = (float) PyFloat_AsDouble(PySequence_ITEM(p, 1)); - Py_CLEAR(p); - p = PySequence_ITEM(vertices, 1); - b.x = (float) PyFloat_AsDouble(PySequence_ITEM(p, 0)); - b.y = (float) PyFloat_AsDouble(PySequence_ITEM(p, 1)); - Py_CLEAR(p); - pdf_set_annot_line(gctx, annot, a, b); - } - else - { - int i, n = PySequence_Size(vertices); - for (i = 0; i < n; i++) - { - p = PySequence_ITEM(vertices, i); - a.x = (float) PyFloat_AsDouble(PySequence_ITEM(p, 0)); - a.y = (float) PyFloat_AsDouble(PySequence_ITEM(p, 1)); - Py_CLEAR(p); - pdf_set_annot_vertex(gctx, annot, i, a); - } - } - - } - fz_catch(gctx) return NULL; - return NONE; - } - //--------------------------------------------------------------------- // annotation colors //--------------------------------------------------------------------- @@ -5010,60 +4330,205 @@ struct fz_annot_s { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); if (!annot) return NONE; - - PyObject *res = PyDict_New(); - PyObject *bc = PyList_New(0); // stroke colors - PyObject *fc = PyList_New(0); // fill colors - - int i; - float col; - pdf_obj *o = pdf_dict_get(gctx, annot->obj, PDF_NAME_C); - if (pdf_is_array(gctx, o)) - { - int n = pdf_array_len(gctx, o); - for (i = 0; i < n; i++) - { - col = pdf_to_real(gctx, pdf_array_get(gctx, o, i)); - PyList_Append(bc, Py_BuildValue("f", col)); - } - } - PyDict_SetItemString(res, "stroke", bc); - - o = pdf_dict_gets(gctx, annot->obj, "IC"); - if (pdf_is_array(gctx, o)) - { - int n = pdf_array_len(gctx, o); - for (i = 0; i < n; i++) - { - col = pdf_to_real(gctx, pdf_array_get(gctx, o, i)); - PyList_Append(fc, Py_BuildValue("f", col)); - } - } - PyDict_SetItemString(res, "fill", fc); - - Py_CLEAR(bc); - Py_CLEAR(fc); - return res; + return JM_annot_colors(gctx, annot->obj); } //--------------------------------------------------------------------- // annotation update appearance //--------------------------------------------------------------------- - PARENTCHECK(updateAppearance) - %feature("autodoc","Update the appearance of an annotation.") updateAppearance; - PyObject *updateAppearance() + PARENTCHECK(update) + %feature("autodoc","Update the appearance of an annotation.") update; + %pythonprepend update %{ + if self.type[0] == ANNOT_WIDGET: + print("Use updateWidget method for form fields.") + return False%} + %pythonappend update %{ + """ + The following code fixes shortcomings of MuPDF's "pdf_update_annot" + function. Currently these are: + 1. Opacity (all annots). MuPDF ignores this proprty. This requires + to add an ExtGState (extended graphics state) object in the + C code as well. + 2. Dashing (all annots). MuPDF ignores this proprty. + 3. Colors and font size for FreeText annotations. + 4. Line end icons also for POLYGON and POLY_LINE annotations. + MuPDF only honors them for LINE annotations. + 5. Always perform a "clean" for the annot, because MuPDF does not + enclose their syntax in a string pair "q ... Q", which may cause + Adobe and other readers not to display the annot. + + """ + if not val is True: # skip if something went wrong + return val + + def color_string(cs, code): + """Return valid PDF color operator for a given color sequence. + """ + if cs is None: return "" + if hasattr(cs, "__float__") or len(cs) == 1: + app = " g\n" if code == "f" else " G\n" + elif len(cs) == 3: + app = " rg\n" if code == "f" else " RG\n" + else: + app = " k\n" if code == "f" else " K\n" + if hasattr(cs, "__len__"): + col = " ".join(map(str, cs)) + app + else: + col = "%g" % cs + app + return bytes(col, "utf8") if str is not bytes else col + + type = self.type[0] # get the annot type + dt = self.border["dashes"] # get the dashes spec + bwidth = self.border["width"] # get border line width + stroke = self.colors["stroke"] # get the stroke color + fill = self.colors["fill"] # get the fill color + rect = None # used if we change the rect here + bfill = color_string(fill, "f") + + line_end_le, line_end_ri = 0, 0 # line end codes + if self.lineEnds: + line_end_le, line_end_ri = self.lineEnds + + ap = self._getAP() # get the annot operator source + ap_updated = False # assume we did nothing + + if type == ANNOT_FREETEXT: + CheckColor(fill_color) + CheckColor(border_color) + CheckColor(text_color) + + ap_tab = ap.splitlines() # split AP stream into lines + idx_BT = ap_tab.index(b"BT") # line no. of text start + # to avoid effort, we rely on a fixed format of this + # annot type: line 0 = fill color, line 5 border color, etc. + if fill_color is not None: + ap_tab[0] = color_string(fill_color, "f") + ap_updated = True + + if idx_BT == 7: + if bwidth > 0: + if border_color is not None: + ap_tab[4] = color_string(border_color, "s") + ap_updated = True + else: # for zero border width suppress border + ap_tab[3] = b"0 w" + ap_tab[4] = ap_tab[5] = ap_tab[6] = b"" + ap_updated = True + + if text_color is not None: + ap_tab[idx_BT + 1] = color_string(text_color, "f") + ap_updated = True + + if fontsize > 0.0: + x = ap_tab[idx_BT + 2].split() + x[1] = b"%g" % fontsize + ap_tab[idx_BT + 2] = b" ".join(x) + ap_updated = True + + if ap_updated: + ap = b"\n".join(ap_tab) # updated AP stream + + if bfill != "": + if type == ANNOT_POLYGON: + ap = ap[:-1] + bfill + b"b" # close, fill, and stroke + ap_updated = True + elif type == ANNOT_POLYLINE: + ap = ap[:-1] + bfill + b"B" # fill and stroke + ap_updated = True + + # Dashes not handled by MuPDF, so we do it here. + if dt: + dash = "[" + " ".join(map(str, dt)) + "] d\n" + ap = dash.encode("utf-8") + ap + # reset dashing - only applies for LINE annots with line ends given + ap = ap.replace(b"\nS\n", b"\nS\n[] d\n", 1) + ap_updated = True + + # Opacity not handled by MuPDF, so we do it here. The /ExtGState object + # "Alp0" referenced here has already been added by our C code. + if 0 <= self.opacity < 1: + ap = b"/Alp0 gs\n" + ap + ap_updated = True + + if line_end_le + line_end_ri > 0 and type in (ANNOT_POLYGON, ANNOT_POLYLINE): + le_funcs = (None, TOOLS._le_square, TOOLS._le_circle, + TOOLS._le_diamond, TOOLS._le_openarrow, + TOOLS._le_closedarrow, TOOLS._le_butt, + TOOLS._le_ropenarrow, TOOLS._le_rclosedarrow, + TOOLS._le_slash) + le_funcs_range = range(1, len(le_funcs)) + d = 4 * max(1, self.border["width"]) + rect = self.rect + (-d, -d, d, d) + ap_updated = True + points = self.vertices + ap = b"q\n" + ap + b"\nQ\n" + if line_end_le in le_funcs_range: + p1 = Point(points[0]) + p2 = Point(points[1]) + left = le_funcs[line_end_le](self, p1, p2, False) + ap += bytes(left, "utf8") if str is not bytes else left + if line_end_ri in le_funcs_range: + p1 = Point(points[-2]) + p2 = Point(points[-1]) + left = le_funcs[line_end_ri](self, p1, p2, True) + ap += bytes(left, "utf8") if str is not bytes else left + + if ap_updated: + if rect: # rect modified here? + self.setRect(rect) + self._setAP(ap, rect = 1) + else: + self._setAP(ap, rect = 0) + + # always perform a clean to wrap stream by "q" / "Q" + self._cleanContents()%} + + PyObject *update(float fontsize = 0.0f, + PyObject *text_color = NULL, + PyObject *border_color=NULL, + PyObject *fill_color = NULL, + int rotate = -1) { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); if (!annot) return NONE; + int type = pdf_annot_type(gctx, annot); fz_try(gctx) { - pdf_update_appearance(gctx, annot); + pdf_dirty_annot(gctx, annot); // enforce MuPDF /AP formatting + if (type == PDF_ANNOT_FREE_TEXT && rotate >= 0) + pdf_dict_put_int(gctx, annot->obj, PDF_NAME(Rotate), rotate); + pdf_update_annot(gctx, annot); } fz_catch(gctx) { PySys_WriteStderr("cannot update annot: '%s'\n", fz_caught_message(gctx)); Py_RETURN_FALSE; } + + // check /AP object + pdf_obj *ap = pdf_dict_getl(gctx, annot->obj, PDF_NAME(AP), + PDF_NAME(N), NULL); + if (!ap) + { + PySys_WriteStderr("annot has no /AP onject!\n"); + Py_RETURN_FALSE; + } + + // get opacity + pdf_obj *ca = pdf_dict_get(gctx, annot->obj, PDF_NAME(CA)); + if (!ca) // no opacity given + Py_RETURN_TRUE; + + pdf_obj *alp0 = pdf_new_dict(gctx, annot->page->doc, 2); + pdf_dict_put(gctx, alp0, PDF_NAME(CA), ca); + pdf_dict_put(gctx, alp0, PDF_NAME(ca), ca); + pdf_obj *extg = pdf_new_dict(gctx, annot->page->doc, 1); + pdf_dict_puts_drop(gctx, extg, "Alp0", alp0); + pdf_dict_putl_drop(gctx, ap, extg, PDF_NAME(Resources), + PDF_NAME(ExtGState), NULL); + pdf_dict_putl_drop(gctx, annot->obj, ap, PDF_NAME(AP), PDF_NAME(N), NULL); + annot->ap = NULL; + Py_RETURN_TRUE; } @@ -5072,18 +4537,6 @@ struct fz_annot_s //--------------------------------------------------------------------- PARENTCHECK(setColors) %feature("autodoc","setColors(dict)\nChanges the 'stroke' and 'fill' colors of an annotation. If provided, values must be lists of up to 4 floats.") setColors; - %pythonappend setColors %{ - if self.type[0] not in range(2, 8): - return - r = Rect(0, 0, self.rect.width, self.rect.height) - if self.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(self) - elif self.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(self) - else: - ap = _make_line_AP(self) - self._checkAP(r, ap) - %} void setColors(PyObject *colors) { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); @@ -5091,7 +4544,7 @@ struct fz_annot_s if (!PyDict_Check(colors)) return; if (pdf_annot_type(gctx, annot) == PDF_ANNOT_WIDGET) { - PySys_WriteStdout("use 'updateWidget' to change form fields\n"); + JM_Warning("use 'updateWidget' to change form fields"); return; } PyObject *ccol, *icol; @@ -5110,7 +4563,7 @@ struct fz_annot_s fz_try(gctx) pdf_set_annot_color(gctx, annot, n, col); fz_catch(gctx) - PySys_WriteStdout("cannot set stroke color for this annot type\n"); + JM_Warning("cannot set stroke color for this annot type"); } n = 0; if (icol) @@ -5120,7 +4573,7 @@ struct fz_annot_s { if (!pdf_annot_has_interior_color(gctx, annot)) { - PySys_WriteStdout("annot type has no fill color\n"); + JM_Warning("annot type has no fill color"); return; } for (i=0; iobj, PDF_NAME_CA); + pdf_obj *ca = pdf_dict_get(gctx, annot->obj, PDF_NAME(CA)); if (pdf_is_number(gctx, ca)) return pdf_to_real(gctx, ca); return -1.0f; @@ -5223,7 +4667,6 @@ struct fz_annot_s // annotation set opacity //--------------------------------------------------------------------- PARENTCHECK(setOpacity) - %pythonappend setOpacity %{_upd_my_AP(self)%} void setOpacity(float opacity) { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); @@ -5351,30 +4794,30 @@ struct fz_annot_s int type = (int) pdf_annot_type(gctx, annot); if (type != ANNOT_FILEATTACHMENT) THROWMSG("not a file attachment annot"); - stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME_FS, - PDF_NAME_EF, PDF_NAME_F, NULL); + stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME(FS), + PDF_NAME(EF), PDF_NAME(F), NULL); if (!stream) THROWMSG("bad PDF: file entry not found"); } fz_catch(gctx) return NULL; - fs = pdf_dict_get(gctx, annot->obj, PDF_NAME_FS); + fs = pdf_dict_get(gctx, annot->obj, PDF_NAME(FS)); - o = pdf_dict_get(gctx, fs, PDF_NAME_UF); - if (o) filename = pdf_to_utf8(gctx, o); + o = pdf_dict_get(gctx, fs, PDF_NAME(UF)); + if (o) filename = (char *) pdf_to_text_string(gctx, o); else { - o = pdf_dict_get(gctx, fs, PDF_NAME_F); - if (o) filename = pdf_to_utf8(gctx, o); + o = pdf_dict_get(gctx, fs, PDF_NAME(F)); + if (o) filename = (char *) pdf_to_text_string(gctx, o); } - o = pdf_dict_get(gctx, fs, PDF_NAME_Desc); - if (o) desc = pdf_to_utf8(gctx, o); + o = pdf_dict_get(gctx, fs, PDF_NAME(Desc)); + if (o) desc = (char *) pdf_to_text_string(gctx, o); - o = pdf_dict_get(gctx, stream, PDF_NAME_Length); + o = pdf_dict_get(gctx, stream, PDF_NAME(Length)); if (o) length = pdf_to_int(gctx, o); - o = pdf_dict_getl(gctx, stream, PDF_NAME_Params, - PDF_NAME_Size, NULL); + o = pdf_dict_getl(gctx, stream, PDF_NAME(Params), + PDF_NAME(Size), NULL); if (o) size = pdf_to_int(gctx, o); PyDict_SetItemString(res, "filename", JM_UNICODE(filename)); @@ -5403,8 +4846,8 @@ struct fz_annot_s int type = (int) pdf_annot_type(gctx, annot); if (type != ANNOT_FILEATTACHMENT) THROWMSG("not a file attachment annot"); - stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME_FS, - PDF_NAME_EF, PDF_NAME_F, NULL); + stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME(FS), + PDF_NAME(EF), PDF_NAME(F), NULL); if (!stream) THROWMSG("bad PDF: file entry not found"); buf = pdf_load_stream(gctx, stream); res = JM_BinFromBuffer(gctx, buf); @@ -5435,43 +4878,43 @@ struct fz_annot_s int type = (int) pdf_annot_type(gctx, annot); if (type != ANNOT_FILEATTACHMENT) THROWMSG("not a file attachment annot"); - stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME_FS, - PDF_NAME_EF, PDF_NAME_F, NULL); + stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME(FS), + PDF_NAME(EF), PDF_NAME(F), NULL); // the object for file content if (!stream) THROWMSG("bad PDF: file entry not found"); - fs = pdf_dict_get(gctx, annot->obj, PDF_NAME_FS); + fs = pdf_dict_get(gctx, annot->obj, PDF_NAME(FS)); // file content is ignored if not bytes / bytearray size = (int64_t) JM_CharFromBytesOrArray(buffer, &data); if (size > 0) { - pdf_obj *s = pdf_new_int(gctx, NULL, size); - pdf_dict_put(gctx, stream, PDF_NAME_Filter, - PDF_NAME_FlateDecode); + pdf_obj *s = pdf_new_int(gctx, size); + pdf_dict_put(gctx, stream, PDF_NAME(Filter), + PDF_NAME(FlateDecode)); pdf_dict_putl_drop(gctx, stream, s, - PDF_NAME_Params, PDF_NAME_Size, NULL); + PDF_NAME(Params), PDF_NAME(Size), NULL); res = JM_deflatebuf(gctx, data, size); pdf_update_stream(gctx, pdf, stream, res, 1); } if (filename) // new filename given { - pdf_dict_put_text_string(gctx, stream, PDF_NAME_F, filename); - pdf_dict_put_text_string(gctx, fs, PDF_NAME_F, filename); + pdf_dict_put_text_string(gctx, stream, PDF_NAME(F), filename); + pdf_dict_put_text_string(gctx, fs, PDF_NAME(F), filename); } if (ufilename) { - pdf_dict_put_text_string(gctx, stream, PDF_NAME_UF, filename); - pdf_dict_put_text_string(gctx, fs, PDF_NAME_UF, filename); + pdf_dict_put_text_string(gctx, stream, PDF_NAME(UF), filename); + pdf_dict_put_text_string(gctx, fs, PDF_NAME(UF), filename); } if (desc) // new description given { - pdf_dict_put_text_string(gctx, stream, PDF_NAME_Desc, desc); - pdf_dict_put_text_string(gctx, fs, PDF_NAME_Desc, desc); + pdf_dict_put_text_string(gctx, stream, PDF_NAME(Desc), desc); + pdf_dict_put_text_string(gctx, fs, PDF_NAME(Desc), desc); } } fz_always(gctx) @@ -5495,39 +4938,39 @@ struct fz_annot_s if (!annot) return res; // not a PDF pdf_obj *o; char *c; - c = pdf_copy_annot_contents(gctx, annot); + c = (char *) pdf_annot_contents(gctx, annot); PyDict_SetItemString(res, "content", JM_UNICODE(c)); - o = pdf_dict_get(gctx, annot->obj, PDF_NAME_Name); + o = pdf_dict_get(gctx, annot->obj, PDF_NAME(Name)); c = (char *) pdf_to_name(gctx, o); PyDict_SetItemString(res, "name", JM_UNICODE(c)); // Title, author - o = pdf_dict_get(gctx, annot->obj, PDF_NAME_T); - c = pdf_to_utf8(gctx, o); + o = pdf_dict_get(gctx, annot->obj, PDF_NAME(T)); + c = (char *) pdf_to_text_string(gctx, o); PyDict_SetItemString(res, "title", JM_UNICODE(c)); // CreationDate o = pdf_dict_gets(gctx, annot->obj, "CreationDate"); - c = pdf_to_utf8(gctx, o); + c = (char *) pdf_to_text_string(gctx, o); PyDict_SetItemString(res, "creationDate", JM_UNICODE(c)); // ModDate - o = pdf_dict_get(gctx, annot->obj, PDF_NAME_M); - c = pdf_to_utf8(gctx, o); + o = pdf_dict_get(gctx, annot->obj, PDF_NAME(M)); + c = (char *) pdf_to_text_string(gctx, o); PyDict_SetItemString(res, "modDate", JM_UNICODE(c)); // Subj o = pdf_dict_gets(gctx, annot->obj, "Subj"); - c = pdf_to_utf8(gctx, o); + c = (char *) pdf_to_text_string(gctx, o); PyDict_SetItemString(res, "subject", JM_UNICODE(c)); return res; } - /**********************************************************************/ + //--------------------------------------------------------------------- // annotation set information - /**********************************************************************/ + //--------------------------------------------------------------------- FITZEXCEPTION(setInfo, !result) PARENTCHECK(setInfo) PyObject *setInfo(PyObject *info) @@ -5569,7 +5012,7 @@ struct fz_annot_s if (uc) { pdf_dict_put_text_string(gctx, annot->obj, - PDF_NAME_CreationDate, uc); + PDF_NAME(CreationDate), uc); JM_Python_str_DelForPy3(uc); } @@ -5578,7 +5021,7 @@ struct fz_annot_s if (uc) { pdf_dict_put_text_string(gctx, annot->obj, - PDF_NAME_M, uc); + PDF_NAME(M), uc); JM_Python_str_DelForPy3(uc); } @@ -5587,7 +5030,7 @@ struct fz_annot_s if (uc) { pdf_dict_puts_drop(gctx, annot->obj, "Subj", - pdf_new_text_string(gctx, NULL, uc)); + pdf_new_text_string(gctx, uc)); JM_Python_str_DelForPy3(uc); } } @@ -5596,154 +5039,27 @@ struct fz_annot_s return NONE; } - /**********************************************************************/ + //--------------------------------------------------------------------- // annotation border - // PDF dictionaries checked are /Border, /BS, and /BE - // return a dictionary - /**********************************************************************/ + //--------------------------------------------------------------------- PARENTCHECK(border) %pythoncode %{@property%} PyObject *border() { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); if (!annot) return NONE; // not a PDF - PyObject *res = PyDict_New(); - PyObject *dash_py = PyList_New(0); - PyObject *effect_py = PyList_New(0); - int i; - char *effect2 = NULL, *style = NULL; - float width = 0; - int effect1 = -1; - - pdf_obj *o = pdf_dict_get(gctx, annot->obj, PDF_NAME_Border); - if (pdf_is_array(gctx, o)) - { - width = pdf_to_real(gctx, pdf_array_get(gctx, o, 2)); - if (pdf_array_len(gctx, o) == 4) - { - pdf_obj *dash = pdf_array_get(gctx, o, 3); - for (i = 0; i < pdf_array_len(gctx, dash); i++) - PyList_Append(dash_py, Py_BuildValue("i", - pdf_to_int(gctx, pdf_array_get(gctx, dash, i)))); - } - } - - pdf_obj *bs_o = pdf_dict_get(gctx, annot->obj, PDF_NAME_BS); - if (bs_o) - { - o = pdf_dict_get(gctx, bs_o, PDF_NAME_W); - if (o) width = pdf_to_real(gctx, o); - o = pdf_dict_get(gctx, bs_o, PDF_NAME_S); - if (o) - { - style = (char *) pdf_to_name(gctx, o); - } - o = pdf_dict_get(gctx, bs_o, PDF_NAME_D); - if (o) - { - for (i = 0; i < pdf_array_len(gctx, o); i++) - PyList_Append(dash_py, Py_BuildValue("i", - pdf_to_int(gctx, pdf_array_get(gctx, o, i)))); - } - } - - pdf_obj *be_o = pdf_dict_gets(gctx, annot->obj, "BE"); - if (be_o) - { - o = pdf_dict_get(gctx, be_o, PDF_NAME_S); - if (o) effect2 = (char *) pdf_to_name(gctx, o); - o = pdf_dict_get(gctx, be_o, PDF_NAME_I); - if (o) effect1 = pdf_to_int(gctx, o); - } - - PyList_Append(effect_py, Py_BuildValue("i", effect1)); - PyList_Append(effect_py, Py_BuildValue("s", effect2)); - - PyDict_SetItemString(res, "width", Py_BuildValue("f", width)); - - PyDict_SetItemString(res, "dashes", dash_py); - - PyDict_SetItemString(res, "style", Py_BuildValue("s", style)); - - if (effect1 > -1) PyDict_SetItemString(res, "effect", effect_py); - Py_CLEAR(effect_py); - Py_CLEAR(dash_py); - return res; + return JM_annot_border(gctx, annot->obj); } //--------------------------------------------------------------------- // set annotation border //--------------------------------------------------------------------- PARENTCHECK(setBorder) - %pythonappend setBorder %{_upd_my_AP(self)%} PyObject *setBorder(PyObject *border) { pdf_annot *annot = pdf_annot_from_fz_annot(gctx, $self); if (!annot) return NONE; // not a PDF - if (!PyDict_Check(border)) - { - PySys_WriteStdout("arg must be a dict"); - return NONE; // not a dict - } - - pdf_document *doc = annot->page->doc; // PDF document - float nwidth = -1; // new width - PyObject *ndashes = NULL; // new dashes - PyObject *nstyle = NULL; // new style - float owidth = -1; // old width - PyObject *odashes = NULL; // old dashes - PyObject *ostyle = NULL; // old style - - nwidth = (float) PyFloat_AsDouble(PyDict_GetItemString(border, "width")); - PyErr_Clear(); - ndashes = PyDict_GetItemString(border, "dashes"); - nstyle = PyDict_GetItemString(border, "style"); - - // first get old border properties - PyObject *oborder = fz_annot_s_border($self); - owidth = (float) PyFloat_AsDouble(PyDict_GetItemString(oborder, "width")); - PyErr_Clear(); - odashes = PyDict_GetItemString(oborder, "dashes"); - ostyle = PyDict_GetItemString(oborder, "style"); - - // then delete current annot entries - pdf_dict_del(gctx, annot->obj, PDF_NAME_BS); - pdf_dict_del(gctx, annot->obj, PDF_NAME_BE); - pdf_dict_del(gctx, annot->obj, PDF_NAME_Border); - - Py_ssize_t i, n; - int d; - // populate new border array - if (nwidth < 0) nwidth = owidth; // no new width: take current - if (nwidth < 0) nwidth = 0; // default if no width given - - if (!ndashes) ndashes = odashes; // no new dashes: take old - - if (!nstyle) nstyle = ostyle; // no new style: take old - - if (ndashes && PySequence_Check(ndashes) && PySequence_Size(ndashes)) - { - n = PySequence_Size(ndashes); - pdf_obj *darr = pdf_new_array(gctx, doc, n); - for (i = 0; i < n; i++) - { - d = (int) PyInt_AsLong(PySequence_GetItem(ndashes, i)); - pdf_array_push_int(gctx, darr, (int64_t) d); - } - pdf_dict_putl_drop(gctx, annot->obj, darr, PDF_NAME_BS, PDF_NAME_D, NULL); - } - - pdf_dict_putl_drop(gctx, annot->obj, pdf_new_real(gctx, doc, nwidth), - PDF_NAME_BS, PDF_NAME_W, NULL); - - pdf_obj *val = JM_get_border_style(gctx, nstyle); - - pdf_dict_putl_drop(gctx, annot->obj, val, - PDF_NAME_BS, PDF_NAME_S, NULL); - - pdf_dirty_annot(gctx, annot); - PyErr_Clear(); - return NONE; + return JM_annot_set_border(gctx, border, annot->page->doc, annot->obj); } //--------------------------------------------------------------------- @@ -5814,12 +5130,11 @@ struct fz_annot_s //--------------------------------------------------------------------- FITZEXCEPTION(getPixmap, !result) PARENTCHECK(getPixmap) - struct fz_pixmap_s *getPixmap(struct fz_matrix_s *matrix = NULL, struct fz_colorspace_s *colorspace = NULL, int alpha = 0) + struct fz_pixmap_s *getPixmap(PyObject *matrix = NULL, struct fz_colorspace_s *colorspace = NULL, int alpha = 0) { - struct fz_matrix_s *ctm = (fz_matrix *) &fz_identity; + fz_matrix ctm = JM_matrix_from_py(matrix); struct fz_colorspace_s *cs = fz_device_rgb(gctx); fz_pixmap *pix = NULL; - if (matrix) ctm = matrix; if (colorspace) cs = colorspace; fz_try(gctx) @@ -5869,13 +5184,13 @@ struct fz_annot_s Py_BuildValue("s", border_style)); float border_width = pdf_to_real(gctx, pdf_dict_getl(gctx, annot->obj, - PDF_NAME_BS, PDF_NAME_W, NULL)); + PDF_NAME(BS), PDF_NAME(W), NULL)); if (border_width == 0.0f) border_width = 1.0f; PyObject_SetAttrString(Widget, "border_width", Py_BuildValue("f", border_width)); pdf_obj *dashes = pdf_dict_getl(gctx, annot->obj, - PDF_NAME_BS, PDF_NAME_D, NULL); + PDF_NAME(BS), PDF_NAME(D), NULL); if (pdf_is_array(gctx, dashes)) { n = (Py_ssize_t) pdf_array_len(gctx, dashes); @@ -5888,7 +5203,7 @@ struct fz_annot_s Py_CLEAR(d); } - int text_maxlen = pdf_to_int(gctx, pdf_get_inheritable(gctx, pdf, annot->obj, PDF_NAME_MaxLen)); + int text_maxlen = pdf_to_int(gctx, pdf_dict_get_inheritable(gctx, annot->obj, PDF_NAME(MaxLen))); PyObject_SetAttrString(Widget, "text_maxlen", Py_BuildValue("i", text_maxlen)); @@ -5898,7 +5213,7 @@ struct fz_annot_s Py_BuildValue("i", text_type)); pdf_obj *bgcol = pdf_dict_getl(gctx, annot->obj, - PDF_NAME_MK, PDF_NAME_BG, NULL); + PDF_NAME(MK), PDF_NAME(BG), NULL); if (pdf_is_array(gctx, bgcol)) { n = (Py_ssize_t) pdf_array_len(gctx, bgcol); @@ -5911,7 +5226,7 @@ struct fz_annot_s Py_CLEAR(col); } - pdf_obj *bccol = pdf_dict_getl(gctx, annot->obj, PDF_NAME_MK, PDF_NAME_BC, NULL); + pdf_obj *bccol = pdf_dict_getl(gctx, annot->obj, PDF_NAME(MK), PDF_NAME(BC), NULL); if (pdf_is_array(gctx, bccol)) { @@ -5925,12 +5240,12 @@ struct fz_annot_s Py_CLEAR(col); } - char *da = pdf_to_str_buf(gctx, pdf_get_inheritable(gctx, - pdf, annot->obj, PDF_NAME_DA)); + char *da = pdf_to_str_buf(gctx, pdf_dict_get_inheritable(gctx, + annot->obj, PDF_NAME(DA))); PyObject_SetAttrString(Widget, "_text_da", Py_BuildValue("s", da)); pdf_obj *ca = pdf_dict_getl(gctx, annot->obj, - PDF_NAME_MK, PDF_NAME_CA, NULL); + PDF_NAME(MK), PDF_NAME(CA), NULL); if (ca) PyObject_SetAttrString(Widget, "button_caption", JM_UNICODE(pdf_to_str_buf(gctx, ca))); @@ -6022,9 +5337,9 @@ struct fz_annot_s }; %clearnodefaultctor; -/*****************************************************************************/ +//----------------------------------------------------------------------------- // fz_link -/*****************************************************************************/ +//----------------------------------------------------------------------------- %rename(Link) fz_link_s; %nodefaultctor; struct fz_link_s @@ -6037,6 +5352,84 @@ struct fz_link_s DEBUGMSG2; } + PyObject *_border(struct fz_document_s *doc, int xref) + { + pdf_document *pdf = pdf_specifics(gctx, doc); + if (!pdf) return NONE; + pdf_obj *link_obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!link_obj) return NONE; + PyObject *b = JM_annot_border(gctx, link_obj); + pdf_drop_obj(gctx, link_obj); + return b; + } + + PyObject *_setBorder(PyObject *border, struct fz_document_s *doc, int xref) + { + pdf_document *pdf = pdf_specifics(gctx, doc); + if (!pdf) return NONE; + pdf_obj *link_obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!link_obj) return NONE; + PyObject *b = JM_annot_set_border(gctx, border, pdf, link_obj); + pdf_drop_obj(gctx, link_obj); + return b; + } + + PyObject *_colors(struct fz_document_s *doc, int xref) + { + pdf_document *pdf = pdf_specifics(gctx, doc); + if (!pdf) return NONE; + pdf_obj *link_obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!link_obj) return NONE; + PyObject *b = JM_annot_colors(gctx, link_obj); + pdf_drop_obj(gctx, link_obj); + return b; + } + + PyObject *_setColors(PyObject *colors, struct fz_document_s *doc, int xref) + { + pdf_document *pdf = pdf_specifics(gctx, doc); + pdf_obj *arr = NULL; + int i; + if (!pdf) return NONE; + if (!PyDict_Check(colors)) return NONE; + float scol[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + int nscol = 0; + float fcol[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + int nfcol = 0; + PyObject *stroke = PyDict_GetItemString(colors, "stroke"); + PyObject *fill = PyDict_GetItemString(colors, "fill"); + JM_color_FromSequence(stroke, &nscol, scol); + JM_color_FromSequence(fill, &nfcol, fcol); + if (!nscol && !nfcol) return NONE; + pdf_obj *link_obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!link_obj) return NONE; + if (nscol > 0) + { + arr = pdf_new_array(gctx, pdf, nscol); + for (i = 0; i < nscol; i++) + pdf_array_push_real(gctx, arr, scol[i]); + pdf_dict_put_drop(gctx, link_obj, PDF_NAME(C), arr); + } + if (nfcol > 0) JM_Warning("this annot type has no fill color)"); + pdf_drop_obj(gctx, link_obj); + return NONE; + } + + %pythoncode %{ + @property + def border(self): + return self._border(self.parent.parent.this, self.xref) + + def setBorder(self, border): + return self._setBorder(border, self.parent.parent.this, self.xref) + + @property + def colors(self): + return self._colors(self.parent.parent.this, self.xref) + + def setColors(self, colors): + return self._setColors(colors, self.parent.parent.this, self.xref) + %} PARENTCHECK(uri) %pythoncode %{@property%} %pythonappend uri %{ @@ -6080,27 +5473,30 @@ struct fz_link_s PARENTCHECK(rect) %pythoncode %{@property%} - struct fz_rect_s *rect() + %pythonappend rect %{val = Rect(val)%} + PyObject *rect() { - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = $self->rect.x0; - r->y0 = $self->rect.y0; - r->x1 = $self->rect.x1; - r->y1 = $self->rect.y1; - return r; + return JM_py_from_rect($self->rect); } - /*********************************************************************/ + //--------------------------------------------------------------------- // next link - /*********************************************************************/ + //--------------------------------------------------------------------- // we need to increase the link refs number // so that it will not be freed when the head is dropped PARENTCHECK(next) - %pythonappend next -%{if val: - val.thisown = True - val.parent = self.parent # copy owning page object from previous annot - val.parent._annot_refs[id(val)] = val%} + %pythonappend next %{ + if val: + val.thisown = True + val.parent = self.parent # copy owning page from prev link + val.parent._annot_refs[id(val)] = val + if self.xref > 0: # prev link has an xref + link_xrefs = self.parent._getLinkXrefs() + idx = link_xrefs.index(self.xref) + val.xref = link_xrefs[idx + 1] + else: + val.xref = 0 + %} %pythoncode %{@property%} struct fz_link_s *next() { @@ -6134,9 +5530,9 @@ struct fz_link_s }; %clearnodefaultctor; -/*****************************************************************************/ -/* fz_display_list */ -/*****************************************************************************/ +//----------------------------------------------------------------------------- +// fz_display_list +//----------------------------------------------------------------------------- %rename(DisplayList) fz_display_list_s; struct fz_display_list_s { %extend @@ -6147,20 +5543,21 @@ struct fz_display_list_s { DEBUGMSG2; } FITZEXCEPTION(fz_display_list_s, !result) - fz_display_list_s(struct fz_rect_s *mediabox) + fz_display_list_s(PyObject *mediabox) { struct fz_display_list_s *dl = NULL; fz_try(gctx) - dl = fz_new_display_list(gctx, mediabox); + dl = fz_new_display_list(gctx, JM_rect_from_py(mediabox)); fz_catch(gctx) return NULL; return dl; } FITZEXCEPTION(run, result) - int run(struct DeviceWrapper *dw, const struct fz_matrix_s *m, const struct fz_rect_s *area) { + int run(struct DeviceWrapper *dw, PyObject *m, PyObject *area) { fz_try(gctx) { - fz_run_display_list(gctx, $self, dw->device, m, area, NULL); + fz_run_display_list(gctx, $self, dw->device, + JM_matrix_from_py(m), JM_rect_from_py(area), NULL); } fz_catch(gctx) return 1; return 0; @@ -6170,31 +5567,27 @@ struct fz_display_list_s { // DisplayList.rect //--------------------------------------------------------------------- %pythoncode%{@property%} - struct fz_rect_s *rect() + %pythonappend rect %{val = Rect(val)%} + PyObject *rect() { - fz_rect *mediabox = JM_Alloc(fz_rect, 1); - fz_bound_display_list(gctx, $self, mediabox); - return mediabox; + return JM_py_from_rect(fz_bound_display_list(gctx, $self)); } //--------------------------------------------------------------------- // DisplayList.getPixmap //--------------------------------------------------------------------- FITZEXCEPTION(getPixmap, !result) - struct fz_pixmap_s *getPixmap(const struct fz_matrix_s *matrix = NULL, struct fz_colorspace_s *colorspace = NULL, int alpha = 0, struct fz_rect_s *clip = NULL) + struct fz_pixmap_s *getPixmap(PyObject *matrix = NULL, struct fz_colorspace_s *colorspace = NULL, int alpha = 0, PyObject *clip = NULL) { - struct fz_matrix_s *m = NULL; struct fz_colorspace_s *cs = NULL; fz_pixmap *pix = NULL; - if (matrix) m = (fz_matrix *) matrix; - else m = (fz_matrix *) &fz_identity; if (colorspace) cs = colorspace; else cs = fz_device_rgb(gctx); fz_try(gctx) { - pix = JM_pixmap_from_display_list(gctx, $self, m, cs, alpha, clip); + pix = JM_pixmap_from_display_list(gctx, $self, matrix, cs, alpha, clip); } fz_catch(gctx) return NULL; return pix; @@ -6231,10 +5624,10 @@ struct fz_display_list_s { struct fz_stext_page_s { %extend { FITZEXCEPTION(fz_stext_page_s, !result) - fz_stext_page_s(struct fz_rect_s *mediabox) { + fz_stext_page_s(PyObject *mediabox) { struct fz_stext_page_s *tp = NULL; fz_try(gctx) - tp = fz_new_stext_page(gctx, mediabox); + tp = fz_new_stext_page(gctx, JM_rect_from_py(mediabox)); fz_catch(gctx) return NULL; return tp; } @@ -6248,28 +5641,36 @@ struct fz_stext_page_s { //--------------------------------------------------------------------- // method search() //--------------------------------------------------------------------- - %pythonappend search %{ - if val: - nval = [] - for r in val: - nval.append(Rect(r)) - val = nval + %pythonappend search%{ + if len(val) == 0: + return val + nval = [] + for v in val: + q = Quad(v) + if not quads: + nval.append(q.rect) + else: + nval.append(q) + val = nval %} - PyObject *search(const char *needle, int hit_max=16) + PyObject *search(const char *needle, int hit_max=16, int quads = 0) { - fz_rect *result = NULL; + fz_quad *result = NULL; PyObject *liste = PyList_New(0); int i, mymax = hit_max; if (mymax < 1) mymax = 16; - result = JM_Alloc(fz_rect, (mymax+1)); - struct fz_rect_s *rect = (struct fz_rect_s *) result; + result = JM_Alloc(fz_quad, (mymax+1)); + struct fz_quad_s *quad = (struct fz_quad_s *) result; int count = fz_search_stext_page(gctx, $self, needle, result, hit_max); for (i = 0; i < count; i++) { PyList_Append(liste, - Py_BuildValue("ffff", rect->x0, rect->y0, - rect->x1,rect->y1)); - rect += 1; + Py_BuildValue("(ff),(ff),(ff),(ff)", + quad->ul.x, quad->ul.y, + quad->ur.x, quad->ur.y, + quad->ll.x, quad->ll.y, + quad->lr.x, quad->lr.y)); + quad += 1; } JM_Free(result); return liste; @@ -6291,7 +5692,7 @@ struct fz_stext_page_s { fz_buffer *res = NULL; for (block = $self->first_block; block; block = block->next) { - fz_rect *blockrect = JM_empty_rect(); + fz_rect blockrect = block->bbox; if (block->type == FZ_STEXT_BLOCK_TEXT) { fz_try(gctx) @@ -6301,24 +5702,23 @@ struct fz_stext_page_s { float last_y0 = 0.0; for (line = block->u.t.first_line; line; line = line->next) { - fz_rect *linerect = JM_empty_rect(); + fz_rect linerect = line->bbox; // append line no. 2 with new-line if (line_n > 0) { - if (linerect->y0 != last_y0) + if (linerect.y0 != last_y0) fz_append_string(gctx, res, "\n"); else fz_append_string(gctx, res, " "); } - last_y0 = linerect->y0; + last_y0 = linerect.y0; line_n++; for (ch = line->first_char; ch; ch = ch->next) { fz_append_rune(gctx, res, ch->c); - JM_join_rect(linerect, &ch->bbox, ch->size); + linerect = fz_union_rect(linerect, JM_char_bbox(line, ch)); } - JM_join_rect(blockrect, linerect, 0.0f); - JM_Free(linerect); + blockrect = fz_union_rect(blockrect, linerect); } text = JM_StrFromBuffer(gctx, res); } @@ -6334,15 +5734,14 @@ struct fz_stext_page_s { fz_image *img = block->u.i.image; fz_colorspace *cs = img->colorspace; text = PyUnicode_FromFormat("", fz_colorspace_name(gctx, cs), img->w, img->h, img->bpc); - JM_join_rect(blockrect, &block->bbox, 0.0f); + blockrect = fz_union_rect(blockrect, block->bbox); } - litem = Py_BuildValue("ffffOii", blockrect->x0, blockrect->y0, - blockrect->x1, blockrect->y1, + litem = Py_BuildValue("ffffOii", blockrect.x0, blockrect.y0, + blockrect.x1, blockrect.y1, text, block_n, block->type); PyList_Append(lines, litem); Py_CLEAR(litem); Py_CLEAR(text); - JM_Free(blockrect); block_n++; } return lines; @@ -6360,7 +5759,7 @@ struct fz_stext_page_s { fz_buffer *buff = NULL; size_t buflen = 0; int block_n = 0, line_n, word_n; - fz_rect *wbbox = JM_empty_rect(); // word bbox + fz_rect wbbox = {0,0,0,0}; // word bbox PyObject *lines = PyList_New(0); for (block = $self->first_block; block; block = block->next) { @@ -6381,7 +5780,7 @@ struct fz_stext_page_s { continue; // skip spaces at line start if (ch->c == 32) { // --> finish the word - word_n = JM_append_word(gctx, lines, buff, wbbox, + word_n = JM_append_word(gctx, lines, buff, &wbbox, block_n, line_n, word_n); fz_drop_buffer(gctx, buff); buff = NULL; @@ -6393,11 +5792,11 @@ struct fz_stext_page_s { fz_append_rune(gctx, buff, ch->c); buflen++; // enlarge word bbox - JM_join_rect(wbbox, &ch->bbox, ch->size); + wbbox = fz_union_rect(wbbox, JM_char_bbox(line, ch)); } if (buff) // store any remaining word { - word_n = JM_append_word(gctx, lines, buff, wbbox, + word_n = JM_append_word(gctx, lines, buff, &wbbox, block_n, line_n, word_n); fz_drop_buffer(gctx, buff); buff = NULL; @@ -6407,7 +5806,6 @@ struct fz_stext_page_s { } block_n++; } - JM_Free(wbbox); return lines; } @@ -6420,13 +5818,18 @@ struct fz_stext_page_s { if format != 2: return val import base64, json - - def convertb64(s): - if str is bytes: - return base64.b64encode(s) - return base64.b64encode(s).decode() - - val = json.dumps(val, separators=(",", ":"), default=convertb64) + + class b64encode(json.JSONEncoder): + def default(self,s): + if str is not bytes and type(s) is bytes: + return base64.b64encode(s).decode() + if type(s) is bytearray: + if str is bytes: + return base64.b64encode(s) + else: + return base64.b64encode(s).decode() + + val = json.dumps(val, separators=(",", ":"), cls=b64encode, indent=1) %} PyObject *_extractText(int format) { @@ -6547,10 +5950,12 @@ struct Tools { %extend { - %feature("autodoc","Return a globally unique integer.") gen_id; + %feature("autodoc","Return a unique integer.") gen_id; int gen_id() { - return fz_gen_id(gctx); + JM_UNIQUE_ID += 1; + if (JM_UNIQUE_ID < 0) JM_UNIQUE_ID = 1; + return JM_UNIQUE_ID; } %feature("autodoc","Free 'percent' of current store size.") store_shrink; @@ -6586,16 +5991,282 @@ struct Tools return JM_fitz_config(); } - void _store_debug() - { - fz_debug_store(gctx); - } - %feature("autodoc","Empty the glyph cache.") glyph_cache_empty; void glyph_cache_empty() { fz_purge_glyph_cache(gctx); } + %pythoncode%{@property%} + PyObject *fitz_stderr() + { + return PyUnicode_Join(Py_BuildValue("s", ""), JM_error_log); + } + + %feature("autodoc","Empty fitz error log.") empty_error_log; + void fitz_stderr_reset() + { + Py_CLEAR(JM_error_log); + JM_error_log = PyList_New(0); + } + + PyObject *transform_rect(PyObject *rect, PyObject *matrix) + { + return JM_py_from_rect(fz_transform_rect(JM_rect_from_py(rect), JM_matrix_from_py(matrix))); + } + + PyObject *invert_matrix(PyObject *matrix) + { + fz_matrix src = JM_matrix_from_py(matrix); + float a = src.a; + float det = a * src.d - src.b * src.c; + if (det < -JM_EPS || det > JM_EPS) + { + fz_matrix dst; + float rdet = 1 / det; + dst.a = src.d * rdet; + dst.b = -src.b * rdet; + dst.c = -src.c * rdet; + dst.d = a * rdet; + a = -src.e * dst.a - src.f * dst.c; + dst.f = -src.e * dst.b - src.f * dst.d; + dst.e = a; + return JM_py_from_matrix(dst); + } + return Py_BuildValue("ffffff", 0,0,0,0,0,0); + } + %pythoncode %{ + +def _hor_matrix(self, C, P): + """Given two points C, P calculate matrix that rotates and translates the vector C -> P such that C is mapped to Point(0, 0), and P to some point on the x axis + """ + S = (P - C).unit # unit vector C -> P + return Matrix(1, 0, 0, 1, -C.x, -C.y) * Matrix(S.x, -S.y, S.y, S.x, 0, 0) + + +def _le_annot_parms(self, annot, p1, p2): + """Get common parameters for making line end symbols. + """ + w = annot.border["width"] # line width + sc = annot.colors["stroke"] # stroke color + if not sc: sc = (0,0,0) + scol = " ".join(map(str, sc)) + " RG\n" + fc = annot.colors["fill"] # fill color + if not fc: fc = (0,0,0) + fcol = " ".join(map(str, fc)) + " rg\n" + nr = annot.rect + h = nr.y1 + np1 = p1 # point coord relative to annot rect + np2 = p2 # point coord relative to annot rect + m = self._hor_matrix(np1, np2) # matrix makes the line horizontal + im = ~m # inverted matrix + L = np1 * m # converted start (left) point + R = np2 * m # converted end (right) point + if 0 <= annot.opacity < 1: + opacity = "/Alp0 gs\n" + else: + opacity = "" + return m, im, L, R, w, h, scol, fcol, opacity + + +def _oval_string(self, h, p1, p2, p3, p4): + """Return /AP string defining an oval within a 4-polygon provided as points + """ + def bezier(p, q, r): + f = "%g %g %g %g %g %g c\n" + return f % (p.x, p.y, q.x, q.y, r.x, r.y) + + kappa = 0.55228474983 # magic number + ml = p1 + (p4 - p1) * 0.5 # middle points ... + mo = p1 + (p2 - p1) * 0.5 # for each ... + mr = p2 + (p3 - p2) * 0.5 # polygon ... + mu = p4 + (p3 - p4) * 0.5 # side + ol1 = ml + (p1 - ml) * kappa # the 8 bezier + ol2 = mo + (p1 - mo) * kappa # helper points + or1 = mo + (p2 - mo) * kappa + or2 = mr + (p2 - mr) * kappa + ur1 = mr + (p3 - mr) * kappa + ur2 = mu + (p3 - mu) * kappa + ul1 = mu + (p4 - mu) * kappa + ul2 = ml + (p4 - ml) * kappa + # now draw, starting from middle point of left side + ap = "%g %g m\n" % (ml.x, ml.y) + ap += bezier(ol1, ol2, mo) + ap += bezier(or1, or2, mr) + ap += bezier(ur1, ur2, mu) + ap += bezier(ul1, ul2, ml) + return ap + + +def _le_diamond(self, annot, p1, p2, lr): + """Make stream commands for diamond line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 # 2*shift*width = length of square edge + d = shift * max(1, w) + M = R - (d/2., 0) if lr else L + (d/2., 0) + r = Rect(M, M) + (-d, -d, d, d) # the square + # the square makes line longer by (2*shift - 1)*width + p = (r.tl + (r.bl - r.tl) * 0.5) * im + ap = "q\n%s%g %g m\n" % (opacity, p.x, p.y) + p = (r.tl + (r.tr - r.tl) * 0.5) * im + ap += "%g %g l\n" % (p.x, p.y) + p = (r.tr + (r.br - r.tr) * 0.5) * im + ap += "%g %g l\n" % (p.x, p.y) + p = (r.br + (r.bl - r.br) * 0.5) * im + ap += "%g %g l\n" % (p.x, p.y) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + +def _le_square(self, annot, p1, p2, lr): + """Make stream commands for square line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 # 2*shift*width = length of square edge + d = shift * max(1, w) + M = R - (d/2., 0) if lr else L + (d/2., 0) + r = Rect(M, M) + (-d, -d, d, d) # the square + # the square makes line longer by (2*shift - 1)*width + p = r.tl * im + ap = "q\n%s%g %g m\n" % (opacity, p.x, p.y) + p = r.tr * im + ap += "%g %g l\n" % (p.x, p.y) + p = r.br * im + ap += "%g %g l\n" % (p.x, p.y) + p = r.bl * im + ap += "%g %g l\n" % (p.x, p.y) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + +def _le_circle(self, annot, p1, p2, lr): + """Make stream commands for circle line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 # 2*shift*width = length of square edge + d = shift * max(1, w) + M = R - (d/2., 0) if lr else L + (d/2., 0) + r = Rect(M, M) + (-d, -d, d, d) # the square + ap = "q\n" + opacity + self._oval_string(h, r.tl * im, r.tr * im, r.br * im, r.bl * im) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + +def _le_butt(self, annot, p1, p2, lr): + """Make stream commands for butt line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 3 + d = shift * max(1, w) + M = R if lr else L + top = (M + (0, -d/2.)) * im + bot = (M + (0, d/2.)) * im + ap = "\nq\n%s%g %g m\n" % (opacity, top.x, top.y) + ap += "%g %g l\n" % (bot.x, bot.y) + ap += "%g w\n" % w + ap += scol + "s\nQ\n" + return ap + + +def _le_slash(self, annot, p1, p2, lr): + """Make stream commands for slash line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + rw = 1.1547 * max(1, w) * 1.0 # makes rect diagonal a 30 deg inclination + M = R if lr else L + r = Rect(M.x - rw, M.y - 2 * w, M.x + rw, M.y + 2 * w) + top = r.tl * im + bot = r.br * im + ap = "\nq\n%s%g %g m\n" % (opacity, top.x, top.y) + ap += "%g %g l\n" % (bot.x, bot.y) + ap += "%g w\n" % w + ap += scol + "s\nQ\n" + return ap + + +def _le_openarrow(self, annot, p1, p2, lr): + """Make stream commands for open arrow line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 + d = shift * max(1, w) + p2 = R + (d/2., 0) if lr else L - (d/2., 0) + p1 = p2 + (-2*d, -d) if lr else p2 + (2*d, -d) + p3 = p2 + (-2*d, d) if lr else p2 + (2*d, d) + p1 *= im + p2 *= im + p3 *= im + ap = "\nq\n%s%g %g m\n" % (opacity, p1.x, p1.y) + ap += "%g %g l\n" % (p2.x, p2.y) + ap += "%g %g l\n" % (p3.x, p3.y) + ap += "%g w\n" % w + ap += scol + "S\nQ\n" + return ap + + +def _le_closedarrow(self, annot, p1, p2, lr): + """Make stream commands for closed arrow line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 + d = shift * max(1, w) + p2 = R + (d/2., 0) if lr else L - (d/2., 0) + p1 = p2 + (-2*d, -d) if lr else p2 + (2*d, -d) + p3 = p2 + (-2*d, d) if lr else p2 + (2*d, d) + p1 *= im + p2 *= im + p3 *= im + ap = "\nq\n%s%g %g m\n" % (opacity, p1.x, p1.y) + ap += "%g %g l\n" % (p2.x, p2.y) + ap += "%g %g l\n" % (p3.x, p3.y) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + +def _le_ropenarrow(self, annot, p1, p2, lr): + """Make stream commands for right open arrow line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 + d = shift * max(1, w) + p2 = R - (d/3., 0) if lr else L + (d/3., 0) + p1 = p2 + (2*d, -d) if lr else p2 + (-2*d, -d) + p3 = p2 + (2*d, d) if lr else p2 + (-2*d, d) + p1 *= im + p2 *= im + p3 *= im + ap = "\nq\n%s%g %g m\n" % (opacity, p1.x, p1.y) + ap += "%g %g l\n" % (p2.x, p2.y) + ap += "%g %g l\n" % (p3.x, p3.y) + ap += "%g w\n" % w + ap += scol + fcol + "S\nQ\n" + return ap + + +def _le_rclosedarrow(self, annot, p1, p2, lr): + """Make stream commands for right closed arrow line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 + d = shift * max(1, w) + p2 = R - (2*d, 0) if lr else L + (2*d, 0) + p1 = p2 + (2*d, -d) if lr else p2 + (-2*d, -d) + p3 = p2 + (2*d, d) if lr else p2 + (-2*d, d) + p1 *= im + p2 *= im + p3 *= im + ap = "\nq\n%s%g %g m\n" % (opacity, p1.x, p1.y) + ap += "%g %g l\n" % (p2.x, p2.y) + ap += "%g %g l\n" % (p3.x, p3.y) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + %} } }; diff --git a/fitz/fitz.py b/fitz/fitz.py index 033472022..4911bcc00 100644 --- a/fitz/fitz.py +++ b/fitz/fitz.py @@ -99,12 +99,909 @@ class _object: import weakref from binascii import hexlify import math +import platform +platform_bitness = platform.architecture()[0] +del platform -VersionFitz = "1.13.0" -VersionBind = "1.13.20" -VersionDate = "2018-09-09 09:54:14" -version = (VersionBind, VersionFitz, "20180909095414") +VersionFitz = "1.14.0" +VersionBind = "1.14.0" +VersionDate = "2018-11-16 05:14:22" +version = (VersionBind, VersionFitz, "20181116051422") + + +class Matrix(): + """Matrix() - all zeros\nMatrix(a, b, c, d, e, f)\nMatrix(zoom-x, zoom-y) - zoom\nMatrix(shear-x, shear-y, 1) - shear\nMatrix(degree) - rotate\nMatrix(Matrix) - new copy\nMatrix(sequence) - from 'sequence'""" + def __init__(self, *args): + if not args: + self.a = self.b = self.c = self.d = self.e = self.f = 0.0 + return None + if len(args) > 6: + raise ValueError("illegal sequ. length") + if len(args) == 6: # 6 numbers + self.a = float(args[0]) + self.b = float(args[1]) + self.c = float(args[2]) + self.d = float(args[3]) + self.e = float(args[4]) + self.f = float(args[5]) + return None + if len(args) == 1: # either an angle or a sequ + if hasattr(args[0], "__float__"): + theta = args[0] * math.pi / 180.0 + c = math.cos(theta) + s = math.sin(theta) + self.a = self.d = c + self.b = s + self.c = -s + self.e = self.f = 0.0 + return None + else: + self.a = float(args[0][0]) + self.b = float(args[0][1]) + self.c = float(args[0][2]) + self.d = float(args[0][3]) + self.e = float(args[0][4]) + self.f = float(args[0][5]) + return None + if len(args) == 2 or len(args) == 3 and args[2] == 0: + self.a, self.b, self.c, self.d, self.e, self.f = float(args[0]), \ + 0.0, 0.0, float(args[1]), 0.0, 0.0 + return None + if len(args) == 3 and args[2] == 1: + self.a, self.b, self.c, self.d, self.e, self.f = 1.0, \ + float(args[1]), float(args[0]), 1.0, 0.0, 0.0 + return None + raise ValueError("illegal Matrix constructor") + + def invert(self, src=None): + """Calculate the inverted matrix. Return 0 if successful and replace + current one. Else return 1 and do nothing. + """ + if src is None: + src = self + dst = TOOLS.invert_matrix(src) + if min(dst) >= max(dst): + return 1 + self.a = dst[0] + self.b = dst[1] + self.c = dst[2] + self.d = dst[3] + self.e = dst[4] + self.f = dst[5] + return 0 + + def preTranslate(self, tx, ty): + """Calculate pre translation and replace current matrix.""" + self.e += tx * self.a + ty * self.c + self.f += tx * self.b + ty * self.d + return self + + def preScale(self, sx, sy): + """Calculate pre scaling and replacing current matrix.""" + self.a *= sx + self.b *= sx + self.c *= sy + self.d *= sy + return self + + def preShear(self, h, v): + """Calculate pre shearing and replace current matrix.""" + a, b = self.a, self.b + self.a += v * self.c + self.b += v * self.d + self.c += h * a + self.d += h * b + return self + + def preRotate(self, theta): + """Calculate pre rotation and replace current matrix.""" + while theta < 0: theta += 360 + while theta >= 360: theta -= 360 + epsilon = 1e-5 + if abs(0 - theta) < epsilon: + pass + + elif abs(90.0 - theta) < epsilon: + a = self.a + b = self.b + self.a = self.c + self.b = self.d + self.c = -a + self.d = -b + + elif abs(180.0 - theta) < epsilon: + self.a = -self.a + self.b = -self.b + self.c = -self.c + self.d = -self.d + + elif abs(270.0 - theta) < epsilon: + a = self.a + b = self.b + self.a = -self.c + self.b = -self.d + self.c = a + self.d = b + + else: + s = math.sin(theta * math.pi / 180.0) + c = math.cos(theta * math.pi / 180.0) + a = self.a + b = self.b + self.a = c * a + s * self.c + self.b = c * b + s * self.d + self.c =-s * a + c * self.c + self.d =-s * b + c * self.d + + return self + + def concat(self, one, two): + """Multiply two matrices and replace current one.""" + dst = Matrix() + dst.a = one[0] * two[0] + one[1] * two[2] + dst.b = one[0] * two[1] + one[1] * two[3] + dst.c = one[2] * two[0] + one[3] * two[2] + dst.d = one[2] * two[1] + one[3] * two[3] + dst.e = one[4] * two[0] + one[5] * two[2] + two[4] + dst.f = one[4] * two[1] + one[5] * two[3] + two[5] + self = dst + return self + + def __getitem__(self, i): + return (self.a, self.b, self.c, self.d, self.e, self.f)[i] + + def __setitem__(self, i, v): + if i == 0: self.a = v + elif i == 1: self.b = v + elif i == 2: self.c = v + elif i == 3: self.d = v + elif i == 4: self.e = v + elif i == 5: self.f = v + else: + raise IndexError("index out of range") + return + + def __len__(self): + return 6 + + def __repr__(self): + return "Matrix" + str(tuple(self)) + + def __invert__(self): + m1 = Matrix() + m1.invert(self) + return m1 + __inv__ = __invert__ + + def __mul__(self, m): + if hasattr(m, "__float__"): + return Matrix(self.a * m, self.b * m, self.c * m, + self.d * m, self.e * m, self.f * m) + a = self.a * m[0] + self.b * m[2] + b = self.a * m[1] + self.b * m[3] + c = self.c * m[0] + self.d * m[2] + d = self.c * m[1] + self.d * m[3] + e = self.e * m[0] + self.f * m[2] + m[4] + f = self.e * m[1] + self.f * m[3] + m[5] + return Matrix(a, b, c, d, e, f) + + def __truediv__(self, m): + if hasattr(m, "__float__"): + return Matrix(self.a * 1./m, self.b * 1./m, self.c * 1./m, + self.d * 1./m, self.e * 1./m, self.f * 1./m) + m1 = TOOLS.invert_matrix(m) + if min(m1) >= max(m1): + raise ZeroDivisionError("op2 is not invertible") + return self.concat(self, m1) + __div__ = __truediv__ + + def __add__(self, m): + if hasattr(m, "__float__"): + return Matrix(self.a + m, self.b + m, self.c + m, + self.d + m, self.e + m, self.f + m) + return Matrix(self.a + m[0], self.b + m[1], self.c + m[2], + self.d + m[3], self.e + m[4], self.f + m[5]) + + def __sub__(self, m): + if hasattr(m, "__float__"): + return Matrix(self.a - m, self.b - m, self.c - m, + self.d - m, self.e - m, self.f - m) + return Matrix(self.a - m[0], self.b - m[1], self.c - m[2], + self.d - m[3], self.e - m[4], self.f - m[5]) + + def __pos__(self): + return Matrix(self) + + def __neg__(self): + return Matrix(-self.a, -self.b, -self.c, -self.d, -self.e, -self.f) + + def __bool__(self): + return max(self) > 0 or min(self) < 0 + + def __nonzero__(self): + return max(self) > 0 or min(self) < 0 + + def __eq__(self, m): + if not hasattr(m, "__len__"): + return False + return len(m) == 6 and bool(self - m) is False + + def __abs__(self): + return math.sqrt(self.a*self.a + self.b*self.b + self.c*self.c + \ + self.d*self.d + self.e*self.e + self.f*self.f) + + @property + def isRectilinear(self): + epsilon = 1e-5 + return (abs(self.b) < epsilon and abs(self.c) < epsilon) or \ + (abs(self.a) < epsilon and abs(self.d) < epsilon); + + def __hash__(self): + return hash(tuple(self)) + + +class IdentityMatrix(Matrix): + """Identity matrix [1, 0, 0, 1, 0, 0]""" + def __init__(self): + Matrix.__init__(self, 1.0, 1.0) + def __setattr__(self, name, value): + if name in "ad": + self.__dict__[name] = 1.0 + elif name in "bcef": + self.__dict__[name] = 0.0 + else: + self.__dict__[name] = value + + def checkargs(*args): + raise NotImplementedError("Identity is readonly") + preRotate = checkargs + preShear = checkargs + preScale = checkargs + preTranslate = checkargs + concat = checkargs + invert = checkargs + def __repr__(self): + return "IdentityMatrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)" + +Identity = IdentityMatrix() + +class Point(): + """Point() - all zeros\nPoint(x, y)\nPoint(Point) - new copy\nPoint(sequence) - from 'sequence'""" + def __init__(self, *args): + if not args: + self.x = 0.0 + self.y = 0.0 + return None + + if len(args) > 2: + raise ValueError("illegal sequ. length") + if len(args) == 2: + self.x = float(args[0]) + self.y = float(args[1]) + return None + if len(args) == 1: + l = args[0] + if hasattr(l, "__getitem__") is False: + raise ValueError("illegal Point constructor") + if len(l) != 2: + raise ValueError("illegal sequ. length") + self.x = float(l[0]) + self.y = float(l[1]) + return None + raise ValueError("illegal Point constructor") + + def transform(self, m): + """Replace point by its transformation with matrix m.""" + x = self.x + self.x = x * m[0] + self.y * m[2] + m[4] + self.y = x * m[1] + self.y * m[3] + m[5] + return self + + @property + def unit(self): + """Return unit vector of a point.""" + s = self.x * self.x + self.y * self.y + if s < 1e-5: + return Point(0,0) + s = math.sqrt(s) + return Point(self.x / s, self.y / s) + + @property + def abs_unit(self): + """Return unit vector of a point with positive coordinates.""" + s = self.x * self.x + self.y * self.y + if s < 1e-5: + return Point(0,0) + s = math.sqrt(s) + return Point(abs(self.x) / s, abs(self.y) / s) + + def distance_to(self, *args): + """Return the distance to a rectangle or another point.""" + assert len(args) > 0, "at least one parameter must be given" + x = args[0] + if len(args) > 1: + unit = args[1] + else: + unit = "px" + u = {"px": (1.,1.), "in": (1.,72.), "cm": (2.54, 72.), "mm": (25.4, 72.)} + f = u[unit][0] / u[unit][1] + if type(x) is Point: + return abs(self - x) * f + +# from here on, x is a rectangle +# as a safeguard, make a finite copy of it + r = Rect(x.top_left, x.top_left) + r = r | x.bottom_right + if self in r: + return 0.0 + if self.x > r.x1: + if self.y >= r.y1: + return self.distance_to(r.bottom_right, unit) + elif self.y <= r.y0: + return self.distance_to(r.top_right, unit) + else: + return (self.x - r.x1) * f + elif r.x0 <= self.x <= r.x1: + if self.y >= r.y1: + return (self.y - r.y1) * f + else: + return (r.y0 - self.y) * f + else: + if self.y >= r.y1: + return self.distance_to(r.bottom_left, unit) + elif self.y <= r.y0: + return self.distance_to(r.top_left, unit) + else: + return (r.x0 - self.x) * f + + def __getitem__(self, i): + return (self.x, self.y)[i] + + def __len__(self): + return 2 + + def __setitem__(self, i, v): + if i == 0: self.x = float(v) + elif i == 1: self.y = float(v) + else: + raise IndexError("index out of range") + return None + + def __repr__(self): + return "Point" + str(tuple(self)) + + def __pos__(self): + return Point(self) + + def __neg__(self): + return Point(-self.x, -self.y) + + def __bool__(self): + return max(self) > 0 or min(self) < 0 + + def __nonzero__(self): + return max(self) > 0 or min(self) < 0 + + def __eq__(self, p): + if not hasattr(p, "__len__"): + return False + return len(p) == 2 and bool(self - p) is False + + def __abs__(self): + return math.sqrt(self.x * self.x + self.y * self.y) + + def __add__(self, p): + if hasattr(p, "__float__"): + return Point(self.x + p, self.y + p) + return Point(self.x + p[0], self.y + p[1]) + + def __sub__(self, p): + if hasattr(p, "__float__"): + return Point(self.x - p, self.y - p) + return Point(self.x - p[0], self.y - p[1]) + + def __mul__(self, m): + if hasattr(m, "__float__"): + return Point(self.x * m, self.y * m) + p = Point(self) + return p.transform(m) + + def __truediv__(self, m): + if hasattr(m, "__float__"): + return Point(self.x * 1./m, self.y * 1./m) + m1 = TOOLS.invert_matrix(m) + if min(m1) >= max(m1): + raise ZeroDivisionError("op2 is not invertible") + p = Point(self) + return p.transform(m1) + + __div__ = __truediv__ + + def __hash__(self): + return hash(tuple(self)) + +class Rect(): + """Rect() - all zeros\nRect(x0, y0, x1, y1)\nRect(top-left, x1, y1)\nRect(x0, y0, bottom-right)\nRect(top-left, bottom-right)\nRect(Rect or IRect) - new copy\nRect(sequence) - from 'sequence'""" + def __init__(self, *args): + if not args: + self.x0 = self.y0 = self.x1 = self.y1 = 0.0 + return None + + if len(args) > 4: + raise ValueError("invalid sequ. length") + if len(args) == 4: + self.x0 = float(args[0]) + self.y0 = float(args[1]) + self.x1 = float(args[2]) + self.y1 = float(args[3]) + return None + if len(args) == 1: + l = args[0] + if hasattr(l, "__getitem__") is False: + raise ValueError("invalid Rect constructor") + if len(l) != 4: + raise ValueError("invalid sequ. length") + self.x0 = float(l[0]) + self.y0 = float(l[1]) + self.x1 = float(l[2]) + self.y1 = float(l[3]) + return None + if len(args) == 2: # 2 Points provided + self.x0 = float(args[0][0]) + self.y0 = float(args[0][1]) + self.x1 = float(args[1][0]) + self.y1 = float(args[1][1]) + return None + if len(args) == 3: # 2 floats and 1 Point provided + a0 = args[0] + a1 = args[1] + a2 = args[2] + if hasattr(a0, "__float__"): # (float, float, Point) provided + self.x0 = float(a0) + self.y0 = float(a1) + self.x1 = float(a2[0]) + self.y1 = float(a2[1]) + return None + self.x0 = float(a0[0]) # (Point, float, float) provided + self.y0 = float(a0[1]) + self.x1 = float(a1) + self.y1 = float(a2) + return None + raise ValueError("invalid Rect constructor") + + def normalize(self): + """Replace rectangle with its finite version.""" + if self.x1 < self.x0: + self.x0, self.x1 = self.x1, self.x0 + if self.y1 < self.y0: + self.y0, self.y1 = self.y1, self.y0 + return self + + @property + def isEmpty(self): + """Check if rectangle area is empty.""" + return self.x0 == self.x1 or self.y0 == self.y1 + + @property + def isInfinite(self): + """Check if rectangle is infinite.""" + return self.x0 > self.x1 or self.y0 > self.y1 + + @property + def top_left(self): + return Point(self.x0, self.y0) + + @property + def top_right(self): + return Point(self.x1, self.y0) + + @property + def bottom_left(self): + return Point(self.x0, self.y1) + + @property + def bottom_right(self): + return Point(self.x1, self.y1) + + tl = top_left + tr = top_right + bl = bottom_left + br = bottom_right + + @property + def quad(self): + return Quad(self.tl, self.tr, self.bl, self.br) + + def round(self): + r = Rect(self).normalize() + ir = IRect(math.floor(r.x0), math.floor(r.y0), math.ceil(r.x1), math.ceil(r.y1)) + return ir + + irect = property(round) + + width = property(lambda self: abs(self.x1 - self.x0)) + height = property(lambda self: abs(self.y1 - self.y0)) + + def includePoint(self, p): + """Extend rectangle to include point p.""" + x0 = min(self.x0, self.x1, p[0]) + x1 = max(self.x0, self.x1, p[0]) + y0 = min(self.y0, self.y1, p[1]) + y1 = max(self.y0, self.y1, p[1]) + self.x0 = x0 + self.y0 = y0 + self.x1 = x1 + self.y1 = y1 + return self + + def includeRect(self, r): + """Extend rectangle to include rectangle r.""" + x0 = min(self.x0, self.x1, r[0], r[2]) + x1 = max(self.x0, self.x1, r[0], r[2]) + y0 = min(self.y0, self.y1, r[1], r[3]) + y1 = max(self.y0, self.y1, r[1], r[3]) + self.x0 = x0 + self.y0 = y0 + self.x1 = x1 + self.y1 = y1 + return self + + def intersect(self, r): + """Restrict rectangle to common area with rectangle r.""" + if self.isEmpty: return Rect() + r1 = Rect(r) + if r1.isEmpty: return Rect() + if r1.isInfinite: return self + if self.isInfinite: return r1 + x0 = max(self.x0, r1.x0) + x1 = min(self.x1, r1.x1) + y0 = max(self.y0, r1.y0) + y1 = min(self.y1, r1.y1) + if x1 < x0 or y1 < y0: + self = Rect() + else: + self = Rect(x0, y0, x1, y1) + return self + + + def __getitem__(self, i): + return (self.x0, self.y0, self.x1, self.y1)[i] + + def __len__(self): + return 4 + + def __setitem__(self, i, v): + if i == 0: self.x0 = float(v) + elif i == 1: self.y0 = float(v) + elif i == 2: self.x1 = float(v) + elif i == 3: self.y1 = float(v) + else: + raise IndexError("index out of range") + return None + + def __repr__(self): + return "Rect" + str(tuple(self)) + + def __pos__(self): + return Rect(self) + + def __neg__(self): + return Rect(-self.x0, -self.y0, -self.x1, -self.y1) + + def __bool__(self): + return max(self) > 0 or min(self) < 0 + + def __nonzero__(self): + return max(self) > 0 or min(self) < 0 + + def __eq__(self, p): + if not hasattr(p, "__len__"): + return False + return len(p) == 4 and bool(self - p) is False + + def __abs__(self): + if self.isEmpty or self.isInfinite: + return 0.0 + return (self.x1 - self.x0) * (self.y1 - self.y0) + + def __add__(self, p): + if hasattr(p, "__float__"): + r = Rect(self.x0 + p, self.y0 + p, self.x1 + p, self.y1 + p) + else: + r = Rect(self.x0 + p[0], self.y0 + p[1], self.x1 + p[2], self.y1 + p[3]) + return r + + def __sub__(self, p): + if hasattr(p, "__float__"): + return Rect(self.x0 - p, self.y0 - p, self.x1 - p, self.y1 - p) + return Rect(self.x0 - p[0], self.y0 - p[1], self.x1 - p[2], self.y1 - p[3]) + + def transform(self, m): + """Replace rectangle with its transformation by matrix m.""" + self = Rect(TOOLS.transform_rect(self, m)) + return self + + def __mul__(self, m): + if hasattr(m, "__float__"): + return Rect(self.x0 * m, self.y0 * m, self.x1 * m, self.y1 * m) + r = Rect(self) + r = r.transform(m) + return r + + def __truediv__(self, m): + if hasattr(m, "__float__"): + return Rect(self.x0 * 1./m, self.y0 * 1./m, self.x1 * 1./m, self.y1 * 1./m) + im = TOOLS.invert_matrix(m) + if min(im) >= max(im): + raise ZeroDivisionError("op2 is not invertible") + r = Rect(self) + r = r.transform(im) + return r + + __div__ = __truediv__ + + def __contains__(self, x): + if hasattr(x, "__float__"): + return x in tuple(self) + l = len(x) + r = Rect(self).normalize() + if l == 4: + if r.isEmpty: return False + xr = Rect(x).normalize() + if xr.isEmpty: return True + if r.x0 <= xr.x0 and r.y0 <= xr.y0 and \ + r.x1 >= xr.x1 and r.y1 >= xr.y1: + return True + return False + if l == 2: + if r.x0 <= x[0] <= r.x1 and \ + r.y0 <= x[1] <= r.y1: + return True + return False + return False + + def __or__(self, x): + if not hasattr(x, "__len__"): + raise ValueError("op2 is an unsupported type") + + r = Rect(self) + if len(x) == 2: + return r.includePoint(x) + if len(x) == 4: + return r.includeRect(x) + raise ValueError("op2 is an unsupported type") + + def __and__(self, x): + if not hasattr(x, "__len__"): + raise ValueError("op2 is an unsupported type") + + r1 = Rect(x) + r = Rect(self) + return r.intersect(r1) + + def intersects(self, x): + """Check if intersection with rectangle x is not empty.""" + r1 = Rect(x) + if self.isEmpty or self.isInfinite or r1.isEmpty: + return False + r = Rect(self) + if r.intersect(r1).isEmpty: + return False + return True + + def __hash__(self): + return hash(tuple(self)) + +class IRect(Rect): + """IRect() - all zeros\nIRect(x0, y0, x1, y1)\nIRect(Rect or IRect) - new copy\nIRect(sequence) - from 'sequence'""" + def __init__(self, *args): + Rect.__init__(self, *args) + self.x0 = math.floor(self.x0) + self.y0 = math.floor(self.y0) + self.x1 = math.ceil(self.x1) + self.y1 = math.ceil(self.y1) + return None + + @property + def round(self): + pass + + irect = round + + @property + def rect(self): + return Rect(self) + + def __repr__(self): + return "IRect" + str(tuple(self)) + + def includePoint(self, p): + """Extend rectangle to include point p.""" + return Rect.includePoint(self, p).round() + + def includeRect(self, r): + """Extend rectangle to include rectangle r.""" + return Rect.includeRect(self, r).round() + + def intersect(self, r): + """Restrict rectangle to intersection with rectangle r.""" + return Rect.intersect(self, r).round() + + def __setitem__(self, i, v): + if i == 0: self.x0 = int(v) + elif i == 1: self.y0 = int(v) + elif i == 2: self.x1 = int(v) + elif i == 3: self.y1 = int(v) + else: + raise IndexError("index out of range") + return None + + def __pos__(self): + return IRect(self) + + def __neg__(self): + return IRect(-self.x0, -self.y0, -self.x1, -self.y1) + + def __add__(self, p): + return Rect.__add__(self, p).round() + + def __sub__(self, p): + return Rect.__sub__(self, p).round() + + def transform(self, m): + return Rect.transform(self, m).round() + + def __mul__(self, m): + return Rect.__mul__(self, m).round() + + def __truediv__(self, m): + return Rect.__truediv__(self, m).round() + + def __or__(self, x): + return Rect.__or__(self, x).round() + + def __and__(self, x): + return Rect.__and__(self, x).round() + +class Quad(): + """Quad() - all zero points\nQuad(ul, ur, ll, lr)\nQuad(quad) - new copy\nQuad(sequence) - from 'sequence'""" + def __init__(self, *args): + if not args: + self.ul = self.ur = self.ll = self.lr = Point() + return None + + if len(args) > 4: + raise ValueError("invalid sequ. length") + if len(args) == 4: + self.ul = Point(args[0]) + self.ur = Point(args[1]) + self.ll = Point(args[2]) + self.lr = Point(args[3]) + return None + if len(args) == 1: + l = args[0] + if hasattr(l, "__getitem__") is False: + raise ValueError("invalid Quad constructor") + if len(l) != 4: + raise ValueError("invalid sequ. length") + self.ul = Point(l[0]) + self.ur = Point(l[1]) + self.ll = Point(l[2]) + self.lr = Point(l[3]) + return None + raise ValueError("invalid Quad constructor") + + @property + def isRectangular(self): + upper = (self.ur - self.ul).unit + if not bool(upper): + return False + right = (self.lr - self.ur).unit + if not bool(right): + return False + left = (self.ll - self.ul).unit + if not bool(left): + return False + lower = (self.lr - self.ll).unit + if not bool(lower): + return False + eps = 1e-5 + + return abs(sum(map(lambda x,y: x*y, upper, right))) <= eps and \ + abs(sum(map(lambda x,y: x*y, upper, left))) <= eps and \ + abs(sum(map(lambda x,y: x*y, left, lower))) <= eps + + @property + def isEmpty(self): + if self.isRectangular: + return False + eps = 1e-5 + ul = Point() + ur = (self.ur - self.ul).abs_unit + lr = (self.lr - self.ul).abs_unit + ll = (self.ll - self.ul).abs_unit + if max(ur.y, lr.y, ll.y) - min(ur.y, lr.y, ll.y) < eps: + return True + return False + + width = property(lambda self: max(abs(self.ul - self.ur), abs(self.ll - self.lr))) + height = property(lambda self: max(abs(self.ul - self.ll), abs(self.ur - self.lr))) + + @property + def rect(self): + return Rect(self.ul, self.ur) | self.ll | self.lr + + def __getitem__(self, i): + return (self.ul, self.ur, self.ll, self.lr)[i] + + def __len__(self): + return 4 + + def __setitem__(self, i, v): + if i == 0: self.ul = Point(v) + elif i == 1: self.ur = Point(v) + elif i == 2: self.ll = Point(v) + elif i == 3: self.lr = Point(v) + else: + raise IndexError("index out of range") + return None + + def __repr__(self): + return "Quad" + str(tuple(self)) + + def __pos__(self): + return Quad(self) + + def __neg__(self): + return Quad(-self.ul, -self.ur, -self.ll, -self.lr) + + def __bool__(self): + return not self.isEmpty + + def __nonzero__(self): + return not self.isEmpty + + def __eq__(self, p): + if not hasattr(p, "__len__"): + return False + return len(p) == 4 and self.ul == p[0] and self.ur == p[1] and \ + self.ll == p[3] and self.lr == p[3] + + def __abs__(self): + if self.isEmpty: + return 0.0 + return abs(self.ul - self.ur) * abs(self.ul - self.ll) + + def transform(self, m): + """Replace quad by its transformation with matrix m.""" + self.ul *= m + self.ur *= m + self.ll *= m + self.lr *= m + return self + + def __mul__(self, m): + r = Quad(self) + r = r.transform(m) + return r + + def __truediv__(self, m): + r = Quad(self) + if hasattr(m, "__float__"): + im = 1. / m + else: + im = TOOLS.invert_matrix(m) + if min(im) >= max(im): + raise ZeroDivisionError("op2 is not invertible") + r = r.transform(im) + return r + + __div__ = __truediv__ + + def __hash__(self): + return hash(tuple(self)) + #------------------------------------------------------------------------------ @@ -297,6 +1194,24 @@ def _check5(self): TEXT_PRESERVE_WHITESPACE = 2 TEXT_PRESERVE_IMAGES = 4 +#------------------------------------------------------------------------------ +# Stamp annotation icon numbers +#------------------------------------------------------------------------------ +STAMP_Approved = 0 +STAMP_AsIs = 1 +STAMP_Confidential = 2 +STAMP_Departmental = 3 +STAMP_Experimental = 4 +STAMP_Expired = 5 +STAMP_Final = 6 +STAMP_ForComment = 7 +STAMP_ForPublicRelease = 8 +STAMP_NotApproved = 9 +STAMP_NotForPublicRelease = 10 +STAMP_Sold = 11 +STAMP_TopSecret = 12 +STAMP_Draft = 13 + #------------------------------------------------------------------------------ # Base 14 font names #------------------------------------------------------------------------------ @@ -311,7 +1226,7 @@ def _check5(self): # Emulate old linkDest class #------------------------------------------------------------------------------ class linkDest(): - '''link or outline destination details''' + """link or outline destination details""" def __init__(self, obj, rlink): isExt = obj.isExternal isInt = not isExt @@ -526,18 +1441,18 @@ def CheckParent(o): def CheckColor(c): if c is not None: - if type(c) not in (list, tuple) or len(c) != 3 or \ + if type(c) not in (list, tuple) or len(c) not in (1, 3, 4) or \ min(c) < 0 or max(c) > 1: - raise ValueError("need 3 color components in range 0 to 1") + raise ValueError("need 1, 3 or 4 color components in range 0 to 1") def CheckMorph(o): if not bool(o): return False if not (type(o) in (list, tuple) and len(o) == 2): raise ValueError("morph must be a sequence of length 2") if not (type(o[0]) == Point and issubclass(type(o[1]), Matrix)): - raise ValueError("invalid morph parameter") + raise ValueError("invalid morph parm 0") if not o[1].e == o[1].f == 0: - raise ValueError("invalid morph parameter") + raise ValueError("invalid morph parm 1") return True def CheckFont(page, fontname): @@ -636,392 +1551,10 @@ def ConversionTrailer(i): elif t == "xhtml": r = xhtml else: - r = text - - return r - -#------------------------------------------------------------------------------ -# -#------------------------------------------------------------------------------ -def _hor_matrix(C, P): - """Given two points C, P calculate matrix that rotates the line C -> P parallel to the x-axis. - """ - S = P - C # vector C -> P - try: - alfa = math.asin(abs(S.y) / abs(S)) # absolute angle from horizontal - except ZeroDivisionError: - print("points are too close:") - return Matrix() - if S.x < 0: # make arcsin result unique - if S.y <= 0: # bottom-left - alfa = -(math.pi - alfa) - else: # top-left - alfa = math.pi - alfa - else: - if S.y >= 0: # top-right - pass - else: # bottom-right - alfa = - alfa - ca = math.cos(alfa) - sa = math.sin(alfa) - m = Matrix(ca, -sa, sa, ca, -C.x, -C.y) - return m - -def _make_rect_AP(annot): - """ Create /AP stream for rectangle annotation. - """ - w = annot.border["width"] # get line width - sc = annot.colors["stroke"] # get stroke color - fc = annot.colors["fill"] # get fill color - ca = annot.opacity # get opacity value - Alp0 = "/Alp0 gs\n" if ca >= 0 else "" - scol = "%g %g %g RG " % (sc[0], sc[1], sc[2]) if sc else "0 0 0 RG " - fcol = "%g %g %g rg " % (fc[0], fc[1], fc[2]) if fc else "" - dt = annot.border.get("dashes") # get annot dashes - dtab = [] - if dt: - dtab = ["[", "]0 d"] - for n in dt: - dtab[0] += "%i " % n - dtab = "".join(dtab) # make dashes command - r = annot.rect # annot rectangle - r1 = r2 = w/2. # rect starts bottom-left here - r3 = r.width - w # rect width reduced by line width - r4 = r.height - w # rect height reduced by line with - ap = "q\n%s%g %g %g %g re %g w 1 J 1 j\n" % (Alp0, r1, r2, r3, r4, w) - ap += scol + fcol + dtab # appearance stream so far - if fcol: # have fill color? - ap += "\nb\nQ\n" - else: - ap += "\ns\nQ\n" - return ap - -def _le_annot_parms(annot, p1, p2): - """Get common parameters for making line end symbols. - """ - w = annot.border["width"] # line width - sc = annot.colors["stroke"] # stroke color - scol = "%g %g %g RG\n" % (sc[0], sc[1], sc[2]) if sc else "0 0 0 RG\n" - fc = annot.colors["fill"] # fill color - fcol = "%g %g %g rg\n" % (fc[0], fc[1], fc[2]) if fc else "1 1 1 rg\n" - delta = Point(annot.rect.x0, annot.rect.y0) - nr = annot.rect - Rect(delta, delta) - h = nr.height - np1 = p1 - delta # point coord relative to annot rect - np2 = p2 - delta # point coord relative to annot rect - m = _hor_matrix(np1, np2) # matrix makes the line horizontal - im = ~m # inverted matrix - L = np1 * m # converted start (left) point - R = np2 * m # converted end (right) point - return m, im, L, R, w, h, scol, fcol - -def _make_circle_AP(annot): - """Create /AP stream for circle annotation - """ - sc = annot.colors["stroke"] # stroke color - scol = "%g %g %g RG " % (sc[0], sc[1], sc[2]) if sc else "0 0 0 RG " - fc = annot.colors["fill"] # fill color - ca = annot.opacity # get opacity value - Alp0 = "/Alp0 gs\n" if ca >= 0 else "" - fcol = "%g %g %g rg " % (fc[0], fc[1], fc[2]) if fc else "" - dt = annot.border.get("dashes") # get annot dashes - dtab = [] - if dt: - dtab = ["[", "]0 d\n"] - for n in dt: - dtab[0] += "%i " % n - dtab = "".join(dtab) # the dashes command - lw = annot.border["width"] # line width - lw2 = lw / 2. - h = annot.rect.height - r = Rect(lw2, lw2, annot.rect.width - lw2, h - lw2) - - ap = "q\n" + Alp0 + _oval_string(h, r.tl, r.tr, r.br, r.bl) - ap += "%g w 1 J 1 j\n" % lw - ap += scol + fcol + dtab # ap stream so far - if fcol: # have fill color? - ap += "\nb\nQ\n" - else: - ap += "\ns\nQ\n" - return ap - -def _oval_string(h, p1, p2, p3, p4): - """Return /AP string defining an oval within a 4-polygon provided as points - """ - def bezier(p, q, r): - f = "%g %g %g %g %g %g c\n" - return f % (p.x, h - p.y, q.x, h - q.y, r.x, h - r.y) - - kappa = 0.55228474983 # magic number - ml = p1 + (p4 - p1) * 0.5 # middle points ... - mo = p1 + (p2 - p1) * 0.5 # for each ... - mr = p2 + (p3 - p2) * 0.5 # polygon ... - mu = p4 + (p3 - p4) * 0.5 # edge - ol1 = ml + (p1 - ml) * kappa # the 8 bezier - ol2 = mo + (p1 - mo) * kappa # helper points - or1 = mo + (p2 - mo) * kappa - or2 = mr + (p2 - mr) * kappa - ur1 = mr + (p3 - mr) * kappa - ur2 = mu + (p3 - mu) * kappa - ul1 = mu + (p4 - mu) * kappa - ul2 = ml + (p4 - ml) * kappa -# now draw, starting from middle point of left edge - ap = "%g %g m\n" % (ml.x, h - ml.y) - ap += bezier(ol1, ol2, mo) - ap += bezier(or1, or2, mr) - ap += bezier(ur1, ur2, mu) - ap += bezier(ul1, ul2, ml) - return ap - -def _le_diamond(annot, p1, p2, lr): - """Make stream commands for diamond line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - shift = 1.75 # 2*shift*width = length of square edge - d = w * shift - M = R - (w, 0) if lr else L + (w, 0) - r = Rect(M, M) + (-d, -d, d, d) # the square -# the square makes line longer by (2*shift - 1)*width - p = (r.tl + (r.bl - r.tl) * 0.5) * im - ap = "q\n%g %g m\n" % (p.x, h - p.y) - p = (r.tl + (r.tr - r.tl) * 0.5) * im - ap += "%g %g l\n" % (p.x, h - p.y) - p = (r.tr + (r.br - r.tr) * 0.5) * im - ap += "%g %g l\n" % (p.x, h - p.y) - p = (r.br + (r.bl - r.br) * 0.5) * im - ap += "%g %g l\n" % (p.x, h - p.y) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _le_square(annot, p1, p2, lr): - """Make stream commands for square line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - shift = 1.25 # 2*shift*width = length of square edge - d = w * shift - M = R - (w, 0) if lr else L + (w, 0) - r = Rect(M, M) + (-d, -d, d, d) # the square -# the square makes line longer by (2*shift - 1)*width - p = r.tl * im - ap = "q\n%g %g m\n" % (p.x, h - p.y) - p = r.tr * im - ap += "%g %g l\n" % (p.x, h - p.y) - p = r.br * im - ap += "%g %g l\n" % (p.x, h - p.y) - p = r.bl * im - ap += "%g %g l\n" % (p.x, h - p.y) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _le_circle(annot, p1, p2, lr): - """Make stream commands for circle line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - shift = 1.50 # 2*shift*width = length of square edge - d = w * shift - M = R - (w, 0) if lr else L + (w, 0) - r = Rect(M, M) + (-d, -d, d, d) # the square - ap = "q\n" + _oval_string(h, r.tl * im, r.tr * im, r.br * im, r.bl * im) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _le_butt(annot, p1, p2, lr): - """Make stream commands for butt line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - M = R if lr else L - top = (M + (0, -2 * w)) * im - bot = (M + (0, 2 * w)) * im - ap = "\nq\n%g %g m\n" % (top.x, h - top.y) - ap += "%g %g l\n" % (bot.x, h - bot.y) - ap += "%g w\n" % w - ap += scol + "s\nQ\n" - return ap - -def _le_slash(annot, p1, p2, lr): - """Make stream commands for slash line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - rw = 1.1547 * w * 0.5 # makes rect diagonal a 30 deg inclination - M = R if lr else L - r = Rect(M.x - rw, M.y - 2 * w, M.x + rw, M.y + 2 * w) - top = r.tl * im - bot = r.br * im - ap = "\nq\n%g %g m\n" % (top.x, h - top.y) - ap += "%g %g l\n" % (bot.x, h - bot.y) - ap += "%g w\n" % w - ap += scol + "s\nQ\n" - return ap - -def _le_openarrow(annot, p1, p2, lr): - """Make stream commands for open arrow line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - p2 = R + (1.5 * w, 0) if lr else L - (1.5 * w, 0) - p1 = p2 + (-3 * w, -1.5 * w) if lr else p2 + (3 * w, -1.5 * w) - p3 = p2 + (-3 * w, 1.5 * w) if lr else p2 + (3 * w, 1.5 * w) - p1 *= im - p2 *= im - p3 *= im - ap = "\nq\n%g %g m\n" % (p1.x, h - p1.y) - ap += "%g %g l\n" % (p2.x, h - p2.y) - ap += "%g %g l\n" % (p3.x, h - p3.y) - ap += "%g w\n" % w - ap += scol + "S\nQ\n" - return ap - -def _le_closedarrow(annot, p1, p2, lr): - """Make stream commands for closed arrow line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - p2 = R + (1.5 * w, 0) if lr else L - (1.5 * w, 0) - p1 = p2 + (-3 * w, -1.5 * w) if lr else p2 + (3 * w, -1.5 * w) - p3 = p2 + (-3 * w, 1.5 * w) if lr else p2 + (3 * w, 1.5 * w) - p1 *= im - p2 *= im - p3 *= im - ap = "\nq\n%g %g m\n" % (p1.x, h - p1.y) - ap += "%g %g l\n" % (p2.x, h - p2.y) - ap += "%g %g l\n" % (p3.x, h - p3.y) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _le_ropenarrow(annot, p1, p2, lr): - """Make stream commands for right open arrow line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - p2 = R - (0.5 * w, 0) if lr else L + (0.5 * w, 0) - p1 = p2 + (3 * w, -1.5 * w) if lr else p2 + (-3 * w, -1.5 * w) - p3 = p2 + (3 * w, 1.5 * w) if lr else p2 + (-3 * w, 1.5 * w) - p1 *= im - p2 *= im - p3 *= im - ap = "\nq\n%g %g m\n" % (p1.x, h - p1.y) - ap += "%g %g l\n" % (p2.x, h - p2.y) - ap += "%g %g l\n" % (p3.x, h - p3.y) - ap += "%g w\n" % w - ap += scol + fcol + "S\nQ\n" - return ap - -def _le_rclosedarrow(annot, p1, p2, lr): - """Make stream commands for right closed arrow line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - p2 = R - (3.0 * w, 0) if lr else L + (3.0 * w, 0) - p1 = p2 + (3 * w, -1.5 * w) if lr else p2 + (-3 * w, -1.5 * w) - p3 = p2 + (3 * w, 1.5 * w) if lr else p2 + (-3 * w, 1.5 * w) - p1 *= im - p2 *= im - p3 *= im - ap = "\nq\n%g %g m\n" % (p1.x, h - p1.y) - ap += "%g %g l\n" % (p2.x, h - p2.y) - ap += "%g %g l\n" % (p3.x, h - p3.y) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _make_line_AP(annot, nv = None, r0 = None): - """ Create the /AP stream for 'Line', 'PolyLine' and 'Polygon' annotations. - """ - w = annot.border["width"] # get line width - sc = annot.colors["stroke"] # get stroke color - fc = annot.colors["fill"] # get fill color - ca = annot.opacity # get opacity value - Alp0 = "/Alp0 gs\n" if ca >= 0 else "" - vert = nv if nv else annot.vertices # get list of points - rn = r0 if r0 else annot.rect - h = rn.height # annot rectangle height - r = Rect(0, 0, rn.width, h) # this is the /BBox of the /AP - x0 = rn.x0 # annot rect origin x - y0 = rn.y0 # annot rect origin y - scol = "%g %g %g RG\n" % (sc[0], sc[1], sc[2]) if sc else "0 0 0 RG\n" - fcol = "%g %g %g rg\n" % (fc[0], fc[1], fc[2]) if fc else "" - - dt = annot.border.get("dashes") # get annot dashes - dtab = [] - if dt: - dtab = ["[", "]0 d\n"] - for n in dt: - dtab[0] += "%i " % n - dtab = "".join(dtab) # dashes command - -# start /AP string with a goto command - ap = "q\n%s%g %g m\n" % (Alp0, vert[0][0] - x0, h - (vert[0][1] - y0)) - -# add line commands for all subsequent points - for v in vert[1:]: - ap += "%g %g l\n" % (v[0] - x0, h - (v[1] - y0)) - -# add color triples and other stuff commands - ap += scol + fcol + dtab + "%g w 1 J 1 j\n" % w - -# add stroke / fill & stroke command depending on type - if fcol and annot.type[0] == ANNOT_POLYGON: - ap += "b" - else: - ap += "S" - ap += "\nQ\n" - -# function names for creating line end symbols - _le_func = (None, _le_square, _le_circle, _le_diamond, _le_openarrow, - _le_closedarrow, _le_butt, _le_ropenarrow, - _le_rclosedarrow, _le_slash) - valid_range = range(1, len(_le_func)) - - le_left, le_right = annot.lineEnds # get line end symbol numbers - - if le_left in valid_range: # uses points 1 and 2 - func = _le_func[le_left] # function for left symbol - ap += func(annot, Point(vert[0]), Point(vert[1]), False) - - if le_right in valid_range: # uses last and second to last points - func = _le_func[le_right] # function for right symbol - ap += func(annot, Point(vert[-2]), Point(vert[-1]), True) - - return ap - -def _upd_my_AP(annot): - """Update /AP stream for annotation types we are handling. - """ - if annot.type[0] not in range(2, 8): # not our business - return - -# this is the /AP rect for circle or square - r = Rect(0, 0, annot.rect.width, annot.rect.height) - - if annot.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(annot) # stream for circle annot - annot._checkAP(r, ap) - return - - if annot.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(annot) # stream for square annot - annot._checkAP(r, ap) - return - -#-------------------------------------------------------------------------- -# we have one of the line annotations -#-------------------------------------------------------------------------- - -# first calculate the rect that contains all the points, including small -# extra rects around first and last point for any line symbols. - w = 3 * annot.border["width"] # 3 times the line width - ov = annot.vertices # get all the points - rect = Rect(Point(ov[0]), Point(ov[0])) + (-w, -w, w, w) - for v in ov[1:-1]: # include remaining points - rect |= v - rect |= Rect(Point(ov[-1]), Point(ov[-1])) + (-w, -w, w, w) + r = text - annot._setRect(rect) # set this as annot rect - r = Rect(0, 0, rect.width, rect.height) # derive the /AP rect + return r - ap = _make_line_AP(annot) # make the /AP stream - annot._checkAP(r, ap) # create or update /AP object - return class Document(_object): """open() - new empty PDF @@ -1071,9 +1604,7 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0 self.openErrCode = self._getGCTXerrcode() self.openErrMsg = self._getGCTXerrmsg() self.thisown = True - tools = Tools() - self._graft_id = tools.gen_id() - tools = None + self._graft_id = TOOLS.gen_id() if self.needsPass: self.isEncrypted = 1 else: # we won't init until doc is decrypted @@ -1199,7 +1730,7 @@ def embeddedFileAdd(self, buffer, name, filename=None, ufilename=None, desc=None def convertToPDF(self, from_page=0, to_page=-1, rotate=0): - """Convert document to PDF selecting copy range and optional rotation. Output bytes object.""" + """Convert document to PDF selecting page range and optional rotation. Output bytes object.""" if self.isClosed or self.isEncrypted: raise ValueError("operation illegal for closed / encrypted doc") @@ -1301,6 +1832,24 @@ def isPDF(self): @property + def _hasXrefStream(self): + """_hasXrefStream(self) -> PyObject *""" + if self.isClosed: + raise ValueError("operation illegal for closed doc") + + return _fitz.Document__hasXrefStream(self) + + @property + + def _hasXrefOldStyle(self): + """_hasXrefOldStyle(self) -> PyObject *""" + if self.isClosed: + raise ValueError("operation illegal for closed doc") + + return _fitz.Document__hasXrefOldStyle(self) + + @property + def isDirty(self): """isDirty(self) -> PyObject *""" if self.isClosed: @@ -1319,12 +1868,12 @@ def _getGCTXerrmsg(self): return _fitz.Document__getGCTXerrmsg(self) - def authenticate(self, arg2): + def authenticate(self, password): """Decrypt document with a password.""" if self.isClosed: raise ValueError("operation illegal for closed doc") - val = _fitz.Document_authenticate(self, arg2) + val = _fitz.Document_authenticate(self, password) if val: # the doc is decrypted successfully and we init the outline self.isEncrypted = 0 @@ -1335,8 +1884,8 @@ def authenticate(self, arg2): return val - def save(self, filename, garbage=0, clean=0, deflate=0, incremental=0, ascii=0, expand=0, linear=0, pretty=0): - """save(self, filename, garbage=0, clean=0, deflate=0, incremental=0, ascii=0, expand=0, linear=0, pretty=0) -> PyObject *""" + def save(self, filename, garbage=0, clean=0, deflate=0, incremental=0, ascii=0, expand=0, linear=0, pretty=0, decrypt=1): + """save(self, filename, garbage=0, clean=0, deflate=0, incremental=0, ascii=0, expand=0, linear=0, pretty=0, decrypt=1) -> PyObject *""" if self.isClosed or self.isEncrypted: raise ValueError("operation illegal for closed / encrypted doc") @@ -1347,21 +1896,27 @@ def save(self, filename, garbage=0, clean=0, deflate=0, incremental=0, ascii=0, else: raise TypeError("filename must be a string") if filename == self.name and not incremental: - raise ValueError("save to original requires incremental") - if incremental and (self.name != filename or self.streamlen > 0): - raise ValueError("incremental save needs original file") + raise ValueError("save to original must be incremental") + if self.pageCount < 1: + raise ValueError("cannot save with zero pages") + if incremental: + if self.name != filename or self.streamlen > 0: + raise ValueError("incremental needs original file") - return _fitz.Document_save(self, filename, garbage, clean, deflate, incremental, ascii, expand, linear, pretty) + return _fitz.Document_save(self, filename, garbage, clean, deflate, incremental, ascii, expand, linear, pretty, decrypt) - def write(self, garbage=0, clean=0, deflate=0, ascii=0, expand=0, linear=0, pretty=0): + def write(self, garbage=0, clean=0, deflate=0, ascii=0, expand=0, linear=0, pretty=0, decrypt=1): """Write document to a bytes object.""" if self.isClosed or self.isEncrypted: raise ValueError("operation illegal for closed / encrypted doc") + if self.pageCount < 1: + raise ValueError("cannot write with zero pages") - return _fitz.Document_write(self, garbage, clean, deflate, ascii, expand, linear, pretty) + + return _fitz.Document_write(self, garbage, clean, deflate, ascii, expand, linear, pretty, decrypt) def insertPDF(self, docsrc, from_page=-1, to_page=-1, start_at=-1, rotate=-1, links=1): @@ -1752,21 +2307,18 @@ def __init__(self, *args, **kwargs): __del__ = lambda self: None def bound(self): - """bound(self) -> Rect""" + """bound(self) -> PyObject *""" CheckParent(self) val = _fitz.Page_bound(self) - - if val: - val.thisown = True - + val = Rect(val) return val rect = property(bound, doc="page rectangle") def run(self, dw, m): - """run(self, dw, m) -> int""" + """run(self, dw, m) -> PyObject *""" CheckParent(self) return _fitz.Page_run(self, dw, m) @@ -1790,18 +2342,6 @@ def addLineAnnot(self, p1, p2): val.parent = weakref.proxy(self) self._annot_refs[id(val)] = val - if val.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(val) - elif val.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(val) - elif val.type[0] in (ANNOT_LINE, ANNOT_POLYLINE, ANNOT_POLYGON): - ap = _make_line_AP(val) - else: - return val - r = Rect(0, 0, val.rect.width, val.rect.height) - val._checkAP(r, ap) - - return val @@ -1819,8 +2359,36 @@ def addTextAnnot(self, point, text): return val + def addInkAnnot(self, list): + """Add a 'handwriting' as a list of list of point-likes. Each sublist forms an independent stroke.""" + CheckParent(self) + + val = _fitz.Page_addInkAnnot(self, list) + + if not val: return + val.thisown = True + val.parent = weakref.proxy(self) + self._annot_refs[id(val)] = val + + return val + + + def addStampAnnot(self, rect, stamp=0): + """Add a 'rubber stamp' in a rectangle.""" + CheckParent(self) + + val = _fitz.Page_addStampAnnot(self, rect, stamp) + + if not val: return + val.thisown = True + val.parent = weakref.proxy(self) + self._annot_refs[id(val)] = val + + return val + + def addFileAnnot(self, point, buffer, filename, ufilename=None, desc=None): - """Add a 'FileAttachment' annotation.""" + """Add a 'FileAttachment' annotation at location 'point'.""" CheckParent(self) val = _fitz.Page_addFileAnnot(self, point, buffer, filename, ufilename, desc) @@ -1834,7 +2402,7 @@ def addFileAnnot(self, point, buffer, filename, ufilename=None, desc=None): def addStrikeoutAnnot(self, rect): - """Strike out content in a rectangle.""" + """Strike out content in a rectangle or quadrilateral.""" CheckParent(self) val = _fitz.Page_addStrikeoutAnnot(self, rect) @@ -1848,7 +2416,7 @@ def addStrikeoutAnnot(self, rect): def addUnderlineAnnot(self, rect): - """Underline content in a rectangle.""" + """Underline content in a rectangle or quadrilateral.""" CheckParent(self) val = _fitz.Page_addUnderlineAnnot(self, rect) @@ -1861,8 +2429,22 @@ def addUnderlineAnnot(self, rect): return val + def addSquigglyAnnot(self, rect): + """Wavy underline content in a rectangle or quadrilateral.""" + CheckParent(self) + + val = _fitz.Page_addSquigglyAnnot(self, rect) + + if not val: return + val.thisown = True + val.parent = weakref.proxy(self) + self._annot_refs[id(val)] = val + + return val + + def addHighlightAnnot(self, rect): - """Highlight content in a rectangle.""" + """Highlight content in a rectangle or quadrilateral.""" CheckParent(self) val = _fitz.Page_addHighlightAnnot(self, rect) @@ -1886,18 +2468,6 @@ def addRectAnnot(self, rect): val.parent = weakref.proxy(self) self._annot_refs[id(val)] = val - if val.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(val) - elif val.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(val) - elif val.type[0] in (ANNOT_LINE, ANNOT_POLYLINE, ANNOT_POLYGON): - ap = _make_line_AP(val) - else: - return val - r = Rect(0, 0, val.rect.width, val.rect.height) - val._checkAP(r, ap) - - return val @@ -1912,18 +2482,6 @@ def addCircleAnnot(self, rect): val.parent = weakref.proxy(self) self._annot_refs[id(val)] = val - if val.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(val) - elif val.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(val) - elif val.type[0] in (ANNOT_LINE, ANNOT_POLYLINE, ANNOT_POLYGON): - ap = _make_line_AP(val) - else: - return val - r = Rect(0, 0, val.rect.width, val.rect.height) - val._checkAP(r, ap) - - return val @@ -1938,18 +2496,6 @@ def addPolylineAnnot(self, points): val.parent = weakref.proxy(self) self._annot_refs[id(val)] = val - if val.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(val) - elif val.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(val) - elif val.type[0] in (ANNOT_LINE, ANNOT_POLYLINE, ANNOT_POLYGON): - ap = _make_line_AP(val) - else: - return val - r = Rect(0, 0, val.rect.width, val.rect.height) - val._checkAP(r, ap) - - return val @@ -1964,26 +2510,14 @@ def addPolygonAnnot(self, points): val.parent = weakref.proxy(self) self._annot_refs[id(val)] = val - if val.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(val) - elif val.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(val) - elif val.type[0] in (ANNOT_LINE, ANNOT_POLYLINE, ANNOT_POLYGON): - ap = _make_line_AP(val) - else: - return val - r = Rect(0, 0, val.rect.width, val.rect.height) - val._checkAP(r, ap) - - return val - def addFreetextAnnot(self, pos, text, fontsize=11, color=None): - """Add a 'FreeText' annotation at position 'point'.""" + def addFreetextAnnot(self, rect, text, fontsize=12, fontname=None, color=None, rotate=0): + """Add a 'FreeText' annotation in rectangle 'rect'.""" CheckParent(self) - val = _fitz.Page_addFreetextAnnot(self, pos, text, fontsize, color) + val = _fitz.Page_addFreetextAnnot(self, rect, text, fontsize, fontname, color, rotate) if not val: return val.thisown = True @@ -2054,8 +2588,8 @@ def getDisplayList(self): return _fitz.Page_getDisplayList(self) - def setCropBox(self, rect=None): - """setCropBox(self, rect=None) -> PyObject *""" + def setCropBox(self, rect): + """setCropBox(self, rect) -> PyObject *""" CheckParent(self) return _fitz.Page_setCropBox(self, rect) @@ -2066,10 +2600,16 @@ def loadLinks(self): CheckParent(self) val = _fitz.Page_loadLinks(self) + if val: val.thisown = True val.parent = weakref.proxy(self) # owning page object self._annot_refs[id(val)] = val + if self.parent.isPDF: + val.xref = self._getLinkXrefs()[0] + else: + val.xref = 0 + return val @@ -2128,7 +2668,8 @@ def MediaBoxSize(self): val = _fitz.Page_MediaBoxSize(self) - if val == Point(0,0): + val = Point(val) + if not bool(val): r = self.rect val = Point(r.width, r.height) @@ -2141,7 +2682,10 @@ def CropBoxPosition(self): """Retrieve position of /CropBox. Return (0,0) for non-PDF, or no /CropBox.""" CheckParent(self) - return _fitz.Page_CropBoxPosition(self) + val = _fitz.Page_CropBoxPosition(self) + val = Point(val) + + return val @property @@ -2278,7 +2822,8 @@ def _reset_annot_refs(self): annot._erase() self._annot_refs.clear() - def _getXref(self): + @property + def xref(self): """Return PDF XREF number of page.""" CheckParent(self) return self.parent._getPageXref(self.number)[0] @@ -2311,294 +2856,16 @@ def CropBox(self): x0 = self.CropBoxPosition.x y0 = self.MediaBoxSize.y - self.CropBoxPosition.y - self.rect.height x1 = x0 + self.rect.width - y1 = y0 + self.rect.height - return Rect(x0, y0, x1, y1) - - @property - def MediaBox(self): - return Rect(0, 0, self.MediaBoxSize) - - -Page_swigregister = _fitz.Page_swigregister -Page_swigregister(Page) - - -def _fz_transform_rect(rect, transform): - """_fz_transform_rect(rect, transform) -> Rect""" - return _fitz._fz_transform_rect(rect, transform) -class Rect(_object): - """Rect() - all zeros -Rect(x0, y0, x1, y1) -Rect(top-left, x1, y1) -Rect(x0, y0, bottom-right) -Rect(top-left, bottom-right) -Rect(Rect or IRect) - new copy -Rect(sequence) - from 'sequence'""" - - __swig_setmethods__ = {} - __setattr__ = lambda self, name, value: _swig_setattr(self, Rect, name, value) - __swig_getmethods__ = {} - __getattr__ = lambda self, name: _swig_getattr(self, Rect, name) - __repr__ = _swig_repr - __swig_setmethods__["x0"] = _fitz.Rect_x0_set - __swig_getmethods__["x0"] = _fitz.Rect_x0_get - if _newclass: - x0 = _swig_property(_fitz.Rect_x0_get, _fitz.Rect_x0_set) - __swig_setmethods__["y0"] = _fitz.Rect_y0_set - __swig_getmethods__["y0"] = _fitz.Rect_y0_get - if _newclass: - y0 = _swig_property(_fitz.Rect_y0_get, _fitz.Rect_y0_set) - __swig_setmethods__["x1"] = _fitz.Rect_x1_set - __swig_getmethods__["x1"] = _fitz.Rect_x1_get - if _newclass: - x1 = _swig_property(_fitz.Rect_x1_get, _fitz.Rect_x1_set) - __swig_setmethods__["y1"] = _fitz.Rect_y1_set - __swig_getmethods__["y1"] = _fitz.Rect_y1_get - if _newclass: - y1 = _swig_property(_fitz.Rect_y1_get, _fitz.Rect_y1_set) - __swig_destroy__ = _fitz.delete_Rect - __del__ = lambda self: None - - def __init__(self, *args): - """ - __init__(self) -> Rect - __init__(self, s) -> Rect - __init__(self, lt, rb) -> Rect - __init__(self, x0, y0, rb) -> Rect - __init__(self, lt, x1, y1) -> Rect - __init__(self, x0, y0, x1, y1) -> Rect - __init__(self, list) -> Rect - """ - this = _fitz.new_Rect(*args) - try: - self.this.append(this) - except __builtin__.Exception: - self.this = this - - def round(self): - """Create enclosing 'IRect'""" - val = _fitz.Rect_round(self) - val.thisown = True - - return val - - - def includePoint(self, p): - """Enlarge to include a 'Point' p""" - return _fitz.Rect_includePoint(self, p) - - - def intersect(self, r): - """Shrink to intersection with another 'Rect' r""" - return _fitz.Rect_intersect(self, r) - - - def includeRect(self, r): - """Enlarge to include another 'Rect' r""" - return _fitz.Rect_includeRect(self, r) - - - def normalize(self): - """Make rectangle finite""" - return _fitz.Rect_normalize(self) - - - def contains(self, *args): - """Check if containing a 'Point' or another rect""" - return _fitz.Rect_contains(self, *args) - - @property - - def isEmpty(self): - """isEmpty(self) -> PyObject *""" - return _fitz.Rect_isEmpty(self) - - @property - - def isInfinite(self): - """isInfinite(self) -> PyObject *""" - return _fitz.Rect_isInfinite(self) - - - def transform(self, m): - """Transform rectangle with Matrix m.""" - _fitz._fz_transform_rect(self, m) - return self - - @property - def top_left(self): - """Return the rectangle's top-left point.""" - return Point(self.x0, self.y0) - - @property - def top_right(self): - """Return the rectangle's top-right point.""" - return Point(self.x1, self.y0) - - @property - def bottom_left(self): - """Return the rectangle's bottom-left point.""" - return Point(self.x0, self.y1) - - @property - def bottom_right(self): - """Return the rectangle's bottom-right point.""" - return Point(self.x1, self.y1) - - def __getitem__(self, i): - return (self.x0, self.y0, self.x1, self.y1)[i] - - def __setitem__(self, i, v): - if i == 0: self.x0 = v - elif i == 1: self.y0 = v - elif i == 2: self.x1 = v - elif i == 3: self.y1 = v - else: - raise IndexError("index out of range") - return - - def __len__(self): - return 4 - - def __repr__(self): - return "fitz.Rect" + str((self.x0, self.y0, self.x1, self.y1)) - - irect = property(round) - width = property(lambda self: self.x1-self.x0) - height = property(lambda self: self.y1-self.y0) - tl = top_left - tr = top_right - br = bottom_right - bl = bottom_left - -Rect_swigregister = _fitz.Rect_swigregister -Rect_swigregister(Rect) - -class IRect(_object): - """IRect() - all zeros -IRect(x0, y0, x1, y1) -IRect(Rect or IRect) - new copy -IRect(sequence) - from 'sequence'""" - - __swig_setmethods__ = {} - __setattr__ = lambda self, name, value: _swig_setattr(self, IRect, name, value) - __swig_getmethods__ = {} - __getattr__ = lambda self, name: _swig_getattr(self, IRect, name) - __repr__ = _swig_repr - __swig_setmethods__["x0"] = _fitz.IRect_x0_set - __swig_getmethods__["x0"] = _fitz.IRect_x0_get - if _newclass: - x0 = _swig_property(_fitz.IRect_x0_get, _fitz.IRect_x0_set) - __swig_setmethods__["y0"] = _fitz.IRect_y0_set - __swig_getmethods__["y0"] = _fitz.IRect_y0_get - if _newclass: - y0 = _swig_property(_fitz.IRect_y0_get, _fitz.IRect_y0_set) - __swig_setmethods__["x1"] = _fitz.IRect_x1_set - __swig_getmethods__["x1"] = _fitz.IRect_x1_get - if _newclass: - x1 = _swig_property(_fitz.IRect_x1_get, _fitz.IRect_x1_set) - __swig_setmethods__["y1"] = _fitz.IRect_y1_set - __swig_getmethods__["y1"] = _fitz.IRect_y1_get - if _newclass: - y1 = _swig_property(_fitz.IRect_y1_get, _fitz.IRect_y1_set) - __swig_destroy__ = _fitz.delete_IRect - __del__ = lambda self: None - - def __init__(self, *args): - """ - __init__(self) -> IRect - __init__(self, s) -> IRect - __init__(self, x0, y0, x1, y1) -> IRect - __init__(self, list) -> IRect - """ - this = _fitz.new_IRect(*args) - try: - self.this.append(this) - except __builtin__.Exception: - self.this = this - @property - - def isEmpty(self): - """isEmpty(self) -> PyObject *""" - return _fitz.IRect_isEmpty(self) - - @property - - def isInfinite(self): - """isInfinite(self) -> PyObject *""" - return _fitz.IRect_isInfinite(self) - - - def normalize(self): - """Make rectangle finite""" - return _fitz.IRect_normalize(self) - - - def contains(self, *args): - """Check if containing a 'Point' or another rect""" - return _fitz.IRect_contains(self, *args) - - - def translate(self, xoff, yoff): - """translate(self, xoff, yoff) -> IRect""" - return _fitz.IRect_translate(self, xoff, yoff) - - - def intersect(self, ir): - """intersect(self, ir) -> IRect""" - return _fitz.IRect_intersect(self, ir) - - - def getRect(self): - return Rect(self.x0, self.y0, self.x1, self.y1) - - rect = property(getRect) - - @property - def top_left(self): - return Point(self.x0, self.y0) - - @property - def top_right(self): - return Point(self.x1, self.y0) - - @property - def bottom_left(self): - return Point(self.x0, self.y1) - - @property - def bottom_right(self): - return Point(self.x1, self.y1) - - def __getitem__(self, i): - return (self.x0, self.y0, self.x1, self.y1)[i] - - def __setitem__(self, i, v): - if i == 0: self.x0 = v - elif i == 1: self.y0 = v - elif i == 2: self.x1 = v - elif i == 3: self.y1 = v - else: - raise IndexError("index out of range") - return - - def __len__(self): - return 4 - - def __repr__(self): - if not type(self) is IRect: return - return "fitz.IRect" + str((self.x0, self.y0, self.x1, self.y1)) + y1 = y0 + self.rect.height + return Rect(x0, y0, x1, y1) - width = property(lambda self: self.x1-self.x0) - height = property(lambda self: self.y1-self.y0) - tl = top_left - tr = top_right - br = bottom_right - bl = bottom_left + @property + def MediaBox(self): + return Rect(0, 0, self.MediaBoxSize) -IRect_swigregister = _fitz.IRect_swigregister -IRect_swigregister(IRect) +Page_swigregister = _fitz.Page_swigregister +Page_swigregister(Page) class Pixmap(_object): """Pixmap(Colorspace, width, height, samples, alpha) @@ -2716,8 +2983,11 @@ def colorspace(self): @property def irect(self): - """irect(self) -> IRect""" - return _fitz.Pixmap_irect(self) + """irect(self) -> PyObject *""" + val = _fitz.Pixmap_irect(self) + val = IRect(val) + + return val @property @@ -2851,124 +3121,6 @@ def __init__(self, *args): Device_swigregister = _fitz.Device_swigregister Device_swigregister(Device) - -def _fz_pre_scale(m, sx, sy): - """_fz_pre_scale(m, sx, sy) -> Matrix""" - return _fitz._fz_pre_scale(m, sx, sy) - -def _fz_pre_shear(m, sx, sy): - """_fz_pre_shear(m, sx, sy) -> Matrix""" - return _fitz._fz_pre_shear(m, sx, sy) - -def _fz_pre_rotate(m, degree): - """_fz_pre_rotate(m, degree) -> Matrix""" - return _fitz._fz_pre_rotate(m, degree) -class Matrix(_object): - """Matrix() - all zeros -Matrix(a, b, c, d, e, f) -Matrix(zoom-x, zoom-y) - zoom -Matrix(shear-x, shear-y, 1) - shear -Matrix(degree) - rotate -Matrix(Matrix) - new copy -Matrix(sequence) - from 'sequence'""" - - __swig_setmethods__ = {} - __setattr__ = lambda self, name, value: _swig_setattr(self, Matrix, name, value) - __swig_getmethods__ = {} - __getattr__ = lambda self, name: _swig_getattr(self, Matrix, name) - __repr__ = _swig_repr - __swig_setmethods__["a"] = _fitz.Matrix_a_set - __swig_getmethods__["a"] = _fitz.Matrix_a_get - if _newclass: - a = _swig_property(_fitz.Matrix_a_get, _fitz.Matrix_a_set) - __swig_setmethods__["b"] = _fitz.Matrix_b_set - __swig_getmethods__["b"] = _fitz.Matrix_b_get - if _newclass: - b = _swig_property(_fitz.Matrix_b_get, _fitz.Matrix_b_set) - __swig_setmethods__["c"] = _fitz.Matrix_c_set - __swig_getmethods__["c"] = _fitz.Matrix_c_get - if _newclass: - c = _swig_property(_fitz.Matrix_c_get, _fitz.Matrix_c_set) - __swig_setmethods__["d"] = _fitz.Matrix_d_set - __swig_getmethods__["d"] = _fitz.Matrix_d_get - if _newclass: - d = _swig_property(_fitz.Matrix_d_get, _fitz.Matrix_d_set) - __swig_setmethods__["e"] = _fitz.Matrix_e_set - __swig_getmethods__["e"] = _fitz.Matrix_e_get - if _newclass: - e = _swig_property(_fitz.Matrix_e_get, _fitz.Matrix_e_set) - __swig_setmethods__["f"] = _fitz.Matrix_f_set - __swig_getmethods__["f"] = _fitz.Matrix_f_get - if _newclass: - f = _swig_property(_fitz.Matrix_f_get, _fitz.Matrix_f_set) - __swig_destroy__ = _fitz.delete_Matrix - __del__ = lambda self: None - - def __init__(self, *args): - """ - __init__(self) -> Matrix - __init__(self, n) -> Matrix - __init__(self, sx, sy, shear=0) -> Matrix - __init__(self, r, s, t, u, v, w) -> Matrix - __init__(self, degree) -> Matrix - __init__(self, list) -> Matrix - """ - this = _fitz.new_Matrix(*args) - try: - self.this.append(this) - except __builtin__.Exception: - self.this = this - - def invert(self, m): - """invert(self, m) -> int""" - return _fitz.Matrix_invert(self, m) - - - def preTranslate(self, sx, sy): - """preTranslate(self, sx, sy) -> Matrix""" - return _fitz.Matrix_preTranslate(self, sx, sy) - - - def concat(self, m1, m2): - """concat(self, m1, m2) -> Matrix""" - return _fitz.Matrix_concat(self, m1, m2) - - - def preScale(self, sx, sy): - """preScale(Matrix self, float sx, float sy) -> Matrix self updated""" - _fitz._fz_pre_scale(self, sx, sy) - return self - def preShear(self, sx, sy): - """preShear(Matrix self, float sx, float sy) -> Matrix self updated""" - _fitz._fz_pre_shear(self, sx, sy) - return self - def preRotate(self, degree): - """preRotate(Matrix self, float degree) -> Matrix self updated""" - _fitz._fz_pre_rotate(self, degree) - return self - def __getitem__(self, i): - return (self.a, self.b, self.c, self.d, self.e, self.f)[i] - - def __setitem__(self, i, v): - if i == 0: self.a = v - elif i == 1: self.b = v - elif i == 2: self.c = v - elif i == 3: self.d = v - elif i == 4: self.e = v - elif i == 5: self.f = v - else: - raise IndexError("index out of range") - return - - def __len__(self): - return 6 - def __repr__(self): - return "fitz.Matrix(%s, %s, %s, %s, %s, %s)" % (self.a, self.b, self.c, self.d, self.e, self.f) - - -Matrix_swigregister = _fitz.Matrix_swigregister -Matrix_swigregister(Matrix) - class Outline(_object): """Proxy of C fz_outline_s struct.""" @@ -3028,109 +3180,6 @@ def dest(self): Outline_swigregister = _fitz.Outline_swigregister Outline_swigregister(Outline) - -def _fz_transform_point(point, transform): - """_fz_transform_point(point, transform) -> Point""" - return _fitz._fz_transform_point(point, transform) -class Point(_object): - """Point() - all zeros -Point(x, y) -Point(Point) - new copy -Point(sequence) - from 'sequence'""" - - __swig_setmethods__ = {} - __setattr__ = lambda self, name, value: _swig_setattr(self, Point, name, value) - __swig_getmethods__ = {} - __getattr__ = lambda self, name: _swig_getattr(self, Point, name) - __repr__ = _swig_repr - __swig_setmethods__["x"] = _fitz.Point_x_set - __swig_getmethods__["x"] = _fitz.Point_x_get - if _newclass: - x = _swig_property(_fitz.Point_x_get, _fitz.Point_x_set) - __swig_setmethods__["y"] = _fitz.Point_y_set - __swig_getmethods__["y"] = _fitz.Point_y_get - if _newclass: - y = _swig_property(_fitz.Point_y_get, _fitz.Point_y_set) - - def __init__(self, *args): - """ - __init__(self) -> Point - __init__(self, q) -> Point - __init__(self, x, y) -> Point - __init__(self, list) -> Point - """ - this = _fitz.new_Point(*args) - try: - self.this.append(this) - except __builtin__.Exception: - self.this = this - - def distance_to(self, *args): - """Return the distance to a rectangle or another point.""" - assert len(args) > 0, "at least one parameter must be given" - x = args[0] - if len(args) > 1: - unit = args[1] - else: - unit = "px" - u = {"px": (1.,1.), "in": (1.,72.), "cm": (2.54, 72.), "mm": (25.4, 72.)} - f = u[unit][0] / u[unit][1] - if type(x) is Point: - return abs(self - x) * f - - # from here on, x is a rectangle - # as a safeguard, make a finite copy of it - r = Rect(x.top_left, x.top_left) - r = r | x.bottom_right - if self in r: - return 0.0 - if self.x > r.x1: - if self.y >= r.y1: - return self.distance_to(r.bottom_right, unit) - elif self.y <= r.y0: - return self.distance_to(r.top_right, unit) - else: - return (self.x - r.x1) * f - elif r.x0 <= self.x <= r.x1: - if self.y >= r.y1: - return (self.y - r.y1) * f - else: - return (r.y0 - self.y) * f - else: - if self.y >= r.y1: - return self.distance_to(r.bottom_left, unit) - elif self.y <= r.y0: - return self.distance_to(r.top_left, unit) - else: - return (r.x0 - self.x) * f - - def transform(self, m): - _fitz._fz_transform_point(self, m) - return self - - def __setitem__(self, i, v): - if i == 0: - self.x = v - elif i == 1: - self.y = v - else: - raise IndexError("index out of range") - return - - def __getitem__(self, i): - return (self.x, self.y)[i] - - def __len__(self): - return 2 - - def __repr__(self): - return "fitz.Point" + str((self.x, self.y)) - - __swig_destroy__ = _fitz.delete_Point - __del__ = lambda self: None -Point_swigregister = _fitz.Point_swigregister -Point_swigregister(Point) - ANNOT_TEXT = _fitz.ANNOT_TEXT ANNOT_LINK = _fitz.ANNOT_LINK ANNOT_FREETEXT = _fitz.ANNOT_FREETEXT @@ -3227,79 +3276,31 @@ def rect(self): """Rectangle containing the annot""" CheckParent(self) - return _fitz.Annot_rect(self) + val = _fitz.Annot_rect(self) + val = Rect(val) + return val - def _getXref(self): - """Xref number of annotation""" - CheckParent(self) + @property - return _fitz.Annot__getXref(self) + def xref(self): + """xref(self) -> int""" + return _fitz.Annot_xref(self) def _getAP(self): """Get contents source of a PDF annot""" - CheckParent(self) - return _fitz.Annot__getAP(self) - def _setAP(self, ap): + def _setAP(self, ap, rect=0): """Update contents source of a PDF annot""" - CheckParent(self) - - return _fitz.Annot__setAP(self, ap) - - - def _checkAP(self, rect, c): - """Check and update /AP object of annot""" - CheckParent(self) - - return _fitz.Annot__checkAP(self, rect, c) - - - def _setRect(self, rect): - """_setRect(self, rect)""" - return _fitz.Annot__setRect(self, rect) + return _fitz.Annot__setAP(self, ap, rect) def setRect(self, rect): - """Change the annot's rectangle.""" - CheckParent(self) - if rect.isEmpty or rect.isInfinite: - raise ValueError("Rect must be finite and not empty.") - # only handle Circle, Square, Line, PolyLine and Polygon here - if self.type[0] not in range(2, 8): - self._setRect(rect) - return - - if self.type[0] == ANNOT_CIRCLE: - self._setRect(rect) - ap = _make_circle_AP(self) - self._checkAP(Rect(0, 0, rect.width, rect.height), ap) - return - - if self.type[0] == ANNOT_SQUARE: - self._setRect(rect) - ap = _make_rect_AP(self) - self._checkAP(Rect(0, 0, rect.width, rect.height), ap) - return - - orect = self.rect - m = Matrix(rect.width / orect.width, rect.height / orect.height) - - # now transform the points of the annot - ov = self.vertices - nv = [(Point(v) - orect.tl) * m + rect.tl for v in ov] # new points - r0 = Rect(nv[0], nv[0]) # recalculate new rectangle - for v in nv[1:]: - r0 |= v # enveloping all points - w = self.border["width"] * 3 # allow for add'l space - r0 += (-w, -w, w, w) # for line end symbols - self._setRect(r0) # this is the final rect - self._setVertices(nv) # put the points in annot - ap = _make_line_AP(self, nv, r0) - self._checkAP(Rect(0, 0, r0.width, r0.height), ap) + """setRect(self, rect)""" + return _fitz.Annot_setRect(self, rect) @property @@ -3309,13 +3310,6 @@ def vertices(self): return _fitz.Annot_vertices(self) - - def _setVertices(self, vertices): - """Change the annot's vertices. Only for 'Line', 'PolyLine' and 'Polygon' types.""" - CheckParent(self) - - return _fitz.Annot__setVertices(self, vertices) - @property def colors(self): @@ -3325,11 +3319,156 @@ def colors(self): return _fitz.Annot_colors(self) - def updateAppearance(self): + def update(self, fontsize=0.0, text_color=None, border_color=None, fill_color=None, rotate=-1): """Update the appearance of an annotation.""" - CheckParent(self) - return _fitz.Annot_updateAppearance(self) + if self.type[0] == ANNOT_WIDGET: + print("Use updateWidget method for form fields.") + return False + + val = _fitz.Annot_update(self, fontsize, text_color, border_color, fill_color, rotate) + + """ + The following code fixes shortcomings of MuPDF's "pdf_update_annot" + function. Currently these are: + 1. Opacity (all annots). MuPDF ignores this proprty. This requires + to add an ExtGState (extended graphics state) object in the + C code as well. + 2. Dashing (all annots). MuPDF ignores this proprty. + 3. Colors and font size for FreeText annotations. + 4. Line end icons also for POLYGON and POLY_LINE annotations. + MuPDF only honors them for LINE annotations. + 5. Always perform a "clean" for the annot, because MuPDF does not + enclose their syntax in a string pair "q ... Q", which may cause + Adobe and other readers not to display the annot. + + """ + if not val is True: # skip if something went wrong + return val + + def color_string(cs, code): + """Return valid PDF color operator for a given color sequence. + """ + if cs is None: return "" + if hasattr(cs, "__float__") or len(cs) == 1: + app = " g\n" if code == "f" else " G\n" + elif len(cs) == 3: + app = " rg\n" if code == "f" else " RG\n" + else: + app = " k\n" if code == "f" else " K\n" + if hasattr(cs, "__len__"): + col = " ".join(map(str, cs)) + app + else: + col = "%g" % cs + app + return bytes(col, "utf8") if str is not bytes else col + + type = self.type[0] # get the annot type + dt = self.border["dashes"] # get the dashes spec + bwidth = self.border["width"] # get border line width + stroke = self.colors["stroke"] # get the stroke color + fill = self.colors["fill"] # get the fill color + rect = None # used if we change the rect here + bfill = color_string(fill, "f") + + line_end_le, line_end_ri = 0, 0 # line end codes + if self.lineEnds: + line_end_le, line_end_ri = self.lineEnds + + ap = self._getAP() # get the annot operator source + ap_updated = False # assume we did nothing + + if type == ANNOT_FREETEXT: + CheckColor(fill_color) + CheckColor(border_color) + CheckColor(text_color) + + ap_tab = ap.splitlines() # split AP stream into lines + idx_BT = ap_tab.index(b"BT") # line no. of text start + # to avoid effort, we rely on a fixed format of this + # annot type: line 0 = fill color, line 5 border color, etc. + if fill_color is not None: + ap_tab[0] = color_string(fill_color, "f") + ap_updated = True + + if idx_BT == 7: + if bwidth > 0: + if border_color is not None: + ap_tab[4] = color_string(border_color, "s") + ap_updated = True + else: # for zero border width suppress border + ap_tab[3] = b"0 w" + ap_tab[4] = ap_tab[5] = ap_tab[6] = b"" + ap_updated = True + + if text_color is not None: + ap_tab[idx_BT + 1] = color_string(text_color, "f") + ap_updated = True + + if fontsize > 0.0: + x = ap_tab[idx_BT + 2].split() + x[1] = b"%g" % fontsize + ap_tab[idx_BT + 2] = b" ".join(x) + ap_updated = True + + if ap_updated: + ap = b"\n".join(ap_tab) # updated AP stream + + if bfill != "": + if type == ANNOT_POLYGON: + ap = ap[:-1] + bfill + b"b" # close, fill, and stroke + ap_updated = True + elif type == ANNOT_POLYLINE: + ap = ap[:-1] + bfill + b"B" # fill and stroke + ap_updated = True + + # Dashes not handled by MuPDF, so we do it here. + if dt: + dash = "[" + " ".join(map(str, dt)) + "] d\n" + ap = dash.encode("utf-8") + ap + # reset dashing - only applies for LINE annots with line ends given + ap = ap.replace(b"\nS\n", b"\nS\n[] d\n", 1) + ap_updated = True + + # Opacity not handled by MuPDF, so we do it here. The /ExtGState object + # "Alp0" referenced here has already been added by our C code. + if 0 <= self.opacity < 1: + ap = b"/Alp0 gs\n" + ap + ap_updated = True + + if line_end_le + line_end_ri > 0 and type in (ANNOT_POLYGON, ANNOT_POLYLINE): + le_funcs = (None, TOOLS._le_square, TOOLS._le_circle, + TOOLS._le_diamond, TOOLS._le_openarrow, + TOOLS._le_closedarrow, TOOLS._le_butt, + TOOLS._le_ropenarrow, TOOLS._le_rclosedarrow, + TOOLS._le_slash) + le_funcs_range = range(1, len(le_funcs)) + d = 4 * max(1, self.border["width"]) + rect = self.rect + (-d, -d, d, d) + ap_updated = True + points = self.vertices + ap = b"q\n" + ap + b"\nQ\n" + if line_end_le in le_funcs_range: + p1 = Point(points[0]) + p2 = Point(points[1]) + left = le_funcs[line_end_le](self, p1, p2, False) + ap += bytes(left, "utf8") if str is not bytes else left + if line_end_ri in le_funcs_range: + p1 = Point(points[-2]) + p2 = Point(points[-1]) + left = le_funcs[line_end_ri](self, p1, p2, True) + ap += bytes(left, "utf8") if str is not bytes else left + + if ap_updated: + if rect: # rect modified here? + self.setRect(rect) + self._setAP(ap, rect = 1) + else: + self._setAP(ap, rect = 0) + + # always perform a clean to wrap stream by "q" / "Q" + self._cleanContents() + + return val def setColors(self, colors): @@ -3339,21 +3478,7 @@ def setColors(self, colors): """ CheckParent(self) - val = _fitz.Annot_setColors(self, colors) - - if self.type[0] not in range(2, 8): - return - r = Rect(0, 0, self.rect.width, self.rect.height) - if self.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(self) - elif self.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(self) - else: - ap = _make_line_AP(self) - self._checkAP(r, ap) - - - return val + return _fitz.Annot_setColors(self, colors) @property @@ -3368,18 +3493,7 @@ def setLineEnds(self, start, end): """setLineEnds(self, start, end)""" CheckParent(self) - val = _fitz.Annot_setLineEnds(self, start, end) - - if self.type[0] not in range(2, 8): - return - if self.type[0] in (ANNOT_CIRCLE, ANNOT_SQUARE): - return - r = Rect(0, 0, self.rect.width, self.rect.height) - ap = _make_line_AP(self) - self._checkAP(r, ap) - - - return val + return _fitz.Annot_setLineEnds(self, start, end) @property @@ -3402,10 +3516,7 @@ def setOpacity(self, opacity): """setOpacity(self, opacity)""" CheckParent(self) - val = _fitz.Annot_setOpacity(self, opacity) - _upd_my_AP(self) - - return val + return _fitz.Annot_setOpacity(self, opacity) @property @@ -3488,10 +3599,7 @@ def setBorder(self, border): """setBorder(self, border) -> PyObject *""" CheckParent(self) - val = _fitz.Annot_setBorder(self, border) - _upd_my_AP(self) - - return val + return _fitz.Annot_setBorder(self, border) @property @@ -3634,6 +3742,41 @@ def __init__(self, *args, **kwargs): __repr__ = _swig_repr __swig_destroy__ = _fitz.delete_Link __del__ = lambda self: None + + def _border(self, doc, xref): + """_border(self, doc, xref) -> PyObject *""" + return _fitz.Link__border(self, doc, xref) + + + def _setBorder(self, border, doc, xref): + """_setBorder(self, border, doc, xref) -> PyObject *""" + return _fitz.Link__setBorder(self, border, doc, xref) + + + def _colors(self, doc, xref): + """_colors(self, doc, xref) -> PyObject *""" + return _fitz.Link__colors(self, doc, xref) + + + def _setColors(self, colors, doc, xref): + """_setColors(self, colors, doc, xref) -> PyObject *""" + return _fitz.Link__setColors(self, colors, doc, xref) + + + @property + def border(self): + return self._border(self.parent.parent.this, self.xref) + + def setBorder(self, border): + return self._setBorder(border, self.parent.parent.this, self.xref) + + @property + def colors(self): + return self._colors(self.parent.parent.this, self.xref) + + def setColors(self, colors): + return self._setColors(colors, self.parent.parent.this, self.xref) + @property def uri(self): @@ -3679,10 +3822,13 @@ def dest(self): @property def rect(self): - """rect(self) -> Rect""" + """rect(self) -> PyObject *""" CheckParent(self) - return _fitz.Link_rect(self) + val = _fitz.Link_rect(self) + val = Rect(val) + + return val @property @@ -3691,10 +3837,18 @@ def next(self): CheckParent(self) val = _fitz.Link_next(self) + if val: val.thisown = True - val.parent = self.parent # copy owning page object from previous annot + val.parent = self.parent # copy owning page from prev link val.parent._annot_refs[id(val)] = val + if self.xref > 0: # prev link has an xref + link_xrefs = self.parent._getLinkXrefs() + idx = link_xrefs.index(self.xref) + val.xref = link_xrefs[idx + 1] + else: + val.xref = 0 + return val @@ -3748,8 +3902,11 @@ def run(self, dw, m, area): @property def rect(self): - """rect(self) -> Rect""" - return _fitz.DisplayList_rect(self) + """rect(self) -> PyObject *""" + val = _fitz.DisplayList_rect(self) + val = Rect(val) + + return val def getPixmap(self, matrix=None, colorspace=None, alpha=0, clip=None): @@ -3788,15 +3945,20 @@ def __init__(self, mediabox): __swig_destroy__ = _fitz.delete_TextPage __del__ = lambda self: None - def search(self, needle, hit_max=16): - """search(self, needle, hit_max=16) -> PyObject *""" - val = _fitz.TextPage_search(self, needle, hit_max) + def search(self, needle, hit_max=16, quads=0): + """search(self, needle, hit_max=16, quads=0) -> PyObject *""" + val = _fitz.TextPage_search(self, needle, hit_max, quads) - if val: - nval = [] - for r in val: - nval.append(Rect(r)) - val = nval + if len(val) == 0: + return val + nval = [] + for v in val: + q = Quad(v) + if not quads: + nval.append(q.rect) + else: + nval.append(q) + val = nval return val @@ -3820,12 +3982,17 @@ def _extractText(self, format): return val import base64, json - def convertb64(s): - if str is bytes: - return base64.b64encode(s) - return base64.b64encode(s).decode() + class b64encode(json.JSONEncoder): + def default(self,s): + if str is not bytes and type(s) is bytes: + return base64.b64encode(s).decode() + if type(s) is bytearray: + if str is bytes: + return base64.b64encode(s) + else: + return base64.b64encode(s).decode() - val = json.dumps(val, separators=(",", ":"), default=convertb64) + val = json.dumps(val, separators=(",", ":"), cls=b64encode, indent=1) return val @@ -3895,7 +4062,7 @@ class Tools(_object): __repr__ = _swig_repr def gen_id(self): - """Return a globally unique integer.""" + """Return a unique integer.""" return _fitz.Tools_gen_id(self) @@ -3922,15 +4089,264 @@ def fitz_config(self): return _fitz.Tools_fitz_config(self) - def _store_debug(self): - """_store_debug(self)""" - return _fitz.Tools__store_debug(self) - - def glyph_cache_empty(self): """Empty the glyph cache.""" return _fitz.Tools_glyph_cache_empty(self) + @property + + def fitz_stderr(self): + """fitz_stderr(self) -> PyObject *""" + return _fitz.Tools_fitz_stderr(self) + + + def fitz_stderr_reset(self): + """fitz_stderr_reset(self)""" + return _fitz.Tools_fitz_stderr_reset(self) + + + def transform_rect(self, rect, matrix): + """transform_rect(self, rect, matrix) -> PyObject *""" + return _fitz.Tools_transform_rect(self, rect, matrix) + + + def invert_matrix(self, matrix): + """invert_matrix(self, matrix) -> PyObject *""" + return _fitz.Tools_invert_matrix(self, matrix) + + + + def _hor_matrix(self, C, P): + """Given two points C, P calculate matrix that rotates and translates the vector C -> P such that C is mapped to Point(0, 0), and P to some point on the x axis + """ + S = (P - C).unit # unit vector C -> P + return Matrix(1, 0, 0, 1, -C.x, -C.y) * Matrix(S.x, -S.y, S.y, S.x, 0, 0) + + + def _le_annot_parms(self, annot, p1, p2): + """Get common parameters for making line end symbols. + """ + w = annot.border["width"] # line width + sc = annot.colors["stroke"] # stroke color + if not sc: sc = (0,0,0) + scol = " ".join(map(str, sc)) + " RG\n" + fc = annot.colors["fill"] # fill color + if not fc: fc = (0,0,0) + fcol = " ".join(map(str, fc)) + " rg\n" + nr = annot.rect + h = nr.y1 + np1 = p1 # point coord relative to annot rect + np2 = p2 # point coord relative to annot rect + m = self._hor_matrix(np1, np2) # matrix makes the line horizontal + im = ~m # inverted matrix + L = np1 * m # converted start (left) point + R = np2 * m # converted end (right) point + if 0 <= annot.opacity < 1: + opacity = "/Alp0 gs\n" + else: + opacity = "" + return m, im, L, R, w, h, scol, fcol, opacity + + + def _oval_string(self, h, p1, p2, p3, p4): + """Return /AP string defining an oval within a 4-polygon provided as points + """ + def bezier(p, q, r): + f = "%g %g %g %g %g %g c\n" + return f % (p.x, p.y, q.x, q.y, r.x, r.y) + + kappa = 0.55228474983 # magic number + ml = p1 + (p4 - p1) * 0.5 # middle points ... + mo = p1 + (p2 - p1) * 0.5 # for each ... + mr = p2 + (p3 - p2) * 0.5 # polygon ... + mu = p4 + (p3 - p4) * 0.5 # side + ol1 = ml + (p1 - ml) * kappa # the 8 bezier + ol2 = mo + (p1 - mo) * kappa # helper points + or1 = mo + (p2 - mo) * kappa + or2 = mr + (p2 - mr) * kappa + ur1 = mr + (p3 - mr) * kappa + ur2 = mu + (p3 - mu) * kappa + ul1 = mu + (p4 - mu) * kappa + ul2 = ml + (p4 - ml) * kappa + # now draw, starting from middle point of left side + ap = "%g %g m\n" % (ml.x, ml.y) + ap += bezier(ol1, ol2, mo) + ap += bezier(or1, or2, mr) + ap += bezier(ur1, ur2, mu) + ap += bezier(ul1, ul2, ml) + return ap + + + def _le_diamond(self, annot, p1, p2, lr): + """Make stream commands for diamond line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 # 2*shift*width = length of square edge + d = shift * max(1, w) + M = R - (d/2., 0) if lr else L + (d/2., 0) + r = Rect(M, M) + (-d, -d, d, d) # the square + # the square makes line longer by (2*shift - 1)*width + p = (r.tl + (r.bl - r.tl) * 0.5) * im + ap = "q\n%s%g %g m\n" % (opacity, p.x, p.y) + p = (r.tl + (r.tr - r.tl) * 0.5) * im + ap += "%g %g l\n" % (p.x, p.y) + p = (r.tr + (r.br - r.tr) * 0.5) * im + ap += "%g %g l\n" % (p.x, p.y) + p = (r.br + (r.bl - r.br) * 0.5) * im + ap += "%g %g l\n" % (p.x, p.y) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + + def _le_square(self, annot, p1, p2, lr): + """Make stream commands for square line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 # 2*shift*width = length of square edge + d = shift * max(1, w) + M = R - (d/2., 0) if lr else L + (d/2., 0) + r = Rect(M, M) + (-d, -d, d, d) # the square + # the square makes line longer by (2*shift - 1)*width + p = r.tl * im + ap = "q\n%s%g %g m\n" % (opacity, p.x, p.y) + p = r.tr * im + ap += "%g %g l\n" % (p.x, p.y) + p = r.br * im + ap += "%g %g l\n" % (p.x, p.y) + p = r.bl * im + ap += "%g %g l\n" % (p.x, p.y) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + + def _le_circle(self, annot, p1, p2, lr): + """Make stream commands for circle line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 # 2*shift*width = length of square edge + d = shift * max(1, w) + M = R - (d/2., 0) if lr else L + (d/2., 0) + r = Rect(M, M) + (-d, -d, d, d) # the square + ap = "q\n" + opacity + self._oval_string(h, r.tl * im, r.tr * im, r.br * im, r.bl * im) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + + def _le_butt(self, annot, p1, p2, lr): + """Make stream commands for butt line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 3 + d = shift * max(1, w) + M = R if lr else L + top = (M + (0, -d/2.)) * im + bot = (M + (0, d/2.)) * im + ap = "\nq\n%s%g %g m\n" % (opacity, top.x, top.y) + ap += "%g %g l\n" % (bot.x, bot.y) + ap += "%g w\n" % w + ap += scol + "s\nQ\n" + return ap + + + def _le_slash(self, annot, p1, p2, lr): + """Make stream commands for slash line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + rw = 1.1547 * max(1, w) * 1.0 # makes rect diagonal a 30 deg inclination + M = R if lr else L + r = Rect(M.x - rw, M.y - 2 * w, M.x + rw, M.y + 2 * w) + top = r.tl * im + bot = r.br * im + ap = "\nq\n%s%g %g m\n" % (opacity, top.x, top.y) + ap += "%g %g l\n" % (bot.x, bot.y) + ap += "%g w\n" % w + ap += scol + "s\nQ\n" + return ap + + + def _le_openarrow(self, annot, p1, p2, lr): + """Make stream commands for open arrow line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 + d = shift * max(1, w) + p2 = R + (d/2., 0) if lr else L - (d/2., 0) + p1 = p2 + (-2*d, -d) if lr else p2 + (2*d, -d) + p3 = p2 + (-2*d, d) if lr else p2 + (2*d, d) + p1 *= im + p2 *= im + p3 *= im + ap = "\nq\n%s%g %g m\n" % (opacity, p1.x, p1.y) + ap += "%g %g l\n" % (p2.x, p2.y) + ap += "%g %g l\n" % (p3.x, p3.y) + ap += "%g w\n" % w + ap += scol + "S\nQ\n" + return ap + + + def _le_closedarrow(self, annot, p1, p2, lr): + """Make stream commands for closed arrow line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 + d = shift * max(1, w) + p2 = R + (d/2., 0) if lr else L - (d/2., 0) + p1 = p2 + (-2*d, -d) if lr else p2 + (2*d, -d) + p3 = p2 + (-2*d, d) if lr else p2 + (2*d, d) + p1 *= im + p2 *= im + p3 *= im + ap = "\nq\n%s%g %g m\n" % (opacity, p1.x, p1.y) + ap += "%g %g l\n" % (p2.x, p2.y) + ap += "%g %g l\n" % (p3.x, p3.y) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + + def _le_ropenarrow(self, annot, p1, p2, lr): + """Make stream commands for right open arrow line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 + d = shift * max(1, w) + p2 = R - (d/3., 0) if lr else L + (d/3., 0) + p1 = p2 + (2*d, -d) if lr else p2 + (-2*d, -d) + p3 = p2 + (2*d, d) if lr else p2 + (-2*d, d) + p1 *= im + p2 *= im + p3 *= im + ap = "\nq\n%s%g %g m\n" % (opacity, p1.x, p1.y) + ap += "%g %g l\n" % (p2.x, p2.y) + ap += "%g %g l\n" % (p3.x, p3.y) + ap += "%g w\n" % w + ap += scol + fcol + "S\nQ\n" + return ap + + + def _le_rclosedarrow(self, annot, p1, p2, lr): + """Make stream commands for right closed arrow line end symbol. "lr" denotes left (False) or right point. + """ + m, im, L, R, w, h, scol, fcol, opacity = self._le_annot_parms(annot, p1, p2) + shift = 2.5 + d = shift * max(1, w) + p2 = R - (2*d, 0) if lr else L + (2*d, 0) + p1 = p2 + (2*d, -d) if lr else p2 + (-2*d, -d) + p3 = p2 + (2*d, d) if lr else p2 + (-2*d, d) + p1 *= im + p2 *= im + p3 *= im + ap = "\nq\n%s%g %g m\n" % (opacity, p1.x, p1.y) + ap += "%g %g l\n" % (p2.x, p2.y) + ap += "%g %g l\n" % (p3.x, p3.y) + ap += "%g w\n" % w + ap += scol + fcol + "b\nQ\n" + return ap + + def __init__(self): """__init__(self) -> Tools""" diff --git a/fitz/fitz_wrap.c b/fitz/fitz_wrap.c index 2d7a6056e..15d6c3216 100644 --- a/fitz/fitz_wrap.c +++ b/fitz/fitz_wrap.c @@ -2986,18 +2986,14 @@ SWIG_Python_NonDynamicSetAttr(PyObject *obj, PyObject *name, PyObject *value) { #define SWIGTYPE_p_fz_colorspace_s swig_types[4] #define SWIGTYPE_p_fz_display_list_s swig_types[5] #define SWIGTYPE_p_fz_document_s swig_types[6] -#define SWIGTYPE_p_fz_irect_s swig_types[7] -#define SWIGTYPE_p_fz_link_s swig_types[8] -#define SWIGTYPE_p_fz_matrix_s swig_types[9] -#define SWIGTYPE_p_fz_outline_s swig_types[10] -#define SWIGTYPE_p_fz_page_s swig_types[11] -#define SWIGTYPE_p_fz_pixmap_s swig_types[12] -#define SWIGTYPE_p_fz_point_s swig_types[13] -#define SWIGTYPE_p_fz_rect_s swig_types[14] -#define SWIGTYPE_p_fz_stext_page_s swig_types[15] -#define SWIGTYPE_p_pdf_graft_map_s swig_types[16] -static swig_type_info *swig_types[18]; -static swig_module_info swig_module = {swig_types, 17, 0, 0, 0, 0}; +#define SWIGTYPE_p_fz_link_s swig_types[7] +#define SWIGTYPE_p_fz_outline_s swig_types[8] +#define SWIGTYPE_p_fz_page_s swig_types[9] +#define SWIGTYPE_p_fz_pixmap_s swig_types[10] +#define SWIGTYPE_p_fz_stext_page_s swig_types[11] +#define SWIGTYPE_p_pdf_graft_map_s swig_types[12] +static swig_type_info *swig_types[14]; +static swig_module_info swig_module = {swig_types, 13, 0, 0, 0, 0}; #define SWIG_TypeQuery(name) SWIG_TypeQueryModule(&swig_module, &swig_module, name) #define SWIG_MangledTypeQuery(name) SWIG_MangledTypeQueryModule(&swig_module, &swig_module, name) @@ -3040,6 +3036,7 @@ static swig_module_info swig_module = {swig_types, 17, 0, 0, 0, 0}; #define SWIG_FILE_WITH_INIT #define SWIG_PYTHON_2_UNICODE +#define JM_EPS 1E-5 // memory allocation macros #define JM_MEMORY 1 @@ -3085,7 +3082,11 @@ static swig_module_info swig_module = {swig_types, 17, 0, 0, 0, 0}; char *JM_Python_str_AsChar(PyObject *str); - fz_context *gctx; +fz_context *gctx; +int JM_UNIQUE_ID = 0; + +PyObject *fitz_stdout = NULL; +PyObject *fitz_stderr = NULL; struct DeviceWrapper { fz_device *device; @@ -3093,12 +3094,266 @@ struct DeviceWrapper { }; + +//----------------------------------------------------------------------------- +// Functions converting betwenn PySequences and fitz geometry objects +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// Incomplete - not in use +//----------------------------------------------------------------------------- +/* +int JM_is_valid_quad(fz_quad q) +{ + fz_point b = fz_normalize_vector(fz_make_point(q.ur.x - q.ul.x, q.ur.y - q.ul.y)); + if ((fabs(b.x) + fabs(b.y)) <= JM_EPS) return 0; // empty quad! + + fz_point c = fz_normalize_vector(fz_make_point(q.ll.x - q.ul.x, q.ll.y - q.ul.y)); + if ((fabs(c.x) + fabs(c.y)) <= JM_EPS) return 0; // empty quad! + + if (fabs(b.x * c.x + b.y * c.y) > JM_EPS) + return 0; // angle at UL != 90 deg + + b = fz_normalize_vector(fz_make_point(q.ur.x - q.lr.x, q.ur.y - q.lr.y)); + c = fz_normalize_vector(fz_make_point(q.ll.x - q.lr.x, q.ll.y - q.lr.y)); + if (fabs(b.x * c.x + b.y * c.y) > JM_EPS) + return 0; // angle at LR != 90 deg + + b = fz_normalize_vector(fz_make_point(q.ul.x - q.ll.x, q.ul.y - q.ll.y)); + c = fz_normalize_vector(fz_make_point(q.lr.x - q.ll.x, q.lr.y - q.ll.y)); + if (fabs(b.x * c.x + b.y * c.y) > JM_EPS) + return 0; // angle at LL != 90 deg + return 1; +} +*/ + +//----------------------------------------------------------------------------- +// PySequence to quad. Default: quad of (0, 0) points. +// Four floats are treated as coordinates of a rect, and its corners will +// define the quad. +//----------------------------------------------------------------------------- +fz_quad JM_quad_from_py(PyObject *r) +{ + fz_quad q; + fz_point p[4]; + size_t i; + q.ul = q.ur = q.ll = q.lr = fz_make_point(0,0); + + if (!r || !PySequence_Check(r) || PySequence_Size(r) != 4) + return q; + + double x0 = PyFloat_AsDouble(PySequence_GetItem(r, 0)); + if (!PyErr_Occurred()) // assume case 1: a rect is given + { + double y0 = PyFloat_AsDouble(PySequence_GetItem(r, 1)); + if (PyErr_Occurred()) goto return_simple; + + double x1 = PyFloat_AsDouble(PySequence_GetItem(r, 2)); + if (PyErr_Occurred()) goto return_simple; + + double y1 = PyFloat_AsDouble(PySequence_GetItem(r, 3)); + if (PyErr_Occurred()) goto return_simple; + + q.ul = fz_make_point(x0, y0); + q.ur = fz_make_point(x1, y0); + q.ll = fz_make_point(x0, y1); + q.lr = fz_make_point(x1, y1); + + return_simple: ; + PyErr_Clear(); + return q; + } + + PyErr_Clear(); + for (i = 0; i < 4; i++) + { + PyObject *o = PySequence_GetItem(r, i); + p[i].x = p[i].y = 0; + if (!PySequence_Check(o) || PySequence_Size(o) != 2) + goto weiter; + + p[i].x = PyFloat_AsDouble(PySequence_GetItem(o, 0)); + if (PyErr_Occurred()) + p[i].x = 0; + + p[i].y = PyFloat_AsDouble(PySequence_GetItem(o, 1)); + if (PyErr_Occurred()) + p[i].y = 0; + + PyErr_Clear(); + weiter: ; + Py_CLEAR(o); + } + q.ul = p[0]; + q.ur = p[1]; + q.ll = p[2]; + q.lr = p[3]; + return q; +} + +//----------------------------------------------------------------------------- +// PySequence to rect. Default: infinite rect +//----------------------------------------------------------------------------- +fz_rect JM_rect_from_py(PyObject *r) +{ + if (!r || !PySequence_Check(r) || PySequence_Size(r) != 4) + return fz_infinite_rect; + + double x0 = PyFloat_AsDouble(PySequence_GetItem(r, 0)); + if (PyErr_Occurred()) goto return_empty; + + double y0 = PyFloat_AsDouble(PySequence_GetItem(r, 1)); + if (PyErr_Occurred()) goto return_empty; + + double x1 = PyFloat_AsDouble(PySequence_GetItem(r, 2)); + if (PyErr_Occurred()) goto return_empty; + + double y1 = PyFloat_AsDouble(PySequence_GetItem(r, 3)); + if (PyErr_Occurred()) goto return_empty; + + return fz_make_rect((float) x0, (float) y0, (float) x1, (float) y1); + + return_empty: ; + PyErr_Clear(); + return fz_infinite_rect; +} + +//----------------------------------------------------------------------------- +// fz_rect to PySequence +//----------------------------------------------------------------------------- +PyObject *JM_py_from_rect(fz_rect r) +{ + return Py_BuildValue("ffff", r.x0, r.y0, r.x1, r.y1); +} + +//----------------------------------------------------------------------------- +// PySequence to fz_irect. Default: infinite irect +//----------------------------------------------------------------------------- +fz_irect JM_irect_from_py(PyObject *r) +{ + if (!r || !PySequence_Check(r) || PySequence_Size(r) != 4) + return fz_infinite_irect; + + long x0 = PyLong_AsLong(PySequence_GetItem(r, 0)); + if (PyErr_Occurred()) goto return_empty; + + long y0 = PyLong_AsLong(PySequence_GetItem(r, 1)); + if (PyErr_Occurred()) goto return_empty; + + long x1 = PyLong_AsLong(PySequence_GetItem(r, 2)); + if (PyErr_Occurred()) goto return_empty; + + long y1 = PyLong_AsLong(PySequence_GetItem(r, 3)); + if (PyErr_Occurred()) goto return_empty; + + return fz_make_irect((int) x0, (int) y0, (int) x1, (int) y1); + + return_empty: ; + PyErr_Clear(); + return fz_infinite_irect; +} + +//----------------------------------------------------------------------------- +// fz_irect to PySequence +//----------------------------------------------------------------------------- +PyObject *JM_py_from_irect(fz_irect r) +{ + return Py_BuildValue("iiii", r.x0, r.y0, r.x1, r.y1); +} + + +//----------------------------------------------------------------------------- +// PySequence to fz_point. Default: (0, 0) +//----------------------------------------------------------------------------- +fz_point JM_point_from_py(PyObject *p) +{ + fz_point p0 = {0,0}; + if (!p || !PySequence_Check(p) || PySequence_Size(p) != 2) + return p0; + + double x = PyFloat_AsDouble(PySequence_GetItem(p, 0)); + if (PyErr_Occurred()) goto zero_point; + + double y = PyFloat_AsDouble(PySequence_GetItem(p, 1)); + if (PyErr_Occurred()) goto zero_point; + + return fz_make_point((float) x, (float) y); + + zero_point: ; + PyErr_Clear(); + return p0; +} + +//----------------------------------------------------------------------------- +// fz_point to PySequence +//----------------------------------------------------------------------------- +PyObject *JM_py_from_point(fz_point p) +{ + return Py_BuildValue("ff", p.x, p.y); +} + + +//----------------------------------------------------------------------------- +// PySequence to fz_matrix. Default: fz_identity +//----------------------------------------------------------------------------- +fz_matrix JM_matrix_from_py(PyObject *m) +{ + fz_matrix m0 = fz_identity; + if (!m || !PySequence_Check(m) || PySequence_Size(m) != 6) + return m0; + + double a = PyFloat_AsDouble(PySequence_GetItem(m, 0)); + if (PyErr_Occurred()) goto fertig; + + double b = PyFloat_AsDouble(PySequence_GetItem(m, 1)); + if (PyErr_Occurred()) goto fertig; + + double c = PyFloat_AsDouble(PySequence_GetItem(m, 2)); + if (PyErr_Occurred()) goto fertig; + + double d = PyFloat_AsDouble(PySequence_GetItem(m, 3)); + if (PyErr_Occurred()) goto fertig; + + double e = PyFloat_AsDouble(PySequence_GetItem(m, 4)); + if (PyErr_Occurred()) goto fertig; + + double f = PyFloat_AsDouble(PySequence_GetItem(m, 5)); + if (PyErr_Occurred()) goto fertig; + + m0.a = a; + m0.b = b; + m0.c = c; + m0.d = d; + m0.e = e; + m0.f = f; + + fertig: ; + PyErr_Clear(); + return m0; +} + +//----------------------------------------------------------------------------- +// fz_matrix to PySequence +//----------------------------------------------------------------------------- +PyObject *JM_py_from_matrix(fz_matrix m) +{ + return Py_BuildValue("ffffff", m.a, m.b, m.c, m.d, m.e, m.f); +} + + + // a simple tracer void JM_TRACE(const char *id) { PySys_WriteStdout("%s\n", id); } +// put warnings on Python-stdout +void JM_Warning(const char *id) +{ + PySys_WriteStdout("warning: %s\n", id); +} + +#if JM_MEMORY == 1 //----------------------------------------------------------------------------- // The following 3 functions replace MuPDF standard memory allocation. // This will ensure, that MuPDF memory handling becomes part of Python's @@ -3106,18 +3361,11 @@ void JM_TRACE(const char *id) //----------------------------------------------------------------------------- static void *JM_Py_Malloc(void *opaque, size_t size) { - //void *p = PyMem_Malloc(size); - //PySys_WriteStdout("%p alloc %u\n", p, size); - //return p; return PyMem_Malloc(size); } static void *JM_Py_Realloc(void *opaque, void *old, size_t size) { - //void *p = PyMem_Realloc(old, size); - //if (old) PySys_WriteStdout("%p free\n", old); - //PySys_WriteStdout("%p alloc %u\n", p, size); - //return p; return PyMem_Realloc(old, size); } @@ -3133,8 +3381,9 @@ const fz_alloc_context JM_Alloc_Context = JM_Py_Realloc, JM_PY_Free }; +#endif - +// return Python bools for a given integer PyObject *JM_BOOL(int v) { if (v == 0) @@ -3204,7 +3453,6 @@ PyObject *JM_fitz_config() PyDict_SetItemString(dict, "svg", JM_BOOL(FZ_ENABLE_SVG)); PyDict_SetItemString(dict, "cbz", JM_BOOL(FZ_ENABLE_CBZ)); PyDict_SetItemString(dict, "img", JM_BOOL(FZ_ENABLE_IMG)); - PyDict_SetItemString(dict, "tiff", JM_BOOL(FZ_ENABLE_TIFF)); PyDict_SetItemString(dict, "html", JM_BOOL(FZ_ENABLE_HTML)); PyDict_SetItemString(dict, "epub", JM_BOOL(FZ_ENABLE_EPUB)); PyDict_SetItemString(dict, "gprf", JM_BOOL(FZ_ENABLE_GPRF)); @@ -3225,20 +3473,90 @@ PyObject *JM_fitz_config() } //---------------------------------------------------------------------------- -// Return a Python bytes object for an fz_buffer +// Update a color float array with values from a Python sequence. +// Any error condition is treated as a no-op. +//---------------------------------------------------------------------------- +void JM_color_FromSequence(PyObject *color, int *n, float col[4]) +{ + if (!color) return; + if (PyFloat_Check(color)) // maybe just a single float + { + float c = (float) PyFloat_AsDouble(color); + if (!INRANGE(c, 0.0f, 1.0f)) return; + col[0] = c; + *n = 1; + return; + } + if (!PySequence_Check(color)) return; + int len = PySequence_Size(color), i; + if (!INRANGE(len, 1, 4) || len == 2) return; + + float mcol[4] = {0,0,0,0}; // local color storage + for (i = 0; i < len; i++) + { + mcol[i] = (float) PyFloat_AsDouble(PySequence_GetItem(color, i)); + if (PyErr_Occurred()) + { + PyErr_Clear(); // reset Py error indicator + return; + } + if (!INRANGE(mcol[i], 0.0f, 1.0f)) return; + } + + *n = len; + for (i = 0; i < len; i++) + col[i] = mcol[i]; + return; +} + +//---------------------------------------------------------------------------- +// Turn fz_buffer into a Python bytes object //---------------------------------------------------------------------------- PyObject *JM_BinFromBuffer(fz_context *ctx, fz_buffer *buffer) { - PyObject *bytes = NULL; + PyObject *bytes = PyBytes_FromString(""); char *c = NULL; if (buffer) { size_t len = fz_buffer_storage(gctx, buffer, &c); bytes = PyBytes_FromStringAndSize(c, (Py_ssize_t) len); } - else + return bytes; +} + +//---------------------------------------------------------------------------- +// Turn fz_buffer into a Python bytearray object +//---------------------------------------------------------------------------- +PyObject *JM_BArrayFromBuffer(fz_context *ctx, fz_buffer *buffer) +{ + PyObject *bytes = PyByteArray_FromObject(Py_BuildValue("s", "")); + char *c = NULL; + if (buffer) + { + size_t len = fz_buffer_storage(ctx, buffer, &c); + bytes = PyByteArray_FromStringAndSize(c, (Py_ssize_t) len); + } + return bytes; +} + +//---------------------------------------------------------------------------- +// Turn fz_buffer to a base64 encoded bytes object +//---------------------------------------------------------------------------- +PyObject *JM_B64FromBuffer(fz_context *ctx, fz_buffer *buffer) +{ + PyObject *bytes = PyBytes_FromString(""); + char *c = NULL; + char *b64 = NULL; + if (buffer) { - bytes = PyBytes_FromString(""); + size_t len = fz_buffer_storage(ctx, buffer, &c); + fz_buffer *res = fz_new_buffer(ctx, len); + fz_output *out = fz_new_output_with_buffer(ctx, res); + fz_write_base64(ctx, out, (const unsigned char *) c, (int) len, 0); + size_t nlen = fz_buffer_storage(ctx, res, &b64); + bytes = PyBytes_FromStringAndSize(b64, (Py_ssize_t) nlen); + fz_drop_buffer(ctx, res); + fz_drop_output(ctx, out); } return bytes; } @@ -3276,7 +3594,7 @@ fz_buffer *JM_deflatebuf(fz_context *ctx, unsigned char *p, size_t n) //---------------------------------------------------------------------------- // update a stream object -// compress stream where beneficial +// compress stream when beneficial //---------------------------------------------------------------------------- void JM_update_stream(fz_context *ctx, pdf_document *doc, pdf_obj *obj, fz_buffer *buffer) { @@ -3292,7 +3610,7 @@ void JM_update_stream(fz_context *ctx, pdf_document *doc, pdf_obj *obj, fz_buffe } if (nlen < len) // was it worth the effort? { - pdf_dict_put(ctx, obj, PDF_NAME_Filter, PDF_NAME_FlateDecode); + pdf_dict_put(ctx, obj, PDF_NAME(Filter), PDF_NAME(FlateDecode)); pdf_update_stream(ctx, doc, obj, nres, 1); } else @@ -3307,21 +3625,22 @@ void JM_update_stream(fz_context *ctx, pdf_document *doc, pdf_obj *obj, fz_buffe // of only the 'clip' part of the displaylist rectangle //----------------------------------------------------------------------------- fz_pixmap * -JM_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, const fz_matrix *ctm, fz_colorspace *cs, int alpha, const fz_rect *clip) +JM_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, PyObject *ctm, fz_colorspace *cs, int alpha, PyObject *clip) { - fz_rect rect; - fz_irect irect; + fz_rect rect = fz_bound_display_list(ctx, list); + fz_matrix matrix = JM_matrix_from_py(ctm); fz_pixmap *pix = NULL; fz_var(pix); fz_device *dev = NULL; fz_var(dev); fz_separations *seps = NULL; - fz_bound_display_list(ctx, list, &rect); - if (clip) fz_intersect_rect(&rect, clip); - fz_transform_rect(&rect, ctm); - fz_round_rect(&irect, &rect); + fz_rect rclip = JM_rect_from_py(clip); + rect = fz_intersect_rect(rect, rclip); // no-op if clip is not given - pix = fz_new_pixmap_with_bbox(ctx, cs, &irect, seps, alpha); + rect = fz_transform_rect(rect, matrix); + fz_irect irect = fz_round_rect(rect); + + pix = fz_new_pixmap_with_bbox(ctx, cs, irect, seps, alpha); if (alpha) fz_clear_pixmap(ctx, pix); else @@ -3329,11 +3648,17 @@ JM_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, const fz_mat fz_try(ctx) { - if (clip) - dev = fz_new_draw_device_with_bbox(ctx, ctm, pix, &irect); + if (!fz_is_infinite_rect(rclip)) + { + dev = fz_new_draw_device_with_bbox(ctx, matrix, pix, &irect); + fz_run_display_list(ctx, list, dev, fz_identity, rclip, NULL); + } else - dev = fz_new_draw_device(ctx, ctm, pix); - fz_run_display_list(ctx, list, dev, &fz_identity, clip, NULL); + { + dev = fz_new_draw_device(ctx, matrix, pix); + fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL); + } + fz_close_device(ctx, dev); } fz_always(ctx) @@ -3389,6 +3714,7 @@ size_t JM_CharFromBytesOrArray(PyObject *stream, char **data) //---------------------------------------------------------------------------- // Return fz_buffer from a PyBytes or PyByteArray object +// Attention: must be freed by caller! //---------------------------------------------------------------------------- fz_buffer *JM_BufferFromBytes(fz_context *ctx, PyObject *stream) { @@ -3457,9 +3783,9 @@ void page_merge(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, i pdf_obj *obj = NULL, *ref = NULL, *subt = NULL; // list of object types (per page) we want to copy - pdf_obj *known_page_objs[] = {PDF_NAME_Contents, PDF_NAME_Resources, - PDF_NAME_MediaBox, PDF_NAME_CropBox, PDF_NAME_BleedBox, PDF_NAME_Annots, - PDF_NAME_TrimBox, PDF_NAME_ArtBox, PDF_NAME_Rotate, PDF_NAME_UserUnit}; + pdf_obj *known_page_objs[] = {PDF_NAME(Contents), PDF_NAME(Resources), + PDF_NAME(MediaBox), PDF_NAME(CropBox), PDF_NAME(BleedBox), PDF_NAME(Annots), + PDF_NAME(TrimBox), PDF_NAME(ArtBox), PDF_NAME(Rotate), PDF_NAME(UserUnit)}; int n = nelem(known_page_objs); // number of list elements int i; int num, j; @@ -3472,7 +3798,7 @@ void page_merge(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, i pdf_flatten_inheritable_page_items(ctx, pageref); // make a new page page_dict = pdf_new_dict(ctx, doc_des, 4); - pdf_dict_put_drop(ctx, page_dict, PDF_NAME_Type, PDF_NAME_Page); + pdf_dict_put_drop(ctx, page_dict, PDF_NAME(Type), PDF_NAME(Page)); // copy objects of source page into it for (i = 0; i < n; i++) @@ -3482,12 +3808,12 @@ void page_merge(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, i pdf_dict_put_drop(ctx, page_dict, known_page_objs[i], pdf_graft_mapped_object(ctx, graft_map, obj)); } // remove any links from annots array - pdf_obj *annots = pdf_dict_get(ctx, page_dict, PDF_NAME_Annots); + pdf_obj *annots = pdf_dict_get(ctx, page_dict, PDF_NAME(Annots)); int len = pdf_array_len(ctx, annots); for (j = 0; j < len; j++) { pdf_obj *o = pdf_array_get(ctx, annots, j); - if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME_Subtype), PDF_NAME_Link)) + if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME(Subtype)), PDF_NAME(Link))) continue; // remove the link annotation pdf_array_delete(ctx, annots, j); @@ -3497,8 +3823,8 @@ void page_merge(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, i // rotate the page as requested if (rotate != -1) { - pdf_obj *rotateobj = pdf_new_int(ctx, doc_des, rotate); - pdf_dict_put_drop(ctx, page_dict, PDF_NAME_Rotate, rotateobj); + pdf_obj *rotateobj = pdf_new_int(ctx, (int64_t) rotate); + pdf_dict_put_drop(ctx, page_dict, PDF_NAME(Rotate), rotateobj); } // Now add the page dictionary to dest PDF obj = pdf_add_object_drop(ctx, doc_des, page_dict); @@ -3556,49 +3882,25 @@ void merge_range(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, } //---------------------------------------------------------------------------- -// Fills table 'res' with outline xref numbers -// 'res' must be a correctly pre-allocated table of integers -// 'obj' must be the first OL item -// returns (int) number of filled-in outline item xref numbers. +// Return list of outline xref numbers. Recursive function. Arguments: +// 'obj' first OL item +// 'xrefs' empty Python list //---------------------------------------------------------------------------- -int fillOLNumbers(fz_context *ctx, int *res, pdf_obj *obj, int oc, int argc) +PyObject *JM_outline_xrefs(fz_context *ctx, pdf_obj *obj, PyObject *xrefs) { - int onum; pdf_obj *first, *parent, *thisobj; - if (!obj) return oc; - if (oc >= argc) return oc; + if (!obj) return xrefs; thisobj = obj; - while (thisobj) { - onum = pdf_to_num(ctx, thisobj); - res[oc] = onum; - oc += 1; - first = pdf_dict_get(ctx, thisobj, PDF_NAME_First); /* try go down */ - if (first) oc = fillOLNumbers(ctx, res, first, oc, argc); /* recurse */ - thisobj = pdf_dict_get(ctx, thisobj, PDF_NAME_Next); /* try go next */ - parent = pdf_dict_get(ctx, thisobj, PDF_NAME_Parent); /* get parent */ - if (!thisobj) thisobj = parent; /* goto parent if no next obj */ - } - return oc; -} - -//---------------------------------------------------------------------------- -// Returns number of outlines -// 'obj' must be first OL item -//---------------------------------------------------------------------------- -int countOutlines(fz_context *ctx, pdf_obj *obj, int oc) -{ - pdf_obj *first, *parent, *thisobj; - if (!obj) return oc; - thisobj = obj; - while (thisobj) { - oc += 1; - first = pdf_dict_get(ctx, thisobj, PDF_NAME_First); /* try go down */ - if (first) oc = countOutlines(ctx, first, oc); - thisobj = pdf_dict_get(ctx, thisobj, PDF_NAME_Next); /* try go next */ - parent = pdf_dict_get(ctx, thisobj, PDF_NAME_Parent); /* get parent */ + while (thisobj) + { + PyList_Append(xrefs, Py_BuildValue("i", pdf_to_num(ctx, thisobj))); + first = pdf_dict_get(ctx, thisobj, PDF_NAME(First)); // try go down + if (first) xrefs = JM_outline_xrefs(ctx, first, xrefs); + thisobj = pdf_dict_get(ctx, thisobj, PDF_NAME(Next)); // try go next + parent = pdf_dict_get(ctx, thisobj, PDF_NAME(Parent)); // get parent if (!thisobj) thisobj = parent; /* goto parent if no next exists */ } - return oc; + return xrefs; } //----------------------------------------------------------------------------- @@ -3610,14 +3912,14 @@ fz_buffer *fontbuffer(fz_context *ctx, pdf_document *doc, int xref) pdf_obj *o, *obj = NULL, *desft, *stream = NULL; char *ext = ""; o = pdf_load_object(ctx, doc, xref); - desft = pdf_dict_get(ctx, o, PDF_NAME_DescendantFonts); + desft = pdf_dict_get(ctx, o, PDF_NAME(DescendantFonts)); if (desft) { obj = pdf_resolve_indirect(ctx, pdf_array_get(ctx, desft, 0)); - obj = pdf_dict_get(ctx, obj, PDF_NAME_FontDescriptor); + obj = pdf_dict_get(ctx, obj, PDF_NAME(FontDescriptor)); } else - obj = pdf_dict_get(ctx, o, PDF_NAME_FontDescriptor); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontDescriptor)); if (!obj) { @@ -3628,29 +3930,29 @@ fz_buffer *fontbuffer(fz_context *ctx, pdf_document *doc, int xref) pdf_drop_obj(ctx, o); o = obj; - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile)); if (obj) stream = obj; // ext = "pfa" - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile2); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile2)); if (obj) stream = obj; // ext = "ttf" - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile3); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile3)); if (obj) { stream = obj; - obj = pdf_dict_get(ctx, obj, PDF_NAME_Subtype); + obj = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)); if (obj && !pdf_is_name(ctx, obj)) { PySys_WriteStdout("invalid font descriptor subtype"); return NULL; } - if (pdf_name_eq(ctx, obj, PDF_NAME_Type1C)) + if (pdf_name_eq(ctx, obj, PDF_NAME(Type1C))) ext = "cff"; - else if (pdf_name_eq(ctx, obj, PDF_NAME_CIDFontType0C)) + else if (pdf_name_eq(ctx, obj, PDF_NAME(CIDFontType0C))) ext = "cid"; - else if (pdf_name_eq(ctx, obj, PDF_NAME_OpenType)) + else if (pdf_name_eq(ctx, obj, PDF_NAME(OpenType))) ext = "otf"; else PySys_WriteStdout("warning: unhandled font type '%s'", pdf_to_name(ctx, obj)); @@ -3673,40 +3975,40 @@ char *fontextension(fz_context *ctx, pdf_document *doc, int xref) if (xref < 1) return "n/a"; pdf_obj *o, *obj = NULL, *desft; o = pdf_load_object(ctx, doc, xref); - desft = pdf_dict_get(ctx, o, PDF_NAME_DescendantFonts); + desft = pdf_dict_get(ctx, o, PDF_NAME(DescendantFonts)); if (desft) { obj = pdf_resolve_indirect(ctx, pdf_array_get(ctx, desft, 0)); - obj = pdf_dict_get(ctx, obj, PDF_NAME_FontDescriptor); + obj = pdf_dict_get(ctx, obj, PDF_NAME(FontDescriptor)); } else - obj = pdf_dict_get(ctx, o, PDF_NAME_FontDescriptor); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontDescriptor)); pdf_drop_obj(ctx, o); if (!obj) return "n/a"; // this is a base-14 font o = obj; // we have the FontDescriptor - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile)); if (obj) return "pfa"; - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile2); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile2)); if (obj) return "ttf"; - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile3); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile3)); if (obj) { - obj = pdf_dict_get(ctx, obj, PDF_NAME_Subtype); + obj = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)); if (obj && !pdf_is_name(ctx, obj)) { PySys_WriteStdout("invalid font descriptor subtype"); return "n/a"; } - if (pdf_name_eq(ctx, obj, PDF_NAME_Type1C)) + if (pdf_name_eq(ctx, obj, PDF_NAME(Type1C))) return "cff"; - else if (pdf_name_eq(ctx, obj, PDF_NAME_CIDFontType0C)) + else if (pdf_name_eq(ctx, obj, PDF_NAME(CIDFontType0C))) return "cid"; - else if (pdf_name_eq(ctx, obj, PDF_NAME_OpenType)) + else if (pdf_name_eq(ctx, obj, PDF_NAME(OpenType))) return "otf"; else PySys_WriteStdout("unhandled font type '%s'", pdf_to_name(ctx, obj)); @@ -3715,6 +4017,33 @@ char *fontextension(fz_context *ctx, pdf_document *doc, int xref) return "n/a"; } +//----------------------------------------------------------------------------- +// create PDF object from given string (new in v1.14.0: MuPDF dropped it) +//----------------------------------------------------------------------------- +pdf_obj *JM_pdf_obj_from_str(fz_context *ctx, pdf_document *doc, char *src) +{ + pdf_obj *result = NULL; + pdf_lexbuf lexbuf; + fz_stream *stream = fz_open_memory(ctx, (unsigned char *)src, strlen(src)); + + pdf_lexbuf_init(ctx, &lexbuf, PDF_LEXBUF_SMALL); + + fz_try(ctx) + result = pdf_parse_stm_obj(ctx, doc, stream, &lexbuf); + + fz_always(ctx) + { + pdf_lexbuf_fin(ctx, &lexbuf); + fz_drop_stream(ctx, stream); + } + + fz_catch(ctx) + fz_rethrow(ctx); + + return result; + +} + //----------------------------------------------------------------------------- // dummy structure for various tools and utilities //----------------------------------------------------------------------------- @@ -3754,5063 +4083,6057 @@ struct fz_store_s int needs_reaping; }; +//----------------------------------------------------------------------------- +// START redirect stdout/stderr +//----------------------------------------------------------------------------- +PyObject *JM_error_log; +PyObject *JM_output_log; +static void +JM_write_stdout(fz_context *ctx, void *opaque, const void *buffer, size_t count) +{ + if (!buffer || !count) return; + PyObject *c = Py_BuildValue("s#", (const char *) buffer, (Py_ssize_t) count); + if (!c || c == NONE) return; + PyList_Append(JM_output_log, c); + Py_CLEAR(c); + return; +} -//---------------------------------------------------------------------------- -// annotation types -//---------------------------------------------------------------------------- -#define ANNOT_TEXT 0 -#define ANNOT_LINK 1 -#define ANNOT_FREETEXT 2 -#define ANNOT_LINE 3 -#define ANNOT_SQUARE 4 -#define ANNOT_CIRCLE 5 -#define ANNOT_POLYGON 6 -#define ANNOT_POLYLINE 7 -#define ANNOT_HIGHLIGHT 8 -#define ANNOT_UNDERLINE 9 -#define ANNOT_SQUIGGLY 10 -#define ANNOT_STRIKEOUT 11 -#define ANNOT_STAMP 12 -#define ANNOT_CARET 13 -#define ANNOT_INK 14 -#define ANNOT_POPUP 15 -#define ANNOT_FILEATTACHMENT 16 -#define ANNOT_SOUND 17 -#define ANNOT_MOVIE 18 -#define ANNOT_WIDGET 19 -#define ANNOT_SCREEN 20 -#define ANNOT_PRINTERMARK 21 -#define ANNOT_TRAPNET 22 -#define ANNOT_WATERMARK 23 -#define ANNOT_3D 24 +static void +JM_write_stderr(fz_context *ctx, void *opaque, const void *buffer, size_t count) +{ + if (!buffer || !count) return; + PyObject *c = Py_BuildValue("s#", (const char *) buffer, (Py_ssize_t) count); + if (!c || c == NONE) return; + PyList_Append(JM_error_log, c); + Py_CLEAR(c); + return; +} -//---------------------------------------------------------------------------- -// annotation flag bits -//---------------------------------------------------------------------------- -#define ANNOT_XF_Invisible 1 << (1-1) -#define ANNOT_XF_Hidden 1 << (2-1) -#define ANNOT_XF_Print 1 << (3-1) -#define ANNOT_XF_NoZoom 1 << (4-1) -#define ANNOT_XF_NoRotate 1 << (5-1) -#define ANNOT_XF_NoView 1 << (6-1) -#define ANNOT_XF_ReadOnly 1 << (7-1) -#define ANNOT_XF_Locked 1 << (8-1) -#define ANNOT_XF_ToggleNoView 1 << (9-1) -#define ANNOT_XF_LockedContents 1 << (10-1) +fz_output *JM_fitz_stdout; +fz_output *JM_fitz_stderr; +//----------------------------------------------------------------------------- +// STOP redirect stdout/stderr +//----------------------------------------------------------------------------- -//---------------------------------------------------------------------------- -// annotation line ending styles -//---------------------------------------------------------------------------- -#define ANNOT_LE_None 0 -#define ANNOT_LE_Square 1 -#define ANNOT_LE_Circle 2 -#define ANNOT_LE_Diamond 3 -#define ANNOT_LE_OpenArrow 4 -#define ANNOT_LE_ClosedArrow 5 -#define ANNOT_LE_Butt 6 -#define ANNOT_LE_ROpenArrow 7 -#define ANNOT_LE_RClosedArrow 8 -#define ANNOT_LE_Slash 9 -//---------------------------------------------------------------------------- -// annotation field (widget) types -//---------------------------------------------------------------------------- -#define ANNOT_WG_NOT_WIDGET -1 -#define ANNOT_WG_PUSHBUTTON 0 -#define ANNOT_WG_CHECKBOX 1 -#define ANNOT_WG_RADIOBUTTON 2 -#define ANNOT_WG_TEXT 3 -#define ANNOT_WG_LISTBOX 4 -#define ANNOT_WG_COMBOBOX 5 -#define ANNOT_WG_SIGNATURE 6 -//---------------------------------------------------------------------------- -// annotation text widget subtypes -//---------------------------------------------------------------------------- -#define ANNOT_WG_TEXT_UNRESTRAINED 0 -#define ANNOT_WG_TEXT_NUMBER 1 -#define ANNOT_WG_TEXT_SPECIAL 2 -#define ANNOT_WG_TEXT_DATE 3 -#define ANNOT_WG_TEXT_TIME 4 +void pdf_dict_put_val_null(fz_context *ctx, pdf_obj *obj, int idx); -//---------------------------------------------------------------------------- -// annotation widget flags -//---------------------------------------------------------------------------- -// Common to all field types -#define WIDGET_Ff_ReadOnly 1 -#define WIDGET_Ff_Required 2 -#define WIDGET_Ff_NoExport 4 +#include +#include +#include -// Text fields -#define WIDGET_Ff_Multiline 4096 -#define WIDGET_Ff_Password 8192 +#define SIG_EXTRAS_SIZE (1024) -#define WIDGET_Ff_FileSelect 1048576 -#define WIDGET_Ff_DoNotSpellCheck 4194304 -#define WIDGET_Ff_DoNotScroll 8388608 -#define WIDGET_Ff_Comb 16777216 -#define WIDGET_Ff_RichText 33554432 +typedef struct pdf_write_state_s pdf_write_state; -// Button fields -#define WIDGET_Ff_NoToggleToOff 16384 -#define WIDGET_Ff_Radio 32768 -#define WIDGET_Ff_Pushbutton 65536 -#define WIDGET_Ff_RadioInUnison 33554432 +/* + As part of linearization, we need to keep a list of what objects are used + by what page. We do this by recording the objects used in a given page + in a page_objects structure. We have a list of these structures (one per + page) in the page_objects_list structure. + + The page_objects structure maintains a heap in the object array, so + insertion takes log n time, and we can heapsort and dedupe at the end for + a total worse case n log n time. + + The magic heap invariant is that: + entry[n] >= entry[(n+1)*2-1] & entry[n] >= entry[(n+1)*2] + or equivalently: + entry[(n-1)>>1] >= entry[n] + + For a discussion of the heap data structure (and heapsort) see Kingston, + "Algorithms and Data Structures". +*/ -// Choice fields -#define WIDGET_Ff_Combo 131072 -#define WIDGET_Ff_Edit 262144 -#define WIDGET_Ff_Sort 524288 -#define WIDGET_Ff_MultiSelect 2097152 -#define WIDGET_Ff_CommitOnSelCHange 67108864 +typedef struct { + int num_shared; + int page_object_number; + int num_objects; + int min_ofs; + int max_ofs; + /* Extensible list of objects used on this page */ + int cap; + int len; + int object[1]; +} page_objects; -//---------------------------------------------------------------------------- -// return string for line end style -//---------------------------------------------------------------------------- -int JM_le_value(fz_context *ctx, char *le) -{ - if (!le) return ANNOT_LE_None; - return pdf_line_ending_from_string(ctx, le); -} +typedef struct { + int cap; + int len; + page_objects *page[1]; +} page_objects_list; -//---------------------------------------------------------------------------- -// return pdf_obj "border style" from Python str -//---------------------------------------------------------------------------- -pdf_obj *JM_get_border_style(fz_context *ctx, PyObject *style) +struct pdf_write_state_s { - pdf_obj *val = PDF_NAME_S; - if (!style) return val; - char *s = JM_Python_str_AsChar(style); - JM_PyErr_Clear; - if (!s) return val; - if (!strncmp(s, "b", 1) || !strncmp(s, "B", 1)) val = PDF_NAME_B; - else if (!strncmp(s, "d", 1) || !strncmp(s, "D", 1)) val = PDF_NAME_D; - else if (!strncmp(s, "i", 1) || !strncmp(s, "I", 1)) val = PDF_NAME_I; - else if (!strncmp(s, "u", 1) || !strncmp(s, "U", 1)) val = PDF_NAME_U; - JM_Python_str_DelForPy3(s); - return val; -} + fz_output *out; + + int do_incremental; + int do_tight; + int do_ascii; + int do_expand; + int do_compress; + int do_compress_images; + int do_compress_fonts; + int do_garbage; + int do_linear; + int do_clean; + + int list_len; + int *use_list; + int64_t *ofs_list; + int *gen_list; + int *renumber_map; + int continue_on_error; + int *errors; + /* The following extras are required for linearization */ + int *rev_renumber_map; + int start; + int64_t first_xref_offset; + int64_t main_xref_offset; + int64_t first_xref_entry_offset; + int64_t file_len; + int hints_shared_offset; + int hintstream_len; + pdf_obj *linear_l; + pdf_obj *linear_h0; + pdf_obj *linear_h1; + pdf_obj *linear_o; + pdf_obj *linear_e; + pdf_obj *linear_n; + pdf_obj *linear_t; + pdf_obj *hints_s; + pdf_obj *hints_length; + int page_count; + page_objects_list *page_object_lists; + int crypt_object_number; +}; -//---------------------------------------------------------------------------- -// refreshes the link and annotation tables of a page -//---------------------------------------------------------------------------- -void refresh_link_table(fz_context *ctx, pdf_page *page) +/* + * Constants for use with use_list. + * + * If use_list[num] = 0, then object num is unused. + * If use_list[num] & PARAMS, then object num is the linearisation params obj. + * If use_list[num] & CATALOGUE, then object num is used by the catalogue. + * If use_list[num] & PAGE1, then object num is used by page 1. + * If use_list[num] & SHARED, then object num is shared between pages. + * If use_list[num] & PAGE_OBJECT then this must be the first object in a page. + * If use_list[num] & OTHER_OBJECTS then this must should appear in section 9. + * Otherwise object num is used by page (use_list[num]>>USE_PAGE_SHIFT). + */ +enum { - pdf_obj *annots_arr = pdf_dict_get(ctx, page->obj, PDF_NAME_Annots); - if (annots_arr) - { - fz_rect page_mediabox; - fz_matrix page_ctm; - pdf_page_transform(ctx, page, &page_mediabox, &page_ctm); - page->links = pdf_load_link_annots(ctx, page->doc, annots_arr, - pdf_to_num(ctx, page->obj), &page_ctm); - pdf_load_annots(ctx, page, annots_arr); - } - return; -} + USE_CATALOGUE = 2, + USE_PAGE1 = 4, + USE_SHARED = 8, + USE_PARAMS = 16, + USE_HINTS = 32, + USE_PAGE_OBJECT = 64, + USE_OTHER_OBJECTS = 128, + USE_PAGE_MASK = ~255, + USE_PAGE_SHIFT = 8 +}; -//----------------------------------------------------------------------------- -// create a strike-out / underline / highlight annotation -//----------------------------------------------------------------------------- -struct fz_annot_s *JM_AnnotTextmarker(fz_context *ctx, pdf_page *page, fz_rect *rect, int type) +/* + * page_objects and page_object_list handling functions + */ +static page_objects_list * +page_objects_list_create(fz_context *ctx) { - pdf_annot *annot = NULL; - float line_thickness = 0.0f; - float line_height = 0.0f; - float alpha = 1.0f; - float h = rect->y1 - rect->y0; - fz_rect bbox = {rect->x0, rect->y0, rect->x1, rect->y1}; - float color[3] = {0,0,0}; - switch (type) - { - case PDF_ANNOT_HIGHLIGHT: - color[0] = 1.0f; - color[1] = 1.0f; - color[2] = 0.0f; - alpha = 0.3f; - line_thickness = 1.0f; - line_height = 0.5f; - break; - case PDF_ANNOT_UNDERLINE: - color[0] = 0.0f; - color[1] = 0.0f; - color[2] = 1.0f; - alpha = 1.0f; - line_thickness = 0.07f; - line_height = 0.075f; - bbox.y0 += 0.8f * h; - bbox.y1 += 0.8f * h; - break; - case PDF_ANNOT_STRIKE_OUT: - color[0] = 1.0f; - color[1] = 0.0f; - color[2] = 0.0f; - alpha = 1.0f; - line_thickness = 0.07f; - line_height = 0.375f; - bbox.y0 += 0.17f * h; - bbox.y1 += 0.17f * h; - break; - } - fz_try(ctx) - { - pdf_document *pdf = page->doc; - annot = pdf_create_annot(ctx, page, type); - pdf_set_annot_color(ctx, annot, 3, color); - pdf_set_annot_border(ctx, annot, line_thickness); - pdf_add_annot_quad_point(ctx, annot, bbox); - pdf_set_annot_rect(ctx, annot, &bbox); - pdf_set_markup_appearance(ctx, pdf, annot, color, alpha, line_thickness, line_height); - } - fz_catch(ctx) fz_rethrow(ctx); - pdf_dirty_annot(ctx, annot); - return (fz_annot *) annot; -} + page_objects_list *pol = fz_calloc(ctx, 1, sizeof(*pol)); -//----------------------------------------------------------------------------- -// create a circle or rectangle annotation -//----------------------------------------------------------------------------- -struct fz_annot_s *JM_AnnotCircleOrRect(fz_context *ctx, pdf_page *page, fz_rect *rect, int type) -{ - pdf_annot *annot; - float col[3] = {0,0,0}; - float width = 1; - fz_try(ctx) - { - pdf_document *pdf = page->doc; - annot = pdf_create_annot(ctx, page, type); - pdf_set_annot_border(ctx, annot, width); - pdf_set_annot_color(ctx, annot, 3, col); - pdf_set_annot_rect(ctx, annot, rect); - } - fz_catch(ctx) fz_rethrow(ctx); - pdf_dirty_annot(ctx, annot); - return (fz_annot *) annot; + pol->cap = 1; + pol->len = 0; + return pol; } -//----------------------------------------------------------------------------- -// create a polyline or polygon annotation -//----------------------------------------------------------------------------- -struct fz_annot_s *JM_AnnotMultiline(fz_context *ctx, pdf_page *page, PyObject *points, int type) +static void +page_objects_list_destroy(fz_context *ctx, page_objects_list *pol) { - pdf_annot *annot; - fz_try(ctx) + int i; + + if (!pol) + return; + for (i = 0; i < pol->len; i++) { - float col[3] = {0,0,0}; - float width = 1; - fz_point point = {0,0}; - fz_rect rect; - - int n = 0, i; - if (PySequence_Check(points)) n = PySequence_Size(points); - if (n < 2) THROWMSG("invalid points list"); - annot = pdf_create_annot(ctx, page, type); - for (i = 0; i < n; i++) - { - PyObject *p = PySequence_ITEM(points, i); - if (!PySequence_Check(p) || PySequence_Size(p) != 2) - THROWMSG("invalid points list"); - point.x = (float) PyFloat_AsDouble(PySequence_GetItem(p, 0)); - point.y = (float) PyFloat_AsDouble(PySequence_GetItem(p, 1)); - Py_CLEAR(p); - pdf_add_annot_vertex(ctx, annot, point); - if (i == 0) - { - rect.x0 = point.x; - rect.y0 = point.y; - rect.x1 = point.x; - rect.y1 = point.y; - } - else - fz_include_point_in_rect(&rect, &point); - } - pdf_set_annot_border(ctx, annot, width); // standard: width = 1 - pdf_set_annot_color(ctx, annot, 3, col); // standard: black - fz_expand_rect(&rect, 3 * width); - pdf_set_annot_rect(ctx, annot, &rect); + fz_free(ctx, pol->page[i]); } - fz_catch(ctx) fz_rethrow(ctx); - pdf_dirty_annot(ctx, annot); - return (fz_annot *) annot; + fz_free(ctx, pol); } -void JM_draw_pushpin1(fz_context *ctx, fz_path *path) -{ - fz_moveto(ctx, path, 2.8f, 29.0f); - fz_lineto(ctx, path, 17.2f, 29.0f); - fz_lineto(ctx, path, 13.6f, 22.0f); - fz_lineto(ctx, path, 13.6f, 17.8f); - fz_lineto(ctx, path, 19.0f, 10.8f); - fz_lineto(ctx, path, 1.0f, 10.8f); - fz_lineto(ctx, path, 6.4f, 17.8f); - fz_lineto(ctx, path, 6.4f, 22.0f); - fz_lineto(ctx, path, 2.8f, 29.0f); - fz_closepath(ctx, path); -} - -void JM_draw_pushpin2(fz_context *ctx, fz_path *path) +static void +page_objects_list_ensure(fz_context *ctx, page_objects_list **pol, int newcap) { - fz_moveto(ctx, path, 13.6f, 22.0f); - fz_lineto(ctx, path, 6.4f, 22.0f); - fz_moveto(ctx, path, 13.6f, 17.8f); - fz_lineto(ctx, path, 6.4f, 17.8f); - fz_closepath(ctx, path); + int oldcap = (*pol)->cap; + if (newcap <= oldcap) + return; + *pol = fz_resize_array(ctx, *pol, 1, sizeof(page_objects_list) + (newcap-1)*sizeof(page_objects *)); + memset(&(*pol)->page[oldcap], 0, (newcap-oldcap)*sizeof(page_objects *)); + (*pol)->cap = newcap; } -void JM_draw_pushpin3(fz_context *ctx, fz_path *path) +static page_objects * +page_objects_create(fz_context *ctx) { - fz_moveto(ctx, path, 9.1f, 10.8f); - fz_lineto(ctx, path, 10.0f, 1.0f); - fz_lineto(ctx, path, 10.9f, 10.8f); - fz_closepath(ctx, path); + int initial_cap = 8; + page_objects *po = fz_calloc(ctx, 1, sizeof(*po) + (initial_cap-1) * sizeof(int)); + + po->cap = initial_cap; + po->len = 0; + return po; } -void JM_update_file_attachment_annot(fz_context *ctx, pdf_document *doc, pdf_annot *annot) +static void +page_objects_insert(fz_context *ctx, page_objects **ppo, int i) { - static float yellow[3] = {1.0f, 1.0f, 0.0f}; - static float blue[3] = {0.0f, 0.0f, 1.0f}; - static float black[3] = {0.0f, 0.0f, 0.0f}; - static float outline_thickness = 0.9f; - fz_display_list *dlist = NULL; - fz_device *dev = NULL; - fz_colorspace *cs = NULL; - fz_path *path = NULL; - fz_stroke_state *stroke = NULL; - fz_matrix page_ctm; - pdf_page_transform(ctx, annot->page, NULL, &page_ctm); - fz_var(path); - fz_var(stroke); - fz_var(dlist); - fz_var(dev); - fz_var(cs); - fz_try(ctx) - { - fz_rect rect; + page_objects *po; - pdf_to_rect(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME_Rect), &rect); - dlist = fz_new_display_list(ctx, NULL); - dev = fz_new_list_device(ctx, dlist); - cs = fz_device_rgb(ctx); /* Borrowed reference */ - stroke = fz_new_stroke_state(ctx); - stroke->linewidth = outline_thickness; - stroke->linejoin = FZ_LINEJOIN_ROUND; - - path = fz_new_path(ctx); - JM_draw_pushpin1(ctx, path); - fz_fill_path(ctx, dev, path, 0, &page_ctm, cs, yellow, 1.0f, NULL); - fz_stroke_path(ctx, dev, path, stroke, &page_ctm, cs, black, 1.0f, NULL); - fz_drop_path(ctx, path); - path = NULL; - - path = fz_new_path(ctx); - JM_draw_pushpin2(ctx, path); - fz_stroke_path(ctx, dev, path, stroke, &page_ctm, cs, black, 1.0f, NULL); - fz_drop_path(ctx, path); - path = NULL; - - path = fz_new_path(ctx); - JM_draw_pushpin3(ctx, path); - fz_fill_path(ctx, dev, path, 0, &page_ctm, cs, blue, 1.0f, NULL); - fz_stroke_path(ctx, dev, path, stroke, &page_ctm, cs, black, 1.0f, NULL); - fz_close_device(ctx, dev); + /* Make a page_objects if we don't have one */ + if (*ppo == NULL) + *ppo = page_objects_create(ctx); - fz_transform_rect(&rect, &page_ctm); - pdf_set_annot_appearance(ctx, doc, annot, &rect, dlist); - rect.x0 = rect.y0 = 0; - rect.x1 = 20; - rect.y1 = 30; - pdf_obj *ap = pdf_dict_getl(ctx, annot->obj, PDF_NAME_AP, PDF_NAME_N, NULL); - pdf_dict_put_rect(ctx, ap, PDF_NAME_BBox, &rect); - pdf_drop_obj(ctx, annot->ap); - annot->ap = NULL; - } - fz_always(ctx) - { - fz_drop_device(ctx, dev); - fz_drop_display_list(ctx, dlist); - fz_drop_stroke_state(ctx, stroke); - fz_drop_path(ctx, path); - } - fz_catch(ctx) + po = *ppo; + /* page_objects insertion: extend the page_objects by 1, and put us on the end */ + if (po->len == po->cap) { - fz_rethrow(ctx); + po = fz_resize_array(ctx, po, 1, sizeof(page_objects) + (po->cap*2 - 1)*sizeof(int)); + po->cap *= 2; + *ppo = po; } + po->object[po->len++] = i; } +static void +page_objects_list_insert(fz_context *ctx, pdf_write_state *opts, int page, int object) +{ + page_objects_list_ensure(ctx, &opts->page_object_lists, page+1); + if (opts->page_object_lists->len < page+1) + opts->page_object_lists->len = page+1; + page_objects_insert(ctx, &opts->page_object_lists->page[page], object); +} +static void +page_objects_list_set_page_object(fz_context *ctx, pdf_write_state *opts, int page, int object) +{ + page_objects_list_ensure(ctx, &opts->page_object_lists, page+1); + opts->page_object_lists->page[page]->page_object_number = object; +} -PyObject *JM_UnicodeFromASCII(const char *in); -//----------------------------------------------------------------------------- -// Plain text output. An identical copy of fz_print_stext_page_as_text, -// but lines within a block are concatenated by space instead a new-line -// character (which else leads to 2 new-lines). -//----------------------------------------------------------------------------- -void -JM_print_stext_page_as_text(fz_context *ctx, fz_output *out, fz_stext_page *page) +static void +page_objects_sort(fz_context *ctx, page_objects *po) { - fz_stext_block *block; - fz_stext_line *line; - fz_stext_char *ch; - char utf[10]; - int i, n; + int i, j; + int n = po->len; - for (block = page->first_block; block; block = block->next) + /* Step 1: Make a heap */ + /* Invariant: Valid heap in [0..i), unsorted elements in [i..n) */ + for (i = 1; i < n; i++) { - if (block->type == FZ_STEXT_BLOCK_TEXT) + /* Now bubble backwards to maintain heap invariant */ + j = i; + while (j != 0) { - int line_n = 0; - for (line = block->u.t.first_line; line; line = line->next) - { - if (line_n > 0) fz_write_string(ctx, out, " "); - line_n++; - for (ch = line->first_char; ch; ch = ch->next) - { - n = fz_runetochar(utf, ch->c); - for (i = 0; i < n; i++) - fz_write_byte(ctx, out, utf[i]); - } - } - fz_write_string(ctx, out, "\n"); + int tmp; + int k = (j-1)>>1; + if (po->object[k] >= po->object[j]) + break; + tmp = po->object[k]; + po->object[k] = po->object[j]; + po->object[j] = tmp; + j = k; } } -} -//----------------------------------------------------------------------------- -// Functions for wordlist output -//----------------------------------------------------------------------------- -int JM_append_word(fz_context *ctx, PyObject *lines, fz_buffer *buff, fz_rect *wbbox, - int block_n, int line_n, int word_n) + /* Step 2: Heap sort */ + /* Invariant: valid heap in [0..i), sorted list in [i..n) */ + /* Initially: i = n */ + for (i = n-1; i > 0; i--) + { + /* Swap the maximum (0th) element from the page_objects into its place + * in the sorted list (position i). */ + int tmp = po->object[0]; + po->object[0] = po->object[i]; + po->object[i] = tmp; + /* Now, the page_objects is invalid because the 0th element is out + * of place. Bubble it until the page_objects is valid. */ + j = 0; + while (1) + { + /* Children are k and k+1 */ + int k = (j+1)*2-1; + /* If both children out of the page_objects, we're done */ + if (k > i-1) + break; + /* If both are in the page_objects, pick the larger one */ + if (k < i-1 && po->object[k] < po->object[k+1]) + k++; + /* If j is bigger than k (i.e. both of its children), + * we're done */ + if (po->object[j] > po->object[k]) + break; + tmp = po->object[k]; + po->object[k] = po->object[j]; + po->object[j] = tmp; + j = k; + } + } +} + +static int +order_ge(int ui, int uj) { - PyObject *litem = Py_BuildValue("ffffOiii", wbbox->x0, wbbox->y0, wbbox->x1, wbbox->y1, - JM_StrFromBuffer(ctx, buff), - block_n, line_n, word_n); - PyList_Append(lines, litem); - Py_CLEAR(litem); - wbbox->x0 = wbbox->y0 = wbbox->x1 = wbbox->y1 = 0; - return word_n + 1; // word counter -} + /* + For linearization, we need to order the sections as follows: + + Remaining pages (Part 7) + Shared objects (Part 8) + Objects not associated with any page (Part 9) + Any "other" objects + (Header)(Part 1) + (Linearization params) (Part 2) + (1st page Xref/Trailer) (Part 3) + Catalogue (and other document level objects) (Part 4) + First page (Part 6) + (Primary Hint stream) (*) (Part 5) + Any free objects + + Note, this is NOT the same order they appear in + the final file! + + (*) The PDF reference gives us the option of putting the hint stream + after the first page, and we take it, for simplicity. + */ -//----------------------------------------------------------------------------- -// Functions for dictionary output -//----------------------------------------------------------------------------- + /* If the 2 objects are in the same section, then page object comes first. */ + if (((ui ^ uj) & ~USE_PAGE_OBJECT) == 0) + return ((ui & USE_PAGE_OBJECT) == 0); + /* Put unused objects last */ + else if (ui == 0) + return 1; + else if (uj == 0) + return 0; + /* Put the hint stream before that... */ + else if (ui & USE_HINTS) + return 1; + else if (uj & USE_HINTS) + return 0; + /* Put page 1 before that... */ + else if (ui & USE_PAGE1) + return 1; + else if (uj & USE_PAGE1) + return 0; + /* Put the catalogue before that... */ + else if (ui & USE_CATALOGUE) + return 1; + else if (uj & USE_CATALOGUE) + return 0; + /* Put the linearization params before that... */ + else if (ui & USE_PARAMS) + return 1; + else if (uj & USE_PARAMS) + return 0; + /* Put other objects before that */ + else if (ui & USE_OTHER_OBJECTS) + return 1; + else if (uj & USE_OTHER_OBJECTS) + return 0; + /* Put shared objects before that... */ + else if (ui & USE_SHARED) + return 1; + else if (uj & USE_SHARED) + return 0; + /* And otherwise, order by the page number on which + * they are used. */ + return (ui>>USE_PAGE_SHIFT) >= (uj>>USE_PAGE_SHIFT); +} -// create an empty rectangle -------------------------------------------------- -fz_rect *JM_empty_rect() +static void +heap_sort(int *list, int n, const int *val, int (*ge)(int, int)) { - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = r->y0 = r->x1 = r->y1 = 0; - return r; -} + int i, j; + + /* Step 1: Make a heap */ + /* Invariant: Valid heap in [0..i), unsorted elements in [i..n) */ + for (i = 1; i < n; i++) + { + /* Now bubble backwards to maintain heap invariant */ + j = i; + while (j != 0) + { + int tmp; + int k = (j-1)>>1; + if (ge(val[list[k]], val[list[j]])) + break; + tmp = list[k]; + list[k] = list[j]; + list[j] = tmp; + j = k; + } + } -// enlarge rect r1 by r2. modify r2-height by size -void JM_join_rect(fz_rect *r1, fz_rect *r2, float size) + /* Step 2: Heap sort */ + /* Invariant: valid heap in [0..i), sorted list in [i..n) */ + /* Initially: i = n */ + for (i = n-1; i > 0; i--) + { + /* Swap the maximum (0th) element from the page_objects into its place + * in the sorted list (position i). */ + int tmp = list[0]; + list[0] = list[i]; + list[i] = tmp; + /* Now, the page_objects is invalid because the 0th element is out + * of place. Bubble it until the page_objects is valid. */ + j = 0; + while (1) + { + /* Children are k and k+1 */ + int k = (j+1)*2-1; + /* If both children out of the page_objects, we're done */ + if (k > i-1) + break; + /* If both are in the page_objects, pick the larger one */ + if (k < i-1 && ge(val[list[k+1]], val[list[k]])) + k++; + /* If j is bigger than k (i.e. both of its children), + * we're done */ + if (ge(val[list[j]], val[list[k]])) + break; + tmp = list[k]; + list[k] = list[j]; + list[j] = tmp; + j = k; + } + } +} + +static void +page_objects_dedupe(fz_context *ctx, page_objects *po) { - fz_rect r = {MIN(r2->x0, r2->x1), MIN(r2->y0, r2->y1), - MAX(r2->x0, r2->x1), MAX(r2->y0, r2->y1)}; - if (abs(r.x1 - r.x0) < 0.00001f) r.x0 = r.x1 - size; - if (abs(r.y1 - r.y0) < 0.00001f) r.y0 = r.y1 - size; + int i, j; + int n = po->len-1; - if (fz_is_empty_rect(r1)) + for (i = 0; i < n; i++) { - r1->x0 = r.x0; - r1->y0 = r.y0; - r1->x1 = r.x1; - r1->y1 = r.y1; + if (po->object[i] == po->object[i+1]) + break; } - else + j = i; /* j points to the last valid one */ + i++; /* i points to the first one we haven't looked at */ + for (; i < n; i++) { - r1->x0 = MIN(r1->x0, r.x0); - r1->y0 = MIN(r1->y0, r.y0); - r1->x1 = MAX(r1->x1, r.x1); - r1->y1 = MAX(r1->y1, r.y1); + if (po->object[j] != po->object[i]) + po->object[++j] = po->object[i]; } + po->len = j+1; } -static int detect_super_script(fz_stext_line *line, fz_stext_char *ch) +static void +page_objects_list_sort_and_dedupe(fz_context *ctx, page_objects_list *pol) { - if (line->wmode == 0 && line->dir.x == 1 && line->dir.y == 0) - return ch->origin.y < line->first_char->origin.y - ch->size * 0.1f; - return 0; -} + int i; + int n = pol->len; -static const char *font_full_name(fz_context *ctx, fz_font *font) -{ - const char *name = fz_font_name(ctx, font); - const char *s = strchr(name, '+'); - return s ? s + 1 : name; + for (i = 0; i < n; i++) + { + page_objects_sort(ctx, pol->page[i]); + page_objects_dedupe(ctx, pol->page[i]); + } } -static void font_family_name(fz_context *ctx, fz_font *font, char *buf, int size) -{ - const char *name = font_full_name(ctx, font); - fz_strlcpy(buf, name, size); -} +/* + * Garbage collect objects not reachable from the trailer. + */ -PyObject * -JM_style_begin_dict(fz_context *ctx, fz_font *font, float size, int sup) +/* Mark a reference. If it's been marked already, return NULL (as no further + * processing is required). If it's not, return the resolved object so + * that we can continue our recursive marking. If it's a duff reference + * return the fact so that we can remove the reference at source. + */ +static pdf_obj *markref(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj, int *duff) { - //JM_TRACE("entering JM_style_begin_dict"); - char family[80]; - font_family_name(ctx, font, family, sizeof family); - int flags = sup; - flags += fz_font_is_italic(ctx, font) * 2; - flags += fz_font_is_serif(ctx, font) * 4; - flags += fz_font_is_monospaced(ctx, font) * 8; - flags += fz_font_is_bold(ctx, font) * 16; - PyObject *span = PyDict_New(); - PyDict_SetItemString(span, "font", JM_UnicodeFromASCII(family)); - PyDict_SetItemString(span, "size", Py_BuildValue("f", size)); - PyDict_SetItemString(span, "flags", Py_BuildValue("i", flags)); - //JM_TRACE("leaving JM_style_begin_dict"); - return span; -} + int num = pdf_to_num(ctx, obj); -void -JM_style_end_dict(fz_context *ctx, fz_buffer *buff, PyObject *span, PyObject *spanlist) -{ - if (buff) - PyDict_SetItemString(span, "text", JM_StrFromBuffer(ctx, buff)); - PyList_Append(spanlist, span); -} + if (num <= 0 || num >= pdf_xref_len(ctx, doc)) + { + *duff = 1; + return NULL; + } + *duff = 0; + if (opts->use_list[num]) + return NULL; -PyObject * -JM_extract_stext_textchar_as_dict(fz_context *ctx, fz_stext_char *ch) -{ - //JM_TRACE("entering JM_extract_stext_textchar_as_dict"); - char data[10]; - Py_ssize_t len = (Py_ssize_t) fz_runetochar(data, ch->c); - PyObject *chardict = PyDict_New(); - PyDict_SetItemString(chardict, "c", PyUnicode_FromStringAndSize(data, len)); - PyDict_SetItemString(chardict, "origin", Py_BuildValue("ff", ch->origin.x, ch->origin.y)); - PyDict_SetItemString(chardict, "bbox", Py_BuildValue("ffff", - ch->bbox.x0, ch->bbox.y0, - ch->bbox.x1, ch->bbox.y1)); - //JM_TRACE("leaving JM_extract_stext_textchar_as_dict"); - return chardict; -} + opts->use_list[num] = 1; -PyObject * -JM_extract_stext_textblock_as_dict(fz_context *ctx, fz_stext_block *block, int rawdict) -{ - //JM_TRACE("enter: JM_extract_stext_textblock_as_dict"); - fz_stext_line *line; - fz_stext_char *ch; - fz_font *font = NULL; - fz_buffer *buff = NULL; - float size = 0; - int sup = 0; - PyObject *span = NULL, *spanlist = NULL, *linelist = NULL, *linedict = NULL; - PyObject *charlist = NULL, *chardict = NULL; - linelist = PyList_New(0); - PyObject *dict = PyDict_New(); - fz_rect *blockrect = JM_empty_rect(); - PyDict_SetItemString(dict, "type", PyInt_FromLong(FZ_STEXT_BLOCK_TEXT)); - //JM_TRACE("before line loop"); - for (line = block->u.t.first_line; line; line = line->next) + /* Bake in /Length in stream objects */ + fz_try(ctx) { - linedict = PyDict_New(); - fz_rect *linerect = JM_empty_rect(); - PyDict_SetItemString(linedict, "wmode", Py_BuildValue("i", line->wmode)); - PyDict_SetItemString(linedict, "dir", Py_BuildValue("ff", line->dir.x, line->dir.y)); - spanlist = PyList_New(0); - font = NULL; - buff = NULL; - size = 0; - //JM_TRACE("before character loop"); - for (ch = line->first_char; ch; ch = ch->next) - { - JM_join_rect(linerect, &ch->bbox, ch->size); - //JM_TRACE("joined char bbox to linerect"); - int ch_sup = detect_super_script(line, ch); - if (ch->font != font || ch->size != size) - { // start new span - //JM_TRACE("starting new span"); - if (font) // must finish old span first - { - //JM_TRACE("finishing old span"); - if (rawdict) - { - PyDict_SetItemString(span, "chars", charlist); - Py_CLEAR(charlist); - } - JM_style_end_dict(ctx, buff, span, spanlist); - Py_CLEAR(span); - fz_drop_buffer(ctx, buff); - buff = NULL; - font = NULL; - //JM_TRACE("finished old span"); - } - font = ch->font; - size = ch->size; - sup = ch_sup; - span = JM_style_begin_dict(ctx, font, size, sup); - if (rawdict) - charlist = PyList_New(0); - else - buff = fz_new_buffer(ctx, 64); - //JM_TRACE("new span started"); - } - if (!rawdict) - fz_append_rune(ctx, buff, ch->c); - else - { - chardict = JM_extract_stext_textchar_as_dict(ctx, ch); - PyList_Append(charlist, chardict); - Py_CLEAR(chardict); - } - //JM_TRACE("finished one char"); - } - if (font) + if (pdf_obj_num_is_stream(ctx, doc, num)) { - //JM_TRACE("start output last span"); - if (rawdict) + pdf_obj *len = pdf_dict_get(ctx, obj, PDF_NAME(Length)); + if (pdf_is_indirect(ctx, len)) { - PyDict_SetItemString(span, "chars", charlist); - Py_CLEAR(charlist); + opts->use_list[pdf_to_num(ctx, len)] = 0; + len = pdf_resolve_indirect(ctx, len); + pdf_dict_put(ctx, obj, PDF_NAME(Length), len); } - JM_style_end_dict(ctx, buff, span, spanlist); - Py_CLEAR(span); - fz_drop_buffer(ctx, buff); - buff = NULL; - font = NULL; } - //JM_TRACE("finishing line"); - PyDict_SetItemString(linedict, "spans", spanlist); - Py_CLEAR(spanlist); - PyDict_SetItemString(linedict, "bbox", Py_BuildValue("ffff", - linerect->x0, linerect->y0, - linerect->x1, linerect->y1)); + } + fz_catch(ctx) + { + fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); + /* Leave broken */ + } - JM_join_rect(blockrect, linerect, 0.0f); + obj = pdf_resolve_indirect(ctx, obj); + if (obj == NULL || pdf_is_null(ctx, obj)) + { + *duff = 1; + opts->use_list[num] = 0; + } - JM_Free(linerect); - PyList_Append(linelist, linedict); - Py_CLEAR(linedict); + return obj; +} + +#ifdef DEBUG_MARK_AND_SWEEP +static int depth = 0; + +static +void indent() +{ + while (depth > 0) + { + int d = depth; + if (d > 16) + d = 16; + printf("%s", &" "[16-d]); + depth -= d; } - //JM_TRACE("after line loop"); - PyDict_SetItemString(dict, "lines", linelist); - Py_CLEAR(linelist); - PyDict_SetItemString(dict, "bbox", Py_BuildValue("ffff", - blockrect->x0, blockrect->y0, - blockrect->x1, blockrect->y1)); - JM_Free(blockrect); - //JM_TRACE("leaving JM_extract_stext_textblock_as_dict"); - return dict; } +#define DEBUGGING_MARKING(A) do { A; } while (0) +#else +#define DEBUGGING_MARKING(A) do { } while (0) +#endif -PyObject * -JM_extract_stext_imageblock_as_dict(fz_context *ctx, fz_stext_block *block) +/* Recursively mark an object. If any references found are duff, then + * replace them with nulls. */ +static int markobj(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj) { - fz_image *image = block->u.i.image; - fz_buffer *buf = NULL, *freebuf = NULL; - fz_var(buf); - fz_var(freebuf); - fz_compressed_buffer *buffer = NULL; - int n = fz_colorspace_n(ctx, image->colorspace); - int w = image->w; - int h = image->h; - int type = 0; - unsigned char ext[5]; - PyObject *bytes = JM_BinFromChar(""); - fz_var(bytes); - buffer = fz_compressed_image_buffer(ctx, image); - if (buffer) type = buffer->params.type; - PyObject *dict = PyDict_New(); - PyDict_SetItemString(dict, "type", PyInt_FromLong(FZ_STEXT_BLOCK_IMAGE)); - PyDict_SetItemString(dict, "bbox", Py_BuildValue("[ffff]", - block->bbox.x0, block->bbox.y0, - block->bbox.x1, block->bbox.y1)); - PyDict_SetItemString(dict, "width", PyInt_FromLong((long) w)); - PyDict_SetItemString(dict, "height", PyInt_FromLong((long) h)); - fz_try(ctx) + int i; + + DEBUGGING_MARKING(depth++); + + while (pdf_is_indirect(ctx, obj)) { - if (image->use_colorkey) type = FZ_IMAGE_UNKNOWN; - if (image->use_decode) type = FZ_IMAGE_UNKNOWN; - if (image->mask) type = FZ_IMAGE_UNKNOWN; - if (type < FZ_IMAGE_BMP) type = FZ_IMAGE_UNKNOWN; - if (n != 1 && n != 3 && type == FZ_IMAGE_JPEG) - type = FZ_IMAGE_UNKNOWN; - if (type != FZ_IMAGE_UNKNOWN) + int duff; + DEBUGGING_MARKING(indent(); printf("Marking object %d\n", pdf_to_num(ctx, obj))); + obj = markref(ctx, doc, opts, obj, &duff); + if (duff) { - buf = buffer->buffer; - switch(type) - { - case(FZ_IMAGE_BMP): strcpy(ext, "bmp"); break; - case(FZ_IMAGE_GIF): strcpy(ext, "gif"); break; - case(FZ_IMAGE_JPEG): strcpy(ext, "jpeg"); break; - case(FZ_IMAGE_JPX): strcpy(ext, "jpx"); break; - case(FZ_IMAGE_JXR): strcpy(ext, "jxr"); break; - case(FZ_IMAGE_PNM): strcpy(ext, "pnm"); break; - case(FZ_IMAGE_TIFF): strcpy(ext, "tiff"); break; - default: strcpy(ext, "png"); break; - } + DEBUGGING_MARKING(depth--); + return 1; } - else + } + + if (pdf_is_dict(ctx, obj)) + { + int n = pdf_dict_len(ctx, obj); + for (i = 0; i < n; i++) { - buf = freebuf = fz_new_buffer_from_image_as_png(ctx, image, NULL); - strcpy(ext, "png"); + DEBUGGING_MARKING(indent(); printf("DICT[%d/%d] = %s\n", i, n, pdf_to_name(ctx, pdf_dict_get_key(ctx, obj, i)))); + if (markobj(ctx, doc, opts, pdf_dict_get_val(ctx, obj, i))) + pdf_dict_put_val_null(ctx, obj, i); } - bytes = JM_BinFromBuffer(ctx, buf); } - fz_always(ctx) + + else if (pdf_is_array(ctx, obj)) { - fz_drop_buffer(ctx, freebuf); - PyDict_SetItemString(dict, "ext", PyString_FromString(ext)); - PyDict_SetItemString(dict, "image", bytes); - Py_CLEAR(bytes); + int n = pdf_array_len(ctx, obj); + for (i = 0; i < n; i++) + { + DEBUGGING_MARKING(indent(); printf("ARRAY[%d/%d]\n", i, n)); + if (markobj(ctx, doc, opts, pdf_array_get(ctx, obj, i))) + pdf_array_put(ctx, obj, i, PDF_NULL); + } } - fz_catch(ctx) {;} - return dict; + + DEBUGGING_MARKING(depth--); + + return 0; } -PyObject * -JM_stext_page_as_dict(fz_context *ctx, fz_stext_page *page, int rawdict) +static void +expand_lists(fz_context *ctx, pdf_write_state *opts, int num) { - PyObject *dict = PyDict_New(); - PyObject *blocklist = PyList_New(0); - fz_stext_block *block; - float w = page->mediabox.x1 - page->mediabox.x0; - float h = page->mediabox.y1 - page->mediabox.y0; - PyDict_SetItemString(dict, "width", Py_BuildValue("f", w)); - PyDict_SetItemString(dict, "height", Py_BuildValue("f", h)); - for (block = page->first_block; block; block = block->next) + int i; + + /* objects are numbered 0..num and maybe two additional objects for linearization */ + num += 3; + opts->use_list = fz_resize_array(ctx, opts->use_list, num, sizeof(*opts->use_list)); + opts->ofs_list = fz_resize_array(ctx, opts->ofs_list, num, sizeof(*opts->ofs_list)); + opts->gen_list = fz_resize_array(ctx, opts->gen_list, num, sizeof(*opts->gen_list)); + opts->renumber_map = fz_resize_array(ctx, opts->renumber_map, num, sizeof(*opts->renumber_map)); + opts->rev_renumber_map = fz_resize_array(ctx, opts->rev_renumber_map, num, sizeof(*opts->rev_renumber_map)); + + for (i = opts->list_len; i < num; i++) { - if (block->type == FZ_STEXT_BLOCK_IMAGE) - PyList_Append(blocklist, JM_extract_stext_imageblock_as_dict(ctx, block)); - else - PyList_Append(blocklist, JM_extract_stext_textblock_as_dict(ctx, block, rawdict)); + opts->use_list[i] = 0; + opts->ofs_list[i] = 0; + opts->gen_list[i] = 0; + opts->renumber_map[i] = i; + opts->rev_renumber_map[i] = i; } - PyDict_SetItemString(dict, "blocks", blocklist); - Py_CLEAR(blocklist); - return dict; + opts->list_len = num; } +/* + * Scan for and remove duplicate objects (slow) + */ -//----------------------------------------------------------------------------- -// Functions dealing with PDF form fields -//----------------------------------------------------------------------------- +static void removeduplicateobjs(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + int num, other, max_num; + int xref_len = pdf_xref_len(ctx, doc); -// PushButton get state -//----------------------------------------------------------------------------- -PyObject *JM_pushbtn_state(fz_context *ctx, pdf_annot *annot) -{ // pushed buttons do not reflect status changes in the PDF - // always reflect them as untouched - Py_RETURN_FALSE; -} + for (num = 1; num < xref_len; num++) + { + /* Only compare an object to objects preceding it */ + for (other = 1; other < num; other++) + { + pdf_obj *a, *b; + int newnum, streama = 0, streamb = 0, differ = 0; -// CheckBox get state -//----------------------------------------------------------------------------- -PyObject *JM_checkbox_state(fz_context *ctx, pdf_annot *annot) -{ - pdf_document *pdf = pdf_get_bound_document(ctx, annot->obj); - pdf_obj *leafv = pdf_get_inheritable(ctx, pdf, annot->obj, PDF_NAME_V); - pdf_obj *leafas = pdf_get_inheritable(ctx, pdf, annot->obj, PDF_NAME_AS); - if (!leafv) Py_RETURN_FALSE; - if (leafv == PDF_NAME_Off) Py_RETURN_FALSE; - if (leafv == pdf_new_name(ctx, NULL, "Yes")) - Py_RETURN_TRUE; - if (pdf_is_string(ctx, leafv) && !strcmp(pdf_to_str_buf(ctx, leafv), "Off")) - Py_RETURN_FALSE; - if (pdf_is_string(ctx, leafv) && !strcmp(pdf_to_str_buf(ctx, leafv), "Yes")) - Py_RETURN_TRUE; - if (leafas && leafas == PDF_NAME_Off) Py_RETURN_FALSE; - Py_RETURN_TRUE; -} + if (num == other || !opts->use_list[num] || !opts->use_list[other]) + continue; -// RadioBox get state -//----------------------------------------------------------------------------- -PyObject *JM_radiobtn_state(fz_context *ctx, pdf_annot *annot) -{ // MuPDF treats radio buttons like check boxes - hence so do we - return JM_checkbox_state(ctx, annot); -} + /* TODO: resolve indirect references to see if we can omit them */ -// Text field retrieve value -//----------------------------------------------------------------------------- -PyObject *JM_text_value(fz_context *ctx, pdf_annot *annot) -{ - char *text = NULL; - pdf_document *pdf = pdf_get_bound_document(ctx, annot->obj); - fz_var(text); - fz_try(ctx) - text = pdf_field_value(ctx, pdf, annot->obj); - fz_catch(ctx) return NONE; - return Py_BuildValue("s", text); -} + /* + * Comparing stream objects data contents would take too long. + * + * pdf_obj_num_is_stream calls pdf_cache_object and ensures + * that the xref table has the objects loaded. + */ + fz_try(ctx) + { + streama = pdf_obj_num_is_stream(ctx, doc, num); + streamb = pdf_obj_num_is_stream(ctx, doc, other); + differ = streama || streamb; + if (streama && streamb && opts->do_garbage >= 4) + differ = 0; + } + fz_catch(ctx) + { + /* Assume different */ + differ = 1; + } + if (differ) + continue; -// ListBox retrieve value -//----------------------------------------------------------------------------- -PyObject *JM_listbox_value(fz_context *ctx, pdf_annot *annot) -{ - int i = 0, n = 0; - // may be single value or array - pdf_obj *optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME_V); - if (pdf_is_string(ctx, optarr)) // a single string - return PyString_FromString(pdf_to_utf8(ctx, optarr)); + a = pdf_get_xref_entry(ctx, doc, num)->obj; + b = pdf_get_xref_entry(ctx, doc, other)->obj; - // value is an array (may have len 0) - n = pdf_array_len(ctx, optarr); - PyObject *liste = PyList_New(0); + if (pdf_objcmp(ctx, a, b)) + continue; - // extract a list of strings - // each entry may again be an array: take second entry then - for (i = 0; i < n; i++) - { - pdf_obj *elem = pdf_array_get(ctx, optarr, i); - if (pdf_is_array(ctx, elem)) - elem = pdf_array_get(ctx, elem, 1); - PyList_Append(liste, PyString_FromString(pdf_to_utf8(ctx, elem))); + if (streama && streamb) + { + /* Check to see if streams match too. */ + fz_buffer *sa = NULL; + fz_buffer *sb = NULL; + + fz_var(sa); + fz_var(sb); + + differ = 1; + fz_try(ctx) + { + unsigned char *dataa, *datab; + size_t lena, lenb; + sa = pdf_load_raw_stream_number(ctx, doc, num); + sb = pdf_load_raw_stream_number(ctx, doc, other); + lena = fz_buffer_storage(ctx, sa, &dataa); + lenb = fz_buffer_storage(ctx, sb, &datab); + if (lena == lenb && memcmp(dataa, datab, lena) == 0) + differ = 0; + } + fz_always(ctx) + { + fz_drop_buffer(ctx, sa); + fz_drop_buffer(ctx, sb); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + if (differ) + continue; + } + + /* Keep the lowest numbered object */ + newnum = fz_mini(num, other); + max_num = fz_maxi(num, other); + if (max_num >= opts->list_len) + expand_lists(ctx, opts, max_num); + opts->renumber_map[num] = newnum; + opts->renumber_map[other] = newnum; + opts->rev_renumber_map[newnum] = num; /* Either will do */ + opts->use_list[fz_maxi(num, other)] = 0; + + /* One duplicate was found, do not look for another */ + break; + } } - return liste; } -// ComboBox retrieve value -//----------------------------------------------------------------------------- -PyObject *JM_combobox_value(fz_context *ctx, pdf_annot *annot) -{ // combobox values are treated like listbox values - return JM_listbox_value(ctx, annot); -} +/* + * Renumber objects sequentially so the xref is more compact + * + * This code assumes that any opts->renumber_map[n] <= n for all n. + */ -// Signature field retrieve value -PyObject *JM_signature_value(fz_context *ctx, pdf_annot *annot) -{ // signatures are currently not supported - return NONE; -} +static void compactxref(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + int num, newnum; + int xref_len = pdf_xref_len(ctx, doc); -// retrieve ListBox / ComboBox choice values -//----------------------------------------------------------------------------- -PyObject *JM_choice_options(fz_context *ctx, pdf_annot *annot) -{ // return list of choices for list or combo boxes - pdf_document *pdf = pdf_get_bound_document(ctx, annot->obj); - int n = pdf_choice_widget_options(ctx, pdf, (pdf_widget *) annot, 0, NULL); - if (n == 0) return NONE; // wrong widget type + /* + * Update renumber_map in-place, clustering all used + * objects together at low object ids. Objects that + * already should be renumbered will have their new + * object ids be updated to reflect the compaction. + */ - pdf_obj *optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME_Opt); - int i, m; - PyObject *liste = PyList_New(0); + if (xref_len > opts->list_len) + expand_lists(ctx, opts, xref_len-1); - for (i = 0; i < n; i++) + newnum = 1; + for (num = 1; num < xref_len; num++) { - m = pdf_array_len(ctx, pdf_array_get(ctx, optarr, i)); - if (m == 2) + /* If it's not used, map it to zero */ + if (!opts->use_list[opts->renumber_map[num]]) { - PyList_Append(liste, Py_BuildValue("ss", - pdf_to_utf8(ctx, pdf_array_get(ctx, pdf_array_get(ctx, optarr, i), 0)), - pdf_to_utf8(ctx, pdf_array_get(ctx, pdf_array_get(ctx, optarr, i), 1)))); + opts->renumber_map[num] = 0; + } + /* If it's not moved, compact it. */ + else if (opts->renumber_map[num] == num) + { + opts->rev_renumber_map[newnum] = opts->rev_renumber_map[num]; + opts->renumber_map[num] = newnum++; } + /* Otherwise it's used, and moved. We know that it must have + * moved down, so the place it's moved to will be in the right + * place already. */ else { - PyList_Append(liste, PyString_FromString(pdf_to_utf8(ctx, pdf_array_get(ctx, optarr, i)))); + opts->renumber_map[num] = opts->renumber_map[opts->renumber_map[num]]; } } - return liste; } -// set ListBox / ComboBox values -//----------------------------------------------------------------------------- -void JM_set_choice_options(fz_context *ctx, pdf_annot *annot, PyObject *liste) +/* + * Update indirect objects according to renumbering established when + * removing duplicate objects and compacting the xref. + */ + +static void renumberobj(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj) { - pdf_document *pdf = pdf_get_bound_document(ctx, annot->obj); - Py_ssize_t i, n = PySequence_Size(liste); - char *opt = NULL; - pdf_obj *optarr = pdf_new_array(ctx, pdf, n); - for (i = 0; i < n; i++) + int i; + int xref_len = pdf_xref_len(ctx, doc); + + if (pdf_is_dict(ctx, obj)) { - opt = JM_Python_str_AsChar(PySequence_GetItem(liste, i)); - pdf_array_push_text_string(ctx, optarr, (const char *) opt); - JM_Python_str_DelForPy3(opt); + int n = pdf_dict_len(ctx, obj); + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, obj, i); + pdf_obj *val = pdf_dict_get_val(ctx, obj, i); + if (pdf_is_indirect(ctx, val)) + { + int o = pdf_to_num(ctx, val); + if (o >= xref_len || o <= 0 || opts->renumber_map[o] == 0) + val = PDF_NULL; + else + val = pdf_new_indirect(ctx, doc, opts->renumber_map[o], 0); + pdf_dict_put_drop(ctx, obj, key, val); + } + else + { + renumberobj(ctx, doc, opts, val); + } + } } - pdf_dict_put(ctx, annot->obj, PDF_NAME_Opt, optarr); - - return; + else if (pdf_is_array(ctx, obj)) + { + int n = pdf_array_len(ctx, obj); + for (i = 0; i < n; i++) + { + pdf_obj *val = pdf_array_get(ctx, obj, i); + if (pdf_is_indirect(ctx, val)) + { + int o = pdf_to_num(ctx, val); + if (o >= xref_len || o <= 0 || opts->renumber_map[o] == 0) + val = PDF_NULL; + else + val = pdf_new_indirect(ctx, doc, opts->renumber_map[o], 0); + pdf_array_put_drop(ctx, obj, i, val); + } + else + { + renumberobj(ctx, doc, opts, val); + } + } + } } -//----------------------------------------------------------------------------- -// Called by "Page.addWidget" and "Annot.updateWidget". -// Set all properties of a new or updated widget, whether changed or not. -// Should be no performance issue, because verifying a change before updating -// is costly as well (and a code bloat). No problem on the PDF side, because -// any change will always lead to the complete new PDF object being stored. -//----------------------------------------------------------------------------- -void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widget, int field_type) +static void renumberobjs(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) { - pdf_document *pdf = annot->page->doc; - pdf_page *page = annot->page; - fz_rect rect = {0,0,0,0}; - pdf_obj *fill_col = NULL, *text_col = NULL, *border_col = NULL; - pdf_obj *dashes = NULL; - Py_ssize_t i, n = 0; - PyObject *value; + pdf_xref_entry *newxref = NULL; + int newlen; + int num; + int *new_use_list; + int xref_len = pdf_xref_len(ctx, doc); - // ensure a font resources dict /DR --- ----------------------------------- - pdf_obj *dr = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), - PDF_NAME_Root, PDF_NAME_AcroForm, PDF_NAME_DR, NULL); - // new /DR using the object prepared in xref - if (!dr) + new_use_list = fz_calloc(ctx, pdf_xref_len(ctx, doc)+3, sizeof(int)); + + fz_var(newxref); + fz_try(ctx) { - pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), - PDF_NAME_Root, PDF_NAME_AcroForm, NULL); - int xref = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, - "_dr_xref")); - pdf_obj *f = pdf_new_indirect(ctx, pdf, xref, 0); - dr = pdf_new_dict(ctx, pdf, 1); - pdf_dict_put(ctx, dr, PDF_NAME_Font, f); - pdf_dict_put_drop(ctx, form, PDF_NAME_DR, dr); - JM_PyErr_Clear; - } + /* Apply renumber map to indirect references in all objects in xref */ + renumberobj(ctx, doc, opts, pdf_trailer(ctx, doc)); + for (num = 0; num < xref_len; num++) + { + pdf_obj *obj; + int to = opts->renumber_map[num]; - // rectangle -------------------------------------------------------------- - value = PyObject_GetAttrString(Widget, "rect"); - rect.x0 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 0)); - rect.y0 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 1)); - rect.x1 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 2)); - rect.y1 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 3)); - Py_CLEAR(value); - JM_PyErr_Clear; - pdf_set_annot_rect(ctx, annot, &rect); // set the rect + /* If object is going to be dropped, don't bother renumbering */ + if (to == 0) + continue; - // fill color ------------------------------------------------------------- - value = PyObject_GetAttrString(Widget, "fill_color"); - if (value && PySequence_Check(value)) - { - n = PySequence_Size(value); - fill_col = pdf_new_array(ctx, pdf, n); - for (i = 0; i < n; i++) - pdf_array_push_real(ctx, fill_col, - PyFloat_AsDouble(PySequence_GetItem(value, i))); - pdf_field_set_fill_color(ctx, pdf, annot->obj, fill_col); - pdf_drop_obj(ctx, fill_col); - } - Py_CLEAR(value); - JM_PyErr_Clear; + obj = pdf_get_xref_entry(ctx, doc, num)->obj; - // dashes ----------------------------------------------------------------- - value = PyObject_GetAttrString(Widget, "border_dashes"); - if (value && PySequence_Check(value)) + if (pdf_is_indirect(ctx, obj)) + { + obj = pdf_new_indirect(ctx, doc, to, 0); + fz_try(ctx) + pdf_update_object(ctx, doc, num, obj); + fz_always(ctx) + pdf_drop_obj(ctx, obj); + fz_catch(ctx) + fz_rethrow(ctx); + } + else + { + renumberobj(ctx, doc, opts, obj); + } + } + + /* Create new table for the reordered, compacted xref */ + newxref = fz_malloc_array(ctx, xref_len + 3, sizeof(pdf_xref_entry)); + newxref[0] = *pdf_get_xref_entry(ctx, doc, 0); + + /* Move used objects into the new compacted xref */ + newlen = 0; + for (num = 1; num < xref_len; num++) + { + if (opts->use_list[num]) + { + pdf_xref_entry *e; + if (newlen < opts->renumber_map[num]) + newlen = opts->renumber_map[num]; + e = pdf_get_xref_entry(ctx, doc, num); + newxref[opts->renumber_map[num]] = *e; + if (e->obj) + { + pdf_set_obj_parent(ctx, e->obj, opts->renumber_map[num]); + e->obj = NULL; + } + new_use_list[opts->renumber_map[num]] = opts->use_list[num]; + } + else + { + pdf_xref_entry *e = pdf_get_xref_entry(ctx, doc, num); + pdf_drop_obj(ctx, e->obj); + e->obj = NULL; + fz_drop_buffer(ctx, e->stm_buf); + e->stm_buf = NULL; + } + } + + pdf_replace_xref(ctx, doc, newxref, newlen + 1); + newxref = NULL; + } + fz_catch(ctx) { - n = PySequence_Size(value); - dashes = pdf_new_array(ctx, pdf, n); - for (i = 0; i < n; i++) - pdf_array_push_int(ctx, dashes, - PyInt_AsLong(PySequence_GetItem(value, i))); - pdf_dict_putl_drop(ctx, annot->obj, dashes, PDF_NAME_BS, - PDF_NAME_D, NULL); + fz_free(ctx, newxref); + fz_free(ctx, new_use_list); + fz_rethrow(ctx); } - Py_CLEAR(value); - JM_PyErr_Clear; + fz_free(ctx, opts->use_list); + opts->use_list = new_use_list; - // border color ----------------------------------------------------------- - value = PyObject_GetAttrString(Widget, "border_color"); - if (value && PySequence_Check(value)) + for (num = 1; num < xref_len; num++) { - n = PySequence_Size(value); - border_col = pdf_new_array(ctx, pdf, n); - for (i = 0; i < n; i++) - pdf_array_push_real(ctx, border_col, - PyFloat_AsDouble(PySequence_GetItem(value, i))); - pdf_dict_putl_drop(ctx, annot->obj, border_col, PDF_NAME_MK, - PDF_NAME_BC, NULL); + opts->renumber_map[num] = num; } - Py_CLEAR(value); - JM_PyErr_Clear; +} - // entry ignored - may be later use - /* - int text_type = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, - "text_type")); - JM_PyErr_Clear; - */ +static void page_objects_list_renumber(pdf_write_state *opts) +{ + int i, j; - // max text len ----------------------------------------------------------- - if (field_type == PDF_WIDGET_TYPE_TEXT) + for (i = 0; i < opts->page_object_lists->len; i++) { - int text_maxlen = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, - "text_maxlen")); - if (text_maxlen) - pdf_dict_put_int(ctx, annot->obj, PDF_NAME_MaxLen, text_maxlen); - JM_PyErr_Clear; + page_objects *po = opts->page_object_lists->page[i]; + for (j = 0; j < po->len; j++) + { + po->object[j] = opts->renumber_map[po->object[j]]; + } + po->page_object_number = opts->renumber_map[po->page_object_number]; } - - // choice values ---------------------------------------------------------- - if (field_type == PDF_WIDGET_TYPE_LISTBOX || - field_type == PDF_WIDGET_TYPE_COMBOBOX) +} + +static void +mark_all(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *val, int flag, int page) +{ + if (pdf_mark_obj(ctx, val)) + return; + + fz_try(ctx) { - value = PyObject_GetAttrString(Widget, "choice_values"); - JM_set_choice_options(ctx, annot, value); - Py_CLEAR(value); - } - JM_PyErr_Clear; + if (pdf_is_indirect(ctx, val)) + { + int num = pdf_to_num(ctx, val); + if (opts->use_list[num] & USE_PAGE_MASK) + /* Already used */ + opts->use_list[num] |= USE_SHARED; + else + opts->use_list[num] |= flag; + if (page >= 0) + page_objects_list_insert(ctx, opts, page, num); + } - // border style ----------------------------------------------------------- - pdf_obj *val = JM_get_border_style(ctx, - PyObject_GetAttrString(Widget, "border_style")); - pdf_dict_putl_drop(ctx, annot->obj, val, PDF_NAME_BS, PDF_NAME_S, NULL); + if (pdf_is_dict(ctx, val)) + { + int i, n = pdf_dict_len(ctx, val); - // border width ----------------------------------------------------------- - float border_width = (float) PyFloat_AsDouble(PyObject_GetAttrString(Widget, - "border_width")); - pdf_dict_putl_drop(ctx, annot->obj, pdf_new_real(ctx, pdf, - border_width), PDF_NAME_BS, PDF_NAME_W, NULL); - JM_PyErr_Clear; + for (i = 0; i < n; i++) + { + mark_all(ctx, doc, opts, pdf_dict_get_val(ctx, val, i), flag, page); + } + } + else if (pdf_is_array(ctx, val)) + { + int i, n = pdf_array_len(ctx, val); - // /DA string ------------------------------------------------------------- - char *da = JM_Python_str_AsChar(PyObject_GetAttrString(Widget, "_text_da")); - if (da) + for (i = 0; i < n; i++) + { + mark_all(ctx, doc, opts, pdf_array_get(ctx, val, i), flag, page); + } + } + } + fz_always(ctx) { - pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME_DA, da); - JM_Python_str_DelForPy3(da); - pdf_dict_dels(ctx, annot->obj, "DS"); // unsupported - pdf_dict_dels(ctx, annot->obj, "RC"); // unsupported + pdf_unmark_obj(ctx, val); } - JM_PyErr_Clear; - - // field flags ------------------------------------------------------------ - int field_flags = 0, Ff = 0; - if (field_type != PDF_WIDGET_TYPE_CHECKBOX) + fz_catch(ctx) { - field_flags = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, - "field_flags")); - if (!PyErr_Occurred()) - { - Ff = pdf_get_field_flags(ctx, pdf, annot->obj); - Ff |= field_flags; - } - JM_PyErr_Clear; + fz_rethrow(ctx); } - pdf_dict_put_int(ctx, annot->obj, PDF_NAME_Ff, Ff); +} - // button caption --------------------------------------------------------- - if (field_type == PDF_WIDGET_TYPE_RADIOBUTTON || - field_type == PDF_WIDGET_TYPE_PUSHBUTTON || - field_type == PDF_WIDGET_TYPE_CHECKBOX) +static int +mark_pages(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *val, int pagenum) +{ + if (pdf_mark_obj(ctx, val)) + return pagenum; + + fz_try(ctx) { - char *ca = JM_Python_str_AsChar(PyObject_GetAttrString(Widget, - "button_caption")); - if (ca) + if (pdf_is_dict(ctx, val)) { - pdf_dict_putl(ctx, annot->obj, pdf_new_text_string(ctx, NULL, ca), - PDF_NAME_MK, PDF_NAME_CA, NULL); - JM_Python_str_DelForPy3(ca); + if (pdf_name_eq(ctx, PDF_NAME(Page), pdf_dict_get(ctx, val, PDF_NAME(Type)))) + { + int num = pdf_to_num(ctx, val); + pdf_unmark_obj(ctx, val); + mark_all(ctx, doc, opts, val, pagenum == 0 ? USE_PAGE1 : (pagenum<use_list[num] |= USE_PAGE_OBJECT; + } + else + { + int i, n = pdf_dict_len(ctx, val); + + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, val, i); + pdf_obj *obj = pdf_dict_get_val(ctx, val, i); + + if (pdf_name_eq(ctx, PDF_NAME(Kids), key)) + pagenum = mark_pages(ctx, doc, opts, obj, pagenum); + else + mark_all(ctx, doc, opts, obj, USE_CATALOGUE, -1); + } + + if (pdf_is_indirect(ctx, val)) + { + int num = pdf_to_num(ctx, val); + opts->use_list[num] |= USE_CATALOGUE; + } + } } - JM_PyErr_Clear; + else if (pdf_is_array(ctx, val)) + { + int i, n = pdf_array_len(ctx, val); + + for (i = 0; i < n; i++) + { + pagenum = mark_pages(ctx, doc, opts, pdf_array_get(ctx, val, i), pagenum); + } + if (pdf_is_indirect(ctx, val)) + { + int num = pdf_to_num(ctx, val); + opts->use_list[num] |= USE_CATALOGUE; + } + } + } + fz_always(ctx) + { + pdf_unmark_obj(ctx, val); + } + fz_catch(ctx) + { + fz_rethrow(ctx); } + return pagenum; +} - // field value ------------------------------------------------------------ - // MuPDF function "pdf_field_set_value" always sets strings. For button - // fields this may lead to an unrecognized state for some PDF viewers. - //------------------------------------------------------------------------- - value = PyObject_GetAttrString(Widget, "field_value"); - int result = 0; - char *text = NULL; - switch(field_type) +static void +mark_root(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *dict) +{ + int i, n = pdf_dict_len(ctx, dict); + + if (pdf_mark_obj(ctx, dict)) + return; + + fz_try(ctx) { - case PDF_WIDGET_TYPE_CHECKBOX: - case PDF_WIDGET_TYPE_RADIOBUTTON: - if (PyObject_RichCompareBool(value, Py_True, Py_EQ)) - { - result = pdf_field_set_value(ctx, pdf, annot->obj, "Yes"); - pdf_dict_put_name(ctx, annot->obj, PDF_NAME_V, "Yes"); - } - else + if (pdf_is_indirect(ctx, dict)) { - result = pdf_field_set_value(ctx, pdf, annot->obj, "Off"); - pdf_dict_put(ctx, annot->obj, PDF_NAME_V, PDF_NAME_Off); + int num = pdf_to_num(ctx, dict); + opts->use_list[num] |= USE_CATALOGUE; } - break; - default: - text = JM_Python_str_AsChar(value); - if (text) + + for (i = 0; i < n; i++) { - result = pdf_field_set_value(ctx, pdf, annot->obj, (const char *)text); - JM_Python_str_DelForPy3(text); + pdf_obj *key = pdf_dict_get_key(ctx, dict, i); + pdf_obj *val = pdf_dict_get_val(ctx, dict, i); + + if (pdf_name_eq(ctx, PDF_NAME(Pages), key)) + opts->page_count = mark_pages(ctx, doc, opts, val, 0); + else if (pdf_name_eq(ctx, PDF_NAME(Names), key)) + mark_all(ctx, doc, opts, val, USE_OTHER_OBJECTS, -1); + else if (pdf_name_eq(ctx, PDF_NAME(Dests), key)) + mark_all(ctx, doc, opts, val, USE_OTHER_OBJECTS, -1); + else if (pdf_name_eq(ctx, PDF_NAME(Outlines), key)) + { + int section; + /* Look at PageMode to decide whether to + * USE_OTHER_OBJECTS or USE_PAGE1 here. */ + if (pdf_name_eq(ctx, pdf_dict_get(ctx, dict, PDF_NAME(PageMode)), PDF_NAME(UseOutlines))) + section = USE_PAGE1; + else + section = USE_OTHER_OBJECTS; + mark_all(ctx, doc, opts, val, section, -1); + } + else + mark_all(ctx, doc, opts, val, USE_CATALOGUE, -1); } } - Py_CLEAR(value); - pdf_dirty_annot(ctx, annot); - pdf_update_page(gctx, page); + fz_always(ctx) + { + pdf_unmark_obj(ctx, dict); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } } +static void +mark_trailer(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *dict) +{ + int i, n = pdf_dict_len(ctx, dict); + if (pdf_mark_obj(ctx, dict)) + return; -//---------------------------------------------------------------------------- -// portfolio schema types -//---------------------------------------------------------------------------- -#define PDF_SCHEMA_NUMBER 0 -#define PDF_SCHEMA_SIZE 1 -#define PDF_SCHEMA_TEXT 2 -#define PDF_SCHEMA_DATE 3 -#define PDF_SCHEMA_DESC 4 -#define PDF_SCHEMA_MODDATE 5 -#define PDF_SCHEMA_CREATIONDATE 6 -#define PDF_SCHEMA_FILENAME 7 -#define PDF_SCHEMA_UNKNOWN 8 -//----------------------------------------------------------------------------- -// finds index of an embedded file in a pdf -// Object "id" contains either the entry name (str) or the index. An index is -// only checked for valid range. -//----------------------------------------------------------------------------- -int JM_find_embedded(fz_context *ctx, PyObject *id, pdf_document *pdf) -{ - char *name = NULL; - char *tname= NULL; - int i = -1, count = pdf_count_portfolio_entries(ctx, pdf); - if (count < 1) return -1; + fz_try(ctx) + { + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, dict, i); + pdf_obj *val = pdf_dict_get_val(ctx, dict, i); - // just return the integer id if in range - if (PyInt_Check(id)) + if (pdf_name_eq(ctx, PDF_NAME(Root), key)) + mark_root(ctx, doc, opts, val); + else + mark_all(ctx, doc, opts, val, USE_CATALOGUE, -1); + } + } + fz_always(ctx) { - i = (int) PyInt_AsLong(id); - if (!INRANGE(i, 0, (count-1))) return -1; - return i; + pdf_unmark_obj(ctx, dict); } - name = JM_Python_str_AsChar(id); - if (!name || strlen(name) == 0) return -1; - for (i = 0; i < count; i++) + fz_catch(ctx) { - tname = pdf_to_utf8(ctx, pdf_portfolio_entry_name(ctx, pdf, i)); - if (!strcmp(tname, name)) - { - JM_Python_str_DelForPy3(name); - return i; - } + fz_rethrow(ctx); } - JM_Python_str_DelForPy3(name); - return -1; } -//----------------------------------------------------------------------------- -// Return the /Names object for embedded files -//----------------------------------------------------------------------------- -pdf_obj *JM_embedded_names(fz_context *ctx, pdf_document *pdf) +static void +add_linearization_objs(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) { - pdf_obj *names = NULL, *kids = NULL, *o = NULL; - int i, n; - names = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), PDF_NAME_Root, PDF_NAME_Names, PDF_NAME_EmbeddedFiles, - PDF_NAME_Names, NULL); - if (names) return names; - - // not found, therefore a /Kids object contains the /Names - kids = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), PDF_NAME_Root, - PDF_NAME_Names, PDF_NAME_EmbeddedFiles, - PDF_NAME_Kids, NULL); - //------------------------------------------------------------------------- - // 'kids' is an array of indirect references pointing to dictionaries. - // Only /Limits and /Names can occur in those dictionaries - // We take the first encounter of /Names. - //------------------------------------------------------------------------- - if (!pdf_is_array(ctx, kids) || !(n = pdf_array_len(ctx, kids))) - return NULL; // should never occur + pdf_obj *params_obj = NULL; + pdf_obj *params_ref = NULL; + pdf_obj *hint_obj = NULL; + pdf_obj *hint_ref = NULL; + pdf_obj *o; + int params_num, hint_num; + + fz_var(params_obj); + fz_var(params_ref); + fz_var(hint_obj); + fz_var(hint_ref); - for (i = 0; i < n; i++) + fz_try(ctx) { - o = pdf_resolve_indirect(ctx, pdf_array_get(ctx, kids, i)); - names = pdf_dict_get(ctx, o, PDF_NAME_Names); - if (names) return names; + /* Linearization params */ + params_obj = pdf_new_dict(ctx, doc, 10); + params_ref = pdf_add_object(ctx, doc, params_obj); + params_num = pdf_to_num(ctx, params_ref); + + opts->use_list[params_num] = USE_PARAMS; + opts->renumber_map[params_num] = params_num; + opts->rev_renumber_map[params_num] = params_num; + opts->gen_list[params_num] = 0; + pdf_dict_put_real(ctx, params_obj, PDF_NAME(Linearized), 1.0f); + opts->linear_l = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(L), opts->linear_l); + opts->linear_h0 = pdf_new_int(ctx, INT_MIN); + o = pdf_new_array(ctx, doc, 2); + pdf_dict_put_drop(ctx, params_obj, PDF_NAME(H), o); + pdf_array_push(ctx, o, opts->linear_h0); + opts->linear_h1 = pdf_new_int(ctx, INT_MIN); + pdf_array_push(ctx, o, opts->linear_h1); + opts->linear_o = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(O), opts->linear_o); + opts->linear_e = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(E), opts->linear_e); + opts->linear_n = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(N), opts->linear_n); + opts->linear_t = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(T), opts->linear_t); + + /* Primary hint stream */ + hint_obj = pdf_new_dict(ctx, doc, 10); + hint_ref = pdf_add_object(ctx, doc, hint_obj); + hint_num = pdf_to_num(ctx, hint_ref); + + opts->use_list[hint_num] = USE_HINTS; + opts->renumber_map[hint_num] = hint_num; + opts->rev_renumber_map[hint_num] = hint_num; + opts->gen_list[hint_num] = 0; + pdf_dict_put_int(ctx, hint_obj, PDF_NAME(P), 0); + opts->hints_s = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, hint_obj, PDF_NAME(S), opts->hints_s); + /* FIXME: Do we have thumbnails? Do a T entry */ + /* FIXME: Do we have outlines? Do an O entry */ + /* FIXME: Do we have article threads? Do an A entry */ + /* FIXME: Do we have named destinations? Do a E entry */ + /* FIXME: Do we have interactive forms? Do a V entry */ + /* FIXME: Do we have document information? Do an I entry */ + /* FIXME: Do we have logical structure hierarchy? Do a C entry */ + /* FIXME: Do L, Page Label hint table */ + pdf_dict_put(ctx, hint_obj, PDF_NAME(Filter), PDF_NAME(FlateDecode)); + opts->hints_length = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, hint_obj, PDF_NAME(Length), opts->hints_length); + pdf_get_xref_entry(ctx, doc, hint_num)->stm_ofs = -1; + } + fz_always(ctx) + { + pdf_drop_obj(ctx, params_obj); + pdf_drop_obj(ctx, params_ref); + pdf_drop_obj(ctx, hint_ref); + pdf_drop_obj(ctx, hint_obj); + } + fz_catch(ctx) + { + fz_rethrow(ctx); } - return NULL; // should never execute } -//----------------------------------------------------------------------------- -// perform some cleaning if we have /EmbeddedFiles: -// (1) remove any /Limits if /Names exists -// (2) remove any empty /Collection -// (3) set /PageMode/UseAttachments -//----------------------------------------------------------------------------- -void JM_embedded_clean(fz_context *ctx, pdf_document *pdf) +static void +lpr_inherit_res_contents(fz_context *ctx, pdf_obj *res, pdf_obj *dict, pdf_obj *text) { - pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, pdf), PDF_NAME_Root); + pdf_obj *o, *r; + int i, n; - // remove any empty /Collection entry - pdf_obj *coll = pdf_dict_get(ctx, root, PDF_NAME_Collection); - if (coll && pdf_dict_len(ctx, coll) == 0) - pdf_dict_del(ctx, root, PDF_NAME_Collection); - - if (!pdf_count_portfolio_entries(ctx, pdf)) + /* If the parent node doesn't have an entry of this type, give up. */ + o = pdf_dict_get(ctx, dict, text); + if (!o) return; - pdf_obj *efiles = pdf_dict_getl(ctx, root, PDF_NAME_Names, - PDF_NAME_EmbeddedFiles, NULL); - if (efiles) // we have embedded files - { // make sure they are displayed - pdf_dict_put_name(ctx, root, PDF_NAME_PageMode, "UseAttachments"); - // remove the limits entry: seems to be a MuPDF bug - pdf_dict_del(ctx, efiles, PDF_NAME_Limits); + /* If the resources dict we are building doesn't have an entry of this + * type yet, then just copy it (ensuring it's not a reference) */ + r = pdf_dict_get(ctx, res, text); + if (r == NULL) + { + o = pdf_resolve_indirect(ctx, o); + if (pdf_is_dict(ctx, o)) + o = pdf_copy_dict(ctx, o); + else if (pdf_is_array(ctx, o)) + o = pdf_copy_array(ctx, o); + else + o = NULL; + if (o) + pdf_dict_put_drop(ctx, res, text, o); + return; } - return; + /* Otherwise we need to merge o into r */ + if (pdf_is_dict(ctx, o)) + { + n = pdf_dict_len(ctx, o); + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, o, i); + pdf_obj *val = pdf_dict_get_val(ctx, o, i); + + if (pdf_dict_get(ctx, res, key)) + continue; + pdf_dict_put(ctx, res, key, val); + } + } } -//----------------------------------------------------------------------------- -// insert new embedded file in PDF -// not necessarily an /EmbeddedFiles entry -//----------------------------------------------------------------------------- -pdf_obj *JM_embed_file(fz_context *ctx, pdf_document *pdf, fz_buffer *buf, - char *filename, char *ufilename, char *desc) +static void +lpr_inherit_res(fz_context *ctx, pdf_obj *node, int depth, pdf_obj *dict) { - size_t len = 0; - pdf_obj *ef, *f, *params; - pdf_obj *val = NULL; - fz_buffer *tbuf; - fz_var(val); + while (1) + { + pdf_obj *o; + + node = pdf_dict_get(ctx, node, PDF_NAME(Parent)); + depth--; + if (!node || depth < 0) + break; + + o = pdf_dict_get(ctx, node, PDF_NAME(Resources)); + if (o) + { + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(ExtGState)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(ColorSpace)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(Pattern)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(Shading)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(XObject)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(Font)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(ProcSet)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(Properties)); + } + } +} + +static pdf_obj * +lpr_inherit(fz_context *ctx, pdf_obj *node, char *text, int depth) +{ + do + { + pdf_obj *o = pdf_dict_gets(ctx, node, text); + + if (o) + return pdf_resolve_indirect(ctx, o); + node = pdf_dict_get(ctx, node, PDF_NAME(Parent)); + depth--; + } + while (depth >= 0 && node); + + return NULL; +} + +static int +lpr(fz_context *ctx, pdf_document *doc, pdf_obj *node, int depth, int page) +{ + pdf_obj *kids; + pdf_obj *o = NULL; + int i, n; + + if (pdf_mark_obj(ctx, node)) + return page; + + fz_var(o); + fz_try(ctx) { - val = pdf_new_dict(ctx, pdf, 6); - pdf_dict_put_dict(ctx, val, PDF_NAME_CI, 4); - ef = pdf_dict_put_dict(ctx, val, PDF_NAME_EF, 4); - pdf_dict_put_text_string(ctx, val, PDF_NAME_F, filename); - pdf_dict_put_text_string(ctx, val, PDF_NAME_UF, filename); - pdf_dict_put_text_string(ctx, val, PDF_NAME_Desc, desc); - pdf_dict_put(ctx, val, PDF_NAME_Type, PDF_NAME_Filespec); - tbuf = fz_new_buffer(ctx, strlen(filename)+1); - fz_append_string(ctx, tbuf, filename); - fz_terminate_buffer(ctx, tbuf); - pdf_dict_put_drop(ctx, ef, PDF_NAME_F, - (f = pdf_add_stream(ctx, pdf, tbuf, NULL, 0))); - fz_drop_buffer(ctx, tbuf); - JM_update_stream(ctx, pdf, f, buf); - len = fz_buffer_storage(ctx, buf, NULL); - pdf_dict_put_int(ctx, f, PDF_NAME_DL, len); - pdf_dict_put_int(ctx, f, PDF_NAME_Length, len); - params = pdf_dict_put_dict(ctx, f, PDF_NAME_Params, 4); - pdf_dict_put_int(ctx, params, PDF_NAME_Size, len); + if (pdf_name_eq(ctx, PDF_NAME(Page), pdf_dict_get(ctx, node, PDF_NAME(Type)))) + { + pdf_obj *r; /* r is deliberately not cleaned up */ + + /* Copy resources down to the child */ + o = pdf_keep_obj(ctx, pdf_dict_get(ctx, node, PDF_NAME(Resources))); + if (!o) + { + o = pdf_keep_obj(ctx, pdf_new_dict(ctx, doc, 2)); + pdf_dict_put(ctx, node, PDF_NAME(Resources), o); + } + lpr_inherit_res(ctx, node, depth, o); + r = lpr_inherit(ctx, node, "MediaBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(MediaBox), r); + r = lpr_inherit(ctx, node, "CropBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(CropBox), r); + r = lpr_inherit(ctx, node, "BleedBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(BleedBox), r); + r = lpr_inherit(ctx, node, "TrimBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(TrimBox), r); + r = lpr_inherit(ctx, node, "ArtBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(ArtBox), r); + r = lpr_inherit(ctx, node, "Rotate", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(Rotate), r); + page++; + } + else + { + kids = pdf_dict_get(ctx, node, PDF_NAME(Kids)); + n = pdf_array_len(ctx, kids); + for(i = 0; i < n; i++) + { + page = lpr(ctx, doc, pdf_array_get(ctx, kids, i), depth+1, page); + } + pdf_dict_del(ctx, node, PDF_NAME(Resources)); + pdf_dict_del(ctx, node, PDF_NAME(MediaBox)); + pdf_dict_del(ctx, node, PDF_NAME(CropBox)); + pdf_dict_del(ctx, node, PDF_NAME(BleedBox)); + pdf_dict_del(ctx, node, PDF_NAME(TrimBox)); + pdf_dict_del(ctx, node, PDF_NAME(ArtBox)); + pdf_dict_del(ctx, node, PDF_NAME(Rotate)); + } } fz_always(ctx) { - ; + pdf_drop_obj(ctx, o); + } + fz_catch(ctx) + { + fz_rethrow(ctx); } - fz_catch(ctx) fz_rethrow(ctx); - return val; -} + pdf_unmark_obj(ctx, node); -//---------------------------------------------------------------------------- -// Helpers for document page selection - main logic was imported -// from pdf_clean_file.c. But instead of analyzing a string-based spec of -// selected pages, we accept a Python sequence. -//---------------------------------------------------------------------------- -typedef struct globals_s -{ - pdf_document *doc; - fz_context *ctx; -} globals; + return page; +} -int string_in_names_list(fz_context *ctx, pdf_obj *p, pdf_obj *names_list) +static void +linearize(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) { - int n = pdf_array_len(ctx, names_list); int i; - char *str = pdf_to_str_buf(ctx, p); + int n = pdf_xref_len(ctx, doc) + 2; + int *reorder; + int *rev_renumber_map; - for (i = 0; i < n ; i += 2) + opts->page_object_lists = page_objects_list_create(ctx); + + /* Ensure that every page has local references of its resources */ + /* FIXME: We could 'thin' the resources according to what is actually + * required for each page, but this would require us to run the page + * content streams. */ + pdf_localise_page_resources(ctx, doc); + + /* Walk the objects for each page, marking which ones are used, where */ + memset(opts->use_list, 0, n * sizeof(int)); + mark_trailer(ctx, doc, opts, pdf_trailer(ctx, doc)); + + /* Add new objects required for linearization */ + add_linearization_objs(ctx, doc, opts); + + /* Allocate/init the structures used for renumbering the objects */ + reorder = fz_calloc(ctx, n, sizeof(int)); + rev_renumber_map = fz_calloc(ctx, n, sizeof(int)); + for (i = 0; i < n; i++) { - if (!strcmp(pdf_to_str_buf(ctx, pdf_array_get(ctx, names_list, i)), str)) - return 1; + reorder[i] = i; } - return 0; + + /* Heap sort the reordering */ + heap_sort(reorder+1, n-1, opts->use_list, &order_ge); + + /* Find the split point */ + for (i = 1; (opts->use_list[reorder[i]] & USE_PARAMS) == 0; i++) {} + opts->start = i; + + /* Roll the reordering into the renumber_map */ + for (i = 0; i < n; i++) + { + opts->renumber_map[reorder[i]] = i; + rev_renumber_map[i] = opts->rev_renumber_map[reorder[i]]; + } + fz_free(ctx, opts->rev_renumber_map); + opts->rev_renumber_map = rev_renumber_map; + fz_free(ctx, reorder); + + /* Apply the renumber_map */ + page_objects_list_renumber(opts); + renumberobjs(ctx, doc, opts); + + page_objects_list_sort_and_dedupe(ctx, opts->page_object_lists); } -//---------------------------------------------------------------------------- -// Recreate page tree to only retain specified pages. -//---------------------------------------------------------------------------- -void retainpage(fz_context *ctx, pdf_document *doc, pdf_obj *parent, pdf_obj *kids, int page) +static void +update_linearization_params(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) { - pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, page); + int64_t offset; + pdf_set_int(ctx, opts->linear_l, opts->file_len); + /* Primary hint stream offset (of object, not stream!) */ + pdf_set_int(ctx, opts->linear_h0, opts->ofs_list[pdf_xref_len(ctx, doc)-1]); + /* Primary hint stream length (of object, not stream!) */ + offset = (opts->start == 1 ? opts->main_xref_offset : opts->ofs_list[1] + opts->hintstream_len); + pdf_set_int(ctx, opts->linear_h1, offset - opts->ofs_list[pdf_xref_len(ctx, doc)-1]); + /* Object number of first pages page object (the first object of page 0) */ + pdf_set_int(ctx, opts->linear_o, opts->page_object_lists->page[0]->object[0]); + /* Offset of end of first page (first page is followed by primary + * hint stream (object n-1) then remaining pages (object 1...). The + * primary hint stream counts as part of the first pages data, I think. + */ + offset = (opts->start == 1 ? opts->main_xref_offset : opts->ofs_list[1] + opts->hintstream_len); + pdf_set_int(ctx, opts->linear_e, offset); + /* Number of pages in document */ + pdf_set_int(ctx, opts->linear_n, opts->page_count); + /* Offset of first entry in main xref table */ + pdf_set_int(ctx, opts->linear_t, opts->first_xref_entry_offset + opts->hintstream_len); + /* Offset of shared objects hint table in the primary hint stream */ + pdf_set_int(ctx, opts->hints_s, opts->hints_shared_offset); + /* Primary hint stream length */ + pdf_set_int(ctx, opts->hints_length, opts->hintstream_len); +} - pdf_flatten_inheritable_page_items(ctx, pageref); +/* + * Make sure we have loaded objects from object streams. + */ - pdf_dict_put(ctx, pageref, PDF_NAME_Parent, parent); +static void preloadobjstms(fz_context *ctx, pdf_document *doc) +{ + pdf_obj *obj; + int num; - /* Store page object in new kids array */ - pdf_array_push(ctx, kids, pageref); + /* xref_len may change due to repair, so check it every iteration */ + for (num = 0; num < pdf_xref_len(ctx, doc); num++) + { + if (pdf_get_xref_entry(ctx, doc, num)->type == 'o') + { + obj = pdf_load_object(ctx, doc, num); + pdf_drop_obj(ctx, obj); + } + } } -int dest_is_valid_page(fz_context *ctx, pdf_obj *obj, int *page_object_nums, int pagecount) -{ - int i; - int num = pdf_to_num(ctx, obj); +/* + * Save streams and objects to the output + */ - if (num == 0) +static inline int isbinary(int c) +{ + if (c == '\n' || c == '\r' || c == '\t') return 0; - for (i = 0; i < pagecount; i++) - { - if (page_object_nums[i] == num) + return c < 32 || c > 127; +} + +static int isbinarystream(fz_context *ctx, fz_buffer *buf) +{ + unsigned char *data; + size_t len = fz_buffer_storage(ctx, buf, &data); + size_t i; + for (i = 0; i < len; i++) + if (isbinary(data[i])) return 1; - } return 0; } -int dest_is_valid(fz_context *ctx, pdf_obj *o, int page_count, int *page_object_nums, pdf_obj *names_list) +static fz_buffer *hexbuf(fz_context *ctx, const unsigned char *p, size_t n) { - pdf_obj *p; - - p = pdf_dict_get(ctx, o, PDF_NAME_A); - if (pdf_name_eq(ctx, pdf_dict_get(ctx, p, PDF_NAME_S), PDF_NAME_GoTo) && - !string_in_names_list(ctx, pdf_dict_get(ctx, p, PDF_NAME_D), names_list)) - return 0; + static const char hex[17] = "0123456789abcdef"; + int x = 0; + size_t len = n * 2 + (n / 32) + 1; + unsigned char *data = fz_malloc(ctx, len); + fz_buffer *buf = fz_new_buffer_from_data(ctx, data, len); - p = pdf_dict_get(ctx, o, PDF_NAME_Dest); - if (p == NULL) - {} - else if (pdf_is_string(ctx, p)) + while (n--) { - return string_in_names_list(ctx, p, names_list); + *data++ = hex[*p >> 4]; + *data++ = hex[*p & 15]; + if (++x == 32) + { + *data++ = '\n'; + x = 0; + } + p++; } - else if (!dest_is_valid_page(ctx, pdf_array_get(ctx, p, 0), page_object_nums, page_count)) - return 0; - return 1; -} + *data++ = '>'; -int strip_outlines(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int page_count, int *page_object_nums, pdf_obj *names_list); + return buf; +} -int strip_outline(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int page_count, int *page_object_nums, pdf_obj *names_list, pdf_obj **pfirst, pdf_obj **plast) +static void addhexfilter(fz_context *ctx, pdf_document *doc, pdf_obj *dict) { - pdf_obj *prev = NULL; - pdf_obj *first = NULL; - pdf_obj *current; - int count = 0; + pdf_obj *f, *dp, *newf, *newdp; - for (current = outlines; current != NULL; ) - { - int nc; + newf = newdp = NULL; + f = pdf_dict_get(ctx, dict, PDF_NAME(Filter)); + dp = pdf_dict_get(ctx, dict, PDF_NAME(DecodeParms)); - /*********************************************************************/ - // Strip any children to start with. This takes care of - // First / Last / Count for us. - /*********************************************************************/ - nc = strip_outlines(ctx, doc, current, page_count, page_object_nums, names_list); + fz_var(newf); + fz_var(newdp); - if (!dest_is_valid(ctx, current, page_count, page_object_nums, names_list)) + fz_try(ctx) + { + if (pdf_is_name(ctx, f)) { - if (nc == 0) - { - /*************************************************************/ - // Outline with invalid dest and no children. Drop it by - // pulling the next one in here. - /*************************************************************/ - pdf_obj *next = pdf_dict_get(ctx, current, PDF_NAME_Next); - if (next == NULL) - { - // There is no next one to pull in - if (prev != NULL) - pdf_dict_del(ctx, prev, PDF_NAME_Next); - } - else if (prev != NULL) - { - pdf_dict_put(ctx, prev, PDF_NAME_Next, next); - pdf_dict_put(ctx, next, PDF_NAME_Prev, prev); - } - else - { - pdf_dict_del(ctx, next, PDF_NAME_Prev); - } - current = next; - } - else + newf = pdf_new_array(ctx, doc, 2); + pdf_array_push(ctx, newf, PDF_NAME(ASCIIHexDecode)); + pdf_array_push(ctx, newf, f); + f = newf; + if (pdf_is_dict(ctx, dp)) { - // Outline with invalid dest, but children. Just drop the dest. - pdf_dict_del(ctx, current, PDF_NAME_Dest); - pdf_dict_del(ctx, current, PDF_NAME_A); - current = pdf_dict_get(ctx, current, PDF_NAME_Next); + newdp = pdf_new_array(ctx, doc, 2); + pdf_array_push(ctx, newdp, PDF_NULL); + pdf_array_push(ctx, newdp, dp); + dp = newdp; } } - else + else if (pdf_is_array(ctx, f)) { - // Keep this one - if (first == NULL) - first = current; - prev = current; - current = pdf_dict_get(ctx, current, PDF_NAME_Next); - count++; + pdf_array_insert(ctx, f, PDF_NAME(ASCIIHexDecode), 0); + if (pdf_is_array(ctx, dp)) + pdf_array_insert(ctx, dp, PDF_NULL, 0); } + else + f = PDF_NAME(ASCIIHexDecode); + + pdf_dict_put(ctx, dict, PDF_NAME(Filter), f); + if (dp) + pdf_dict_put(ctx, dict, PDF_NAME(DecodeParms), dp); + } + fz_always(ctx) + { + pdf_drop_obj(ctx, newf); + pdf_drop_obj(ctx, newdp); } + fz_catch(ctx) + fz_rethrow(ctx); +} - *pfirst = first; - *plast = prev; +static fz_buffer *deflatebuf(fz_context *ctx, const unsigned char *p, size_t n) +{ + fz_buffer *buf; + uLongf csize; + int t; + uLong longN = (uLong)n; + unsigned char *data; + size_t cap; - return count; + if (n != (size_t)longN) + fz_throw(ctx, FZ_ERROR_GENERIC, "Buffer too large to deflate"); + + cap = compressBound(longN); + data = fz_malloc(ctx, cap); + buf = fz_new_buffer_from_data(ctx, data, cap); + csize = (uLongf)cap; + t = compress(data, &csize, p, longN); + if (t != Z_OK) + { + fz_drop_buffer(ctx, buf); + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot deflate buffer"); + } + fz_resize_buffer(ctx, buf, csize); + return buf; } -int strip_outlines(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int page_count, int *page_object_nums, pdf_obj *names_list) +static void write_data(fz_context *ctx, void *arg, const unsigned char *data, int len) { - int nc; - pdf_obj *first; - pdf_obj *last; + fz_write_data(ctx, (fz_output *)arg, data, len); +} - if (outlines == NULL) - return 0; +static void copystream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj_orig, int num, int gen, int do_deflate) +{ + fz_buffer *buf, *tmp; + pdf_obj *obj; + size_t len; + unsigned char *data; - first = pdf_dict_get(ctx, outlines, PDF_NAME_First); - if (first == NULL) - nc = 0; - else - nc = strip_outline(ctx, doc, first, page_count, page_object_nums, - names_list, &first, &last); + buf = pdf_load_raw_stream_number(ctx, doc, num); - if (nc == 0) + obj = pdf_copy_dict(ctx, obj_orig); + + len = fz_buffer_storage(ctx, buf, &data); + if (do_deflate && !pdf_dict_get(ctx, obj, PDF_NAME(Filter))) { - pdf_dict_del(ctx, outlines, PDF_NAME_First); - pdf_dict_del(ctx, outlines, PDF_NAME_Last); - pdf_dict_del(ctx, outlines, PDF_NAME_Count); + size_t clen; + unsigned char *cdata; + tmp = deflatebuf(ctx, data, len); + clen = fz_buffer_storage(ctx, tmp, &cdata); + if (clen >= len) + { + /* Don't bother compressing, as we gain nothing. */ + fz_drop_buffer(ctx, tmp); + } + else + { + len = clen; + data = cdata; + pdf_dict_put(ctx, obj, PDF_NAME(Filter), PDF_NAME(FlateDecode)); + fz_drop_buffer(ctx, buf); + buf = tmp; + } } - else + + if (opts->do_ascii && isbinarystream(ctx, buf)) { - int old_count = pdf_to_int(ctx, pdf_dict_get(ctx, outlines, PDF_NAME_Count)); - pdf_dict_put(ctx, outlines, PDF_NAME_First, first); - pdf_dict_put(ctx, outlines, PDF_NAME_Last, last); - pdf_dict_put_drop(ctx, outlines, PDF_NAME_Count, pdf_new_int(ctx, doc, old_count > 0 ? nc : -nc)); + tmp = hexbuf(ctx, data, len); + fz_drop_buffer(ctx, buf); + buf = tmp; + len = fz_buffer_storage(ctx, buf, &data); + + addhexfilter(ctx, doc, obj); } - return nc; + + pdf_dict_put_int(ctx, obj, PDF_NAME(Length), pdf_encrypted_len(ctx, NULL, num, gen, (int)len)); + + fz_write_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_encrypted_obj(ctx, opts->out, obj, opts->do_tight, NULL, num, gen); + fz_write_string(ctx, opts->out, "\nstream\n"); + pdf_encrypt_data(ctx, NULL, num, gen, write_data, opts->out, data, (int) len); + fz_write_string(ctx, opts->out, "\nendstream\nendobj\n\n"); + + fz_drop_buffer(ctx, buf); + pdf_drop_obj(ctx, obj); } -//---------------------------------------------------------------------------- -// This is called by PyMuPDF: -// liste = page numbers to retain -//---------------------------------------------------------------------------- -void retainpages(fz_context *ctx, globals *glo, PyObject *liste) +static void expandstream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj_orig, int num, int gen, int do_deflate) { - pdf_obj *oldroot, *root, *pages, *kids, *countobj, *olddests; - Py_ssize_t argc = PySequence_Size(liste); - pdf_document *doc = glo->doc; - pdf_obj *names_list = NULL; - pdf_obj *outlines; - pdf_obj *ocproperties; - int pagecount = pdf_count_pages(ctx, doc); + fz_buffer *buf, *tmp; + pdf_obj *obj; + int truncated = 0; + size_t len; + unsigned char *data; + + buf = pdf_load_stream_truncated(ctx, doc, num, (opts->continue_on_error ? &truncated : NULL)); + if (truncated && opts->errors) + (*opts->errors)++; + + obj = pdf_copy_dict(ctx, obj_orig); + pdf_dict_del(ctx, obj, PDF_NAME(Filter)); + pdf_dict_del(ctx, obj, PDF_NAME(DecodeParms)); + + len = fz_buffer_storage(ctx, buf, &data); + if (do_deflate) + { + unsigned char *cdata; + size_t clen; + tmp = deflatebuf(ctx, data, len); + clen = fz_buffer_storage(ctx, tmp, &cdata); + if (clen >= len) + { + /* Don't bother compressing, as we gain nothing. */ + fz_drop_buffer(ctx, tmp); + } + else + { + len = clen; + data = cdata; + pdf_dict_put(ctx, obj, PDF_NAME(Filter), PDF_NAME(FlateDecode)); + fz_drop_buffer(ctx, buf); + buf = tmp; + } + } - int i; - int *page_object_nums; + if (opts->do_ascii && isbinarystream(ctx, buf)) + { + tmp = hexbuf(ctx, data, len); + fz_drop_buffer(ctx, buf); + buf = tmp; + len = fz_buffer_storage(ctx, buf, &data); -/******************************************************************************/ -// Keep only pages/type and (reduced) dest entries to avoid -// references to dropped pages -/******************************************************************************/ - oldroot = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME_Root); - pages = pdf_dict_get(ctx, oldroot, PDF_NAME_Pages); - olddests = pdf_load_name_tree(ctx, doc, PDF_NAME_Dests); - outlines = pdf_dict_get(ctx, oldroot, PDF_NAME_Outlines); - ocproperties = pdf_dict_get(ctx, oldroot, PDF_NAME_OCProperties); + addhexfilter(ctx, doc, obj); + } - root = pdf_new_dict(ctx, doc, 3); - pdf_dict_put(ctx, root, PDF_NAME_Type, pdf_dict_get(ctx, oldroot, PDF_NAME_Type)); - pdf_dict_put(ctx, root, PDF_NAME_Pages, pdf_dict_get(ctx, oldroot, PDF_NAME_Pages)); - if (outlines) - pdf_dict_put(ctx, root, PDF_NAME_Outlines, outlines); - if (ocproperties) - pdf_dict_put(ctx, root, PDF_NAME_OCProperties, ocproperties); + pdf_dict_put_int(ctx, obj, PDF_NAME(Length), len); - pdf_update_object(ctx, doc, pdf_to_num(ctx, oldroot), root); + fz_write_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_encrypted_obj(ctx, opts->out, obj, opts->do_tight, NULL, num, gen); + fz_write_string(ctx, opts->out, "\nstream\n"); + fz_write_data(ctx, opts->out, data, len); + fz_write_string(ctx, opts->out, "\nendstream\nendobj\n\n"); - // Create a new kids array with only the pages we want to keep - kids = pdf_new_array(ctx, doc, 1); + fz_drop_buffer(ctx, buf); + pdf_drop_obj(ctx, obj); +} - // Retain pages specified - Py_ssize_t page; - fz_try(ctx) +static int is_image_filter(const char *s) +{ + if (!strcmp(s, "CCITTFaxDecode") || !strcmp(s, "CCF") || + !strcmp(s, "DCTDecode") || !strcmp(s, "DCT") || + !strcmp(s, "RunLengthDecode") || !strcmp(s, "RL") || + !strcmp(s, "JBIG2Decode") || + !strcmp(s, "JPXDecode")) + return 1; + return 0; +} + +static int filter_implies_image(fz_context *ctx, pdf_obj *o) +{ + if (!o) + return 0; + if (pdf_is_name(ctx, o)) + return is_image_filter(pdf_to_name(ctx, o)); + if (pdf_is_array(ctx, o)) { - for (page = 0; page < argc; page++) - { - i = (int) PyInt_AsLong(PySequence_GetItem(liste, page)); - if (i < 0 || i >= pagecount) - THROWMSG("invalid page number(s)"); - retainpage(ctx, doc, pages, kids, i); - } + int i, len; + len = pdf_array_len(ctx, o); + for (i = 0; i < len; i++) + if (is_image_filter(pdf_to_name(ctx, pdf_array_get(ctx, o, i)))) + return 1; } - fz_catch(ctx) fz_rethrow(ctx); + return 0; +} - // Update page count and kids array - countobj = pdf_new_int(ctx, doc, pdf_array_len(ctx, kids)); - pdf_dict_put_drop(ctx, pages, PDF_NAME_Count, countobj); - pdf_dict_put_drop(ctx, pages, PDF_NAME_Kids, kids); +static int is_image_stream(fz_context *ctx, pdf_obj *obj) +{ + pdf_obj *o; + if ((o = pdf_dict_get(ctx, obj, PDF_NAME(Type)), pdf_name_eq(ctx, o, PDF_NAME(XObject)))) + if ((o = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)), pdf_name_eq(ctx, o, PDF_NAME(Image)))) + return 1; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Filter)), filter_implies_image(ctx, o)) + return 1; + if (pdf_dict_get(ctx, obj, PDF_NAME(Width)) != NULL && pdf_dict_get(ctx, obj, PDF_NAME(Height)) != NULL) + return 1; + return 0; +} - pagecount = pdf_count_pages(ctx, doc); - page_object_nums = fz_calloc(ctx, pagecount, sizeof(*page_object_nums)); - for (i = 0; i < pagecount; i++) +static int is_font_stream(fz_context *ctx, pdf_obj *obj) +{ + pdf_obj *o; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Type)), pdf_name_eq(ctx, o, PDF_NAME(Font))) + return 1; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Type)), pdf_name_eq(ctx, o, PDF_NAME(FontDescriptor))) + return 1; + if (pdf_dict_get(ctx, obj, PDF_NAME(Length1)) != NULL) + return 1; + if (pdf_dict_get(ctx, obj, PDF_NAME(Length2)) != NULL) + return 1; + if (pdf_dict_get(ctx, obj, PDF_NAME(Length3)) != NULL) + return 1; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)), pdf_name_eq(ctx, o, PDF_NAME(Type1C))) + return 1; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)), pdf_name_eq(ctx, o, PDF_NAME(CIDFontType0C))) + return 1; + return 0; +} + +static int is_xml_metadata(fz_context *ctx, pdf_obj *obj) +{ + if (pdf_name_eq(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Type)), PDF_NAME(Metadata))) + if (pdf_name_eq(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Subtype)), PDF_NAME(XML))) + return 1; + return 0; +} + +static void writeobject(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int num, int gen, int skip_xrefs, int unenc) +{ + if (unenc) return; + pdf_xref_entry *entry; + pdf_obj *obj; + pdf_obj *type; + + fz_try(ctx) { - pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, i); - page_object_nums[i] = pdf_to_num(ctx, pageref); + obj = pdf_load_object(ctx, doc, num); } - -/******************************************************************************/ -// If we had an old Dests tree (now reformed as an olddests dictionary), -// keep any entries in there that point to valid pages. -// This may mean we keep more than we need, but it is safe at least. -/******************************************************************************/ - if (olddests) + fz_catch(ctx) { - pdf_obj *names = pdf_new_dict(ctx, doc, 1); - pdf_obj *dests = pdf_new_dict(ctx, doc, 1); - int len = pdf_dict_len(ctx, olddests); - - names_list = pdf_new_array(ctx, doc, 32); + fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); + if (opts->continue_on_error) + { + fz_write_printf(ctx, opts->out, "%d %d obj\nnull\nendobj\n", num, gen); + if (opts->errors) + (*opts->errors)++; + fz_warn(ctx, "%s", fz_caught_message(ctx)); + return; + } + else + fz_rethrow(ctx); + } - for (i = 0; i < len; i++) + /* skip ObjStm and XRef objects */ + if (pdf_is_dict(ctx, obj)) + { + type = pdf_dict_get(ctx, obj, PDF_NAME(Type)); + if (pdf_name_eq(ctx, type, PDF_NAME(ObjStm))) { - pdf_obj *key = pdf_dict_get_key(ctx, olddests, i); - pdf_obj *val = pdf_dict_get_val(ctx, olddests, i); - pdf_obj *dest = pdf_dict_get(ctx, val, PDF_NAME_D); + opts->use_list[num] = 0; + pdf_drop_obj(ctx, obj); + return; + } + if (skip_xrefs && pdf_name_eq(ctx, type, PDF_NAME(XRef))) + { + opts->use_list[num] = 0; + pdf_drop_obj(ctx, obj); + return; + } + } - dest = pdf_array_get(ctx, dest ? dest : val, 0); - if (dest_is_valid_page(ctx, dest, page_object_nums, pagecount)) + entry = pdf_get_xref_entry(ctx, doc, num); + if (!pdf_obj_num_is_stream(ctx, doc, num)) + { + fz_write_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_encrypted_obj(ctx, opts->out, obj, opts->do_tight, NULL, num, gen); + fz_write_string(ctx, opts->out, "\nendobj\n\n"); + } + else if (entry->stm_ofs < 0 && entry->stm_buf == NULL) + { + fz_write_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_encrypted_obj(ctx, opts->out, obj, opts->do_tight, NULL, num, gen); + fz_write_string(ctx, opts->out, "\nstream\nendstream\nendobj\n\n"); + } + else + { + fz_try(ctx) + { + int do_deflate = opts->do_compress; + int do_expand = opts->do_expand; + if (opts->do_compress_images && is_image_stream(ctx, obj)) + do_deflate = 1, do_expand = 0; + if (opts->do_compress_fonts && is_font_stream(ctx, obj)) + do_deflate = 1, do_expand = 0; + if (is_xml_metadata(ctx, obj)) + do_deflate = 0, do_expand = 0; + if (do_expand) + expandstream(ctx, doc, opts, obj, num, gen, do_deflate); + else + copystream(ctx, doc, opts, obj, num, gen, do_deflate); + } + fz_catch(ctx) + { + fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); + if (opts->continue_on_error) { - pdf_obj *key_str = pdf_new_string(ctx, doc, pdf_to_name(ctx, key), strlen(pdf_to_name(ctx, key))); - pdf_array_push_drop(ctx, names_list, key_str); - pdf_array_push(ctx, names_list, val); + fz_write_printf(ctx, opts->out, "%d %d obj\nnull\nendobj\n", num, gen); + if (opts->errors) + (*opts->errors)++; + fz_warn(ctx, "%s", fz_caught_message(ctx)); + } + else + { + pdf_drop_obj(ctx, obj); + fz_rethrow(ctx); } } + } - pdf_dict_put(ctx, dests, PDF_NAME_Names, names_list); - pdf_dict_put(ctx, names, PDF_NAME_Dests, dests); - pdf_dict_put(ctx, root, PDF_NAME_Names, names); + pdf_drop_obj(ctx, obj); +} - pdf_drop_obj(ctx, names); - pdf_drop_obj(ctx, dests); - pdf_drop_obj(ctx, olddests); - } +static void writexrefsubsect(fz_context *ctx, pdf_write_state *opts, int from, int to) +{ + int num; -/*****************************************************************************/ -// Edit each pages /Annot list to remove any links pointing to nowhere. -/*****************************************************************************/ - for (i = 0; i < pagecount; i++) + fz_write_printf(ctx, opts->out, "%d %d\n", from, to - from); + for (num = from; num < to; num++) { - pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, i); + if (opts->use_list[num]) + fz_write_printf(ctx, opts->out, "%010lu %05d n \n", opts->ofs_list[num], opts->gen_list[num]); + else + fz_write_printf(ctx, opts->out, "%010lu %05d f \n", opts->ofs_list[num], opts->gen_list[num]); + } +} - pdf_obj *annots = pdf_dict_get(ctx, pageref, PDF_NAME_Annots); +static void writexref(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int from, int to, int first, int64_t main_xref_offset, int64_t startxref) +{ + pdf_obj *trailer = NULL; + pdf_obj *obj; + pdf_obj *nobj = NULL; - int len = pdf_array_len(ctx, annots); - int j; + fz_write_string(ctx, opts->out, "xref\n"); + opts->first_xref_entry_offset = fz_tell_output(ctx, opts->out); - for (j = 0; j < len; j++) + if (opts->do_incremental) + { + int subfrom = from; + int subto; + + while (subfrom < to) { - pdf_obj *o = pdf_array_get(ctx, annots, j); + while (subfrom < to && !pdf_xref_is_incremental(ctx, doc, subfrom)) + subfrom++; - if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME_Subtype), PDF_NAME_Link)) - continue; + subto = subfrom; + while (subto < to && pdf_xref_is_incremental(ctx, doc, subto)) + subto++; - if (!dest_is_valid(ctx, o, pagecount, page_object_nums, names_list)) - { - // Remove this annotation - pdf_array_delete(ctx, annots, j); - len--; - j--; - } + if (subfrom < subto) + writexrefsubsect(ctx, opts, subfrom, subto); + + subfrom = subto; } } + else + { + writexrefsubsect(ctx, opts, from, to); + } - if (strip_outlines(ctx, doc, outlines, pagecount, page_object_nums, names_list) == 0) + fz_write_string(ctx, opts->out, "\n"); + + fz_var(trailer); + + if (opts->do_incremental) { - pdf_dict_del(ctx, root, PDF_NAME_Outlines); + trailer = pdf_keep_obj(ctx, pdf_trailer(ctx, doc)); + pdf_dict_put_int(ctx, trailer, PDF_NAME(Size), pdf_xref_len(ctx, doc)); + pdf_dict_put_int(ctx, trailer, PDF_NAME(Prev), doc->startxref); + doc->startxref = startxref; } + else + { + trailer = pdf_new_dict(ctx, doc, 5); - fz_free(ctx, page_object_nums); - pdf_drop_obj(ctx, names_list); - pdf_drop_obj(ctx, root); + nobj = pdf_new_int(ctx, to); + pdf_dict_put_drop(ctx, trailer, PDF_NAME(Size), nobj); + + if (first) + { + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Info)); + if (obj) + pdf_dict_put(ctx, trailer, PDF_NAME(Info), obj); + + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); + if (obj) + pdf_dict_put(ctx, trailer, PDF_NAME(Root), obj); + + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(ID)); + if (obj) + pdf_dict_put(ctx, trailer, PDF_NAME(ID), obj); + } + if (main_xref_offset != 0) + { + nobj = pdf_new_int(ctx, main_xref_offset); + pdf_dict_put_drop(ctx, trailer, PDF_NAME(Prev), nobj); + } + } + + fz_write_string(ctx, opts->out, "trailer\n"); + /* Trailer is NOT encrypted */ + pdf_print_obj(ctx, opts->out, trailer, opts->do_tight); + fz_write_string(ctx, opts->out, "\n"); + + pdf_drop_obj(ctx, trailer); + + fz_write_printf(ctx, opts->out, "startxref\n%lu\n%%%%EOF\n", startxref); + + doc->has_xref_streams = 0; } +static void writexrefstreamsubsect(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *index, fz_buffer *fzbuf, int from, int to) +{ + int num; -//----------------------------------------------------------------------------- -// Make an XObject from a PDF page -// For a positive xref assume that that object can be used instead -//----------------------------------------------------------------------------- -pdf_obj *JM_xobject_from_page(fz_context *ctx, pdf_document *pdfout, pdf_document *pdfsrc, int pno, fz_rect *mediabox, fz_rect *cropbox, int xref, pdf_graft_map *gmap) + pdf_array_push_int(ctx, index, from); + pdf_array_push_int(ctx, index, to - from); + for (num = from; num < to; num++) + { + fz_append_byte(ctx, fzbuf, opts->use_list[num] ? 1 : 0); + fz_append_byte(ctx, fzbuf, opts->ofs_list[num]>>24); + fz_append_byte(ctx, fzbuf, opts->ofs_list[num]>>16); + fz_append_byte(ctx, fzbuf, opts->ofs_list[num]>>8); + fz_append_byte(ctx, fzbuf, opts->ofs_list[num]); + fz_append_byte(ctx, fzbuf, opts->gen_list[num]); + } +} + +static void writexrefstream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int from, int to, int first, int64_t main_xref_offset, int64_t startxref) { - fz_buffer *nres = NULL, *res = NULL; - pdf_obj *xobj1, *contents, *resources, *o, *spageref; - int i; + int num; + pdf_obj *dict = NULL; + pdf_obj *obj; + pdf_obj *w = NULL; + pdf_obj *index; + fz_buffer *fzbuf = NULL; + + fz_var(dict); + fz_var(w); + fz_var(fzbuf); fz_try(ctx) { - if (pno < 0 || pno >= pdf_count_pages(ctx, pdfsrc)) - THROWMSG("invalid page number(s)"); - spageref = pdf_lookup_page_obj(ctx, pdfsrc, pno); - pdf_obj *mb = pdf_dict_get(ctx, spageref, PDF_NAME_MediaBox); - if (mb) - pdf_to_rect(ctx, mb, mediabox); - else - pdf_bound_page(ctx, pdf_load_page(ctx, pdfsrc, pno), mediabox); - o = pdf_dict_get(ctx, spageref, PDF_NAME_CropBox); - if (!o) + num = pdf_create_object(ctx, doc); + dict = pdf_new_dict(ctx, doc, 6); + pdf_update_object(ctx, doc, num, dict); + + opts->first_xref_entry_offset = fz_tell_output(ctx, opts->out); + + to++; + + if (first) { - cropbox->x0 = mediabox->x0; - cropbox->y0 = mediabox->y0; - cropbox->x1 = mediabox->x1; - cropbox->y1 = mediabox->y1; + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Info)); + if (obj) + pdf_dict_put(ctx, dict, PDF_NAME(Info), obj); + + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); + if (obj) + pdf_dict_put(ctx, dict, PDF_NAME(Root), obj); + + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(ID)); + if (obj) + pdf_dict_put(ctx, dict, PDF_NAME(ID), obj); + + if (opts->do_incremental) + { + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Encrypt)); + if (obj) + pdf_dict_put(ctx, dict, PDF_NAME(Encrypt), obj); + } } - else - pdf_to_rect(ctx, o, cropbox); - if (xref > 0) // we can reuse an XObject! + pdf_dict_put_int(ctx, dict, PDF_NAME(Size), to); + + if (opts->do_incremental) { - if (xref >= pdf_xref_len(ctx, pdfout)) - THROWMSG("xref out of range"); - xobj1 = pdf_new_indirect(ctx, pdfout, xref, 0); + pdf_dict_put_int(ctx, dict, PDF_NAME(Prev), doc->startxref); + doc->startxref = startxref; } - else // need to create new XObject + else { - // Deep-copy resources object of source page - o = pdf_dict_get(ctx, spageref, PDF_NAME_Resources); - if (gmap) // use graftmap when possible - resources = pdf_graft_mapped_object(ctx, gmap, o); - else - resources = pdf_graft_object(ctx, pdfout, o); - - // get spgage contents source; combine when several objects - contents = pdf_dict_get(ctx, spageref, PDF_NAME_Contents); - if (pdf_is_array(ctx, contents)) // more than one! - { - res = fz_new_buffer(ctx, 1024); - for (i=0; i < pdf_array_len(ctx, contents); i++) - { - nres = pdf_load_stream(ctx, pdf_array_get(ctx, contents, i)); - fz_append_buffer(ctx, res, nres); - fz_drop_buffer(ctx, nres); - } - } - else + if (main_xref_offset != 0) + pdf_dict_put_int(ctx, dict, PDF_NAME(Prev), main_xref_offset); + } + + pdf_dict_put(ctx, dict, PDF_NAME(Type), PDF_NAME(XRef)); + + w = pdf_new_array(ctx, doc, 3); + pdf_dict_put(ctx, dict, PDF_NAME(W), w); + pdf_array_push_int(ctx, w, 1); + pdf_array_push_int(ctx, w, 4); + pdf_array_push_int(ctx, w, 1); + + index = pdf_new_array(ctx, doc, 2); + pdf_dict_put_drop(ctx, dict, PDF_NAME(Index), index); + + /* opts->gen_list[num] is already initialized by fz_calloc. */ + opts->use_list[num] = 1; + opts->ofs_list[num] = opts->first_xref_entry_offset; + + fzbuf = fz_new_buffer(ctx, (1 + 4 + 1) * (to-from)); + + if (opts->do_incremental) + { + int subfrom = from; + int subto; + + while (subfrom < to) { - res = pdf_load_stream(ctx, contents); - } + while (subfrom < to && !pdf_xref_is_incremental(ctx, doc, subfrom)) + subfrom++; - //------------------------------------------------------------- - // create XObject representing the source page - //------------------------------------------------------------- - xobj1 = pdf_new_xobject(ctx, pdfout, mediabox, &fz_identity); - // store spage contents - JM_update_stream(ctx, pdfout, xobj1, res); - fz_drop_buffer(ctx, res); + subto = subfrom; + while (subto < to && pdf_xref_is_incremental(ctx, doc, subto)) + subto++; - // store spage resources - pdf_dict_put_drop(ctx, xobj1, PDF_NAME_Resources, resources); + if (subfrom < subto) + writexrefstreamsubsect(ctx, doc, opts, index, fzbuf, subfrom, subto); + + subfrom = subto; + } } + else + { + writexrefstreamsubsect(ctx, doc, opts, index, fzbuf, from, to); + } + + pdf_update_stream(ctx, doc, dict, fzbuf, 0); + + writeobject(ctx, doc, opts, num, 0, 0, 0); + fz_write_printf(ctx, opts->out, "startxref\n%lu\n%%%%EOF\n", startxref); } - fz_catch(ctx) fz_rethrow(ctx); - return xobj1; + fz_always(ctx) + { + pdf_drop_obj(ctx, dict); + pdf_drop_obj(ctx, w); + fz_drop_buffer(ctx, fzbuf); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + + doc->has_old_style_xrefs = 0; } -//----------------------------------------------------------------------------- -// Insert a buffer as a new separate /Contents object of a page. -//----------------------------------------------------------------------------- -void JM_insert_contents(fz_context *ctx, pdf_document *pdf, - pdf_obj *pageref, fz_buffer *newcont, int overlay) +static void +padto(fz_context *ctx, fz_output *out, int64_t target) { - fz_try(ctx) + int64_t pos = fz_tell_output(ctx, out); + + assert(pos <= target); + while (pos < target) { - pdf_obj *contents = pdf_dict_get(ctx, pageref, PDF_NAME_Contents); - pdf_obj *newconts = pdf_add_stream(ctx, pdf, newcont, NULL, 0); - if (pdf_is_array(ctx, contents)) - { - if (overlay) - pdf_array_push_drop(ctx, contents, newconts); - else - pdf_array_insert_drop(ctx, contents, newconts, 0); - } - else + fz_write_byte(ctx, out, '\n'); + pos++; + } +} + +static void +dowriteobject(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int num, int pass) +{ + pdf_xref_entry *entry = pdf_get_xref_entry(ctx, doc, num); + if (entry->type == 'f') + opts->gen_list[num] = entry->gen; + if (entry->type == 'n') + opts->gen_list[num] = entry->gen; + if (entry->type == 'o') + opts->gen_list[num] = 0; + + /* If we are renumbering, then make sure all generation numbers are + * zero (except object 0 which must be free, and have a gen number of + * 65535). Changing the generation numbers (and indeed object numbers) + * will break encryption - so only do this if we are renumbering + * anyway. */ + if (opts->do_garbage >= 2) + opts->gen_list[num] = (num == 0 ? 65535 : 0); + + if (opts->do_garbage && !opts->use_list[num]) + return; + + if (entry->type == 'n' || entry->type == 'o') + { + if (pass > 0) + padto(ctx, opts->out, opts->ofs_list[num]); + if (!opts->do_incremental || pdf_xref_is_incremental(ctx, doc, num)) { - pdf_obj *carr = pdf_new_array(ctx, pdf, 2); - if (overlay) - { - pdf_array_push(ctx, carr, contents); - pdf_array_push_drop(ctx, carr, newconts); - } - else - { - pdf_array_push_drop(ctx, carr, newconts); - pdf_array_push(ctx, carr, contents); - } - pdf_dict_put_drop(ctx, pageref, PDF_NAME_Contents, carr); + opts->ofs_list[num] = fz_tell_output(ctx, opts->out); + writeobject(ctx, doc, opts, num, opts->gen_list[num], 1, num == opts->crypt_object_number); } } - fz_catch(ctx) fz_rethrow(ctx); - return; + else + opts->use_list[num] = 0; } -/*----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -// Append / prepend a buffer to the /Contents of a page. -//----------------------------------------------------------------------------- -void JM_extend_contents(fz_context *ctx, pdf_document *pdfout, - pdf_obj *pageref, fz_buffer *newcont, int overlay) + +static void +writeobjects(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int pass) { - int i; - fz_buffer *oldcont = NULL, *endcont = NULL; - pdf_obj *contents = pdf_dict_get(ctx, pageref, PDF_NAME_Contents); - fz_try(ctx) + int num; + int xref_len = pdf_xref_len(ctx, doc); + + if (!opts->do_incremental) { - if (pdf_is_array(ctx, contents)) // multiple contents objects! - { // choose the correct one (first / last) - if (overlay == 1) i = pdf_array_len(ctx, contents) - 1; - else i = 0; - contents = pdf_array_get(ctx, contents, i); - } - oldcont = pdf_load_stream(ctx, contents); // old contents buffer + fz_write_printf(ctx, opts->out, "%%PDF-%d.%d\n", doc->version / 10, doc->version % 10); + fz_write_string(ctx, opts->out, "%\xC2\xB5\xC2\xB6\n\n"); + } - // allocate result buffer - endcont = fz_new_buffer(ctx, fz_buffer_storage(ctx, oldcont, NULL) + - fz_buffer_storage(ctx, newcont, NULL)); + dowriteobject(ctx, doc, opts, opts->start, pass); - if (overlay == 1) // append new buffer - { - fz_append_buffer(ctx, endcont, oldcont); - fz_append_buffer(ctx, endcont, newcont); - } - else // prepend new buffer - { - fz_append_buffer(ctx, endcont, newcont); - fz_append_buffer(ctx, endcont, oldcont); - } - fz_terminate_buffer(ctx, endcont); // finalize result buffer - - // now update the content stream - JM_update_stream(ctx, pdfout, contents, endcont); + if (opts->do_linear) + { + /* Write first xref */ + if (pass == 0) + opts->first_xref_offset = fz_tell_output(ctx, opts->out); + else + padto(ctx, opts->out, opts->first_xref_offset); + writexref(ctx, doc, opts, opts->start, pdf_xref_len(ctx, doc), 1, opts->main_xref_offset, 0); } - fz_always(ctx) + + for (num = opts->start+1; num < xref_len; num++) + dowriteobject(ctx, doc, opts, num, pass); + if (opts->do_linear && pass == 1) { - fz_drop_buffer(ctx, endcont); - fz_drop_buffer(ctx, oldcont); + int64_t offset = (opts->start == 1 ? opts->main_xref_offset : opts->ofs_list[1] + opts->hintstream_len); + padto(ctx, opts->out, offset); + } + for (num = 1; num < opts->start; num++) + { + if (pass == 1) + opts->ofs_list[num] += opts->hintstream_len; + dowriteobject(ctx, doc, opts, num, pass); } - fz_catch(ctx) fz_rethrow(ctx); - return; } ------------------------------------------------------------------------------*/ -//----------------------------------------------------------------------------- -// Create / check AP object for the annotation -// Always reset /BBOX to rect and /Matrix to the identity matrix. -//----------------------------------------------------------------------------- -void JM_make_ap_object(fz_context *ctx, fz_annot *fzannot, fz_rect *rect, char *c) + +static int +my_log2(int x) { - pdf_annot *annot = pdf_annot_from_fz_annot(ctx, fzannot); - pdf_document *pdf = annot->page->doc; - pdf_obj *xobj, *ca = NULL, *extgstate = NULL; - fz_buffer *contbuffer = NULL; - fz_try(ctx) + int i = 0; + + if (x <= 0) + return 0; + + while ((1< 0) + i++; + + if ((1<page_object_lists->page[0]; + int page_len_bits, shared_object_bits, shared_object_id_bits; + int shared_length_bits; + int xref_len = pdf_xref_len(ctx, doc); + + min_shared_object = pdf_xref_len(ctx, doc); + max_shared_object = 1; + min_shared_length = opts->file_len; + max_shared_length = 0; + for (i=1; i < xref_len; i++) { - xobj = pdf_dict_getl(ctx, annot->obj, PDF_NAME_AP, PDF_NAME_N, NULL); - if (xobj) + int min, max, page; + + min = opts->ofs_list[i]; + if (i == opts->start-1 || (opts->start == 1 && i == xref_len-1)) + max = opts->main_xref_offset; + else if (i == xref_len-1) + max = opts->ofs_list[1]; + else + max = opts->ofs_list[i+1]; + + assert(max > min); + + if (opts->use_list[i] & USE_SHARED) { - pdf_dict_put_rect(ctx, xobj, PDF_NAME_BBox, rect); - pdf_dict_put_matrix(ctx, xobj, PDF_NAME_Matrix, &fz_identity); - } + page = -1; + if (i < min_shared_object) + min_shared_object = i; + if (i > max_shared_object) + max_shared_object = i; + if (min_shared_length > max - min) + min_shared_length = max - min; + if (max_shared_length < max - min) + max_shared_length = max - min; + } + else if (opts->use_list[i] & (USE_CATALOGUE | USE_HINTS | USE_PARAMS)) + page = -1; + else if (opts->use_list[i] & USE_PAGE1) + { + page = 0; + if (min_shared_length > max - min) + min_shared_length = max - min; + if (max_shared_length < max - min) + max_shared_length = max - min; + } + else if (opts->use_list[i] == 0) + page = -1; else - xobj = pdf_new_xobject(ctx, pdf, rect, &fz_identity); + page = opts->use_list[i]>>USE_PAGE_SHIFT; - ca = pdf_dict_get(ctx, annot->obj, PDF_NAME_CA); - if (ca) + if (page >= 0) { - pdf_obj *fcao = pdf_new_real(ctx, pdf, pdf_to_real(ctx, ca)); - pdf_obj *alp0 = pdf_new_dict(ctx, pdf, 2); - pdf_dict_put(ctx, alp0, PDF_NAME_CA, fcao); - pdf_dict_put(ctx, alp0, PDF_NAME_ca, fcao); - pdf_obj *extg = pdf_new_dict(ctx, pdf, 1); - pdf_dict_puts_drop(ctx, extg, "Alp0", alp0); - pdf_dict_putl_drop(ctx, xobj, extg, PDF_NAME_Resources, PDF_NAME_ExtGState, NULL); + pop[page]->num_objects++; + if (pop[page]->min_ofs > min) + pop[page]->min_ofs = min; + if (pop[page]->max_ofs < max) + pop[page]->max_ofs = max; } + } - contbuffer = fz_new_buffer_from_copied_data(ctx, c, strlen(c)); - JM_update_stream(ctx, pdf, xobj, contbuffer); - pdf_dict_putl_drop(ctx, annot->obj, xobj, PDF_NAME_AP, PDF_NAME_N, NULL); - annot->ap = NULL; + min_objs_per_page = max_objs_per_page = pop[0]->num_objects; + min_page_length = max_page_length = pop[0]->max_ofs - pop[0]->min_ofs; + for (i=1; i < opts->page_count; i++) + { + int tmp; + if (min_objs_per_page > pop[i]->num_objects) + min_objs_per_page = pop[i]->num_objects; + if (max_objs_per_page < pop[i]->num_objects) + max_objs_per_page = pop[i]->num_objects; + tmp = pop[i]->max_ofs - pop[i]->min_ofs; + if (tmp < min_page_length) + min_page_length = tmp; + if (tmp > max_page_length) + max_page_length = tmp; + } + + for (i=0; i < opts->page_count; i++) + { + int count = 0; + page_objects *po = opts->page_object_lists->page[i]; + for (j = 0; j < po->len; j++) + { + if (i == 0 && opts->use_list[po->object[j]] & USE_PAGE1) + count++; + else if (i != 0 && opts->use_list[po->object[j]] & USE_SHARED) + count++; + } + po->num_shared = count; + if (i == 0 || count > max_shared_object_refs) + max_shared_object_refs = count; + } + if (min_shared_object > max_shared_object) + min_shared_object = max_shared_object = 0; + + /* Table F.3 - Header */ + /* Header Item 1: Least number of objects in a page */ + fz_append_bits(ctx, buf, min_objs_per_page, 32); + /* Header Item 2: Location of first pages page object */ + fz_append_bits(ctx, buf, opts->ofs_list[pop[0]->page_object_number], 32); + /* Header Item 3: Number of bits required to represent the difference + * between the greatest and least number of objects in a page. */ + objs_per_page_bits = my_log2(max_objs_per_page - min_objs_per_page); + fz_append_bits(ctx, buf, objs_per_page_bits, 16); + /* Header Item 4: Least length of a page. */ + fz_append_bits(ctx, buf, min_page_length, 32); + /* Header Item 5: Number of bits needed to represent the difference + * between the greatest and least length of a page. */ + page_len_bits = my_log2(max_page_length - min_page_length); + fz_append_bits(ctx, buf, page_len_bits, 16); + /* Header Item 6: Least offset to start of content stream (Acrobat + * sets this to always be 0) */ + fz_append_bits(ctx, buf, 0, 32); + /* Header Item 7: Number of bits needed to represent the difference + * between the greatest and least offset to content stream (Acrobat + * sets this to always be 0) */ + fz_append_bits(ctx, buf, 0, 16); + /* Header Item 8: Least content stream length. (Acrobat + * sets this to always be 0) */ + fz_append_bits(ctx, buf, 0, 32); + /* Header Item 9: Number of bits needed to represent the difference + * between the greatest and least content stream length (Acrobat + * sets this to always be the same as item 5) */ + fz_append_bits(ctx, buf, page_len_bits, 16); + /* Header Item 10: Number of bits needed to represent the greatest + * number of shared object references. */ + shared_object_bits = my_log2(max_shared_object_refs); + fz_append_bits(ctx, buf, shared_object_bits, 16); + /* Header Item 11: Number of bits needed to represent the greatest + * shared object identifier. */ + shared_object_id_bits = my_log2(max_shared_object - min_shared_object + pop[0]->num_shared); + fz_append_bits(ctx, buf, shared_object_id_bits, 16); + /* Header Item 12: Number of bits needed to represent the numerator + * of the fractions. We always send 0. */ + fz_append_bits(ctx, buf, 0, 16); + /* Header Item 13: Number of bits needed to represent the denominator + * of the fractions. We always send 0. */ + fz_append_bits(ctx, buf, 0, 16); + + /* Table F.4 - Page offset hint table (per page) */ + /* Item 1: A number that, when added to the least number of objects + * on a page, gives the number of objects in the page. */ + for (i = 0; i < opts->page_count; i++) + { + fz_append_bits(ctx, buf, pop[i]->num_objects - min_objs_per_page, objs_per_page_bits); } - fz_always(ctx) + fz_append_bits_pad(ctx, buf); + /* Item 2: A number that, when added to the least page length, gives + * the length of the page in bytes. */ + for (i = 0; i < opts->page_count; i++) { - fz_drop_buffer(ctx, contbuffer); + fz_append_bits(ctx, buf, pop[i]->max_ofs - pop[i]->min_ofs - min_page_length, page_len_bits); } - fz_catch(ctx) fz_rethrow(ctx); - return; -} - - - -//---------------------------------------------------------------------------- -// convert (char *) to ASCII-only (char*) (which must be freed!) -//---------------------------------------------------------------------------- -char *JM_ASCIIFromChar(char *in) -{ - if (!in) return NULL; - size_t i, j = strlen(in) + 1; - unsigned char *out = JM_Alloc(unsigned char, j); - if (!out) return NULL; - memcpy(out, in, j); - for (i = 0; i < j-1; i++) + fz_append_bits_pad(ctx, buf); + /* Item 3: The number of shared objects referenced from the page. */ + for (i = 0; i < opts->page_count; i++) { - if (out[i] > 126) + fz_append_bits(ctx, buf, pop[i]->num_shared, shared_object_bits); + } + fz_append_bits_pad(ctx, buf); + /* Item 4: Shared object id for each shared object ref in every page. + * Spec says "not for page 1", but acrobat does send page 1's - all + * as zeros. */ + for (i = 0; i < opts->page_count; i++) + { + for (j = 0; j < pop[i]->len; j++) { - out[i] = 63; - continue; - } - if (out[i] < 32) - out[i] = 32; + int o = pop[i]->object[j]; + if (i == 0 && opts->use_list[o] & USE_PAGE1) + fz_append_bits(ctx, buf, 0 /* o - pop[0]->page_object_number */, shared_object_id_bits); + if (i != 0 && opts->use_list[o] & USE_SHARED) + fz_append_bits(ctx, buf, o - min_shared_object + pop[0]->num_shared, shared_object_id_bits); + } + } + fz_append_bits_pad(ctx, buf); + /* Item 5: Numerator of fractional position for each shared object reference. */ + /* We always send 0 in 0 bits */ + /* Item 6: A number that, when added to the least offset to the start + * of the content stream (F.3 Item 6), gives the offset in bytes of + * start of the pages content stream object relative to the beginning + * of the page. Always 0 in 0 bits. */ + /* Item 7: A number that, when added to the least content stream length + * (F.3 Item 8), gives the length of the pages content stream object. + * Always == Item 2 as least content stream length = least page stream + * length. + */ + for (i = 0; i < opts->page_count; i++) + { + fz_append_bits(ctx, buf, pop[i]->max_ofs - pop[i]->min_ofs - min_page_length, page_len_bits); + } + + /* Pad, and then do shared object hint table */ + fz_append_bits_pad(ctx, buf); + opts->hints_shared_offset = (int)fz_buffer_storage(ctx, buf, NULL); + + /* Table F.5: */ + /* Header Item 1: Object number of the first object in the shared + * objects section. */ + fz_append_bits(ctx, buf, min_shared_object, 32); + /* Header Item 2: Location of first object in the shared objects + * section. */ + fz_append_bits(ctx, buf, opts->ofs_list[min_shared_object], 32); + /* Header Item 3: The number of shared object entries for the first + * page. */ + fz_append_bits(ctx, buf, pop[0]->num_shared, 32); + /* Header Item 4: The number of shared object entries for the shared + * objects section + first page. */ + fz_append_bits(ctx, buf, max_shared_object - min_shared_object + pop[0]->num_shared, 32); + /* Header Item 5: The number of bits needed to represent the greatest + * number of objects in a shared object group (Always 0). */ + fz_append_bits(ctx, buf, 0, 16); + /* Header Item 6: The least length of a shared object group in bytes. */ + fz_append_bits(ctx, buf, min_shared_length, 32); + /* Header Item 7: The number of bits required to represent the + * difference between the greatest and least length of a shared object + * group. */ + shared_length_bits = my_log2(max_shared_length - min_shared_length); + fz_append_bits(ctx, buf, shared_length_bits, 16); + + /* Table F.6 */ + /* Item 1: Shared object group length (page 1 objects) */ + for (j = 0; j < pop[0]->len; j++) + { + int o = pop[0]->object[j]; + int64_t min, max; + min = opts->ofs_list[o]; + if (o == opts->start-1) + max = opts->main_xref_offset; + else if (o < xref_len-1) + max = opts->ofs_list[o+1]; + else + max = opts->ofs_list[1]; + if (opts->use_list[o] & USE_PAGE1) + fz_append_bits(ctx, buf, max - min - min_shared_length, shared_length_bits); } - return (char *) out; -} + /* Item 1: Shared object group length (shared objects) */ + for (i = min_shared_object; i <= max_shared_object; i++) + { + int min, max; + min = opts->ofs_list[i]; + if (i == opts->start-1) + max = opts->main_xref_offset; + else if (i < xref_len-1) + max = opts->ofs_list[i+1]; + else + max = opts->ofs_list[1]; + fz_append_bits(ctx, buf, max - min - min_shared_length, shared_length_bits); + } + fz_append_bits_pad(ctx, buf); -//---------------------------------------------------------------------------- -// create an ASCII-only Python string of a (char*) -//---------------------------------------------------------------------------- -PyObject *JM_UnicodeFromASCII(const char *in) -{ - char *c = JM_ASCIIFromChar((char *) in); - PyObject *p = Py_BuildValue("s", c); - JM_Free(c); - return p; + /* Item 2: MD5 presence flags */ + for (i = max_shared_object - min_shared_object + pop[0]->num_shared; i > 0; i--) + { + fz_append_bits(ctx, buf, 0, 1); + } + fz_append_bits_pad(ctx, buf); + /* Item 3: MD5 sums (not present) */ + fz_append_bits_pad(ctx, buf); + /* Item 4: Number of objects in the group (not present) */ } -//----------------------------------------------------------------------------- -// Store info of a font in Python list -//----------------------------------------------------------------------------- -void JM_gather_fonts(fz_context *ctx, pdf_document *pdf, pdf_obj *dict, - PyObject *fontlist) +static void +make_hint_stream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) { - int i, n; - n = pdf_dict_len(ctx, dict); - for (i = 0; i < n; i++) - { - pdf_obj *fontdict = NULL; - pdf_obj *subtype = NULL; - pdf_obj *basefont = NULL; - pdf_obj *name = NULL; - pdf_obj *refname = NULL; - pdf_obj *encoding = NULL; + fz_buffer *buf = fz_new_buffer(ctx, 100); - fontdict = pdf_dict_get_val(ctx, dict, i); - if (!pdf_is_dict(ctx, fontdict)) - { - PySys_WriteStdout("warning: not a font dict (%d 0 R)", - pdf_to_num(ctx, fontdict)); - continue; - } - refname = pdf_dict_get_key(ctx, dict, i); - subtype = pdf_dict_get(ctx, fontdict, PDF_NAME_Subtype); - basefont = pdf_dict_get(ctx, fontdict, PDF_NAME_BaseFont); - if (!basefont || pdf_is_null(ctx, basefont)) - name = pdf_dict_get(ctx, fontdict, PDF_NAME_Name); - else - name = basefont; - encoding = pdf_dict_get(ctx, fontdict, PDF_NAME_Encoding); - if (pdf_is_dict(ctx, encoding)) - encoding = pdf_dict_get(ctx, encoding, PDF_NAME_BaseEncoding); - int xref = pdf_to_num(ctx, fontdict); - char *ext = "n/a"; - if (xref) ext = fontextension(ctx, pdf, xref); - PyObject *entry = PyList_New(0); - PyList_Append(entry, Py_BuildValue("i", xref)); - PyList_Append(entry, Py_BuildValue("s", ext)); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, subtype))); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, name))); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, refname))); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, encoding))); - PyList_Append(fontlist, entry); - Py_CLEAR(entry); + fz_try(ctx) + { + make_page_offset_hints(ctx, doc, opts, buf); + pdf_update_stream(ctx, doc, pdf_load_object(ctx, doc, pdf_xref_len(ctx, doc)-1), buf, 0); + opts->hintstream_len = (int)fz_buffer_storage(ctx, buf, NULL); + fz_drop_buffer(ctx, buf); + } + fz_catch(ctx) + { + fz_drop_buffer(ctx, buf); + fz_rethrow(ctx); } } -//----------------------------------------------------------------------------- -// Store info of an image in Python list -//----------------------------------------------------------------------------- -void JM_gather_images(fz_context *ctx, pdf_document *doc, pdf_obj *dict, - PyObject *imagelist) +static void presize_unsaved_signature_byteranges(fz_context *ctx, pdf_document *doc) { - int i, n; - n = pdf_dict_len(ctx, dict); - for (i = 0; i < n; i++) + int s; + + for (s = 0; s < doc->num_incremental_sections; s++) { - pdf_obj *imagedict, *smask; - pdf_obj *refname = NULL; - pdf_obj *type; - pdf_obj *width; - pdf_obj *height; - pdf_obj *bpc = NULL; - pdf_obj *filter = NULL; - pdf_obj *cs = NULL; - pdf_obj *altcs; + pdf_xref *xref = &doc->xref_sections[s]; - imagedict = pdf_dict_get_val(ctx, dict, i); - if (!pdf_is_dict(ctx, imagedict)) + if (xref->unsaved_sigs) { - PySys_WriteStdout("warning: not an image dict (%d 0 R)", - pdf_to_num(ctx, imagedict)); - continue; - } - refname = pdf_dict_get_key(ctx, dict, i); + /* The ByteRange objects of signatures are initially written out with + * dummy values, and then overwritten later. We need to make sure their + * initial form at least takes enough sufficient file space */ + pdf_unsaved_sig *usig; + int n = 0; - type = pdf_dict_get(ctx, imagedict, PDF_NAME_Subtype); - if (!pdf_name_eq(ctx, type, PDF_NAME_Image)) - continue; - - int xref = pdf_to_num(ctx, imagedict); - int gen = 0; - smask = pdf_dict_get(ctx, imagedict, PDF_NAME_SMask); - if (smask) - gen = pdf_to_num(ctx, smask); - filter = pdf_dict_get(ctx, imagedict, PDF_NAME_Filter); + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + n++; - altcs = NULL; - cs = pdf_dict_get(ctx, imagedict, PDF_NAME_ColorSpace); - if (pdf_is_array(ctx, cs)) - { - pdf_obj *cses = cs; - cs = pdf_array_get(ctx, cses, 0); - if (pdf_name_eq(ctx, cs, PDF_NAME_DeviceN) || - pdf_name_eq(ctx, cs, PDF_NAME_Separation)) + for (usig = xref->unsaved_sigs; usig; usig = usig->next) { - altcs = pdf_array_get(ctx, cses, 2); - if (pdf_is_array(ctx, altcs)) - altcs = pdf_array_get(ctx, altcs, 0); + /* There will be segments of bytes at the beginning, at + * the end and between each consecutive pair of signatures, + * hence n + 1 */ + int i; + pdf_obj *byte_range = pdf_dict_getl(ctx, usig->field, PDF_NAME(V), PDF_NAME(ByteRange), NULL); + + for (i = 0; i < n+1; i++) + { + pdf_array_push_int(ctx, byte_range, INT_MAX); + pdf_array_push_int(ctx, byte_range, INT_MAX); + } } } - - width = pdf_dict_get(ctx, imagedict, PDF_NAME_Width); - height = pdf_dict_get(ctx, imagedict, PDF_NAME_Height); - bpc = pdf_dict_get(ctx, imagedict, PDF_NAME_BitsPerComponent); - - PyObject *entry = PyList_New(0); - PyList_Append(entry, Py_BuildValue("i", xref)); - PyList_Append(entry, Py_BuildValue("i", gen)); - PyList_Append(entry, Py_BuildValue("i", pdf_to_int(ctx, width))); - PyList_Append(entry, Py_BuildValue("i", pdf_to_int(ctx, height))); - PyList_Append(entry, Py_BuildValue("i", pdf_to_int(ctx, bpc))); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, cs))); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, altcs))); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, refname))); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, filter))); - PyList_Append(imagelist, entry); - Py_CLEAR(entry); } } -//----------------------------------------------------------------------------- -// Store info of a /Form in Python list -//----------------------------------------------------------------------------- -void JM_gather_forms(fz_context *ctx, pdf_document *doc, pdf_obj *dict, - PyObject *imagelist) +static void complete_signatures(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) { - int i, n; - n = pdf_dict_len(ctx, dict); - for (i = 0; i < n; i++) - { - pdf_obj *imagedict; - pdf_obj *refname = NULL; - pdf_obj *type; + pdf_unsaved_sig *usig; + char *buf = NULL; + int buf_size; + int s; + int i; + int last_end; + fz_stream *stm = NULL; + fz_var(stm); + fz_var(buf); - imagedict = pdf_dict_get_val(ctx, dict, i); - if (!pdf_is_dict(ctx, imagedict)) + fz_try(ctx) + { + for (s = 0; s < doc->num_incremental_sections; s++) { - PySys_WriteStdout("warning: not a form dict (%d 0 R)", - pdf_to_num(ctx, imagedict)); - continue; - } - refname = pdf_dict_get_key(ctx, dict, i); + pdf_xref *xref = &doc->xref_sections[doc->num_incremental_sections - s - 1]; - type = pdf_dict_get(ctx, imagedict, PDF_NAME_Subtype); - if (!pdf_name_eq(ctx, type, PDF_NAME_Form)) - continue; + if (xref->unsaved_sigs) + { + pdf_obj *byte_range; + buf_size = 0; - int xref = pdf_to_num(ctx, imagedict); + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + int size = usig->signer->max_digest_size(usig->signer); - PyObject *entry = PyList_New(0); - PyList_Append(entry, Py_BuildValue("i", xref)); - PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, refname))); - PyList_Append(imagelist, entry); - Py_CLEAR(entry); + buf_size = fz_maxi(buf_size, size); + } + + buf_size = buf_size * 2 + SIG_EXTRAS_SIZE; + + buf = fz_calloc(ctx, buf_size, 1); + + stm = fz_stream_from_output(ctx, opts->out); + /* Locate the byte ranges and contents in the saved file */ + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + char *bstr, *cstr, *fstr; + int pnum = pdf_obj_parent_num(ctx, pdf_dict_getl(ctx, usig->field, PDF_NAME(V), PDF_NAME(ByteRange), NULL)); + fz_seek(ctx, stm, opts->ofs_list[pnum], SEEK_SET); + (void)fz_read(ctx, stm, (unsigned char *)buf, buf_size); + buf[buf_size-1] = 0; + + bstr = strstr(buf, "/ByteRange"); + cstr = strstr(buf, "/Contents"); + fstr = strstr(buf, "/Filter"); + + if (bstr && cstr && fstr && bstr < cstr && cstr < fstr) + { + usig->byte_range_start = bstr - buf + 10 + opts->ofs_list[pnum]; + usig->byte_range_end = cstr - buf + opts->ofs_list[pnum]; + usig->contents_start = cstr - buf + 9 + opts->ofs_list[pnum]; + usig->contents_end = fstr - buf + opts->ofs_list[pnum]; + } + } + + fz_drop_stream(ctx, stm); + stm = NULL; + + /* Recreate ByteRange with correct values. Initially store the + * recreated object in the first of the unsaved signatures */ + byte_range = pdf_new_array(ctx, doc, 4); + pdf_dict_putl_drop(ctx, xref->unsaved_sigs->field, byte_range, PDF_NAME(V), PDF_NAME(ByteRange), NULL); + + last_end = 0; + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + pdf_array_push_int(ctx, byte_range, last_end); + pdf_array_push_int(ctx, byte_range, usig->contents_start - last_end); + last_end = usig->contents_end; + } + pdf_array_push_int(ctx, byte_range, last_end); + pdf_array_push_int(ctx, byte_range, xref->end_ofs - last_end); + + /* Copy the new ByteRange to the other unsaved signatures */ + for (usig = xref->unsaved_sigs->next; usig; usig = usig->next) + pdf_dict_putl_drop(ctx, usig->field, pdf_copy_array(ctx, byte_range), PDF_NAME(V), PDF_NAME(ByteRange), NULL); + + /* Write the byte range into buf, padding with spaces*/ + i = pdf_sprint_obj(ctx, buf, buf_size, byte_range, 1); + memset(buf+i, ' ', buf_size-i); + + /* Write the byte range to the file */ + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + fz_seek_output(ctx, opts->out, usig->byte_range_start, SEEK_SET); + fz_write_data(ctx, opts->out, buf, usig->byte_range_end - usig->byte_range_start); + } + + /* Write the digests into the file */ + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + pdf_write_digest(ctx, opts->out, byte_range, usig->contents_start, usig->contents_end - usig->contents_start, usig->signer); + + /* delete the unsaved_sigs records */ + while ((usig = xref->unsaved_sigs) != NULL) + { + xref->unsaved_sigs = usig->next; + pdf_drop_obj(ctx, usig->field); + usig->signer->drop(usig->signer); + fz_free(ctx, usig); + } + + xref->unsaved_sigs_end = NULL; + + fz_free(ctx, buf); + buf = NULL; + } + } + } + fz_catch(ctx) + { + fz_drop_stream(ctx, stm); + fz_free(ctx, buf); + fz_rethrow(ctx); } } -//----------------------------------------------------------------------------- -// Step through /Resources, looking up image or font information -//----------------------------------------------------------------------------- -void JM_scan_resources(fz_context *ctx, pdf_document *pdf, pdf_obj *rsrc, - PyObject *liste, int what) +static void clean_content_streams(fz_context *ctx, pdf_document *doc, int sanitize, int ascii) { - pdf_obj *font, *xobj, *subrsrc; - int i, n; - if (pdf_mark_obj(ctx, rsrc)) return; // stop on cylic dependencies - fz_try(ctx) + int n = pdf_count_pages(ctx, doc); + int i; + + for (i = 0; i < n; i++) { - if (what == 1) // look up fonts + pdf_annot *annot; + pdf_page *page = pdf_load_page(ctx, doc, i); + pdf_clean_page_contents(ctx, doc, page, NULL, NULL, NULL, sanitize, ascii); + + for (annot = pdf_first_annot(ctx, page); annot != NULL; annot = pdf_next_annot(ctx, annot)) { - font = pdf_dict_get(ctx, rsrc, PDF_NAME_Font); - JM_gather_fonts(ctx, pdf, font, liste); - n = pdf_dict_len(ctx, font); - for (i = 0; i < n; i++) - { - pdf_obj *obj = pdf_dict_get_val(ctx, font, i); - subrsrc = pdf_dict_get(ctx, obj, PDF_NAME_Resources); - if (subrsrc) - JM_scan_resources(ctx, pdf, subrsrc, liste, what); - } + pdf_clean_annot_contents(ctx, doc, annot, NULL, NULL, NULL, sanitize, ascii); } - xobj = pdf_dict_get(ctx, rsrc, PDF_NAME_XObject); + fz_drop_page(ctx, &page->super); + } +} - if (what == 2) // look up images +/* Initialise the pdf_write_state, used dynamically during the write, from the static + * pdf_write_options, passed into pdf_save_document */ +static void initialise_write_state(fz_context *ctx, pdf_document *doc, const pdf_write_options *in_opts, pdf_write_state *opts) +{ + int xref_len = pdf_xref_len(ctx, doc); + + opts->do_incremental = in_opts->do_incremental; + opts->do_ascii = in_opts->do_ascii; + opts->do_tight = !in_opts->do_pretty; + opts->do_expand = in_opts->do_decompress; + opts->do_compress = in_opts->do_compress; + opts->do_compress_images = in_opts->do_compress_images; + opts->do_compress_fonts = in_opts->do_compress_fonts; + + opts->do_garbage = in_opts->do_garbage; + opts->do_linear = in_opts->do_linear; + opts->do_clean = in_opts->do_clean; + opts->start = 0; + opts->main_xref_offset = INT_MIN; + + /* We deliberately make these arrays long enough to cope with + * 1 to n access rather than 0..n-1, and add space for 2 new + * extra entries that may be required for linearization. */ + opts->list_len = 0; + opts->use_list = NULL; + opts->ofs_list = NULL; + opts->gen_list = NULL; + opts->renumber_map = NULL; + opts->rev_renumber_map = NULL; + opts->continue_on_error = in_opts->continue_on_error; + opts->errors = in_opts->errors; + + expand_lists(ctx, opts, xref_len); +} + +/* Free the resources held by the dynamic write options */ +static void finalise_write_state(fz_context *ctx, pdf_write_state *opts) +{ + fz_free(ctx, opts->use_list); + fz_free(ctx, opts->ofs_list); + fz_free(ctx, opts->gen_list); + fz_free(ctx, opts->renumber_map); + fz_free(ctx, opts->rev_renumber_map); + pdf_drop_obj(ctx, opts->linear_l); + pdf_drop_obj(ctx, opts->linear_h0); + pdf_drop_obj(ctx, opts->linear_h1); + pdf_drop_obj(ctx, opts->linear_o); + pdf_drop_obj(ctx, opts->linear_e); + pdf_drop_obj(ctx, opts->linear_n); + pdf_drop_obj(ctx, opts->linear_t); + pdf_drop_obj(ctx, opts->hints_s); + pdf_drop_obj(ctx, opts->hints_length); + page_objects_list_destroy(ctx, opts->page_object_lists); +} + +static void +prepare_for_save(fz_context *ctx, pdf_document *doc, pdf_write_options *in_opts) +{ + doc->freeze_updates = 1; + + /* Rewrite (and possibly sanitize) the operator streams */ + if (in_opts->do_clean || in_opts->do_sanitize) + clean_content_streams(ctx, doc, in_opts->do_sanitize, in_opts->do_ascii); + + pdf_finish_edit(ctx, doc); + presize_unsaved_signature_byteranges(ctx, doc); +} + +static void +change_identity(fz_context *ctx, pdf_document *doc) +{ + pdf_obj *identity = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(ID)); + pdf_obj *str; + unsigned char rnd[16]; + + if (pdf_array_len(ctx, identity) < 2) + return; + + /* Maybe recalculate this in future. For now, just change the second one. */ + fz_memrnd(ctx, rnd, 16); + str = pdf_new_string(ctx, (char *)rnd, 16); + pdf_array_put_drop(ctx, identity, 1, str); + +} + +static void +do_pdf_save_document(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_write_options *in_opts) +{ + int lastfree; + int num; + int xref_len; + + if (in_opts->do_incremental) + { + /* If no changes, nothing to write */ + if (doc->num_incremental_sections == 0) + return; + if (opts->out) { - JM_gather_images(ctx, pdf, xobj, liste); + fz_seek_output(ctx, opts->out, 0, SEEK_END); + fz_write_string(ctx, opts->out, "\n"); } + } - if (what == 3) // look up forms + xref_len = pdf_xref_len(ctx, doc); + + fz_try(ctx) + { + initialise_write_state(ctx, doc, in_opts, opts); + + /* Make sure any objects hidden in compressed streams have been loaded */ + if (!opts->do_incremental) { - JM_gather_forms(ctx, pdf, xobj, liste); + pdf_ensure_solid_xref(ctx, doc, xref_len); + preloadobjstms(ctx, doc); + change_identity(ctx, doc); + xref_len = pdf_xref_len(ctx, doc); /* May have changed due to repair */ + expand_lists(ctx, opts, xref_len); } - n = pdf_dict_len(ctx, xobj); - for (i = 0; i < n; i++) + /* Sweep & mark objects from the trailer */ + if (opts->do_garbage >= 1 || opts->do_linear) + (void)markobj(ctx, doc, opts, pdf_trailer(ctx, doc)); + else { - pdf_obj *obj = pdf_dict_get_val(ctx, xobj, i); - subrsrc = pdf_dict_get(ctx, obj, PDF_NAME_Resources); - if (subrsrc) - JM_scan_resources(ctx, pdf, subrsrc, liste, what); + xref_len = pdf_xref_len(ctx, doc); /* May have changed due to repair */ + expand_lists(ctx, opts, xref_len); + for (num = 0; num < xref_len; num++) + opts->use_list[num] = 1; } - } - fz_always(ctx) pdf_unmark_obj(ctx, rsrc); - fz_catch(ctx) fz_rethrow(ctx); -} + /* Coalesce and renumber duplicate objects */ + if (opts->do_garbage >= 3) + removeduplicateobjs(ctx, doc, opts); + /* Compact xref by renumbering and removing unused objects */ + if (opts->do_garbage >= 2 || opts->do_linear) + compactxref(ctx, doc, opts); -//----------------------------------------------------------------------------- -// Convert any MuPDF document to a PDF -// Returns bytes object containing the PDF, created via 'write' function. -//----------------------------------------------------------------------------- -PyObject *JM_convert_to_pdf(fz_context *ctx, fz_document *doc, int fp, int tp, int rotate) -{ - pdf_document *pdfout = pdf_create_document(ctx); // new PDF document - int i, incr = 1, s = fp, e = tp; - if (fp > tp) // revert page sequence - { - incr = -1; // count backwards - s = tp; // adjust ... - e = fp; // ... range - } - fz_rect mediabox; - fz_device *dev = NULL; - fz_buffer *contents = NULL; - pdf_obj *resources = NULL; - fz_page *page; - fz_var(dev); - fz_var(contents); - fz_var(resources); - fz_var(page); - for (i = fp; INRANGE(i, s, e); i += incr) - { // interpret & write document pages as PDF pages - fz_try(ctx) + opts->crypt_object_number = 0; + + if (doc->crypt) { - page = fz_load_page(ctx, doc, i); - fz_bound_page(ctx, page, &mediabox); - dev = pdf_page_write(ctx, pdfout, &mediabox, &resources, &contents); - fz_run_page(ctx, page, dev, &fz_identity, NULL); - fz_close_device(ctx, dev); - fz_drop_device(ctx, dev); - dev = NULL; - pdf_obj *page_obj = pdf_add_page(ctx, pdfout, &mediabox, rotate, resources, contents); - pdf_insert_page(ctx, pdfout, -1, page_obj); - pdf_drop_obj(ctx, page_obj); + pdf_obj *crypt = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Encrypt)); + int crypt_num = pdf_to_num(ctx, crypt); + opts->crypt_object_number = opts->renumber_map[crypt_num]; } - fz_always(ctx) + + /* Make renumbering affect all indirect references and update xref */ + if (opts->do_garbage >= 2 || opts->do_linear) + renumberobjs(ctx, doc, opts); + + /* Truncate the xref after compacting and renumbering */ + if ((opts->do_garbage >= 2 || opts->do_linear) && !opts->do_incremental) { - pdf_drop_obj(ctx, resources); - fz_drop_buffer(ctx, contents); - fz_drop_device(ctx, dev); + xref_len = pdf_xref_len(ctx, doc); /* May have changed due to repair */ + expand_lists(ctx, opts, xref_len); + while (xref_len > 0 && !opts->use_list[xref_len-1]) + xref_len--; } - fz_catch(ctx) + + if (opts->do_linear) + linearize(ctx, doc, opts); + + if (opts->do_incremental) { - fz_drop_page(ctx, page); - fz_rethrow(ctx); + int i; + + doc->disallow_new_increments = 1; + + for (i = 0; i < doc->num_incremental_sections; i++) + { + doc->xref_base = doc->num_incremental_sections - i - 1; + + writeobjects(ctx, doc, opts, 0); + + for (num = 0; num < xref_len; num++) + { + if (!opts->use_list[num] && pdf_xref_is_incremental(ctx, doc, num)) + { + /* Make unreusable. FIXME: would be better to link to existing free list */ + opts->gen_list[num] = 65535; + opts->ofs_list[num] = 0; + } + } + + opts->first_xref_offset = fz_tell_output(ctx, opts->out); + if (doc->has_xref_streams) + writexrefstream(ctx, doc, opts, 0, xref_len, 1, 0, opts->first_xref_offset); + else + writexref(ctx, doc, opts, 0, xref_len, 1, 0, opts->first_xref_offset); + + doc->xref_sections[doc->xref_base].end_ofs = fz_tell_output(ctx, opts->out); + } + + doc->xref_base = 0; + doc->disallow_new_increments = 0; + } + else + { + writeobjects(ctx, doc, opts, 0); + + /* Construct linked list of free object slots */ + lastfree = 0; + for (num = 0; num < xref_len; num++) + { + if (!opts->use_list[num]) + { + opts->gen_list[num]++; + opts->ofs_list[lastfree] = num; + lastfree = num; + } + } + + if (opts->do_linear && opts->page_count > 0) + { + opts->main_xref_offset = fz_tell_output(ctx, opts->out); + writexref(ctx, doc, opts, 0, opts->start, 0, 0, opts->first_xref_offset); + opts->file_len = fz_tell_output(ctx, opts->out); + + make_hint_stream(ctx, doc, opts); + if (opts->do_ascii) + { + opts->hintstream_len *= 2; + opts->hintstream_len += 1 + ((opts->hintstream_len+63)>>6); + } + opts->file_len += opts->hintstream_len; + opts->main_xref_offset += opts->hintstream_len; + update_linearization_params(ctx, doc, opts); + fz_seek_output(ctx, opts->out, 0, 0); + writeobjects(ctx, doc, opts, 1); + + padto(ctx, opts->out, opts->main_xref_offset); + writexref(ctx, doc, opts, 0, opts->start, 0, 0, opts->first_xref_offset); + } + else + { + opts->first_xref_offset = fz_tell_output(ctx, opts->out); + writexref(ctx, doc, opts, 0, xref_len, 1, 0, opts->first_xref_offset); + } + + doc->xref_sections[0].end_ofs = fz_tell_output(ctx, opts->out); } + + complete_signatures(ctx, doc, opts); + + doc->dirty = 0; } - // PDF created - now write it to Python bytes - PyObject *r; - fz_buffer *res = NULL; - fz_output *out = NULL; - // prepare write options structure - int errors = 0; - pdf_write_options opts = { 0 }; - opts.do_garbage = 4; - opts.do_compress = 1; - opts.do_compress_images = 1; - opts.do_compress_fonts = 1; - opts.do_sanitize = 1; - opts.do_incremental = 0; - opts.do_ascii = 0; - opts.do_decompress = 0; - opts.do_linear = 0; - opts.do_clean = 1; - opts.do_pretty = 0; - opts.continue_on_error = 1; - opts.errors = &errors; - fz_try(ctx) + fz_always(ctx) { - res = fz_new_buffer(ctx, 1024); - out = fz_new_output_with_buffer(ctx, res); - pdf_write_document(ctx, pdfout, out, &opts); - r = JM_BinFromBuffer(ctx, res); + finalise_write_state(ctx, opts); + + doc->freeze_updates = 0; } - fz_always(ctx) + fz_catch(ctx) { - fz_drop_output(ctx, out); - fz_drop_buffer(ctx, res); + fz_rethrow(ctx); } - fz_catch(ctx) fz_rethrow(ctx); - return r; } -SWIGINTERN void delete_fz_document_s(struct fz_document_s *self){ - DEBUGMSG1("document w/o close"); - fz_drop_document(gctx, self); - DEBUGMSG2; - } - -SWIGINTERN swig_type_info* -SWIG_pchar_descriptor(void) +void JM_write_document(fz_context *ctx, pdf_document *doc, fz_output *out, pdf_write_options *in_opts, int decrypt) { - static int init = 0; - static swig_type_info* info = 0; - if (!init) { - info = SWIG_TypeQuery("_p_char"); - init = 1; - } - return info; -} + if (decrypt == 0) + { + pdf_write_document(ctx, doc, out, in_opts); + return; + } + pdf_write_options opts_defaults = { 0 }; + pdf_write_state opts = { 0 }; + + if (!doc) + return; + if (!in_opts) + in_opts = &opts_defaults; -SWIGINTERN int -SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) + if (in_opts->do_incremental && doc->repair_attempted) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes on a repaired file"); + if (in_opts->do_incremental && in_opts->do_garbage) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes with garbage collection"); + if (in_opts->do_incremental && in_opts->do_linear) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes with linearisation"); + if (pdf_has_unsaved_sigs(ctx, doc) && !out->as_stream) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't write pdf that has unsaved sigs to a fz_output unless it supports fz_stream_from_output!"); + + prepare_for_save(ctx, doc, in_opts); + + opts.out = out; + + do_pdf_save_document(ctx, doc, &opts, in_opts); +} + +void JM_save_document(fz_context *ctx, pdf_document *doc, const char *filename, pdf_write_options *in_opts, int decrypt) { -#if PY_VERSION_HEX>=0x03000000 -#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) - if (PyBytes_Check(obj)) -#else - if (PyUnicode_Check(obj)) -#endif -#else - if (PyString_Check(obj)) -#endif - { - char *cstr; Py_ssize_t len; -#if PY_VERSION_HEX>=0x03000000 -#if !defined(SWIG_PYTHON_STRICT_BYTE_CHAR) - if (!alloc && cptr) { - /* We can't allow converting without allocation, since the internal - representation of string in Python 3 is UCS-2/UCS-4 but we require - a UTF-8 representation. - TODO(bhy) More detailed explanation */ - return SWIG_RuntimeError; + if (decrypt == 0) + { + pdf_save_document(ctx, doc, filename, in_opts); + return; } - obj = PyUnicode_AsUTF8String(obj); - if(alloc) *alloc = SWIG_NEWOBJ; -#endif - PyBytes_AsStringAndSize(obj, &cstr, &len); -#else - PyString_AsStringAndSize(obj, &cstr, &len); -#endif - if (cptr) { - if (alloc) { - /* - In python the user should not be able to modify the inner - string representation. To warranty that, if you define - SWIG_PYTHON_SAFE_CSTRINGS, a new/copy of the python string - buffer is always returned. + pdf_write_options opts_defaults = { 0 }; + pdf_write_state opts = { 0 }; - The default behavior is just to return the pointer value, - so, be careful. - */ -#if defined(SWIG_PYTHON_SAFE_CSTRINGS) - if (*alloc != SWIG_OLDOBJ) -#else - if (*alloc == SWIG_NEWOBJ) -#endif - { - *cptr = (char *)memcpy(malloc((len + 1)*sizeof(char)), cstr, sizeof(char)*(len + 1)); - *alloc = SWIG_NEWOBJ; - } else { - *cptr = cstr; - *alloc = SWIG_OLDOBJ; - } - } else { -#if PY_VERSION_HEX>=0x03000000 -#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) - *cptr = PyBytes_AsString(obj); -#else - assert(0); /* Should never reach here with Unicode strings in Python 3 */ -#endif -#else - *cptr = SWIG_Python_str_AsChar(obj); -#endif - } - } - if (psize) *psize = len + 1; -#if PY_VERSION_HEX>=0x03000000 && !defined(SWIG_PYTHON_STRICT_BYTE_CHAR) - Py_XDECREF(obj); -#endif - return SWIG_OK; - } else { -#if defined(SWIG_PYTHON_2_UNICODE) -#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) -#error "Cannot use both SWIG_PYTHON_2_UNICODE and SWIG_PYTHON_STRICT_BYTE_CHAR at once" -#endif -#if PY_VERSION_HEX<0x03000000 - if (PyUnicode_Check(obj)) { - char *cstr; Py_ssize_t len; - if (!alloc && cptr) { - return SWIG_RuntimeError; - } - obj = PyUnicode_AsUTF8String(obj); - if (PyString_AsStringAndSize(obj, &cstr, &len) != -1) { - if (cptr) { - if (alloc) *alloc = SWIG_NEWOBJ; - *cptr = (char *)memcpy(malloc((len + 1)*sizeof(char)), cstr, sizeof(char)*(len + 1)); - } - if (psize) *psize = len + 1; + if (!doc) + return; - Py_XDECREF(obj); - return SWIG_OK; - } else { - Py_XDECREF(obj); - } - } -#endif -#endif + if (!in_opts) + in_opts = &opts_defaults; - swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); - if (pchar_descriptor) { - void* vptr = 0; - if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) { - if (cptr) *cptr = (char *) vptr; - if (psize) *psize = vptr ? (strlen((char *)vptr) + 1) : 0; - if (alloc) *alloc = SWIG_OLDOBJ; - return SWIG_OK; - } - } - } - return SWIG_TypeError; -} + if (in_opts->do_incremental && !doc->file) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes on a new document"); + if (in_opts->do_incremental && doc->repair_attempted) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes on a repaired file"); + if (in_opts->do_incremental && in_opts->do_garbage) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes with garbage collection"); + if (in_opts->do_incremental && in_opts->do_linear) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes with linearisation"); + prepare_for_save(ctx, doc, in_opts); + if (in_opts->do_incremental) + { + /* If no changes, nothing to write */ + if (doc->num_incremental_sections == 0) + return; + opts.out = fz_new_output_with_path(ctx, filename, 1); + } + else + { + opts.out = fz_new_output_with_path(ctx, filename, 0); + } + fz_try(ctx) + { + do_pdf_save_document(ctx, doc, &opts, in_opts); + fz_close_output(ctx, opts.out); + } + fz_always(ctx) + { + fz_drop_output(ctx, opts.out); + opts.out = NULL; + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } +} +//---------------------------------------------------------------------------- +// annotation types +//---------------------------------------------------------------------------- +#define ANNOT_TEXT 0 +#define ANNOT_LINK 1 +#define ANNOT_FREETEXT 2 +#define ANNOT_LINE 3 +#define ANNOT_SQUARE 4 +#define ANNOT_CIRCLE 5 +#define ANNOT_POLYGON 6 +#define ANNOT_POLYLINE 7 +#define ANNOT_HIGHLIGHT 8 +#define ANNOT_UNDERLINE 9 +#define ANNOT_SQUIGGLY 10 +#define ANNOT_STRIKEOUT 11 +#define ANNOT_STAMP 12 +#define ANNOT_CARET 13 +#define ANNOT_INK 14 +#define ANNOT_POPUP 15 +#define ANNOT_FILEATTACHMENT 16 +#define ANNOT_SOUND 17 +#define ANNOT_MOVIE 18 +#define ANNOT_WIDGET 19 +#define ANNOT_SCREEN 20 +#define ANNOT_PRINTERMARK 21 +#define ANNOT_TRAPNET 22 +#define ANNOT_WATERMARK 23 +#define ANNOT_3D 24 -#include +//---------------------------------------------------------------------------- +// annotation flag bits +//---------------------------------------------------------------------------- +#define ANNOT_XF_Invisible 1 << (1-1) +#define ANNOT_XF_Hidden 1 << (2-1) +#define ANNOT_XF_Print 1 << (3-1) +#define ANNOT_XF_NoZoom 1 << (4-1) +#define ANNOT_XF_NoRotate 1 << (5-1) +#define ANNOT_XF_NoView 1 << (6-1) +#define ANNOT_XF_ReadOnly 1 << (7-1) +#define ANNOT_XF_Locked 1 << (8-1) +#define ANNOT_XF_ToggleNoView 1 << (9-1) +#define ANNOT_XF_LockedContents 1 << (10-1) +//---------------------------------------------------------------------------- +// annotation line ending styles +//---------------------------------------------------------------------------- +#define ANNOT_LE_None 0 +#define ANNOT_LE_Square 1 +#define ANNOT_LE_Circle 2 +#define ANNOT_LE_Diamond 3 +#define ANNOT_LE_OpenArrow 4 +#define ANNOT_LE_ClosedArrow 5 +#define ANNOT_LE_Butt 6 +#define ANNOT_LE_ROpenArrow 7 +#define ANNOT_LE_RClosedArrow 8 +#define ANNOT_LE_Slash 9 -#include +//---------------------------------------------------------------------------- +// annotation field (widget) types +//---------------------------------------------------------------------------- +#define ANNOT_WG_NOT_WIDGET -1 +#define ANNOT_WG_PUSHBUTTON 0 +#define ANNOT_WG_CHECKBOX 1 +#define ANNOT_WG_RADIOBUTTON 2 +#define ANNOT_WG_TEXT 3 +#define ANNOT_WG_LISTBOX 4 +#define ANNOT_WG_COMBOBOX 5 +#define ANNOT_WG_SIGNATURE 6 +//---------------------------------------------------------------------------- +// annotation text widget subtypes +//---------------------------------------------------------------------------- +#define ANNOT_WG_TEXT_UNRESTRAINED 0 +#define ANNOT_WG_TEXT_NUMBER 1 +#define ANNOT_WG_TEXT_SPECIAL 2 +#define ANNOT_WG_TEXT_DATE 3 +#define ANNOT_WG_TEXT_TIME 4 -/* Getting isfinite working pre C99 across multiple platforms is non-trivial. Users can provide SWIG_isfinite on older platforms. */ -#ifndef SWIG_isfinite -/* isfinite() is a macro for C99 */ -# if defined(isfinite) -# define SWIG_isfinite(X) (isfinite(X)) -# elif defined __cplusplus && __cplusplus >= 201103L -/* Use a template so that this works whether isfinite() is std::isfinite() or - * in the global namespace. The reality seems to vary between compiler - * versions. - * - * Make sure namespace std exists to avoid compiler warnings. - * - * extern "C++" is required as this fragment can end up inside an extern "C" { } block - */ -namespace std { } -extern "C++" template -inline int SWIG_isfinite_func(T x) { - using namespace std; - return isfinite(x); -} -# define SWIG_isfinite(X) (SWIG_isfinite_func(X)) -# elif defined(_MSC_VER) -# define SWIG_isfinite(X) (_finite(X)) -# elif defined(__sun) && defined(__SVR4) -# include -# define SWIG_isfinite(X) (finite(X)) -# endif -#endif +//---------------------------------------------------------------------------- +// annotation widget flags +//---------------------------------------------------------------------------- +// Common to all field types +#define WIDGET_Ff_ReadOnly 1 +#define WIDGET_Ff_Required 2 +#define WIDGET_Ff_NoExport 4 +// Text fields +#define WIDGET_Ff_Multiline 4096 +#define WIDGET_Ff_Password 8192 -/* Accept infinite as a valid float value unless we are unable to check if a value is finite */ -#ifdef SWIG_isfinite -# define SWIG_Float_Overflow_Check(X) ((X < -FLT_MAX || X > FLT_MAX) && SWIG_isfinite(X)) -#else -# define SWIG_Float_Overflow_Check(X) ((X < -FLT_MAX || X > FLT_MAX)) -#endif +#define WIDGET_Ff_FileSelect 1048576 +#define WIDGET_Ff_DoNotSpellCheck 4194304 +#define WIDGET_Ff_DoNotScroll 8388608 +#define WIDGET_Ff_Comb 16777216 +#define WIDGET_Ff_RichText 33554432 +// Button fields +#define WIDGET_Ff_NoToggleToOff 16384 +#define WIDGET_Ff_Radio 32768 +#define WIDGET_Ff_Pushbutton 65536 +#define WIDGET_Ff_RadioInUnison 33554432 -SWIGINTERN int -SWIG_AsVal_double (PyObject *obj, double *val) -{ - int res = SWIG_TypeError; - if (PyFloat_Check(obj)) { - if (val) *val = PyFloat_AsDouble(obj); - return SWIG_OK; -#if PY_VERSION_HEX < 0x03000000 - } else if (PyInt_Check(obj)) { - if (val) *val = (double) PyInt_AsLong(obj); - return SWIG_OK; -#endif - } else if (PyLong_Check(obj)) { - double v = PyLong_AsDouble(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return SWIG_OK; - } else { - PyErr_Clear(); - } - } -#ifdef SWIG_PYTHON_CAST_MODE - { - int dispatch = 0; - double d = PyFloat_AsDouble(obj); - if (!PyErr_Occurred()) { - if (val) *val = d; - return SWIG_AddCast(SWIG_OK); - } else { - PyErr_Clear(); +// Choice fields +#define WIDGET_Ff_Combo 131072 +#define WIDGET_Ff_Edit 262144 +#define WIDGET_Ff_Sort 524288 +#define WIDGET_Ff_MultiSelect 2097152 +#define WIDGET_Ff_CommitOnSelCHange 67108864 + +//---------------------------------------------------------------------------- +// return code for line end style string +//---------------------------------------------------------------------------- +int JM_le_value(fz_context *ctx, char *le) +{ + if (!le) return ANNOT_LE_None; + return pdf_line_ending_from_string(ctx, le); +} + +//---------------------------------------------------------------------------- +// return pdf_obj "border style" from Python str +//---------------------------------------------------------------------------- +pdf_obj *JM_get_border_style(fz_context *ctx, PyObject *style) +{ + pdf_obj *val = PDF_NAME(S); + if (!style) return val; + char *s = JM_Python_str_AsChar(style); + JM_PyErr_Clear; + if (!s) return val; + if (!strncmp(s, "b", 1) || !strncmp(s, "B", 1)) val = PDF_NAME(B); + else if (!strncmp(s, "d", 1) || !strncmp(s, "D", 1)) val = PDF_NAME(D); + else if (!strncmp(s, "i", 1) || !strncmp(s, "I", 1)) val = PDF_NAME(I); + else if (!strncmp(s, "u", 1) || !strncmp(s, "U", 1)) val = PDF_NAME(U); + JM_Python_str_DelForPy3(s); + return val; +} + +//---------------------------------------------------------------------------- +// Make /DA string of annotation +//---------------------------------------------------------------------------- +const char *JM_expand_fname(const char **name) +{ + if (!*name) return "Helv"; + if (!strncmp(*name, "Co", 2)) return "Cour"; + if (!strncmp(*name, "co", 2)) return "Cour"; + if (!strncmp(*name, "Ti", 2)) return "TiRo"; + if (!strncmp(*name, "ti", 2)) return "TiRo"; + if (!strncmp(*name, "Sy", 2)) return "Symb"; + if (!strncmp(*name, "sy", 2)) return "Symb"; + if (!strncmp(*name, "Za", 2)) return "ZaDb"; + if (!strncmp(*name, "za", 2)) return "ZaDb"; + return "Helv"; +} + +void JM_make_annot_DA(fz_context *ctx, pdf_annot *annot, int ncol, float col[4], const char *fontname, float fontsize) +{ + fz_buffer *buf = NULL; + fz_try(ctx) + { + buf = fz_new_buffer(ctx, 50); + if (ncol == 1) + fz_append_printf(ctx, buf, "%g g ", col[0]); + else if (ncol == 3) + fz_append_printf(ctx, buf, "%g %g %g rg ", col[0], col[1], col[2]); + else + fz_append_printf(ctx, buf, "%g %g %g %g k ", col[0], col[1], col[2], col[3]); + fz_append_printf(ctx, buf, "/%s %g Tf", JM_expand_fname(&fontname), fontsize); + char *da = NULL; + size_t len = fz_buffer_storage(ctx, buf, &da); + pdf_dict_put_string(ctx, annot->obj, PDF_NAME(DA), (const char *)da, len); } - if (!dispatch) { - long v = PyLong_AsLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return SWIG_AddCast(SWIG_AddCast(SWIG_OK)); - } else { - PyErr_Clear(); - } + fz_always(ctx) fz_drop_buffer(ctx, buf); + fz_catch(ctx) fz_rethrow(ctx); + return; +} + +//---------------------------------------------------------------------------- +// refreshes the link and annotation tables of a page +//---------------------------------------------------------------------------- +void refresh_link_table(fz_context *ctx, pdf_page *page) +{ + pdf_obj *annots_arr = pdf_dict_get(ctx, page->obj, PDF_NAME(Annots)); + if (annots_arr) + { + fz_rect page_mediabox; + fz_matrix page_ctm; + pdf_page_transform(ctx, page, &page_mediabox, &page_ctm); + page->links = pdf_load_link_annots(ctx, page->doc, annots_arr, + pdf_to_num(ctx, page->obj), page_ctm); + pdf_load_annots(ctx, page, annots_arr); } - } -#endif - return res; + return; } +//----------------------------------------------------------------------------- +// create a strike-out / underline / highlight annotation +//----------------------------------------------------------------------------- +struct fz_annot_s *JM_AnnotTextmarker(fz_context *ctx, pdf_page *page, fz_quad q, int type) +{ + pdf_annot *annot = NULL; + float width = 0; + float color[3] = {0,0,0}; + switch (type) + { + case PDF_ANNOT_HIGHLIGHT: + color[0] = color[1] = 1; color[2] = 0; + width = 1.0f; + break; + case PDF_ANNOT_UNDERLINE: + color[0] = color[1] = 0; color[2] = 1; + width = 0.07f; + break; + case PDF_ANNOT_SQUIGGLY: + color[0] = color[1] = 0; color[2] = 1; + width = 0.07f; + break; + case PDF_ANNOT_STRIKE_OUT: + color[0] = 1; color[1] = color[2] = 0; + width = 0.07f; + break; + } + fz_try(ctx) + { + pdf_document *pdf = page->doc; + annot = pdf_create_annot(ctx, page, type); + pdf_set_annot_color(ctx, annot, 3, color); + pdf_set_annot_border(ctx, annot, width); + pdf_add_annot_quad_point(ctx, annot, q); + pdf_set_annot_rect(ctx, annot, fz_rect_from_quad(q)); + pdf_update_annot(ctx, annot); + } + fz_catch(ctx) fz_rethrow(ctx); + return (fz_annot *) annot; +} -SWIGINTERN int -SWIG_AsVal_float (PyObject * obj, float *val) +//----------------------------------------------------------------------------- +// create a circle or rectangle annotation +//----------------------------------------------------------------------------- +struct fz_annot_s *JM_AnnotCircleOrRect(fz_context *ctx, pdf_page *page, PyObject *rect, int type) { - double v; - int res = SWIG_AsVal_double (obj, &v); - if (SWIG_IsOK(res)) { - if (SWIG_Float_Overflow_Check(v)) { - return SWIG_OverflowError; - } else { - if (val) *val = (float)(v); + pdf_annot *annot; + float col[3] = {0,0,0}; + float width = 1; + fz_try(ctx) + { + pdf_document *pdf = page->doc; + annot = pdf_create_annot(ctx, page, type); + pdf_set_annot_border(ctx, annot, width); + pdf_set_annot_color(ctx, annot, 3, col); + pdf_set_annot_rect(ctx, annot, JM_rect_from_py(rect)); + pdf_update_annot(ctx, annot); } - } - return res; + fz_catch(ctx) fz_rethrow(ctx); + return (fz_annot *) annot; } -SWIGINTERN struct fz_document_s *new_fz_document_s(char const *filename,PyObject *stream,char const *filetype,struct fz_rect_s *rect,float width,float height,float fontsize){ - gctx->error->errcode = 0; // reset any error code - gctx->error->message[0] = 0; // reset any error message - struct fz_document_s *doc = NULL; - fz_stream *data = NULL; - char *streamdata; - float w = width, h = height; - size_t streamlen = JM_CharFromBytesOrArray(stream, &streamdata); - fz_try(gctx) +//----------------------------------------------------------------------------- +// create a polyline or polygon annotation +//----------------------------------------------------------------------------- +struct fz_annot_s *JM_AnnotMultiline(fz_context *ctx, pdf_page *page, PyObject *points, int type) +{ + pdf_annot *annot; + fz_try(ctx) + { + float col[3] = {0,0,0}; + float width = 1; + fz_point point = {0,0}; + fz_rect rect; + + int n = 0, i; + if (PySequence_Check(points)) n = PySequence_Size(points); + if (n < 2) THROWMSG("invalid points list"); + annot = pdf_create_annot(ctx, page, type); + for (i = 0; i < n; i++) + { + PyObject *p = PySequence_ITEM(points, i); + if (!PySequence_Check(p) || PySequence_Size(p) != 2) + THROWMSG("invalid points list"); + point.x = (float) PyFloat_AsDouble(PySequence_GetItem(p, 0)); + point.y = (float) PyFloat_AsDouble(PySequence_GetItem(p, 1)); + Py_CLEAR(p); + pdf_add_annot_vertex(ctx, annot, point); + if (i == 0) { - if (rect) - { - if (fz_is_empty_rect(rect) || fz_is_infinite_rect(rect)) - THROWMSG("rect must be finite and not empty"); - w = rect->x1 - rect->x0; - h = rect->y1 - rect->y0; - } - if (streamlen > 0) - { - data = fz_open_memory(gctx, streamdata, streamlen); - char *magic = (char *)filename; - if (!magic) magic = (char *)filetype; - doc = fz_open_document_with_stream(gctx, magic, data); - } - else - { - if (filename) - { - if (!filetype || strlen(filetype) == 0) doc = fz_open_document(gctx, filename); - else - { - const fz_document_handler *handler; - handler = fz_recognize_document(gctx, filetype); - if (handler && handler->open) - doc = handler->open(gctx, filename); - else THROWMSG("unrecognized file type"); - } - } - else - { - pdf_document *pdf = pdf_create_document(gctx); - pdf->dirty = 1; - doc = (fz_document *) pdf; - } - } + rect.x0 = point.x; + rect.y0 = point.y; + rect.x1 = point.x; + rect.y1 = point.y; } - fz_catch(gctx) return NULL; - if (w > 0 && h > 0) - fz_layout_document(gctx, doc, w, h, fontsize); - return doc; + else + rect = fz_include_point_in_rect(rect, point); } -SWIGINTERN void fz_document_s_close(struct fz_document_s *self){ - DEBUGMSG1("doc.close()"); - while(self->refs > 1) { - fz_drop_document(gctx, self); - } - fz_drop_document(gctx, self); - DEBUGMSG2; + pdf_set_annot_border(ctx, annot, width); // standard: width = 1 + pdf_set_annot_color(ctx, annot, 3, col); // standard: black + rect = fz_expand_rect(rect, 3 * width); + pdf_set_annot_rect(ctx, annot, rect); + pdf_update_annot(ctx, annot); + } + fz_catch(ctx) fz_rethrow(ctx); + return (fz_annot *) annot; +} + +PyObject *JM_annot_border(fz_context *ctx, pdf_obj *annot_obj) +{ + PyObject *res = PyDict_New(); + PyObject *dash_py = PyList_New(0); + PyObject *effect_py = PyList_New(0); + int i; + char *effect2 = NULL, *style = NULL; + float width = -1.0f; + int effect1 = -1; + + pdf_obj *o = pdf_dict_get(ctx, annot_obj, PDF_NAME(Border)); + if (pdf_is_array(ctx, o)) + { + width = pdf_to_real(ctx, pdf_array_get(ctx, o, 2)); + if (pdf_array_len(ctx, o) == 4) + { + pdf_obj *dash = pdf_array_get(ctx, o, 3); + for (i = 0; i < pdf_array_len(ctx, dash); i++) + PyList_Append(dash_py, Py_BuildValue("i", + pdf_to_int(ctx, pdf_array_get(ctx, dash, i)))); } + } -#include -#if !defined(SWIG_NO_LLONG_MAX) -# if !defined(LLONG_MAX) && defined(__GNUC__) && defined (__LONG_LONG_MAX__) -# define LLONG_MAX __LONG_LONG_MAX__ -# define LLONG_MIN (-LLONG_MAX - 1LL) -# define ULLONG_MAX (LLONG_MAX * 2ULL + 1ULL) -# endif -#endif + pdf_obj *bs_o = pdf_dict_get(ctx, annot_obj, PDF_NAME(BS)); + if (bs_o) + { + o = pdf_dict_get(ctx, bs_o, PDF_NAME(W)); + if (o) width = pdf_to_real(ctx, o); + o = pdf_dict_get(ctx, bs_o, PDF_NAME(S)); + if (o) style = (char *) pdf_to_name(ctx, o); + o = pdf_dict_get(ctx, bs_o, PDF_NAME(D)); + if (o) + { + for (i = 0; i < pdf_array_len(ctx, o); i++) + PyList_Append(dash_py, Py_BuildValue("i", + pdf_to_int(ctx, pdf_array_get(ctx, o, i)))); + } + } + pdf_obj *be_o = pdf_dict_gets(ctx, annot_obj, "BE"); + if (be_o) + { + o = pdf_dict_get(ctx, be_o, PDF_NAME(S)); + if (o) effect2 = (char *) pdf_to_name(ctx, o); + o = pdf_dict_get(ctx, be_o, PDF_NAME(I)); + if (o) effect1 = pdf_to_int(ctx, o); + } -SWIGINTERNINLINE int -SWIG_CanCastAsInteger(double *d, double min, double max) { - double x = *d; - if ((min <= x && x <= max)) { - double fx = floor(x); - double cx = ceil(x); - double rd = ((x - fx) < 0.5) ? fx : cx; /* simple rint */ - if ((errno == EDOM) || (errno == ERANGE)) { - errno = 0; - } else { - double summ, reps, diff; - if (rd < x) { - diff = x - rd; - } else if (rd > x) { - diff = rd - x; - } else { - return 1; - } - summ = rd + x; - reps = diff/summ; - if (reps < 8*DBL_EPSILON) { - *d = rd; - return 1; - } - } - } - return 0; -} + PyList_Append(effect_py, Py_BuildValue("i", effect1)); + PyList_Append(effect_py, Py_BuildValue("s", effect2)); + PyDict_SetItemString(res, "width", Py_BuildValue("f", width)); -SWIGINTERN int -SWIG_AsVal_long (PyObject *obj, long* val) -{ -#if PY_VERSION_HEX < 0x03000000 - if (PyInt_Check(obj)) { - if (val) *val = PyInt_AsLong(obj); - return SWIG_OK; - } else -#endif - if (PyLong_Check(obj)) { - long v = PyLong_AsLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return SWIG_OK; - } else { - PyErr_Clear(); - return SWIG_OverflowError; - } - } -#ifdef SWIG_PYTHON_CAST_MODE - { - int dispatch = 0; - long v = PyInt_AsLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return SWIG_AddCast(SWIG_OK); - } else { - PyErr_Clear(); - } - if (!dispatch) { - double d; - int res = SWIG_AddCast(SWIG_AsVal_double (obj,&d)); - if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, LONG_MIN, LONG_MAX)) { - if (val) *val = (long)(d); - return res; - } - } - } -#endif - return SWIG_TypeError; -} + PyDict_SetItemString(res, "dashes", dash_py); + PyDict_SetItemString(res, "style", Py_BuildValue("s", style)); -SWIGINTERN int -SWIG_AsVal_int (PyObject * obj, int *val) + if (effect1 > -1) PyDict_SetItemString(res, "effect", effect_py); + Py_CLEAR(effect_py); + Py_CLEAR(dash_py); + return res; +} + +PyObject *JM_annot_set_border(fz_context *ctx, PyObject *border, pdf_document *doc, pdf_obj *annot_obj) { - long v; - int res = SWIG_AsVal_long (obj, &v); - if (SWIG_IsOK(res)) { - if ((v < INT_MIN || v > INT_MAX)) { - return SWIG_OverflowError; - } else { - if (val) *val = (int)(v); + if (!PyDict_Check(border)) + { + JM_Warning("arg must be a dict"); + return NONE; // not a dict + } + + double nwidth = -1; // new width + double owidth = -1; // old width + PyObject *ndashes = NULL; // new dashes + PyObject *odashes = NULL; // old dashes + PyObject *nstyle = NULL; // new style + PyObject *ostyle = NULL; // old style + + nwidth = PyFloat_AsDouble(PyDict_GetItemString(border, "width")); + ndashes = PyDict_GetItemString(border, "dashes"); + nstyle = PyDict_GetItemString(border, "style"); + + // first get old border properties + PyObject *oborder = JM_annot_border(ctx, annot_obj); + owidth = PyFloat_AsDouble(PyDict_GetItemString(oborder, "width")); + odashes = PyDict_GetItemString(oborder, "dashes"); + ostyle = PyDict_GetItemString(oborder, "style"); + + // then delete any relevant entries + pdf_dict_del(ctx, annot_obj, PDF_NAME(BS)); + pdf_dict_del(ctx, annot_obj, PDF_NAME(BE)); + pdf_dict_del(ctx, annot_obj, PDF_NAME(Border)); + + Py_ssize_t i, n; + int d; + // populate new border array + if (nwidth < 0) nwidth = owidth; // no new width: take current + if (nwidth < 0) nwidth = 0.0f; // default if no width given + if (!ndashes) ndashes = odashes; // no new dashes: take old + if (!nstyle) nstyle = ostyle; // no new style: take old + + if (ndashes && PySequence_Check(ndashes) && PySequence_Size(ndashes) > 0) + { + n = PySequence_Size(ndashes); + pdf_obj *darr = pdf_new_array(ctx, doc, n); + for (i = 0; i < n; i++) + { + d = (int) PyInt_AsLong(PySequence_GetItem(ndashes, i)); + pdf_array_push_int(ctx, darr, (int64_t) d); + } + pdf_dict_putl_drop(ctx, annot_obj, darr, PDF_NAME(BS), PDF_NAME(D), NULL); + nstyle = Py_BuildValue("s", "D"); } - } - return res; + + pdf_dict_putl_drop(ctx, annot_obj, pdf_new_real(ctx, nwidth), + PDF_NAME(BS), PDF_NAME(W), NULL); + + pdf_obj *val = JM_get_border_style(ctx, nstyle); + + pdf_dict_putl_drop(ctx, annot_obj, val, + PDF_NAME(BS), PDF_NAME(S), NULL); + + PyErr_Clear(); + return NONE; } -SWIGINTERN struct fz_page_s *fz_document_s_loadPage(struct fz_document_s *self,int number){ - struct fz_page_s *page = NULL; - fz_try(gctx) - { - int pageCount = fz_count_pages(gctx, self); - if (pageCount < 1) THROWMSG("document has no pages"); - int n = number; - while (n < 0) n += pageCount; - page = fz_load_page(gctx, self, n); - } - fz_catch(gctx) return NULL; - return page; - } -SWIGINTERN struct fz_outline_s *fz_document_s__loadOutline(struct fz_document_s *self){ - fz_outline *ol = NULL; - fz_try(gctx) ol = fz_load_outline(gctx, self); - fz_catch(gctx) return NULL; - return ol; - } -SWIGINTERN void fz_document_s__dropOutline(struct fz_document_s *self,struct fz_outline_s *ol){ - DEBUGMSG1("outline"); - fz_drop_outline(gctx, ol); - DEBUGMSG2; +PyObject *JM_annot_colors(fz_context *ctx, pdf_obj *annot_obj) +{ + PyObject *res = PyDict_New(); + PyObject *bc = PyList_New(0); // stroke colors + PyObject *fc = PyList_New(0); // fill colors + + int i; + float col; + pdf_obj *o = pdf_dict_get(ctx, annot_obj, PDF_NAME(C)); + if (pdf_is_array(ctx, o)) + { + int n = pdf_array_len(ctx, o); + for (i = 0; i < n; i++) + { + col = pdf_to_real(ctx, pdf_array_get(ctx, o, i)); + PyList_Append(bc, Py_BuildValue("f", col)); } -SWIGINTERN int fz_document_s_embeddedFileCount(struct fz_document_s *self){ - pdf_document *pdf = pdf_document_from_fz_document(gctx, self); - if (!pdf) return 0; - return pdf_count_portfolio_entries(gctx, pdf); + } + PyDict_SetItemString(res, "stroke", bc); + + o = pdf_dict_gets(ctx, annot_obj, "IC"); + if (pdf_is_array(ctx, o)) + { + int n = pdf_array_len(ctx, o); + for (i = 0; i < n; i++) + { + col = pdf_to_real(ctx, pdf_array_get(ctx, o, i)); + PyList_Append(fc, Py_BuildValue("f", col)); } + } + PyDict_SetItemString(res, "fill", fc); -SWIGINTERNINLINE PyObject* - SWIG_From_int (int value) -{ - return PyInt_FromLong((long) value); + Py_CLEAR(bc); + Py_CLEAR(fc); + return res; } -SWIGINTERN int fz_document_s_embeddedFileDel(struct fz_document_s *self,char *name){ - pdf_document *pdf = pdf_document_from_fz_document(gctx, self); - pdf_obj *names; - int i, n, m; - fz_var(names); - fz_try(gctx) - { - assert_PDF(pdf); - // check presence of name - if (JM_find_embedded(gctx, Py_BuildValue("s", name), pdf) < 0) - THROWMSG("name not found"); - names = JM_embedded_names(gctx, pdf); - if (!pdf_is_array(gctx, names)) - THROWMSG("could not find names array"); - n = pdf_array_len(gctx, names); +PyObject *JM_UnicodeFromASCII(const char *in); +//----------------------------------------------------------------------------- +// Plain text output. An identical copy of fz_print_stext_page_as_text, +// but lines within a block are concatenated by space instead a new-line +// character (which else leads to 2 new-lines). +//----------------------------------------------------------------------------- +void +JM_print_stext_page_as_text(fz_context *ctx, fz_output *out, fz_stext_page *page) +{ + fz_stext_block *block; + fz_stext_line *line; + fz_stext_char *ch; + char utf[10]; + int i, n, last_char; - //------------------------------------------------------------- - // Every file has 2 entries: name and file descriptor. - // First delete file descriptor, then the name entry. - // Because it might be referenced elsewhere, we leave deletion - // of stream object to garbage collection. - //------------------------------------------------------------- - for (i = 0; i < n; i += 2) + for (block = page->first_block; block; block = block->next) + { + if (block->type == FZ_STEXT_BLOCK_TEXT) + { + int line_n = 0; + for (line = block->u.t.first_line; line; line = line->next) + { + if (line_n > 0 && last_char != 10) fz_write_string(ctx, out, "\n"); + line_n++; + for (ch = line->first_char; ch; ch = ch->next) { - char *test = pdf_to_utf8(gctx, pdf_array_get(gctx, names, i)); - if (!strcmp(test, name)) - { - pdf_array_delete(gctx, names, i + 1); - pdf_array_delete(gctx, names, i); - } + n = fz_runetochar(utf, ch->c); + for (i = 0; i < n; i++) + fz_write_byte(ctx, out, utf[i]); + last_char = ch->c; } - m = (n - pdf_array_len(gctx, names)) / 2; } - fz_catch(gctx) return -1; - return m; + fz_write_string(ctx, out, "\n"); } -SWIGINTERN PyObject *fz_document_s_embeddedFileInfo(struct fz_document_s *self,PyObject *id){ - pdf_document *pdf = pdf_document_from_fz_document(gctx, self); - Py_ssize_t name_len = 0; - int n = -1; - char *name = NULL; - char *sname = NULL; - fz_try(gctx) - { - assert_PDF(pdf); - n = JM_find_embedded(gctx, id, pdf); - if (n < 0) THROWMSG("entry not found"); - } - fz_catch(gctx) return NULL; + } +} - PyObject *infodict = PyDict_New(); - // name of file entry - name = pdf_to_utf8(gctx, pdf_portfolio_entry_name(gctx, pdf, n)); - PyDict_SetItemString(infodict, "name", JM_UNICODE(name)); +//----------------------------------------------------------------------------- +// Functions for wordlist output +//----------------------------------------------------------------------------- +int JM_append_word(fz_context *ctx, PyObject *lines, fz_buffer *buff, fz_rect *wbbox, + int block_n, int line_n, int word_n) +{ + PyObject *litem = Py_BuildValue("ffffOiii", wbbox->x0, wbbox->y0, wbbox->x1, wbbox->y1, + JM_StrFromBuffer(ctx, buff), + block_n, line_n, word_n); + PyList_Append(lines, litem); + Py_CLEAR(litem); + wbbox->x0 = wbbox->y0 = wbbox->x1 = wbbox->y1 = 0; + return word_n + 1; // word counter +} - pdf_obj *o = pdf_portfolio_entry_obj(gctx, pdf, n); +//----------------------------------------------------------------------------- +// Functions for dictionary output +//----------------------------------------------------------------------------- - name = pdf_to_utf8(gctx, pdf_dict_get(gctx, o, PDF_NAME_F)); - PyDict_SetItemString(infodict, "filename", JM_UNICODE(name)); +// create the char rectangle from the char quad +fz_rect JM_char_bbox(fz_stext_line *line, fz_stext_char *ch) +{ + fz_rect r = fz_rect_from_quad(ch->quad); + if (!fz_is_empty_rect(r)) return r; + // we need to correct erroneous font! + if ((r.y1 - r.y0) <= JM_EPS) r.y0 = r.y1 - ch->size; + if ((r.x1 - r.x0) <= JM_EPS) r.x0 = r.x1 - ch->size; + return r; +} - name = pdf_to_utf8(gctx, pdf_dict_get(gctx, o, PDF_NAME_UF)); - PyDict_SetItemString(infodict, "ufilename", JM_UNICODE(name)); +static int detect_super_script(fz_stext_line *line, fz_stext_char *ch) +{ + if (line->wmode == 0 && line->dir.x == 1 && line->dir.y == 0) + return ch->origin.y < line->first_char->origin.y - ch->size * 0.1f; + return 0; +} - name = pdf_to_utf8(gctx, pdf_dict_get(gctx, o, PDF_NAME_Desc)); - PyDict_SetItemString(infodict, "desc", JM_UNICODE(name)); +static const char *font_full_name(fz_context *ctx, fz_font *font) +{ + const char *name = fz_font_name(ctx, font); + const char *s = strchr(name, '+'); + return s ? s + 1 : name; +} - int len = -1, DL = -1; - pdf_obj *ef = pdf_dict_get(gctx, o, PDF_NAME_EF); - o = pdf_dict_getl(gctx, ef, PDF_NAME_F, - PDF_NAME_Length, NULL); - if (o) len = pdf_to_int(gctx, o); +static void font_family_name(fz_context *ctx, fz_font *font, char *buf, int size) +{ + const char *name = font_full_name(ctx, font); + fz_strlcpy(buf, name, size); +} - o = pdf_dict_getl(gctx, ef, PDF_NAME_F, PDF_NAME_DL, NULL); - if (o) DL = pdf_to_int(gctx, o); - else - { - o = pdf_dict_getl(gctx, ef, PDF_NAME_F, PDF_NAME_Params, - PDF_NAME_Size, NULL); - if (o) DL = pdf_to_int(gctx, o); - } - - PyDict_SetItemString(infodict, "size", Py_BuildValue("i", DL)); - PyDict_SetItemString(infodict, "length", Py_BuildValue("i", len)); - return infodict; - } -SWIGINTERN PyObject *fz_document_s_embeddedFileUpd(struct fz_document_s *self,PyObject *id,PyObject *buffer,char *filename,char *ufilename,char *desc){ - pdf_document *pdf = pdf_document_from_fz_document(gctx, self); - fz_buffer *res = NULL; - fz_var(res); - fz_try(gctx) - { - assert_PDF(pdf); +PyObject * +JM_style_begin_dict(fz_context *ctx, fz_font *font, float size, int sup) +{ + //JM_TRACE("entering JM_style_begin_dict"); + char family[80]; + font_family_name(ctx, font, family, sizeof family); + int flags = sup; + flags += fz_font_is_italic(ctx, font) * 2; + flags += fz_font_is_serif(ctx, font) * 4; + flags += fz_font_is_monospaced(ctx, font) * 8; + flags += fz_font_is_bold(ctx, font) * 16; + PyObject *span = PyDict_New(); + PyDict_SetItemString(span, "font", JM_UnicodeFromASCII(family)); + PyDict_SetItemString(span, "size", Py_BuildValue("f", size)); + PyDict_SetItemString(span, "flags", Py_BuildValue("i", flags)); + //JM_TRACE("leaving JM_style_begin_dict"); + return span; +} - int n = JM_find_embedded(gctx, id, pdf); - if (n < 0) THROWMSG("entry not found"); +void +JM_style_end_dict(fz_context *ctx, fz_buffer *buff, PyObject *span, PyObject *spanlist) +{ + if (buff) + PyDict_SetItemString(span, "text", JM_StrFromBuffer(ctx, buff)); + PyList_Append(spanlist, span); +} - pdf_obj *entry = pdf_portfolio_entry_obj(gctx, pdf, n); - pdf_obj *filespec = pdf_dict_getl(gctx, entry, PDF_NAME_EF, - PDF_NAME_F, NULL); +PyObject * +JM_extract_stext_textchar_as_dict(fz_context *ctx, fz_stext_char *ch, fz_rect *ch_bbox) +{ + //JM_TRACE("entering JM_extract_stext_textchar_as_dict"); + char data[10]; + Py_ssize_t len = (Py_ssize_t) fz_runetochar(data, ch->c); + PyObject *chardict = PyDict_New(); + PyDict_SetItemString(chardict, "c", PyUnicode_FromStringAndSize(data, len)); + PyDict_SetItemString(chardict, "origin", Py_BuildValue("ff", ch->origin.x, ch->origin.y)); + PyDict_SetItemString(chardict, "bbox", Py_BuildValue("ffff", + ch_bbox->x0, ch_bbox->y0, + ch_bbox->x1, ch_bbox->y1)); + //JM_TRACE("leaving JM_extract_stext_textchar_as_dict"); + return chardict; +} - char *data = NULL; - size_t len = JM_CharFromBytesOrArray(buffer, &data); - if (len > 0) +PyObject * +JM_extract_stext_textblock_as_dict(fz_context *ctx, fz_stext_block *block, int rawdict) +{ + //JM_TRACE("enter: JM_extract_stext_textblock_as_dict"); + fz_stext_line *line; + fz_stext_char *ch; + fz_font *font = NULL; + fz_buffer *buff = NULL; + float size = 0; + int sup = 0; + PyObject *span = NULL, *spanlist = NULL, *linelist = NULL, *linedict = NULL; + PyObject *charlist = NULL, *chardict = NULL; + linelist = PyList_New(0); + PyObject *dict = PyDict_New(); + fz_rect blockrect = block->bbox, linerect, ch_bbox; + PyDict_SetItemString(dict, "type", PyInt_FromLong(FZ_STEXT_BLOCK_TEXT)); + //JM_TRACE("before line loop"); + for (line = block->u.t.first_line; line; line = line->next) + { + linedict = PyDict_New(); + linerect = line->bbox; + PyDict_SetItemString(linedict, "wmode", Py_BuildValue("i", line->wmode)); + PyDict_SetItemString(linedict, "dir", Py_BuildValue("ff", line->dir.x, line->dir.y)); + spanlist = PyList_New(0); + font = NULL; + buff = NULL; + size = 0; + //JM_TRACE("before character loop"); + for (ch = line->first_char; ch; ch = ch->next) + { + ch_bbox = JM_char_bbox(line, ch); + linerect = fz_union_rect(linerect, ch_bbox); + //JM_TRACE("joined char bbox to linerect"); + int ch_sup = detect_super_script(line, ch); + if (ch->font != font || ch->size != size) + { // start new span + //JM_TRACE("starting new span"); + if (font) // must finish old span first { - if (!filespec) THROWMSG("/EF object not found"); - res = fz_new_buffer_from_copied_data(gctx, data, len); - JM_update_stream(gctx, pdf, filespec, res); - // adjust /DL and /Size parameters - pdf_obj *l = pdf_new_int(gctx, NULL, (int64_t) len); - pdf_dict_put(gctx, filespec, PDF_NAME_DL, l); - pdf_dict_putl(gctx, filespec, l, PDF_NAME_Params, PDF_NAME_Size, NULL); + //JM_TRACE("finishing old span"); + if (rawdict) + { + PyDict_SetItemString(span, "chars", charlist); + Py_CLEAR(charlist); + } + JM_style_end_dict(ctx, buff, span, spanlist); + Py_CLEAR(span); + fz_drop_buffer(ctx, buff); + buff = NULL; + font = NULL; + //JM_TRACE("finished old span"); } - if (filename) - pdf_dict_put_text_string(gctx, entry, PDF_NAME_F, filename); - - if (ufilename) - pdf_dict_put_text_string(gctx, entry, PDF_NAME_UF, ufilename); - - if (desc) - pdf_dict_put_text_string(gctx, entry, PDF_NAME_Desc, desc); - } - fz_always(gctx) - fz_drop_buffer(gctx, res); - fz_catch(gctx) - return NULL; - pdf->dirty = 1; - return NONE; - } -SWIGINTERN PyObject *fz_document_s_embeddedFileGet(struct fz_document_s *self,PyObject *id){ - PyObject *cont = NULL; - pdf_document *pdf = pdf_document_from_fz_document(gctx, self); - fz_buffer *buf = NULL; - fz_var(buf); - fz_try(gctx) - { - assert_PDF(pdf); - int i = JM_find_embedded(gctx, id, pdf); - if (i < 0) THROWMSG("entry not found"); - buf = pdf_portfolio_entry(gctx, pdf, i); - cont = JM_BinFromBuffer(gctx, buf); - } - fz_always(gctx) fz_drop_buffer(gctx, buf); - fz_catch(gctx) return NULL; - return cont; - } -SWIGINTERN PyObject *fz_document_s_embeddedFileAdd(struct fz_document_s *self,PyObject *buffer,char const *name,char *filename,char *ufilename,char *desc){ - pdf_document *pdf = pdf_document_from_fz_document(gctx, self); - fz_buffer *data = NULL, *buf = NULL; - char *buffdata; - fz_var(data); - fz_var(buf); - int entry = 0; - size_t size = 0; - char *f = filename, *uf = ufilename, *d = desc; - int name_len = (int) strlen(name); - // make adjustments for omitted arguments - if (!f) f = (char *)name; - if (!uf) uf = f; - if (!d) d = f; - - fz_try(gctx) - { - assert_PDF(pdf); - size = JM_CharFromBytesOrArray(buffer, &buffdata); - if (size < 1) THROWMSG("buffer not bytes / bytearray"); - - // we do not allow duplicate names - entry = JM_find_embedded(gctx, Py_BuildValue("s", name), pdf); - if (entry >= 0) THROWMSG("name already exists"); - - // first insert a dummy entry with no more than the name - buf = fz_new_buffer(gctx, name_len + 1); // has no real meaning - fz_append_string(gctx, buf, name); // fill something in - fz_terminate_buffer(gctx, buf); // to make it usable - pdf_add_portfolio_entry(gctx, pdf, // insert the entry - name, name_len, // except the name, - name, name_len, // everythinh will - name, name_len, // be overwritten - name, name_len, - buf); - fz_drop_buffer(gctx, buf); // kick stupid buffer - buf = NULL; - //------------------------------------------------------------- - // now modify the entry just created: - // (1) allow unicode values for filenames and description - // (2) deflate the file content - //------------------------------------------------------------- - // locate the entry again - entry = JM_find_embedded(gctx, Py_BuildValue("s", name), pdf); - // (1) insert the real metadata - pdf_obj *o = pdf_portfolio_entry_obj(gctx, pdf, entry); - pdf_dict_put_text_string(gctx, o, PDF_NAME_F, f); - pdf_dict_put_text_string(gctx, o, PDF_NAME_UF, uf); - pdf_dict_put_text_string(gctx, o, PDF_NAME_Desc, d); - // (2) insert the real file contents - pdf_obj *filespec = pdf_dict_getl(gctx, o, PDF_NAME_EF, - PDF_NAME_F, NULL); - data = fz_new_buffer_from_copied_data(gctx, buffdata, size); - JM_update_stream(gctx, pdf, filespec, data); - // finally update some size attributes - pdf_obj *l = pdf_new_int(gctx, NULL, (int64_t) size); - pdf_dict_put(gctx, filespec, PDF_NAME_DL, l); - pdf_dict_putl(gctx, filespec, l, PDF_NAME_Params, PDF_NAME_Size, NULL); - } - fz_always(gctx) - { - fz_drop_buffer(gctx, buf); - fz_drop_buffer(gctx, data); + font = ch->font; + size = ch->size; + sup = ch_sup; + span = JM_style_begin_dict(ctx, font, size, sup); + if (rawdict) + charlist = PyList_New(0); + else + buff = fz_new_buffer(ctx, 64); + //JM_TRACE("new span started"); } - fz_catch(gctx) return NULL; - pdf->dirty = 1; - return NONE; - } -SWIGINTERN PyObject *fz_document_s_convertToPDF(struct fz_document_s *self,int from_page,int to_page,int rotate){ - PyObject *doc = NULL; - fz_try(gctx) + if (!rawdict) + fz_append_rune(ctx, buff, ch->c); + else { - int fp = from_page, tp = to_page, srcCount = fz_count_pages(gctx, self); - if (pdf_specifics(gctx, self)) - THROWMSG("document is PDF already"); - if (fp < 0) fp = 0; - if (fp > srcCount - 1) fp = srcCount - 1; - if (tp < 0) tp = srcCount - 1; - if (tp > srcCount - 1) tp = srcCount - 1; - doc = JM_convert_to_pdf(gctx, self, fp, tp, rotate); + chardict = JM_extract_stext_textchar_as_dict(ctx, ch, &ch_bbox); + PyList_Append(charlist, chardict); + Py_CLEAR(chardict); } - fz_catch(gctx) return NULL; - return doc; - } -SWIGINTERN int fz_document_s_pageCount(struct fz_document_s *self){ - return fz_count_pages(gctx, self); + //JM_TRACE("finished one char"); } -SWIGINTERN char *fz_document_s__getMetadata(struct fz_document_s *self,char const *key){ - int vsize; - char *value; - vsize = fz_lookup_metadata(gctx, self, key, NULL, 0)+1; - if(vsize > 1) + if (font) + { + //JM_TRACE("start output last span"); + if (rawdict) { - value = JM_Alloc(char, vsize); - fz_lookup_metadata(gctx, self, key, value, vsize); - return value; + PyDict_SetItemString(span, "chars", charlist); + Py_CLEAR(charlist); } - else - return NULL; + JM_style_end_dict(ctx, buff, span, spanlist); + Py_CLEAR(span); + fz_drop_buffer(ctx, buff); + buff = NULL; + font = NULL; } + //JM_TRACE("finishing line"); + PyDict_SetItemString(linedict, "spans", spanlist); + Py_CLEAR(spanlist); + PyDict_SetItemString(linedict, "bbox", Py_BuildValue("ffff", + linerect.x0, linerect.y0, + linerect.x1, linerect.y1)); -SWIGINTERNINLINE PyObject * -SWIG_FromCharPtrAndSize(const char* carray, size_t size) -{ - if (carray) { - if (size > INT_MAX) { - swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); - return pchar_descriptor ? - SWIG_InternalNewPointerObj((char *)(carray), pchar_descriptor, 0) : SWIG_Py_Void(); - } else { -#if PY_VERSION_HEX >= 0x03000000 -#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) - return PyBytes_FromStringAndSize(carray, (Py_ssize_t)(size)); -#else -#if PY_VERSION_HEX >= 0x03010000 - return PyUnicode_DecodeUTF8(carray, (Py_ssize_t)(size), "surrogateescape"); -#else - return PyUnicode_FromStringAndSize(carray, (Py_ssize_t)(size)); -#endif -#endif -#else - return PyString_FromStringAndSize(carray, (Py_ssize_t)(size)); -#endif - } - } else { - return SWIG_Py_Void(); - } -} + blockrect = fz_union_rect(blockrect, linerect); - -SWIGINTERNINLINE PyObject * -SWIG_FromCharPtr(const char *cptr) -{ - return SWIG_FromCharPtrAndSize(cptr, (cptr ? strlen(cptr) : 0)); + PyList_Append(linelist, linedict); + Py_CLEAR(linedict); + } + //JM_TRACE("after line loop"); + PyDict_SetItemString(dict, "lines", linelist); + Py_CLEAR(linelist); + PyDict_SetItemString(dict, "bbox", Py_BuildValue("ffff", + blockrect.x0, blockrect.y0, + blockrect.x1, blockrect.y1)); + //JM_TRACE("leaving JM_extract_stext_textblock_as_dict"); + return dict; } -SWIGINTERN int fz_document_s_needsPass(struct fz_document_s *self){ - return fz_needs_password(gctx, self); - } -SWIGINTERN PyObject *fz_document_s_resolveLink(struct fz_document_s *self,char *uri){ - if (!uri) return NONE; - float xp = 0.0f, yp = 0.0f; - int pno = -1; - fz_try(gctx) - pno = fz_resolve_link(gctx, self, uri, &xp, &yp); - fz_catch(gctx) - return NONE; - if (pno < 0) return NONE; - return Py_BuildValue("iff", pno, xp, yp); - } -SWIGINTERN PyObject *fz_document_s_layout(struct fz_document_s *self,struct fz_rect_s *rect,float width,float height,float fontsize){ - if (!fz_is_document_reflowable(gctx, self)) return NONE; - fz_try(gctx) +PyObject * +JM_extract_stext_imageblock_as_dict(fz_context *ctx, fz_stext_block *block) +{ + fz_image *image = block->u.i.image; + fz_buffer *buf = NULL, *freebuf = NULL; + fz_var(buf); + fz_var(freebuf); + fz_compressed_buffer *buffer = NULL; + int n = fz_colorspace_n(ctx, image->colorspace); + int w = image->w; + int h = image->h; + int type = 0; + unsigned char ext[5]; + PyObject *bytes = JM_BinFromChar(""); + fz_var(bytes); + buffer = fz_compressed_image_buffer(ctx, image); + if (buffer) type = buffer->params.type; + PyObject *dict = PyDict_New(); + PyDict_SetItemString(dict, "type", PyInt_FromLong(FZ_STEXT_BLOCK_IMAGE)); + PyDict_SetItemString(dict, "bbox", Py_BuildValue("[ffff]", + block->bbox.x0, block->bbox.y0, + block->bbox.x1, block->bbox.y1)); + PyDict_SetItemString(dict, "width", PyInt_FromLong((long) w)); + PyDict_SetItemString(dict, "height", PyInt_FromLong((long) h)); + fz_try(ctx) + { + if (image->use_colorkey) type = FZ_IMAGE_UNKNOWN; + if (image->use_decode) type = FZ_IMAGE_UNKNOWN; + if (image->mask) type = FZ_IMAGE_UNKNOWN; + if (type < FZ_IMAGE_BMP) type = FZ_IMAGE_UNKNOWN; + if (n != 1 && n != 3 && type == FZ_IMAGE_JPEG) + type = FZ_IMAGE_UNKNOWN; + if (type != FZ_IMAGE_UNKNOWN) + { + buf = buffer->buffer; + switch(type) { - float w = width, h = height; - if (rect) - { - if (fz_is_empty_rect(rect) || fz_is_infinite_rect(rect)) - THROWMSG("rect must be finite and not empty"); - if (rect->x0 != 0.0f || rect->y0 != 0.0f) - THROWMSG("rect must start at (0, 0)"); - w = rect->x1; - h = rect->y1; - } - if (w <= 0.0f || h <= 0.0f) - THROWMSG("invalid page size"); - fz_layout_document(gctx, self, w, h, fontsize); + case(FZ_IMAGE_BMP): strcpy(ext, "bmp"); break; + case(FZ_IMAGE_GIF): strcpy(ext, "gif"); break; + case(FZ_IMAGE_JPEG): strcpy(ext, "jpeg"); break; + case(FZ_IMAGE_JPX): strcpy(ext, "jpx"); break; + case(FZ_IMAGE_JXR): strcpy(ext, "jxr"); break; + case(FZ_IMAGE_PNM): strcpy(ext, "pnm"); break; + case(FZ_IMAGE_TIFF): strcpy(ext, "tiff"); break; + default: strcpy(ext, "png"); break; } - fz_catch(gctx) return NULL; - return NONE; } -SWIGINTERN PyObject *fz_document_s_makeBookmark(struct fz_document_s *self,int pno){ - if (!fz_is_document_reflowable(gctx, self)) return NONE; - int n = pno, cp = fz_count_pages(gctx, self); - while(n < 0) n += cp; - long long mark = (long long) fz_make_bookmark(gctx, self, n); - return PyLong_FromLongLong(mark); + else + { + buf = freebuf = fz_new_buffer_from_image_as_png(ctx, image, NULL); + strcpy(ext, "png"); } + if (PY_MAJOR_VERSION > 2) + bytes = JM_BinFromBuffer(ctx, buf); + else + bytes = JM_BArrayFromBuffer(ctx, buf); + } + fz_always(ctx) + { + fz_drop_buffer(ctx, freebuf); + PyDict_SetItemString(dict, "ext", PyString_FromString(ext)); + PyDict_SetItemString(dict, "image", bytes); + Py_CLEAR(bytes); + } + fz_catch(ctx) {;} + return dict; +} -#if defined(LLONG_MAX) && !defined(SWIG_LONG_LONG_AVAILABLE) -# define SWIG_LONG_LONG_AVAILABLE -#endif +PyObject * +JM_stext_page_as_dict(fz_context *ctx, fz_stext_page *page, int rawdict) +{ + PyObject *dict = PyDict_New(); + PyObject *blocklist = PyList_New(0); + fz_stext_block *block; + float w = page->mediabox.x1 - page->mediabox.x0; + float h = page->mediabox.y1 - page->mediabox.y0; + PyDict_SetItemString(dict, "width", Py_BuildValue("f", w)); + PyDict_SetItemString(dict, "height", Py_BuildValue("f", h)); + for (block = page->first_block; block; block = block->next) + { + if (block->type == FZ_STEXT_BLOCK_IMAGE) + PyList_Append(blocklist, JM_extract_stext_imageblock_as_dict(ctx, block)); + else + PyList_Append(blocklist, JM_extract_stext_textblock_as_dict(ctx, block, rawdict)); + } + PyDict_SetItemString(dict, "blocks", blocklist); + Py_CLEAR(blocklist); + return dict; +} -#ifdef SWIG_LONG_LONG_AVAILABLE -SWIGINTERN int -SWIG_AsVal_long_SS_long (PyObject *obj, long long *val) +//----------------------------------------------------------------------------- +// Functions dealing with PDF form fields +//----------------------------------------------------------------------------- + +// PushButton get state +//----------------------------------------------------------------------------- +PyObject *JM_pushbtn_state(fz_context *ctx, pdf_annot *annot) +{ // pushed buttons do not reflect status changes in the PDF + // always reflect them as untouched + Py_RETURN_FALSE; +} + +// CheckBox get state +//----------------------------------------------------------------------------- +PyObject *JM_checkbox_state(fz_context *ctx, pdf_annot *annot) { - int res = SWIG_TypeError; - if (PyLong_Check(obj)) { - long long v = PyLong_AsLongLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return SWIG_OK; - } else { - PyErr_Clear(); - res = SWIG_OverflowError; - } - } else { - long v; - res = SWIG_AsVal_long (obj,&v); - if (SWIG_IsOK(res)) { - if (val) *val = v; - return res; - } - } -#ifdef SWIG_PYTHON_CAST_MODE - { - const double mant_max = 1LL << DBL_MANT_DIG; - const double mant_min = -mant_max; - double d; - res = SWIG_AsVal_double (obj,&d); - if (SWIG_IsOK(res) && !SWIG_CanCastAsInteger(&d, mant_min, mant_max)) - return SWIG_OverflowError; - if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, mant_min, mant_max)) { - if (val) *val = (long long)(d); - return SWIG_AddCast(res); + pdf_obj *leafv = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(V)); + pdf_obj *leafas = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(AS)); + if (!leafv) Py_RETURN_FALSE; + if (leafv == PDF_NAME(Off)) Py_RETURN_FALSE; + if (leafv == pdf_new_name(ctx, "Yes")) + Py_RETURN_TRUE; + if (pdf_is_string(ctx, leafv) && !strcmp(pdf_to_str_buf(ctx, leafv), "Off")) + Py_RETURN_FALSE; + if (pdf_is_string(ctx, leafv) && !strcmp(pdf_to_str_buf(ctx, leafv), "Yes")) + Py_RETURN_TRUE; + if (leafas && leafas == PDF_NAME(Off)) Py_RETURN_FALSE; + Py_RETURN_TRUE; +} + +// RadioBox get state +//----------------------------------------------------------------------------- +PyObject *JM_radiobtn_state(fz_context *ctx, pdf_annot *annot) +{ // MuPDF treats radio buttons like check boxes - hence so do we + return JM_checkbox_state(ctx, annot); +} + +// Text field retrieve value +//----------------------------------------------------------------------------- +PyObject *JM_text_value(fz_context *ctx, pdf_annot *annot) +{ + char *text = NULL; + pdf_document *pdf = pdf_get_bound_document(ctx, annot->obj); + fz_var(text); + fz_try(ctx) + text = pdf_field_value(ctx, pdf, annot->obj); + fz_catch(ctx) return NONE; + return Py_BuildValue("s", text); +} + +// ListBox retrieve value +//----------------------------------------------------------------------------- +PyObject *JM_listbox_value(fz_context *ctx, pdf_annot *annot) +{ + int i = 0, n = 0; + // may be single value or array + pdf_obj *optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME(V)); + if (pdf_is_string(ctx, optarr)) // a single string + return PyString_FromString(pdf_to_text_string(ctx, optarr)); + + // value is an array (may have len 0) + n = pdf_array_len(ctx, optarr); + PyObject *liste = PyList_New(0); + + // extract a list of strings + // each entry may again be an array: take second entry then + for (i = 0; i < n; i++) + { + pdf_obj *elem = pdf_array_get(ctx, optarr, i); + if (pdf_is_array(ctx, elem)) + elem = pdf_array_get(ctx, elem, 1); + PyList_Append(liste, PyString_FromString(pdf_to_text_string(ctx, elem))); } - res = SWIG_TypeError; - } -#endif - return res; + return liste; } -#endif -SWIGINTERN int fz_document_s_findBookmark(struct fz_document_s *self,long long bookmark){ - if (!fz_is_document_reflowable(gctx, self)) return -1; - fz_bookmark m = (fz_bookmark) bookmark; - return fz_lookup_bookmark(gctx, self, m); - } -SWIGINTERN PyObject *fz_document_s_isReflowable(struct fz_document_s *self){ - return JM_BOOL(fz_is_document_reflowable(gctx, self)); +// ComboBox retrieve value +//----------------------------------------------------------------------------- +PyObject *JM_combobox_value(fz_context *ctx, pdf_annot *annot) +{ // combobox values are treated like listbox values + return JM_listbox_value(ctx, annot); +} + +// Signature field retrieve value +PyObject *JM_signature_value(fz_context *ctx, pdf_annot *annot) +{ // signatures are currently not supported + return NONE; +} + +// retrieve ListBox / ComboBox choice values +//----------------------------------------------------------------------------- +PyObject *JM_choice_options(fz_context *ctx, pdf_annot *annot) +{ // return list of choices for list or combo boxes + pdf_document *pdf = pdf_get_bound_document(ctx, annot->obj); + int n = pdf_choice_widget_options(ctx, pdf, (pdf_widget *) annot, 0, NULL); + if (n == 0) return NONE; // wrong widget type + + pdf_obj *optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME(Opt)); + int i, m; + PyObject *liste = PyList_New(0); + + for (i = 0; i < n; i++) + { + m = pdf_array_len(ctx, pdf_array_get(ctx, optarr, i)); + if (m == 2) + { + PyList_Append(liste, Py_BuildValue("ss", + pdf_to_text_string(ctx, pdf_array_get(ctx, pdf_array_get(ctx, optarr, i), 0)), + pdf_to_text_string(ctx, pdf_array_get(ctx, pdf_array_get(ctx, optarr, i), 1)))); } -SWIGINTERN PyObject *fz_document_s__deleteObject(struct fz_document_s *self,int xref){ - pdf_document *pdf = pdf_specifics(gctx, self); - fz_try(gctx) - { - assert_PDF(pdf); - if (!INRANGE(xref, 1, pdf_xref_len(gctx, pdf)-1)) - THROWMSG("xref out of range"); - pdf_delete_object(gctx, pdf, xref); - } - fz_catch(gctx) return NULL; - return NONE; + else + { + PyList_Append(liste, PyString_FromString(pdf_to_text_string(ctx, pdf_array_get(ctx, optarr, i)))); } -SWIGINTERN int fz_document_s__getPDFroot(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); - int xref = 0; - if (!pdf) return xref; - fz_try(gctx) - { - pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), - PDF_NAME_Root); - xref = pdf_to_num(gctx, root); - } - fz_catch(gctx) {;} - return xref; + } + return liste; +} + +// set ListBox / ComboBox values +//----------------------------------------------------------------------------- +void JM_set_choice_options(fz_context *ctx, pdf_annot *annot, PyObject *liste) +{ + pdf_document *pdf = pdf_get_bound_document(ctx, annot->obj); + Py_ssize_t i, n = PySequence_Size(liste); + char *opt = NULL; + pdf_obj *optarr = pdf_new_array(ctx, pdf, n); + for (i = 0; i < n; i++) + { + opt = JM_Python_str_AsChar(PySequence_GetItem(liste, i)); + pdf_array_push_text_string(ctx, optarr, (const char *) opt); + JM_Python_str_DelForPy3(opt); + } + + pdf_dict_put(ctx, annot->obj, PDF_NAME(Opt), optarr); + + return; +} + +//----------------------------------------------------------------------------- +// Called by "Page.addWidget" and "Annot.updateWidget". +// Set all properties of a new or updated widget, whether changed or not. +// Should be no performance issue, because verifying a change before updating +// is costly as well (and a code bloat). No problem on the PDF side, because +// any change will always lead to the complete new PDF object being stored. +//----------------------------------------------------------------------------- +void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widget, int field_type) +{ + pdf_document *pdf = annot->page->doc; + pdf_page *page = annot->page; + fz_rect rect = {0,0,0,0}; + pdf_obj *fill_col = NULL, *text_col = NULL, *border_col = NULL; + pdf_obj *dashes = NULL; + Py_ssize_t i, n = 0; + PyObject *value; + + // ensure a font resources dict /DR --- ----------------------------------- + pdf_obj *dr = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), + PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(DR), NULL); + // new /DR using the object prepared in xref + if (!dr) + { + pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), + PDF_NAME(Root), PDF_NAME(AcroForm), NULL); + int xref = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, + "_dr_xref")); + pdf_obj *f = pdf_new_indirect(ctx, pdf, xref, 0); + dr = pdf_new_dict(ctx, pdf, 1); + pdf_dict_put(ctx, dr, PDF_NAME(Font), f); + pdf_dict_put_drop(ctx, form, PDF_NAME(DR), dr); + JM_PyErr_Clear; + } + + // rectangle -------------------------------------------------------------- + value = PyObject_GetAttrString(Widget, "rect"); + rect.x0 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 0)); + rect.y0 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 1)); + rect.x1 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 2)); + rect.y1 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 3)); + Py_CLEAR(value); + JM_PyErr_Clear; + pdf_set_annot_rect(ctx, annot, rect); // set the rect + + // fill color ------------------------------------------------------------- + value = PyObject_GetAttrString(Widget, "fill_color"); + if (value && PySequence_Check(value)) + { + n = PySequence_Size(value); + fill_col = pdf_new_array(ctx, pdf, n); + for (i = 0; i < n; i++) + pdf_array_push_real(ctx, fill_col, + PyFloat_AsDouble(PySequence_GetItem(value, i))); + pdf_field_set_fill_color(ctx, pdf, annot->obj, fill_col); + pdf_drop_obj(ctx, fill_col); + } + Py_CLEAR(value); + JM_PyErr_Clear; + + // dashes ----------------------------------------------------------------- + value = PyObject_GetAttrString(Widget, "border_dashes"); + if (value && PySequence_Check(value)) + { + n = PySequence_Size(value); + dashes = pdf_new_array(ctx, pdf, n); + for (i = 0; i < n; i++) + pdf_array_push_int(ctx, dashes, + PyInt_AsLong(PySequence_GetItem(value, i))); + pdf_dict_putl_drop(ctx, annot->obj, dashes, PDF_NAME(BS), + PDF_NAME(D), NULL); + } + Py_CLEAR(value); + JM_PyErr_Clear; + + // border color ----------------------------------------------------------- + value = PyObject_GetAttrString(Widget, "border_color"); + if (value && PySequence_Check(value)) + { + n = PySequence_Size(value); + border_col = pdf_new_array(ctx, pdf, n); + for (i = 0; i < n; i++) + pdf_array_push_real(ctx, border_col, + PyFloat_AsDouble(PySequence_GetItem(value, i))); + pdf_dict_putl_drop(ctx, annot->obj, border_col, PDF_NAME(MK), + PDF_NAME(BC), NULL); + } + Py_CLEAR(value); + JM_PyErr_Clear; + + // entry ignored - may be later use + /* + int text_type = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, + "text_type")); + JM_PyErr_Clear; + */ + + // max text len ----------------------------------------------------------- + if (field_type == PDF_WIDGET_TYPE_TEXT) + { + int text_maxlen = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, + "text_maxlen")); + if (text_maxlen) + pdf_dict_put_int(ctx, annot->obj, PDF_NAME(MaxLen), text_maxlen); + JM_PyErr_Clear; + } + + // choice values ---------------------------------------------------------- + if (field_type == PDF_WIDGET_TYPE_LISTBOX || + field_type == PDF_WIDGET_TYPE_COMBOBOX) + { + value = PyObject_GetAttrString(Widget, "choice_values"); + JM_set_choice_options(ctx, annot, value); + Py_CLEAR(value); + } + JM_PyErr_Clear; + + // border style ----------------------------------------------------------- + pdf_obj *val = JM_get_border_style(ctx, + PyObject_GetAttrString(Widget, "border_style")); + pdf_dict_putl_drop(ctx, annot->obj, val, PDF_NAME(BS), PDF_NAME(S), NULL); + + // border width ----------------------------------------------------------- + float border_width = (float) PyFloat_AsDouble(PyObject_GetAttrString(Widget, + "border_width")); + pdf_dict_putl_drop(ctx, annot->obj, pdf_new_real(ctx, border_width), + PDF_NAME(BS), PDF_NAME(W), NULL); + JM_PyErr_Clear; + + // /DA string ------------------------------------------------------------- + char *da = JM_Python_str_AsChar(PyObject_GetAttrString(Widget, "_text_da")); + if (da) + { + pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(DA), da); + JM_Python_str_DelForPy3(da); + pdf_dict_dels(ctx, annot->obj, "DS"); // unsupported + pdf_dict_dels(ctx, annot->obj, "RC"); // unsupported + } + JM_PyErr_Clear; + + // field flags ------------------------------------------------------------ + int field_flags = 0, Ff = 0; + if (field_type != PDF_WIDGET_TYPE_CHECKBOX) + { + field_flags = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, + "field_flags")); + if (!PyErr_Occurred()) + { + Ff = pdf_get_field_flags(ctx, pdf, annot->obj); + Ff |= field_flags; } -SWIGINTERN PyObject *fz_document_s_isPDF(struct fz_document_s *self){ - if (pdf_specifics(gctx, self)) Py_RETURN_TRUE; - else Py_RETURN_FALSE; + JM_PyErr_Clear; + } + pdf_dict_put_int(ctx, annot->obj, PDF_NAME(Ff), Ff); + + // button caption --------------------------------------------------------- + if (field_type == PDF_WIDGET_TYPE_RADIOBUTTON || + field_type == PDF_WIDGET_TYPE_PUSHBUTTON || + field_type == PDF_WIDGET_TYPE_CHECKBOX) + { + char *ca = JM_Python_str_AsChar(PyObject_GetAttrString(Widget, + "button_caption")); + if (ca) + { + pdf_dict_putl(ctx, annot->obj, pdf_new_text_string(ctx, ca), + PDF_NAME(MK), PDF_NAME(CA), NULL); + JM_Python_str_DelForPy3(ca); } -SWIGINTERN PyObject *fz_document_s_isDirty(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); - if (!pdf) Py_RETURN_FALSE; - return JM_BOOL(pdf_has_unsaved_changes(gctx, pdf)); + JM_PyErr_Clear; + } + + // field value ------------------------------------------------------------ + // MuPDF function "pdf_field_set_value" always sets strings. For button + // fields this may lead to an unrecognized state for some PDF viewers. + //------------------------------------------------------------------------- + value = PyObject_GetAttrString(Widget, "field_value"); + int result = 0; + char *text = NULL; + switch(field_type) + { + case PDF_WIDGET_TYPE_CHECKBOX: + case PDF_WIDGET_TYPE_RADIOBUTTON: + if (PyObject_RichCompareBool(value, Py_True, Py_EQ)) + { + result = pdf_field_set_value(ctx, pdf, annot->obj, "Yes"); + pdf_dict_put_name(ctx, annot->obj, PDF_NAME(V), "Yes"); } -SWIGINTERN int fz_document_s__getGCTXerrcode(struct fz_document_s *self){ - return fz_caught(gctx); + else + { + result = pdf_field_set_value(ctx, pdf, annot->obj, "Off"); + pdf_dict_put(ctx, annot->obj, PDF_NAME(V), PDF_NAME(Off)); } -SWIGINTERN char const *fz_document_s__getGCTXerrmsg(struct fz_document_s *self){ - return fz_caught_message(gctx); - } -SWIGINTERN int fz_document_s_authenticate(struct fz_document_s *self,char const *pass){ - return fz_authenticate_password(gctx, self, pass); - } -SWIGINTERN PyObject *fz_document_s_save(struct fz_document_s *self,char *filename,int garbage,int clean,int deflate,int incremental,int ascii,int expand,int linear,int pretty){ - int errors = 0; - pdf_write_options opts = { 0 }; - opts.do_incremental = incremental; - opts.do_ascii = ascii; - opts.do_compress = deflate; - opts.do_compress_images = deflate; - opts.do_compress_fonts = deflate; - opts.do_decompress = expand; - opts.do_garbage = garbage; - opts.do_linear = linear; - opts.do_clean = clean; - opts.do_pretty = pretty; - opts.do_sanitize = clean; - opts.continue_on_error = 1; - opts.errors = &errors; - pdf_document *pdf = pdf_specifics(gctx, self); - fz_try(gctx) - { - assert_PDF(pdf); - if (fz_count_pages(gctx, self) < 1) - THROWMSG("document has no pages"); - if ((incremental) && (fz_needs_password(gctx, self))) - THROWMSG("decrypted file - save to new"); - pdf_finish_edit(gctx, pdf); - JM_embedded_clean(gctx, pdf); - pdf_save_document(gctx, pdf, filename, &opts); - pdf->dirty = 0; - } - fz_catch(gctx) return NULL; - return NONE; - } -SWIGINTERN PyObject *fz_document_s_write(struct fz_document_s *self,int garbage,int clean,int deflate,int ascii,int expand,int linear,int pretty){ - PyObject *r; - struct fz_buffer_s *res = NULL; - fz_output *out = NULL; - int errors = 0; - pdf_write_options opts = { 0 }; - opts.do_incremental = 0; - opts.do_ascii = ascii; - opts.do_compress = deflate; - opts.do_compress_images = deflate; - opts.do_compress_fonts = deflate; - opts.do_decompress = expand; - opts.do_garbage = garbage; - opts.do_linear = linear; - opts.do_clean = clean; - opts.do_pretty = pretty; - opts.do_sanitize = clean; - opts.continue_on_error = 1; - opts.errors = &errors; - pdf_document *pdf = pdf_specifics(gctx, self); - fz_var(out); - fz_var(res); - fz_try(gctx) - { - assert_PDF(pdf); - if (fz_count_pages(gctx, self) < 1) - THROWMSG("document has zero pages"); - pdf_finish_edit(gctx, pdf); - JM_embedded_clean(gctx, pdf); - res = fz_new_buffer(gctx, 1024); - out = fz_new_output_with_buffer(gctx, res); - pdf_write_document(gctx, pdf, out, &opts); - pdf->dirty = 0; - r = JM_BinFromBuffer(gctx, res); - } - fz_always(gctx) - { - fz_drop_output(gctx, out); - fz_drop_buffer(gctx, res); - } - fz_catch(gctx) return NULL; - return r; - } -SWIGINTERN PyObject *fz_document_s_insertPDF(struct fz_document_s *self,struct fz_document_s *docsrc,int from_page,int to_page,int start_at,int rotate,int links){ - pdf_document *pdfout = pdf_specifics(gctx, self); - pdf_document *pdfsrc = pdf_specifics(gctx, docsrc); - int outCount = fz_count_pages(gctx, self); - int srcCount = fz_count_pages(gctx, docsrc); - int fp, tp, sa; - // local copies of page numbers - fp = from_page; - tp = to_page; - sa = start_at; - /* normalize page specifications */ - if (fp < 0) fp = 0; - if (fp > srcCount - 1) fp = srcCount - 1; - if (tp < 0) tp = srcCount - 1; - if (tp > srcCount - 1) tp = srcCount - 1; - if (sa < 0) sa = outCount; - if (sa > outCount) sa = outCount; - fz_try(gctx) - { - if (!pdfout || !pdfsrc) THROWMSG("source or target not a PDF"); - merge_range(gctx, pdfout, pdfsrc, fp, tp, sa, rotate); - } - fz_catch(gctx) return NULL; - pdfout->dirty = 1; - return NONE; - } -SWIGINTERN int fz_document_s_insertPage(struct fz_document_s *self,int pno,PyObject *text,float fontsize,float width,float height,int idx,char *fontname,char *fontfile,int set_simple,PyObject *color){ - pdf_document *pdf = pdf_specifics(gctx, self); - fz_rect mediabox = { 0, 0, 595, 842 }; // ISO-A4 portrait values - mediabox.x1 = width; - mediabox.y1 = height; - pdf_obj *resources = NULL, *page_obj = NULL; - fz_buffer *contents = NULL; - fz_try(gctx) - { - assert_PDF(pdf); - if (pno < -1) THROWMSG("invalid page number(s)"); - // create /Resources and /Contents objects - resources = pdf_add_object_drop(gctx, pdf, pdf_new_dict(gctx, pdf, 1)); - contents = fz_new_buffer(gctx, 10); - fz_append_string(gctx, contents, ""); - fz_terminate_buffer(gctx, contents); - page_obj = pdf_add_page(gctx, pdf, &mediabox, 0, resources, contents); - pdf_insert_page(gctx, pdf, pno , page_obj); - pdf_finish_edit(gctx, pdf); - } - fz_always(gctx) - { - fz_drop_buffer(gctx, contents); - pdf_drop_obj(gctx, page_obj); - } - fz_catch(gctx) return -1; - pdf->dirty = 1; - return 0; - } -SWIGINTERN PyObject *fz_document_s_select(struct fz_document_s *self,PyObject *pyliste){ - // preparatory stuff: - // (1) get underlying pdf document, - // (2) transform Python list into integer array - - pdf_document *pdf = pdf_specifics(gctx, self); - fz_try(gctx) - { - assert_PDF(pdf); - if (!PySequence_Check(pyliste)) - THROWMSG("expected a sequence"); - if (PySequence_Size(pyliste) < 1) - THROWMSG("len(sequence) invalid"); - // now call retainpages (code copy of fz_clean_file.c) - globals glo = {0}; - glo.ctx = gctx; - glo.doc = pdf; - retainpages(gctx, &glo, pyliste); - pdf_finish_edit(gctx, pdf); - } - fz_catch(gctx) return NULL; - pdf->dirty = 1; - return NONE; + break; + default: + text = JM_Python_str_AsChar(value); + if (text) + { + result = pdf_field_set_value(ctx, pdf, annot->obj, (const char *)text); + JM_Python_str_DelForPy3(text); } -SWIGINTERN PyObject *fz_document_s_permissions(struct fz_document_s *self){ - PyObject *p = JM_BOOL(fz_has_permission(gctx, self, FZ_PERMISSION_PRINT)); - PyObject *e = JM_BOOL(fz_has_permission(gctx, self, FZ_PERMISSION_EDIT)); - PyObject *c = JM_BOOL(fz_has_permission(gctx, self, FZ_PERMISSION_COPY)); - PyObject *n = JM_BOOL(fz_has_permission(gctx, self, FZ_PERMISSION_ANNOTATE)); - PyObject *res = PyDict_New(); - PyDict_SetItemString(res, "print", p); - PyDict_SetItemString(res, "edit", e); - PyDict_SetItemString(res, "copy", c); - PyDict_SetItemString(res, "note", n); - return res; + } + Py_CLEAR(value); + pdf_dirty_annot(ctx, annot); + pdf_update_page(gctx, page); +} + + + +//---------------------------------------------------------------------------- +// portfolio schema types +//---------------------------------------------------------------------------- +#define PDF_SCHEMA_NUMBER 0 +#define PDF_SCHEMA_SIZE 1 +#define PDF_SCHEMA_TEXT 2 +#define PDF_SCHEMA_DATE 3 +#define PDF_SCHEMA_DESC 4 +#define PDF_SCHEMA_MODDATE 5 +#define PDF_SCHEMA_CREATIONDATE 6 +#define PDF_SCHEMA_FILENAME 7 +#define PDF_SCHEMA_UNKNOWN 8 +//----------------------------------------------------------------------------- +// finds index of an embedded file in a pdf +// Object "id" contains either the entry name (str) or the index. An index is +// only checked for valid range. +//----------------------------------------------------------------------------- +int JM_find_embedded(fz_context *ctx, PyObject *id, pdf_document *pdf) +{ + char *name = NULL; + char *tname= NULL; + int i = -1, count = pdf_count_portfolio_entries(ctx, pdf); + if (count < 1) return -1; + + // just return the integer id if in range + if (PyInt_Check(id)) + { + i = (int) PyInt_AsLong(id); + if (!INRANGE(i, 0, (count-1))) return -1; + return i; + } + name = JM_Python_str_AsChar(id); + if (!name || strlen(name) == 0) return -1; + for (i = 0; i < count; i++) + { + tname = (char *) pdf_to_text_string(ctx, pdf_portfolio_entry_name(ctx, pdf, i)); + if (!strcmp(tname, name)) + { + JM_Python_str_DelForPy3(name); + return i; } -SWIGINTERN PyObject *fz_document_s__getCharWidths(struct fz_document_s *self,int xref,int limit,int idx){ - pdf_document *pdf = pdf_specifics(gctx, self); - PyObject *wlist = NULL; - int i, glyph, mylimit; - mylimit = limit; - if (mylimit < 256) mylimit = 256; - int cwlen = 0; - const char *data; - int size; - fz_font *font = NULL; - fz_buffer *buf = NULL; - pdf_obj *basefont = NULL; - const char *bfname = NULL; - fz_try(gctx) - { - assert_PDF(pdf); - if (xref < 1) THROWMSG("xref must at least 1"); - pdf_obj *o = pdf_load_object(gctx, pdf, xref); - if (pdf_is_dict(gctx, o)) - { - basefont = pdf_dict_get(gctx, o, PDF_NAME_BaseFont); - if (pdf_is_name(gctx, basefont)) - { - bfname = (char *) pdf_to_name(gctx, basefont); - data = fz_lookup_base14_font(gctx, bfname, &size); - if (data) - { - font = fz_new_font_from_memory(gctx, bfname, data, size, 0, 0); - } - else - { - buf = fontbuffer(gctx, pdf, xref); - if (!buf) THROWMSG("xref is not a supported font"); - font = fz_new_font_from_buffer(gctx, NULL, buf, idx, 0); - } - } - } - else - { - buf = fontbuffer(gctx, pdf, xref); - if (!buf) THROWMSG("xref is not a supported font"); - font = fz_new_font_from_buffer(gctx, NULL, buf, idx, 0); - } - wlist = PyList_New(0); - for (i = 0; i < mylimit; i++) - { - glyph = fz_encode_character(gctx, font, i); - if (glyph > 0) - { - PyList_Append(wlist, Py_BuildValue("(i, f)", glyph, fz_advance_glyph(gctx, font, glyph, 0))); - } - else - { - PyList_Append(wlist, Py_BuildValue("(i, f)", glyph, 0.0)); - } - } - } - fz_always(gctx) - { - fz_drop_buffer(gctx, buf); - fz_drop_font(gctx, font); - } - fz_catch(gctx) - { - return NULL; - } - return wlist; - } -SWIGINTERN PyObject *fz_document_s__getPageObjNumber(struct fz_document_s *self,int pno){ - int pageCount = fz_count_pages(gctx, self); - pdf_document *pdf = pdf_specifics(gctx, self); - fz_try(gctx) - { - if (pno >= pageCount) THROWMSG("invalid page number(s)"); - assert_PDF(pdf); - } - fz_catch(gctx) return NULL; - int n = pno; - while (n < 0) n += pageCount; - pdf_obj *pageref = pdf_lookup_page_obj(gctx, pdf, n); - long objnum = (long) pdf_to_num(gctx, pageref); - long objgen = (long) pdf_to_gen(gctx, pageref); - return Py_BuildValue("(l, l)", objnum, objgen); - } -SWIGINTERN PyObject *fz_document_s__getPageInfo(struct fz_document_s *self,int pno,int what){ - pdf_document *pdf = pdf_specifics(gctx, self); - int pageCount = fz_count_pages(gctx, self); - pdf_obj *pageref, *rsrc; - PyObject *liste = NULL; // returned object - int n = pno; // pno < 0 is allowed - while (n < 0) n += pageCount; - fz_var(liste); - fz_try(gctx) - { - if (n >= pageCount) THROWMSG("invalid page number(s)"); - assert_PDF(pdf); - pageref = pdf_lookup_page_obj(gctx, pdf, n); - rsrc = pdf_dict_get(gctx, pageref, PDF_NAME_Resources); - if (!pageref || !rsrc) THROWMSG("cannot retrieve page info"); - liste = PyList_New(0); - JM_scan_resources(gctx, pdf, rsrc, liste, what); - } - fz_catch(gctx) - { - Py_XDECREF(liste); - return NULL; - } - return liste; - } -SWIGINTERN PyObject *fz_document_s_extractFont(struct fz_document_s *self,int xref,int info_only){ - pdf_document *pdf = pdf_specifics(gctx, self); - - fz_try(gctx) assert_PDF(pdf); - fz_catch(gctx) return NULL; + } + JM_Python_str_DelForPy3(name); + return -1; +} - fz_buffer *buffer = NULL; - pdf_obj *obj, *basefont, *bname; - PyObject *bytes = PyBytes_FromString(""); - char *ext = NULL; - char *fontname = NULL; - PyObject *nulltuple = Py_BuildValue("sssO", "", "", "", bytes); - PyObject *tuple; - Py_ssize_t len = 0; - fz_try(gctx) - { - obj = pdf_load_object(gctx, pdf, xref); - pdf_obj *type = pdf_dict_get(gctx, obj, PDF_NAME_Type); - pdf_obj *subtype = pdf_dict_get(gctx, obj, PDF_NAME_Subtype); - if(pdf_name_eq(gctx, type, PDF_NAME_Font) && - strncmp(pdf_to_name(gctx, subtype), "CIDFontType", 11) != 0) - { - basefont = pdf_dict_get(gctx, obj, PDF_NAME_BaseFont); - if (!basefont || pdf_is_null(gctx, basefont)) - bname = pdf_dict_get(gctx, obj, PDF_NAME_Name); - else - bname = basefont; - ext = fontextension(gctx, pdf, xref); - if (strcmp(ext, "n/a") != 0 && !info_only) - { - buffer = fontbuffer(gctx, pdf, xref); - bytes = JM_BinFromBuffer(gctx, buffer); - fz_drop_buffer(gctx, buffer); - } - fontname = JM_ASCIIFromChar(pdf_to_name(gctx, bname)); - tuple = Py_BuildValue("sssO", - fontname, - ext, - pdf_to_name(gctx, subtype), - bytes); - } - else - { - tuple = nulltuple; - } - } - fz_always(gctx) - { - JM_PyErr_Clear; - JM_Free(fontname); - } +//----------------------------------------------------------------------------- +// Return the /Names object for embedded files +//----------------------------------------------------------------------------- +pdf_obj *JM_embedded_names(fz_context *ctx, pdf_document *pdf) +{ + pdf_obj *names = NULL, *kids = NULL, *o = NULL; + int i, n; + names = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), PDF_NAME(Root), PDF_NAME(Names), PDF_NAME(EmbeddedFiles), + PDF_NAME(Names), NULL); + if (names) return names; + + // not found, therefore a /Kids object contains the /Names + kids = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), PDF_NAME(Root), + PDF_NAME(Names), PDF_NAME(EmbeddedFiles), + PDF_NAME(Kids), NULL); + //------------------------------------------------------------------------- + // 'kids' is an array of indirect references pointing to dictionaries. + // Only /Limits and /Names can occur in those dictionaries + // We take the first encounter of /Names. + //------------------------------------------------------------------------- + if (!pdf_is_array(ctx, kids) || !(n = pdf_array_len(ctx, kids))) + return NULL; // should never occur - fz_catch(gctx) - { - tuple = Py_BuildValue("sssO", "invalid-name", "", "", bytes); - } - return tuple; - } -SWIGINTERN PyObject *fz_document_s_extractImage(struct fz_document_s *self,int xref){ - pdf_document *pdf = pdf_specifics(gctx, self); - fz_try(gctx) - { - assert_PDF(pdf); - if (!INRANGE(xref, 1, pdf_xref_len(gctx, pdf)-1)) - THROWMSG("xref out of range"); - } - fz_catch(gctx) return NULL; + for (i = 0; i < n; i++) + { + o = pdf_resolve_indirect(ctx, pdf_array_get(ctx, kids, i)); + names = pdf_dict_get(ctx, o, PDF_NAME(Names)); + if (names) return names; + } + return NULL; // should never execute +} - fz_buffer *buffer = NULL, *freebuf = NULL; - fz_var(freebuf); - fz_pixmap *pix = NULL; - fz_var(pix); - pdf_obj *obj = NULL; - PyObject *rc = NULL; - unsigned char ext[5]; - fz_image *image = NULL; - fz_var(image); - fz_output *out = NULL; - fz_var(out); - fz_compressed_buffer *cbuf = NULL; - int type = 0, n = 0, xres = 0, yres = 0; - int smask = 0, width = 0, height = 0; - fz_try(gctx) - { - obj = pdf_new_indirect(gctx, pdf, xref, 0); - pdf_obj *subtype = pdf_dict_get(gctx, obj, PDF_NAME_Subtype); - if (pdf_name_eq(gctx, subtype, PDF_NAME_Image)) - { - image = pdf_load_image(gctx, pdf, obj); - cbuf = fz_compressed_image_buffer(gctx, image); - type = cbuf == NULL ? FZ_IMAGE_UNKNOWN : cbuf->params.type; - // ensure returning a PNG for unsupported images ---------- - if (type < FZ_IMAGE_BMP) type = FZ_IMAGE_UNKNOWN; +//----------------------------------------------------------------------------- +// perform some cleaning if we have /EmbeddedFiles: +// (1) remove any /Limits if /Names exists +// (2) remove any empty /Collection +// (3) set /PageMode/UseAttachments +//----------------------------------------------------------------------------- +void JM_embedded_clean(fz_context *ctx, pdf_document *pdf) +{ + pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, pdf), PDF_NAME(Root)); - pdf_obj *o = pdf_dict_get(gctx, obj, PDF_NAME_SMask); - if (o) smask = pdf_to_num(gctx, o); + // remove any empty /Collection entry + pdf_obj *coll = pdf_dict_get(ctx, root, PDF_NAME(Collection)); + if (coll && pdf_dict_len(ctx, coll) == 0) + pdf_dict_del(ctx, root, PDF_NAME(Collection)); + + if (!pdf_count_portfolio_entries(ctx, pdf)) + return; - o = pdf_dict_get(gctx, obj, PDF_NAME_Width); - if (o) width = pdf_to_int(gctx, o); + pdf_obj *efiles = pdf_dict_getl(ctx, root, PDF_NAME(Names), + PDF_NAME(EmbeddedFiles), NULL); + if (efiles) // we have embedded files + { // make sure they are displayed + pdf_dict_put_name(ctx, root, PDF_NAME(PageMode), "UseAttachments"); + // remove the limits entry: seems to be a MuPDF bug + pdf_dict_del(ctx, efiles, PDF_NAME(Limits)); + } - o = pdf_dict_get(gctx, obj, PDF_NAME_Height); - if (o) height = pdf_to_int(gctx, o); + return; +} - n = fz_colorspace_n(gctx, image->colorspace); - fz_image_resolution(image, &xres, &yres); - if (type != FZ_IMAGE_UNKNOWN) - { - buffer = cbuf->buffer; // we will return this buffer - switch(type) - { - case(FZ_IMAGE_BMP): strcpy(ext, "bmp"); break; - case(FZ_IMAGE_GIF): strcpy(ext, "gif"); break; - case(FZ_IMAGE_JPEG): strcpy(ext, "jpeg"); break; - case(FZ_IMAGE_JPX): strcpy(ext, "jpx"); break; - case(FZ_IMAGE_JXR): strcpy(ext, "jxr"); break; - case(FZ_IMAGE_PNM): strcpy(ext, "pnm"); break; - case(FZ_IMAGE_TIFF): strcpy(ext, "tiff"); break; - default: strcpy(ext, "png"); break; - } - } - else // we need a pixmap for making the PNG buffer - { - pix = fz_get_pixmap_from_image(gctx, image, - NULL, NULL, NULL, NULL); - n = pix->n; - // only gray & rgb pixmaps support PNG! - if (pix->colorspace && - pix->colorspace != fz_device_gray(gctx) && - pix->colorspace != fz_device_rgb(gctx)) - { - fz_pixmap *pix2 = fz_convert_pixmap(gctx, pix, - fz_device_rgb(gctx), NULL, NULL, NULL, 1); - fz_drop_pixmap(gctx, pix); - pix = pix2; - } +//----------------------------------------------------------------------------- +// insert new embedded file in PDF +// not necessarily an /EmbeddedFiles entry +//----------------------------------------------------------------------------- +pdf_obj *JM_embed_file(fz_context *ctx, pdf_document *pdf, fz_buffer *buf, + char *filename, char *ufilename, char *desc) +{ + size_t len = 0; + pdf_obj *ef, *f, *params; + pdf_obj *val = NULL; + fz_buffer *tbuf; + fz_var(val); + fz_try(ctx) + { + val = pdf_new_dict(ctx, pdf, 6); + pdf_dict_put_dict(ctx, val, PDF_NAME(CI), 4); + ef = pdf_dict_put_dict(ctx, val, PDF_NAME(EF), 4); + pdf_dict_put_text_string(ctx, val, PDF_NAME(F), filename); + pdf_dict_put_text_string(ctx, val, PDF_NAME(UF), filename); + pdf_dict_put_text_string(ctx, val, PDF_NAME(Desc), desc); + pdf_dict_put(ctx, val, PDF_NAME(Type), PDF_NAME(Filespec)); + tbuf = fz_new_buffer(ctx, strlen(filename)+1); + fz_append_string(ctx, tbuf, filename); + fz_terminate_buffer(ctx, tbuf); + pdf_dict_put_drop(ctx, ef, PDF_NAME(F), + (f = pdf_add_stream(ctx, pdf, tbuf, NULL, 0))); + fz_drop_buffer(ctx, tbuf); + JM_update_stream(ctx, pdf, f, buf); + len = fz_buffer_storage(ctx, buf, NULL); + pdf_dict_put_int(ctx, f, PDF_NAME(DL), len); + pdf_dict_put_int(ctx, f, PDF_NAME(Length), len); + params = pdf_dict_put_dict(ctx, f, PDF_NAME(Params), 4); + pdf_dict_put_int(ctx, params, PDF_NAME(Size), len); + } + fz_always(ctx) + { + ; + } + fz_catch(ctx) fz_rethrow(ctx); + return val; +} - freebuf = fz_new_buffer(gctx, 2048); - out = fz_new_output_with_buffer(gctx, freebuf); - fz_write_pixmap_as_png(gctx, out, pix); - buffer = freebuf; - strcpy(ext, "png"); - } - PyObject *bytes = JM_BinFromBuffer(gctx, buffer); - rc = Py_BuildValue("{s:s,s:i,s:i,s:i,s:i,s:i,s:i,s:O}", - "ext", ext, - "smask", smask, - "width", width, - "height", height, - "colorspace", n, - "xres", xres, - "yres", yres, - "image", bytes); - Py_CLEAR(bytes); - } - else - rc = PyDict_New(); - } - fz_always(gctx) - { - fz_drop_image(gctx, image); - fz_drop_buffer(gctx, freebuf); - fz_drop_output(gctx, out); - fz_drop_pixmap(gctx, pix); - pdf_drop_obj(gctx, obj); - } - fz_catch(gctx) {;} - return rc; - } -SWIGINTERN PyObject *fz_document_s__delToC(struct fz_document_s *self){ - PyObject *xrefs = PyList_New(0); // create Python list - pdf_document *pdf = pdf_specifics(gctx, self); // conv doc to pdf - if (!pdf) return xrefs; // not a pdf - pdf_obj *root, *olroot, *first; - // get the main root - root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root); - // get the outline root - olroot = pdf_dict_get(gctx, root, PDF_NAME_Outlines); - if (!olroot) return xrefs; // no outlines or some problem - int objcount, argc, i; - int *res; - objcount = 0; - argc = 0; - first = pdf_dict_get(gctx, olroot, PDF_NAME_First); // first outline - if (!first) return xrefs; - argc = countOutlines(gctx, first, argc); // get number of outlines - if (argc < 1) return xrefs; - res = JM_Alloc(int, (size_t) argc); // object number table - objcount = fillOLNumbers(gctx, res, first, objcount, argc); // fill table - pdf_dict_del(gctx, olroot, PDF_NAME_First); - pdf_dict_del(gctx, olroot, PDF_NAME_Last); - pdf_dict_del(gctx, olroot, PDF_NAME_Count); - - for (i = 0; i < objcount; i++) - { - pdf_delete_object(gctx, pdf, res[i]); // delete outline item - PyList_Append(xrefs, PyInt_FromLong((long) res[i])); - } - JM_Free(res); - pdf->dirty = 1; - return xrefs; - } -SWIGINTERN PyObject *fz_document_s_isFormPDF(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); - if (!pdf) Py_RETURN_FALSE; // not a PDF - pdf_obj *form = NULL; - pdf_obj *fields = NULL; - int have_form = 0; // preset indicator - fz_try(gctx) +//---------------------------------------------------------------------------- +// Helpers for document page selection - main logic was imported +// from pdf_clean_file.c. But instead of analyzing a string-based spec of +// selected pages, we accept a Python sequence. +//---------------------------------------------------------------------------- +typedef struct globals_s +{ + pdf_document *doc; + fz_context *ctx; +} globals; + +int string_in_names_list(fz_context *ctx, pdf_obj *p, pdf_obj *names_list) +{ + int n = pdf_array_len(ctx, names_list); + int i; + char *str = pdf_to_str_buf(ctx, p); + + for (i = 0; i < n ; i += 2) + { + if (!strcmp(pdf_to_str_buf(ctx, pdf_array_get(ctx, names_list, i)), str)) + return 1; + } + return 0; +} + +//---------------------------------------------------------------------------- +// Recreate page tree to only retain specified pages. +//---------------------------------------------------------------------------- +void retainpage(fz_context *ctx, pdf_document *doc, pdf_obj *parent, pdf_obj *kids, int page) +{ + pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, page); + + pdf_flatten_inheritable_page_items(ctx, pageref); + + pdf_dict_put(ctx, pageref, PDF_NAME(Parent), parent); + + /* Store page object in new kids array */ + pdf_array_push(ctx, kids, pageref); +} + +int dest_is_valid_page(fz_context *ctx, pdf_obj *obj, int *page_object_nums, int pagecount) +{ + int i; + int num = pdf_to_num(ctx, obj); + + if (num == 0) + return 0; + for (i = 0; i < pagecount; i++) + { + if (page_object_nums[i] == num) + return 1; + } + return 0; +} + +int dest_is_valid(fz_context *ctx, pdf_obj *o, int page_count, int *page_object_nums, pdf_obj *names_list) +{ + pdf_obj *p; + + p = pdf_dict_get(ctx, o, PDF_NAME(A)); + if (pdf_name_eq(ctx, pdf_dict_get(ctx, p, PDF_NAME(S)), PDF_NAME(GoTo)) && + !string_in_names_list(ctx, pdf_dict_get(ctx, p, PDF_NAME(D)), names_list)) + return 0; + + p = pdf_dict_get(ctx, o, PDF_NAME(Dest)); + if (p == NULL) + {} + else if (pdf_is_string(ctx, p)) + { + return string_in_names_list(ctx, p, names_list); + } + else if (!dest_is_valid_page(ctx, pdf_array_get(ctx, p, 0), page_object_nums, page_count)) + return 0; + + return 1; +} + +int strip_outlines(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int page_count, int *page_object_nums, pdf_obj *names_list); + +int strip_outline(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int page_count, int *page_object_nums, pdf_obj *names_list, pdf_obj **pfirst, pdf_obj **plast) +{ + pdf_obj *prev = NULL; + pdf_obj *first = NULL; + pdf_obj *current; + int count = 0; + + for (current = outlines; current != NULL; ) + { + int nc; + + /*********************************************************************/ + // Strip any children to start with. This takes care of + // First / Last / Count for us. + /*********************************************************************/ + nc = strip_outlines(ctx, doc, current, page_count, page_object_nums, names_list); + + if (!dest_is_valid(ctx, current, page_count, page_object_nums, names_list)) + { + if (nc == 0) { - form = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root, PDF_NAME_AcroForm, NULL); - if (form) // form obj exists + /*************************************************************/ + // Outline with invalid dest and no children. Drop it by + // pulling the next one in here. + /*************************************************************/ + pdf_obj *next = pdf_dict_get(ctx, current, PDF_NAME(Next)); + if (next == NULL) { - fields = pdf_dict_get(gctx, form, PDF_NAME_Fields); - if (fields && pdf_array_len(gctx, fields) > 0) have_form = 1; + // There is no next one to pull in + if (prev != NULL) + pdf_dict_del(ctx, prev, PDF_NAME(Next)); } - } - fz_catch(gctx) Py_RETURN_FALSE; // any problem yields false - if (!have_form) Py_RETURN_FALSE; // no form / no fields - Py_RETURN_TRUE; - } -SWIGINTERN PyObject *fz_document_s_FormFonts(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); - if (!pdf) return NONE; // not a PDF - pdf_obj *fonts = NULL; - PyObject *liste = PyList_New(0); - fz_try(gctx) - { - fonts = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root, PDF_NAME_AcroForm, PDF_NAME_DR, PDF_NAME_Font, NULL); - if (fonts && pdf_is_dict(gctx, fonts)) // fonts exist + else if (prev != NULL) { - int i, n = pdf_dict_len(gctx, fonts); - for (i = 0; i < n; i++) - { - pdf_obj *f = pdf_dict_get_key(gctx, fonts, i); - PyList_Append(liste, Py_BuildValue("s", pdf_to_name(gctx, f))); - } + pdf_dict_put(ctx, prev, PDF_NAME(Next), next); + pdf_dict_put(ctx, next, PDF_NAME(Prev), prev); } + else + { + pdf_dict_del(ctx, next, PDF_NAME(Prev)); + } + current = next; } - fz_catch(gctx) NONE; // any problem yields None - return liste; - } -SWIGINTERN PyObject *fz_document_s__addFormFont(struct fz_document_s *self,char *name,char *font){ - pdf_document *pdf = pdf_specifics(gctx, self); - if (!pdf) NONE; // not a PDF - pdf_obj *fonts = NULL; - fz_try(gctx) - { - fonts = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root, - PDF_NAME_AcroForm, PDF_NAME_DR, PDF_NAME_Font, NULL); - if (!fonts || !pdf_is_dict(gctx, fonts)) - THROWMSG("PDF has no form fonts yet"); - pdf_obj *k = pdf_new_name(gctx, pdf, (const char *) name); - pdf_obj *v = pdf_new_obj_from_str(gctx, pdf, font); - pdf_dict_put(gctx, fonts, k, v); - } - fz_catch(gctx) NULL; - return NONE; - } -SWIGINTERN int fz_document_s__getOLRootNumber(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); - fz_try(gctx) assert_PDF(pdf); - fz_catch(gctx) return -1; - - pdf_obj *root, *olroot, *ind_obj; - // get main root - root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root); - // get outline root - olroot = pdf_dict_get(gctx, root, PDF_NAME_Outlines); - if (olroot == NULL) - { - olroot = pdf_new_dict(gctx, pdf, 4); - pdf_dict_put(gctx, olroot, PDF_NAME_Type, PDF_NAME_Outlines); - ind_obj = pdf_add_object(gctx, pdf, olroot); - pdf_dict_put(gctx, root, PDF_NAME_Outlines, ind_obj); - olroot = pdf_dict_get(gctx, root, PDF_NAME_Outlines); - pdf_drop_obj(gctx, ind_obj); - pdf->dirty = 1; - } - return pdf_to_num(gctx, olroot); - } -SWIGINTERN int fz_document_s__getNewXref(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); /* conv doc to pdf*/ - fz_try(gctx) assert_PDF(pdf); - fz_catch(gctx) return -1; - pdf->dirty = 1; - return pdf_create_object(gctx, pdf); - } -SWIGINTERN int fz_document_s__getXrefLength(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); - if (!pdf) return 0; - return pdf_xref_len(gctx, pdf); - } -SWIGINTERN int fz_document_s__getXmlMetadataXref(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); // get pdf document - pdf_obj *xml; - int xref = 0; - fz_try(gctx) + else { - assert_PDF(pdf); - pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root); - if (!root) THROWMSG("could not load root object"); - xml = pdf_dict_gets(gctx, root, "Metadata"); - if (xml) xref = pdf_to_num(gctx, xml); + // Outline with invalid dest, but children. Just drop the dest. + pdf_dict_del(ctx, current, PDF_NAME(Dest)); + pdf_dict_del(ctx, current, PDF_NAME(A)); + current = pdf_dict_get(ctx, current, PDF_NAME(Next)); } - fz_catch(gctx) return -1; - return xref; } -SWIGINTERN PyObject *fz_document_s__delXmlMetadata(struct fz_document_s *self){ - pdf_document *pdf = pdf_specifics(gctx, self); // get pdf document - fz_try(gctx) - { - assert_PDF(pdf); - pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Root); - if (root) pdf_dict_dels(gctx, root, "Metadata"); - } - fz_catch(gctx) return NULL; - pdf->dirty = 1; - return NONE; + else + { + // Keep this one + if (first == NULL) + first = current; + prev = current; + current = pdf_dict_get(ctx, current, PDF_NAME(Next)); + count++; } -SWIGINTERN char const *fz_document_s__getObjectString(struct fz_document_s *self,int xref){ - pdf_document *pdf = pdf_specifics(gctx, self); // conv doc to pdf - pdf_obj *obj = NULL; - fz_buffer *res = NULL; - fz_output *out = NULL; - fz_try(gctx) - { - assert_PDF(pdf); - int xreflen = pdf_xref_len(gctx, pdf); - if (!INRANGE(xref, 1, xreflen-1)) - THROWMSG("xref out of range"); - res = fz_new_buffer(gctx, 1024); - out = fz_new_output_with_buffer(gctx, res); - obj = pdf_load_object(gctx, pdf, xref); - pdf_print_obj(gctx, out, pdf_resolve_indirect(gctx, obj), 1); - } - fz_always(gctx) - { - pdf_drop_obj(gctx, obj); - fz_drop_output(gctx, out); + } + + *pfirst = first; + *plast = prev; + + return count; +} + +int strip_outlines(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int page_count, int *page_object_nums, pdf_obj *names_list) +{ + int nc; + pdf_obj *first; + pdf_obj *last; + + if (outlines == NULL) + return 0; + + first = pdf_dict_get(ctx, outlines, PDF_NAME(First)); + if (first == NULL) + nc = 0; + else + nc = strip_outline(ctx, doc, first, page_count, page_object_nums, + names_list, &first, &last); + + if (nc == 0) + { + pdf_dict_del(ctx, outlines, PDF_NAME(First)); + pdf_dict_del(ctx, outlines, PDF_NAME(Last)); + pdf_dict_del(ctx, outlines, PDF_NAME(Count)); + } + else + { + int old_count = pdf_to_int(ctx, pdf_dict_get(ctx, outlines, PDF_NAME(Count))); + pdf_dict_put(ctx, outlines, PDF_NAME(First), first); + pdf_dict_put(ctx, outlines, PDF_NAME(Last), last); + pdf_dict_put_drop(ctx, outlines, PDF_NAME(Count), pdf_new_int(ctx, old_count > 0 ? nc : -nc)); + } + return nc; +} + +//---------------------------------------------------------------------------- +// This is called by PyMuPDF: +// liste = page numbers to retain +//---------------------------------------------------------------------------- +void retainpages(fz_context *ctx, globals *glo, PyObject *liste) +{ + pdf_obj *oldroot, *root, *pages, *kids, *countobj, *olddests; + Py_ssize_t argc = PySequence_Size(liste); + pdf_document *doc = glo->doc; + pdf_obj *names_list = NULL; + pdf_obj *outlines; + pdf_obj *ocproperties; + int pagecount = pdf_count_pages(ctx, doc); + + int i; + int *page_object_nums; + +/******************************************************************************/ +// Keep only pages/type and (reduced) dest entries to avoid +// references to dropped pages +/******************************************************************************/ + oldroot = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); + pages = pdf_dict_get(ctx, oldroot, PDF_NAME(Pages)); + olddests = pdf_load_name_tree(ctx, doc, PDF_NAME(Dests)); + outlines = pdf_dict_get(ctx, oldroot, PDF_NAME(Outlines)); + ocproperties = pdf_dict_get(ctx, oldroot, PDF_NAME(OCProperties)); + + root = pdf_new_dict(ctx, doc, 3); + pdf_dict_put(ctx, root, PDF_NAME(Type), pdf_dict_get(ctx, oldroot, PDF_NAME(Type))); + pdf_dict_put(ctx, root, PDF_NAME(Pages), pdf_dict_get(ctx, oldroot, PDF_NAME(Pages))); + if (outlines) + pdf_dict_put(ctx, root, PDF_NAME(Outlines), outlines); + if (ocproperties) + pdf_dict_put(ctx, root, PDF_NAME(OCProperties), ocproperties); + + pdf_update_object(ctx, doc, pdf_to_num(ctx, oldroot), root); + + // Create a new kids array with only the pages we want to keep + kids = pdf_new_array(ctx, doc, 1); + + // Retain pages specified + Py_ssize_t page; + fz_try(ctx) + { + for (page = 0; page < argc; page++) + { + i = (int) PyInt_AsLong(PySequence_GetItem(liste, page)); + if (i < 0 || i >= pagecount) + THROWMSG("invalid page number(s)"); + retainpage(ctx, doc, pages, kids, i); } - fz_catch(gctx) + } + fz_catch(ctx) fz_rethrow(ctx); + + // Update page count and kids array + countobj = pdf_new_int(ctx, pdf_array_len(ctx, kids)); + pdf_dict_put_drop(ctx, pages, PDF_NAME(Count), countobj); + pdf_dict_put_drop(ctx, pages, PDF_NAME(Kids), kids); + + pagecount = pdf_count_pages(ctx, doc); + page_object_nums = fz_calloc(ctx, pagecount, sizeof(*page_object_nums)); + for (i = 0; i < pagecount; i++) + { + pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, i); + page_object_nums[i] = pdf_to_num(ctx, pageref); + } + +/******************************************************************************/ +// If we had an old Dests tree (now reformed as an olddests dictionary), +// keep any entries in there that point to valid pages. +// This may mean we keep more than we need, but it is safe at least. +/******************************************************************************/ + if (olddests) + { + pdf_obj *names = pdf_new_dict(ctx, doc, 1); + pdf_obj *dests = pdf_new_dict(ctx, doc, 1); + int len = pdf_dict_len(ctx, olddests); + + names_list = pdf_new_array(ctx, doc, 32); + + for (i = 0; i < len; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, olddests, i); + pdf_obj *val = pdf_dict_get_val(ctx, olddests, i); + pdf_obj *dest = pdf_dict_get(ctx, val, PDF_NAME(D)); + + dest = pdf_array_get(ctx, dest ? dest : val, 0); + if (dest_is_valid_page(ctx, dest, page_object_nums, pagecount)) { - fz_drop_buffer(gctx, res); - return NULL; + pdf_obj *key_str = pdf_new_string(ctx, pdf_to_name(ctx, key), strlen(pdf_to_name(ctx, key))); + pdf_array_push_drop(ctx, names_list, key_str); + pdf_array_push(ctx, names_list, val); } - return fz_string_from_buffer(gctx, res); } -SWIGINTERN PyObject *fz_document_s__getXrefStream(struct fz_document_s *self,int xref){ - pdf_document *pdf = pdf_specifics(gctx, self); - PyObject *r = NONE; - pdf_obj *obj = NULL; - fz_var(obj); - fz_buffer *res = NULL; - fz_var(res); - fz_try(gctx) + + pdf_dict_put(ctx, dests, PDF_NAME(Names), names_list); + pdf_dict_put(ctx, names, PDF_NAME(Dests), dests); + pdf_dict_put(ctx, root, PDF_NAME(Names), names); + + pdf_drop_obj(ctx, names); + pdf_drop_obj(ctx, dests); + pdf_drop_obj(ctx, olddests); + } + +/*****************************************************************************/ +// Edit each pages /Annot list to remove any links pointing to nowhere. +/*****************************************************************************/ + for (i = 0; i < pagecount; i++) + { + pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, i); + + pdf_obj *annots = pdf_dict_get(ctx, pageref, PDF_NAME(Annots)); + + int len = pdf_array_len(ctx, annots); + int j; + + for (j = 0; j < len; j++) + { + pdf_obj *o = pdf_array_get(ctx, annots, j); + + if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME(Subtype)), PDF_NAME(Link))) + continue; + + if (!dest_is_valid(ctx, o, pagecount, page_object_nums, names_list)) { - assert_PDF(pdf); - int xreflen = pdf_xref_len(gctx, pdf); - if (!INRANGE(xref, 1, xreflen-1)) - THROWMSG("xref out of range"); - obj = pdf_new_indirect(gctx, pdf, xref, 0); - if (pdf_is_stream(gctx, obj)) - { - res = pdf_load_stream_number(gctx, pdf, xref); - r = JM_BinFromBuffer(gctx, res); - } + // Remove this annotation + pdf_array_delete(ctx, annots, j); + len--; + j--; } - fz_always(gctx) + } + } + + if (strip_outlines(ctx, doc, outlines, pagecount, page_object_nums, names_list) == 0) + { + pdf_dict_del(ctx, root, PDF_NAME(Outlines)); + } + + fz_free(ctx, page_object_nums); + pdf_drop_obj(ctx, names_list); + pdf_drop_obj(ctx, root); +} + + +//----------------------------------------------------------------------------- +// Make an XObject from a PDF page +// For a positive xref assume that that object can be used instead +//----------------------------------------------------------------------------- +pdf_obj *JM_xobject_from_page(fz_context *ctx, pdf_document *pdfout, pdf_document *pdfsrc, int pno, fz_rect *mediabox, fz_rect *cropbox, int xref, pdf_graft_map *gmap) +{ + fz_buffer *nres = NULL, *res = NULL; + pdf_obj *xobj1, *contents = NULL, *resources = NULL, *o, *spageref; + int i; + fz_try(ctx) + { + if (pno < 0 || pno >= pdf_count_pages(ctx, pdfsrc)) + THROWMSG("invalid page number(s)"); + spageref = pdf_lookup_page_obj(ctx, pdfsrc, pno); + pdf_obj *mb = pdf_dict_get(ctx, spageref, PDF_NAME(MediaBox)); + if (mb) + *mediabox = pdf_to_rect(ctx, mb); + else + *mediabox = pdf_bound_page(ctx, pdf_load_page(ctx, pdfsrc, pno)); + o = pdf_dict_get(ctx, spageref, PDF_NAME(CropBox)); + if (!o) + { + cropbox->x0 = mediabox->x0; + cropbox->y0 = mediabox->y0; + cropbox->x1 = mediabox->x1; + cropbox->y1 = mediabox->y1; + } + else + *cropbox = pdf_to_rect(ctx, o); + + if (xref > 0) // we can reuse an XObject! + { + if (xref >= pdf_xref_len(ctx, pdfout)) + THROWMSG("xref out of range"); + xobj1 = pdf_new_indirect(ctx, pdfout, xref, 0); + } + else // need to create new XObject + { + // Deep-copy resources object of source page + o = pdf_dict_get(ctx, spageref, PDF_NAME(Resources)); + if (gmap) // use graftmap when possible + resources = pdf_graft_mapped_object(ctx, gmap, o); + else + resources = pdf_graft_object(ctx, pdfout, o); + + // get spgage contents source; combine when several objects + contents = pdf_dict_get(ctx, spageref, PDF_NAME(Contents)); + if (pdf_is_array(ctx, contents)) // more than one! { - fz_drop_buffer(gctx, res); - pdf_drop_obj(gctx, obj); + res = fz_new_buffer(ctx, 1024); + for (i=0; i < pdf_array_len(ctx, contents); i++) + { + nres = pdf_load_stream(ctx, pdf_array_get(ctx, contents, i)); + fz_append_buffer(ctx, res, nres); + fz_drop_buffer(ctx, nres); + } } - fz_catch(gctx) + else { - Py_CLEAR(r); - return NULL; + res = pdf_load_stream(ctx, contents); } - return r; + + //------------------------------------------------------------- + // create XObject representing the source page + //------------------------------------------------------------- + xobj1 = pdf_new_xobject(ctx, pdfout, *mediabox, fz_identity, NULL, res); + // store spage contents + JM_update_stream(ctx, pdfout, xobj1, res); + fz_drop_buffer(ctx, res); + + // store spage resources + pdf_dict_put_drop(ctx, xobj1, PDF_NAME(Resources), resources); } -SWIGINTERN PyObject *fz_document_s__updateObject(struct fz_document_s *self,int xref,char *text,struct fz_page_s *page){ - pdf_obj *new_obj; - pdf_document *pdf = pdf_specifics(gctx, self); // get pdf doc - fz_try(gctx) - { - assert_PDF(pdf); - int xreflen = pdf_xref_len(gctx, pdf); - if (!INRANGE(xref, 1, xreflen-1)) - THROWMSG("xref out of range"); - // create new object with passed-in string - new_obj = pdf_new_obj_from_str(gctx, pdf, text); - pdf_update_object(gctx, pdf, xref, new_obj); - pdf_drop_obj(gctx, new_obj); - if (page) refresh_link_table(gctx, pdf_page_from_fz_page(gctx, page)); - } - fz_catch(gctx) return NULL; - pdf->dirty = 1; - return NONE; + } + fz_catch(ctx) fz_rethrow(ctx); + return xobj1; +} + +//----------------------------------------------------------------------------- +// Insert a buffer as a new separate /Contents object of a page. +//----------------------------------------------------------------------------- +void JM_insert_contents(fz_context *ctx, pdf_document *pdf, + pdf_obj *pageref, fz_buffer *newcont, int overlay) +{ + fz_try(ctx) + { + pdf_obj *contents = pdf_dict_get(ctx, pageref, PDF_NAME(Contents)); + pdf_obj *newconts = pdf_add_stream(ctx, pdf, newcont, NULL, 0); + if (pdf_is_array(ctx, contents)) + { + if (overlay) + pdf_array_push_drop(ctx, contents, newconts); + else + pdf_array_insert_drop(ctx, contents, newconts, 0); } -SWIGINTERN PyObject *fz_document_s__updateStream(struct fz_document_s *self,int xref,PyObject *stream,int new){ - pdf_obj *obj = NULL; - fz_var(obj); - fz_buffer *res = NULL; - fz_var(res); - pdf_document *pdf = pdf_specifics(gctx, self); // get pdf doc - fz_try(gctx) - { - assert_PDF(pdf); - int xreflen = pdf_xref_len(gctx, pdf); - if (!INRANGE(xref, 1, xreflen-1)) - THROWMSG("xref out of range"); - // get the object - obj = pdf_new_indirect(gctx, pdf, xref, 0); - if (!new && !pdf_is_stream(gctx, obj)) - THROWMSG("xref not a stream object"); - res = JM_BufferFromBytes(gctx, stream); - if (!res) THROWMSG("stream must be bytes or bytearray"); - JM_update_stream(gctx, pdf, obj, res); - - } - fz_always(gctx) + else + { + pdf_obj *carr = pdf_new_array(ctx, pdf, 2); + if (overlay) { - fz_drop_buffer(gctx, res); - pdf_drop_obj(gctx, obj); - } - fz_catch(gctx) - return NULL; - pdf->dirty = 1; - return NONE; - } -SWIGINTERN PyObject *fz_document_s__setMetadata(struct fz_document_s *self,char *text){ - pdf_obj *info, *new_info, *new_info_ind; - int info_num = 0; // will contain xref no of info object - pdf_document *pdf = pdf_specifics(gctx, self); // get pdf doc - fz_try(gctx) { - assert_PDF(pdf); - // create new /Info object based on passed-in string - new_info = pdf_new_obj_from_str(gctx, pdf, text); + pdf_array_push(ctx, carr, contents); + pdf_array_push_drop(ctx, carr, newconts); } - fz_catch(gctx) return NULL; - pdf->dirty = 1; - // replace existing /Info object - info = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Info); - if (info) + else { - info_num = pdf_to_num(gctx, info); // get xref no of old info - pdf_update_object(gctx, pdf, info_num, new_info); // insert new - pdf_drop_obj(gctx, new_info); - return NONE; + pdf_array_push_drop(ctx, carr, newconts); + pdf_array_push(ctx, carr, contents); } - // create new indirect object from /Info object - new_info_ind = pdf_add_object(gctx, pdf, new_info); - // put this in the trailer dictionary - pdf_dict_put_drop(gctx, pdf_trailer(gctx, pdf), PDF_NAME_Info, new_info_ind); - return NONE; + pdf_dict_put_drop(ctx, pageref, PDF_NAME(Contents), carr); } -SWIGINTERN void delete_fz_page_s(struct fz_page_s *self){ - DEBUGMSG1("page"); - fz_drop_page(gctx, self); - DEBUGMSG2; + } + fz_catch(ctx) fz_rethrow(ctx); + return; +} +/*----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// Append / prepend a buffer to the /Contents of a page. +//----------------------------------------------------------------------------- +void JM_extend_contents(fz_context *ctx, pdf_document *pdfout, + pdf_obj *pageref, fz_buffer *newcont, int overlay) +{ + int i; + fz_buffer *oldcont = NULL, *endcont = NULL; + pdf_obj *contents = pdf_dict_get(ctx, pageref, PDF_NAME(Contents)); + fz_try(ctx) + { + if (pdf_is_array(ctx, contents)) // multiple contents objects! + { // choose the correct one (first / last) + if (overlay == 1) i = pdf_array_len(ctx, contents) - 1; + else i = 0; + contents = pdf_array_get(ctx, contents, i); } -SWIGINTERN struct fz_rect_s *fz_page_s_bound(struct fz_page_s *self){ - fz_rect *rect = JM_Alloc(fz_rect, 1); - fz_bound_page(gctx, self, rect); - return rect; + oldcont = pdf_load_stream(ctx, contents); // old contents buffer + + // allocate result buffer + endcont = fz_new_buffer(ctx, fz_buffer_storage(ctx, oldcont, NULL) + + fz_buffer_storage(ctx, newcont, NULL)); + + if (overlay == 1) // append new buffer + { + fz_append_buffer(ctx, endcont, oldcont); + fz_append_buffer(ctx, endcont, newcont); } -SWIGINTERN int fz_page_s_run(struct fz_page_s *self,struct DeviceWrapper *dw,struct fz_matrix_s const *m){ - fz_try(gctx) fz_run_page(gctx, self, dw->device, m, NULL); - fz_catch(gctx) return 1; - return 0; + else // prepend new buffer + { + fz_append_buffer(ctx, endcont, newcont); + fz_append_buffer(ctx, endcont, oldcont); } -SWIGINTERN PyObject *fz_page_s_getSVGimage(struct fz_page_s *self,struct fz_matrix_s *matrix){ - fz_rect mediabox; - fz_bound_page(gctx, self, &mediabox); - fz_device *dev = NULL; - fz_buffer *res = NULL; - PyObject *text = NULL; - fz_matrix *ctm = matrix; - if (!matrix) ctm = (fz_matrix *) &fz_identity; - fz_rect tbounds; - fz_cookie *cookie = NULL; - fz_output *out = NULL; - fz_separations *seps = NULL; - fz_var(out); - fz_var(dev); - fz_var(res); - tbounds = mediabox; - fz_transform_rect(&tbounds, ctm); + fz_terminate_buffer(ctx, endcont); // finalize result buffer + + // now update the content stream + JM_update_stream(ctx, pdfout, contents, endcont); + } + fz_always(ctx) + { + fz_drop_buffer(ctx, endcont); + fz_drop_buffer(ctx, oldcont); + } + fz_catch(ctx) fz_rethrow(ctx); + return; +} +-----------------------------------------------------------------------------*/ - fz_try(gctx) - { - res = fz_new_buffer(gctx, 1024); - out = fz_new_output_with_buffer(gctx, res); - dev = fz_new_svg_device(gctx, out, tbounds.x1-tbounds.x0, tbounds.y1-tbounds.y0, FZ_SVG_TEXT_AS_PATH, 1); - fz_run_page(gctx, self, dev, ctm, cookie); - fz_close_device(gctx, dev); - text = JM_StrFromBuffer(gctx, res); - } - fz_always(gctx) - { - fz_drop_device(gctx, dev); - fz_drop_output(gctx, out); - fz_drop_buffer(gctx, res); - } - fz_catch(gctx) - { - return NULL; - } - return text; - } -SWIGINTERN struct fz_annot_s *fz_page_s_addLineAnnot(struct fz_page_s *self,struct fz_point_s *p1,struct fz_point_s *p2){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - pdf_annot *annot = NULL; - float col[3] = {0, 0, 0}; - float width = 1.0f; - fz_point a = {p1->x, p1->y}; - fz_point b = {p2->x, p2->y}; - fz_rect r = {MIN(a.x, b.x), MIN(a.y, b.y), MAX(a.x, b.x), MAX(a.y, b.y)}; - fz_var(annot); - fz_try(gctx) - { - assert_PDF(page); - pdf_document *pdf = page->doc; - annot = pdf_create_annot(gctx, page, PDF_ANNOT_LINE); - pdf_set_annot_line(gctx, annot, a, b); - pdf_set_annot_border(gctx, annot, width); - pdf_set_annot_color(gctx, annot, 3, col); - fz_expand_rect(&r, 3 * width); - pdf_set_annot_rect(gctx, annot, &r); - } - fz_catch(gctx) return NULL; - fz_annot *fzannot = (fz_annot *) annot; - return fz_keep_annot(gctx, fzannot); + + +//---------------------------------------------------------------------------- +// convert (char *) to ASCII-only (char*) (which must be freed!) +//---------------------------------------------------------------------------- +char *JM_ASCIIFromChar(char *in) +{ + if (!in) return NULL; + size_t i, j = strlen(in) + 1; + unsigned char *out = JM_Alloc(unsigned char, j); + if (!out) return NULL; + memcpy(out, in, j); + for (i = 0; i < j-1; i++) + { + if (out[i] > 126) + { + out[i] = 63; + continue; } -SWIGINTERN struct fz_annot_s *fz_page_s_addTextAnnot(struct fz_page_s *self,struct fz_point_s *point,char *text){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - pdf_annot *annot = NULL; - char *name = "Note"; - fz_point pos = {point->x, point->y}; - fz_var(annot); - fz_try(gctx) - { - assert_PDF(page); - annot = pdf_create_annot(gctx, page, PDF_ANNOT_TEXT); - pdf_set_text_annot_position(gctx, annot, pos); - pdf_set_annot_contents(gctx, annot, text); - pdf_set_annot_icon_name(gctx, annot, name); - pdf_update_appearance(gctx, annot); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, page); - } - fz_catch(gctx) return NULL; - fz_annot *fzannot = (fz_annot *) annot; - return fz_keep_annot(gctx, fzannot); + if (out[i] < 32) + out[i] = 32; + } + return (char *) out; +} + +//---------------------------------------------------------------------------- +// create an ASCII-only Python string of a (char*) +//---------------------------------------------------------------------------- +PyObject *JM_UnicodeFromASCII(const char *in) +{ + char *c = JM_ASCIIFromChar((char *) in); + PyObject *p = Py_BuildValue("s", c); + JM_Free(c); + return p; +} + +//----------------------------------------------------------------------------- +// Store info of a font in Python list +//----------------------------------------------------------------------------- +void JM_gather_fonts(fz_context *ctx, pdf_document *pdf, pdf_obj *dict, + PyObject *fontlist) +{ + int i, n; + n = pdf_dict_len(ctx, dict); + for (i = 0; i < n; i++) + { + pdf_obj *fontdict = NULL; + pdf_obj *subtype = NULL; + pdf_obj *basefont = NULL; + pdf_obj *name = NULL; + pdf_obj *refname = NULL; + pdf_obj *encoding = NULL; + + fontdict = pdf_dict_get_val(ctx, dict, i); + if (!pdf_is_dict(ctx, fontdict)) + { + PySys_WriteStdout("warning: not a font dict (%d 0 R)", + pdf_to_num(ctx, fontdict)); + continue; } -SWIGINTERN struct fz_annot_s *fz_page_s_addFileAnnot(struct fz_page_s *self,struct fz_point_s *point,PyObject *buffer,char *filename,char *ufilename,char *desc){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_annot *fzannot = NULL; - pdf_annot *annot = NULL; - char *data = NULL, *uf = ufilename, *d = desc; - if (!ufilename) uf = filename; - if (!desc) d = filename; - size_t len = 0; - fz_buffer *filebuf = NULL; - fz_rect r = {point->x, point->y, point->x + 20, point->y + 30}; - fz_var(annot); - fz_try(gctx) - { - assert_PDF(page); - annot = pdf_create_annot(gctx, page, ANNOT_FILEATTACHMENT); - pdf_set_annot_rect(gctx, annot, &r); - pdf_set_annot_icon_name(gctx, annot, "PushPin"); - len = JM_CharFromBytesOrArray(buffer, &data); - filebuf = fz_new_buffer_from_shared_data(gctx, data, len); - pdf_obj *val = JM_embed_file(gctx, page->doc, filebuf, - filename, uf, d); - pdf_dict_put(gctx, annot->obj, PDF_NAME_FS, val); - pdf_dict_put_text_string(gctx, annot->obj, PDF_NAME_Contents, filename); - JM_update_file_attachment_annot(gctx, page->doc, annot); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, page); - } - fz_catch(gctx) return NULL; - fzannot = (fz_annot *) annot; - return fz_keep_annot(gctx, fzannot); - } -SWIGINTERN struct fz_annot_s *fz_page_s_addStrikeoutAnnot(struct fz_page_s *self,struct fz_rect_s *rect){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_annot *annot = NULL; - fz_var(annot); - fz_try(gctx) - { - assert_PDF(page); - annot = JM_AnnotTextmarker(gctx, page, rect, PDF_ANNOT_STRIKE_OUT); - } - fz_catch(gctx) return NULL; - return fz_keep_annot(gctx, annot); - } -SWIGINTERN struct fz_annot_s *fz_page_s_addUnderlineAnnot(struct fz_page_s *self,struct fz_rect_s *rect){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_annot *annot = NULL; - fz_var(annot); - fz_try(gctx) - { - assert_PDF(page); - annot = JM_AnnotTextmarker(gctx, page, rect, PDF_ANNOT_UNDERLINE); - } - fz_catch(gctx) return NULL; - return fz_keep_annot(gctx, annot); + refname = pdf_dict_get_key(ctx, dict, i); + subtype = pdf_dict_get(ctx, fontdict, PDF_NAME(Subtype)); + basefont = pdf_dict_get(ctx, fontdict, PDF_NAME(BaseFont)); + if (!basefont || pdf_is_null(ctx, basefont)) + name = pdf_dict_get(ctx, fontdict, PDF_NAME(Name)); + else + name = basefont; + encoding = pdf_dict_get(ctx, fontdict, PDF_NAME(Encoding)); + if (pdf_is_dict(ctx, encoding)) + encoding = pdf_dict_get(ctx, encoding, PDF_NAME(BaseEncoding)); + int xref = pdf_to_num(ctx, fontdict); + char *ext = "n/a"; + if (xref) ext = fontextension(ctx, pdf, xref); + PyObject *entry = PyList_New(0); + PyList_Append(entry, Py_BuildValue("i", xref)); + PyList_Append(entry, Py_BuildValue("s", ext)); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, subtype))); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, name))); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, refname))); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, encoding))); + PyList_Append(fontlist, entry); + Py_CLEAR(entry); + } +} + +//----------------------------------------------------------------------------- +// Store info of an image in Python list +//----------------------------------------------------------------------------- +void JM_gather_images(fz_context *ctx, pdf_document *doc, pdf_obj *dict, + PyObject *imagelist) +{ + int i, n; + n = pdf_dict_len(ctx, dict); + for (i = 0; i < n; i++) + { + pdf_obj *imagedict, *smask; + pdf_obj *refname = NULL; + pdf_obj *type; + pdf_obj *width; + pdf_obj *height; + pdf_obj *bpc = NULL; + pdf_obj *filter = NULL; + pdf_obj *cs = NULL; + pdf_obj *altcs; + + imagedict = pdf_dict_get_val(ctx, dict, i); + if (!pdf_is_dict(ctx, imagedict)) + { + PySys_WriteStdout("warning: not an image dict (%d 0 R)", + pdf_to_num(ctx, imagedict)); + continue; } -SWIGINTERN struct fz_annot_s *fz_page_s_addHighlightAnnot(struct fz_page_s *self,struct fz_rect_s *rect){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_annot *annot = NULL; - fz_var(annot); - fz_try(gctx) + refname = pdf_dict_get_key(ctx, dict, i); + + type = pdf_dict_get(ctx, imagedict, PDF_NAME(Subtype)); + if (!pdf_name_eq(ctx, type, PDF_NAME(Image))) + continue; + + int xref = pdf_to_num(ctx, imagedict); + int gen = 0; + smask = pdf_dict_get(ctx, imagedict, PDF_NAME(SMask)); + if (smask) + gen = pdf_to_num(ctx, smask); + filter = pdf_dict_get(ctx, imagedict, PDF_NAME(Filter)); + + altcs = NULL; + cs = pdf_dict_get(ctx, imagedict, PDF_NAME(ColorSpace)); + if (pdf_is_array(ctx, cs)) + { + pdf_obj *cses = cs; + cs = pdf_array_get(ctx, cses, 0); + if (pdf_name_eq(ctx, cs, PDF_NAME(DeviceN)) || + pdf_name_eq(ctx, cs, PDF_NAME(Separation))) { - assert_PDF(page); - annot = JM_AnnotTextmarker(gctx, page, rect, PDF_ANNOT_HIGHLIGHT); + altcs = pdf_array_get(ctx, cses, 2); + if (pdf_is_array(ctx, altcs)) + altcs = pdf_array_get(ctx, altcs, 0); } - fz_catch(gctx) return NULL; - return fz_keep_annot(gctx, annot); } -SWIGINTERN struct fz_annot_s *fz_page_s_addRectAnnot(struct fz_page_s *self,struct fz_rect_s *rect){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_annot *fzannot = NULL; - fz_var(fzannot); - fz_try(gctx) - { - assert_PDF(page); - fzannot = JM_AnnotCircleOrRect(gctx, page, rect, PDF_ANNOT_SQUARE); - } - fz_catch(gctx) return NULL; - return fz_keep_annot(gctx, fzannot); + + width = pdf_dict_get(ctx, imagedict, PDF_NAME(Width)); + height = pdf_dict_get(ctx, imagedict, PDF_NAME(Height)); + bpc = pdf_dict_get(ctx, imagedict, PDF_NAME(BitsPerComponent)); + + PyObject *entry = PyList_New(0); + PyList_Append(entry, Py_BuildValue("i", xref)); + PyList_Append(entry, Py_BuildValue("i", gen)); + PyList_Append(entry, Py_BuildValue("i", pdf_to_int(ctx, width))); + PyList_Append(entry, Py_BuildValue("i", pdf_to_int(ctx, height))); + PyList_Append(entry, Py_BuildValue("i", pdf_to_int(ctx, bpc))); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, cs))); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, altcs))); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, refname))); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, filter))); + PyList_Append(imagelist, entry); + Py_CLEAR(entry); + } +} + +//----------------------------------------------------------------------------- +// Store info of a /Form in Python list +//----------------------------------------------------------------------------- +void JM_gather_forms(fz_context *ctx, pdf_document *doc, pdf_obj *dict, + PyObject *imagelist) +{ + int i, n; + n = pdf_dict_len(ctx, dict); + for (i = 0; i < n; i++) + { + pdf_obj *imagedict; + pdf_obj *refname = NULL; + pdf_obj *type; + + imagedict = pdf_dict_get_val(ctx, dict, i); + if (!pdf_is_dict(ctx, imagedict)) + { + PySys_WriteStdout("warning: not a form dict (%d 0 R)", + pdf_to_num(ctx, imagedict)); + continue; } -SWIGINTERN struct fz_annot_s *fz_page_s_addCircleAnnot(struct fz_page_s *self,struct fz_rect_s *rect){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_annot *fzannot = NULL; - fz_var(fzannot); - fz_try(gctx) + refname = pdf_dict_get_key(ctx, dict, i); + + type = pdf_dict_get(ctx, imagedict, PDF_NAME(Subtype)); + if (!pdf_name_eq(ctx, type, PDF_NAME(Form))) + continue; + + int xref = pdf_to_num(ctx, imagedict); + + PyObject *entry = PyList_New(0); + PyList_Append(entry, Py_BuildValue("i", xref)); + PyList_Append(entry, JM_UnicodeFromASCII(pdf_to_name(ctx, refname))); + PyList_Append(imagelist, entry); + Py_CLEAR(entry); + } +} + +//----------------------------------------------------------------------------- +// Step through /Resources, looking up image or font information +//----------------------------------------------------------------------------- +void JM_scan_resources(fz_context *ctx, pdf_document *pdf, pdf_obj *rsrc, + PyObject *liste, int what) +{ + pdf_obj *font, *xobj, *subrsrc; + int i, n; + if (pdf_mark_obj(ctx, rsrc)) return; // stop on cylic dependencies + fz_try(ctx) + { + if (what == 1) // look up fonts + { + font = pdf_dict_get(ctx, rsrc, PDF_NAME(Font)); + JM_gather_fonts(ctx, pdf, font, liste); + n = pdf_dict_len(ctx, font); + for (i = 0; i < n; i++) { - assert_PDF(page); - fzannot = JM_AnnotCircleOrRect(gctx, page, rect, PDF_ANNOT_CIRCLE); + pdf_obj *obj = pdf_dict_get_val(ctx, font, i); + subrsrc = pdf_dict_get(ctx, obj, PDF_NAME(Resources)); + if (subrsrc) + JM_scan_resources(ctx, pdf, subrsrc, liste, what); } - fz_catch(gctx) return NULL; - return fz_keep_annot(gctx, fzannot); } -SWIGINTERN struct fz_annot_s *fz_page_s_addPolylineAnnot(struct fz_page_s *self,PyObject *points){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_annot *fzannot = NULL; - fz_var(fzannot); - fz_try(gctx) - { - assert_PDF(page); - fzannot = JM_AnnotMultiline(gctx, page, points, PDF_ANNOT_POLY_LINE); - } - fz_catch(gctx) return NULL; - return fz_keep_annot(gctx, fzannot); + + xobj = pdf_dict_get(ctx, rsrc, PDF_NAME(XObject)); + + if (what == 2) // look up images + { + JM_gather_images(ctx, pdf, xobj, liste); } -SWIGINTERN struct fz_annot_s *fz_page_s_addPolygonAnnot(struct fz_page_s *self,PyObject *points){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_annot *fzannot = NULL; - fz_var(fzannot); - fz_try(gctx) - { - assert_PDF(page); - fzannot = JM_AnnotMultiline(gctx, page, points, PDF_ANNOT_POLYGON); - } - fz_catch(gctx) return NULL; - return fz_keep_annot(gctx, fzannot); + + if (what == 3) // look up forms + { + JM_gather_forms(ctx, pdf, xobj, liste); } -SWIGINTERN struct fz_annot_s *fz_page_s_addFreetextAnnot(struct fz_page_s *self,struct fz_point_s *pos,char *text,float fontsize,PyObject *color){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - float col[3] = {0, 0, 0}; - if (color && PySequence_Check(color) && PySequence_Size(color) == 3) - { - col[0] = (float) PyFloat_AsDouble(PySequence_GetItem(color, 0)); - col[1] = (float) PyFloat_AsDouble(PySequence_GetItem(color, 1)); - col[2] = (float) PyFloat_AsDouble(PySequence_GetItem(color, 2)); - } - char *fname = "Helvetica"; - pdf_annot *annot = NULL; - char *ascii = JM_ASCIIFromChar(text); - fz_var(ascii); - fz_var(annot); - fz_try(gctx) - { - assert_PDF(page); - pdf_document *pdf = page->doc; - annot = pdf_create_annot(gctx, page, PDF_ANNOT_FREE_TEXT); - pdf_set_free_text_details(gctx, annot, pos, - ascii, fname, fontsize, col); - pdf_update_free_text_annot_appearance(gctx, pdf, annot); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, page); - } - fz_always(gctx) JM_Free(ascii); - fz_catch(gctx) return NULL; - fz_annot *fzannot = (fz_annot *) annot; - return fz_keep_annot(gctx, fzannot); + + n = pdf_dict_len(ctx, xobj); + for (i = 0; i < n; i++) + { + pdf_obj *obj = pdf_dict_get_val(ctx, xobj, i); + subrsrc = pdf_dict_get(ctx, obj, PDF_NAME(Resources)); + if (subrsrc) + JM_scan_resources(ctx, pdf, subrsrc, liste, what); } -SWIGINTERN struct fz_annot_s *fz_page_s__addWidget(struct fz_page_s *self,PyObject *Widget){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - pdf_document *pdf = page->doc; - pdf_annot *annot = NULL; - pdf_widget *widget = NULL; - fz_var(annot); - fz_try(gctx) - { - //------------------------------------------------------------- - // create the widget - only need type and field name for this - //------------------------------------------------------------- - int field_type = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, - "field_type")); - JM_PyErr_Clear; + } + fz_always(ctx) pdf_unmark_obj(ctx, rsrc); + fz_catch(ctx) fz_rethrow(ctx); +} - char *field_name = JM_Python_str_AsChar(PyObject_GetAttrString(Widget, - "field_name")); - JM_PyErr_Clear; - widget = pdf_create_widget(gctx, pdf, page, field_type, field_name); - JM_Python_str_DelForPy3(field_name); - JM_PyErr_Clear; - annot = (pdf_annot *) widget; - //------------------------------------------------------------- - // now field exists, adjust its properties - //------------------------------------------------------------- - JM_set_widget_properties(gctx, annot, Widget, field_type); - } - fz_always(gctx) JM_PyErr_Clear; - fz_catch(gctx) return NULL; - fz_annot *fzannot = (fz_annot *) annot; - return fz_keep_annot(gctx, fzannot); - } -SWIGINTERN struct fz_display_list_s *fz_page_s_getDisplayList(struct fz_page_s *self){ - fz_display_list *dl = NULL; - fz_try(gctx) dl = fz_new_display_list_from_page(gctx, self); - fz_catch(gctx) return NULL; - return dl; - } -SWIGINTERN PyObject *fz_page_s_setCropBox(struct fz_page_s *self,struct fz_rect_s *rect){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - fz_try(gctx) - { - assert_PDF(page); - if (!rect) THROWMSG("rect must be given"); - fz_rect mediabox = {0,0,0,0}; - fz_rect cropbox = {0,0,0,0}; - pdf_to_rect(gctx, pdf_dict_get(gctx, page->obj, PDF_NAME_MediaBox), &mediabox); - cropbox.x0 = rect->x0; - cropbox.y0 = mediabox.y1 - rect->y1; - cropbox.x1 = rect->x1; - cropbox.y1 = mediabox.y1 - rect->y0; - pdf_dict_put_drop(gctx, page->obj, PDF_NAME_CropBox, - pdf_new_rect(gctx, page->doc, &cropbox)); - } - fz_catch(gctx) return NULL; - page->doc->dirty = 1; - return NONE; - } -SWIGINTERN struct fz_link_s *fz_page_s_loadLinks(struct fz_page_s *self){ - fz_link *l = NULL; - fz_try(gctx) l = fz_load_links(gctx, self); - fz_catch(gctx) return NULL; - return l; - } -SWIGINTERN struct fz_annot_s *fz_page_s_firstAnnot(struct fz_page_s *self){ - fz_annot *annot; - fz_try(gctx) annot = fz_first_annot(gctx, self); - fz_catch(gctx) annot = NULL; - if (annot) fz_keep_annot(gctx, annot); - return annot; - } -SWIGINTERN void fz_page_s_deleteLink(struct fz_page_s *self,PyObject *linkdict){ - if (!linkdict) return; // have no parameter - if (!PyDict_Check(linkdict)) return; // have no dictionary - pdf_page *page = pdf_page_from_fz_page(gctx, self); - if (!page) return; // have no PDF - int xref = (int) PyInt_AsLong(PyDict_GetItemString(linkdict, "xref")); - if (xref < 1) return; // invalid xref - pdf_obj *annots = pdf_dict_get(gctx, page->obj, PDF_NAME_Annots); - if (!annots) return; // have no annotations - int len = pdf_array_len(gctx, annots); - int i, oxref = 0; - for (i = 0; i < len; i++) - { - oxref = pdf_to_num(gctx, pdf_array_get(gctx, annots, i)); - if (xref == oxref) break; // found xref in annotations - } - if (xref != oxref) return; // xref not in annotations - pdf_array_delete(gctx, annots, i); // delete entry in annotations - pdf_delete_object(gctx, page->doc, xref); // delete link object - pdf_dict_put(gctx, page->obj, PDF_NAME_Annots, annots); - refresh_link_table(gctx, page); // reload link / annot tables - page->doc->dirty = 1; - return; - } -SWIGINTERN struct fz_annot_s *fz_page_s_deleteAnnot(struct fz_page_s *self,struct fz_annot_s *fannot){ - if (!fannot) return NULL; - fz_annot *nextannot = fz_next_annot(gctx, fannot); // store next - pdf_page *page = pdf_page_from_fz_page(gctx, self); - if (!page) // no PDF, just return next annotation - { - if (nextannot) fz_keep_annot(gctx, nextannot); - return nextannot; - } - pdf_annot *pannot = pdf_annot_from_fz_annot(gctx, fannot); - pdf_delete_annot(gctx, page, pannot); - if (nextannot) fz_keep_annot(gctx, nextannot); - page->doc->dirty = 1; - return nextannot; +//----------------------------------------------------------------------------- +// Convert any MuPDF document to a PDF +// Returns bytes object containing the PDF, created via 'write' function. +//----------------------------------------------------------------------------- +PyObject *JM_convert_to_pdf(fz_context *ctx, fz_document *doc, int fp, int tp, int rotate) +{ + pdf_document *pdfout = pdf_create_document(ctx); // new PDF document + int i, incr = 1, s = fp, e = tp; + if (fp > tp) // revert page sequence + { + incr = -1; // count backwards + s = tp; // adjust ... + e = fp; // ... range + } + fz_rect mediabox; + fz_device *dev = NULL; + fz_buffer *contents = NULL; + pdf_obj *resources = NULL; + fz_page *page; + fz_var(dev); + fz_var(contents); + fz_var(resources); + fz_var(page); + for (i = fp; INRANGE(i, s, e); i += incr) + { // interpret & write document pages as PDF pages + fz_try(ctx) + { + page = fz_load_page(ctx, doc, i); + mediabox = fz_bound_page(ctx, page); + dev = pdf_page_write(ctx, pdfout, mediabox, &resources, &contents); + fz_run_page(ctx, page, dev, fz_identity, NULL); + fz_close_device(ctx, dev); + fz_drop_device(ctx, dev); + dev = NULL; + pdf_obj *page_obj = pdf_add_page(ctx, pdfout, mediabox, rotate, resources, contents); + pdf_insert_page(ctx, pdfout, -1, page_obj); + pdf_drop_obj(ctx, page_obj); } -SWIGINTERN struct fz_point_s *fz_page_s_MediaBoxSize(struct fz_page_s *self){ - fz_point *p = JM_Alloc(fz_point, 1); - p->x = p->y = 0.0; - pdf_page *page = pdf_page_from_fz_page(gctx, self); - if (!page) return p; - fz_rect r = {0,0,0,0}; - pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME_MediaBox); - if (!o) return p; - pdf_to_rect(gctx, o, &r); - p->x = r.x1 - r.x0; - p->y = r.y1 - r.y0; - return p; - } -SWIGINTERN struct fz_point_s *fz_page_s_CropBoxPosition(struct fz_page_s *self){ - fz_point *p = JM_Alloc(fz_point, 1); - p->x = p->y = 0.0; - pdf_page *page = pdf_page_from_fz_page(gctx, self); - if (!page) return p; // not a PDF - fz_rect cbox = {0,0,0,0}; - pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME_CropBox); - if (!o) return p; // no CropBox specified - pdf_to_rect(gctx, o, &cbox); - p->x = cbox.x0; - p->y = cbox.y0; - return p; + fz_always(ctx) + { + pdf_drop_obj(ctx, resources); + fz_drop_buffer(ctx, contents); + fz_drop_device(ctx, dev); } -SWIGINTERN int fz_page_s_rotation(struct fz_page_s *self){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - if (!page) return -1; - pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME_Rotate); - if (!o) return 0; - return pdf_to_int(gctx, o); + fz_catch(ctx) + { + fz_drop_page(ctx, page); + fz_rethrow(ctx); } -SWIGINTERN PyObject *fz_page_s_setRotation(struct fz_page_s *self,int rot){ - fz_try(gctx) - { - pdf_page *page = pdf_page_from_fz_page(gctx, self); - assert_PDF(page); - if (rot % 90) THROWMSG("rotate not 90 * int"); - pdf_dict_put_int(gctx, page->obj, PDF_NAME_Rotate, (int64_t) rot); - page->doc->dirty = 1; - } - fz_catch(gctx) return NULL; - return NONE; + } + // PDF created - now write it to Python bytes + PyObject *r; + fz_buffer *res = NULL; + fz_output *out = NULL; + // prepare write options structure + int errors = 0; + pdf_write_options opts = { 0 }; + opts.do_garbage = 4; + opts.do_compress = 1; + opts.do_compress_images = 1; + opts.do_compress_fonts = 1; + opts.do_sanitize = 1; + opts.do_incremental = 0; + opts.do_ascii = 0; + opts.do_decompress = 0; + opts.do_linear = 0; + opts.do_clean = 1; + opts.do_pretty = 0; + opts.continue_on_error = 1; + opts.errors = &errors; + fz_try(ctx) + { + res = fz_new_buffer(ctx, 1024); + out = fz_new_output_with_buffer(ctx, res); + pdf_write_document(ctx, pdfout, out, &opts); + r = JM_BinFromBuffer(ctx, res); + } + fz_always(ctx) + { + fz_drop_output(ctx, out); + fz_drop_buffer(ctx, res); + } + fz_catch(ctx) fz_rethrow(ctx); + return r; +} + +SWIGINTERN void delete_fz_document_s(struct fz_document_s *self){ + DEBUGMSG1("document w/o close"); + fz_drop_document(gctx, self); + DEBUGMSG2; } -SWIGINTERN PyObject *fz_page_s__addAnnot_FromString(struct fz_page_s *self,PyObject *linklist){ - pdf_obj *annots, *annots_arr, *annot, *ind_obj, *new_array; - pdf_page *page = pdf_page_from_fz_page(gctx, self); - PyObject *txtpy; - char *text; - int lcount = (int) PySequence_Size(linklist); // new object count - if (lcount < 1) return NONE; - int i; - fz_try(gctx) - { - assert_PDF(page); // make sure we have a PDF - // get existing annots array - annots = pdf_dict_get(gctx, page->obj, PDF_NAME_Annots); - if (annots) - { - if (pdf_is_indirect(gctx, annots)) - annots_arr = pdf_resolve_indirect(gctx, annots); - else annots_arr = annots; - } - else - annots_arr = NULL; - int new_len = lcount; - if (annots_arr) new_len += pdf_array_len(gctx, annots_arr); - // allocate new array of old plus new size - new_array = pdf_new_array(gctx, page->doc, new_len); - if (annots_arr) - { // copy existing annots to new array - for (i = 0; i < pdf_array_len(gctx, annots_arr); i++) - pdf_array_push(gctx, new_array, pdf_array_get(gctx, annots_arr, i)); - } - } - fz_catch(gctx) return NULL; - // extract object sources from Python list and store as annotations - for (i = 0; i < lcount; i++) - { - fz_try(gctx) - { - text = NULL; - txtpy = PySequence_ITEM(linklist, (Py_ssize_t) i); - text = JM_Python_str_AsChar(txtpy); - if (!text) THROWMSG("non-string linklist item"); - annot = pdf_new_obj_from_str(gctx, page->doc, text); - JM_Python_str_DelForPy3(text); - ind_obj = pdf_add_object(gctx, page->doc, annot); - pdf_array_push_drop(gctx, new_array, ind_obj); - pdf_drop_obj(gctx, annot); - } - fz_catch(gctx) - { - if (text) - PySys_WriteStderr("%s (%i): '%s'\n", fz_caught_message(gctx), i, text); - else - PySys_WriteStderr("%s (%i)\n", fz_caught_message(gctx), i); - JM_Python_str_DelForPy3(text); - PyErr_Clear(); - } - } - fz_try(gctx) - { - pdf_dict_put_drop(gctx, page->obj, PDF_NAME_Annots, new_array); - refresh_link_table(gctx, page); - } - fz_catch(gctx) return NULL; - page->doc->dirty = 1; - return NONE; - } -SWIGINTERN PyObject *fz_page_s__getLinkXrefs(struct fz_page_s *self){ - pdf_obj *annots, *annots_arr, *link, *obj; - int i, lcount; - pdf_page *page = pdf_page_from_fz_page(gctx, self); - PyObject *linkxrefs = PyList_New(0); - if (!page) return linkxrefs; // empty list if not PDF - annots = pdf_dict_get(gctx, page->obj, PDF_NAME_Annots); - if (!annots) return linkxrefs; - if (pdf_is_indirect(gctx, annots)) - annots_arr = pdf_resolve_indirect(gctx, annots); - else - annots_arr = annots; - lcount = pdf_array_len(gctx, annots_arr); - for (i = 0; i < lcount; i++) - { - link = pdf_array_get(gctx, annots_arr, i); - obj = pdf_dict_get(gctx, link, PDF_NAME_Subtype); - if (pdf_name_eq(gctx, obj, PDF_NAME_Link)) - { - int xref = pdf_to_num(gctx, link); - PyList_Append(linkxrefs, PyInt_FromLong((long) xref)); - } - } - return linkxrefs; - } -SWIGINTERN PyObject *fz_page_s__cleanContents(struct fz_page_s *self){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - pdf_annot *annot; - fz_try(gctx) - { - assert_PDF(page); - pdf_clean_page_contents(gctx, page->doc, page, NULL, NULL, NULL, 1, 0); - for (annot = pdf_first_annot(gctx, page); annot != NULL; annot = pdf_next_annot(gctx, annot)) - { - pdf_clean_annot_contents(gctx, page->doc, annot, NULL, NULL, NULL, 1, 0); - } - } - fz_catch(gctx) return NULL; - page->doc->dirty = 1; - return NONE; +SWIGINTERN swig_type_info* +SWIG_pchar_descriptor(void) +{ + static int init = 0; + static swig_type_info* info = 0; + if (!init) { + info = SWIG_TypeQuery("_p_char"); + init = 1; + } + return info; +} + + +SWIGINTERN int +SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) +{ +#if PY_VERSION_HEX>=0x03000000 +#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) + if (PyBytes_Check(obj)) +#else + if (PyUnicode_Check(obj)) +#endif +#else + if (PyString_Check(obj)) +#endif + { + char *cstr; Py_ssize_t len; +#if PY_VERSION_HEX>=0x03000000 +#if !defined(SWIG_PYTHON_STRICT_BYTE_CHAR) + if (!alloc && cptr) { + /* We can't allow converting without allocation, since the internal + representation of string in Python 3 is UCS-2/UCS-4 but we require + a UTF-8 representation. + TODO(bhy) More detailed explanation */ + return SWIG_RuntimeError; + } + obj = PyUnicode_AsUTF8String(obj); + if(alloc) *alloc = SWIG_NEWOBJ; +#endif + PyBytes_AsStringAndSize(obj, &cstr, &len); +#else + PyString_AsStringAndSize(obj, &cstr, &len); +#endif + if (cptr) { + if (alloc) { + /* + In python the user should not be able to modify the inner + string representation. To warranty that, if you define + SWIG_PYTHON_SAFE_CSTRINGS, a new/copy of the python string + buffer is always returned. + + The default behavior is just to return the pointer value, + so, be careful. + */ +#if defined(SWIG_PYTHON_SAFE_CSTRINGS) + if (*alloc != SWIG_OLDOBJ) +#else + if (*alloc == SWIG_NEWOBJ) +#endif + { + *cptr = (char *)memcpy(malloc((len + 1)*sizeof(char)), cstr, sizeof(char)*(len + 1)); + *alloc = SWIG_NEWOBJ; + } else { + *cptr = cstr; + *alloc = SWIG_OLDOBJ; + } + } else { +#if PY_VERSION_HEX>=0x03000000 +#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) + *cptr = PyBytes_AsString(obj); +#else + assert(0); /* Should never reach here with Unicode strings in Python 3 */ +#endif +#else + *cptr = SWIG_Python_str_AsChar(obj); +#endif + } + } + if (psize) *psize = len + 1; +#if PY_VERSION_HEX>=0x03000000 && !defined(SWIG_PYTHON_STRICT_BYTE_CHAR) + Py_XDECREF(obj); +#endif + return SWIG_OK; + } else { +#if defined(SWIG_PYTHON_2_UNICODE) +#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) +#error "Cannot use both SWIG_PYTHON_2_UNICODE and SWIG_PYTHON_STRICT_BYTE_CHAR at once" +#endif +#if PY_VERSION_HEX<0x03000000 + if (PyUnicode_Check(obj)) { + char *cstr; Py_ssize_t len; + if (!alloc && cptr) { + return SWIG_RuntimeError; + } + obj = PyUnicode_AsUTF8String(obj); + if (PyString_AsStringAndSize(obj, &cstr, &len) != -1) { + if (cptr) { + if (alloc) *alloc = SWIG_NEWOBJ; + *cptr = (char *)memcpy(malloc((len + 1)*sizeof(char)), cstr, sizeof(char)*(len + 1)); } -SWIGINTERN int fz_page_s__showPDFpage(struct fz_page_s *self,struct fz_rect_s *rect,struct fz_document_s *docsrc,int pno,int overlay,int keep_proportion,int reuse_xref,struct fz_rect_s *clip,struct pdf_graft_map_s *graftmap,char *_imgname){ - int xref; - xref = reuse_xref; - pdf_obj *xobj1, *xobj2, *resources, *o; - fz_buffer *res, *nres; - fz_rect mediabox = {0,0,0,0}; - fz_rect cropbox = {0,0,0,0}; - fz_try(gctx) - { - pdf_page *tpage = pdf_page_from_fz_page(gctx, self); - assert_PDF(tpage); - pdf_obj *tpageref = tpage->obj; - pdf_document *pdfout = tpage->doc; // target PDF - pdf_document *pdfsrc = pdf_specifics(gctx, docsrc); - assert_PDF(pdfsrc); + if (psize) *psize = len + 1; - //------------------------------------------------------------- - // make XObject of source page and get its xref - //------------------------------------------------------------- - xobj1 = JM_xobject_from_page(gctx, pdfout, pdfsrc, pno, - &mediabox, &cropbox, xref, graftmap); - xref = pdf_to_num(gctx, xobj1); + Py_XDECREF(obj); + return SWIG_OK; + } else { + Py_XDECREF(obj); + } + } +#endif +#endif - //------------------------------------------------------------- - // Calculate /Matrix and /BBox of the referencing XObject - //------------------------------------------------------------- - if (clip) - { // set cropbox if clip given - cropbox.x0 = clip->x0; - cropbox.y0 = mediabox.y1 - clip->y1; - cropbox.x1 = clip->x1; - cropbox.y1 = mediabox.y1 - clip->y0; - } - fz_matrix mat = {1, 0, 0, 1, 0, 0}; - fz_rect prect = {0, 0, 0, 0}; - fz_rect r = {0, 0, 0, 0}; - fz_bound_page(gctx, self, &prect); - o = pdf_dict_get(gctx, tpageref, PDF_NAME_CropBox); - pdf_to_rect(gctx, o, &r); - if (o) - { - prect.x0 = r.x0; - prect.y0 = r.y0; - } - o = pdf_dict_get(gctx, tpageref, PDF_NAME_MediaBox); - pdf_to_rect(gctx, o, &r); - if (o) - { - prect.x1 = r.x1; - prect.y1 = r.y1; - } - float W = rect->x1 - rect->x0; - float H = rect->y1 - rect->y0; - float fw = W / (cropbox.x1 - cropbox.x0); - float fh = H / (cropbox.y1 - cropbox.y0); - if ((fw < fh) && keep_proportion) // zoom factors in matrix - fh = fw; - float X = rect->x0 + prect.x0 - fw*cropbox.x0; - float Y = prect.y1 - (rect->y1 + prect.y0 + fh*cropbox.y0); - mat.a = fw; - mat.d = fh; - mat.e = X; - mat.f = Y; + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + if (pchar_descriptor) { + void* vptr = 0; + if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) { + if (cptr) *cptr = (char *) vptr; + if (psize) *psize = vptr ? (strlen((char *)vptr) + 1) : 0; + if (alloc) *alloc = SWIG_OLDOBJ; + return SWIG_OK; + } + } + } + return SWIG_TypeError; +} - //------------------------------------------------------------- - // create referencing XObject (controls actual display) - //------------------------------------------------------------- - xobj2 = pdf_new_xobject(gctx, pdfout, &cropbox, &mat); - // fill reference to xobj1 into its /Resources - o = pdf_xobject_resources(gctx, xobj2); - pdf_obj *subres = pdf_new_dict(gctx, pdfout, 10); - pdf_dict_put(gctx, o, PDF_NAME_XObject, subres); - pdf_dict_puts(gctx, subres, "fullpage", xobj1); - pdf_drop_obj(gctx, subres); - // xobj2 invokes xobj1 via one statement - res = fz_new_buffer(gctx, 50); - fz_append_string(gctx, res, "/fullpage Do"); - pdf_update_xobject_contents(gctx, pdfout, xobj2, res); - fz_drop_buffer(gctx, res); - //------------------------------------------------------------- - // update target page: - //------------------------------------------------------------- - // 1. resources object - //------------------------------------------------------------- - resources = pdf_dict_get(gctx, tpageref, PDF_NAME_Resources); - subres = pdf_dict_get(gctx, resources, PDF_NAME_XObject); - if (!subres) // has no XObject dict yet: create one - { - subres = pdf_new_dict(gctx, pdfout, 10); - pdf_dict_put(gctx, resources, PDF_NAME_XObject, subres); - } - pdf_dict_puts(gctx, subres, _imgname, xobj2); +#include - //------------------------------------------------------------- - // 2. contents object - //------------------------------------------------------------- - nres = fz_new_buffer(gctx, 50); // buffer for Do-command - fz_append_string(gctx, nres, " q /"); // Do-command - fz_append_string(gctx, nres, _imgname); - fz_append_string(gctx, nres, " Do Q "); - JM_insert_contents(gctx, pdfout, tpageref, nres, overlay); - fz_drop_buffer(gctx, nres); - } - fz_catch(gctx) return -1; - return xref; - } -SWIGINTERN PyObject *fz_page_s_insertImage(struct fz_page_s *self,struct fz_rect_s *rect,char const *filename,struct fz_pixmap_s *pixmap,PyObject *stream,int overlay,char *_imgname){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - pdf_document *pdf; - fz_pixmap *pm = NULL; - fz_pixmap *pix = NULL; - fz_image *mask = NULL; - fz_separations *seps = NULL; - pdf_obj *resources, *subres, *ref; - fz_buffer *res = NULL, *nres = NULL, *imgbuf = NULL; +#include - unsigned char *streamdata = NULL; - size_t streamlen = JM_CharFromBytesOrArray(stream, &streamdata); - const char *template = " q %g 0 0 %g %g %g cm /%s Do Q "; - char *cont = NULL; - Py_ssize_t name_len = 0; - fz_image *zimg = NULL, *image = NULL; - int parm_count = 0; - if (filename) parm_count++; - if (pixmap) parm_count++; - if (streamlen > 0) parm_count++; - fz_try(gctx) - { - assert_PDF(page); - if (parm_count != 1) - THROWMSG("need exactly one of filename, pixmap or stream"); - if (fz_is_empty_rect(rect) || fz_is_infinite_rect(rect)) - THROWMSG("rect must be finite and not empty"); - // calculate coordinates for image matrix - fz_rect prect = {0, 0, 0, 0}; // normal page rectangle - fz_bound_page(gctx, self, &prect); // get page mediabox - fz_rect r = {0, 0, 0, 0}; // modify where necessary - pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME_CropBox); - if (o) - { // set top-left of page rect to new values - pdf_to_rect(gctx, o, &r); - prect.x0 = r.x0; - prect.y0 = r.y0; - } - o = pdf_dict_get(gctx, page->obj, PDF_NAME_MediaBox); - if (o) - { // set bottom-right to new values - pdf_to_rect(gctx, o, &r); - prect.x1 = r.x1; - prect.y1 = r.y1; - } - // adjust rect.x0, rect.y0 by CropBox start - float X = rect->x0 + prect.x0; - float Y = prect.y1 - (rect->y1 + prect.y0); - float W = rect->x1 - rect->x0; - float H = rect->y1 - rect->y0; +/* Getting isfinite working pre C99 across multiple platforms is non-trivial. Users can provide SWIG_isfinite on older platforms. */ +#ifndef SWIG_isfinite +/* isfinite() is a macro for C99 */ +# if defined(isfinite) +# define SWIG_isfinite(X) (isfinite(X)) +# elif defined __cplusplus && __cplusplus >= 201103L +/* Use a template so that this works whether isfinite() is std::isfinite() or + * in the global namespace. The reality seems to vary between compiler + * versions. + * + * Make sure namespace std exists to avoid compiler warnings. + * + * extern "C++" is required as this fragment can end up inside an extern "C" { } block + */ +namespace std { } +extern "C++" template +inline int SWIG_isfinite_func(T x) { + using namespace std; + return isfinite(x); +} +# define SWIG_isfinite(X) (SWIG_isfinite_func(X)) +# elif defined(_MSC_VER) +# define SWIG_isfinite(X) (_finite(X)) +# elif defined(__sun) && defined(__SVR4) +# include +# define SWIG_isfinite(X) (finite(X)) +# endif +#endif - pdf = page->doc; - // get objects "Resources" & "XObject" - resources = pdf_dict_get(gctx, page->obj, PDF_NAME_Resources); - subres = pdf_dict_get(gctx, resources, PDF_NAME_XObject); - if (!subres) // has no XObject yet, create one - { - subres = pdf_new_dict(gctx, pdf, 10); - pdf_dict_put_drop(gctx, resources, PDF_NAME_XObject, subres); - } +/* Accept infinite as a valid float value unless we are unable to check if a value is finite */ +#ifdef SWIG_isfinite +# define SWIG_Float_Overflow_Check(X) ((X < -FLT_MAX || X > FLT_MAX) && SWIG_isfinite(X)) +#else +# define SWIG_Float_Overflow_Check(X) ((X < -FLT_MAX || X > FLT_MAX)) +#endif - // create the image - if (filename || streamlen > 0) - { - if (filename) - image = fz_new_image_from_file(gctx, filename); - else - { - imgbuf = fz_new_buffer_from_shared_data(gctx, - streamdata, streamlen); - image = fz_new_image_from_buffer(gctx, imgbuf); - } - pix = fz_get_pixmap_from_image(gctx, image, NULL, NULL, 0, 0); - if (pix->alpha == 1) - { // have alpha, therefore create a mask - pm = fz_convert_pixmap(gctx, pix, NULL, NULL, NULL, NULL, 1); - pm->alpha = 0; - pm->colorspace = fz_keep_colorspace(gctx, fz_device_gray(gctx)); - mask = fz_new_image_from_pixmap(gctx, pm, NULL); - zimg = fz_new_image_from_pixmap(gctx, pix, mask); - fz_drop_image(gctx, image); - image = zimg; - zimg = NULL; - } - } - if (pixmap) - { - if (pixmap->alpha == 0) - image = fz_new_image_from_pixmap(gctx, pixmap, NULL); - else - { // pixmap has alpha, therefore create a mask - pm = fz_convert_pixmap(gctx, pixmap, NULL, NULL, NULL, NULL, 1); - pm->alpha = 0; - pm->colorspace = fz_keep_colorspace(gctx, fz_device_gray(gctx)); - mask = fz_new_image_from_pixmap(gctx, pm, NULL); - image = fz_new_image_from_pixmap(gctx, pixmap, mask); - } - } - // put image in PDF - ref = pdf_add_image(gctx, pdf, image, 0); - pdf_dict_puts(gctx, subres, _imgname, ref); // store ref-name - // prep contents stream buffer with invoking command - nres = fz_new_buffer(gctx, 50); - fz_append_printf(gctx, nres, template, W, H, X, Y, _imgname); - JM_insert_contents(gctx, pdf, page->obj, nres, overlay); - fz_drop_buffer(gctx, nres); - } - fz_always(gctx) +SWIGINTERN int +SWIG_AsVal_double (PyObject *obj, double *val) +{ + int res = SWIG_TypeError; + if (PyFloat_Check(obj)) { + if (val) *val = PyFloat_AsDouble(obj); + return SWIG_OK; +#if PY_VERSION_HEX < 0x03000000 + } else if (PyInt_Check(obj)) { + if (val) *val = (double) PyInt_AsLong(obj); + return SWIG_OK; +#endif + } else if (PyLong_Check(obj)) { + double v = PyLong_AsDouble(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + } + } +#ifdef SWIG_PYTHON_CAST_MODE + { + int dispatch = 0; + double d = PyFloat_AsDouble(obj); + if (!PyErr_Occurred()) { + if (val) *val = d; + return SWIG_AddCast(SWIG_OK); + } else { + PyErr_Clear(); + } + if (!dispatch) { + long v = PyLong_AsLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_AddCast(SWIG_AddCast(SWIG_OK)); + } else { + PyErr_Clear(); + } + } + } +#endif + return res; +} + + +SWIGINTERN int +SWIG_AsVal_float (PyObject * obj, float *val) +{ + double v; + int res = SWIG_AsVal_double (obj, &v); + if (SWIG_IsOK(res)) { + if (SWIG_Float_Overflow_Check(v)) { + return SWIG_OverflowError; + } else { + if (val) *val = (float)(v); + } + } + return res; +} + +SWIGINTERN struct fz_document_s *new_fz_document_s(char const *filename,PyObject *stream,char const *filetype,PyObject *rect,float width,float height,float fontsize){ + gctx->error->errcode = 0; // reset any error code + gctx->error->message[0] = 0; // reset any error message + struct fz_document_s *doc = NULL; + fz_stream *data = NULL; + char *streamdata; + float w = width, h = height; + fz_rect r = JM_rect_from_py(rect); + if (!(fz_is_empty_rect(r) && !fz_is_infinite_rect(r))) { - fz_drop_image(gctx, image); - fz_drop_image(gctx, mask); - fz_drop_pixmap(gctx, pix); - fz_drop_pixmap(gctx, pm); + w = r.x1 - r.x0; + h = r.y1 - r.y0; } - fz_catch(gctx) return NULL; - pdf->dirty = 1; - return NONE; - } -SWIGINTERN PyObject *fz_page_s_insertFont(struct fz_page_s *self,char const *fontname,char const *fontfile,PyObject *fontbuffer,int xref,int set_simple,int idx){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - pdf_document *pdf; - pdf_obj *resources, *fonts, *font_obj; - fz_font *font; - const char *data = NULL; - int size, ixref = 0; - PyObject *info; + + size_t streamlen = JM_CharFromBytesOrArray(stream, &streamdata); fz_try(gctx) { - assert_PDF(page); - pdf = page->doc; - // get objects "Resources", "Resources/Font" - resources = pdf_dict_get(gctx, page->obj, PDF_NAME_Resources); - fonts = pdf_dict_get(gctx, resources, PDF_NAME_Font); - int simple = 0; - if (!fonts) // page has no fonts yet - fonts = pdf_add_object_drop(gctx, pdf, pdf_new_dict(gctx, pdf, 1)); - - data = fz_lookup_base14_font(gctx, fontname, &size); - if (data) // base 14 font found + if (streamlen > 0) { - font = fz_new_font_from_memory(gctx, fontname, data, size, 0, 0); - font_obj = pdf_add_simple_font(gctx, pdf, font, PDF_SIMPLE_ENCODING_LATIN); - simple = 1; + data = fz_open_memory(gctx, streamdata, streamlen); + char *magic = (char *)filename; + if (!magic) magic = (char *)filetype; + doc = fz_open_document_with_stream(gctx, magic, data); } else { - if (!fontfile && !fontbuffer) THROWMSG("unknown PDF Base 14 font"); - if (fontfile) - { - font = fz_new_font_from_file(gctx, NULL, fontfile, idx, 0); - } - else - { - if (!PyBytes_Check(fontbuffer)) THROWMSG("fontbuffer must be bytes"); - data = PyBytes_AsString(fontbuffer); - size = PyBytes_Size(fontbuffer); - font = fz_new_font_from_memory(gctx, NULL, data, size, idx, 0); - } - if (set_simple == 0) + if (filename) { - font_obj = pdf_add_cid_font(gctx, pdf, font); - simple = 0; + if (!filetype || strlen(filetype) == 0) doc = fz_open_document(gctx, filename); + else + { + const fz_document_handler *handler; + handler = fz_recognize_document(gctx, filetype); + if (handler && handler->open) + doc = handler->open(gctx, filename); + else THROWMSG("unrecognized file type"); + } } else { - font_obj = pdf_add_simple_font(gctx, pdf, font, PDF_SIMPLE_ENCODING_LATIN); - simple = 2; + pdf_document *pdf = pdf_create_document(gctx); + pdf->dirty = 1; + doc = (fz_document *) pdf; } } - ixref = pdf_to_num(gctx, font_obj); - PyObject *name = PyString_FromString(fz_font_name(gctx, font)); - PyObject *exto; - if (simple != 1) - exto = PyString_FromString(fontextension(gctx, pdf, ixref)); - else - exto = PyString_FromString("n/a"); - PyObject *simpleo = JM_BOOL(simple); - PyObject *idxo = PyInt_FromLong((long) idx); - info = PyDict_New(); - PyDict_SetItemString(info, "name", name); - PyDict_SetItemString(info, "simple", simpleo); - PyDict_SetItemString(info, "ext", exto); - fz_drop_font(gctx, font); - // resources and fonts objects will contain named reference to font - pdf_dict_puts(gctx, fonts, fontname, font_obj); - pdf_dict_put(gctx, resources, PDF_NAME_Font, fonts); } fz_catch(gctx) return NULL; - pdf->dirty = 1; - return Py_BuildValue("[i, O]", ixref, info); + if (w > 0 && h > 0) + fz_layout_document(gctx, doc, w, h, fontsize); + return doc; } -SWIGINTERN PyObject *fz_page_s__getContents(struct fz_page_s *self){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - PyObject *list = NULL; - pdf_obj *contents = NULL, *icont = NULL; - int i, xref; - fz_try(gctx) - { - assert_PDF(page); - list = PyList_New(0); - contents = pdf_dict_get(gctx, page->obj, PDF_NAME_Contents); - if (pdf_is_array(gctx, contents)) - { for (i=0; i < pdf_array_len(gctx, contents); i++) - { - icont = pdf_array_get(gctx, contents, i); - xref = pdf_to_num(gctx, icont); - PyList_Append(list, PyInt_FromLong((long) xref)); - } - } - else - { - xref = pdf_to_num(gctx, contents); - PyList_Append(list, PyInt_FromLong((long) xref)); - } - } - fz_catch(gctx) return NULL; - return list; - } -SWIGINTERN PyObject *fz_page_s__setContents(struct fz_page_s *self,int xref){ - pdf_page *page = pdf_page_from_fz_page(gctx, self); - pdf_obj *contents = NULL; - - fz_try(gctx) - { - assert_PDF(page); - if (!INRANGE(xref, 1, pdf_xref_len(gctx, page->doc) - 1)) - THROWMSG("xref out of range"); - contents = pdf_new_indirect(gctx, page->doc, xref, 0); - if (!pdf_is_stream(gctx, contents)) - THROWMSG("xref is not a stream"); - pdf_dict_put_drop(gctx, page->obj, PDF_NAME_Contents, contents); +SWIGINTERN void fz_document_s_close(struct fz_document_s *self){ + DEBUGMSG1("doc.close()"); + while(self->refs > 1) { + fz_drop_document(gctx, self); } - fz_catch(gctx) return NULL; - page->doc->dirty = 1; - return NONE; + fz_drop_document(gctx, self); + DEBUGMSG2; } - #define SWIG_From_double PyFloat_FromDouble +#include +#if !defined(SWIG_NO_LLONG_MAX) +# if !defined(LLONG_MAX) && defined(__GNUC__) && defined (__LONG_LONG_MAX__) +# define LLONG_MAX __LONG_LONG_MAX__ +# define LLONG_MIN (-LLONG_MAX - 1LL) +# define ULLONG_MAX (LLONG_MAX * 2ULL + 1ULL) +# endif +#endif -SWIGINTERNINLINE PyObject * -SWIG_From_float (float value) -{ - return SWIG_From_double (value); +SWIGINTERNINLINE int +SWIG_CanCastAsInteger(double *d, double min, double max) { + double x = *d; + if ((min <= x && x <= max)) { + double fx = floor(x); + double cx = ceil(x); + double rd = ((x - fx) < 0.5) ? fx : cx; /* simple rint */ + if ((errno == EDOM) || (errno == ERANGE)) { + errno = 0; + } else { + double summ, reps, diff; + if (rd < x) { + diff = x - rd; + } else if (rd > x) { + diff = rd - x; + } else { + return 1; + } + summ = rd + x; + reps = diff/summ; + if (reps < 8*DBL_EPSILON) { + *d = rd; + return 1; + } + } + } + return 0; } -SWIGINTERN void delete_fz_rect_s(struct fz_rect_s *self){ - DEBUGMSG1("rect"); - JM_Free(self); - DEBUGMSG2; - } -SWIGINTERN struct fz_rect_s *new_fz_rect_s__SWIG_1(struct fz_rect_s const *s){ - fz_rect *r = JM_Alloc(fz_rect, 1); - if (!s) + +SWIGINTERN int +SWIG_AsVal_long (PyObject *obj, long* val) +{ +#if PY_VERSION_HEX < 0x03000000 + if (PyInt_Check(obj)) { + if (val) *val = PyInt_AsLong(obj); + return SWIG_OK; + } else +#endif + if (PyLong_Check(obj)) { + long v = PyLong_AsLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + return SWIG_OverflowError; + } + } +#ifdef SWIG_PYTHON_CAST_MODE + { + int dispatch = 0; + long v = PyInt_AsLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_AddCast(SWIG_OK); + } else { + PyErr_Clear(); + } + if (!dispatch) { + double d; + int res = SWIG_AddCast(SWIG_AsVal_double (obj,&d)); + if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, LONG_MIN, LONG_MAX)) { + if (val) *val = (long)(d); + return res; + } + } + } +#endif + return SWIG_TypeError; +} + + +SWIGINTERN int +SWIG_AsVal_int (PyObject * obj, int *val) +{ + long v; + int res = SWIG_AsVal_long (obj, &v); + if (SWIG_IsOK(res)) { + if ((v < INT_MIN || v > INT_MAX)) { + return SWIG_OverflowError; + } else { + if (val) *val = (int)(v); + } + } + return res; +} + +SWIGINTERN struct fz_page_s *fz_document_s_loadPage(struct fz_document_s *self,int number){ + struct fz_page_s *page = NULL; + fz_try(gctx) { - r->x0 = r->y0 = r->x1 = r->y1 = 0; + int pageCount = fz_count_pages(gctx, self); + if (pageCount < 1) THROWMSG("document has no pages"); + int n = number; + while (n < 0) n += pageCount; + page = fz_load_page(gctx, self, n); } - else *r = *s; - return r; - } -SWIGINTERN struct fz_rect_s *new_fz_rect_s__SWIG_2(struct fz_point_s const *lt,struct fz_point_s const *rb){ - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = lt->x; - r->y0 = lt->y; - r->x1 = rb->x; - r->y1 = rb->y; - return r; + fz_catch(gctx) return NULL; + return page; } -SWIGINTERN struct fz_rect_s *new_fz_rect_s__SWIG_3(float x0,float y0,struct fz_point_s const *rb){ - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = x0; - r->y0 = y0; - r->x1 = rb->x; - r->y1 = rb->y; - return r; +SWIGINTERN struct fz_outline_s *fz_document_s__loadOutline(struct fz_document_s *self){ + fz_outline *ol = NULL; + fz_try(gctx) ol = fz_load_outline(gctx, self); + fz_catch(gctx) return NULL; + return ol; } -SWIGINTERN struct fz_rect_s *new_fz_rect_s__SWIG_4(struct fz_point_s const *lt,float x1,float y1){ - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = lt->x; - r->y0 = lt->y; - r->x1 = x1; - r->y1 = y1; - return r; +SWIGINTERN void fz_document_s__dropOutline(struct fz_document_s *self,struct fz_outline_s *ol){ + DEBUGMSG1("outline"); + fz_drop_outline(gctx, ol); + DEBUGMSG2; } -SWIGINTERN struct fz_rect_s *new_fz_rect_s__SWIG_5(float x0,float y0,float x1,float y1){ - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = x0; - r->y0 = y0; - r->x1 = x1; - r->y1 = y1; - return r; +SWIGINTERN int fz_document_s_embeddedFileCount(struct fz_document_s *self){ + pdf_document *pdf = pdf_document_from_fz_document(gctx, self); + if (!pdf) return 0; + return pdf_count_portfolio_entries(gctx, pdf); } -SWIGINTERN struct fz_rect_s *new_fz_rect_s__SWIG_6(PyObject *list){ - fz_rect *r = JM_Alloc(fz_rect, 1); + +SWIGINTERNINLINE PyObject* + SWIG_From_int (int value) +{ + return PyInt_FromLong((long) value); +} + +SWIGINTERN int fz_document_s_embeddedFileDel(struct fz_document_s *self,char *name){ + pdf_document *pdf = pdf_document_from_fz_document(gctx, self); + pdf_obj *names; + int i, n, m; + fz_var(names); fz_try(gctx) { - if (!PySequence_Check(list)) THROWMSG("expected a sequence"); - if (PySequence_Size(list) != 4) THROWMSG("len(sequence) invalid"); - r->x0 = (float) PyFloat_AsDouble(PySequence_GetItem(list, 0)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->y0 = (float) PyFloat_AsDouble(PySequence_GetItem(list, 1)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->x1 = (float) PyFloat_AsDouble(PySequence_GetItem(list, 2)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->y1 = (float) PyFloat_AsDouble(PySequence_GetItem(list, 3)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - } - fz_catch(gctx) - { - JM_Free(r); - return NULL; + assert_PDF(pdf); + // check presence of name + if (JM_find_embedded(gctx, Py_BuildValue("s", name), pdf) < 0) + THROWMSG("name not found"); + + names = JM_embedded_names(gctx, pdf); + if (!pdf_is_array(gctx, names)) + THROWMSG("could not find names array"); + n = pdf_array_len(gctx, names); + + //------------------------------------------------------------- + // Every file has 2 entries: name and file descriptor. + // First delete file descriptor, then the name entry. + // Because it might be referenced elsewhere, we leave deletion + // of stream object to garbage collection. + //------------------------------------------------------------- + for (i = 0; i < n; i += 2) + { + char *test = (char *) pdf_to_text_string(gctx, pdf_array_get(gctx, names, i)); + if (!strcmp(test, name)) + { + pdf_array_delete(gctx, names, i + 1); + pdf_array_delete(gctx, names, i); + } + } + m = (n - pdf_array_len(gctx, names)) / 2; } - return r; + fz_catch(gctx) return -1; + return m; } -SWIGINTERN struct fz_irect_s *fz_rect_s_round(struct fz_rect_s *self){ - fz_irect *irect = JM_Alloc(fz_irect, 1); - fz_rect rect = {MIN(self->x0, self->x1), - MIN(self->y0, self->y1), - MAX(self->x0, self->x1), - MAX(self->y0, self->y1)}; - fz_round_rect(irect, &rect); - return irect; - } -SWIGINTERN struct fz_rect_s *fz_rect_s_includePoint(struct fz_rect_s *self,struct fz_point_s const *p){ - if (fz_is_infinite_rect(self)) return self; - return fz_include_point_in_rect(self, p); - } -SWIGINTERN struct fz_rect_s *fz_rect_s_intersect(struct fz_rect_s *self,struct fz_rect_s *r){ - fz_intersect_rect(self, r); - return self; - } -SWIGINTERN struct fz_rect_s *fz_rect_s_includeRect(struct fz_rect_s *self,struct fz_rect_s *r){ - fz_union_rect(self, r); - return self; - } -SWIGINTERN struct fz_rect_s *fz_rect_s_normalize(struct fz_rect_s *self){ - float f; - if (self->x1 < self->x0) +SWIGINTERN PyObject *fz_document_s_embeddedFileInfo(struct fz_document_s *self,PyObject *id){ + pdf_document *pdf = pdf_document_from_fz_document(gctx, self); + Py_ssize_t name_len = 0; + int n = -1; + char *name = NULL; + char *sname = NULL; + fz_try(gctx) { - f = self->x1; - self->x1 = self->x0; - self->x0 = f; + assert_PDF(pdf); + n = JM_find_embedded(gctx, id, pdf); + if (n < 0) THROWMSG("entry not found"); } - if (self->y1 < self->y0) + fz_catch(gctx) return NULL; + + PyObject *infodict = PyDict_New(); + // name of file entry + name = (char *) pdf_to_text_string(gctx, pdf_portfolio_entry_name(gctx, pdf, n)); + PyDict_SetItemString(infodict, "name", JM_UNICODE(name)); + + pdf_obj *o = pdf_portfolio_entry_obj(gctx, pdf, n); + + name = (char *) pdf_to_text_string(gctx, pdf_dict_get(gctx, o, PDF_NAME(F))); + PyDict_SetItemString(infodict, "filename", JM_UNICODE(name)); + + name = (char *) pdf_to_text_string(gctx, pdf_dict_get(gctx, o, PDF_NAME(UF))); + PyDict_SetItemString(infodict, "ufilename", JM_UNICODE(name)); + + name = (char *) pdf_to_text_string(gctx, pdf_dict_get(gctx, o, PDF_NAME(Desc))); + PyDict_SetItemString(infodict, "desc", JM_UNICODE(name)); + + int len = -1, DL = -1; + pdf_obj *ef = pdf_dict_get(gctx, o, PDF_NAME(EF)); + o = pdf_dict_getl(gctx, ef, PDF_NAME(F), + PDF_NAME(Length), NULL); + if (o) len = pdf_to_int(gctx, o); + + o = pdf_dict_getl(gctx, ef, PDF_NAME(F), PDF_NAME(DL), NULL); + if (o) DL = pdf_to_int(gctx, o); + else { - f = self->y1; - self->y1 = self->y0; - self->y0 = f; + o = pdf_dict_getl(gctx, ef, PDF_NAME(F), PDF_NAME(Params), + PDF_NAME(Size), NULL); + if (o) DL = pdf_to_int(gctx, o); } - return self; - } -SWIGINTERN PyObject *fz_rect_s_contains__SWIG_0(struct fz_rect_s *self,struct fz_rect_s *rect){ - if (fz_is_empty_rect(rect)) Py_RETURN_TRUE; - if (fz_is_empty_rect(self)) Py_RETURN_FALSE; - float l = MIN(self->x0, self->x1); - float r = MAX(self->x0, self->x1); - float t = MIN(self->y0, self->y1); - float b = MAX(self->y0, self->y1); - - return JM_BOOL(INRANGE(rect->x0, l, r) && - INRANGE(rect->x1, l, r) && - INRANGE(rect->y0, t, b) && - INRANGE(rect->y1, t, b)); - } -SWIGINTERN PyObject *fz_rect_s_contains__SWIG_1(struct fz_rect_s *self,struct fz_irect_s *rect){ - if (fz_is_empty_irect(rect)) Py_RETURN_TRUE; - if (fz_is_empty_rect(self)) Py_RETURN_FALSE; - float l = MIN(self->x0, self->x1); - float r = MAX(self->x0, self->x1); - float t = MIN(self->y0, self->y1); - float b = MAX(self->y0, self->y1); - - return JM_BOOL(INRANGE(rect->x0, l, r) && - INRANGE(rect->x1, l, r) && - INRANGE(rect->y0, t, b) && - INRANGE(rect->y1, t, b)); - } -SWIGINTERN PyObject *fz_rect_s_contains__SWIG_2(struct fz_rect_s *self,struct fz_point_s *p){ - if (fz_is_empty_rect(self)) Py_RETURN_FALSE; - float l = MIN(self->x0, self->x1); - float r = MAX(self->x0, self->x1); - float t = MIN(self->y0, self->y1); - float b = MAX(self->y0, self->y1); - - return JM_BOOL(INRANGE(p->x, l, r) && - INRANGE(p->y, t, b)); - } -SWIGINTERN PyObject *fz_rect_s_isEmpty(struct fz_rect_s *self){ - return JM_BOOL(fz_is_empty_rect(self)); - } -SWIGINTERN PyObject *fz_rect_s_isInfinite(struct fz_rect_s *self){ - return JM_BOOL(fz_is_infinite_rect(self)); - } -SWIGINTERN void delete_fz_irect_s(struct fz_irect_s *self){ - DEBUGMSG1("irect"); - JM_Free(self); - DEBUGMSG2; + + PyDict_SetItemString(infodict, "size", Py_BuildValue("i", DL)); + PyDict_SetItemString(infodict, "length", Py_BuildValue("i", len)); + return infodict; } -SWIGINTERN struct fz_irect_s *new_fz_irect_s__SWIG_1(struct fz_irect_s const *s){ - fz_irect *r = JM_Alloc(fz_irect, 1); - if (!s) - { - r->x0 = r->y0 = r->x1 = r->y1 = 0; - } - else *r = *s; - return r; - } -SWIGINTERN struct fz_irect_s *new_fz_irect_s__SWIG_2(int x0,int y0,int x1,int y1){ - fz_irect *r = JM_Alloc(fz_irect, 1); - r->x0 = x0; - r->y0 = y0; - r->x1 = x1; - r->y1 = y1; - return r; - } -SWIGINTERN struct fz_irect_s *new_fz_irect_s__SWIG_3(PyObject *list){ - fz_irect *r = JM_Alloc(fz_irect, 1); - fz_try(gctx) - { - if (!PySequence_Check(list)) THROWMSG("expected a sequence"); - if (PySequence_Size(list) != 4) THROWMSG("len(sequence) invalid"); - r->x0 = (int) PyInt_AsLong(PySequence_GetItem(list, 0)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->y0 = (int) PyInt_AsLong(PySequence_GetItem(list, 1)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->x1 = (int) PyInt_AsLong(PySequence_GetItem(list, 2)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - r->y1 = (int) PyInt_AsLong(PySequence_GetItem(list, 3)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - } - fz_catch(gctx) - { - JM_Free(r); - return NULL; - } - return r; - } -SWIGINTERN PyObject *fz_irect_s_isEmpty(struct fz_irect_s *self){ - return JM_BOOL(fz_is_empty_irect(self)); - } -SWIGINTERN PyObject *fz_irect_s_isInfinite(struct fz_irect_s *self){ - return JM_BOOL(fz_is_infinite_irect(self)); - } -SWIGINTERN struct fz_irect_s *fz_irect_s_normalize(struct fz_irect_s *self){ - int f; - if (self->x1 < self->x0) - { - f = self->x1; - self->x1 = self->x0; - self->x0 = f; - } - if (self->y1 < self->y0) - { - f = self->y1; - self->y1 = self->y0; - self->y0 = f; - } - return self; - } -SWIGINTERN PyObject *fz_irect_s_contains__SWIG_0(struct fz_irect_s *self,struct fz_irect_s *rect){ - if (fz_is_empty_irect(rect)) Py_RETURN_TRUE; - if (fz_is_empty_irect(self)) Py_RETURN_FALSE; - int l = MIN(self->x0, self->x1); - int r = MAX(self->x0, self->x1); - int t = MIN(self->y0, self->y1); - int b = MAX(self->y0, self->y1); - - return JM_BOOL(INRANGE(rect->x0, l, r) && - INRANGE(rect->x1, l, r) && - INRANGE(rect->y0, t, b) && - INRANGE(rect->y1, t, b)); - } -SWIGINTERN PyObject *fz_irect_s_contains__SWIG_1(struct fz_irect_s *self,struct fz_rect_s *rect){ - if (fz_is_empty_rect(rect)) Py_RETURN_TRUE; - if (fz_is_empty_irect(self)) Py_RETURN_FALSE; - float l = MIN(self->x0, self->x1); - float r = MAX(self->x0, self->x1); - float t = MIN(self->y0, self->y1); - float b = MAX(self->y0, self->y1); - - return JM_BOOL(INRANGE(rect->x0, l, r) && - INRANGE(rect->x1, l, r) && - INRANGE(rect->y0, t, b) && - INRANGE(rect->y1, t, b)); - } -SWIGINTERN PyObject *fz_irect_s_contains__SWIG_2(struct fz_irect_s *self,struct fz_point_s *p){ - if (fz_is_empty_irect(self)) Py_RETURN_FALSE; - float l = MIN(self->x0, self->x1); - float r = MAX(self->x0, self->x1); - float t = MIN(self->y0, self->y1); - float b = MAX(self->y0, self->y1); - - return JM_BOOL(INRANGE(p->x, l, r) && - INRANGE(p->y, t, b)); - } -SWIGINTERN struct fz_irect_s *fz_irect_s_translate(struct fz_irect_s *self,int xoff,int yoff){ - fz_translate_irect(self, xoff, yoff); - return self; - } -SWIGINTERN struct fz_irect_s *fz_irect_s_intersect(struct fz_irect_s *self,struct fz_irect_s *ir){ - fz_intersect_irect(self, ir); - return self; - } -SWIGINTERN void delete_fz_pixmap_s(struct fz_pixmap_s *self){ - DEBUGMSG1("pixmap"); - fz_drop_pixmap(gctx, self); - DEBUGMSG2; - } -SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_0(struct fz_colorspace_s *cs,struct fz_irect_s const *bbox,int alpha){ - fz_pixmap *pm = NULL; - fz_separations *seps = NULL; - fz_try(gctx) - pm = fz_new_pixmap_with_bbox(gctx, cs, bbox, seps, alpha); - fz_catch(gctx) return NULL; - return pm; - } -SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_1(struct fz_colorspace_s *cs,struct fz_pixmap_s *spix){ - fz_pixmap *pm = NULL; - fz_try(gctx) - { - if (!fz_pixmap_colorspace(gctx, spix)) - THROWMSG("cannot copy pixmap with NULL colorspace"); - pm = fz_convert_pixmap(gctx, spix, cs, NULL, NULL, NULL, 1); - } - fz_catch(gctx) return NULL; - return pm; - } -SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_2(struct fz_pixmap_s *spix,float w,float h,struct fz_irect_s *clip){ - fz_pixmap *pm = NULL; - fz_try(gctx) - { - pm = fz_scale_pixmap(gctx, spix, spix->x, spix->y, w, h, clip); - } - fz_catch(gctx) return NULL; - return pm; - } -SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_3(struct fz_pixmap_s *spix,int alpha){ - fz_pixmap *pm = NULL; - int n, w, h, i; - fz_separations *seps = NULL; +SWIGINTERN PyObject *fz_document_s_embeddedFileUpd(struct fz_document_s *self,PyObject *id,PyObject *buffer,char *filename,char *ufilename,char *desc){ + pdf_document *pdf = pdf_document_from_fz_document(gctx, self); + fz_buffer *res = NULL; + fz_var(res); fz_try(gctx) { - if (!INRANGE(alpha, 0, 1)) - THROWMSG("illegal alpha value"); - fz_colorspace *cs = fz_pixmap_colorspace(gctx, spix); - if (!cs && !alpha) - THROWMSG("cannot drop alpha for 'NULL' colorspace"); - n = fz_pixmap_colorants(gctx, spix); - w = fz_pixmap_width(gctx, spix); - h = fz_pixmap_height(gctx, spix); - pm = fz_new_pixmap(gctx, cs, w, h, seps, alpha); - pm->x = spix->x; - pm->y = spix->y; - pm->xres = spix->xres; - pm->yres = spix->yres; + assert_PDF(pdf); - // copy samples data ------------------------------------------ - unsigned char *sptr = spix->samples; - unsigned char *tptr = pm->samples; - if (spix->alpha == pm->alpha) // identical samples --------- - memcpy(tptr, sptr, w * h * (n + alpha)); - else + int n = JM_find_embedded(gctx, id, pdf); + if (n < 0) THROWMSG("entry not found"); + + pdf_obj *entry = pdf_portfolio_entry_obj(gctx, pdf, n); + pdf_obj *filespec = pdf_dict_getl(gctx, entry, PDF_NAME(EF), + PDF_NAME(F), NULL); + + char *data = NULL; + size_t len = JM_CharFromBytesOrArray(buffer, &data); + if (len > 0) { - for (i = 0; i < w * h; i++) - { - memcpy(tptr, sptr, n); - tptr += n; - if (pm->alpha) - { - tptr[0] = 255; - tptr++; - } - sptr += n + spix->alpha; - } + if (!filespec) THROWMSG("/EF object not found"); + res = fz_new_buffer_from_copied_data(gctx, data, len); + JM_update_stream(gctx, pdf, filespec, res); + // adjust /DL and /Size parameters + pdf_obj *l = pdf_new_int(gctx, (int64_t) len); + pdf_dict_put(gctx, filespec, PDF_NAME(DL), l); + pdf_dict_putl(gctx, filespec, l, PDF_NAME(Params), PDF_NAME(Size), NULL); } + if (filename) + pdf_dict_put_text_string(gctx, entry, PDF_NAME(F), filename); + + if (ufilename) + pdf_dict_put_text_string(gctx, entry, PDF_NAME(UF), ufilename); + + if (desc) + pdf_dict_put_text_string(gctx, entry, PDF_NAME(Desc), desc); } - fz_catch(gctx) return NULL; - return pm; + fz_always(gctx) + fz_drop_buffer(gctx, res); + fz_catch(gctx) + return NULL; + pdf->dirty = 1; + return NONE; } -SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_4(struct fz_colorspace_s *cs,int w,int h,PyObject *samples,int alpha){ - char *data = NULL; - int n = fz_colorspace_n(gctx, cs); - int stride = (n + alpha)*w; - fz_separations *seps = NULL; - fz_pixmap *pm = NULL; - size_t size = JM_CharFromBytesOrArray(samples, &data); +SWIGINTERN PyObject *fz_document_s_embeddedFileGet(struct fz_document_s *self,PyObject *id){ + PyObject *cont = NULL; + pdf_document *pdf = pdf_document_from_fz_document(gctx, self); + fz_buffer *buf = NULL; + fz_var(buf); fz_try(gctx) { - if (size < 1) THROWMSG("invalid arg type samples"); - if (stride * h != size) THROWMSG("invalid arg len samples"); - pm = fz_new_pixmap_with_data(gctx, cs, w, h, seps, alpha, stride, data); - } - fz_catch(gctx) return NULL; - return pm; - } -SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_5(char *filename){ - fz_image *img = NULL; - fz_pixmap *pm = NULL; - fz_try(gctx) { - if (!filename) THROWMSG("invalid argument type"); - img = fz_new_image_from_file(gctx, filename); - pm = fz_get_pixmap_from_image(gctx, img, NULL, NULL, NULL, NULL); + assert_PDF(pdf); + int i = JM_find_embedded(gctx, id, pdf); + if (i < 0) THROWMSG("entry not found"); + buf = pdf_portfolio_entry(gctx, pdf, i); + cont = JM_BinFromBuffer(gctx, buf); } - fz_always(gctx) fz_drop_image(gctx, img); + fz_always(gctx) fz_drop_buffer(gctx, buf); fz_catch(gctx) return NULL; - return pm; + return cont; } -SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_6(PyObject *imagedata){ +SWIGINTERN PyObject *fz_document_s_embeddedFileAdd(struct fz_document_s *self,PyObject *buffer,char const *name,char *filename,char *ufilename,char *desc){ + pdf_document *pdf = pdf_document_from_fz_document(gctx, self); + fz_buffer *data = NULL, *buf = NULL; + char *buffdata; + fz_var(data); + fz_var(buf); + int entry = 0; size_t size = 0; - char *streamdata; - fz_buffer *data = NULL; - fz_image *img = NULL; - fz_pixmap *pm = NULL; + char *f = filename, *uf = ufilename, *d = desc; + int name_len = (int) strlen(name); + // make adjustments for omitted arguments + if (!f) f = (char *)name; + if (!uf) uf = f; + if (!d) d = f; + fz_try(gctx) { - size = JM_CharFromBytesOrArray(imagedata, &streamdata); - if (size < 1) THROWMSG("invalid argument type"); - data = fz_new_buffer_from_shared_data(gctx, - streamdata, size); - img = fz_new_image_from_buffer(gctx, data); - pm = fz_get_pixmap_from_image(gctx, img, NULL, NULL, NULL, NULL); + assert_PDF(pdf); + size = JM_CharFromBytesOrArray(buffer, &buffdata); + if (size < 1) THROWMSG("buffer not bytes / bytearray"); + + // we do not allow duplicate names + entry = JM_find_embedded(gctx, Py_BuildValue("s", name), pdf); + if (entry >= 0) THROWMSG("name already exists"); + + // first insert a dummy entry with no more than the name + buf = fz_new_buffer(gctx, name_len + 1); // has no real meaning + fz_append_string(gctx, buf, name); // fill something in + fz_terminate_buffer(gctx, buf); // to make it usable + pdf_add_portfolio_entry(gctx, pdf, // insert the entry + name, name_len, // except the name, + name, name_len, // everythinh will + name, name_len, // be overwritten + name, name_len, + buf); + fz_drop_buffer(gctx, buf); // kick stupid buffer + buf = NULL; + //------------------------------------------------------------- + // now modify the entry just created: + // (1) allow unicode values for filenames and description + // (2) deflate the file content + //------------------------------------------------------------- + // locate the entry again + entry = JM_find_embedded(gctx, Py_BuildValue("s", name), pdf); + // (1) insert the real metadata + pdf_obj *o = pdf_portfolio_entry_obj(gctx, pdf, entry); + pdf_dict_put_text_string(gctx, o, PDF_NAME(F), f); + pdf_dict_put_text_string(gctx, o, PDF_NAME(UF), uf); + pdf_dict_put_text_string(gctx, o, PDF_NAME(Desc), d); + // (2) insert the real file contents + pdf_obj *filespec = pdf_dict_getl(gctx, o, PDF_NAME(EF), + PDF_NAME(F), NULL); + data = fz_new_buffer_from_copied_data(gctx, buffdata, size); + JM_update_stream(gctx, pdf, filespec, data); + // finally update some size attributes + pdf_obj *l = pdf_new_int(gctx, (int64_t) size); + pdf_dict_put(gctx, filespec, PDF_NAME(DL), l); + pdf_dict_putl(gctx, filespec, l, PDF_NAME(Params), PDF_NAME(Size), NULL); } fz_always(gctx) { - fz_drop_image(gctx, img); + fz_drop_buffer(gctx, buf); + fz_drop_buffer(gctx, data); } fz_catch(gctx) return NULL; - return pm; + pdf->dirty = 1; + return NONE; } -SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_7(struct fz_document_s *doc,int xref){ - fz_image *img = NULL; - fz_pixmap *pix = NULL; - pdf_obj *ref = NULL; - pdf_obj *type; - pdf_document *pdf = pdf_specifics(gctx, doc); +SWIGINTERN PyObject *fz_document_s_convertToPDF(struct fz_document_s *self,int from_page,int to_page,int rotate){ + PyObject *doc = NULL; fz_try(gctx) { - assert_PDF(pdf); - int xreflen = pdf_xref_len(gctx, pdf); - if (!INRANGE(xref, 1, xreflen-1)) - THROWMSG("xref out of range"); - ref = pdf_new_indirect(gctx, pdf, xref, 0); - type = pdf_dict_get(gctx, ref, PDF_NAME_Subtype); - if (!pdf_name_eq(gctx, type, PDF_NAME_Image)) - THROWMSG("xref not an image"); - img = pdf_load_image(gctx, pdf, ref); - pix = fz_get_pixmap_from_image(gctx, img, NULL, NULL, NULL, NULL); + int fp = from_page, tp = to_page, srcCount = fz_count_pages(gctx, self); + if (pdf_specifics(gctx, self)) + THROWMSG("use select+write or insertPDF for PDF docs instead"); + if (fp < 0) fp = 0; + if (fp > srcCount - 1) fp = srcCount - 1; + if (tp < 0) tp = srcCount - 1; + if (tp > srcCount - 1) tp = srcCount - 1; + doc = JM_convert_to_pdf(gctx, self, fp, tp, rotate); } - fz_always(gctx) + fz_catch(gctx) return NULL; + return doc; + } +SWIGINTERN int fz_document_s_pageCount(struct fz_document_s *self){ + return fz_count_pages(gctx, self); + } +SWIGINTERN char *fz_document_s__getMetadata(struct fz_document_s *self,char const *key){ + int vsize; + char *value; + vsize = fz_lookup_metadata(gctx, self, key, NULL, 0)+1; + if(vsize > 1) { - fz_drop_image(gctx, img); - pdf_drop_obj(gctx, ref); + value = JM_Alloc(char, vsize); + fz_lookup_metadata(gctx, self, key, value, vsize); + return value; } - fz_catch(gctx) - { - fz_drop_pixmap(gctx, pix); + else return NULL; - } - return pix; } -SWIGINTERN void fz_pixmap_s_shrink(struct fz_pixmap_s *self,int factor){ - if (factor < 1) - { - PySys_WriteStdout("warning: ignoring shrink factor < 1\n"); - return; - } - fz_subsample_pixmap(gctx, self, factor); + +SWIGINTERNINLINE PyObject * +SWIG_FromCharPtrAndSize(const char* carray, size_t size) +{ + if (carray) { + if (size > INT_MAX) { + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + return pchar_descriptor ? + SWIG_InternalNewPointerObj((char *)(carray), pchar_descriptor, 0) : SWIG_Py_Void(); + } else { +#if PY_VERSION_HEX >= 0x03000000 +#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) + return PyBytes_FromStringAndSize(carray, (Py_ssize_t)(size)); +#else +#if PY_VERSION_HEX >= 0x03010000 + return PyUnicode_DecodeUTF8(carray, (Py_ssize_t)(size), "surrogateescape"); +#else + return PyUnicode_FromStringAndSize(carray, (Py_ssize_t)(size)); +#endif +#endif +#else + return PyString_FromStringAndSize(carray, (Py_ssize_t)(size)); +#endif + } + } else { + return SWIG_Py_Void(); + } +} + + +SWIGINTERNINLINE PyObject * +SWIG_FromCharPtr(const char *cptr) +{ + return SWIG_FromCharPtrAndSize(cptr, (cptr ? strlen(cptr) : 0)); +} + +SWIGINTERN int fz_document_s_needsPass(struct fz_document_s *self){ + return fz_needs_password(gctx, self); } -SWIGINTERN void fz_pixmap_s_gammaWith(struct fz_pixmap_s *self,float gamma){ - if (!fz_pixmap_colorspace(gctx, self)) +SWIGINTERN PyObject *fz_document_s_resolveLink(struct fz_document_s *self,char *uri){ + if (!uri) return NONE; + float xp = 0.0f, yp = 0.0f; + int pno = -1; + fz_try(gctx) + pno = fz_resolve_link(gctx, self, uri, &xp, &yp); + fz_catch(gctx) + return NONE; + if (pno < 0) return NONE; + return Py_BuildValue("iff", pno, xp, yp); + } +SWIGINTERN PyObject *fz_document_s_layout(struct fz_document_s *self,PyObject *rect,float width,float height,float fontsize){ + if (!fz_is_document_reflowable(gctx, self)) return NONE; + fz_try(gctx) { - PySys_WriteStdout("warning: colorspace invalid for function\n"); - return; + float w = width, h = height; + fz_rect r = JM_rect_from_py(rect); + if (!fz_is_empty_rect(r) && !fz_is_infinite_rect(r)) + { + w = r.x1 - r.x0; + h = r.y1 - r.y0; + } + if (w <= 0.0f || h <= 0.0f) + THROWMSG("invalid page size"); + fz_layout_document(gctx, self, w, h, fontsize); } - fz_gamma_pixmap(gctx, self, gamma); - } -SWIGINTERN void fz_pixmap_s_tintWith(struct fz_pixmap_s *self,int red,int green,int blue){ - fz_tint_pixmap(gctx, self, red, green, blue); + fz_catch(gctx) return NULL; + return NONE; } -SWIGINTERN void fz_pixmap_s_clearWith__SWIG_0(struct fz_pixmap_s *self){ - fz_clear_pixmap(gctx, self); +SWIGINTERN PyObject *fz_document_s_makeBookmark(struct fz_document_s *self,int pno){ + if (!fz_is_document_reflowable(gctx, self)) return NONE; + int n = pno, cp = fz_count_pages(gctx, self); + while(n < 0) n += cp; + long long mark = (long long) fz_make_bookmark(gctx, self, n); + return PyLong_FromLongLong(mark); } -SWIGINTERN void fz_pixmap_s_clearWith__SWIG_1(struct fz_pixmap_s *self,int value){ - fz_clear_pixmap_with_value(gctx, self, value); + +#if defined(LLONG_MAX) && !defined(SWIG_LONG_LONG_AVAILABLE) +# define SWIG_LONG_LONG_AVAILABLE +#endif + + +#ifdef SWIG_LONG_LONG_AVAILABLE +SWIGINTERN int +SWIG_AsVal_long_SS_long (PyObject *obj, long long *val) +{ + int res = SWIG_TypeError; + if (PyLong_Check(obj)) { + long long v = PyLong_AsLongLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + res = SWIG_OverflowError; + } + } else { + long v; + res = SWIG_AsVal_long (obj,&v); + if (SWIG_IsOK(res)) { + if (val) *val = v; + return res; + } + } +#ifdef SWIG_PYTHON_CAST_MODE + { + const double mant_max = 1LL << DBL_MANT_DIG; + const double mant_min = -mant_max; + double d; + res = SWIG_AsVal_double (obj,&d); + if (SWIG_IsOK(res) && !SWIG_CanCastAsInteger(&d, mant_min, mant_max)) + return SWIG_OverflowError; + if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, mant_min, mant_max)) { + if (val) *val = (long long)(d); + return SWIG_AddCast(res); + } + res = SWIG_TypeError; + } +#endif + return res; +} +#endif + +SWIGINTERN int fz_document_s_findBookmark(struct fz_document_s *self,long long bookmark){ + if (!fz_is_document_reflowable(gctx, self)) return -1; + fz_bookmark m = (fz_bookmark) bookmark; + return fz_lookup_bookmark(gctx, self, m); } -SWIGINTERN void fz_pixmap_s_clearWith__SWIG_2(struct fz_pixmap_s *self,int value,struct fz_irect_s const *bbox){ - fz_clear_pixmap_rect_with_value(gctx, self, value, bbox); +SWIGINTERN PyObject *fz_document_s_isReflowable(struct fz_document_s *self){ + return JM_BOOL(fz_is_document_reflowable(gctx, self)); } -SWIGINTERN PyObject *fz_pixmap_s_copyPixmap(struct fz_pixmap_s *self,struct fz_pixmap_s *src,struct fz_irect_s const *bbox){ +SWIGINTERN PyObject *fz_document_s__deleteObject(struct fz_document_s *self,int xref){ + pdf_document *pdf = pdf_specifics(gctx, self); fz_try(gctx) { - if (!fz_pixmap_colorspace(gctx, src)) - THROWMSG("cannot copy pixmap with NULL colorspace"); - fz_copy_pixmap_rect(gctx, self, src, bbox, NULL); + assert_PDF(pdf); + if (!INRANGE(xref, 1, pdf_xref_len(gctx, pdf)-1)) + THROWMSG("xref out of range"); + pdf_delete_object(gctx, pdf, xref); } fz_catch(gctx) return NULL; return NONE; } -SWIGINTERN int fz_pixmap_s_stride(struct fz_pixmap_s *self){ - return fz_pixmap_stride(gctx, self); +SWIGINTERN int fz_document_s__getPDFroot(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); + int xref = 0; + if (!pdf) return xref; + fz_try(gctx) + { + pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), + PDF_NAME(Root)); + xref = pdf_to_num(gctx, root); + } + fz_catch(gctx) {;} + return xref; } -SWIGINTERN int fz_pixmap_s_alpha(struct fz_pixmap_s *self){ - return self->alpha; +SWIGINTERN PyObject *fz_document_s_isPDF(struct fz_document_s *self){ + if (pdf_specifics(gctx, self)) Py_RETURN_TRUE; + else Py_RETURN_FALSE; } -SWIGINTERN struct fz_colorspace_s *fz_pixmap_s_colorspace(struct fz_pixmap_s *self){ - return fz_pixmap_colorspace(gctx, self); +SWIGINTERN PyObject *fz_document_s__hasXrefStream(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); + if (!pdf) Py_RETURN_FALSE; + if (pdf->has_xref_streams) Py_RETURN_TRUE; + Py_RETURN_FALSE; } -SWIGINTERN struct fz_irect_s *fz_pixmap_s_irect(struct fz_pixmap_s *self){ - fz_irect *r = JM_Alloc(fz_irect, 1); - r->x0 = r->y0 = r->x1 = r->y1 = 0; - return fz_pixmap_bbox(gctx, self, r); +SWIGINTERN PyObject *fz_document_s__hasXrefOldStyle(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); + if (!pdf) Py_RETURN_FALSE; + if (pdf->has_old_style_xrefs) Py_RETURN_TRUE; + Py_RETURN_FALSE; } -SWIGINTERN int fz_pixmap_s_size(struct fz_pixmap_s *self){ - return (int) fz_pixmap_size(gctx, self); +SWIGINTERN PyObject *fz_document_s_isDirty(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); + if (!pdf) Py_RETURN_FALSE; + return JM_BOOL(pdf_has_unsaved_changes(gctx, pdf)); } -SWIGINTERN PyObject *fz_pixmap_s_setAlpha(struct fz_pixmap_s *self,PyObject *alphavalues){ +SWIGINTERN int fz_document_s__getGCTXerrcode(struct fz_document_s *self){ + return fz_caught(gctx); + } +SWIGINTERN char const *fz_document_s__getGCTXerrmsg(struct fz_document_s *self){ + return fz_caught_message(gctx); + } +SWIGINTERN int fz_document_s_authenticate(struct fz_document_s *self,char *password){ + return fz_authenticate_password(gctx, self, (const char *) password); + } +SWIGINTERN PyObject *fz_document_s_save(struct fz_document_s *self,char *filename,int garbage,int clean,int deflate,int incremental,int ascii,int expand,int linear,int pretty,int decrypt){ + int errors = 0; + pdf_write_options opts = { 0 }; + opts.do_incremental = incremental; + opts.do_ascii = ascii; + opts.do_compress = deflate; + opts.do_compress_images = deflate; + opts.do_compress_fonts = deflate; + opts.do_decompress = expand; + opts.do_garbage = garbage; + opts.do_linear = linear; + opts.do_clean = clean; + opts.do_pretty = pretty; + opts.do_sanitize = clean; + opts.continue_on_error = 1; + opts.errors = &errors; + pdf_document *pdf = pdf_specifics(gctx, self); fz_try(gctx) { - if (self->alpha == 0) THROWMSG("pixmap has no alpha"); - int n = fz_pixmap_colorants(gctx, self); - int w = fz_pixmap_width(gctx, self); - int h = fz_pixmap_height(gctx, self); - int balen = w * h * (n+1); - unsigned char *data = NULL; - size_t data_len = 0; - if (alphavalues) - { - data_len = JM_CharFromBytesOrArray(alphavalues, &data); - if (data_len > 0 && data_len < w * h) - THROWMSG("not enough alpha values"); - } - int i = 0, k = 0; - while (i < balen) - { - if (data_len > 0) self->samples[i+n] = data[k]; - else self->samples[i+n] = 255; - i += n+1; - k += 1; - } + assert_PDF(pdf); + JM_embedded_clean(gctx, pdf); + JM_save_document(gctx, pdf, filename, &opts, decrypt); + pdf->dirty = 0; } fz_catch(gctx) return NULL; return NONE; } -SWIGINTERN PyObject *fz_pixmap_s_getPNGData(struct fz_pixmap_s *self,int savealpha){ +SWIGINTERN PyObject *fz_document_s_write(struct fz_document_s *self,int garbage,int clean,int deflate,int ascii,int expand,int linear,int pretty,int decrypt){ + PyObject *r; struct fz_buffer_s *res = NULL; fz_output *out = NULL; - PyObject *r; - if (savealpha != -1) PySys_WriteStdout("warning: ignoring savealpha\n"); - fz_try(gctx) { + int errors = 0; + pdf_write_options opts = { 0 }; + opts.do_incremental = 0; + opts.do_ascii = ascii; + opts.do_compress = deflate; + opts.do_compress_images = deflate; + opts.do_compress_fonts = deflate; + opts.do_decompress = expand; + opts.do_garbage = garbage; + opts.do_linear = linear; + opts.do_clean = clean; + opts.do_pretty = pretty; + opts.do_sanitize = clean; + opts.continue_on_error = 1; + opts.errors = &errors; + pdf_document *pdf = pdf_specifics(gctx, self); + fz_var(out); + fz_var(res); + fz_try(gctx) + { + assert_PDF(pdf); + if (fz_count_pages(gctx, self) < 1) + THROWMSG("cannot save with zero pages"); + JM_embedded_clean(gctx, pdf); res = fz_new_buffer(gctx, 1024); out = fz_new_output_with_buffer(gctx, res); - fz_write_pixmap_as_png(gctx, out, self); + JM_write_document(gctx, pdf, out, &opts, decrypt); + pdf->dirty = 0; r = JM_BinFromBuffer(gctx, res); } fz_always(gctx) @@ -8821,4776 +10144,3548 @@ SWIGINTERN PyObject *fz_pixmap_s_getPNGData(struct fz_pixmap_s *self,int savealp fz_catch(gctx) return NULL; return r; } -SWIGINTERN PyObject *fz_pixmap_s__writeIMG(struct fz_pixmap_s *self,char *filename,int format,int savealpha){ - if (savealpha != -1) PySys_WriteStdout("warning: ignoring savealpha\n"); - fz_try(gctx) { - switch(format) - { - case(1): - fz_save_pixmap_as_png(gctx, self, filename); - break; - case(2): - fz_save_pixmap_as_pnm(gctx, self, filename); - break; - case(3): - fz_save_pixmap_as_pam(gctx, self, filename); - break; - case(4): - fz_save_pixmap_as_tga(gctx, self, filename); - break; - } - } - fz_catch(gctx) return NULL; - return NONE; - } -SWIGINTERN void fz_pixmap_s_invertIRect(struct fz_pixmap_s *self,struct fz_irect_s const *irect){ - if (!fz_pixmap_colorspace(gctx, self)) - { - PySys_WriteStdout("warning: ignored for stencil pixmap\n"); - return; - } - - if (irect) fz_invert_pixmap_rect(gctx, self, irect); - else fz_invert_pixmap(gctx, self); - } -SWIGINTERN PyObject *fz_pixmap_s_samples(struct fz_pixmap_s *self){ - return PyBytes_FromStringAndSize((const char *)self->samples, (Py_ssize_t) (self->w)*(self->h)*(self->n)); - } +SWIGINTERN PyObject *fz_document_s_insertPDF(struct fz_document_s *self,struct fz_document_s *docsrc,int from_page,int to_page,int start_at,int rotate,int links){ + pdf_document *pdfout = pdf_specifics(gctx, self); + pdf_document *pdfsrc = pdf_specifics(gctx, docsrc); + int outCount = fz_count_pages(gctx, self); + int srcCount = fz_count_pages(gctx, docsrc); - #define CS_RGB 1 - #define CS_GRAY 2 - #define CS_CMYK 3 + // local copies of page numbers + int fp = from_page, tp = to_page, sa = start_at; -SWIGINTERN void delete_fz_colorspace_s(struct fz_colorspace_s *self){ - DEBUGMSG1("colorspace"); - fz_drop_colorspace(gctx, self); - DEBUGMSG2; - } -SWIGINTERN struct fz_colorspace_s *new_fz_colorspace_s(int type){ - switch(type) { - case 2: - return fz_device_gray(gctx); - break; - case 3: - return fz_device_cmyk(gctx); - break; - case 1: - default: - return fz_device_rgb(gctx); - break; - } - } -SWIGINTERN int fz_colorspace_s_n(struct fz_colorspace_s *self){ - return fz_colorspace_n(gctx, self); - } -SWIGINTERN char const *fz_colorspace_s__name(struct fz_colorspace_s *self){ - return fz_colorspace_name(gctx, self); - } -SWIGINTERN struct DeviceWrapper *new_DeviceWrapper__SWIG_0(struct fz_pixmap_s *pm,struct fz_irect_s *clip){ - struct DeviceWrapper *dw = NULL; - fz_try(gctx) { - dw = (struct DeviceWrapper *)calloc(1, sizeof(struct DeviceWrapper)); - if (!clip) - dw->device = fz_new_draw_device(gctx, &fz_identity, pm); - else - dw->device = fz_new_draw_device_with_bbox(gctx, &fz_identity, pm, clip); - } - fz_catch(gctx) return NULL; - return dw; - } -SWIGINTERN struct DeviceWrapper *new_DeviceWrapper__SWIG_1(struct fz_display_list_s *dl){ - struct DeviceWrapper *dw = NULL; - fz_try(gctx) { - dw = (struct DeviceWrapper *)calloc(1, sizeof(struct DeviceWrapper)); - dw->device = fz_new_list_device(gctx, dl); - dw->list = dl; - fz_keep_display_list(gctx, dl); - } - fz_catch(gctx) return NULL; - return dw; - } -SWIGINTERN struct DeviceWrapper *new_DeviceWrapper__SWIG_2(struct fz_stext_page_s *tp,int flags){ - struct DeviceWrapper *dw = NULL; - fz_try(gctx) { - dw = (struct DeviceWrapper *)calloc(1, sizeof(struct DeviceWrapper)); - fz_stext_options opts; - opts.flags = flags; - dw->device = fz_new_stext_device(gctx, tp, &opts); + // normalize page numbers + fp = MAX(fp, 0); // -1 = first page + fp = MIN(fp, srcCount - 1); // but do not exceed last page + + if (tp < 0) tp = srcCount - 1; // -1 = last page + tp = MIN(tp, srcCount - 1); // but do not exceed last page + + if (sa < 0) sa = outCount; // -1 = behind last page + sa = MIN(sa, outCount); // but that is also the limit + + fz_try(gctx) + { + if (!pdfout || !pdfsrc) THROWMSG("source or target not a PDF"); + merge_range(gctx, pdfout, pdfsrc, fp, tp, sa, rotate); } fz_catch(gctx) return NULL; - return dw; + pdfout->dirty = 1; + return NONE; } -SWIGINTERN void delete_DeviceWrapper(struct DeviceWrapper *self){ - fz_display_list *list = self->list; - DEBUGMSG1("device"); - fz_close_device(gctx, self->device); - fz_drop_device(gctx, self->device); - DEBUGMSG2; - if(list) +SWIGINTERN int fz_document_s_insertPage(struct fz_document_s *self,int pno,PyObject *text,float fontsize,float width,float height,int idx,char *fontname,char *fontfile,int set_simple,PyObject *color){ + pdf_document *pdf = pdf_specifics(gctx, self); + fz_rect mediabox = { 0, 0, 595, 842 }; // ISO-A4 portrait values + mediabox.x1 = width; + mediabox.y1 = height; + pdf_obj *resources = NULL, *page_obj = NULL; + fz_buffer *contents = NULL; + fz_try(gctx) { - DEBUGMSG1("display list after device"); - fz_drop_display_list(gctx, list); - DEBUGMSG2; + assert_PDF(pdf); + if (pno < -1) THROWMSG("invalid page number(s)"); + // create /Resources and /Contents objects + resources = pdf_add_object_drop(gctx, pdf, pdf_new_dict(gctx, pdf, 1)); + contents = fz_new_buffer(gctx, 10); + fz_append_string(gctx, contents, ""); + fz_terminate_buffer(gctx, contents); + page_obj = pdf_add_page(gctx, pdf, mediabox, 0, resources, contents); + pdf_insert_page(gctx, pdf, pno , page_obj); } - } -SWIGINTERN void delete_fz_matrix_s(struct fz_matrix_s *self){ - DEBUGMSG1("matrix"); - JM_Free(self); - DEBUGMSG2; - } -SWIGINTERN struct fz_matrix_s *new_fz_matrix_s__SWIG_1(struct fz_matrix_s const *n){ - fz_matrix *m = JM_Alloc(fz_matrix, 1); - if (!n) + fz_always(gctx) { - m->a = m->b = m->c = m->d = m->e = m->f = 0; + fz_drop_buffer(gctx, contents); + pdf_drop_obj(gctx, page_obj); } - else + fz_catch(gctx) return -1; + pdf->dirty = 1; + return 0; + } +SWIGINTERN PyObject *fz_document_s_select(struct fz_document_s *self,PyObject *pyliste){ + // preparatory stuff: + // (1) get underlying pdf document, + // (2) transform Python list into integer array + + pdf_document *pdf = pdf_specifics(gctx, self); + fz_try(gctx) { - *m = *n; + assert_PDF(pdf); + if (!PySequence_Check(pyliste)) + THROWMSG("sequence required"); + if (PySequence_Size(pyliste) < 1) + THROWMSG("invalid sequ. length"); + // now call retainpages (code copy of fz_clean_file.c) + globals glo = {0}; + glo.ctx = gctx; + glo.doc = pdf; + retainpages(gctx, &glo, pyliste); } - return m; - } -SWIGINTERN struct fz_matrix_s *new_fz_matrix_s__SWIG_2(float sx,float sy,int shear){ - fz_matrix *m = JM_Alloc(fz_matrix, 1); - if(shear) return fz_shear(m, sx, sy); - return fz_scale(m, sx, sy); - } -SWIGINTERN struct fz_matrix_s *new_fz_matrix_s__SWIG_3(float r,float s,float t,float u,float v,float w){ - fz_matrix *m = JM_Alloc(fz_matrix, 1); - m->a = r; - m->b = s; - m->c = t; - m->d = u; - m->e = v; - m->f = w; - return m; + fz_catch(gctx) return NULL; + pdf->dirty = 1; + return NONE; } -SWIGINTERN struct fz_matrix_s *new_fz_matrix_s__SWIG_4(float degree){ - fz_matrix *m = JM_Alloc(fz_matrix, 1); - return fz_rotate(m, degree); +SWIGINTERN PyObject *fz_document_s_permissions(struct fz_document_s *self){ + PyObject *p = JM_BOOL(fz_has_permission(gctx, self, FZ_PERMISSION_PRINT)); + PyObject *e = JM_BOOL(fz_has_permission(gctx, self, FZ_PERMISSION_EDIT)); + PyObject *c = JM_BOOL(fz_has_permission(gctx, self, FZ_PERMISSION_COPY)); + PyObject *n = JM_BOOL(fz_has_permission(gctx, self, FZ_PERMISSION_ANNOTATE)); + PyObject *res = PyDict_New(); + PyDict_SetItemString(res, "print", p); + PyDict_SetItemString(res, "edit", e); + PyDict_SetItemString(res, "copy", c); + PyDict_SetItemString(res, "note", n); + return res; } -SWIGINTERN struct fz_matrix_s *new_fz_matrix_s__SWIG_5(PyObject *list){ - fz_matrix *m = JM_Alloc(fz_matrix, 1); +SWIGINTERN PyObject *fz_document_s__getCharWidths(struct fz_document_s *self,int xref,int limit,int idx){ + pdf_document *pdf = pdf_specifics(gctx, self); + PyObject *wlist = NULL; + int i, glyph, mylimit; + mylimit = limit; + if (mylimit < 256) mylimit = 256; + int cwlen = 0; + const char *data; + int size; + fz_font *font = NULL; + fz_buffer *buf = NULL; + pdf_obj *basefont = NULL; + const char *bfname = NULL; fz_try(gctx) { - if (!PySequence_Check(list)) THROWMSG("expected a sequence"); - if (PySequence_Size(list) != 6) THROWMSG("len(sequence) invalid"); - m->a = (float) PyFloat_AsDouble(PySequence_GetItem(list, 0)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->b = (float) PyFloat_AsDouble(PySequence_GetItem(list, 1)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->c = (float) PyFloat_AsDouble(PySequence_GetItem(list, 2)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->d = (float) PyFloat_AsDouble(PySequence_GetItem(list, 3)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->e = (float) PyFloat_AsDouble(PySequence_GetItem(list, 4)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - m->f = (float) PyFloat_AsDouble(PySequence_GetItem(list, 5)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); + assert_PDF(pdf); + if (xref < 1) THROWMSG("xref must at least 1"); + pdf_obj *o = pdf_load_object(gctx, pdf, xref); + if (pdf_is_dict(gctx, o)) + { + basefont = pdf_dict_get(gctx, o, PDF_NAME(BaseFont)); + if (pdf_is_name(gctx, basefont)) + { + bfname = (char *) pdf_to_name(gctx, basefont); + data = fz_lookup_base14_font(gctx, bfname, &size); + if (data) + { + font = fz_new_font_from_memory(gctx, bfname, data, size, 0, 0); + } + else + { + buf = fontbuffer(gctx, pdf, xref); + if (!buf) THROWMSG("xref is not a supported font"); + font = fz_new_font_from_buffer(gctx, NULL, buf, idx, 0); + } + } + } + else + { + buf = fontbuffer(gctx, pdf, xref); + if (!buf) THROWMSG("xref is not a supported font"); + font = fz_new_font_from_buffer(gctx, NULL, buf, idx, 0); + } + wlist = PyList_New(0); + for (i = 0; i < mylimit; i++) + { + glyph = fz_encode_character(gctx, font, i); + if (glyph > 0) + { + PyList_Append(wlist, Py_BuildValue("(i, f)", glyph, fz_advance_glyph(gctx, font, glyph, 0))); + } + else + { + PyList_Append(wlist, Py_BuildValue("(i, f)", glyph, 0.0)); + } + } + } + fz_always(gctx) + { + fz_drop_buffer(gctx, buf); + fz_drop_font(gctx, font); } fz_catch(gctx) { - JM_Free(m); return NULL; } - return m; - } -SWIGINTERN int fz_matrix_s_invert(struct fz_matrix_s *self,struct fz_matrix_s const *m){ - int rc = fz_try_invert_matrix(self, m); - return rc; - } -SWIGINTERN struct fz_matrix_s *fz_matrix_s_preTranslate(struct fz_matrix_s *self,float sx,float sy){ - fz_pre_translate(self, sx, sy); - return self; - } -SWIGINTERN struct fz_matrix_s *fz_matrix_s_concat(struct fz_matrix_s *self,struct fz_matrix_s *m1,struct fz_matrix_s *m2){ - fz_concat(self, m1, m2); - return self; + return wlist; } -SWIGINTERN char *fz_outline_s_uri(struct fz_outline_s *self){ - return self->uri; - } -SWIGINTERN int fz_outline_s_isExternal(struct fz_outline_s *self){ - if (!self->uri) return 0; - return fz_is_external_link(gctx, self->uri); - } -SWIGINTERN struct fz_point_s *new_fz_point_s__SWIG_1(struct fz_point_s const *q){ - fz_point *p = JM_Alloc(fz_point, 1); - if (!q) +SWIGINTERN PyObject *fz_document_s__getPageObjNumber(struct fz_document_s *self,int pno){ + int pageCount = fz_count_pages(gctx, self); + pdf_document *pdf = pdf_specifics(gctx, self); + fz_try(gctx) { - p->x = p->y = 0; + if (pno >= pageCount) THROWMSG("invalid page number(s)"); + assert_PDF(pdf); } - else *p = *q; - return p; - } -SWIGINTERN struct fz_point_s *new_fz_point_s__SWIG_2(float x,float y){ - fz_point *p = JM_Alloc(fz_point, 1); - p->x = x; - p->y = y; - return p; + fz_catch(gctx) return NULL; + int n = pno; + while (n < 0) n += pageCount; + pdf_obj *pageref = pdf_lookup_page_obj(gctx, pdf, n); + long objnum = (long) pdf_to_num(gctx, pageref); + long objgen = (long) pdf_to_gen(gctx, pageref); + return Py_BuildValue("(l, l)", objnum, objgen); } -SWIGINTERN struct fz_point_s *new_fz_point_s__SWIG_3(PyObject *list){ - fz_point *p = JM_Alloc(fz_point, 1); +SWIGINTERN PyObject *fz_document_s__getPageInfo(struct fz_document_s *self,int pno,int what){ + pdf_document *pdf = pdf_specifics(gctx, self); + int pageCount = fz_count_pages(gctx, self); + pdf_obj *pageref, *rsrc; + PyObject *liste = NULL; // returned object + int n = pno; // pno < 0 is allowed + while (n < 0) n += pageCount; + fz_var(liste); fz_try(gctx) { - if (!PySequence_Check(list)) THROWMSG("expected a sequence"); - if (PySequence_Size(list) != 2) THROWMSG("len(sequence) invalid"); - p->x = (float) PyFloat_AsDouble(PySequence_GetItem(list, 0)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); - p->y = (float) PyFloat_AsDouble(PySequence_GetItem(list, 1)); - if (PyErr_Occurred()) THROWMSG("invalid sequ. item"); + if (n >= pageCount) THROWMSG("invalid page number(s)"); + assert_PDF(pdf); + pageref = pdf_lookup_page_obj(gctx, pdf, n); + rsrc = pdf_dict_get(gctx, pageref, PDF_NAME(Resources)); + if (!pageref || !rsrc) THROWMSG("cannot retrieve page info"); + liste = PyList_New(0); + JM_scan_resources(gctx, pdf, rsrc, liste, what); } fz_catch(gctx) { - JM_Free(p); + Py_XDECREF(liste); return NULL; } - return p; - } -SWIGINTERN void delete_fz_point_s(struct fz_point_s *self){ - DEBUGMSG1("point"); - JM_Free(self); - DEBUGMSG2; - } -SWIGINTERN void delete_fz_annot_s(struct fz_annot_s *self){ - DEBUGMSG1("annot"); - fz_drop_annot(gctx, self); - DEBUGMSG2; - } -SWIGINTERN struct fz_rect_s *fz_annot_s_rect(struct fz_annot_s *self){ - fz_rect *r = JM_Alloc(fz_rect, 1); - return fz_bound_annot(gctx, self, r); - } -SWIGINTERN int fz_annot_s__getXref(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if(!annot) return 0; - return pdf_to_num(gctx, annot->obj); - } -SWIGINTERN PyObject *fz_annot_s__getAP(struct fz_annot_s *self){ - PyObject *r = NONE; - fz_buffer *res = NULL; - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; - fz_try(gctx) - { - pdf_obj *ap = pdf_dict_getl(gctx, annot->obj, PDF_NAME_AP, - PDF_NAME_N, NULL); - - if (pdf_is_stream(gctx, ap)) res = pdf_load_stream(gctx, ap); - if (res) r = JM_BinFromBuffer(gctx, res); - } - fz_always(gctx) fz_drop_buffer(gctx, res); - fz_catch(gctx) return NONE; - return r; - } -SWIGINTERN PyObject *fz_annot_s__setAP(struct fz_annot_s *self,PyObject *ap){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - fz_buffer *res = NULL; - fz_var(res); - fz_try(gctx) - { - assert_PDF(annot); - pdf_obj *apobj = pdf_dict_getl(gctx, annot->obj, PDF_NAME_AP, - PDF_NAME_N, NULL); - if (!apobj) THROWMSG("annot has no /AP/N object"); - if (!pdf_is_stream(gctx, apobj)) - THROWMSG("/AP/N object is no stream"); - char *c = NULL; - size_t len = JM_CharFromBytesOrArray(ap, &c); - if (!c) THROWMSG("invalid /AP stream argument"); - res = fz_new_buffer_from_copied_data(gctx, c, strlen(c)); - JM_update_stream(gctx, annot->page->doc, apobj, res); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, annot->page); - } - fz_always(gctx) - fz_drop_buffer(gctx, res); - fz_catch(gctx) return NULL; - return NONE; + return liste; } -SWIGINTERN PyObject *fz_annot_s__checkAP(struct fz_annot_s *self,struct fz_rect_s *rect,char *c){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - fz_try(gctx) - { - assert_PDF(annot); - JM_make_ap_object(gctx, self, rect, c); - pdf_dirty_annot(gctx, annot); - pdf_update_page(gctx, annot->page); - } +SWIGINTERN PyObject *fz_document_s_extractFont(struct fz_document_s *self,int xref,int info_only){ + pdf_document *pdf = pdf_specifics(gctx, self); + + fz_try(gctx) assert_PDF(pdf); fz_catch(gctx) return NULL; - return NONE; - } -SWIGINTERN void fz_annot_s__setRect(struct fz_annot_s *self,struct fz_rect_s *rect){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return; - fz_try(gctx) - { - pdf_set_annot_rect(gctx, annot, rect); - } - fz_catch(gctx) {;} - return; - } -SWIGINTERN PyObject *fz_annot_s_vertices(struct fz_annot_s *self){ - PyObject *res, *list; - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; // not a PDF! - - //---------------------------------------------------------------- - // The following objects occur in different annotation types. - // So we are sure that o != NULL occurs at most once. - // Every pair of floats is one point, that needs to be separately - // transformed with the page's transformation matrix. - //---------------------------------------------------------------- - pdf_obj *o = pdf_dict_get(gctx, annot->obj, PDF_NAME_Vertices); - if (!o) o = pdf_dict_get(gctx, annot->obj, PDF_NAME_L); - if (!o) o = pdf_dict_get(gctx, annot->obj, PDF_NAME_QuadPoints); - if (!o) o = pdf_dict_gets(gctx, annot->obj, "CL"); - int i, j, n; - fz_point point; // point object to work with - fz_matrix page_ctm; // page transformation matrix - pdf_page_transform(gctx, annot->page, NULL, &page_ctm); - if (o) // anything found yet? - { - res = PyList_New(0); // create Python list - n = pdf_array_len(gctx, o); - for (i = 0; i < n; i += 2) - { - point.x = pdf_to_real(gctx, pdf_array_get(gctx, o, i)); - point.y = pdf_to_real(gctx, pdf_array_get(gctx, o, i+1)); - fz_transform_point(&point, &page_ctm); - PyList_Append(res, Py_BuildValue("ff", point.x, point.y)); - } - return res; - } - // nothing found so far - maybe an Ink annotation? - pdf_obj *il_o = pdf_dict_get(gctx, annot->obj, PDF_NAME_InkList); - if (!il_o) return NONE; // no inkList - res = PyList_New(0); // create result list - n = pdf_array_len(gctx, il_o); - for (i = 0; i < n; i++) - { - list = PyList_New(0); - o = pdf_array_get(gctx, il_o, i); - int m = pdf_array_len(gctx, o); - for (j = 0; j < m; j += 2) - { - point.x = pdf_to_real(gctx, pdf_array_get(gctx, o, j)); - point.y = pdf_to_real(gctx, pdf_array_get(gctx, o, j+1)); - fz_transform_point(&point, &page_ctm); - PyList_Append(list, Py_BuildValue("ff", point.x, point.y)); - } - PyList_Append(res, list); - Py_CLEAR(list); - } - return res; - } -SWIGINTERN PyObject *fz_annot_s__setVertices(struct fz_annot_s *self,PyObject *vertices){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; - int type = pdf_annot_type(gctx, annot); - if (!INRANGE(type, 3, 7)) return NONE; - if (INRANGE(type, 4, 5)) return NONE; // only handling 3 types + fz_buffer *buffer = NULL; + pdf_obj *obj, *basefont, *bname; + PyObject *bytes = PyBytes_FromString(""); + char *ext = NULL; + char *fontname = NULL; + PyObject *nulltuple = Py_BuildValue("sssO", "", "", "", bytes); + PyObject *tuple; + Py_ssize_t len = 0; fz_try(gctx) { - fz_point a, b; - PyObject *p = NULL; - if (type == PDF_ANNOT_LINE) - { - p = PySequence_ITEM(vertices, 0); - a.x = (float) PyFloat_AsDouble(PySequence_ITEM(p, 0)); - a.y = (float) PyFloat_AsDouble(PySequence_ITEM(p, 1)); - Py_CLEAR(p); - p = PySequence_ITEM(vertices, 1); - b.x = (float) PyFloat_AsDouble(PySequence_ITEM(p, 0)); - b.y = (float) PyFloat_AsDouble(PySequence_ITEM(p, 1)); - Py_CLEAR(p); - pdf_set_annot_line(gctx, annot, a, b); - } - else + obj = pdf_load_object(gctx, pdf, xref); + pdf_obj *type = pdf_dict_get(gctx, obj, PDF_NAME(Type)); + pdf_obj *subtype = pdf_dict_get(gctx, obj, PDF_NAME(Subtype)); + if(pdf_name_eq(gctx, type, PDF_NAME(Font)) && + strncmp(pdf_to_name(gctx, subtype), "CIDFontType", 11) != 0) { - int i, n = PySequence_Size(vertices); - for (i = 0; i < n; i++) + basefont = pdf_dict_get(gctx, obj, PDF_NAME(BaseFont)); + if (!basefont || pdf_is_null(gctx, basefont)) + bname = pdf_dict_get(gctx, obj, PDF_NAME(Name)); + else + bname = basefont; + ext = fontextension(gctx, pdf, xref); + if (strcmp(ext, "n/a") != 0 && !info_only) { - p = PySequence_ITEM(vertices, i); - a.x = (float) PyFloat_AsDouble(PySequence_ITEM(p, 0)); - a.y = (float) PyFloat_AsDouble(PySequence_ITEM(p, 1)); - Py_CLEAR(p); - pdf_set_annot_vertex(gctx, annot, i, a); + buffer = fontbuffer(gctx, pdf, xref); + bytes = JM_BinFromBuffer(gctx, buffer); + fz_drop_buffer(gctx, buffer); } + fontname = (char *) JM_ASCIIFromChar((char *) pdf_to_name(gctx, bname)); + tuple = Py_BuildValue("sssO", + fontname, + ext, + pdf_to_name(gctx, subtype), + bytes); } - - } - fz_catch(gctx) return NULL; - return NONE; - } -SWIGINTERN PyObject *fz_annot_s_colors(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; - - PyObject *res = PyDict_New(); - PyObject *bc = PyList_New(0); // stroke colors - PyObject *fc = PyList_New(0); // fill colors - - int i; - float col; - pdf_obj *o = pdf_dict_get(gctx, annot->obj, PDF_NAME_C); - if (pdf_is_array(gctx, o)) - { - int n = pdf_array_len(gctx, o); - for (i = 0; i < n; i++) + else { - col = pdf_to_real(gctx, pdf_array_get(gctx, o, i)); - PyList_Append(bc, Py_BuildValue("f", col)); + tuple = nulltuple; } } - PyDict_SetItemString(res, "stroke", bc); - - o = pdf_dict_gets(gctx, annot->obj, "IC"); - if (pdf_is_array(gctx, o)) + fz_always(gctx) { - int n = pdf_array_len(gctx, o); - for (i = 0; i < n; i++) - { - col = pdf_to_real(gctx, pdf_array_get(gctx, o, i)); - PyList_Append(fc, Py_BuildValue("f", col)); - } + JM_PyErr_Clear; + JM_Free(fontname); } - PyDict_SetItemString(res, "fill", fc); - Py_CLEAR(bc); - Py_CLEAR(fc); - return res; - } -SWIGINTERN PyObject *fz_annot_s_updateAppearance(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; - fz_try(gctx) - { - pdf_update_appearance(gctx, annot); - } fz_catch(gctx) { - PySys_WriteStderr("cannot update annot: '%s'\n", fz_caught_message(gctx)); - Py_RETURN_FALSE; + tuple = Py_BuildValue("sssO", "invalid-name", "", "", bytes); } - Py_RETURN_TRUE; + return tuple; } -SWIGINTERN void fz_annot_s_setColors(struct fz_annot_s *self,PyObject *colors){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return; - if (!PyDict_Check(colors)) return; - if (pdf_annot_type(gctx, annot) == PDF_ANNOT_WIDGET) - { - PySys_WriteStdout("use 'updateWidget' to change form fields\n"); - return; - } - PyObject *ccol, *icol; - ccol = PyDict_GetItemString(colors, "stroke"); - icol = PyDict_GetItemString(colors, "fill"); - int i, n; - float col[4]; - n = 0; - if (ccol) - if (PySequence_Check(ccol)) - n = (int) PySequence_Size(ccol); - if (n>0) +SWIGINTERN PyObject *fz_document_s_extractImage(struct fz_document_s *self,int xref){ + pdf_document *pdf = pdf_specifics(gctx, self); + fz_try(gctx) { - for (i=0; i0) + fz_catch(gctx) return NULL; + + fz_buffer *buffer = NULL, *freebuf = NULL; + fz_var(freebuf); + fz_pixmap *pix = NULL; + fz_var(pix); + pdf_obj *obj = NULL; + PyObject *rc = NULL; + unsigned char ext[5]; + fz_image *image = NULL; + fz_var(image); + fz_output *out = NULL; + fz_var(out); + fz_compressed_buffer *cbuf = NULL; + int type = 0, n = 0, xres = 0, yres = 0; + int smask = 0, width = 0, height = 0; + fz_try(gctx) { - if (!pdf_annot_has_interior_color(gctx, annot)) + obj = pdf_new_indirect(gctx, pdf, xref, 0); + pdf_obj *subtype = pdf_dict_get(gctx, obj, PDF_NAME(Subtype)); + if (pdf_name_eq(gctx, subtype, PDF_NAME(Image))) { - PySys_WriteStdout("annot type has no fill color\n"); - return; - } - for (i=0; iobj, "LE"); - if (!o) return res; // no LE: empty dict - char *lstart = NULL; - char *lend = NULL; - if (pdf_is_name(gctx, o)) lstart = (char *) pdf_to_name(gctx, o); - else if (pdf_is_array(gctx, o)) - { - lstart = (char *) pdf_to_name(gctx, pdf_array_get(gctx, o, 0)); - if (pdf_array_len(gctx, o) > 1) - lend = (char *) pdf_to_name(gctx, pdf_array_get(gctx, o, 1)); - } - PyList_SetItem(res, 0, Py_BuildValue("i", JM_le_value(gctx, lstart))); - PyList_SetItem(res, 1, Py_BuildValue("i", JM_le_value(gctx, lend))); - return res; - } -SWIGINTERN void fz_annot_s_setLineEnds(struct fz_annot_s *self,int start,int end){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return; - if (pdf_annot_has_line_ending_styles(gctx, annot)) - pdf_set_annot_line_ending_styles(gctx, annot, start, end); - else - PySys_WriteStdout("annot type has no line ends\n"); - } -SWIGINTERN PyObject *fz_annot_s_type(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; // not a PDF - int type = pdf_annot_type(gctx, annot); - const char *c = pdf_string_from_annot_type(gctx, type); - pdf_obj *o = pdf_dict_gets(gctx, annot->obj, "IT"); - if (!o || !pdf_is_name(gctx, o)) - return Py_BuildValue("is", type, c); // no IT entry - const char *it = pdf_to_name(gctx, o); - return Py_BuildValue("iss", type, c, it); - } -SWIGINTERN float fz_annot_s_opacity(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return -1.0f; // not a PDF - pdf_obj *ca = pdf_dict_get(gctx, annot->obj, PDF_NAME_CA); - if (pdf_is_number(gctx, ca)) - return pdf_to_real(gctx, ca); - return -1.0f; - } -SWIGINTERN void fz_annot_s_setOpacity(struct fz_annot_s *self,float opacity){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return; // not a PDF - if (INRANGE(opacity, 0.0f, 1.0f)) - pdf_set_annot_opacity(gctx, annot, opacity); - else - pdf_set_annot_opacity(gctx, annot, 1.0f); - } -SWIGINTERN PyObject *fz_annot_s_widget_type(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - int wtype; - if (!annot) return NONE; // not a PDF + image = pdf_load_image(gctx, pdf, obj); + cbuf = fz_compressed_image_buffer(gctx, image); + type = cbuf == NULL ? FZ_IMAGE_UNKNOWN : cbuf->params.type; + // ensure returning a PNG for unsupported images ---------- + if (type < FZ_IMAGE_BMP) type = FZ_IMAGE_UNKNOWN; - wtype = pdf_field_type(gctx, pdf_get_bound_document(gctx, annot->obj), annot->obj); - switch(wtype) - { - case(PDF_WIDGET_TYPE_PUSHBUTTON): - return Py_BuildValue("is", wtype, "PushButton"); - case(PDF_WIDGET_TYPE_CHECKBOX): - return Py_BuildValue("is", wtype, "CheckBox"); - case(PDF_WIDGET_TYPE_RADIOBUTTON): - return Py_BuildValue("is", wtype, "RadioButton"); - case(PDF_WIDGET_TYPE_TEXT): - return Py_BuildValue("is", wtype, "Text"); - case(PDF_WIDGET_TYPE_LISTBOX): - return Py_BuildValue("is", wtype, "ListBox"); - case(PDF_WIDGET_TYPE_COMBOBOX): - return Py_BuildValue("is", wtype, "ComboBox"); - case(PDF_WIDGET_TYPE_SIGNATURE): - return Py_BuildValue("is", wtype, "Signature"); - default: - return NONE; + pdf_obj *o = pdf_dict_get(gctx, obj, PDF_NAME(SMask)); + if (o) smask = pdf_to_num(gctx, o); + + o = pdf_dict_get(gctx, obj, PDF_NAME(Width)); + if (o) width = pdf_to_int(gctx, o); + + o = pdf_dict_get(gctx, obj, PDF_NAME(Height)); + if (o) height = pdf_to_int(gctx, o); + + n = fz_colorspace_n(gctx, image->colorspace); + fz_image_resolution(image, &xres, &yres); + if (type != FZ_IMAGE_UNKNOWN) + { + buffer = cbuf->buffer; // we will return this buffer + switch(type) + { + case(FZ_IMAGE_BMP): strcpy(ext, "bmp"); break; + case(FZ_IMAGE_GIF): strcpy(ext, "gif"); break; + case(FZ_IMAGE_JPEG): strcpy(ext, "jpeg"); break; + case(FZ_IMAGE_JPX): strcpy(ext, "jpx"); break; + case(FZ_IMAGE_JXR): strcpy(ext, "jxr"); break; + case(FZ_IMAGE_PNM): strcpy(ext, "pnm"); break; + case(FZ_IMAGE_TIFF): strcpy(ext, "tiff"); break; + default: strcpy(ext, "png"); break; + } + } + else // we need a pixmap for making the PNG buffer + { + pix = fz_get_pixmap_from_image(gctx, image, + NULL, NULL, NULL, NULL); + n = pix->n; + // only gray & rgb pixmaps support PNG! + if (pix->colorspace && + pix->colorspace != fz_device_gray(gctx) && + pix->colorspace != fz_device_rgb(gctx)) + { + fz_pixmap *pix2 = fz_convert_pixmap(gctx, pix, + fz_device_rgb(gctx), NULL, NULL, NULL, 1); + fz_drop_pixmap(gctx, pix); + pix = pix2; + } + + freebuf = fz_new_buffer(gctx, 2048); + out = fz_new_output_with_buffer(gctx, freebuf); + fz_write_pixmap_as_png(gctx, out, pix); + buffer = freebuf; + strcpy(ext, "png"); + } + PyObject *bytes = JM_BinFromBuffer(gctx, buffer); + rc = Py_BuildValue("{s:s,s:i,s:i,s:i,s:i,s:i,s:i,s:O}", + "ext", ext, + "smask", smask, + "width", width, + "height", height, + "colorspace", n, + "xres", xres, + "yres", yres, + "image", bytes); + Py_CLEAR(bytes); + } + else + rc = PyDict_New(); } - } -SWIGINTERN PyObject *fz_annot_s_widget_value(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; // not a PDF - if (pdf_annot_type(gctx, annot) != PDF_ANNOT_WIDGET) - return NONE; - int wtype = pdf_field_type(gctx, pdf_get_bound_document(gctx, annot->obj), annot->obj); - switch(wtype) + fz_always(gctx) { - case(PDF_WIDGET_TYPE_PUSHBUTTON): - return JM_pushbtn_state(gctx, annot); - case(PDF_WIDGET_TYPE_CHECKBOX): - return JM_checkbox_state(gctx, annot); - case(PDF_WIDGET_TYPE_RADIOBUTTON): - return JM_radiobtn_state(gctx, annot); - case(PDF_WIDGET_TYPE_TEXT): - return JM_text_value(gctx, annot); - case(PDF_WIDGET_TYPE_LISTBOX): - return JM_listbox_value(gctx, annot); - case(PDF_WIDGET_TYPE_COMBOBOX): - return JM_combobox_value(gctx, annot); - case(PDF_WIDGET_TYPE_SIGNATURE): - return JM_signature_value(gctx, annot); - default: - return NONE; + fz_drop_image(gctx, image); + fz_drop_buffer(gctx, freebuf); + fz_drop_output(gctx, out); + fz_drop_pixmap(gctx, pix); + pdf_drop_obj(gctx, obj); } + fz_catch(gctx) {;} + return rc; } -SWIGINTERN PyObject *fz_annot_s_widget_name(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; - if (pdf_annot_type(gctx, annot) != PDF_ANNOT_WIDGET) - return NONE; - return PyString_FromString(pdf_field_name(gctx, - pdf_get_bound_document(gctx, annot->obj), - annot->obj)); - } -SWIGINTERN PyObject *fz_annot_s_widget_choices(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; - if (pdf_annot_type(gctx, annot) != PDF_ANNOT_WIDGET) - return NONE; - return JM_choice_options(gctx, annot); - } -SWIGINTERN PyObject *fz_annot_s_fileInfo(struct fz_annot_s *self){ - PyObject *res = PyDict_New(); // create Python dict - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - char *filename = NULL; - char *desc = NULL; - int length = -1, size = -1; - pdf_obj *stream = NULL, *o = NULL, *fs = NULL; +SWIGINTERN PyObject *fz_document_s__delToC(struct fz_document_s *self){ + PyObject *xrefs = PyList_New(0); // create Python list - fz_try(gctx) - { - assert_PDF(annot); - int type = (int) pdf_annot_type(gctx, annot); - if (type != 16) - THROWMSG("not a file attachment annot"); - stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME_FS, - PDF_NAME_EF, PDF_NAME_F, NULL); - if (!stream) THROWMSG("bad PDF: file entry not found"); - } - fz_catch(gctx) return NULL; + pdf_document *pdf = pdf_specifics(gctx, self); + if (!pdf) return xrefs; // not a pdf - fs = pdf_dict_get(gctx, annot->obj, PDF_NAME_FS); + pdf_obj *root, *olroot, *first; + int xref_count, olroot_xref, i, xref; - o = pdf_dict_get(gctx, fs, PDF_NAME_UF); - if (o) filename = pdf_to_utf8(gctx, o); - else - { - o = pdf_dict_get(gctx, fs, PDF_NAME_F); - if (o) filename = pdf_to_utf8(gctx, o); - } + // get the main root + root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root)); + // get the outline root + olroot = pdf_dict_get(gctx, root, PDF_NAME(Outlines)); + if (!olroot) return xrefs; // no outlines or some problem - o = pdf_dict_get(gctx, fs, PDF_NAME_Desc); - if (o) desc = pdf_to_utf8(gctx, o); + first = pdf_dict_get(gctx, olroot, PDF_NAME(First)); // first outline - o = pdf_dict_get(gctx, stream, PDF_NAME_Length); - if (o) length = pdf_to_int(gctx, o); + xrefs = JM_outline_xrefs(gctx, first, xrefs); + xref_count = (int) PyList_Size(xrefs); - o = pdf_dict_getl(gctx, stream, PDF_NAME_Params, - PDF_NAME_Size, NULL); - if (o) size = pdf_to_int(gctx, o); + olroot_xref = pdf_to_num(gctx, olroot); // delete OL root + pdf_delete_object(gctx, pdf, olroot_xref); // delete OL root + pdf_dict_del(gctx, root, PDF_NAME(Outlines)); // delete OL root - PyDict_SetItemString(res, "filename", JM_UNICODE(filename)); - PyDict_SetItemString(res, "desc", JM_UNICODE(desc)); - PyDict_SetItemString(res, "length", Py_BuildValue("i", length)); - PyDict_SetItemString(res, "size", Py_BuildValue("i", size)); - return res; - } -SWIGINTERN PyObject *fz_annot_s_fileGet(struct fz_annot_s *self){ - PyObject *res = NULL; - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - pdf_obj *stream = NULL; - fz_buffer *buf = NULL; - fz_var(buf); - fz_try(gctx) + for (i = 0; i < xref_count; i++) { - assert_PDF(annot); - int type = (int) pdf_annot_type(gctx, annot); - if (type != 16) - THROWMSG("not a file attachment annot"); - stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME_FS, - PDF_NAME_EF, PDF_NAME_F, NULL); - if (!stream) THROWMSG("bad PDF: file entry not found"); - buf = pdf_load_stream(gctx, stream); - res = JM_BinFromBuffer(gctx, buf); + xref = (int) PyInt_AsLong(PyList_GetItem(xrefs, i)); + pdf_delete_object(gctx, pdf, xref); // delete outline item } - fz_always(gctx) fz_drop_buffer(gctx, buf); - fz_catch(gctx) return NULL; - return res; + PyList_Append(xrefs, Py_BuildValue("i", olroot_xref)); + pdf->dirty = 1; + return xrefs; } -SWIGINTERN PyObject *fz_annot_s_fileUpd(struct fz_annot_s *self,PyObject *buffer,char *filename,char *ufilename,char *desc){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - pdf_document *pdf = NULL; // to be filled in - char *data = NULL; // for new file content - fz_buffer *res = NULL; // for compressed content - pdf_obj *stream = NULL, *fs = NULL; - int64_t size = 0; +SWIGINTERN PyObject *fz_document_s_isFormPDF(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); + if (!pdf) Py_RETURN_FALSE; // not a PDF + pdf_obj *form = NULL; + pdf_obj *fields = NULL; + int have_form = 0; // preset indicator fz_try(gctx) { - assert_PDF(annot); // must be a PDF - pdf = annot->page->doc; // this is the PDF - int type = (int) pdf_annot_type(gctx, annot); - if (type != 16) - THROWMSG("not a file attachment annot"); - stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME_FS, - PDF_NAME_EF, PDF_NAME_F, NULL); - // the object for file content - if (!stream) THROWMSG("bad PDF: file entry not found"); - - fs = pdf_dict_get(gctx, annot->obj, PDF_NAME_FS); - - // file content is ignored if not bytes / bytearray - size = (int64_t) JM_CharFromBytesOrArray(buffer, &data); - if (size > 0) - { - pdf_obj *s = pdf_new_int(gctx, NULL, size); - pdf_dict_put(gctx, stream, PDF_NAME_Filter, - PDF_NAME_FlateDecode); - - pdf_dict_putl_drop(gctx, stream, s, - PDF_NAME_Params, PDF_NAME_Size, NULL); - res = JM_deflatebuf(gctx, data, size); - pdf_update_stream(gctx, pdf, stream, res, 1); - } - - if (filename) // new filename given - { - pdf_dict_put_text_string(gctx, stream, PDF_NAME_F, filename); - pdf_dict_put_text_string(gctx, fs, PDF_NAME_F, filename); - } - - if (ufilename) - { - pdf_dict_put_text_string(gctx, stream, PDF_NAME_UF, filename); - pdf_dict_put_text_string(gctx, fs, PDF_NAME_UF, filename); - } - - if (desc) // new description given + form = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root), PDF_NAME(AcroForm), NULL); + if (form) // form obj exists { - pdf_dict_put_text_string(gctx, stream, PDF_NAME_Desc, desc); - pdf_dict_put_text_string(gctx, fs, PDF_NAME_Desc, desc); + fields = pdf_dict_get(gctx, form, PDF_NAME(Fields)); + if (fields && pdf_array_len(gctx, fields) > 0) have_form = 1; } } - fz_always(gctx) - { - fz_drop_buffer(gctx, res); - } - fz_catch(gctx) return NULL; - pdf->dirty = 1; - return NONE; - } -SWIGINTERN PyObject *fz_annot_s_info(struct fz_annot_s *self){ - PyObject *res = PyDict_New(); - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return res; // not a PDF - pdf_obj *o; - char *c; - c = pdf_copy_annot_contents(gctx, annot); - PyDict_SetItemString(res, "content", JM_UNICODE(c)); - - o = pdf_dict_get(gctx, annot->obj, PDF_NAME_Name); - c = (char *) pdf_to_name(gctx, o); - PyDict_SetItemString(res, "name", JM_UNICODE(c)); - - // Title, author - o = pdf_dict_get(gctx, annot->obj, PDF_NAME_T); - c = pdf_to_utf8(gctx, o); - PyDict_SetItemString(res, "title", JM_UNICODE(c)); - - // CreationDate - o = pdf_dict_gets(gctx, annot->obj, "CreationDate"); - c = pdf_to_utf8(gctx, o); - PyDict_SetItemString(res, "creationDate", JM_UNICODE(c)); - - // ModDate - o = pdf_dict_get(gctx, annot->obj, PDF_NAME_M); - c = pdf_to_utf8(gctx, o); - PyDict_SetItemString(res, "modDate", JM_UNICODE(c)); - - // Subj - o = pdf_dict_gets(gctx, annot->obj, "Subj"); - c = pdf_to_utf8(gctx, o); - PyDict_SetItemString(res, "subject", JM_UNICODE(c)); - - return res; + fz_catch(gctx) Py_RETURN_FALSE; // any problem yields false + if (!have_form) Py_RETURN_FALSE; // no form / no fields + Py_RETURN_TRUE; } -SWIGINTERN PyObject *fz_annot_s_setInfo(struct fz_annot_s *self,PyObject *info){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - char *uc = NULL; - - // use this to indicate a 'markup' annot type - int is_markup = pdf_annot_has_author(gctx, annot); - fz_var(is_markup); - fz_var(annot); +SWIGINTERN PyObject *fz_document_s_FormFonts(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); + if (!pdf) return NONE; // not a PDF + pdf_obj *fonts = NULL; + PyObject *liste = PyList_New(0); fz_try(gctx) { - assert_PDF(annot); - if (!PyDict_Check(info)) - THROWMSG("info not a dict"); - - // contents - uc = JM_Python_str_AsChar(PyDict_GetItemString(info, "content")); - if (uc) - { - pdf_set_annot_contents(gctx, annot, uc); - JM_Python_str_DelForPy3(uc); - } - - if (is_markup) + fonts = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(DR), PDF_NAME(Font), NULL); + if (fonts && pdf_is_dict(gctx, fonts)) // fonts exist { - // title (= author) - uc = JM_Python_str_AsChar(PyDict_GetItemString(info, "title")); - if (uc) - { - pdf_set_annot_author(gctx, annot, uc); - JM_Python_str_DelForPy3(uc); - } - - // creation date - uc = JM_Python_str_AsChar(PyDict_GetItemString(info, - "creationDate")); - if (uc) - { - pdf_dict_put_text_string(gctx, annot->obj, - PDF_NAME_CreationDate, uc); - JM_Python_str_DelForPy3(uc); - } - - // mod date - uc = JM_Python_str_AsChar(PyDict_GetItemString(info, "modDate")); - if (uc) - { - pdf_dict_put_text_string(gctx, annot->obj, - PDF_NAME_M, uc); - JM_Python_str_DelForPy3(uc); - } - - // subject - uc = JM_Python_str_AsChar(PyDict_GetItemString(info, "subject")); - if (uc) + int i, n = pdf_dict_len(gctx, fonts); + for (i = 0; i < n; i++) { - pdf_dict_puts_drop(gctx, annot->obj, "Subj", - pdf_new_text_string(gctx, NULL, uc)); - JM_Python_str_DelForPy3(uc); + pdf_obj *f = pdf_dict_get_key(gctx, fonts, i); + PyList_Append(liste, Py_BuildValue("s", pdf_to_name(gctx, f))); } } } - fz_catch(gctx) return NULL; - return NONE; + fz_catch(gctx) NONE; // any problem yields None + return liste; } -SWIGINTERN PyObject *fz_annot_s_border(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; // not a PDF - PyObject *res = PyDict_New(); - PyObject *dash_py = PyList_New(0); - PyObject *effect_py = PyList_New(0); - int i; - char *effect2 = NULL, *style = NULL; - float width = 0; - int effect1 = -1; - - pdf_obj *o = pdf_dict_get(gctx, annot->obj, PDF_NAME_Border); - if (pdf_is_array(gctx, o)) - { - width = pdf_to_real(gctx, pdf_array_get(gctx, o, 2)); - if (pdf_array_len(gctx, o) == 4) - { - pdf_obj *dash = pdf_array_get(gctx, o, 3); - for (i = 0; i < pdf_array_len(gctx, dash); i++) - PyList_Append(dash_py, Py_BuildValue("i", - pdf_to_int(gctx, pdf_array_get(gctx, dash, i)))); - } - } - - pdf_obj *bs_o = pdf_dict_get(gctx, annot->obj, PDF_NAME_BS); - if (bs_o) - { - o = pdf_dict_get(gctx, bs_o, PDF_NAME_W); - if (o) width = pdf_to_real(gctx, o); - o = pdf_dict_get(gctx, bs_o, PDF_NAME_S); - if (o) - { - style = (char *) pdf_to_name(gctx, o); - } - o = pdf_dict_get(gctx, bs_o, PDF_NAME_D); - if (o) - { - for (i = 0; i < pdf_array_len(gctx, o); i++) - PyList_Append(dash_py, Py_BuildValue("i", - pdf_to_int(gctx, pdf_array_get(gctx, o, i)))); - } - } - - pdf_obj *be_o = pdf_dict_gets(gctx, annot->obj, "BE"); - if (be_o) +SWIGINTERN PyObject *fz_document_s__addFormFont(struct fz_document_s *self,char *name,char *font){ + pdf_document *pdf = pdf_specifics(gctx, self); + if (!pdf) NONE; // not a PDF + pdf_obj *fonts = NULL; + fz_try(gctx) { - o = pdf_dict_get(gctx, be_o, PDF_NAME_S); - if (o) effect2 = (char *) pdf_to_name(gctx, o); - o = pdf_dict_get(gctx, be_o, PDF_NAME_I); - if (o) effect1 = pdf_to_int(gctx, o); + fonts = pdf_dict_getl(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root), + PDF_NAME(AcroForm), PDF_NAME(DR), PDF_NAME(Font), NULL); + if (!fonts || !pdf_is_dict(gctx, fonts)) + THROWMSG("PDF has no form fonts yet"); + pdf_obj *k = pdf_new_name(gctx, (const char *) name); + pdf_obj *v = JM_pdf_obj_from_str(gctx, pdf, font); + pdf_dict_put(gctx, fonts, k, v); } - - PyList_Append(effect_py, Py_BuildValue("i", effect1)); - PyList_Append(effect_py, Py_BuildValue("s", effect2)); - - PyDict_SetItemString(res, "width", Py_BuildValue("f", width)); - - PyDict_SetItemString(res, "dashes", dash_py); - - PyDict_SetItemString(res, "style", Py_BuildValue("s", style)); - - if (effect1 > -1) PyDict_SetItemString(res, "effect", effect_py); - Py_CLEAR(effect_py); - Py_CLEAR(dash_py); - return res; + fz_catch(gctx) NULL; + return NONE; } -SWIGINTERN PyObject *fz_annot_s_setBorder(struct fz_annot_s *self,PyObject *border){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (!annot) return NONE; // not a PDF - if (!PyDict_Check(border)) - { - PySys_WriteStdout("arg must be a dict"); - return NONE; // not a dict - } - - pdf_document *doc = annot->page->doc; // PDF document - float nwidth = -1; // new width - PyObject *ndashes = NULL; // new dashes - PyObject *nstyle = NULL; // new style - float owidth = -1; // old width - PyObject *odashes = NULL; // old dashes - PyObject *ostyle = NULL; // old style - - nwidth = (float) PyFloat_AsDouble(PyDict_GetItemString(border, "width")); - PyErr_Clear(); - ndashes = PyDict_GetItemString(border, "dashes"); - nstyle = PyDict_GetItemString(border, "style"); - - // first get old border properties - PyObject *oborder = fz_annot_s_border(self); - owidth = (float) PyFloat_AsDouble(PyDict_GetItemString(oborder, "width")); - PyErr_Clear(); - odashes = PyDict_GetItemString(oborder, "dashes"); - ostyle = PyDict_GetItemString(oborder, "style"); - - // then delete current annot entries - pdf_dict_del(gctx, annot->obj, PDF_NAME_BS); - pdf_dict_del(gctx, annot->obj, PDF_NAME_BE); - pdf_dict_del(gctx, annot->obj, PDF_NAME_Border); - - Py_ssize_t i, n; - int d; - // populate new border array - if (nwidth < 0) nwidth = owidth; // no new width: take current - if (nwidth < 0) nwidth = 0; // default if no width given +SWIGINTERN int fz_document_s__getOLRootNumber(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); + fz_try(gctx) assert_PDF(pdf); + fz_catch(gctx) return -1; - if (!ndashes) ndashes = odashes; // no new dashes: take old - - if (!nstyle) nstyle = ostyle; // no new style: take old - - if (ndashes && PySequence_Check(ndashes) && PySequence_Size(ndashes)) + pdf_obj *root, *olroot, *ind_obj; + // get main root + root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root)); + // get outline root + olroot = pdf_dict_get(gctx, root, PDF_NAME(Outlines)); + if (olroot == NULL) { - n = PySequence_Size(ndashes); - pdf_obj *darr = pdf_new_array(gctx, doc, n); - for (i = 0; i < n; i++) - { - d = (int) PyInt_AsLong(PySequence_GetItem(ndashes, i)); - pdf_array_push_int(gctx, darr, (int64_t) d); - } - pdf_dict_putl_drop(gctx, annot->obj, darr, PDF_NAME_BS, PDF_NAME_D, NULL); + olroot = pdf_new_dict(gctx, pdf, 4); + pdf_dict_put(gctx, olroot, PDF_NAME(Type), PDF_NAME(Outlines)); + ind_obj = pdf_add_object(gctx, pdf, olroot); + pdf_dict_put(gctx, root, PDF_NAME(Outlines), ind_obj); + olroot = pdf_dict_get(gctx, root, PDF_NAME(Outlines)); + pdf_drop_obj(gctx, ind_obj); + pdf->dirty = 1; } - - pdf_dict_putl_drop(gctx, annot->obj, pdf_new_real(gctx, doc, nwidth), - PDF_NAME_BS, PDF_NAME_W, NULL); - - pdf_obj *val = JM_get_border_style(gctx, nstyle); - - pdf_dict_putl_drop(gctx, annot->obj, val, - PDF_NAME_BS, PDF_NAME_S, NULL); - - pdf_dirty_annot(gctx, annot); - PyErr_Clear(); - return NONE; + return pdf_to_num(gctx, olroot); } -SWIGINTERN int fz_annot_s_flags(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (annot) return pdf_annot_flags(gctx, annot); - return -1; +SWIGINTERN int fz_document_s__getNewXref(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); /* conv doc to pdf*/ + fz_try(gctx) assert_PDF(pdf); + fz_catch(gctx) return -1; + pdf->dirty = 1; + return pdf_create_object(gctx, pdf); } -SWIGINTERN PyObject *fz_annot_s__cleanContents(struct fz_annot_s *self){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); +SWIGINTERN int fz_document_s__getXrefLength(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); + if (!pdf) return 0; + return pdf_xref_len(gctx, pdf); + } +SWIGINTERN int fz_document_s__getXmlMetadataXref(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); // get pdf document + pdf_obj *xml; + int xref = 0; fz_try(gctx) { - assert_PDF(annot); - pdf_clean_annot_contents(gctx, annot->page->doc, annot, - NULL, NULL, NULL, 1, 0); + assert_PDF(pdf); + pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root)); + if (!root) THROWMSG("could not load root object"); + xml = pdf_dict_gets(gctx, root, "Metadata"); + if (xml) xref = pdf_to_num(gctx, xml); } - fz_catch(gctx) return NULL; - pdf_dirty_annot(gctx, annot); - return NONE; + fz_catch(gctx) return -1; + return xref; } -SWIGINTERN void fz_annot_s_setFlags(struct fz_annot_s *self,int flags){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - if (annot) +SWIGINTERN PyObject *fz_document_s__delXmlMetadata(struct fz_document_s *self){ + pdf_document *pdf = pdf_specifics(gctx, self); // get pdf document + fz_try(gctx) { - pdf_set_annot_flags(gctx, annot, flags); - pdf_dirty_annot(gctx, annot); + assert_PDF(pdf); + pdf_obj *root = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Root)); + if (root) pdf_dict_dels(gctx, root, "Metadata"); } - } -SWIGINTERN struct fz_annot_s *fz_annot_s_next(struct fz_annot_s *self){ - fz_annot *annot = fz_next_annot(gctx, self); - if (annot) - fz_keep_annot(gctx, annot); - return annot; - } -SWIGINTERN struct fz_pixmap_s *fz_annot_s_getPixmap(struct fz_annot_s *self,struct fz_matrix_s *matrix,struct fz_colorspace_s *colorspace,int alpha){ - struct fz_matrix_s *ctm = (fz_matrix *) &fz_identity; - struct fz_colorspace_s *cs = fz_device_rgb(gctx); - fz_pixmap *pix = NULL; - if (matrix) ctm = matrix; - if (colorspace) cs = colorspace; - - fz_try(gctx) - pix = fz_new_pixmap_from_annot(gctx, self, ctm, cs, alpha); fz_catch(gctx) return NULL; - return pix; + pdf->dirty = 1; + return NONE; } -SWIGINTERN PyObject *fz_annot_s__updateWidget(struct fz_annot_s *self,PyObject *Widget){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - pdf_document *pdf = pdf_get_bound_document(gctx, annot->obj); +SWIGINTERN char const *fz_document_s__getObjectString(struct fz_document_s *self,int xref){ + pdf_document *pdf = pdf_specifics(gctx, self); // conv doc to pdf + pdf_obj *obj = NULL; + fz_buffer *res = NULL; + fz_output *out = NULL; fz_try(gctx) { - int field_type = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, - "field_type")); - JM_set_widget_properties(gctx, annot, Widget, field_type); + assert_PDF(pdf); + int xreflen = pdf_xref_len(gctx, pdf); + if (!INRANGE(xref, 1, xreflen-1)) + THROWMSG("xref out of range"); + res = fz_new_buffer(gctx, 1024); + out = fz_new_output_with_buffer(gctx, res); + obj = pdf_load_object(gctx, pdf, xref); + pdf_print_obj(gctx, out, pdf_resolve_indirect(gctx, obj), 1); } fz_always(gctx) { - JM_PyErr_Clear; + pdf_drop_obj(gctx, obj); + fz_drop_output(gctx, out); } - fz_catch(gctx) return NULL; - return NONE; + fz_catch(gctx) + { + fz_drop_buffer(gctx, res); + return NULL; + } + return fz_string_from_buffer(gctx, res); } -SWIGINTERN PyObject *fz_annot_s__getWidget(struct fz_annot_s *self,PyObject *Widget){ - pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); - pdf_document *pdf = annot->page->doc; - pdf_widget *tw = (pdf_widget *) annot; - Py_ssize_t i = 0, n = 0; +SWIGINTERN PyObject *fz_document_s__getXrefStream(struct fz_document_s *self,int xref){ + pdf_document *pdf = pdf_specifics(gctx, self); + PyObject *r = NONE; + pdf_obj *obj = NULL; + fz_var(obj); + fz_buffer *res = NULL; + fz_var(res); fz_try(gctx) { - char *border_style = pdf_field_border_style(gctx, pdf, annot->obj); - PyObject_SetAttrString(Widget, "border_style", - Py_BuildValue("s", border_style)); - - float border_width = pdf_to_real(gctx, pdf_dict_getl(gctx, annot->obj, - PDF_NAME_BS, PDF_NAME_W, NULL)); - if (border_width == 0.0f) border_width = 1.0f; - PyObject_SetAttrString(Widget, "border_width", - Py_BuildValue("f", border_width)); - - pdf_obj *dashes = pdf_dict_getl(gctx, annot->obj, - PDF_NAME_BS, PDF_NAME_D, NULL); - if (pdf_is_array(gctx, dashes)) - { - n = (Py_ssize_t) pdf_array_len(gctx, dashes); - PyObject *d = PyList_New(n); - for (i = 0; i < n; i++) - PyList_SetItem(d, i, Py_BuildValue("i", pdf_to_int(gctx, - pdf_array_get(gctx, dashes, (int) i)))); - - PyObject_SetAttrString(Widget, "border_dashes", d); - Py_CLEAR(d); - } - - int text_maxlen = pdf_to_int(gctx, pdf_get_inheritable(gctx, pdf, annot->obj, PDF_NAME_MaxLen)); - PyObject_SetAttrString(Widget, "text_maxlen", - Py_BuildValue("i", text_maxlen)); - - // entry ignored for new / updated widgets - int text_type = pdf_text_widget_content_type(gctx, pdf, tw); - PyObject_SetAttrString(Widget, "text_type", - Py_BuildValue("i", text_type)); - - pdf_obj *bgcol = pdf_dict_getl(gctx, annot->obj, - PDF_NAME_MK, PDF_NAME_BG, NULL); - if (pdf_is_array(gctx, bgcol)) - { - n = (Py_ssize_t) pdf_array_len(gctx, bgcol); - PyObject *col = PyList_New(n); - for (i = 0; i < n; i++) - PyList_SetItem(col, i, Py_BuildValue("f", - pdf_to_real(gctx, pdf_array_get(gctx, bgcol, (int) i)))); - - PyObject_SetAttrString(Widget, "fill_color", col); - Py_CLEAR(col); - } - - pdf_obj *bccol = pdf_dict_getl(gctx, annot->obj, PDF_NAME_MK, PDF_NAME_BC, NULL); - - if (pdf_is_array(gctx, bccol)) + assert_PDF(pdf); + int xreflen = pdf_xref_len(gctx, pdf); + if (!INRANGE(xref, 1, xreflen-1)) + THROWMSG("xref out of range"); + obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (pdf_is_stream(gctx, obj)) { - n = (Py_ssize_t) pdf_array_len(gctx, bccol); - PyObject *col = PyList_New(n); - for (i = 0; i < n; i++) - PyList_SetItem(col, i, Py_BuildValue("f", - pdf_to_real(gctx, pdf_array_get(gctx, bccol, (int) i)))); - - PyObject_SetAttrString(Widget, "border_color", col); - Py_CLEAR(col); + res = pdf_load_stream_number(gctx, pdf, xref); + r = JM_BinFromBuffer(gctx, res); } - - char *da = pdf_to_str_buf(gctx, pdf_get_inheritable(gctx, - pdf, annot->obj, PDF_NAME_DA)); - PyObject_SetAttrString(Widget, "_text_da", Py_BuildValue("s", da)); - - pdf_obj *ca = pdf_dict_getl(gctx, annot->obj, - PDF_NAME_MK, PDF_NAME_CA, NULL); - if (ca) - PyObject_SetAttrString(Widget, "button_caption", - JM_UNICODE(pdf_to_str_buf(gctx, ca))); - - int field_flags = pdf_get_field_flags(gctx, pdf, annot->obj); - PyObject_SetAttrString(Widget, "field_flags", - Py_BuildValue("i", field_flags)); - - // call Py method to reconstruct text color, font name, size - PyObject *call = PyObject_CallMethod(Widget, - "_parse_da", NULL); - Py_XDECREF(call); - } - fz_always(gctx) PyErr_Clear(); - fz_catch(gctx) return NULL; - return NONE; - } -SWIGINTERN void delete_fz_link_s(struct fz_link_s *self){ - DEBUGMSG1("link"); - fz_drop_link(gctx, self); - DEBUGMSG2; - } -SWIGINTERN char *fz_link_s_uri(struct fz_link_s *self){ - return self->uri; - } -SWIGINTERN int fz_link_s_isExternal(struct fz_link_s *self){ - if (!self->uri) return 0; - return fz_is_external_link(gctx, self->uri); - } -SWIGINTERN struct fz_rect_s *fz_link_s_rect(struct fz_link_s *self){ - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = self->rect.x0; - r->y0 = self->rect.y0; - r->x1 = self->rect.x1; - r->y1 = self->rect.y1; - return r; - } -SWIGINTERN struct fz_link_s *fz_link_s_next(struct fz_link_s *self){ - fz_keep_link(gctx, self->next); - return self->next; - } -SWIGINTERN void delete_fz_display_list_s(struct fz_display_list_s *self){ - DEBUGMSG1("display list"); - fz_drop_display_list(gctx, self); - DEBUGMSG2; - } -SWIGINTERN struct fz_display_list_s *new_fz_display_list_s(struct fz_rect_s *mediabox){ - struct fz_display_list_s *dl = NULL; - fz_try(gctx) - dl = fz_new_display_list(gctx, mediabox); - fz_catch(gctx) return NULL; - return dl; - } -SWIGINTERN int fz_display_list_s_run(struct fz_display_list_s *self,struct DeviceWrapper *dw,struct fz_matrix_s const *m,struct fz_rect_s const *area){ - fz_try(gctx) + fz_always(gctx) { - fz_run_display_list(gctx, self, dw->device, m, area, NULL); + fz_drop_buffer(gctx, res); + pdf_drop_obj(gctx, obj); } - fz_catch(gctx) return 1; - return 0; - } -SWIGINTERN struct fz_rect_s *fz_display_list_s_rect(struct fz_display_list_s *self){ - fz_rect *mediabox = JM_Alloc(fz_rect, 1); - fz_bound_display_list(gctx, self, mediabox); - return mediabox; + fz_catch(gctx) + { + Py_CLEAR(r); + return NULL; + } + return r; } -SWIGINTERN struct fz_pixmap_s *fz_display_list_s_getPixmap(struct fz_display_list_s *self,struct fz_matrix_s const *matrix,struct fz_colorspace_s *colorspace,int alpha,struct fz_rect_s *clip){ - struct fz_matrix_s *m = NULL; - struct fz_colorspace_s *cs = NULL; - fz_pixmap *pix = NULL; - if (matrix) m = (fz_matrix *) matrix; - else m = (fz_matrix *) &fz_identity; - - if (colorspace) cs = colorspace; - else cs = fz_device_rgb(gctx); - +SWIGINTERN PyObject *fz_document_s__updateObject(struct fz_document_s *self,int xref,char *text,struct fz_page_s *page){ + pdf_obj *new_obj; + pdf_document *pdf = pdf_specifics(gctx, self); // get pdf doc fz_try(gctx) { - pix = JM_pixmap_from_display_list(gctx, self, m, cs, alpha, clip); + assert_PDF(pdf); + int xreflen = pdf_xref_len(gctx, pdf); + if (!INRANGE(xref, 1, xreflen-1)) + THROWMSG("xref out of range"); + // create new object with passed-in string + new_obj = JM_pdf_obj_from_str(gctx, pdf, text); + pdf_update_object(gctx, pdf, xref, new_obj); + pdf_drop_obj(gctx, new_obj); + if (page) refresh_link_table(gctx, pdf_page_from_fz_page(gctx, page)); } fz_catch(gctx) return NULL; - return pix; + pdf->dirty = 1; + return NONE; } -SWIGINTERN struct fz_stext_page_s *fz_display_list_s_getTextPage(struct fz_display_list_s *self,int flags){ - struct fz_stext_page_s *tp = NULL; +SWIGINTERN PyObject *fz_document_s__updateStream(struct fz_document_s *self,int xref,PyObject *stream,int new){ + pdf_obj *obj = NULL; + fz_var(obj); + fz_buffer *res = NULL; + fz_var(res); + pdf_document *pdf = pdf_specifics(gctx, self); // get pdf doc fz_try(gctx) { - fz_stext_options stext_options = { 0 }; - stext_options.flags = flags; - tp = fz_new_stext_page_from_display_list(gctx, self, &stext_options); + assert_PDF(pdf); + int xreflen = pdf_xref_len(gctx, pdf); + if (!INRANGE(xref, 1, xreflen-1)) + THROWMSG("xref out of range"); + // get the object + obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!new && !pdf_is_stream(gctx, obj)) + THROWMSG("xref not a stream object"); + res = JM_BufferFromBytes(gctx, stream); + if (!res) THROWMSG("stream must be bytes or bytearray"); + JM_update_stream(gctx, pdf, obj, res); + } - fz_catch(gctx) return NULL; - return tp; + fz_always(gctx) + { + fz_drop_buffer(gctx, res); + pdf_drop_obj(gctx, obj); + } + fz_catch(gctx) + return NULL; + pdf->dirty = 1; + return NONE; } -SWIGINTERN struct fz_stext_page_s *new_fz_stext_page_s(struct fz_rect_s *mediabox){ - struct fz_stext_page_s *tp = NULL; - fz_try(gctx) - tp = fz_new_stext_page(gctx, mediabox); +SWIGINTERN PyObject *fz_document_s__setMetadata(struct fz_document_s *self,char *text){ + pdf_obj *info, *new_info, *new_info_ind; + int info_num = 0; // will contain xref no of info object + pdf_document *pdf = pdf_specifics(gctx, self); // get pdf doc + fz_try(gctx) { + assert_PDF(pdf); + // create new /Info object based on passed-in string + new_info = JM_pdf_obj_from_str(gctx, pdf, text); + } fz_catch(gctx) return NULL; - return tp; - } -SWIGINTERN void delete_fz_stext_page_s(struct fz_stext_page_s *self){ - DEBUGMSG1("text page"); - fz_drop_stext_page(gctx, self); - DEBUGMSG2; - } -SWIGINTERN PyObject *fz_stext_page_s_search(struct fz_stext_page_s *self,char const *needle,int hit_max){ - fz_rect *result = NULL; - PyObject *liste = PyList_New(0); - int i, mymax = hit_max; - if (mymax < 1) mymax = 16; - result = JM_Alloc(fz_rect, (mymax+1)); - struct fz_rect_s *rect = (struct fz_rect_s *) result; - int count = fz_search_stext_page(gctx, self, needle, result, hit_max); - for (i = 0; i < count; i++) + pdf->dirty = 1; + // replace existing /Info object + info = pdf_dict_get(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Info)); + if (info) { - PyList_Append(liste, - Py_BuildValue("ffff", rect->x0, rect->y0, - rect->x1,rect->y1)); - rect += 1; + info_num = pdf_to_num(gctx, info); // get xref no of old info + pdf_update_object(gctx, pdf, info_num, new_info); // insert new + pdf_drop_obj(gctx, new_info); + return NONE; } - JM_Free(result); - return liste; + // create new indirect object from /Info object + new_info_ind = pdf_add_object(gctx, pdf, new_info); + // put this in the trailer dictionary + pdf_dict_put_drop(gctx, pdf_trailer(gctx, pdf), PDF_NAME(Info), new_info_ind); + return NONE; } -SWIGINTERN PyObject *fz_stext_page_s__extractTextBlocks_AsList(struct fz_stext_page_s *self){ - fz_stext_block *block; - fz_stext_line *line; - fz_stext_char *ch; - int block_n = 0; - PyObject *lines = PyList_New(0); - PyObject *text = NULL, *litem; - fz_buffer *res = NULL; - for (block = self->first_block; block; block = block->next) - { - fz_rect *blockrect = JM_empty_rect(); - if (block->type == FZ_STEXT_BLOCK_TEXT) - { - fz_try(gctx) - { - res = fz_new_buffer(gctx, 1024); - int line_n = 0; - float last_y0 = 0.0; - for (line = block->u.t.first_line; line; line = line->next) - { - fz_rect *linerect = JM_empty_rect(); - // append line no. 2 with new-line - if (line_n > 0) - { - if (linerect->y0 != last_y0) - fz_append_string(gctx, res, "\n"); - else - fz_append_string(gctx, res, " "); - } - last_y0 = linerect->y0; - line_n++; - for (ch = line->first_char; ch; ch = ch->next) - { - fz_append_rune(gctx, res, ch->c); - JM_join_rect(linerect, &ch->bbox, ch->size); - } - JM_join_rect(blockrect, linerect, 0.0f); - JM_Free(linerect); - } - text = JM_StrFromBuffer(gctx, res); - } - fz_always(gctx) - { - fz_drop_buffer(gctx, res); - res = NULL; - } - fz_catch(gctx) return NULL; - } - else - { - fz_image *img = block->u.i.image; - fz_colorspace *cs = img->colorspace; - text = PyUnicode_FromFormat("", fz_colorspace_name(gctx, cs), img->w, img->h, img->bpc); - JM_join_rect(blockrect, &block->bbox, 0.0f); - } - litem = Py_BuildValue("ffffOii", blockrect->x0, blockrect->y0, - blockrect->x1, blockrect->y1, - text, block_n, block->type); - PyList_Append(lines, litem); - Py_CLEAR(litem); - Py_CLEAR(text); - JM_Free(blockrect); - block_n++; - } - return lines; +SWIGINTERN void delete_fz_page_s(struct fz_page_s *self){ + DEBUGMSG1("page"); + fz_drop_page(gctx, self); + DEBUGMSG2; } -SWIGINTERN PyObject *fz_stext_page_s__extractTextWords_AsList(struct fz_stext_page_s *self){ - fz_stext_block *block; - fz_stext_line *line; - fz_stext_char *ch; - fz_buffer *buff = NULL; - size_t buflen = 0; - int block_n = 0, line_n, word_n; - fz_rect *wbbox = JM_empty_rect(); // word bbox - PyObject *lines = PyList_New(0); - for (block = self->first_block; block; block = block->next) - { - if (block->type != FZ_STEXT_BLOCK_TEXT) - { - block_n++; - continue; - } - line_n = 0; - for (line = block->u.t.first_line; line; line = line->next) - { - word_n = 0; // word counter per line - buff = NULL; // reset word buffer - buflen = 0; // reset char counter - for (ch = line->first_char; ch; ch = ch->next) - { - if (ch->c == 32 && buflen == 0) - continue; // skip spaces at line start - if (ch->c == 32) - { // --> finish the word - word_n = JM_append_word(gctx, lines, buff, wbbox, - block_n, line_n, word_n); - fz_drop_buffer(gctx, buff); - buff = NULL; - buflen = 0; // reset char counter - continue; - } - // append one unicode character to the word - if (!buff) buff = fz_new_buffer(gctx, 64); - fz_append_rune(gctx, buff, ch->c); - buflen++; - // enlarge word bbox - JM_join_rect(wbbox, &ch->bbox, ch->size); - } - if (buff) // store any remaining word - { - word_n = JM_append_word(gctx, lines, buff, wbbox, - block_n, line_n, word_n); - fz_drop_buffer(gctx, buff); - buff = NULL; - buflen = 0; - } - line_n++; - } - block_n++; - } - JM_Free(wbbox); - return lines; +SWIGINTERN PyObject *fz_page_s_bound(struct fz_page_s *self){ + fz_rect rect = fz_bound_page(gctx, self); + return JM_py_from_rect(rect); } -SWIGINTERN PyObject *fz_stext_page_s__extractText(struct fz_stext_page_s *self,int format){ +SWIGINTERN PyObject *fz_page_s_run(struct fz_page_s *self,struct DeviceWrapper *dw,PyObject *m){ + fz_try(gctx) fz_run_page(gctx, self, dw->device, JM_matrix_from_py(m), NULL); + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN PyObject *fz_page_s_getSVGimage(struct fz_page_s *self,PyObject *matrix){ + fz_rect mediabox = fz_bound_page(gctx, self); + fz_device *dev = NULL; fz_buffer *res = NULL; - fz_output *out = NULL; PyObject *text = NULL; - fz_var(res); + fz_matrix ctm = JM_matrix_from_py(matrix); + fz_cookie *cookie = NULL; + fz_output *out = NULL; + fz_separations *seps = NULL; fz_var(out); + fz_var(dev); + fz_var(res); + fz_rect tbounds = mediabox; + tbounds = fz_transform_rect(tbounds, ctm); + fz_try(gctx) { res = fz_new_buffer(gctx, 1024); out = fz_new_output_with_buffer(gctx, res); - switch(format) - { - case(1): - fz_print_stext_page_as_html(gctx, out, self); - break; - case(2): - text = JM_stext_page_as_dict(gctx, self, 0); - break; - case(3): - fz_print_stext_page_as_xml(gctx, out, self); - break; - case(4): - fz_print_stext_page_as_xhtml(gctx, out, self); - break; - case(5): - text = JM_stext_page_as_dict(gctx, self, 0); - break; - case(6): - text = JM_stext_page_as_dict(gctx, self, 1); - break; - default: - JM_print_stext_page_as_text(gctx, out, self); - break; - } - if (!text) text = JM_StrFromBuffer(gctx, res); - + dev = fz_new_svg_device(gctx, out, tbounds.x1-tbounds.x0, tbounds.y1-tbounds.y0, FZ_SVG_TEXT_AS_PATH, 1); + fz_run_page(gctx, self, dev, ctm, cookie); + fz_close_device(gctx, dev); + text = JM_StrFromBuffer(gctx, res); } fz_always(gctx) { - fz_drop_buffer(gctx, res); + fz_drop_device(gctx, dev); fz_drop_output(gctx, out); + fz_drop_buffer(gctx, res); + } + fz_catch(gctx) + { + return NULL; } - fz_catch(gctx) return NULL; - return text; } -SWIGINTERN void delete_pdf_graft_map_s(struct pdf_graft_map_s *self){ - DEBUGMSG1("graftmap"); - pdf_drop_graft_map(gctx, self); - DEBUGMSG2; - } -SWIGINTERN struct pdf_graft_map_s *new_pdf_graft_map_s(struct fz_document_s *doc){ - pdf_graft_map *map = NULL; +SWIGINTERN struct fz_annot_s *fz_page_s_addLineAnnot(struct fz_page_s *self,PyObject *p1,PyObject *p2){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_annot *annot = NULL; + float col[3] = {0, 0, 0}; + float width = 1.0f; + fz_point a = JM_point_from_py(p1); + fz_point b = JM_point_from_py(p2); + fz_rect r = {MIN(a.x, b.x), MIN(a.y, b.y), MAX(a.x, b.x), MAX(a.y, b.y)}; + fz_var(annot); fz_try(gctx) { - pdf_document *dst = pdf_specifics(gctx, doc); - assert_PDF(dst); - map = pdf_new_graft_map(gctx, dst); + assert_PDF(page); + pdf_document *pdf = page->doc; + annot = pdf_create_annot(gctx, page, PDF_ANNOT_LINE); + pdf_set_annot_line(gctx, annot, a, b); + pdf_set_annot_border(gctx, annot, width); + pdf_set_annot_color(gctx, annot, 3, col); + r = fz_expand_rect(r, 3 * width); + pdf_set_annot_rect(gctx, annot, r); + pdf_update_annot(gctx, annot); } fz_catch(gctx) return NULL; - return map; + fz_annot *fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); } -SWIGINTERN int Tools_gen_id(struct Tools *self){ - return fz_gen_id(gctx); +SWIGINTERN struct fz_annot_s *fz_page_s_addTextAnnot(struct fz_page_s *self,PyObject *point,char *text){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_annot *annot = NULL; + fz_point pos = JM_point_from_py(point); + fz_var(annot); + fz_try(gctx) + { + assert_PDF(page); + annot = pdf_create_annot(gctx, page, PDF_ANNOT_TEXT); + pdf_set_text_annot_position(gctx, annot, pos); + pdf_set_annot_contents(gctx, annot, text); + pdf_set_annot_icon_name(gctx, annot, "Note"); + pdf_update_annot(gctx, annot); + } + fz_catch(gctx) return NULL; + fz_annot *fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); } +SWIGINTERN struct fz_annot_s *fz_page_s_addInkAnnot(struct fz_page_s *self,PyObject *list){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_annot *annot = NULL; + PyObject *p = NULL, *sublist = NULL; + pdf_obj *inklist = NULL, *stroke = NULL; + double x, y, height; + fz_rect prect; + fz_var(annot); + fz_try(gctx) + { + assert_PDF(page); + if (!PySequence_Check(list)) THROWMSG("arg must be a sequence"); + prect = pdf_bound_page(gctx, page); + height = prect.y1 - prect.y0; + annot = pdf_create_annot(gctx, page, PDF_ANNOT_INK); + Py_ssize_t i, j, n0 = PySequence_Size(list), n1; + inklist = pdf_new_array(gctx, annot->page->doc, n0); + for (j = 0; j < n0; j++) + { + sublist = PySequence_GetItem(list, j); + n1 = PySequence_Size(sublist); + stroke = pdf_new_array(gctx, annot->page->doc, 2 * n1); + for (i = 0; i < n1; i++) + { + p = PySequence_GetItem(sublist, i); + if (!PySequence_Check(p) || PySequence_Size(p) != 2) + THROWMSG("3rd level entries must be pairs of floats"); + x = PyFloat_AsDouble(PySequence_GetItem(p, 0)); + if (PyErr_Occurred()) + THROWMSG("invalid point coordinate"); + y = PyFloat_AsDouble(PySequence_GetItem(p, 1)); + if (PyErr_Occurred()) + THROWMSG("invalid point coordinate"); + Py_CLEAR(p); + pdf_array_push_real(gctx, stroke, x); + pdf_array_push_real(gctx, stroke, height - y); + } + pdf_array_push_drop(gctx, inklist, stroke); + stroke = NULL; + Py_CLEAR(sublist); + } + pdf_dict_put_drop(gctx, annot->obj, PDF_NAME(InkList), inklist); + inklist = NULL; + pdf_dirty_annot(gctx, annot); + pdf_update_annot(gctx, annot); + } + fz_catch(gctx) + { + Py_CLEAR(p); + Py_CLEAR(sublist); + return NULL; + } -SWIGINTERN int -SWIG_AsVal_unsigned_SS_long (PyObject *obj, unsigned long *val) -{ -#if PY_VERSION_HEX < 0x03000000 - if (PyInt_Check(obj)) { - long v = PyInt_AsLong(obj); - if (v >= 0) { - if (val) *val = v; - return SWIG_OK; - } else { - return SWIG_OverflowError; - } - } else -#endif - if (PyLong_Check(obj)) { - unsigned long v = PyLong_AsUnsignedLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return SWIG_OK; - } else { - PyErr_Clear(); - return SWIG_OverflowError; - } - } -#ifdef SWIG_PYTHON_CAST_MODE - { - int dispatch = 0; - unsigned long v = PyLong_AsUnsignedLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return SWIG_AddCast(SWIG_OK); - } else { - PyErr_Clear(); - } - if (!dispatch) { - double d; - int res = SWIG_AddCast(SWIG_AsVal_double (obj,&d)); - if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, 0, ULONG_MAX)) { - if (val) *val = (unsigned long)(d); - return res; - } - } - } -#endif - return SWIG_TypeError; -} - - -SWIGINTERN int -SWIG_AsVal_unsigned_SS_int (PyObject * obj, unsigned int *val) -{ - unsigned long v; - int res = SWIG_AsVal_unsigned_SS_long (obj, &v); - if (SWIG_IsOK(res)) { - if ((v > UINT_MAX)) { - return SWIG_OverflowError; - } else { - if (val) *val = (unsigned int)(v); - } - } - return res; -} - -SWIGINTERN size_t Tools_store_shrink(struct Tools *self,unsigned int percent){ - if (percent >= 100) + fz_annot *fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); + } +SWIGINTERN struct fz_annot_s *fz_page_s_addStampAnnot(struct fz_page_s *self,PyObject *rect,int stamp){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_annot *annot = NULL; + pdf_obj *stamp_id[] = {PDF_NAME(Approved), PDF_NAME(AsIs), + PDF_NAME(Confidential), PDF_NAME(Departmental), + PDF_NAME(Experimental), PDF_NAME(Expired), + PDF_NAME(Final), PDF_NAME(ForComment), + PDF_NAME(ForPublicRelease), PDF_NAME(NotApproved), + PDF_NAME(NotForPublicRelease), PDF_NAME(Sold), + PDF_NAME(TopSecret), PDF_NAME(Draft)}; + int n = nelem(stamp_id); + pdf_obj *name = stamp_id[0]; + fz_var(annot); + fz_try(gctx) { - fz_empty_store(gctx); - return 0; + assert_PDF(page); + if (INRANGE(stamp, 0, n-1)) + name = stamp_id[stamp]; + annot = pdf_create_annot(gctx, page, PDF_ANNOT_STAMP); + pdf_set_annot_rect(gctx, annot, JM_rect_from_py(rect)); + pdf_dict_put(gctx, annot->obj, PDF_NAME(Name), name); + pdf_set_annot_contents(gctx, annot, + pdf_dict_get_name(gctx, annot->obj, PDF_NAME(Name))); + pdf_update_annot(gctx, annot); } - if (percent > 0) fz_shrink_store(gctx, 100 - percent); - return gctx->store->size; + fz_catch(gctx) return NULL; + fz_annot *fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); } - - #define SWIG_From_long PyInt_FromLong - - -SWIGINTERNINLINE PyObject* -SWIG_From_unsigned_SS_long (unsigned long value) -{ - return (value > LONG_MAX) ? - PyLong_FromUnsignedLong(value) : PyInt_FromLong((long)(value)); -} - - -#ifdef SWIG_LONG_LONG_AVAILABLE -SWIGINTERNINLINE PyObject* -SWIG_From_unsigned_SS_long_SS_long (unsigned long long value) -{ - return (value > LONG_MAX) ? - PyLong_FromUnsignedLongLong(value) : PyInt_FromLong((long)(value)); -} -#endif - - -SWIGINTERNINLINE PyObject * -SWIG_From_size_t (size_t value) -{ -#ifdef SWIG_LONG_LONG_AVAILABLE - if (sizeof(size_t) <= sizeof(unsigned long)) { -#endif - return SWIG_From_unsigned_SS_long ((unsigned long)(value)); -#ifdef SWIG_LONG_LONG_AVAILABLE - } else { - /* assume sizeof(size_t) <= sizeof(unsigned long long) */ - return SWIG_From_unsigned_SS_long_SS_long ((unsigned long long)(value)); - } -#endif -} - -SWIGINTERN size_t Tools_store_size(struct Tools *self){ - return gctx->store->size; +SWIGINTERN struct fz_annot_s *fz_page_s_addFileAnnot(struct fz_page_s *self,PyObject *point,PyObject *buffer,char *filename,char *ufilename,char *desc){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *fzannot = NULL; + pdf_annot *annot = NULL; + char *data = NULL, *uf = ufilename, *d = desc; + if (!ufilename) uf = filename; + if (!desc) d = filename; + size_t len = 0; + fz_buffer *filebuf = NULL; + fz_point p = JM_point_from_py(point); + fz_rect r = {p.x, p.y, p.x + 20, p.y + 30}; + fz_var(annot); + fz_try(gctx) + { + assert_PDF(page); + annot = pdf_create_annot(gctx, page, ANNOT_FILEATTACHMENT); + pdf_set_annot_rect(gctx, annot, r); + pdf_set_annot_icon_name(gctx, annot, "PushPin"); + len = JM_CharFromBytesOrArray(buffer, &data); + filebuf = fz_new_buffer_from_shared_data(gctx, data, len); + pdf_obj *val = JM_embed_file(gctx, page->doc, filebuf, + filename, uf, d); + pdf_dict_put(gctx, annot->obj, PDF_NAME(FS), val); + pdf_dict_put_text_string(gctx, annot->obj, PDF_NAME(Contents), filename); + pdf_update_annot(gctx, annot); + } + fz_catch(gctx) return NULL; + fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); } -SWIGINTERN size_t Tools_store_maxsize(struct Tools *self){ - return gctx->store->max; +SWIGINTERN struct fz_annot_s *fz_page_s_addStrikeoutAnnot(struct fz_page_s *self,PyObject *rect){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *annot = NULL; + fz_var(annot); + fz_quad quad = JM_quad_from_py(rect); + fz_try(gctx) + { + assert_PDF(page); + annot = JM_AnnotTextmarker(gctx, page, quad, PDF_ANNOT_STRIKE_OUT); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, annot); } -SWIGINTERN PyObject *Tools_fitz_config(struct Tools *self){ - return JM_fitz_config(); +SWIGINTERN struct fz_annot_s *fz_page_s_addUnderlineAnnot(struct fz_page_s *self,PyObject *rect){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *annot = NULL; + fz_var(annot); + fz_quad quad = JM_quad_from_py(rect); + fz_try(gctx) + { + assert_PDF(page); + annot = JM_AnnotTextmarker(gctx, page, quad, PDF_ANNOT_UNDERLINE); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, annot); } -SWIGINTERN void Tools__store_debug(struct Tools *self){ - fz_debug_store(gctx); +SWIGINTERN struct fz_annot_s *fz_page_s_addSquigglyAnnot(struct fz_page_s *self,PyObject *rect){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *annot = NULL; + fz_var(annot); + fz_quad quad = JM_quad_from_py(rect); + fz_try(gctx) + { + assert_PDF(page); + annot = JM_AnnotTextmarker(gctx, page, quad, PDF_ANNOT_SQUIGGLY); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, annot); } -SWIGINTERN void Tools_glyph_cache_empty(struct Tools *self){ - fz_purge_glyph_cache(gctx); +SWIGINTERN struct fz_annot_s *fz_page_s_addHighlightAnnot(struct fz_page_s *self,PyObject *rect){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *annot = NULL; + fz_var(annot); + fz_quad quad = JM_quad_from_py(rect); + fz_try(gctx) + { + assert_PDF(page); + annot = JM_AnnotTextmarker(gctx, page, quad, PDF_ANNOT_HIGHLIGHT); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, annot); } -#ifdef __cplusplus -extern "C" { -#endif -SWIGINTERN PyObject *_wrap_delete_Document(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:delete_Document",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, SWIG_POINTER_DISOWN | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Document" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - delete_fz_document_s(arg1); - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Document(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - char *arg1 = (char *) NULL ; - PyObject *arg2 = (PyObject *) NULL ; - char *arg3 = (char *) NULL ; - struct fz_rect_s *arg4 = (struct fz_rect_s *) NULL ; - float arg5 = (float) 0 ; - float arg6 = (float) 0 ; - float arg7 = (float) 11 ; - int res1 ; - char *buf1 = 0 ; - int alloc1 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - void *argp4 = 0 ; - int res4 = 0 ; - float val5 ; - int ecode5 = 0 ; - float val6 ; - int ecode6 = 0 ; - float val7 ; - int ecode7 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject * obj6 = 0 ; - struct fz_document_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"|OOOOOOO:new_Document",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; - if (obj0) { - res1 = SWIG_AsCharPtrAndSize(obj0, &buf1, NULL, &alloc1); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Document" "', argument " "1"" of type '" "char const *""'"); - } - arg1 = (char *)(buf1); - } - if (obj1) { - arg2 = obj1; - } - if (obj2) { - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "new_Document" "', argument " "3"" of type '" "char const *""'"); - } - arg3 = (char *)(buf3); - } - if (obj3) { - res4 = SWIG_ConvertPtr(obj3, &argp4,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res4)) { - SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "new_Document" "', argument " "4"" of type '" "struct fz_rect_s *""'"); - } - arg4 = (struct fz_rect_s *)(argp4); - } - if (obj4) { - ecode5 = SWIG_AsVal_float(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "new_Document" "', argument " "5"" of type '" "float""'"); - } - arg5 = (float)(val5); - } - if (obj5) { - ecode6 = SWIG_AsVal_float(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "new_Document" "', argument " "6"" of type '" "float""'"); - } - arg6 = (float)(val6); - } - if (obj6) { - ecode7 = SWIG_AsVal_float(obj6, &val7); - if (!SWIG_IsOK(ecode7)) { - SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "new_Document" "', argument " "7"" of type '" "float""'"); - } - arg7 = (float)(val7); - } - { - result = (struct fz_document_s *)new_fz_document_s((char const *)arg1,arg2,(char const *)arg3,arg4,arg5,arg6,arg7); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_document_s, SWIG_POINTER_NEW | 0 ); - if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return resultobj; -fail: - if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_close(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_close",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_close" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - fz_document_s_close(arg1); - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_loadPage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_page_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|O:Document_loadPage",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_loadPage" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_loadPage" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - } - { - result = (struct fz_page_s *)fz_document_s_loadPage(arg1,arg2); - if(result==NULL) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_page_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document__loadOutline(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - struct fz_outline_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__loadOutline",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__loadOutline" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (struct fz_outline_s *)fz_document_s__loadOutline(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_outline_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document__dropOutline(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - struct fz_outline_s *arg2 = (struct fz_outline_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document__dropOutline",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__dropOutline" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_outline_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document__dropOutline" "', argument " "2"" of type '" "struct fz_outline_s *""'"); - } - arg2 = (struct fz_outline_s *)(argp2); - fz_document_s__dropOutline(arg1,arg2); - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_embeddedFileCount(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_embeddedFileCount",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileCount" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (int)fz_document_s_embeddedFileCount(arg1); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_embeddedFileDel(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - char *arg2 = (char *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int res2 ; - char *buf2 = 0 ; - int alloc2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document_embeddedFileDel",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileDel" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_embeddedFileDel" "', argument " "2"" of type '" "char *""'"); - } - arg2 = (char *)(buf2); - { - result = (int)fz_document_s_embeddedFileDel(arg1,arg2); - if(result < 1) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_From_int((int)(result)); - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return resultobj; -fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_embeddedFileInfo(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document_embeddedFileInfo",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileInfo" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - arg2 = obj1; - { - result = (PyObject *)fz_document_s_embeddedFileInfo(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_embeddedFileUpd(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - PyObject *arg3 = (PyObject *) NULL ; - char *arg4 = (char *) NULL ; - char *arg5 = (char *) NULL ; - char *arg6 = (char *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - int res4 ; - char *buf4 = 0 ; - int alloc4 = 0 ; - int res5 ; - char *buf5 = 0 ; - int alloc5 = 0 ; - int res6 ; - char *buf6 = 0 ; - int alloc6 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO|OOOO:Document_embeddedFileUpd",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileUpd" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - arg2 = obj1; - if (obj2) { - arg3 = obj2; - } - if (obj3) { - res4 = SWIG_AsCharPtrAndSize(obj3, &buf4, NULL, &alloc4); - if (!SWIG_IsOK(res4)) { - SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Document_embeddedFileUpd" "', argument " "4"" of type '" "char *""'"); - } - arg4 = (char *)(buf4); - } - if (obj4) { - res5 = SWIG_AsCharPtrAndSize(obj4, &buf5, NULL, &alloc5); - if (!SWIG_IsOK(res5)) { - SWIG_exception_fail(SWIG_ArgError(res5), "in method '" "Document_embeddedFileUpd" "', argument " "5"" of type '" "char *""'"); - } - arg5 = (char *)(buf5); - } - if (obj5) { - res6 = SWIG_AsCharPtrAndSize(obj5, &buf6, NULL, &alloc6); - if (!SWIG_IsOK(res6)) { - SWIG_exception_fail(SWIG_ArgError(res6), "in method '" "Document_embeddedFileUpd" "', argument " "6"" of type '" "char *""'"); - } - arg6 = (char *)(buf6); - } - { - result = (PyObject *)fz_document_s_embeddedFileUpd(arg1,arg2,arg3,arg4,arg5,arg6); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); - if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); - if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); - return resultobj; -fail: - if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); - if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); - if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_embeddedFileGet(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document_embeddedFileGet",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileGet" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - arg2 = obj1; - { - result = (PyObject *)fz_document_s_embeddedFileGet(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_embeddedFileAdd(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - char *arg3 = (char *) 0 ; - char *arg4 = (char *) NULL ; - char *arg5 = (char *) NULL ; - char *arg6 = (char *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - int res4 ; - char *buf4 = 0 ; - int alloc4 = 0 ; - int res5 ; - char *buf5 = 0 ; - int alloc5 = 0 ; - int res6 ; - char *buf6 = 0 ; - int alloc6 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO|OOO:Document_embeddedFileAdd",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileAdd" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - arg2 = obj1; - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Document_embeddedFileAdd" "', argument " "3"" of type '" "char const *""'"); - } - arg3 = (char *)(buf3); - if (obj3) { - res4 = SWIG_AsCharPtrAndSize(obj3, &buf4, NULL, &alloc4); - if (!SWIG_IsOK(res4)) { - SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Document_embeddedFileAdd" "', argument " "4"" of type '" "char *""'"); - } - arg4 = (char *)(buf4); - } - if (obj4) { - res5 = SWIG_AsCharPtrAndSize(obj4, &buf5, NULL, &alloc5); - if (!SWIG_IsOK(res5)) { - SWIG_exception_fail(SWIG_ArgError(res5), "in method '" "Document_embeddedFileAdd" "', argument " "5"" of type '" "char *""'"); - } - arg5 = (char *)(buf5); - } - if (obj5) { - res6 = SWIG_AsCharPtrAndSize(obj5, &buf6, NULL, &alloc6); - if (!SWIG_IsOK(res6)) { - SWIG_exception_fail(SWIG_ArgError(res6), "in method '" "Document_embeddedFileAdd" "', argument " "6"" of type '" "char *""'"); - } - arg6 = (char *)(buf6); - } - { - result = (PyObject *)fz_document_s_embeddedFileAdd(arg1,arg2,(char const *)arg3,arg4,arg5,arg6); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); - if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); - if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); - return resultobj; -fail: - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); - if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); - if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_convertToPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 = (int) 0 ; - int arg3 = (int) -1 ; - int arg4 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; - int val4 ; - int ecode4 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|OOO:Document_convertToPDF",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_convertToPDF" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_convertToPDF" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - } - if (obj2) { - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_convertToPDF" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - } - if (obj3) { - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_convertToPDF" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); - } - { - result = (PyObject *)fz_document_s_convertToPDF(arg1,arg2,arg3,arg4); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_pageCount(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_pageCount",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_pageCount" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (int)fz_document_s_pageCount(arg1); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document__getMetadata(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - char *arg2 = (char *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int res2 ; - char *buf2 = 0 ; - int alloc2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - char *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document__getMetadata",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getMetadata" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document__getMetadata" "', argument " "2"" of type '" "char const *""'"); - } - arg2 = (char *)(buf2); - result = (char *)fz_document_s__getMetadata(arg1,(char const *)arg2); - resultobj = SWIG_FromCharPtr((const char *)result); - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return resultobj; -fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_needsPass(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_needsPass",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_needsPass" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (int)fz_document_s_needsPass(arg1); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_resolveLink(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - char *arg2 = (char *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - int res2 ; - char *buf2 = 0 ; - int alloc2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|O:Document_resolveLink",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_resolveLink" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_resolveLink" "', argument " "2"" of type '" "char *""'"); - } - arg2 = (char *)(buf2); - } - result = (PyObject *)fz_document_s_resolveLink(arg1,arg2); - resultobj = result; - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return resultobj; -fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_layout(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) NULL ; - float arg3 = (float) 0 ; - float arg4 = (float) 0 ; - float arg5 = (float) 11 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - float val3 ; - int ecode3 = 0 ; - float val4 ; - int ecode4 = 0 ; - float val5 ; - int ecode5 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|OOOO:Document_layout",&obj0,&obj1,&obj2,&obj3,&obj4)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_layout" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_layout" "', argument " "2"" of type '" "struct fz_rect_s *""'"); - } - arg2 = (struct fz_rect_s *)(argp2); - } - if (obj2) { - ecode3 = SWIG_AsVal_float(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_layout" "', argument " "3"" of type '" "float""'"); - } - arg3 = (float)(val3); - } - if (obj3) { - ecode4 = SWIG_AsVal_float(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_layout" "', argument " "4"" of type '" "float""'"); - } - arg4 = (float)(val4); - } - if (obj4) { - ecode5 = SWIG_AsVal_float(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_layout" "', argument " "5"" of type '" "float""'"); - } - arg5 = (float)(val5); - } - { - result = (PyObject *)fz_document_s_layout(arg1,arg2,arg3,arg4,arg5); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_makeBookmark(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|O:Document_makeBookmark",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_makeBookmark" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_makeBookmark" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - } - result = (PyObject *)fz_document_s_makeBookmark(arg1,arg2); - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_findBookmark(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - long long arg2 ; - void *argp1 = 0 ; - int res1 = 0 ; - long long val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document_findBookmark",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_findBookmark" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - ecode2 = SWIG_AsVal_long_SS_long(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_findBookmark" "', argument " "2"" of type '" "long long""'"); - } - arg2 = (long long)(val2); - result = (int)fz_document_s_findBookmark(arg1,arg2); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_isReflowable(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_isReflowable",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_isReflowable" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (PyObject *)fz_document_s_isReflowable(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document__deleteObject(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document__deleteObject",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__deleteObject" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__deleteObject" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - { - result = (PyObject *)fz_document_s__deleteObject(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document__getPDFroot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__getPDFroot",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getPDFroot" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (int)fz_document_s__getPDFroot(arg1); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_isPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_isPDF",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_isPDF" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (PyObject *)fz_document_s_isPDF(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_isDirty(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_isDirty",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_isDirty" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (PyObject *)fz_document_s_isDirty(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document__getGCTXerrcode(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__getGCTXerrcode",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getGCTXerrcode" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (int)fz_document_s__getGCTXerrcode(arg1); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document__getGCTXerrmsg(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - char *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__getGCTXerrmsg",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getGCTXerrmsg" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (char *)fz_document_s__getGCTXerrmsg(arg1); - resultobj = SWIG_FromCharPtr((const char *)result); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_authenticate(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - char *arg2 = (char *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int res2 ; - char *buf2 = 0 ; - int alloc2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document_authenticate",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_authenticate" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_authenticate" "', argument " "2"" of type '" "char const *""'"); - } - arg2 = (char *)(buf2); - result = (int)fz_document_s_authenticate(arg1,(char const *)arg2); - resultobj = SWIG_From_int((int)(result)); - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return resultobj; -fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_save(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - char *arg2 = (char *) 0 ; - int arg3 = (int) 0 ; - int arg4 = (int) 0 ; - int arg5 = (int) 0 ; - int arg6 = (int) 0 ; - int arg7 = (int) 0 ; - int arg8 = (int) 0 ; - int arg9 = (int) 0 ; - int arg10 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int res2 ; - char *buf2 = 0 ; - int alloc2 = 0 ; - int val3 ; - int ecode3 = 0 ; - int val4 ; - int ecode4 = 0 ; - int val5 ; - int ecode5 = 0 ; - int val6 ; - int ecode6 = 0 ; - int val7 ; - int ecode7 = 0 ; - int val8 ; - int ecode8 = 0 ; - int val9 ; - int ecode9 = 0 ; - int val10 ; - int ecode10 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject * obj6 = 0 ; - PyObject * obj7 = 0 ; - PyObject * obj8 = 0 ; - PyObject * obj9 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO|OOOOOOOO:Document_save",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6,&obj7,&obj8,&obj9)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_save" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_save" "', argument " "2"" of type '" "char *""'"); - } - arg2 = (char *)(buf2); - if (obj2) { - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_save" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - } - if (obj3) { - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_save" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); - } - if (obj4) { - ecode5 = SWIG_AsVal_int(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_save" "', argument " "5"" of type '" "int""'"); - } - arg5 = (int)(val5); - } - if (obj5) { - ecode6 = SWIG_AsVal_int(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Document_save" "', argument " "6"" of type '" "int""'"); - } - arg6 = (int)(val6); - } - if (obj6) { - ecode7 = SWIG_AsVal_int(obj6, &val7); - if (!SWIG_IsOK(ecode7)) { - SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Document_save" "', argument " "7"" of type '" "int""'"); - } - arg7 = (int)(val7); - } - if (obj7) { - ecode8 = SWIG_AsVal_int(obj7, &val8); - if (!SWIG_IsOK(ecode8)) { - SWIG_exception_fail(SWIG_ArgError(ecode8), "in method '" "Document_save" "', argument " "8"" of type '" "int""'"); - } - arg8 = (int)(val8); - } - if (obj8) { - ecode9 = SWIG_AsVal_int(obj8, &val9); - if (!SWIG_IsOK(ecode9)) { - SWIG_exception_fail(SWIG_ArgError(ecode9), "in method '" "Document_save" "', argument " "9"" of type '" "int""'"); - } - arg9 = (int)(val9); - } - if (obj9) { - ecode10 = SWIG_AsVal_int(obj9, &val10); - if (!SWIG_IsOK(ecode10)) { - SWIG_exception_fail(SWIG_ArgError(ecode10), "in method '" "Document_save" "', argument " "10"" of type '" "int""'"); - } - arg10 = (int)(val10); - } - { - result = (PyObject *)fz_document_s_save(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return resultobj; -fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_write(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 = (int) 0 ; - int arg3 = (int) 0 ; - int arg4 = (int) 0 ; - int arg5 = (int) 0 ; - int arg6 = (int) 0 ; - int arg7 = (int) 0 ; - int arg8 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; - int val4 ; - int ecode4 = 0 ; - int val5 ; - int ecode5 = 0 ; - int val6 ; - int ecode6 = 0 ; - int val7 ; - int ecode7 = 0 ; - int val8 ; - int ecode8 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject * obj6 = 0 ; - PyObject * obj7 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|OOOOOOO:Document_write",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6,&obj7)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_write" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_write" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - } - if (obj2) { - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_write" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - } - if (obj3) { - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_write" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); - } - if (obj4) { - ecode5 = SWIG_AsVal_int(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_write" "', argument " "5"" of type '" "int""'"); - } - arg5 = (int)(val5); - } - if (obj5) { - ecode6 = SWIG_AsVal_int(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Document_write" "', argument " "6"" of type '" "int""'"); - } - arg6 = (int)(val6); - } - if (obj6) { - ecode7 = SWIG_AsVal_int(obj6, &val7); - if (!SWIG_IsOK(ecode7)) { - SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Document_write" "', argument " "7"" of type '" "int""'"); - } - arg7 = (int)(val7); - } - if (obj7) { - ecode8 = SWIG_AsVal_int(obj7, &val8); - if (!SWIG_IsOK(ecode8)) { - SWIG_exception_fail(SWIG_ArgError(ecode8), "in method '" "Document_write" "', argument " "8"" of type '" "int""'"); - } - arg8 = (int)(val8); - } - { - result = (PyObject *)fz_document_s_write(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Document_insertPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - struct fz_document_s *arg2 = (struct fz_document_s *) 0 ; - int arg3 = (int) -1 ; - int arg4 = (int) -1 ; - int arg5 = (int) -1 ; - int arg6 = (int) -1 ; - int arg7 = (int) 1 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int val3 ; - int ecode3 = 0 ; - int val4 ; - int ecode4 = 0 ; - int val5 ; - int ecode5 = 0 ; - int val6 ; - int ecode6 = 0 ; - int val7 ; - int ecode7 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject * obj6 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO|OOOOO:Document_insertPDF",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_insertPDF" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_insertPDF" "', argument " "2"" of type '" "struct fz_document_s *""'"); - } - arg2 = (struct fz_document_s *)(argp2); - if (obj2) { - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_insertPDF" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - } - if (obj3) { - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_insertPDF" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); - } - if (obj4) { - ecode5 = SWIG_AsVal_int(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_insertPDF" "', argument " "5"" of type '" "int""'"); - } - arg5 = (int)(val5); - } - if (obj5) { - ecode6 = SWIG_AsVal_int(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Document_insertPDF" "', argument " "6"" of type '" "int""'"); - } - arg6 = (int)(val6); - } - if (obj6) { - ecode7 = SWIG_AsVal_int(obj6, &val7); - if (!SWIG_IsOK(ecode7)) { - SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Document_insertPDF" "', argument " "7"" of type '" "int""'"); - } - arg7 = (int)(val7); - } - { - result = (PyObject *)fz_document_s_insertPDF(arg1,arg2,arg3,arg4,arg5,arg6,arg7); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - +SWIGINTERN struct fz_annot_s *fz_page_s_addRectAnnot(struct fz_page_s *self,PyObject *rect){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *fzannot = NULL; + fz_var(fzannot); + fz_try(gctx) + { + assert_PDF(page); + fzannot = JM_AnnotCircleOrRect(gctx, page, rect, PDF_ANNOT_SQUARE); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, fzannot); + } +SWIGINTERN struct fz_annot_s *fz_page_s_addCircleAnnot(struct fz_page_s *self,PyObject *rect){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *fzannot = NULL; + fz_var(fzannot); + fz_try(gctx) + { + assert_PDF(page); + fzannot = JM_AnnotCircleOrRect(gctx, page, rect, PDF_ANNOT_CIRCLE); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, fzannot); + } +SWIGINTERN struct fz_annot_s *fz_page_s_addPolylineAnnot(struct fz_page_s *self,PyObject *points){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *fzannot = NULL; + fz_var(fzannot); + fz_try(gctx) + { + assert_PDF(page); + fzannot = JM_AnnotMultiline(gctx, page, points, PDF_ANNOT_POLY_LINE); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, fzannot); + } +SWIGINTERN struct fz_annot_s *fz_page_s_addPolygonAnnot(struct fz_page_s *self,PyObject *points){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_annot *fzannot = NULL; + fz_var(fzannot); + fz_try(gctx) + { + assert_PDF(page); + fzannot = JM_AnnotMultiline(gctx, page, points, PDF_ANNOT_POLYGON); + } + fz_catch(gctx) return NULL; + return fz_keep_annot(gctx, fzannot); + } +SWIGINTERN struct fz_annot_s *fz_page_s_addFreetextAnnot(struct fz_page_s *self,PyObject *rect,char *text,float fontsize,char *fontname,PyObject *color,int rotate){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + float bcol[3] = {1.0f, 1.0f, 1.0f}; // border, box color: white + float col[4] = {0.0f, 0.0f, 0.0f, 0.0f}; // std. text color: black + int ncol = 3; + JM_color_FromSequence(color, &ncol, col); + fz_rect r = JM_rect_from_py(rect); + pdf_annot *annot = NULL; + fz_var(annot); + fz_try(gctx) + { + assert_PDF(page); + annot = pdf_create_annot(gctx, page, PDF_ANNOT_FREE_TEXT); + pdf_set_annot_contents(gctx, annot, text); + pdf_set_annot_color(gctx, annot, 3, bcol); // set rect colors + pdf_dict_put_int(gctx, annot->obj, PDF_NAME(Rotate), rotate); + + pdf_set_text_annot_position(gctx, annot, fz_make_point(r.x0, r.y0)); + pdf_set_annot_rect(gctx, annot, r); + // insert the default appearance string + JM_make_annot_DA(gctx, annot, ncol, col, fontname, fontsize); + pdf_update_annot(gctx, annot); + } + fz_always(gctx) {;} + fz_catch(gctx) return NULL; + fz_annot *fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); + } +SWIGINTERN struct fz_annot_s *fz_page_s__addWidget(struct fz_page_s *self,PyObject *Widget){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_document *pdf = page->doc; + pdf_annot *annot = NULL; + pdf_widget *widget = NULL; + fz_var(annot); + fz_try(gctx) + { + //------------------------------------------------------------- + // create the widget - only need type and field name for this + //------------------------------------------------------------- + int field_type = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, + "field_type")); + JM_PyErr_Clear; -SWIGINTERN PyObject *_wrap_Document_insertPage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 = (int) -1 ; - PyObject *arg3 = (PyObject *) NULL ; - float arg4 = (float) 11 ; - float arg5 = (float) 595 ; - float arg6 = (float) 842 ; - int arg7 = (int) 0 ; - char *arg8 = (char *) NULL ; - char *arg9 = (char *) NULL ; - int arg10 = (int) 0 ; - PyObject *arg11 = (PyObject *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - float val4 ; - int ecode4 = 0 ; - float val5 ; - int ecode5 = 0 ; - float val6 ; - int ecode6 = 0 ; - int val7 ; - int ecode7 = 0 ; - int res8 ; - char *buf8 = 0 ; - int alloc8 = 0 ; - int res9 ; - char *buf9 = 0 ; - int alloc9 = 0 ; - int val10 ; - int ecode10 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject * obj6 = 0 ; - PyObject * obj7 = 0 ; - PyObject * obj8 = 0 ; - PyObject * obj9 = 0 ; - PyObject * obj10 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O|OOOOOOOOOO:Document_insertPage",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6,&obj7,&obj8,&obj9,&obj10)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_insertPage" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_insertPage" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - } - if (obj2) { - arg3 = obj2; - } - if (obj3) { - ecode4 = SWIG_AsVal_float(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_insertPage" "', argument " "4"" of type '" "float""'"); - } - arg4 = (float)(val4); - } - if (obj4) { - ecode5 = SWIG_AsVal_float(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_insertPage" "', argument " "5"" of type '" "float""'"); - } - arg5 = (float)(val5); - } - if (obj5) { - ecode6 = SWIG_AsVal_float(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Document_insertPage" "', argument " "6"" of type '" "float""'"); - } - arg6 = (float)(val6); - } - if (obj6) { - ecode7 = SWIG_AsVal_int(obj6, &val7); - if (!SWIG_IsOK(ecode7)) { - SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Document_insertPage" "', argument " "7"" of type '" "int""'"); - } - arg7 = (int)(val7); - } - if (obj7) { - res8 = SWIG_AsCharPtrAndSize(obj7, &buf8, NULL, &alloc8); - if (!SWIG_IsOK(res8)) { - SWIG_exception_fail(SWIG_ArgError(res8), "in method '" "Document_insertPage" "', argument " "8"" of type '" "char *""'"); - } - arg8 = (char *)(buf8); - } - if (obj8) { - res9 = SWIG_AsCharPtrAndSize(obj8, &buf9, NULL, &alloc9); - if (!SWIG_IsOK(res9)) { - SWIG_exception_fail(SWIG_ArgError(res9), "in method '" "Document_insertPage" "', argument " "9"" of type '" "char *""'"); - } - arg9 = (char *)(buf9); - } - if (obj9) { - ecode10 = SWIG_AsVal_int(obj9, &val10); - if (!SWIG_IsOK(ecode10)) { - SWIG_exception_fail(SWIG_ArgError(ecode10), "in method '" "Document_insertPage" "', argument " "10"" of type '" "int""'"); - } - arg10 = (int)(val10); - } - if (obj10) { - arg11 = obj10; - } - { - result = (int)fz_document_s_insertPage(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11); - if(result<0) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_From_int((int)(result)); - if (alloc8 == SWIG_NEWOBJ) free((char*)buf8); - if (alloc9 == SWIG_NEWOBJ) free((char*)buf9); - return resultobj; -fail: - if (alloc8 == SWIG_NEWOBJ) free((char*)buf8); - if (alloc9 == SWIG_NEWOBJ) free((char*)buf9); - return NULL; -} + char *field_name = JM_Python_str_AsChar(PyObject_GetAttrString(Widget, + "field_name")); + JM_PyErr_Clear; + widget = pdf_create_widget(gctx, pdf, page, field_type, field_name); + JM_Python_str_DelForPy3(field_name); + JM_PyErr_Clear; + annot = (pdf_annot *) widget; -SWIGINTERN PyObject *_wrap_Document_select(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document_select",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_select" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - arg2 = obj1; - { - result = (PyObject *)fz_document_s_select(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + //------------------------------------------------------------- + // now field exists, adjust its properties + //------------------------------------------------------------- + JM_set_widget_properties(gctx, annot, Widget, field_type); + } + fz_always(gctx) JM_PyErr_Clear; + fz_catch(gctx) return NULL; + fz_annot *fzannot = (fz_annot *) annot; + return fz_keep_annot(gctx, fzannot); + } +SWIGINTERN struct fz_display_list_s *fz_page_s_getDisplayList(struct fz_page_s *self){ + fz_display_list *dl = NULL; + fz_try(gctx) dl = fz_new_display_list_from_page(gctx, self); + fz_catch(gctx) return NULL; + return dl; + } +SWIGINTERN PyObject *fz_page_s_setCropBox(struct fz_page_s *self,PyObject *rect){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + fz_try(gctx) + { + assert_PDF(page); + fz_rect mediabox = pdf_bound_page(gctx, page); + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(MediaBox)); + if (o) mediabox = pdf_to_rect(gctx, o); + fz_rect cropbox = fz_empty_rect; + fz_rect r = JM_rect_from_py(rect); + cropbox.x0 = r.x0; + cropbox.y0 = mediabox.y1 - r.y1; + cropbox.x1 = r.x1; + cropbox.y1 = mediabox.y1 - r.y0; + pdf_dict_put_drop(gctx, page->obj, PDF_NAME(CropBox), + pdf_new_rect(gctx, page->doc, cropbox)); + } + fz_catch(gctx) return NULL; + page->doc->dirty = 1; + return NONE; + } +SWIGINTERN struct fz_link_s *fz_page_s_loadLinks(struct fz_page_s *self){ + fz_link *l = NULL; + fz_try(gctx) l = fz_load_links(gctx, self); + fz_catch(gctx) return NULL; + return l; + } +SWIGINTERN struct fz_annot_s *fz_page_s_firstAnnot(struct fz_page_s *self){ + fz_annot *annot; + fz_try(gctx) annot = fz_first_annot(gctx, self); + fz_catch(gctx) annot = NULL; + if (annot) fz_keep_annot(gctx, annot); + return annot; + } +SWIGINTERN void fz_page_s_deleteLink(struct fz_page_s *self,PyObject *linkdict){ + if (!linkdict) return; // have no parameter + if (!PyDict_Check(linkdict)) return; // have no dictionary + pdf_page *page = pdf_page_from_fz_page(gctx, self); + if (!page) return; // have no PDF + int xref = (int) PyInt_AsLong(PyDict_GetItemString(linkdict, "xref")); + if (xref < 1) return; // invalid xref + pdf_obj *annots = pdf_dict_get(gctx, page->obj, PDF_NAME(Annots)); + if (!annots) return; // have no annotations + int len = pdf_array_len(gctx, annots); + int i, oxref = 0; + for (i = 0; i < len; i++) + { + oxref = pdf_to_num(gctx, pdf_array_get(gctx, annots, i)); + if (xref == oxref) break; // found xref in annotations + } + if (xref != oxref) return; // xref not in annotations + pdf_array_delete(gctx, annots, i); // delete entry in annotations + pdf_delete_object(gctx, page->doc, xref); // delete link object + pdf_dict_put(gctx, page->obj, PDF_NAME(Annots), annots); + refresh_link_table(gctx, page); // reload link / annot tables + page->doc->dirty = 1; + return; + } +SWIGINTERN struct fz_annot_s *fz_page_s_deleteAnnot(struct fz_page_s *self,struct fz_annot_s *fannot){ + if (!fannot) return NULL; + fz_annot *nextannot = fz_next_annot(gctx, fannot); // store next + pdf_page *page = pdf_page_from_fz_page(gctx, self); + if (!page) // no PDF, just return next annotation + { + if (nextannot) fz_keep_annot(gctx, nextannot); + return nextannot; + } + pdf_annot *pannot = pdf_annot_from_fz_annot(gctx, fannot); + pdf_delete_annot(gctx, page, pannot); + if (nextannot) fz_keep_annot(gctx, nextannot); + page->doc->dirty = 1; + return nextannot; + } +SWIGINTERN PyObject *fz_page_s_MediaBoxSize(struct fz_page_s *self){ + PyObject *p = JM_py_from_point(fz_make_point(0, 0)); + pdf_page *page = pdf_page_from_fz_page(gctx, self); + if (!page) return p; + fz_rect r = fz_empty_rect; + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(MediaBox)); + if (!o) return p; + r = pdf_to_rect(gctx, o); + return JM_py_from_point(fz_make_point(r.x1 - r.x0, r.y1 - r.y0)); + } +SWIGINTERN PyObject *fz_page_s_CropBoxPosition(struct fz_page_s *self){ + PyObject *p = JM_py_from_point(fz_make_point(0, 0)); + pdf_page *page = pdf_page_from_fz_page(gctx, self); + if (!page) return p; // not a PDF + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(CropBox)); + if (!o) return p; // no CropBox specified + fz_rect cbox = pdf_to_rect(gctx, o); + return JM_py_from_point(fz_make_point(cbox.x0, cbox.y0));; + } +SWIGINTERN int fz_page_s_rotation(struct fz_page_s *self){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + if (!page) return -1; + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(Rotate)); + if (!o) return 0; + return pdf_to_int(gctx, o); + } +SWIGINTERN PyObject *fz_page_s_setRotation(struct fz_page_s *self,int rot){ + fz_try(gctx) + { + pdf_page *page = pdf_page_from_fz_page(gctx, self); + assert_PDF(page); + if (rot % 90) THROWMSG("rotate not 90 * int"); + pdf_dict_put_int(gctx, page->obj, PDF_NAME(Rotate), (int64_t) rot); + page->doc->dirty = 1; + } + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN PyObject *fz_page_s__addAnnot_FromString(struct fz_page_s *self,PyObject *linklist){ + pdf_obj *annots, *annots_arr, *annot, *ind_obj, *new_array; + pdf_page *page = pdf_page_from_fz_page(gctx, self); + PyObject *txtpy; + char *text; + int lcount = (int) PySequence_Size(linklist); // new object count + if (lcount < 1) return NONE; + int i; + fz_try(gctx) + { + assert_PDF(page); // make sure we have a PDF + // get existing annots array + annots = pdf_dict_get(gctx, page->obj, PDF_NAME(Annots)); + if (annots) + { + if (pdf_is_indirect(gctx, annots)) + annots_arr = pdf_resolve_indirect(gctx, annots); + else annots_arr = annots; + } + else + annots_arr = NULL; + int new_len = lcount; + if (annots_arr) new_len += pdf_array_len(gctx, annots_arr); + // allocate new array of old plus new size + new_array = pdf_new_array(gctx, page->doc, new_len); + if (annots_arr) + { // copy existing annots to new array + for (i = 0; i < pdf_array_len(gctx, annots_arr); i++) + pdf_array_push(gctx, new_array, pdf_array_get(gctx, annots_arr, i)); + } + } + fz_catch(gctx) return NULL; -SWIGINTERN PyObject *_wrap_Document_permissions(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_permissions",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_permissions" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (PyObject *)fz_document_s_permissions(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} + // extract object sources from Python list and store as annotations + for (i = 0; i < lcount; i++) + { + fz_try(gctx) + { + text = NULL; + txtpy = PySequence_ITEM(linklist, (Py_ssize_t) i); + text = JM_Python_str_AsChar(txtpy); + if (!text) THROWMSG("non-string linklist item"); + annot = JM_pdf_obj_from_str(gctx, page->doc, text); + JM_Python_str_DelForPy3(text); + ind_obj = pdf_add_object(gctx, page->doc, annot); + pdf_array_push_drop(gctx, new_array, ind_obj); + pdf_drop_obj(gctx, annot); + } + fz_catch(gctx) + { + if (text) + PySys_WriteStderr("%s (%i): '%s'\n", fz_caught_message(gctx), i, text); + else + PySys_WriteStderr("%s (%i)\n", fz_caught_message(gctx), i); + JM_Python_str_DelForPy3(text); + PyErr_Clear(); + } + } + fz_try(gctx) + { + pdf_dict_put_drop(gctx, page->obj, PDF_NAME(Annots), new_array); + refresh_link_table(gctx, page); + } + fz_catch(gctx) return NULL; + page->doc->dirty = 1; + return NONE; + } +SWIGINTERN PyObject *fz_page_s__getLinkXrefs(struct fz_page_s *self){ + pdf_obj *annots, *annots_arr, *link, *obj; + int i, lcount; + pdf_page *page = pdf_page_from_fz_page(gctx, self); + PyObject *linkxrefs = PyList_New(0); + if (!page) return linkxrefs; // empty list for non-PDF + annots = pdf_dict_get(gctx, page->obj, PDF_NAME(Annots)); + if (!annots) return linkxrefs; + if (pdf_is_indirect(gctx, annots)) + annots_arr = pdf_resolve_indirect(gctx, annots); + else + annots_arr = annots; + lcount = pdf_array_len(gctx, annots_arr); + for (i = 0; i < lcount; i++) + { + link = pdf_array_get(gctx, annots_arr, i); + obj = pdf_dict_get(gctx, link, PDF_NAME(Subtype)); + if (pdf_name_eq(gctx, obj, PDF_NAME(Link))) + PyList_Append(linkxrefs, Py_BuildValue("i", pdf_to_num(gctx, link))); + } + return linkxrefs; + } +SWIGINTERN PyObject *fz_page_s__cleanContents(struct fz_page_s *self){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_annot *annot; + fz_try(gctx) + { + assert_PDF(page); + pdf_clean_page_contents(gctx, page->doc, page, NULL, NULL, NULL, 1, 0); + for (annot = pdf_first_annot(gctx, page); annot != NULL; annot = pdf_next_annot(gctx, annot)) + { + pdf_clean_annot_contents(gctx, page->doc, annot, NULL, NULL, NULL, 1, 0); + } + } + fz_catch(gctx) return NULL; + page->doc->dirty = 1; + return NONE; + } +SWIGINTERN int fz_page_s__showPDFpage(struct fz_page_s *self,PyObject *rect,struct fz_document_s *docsrc,int pno,int overlay,int keep_proportion,int reuse_xref,PyObject *clip,struct pdf_graft_map_s *graftmap,char *_imgname){ + int xref; + xref = reuse_xref; + pdf_obj *xobj1, *xobj2, *resources, *o; + fz_buffer *res, *nres; + fz_rect mediabox; + fz_rect cropbox; + fz_rect rrect = JM_rect_from_py(rect); + fz_try(gctx) + { + pdf_page *tpage = pdf_page_from_fz_page(gctx, self); + assert_PDF(tpage); + if (fz_is_infinite_rect(rrect) || fz_is_empty_rect(rrect)) + THROWMSG("rect must be finite and not empty"); + pdf_obj *tpageref = tpage->obj; + pdf_document *pdfout = tpage->doc; // target PDF + pdf_document *pdfsrc = pdf_specifics(gctx, docsrc); + assert_PDF(pdfsrc); + //------------------------------------------------------------- + // make XObject of source page and get its xref + //------------------------------------------------------------- + xobj1 = JM_xobject_from_page(gctx, pdfout, pdfsrc, pno, + &mediabox, &cropbox, xref, graftmap); + xref = pdf_to_num(gctx, xobj1); -SWIGINTERN PyObject *_wrap_Document__getCharWidths(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 ; - int arg3 ; - int arg4 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; - int val4 ; - int ecode4 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO|O:Document__getCharWidths",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getCharWidths" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getCharWidths" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document__getCharWidths" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - if (obj3) { - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document__getCharWidths" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); - } - { - result = (PyObject *)fz_document_s__getCharWidths(arg1,arg2,arg3,arg4); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + //------------------------------------------------------------- + // Calculate /Matrix and /BBox of the referencing XObject + //------------------------------------------------------------- + fz_rect rclip = JM_rect_from_py(clip); + if (!fz_is_infinite_rect(rclip)) // set cropbox if clip given + { + cropbox.x0 = rclip.x0; + cropbox.y0 = mediabox.y1 - rclip.y1; + cropbox.x1 = rclip.x1; + cropbox.y1 = mediabox.y1 - rclip.y0; + } + fz_matrix mat = fz_identity; + fz_rect prect = fz_bound_page(gctx, self); + fz_rect r = fz_empty_rect; + o = pdf_dict_get(gctx, tpageref, PDF_NAME(CropBox)); + if (o) + { + r = pdf_to_rect(gctx, o); + prect.x0 = r.x0; + prect.y0 = r.y0; + } + o = pdf_dict_get(gctx, tpageref, PDF_NAME(MediaBox)); + + if (o) + { + r = pdf_to_rect(gctx, o); + prect.x1 = r.x1; + prect.y1 = r.y1; + } + + float W = rrect.x1 - rrect.x0; + float H = rrect.y1 - rrect.y0; + float fw = W / (cropbox.x1 - cropbox.x0); + float fh = H / (cropbox.y1 - cropbox.y0); + if ((fw < fh) && keep_proportion) // zoom factors in matrix + fh = fw; + float X = rrect.x0 + prect.x0 - fw*cropbox.x0; + float Y = prect.y1 - (rrect.y1 + prect.y0 + fh*cropbox.y0); + mat.a = fw; + mat.d = fh; + mat.e = X; + mat.f = Y; -SWIGINTERN PyObject *_wrap_Document__getPageObjNumber(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document__getPageObjNumber",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getPageObjNumber" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getPageObjNumber" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - { - result = (PyObject *)fz_document_s__getPageObjNumber(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + //------------------------------------------------------------- + // create referencing XObject (controls actual display) + //------------------------------------------------------------- + // fill reference to xobj1 into its /Resources + //------------------------------------------------------------- + pdf_obj *subres1 = pdf_new_dict(gctx, pdfout, 5); + pdf_dict_puts(gctx, subres1, "fullpage", xobj1); + pdf_obj *subres = pdf_new_dict(gctx, pdfout, 5); + pdf_dict_put_drop(gctx, subres, PDF_NAME(XObject), subres1); + res = fz_new_buffer(gctx, 50); + fz_append_string(gctx, res, "/fullpage Do"); -SWIGINTERN PyObject *_wrap_Document__getPageInfo(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 ; - int arg3 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO:Document__getPageInfo",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getPageInfo" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getPageInfo" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document__getPageInfo" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - { - result = (PyObject *)fz_document_s__getPageInfo(arg1,arg2,arg3); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + xobj2 = pdf_new_xobject(gctx, pdfout, cropbox, mat, subres, res); + pdf_drop_obj(gctx, subres); + fz_drop_buffer(gctx, res); -SWIGINTERN PyObject *_wrap_Document_extractFont(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 = (int) 0 ; - int arg3 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|OO:Document_extractFont",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_extractFont" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_extractFont" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - } - if (obj2) { - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_extractFont" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - } - { - result = (PyObject *)fz_document_s_extractFont(arg1,arg2,arg3); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + //------------------------------------------------------------- + // update target page: + //------------------------------------------------------------- + // 1. resources object + //------------------------------------------------------------- + resources = pdf_dict_get(gctx, tpageref, PDF_NAME(Resources)); + subres = pdf_dict_get(gctx, resources, PDF_NAME(XObject)); + if (!subres) // has no XObject dict yet: create one + { + subres = pdf_new_dict(gctx, pdfout, 10); + pdf_dict_put(gctx, resources, PDF_NAME(XObject), subres); + } + pdf_dict_puts(gctx, subres, _imgname, xobj2); -SWIGINTERN PyObject *_wrap_Document_extractImage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|O:Document_extractImage",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_extractImage" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_extractImage" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - } - { - result = (PyObject *)fz_document_s_extractImage(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + //------------------------------------------------------------- + // 2. contents object + //------------------------------------------------------------- + nres = fz_new_buffer(gctx, 50); // buffer for Do-command + fz_append_string(gctx, nres, " q /"); // Do-command + fz_append_string(gctx, nres, _imgname); + fz_append_string(gctx, nres, " Do Q "); + + JM_insert_contents(gctx, pdfout, tpageref, nres, overlay); + fz_drop_buffer(gctx, nres); + } + fz_catch(gctx) return -1; + return xref; + } +SWIGINTERN PyObject *fz_page_s_insertImage(struct fz_page_s *self,PyObject *rect,char const *filename,struct fz_pixmap_s *pixmap,PyObject *stream,int overlay,char *_imgname){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_document *pdf; + fz_pixmap *pm = NULL; + fz_pixmap *pix = NULL; + fz_image *mask = NULL; + fz_separations *seps = NULL; + pdf_obj *resources, *subres, *ref; + fz_buffer *res = NULL, *nres = NULL, *imgbuf = NULL; + + unsigned char *streamdata = NULL; + size_t streamlen = JM_CharFromBytesOrArray(stream, &streamdata); + + const char *template = " q %g 0 0 %g %g %g cm /%s Do Q "; + char *cont = NULL; + Py_ssize_t name_len = 0; + fz_image *zimg = NULL, *image = NULL; + int parm_count = 0; + if (filename) parm_count++; + if (pixmap) parm_count++; + if (streamlen > 0) parm_count++; + fz_try(gctx) + { + assert_PDF(page); + if (parm_count != 1) + THROWMSG("need exactly one of filename, pixmap or stream"); + + fz_rect rect_py = JM_rect_from_py(rect); + if (fz_is_empty_rect(rect_py) || fz_is_infinite_rect(rect_py)) + THROWMSG("rect must be finite and not empty"); + + // calculate coordinates for image matrix + fz_rect prect = fz_bound_page(gctx, self); // page rectangle + + fz_rect r = fz_empty_rect; // modify where necessary + pdf_obj *o = pdf_dict_get(gctx, page->obj, PDF_NAME(CropBox)); + if (o) + { // set top-left of page rect to new values + r = pdf_to_rect(gctx, o); + prect.x0 = r.x0; + prect.y0 = r.y0; + } + o = pdf_dict_get(gctx, page->obj, PDF_NAME(MediaBox)); + if (o) + { // set bottom-right to new values + r = pdf_to_rect(gctx, o); + prect.x1 = r.x1; + prect.y1 = r.y1; + } + // adjust rect.x0, rect.y0 by CropBox start + float X = rect_py.x0 + prect.x0; + float Y = prect.y1 - (rect_py.y1 + prect.y0); + float W = rect_py.x1 - rect_py.x0; + float H = rect_py.y1 - rect_py.y0; + pdf = page->doc; -SWIGINTERN PyObject *_wrap_Document__delToC(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__delToC",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__delToC" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (PyObject *)fz_document_s__delToC(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} + // get objects "Resources" & "XObject" + resources = pdf_dict_get(gctx, page->obj, PDF_NAME(Resources)); + subres = pdf_dict_get(gctx, resources, PDF_NAME(XObject)); + if (!subres) // has no XObject yet, create one + { + subres = pdf_new_dict(gctx, pdf, 10); + pdf_dict_put_drop(gctx, resources, PDF_NAME(XObject), subres); + } + // create the image + if (filename || streamlen > 0) + { + if (filename) + image = fz_new_image_from_file(gctx, filename); + else + { + imgbuf = fz_new_buffer_from_shared_data(gctx, + streamdata, streamlen); + image = fz_new_image_from_buffer(gctx, imgbuf); + } + pix = fz_get_pixmap_from_image(gctx, image, NULL, NULL, 0, 0); + if (pix->alpha == 1) + { // have alpha, therefore create a mask + pm = fz_convert_pixmap(gctx, pix, NULL, NULL, NULL, NULL, 1); + pm->alpha = 0; + pm->colorspace = fz_keep_colorspace(gctx, fz_device_gray(gctx)); + mask = fz_new_image_from_pixmap(gctx, pm, NULL); + zimg = fz_new_image_from_pixmap(gctx, pix, mask); + fz_drop_image(gctx, image); + image = zimg; + zimg = NULL; + } + } + if (pixmap) + { + if (pixmap->alpha == 0) + image = fz_new_image_from_pixmap(gctx, pixmap, NULL); + else + { // pixmap has alpha, therefore create a mask + pm = fz_convert_pixmap(gctx, pixmap, NULL, NULL, NULL, NULL, 1); + pm->alpha = 0; + pm->colorspace = fz_keep_colorspace(gctx, fz_device_gray(gctx)); + mask = fz_new_image_from_pixmap(gctx, pm, NULL); + image = fz_new_image_from_pixmap(gctx, pixmap, mask); + } + } + // put image in PDF + ref = pdf_add_image(gctx, pdf, image, 0); + pdf_dict_puts(gctx, subres, _imgname, ref); // store ref-name -SWIGINTERN PyObject *_wrap_Document_isFormPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_isFormPDF",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_isFormPDF" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (PyObject *)fz_document_s_isFormPDF(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} + // prep contents stream buffer with invoking command + nres = fz_new_buffer(gctx, 50); + fz_append_printf(gctx, nres, template, W, H, X, Y, _imgname); + JM_insert_contents(gctx, pdf, page->obj, nres, overlay); + fz_drop_buffer(gctx, nres); + } + fz_always(gctx) + { + fz_drop_image(gctx, image); + fz_drop_image(gctx, mask); + fz_drop_pixmap(gctx, pix); + fz_drop_pixmap(gctx, pm); + } + fz_catch(gctx) return NULL; + pdf->dirty = 1; + return NONE; + } +SWIGINTERN PyObject *fz_page_s_insertFont(struct fz_page_s *self,char const *fontname,char const *fontfile,PyObject *fontbuffer,int xref,int set_simple,int idx){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_document *pdf; + pdf_obj *resources, *fonts, *font_obj; + fz_font *font; + const char *data = NULL; + int size, ixref = 0; + PyObject *info; + fz_try(gctx) + { + assert_PDF(page); + pdf = page->doc; + // get objects "Resources", "Resources/Font" + resources = pdf_dict_get(gctx, page->obj, PDF_NAME(Resources)); + fonts = pdf_dict_get(gctx, resources, PDF_NAME(Font)); + int simple = 0; + if (!fonts) // page has no fonts yet + fonts = pdf_add_object_drop(gctx, pdf, pdf_new_dict(gctx, pdf, 1)); + + data = fz_lookup_base14_font(gctx, fontname, &size); + if (data) // base 14 font found + { + font = fz_new_font_from_memory(gctx, fontname, data, size, 0, 0); + font_obj = pdf_add_simple_font(gctx, pdf, font, PDF_SIMPLE_ENCODING_LATIN); + simple = 1; + } + else + { + if (!fontfile && !fontbuffer) THROWMSG("unknown PDF Base 14 font"); + if (fontfile) + { + font = fz_new_font_from_file(gctx, NULL, fontfile, idx, 0); + } + else + { + if (!PyBytes_Check(fontbuffer)) THROWMSG("fontbuffer must be bytes"); + data = PyBytes_AsString(fontbuffer); + size = PyBytes_Size(fontbuffer); + font = fz_new_font_from_memory(gctx, NULL, data, size, idx, 0); + } + if (set_simple == 0) + { + font_obj = pdf_add_cid_font(gctx, pdf, font); + simple = 0; + } + else + { + font_obj = pdf_add_simple_font(gctx, pdf, font, PDF_SIMPLE_ENCODING_LATIN); + simple = 2; + } + } + ixref = pdf_to_num(gctx, font_obj); + PyObject *name = PyString_FromString(fz_font_name(gctx, font)); + PyObject *exto; + if (simple != 1) + exto = PyString_FromString(fontextension(gctx, pdf, ixref)); + else + exto = PyString_FromString("n/a"); + PyObject *simpleo = JM_BOOL(simple); + PyObject *idxo = PyInt_FromLong((long) idx); + info = PyDict_New(); + PyDict_SetItemString(info, "name", name); + PyDict_SetItemString(info, "simple", simpleo); + PyDict_SetItemString(info, "ext", exto); + fz_drop_font(gctx, font); + // resources and fonts objects will contain named reference to font + pdf_dict_puts(gctx, fonts, fontname, font_obj); + pdf_dict_put(gctx, resources, PDF_NAME(Font), fonts); + } + fz_catch(gctx) return NULL; + pdf->dirty = 1; + return Py_BuildValue("[i, O]", ixref, info); + } +SWIGINTERN PyObject *fz_page_s__getContents(struct fz_page_s *self){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + PyObject *list = NULL; + pdf_obj *contents = NULL, *icont = NULL; + int i, xref; + fz_try(gctx) + { + assert_PDF(page); + list = PyList_New(0); + contents = pdf_dict_get(gctx, page->obj, PDF_NAME(Contents)); + if (pdf_is_array(gctx, contents)) + { for (i=0; i < pdf_array_len(gctx, contents); i++) + { + icont = pdf_array_get(gctx, contents, i); + xref = pdf_to_num(gctx, icont); + PyList_Append(list, PyInt_FromLong((long) xref)); + } + } + else + { + xref = pdf_to_num(gctx, contents); + PyList_Append(list, PyInt_FromLong((long) xref)); + } + } + fz_catch(gctx) return NULL; + return list; + } +SWIGINTERN PyObject *fz_page_s__setContents(struct fz_page_s *self,int xref){ + pdf_page *page = pdf_page_from_fz_page(gctx, self); + pdf_obj *contents = NULL; + + fz_try(gctx) + { + assert_PDF(page); + if (!INRANGE(xref, 1, pdf_xref_len(gctx, page->doc) - 1)) + THROWMSG("xref out of range"); + contents = pdf_new_indirect(gctx, page->doc, xref, 0); + if (!pdf_is_stream(gctx, contents)) + THROWMSG("xref is not a stream"); + pdf_dict_put_drop(gctx, page->obj, PDF_NAME(Contents), contents); + } + fz_catch(gctx) return NULL; + page->doc->dirty = 1; + return NONE; + } +SWIGINTERN void delete_fz_pixmap_s(struct fz_pixmap_s *self){ + DEBUGMSG1("pixmap"); + fz_drop_pixmap(gctx, self); + DEBUGMSG2; + } +SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_0(struct fz_colorspace_s *cs,PyObject *bbox,int alpha){ + fz_pixmap *pm = NULL; + fz_separations *seps = NULL; + fz_try(gctx) + pm = fz_new_pixmap_with_bbox(gctx, cs, JM_irect_from_py(bbox), seps, alpha); + fz_catch(gctx) return NULL; + return pm; + } +SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_1(struct fz_colorspace_s *cs,struct fz_pixmap_s *spix){ + fz_pixmap *pm = NULL; + fz_try(gctx) + { + if (!fz_pixmap_colorspace(gctx, spix)) + THROWMSG("cannot copy pixmap with NULL colorspace"); + pm = fz_convert_pixmap(gctx, spix, cs, NULL, NULL, NULL, 1); + } + fz_catch(gctx) return NULL; + return pm; + } +SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_2(struct fz_pixmap_s *spix,float w,float h,PyObject *clip){ + fz_pixmap *pm = NULL; + fz_try(gctx) + { + fz_irect bbox = JM_irect_from_py(clip); + if (!fz_is_infinite_irect(bbox)) + { + pm = fz_scale_pixmap(gctx, spix, spix->x, spix->y, w, h, &bbox); + } + else + pm = fz_scale_pixmap(gctx, spix, spix->x, spix->y, w, h, NULL); + } + fz_catch(gctx) return NULL; + return pm; + } +SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_3(struct fz_pixmap_s *spix,int alpha){ + fz_pixmap *pm = NULL; + int n, w, h, i; + fz_separations *seps = NULL; + fz_try(gctx) + { + if (!INRANGE(alpha, 0, 1)) + THROWMSG("illegal alpha value"); + fz_colorspace *cs = fz_pixmap_colorspace(gctx, spix); + if (!cs && !alpha) + THROWMSG("cannot drop alpha for 'NULL' colorspace"); + n = fz_pixmap_colorants(gctx, spix); + w = fz_pixmap_width(gctx, spix); + h = fz_pixmap_height(gctx, spix); + pm = fz_new_pixmap(gctx, cs, w, h, seps, alpha); + pm->x = spix->x; + pm->y = spix->y; + pm->xres = spix->xres; + pm->yres = spix->yres; + // copy samples data ------------------------------------------ + unsigned char *sptr = spix->samples; + unsigned char *tptr = pm->samples; + if (spix->alpha == pm->alpha) // identical samples --------- + memcpy(tptr, sptr, w * h * (n + alpha)); + else + { + for (i = 0; i < w * h; i++) + { + memcpy(tptr, sptr, n); + tptr += n; + if (pm->alpha) + { + tptr[0] = 255; + tptr++; + } + sptr += n + spix->alpha; + } + } + } + fz_catch(gctx) return NULL; + return pm; + } +SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_4(struct fz_colorspace_s *cs,int w,int h,PyObject *samples,int alpha){ + char *data = NULL; + int n = fz_colorspace_n(gctx, cs); + int stride = (n + alpha)*w; + fz_separations *seps = NULL; + fz_pixmap *pm = NULL; + size_t size = JM_CharFromBytesOrArray(samples, &data); + fz_try(gctx) + { + if (size < 1) THROWMSG("invalid arg type samples"); + if (stride * h != size) THROWMSG("invalid arg len samples"); + pm = fz_new_pixmap_with_data(gctx, cs, w, h, seps, alpha, stride, data); + } + fz_catch(gctx) return NULL; + return pm; + } +SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_5(char *filename){ + fz_image *img = NULL; + fz_pixmap *pm = NULL; + fz_try(gctx) { + if (!filename) THROWMSG("invalid argument type"); + img = fz_new_image_from_file(gctx, filename); + pm = fz_get_pixmap_from_image(gctx, img, NULL, NULL, NULL, NULL); + } + fz_always(gctx) fz_drop_image(gctx, img); + fz_catch(gctx) return NULL; + return pm; + } +SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_6(PyObject *imagedata){ + size_t size = 0; + char *streamdata; + fz_buffer *data = NULL; + fz_image *img = NULL; + fz_pixmap *pm = NULL; + fz_try(gctx) + { + size = JM_CharFromBytesOrArray(imagedata, &streamdata); + if (size < 1) THROWMSG("invalid argument type"); + data = fz_new_buffer_from_shared_data(gctx, + streamdata, size); + img = fz_new_image_from_buffer(gctx, data); + pm = fz_get_pixmap_from_image(gctx, img, NULL, NULL, NULL, NULL); + } + fz_always(gctx) + { + fz_drop_image(gctx, img); + } + fz_catch(gctx) return NULL; + return pm; + } +SWIGINTERN struct fz_pixmap_s *new_fz_pixmap_s__SWIG_7(struct fz_document_s *doc,int xref){ + fz_image *img = NULL; + fz_pixmap *pix = NULL; + pdf_obj *ref = NULL; + pdf_obj *type; + pdf_document *pdf = pdf_specifics(gctx, doc); + fz_try(gctx) + { + assert_PDF(pdf); + int xreflen = pdf_xref_len(gctx, pdf); + if (!INRANGE(xref, 1, xreflen-1)) + THROWMSG("xref out of range"); + ref = pdf_new_indirect(gctx, pdf, xref, 0); + type = pdf_dict_get(gctx, ref, PDF_NAME(Subtype)); + if (!pdf_name_eq(gctx, type, PDF_NAME(Image))) + THROWMSG("xref not an image"); + img = pdf_load_image(gctx, pdf, ref); + pix = fz_get_pixmap_from_image(gctx, img, NULL, NULL, NULL, NULL); + } + fz_always(gctx) + { + fz_drop_image(gctx, img); + pdf_drop_obj(gctx, ref); + } + fz_catch(gctx) + { + fz_drop_pixmap(gctx, pix); + return NULL; + } + return pix; + } +SWIGINTERN void fz_pixmap_s_shrink(struct fz_pixmap_s *self,int factor){ + if (factor < 1) + { + JM_Warning("ignoring shrink factor < 1"); + return; + } + fz_subsample_pixmap(gctx, self, factor); + } +SWIGINTERN void fz_pixmap_s_gammaWith(struct fz_pixmap_s *self,float gamma){ + if (!fz_pixmap_colorspace(gctx, self)) + { + JM_Warning("colorspace invalid for function"); + return; + } + fz_gamma_pixmap(gctx, self, gamma); + } +SWIGINTERN void fz_pixmap_s_tintWith(struct fz_pixmap_s *self,int red,int green,int blue){ + fz_tint_pixmap(gctx, self, red, green, blue); + } +SWIGINTERN void fz_pixmap_s_clearWith__SWIG_0(struct fz_pixmap_s *self){ + fz_clear_pixmap(gctx, self); + } +SWIGINTERN void fz_pixmap_s_clearWith__SWIG_1(struct fz_pixmap_s *self,int value){ + fz_clear_pixmap_with_value(gctx, self, value); + } +SWIGINTERN void fz_pixmap_s_clearWith__SWIG_2(struct fz_pixmap_s *self,int value,PyObject *bbox){ + fz_clear_pixmap_rect_with_value(gctx, self, value, JM_irect_from_py(bbox)); + } +SWIGINTERN PyObject *fz_pixmap_s_copyPixmap(struct fz_pixmap_s *self,struct fz_pixmap_s *src,PyObject *bbox){ + fz_try(gctx) + { + if (!fz_pixmap_colorspace(gctx, src)) + THROWMSG("cannot copy pixmap with NULL colorspace"); + fz_copy_pixmap_rect(gctx, self, src, JM_irect_from_py(bbox), NULL); + } + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN int fz_pixmap_s_stride(struct fz_pixmap_s *self){ + return fz_pixmap_stride(gctx, self); + } +SWIGINTERN int fz_pixmap_s_alpha(struct fz_pixmap_s *self){ + return self->alpha; + } +SWIGINTERN struct fz_colorspace_s *fz_pixmap_s_colorspace(struct fz_pixmap_s *self){ + return fz_pixmap_colorspace(gctx, self); + } +SWIGINTERN PyObject *fz_pixmap_s_irect(struct fz_pixmap_s *self){ + return JM_py_from_irect(fz_pixmap_bbox(gctx, self)); + } +SWIGINTERN int fz_pixmap_s_size(struct fz_pixmap_s *self){ + return (int) fz_pixmap_size(gctx, self); + } +SWIGINTERN PyObject *fz_pixmap_s_setAlpha(struct fz_pixmap_s *self,PyObject *alphavalues){ + fz_try(gctx) + { + if (self->alpha == 0) THROWMSG("pixmap has no alpha"); + int n = fz_pixmap_colorants(gctx, self); + int w = fz_pixmap_width(gctx, self); + int h = fz_pixmap_height(gctx, self); + int balen = w * h * (n+1); + unsigned char *data = NULL; + int data_len = 0; + if (alphavalues) + { + data_len = (int) JM_CharFromBytesOrArray(alphavalues, &data); + if (data_len && data_len < w * h) + THROWMSG("not enough alpha values"); + } + int i = 0, k = 0; + while (i < balen) + { + if (data_len) self->samples[i+n] = data[k]; + else self->samples[i+n] = 255; + i += n+1; + k += 1; + } + } + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN PyObject *fz_pixmap_s_getPNGData(struct fz_pixmap_s *self,int savealpha){ + struct fz_buffer_s *res = NULL; + fz_output *out = NULL; + PyObject *r; + if (savealpha != -1) JM_Warning("ignoring savealpha"); + fz_try(gctx) { + res = fz_new_buffer(gctx, 1024); + out = fz_new_output_with_buffer(gctx, res); + fz_write_pixmap_as_png(gctx, out, self); + r = JM_BinFromBuffer(gctx, res); + } + fz_always(gctx) + { + fz_drop_output(gctx, out); + fz_drop_buffer(gctx, res); + } + fz_catch(gctx) return NULL; + return r; + } +SWIGINTERN PyObject *fz_pixmap_s__writeIMG(struct fz_pixmap_s *self,char *filename,int format,int savealpha){ + if (savealpha != -1) JM_Warning("ignoring savealpha"); + fz_try(gctx) { + switch(format) + { + case(1): + fz_save_pixmap_as_png(gctx, self, filename); + break; + case(2): + fz_save_pixmap_as_pnm(gctx, self, filename); + break; + case(3): + fz_save_pixmap_as_pam(gctx, self, filename); + break; + case(4): + fz_save_pixmap_as_tga(gctx, self, filename); + break; + } + } + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN void fz_pixmap_s_invertIRect(struct fz_pixmap_s *self,PyObject *irect){ + if (!fz_pixmap_colorspace(gctx, self)) + { + JM_Warning("ignored for stencil pixmap"); + return; + } -SWIGINTERN PyObject *_wrap_Document_FormFonts(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document_FormFonts",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_FormFonts" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (PyObject *)fz_document_s_FormFonts(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} + fz_irect r = JM_irect_from_py(irect); + if (!fz_is_infinite_irect(r)) + fz_invert_pixmap_rect(gctx, self, r); + else + fz_invert_pixmap(gctx, self); + } +SWIGINTERN PyObject *fz_pixmap_s_samples(struct fz_pixmap_s *self){ + return PyBytes_FromStringAndSize((const char *)self->samples, (Py_ssize_t) (self->w)*(self->h)*(self->n)); + } + #define CS_RGB 1 + #define CS_GRAY 2 + #define CS_CMYK 3 -SWIGINTERN PyObject *_wrap_Document__addFormFont(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - char *arg2 = (char *) 0 ; - char *arg3 = (char *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int res2 ; - char *buf2 = 0 ; - int alloc2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO:Document__addFormFont",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__addFormFont" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document__addFormFont" "', argument " "2"" of type '" "char *""'"); - } - arg2 = (char *)(buf2); - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Document__addFormFont" "', argument " "3"" of type '" "char *""'"); - } - arg3 = (char *)(buf3); - { - result = (PyObject *)fz_document_s__addFormFont(arg1,arg2,arg3); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return resultobj; -fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return NULL; -} +SWIGINTERN void delete_fz_colorspace_s(struct fz_colorspace_s *self){ + DEBUGMSG1("colorspace"); + fz_drop_colorspace(gctx, self); + DEBUGMSG2; + } +SWIGINTERN struct fz_colorspace_s *new_fz_colorspace_s(int type){ + switch(type) { + case 2: + return fz_device_gray(gctx); + break; + case 3: + return fz_device_cmyk(gctx); + break; + case 1: + default: + return fz_device_rgb(gctx); + break; + } + } +SWIGINTERN int fz_colorspace_s_n(struct fz_colorspace_s *self){ + return fz_colorspace_n(gctx, self); + } +SWIGINTERN char const *fz_colorspace_s__name(struct fz_colorspace_s *self){ + return fz_colorspace_name(gctx, self); + } +SWIGINTERN struct DeviceWrapper *new_DeviceWrapper__SWIG_0(struct fz_pixmap_s *pm,PyObject *clip){ + struct DeviceWrapper *dw = NULL; + fz_try(gctx) { + dw = (struct DeviceWrapper *)calloc(1, sizeof(struct DeviceWrapper)); + fz_irect bbox = JM_irect_from_py(clip); + if (fz_is_infinite_irect(bbox)) + dw->device = fz_new_draw_device(gctx, fz_identity, pm); + else + dw->device = fz_new_draw_device_with_bbox(gctx, fz_identity, pm, &bbox); + } + fz_catch(gctx) return NULL; + return dw; + } +SWIGINTERN struct DeviceWrapper *new_DeviceWrapper__SWIG_1(struct fz_display_list_s *dl){ + struct DeviceWrapper *dw = NULL; + fz_try(gctx) { + dw = (struct DeviceWrapper *)calloc(1, sizeof(struct DeviceWrapper)); + dw->device = fz_new_list_device(gctx, dl); + dw->list = dl; + fz_keep_display_list(gctx, dl); + } + fz_catch(gctx) return NULL; + return dw; + } +SWIGINTERN struct DeviceWrapper *new_DeviceWrapper__SWIG_2(struct fz_stext_page_s *tp,int flags){ + struct DeviceWrapper *dw = NULL; + fz_try(gctx) { + dw = (struct DeviceWrapper *)calloc(1, sizeof(struct DeviceWrapper)); + fz_stext_options opts; + opts.flags = flags; + dw->device = fz_new_stext_device(gctx, tp, &opts); + } + fz_catch(gctx) return NULL; + return dw; + } +SWIGINTERN void delete_DeviceWrapper(struct DeviceWrapper *self){ + fz_display_list *list = self->list; + DEBUGMSG1("device"); + fz_close_device(gctx, self->device); + fz_drop_device(gctx, self->device); + DEBUGMSG2; + if(list) + { + DEBUGMSG1("display list after device"); + fz_drop_display_list(gctx, list); + DEBUGMSG2; + } + } +SWIGINTERN char *fz_outline_s_uri(struct fz_outline_s *self){ + return self->uri; + } +SWIGINTERN int fz_outline_s_isExternal(struct fz_outline_s *self){ + if (!self->uri) return 0; + return fz_is_external_link(gctx, self->uri); + } +SWIGINTERN void delete_fz_annot_s(struct fz_annot_s *self){ + DEBUGMSG1("annot"); + fz_drop_annot(gctx, self); + DEBUGMSG2; + } +SWIGINTERN PyObject *fz_annot_s_rect(struct fz_annot_s *self){ + fz_rect r = fz_bound_annot(gctx, self); + return JM_py_from_rect(r); + } +SWIGINTERN int fz_annot_s_xref(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if(!annot) return 0; + return pdf_to_num(gctx, annot->obj); + } +SWIGINTERN PyObject *fz_annot_s__getAP(struct fz_annot_s *self){ + PyObject *r = NONE; + fz_buffer *res = NULL; + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; + fz_try(gctx) + { + pdf_obj *ap = pdf_dict_getl(gctx, annot->obj, PDF_NAME(AP), + PDF_NAME(N), NULL); + + if (pdf_is_stream(gctx, ap)) res = pdf_load_stream(gctx, ap); + if (res) r = JM_BinFromBuffer(gctx, res); + } + fz_always(gctx) fz_drop_buffer(gctx, res); + fz_catch(gctx) return NONE; + return r; + } +SWIGINTERN PyObject *fz_annot_s__setAP(struct fz_annot_s *self,PyObject *ap,int rect){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + fz_buffer *res = NULL; + fz_var(res); + fz_try(gctx) + { + assert_PDF(annot); + pdf_obj *apobj = pdf_dict_getl(gctx, annot->obj, PDF_NAME(AP), + PDF_NAME(N), NULL); + if (!apobj) THROWMSG("annot has no /AP/N object"); + if (!pdf_is_stream(gctx, apobj)) + THROWMSG("/AP/N object is no stream"); + char *c = NULL; + size_t len = JM_CharFromBytesOrArray(ap, &c); + if (!c) THROWMSG("invalid /AP stream argument"); + res = fz_new_buffer_from_copied_data(gctx, c, strlen(c)); + JM_update_stream(gctx, annot->page->doc, apobj, res); + if (rect) + { + fz_rect bbox = pdf_dict_get_rect(gctx, annot->obj, PDF_NAME(Rect)); + pdf_dict_put_rect(gctx, apobj, PDF_NAME(BBox), bbox); + annot->ap = NULL; + } + } + fz_always(gctx) + fz_drop_buffer(gctx, res); + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN void fz_annot_s_setRect(struct fz_annot_s *self,PyObject *rect){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return; + fz_try(gctx) + { + pdf_set_annot_rect(gctx, annot, JM_rect_from_py(rect)); + } + fz_catch(gctx) {;} + return; + } +SWIGINTERN PyObject *fz_annot_s_vertices(struct fz_annot_s *self){ + PyObject *res, *list; + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; // not a PDF! + //---------------------------------------------------------------- + // The following objects occur in different annotation types. + // So we are sure that o != NULL occurs at most once. + // Every pair of floats is one point, that needs to be separately + // transformed with the page's transformation matrix. + //---------------------------------------------------------------- + pdf_obj *o = pdf_dict_get(gctx, annot->obj, PDF_NAME(Vertices)); + if (!o) o = pdf_dict_get(gctx, annot->obj, PDF_NAME(L)); + if (!o) o = pdf_dict_get(gctx, annot->obj, PDF_NAME(QuadPoints)); + if (!o) o = pdf_dict_gets(gctx, annot->obj, "CL"); + int i, j, n; + fz_point point; // point object to work with + fz_matrix page_ctm; // page transformation matrix + pdf_page_transform(gctx, annot->page, NULL, &page_ctm); -SWIGINTERN PyObject *_wrap_Document__getOLRootNumber(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__getOLRootNumber",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getOLRootNumber" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - { - result = (int)fz_document_s__getOLRootNumber(arg1); - if(result<0) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} + if (o) // anything found yet? + { + res = PyList_New(0); // create Python list + n = pdf_array_len(gctx, o); + for (i = 0; i < n; i += 2) + { + point.x = pdf_to_real(gctx, pdf_array_get(gctx, o, i)); + point.y = pdf_to_real(gctx, pdf_array_get(gctx, o, i+1)); + fz_transform_point(point, page_ctm); + PyList_Append(res, Py_BuildValue("ff", point.x, point.y)); + } + return res; + } + // nothing found so far - maybe an Ink annotation? + pdf_obj *il_o = pdf_dict_get(gctx, annot->obj, PDF_NAME(InkList)); + if (!il_o) return NONE; // no inkList + res = PyList_New(0); // create result list + fz_rect prect = pdf_bound_page(gctx, annot->page); + double h = prect.y1 - prect.y0; + n = pdf_array_len(gctx, il_o); + for (i = 0; i < n; i++) + { + list = PyList_New(0); + o = pdf_array_get(gctx, il_o, i); + int m = pdf_array_len(gctx, o); + for (j = 0; j < m; j += 2) + { + point.x = pdf_to_real(gctx, pdf_array_get(gctx, o, j)); + point.y = pdf_to_real(gctx, pdf_array_get(gctx, o, j+1)); + PyList_Append(list, Py_BuildValue("ff", point.x, h - point.y)); + } + PyList_Append(res, list); + Py_CLEAR(list); + } + return res; + } +SWIGINTERN PyObject *fz_annot_s_colors(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; + return JM_annot_colors(gctx, annot->obj); + } +SWIGINTERN PyObject *fz_annot_s_update(struct fz_annot_s *self,float fontsize,PyObject *text_color,PyObject *border_color,PyObject *fill_color,int rotate){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; + int type = pdf_annot_type(gctx, annot); + fz_try(gctx) + { + pdf_dirty_annot(gctx, annot); // enforce MuPDF /AP formatting + if (type == PDF_ANNOT_FREE_TEXT && rotate >= 0) + pdf_dict_put_int(gctx, annot->obj, PDF_NAME(Rotate), rotate); + pdf_update_annot(gctx, annot); + } + fz_catch(gctx) + { + PySys_WriteStderr("cannot update annot: '%s'\n", fz_caught_message(gctx)); + Py_RETURN_FALSE; + } + // check /AP object + pdf_obj *ap = pdf_dict_getl(gctx, annot->obj, PDF_NAME(AP), + PDF_NAME(N), NULL); + if (!ap) + { + PySys_WriteStderr("annot has no /AP onject!\n"); + Py_RETURN_FALSE; + } -SWIGINTERN PyObject *_wrap_Document__getNewXref(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__getNewXref",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getNewXref" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - { - result = (int)fz_document_s__getNewXref(arg1); - if(result<0) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} + // get opacity + pdf_obj *ca = pdf_dict_get(gctx, annot->obj, PDF_NAME(CA)); + if (!ca) // no opacity given + Py_RETURN_TRUE; + + pdf_obj *alp0 = pdf_new_dict(gctx, annot->page->doc, 2); + pdf_dict_put(gctx, alp0, PDF_NAME(CA), ca); + pdf_dict_put(gctx, alp0, PDF_NAME(ca), ca); + pdf_obj *extg = pdf_new_dict(gctx, annot->page->doc, 1); + pdf_dict_puts_drop(gctx, extg, "Alp0", alp0); + pdf_dict_putl_drop(gctx, ap, extg, PDF_NAME(Resources), + PDF_NAME(ExtGState), NULL); + pdf_dict_putl_drop(gctx, annot->obj, ap, PDF_NAME(AP), PDF_NAME(N), NULL); + annot->ap = NULL; + Py_RETURN_TRUE; + } +SWIGINTERN void fz_annot_s_setColors(struct fz_annot_s *self,PyObject *colors){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return; + if (!PyDict_Check(colors)) return; + if (pdf_annot_type(gctx, annot) == PDF_ANNOT_WIDGET) + { + JM_Warning("use 'updateWidget' to change form fields"); + return; + } + PyObject *ccol, *icol; + ccol = PyDict_GetItemString(colors, "stroke"); + icol = PyDict_GetItemString(colors, "fill"); + int i, n; + float col[4]; + n = 0; + if (ccol) + if (PySequence_Check(ccol)) + n = (int) PySequence_Size(ccol); + if (n>0) + { + for (i=0; i0) + { + if (!pdf_annot_has_interior_color(gctx, annot)) + { + JM_Warning("annot type has no fill color"); + return; + } + for (i=0; iobj, "LE"); + if (!o) return res; // no LE: empty dict + char *lstart = NULL; + char *lend = NULL; + if (pdf_is_name(gctx, o)) lstart = (char *) pdf_to_name(gctx, o); + else if (pdf_is_array(gctx, o)) + { + lstart = (char *) pdf_to_name(gctx, pdf_array_get(gctx, o, 0)); + if (pdf_array_len(gctx, o) > 1) + lend = (char *) pdf_to_name(gctx, pdf_array_get(gctx, o, 1)); + } + PyList_SetItem(res, 0, Py_BuildValue("i", JM_le_value(gctx, lstart))); + PyList_SetItem(res, 1, Py_BuildValue("i", JM_le_value(gctx, lend))); + return res; + } +SWIGINTERN void fz_annot_s_setLineEnds(struct fz_annot_s *self,int start,int end){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return; + if (pdf_annot_has_line_ending_styles(gctx, annot)) + pdf_set_annot_line_ending_styles(gctx, annot, start, end); + else + JM_Warning("annot type has no line ends"); + } +SWIGINTERN PyObject *fz_annot_s_type(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; // not a PDF + int type = pdf_annot_type(gctx, annot); + const char *c = pdf_string_from_annot_type(gctx, type); + pdf_obj *o = pdf_dict_gets(gctx, annot->obj, "IT"); + if (!o || !pdf_is_name(gctx, o)) + return Py_BuildValue("is", type, c); // no IT entry + const char *it = pdf_to_name(gctx, o); + return Py_BuildValue("iss", type, c, it); + } +SWIGINTERN float fz_annot_s_opacity(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return -1.0f; // not a PDF + pdf_obj *ca = pdf_dict_get(gctx, annot->obj, PDF_NAME(CA)); + if (pdf_is_number(gctx, ca)) + return pdf_to_real(gctx, ca); + return -1.0f; + } -SWIGINTERN PyObject *_wrap_Document__getXrefLength(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__getXrefLength",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getXrefLength" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - result = (int)fz_document_s__getXrefLength(arg1); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} + #define SWIG_From_double PyFloat_FromDouble -SWIGINTERN PyObject *_wrap_Document__getXmlMetadataXref(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__getXmlMetadataXref",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getXmlMetadataXref" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - { - result = (int)fz_document_s__getXmlMetadataXref(arg1); - if(result<0) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; +SWIGINTERNINLINE PyObject * +SWIG_From_float (float value) +{ + return SWIG_From_double (value); } +SWIGINTERN void fz_annot_s_setOpacity(struct fz_annot_s *self,float opacity){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return; // not a PDF + if (INRANGE(opacity, 0.0f, 1.0f)) + pdf_set_annot_opacity(gctx, annot, opacity); + else + pdf_set_annot_opacity(gctx, annot, 1.0f); + } +SWIGINTERN PyObject *fz_annot_s_widget_type(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + int wtype; + if (!annot) return NONE; // not a PDF -SWIGINTERN PyObject *_wrap_Document__delXmlMetadata(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Document__delXmlMetadata",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__delXmlMetadata" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - { - result = (PyObject *)fz_document_s__delXmlMetadata(arg1); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - + wtype = pdf_field_type(gctx, pdf_get_bound_document(gctx, annot->obj), annot->obj); + switch(wtype) + { + case(PDF_WIDGET_TYPE_PUSHBUTTON): + return Py_BuildValue("is", wtype, "PushButton"); + case(PDF_WIDGET_TYPE_CHECKBOX): + return Py_BuildValue("is", wtype, "CheckBox"); + case(PDF_WIDGET_TYPE_RADIOBUTTON): + return Py_BuildValue("is", wtype, "RadioButton"); + case(PDF_WIDGET_TYPE_TEXT): + return Py_BuildValue("is", wtype, "Text"); + case(PDF_WIDGET_TYPE_LISTBOX): + return Py_BuildValue("is", wtype, "ListBox"); + case(PDF_WIDGET_TYPE_COMBOBOX): + return Py_BuildValue("is", wtype, "ComboBox"); + case(PDF_WIDGET_TYPE_SIGNATURE): + return Py_BuildValue("is", wtype, "Signature"); + default: + return NONE; + } + } +SWIGINTERN PyObject *fz_annot_s_widget_value(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; // not a PDF + if (pdf_annot_type(gctx, annot) != PDF_ANNOT_WIDGET) + return NONE; + int wtype = pdf_field_type(gctx, pdf_get_bound_document(gctx, annot->obj), annot->obj); + switch(wtype) + { + case(PDF_WIDGET_TYPE_PUSHBUTTON): + return JM_pushbtn_state(gctx, annot); + case(PDF_WIDGET_TYPE_CHECKBOX): + return JM_checkbox_state(gctx, annot); + case(PDF_WIDGET_TYPE_RADIOBUTTON): + return JM_radiobtn_state(gctx, annot); + case(PDF_WIDGET_TYPE_TEXT): + return JM_text_value(gctx, annot); + case(PDF_WIDGET_TYPE_LISTBOX): + return JM_listbox_value(gctx, annot); + case(PDF_WIDGET_TYPE_COMBOBOX): + return JM_combobox_value(gctx, annot); + case(PDF_WIDGET_TYPE_SIGNATURE): + return JM_signature_value(gctx, annot); + default: + return NONE; + } + } +SWIGINTERN PyObject *fz_annot_s_widget_name(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; + if (pdf_annot_type(gctx, annot) != PDF_ANNOT_WIDGET) + return NONE; + return PyString_FromString(pdf_field_name(gctx, + pdf_get_bound_document(gctx, annot->obj), + annot->obj)); + } +SWIGINTERN PyObject *fz_annot_s_widget_choices(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; + if (pdf_annot_type(gctx, annot) != PDF_ANNOT_WIDGET) + return NONE; + return JM_choice_options(gctx, annot); + } +SWIGINTERN PyObject *fz_annot_s_fileInfo(struct fz_annot_s *self){ + PyObject *res = PyDict_New(); // create Python dict + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + char *filename = NULL; + char *desc = NULL; + int length = -1, size = -1; + pdf_obj *stream = NULL, *o = NULL, *fs = NULL; -SWIGINTERN PyObject *_wrap_Document__getObjectString(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - char *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document__getObjectString",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getObjectString" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getObjectString" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - { - result = (char *)fz_document_s__getObjectString(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_FromCharPtr((const char *)result); - return resultobj; -fail: - return NULL; -} + fz_try(gctx) + { + assert_PDF(annot); + int type = (int) pdf_annot_type(gctx, annot); + if (type != 16) + THROWMSG("not a file attachment annot"); + stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME(FS), + PDF_NAME(EF), PDF_NAME(F), NULL); + if (!stream) THROWMSG("bad PDF: file entry not found"); + } + fz_catch(gctx) return NULL; + fs = pdf_dict_get(gctx, annot->obj, PDF_NAME(FS)); -SWIGINTERN PyObject *_wrap_Document__getXrefStream(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document__getXrefStream",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getXrefStream" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getXrefStream" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - { - result = (PyObject *)fz_document_s__getXrefStream(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + o = pdf_dict_get(gctx, fs, PDF_NAME(UF)); + if (o) filename = (char *) pdf_to_text_string(gctx, o); + else + { + o = pdf_dict_get(gctx, fs, PDF_NAME(F)); + if (o) filename = (char *) pdf_to_text_string(gctx, o); + } + o = pdf_dict_get(gctx, fs, PDF_NAME(Desc)); + if (o) desc = (char *) pdf_to_text_string(gctx, o); -SWIGINTERN PyObject *_wrap_Document__updateObject(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 ; - char *arg3 = (char *) 0 ; - struct fz_page_s *arg4 = (struct fz_page_s *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - void *argp4 = 0 ; - int res4 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO|O:Document__updateObject",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__updateObject" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__updateObject" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Document__updateObject" "', argument " "3"" of type '" "char *""'"); - } - arg3 = (char *)(buf3); - if (obj3) { - res4 = SWIG_ConvertPtr(obj3, &argp4,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res4)) { - SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Document__updateObject" "', argument " "4"" of type '" "struct fz_page_s *""'"); - } - arg4 = (struct fz_page_s *)(argp4); - } - { - result = (PyObject *)fz_document_s__updateObject(arg1,arg2,arg3,arg4); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return resultobj; -fail: - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return NULL; -} + o = pdf_dict_get(gctx, stream, PDF_NAME(Length)); + if (o) length = pdf_to_int(gctx, o); + o = pdf_dict_getl(gctx, stream, PDF_NAME(Params), + PDF_NAME(Size), NULL); + if (o) size = pdf_to_int(gctx, o); -SWIGINTERN PyObject *_wrap_Document__updateStream(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - int arg2 = (int) 0 ; - PyObject *arg3 = (PyObject *) NULL ; - int arg4 = (int) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val4 ; - int ecode4 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|OOO:Document__updateStream",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__updateStream" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__updateStream" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - } - if (obj2) { - arg3 = obj2; - } - if (obj3) { - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document__updateStream" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); - } - { - result = (PyObject *)fz_document_s__updateStream(arg1,arg2,arg3,arg4); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + PyDict_SetItemString(res, "filename", JM_UNICODE(filename)); + PyDict_SetItemString(res, "desc", JM_UNICODE(desc)); + PyDict_SetItemString(res, "length", Py_BuildValue("i", length)); + PyDict_SetItemString(res, "size", Py_BuildValue("i", size)); + return res; + } +SWIGINTERN PyObject *fz_annot_s_fileGet(struct fz_annot_s *self){ + PyObject *res = NULL; + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + pdf_obj *stream = NULL; + fz_buffer *buf = NULL; + fz_var(buf); + fz_try(gctx) + { + assert_PDF(annot); + int type = (int) pdf_annot_type(gctx, annot); + if (type != 16) + THROWMSG("not a file attachment annot"); + stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME(FS), + PDF_NAME(EF), PDF_NAME(F), NULL); + if (!stream) THROWMSG("bad PDF: file entry not found"); + buf = pdf_load_stream(gctx, stream); + res = JM_BinFromBuffer(gctx, buf); + } + fz_always(gctx) fz_drop_buffer(gctx, buf); + fz_catch(gctx) return NULL; + return res; + } +SWIGINTERN PyObject *fz_annot_s_fileUpd(struct fz_annot_s *self,PyObject *buffer,char *filename,char *ufilename,char *desc){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + pdf_document *pdf = NULL; // to be filled in + char *data = NULL; // for new file content + fz_buffer *res = NULL; // for compressed content + pdf_obj *stream = NULL, *fs = NULL; + int64_t size = 0; + fz_try(gctx) + { + assert_PDF(annot); // must be a PDF + pdf = annot->page->doc; // this is the PDF + int type = (int) pdf_annot_type(gctx, annot); + if (type != 16) + THROWMSG("not a file attachment annot"); + stream = pdf_dict_getl(gctx, annot->obj, PDF_NAME(FS), + PDF_NAME(EF), PDF_NAME(F), NULL); + // the object for file content + if (!stream) THROWMSG("bad PDF: file entry not found"); + + fs = pdf_dict_get(gctx, annot->obj, PDF_NAME(FS)); + // file content is ignored if not bytes / bytearray + size = (int64_t) JM_CharFromBytesOrArray(buffer, &data); + if (size > 0) + { + pdf_obj *s = pdf_new_int(gctx, size); + pdf_dict_put(gctx, stream, PDF_NAME(Filter), + PDF_NAME(FlateDecode)); -SWIGINTERN PyObject *_wrap_Document__setMetadata(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; - char *arg2 = (char *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - int res2 ; - char *buf2 = 0 ; - int alloc2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Document__setMetadata",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__setMetadata" "', argument " "1"" of type '" "struct fz_document_s *""'"); - } - arg1 = (struct fz_document_s *)(argp1); - res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document__setMetadata" "', argument " "2"" of type '" "char *""'"); - } - arg2 = (char *)(buf2); - { - result = (PyObject *)fz_document_s__setMetadata(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return resultobj; -fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - return NULL; -} + pdf_dict_putl_drop(gctx, stream, s, + PDF_NAME(Params), PDF_NAME(Size), NULL); + res = JM_deflatebuf(gctx, data, size); + pdf_update_stream(gctx, pdf, stream, res, 1); + } + if (filename) // new filename given + { + pdf_dict_put_text_string(gctx, stream, PDF_NAME(F), filename); + pdf_dict_put_text_string(gctx, fs, PDF_NAME(F), filename); + } -SWIGINTERN PyObject *Document_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_document_s, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); -} + if (ufilename) + { + pdf_dict_put_text_string(gctx, stream, PDF_NAME(UF), filename); + pdf_dict_put_text_string(gctx, fs, PDF_NAME(UF), filename); + } -SWIGINTERN PyObject *_wrap_delete_Page(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:delete_Page",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, SWIG_POINTER_DISOWN | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Page" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - delete_fz_page_s(arg1); - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} + if (desc) // new description given + { + pdf_dict_put_text_string(gctx, stream, PDF_NAME(Desc), desc); + pdf_dict_put_text_string(gctx, fs, PDF_NAME(Desc), desc); + } + } + fz_always(gctx) + { + fz_drop_buffer(gctx, res); + } + fz_catch(gctx) return NULL; + pdf->dirty = 1; + return NONE; + } +SWIGINTERN PyObject *fz_annot_s_info(struct fz_annot_s *self){ + PyObject *res = PyDict_New(); + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return res; // not a PDF + pdf_obj *o; + char *c; + c = (char *) pdf_annot_contents(gctx, annot); + PyDict_SetItemString(res, "content", JM_UNICODE(c)); + o = pdf_dict_get(gctx, annot->obj, PDF_NAME(Name)); + c = (char *) pdf_to_name(gctx, o); + PyDict_SetItemString(res, "name", JM_UNICODE(c)); -SWIGINTERN PyObject *_wrap_Page_bound(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - struct fz_rect_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Page_bound",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_bound" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - result = (struct fz_rect_s *)fz_page_s_bound(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} + // Title, author + o = pdf_dict_get(gctx, annot->obj, PDF_NAME(T)); + c = (char *) pdf_to_text_string(gctx, o); + PyDict_SetItemString(res, "title", JM_UNICODE(c)); + // CreationDate + o = pdf_dict_gets(gctx, annot->obj, "CreationDate"); + c = (char *) pdf_to_text_string(gctx, o); + PyDict_SetItemString(res, "creationDate", JM_UNICODE(c)); -SWIGINTERN PyObject *_wrap_Page_run(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct DeviceWrapper *arg2 = (struct DeviceWrapper *) 0 ; - struct fz_matrix_s *arg3 = (struct fz_matrix_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - void *argp3 = 0 ; - int res3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"OOO:Page_run",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_run" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_DeviceWrapper, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_run" "', argument " "2"" of type '" "struct DeviceWrapper *""'"); - } - arg2 = (struct DeviceWrapper *)(argp2); - res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_run" "', argument " "3"" of type '" "struct fz_matrix_s const *""'"); - } - arg3 = (struct fz_matrix_s *)(argp3); - { - result = (int)fz_page_s_run(arg1,arg2,(struct fz_matrix_s const *)arg3); - if(result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} + // ModDate + o = pdf_dict_get(gctx, annot->obj, PDF_NAME(M)); + c = (char *) pdf_to_text_string(gctx, o); + PyDict_SetItemString(res, "modDate", JM_UNICODE(c)); + // Subj + o = pdf_dict_gets(gctx, annot->obj, "Subj"); + c = (char *) pdf_to_text_string(gctx, o); + PyDict_SetItemString(res, "subject", JM_UNICODE(c)); -SWIGINTERN PyObject *_wrap_Page_getSVGimage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_matrix_s *arg2 = (struct fz_matrix_s *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|O:Page_getSVGimage",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_getSVGimage" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - if (obj1) { - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_getSVGimage" "', argument " "2"" of type '" "struct fz_matrix_s *""'"); - } - arg2 = (struct fz_matrix_s *)(argp2); - } - { - result = (PyObject *)fz_page_s_getSVGimage(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} + return res; + } +SWIGINTERN PyObject *fz_annot_s_setInfo(struct fz_annot_s *self,PyObject *info){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + char *uc = NULL; + + // use this to indicate a 'markup' annot type + int is_markup = pdf_annot_has_author(gctx, annot); + fz_var(is_markup); + fz_var(annot); + fz_try(gctx) + { + assert_PDF(annot); + if (!PyDict_Check(info)) + THROWMSG("info not a dict"); + // contents + uc = JM_Python_str_AsChar(PyDict_GetItemString(info, "content")); + if (uc) + { + pdf_set_annot_contents(gctx, annot, uc); + JM_Python_str_DelForPy3(uc); + } -SWIGINTERN PyObject *_wrap_Page_addLineAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_point_s *arg2 = (struct fz_point_s *) 0 ; - struct fz_point_s *arg3 = (struct fz_point_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - void *argp3 = 0 ; - int res3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO:Page_addLineAnnot",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addLineAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addLineAnnot" "', argument " "2"" of type '" "struct fz_point_s *""'"); - } - arg2 = (struct fz_point_s *)(argp2); - res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_addLineAnnot" "', argument " "3"" of type '" "struct fz_point_s *""'"); - } - arg3 = (struct fz_point_s *)(argp3); - { - result = (struct fz_annot_s *)fz_page_s_addLineAnnot(arg1,arg2,arg3); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} + if (is_markup) + { + // title (= author) + uc = JM_Python_str_AsChar(PyDict_GetItemString(info, "title")); + if (uc) + { + pdf_set_annot_author(gctx, annot, uc); + JM_Python_str_DelForPy3(uc); + } + // creation date + uc = JM_Python_str_AsChar(PyDict_GetItemString(info, + "creationDate")); + if (uc) + { + pdf_dict_put_text_string(gctx, annot->obj, + PDF_NAME(CreationDate), uc); + JM_Python_str_DelForPy3(uc); + } -SWIGINTERN PyObject *_wrap_Page_addTextAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_point_s *arg2 = (struct fz_point_s *) 0 ; - char *arg3 = (char *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO:Page_addTextAnnot",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addTextAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addTextAnnot" "', argument " "2"" of type '" "struct fz_point_s *""'"); - } - arg2 = (struct fz_point_s *)(argp2); - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_addTextAnnot" "', argument " "3"" of type '" "char *""'"); - } - arg3 = (char *)(buf3); - { - result = (struct fz_annot_s *)fz_page_s_addTextAnnot(arg1,arg2,arg3); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return resultobj; -fail: - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return NULL; -} + // mod date + uc = JM_Python_str_AsChar(PyDict_GetItemString(info, "modDate")); + if (uc) + { + pdf_dict_put_text_string(gctx, annot->obj, + PDF_NAME(M), uc); + JM_Python_str_DelForPy3(uc); + } + // subject + uc = JM_Python_str_AsChar(PyDict_GetItemString(info, "subject")); + if (uc) + { + pdf_dict_puts_drop(gctx, annot->obj, "Subj", + pdf_new_text_string(gctx, uc)); + JM_Python_str_DelForPy3(uc); + } + } + } + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN PyObject *fz_annot_s_border(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; // not a PDF + return JM_annot_border(gctx, annot->obj); + } +SWIGINTERN PyObject *fz_annot_s_setBorder(struct fz_annot_s *self,PyObject *border){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (!annot) return NONE; // not a PDF + return JM_annot_set_border(gctx, border, annot->page->doc, annot->obj); + } +SWIGINTERN int fz_annot_s_flags(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (annot) return pdf_annot_flags(gctx, annot); + return -1; + } +SWIGINTERN PyObject *fz_annot_s__cleanContents(struct fz_annot_s *self){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + fz_try(gctx) + { + assert_PDF(annot); + pdf_clean_annot_contents(gctx, annot->page->doc, annot, + NULL, NULL, NULL, 1, 0); + } + fz_catch(gctx) return NULL; + pdf_dirty_annot(gctx, annot); + return NONE; + } +SWIGINTERN void fz_annot_s_setFlags(struct fz_annot_s *self,int flags){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + if (annot) + { + pdf_set_annot_flags(gctx, annot, flags); + pdf_dirty_annot(gctx, annot); + } + } +SWIGINTERN struct fz_annot_s *fz_annot_s_next(struct fz_annot_s *self){ + fz_annot *annot = fz_next_annot(gctx, self); + if (annot) + fz_keep_annot(gctx, annot); + return annot; + } +SWIGINTERN struct fz_pixmap_s *fz_annot_s_getPixmap(struct fz_annot_s *self,PyObject *matrix,struct fz_colorspace_s *colorspace,int alpha){ + fz_matrix ctm = JM_matrix_from_py(matrix); + struct fz_colorspace_s *cs = fz_device_rgb(gctx); + fz_pixmap *pix = NULL; + if (colorspace) cs = colorspace; -SWIGINTERN PyObject *_wrap_Page_addFileAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_point_s *arg2 = (struct fz_point_s *) 0 ; - PyObject *arg3 = (PyObject *) 0 ; - char *arg4 = (char *) 0 ; - char *arg5 = (char *) NULL ; - char *arg6 = (char *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int res4 ; - char *buf4 = 0 ; - int alloc4 = 0 ; - int res5 ; - char *buf5 = 0 ; - int alloc5 = 0 ; - int res6 ; - char *buf6 = 0 ; - int alloc6 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOOO|OO:Page_addFileAnnot",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addFileAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addFileAnnot" "', argument " "2"" of type '" "struct fz_point_s *""'"); - } - arg2 = (struct fz_point_s *)(argp2); - arg3 = obj2; - res4 = SWIG_AsCharPtrAndSize(obj3, &buf4, NULL, &alloc4); - if (!SWIG_IsOK(res4)) { - SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Page_addFileAnnot" "', argument " "4"" of type '" "char *""'"); - } - arg4 = (char *)(buf4); - if (obj4) { - res5 = SWIG_AsCharPtrAndSize(obj4, &buf5, NULL, &alloc5); - if (!SWIG_IsOK(res5)) { - SWIG_exception_fail(SWIG_ArgError(res5), "in method '" "Page_addFileAnnot" "', argument " "5"" of type '" "char *""'"); - } - arg5 = (char *)(buf5); - } - if (obj5) { - res6 = SWIG_AsCharPtrAndSize(obj5, &buf6, NULL, &alloc6); - if (!SWIG_IsOK(res6)) { - SWIG_exception_fail(SWIG_ArgError(res6), "in method '" "Page_addFileAnnot" "', argument " "6"" of type '" "char *""'"); - } - arg6 = (char *)(buf6); - } - { - result = (struct fz_annot_s *)fz_page_s_addFileAnnot(arg1,arg2,arg3,arg4,arg5,arg6); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); - if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); - if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); - return resultobj; -fail: - if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); - if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); - if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); - return NULL; -} + fz_try(gctx) + pix = fz_new_pixmap_from_annot(gctx, self, ctm, cs, alpha); + fz_catch(gctx) return NULL; + return pix; + } +SWIGINTERN PyObject *fz_annot_s__updateWidget(struct fz_annot_s *self,PyObject *Widget){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + pdf_document *pdf = pdf_get_bound_document(gctx, annot->obj); + fz_try(gctx) + { + int field_type = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, + "field_type")); + JM_set_widget_properties(gctx, annot, Widget, field_type); + } + fz_always(gctx) + { + JM_PyErr_Clear; + } + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN PyObject *fz_annot_s__getWidget(struct fz_annot_s *self,PyObject *Widget){ + pdf_annot *annot = pdf_annot_from_fz_annot(gctx, self); + pdf_document *pdf = annot->page->doc; + pdf_widget *tw = (pdf_widget *) annot; + Py_ssize_t i = 0, n = 0; + fz_try(gctx) + { + char *border_style = pdf_field_border_style(gctx, pdf, annot->obj); + PyObject_SetAttrString(Widget, "border_style", + Py_BuildValue("s", border_style)); + float border_width = pdf_to_real(gctx, pdf_dict_getl(gctx, annot->obj, + PDF_NAME(BS), PDF_NAME(W), NULL)); + if (border_width == 0.0f) border_width = 1.0f; + PyObject_SetAttrString(Widget, "border_width", + Py_BuildValue("f", border_width)); -SWIGINTERN PyObject *_wrap_Page_addStrikeoutAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Page_addStrikeoutAnnot",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addStrikeoutAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addStrikeoutAnnot" "', argument " "2"" of type '" "struct fz_rect_s *""'"); - } - arg2 = (struct fz_rect_s *)(argp2); - { - result = (struct fz_annot_s *)fz_page_s_addStrikeoutAnnot(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} + pdf_obj *dashes = pdf_dict_getl(gctx, annot->obj, + PDF_NAME(BS), PDF_NAME(D), NULL); + if (pdf_is_array(gctx, dashes)) + { + n = (Py_ssize_t) pdf_array_len(gctx, dashes); + PyObject *d = PyList_New(n); + for (i = 0; i < n; i++) + PyList_SetItem(d, i, Py_BuildValue("i", pdf_to_int(gctx, + pdf_array_get(gctx, dashes, (int) i)))); + PyObject_SetAttrString(Widget, "border_dashes", d); + Py_CLEAR(d); + } -SWIGINTERN PyObject *_wrap_Page_addUnderlineAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Page_addUnderlineAnnot",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addUnderlineAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addUnderlineAnnot" "', argument " "2"" of type '" "struct fz_rect_s *""'"); - } - arg2 = (struct fz_rect_s *)(argp2); - { - result = (struct fz_annot_s *)fz_page_s_addUnderlineAnnot(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} + int text_maxlen = pdf_to_int(gctx, pdf_dict_get_inheritable(gctx, annot->obj, PDF_NAME(MaxLen))); + PyObject_SetAttrString(Widget, "text_maxlen", + Py_BuildValue("i", text_maxlen)); + + // entry ignored for new / updated widgets + int text_type = pdf_text_widget_content_type(gctx, pdf, tw); + PyObject_SetAttrString(Widget, "text_type", + Py_BuildValue("i", text_type)); + + pdf_obj *bgcol = pdf_dict_getl(gctx, annot->obj, + PDF_NAME(MK), PDF_NAME(BG), NULL); + if (pdf_is_array(gctx, bgcol)) + { + n = (Py_ssize_t) pdf_array_len(gctx, bgcol); + PyObject *col = PyList_New(n); + for (i = 0; i < n; i++) + PyList_SetItem(col, i, Py_BuildValue("f", + pdf_to_real(gctx, pdf_array_get(gctx, bgcol, (int) i)))); + + PyObject_SetAttrString(Widget, "fill_color", col); + Py_CLEAR(col); + } + + pdf_obj *bccol = pdf_dict_getl(gctx, annot->obj, PDF_NAME(MK), PDF_NAME(BC), NULL); + + if (pdf_is_array(gctx, bccol)) + { + n = (Py_ssize_t) pdf_array_len(gctx, bccol); + PyObject *col = PyList_New(n); + for (i = 0; i < n; i++) + PyList_SetItem(col, i, Py_BuildValue("f", + pdf_to_real(gctx, pdf_array_get(gctx, bccol, (int) i)))); + + PyObject_SetAttrString(Widget, "border_color", col); + Py_CLEAR(col); + } + + char *da = pdf_to_str_buf(gctx, pdf_dict_get_inheritable(gctx, + annot->obj, PDF_NAME(DA))); + PyObject_SetAttrString(Widget, "_text_da", Py_BuildValue("s", da)); + + pdf_obj *ca = pdf_dict_getl(gctx, annot->obj, + PDF_NAME(MK), PDF_NAME(CA), NULL); + if (ca) + PyObject_SetAttrString(Widget, "button_caption", + JM_UNICODE(pdf_to_str_buf(gctx, ca))); + + int field_flags = pdf_get_field_flags(gctx, pdf, annot->obj); + PyObject_SetAttrString(Widget, "field_flags", + Py_BuildValue("i", field_flags)); + + // call Py method to reconstruct text color, font name, size + PyObject *call = PyObject_CallMethod(Widget, + "_parse_da", NULL); + Py_XDECREF(call); + + } + fz_always(gctx) PyErr_Clear(); + fz_catch(gctx) return NULL; + return NONE; + } +SWIGINTERN void delete_fz_link_s(struct fz_link_s *self){ + DEBUGMSG1("link"); + fz_drop_link(gctx, self); + DEBUGMSG2; + } +SWIGINTERN PyObject *fz_link_s__border(struct fz_link_s *self,struct fz_document_s *doc,int xref){ + pdf_document *pdf = pdf_specifics(gctx, doc); + if (!pdf) return NONE; + pdf_obj *link_obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!link_obj) return NONE; + PyObject *b = JM_annot_border(gctx, link_obj); + pdf_drop_obj(gctx, link_obj); + return b; + } +SWIGINTERN PyObject *fz_link_s__setBorder(struct fz_link_s *self,PyObject *border,struct fz_document_s *doc,int xref){ + pdf_document *pdf = pdf_specifics(gctx, doc); + if (!pdf) return NONE; + pdf_obj *link_obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!link_obj) return NONE; + PyObject *b = JM_annot_set_border(gctx, border, pdf, link_obj); + pdf_drop_obj(gctx, link_obj); + return b; + } +SWIGINTERN PyObject *fz_link_s__colors(struct fz_link_s *self,struct fz_document_s *doc,int xref){ + pdf_document *pdf = pdf_specifics(gctx, doc); + if (!pdf) return NONE; + pdf_obj *link_obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!link_obj) return NONE; + PyObject *b = JM_annot_colors(gctx, link_obj); + pdf_drop_obj(gctx, link_obj); + return b; + } +SWIGINTERN PyObject *fz_link_s__setColors(struct fz_link_s *self,PyObject *colors,struct fz_document_s *doc,int xref){ + pdf_document *pdf = pdf_specifics(gctx, doc); + pdf_obj *arr = NULL; + int i; + if (!pdf) return NONE; + if (!PyDict_Check(colors)) return NONE; + float scol[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + int nscol = 0; + float fcol[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + int nfcol = 0; + PyObject *stroke = PyDict_GetItemString(colors, "stroke"); + PyObject *fill = PyDict_GetItemString(colors, "fill"); + JM_color_FromSequence(stroke, &nscol, scol); + JM_color_FromSequence(fill, &nfcol, fcol); + if (!nscol && !nfcol) return NONE; + pdf_obj *link_obj = pdf_new_indirect(gctx, pdf, xref, 0); + if (!link_obj) return NONE; + if (nscol > 0) + { + arr = pdf_new_array(gctx, pdf, nscol); + for (i = 0; i < nscol; i++) + pdf_array_push_real(gctx, arr, scol[i]); + pdf_dict_put_drop(gctx, link_obj, PDF_NAME(C), arr); + } + if (nfcol > 0) JM_Warning("this annot type has no fill color)"); + pdf_drop_obj(gctx, link_obj); + return NONE; + } +SWIGINTERN char *fz_link_s_uri(struct fz_link_s *self){ + return self->uri; + } +SWIGINTERN int fz_link_s_isExternal(struct fz_link_s *self){ + if (!self->uri) return 0; + return fz_is_external_link(gctx, self->uri); + } +SWIGINTERN PyObject *fz_link_s_rect(struct fz_link_s *self){ + return JM_py_from_rect(self->rect); + } +SWIGINTERN struct fz_link_s *fz_link_s_next(struct fz_link_s *self){ + fz_keep_link(gctx, self->next); + return self->next; + } +SWIGINTERN void delete_fz_display_list_s(struct fz_display_list_s *self){ + DEBUGMSG1("display list"); + fz_drop_display_list(gctx, self); + DEBUGMSG2; + } +SWIGINTERN struct fz_display_list_s *new_fz_display_list_s(PyObject *mediabox){ + struct fz_display_list_s *dl = NULL; + fz_try(gctx) + dl = fz_new_display_list(gctx, JM_rect_from_py(mediabox)); + fz_catch(gctx) return NULL; + return dl; + } +SWIGINTERN int fz_display_list_s_run(struct fz_display_list_s *self,struct DeviceWrapper *dw,PyObject *m,PyObject *area){ + fz_try(gctx) + { + fz_run_display_list(gctx, self, dw->device, + JM_matrix_from_py(m), JM_rect_from_py(area), NULL); + } + fz_catch(gctx) return 1; + return 0; + } +SWIGINTERN PyObject *fz_display_list_s_rect(struct fz_display_list_s *self){ + return JM_py_from_rect(fz_bound_display_list(gctx, self)); + } +SWIGINTERN struct fz_pixmap_s *fz_display_list_s_getPixmap(struct fz_display_list_s *self,PyObject *matrix,struct fz_colorspace_s *colorspace,int alpha,PyObject *clip){ + struct fz_colorspace_s *cs = NULL; + fz_pixmap *pix = NULL; + + if (colorspace) cs = colorspace; + else cs = fz_device_rgb(gctx); + + fz_try(gctx) + { + pix = JM_pixmap_from_display_list(gctx, self, matrix, cs, alpha, clip); + } + fz_catch(gctx) return NULL; + return pix; + } +SWIGINTERN struct fz_stext_page_s *fz_display_list_s_getTextPage(struct fz_display_list_s *self,int flags){ + struct fz_stext_page_s *tp = NULL; + fz_try(gctx) + { + fz_stext_options stext_options = { 0 }; + stext_options.flags = flags; + tp = fz_new_stext_page_from_display_list(gctx, self, &stext_options); + } + fz_catch(gctx) return NULL; + return tp; + } +SWIGINTERN struct fz_stext_page_s *new_fz_stext_page_s(PyObject *mediabox){ + struct fz_stext_page_s *tp = NULL; + fz_try(gctx) + tp = fz_new_stext_page(gctx, JM_rect_from_py(mediabox)); + fz_catch(gctx) return NULL; + return tp; + } +SWIGINTERN void delete_fz_stext_page_s(struct fz_stext_page_s *self){ + DEBUGMSG1("text page"); + fz_drop_stext_page(gctx, self); + DEBUGMSG2; + } +SWIGINTERN PyObject *fz_stext_page_s_search(struct fz_stext_page_s *self,char const *needle,int hit_max,int quads){ + fz_quad *result = NULL; + PyObject *liste = PyList_New(0); + int i, mymax = hit_max; + if (mymax < 1) mymax = 16; + result = JM_Alloc(fz_quad, (mymax+1)); + struct fz_quad_s *quad = (struct fz_quad_s *) result; + int count = fz_search_stext_page(gctx, self, needle, result, hit_max); + for (i = 0; i < count; i++) + { + PyList_Append(liste, + Py_BuildValue("(ff),(ff),(ff),(ff)", + quad->ul.x, quad->ul.y, + quad->ur.x, quad->ur.y, + quad->ll.x, quad->ll.y, + quad->lr.x, quad->lr.y)); + quad += 1; + } + JM_Free(result); + return liste; + } +SWIGINTERN PyObject *fz_stext_page_s__extractTextBlocks_AsList(struct fz_stext_page_s *self){ + fz_stext_block *block; + fz_stext_line *line; + fz_stext_char *ch; + int block_n = 0; + PyObject *lines = PyList_New(0); + PyObject *text = NULL, *litem; + fz_buffer *res = NULL; + for (block = self->first_block; block; block = block->next) + { + fz_rect blockrect = block->bbox; + if (block->type == FZ_STEXT_BLOCK_TEXT) + { + fz_try(gctx) + { + res = fz_new_buffer(gctx, 1024); + int line_n = 0; + float last_y0 = 0.0; + for (line = block->u.t.first_line; line; line = line->next) + { + fz_rect linerect = line->bbox; + // append line no. 2 with new-line + if (line_n > 0) + { + if (linerect.y0 != last_y0) + fz_append_string(gctx, res, "\n"); + else + fz_append_string(gctx, res, " "); + } + last_y0 = linerect.y0; + line_n++; + for (ch = line->first_char; ch; ch = ch->next) + { + fz_append_rune(gctx, res, ch->c); + linerect = fz_union_rect(linerect, JM_char_bbox(line, ch)); + } + blockrect = fz_union_rect(blockrect, linerect); + } + text = JM_StrFromBuffer(gctx, res); + } + fz_always(gctx) + { + fz_drop_buffer(gctx, res); + res = NULL; + } + fz_catch(gctx) return NULL; + } + else + { + fz_image *img = block->u.i.image; + fz_colorspace *cs = img->colorspace; + text = PyUnicode_FromFormat("", fz_colorspace_name(gctx, cs), img->w, img->h, img->bpc); + blockrect = fz_union_rect(blockrect, block->bbox); + } + litem = Py_BuildValue("ffffOii", blockrect.x0, blockrect.y0, + blockrect.x1, blockrect.y1, + text, block_n, block->type); + PyList_Append(lines, litem); + Py_CLEAR(litem); + Py_CLEAR(text); + block_n++; + } + return lines; + } +SWIGINTERN PyObject *fz_stext_page_s__extractTextWords_AsList(struct fz_stext_page_s *self){ + fz_stext_block *block; + fz_stext_line *line; + fz_stext_char *ch; + fz_buffer *buff = NULL; + size_t buflen = 0; + int block_n = 0, line_n, word_n; + fz_rect wbbox = {0,0,0,0}; // word bbox + PyObject *lines = PyList_New(0); + for (block = self->first_block; block; block = block->next) + { + if (block->type != FZ_STEXT_BLOCK_TEXT) + { + block_n++; + continue; + } + line_n = 0; + for (line = block->u.t.first_line; line; line = line->next) + { + word_n = 0; // word counter per line + buff = NULL; // reset word buffer + buflen = 0; // reset char counter + for (ch = line->first_char; ch; ch = ch->next) + { + if (ch->c == 32 && buflen == 0) + continue; // skip spaces at line start + if (ch->c == 32) + { // --> finish the word + word_n = JM_append_word(gctx, lines, buff, &wbbox, + block_n, line_n, word_n); + fz_drop_buffer(gctx, buff); + buff = NULL; + buflen = 0; // reset char counter + continue; + } + // append one unicode character to the word + if (!buff) buff = fz_new_buffer(gctx, 64); + fz_append_rune(gctx, buff, ch->c); + buflen++; + // enlarge word bbox + wbbox = fz_union_rect(wbbox, JM_char_bbox(line, ch)); + } + if (buff) // store any remaining word + { + word_n = JM_append_word(gctx, lines, buff, &wbbox, + block_n, line_n, word_n); + fz_drop_buffer(gctx, buff); + buff = NULL; + buflen = 0; + } + line_n++; + } + block_n++; + } + return lines; + } +SWIGINTERN PyObject *fz_stext_page_s__extractText(struct fz_stext_page_s *self,int format){ + fz_buffer *res = NULL; + fz_output *out = NULL; + PyObject *text = NULL; + fz_var(res); + fz_var(out); + fz_try(gctx) + { + res = fz_new_buffer(gctx, 1024); + out = fz_new_output_with_buffer(gctx, res); + switch(format) + { + case(1): + fz_print_stext_page_as_html(gctx, out, self); + break; + case(2): + text = JM_stext_page_as_dict(gctx, self, 0); + break; + case(3): + fz_print_stext_page_as_xml(gctx, out, self); + break; + case(4): + fz_print_stext_page_as_xhtml(gctx, out, self); + break; + case(5): + text = JM_stext_page_as_dict(gctx, self, 0); + break; + case(6): + text = JM_stext_page_as_dict(gctx, self, 1); + break; + default: + JM_print_stext_page_as_text(gctx, out, self); + break; + } + if (!text) text = JM_StrFromBuffer(gctx, res); + + } + fz_always(gctx) + { + fz_drop_buffer(gctx, res); + fz_drop_output(gctx, out); + } + fz_catch(gctx) return NULL; + return text; + } +SWIGINTERN void delete_pdf_graft_map_s(struct pdf_graft_map_s *self){ + DEBUGMSG1("graftmap"); + pdf_drop_graft_map(gctx, self); + DEBUGMSG2; + } +SWIGINTERN struct pdf_graft_map_s *new_pdf_graft_map_s(struct fz_document_s *doc){ + pdf_graft_map *map = NULL; + fz_try(gctx) + { + pdf_document *dst = pdf_specifics(gctx, doc); + assert_PDF(dst); + map = pdf_new_graft_map(gctx, dst); + } + fz_catch(gctx) return NULL; + return map; + } +SWIGINTERN int Tools_gen_id(struct Tools *self){ + JM_UNIQUE_ID += 1; + if (JM_UNIQUE_ID < 0) JM_UNIQUE_ID = 1; + return JM_UNIQUE_ID; + } -SWIGINTERN PyObject *_wrap_Page_addHighlightAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Page_addHighlightAnnot",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addHighlightAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addHighlightAnnot" "', argument " "2"" of type '" "struct fz_rect_s *""'"); - } - arg2 = (struct fz_rect_s *)(argp2); - { - result = (struct fz_annot_s *)fz_page_s_addHighlightAnnot(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; +SWIGINTERN int +SWIG_AsVal_unsigned_SS_long (PyObject *obj, unsigned long *val) +{ +#if PY_VERSION_HEX < 0x03000000 + if (PyInt_Check(obj)) { + long v = PyInt_AsLong(obj); + if (v >= 0) { + if (val) *val = v; + return SWIG_OK; + } else { + return SWIG_OverflowError; } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Page_addRectAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Page_addRectAnnot",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addRectAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addRectAnnot" "', argument " "2"" of type '" "struct fz_rect_s *""'"); - } - arg2 = (struct fz_rect_s *)(argp2); - { - result = (struct fz_annot_s *)fz_page_s_addRectAnnot(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; + } else +#endif + if (PyLong_Check(obj)) { + unsigned long v = PyLong_AsUnsignedLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + return SWIG_OverflowError; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Page_addCircleAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Page_addCircleAnnot",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addCircleAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addCircleAnnot" "', argument " "2"" of type '" "struct fz_rect_s *""'"); - } - arg2 = (struct fz_rect_s *)(argp2); - { - result = (struct fz_annot_s *)fz_page_s_addCircleAnnot(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; +#ifdef SWIG_PYTHON_CAST_MODE + { + int dispatch = 0; + unsigned long v = PyLong_AsUnsignedLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_AddCast(SWIG_OK); + } else { + PyErr_Clear(); + } + if (!dispatch) { + double d; + int res = SWIG_AddCast(SWIG_AsVal_double (obj,&d)); + if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, 0, ULONG_MAX)) { + if (val) *val = (unsigned long)(d); + return res; + } } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; +#endif + return SWIG_TypeError; } -SWIGINTERN PyObject *_wrap_Page_addPolylineAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Page_addPolylineAnnot",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addPolylineAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - arg2 = obj1; - { - result = (struct fz_annot_s *)fz_page_s_addPolylineAnnot(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; +SWIGINTERN int +SWIG_AsVal_unsigned_SS_int (PyObject * obj, unsigned int *val) +{ + unsigned long v; + int res = SWIG_AsVal_unsigned_SS_long (obj, &v); + if (SWIG_IsOK(res)) { + if ((v > UINT_MAX)) { + return SWIG_OverflowError; + } else { + if (val) *val = (unsigned int)(v); } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; + } + return res; } +SWIGINTERN size_t Tools_store_shrink(struct Tools *self,unsigned int percent){ + if (percent >= 100) + { + fz_empty_store(gctx); + return 0; + } + if (percent > 0) fz_shrink_store(gctx, 100 - percent); + return gctx->store->size; + } + + #define SWIG_From_long PyInt_FromLong -SWIGINTERN PyObject *_wrap_Page_addPolygonAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Page_addPolygonAnnot",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addPolygonAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - arg2 = obj1; - { - result = (struct fz_annot_s *)fz_page_s_addPolygonAnnot(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; + +SWIGINTERNINLINE PyObject* +SWIG_From_unsigned_SS_long (unsigned long value) +{ + return (value > LONG_MAX) ? + PyLong_FromUnsignedLong(value) : PyInt_FromLong((long)(value)); } -SWIGINTERN PyObject *_wrap_Page_addFreetextAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_point_s *arg2 = (struct fz_point_s *) 0 ; - char *arg3 = (char *) 0 ; - float arg4 = (float) 11 ; - PyObject *arg5 = (PyObject *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - float val4 ; - int ecode4 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO|OO:Page_addFreetextAnnot",&obj0,&obj1,&obj2,&obj3,&obj4)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addFreetextAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_addFreetextAnnot" "', argument " "2"" of type '" "struct fz_point_s *""'"); - } - arg2 = (struct fz_point_s *)(argp2); - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_addFreetextAnnot" "', argument " "3"" of type '" "char *""'"); - } - arg3 = (char *)(buf3); - if (obj3) { - ecode4 = SWIG_AsVal_float(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Page_addFreetextAnnot" "', argument " "4"" of type '" "float""'"); - } - arg4 = (float)(val4); - } - if (obj4) { - arg5 = obj4; - } - { - result = (struct fz_annot_s *)fz_page_s_addFreetextAnnot(arg1,arg2,arg3,arg4,arg5); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return resultobj; -fail: - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return NULL; +#ifdef SWIG_LONG_LONG_AVAILABLE +SWIGINTERNINLINE PyObject* +SWIG_From_unsigned_SS_long_SS_long (unsigned long long value) +{ + return (value > LONG_MAX) ? + PyLong_FromUnsignedLongLong(value) : PyInt_FromLong((long)(value)); } +#endif -SWIGINTERN PyObject *_wrap_Page__addWidget(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Page__addWidget",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__addWidget" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - arg2 = obj1; - { - result = (struct fz_annot_s *)fz_page_s__addWidget(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } +SWIGINTERNINLINE PyObject * +SWIG_From_size_t (size_t value) +{ +#ifdef SWIG_LONG_LONG_AVAILABLE + if (sizeof(size_t) <= sizeof(unsigned long)) { +#endif + return SWIG_From_unsigned_SS_long ((unsigned long)(value)); +#ifdef SWIG_LONG_LONG_AVAILABLE + } else { + /* assume sizeof(size_t) <= sizeof(unsigned long long) */ + return SWIG_From_unsigned_SS_long_SS_long ((unsigned long long)(value)); } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; +#endif } - -SWIGINTERN PyObject *_wrap_Page_getDisplayList(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN size_t Tools_store_size(struct Tools *self){ + return gctx->store->size; + } +SWIGINTERN size_t Tools_store_maxsize(struct Tools *self){ + return gctx->store->max; + } +SWIGINTERN PyObject *Tools_fitz_config(struct Tools *self){ + return JM_fitz_config(); + } +SWIGINTERN void Tools_glyph_cache_empty(struct Tools *self){ + fz_purge_glyph_cache(gctx); + } +SWIGINTERN PyObject *Tools_fitz_stderr(struct Tools *self){ + return PyUnicode_Join(Py_BuildValue("s", ""), JM_error_log); + } +SWIGINTERN void Tools_fitz_stderr_reset(struct Tools *self){ + Py_CLEAR(JM_error_log); + JM_error_log = PyList_New(0); + } +SWIGINTERN PyObject *Tools_transform_rect(struct Tools *self,PyObject *rect,PyObject *matrix){ + return JM_py_from_rect(fz_transform_rect(JM_rect_from_py(rect), JM_matrix_from_py(matrix))); + } +SWIGINTERN PyObject *Tools_invert_matrix(struct Tools *self,PyObject *matrix){ + fz_matrix src = JM_matrix_from_py(matrix); + float a = src.a; + float det = a * src.d - src.b * src.c; + if (det < -JM_EPS || det > JM_EPS) + { + fz_matrix dst; + float rdet = 1 / det; + dst.a = src.d * rdet; + dst.b = -src.b * rdet; + dst.c = -src.c * rdet; + dst.d = a * rdet; + a = -src.e * dst.a - src.f * dst.c; + dst.f = -src.e * dst.b - src.f * dst.d; + dst.e = a; + return JM_py_from_matrix(dst); + } + return Py_BuildValue("ffffff", 0,0,0,0,0,0); + } +#ifdef __cplusplus +extern "C" { +#endif +SWIGINTERN PyObject *_wrap_delete_Document(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - struct fz_display_list_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Page_getDisplayList",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_getDisplayList" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - { - result = (struct fz_display_list_s *)fz_page_s_getDisplayList(arg1); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:delete_Document",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, SWIG_POINTER_DISOWN | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Document" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_display_list_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + delete_fz_document_s(arg1); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Page_setCropBox(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Document(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) NULL ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; + char *arg1 = (char *) NULL ; + PyObject *arg2 = (PyObject *) NULL ; + char *arg3 = (char *) NULL ; + PyObject *arg4 = (PyObject *) NULL ; + float arg5 = (float) 0 ; + float arg6 = (float) 0 ; + float arg7 = (float) 11 ; + int res1 ; + char *buf1 = 0 ; + int alloc1 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; + float val5 ; + int ecode5 = 0 ; + float val6 ; + int ecode6 = 0 ; + float val7 ; + int ecode7 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject *result = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + struct fz_document_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O|O:Page_setCropBox",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_setCropBox" "', argument " "1"" of type '" "struct fz_page_s *""'"); + if (!PyArg_ParseTuple(args,(char *)"|OOOOOOO:new_Document",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; + if (obj0) { + res1 = SWIG_AsCharPtrAndSize(obj0, &buf1, NULL, &alloc1); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Document" "', argument " "1"" of type '" "char const *""'"); + } + arg1 = (char *)(buf1); } - arg1 = (struct fz_page_s *)(argp1); if (obj1) { - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_setCropBox" "', argument " "2"" of type '" "struct fz_rect_s *""'"); + arg2 = obj1; + } + if (obj2) { + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "new_Document" "', argument " "3"" of type '" "char const *""'"); } - arg2 = (struct fz_rect_s *)(argp2); + arg3 = (char *)(buf3); + } + if (obj3) { + arg4 = obj3; + } + if (obj4) { + ecode5 = SWIG_AsVal_float(obj4, &val5); + if (!SWIG_IsOK(ecode5)) { + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "new_Document" "', argument " "5"" of type '" "float""'"); + } + arg5 = (float)(val5); + } + if (obj5) { + ecode6 = SWIG_AsVal_float(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "new_Document" "', argument " "6"" of type '" "float""'"); + } + arg6 = (float)(val6); + } + if (obj6) { + ecode7 = SWIG_AsVal_float(obj6, &val7); + if (!SWIG_IsOK(ecode7)) { + SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "new_Document" "', argument " "7"" of type '" "float""'"); + } + arg7 = (float)(val7); } { - result = (PyObject *)fz_page_s_setCropBox(arg1,arg2); + result = (struct fz_document_s *)new_fz_document_s((char const *)arg1,arg2,(char const *)arg3,arg4,arg5,arg6,arg7); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = result; + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_document_s, SWIG_POINTER_NEW | 0 ); + if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return resultobj; fail: + if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return NULL; } -SWIGINTERN PyObject *_wrap_Page_loadLinks(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_close(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_link_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Page_loadLinks",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document_close",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_loadLinks" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_close" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - result = (struct fz_link_s *)fz_page_s_loadLinks(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_link_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + fz_document_s_close(arg1); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Page_firstAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_loadPage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; - struct fz_annot_s *result = 0 ; + PyObject * obj1 = 0 ; + struct fz_page_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Page_firstAnnot",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|O:Document_loadPage",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_firstAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_loadPage" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - result = (struct fz_annot_s *)fz_page_s_firstAnnot(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_loadPage" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + } + { + result = (struct fz_page_s *)fz_document_s_loadPage(arg1,arg2); + if(result==NULL) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_page_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Page_deleteLink(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__loadOutline(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; + struct fz_outline_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Page_deleteLink",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document__loadOutline",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_deleteLink" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__loadOutline" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - arg2 = obj1; - fz_page_s_deleteLink(arg1,arg2); - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_document_s *)(argp1); + result = (struct fz_outline_s *)fz_document_s__loadOutline(arg1); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_outline_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Page_deleteAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__dropOutline(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_annot_s *arg2 = (struct fz_annot_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + struct fz_outline_s *arg2 = (struct fz_outline_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; void *argp2 = 0 ; int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Page_deleteAnnot",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document__dropOutline",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_deleteAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__dropOutline" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_annot_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_outline_s, 0 | 0 ); if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_deleteAnnot" "', argument " "2"" of type '" "struct fz_annot_s *""'"); - } - arg2 = (struct fz_annot_s *)(argp2); - result = (struct fz_annot_s *)fz_page_s_deleteAnnot(arg1,arg2); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Page_MediaBoxSize(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - struct fz_point_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Page_MediaBoxSize",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_MediaBoxSize" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document__dropOutline" "', argument " "2"" of type '" "struct fz_outline_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - result = (struct fz_point_s *)fz_page_s_MediaBoxSize(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_point_s, 0 | 0 ); + arg2 = (struct fz_outline_s *)(argp2); + fz_document_s__dropOutline(arg1,arg2); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Page_CropBoxPosition(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_embeddedFileCount(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_point_s *result = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"O:Page_CropBoxPosition",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document_embeddedFileCount",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_CropBoxPosition" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileCount" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - result = (struct fz_point_s *)fz_page_s_CropBoxPosition(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_point_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + result = (int)fz_document_s_embeddedFileCount(arg1); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Page_rotation(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_embeddedFileDel(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + char *arg2 = (char *) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; int result; - if (!PyArg_ParseTuple(args,(char *)"O:Page_rotation",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document_embeddedFileDel",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_rotation" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileDel" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_embeddedFileDel" "', argument " "2"" of type '" "char *""'"); + } + arg2 = (char *)(buf2); + { + result = (int)fz_document_s_embeddedFileDel(arg1,arg2); + if(result < 1) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg1 = (struct fz_page_s *)(argp1); - result = (int)fz_page_s_rotation(arg1); resultobj = SWIG_From_int((int)(result)); + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } -SWIGINTERN PyObject *_wrap_Page_setRotation(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_embeddedFileInfo(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - int arg2 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Page_setRotation",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document_embeddedFileInfo",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_setRotation" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileInfo" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Page_setRotation" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); + arg1 = (struct fz_document_s *)(argp1); + arg2 = obj1; { - result = (PyObject *)fz_page_s_setRotation(arg1,arg2); + result = (PyObject *)fz_document_s_embeddedFileInfo(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -13604,25 +13699,66 @@ SWIGINTERN PyObject *_wrap_Page_setRotation(PyObject *SWIGUNUSEDPARM(self), PyOb } -SWIGINTERN PyObject *_wrap_Page__addAnnot_FromString(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_embeddedFileUpd(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; PyObject *arg2 = (PyObject *) 0 ; + PyObject *arg3 = (PyObject *) NULL ; + char *arg4 = (char *) NULL ; + char *arg5 = (char *) NULL ; + char *arg6 = (char *) NULL ; void *argp1 = 0 ; int res1 = 0 ; + int res4 ; + char *buf4 = 0 ; + int alloc4 = 0 ; + int res5 ; + char *buf5 = 0 ; + int alloc5 = 0 ; + int res6 ; + char *buf6 = 0 ; + int alloc6 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Page__addAnnot_FromString",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO|OOOO:Document_embeddedFileUpd",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__addAnnot_FromString" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileUpd" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); + arg1 = (struct fz_document_s *)(argp1); arg2 = obj1; + if (obj2) { + arg3 = obj2; + } + if (obj3) { + res4 = SWIG_AsCharPtrAndSize(obj3, &buf4, NULL, &alloc4); + if (!SWIG_IsOK(res4)) { + SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Document_embeddedFileUpd" "', argument " "4"" of type '" "char *""'"); + } + arg4 = (char *)(buf4); + } + if (obj4) { + res5 = SWIG_AsCharPtrAndSize(obj4, &buf5, NULL, &alloc5); + if (!SWIG_IsOK(res5)) { + SWIG_exception_fail(SWIG_ArgError(res5), "in method '" "Document_embeddedFileUpd" "', argument " "5"" of type '" "char *""'"); + } + arg5 = (char *)(buf5); + } + if (obj5) { + res6 = SWIG_AsCharPtrAndSize(obj5, &buf6, NULL, &alloc6); + if (!SWIG_IsOK(res6)) { + SWIG_exception_fail(SWIG_ArgError(res6), "in method '" "Document_embeddedFileUpd" "', argument " "6"" of type '" "char *""'"); + } + arg6 = (char *)(buf6); + } { - result = (PyObject *)fz_page_s__addAnnot_FromString(arg1,arg2); + result = (PyObject *)fz_document_s_embeddedFileUpd(arg1,arg2,arg3,arg4,arg5,arg6); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -13630,50 +13766,37 @@ SWIGINTERN PyObject *_wrap_Page__addAnnot_FromString(PyObject *SWIGUNUSEDPARM(se } } resultobj = result; + if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); + if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); + if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); return resultobj; fail: + if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); + if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); + if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); return NULL; } -SWIGINTERN PyObject *_wrap_Page__getLinkXrefs(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Page__getLinkXrefs",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__getLinkXrefs" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - result = (PyObject *)fz_page_s__getLinkXrefs(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Page__cleanContents(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_embeddedFileGet(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Page__cleanContents",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document_embeddedFileGet",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__cleanContents" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileGet" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); + arg1 = (struct fz_document_s *)(argp1); + arg2 = obj1; { - result = (PyObject *)fz_page_s__cleanContents(arg1); + result = (PyObject *)fz_document_s_embeddedFileGet(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -13687,209 +13810,141 @@ SWIGINTERN PyObject *_wrap_Page__cleanContents(PyObject *SWIGUNUSEDPARM(self), P } -SWIGINTERN PyObject *_wrap_Page__showPDFpage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_embeddedFileAdd(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; - struct fz_document_s *arg3 = (struct fz_document_s *) 0 ; - int arg4 = (int) 0 ; - int arg5 = (int) 1 ; - int arg6 = (int) 1 ; - int arg7 = (int) 0 ; - struct fz_rect_s *arg8 = (struct fz_rect_s *) NULL ; - struct pdf_graft_map_s *arg9 = (struct pdf_graft_map_s *) NULL ; - char *arg10 = (char *) NULL ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + char *arg3 = (char *) 0 ; + char *arg4 = (char *) NULL ; + char *arg5 = (char *) NULL ; + char *arg6 = (char *) NULL ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - void *argp3 = 0 ; - int res3 = 0 ; - int val4 ; - int ecode4 = 0 ; - int val5 ; - int ecode5 = 0 ; - int val6 ; - int ecode6 = 0 ; - int val7 ; - int ecode7 = 0 ; - void *argp8 = 0 ; - int res8 = 0 ; - void *argp9 = 0 ; - int res9 = 0 ; - int res10 ; - char *buf10 = 0 ; - int alloc10 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; + int res4 ; + char *buf4 = 0 ; + int alloc4 = 0 ; + int res5 ; + char *buf5 = 0 ; + int alloc5 = 0 ; + int res6 ; + char *buf6 = 0 ; + int alloc6 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; PyObject * obj3 = 0 ; PyObject * obj4 = 0 ; PyObject * obj5 = 0 ; - PyObject * obj6 = 0 ; - PyObject * obj7 = 0 ; - PyObject * obj8 = 0 ; - PyObject * obj9 = 0 ; - int result; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO|OOOOOOO:Page__showPDFpage",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6,&obj7,&obj8,&obj9)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO|OOO:Document_embeddedFileAdd",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__showPDFpage" "', argument " "1"" of type '" "struct fz_page_s *""'"); - } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page__showPDFpage" "', argument " "2"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_embeddedFileAdd" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg2 = (struct fz_rect_s *)(argp2); - res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_document_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + arg2 = obj1; + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page__showPDFpage" "', argument " "3"" of type '" "struct fz_document_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Document_embeddedFileAdd" "', argument " "3"" of type '" "char const *""'"); } - arg3 = (struct fz_document_s *)(argp3); + arg3 = (char *)(buf3); if (obj3) { - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Page__showPDFpage" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); - } - if (obj4) { - ecode5 = SWIG_AsVal_int(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Page__showPDFpage" "', argument " "5"" of type '" "int""'"); - } - arg5 = (int)(val5); - } - if (obj5) { - ecode6 = SWIG_AsVal_int(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Page__showPDFpage" "', argument " "6"" of type '" "int""'"); - } - arg6 = (int)(val6); - } - if (obj6) { - ecode7 = SWIG_AsVal_int(obj6, &val7); - if (!SWIG_IsOK(ecode7)) { - SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Page__showPDFpage" "', argument " "7"" of type '" "int""'"); - } - arg7 = (int)(val7); - } - if (obj7) { - res8 = SWIG_ConvertPtr(obj7, &argp8,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res8)) { - SWIG_exception_fail(SWIG_ArgError(res8), "in method '" "Page__showPDFpage" "', argument " "8"" of type '" "struct fz_rect_s *""'"); + res4 = SWIG_AsCharPtrAndSize(obj3, &buf4, NULL, &alloc4); + if (!SWIG_IsOK(res4)) { + SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Document_embeddedFileAdd" "', argument " "4"" of type '" "char *""'"); } - arg8 = (struct fz_rect_s *)(argp8); + arg4 = (char *)(buf4); } - if (obj8) { - res9 = SWIG_ConvertPtr(obj8, &argp9,SWIGTYPE_p_pdf_graft_map_s, 0 | 0 ); - if (!SWIG_IsOK(res9)) { - SWIG_exception_fail(SWIG_ArgError(res9), "in method '" "Page__showPDFpage" "', argument " "9"" of type '" "struct pdf_graft_map_s *""'"); + if (obj4) { + res5 = SWIG_AsCharPtrAndSize(obj4, &buf5, NULL, &alloc5); + if (!SWIG_IsOK(res5)) { + SWIG_exception_fail(SWIG_ArgError(res5), "in method '" "Document_embeddedFileAdd" "', argument " "5"" of type '" "char *""'"); } - arg9 = (struct pdf_graft_map_s *)(argp9); + arg5 = (char *)(buf5); } - if (obj9) { - res10 = SWIG_AsCharPtrAndSize(obj9, &buf10, NULL, &alloc10); - if (!SWIG_IsOK(res10)) { - SWIG_exception_fail(SWIG_ArgError(res10), "in method '" "Page__showPDFpage" "', argument " "10"" of type '" "char *""'"); + if (obj5) { + res6 = SWIG_AsCharPtrAndSize(obj5, &buf6, NULL, &alloc6); + if (!SWIG_IsOK(res6)) { + SWIG_exception_fail(SWIG_ArgError(res6), "in method '" "Document_embeddedFileAdd" "', argument " "6"" of type '" "char *""'"); } - arg10 = (char *)(buf10); + arg6 = (char *)(buf6); } { - result = (int)fz_page_s__showPDFpage(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10); - if(result<0) + result = (PyObject *)fz_document_s_embeddedFileAdd(arg1,arg2,(char const *)arg3,arg4,arg5,arg6); + if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_From_int((int)(result)); - if (alloc10 == SWIG_NEWOBJ) free((char*)buf10); + resultobj = result; + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); + if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); + if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); return resultobj; fail: - if (alloc10 == SWIG_NEWOBJ) free((char*)buf10); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); + if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); + if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); return NULL; } -SWIGINTERN PyObject *_wrap_Page_insertImage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_convertToPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; - char *arg3 = (char *) NULL ; - struct fz_pixmap_s *arg4 = (struct fz_pixmap_s *) NULL ; - PyObject *arg5 = (PyObject *) NULL ; - int arg6 = (int) 1 ; - char *arg7 = (char *) NULL ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 = (int) 0 ; + int arg3 = (int) -1 ; + int arg4 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - void *argp4 = 0 ; - int res4 = 0 ; - int val6 ; - int ecode6 = 0 ; - int res7 ; - char *buf7 = 0 ; - int alloc7 = 0 ; + int val2 ; + int ecode2 = 0 ; + int val3 ; + int ecode3 = 0 ; + int val4 ; + int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject * obj6 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO|OOOOO:Page_insertImage",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|OOO:Document_convertToPDF",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_insertImage" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_convertToPDF" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_insertImage" "', argument " "2"" of type '" "struct fz_rect_s *""'"); + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_convertToPDF" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); } - arg2 = (struct fz_rect_s *)(argp2); if (obj2) { - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_insertImage" "', argument " "3"" of type '" "char const *""'"); - } - arg3 = (char *)(buf3); + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_convertToPDF" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); } if (obj3) { - res4 = SWIG_ConvertPtr(obj3, &argp4,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); - if (!SWIG_IsOK(res4)) { - SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Page_insertImage" "', argument " "4"" of type '" "struct fz_pixmap_s *""'"); - } - arg4 = (struct fz_pixmap_s *)(argp4); - } - if (obj4) { - arg5 = obj4; - } - if (obj5) { - ecode6 = SWIG_AsVal_int(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Page_insertImage" "', argument " "6"" of type '" "int""'"); + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_convertToPDF" "', argument " "4"" of type '" "int""'"); } - arg6 = (int)(val6); - } - if (obj6) { - res7 = SWIG_AsCharPtrAndSize(obj6, &buf7, NULL, &alloc7); - if (!SWIG_IsOK(res7)) { - SWIG_exception_fail(SWIG_ArgError(res7), "in method '" "Page_insertImage" "', argument " "7"" of type '" "char *""'"); - } - arg7 = (char *)(buf7); + arg4 = (int)(val4); } { - result = (PyObject *)fz_page_s_insertImage(arg1,arg2,(char const *)arg3,arg4,arg5,arg6,arg7); + result = (PyObject *)fz_document_s_convertToPDF(arg1,arg2,arg3,arg4); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -13897,94 +13952,180 @@ SWIGINTERN PyObject *_wrap_Page_insertImage(PyObject *SWIGUNUSEDPARM(self), PyOb } } resultobj = result; - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - if (alloc7 == SWIG_NEWOBJ) free((char*)buf7); return resultobj; fail: - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - if (alloc7 == SWIG_NEWOBJ) free((char*)buf7); return NULL; } -SWIGINTERN PyObject *_wrap_Page_insertFont(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_pageCount(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + int result; + + if (!PyArg_ParseTuple(args,(char *)"O:Document_pageCount",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_pageCount" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + result = (int)fz_document_s_pageCount(arg1); + resultobj = SWIG_From_int((int)(result)); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document__getMetadata(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + char *arg2 = (char *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + char *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:Document__getMetadata",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getMetadata" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document__getMetadata" "', argument " "2"" of type '" "char const *""'"); + } + arg2 = (char *)(buf2); + result = (char *)fz_document_s__getMetadata(arg1,(char const *)arg2); + resultobj = SWIG_FromCharPtr((const char *)result); + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document_needsPass(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + int result; + + if (!PyArg_ParseTuple(args,(char *)"O:Document_needsPass",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_needsPass" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + result = (int)fz_document_s_needsPass(arg1); + resultobj = SWIG_From_int((int)(result)); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document_resolveLink(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; char *arg2 = (char *) NULL ; - char *arg3 = (char *) NULL ; - PyObject *arg4 = (PyObject *) NULL ; - int arg5 = (int) 0 ; - int arg6 = (int) 0 ; - int arg7 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; int res2 ; char *buf2 = 0 ; int alloc2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - int val5 ; - int ecode5 = 0 ; - int val6 ; - int ecode6 = 0 ; - int val7 ; - int ecode7 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - PyObject * obj6 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O|OOOOOO:Page_insertFont",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|O:Document_resolveLink",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_insertFont" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_resolveLink" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); + arg1 = (struct fz_document_s *)(argp1); if (obj1) { res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_insertFont" "', argument " "2"" of type '" "char const *""'"); + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_resolveLink" "', argument " "2"" of type '" "char *""'"); } arg2 = (char *)(buf2); } - if (obj2) { - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_insertFont" "', argument " "3"" of type '" "char const *""'"); - } - arg3 = (char *)(buf3); + result = (PyObject *)fz_document_s_resolveLink(arg1,arg2); + resultobj = result; + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document_layout(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + PyObject *arg2 = (PyObject *) NULL ; + float arg3 = (float) 0 ; + float arg4 = (float) 0 ; + float arg5 = (float) 11 ; + void *argp1 = 0 ; + int res1 = 0 ; + float val3 ; + int ecode3 = 0 ; + float val4 ; + int ecode4 = 0 ; + float val5 ; + int ecode5 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O|OOOO:Document_layout",&obj0,&obj1,&obj2,&obj3,&obj4)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_layout" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - if (obj3) { - arg4 = obj3; + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + arg2 = obj1; } - if (obj4) { - ecode5 = SWIG_AsVal_int(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Page_insertFont" "', argument " "5"" of type '" "int""'"); + if (obj2) { + ecode3 = SWIG_AsVal_float(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_layout" "', argument " "3"" of type '" "float""'"); } - arg5 = (int)(val5); + arg3 = (float)(val3); } - if (obj5) { - ecode6 = SWIG_AsVal_int(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Page_insertFont" "', argument " "6"" of type '" "int""'"); + if (obj3) { + ecode4 = SWIG_AsVal_float(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_layout" "', argument " "4"" of type '" "float""'"); } - arg6 = (int)(val6); + arg4 = (float)(val4); } - if (obj6) { - ecode7 = SWIG_AsVal_int(obj6, &val7); - if (!SWIG_IsOK(ecode7)) { - SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Page_insertFont" "', argument " "7"" of type '" "int""'"); + if (obj4) { + ecode5 = SWIG_AsVal_float(obj4, &val5); + if (!SWIG_IsOK(ecode5)) { + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_layout" "', argument " "5"" of type '" "float""'"); } - arg7 = (int)(val7); + arg5 = (float)(val5); } { - result = (PyObject *)fz_page_s_insertFont(arg1,(char const *)arg2,(char const *)arg3,arg4,arg5,arg6,arg7); + result = (PyObject *)fz_document_s_layout(arg1,arg2,arg3,arg4,arg5); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -13992,38 +14133,38 @@ SWIGINTERN PyObject *_wrap_Page_insertFont(PyObject *SWIGUNUSEDPARM(self), PyObj } } resultobj = result; - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return resultobj; fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return NULL; } -SWIGINTERN PyObject *_wrap_Page__getContents(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_makeBookmark(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Page__getContents",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|O:Document_makeBookmark",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__getContents" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_makeBookmark" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - { - result = (PyObject *)fz_page_s__getContents(arg1); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_makeBookmark" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); } + result = (PyObject *)fz_document_s_makeBookmark(arg1,arg2); resultobj = result; return resultobj; fail: @@ -14031,33 +14172,84 @@ SWIGINTERN PyObject *_wrap_Page__getContents(PyObject *SWIGUNUSEDPARM(self), PyO } -SWIGINTERN PyObject *_wrap_Page__setContents(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_findBookmark(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; - int arg2 = (int) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + long long arg2 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; + long long val2 ; int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + int result; + + if (!PyArg_ParseTuple(args,(char *)"OO:Document_findBookmark",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_findBookmark" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + ecode2 = SWIG_AsVal_long_SS_long(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_findBookmark" "', argument " "2"" of type '" "long long""'"); + } + arg2 = (long long)(val2); + result = (int)fz_document_s_findBookmark(arg1,arg2); + resultobj = SWIG_From_int((int)(result)); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document_isReflowable(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O|O:Page__setContents",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document_isReflowable",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__setContents" "', argument " "1"" of type '" "struct fz_page_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_isReflowable" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_page_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Page__setContents" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s_isReflowable(arg1); + resultobj = result; + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document__deleteObject(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 ; + void *argp1 = 0 ; + int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:Document__deleteObject",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__deleteObject" "', argument " "1"" of type '" "struct fz_document_s *""'"); } + arg1 = (struct fz_document_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__deleteObject" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); { - result = (PyObject *)fz_page_s__setContents(arg1,arg2); + result = (PyObject *)fz_document_s__deleteObject(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -14071,966 +14263,1215 @@ SWIGINTERN PyObject *_wrap_Page__setContents(PyObject *SWIGUNUSEDPARM(self), PyO } -SWIGINTERN PyObject *Page_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_page_s, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); +SWIGINTERN PyObject *_wrap_Document__getPDFroot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + int result; + + if (!PyArg_ParseTuple(args,(char *)"O:Document__getPDFroot",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getPDFroot" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + result = (int)fz_document_s__getPDFroot(arg1); + resultobj = SWIG_From_int((int)(result)); + return resultobj; +fail: + return NULL; } -SWIGINTERN PyObject *_wrap__fz_transform_rect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + +SWIGINTERN PyObject *_wrap_Document_isPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - struct fz_matrix_s *arg2 = (struct fz_matrix_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:_fz_transform_rect",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document_isPDF",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "_fz_transform_rect" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_isPDF" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "_fz_transform_rect" "', argument " "2"" of type '" "struct fz_matrix_s const *""'"); + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s_isPDF(arg1); + resultobj = result; + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document__hasXrefStream(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:Document__hasXrefStream",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__hasXrefStream" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg2 = (struct fz_matrix_s *)(argp2); - result = (struct fz_rect_s *)fz_transform_rect(arg1,(struct fz_matrix_s const *)arg2); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s__hasXrefStream(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_x0_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__hasXrefOldStyle(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - float arg2 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_x0_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document__hasXrefOldStyle",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_x0_set" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__hasXrefOldStyle" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Rect_x0_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->x0 = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s__hasXrefOldStyle(arg1); + resultobj = result; + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document_isDirty(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:Document_isDirty",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_isDirty" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s_isDirty(arg1); + resultobj = result; + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document__getGCTXerrcode(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + int result; + + if (!PyArg_ParseTuple(args,(char *)"O:Document__getGCTXerrcode",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getGCTXerrcode" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + result = (int)fz_document_s__getGCTXerrcode(arg1); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_x0_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__getGCTXerrmsg(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - float result; + char *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Rect_x0_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document__getGCTXerrmsg",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_x0_get" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getGCTXerrmsg" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - result = (float) ((arg1)->x0); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_document_s *)(argp1); + result = (char *)fz_document_s__getGCTXerrmsg(arg1); + resultobj = SWIG_FromCharPtr((const char *)result); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_y0_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_authenticate(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - float arg2 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + char *arg2 = (char *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_y0_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document_authenticate",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_y0_set" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_authenticate" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Rect_y0_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->y0 = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_document_s *)(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_authenticate" "', argument " "2"" of type '" "char *""'"); + } + arg2 = (char *)(buf2); + result = (int)fz_document_s_authenticate(arg1,arg2); + resultobj = SWIG_From_int((int)(result)); + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } -SWIGINTERN PyObject *_wrap_Rect_y0_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_save(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + char *arg2 = (char *) 0 ; + int arg3 = (int) 0 ; + int arg4 = (int) 0 ; + int arg5 = (int) 0 ; + int arg6 = (int) 0 ; + int arg7 = (int) 0 ; + int arg8 = (int) 0 ; + int arg9 = (int) 0 ; + int arg10 = (int) 0 ; + int arg11 = (int) 1 ; void *argp1 = 0 ; int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + int val3 ; + int ecode3 = 0 ; + int val4 ; + int ecode4 = 0 ; + int val5 ; + int ecode5 = 0 ; + int val6 ; + int ecode6 = 0 ; + int val7 ; + int ecode7 = 0 ; + int val8 ; + int ecode8 = 0 ; + int val9 ; + int ecode9 = 0 ; + int val10 ; + int ecode10 = 0 ; + int val11 ; + int ecode11 = 0 ; PyObject * obj0 = 0 ; - float result; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + PyObject * obj7 = 0 ; + PyObject * obj8 = 0 ; + PyObject * obj9 = 0 ; + PyObject * obj10 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Rect_y0_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO|OOOOOOOOO:Document_save",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6,&obj7,&obj8,&obj9,&obj10)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_y0_get" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_save" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - result = (float) ((arg1)->y0); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_document_s *)(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_save" "', argument " "2"" of type '" "char *""'"); + } + arg2 = (char *)(buf2); + if (obj2) { + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_save" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + } + if (obj3) { + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_save" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + } + if (obj4) { + ecode5 = SWIG_AsVal_int(obj4, &val5); + if (!SWIG_IsOK(ecode5)) { + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_save" "', argument " "5"" of type '" "int""'"); + } + arg5 = (int)(val5); + } + if (obj5) { + ecode6 = SWIG_AsVal_int(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Document_save" "', argument " "6"" of type '" "int""'"); + } + arg6 = (int)(val6); + } + if (obj6) { + ecode7 = SWIG_AsVal_int(obj6, &val7); + if (!SWIG_IsOK(ecode7)) { + SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Document_save" "', argument " "7"" of type '" "int""'"); + } + arg7 = (int)(val7); + } + if (obj7) { + ecode8 = SWIG_AsVal_int(obj7, &val8); + if (!SWIG_IsOK(ecode8)) { + SWIG_exception_fail(SWIG_ArgError(ecode8), "in method '" "Document_save" "', argument " "8"" of type '" "int""'"); + } + arg8 = (int)(val8); + } + if (obj8) { + ecode9 = SWIG_AsVal_int(obj8, &val9); + if (!SWIG_IsOK(ecode9)) { + SWIG_exception_fail(SWIG_ArgError(ecode9), "in method '" "Document_save" "', argument " "9"" of type '" "int""'"); + } + arg9 = (int)(val9); + } + if (obj9) { + ecode10 = SWIG_AsVal_int(obj9, &val10); + if (!SWIG_IsOK(ecode10)) { + SWIG_exception_fail(SWIG_ArgError(ecode10), "in method '" "Document_save" "', argument " "10"" of type '" "int""'"); + } + arg10 = (int)(val10); + } + if (obj10) { + ecode11 = SWIG_AsVal_int(obj10, &val11); + if (!SWIG_IsOK(ecode11)) { + SWIG_exception_fail(SWIG_ArgError(ecode11), "in method '" "Document_save" "', argument " "11"" of type '" "int""'"); + } + arg11 = (int)(val11); + } + { + result = (PyObject *)fz_document_s_save(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } -SWIGINTERN PyObject *_wrap_Rect_x1_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_write(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - float arg2 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 = (int) 0 ; + int arg3 = (int) 0 ; + int arg4 = (int) 0 ; + int arg5 = (int) 0 ; + int arg6 = (int) 0 ; + int arg7 = (int) 0 ; + int arg8 = (int) 0 ; + int arg9 = (int) 1 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; + int val2 ; int ecode2 = 0 ; + int val3 ; + int ecode3 = 0 ; + int val4 ; + int ecode4 = 0 ; + int val5 ; + int ecode5 = 0 ; + int val6 ; + int ecode6 = 0 ; + int val7 ; + int ecode7 = 0 ; + int val8 ; + int ecode8 = 0 ; + int val9 ; + int ecode9 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + PyObject * obj7 = 0 ; + PyObject * obj8 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_x1_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|OOOOOOOO:Document_write",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6,&obj7,&obj8)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_x1_set" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_write" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_write" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + } + if (obj2) { + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_write" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + } + if (obj3) { + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_write" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + } + if (obj4) { + ecode5 = SWIG_AsVal_int(obj4, &val5); + if (!SWIG_IsOK(ecode5)) { + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_write" "', argument " "5"" of type '" "int""'"); + } + arg5 = (int)(val5); + } + if (obj5) { + ecode6 = SWIG_AsVal_int(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Document_write" "', argument " "6"" of type '" "int""'"); + } + arg6 = (int)(val6); + } + if (obj6) { + ecode7 = SWIG_AsVal_int(obj6, &val7); + if (!SWIG_IsOK(ecode7)) { + SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Document_write" "', argument " "7"" of type '" "int""'"); + } + arg7 = (int)(val7); + } + if (obj7) { + ecode8 = SWIG_AsVal_int(obj7, &val8); + if (!SWIG_IsOK(ecode8)) { + SWIG_exception_fail(SWIG_ArgError(ecode8), "in method '" "Document_write" "', argument " "8"" of type '" "int""'"); + } + arg8 = (int)(val8); + } + if (obj8) { + ecode9 = SWIG_AsVal_int(obj8, &val9); + if (!SWIG_IsOK(ecode9)) { + SWIG_exception_fail(SWIG_ArgError(ecode9), "in method '" "Document_write" "', argument " "9"" of type '" "int""'"); + } + arg9 = (int)(val9); + } + { + result = (PyObject *)fz_document_s_write(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg1 = (struct fz_rect_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Rect_x1_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->x1 = arg2; - resultobj = SWIG_Py_Void(); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_x1_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_insertPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + struct fz_document_s *arg2 = (struct fz_document_s *) 0 ; + int arg3 = (int) -1 ; + int arg4 = (int) -1 ; + int arg5 = (int) -1 ; + int arg6 = (int) -1 ; + int arg7 = (int) 1 ; void *argp1 = 0 ; int res1 = 0 ; + void *argp2 = 0 ; + int res2 = 0 ; + int val3 ; + int ecode3 = 0 ; + int val4 ; + int ecode4 = 0 ; + int val5 ; + int ecode5 = 0 ; + int val6 ; + int ecode6 = 0 ; + int val7 ; + int ecode7 = 0 ; PyObject * obj0 = 0 ; - float result; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Rect_x1_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO|OOOOO:Document_insertPDF",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_x1_get" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_insertPDF" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - result = (float) ((arg1)->x1); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_document_s *)(argp1); + res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document_insertPDF" "', argument " "2"" of type '" "struct fz_document_s *""'"); + } + arg2 = (struct fz_document_s *)(argp2); + if (obj2) { + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_insertPDF" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + } + if (obj3) { + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_insertPDF" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + } + if (obj4) { + ecode5 = SWIG_AsVal_int(obj4, &val5); + if (!SWIG_IsOK(ecode5)) { + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_insertPDF" "', argument " "5"" of type '" "int""'"); + } + arg5 = (int)(val5); + } + if (obj5) { + ecode6 = SWIG_AsVal_int(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Document_insertPDF" "', argument " "6"" of type '" "int""'"); + } + arg6 = (int)(val6); + } + if (obj6) { + ecode7 = SWIG_AsVal_int(obj6, &val7); + if (!SWIG_IsOK(ecode7)) { + SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Document_insertPDF" "', argument " "7"" of type '" "int""'"); + } + arg7 = (int)(val7); + } + { + result = (PyObject *)fz_document_s_insertPDF(arg1,arg2,arg3,arg4,arg5,arg6,arg7); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_y1_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_insertPage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - float arg2 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 = (int) -1 ; + PyObject *arg3 = (PyObject *) NULL ; + float arg4 = (float) 11 ; + float arg5 = (float) 595 ; + float arg6 = (float) 842 ; + int arg7 = (int) 0 ; + char *arg8 = (char *) NULL ; + char *arg9 = (char *) NULL ; + int arg10 = (int) 0 ; + PyObject *arg11 = (PyObject *) NULL ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; + int val2 ; int ecode2 = 0 ; + float val4 ; + int ecode4 = 0 ; + float val5 ; + int ecode5 = 0 ; + float val6 ; + int ecode6 = 0 ; + int val7 ; + int ecode7 = 0 ; + int res8 ; + char *buf8 = 0 ; + int alloc8 = 0 ; + int res9 ; + char *buf9 = 0 ; + int alloc9 = 0 ; + int val10 ; + int ecode10 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + PyObject * obj7 = 0 ; + PyObject * obj8 = 0 ; + PyObject * obj9 = 0 ; + PyObject * obj10 = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_y1_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|OOOOOOOOOO:Document_insertPage",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6,&obj7,&obj8,&obj9,&obj10)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_y1_set" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_insertPage" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Rect_y1_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->y1 = arg2; - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Rect_y1_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - float result; - - if (!PyArg_ParseTuple(args,(char *)"O:Rect_y1_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_y1_get" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_insertPage" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); } - arg1 = (struct fz_rect_s *)(argp1); - result = (float) ((arg1)->y1); - resultobj = SWIG_From_float((float)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Rect__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_rect_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)":new_Rect")) SWIG_fail; - result = (struct fz_rect_s *)calloc(1, sizeof(struct fz_rect_s)); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, SWIG_POINTER_NEW | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_delete_Rect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:delete_Rect",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, SWIG_POINTER_DISOWN | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Rect" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + if (obj2) { + arg3 = obj2; } - arg1 = (struct fz_rect_s *)(argp1); - delete_fz_rect_s(arg1); - resultobj = SWIG_Py_Void(); + if (obj3) { + ecode4 = SWIG_AsVal_float(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document_insertPage" "', argument " "4"" of type '" "float""'"); + } + arg4 = (float)(val4); + } + if (obj4) { + ecode5 = SWIG_AsVal_float(obj4, &val5); + if (!SWIG_IsOK(ecode5)) { + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Document_insertPage" "', argument " "5"" of type '" "float""'"); + } + arg5 = (float)(val5); + } + if (obj5) { + ecode6 = SWIG_AsVal_float(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Document_insertPage" "', argument " "6"" of type '" "float""'"); + } + arg6 = (float)(val6); + } + if (obj6) { + ecode7 = SWIG_AsVal_int(obj6, &val7); + if (!SWIG_IsOK(ecode7)) { + SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Document_insertPage" "', argument " "7"" of type '" "int""'"); + } + arg7 = (int)(val7); + } + if (obj7) { + res8 = SWIG_AsCharPtrAndSize(obj7, &buf8, NULL, &alloc8); + if (!SWIG_IsOK(res8)) { + SWIG_exception_fail(SWIG_ArgError(res8), "in method '" "Document_insertPage" "', argument " "8"" of type '" "char *""'"); + } + arg8 = (char *)(buf8); + } + if (obj8) { + res9 = SWIG_AsCharPtrAndSize(obj8, &buf9, NULL, &alloc9); + if (!SWIG_IsOK(res9)) { + SWIG_exception_fail(SWIG_ArgError(res9), "in method '" "Document_insertPage" "', argument " "9"" of type '" "char *""'"); + } + arg9 = (char *)(buf9); + } + if (obj9) { + ecode10 = SWIG_AsVal_int(obj9, &val10); + if (!SWIG_IsOK(ecode10)) { + SWIG_exception_fail(SWIG_ArgError(ecode10), "in method '" "Document_insertPage" "', argument " "10"" of type '" "int""'"); + } + arg10 = (int)(val10); + } + if (obj10) { + arg11 = obj10; + } + { + result = (int)fz_document_s_insertPage(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11); + if(result<0) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_From_int((int)(result)); + if (alloc8 == SWIG_NEWOBJ) free((char*)buf8); + if (alloc9 == SWIG_NEWOBJ) free((char*)buf9); return resultobj; fail: + if (alloc8 == SWIG_NEWOBJ) free((char*)buf8); + if (alloc9 == SWIG_NEWOBJ) free((char*)buf9); return NULL; } -SWIGINTERN PyObject *_wrap_new_Rect__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_select(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:new_Rect",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document_select",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Rect" "', argument " "1"" of type '" "struct fz_rect_s const *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_select" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); + arg1 = (struct fz_document_s *)(argp1); + arg2 = obj1; { - result = (struct fz_rect_s *)new_fz_rect_s__SWIG_1((struct fz_rect_s const *)arg1); + result = (PyObject *)fz_document_s_select(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, SWIG_POINTER_NEW | 0 ); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Rect__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_permissions(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; - struct fz_point_s *arg2 = (struct fz_point_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:new_Rect",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document_permissions",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Rect" "', argument " "1"" of type '" "struct fz_point_s const *""'"); - } - arg1 = (struct fz_point_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "new_Rect" "', argument " "2"" of type '" "struct fz_point_s const *""'"); - } - arg2 = (struct fz_point_s *)(argp2); - { - result = (struct fz_rect_s *)new_fz_rect_s__SWIG_2((struct fz_point_s const *)arg1,(struct fz_point_s const *)arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_permissions" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, SWIG_POINTER_NEW | 0 ); + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s_permissions(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Rect__SWIG_3(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__getCharWidths(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - float arg1 ; - float arg2 ; - struct fz_point_s *arg3 = (struct fz_point_s *) 0 ; - float val1 ; - int ecode1 = 0 ; - float val2 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 ; + int arg3 ; + int arg4 = (int) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int val2 ; int ecode2 = 0 ; - void *argp3 = 0 ; - int res3 = 0 ; + int val3 ; + int ecode3 = 0 ; + int val4 ; + int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject * obj3 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO:new_Rect",&obj0,&obj1,&obj2)) SWIG_fail; - ecode1 = SWIG_AsVal_float(obj0, &val1); - if (!SWIG_IsOK(ecode1)) { - SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_Rect" "', argument " "1"" of type '" "float""'"); - } - arg1 = (float)(val1); - ecode2 = SWIG_AsVal_float(obj1, &val2); + if (!PyArg_ParseTuple(args,(char *)"OOO|O:Document__getCharWidths",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getCharWidths" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Rect" "', argument " "2"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getCharWidths" "', argument " "2"" of type '" "int""'"); } - arg2 = (float)(val2); - res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "new_Rect" "', argument " "3"" of type '" "struct fz_point_s const *""'"); + arg2 = (int)(val2); + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document__getCharWidths" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + if (obj3) { + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document__getCharWidths" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); } - arg3 = (struct fz_point_s *)(argp3); { - result = (struct fz_rect_s *)new_fz_rect_s__SWIG_3(arg1,arg2,(struct fz_point_s const *)arg3); + result = (PyObject *)fz_document_s__getCharWidths(arg1,arg2,arg3,arg4); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, SWIG_POINTER_NEW | 0 ); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Rect__SWIG_4(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__getPageObjNumber(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; - float arg2 ; - float arg3 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; + int val2 ; int ecode2 = 0 ; - float val3 ; - int ecode3 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO:new_Rect",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document__getPageObjNumber",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Rect" "', argument " "1"" of type '" "struct fz_point_s const *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getPageObjNumber" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_point_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); + arg1 = (struct fz_document_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Rect" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - ecode3 = SWIG_AsVal_float(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Rect" "', argument " "3"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getPageObjNumber" "', argument " "2"" of type '" "int""'"); } - arg3 = (float)(val3); + arg2 = (int)(val2); { - result = (struct fz_rect_s *)new_fz_rect_s__SWIG_4((struct fz_point_s const *)arg1,arg2,arg3); + result = (PyObject *)fz_document_s__getPageObjNumber(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, SWIG_POINTER_NEW | 0 ); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Rect__SWIG_5(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__getPageInfo(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - float arg1 ; - float arg2 ; - float arg3 ; - float arg4 ; - float val1 ; - int ecode1 = 0 ; - float val2 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 ; + int arg3 ; + void *argp1 = 0 ; + int res1 = 0 ; + int val2 ; int ecode2 = 0 ; - float val3 ; + int val3 ; int ecode3 = 0 ; - float val4 ; - int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOOO:new_Rect",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; - ecode1 = SWIG_AsVal_float(obj0, &val1); - if (!SWIG_IsOK(ecode1)) { - SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_Rect" "', argument " "1"" of type '" "float""'"); - } - arg1 = (float)(val1); - ecode2 = SWIG_AsVal_float(obj1, &val2); + if (!PyArg_ParseTuple(args,(char *)"OOO:Document__getPageInfo",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getPageInfo" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Rect" "', argument " "2"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getPageInfo" "', argument " "2"" of type '" "int""'"); } - arg2 = (float)(val2); - ecode3 = SWIG_AsVal_float(obj2, &val3); + arg2 = (int)(val2); + ecode3 = SWIG_AsVal_int(obj2, &val3); if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Rect" "', argument " "3"" of type '" "float""'"); - } - arg3 = (float)(val3); - ecode4 = SWIG_AsVal_float(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "new_Rect" "', argument " "4"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document__getPageInfo" "', argument " "3"" of type '" "int""'"); } - arg4 = (float)(val4); - { - result = (struct fz_rect_s *)new_fz_rect_s__SWIG_5(arg1,arg2,arg3,arg4); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, SWIG_POINTER_NEW | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Rect__SWIG_6(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - PyObject *arg1 = (PyObject *) 0 ; - PyObject * obj0 = 0 ; - struct fz_rect_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:new_Rect",&obj0)) SWIG_fail; - arg1 = obj0; + arg3 = (int)(val3); { - result = (struct fz_rect_s *)new_fz_rect_s__SWIG_6(arg1); + result = (PyObject *)fz_document_s__getPageInfo(arg1,arg2,arg3); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, SWIG_POINTER_NEW | 0 ); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Rect(PyObject *self, PyObject *args) { - Py_ssize_t argc; - PyObject *argv[5] = { - 0 - }; - Py_ssize_t ii; - - if (!PyTuple_Check(args)) SWIG_fail; - argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 4) && (ii < argc); ii++) { - argv[ii] = PyTuple_GET_ITEM(args,ii); - } - if (argc == 0) { - return _wrap_new_Rect__SWIG_0(self, args); - } - if (argc == 1) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_rect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_new_Rect__SWIG_1(self, args); - } - } - if (argc == 1) { - int _v; - _v = (argv[0] != 0); - if (_v) { - return _wrap_new_Rect__SWIG_6(self, args); - } - } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_point_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_point_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_new_Rect__SWIG_2(self, args); - } - } - } - if (argc == 3) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_point_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - { - int res = SWIG_AsVal_float(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[2], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Rect__SWIG_4(self, args); - } - } - } - } - if (argc == 3) { - int _v; - { - int res = SWIG_AsVal_float(argv[0], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[2], &vptr, SWIGTYPE_p_fz_point_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_new_Rect__SWIG_3(self, args); - } - } - } - } - if (argc == 4) { - int _v; - { - int res = SWIG_AsVal_float(argv[0], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[2], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[3], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Rect__SWIG_5(self, args); - } - } - } - } - } - -fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'new_Rect'.\n" - " Possible C/C++ prototypes are:\n" - " fz_rect_s::fz_rect_s()\n" - " fz_rect_s::fz_rect_s(struct fz_rect_s const *)\n" - " fz_rect_s::fz_rect_s(struct fz_point_s const *,struct fz_point_s const *)\n" - " fz_rect_s::fz_rect_s(float,float,struct fz_point_s const *)\n" - " fz_rect_s::fz_rect_s(struct fz_point_s const *,float,float)\n" - " fz_rect_s::fz_rect_s(float,float,float,float)\n" - " fz_rect_s::fz_rect_s(PyObject *)\n"); - return 0; -} - - -SWIGINTERN PyObject *_wrap_Rect_round(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_extractFont(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 = (int) 0 ; + int arg3 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; + int val3 ; + int ecode3 = 0 ; PyObject * obj0 = 0 ; - struct fz_irect_s *result = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Rect_round",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|OO:Document_extractFont",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_round" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_extractFont" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_extractFont" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + } + if (obj2) { + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Document_extractFont" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + } + { + result = (PyObject *)fz_document_s_extractFont(arg1,arg2,arg3); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg1 = (struct fz_rect_s *)(argp1); - result = (struct fz_irect_s *)fz_rect_s_round(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, 0 | 0 ); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_includePoint(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_extractImage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - struct fz_point_s *arg2 = (struct fz_point_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_includePoint",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|O:Document_extractImage",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_includePoint" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_extractImage" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Rect_includePoint" "', argument " "2"" of type '" "struct fz_point_s const *""'"); + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document_extractImage" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + } + { + result = (PyObject *)fz_document_s_extractImage(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg2 = (struct fz_point_s *)(argp2); - result = (struct fz_rect_s *)fz_rect_s_includePoint(arg1,(struct fz_point_s const *)arg2); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_intersect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__delToC(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_intersect",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document__delToC",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_intersect" "', argument " "1"" of type '" "struct fz_rect_s *""'"); - } - arg1 = (struct fz_rect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Rect_intersect" "', argument " "2"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__delToC" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg2 = (struct fz_rect_s *)(argp2); - result = (struct fz_rect_s *)fz_rect_s_intersect(arg1,arg2); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s__delToC(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_includeRect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_isFormPDF(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_includeRect",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document_isFormPDF",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_includeRect" "', argument " "1"" of type '" "struct fz_rect_s *""'"); - } - arg1 = (struct fz_rect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Rect_includeRect" "', argument " "2"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_isFormPDF" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg2 = (struct fz_rect_s *)(argp2); - result = (struct fz_rect_s *)fz_rect_s_includeRect(arg1,arg2); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s_isFormPDF(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_normalize(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document_FormFonts(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Rect_normalize",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document_FormFonts",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_normalize" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document_FormFonts" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - result = (struct fz_rect_s *)fz_rect_s_normalize(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + result = (PyObject *)fz_document_s_FormFonts(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_contains__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__addFormFont(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + char *arg2 = (char *) 0 ; + char *arg3 = (char *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_contains",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO:Document__addFormFont",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_contains" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__addFormFont" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + arg1 = (struct fz_document_s *)(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Rect_contains" "', argument " "2"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document__addFormFont" "', argument " "2"" of type '" "char *""'"); + } + arg2 = (char *)(buf2); + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Document__addFormFont" "', argument " "3"" of type '" "char *""'"); + } + arg3 = (char *)(buf3); + { + result = (PyObject *)fz_document_s__addFormFont(arg1,arg2,arg3); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg2 = (struct fz_rect_s *)(argp2); - result = (PyObject *)fz_rect_s_contains__SWIG_0(arg1,arg2); resultobj = result; + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return resultobj; fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return NULL; } -SWIGINTERN PyObject *_wrap_Rect_contains__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__getOLRootNumber(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - struct fz_irect_s *arg2 = (struct fz_irect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_contains",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document__getOLRootNumber",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_contains" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getOLRootNumber" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Rect_contains" "', argument " "2"" of type '" "struct fz_irect_s *""'"); + arg1 = (struct fz_document_s *)(argp1); + { + result = (int)fz_document_s__getOLRootNumber(arg1); + if(result<0) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg2 = (struct fz_irect_s *)(argp2); - result = (PyObject *)fz_rect_s_contains__SWIG_1(arg1,arg2); - resultobj = result; + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_contains__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__getNewXref(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - struct fz_point_s *arg2 = (struct fz_point_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OO:Rect_contains",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document__getNewXref",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_contains" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getNewXref" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Rect_contains" "', argument " "2"" of type '" "struct fz_point_s *""'"); + arg1 = (struct fz_document_s *)(argp1); + { + result = (int)fz_document_s__getNewXref(arg1); + if(result<0) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg2 = (struct fz_point_s *)(argp2); - result = (PyObject *)fz_rect_s_contains__SWIG_2(arg1,arg2); - resultobj = result; + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Rect_contains(PyObject *self, PyObject *args) { - Py_ssize_t argc; - PyObject *argv[3] = { - 0 - }; - Py_ssize_t ii; +SWIGINTERN PyObject *_wrap_Document__getXrefLength(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + int result; - if (!PyTuple_Check(args)) SWIG_fail; - argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 2) && (ii < argc); ii++) { - argv[ii] = PyTuple_GET_ITEM(args,ii); - } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_rect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_rect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_Rect_contains__SWIG_0(self, args); - } - } + if (!PyArg_ParseTuple(args,(char *)"O:Document__getXrefLength",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getXrefLength" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_rect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_Rect_contains__SWIG_1(self, args); - } - } + arg1 = (struct fz_document_s *)(argp1); + result = (int)fz_document_s__getXrefLength(arg1); + resultobj = SWIG_From_int((int)(result)); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Document__getXmlMetadataXref(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + int result; + + if (!PyArg_ParseTuple(args,(char *)"O:Document__getXmlMetadataXref",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getXmlMetadataXref" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_rect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_point_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_Rect_contains__SWIG_2(self, args); - } + arg1 = (struct fz_document_s *)(argp1); + { + result = (int)fz_document_s__getXmlMetadataXref(arg1); + if(result<0) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; } } - + resultobj = SWIG_From_int((int)(result)); + return resultobj; fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'Rect_contains'.\n" - " Possible C/C++ prototypes are:\n" - " fz_rect_s::contains(struct fz_rect_s *)\n" - " fz_rect_s::contains(struct fz_irect_s *)\n" - " fz_rect_s::contains(struct fz_point_s *)\n"); - return 0; + return NULL; } -SWIGINTERN PyObject *_wrap_Rect_isEmpty(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__delXmlMetadata(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Rect_isEmpty",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Document__delXmlMetadata",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_isEmpty" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__delXmlMetadata" "', argument " "1"" of type '" "struct fz_document_s *""'"); + } + arg1 = (struct fz_document_s *)(argp1); + { + result = (PyObject *)fz_document_s__delXmlMetadata(arg1); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg1 = (struct fz_rect_s *)(argp1); - result = (PyObject *)fz_rect_s_isEmpty(arg1); resultobj = result; return resultobj; fail: @@ -15038,38 +15479,47 @@ SWIGINTERN PyObject *_wrap_Rect_isEmpty(PyObject *SWIGUNUSEDPARM(self), PyObject } -SWIGINTERN PyObject *_wrap_Rect_isInfinite(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__getObjectString(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject *result = 0 ; + PyObject * obj1 = 0 ; + char *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Rect_isInfinite",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document__getObjectString",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Rect_isInfinite" "', argument " "1"" of type '" "struct fz_rect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getObjectString" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_rect_s *)(argp1); - result = (PyObject *)fz_rect_s_isInfinite(arg1); - resultobj = result; + arg1 = (struct fz_document_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getObjectString" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + { + result = (char *)fz_document_s__getObjectString(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_FromCharPtr((const char *)result); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *Rect_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_rect_s, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); -} - -SWIGINTERN PyObject *_wrap_IRect_x0_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__getXrefStream(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; int arg2 ; void *argp1 = 0 ; int res1 = 0 ; @@ -15077,123 +15527,213 @@ SWIGINTERN PyObject *_wrap_IRect_x0_set(PyObject *SWIGUNUSEDPARM(self), PyObject int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:IRect_x0_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document__getXrefStream",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_x0_set" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__getXrefStream" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); + arg1 = (struct fz_document_s *)(argp1); ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "IRect_x0_set" "', argument " "2"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__getXrefStream" "', argument " "2"" of type '" "int""'"); } arg2 = (int)(val2); - if (arg1) (arg1)->x0 = arg2; - resultobj = SWIG_Py_Void(); + { + result = (PyObject *)fz_document_s__getXrefStream(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_x0_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__updateObject(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 ; + char *arg3 = (char *) 0 ; + struct fz_page_s *arg4 = (struct fz_page_s *) NULL ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; + void *argp4 = 0 ; + int res4 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:IRect_x0_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO|O:Document__updateObject",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_x0_get" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__updateObject" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - result = (int) ((arg1)->x0); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_document_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__updateObject" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Document__updateObject" "', argument " "3"" of type '" "char *""'"); + } + arg3 = (char *)(buf3); + if (obj3) { + res4 = SWIG_ConvertPtr(obj3, &argp4,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!SWIG_IsOK(res4)) { + SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Document__updateObject" "', argument " "4"" of type '" "struct fz_page_s *""'"); + } + arg4 = (struct fz_page_s *)(argp4); + } + { + result = (PyObject *)fz_document_s__updateObject(arg1,arg2,arg3,arg4); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return resultobj; fail: + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return NULL; } -SWIGINTERN PyObject *_wrap_IRect_y0_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__updateStream(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - int arg2 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 = (int) 0 ; + PyObject *arg3 = (PyObject *) NULL ; + int arg4 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; int val2 ; int ecode2 = 0 ; + int val4 ; + int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:IRect_y0_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|OOO:Document__updateStream",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_y0_set" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__updateStream" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "IRect_y0_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->y0 = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_document_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Document__updateStream" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + } + if (obj2) { + arg3 = obj2; + } + if (obj3) { + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Document__updateStream" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + } + { + result = (PyObject *)fz_document_s__updateStream(arg1,arg2,arg3,arg4); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_y0_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Document__setMetadata(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + char *arg2 = (char *) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:IRect_y0_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Document__setMetadata",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_y0_get" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Document__setMetadata" "', argument " "1"" of type '" "struct fz_document_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - result = (int) ((arg1)->y0); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_document_s *)(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Document__setMetadata" "', argument " "2"" of type '" "char *""'"); + } + arg2 = (char *)(buf2); + { + result = (PyObject *)fz_document_s__setMetadata(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } -SWIGINTERN PyObject *_wrap_IRect_x1_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *Document_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *obj; + if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; + SWIG_TypeNewClientData(SWIGTYPE_p_fz_document_s, SWIG_NewClientData(obj)); + return SWIG_Py_Void(); +} + +SWIGINTERN PyObject *_wrap_delete_Page(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - int arg2 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:IRect_x1_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:delete_Page",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, SWIG_POINTER_DISOWN | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_x1_set" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Page" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "IRect_x1_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->x1 = arg2; + arg1 = (struct fz_page_s *)(argp1); + delete_fz_page_s(arg1); resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -15201,900 +15741,924 @@ SWIGINTERN PyObject *_wrap_IRect_x1_set(PyObject *SWIGUNUSEDPARM(self), PyObject } -SWIGINTERN PyObject *_wrap_IRect_x1_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_bound(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:IRect_x1_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page_bound",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_x1_get" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_bound" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - result = (int) ((arg1)->x1); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_page_s *)(argp1); + result = (PyObject *)fz_page_s_bound(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_y1_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_run(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - int arg2 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct DeviceWrapper *arg2 = (struct DeviceWrapper *) 0 ; + PyObject *arg3 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; + void *argp2 = 0 ; + int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:IRect_y1_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO:Page_run",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_y1_set" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_run" "', argument " "1"" of type '" "struct fz_page_s *""'"); + } + arg1 = (struct fz_page_s *)(argp1); + res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_DeviceWrapper, 0 | 0 ); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_run" "', argument " "2"" of type '" "struct DeviceWrapper *""'"); + } + arg2 = (struct DeviceWrapper *)(argp2); + arg3 = obj2; + { + result = (PyObject *)fz_page_s_run(arg1,arg2,arg3); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg1 = (struct fz_irect_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "IRect_y1_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->y1 = arg2; - resultobj = SWIG_Py_Void(); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_y1_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_getSVGimage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) NULL ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:IRect_y1_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|O:Page_getSVGimage",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_y1_get" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_getSVGimage" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - result = (int) ((arg1)->y1); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_IRect__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_irect_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)":new_IRect")) SWIG_fail; - result = (struct fz_irect_s *)calloc(1, sizeof(struct fz_irect_s)); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, SWIG_POINTER_NEW | 0 ); + arg1 = (struct fz_page_s *)(argp1); + if (obj1) { + arg2 = obj1; + } + { + result = (PyObject *)fz_page_s_getSVGimage(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_delete_IRect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addLineAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + PyObject *arg3 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:delete_IRect",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, SWIG_POINTER_DISOWN | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO:Page_addLineAnnot",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_IRect" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addLineAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - delete_fz_irect_s(arg1); - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + arg3 = obj2; + { + result = (struct fz_annot_s *)fz_page_s_addLineAnnot(arg1,arg2,arg3); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_IRect__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addTextAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + char *arg3 = (char *) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; PyObject * obj0 = 0 ; - struct fz_irect_s *result = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:new_IRect",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO:Page_addTextAnnot",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_IRect" "', argument " "1"" of type '" "struct fz_irect_s const *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addTextAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); + } + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_addTextAnnot" "', argument " "3"" of type '" "char *""'"); } - arg1 = (struct fz_irect_s *)(argp1); + arg3 = (char *)(buf3); { - result = (struct fz_irect_s *)new_fz_irect_s__SWIG_1((struct fz_irect_s const *)arg1); + result = (struct fz_annot_s *)fz_page_s_addTextAnnot(arg1,arg2,arg3); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return resultobj; fail: + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return NULL; } -SWIGINTERN PyObject *_wrap_new_IRect__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addInkAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - int arg1 ; - int arg2 ; - int arg3 ; - int arg4 ; - int val1 ; - int ecode1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; - int val4 ; - int ecode4 = 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - struct fz_irect_s *result = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOOO:new_IRect",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; - ecode1 = SWIG_AsVal_int(obj0, &val1); - if (!SWIG_IsOK(ecode1)) { - SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_IRect" "', argument " "1"" of type '" "int""'"); - } - arg1 = (int)(val1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_IRect" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_IRect" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "new_IRect" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addInkAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addInkAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); + } + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; { - result = (struct fz_irect_s *)new_fz_irect_s__SWIG_2(arg1,arg2,arg3,arg4); + result = (struct fz_annot_s *)fz_page_s_addInkAnnot(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_IRect__SWIG_3(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addStampAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - PyObject *arg1 = (PyObject *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + int arg3 = (int) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int val3 ; + int ecode3 = 0 ; PyObject * obj0 = 0 ; - struct fz_irect_s *result = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:new_IRect",&obj0)) SWIG_fail; - arg1 = obj0; + if (!PyArg_ParseTuple(args,(char *)"OO|O:Page_addStampAnnot",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addStampAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); + } + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + if (obj2) { + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Page_addStampAnnot" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + } { - result = (struct fz_irect_s *)new_fz_irect_s__SWIG_3(arg1); + result = (struct fz_annot_s *)fz_page_s_addStampAnnot(arg1,arg2,arg3); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_IRect(PyObject *self, PyObject *args) { - Py_ssize_t argc; - PyObject *argv[5] = { - 0 - }; - Py_ssize_t ii; +SWIGINTERN PyObject *_wrap_Page_addFileAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + PyObject *arg3 = (PyObject *) 0 ; + char *arg4 = (char *) 0 ; + char *arg5 = (char *) NULL ; + char *arg6 = (char *) NULL ; + void *argp1 = 0 ; + int res1 = 0 ; + int res4 ; + char *buf4 = 0 ; + int alloc4 = 0 ; + int res5 ; + char *buf5 = 0 ; + int alloc5 = 0 ; + int res6 ; + char *buf6 = 0 ; + int alloc6 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyTuple_Check(args)) SWIG_fail; - argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 4) && (ii < argc); ii++) { - argv[ii] = PyTuple_GET_ITEM(args,ii); + if (!PyArg_ParseTuple(args,(char *)"OOOO|OO:Page_addFileAnnot",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addFileAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - if (argc == 0) { - return _wrap_new_IRect__SWIG_0(self, args); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + arg3 = obj2; + res4 = SWIG_AsCharPtrAndSize(obj3, &buf4, NULL, &alloc4); + if (!SWIG_IsOK(res4)) { + SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Page_addFileAnnot" "', argument " "4"" of type '" "char *""'"); } - if (argc == 1) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_new_IRect__SWIG_1(self, args); + arg4 = (char *)(buf4); + if (obj4) { + res5 = SWIG_AsCharPtrAndSize(obj4, &buf5, NULL, &alloc5); + if (!SWIG_IsOK(res5)) { + SWIG_exception_fail(SWIG_ArgError(res5), "in method '" "Page_addFileAnnot" "', argument " "5"" of type '" "char *""'"); } + arg5 = (char *)(buf5); } - if (argc == 1) { - int _v; - _v = (argv[0] != 0); - if (_v) { - return _wrap_new_IRect__SWIG_3(self, args); + if (obj5) { + res6 = SWIG_AsCharPtrAndSize(obj5, &buf6, NULL, &alloc6); + if (!SWIG_IsOK(res6)) { + SWIG_exception_fail(SWIG_ArgError(res6), "in method '" "Page_addFileAnnot" "', argument " "6"" of type '" "char *""'"); } + arg6 = (char *)(buf6); } - if (argc == 4) { - int _v; + { + result = (struct fz_annot_s *)fz_page_s_addFileAnnot(arg1,arg2,arg3,arg4,arg5,arg6); + if(!result) { - int res = SWIG_AsVal_int(argv[0], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_int(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_int(argv[2], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_int(argv[3], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_IRect__SWIG_2(self, args); - } - } - } + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; } } - -fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'new_IRect'.\n" - " Possible C/C++ prototypes are:\n" - " fz_irect_s::fz_irect_s()\n" - " fz_irect_s::fz_irect_s(struct fz_irect_s const *)\n" - " fz_irect_s::fz_irect_s(int,int,int,int)\n" - " fz_irect_s::fz_irect_s(PyObject *)\n"); - return 0; -} - - -SWIGINTERN PyObject *_wrap_IRect_isEmpty(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:IRect_isEmpty",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_isEmpty" "', argument " "1"" of type '" "struct fz_irect_s *""'"); - } - arg1 = (struct fz_irect_s *)(argp1); - result = (PyObject *)fz_irect_s_isEmpty(arg1); - resultobj = result; + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); + if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); + if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); + if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); return resultobj; fail: + if (alloc4 == SWIG_NEWOBJ) free((char*)buf4); + if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); + if (alloc6 == SWIG_NEWOBJ) free((char*)buf6); return NULL; } -SWIGINTERN PyObject *_wrap_IRect_isInfinite(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addStrikeoutAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - PyObject *result = 0 ; + PyObject * obj1 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:IRect_isInfinite",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addStrikeoutAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_isInfinite" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addStrikeoutAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - result = (PyObject *)fz_irect_s_isInfinite(arg1); - resultobj = result; + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s_addStrikeoutAnnot(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_normalize(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addUnderlineAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_irect_s *result = 0 ; + PyObject * obj1 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:IRect_normalize",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addUnderlineAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_normalize" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addUnderlineAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); + } + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s_addUnderlineAnnot(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg1 = (struct fz_irect_s *)(argp1); - result = (struct fz_irect_s *)fz_irect_s_normalize(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, 0 | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_contains__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addSquigglyAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - struct fz_irect_s *arg2 = (struct fz_irect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject *result = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:IRect_contains",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addSquigglyAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_contains" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addSquigglyAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "IRect_contains" "', argument " "2"" of type '" "struct fz_irect_s *""'"); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s_addSquigglyAnnot(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg2 = (struct fz_irect_s *)(argp2); - result = (PyObject *)fz_irect_s_contains__SWIG_0(arg1,arg2); - resultobj = result; + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_contains__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addHighlightAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject *result = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:IRect_contains",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addHighlightAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_contains" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addHighlightAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "IRect_contains" "', argument " "2"" of type '" "struct fz_rect_s *""'"); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s_addHighlightAnnot(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg2 = (struct fz_rect_s *)(argp2); - result = (PyObject *)fz_irect_s_contains__SWIG_1(arg1,arg2); - resultobj = result; + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_contains__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addRectAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - struct fz_point_s *arg2 = (struct fz_point_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject *result = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:IRect_contains",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addRectAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_contains" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addRectAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "IRect_contains" "', argument " "2"" of type '" "struct fz_point_s *""'"); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s_addRectAnnot(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg2 = (struct fz_point_s *)(argp2); - result = (PyObject *)fz_irect_s_contains__SWIG_2(arg1,arg2); - resultobj = result; + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_contains(PyObject *self, PyObject *args) { - Py_ssize_t argc; - PyObject *argv[3] = { - 0 - }; - Py_ssize_t ii; - - if (!PyTuple_Check(args)) SWIG_fail; - argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 2) && (ii < argc); ii++) { - argv[ii] = PyTuple_GET_ITEM(args,ii); - } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_IRect_contains__SWIG_0(self, args); - } - } - } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_rect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_IRect_contains__SWIG_1(self, args); - } - } - } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_point_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_IRect_contains__SWIG_2(self, args); - } - } - } - -fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'IRect_contains'.\n" - " Possible C/C++ prototypes are:\n" - " fz_irect_s::contains(struct fz_irect_s *)\n" - " fz_irect_s::contains(struct fz_rect_s *)\n" - " fz_irect_s::contains(struct fz_point_s *)\n"); - return 0; -} - - -SWIGINTERN PyObject *_wrap_IRect_translate(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addCircleAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - int arg2 ; - int arg3 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_irect_s *result = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO:IRect_translate",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addCircleAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_translate" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addCircleAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "IRect_translate" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "IRect_translate" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - result = (struct fz_irect_s *)fz_irect_s_translate(arg1,arg2,arg3); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, 0 | 0 ); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s_addCircleAnnot(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_IRect_intersect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addPolylineAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_irect_s *arg1 = (struct fz_irect_s *) 0 ; - struct fz_irect_s *arg2 = (struct fz_irect_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - struct fz_irect_s *result = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:IRect_intersect",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_irect_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addPolylineAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IRect_intersect" "', argument " "1"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addPolylineAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_irect_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "IRect_intersect" "', argument " "2"" of type '" "struct fz_irect_s *""'"); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s_addPolylineAnnot(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg2 = (struct fz_irect_s *)(argp2); - result = (struct fz_irect_s *)fz_irect_s_intersect(arg1,arg2); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, 0 | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *IRect_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_irect_s, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); -} - -SWIGINTERN PyObject *_wrap_Pixmap_x_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addPolygonAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - int arg2 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_x_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_addPolygonAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_x_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addPolygonAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_x_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->x = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s_addPolygonAnnot(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_x_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_addFreetextAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + char *arg3 = (char *) 0 ; + float arg4 = (float) 12 ; + char *arg5 = (char *) NULL ; + PyObject *arg6 = (PyObject *) NULL ; + int arg7 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; + float val4 ; + int ecode4 = 0 ; + int res5 ; + char *buf5 = 0 ; + int alloc5 = 0 ; + int val7 ; + int ecode7 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_x_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO|OOOO:Page_addFreetextAnnot",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_x_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_addFreetextAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - result = (int) ((arg1)->x); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_addFreetextAnnot" "', argument " "3"" of type '" "char *""'"); + } + arg3 = (char *)(buf3); + if (obj3) { + ecode4 = SWIG_AsVal_float(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Page_addFreetextAnnot" "', argument " "4"" of type '" "float""'"); + } + arg4 = (float)(val4); + } + if (obj4) { + res5 = SWIG_AsCharPtrAndSize(obj4, &buf5, NULL, &alloc5); + if (!SWIG_IsOK(res5)) { + SWIG_exception_fail(SWIG_ArgError(res5), "in method '" "Page_addFreetextAnnot" "', argument " "5"" of type '" "char *""'"); + } + arg5 = (char *)(buf5); + } + if (obj5) { + arg6 = obj5; + } + if (obj6) { + ecode7 = SWIG_AsVal_int(obj6, &val7); + if (!SWIG_IsOK(ecode7)) { + SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Page_addFreetextAnnot" "', argument " "7"" of type '" "int""'"); + } + arg7 = (int)(val7); + } + { + result = (struct fz_annot_s *)fz_page_s_addFreetextAnnot(arg1,arg2,arg3,arg4,arg5,arg6,arg7); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); return resultobj; fail: + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + if (alloc5 == SWIG_NEWOBJ) free((char*)buf5); return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_y_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page__addWidget(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - int arg2 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_y_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page__addWidget",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_y_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__addWidget" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_y_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->y = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (struct fz_annot_s *)fz_page_s__addWidget(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_y_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_getDisplayList(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - int result; + struct fz_display_list_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_y_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page_getDisplayList",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_y_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_getDisplayList" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - result = (int) ((arg1)->y); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_page_s *)(argp1); + { + result = (struct fz_display_list_s *)fz_page_s_getDisplayList(arg1); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_display_list_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_w_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_setCropBox(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - int arg2 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_w_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_setCropBox",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_w_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_setCropBox" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_w_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->w = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (PyObject *)fz_page_s_setCropBox(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_w_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_loadLinks(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - int result; + struct fz_link_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_w_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page_loadLinks",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_w_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_loadLinks" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - result = (int) ((arg1)->w); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_page_s *)(argp1); + result = (struct fz_link_s *)fz_page_s_loadLinks(arg1); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_link_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_h_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_firstAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - int arg2 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_h_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page_firstAnnot",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_h_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_firstAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_h_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->h = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_page_s *)(argp1); + result = (struct fz_annot_s *)fz_page_s_firstAnnot(arg1); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_h_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_deleteLink(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_h_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_deleteLink",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_h_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_deleteLink" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - result = (int) ((arg1)->h); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + fz_page_s_deleteLink(arg1,arg2); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_n_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_deleteAnnot(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - int arg2 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + struct fz_annot_s *arg2 = (struct fz_annot_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; + void *argp2 = 0 ; + int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + struct fz_annot_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_n_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_deleteAnnot",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_n_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_deleteAnnot" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_n_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->n = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_page_s *)(argp1); + res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_annot_s, 0 | 0 ); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_deleteAnnot" "', argument " "2"" of type '" "struct fz_annot_s *""'"); + } + arg2 = (struct fz_annot_s *)(argp2); + result = (struct fz_annot_s *)fz_page_s_deleteAnnot(arg1,arg2); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_annot_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_n_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_MediaBoxSize(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_n_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page_MediaBoxSize",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_n_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_MediaBoxSize" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - result = (int) ((arg1)->n); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_page_s *)(argp1); + result = (PyObject *)fz_page_s_MediaBoxSize(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_xres_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_CropBoxPosition(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - int arg2 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_xres_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page_CropBoxPosition",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_xres_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_CropBoxPosition" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_xres_set" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - if (arg1) (arg1)->xres = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_page_s *)(argp1); + result = (PyObject *)fz_page_s_CropBoxPosition(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_xres_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_rotation(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; int result; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_xres_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page_rotation",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_xres_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_rotation" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - result = (int) ((arg1)->xres); + arg1 = (struct fz_page_s *)(argp1); + result = (int)fz_page_s_rotation(arg1); resultobj = SWIG_From_int((int)(result)); return resultobj; fail: @@ -16102,9 +16666,9 @@ SWIGINTERN PyObject *_wrap_Pixmap_xres_get(PyObject *SWIGUNUSEDPARM(self), PyObj } -SWIGINTERN PyObject *_wrap_Pixmap_yres_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_setRotation(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; int arg2 ; void *argp1 = 0 ; int res1 = 0 ; @@ -16112,373 +16676,493 @@ SWIGINTERN PyObject *_wrap_Pixmap_yres_set(PyObject *SWIGUNUSEDPARM(self), PyObj int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_yres_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page_setRotation",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_yres_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_setRotation" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); + arg1 = (struct fz_page_s *)(argp1); ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_yres_set" "', argument " "2"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Page_setRotation" "', argument " "2"" of type '" "int""'"); } arg2 = (int)(val2); - if (arg1) (arg1)->yres = arg2; - resultobj = SWIG_Py_Void(); + { + result = (PyObject *)fz_page_s_setRotation(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_yres_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page__addAnnot_FromString(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_yres_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Page__addAnnot_FromString",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_yres_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__addAnnot_FromString" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - result = (int) ((arg1)->yres); - resultobj = SWIG_From_int((int)(result)); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + { + result = (PyObject *)fz_page_s__addAnnot_FromString(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_delete_Pixmap(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page__getLinkXrefs(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:delete_Pixmap",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_DISOWN | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page__getLinkXrefs",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Pixmap" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__getLinkXrefs" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - delete_fz_pixmap_s(arg1); - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_page_s *)(argp1); + result = (PyObject *)fz_page_s__getLinkXrefs(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page__cleanContents(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; - struct fz_irect_s *arg2 = (struct fz_irect_s *) 0 ; - int arg3 = (int) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int val3 ; - int ecode3 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_pixmap_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO|O:new_Pixmap",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Page__cleanContents",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); - } - arg1 = (struct fz_colorspace_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "struct fz_irect_s const *""'"); - } - arg2 = (struct fz_irect_s *)(argp2); - if (obj2) { - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Pixmap" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__cleanContents" "', argument " "1"" of type '" "struct fz_page_s *""'"); } + arg1 = (struct fz_page_s *)(argp1); { - result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_0(arg1,(struct fz_irect_s const *)arg2,arg3); + result = (PyObject *)fz_page_s__cleanContents(arg1); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page__showPDFpage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; - struct fz_pixmap_s *arg2 = (struct fz_pixmap_s *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + struct fz_document_s *arg3 = (struct fz_document_s *) 0 ; + int arg4 = (int) 0 ; + int arg5 = (int) 1 ; + int arg6 = (int) 1 ; + int arg7 = (int) 0 ; + PyObject *arg8 = (PyObject *) NULL ; + struct pdf_graft_map_s *arg9 = (struct pdf_graft_map_s *) NULL ; + char *arg10 = (char *) NULL ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; + void *argp3 = 0 ; + int res3 = 0 ; + int val4 ; + int ecode4 = 0 ; + int val5 ; + int ecode5 = 0 ; + int val6 ; + int ecode6 = 0 ; + int val7 ; + int ecode7 = 0 ; + void *argp9 = 0 ; + int res9 = 0 ; + int res10 ; + char *buf10 = 0 ; + int alloc10 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - struct fz_pixmap_s *result = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + PyObject * obj7 = 0 ; + PyObject * obj8 = 0 ; + PyObject * obj9 = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OO:new_Pixmap",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO|OOOOOOO:Page__showPDFpage",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6,&obj7,&obj8,&obj9)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__showPDFpage" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_colorspace_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "struct fz_pixmap_s *""'"); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page__showPDFpage" "', argument " "3"" of type '" "struct fz_document_s *""'"); + } + arg3 = (struct fz_document_s *)(argp3); + if (obj3) { + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Page__showPDFpage" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + } + if (obj4) { + ecode5 = SWIG_AsVal_int(obj4, &val5); + if (!SWIG_IsOK(ecode5)) { + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Page__showPDFpage" "', argument " "5"" of type '" "int""'"); + } + arg5 = (int)(val5); + } + if (obj5) { + ecode6 = SWIG_AsVal_int(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Page__showPDFpage" "', argument " "6"" of type '" "int""'"); + } + arg6 = (int)(val6); + } + if (obj6) { + ecode7 = SWIG_AsVal_int(obj6, &val7); + if (!SWIG_IsOK(ecode7)) { + SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Page__showPDFpage" "', argument " "7"" of type '" "int""'"); + } + arg7 = (int)(val7); + } + if (obj7) { + arg8 = obj7; + } + if (obj8) { + res9 = SWIG_ConvertPtr(obj8, &argp9,SWIGTYPE_p_pdf_graft_map_s, 0 | 0 ); + if (!SWIG_IsOK(res9)) { + SWIG_exception_fail(SWIG_ArgError(res9), "in method '" "Page__showPDFpage" "', argument " "9"" of type '" "struct pdf_graft_map_s *""'"); + } + arg9 = (struct pdf_graft_map_s *)(argp9); + } + if (obj9) { + res10 = SWIG_AsCharPtrAndSize(obj9, &buf10, NULL, &alloc10); + if (!SWIG_IsOK(res10)) { + SWIG_exception_fail(SWIG_ArgError(res10), "in method '" "Page__showPDFpage" "', argument " "10"" of type '" "char *""'"); + } + arg10 = (char *)(buf10); } - arg2 = (struct fz_pixmap_s *)(argp2); { - result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_1(arg1,arg2); - if(!result) + result = (int)fz_page_s__showPDFpage(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10); + if(result<0) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_From_int((int)(result)); + if (alloc10 == SWIG_NEWOBJ) free((char*)buf10); return resultobj; fail: + if (alloc10 == SWIG_NEWOBJ) free((char*)buf10); return NULL; } -SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_insertImage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - float arg2 ; - float arg3 ; - struct fz_irect_s *arg4 = (struct fz_irect_s *) NULL ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + char *arg3 = (char *) NULL ; + struct fz_pixmap_s *arg4 = (struct fz_pixmap_s *) NULL ; + PyObject *arg5 = (PyObject *) NULL ; + int arg6 = (int) 1 ; + char *arg7 = (char *) NULL ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; - float val3 ; - int ecode3 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; void *argp4 = 0 ; int res4 = 0 ; + int val6 ; + int ecode6 = 0 ; + int res7 ; + char *buf7 = 0 ; + int alloc7 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; PyObject * obj3 = 0 ; - struct fz_pixmap_s *result = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO|O:new_Pixmap",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO|OOOOO:Page_insertImage",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_insertImage" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - ecode3 = SWIG_AsVal_float(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Pixmap" "', argument " "3"" of type '" "float""'"); - } - arg3 = (float)(val3); - if (obj3) { - res4 = SWIG_ConvertPtr(obj3, &argp4,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res4)) { - SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "new_Pixmap" "', argument " "4"" of type '" "struct fz_irect_s *""'"); + arg1 = (struct fz_page_s *)(argp1); + arg2 = obj1; + if (obj2) { + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_insertImage" "', argument " "3"" of type '" "char const *""'"); } - arg4 = (struct fz_irect_s *)(argp4); + arg3 = (char *)(buf3); } - { - result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_2(arg1,arg2,arg3,arg4); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; + if (obj3) { + res4 = SWIG_ConvertPtr(obj3, &argp4,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!SWIG_IsOK(res4)) { + SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "Page_insertImage" "', argument " "4"" of type '" "struct fz_pixmap_s *""'"); } + arg4 = (struct fz_pixmap_s *)(argp4); } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_3(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - int arg2 = (int) 1 ; - void *argp1 = 0 ; - int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_pixmap_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O|O:new_Pixmap",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + if (obj4) { + arg5 = obj4; } - arg1 = (struct fz_pixmap_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "int""'"); + if (obj5) { + ecode6 = SWIG_AsVal_int(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Page_insertImage" "', argument " "6"" of type '" "int""'"); } - arg2 = (int)(val2); + arg6 = (int)(val6); + } + if (obj6) { + res7 = SWIG_AsCharPtrAndSize(obj6, &buf7, NULL, &alloc7); + if (!SWIG_IsOK(res7)) { + SWIG_exception_fail(SWIG_ArgError(res7), "in method '" "Page_insertImage" "', argument " "7"" of type '" "char *""'"); + } + arg7 = (char *)(buf7); } { - result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_3(arg1,arg2); + result = (PyObject *)fz_page_s_insertImage(arg1,arg2,(char const *)arg3,arg4,arg5,arg6,arg7); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); + resultobj = result; + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + if (alloc7 == SWIG_NEWOBJ) free((char*)buf7); return resultobj; fail: + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + if (alloc7 == SWIG_NEWOBJ) free((char*)buf7); return NULL; } -SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_4(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page_insertFont(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; - int arg2 ; - int arg3 ; - PyObject *arg4 = (PyObject *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + char *arg2 = (char *) NULL ; + char *arg3 = (char *) NULL ; + PyObject *arg4 = (PyObject *) NULL ; int arg5 = (int) 0 ; + int arg6 = (int) 0 ; + int arg7 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; int val5 ; int ecode5 = 0 ; + int val6 ; + int ecode6 = 0 ; + int val7 ; + int ecode7 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; PyObject * obj3 = 0 ; PyObject * obj4 = 0 ; - struct fz_pixmap_s *result = 0 ; + PyObject * obj5 = 0 ; + PyObject * obj6 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOOO|O:new_Pixmap",&obj0,&obj1,&obj2,&obj3,&obj4)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|OOOOOO:Page_insertFont",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5,&obj6)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page_insertFont" "', argument " "1"" of type '" "struct fz_page_s *""'"); + } + arg1 = (struct fz_page_s *)(argp1); + if (obj1) { + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Page_insertFont" "', argument " "2"" of type '" "char const *""'"); + } + arg2 = (char *)(buf2); + } + if (obj2) { + res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Page_insertFont" "', argument " "3"" of type '" "char const *""'"); + } + arg3 = (char *)(buf3); + } + if (obj3) { + arg4 = obj3; } - arg1 = (struct fz_colorspace_s *)(argp1); - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Pixmap" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - arg4 = obj3; if (obj4) { ecode5 = SWIG_AsVal_int(obj4, &val5); if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "new_Pixmap" "', argument " "5"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "Page_insertFont" "', argument " "5"" of type '" "int""'"); } arg5 = (int)(val5); } + if (obj5) { + ecode6 = SWIG_AsVal_int(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Page_insertFont" "', argument " "6"" of type '" "int""'"); + } + arg6 = (int)(val6); + } + if (obj6) { + ecode7 = SWIG_AsVal_int(obj6, &val7); + if (!SWIG_IsOK(ecode7)) { + SWIG_exception_fail(SWIG_ArgError(ecode7), "in method '" "Page_insertFont" "', argument " "7"" of type '" "int""'"); + } + arg7 = (int)(val7); + } { - result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_4(arg1,arg2,arg3,arg4,arg5); + result = (PyObject *)fz_page_s_insertFont(arg1,(char const *)arg2,(char const *)arg3,arg4,arg5,arg6,arg7); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); + resultobj = result; + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return resultobj; fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return NULL; } -SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_5(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page__getContents(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - char *arg1 = (char *) 0 ; - int res1 ; - char *buf1 = 0 ; - int alloc1 = 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_pixmap_s *result = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:new_Pixmap",&obj0)) SWIG_fail; - res1 = SWIG_AsCharPtrAndSize(obj0, &buf1, NULL, &alloc1); + if (!PyArg_ParseTuple(args,(char *)"O:Page__getContents",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "char *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__getContents" "', argument " "1"" of type '" "struct fz_page_s *""'"); } - arg1 = (char *)(buf1); + arg1 = (struct fz_page_s *)(argp1); { - result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_5(arg1); + result = (PyObject *)fz_page_s__getContents(arg1); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); - if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); + resultobj = result; return resultobj; fail: - if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); return NULL; } -SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_6(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Page__setContents(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - PyObject *arg1 = (PyObject *) 0 ; + struct fz_page_s *arg1 = (struct fz_page_s *) 0 ; + int arg2 = (int) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; - struct fz_pixmap_s *result = 0 ; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:new_Pixmap",&obj0)) SWIG_fail; - arg1 = obj0; + if (!PyArg_ParseTuple(args,(char *)"O|O:Page__setContents",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_page_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Page__setContents" "', argument " "1"" of type '" "struct fz_page_s *""'"); + } + arg1 = (struct fz_page_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Page__setContents" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + } { - result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_6(arg1); + result = (PyObject *)fz_page_s__setContents(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_7(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *Page_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *obj; + if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; + SWIG_TypeNewClientData(SWIGTYPE_p_fz_page_s, SWIG_NewClientData(obj)); + return SWIG_Py_Void(); +} + +SWIGINTERN PyObject *_wrap_Pixmap_x_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; int arg2 ; void *argp1 = 0 ; int res1 = 0 ; @@ -16486,210 +17170,49 @@ SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_7(PyObject *SWIGUNUSEDPARM(self), Py int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:new_Pixmap",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_x_set",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_document_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_x_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_document_s *)(argp1); + arg1 = (struct fz_pixmap_s *)(argp1); ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_x_set" "', argument " "2"" of type '" "int""'"); } arg2 = (int)(val2); - { - result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_7(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); + if (arg1) (arg1)->x = arg2; + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Pixmap(PyObject *self, PyObject *args) { - Py_ssize_t argc; - PyObject *argv[6] = { - 0 - }; - Py_ssize_t ii; +SWIGINTERN PyObject *_wrap_Pixmap_x_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + int result; - if (!PyTuple_Check(args)) SWIG_fail; - argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 5) && (ii < argc); ii++) { - argv[ii] = PyTuple_GET_ITEM(args,ii); - } - if ((argc >= 1) && (argc <= 2)) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - if (argc <= 1) { - return _wrap_new_Pixmap__SWIG_3(self, args); - } - { - int res = SWIG_AsVal_int(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Pixmap__SWIG_3(self, args); - } - } - } - if (argc == 1) { - int _v; - int res = SWIG_AsCharPtrAndSize(argv[0], 0, NULL, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_new_Pixmap__SWIG_5(self, args); - } - } - if (argc == 1) { - int _v; - _v = (argv[0] != 0); - if (_v) { - return _wrap_new_Pixmap__SWIG_6(self, args); - } - } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_colorspace_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_new_Pixmap__SWIG_1(self, args); - } - } - } - if ((argc >= 2) && (argc <= 3)) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_colorspace_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - if (argc <= 2) { - return _wrap_new_Pixmap__SWIG_0(self, args); - } - { - int res = SWIG_AsVal_int(argv[2], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Pixmap__SWIG_0(self, args); - } - } - } - } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_document_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - { - int res = SWIG_AsVal_int(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Pixmap__SWIG_7(self, args); - } - } - } - if ((argc >= 3) && (argc <= 4)) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - { - int res = SWIG_AsVal_float(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[2], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - if (argc <= 3) { - return _wrap_new_Pixmap__SWIG_2(self, args); - } - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[3], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_new_Pixmap__SWIG_2(self, args); - } - } - } - } - } - if ((argc >= 4) && (argc <= 5)) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_colorspace_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - { - int res = SWIG_AsVal_int(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_int(argv[2], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - _v = (argv[3] != 0); - if (_v) { - if (argc <= 4) { - return _wrap_new_Pixmap__SWIG_4(self, args); - } - { - int res = SWIG_AsVal_int(argv[4], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Pixmap__SWIG_4(self, args); - } - } - } - } - } + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_x_get",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_x_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - + arg1 = (struct fz_pixmap_s *)(argp1); + result = (int) ((arg1)->x); + resultobj = SWIG_From_int((int)(result)); + return resultobj; fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'new_Pixmap'.\n" - " Possible C/C++ prototypes are:\n" - " fz_pixmap_s::fz_pixmap_s(struct fz_colorspace_s *,struct fz_irect_s const *,int)\n" - " fz_pixmap_s::fz_pixmap_s(struct fz_colorspace_s *,struct fz_pixmap_s *)\n" - " fz_pixmap_s::fz_pixmap_s(struct fz_pixmap_s *,float,float,struct fz_irect_s *)\n" - " fz_pixmap_s::fz_pixmap_s(struct fz_pixmap_s *,int)\n" - " fz_pixmap_s::fz_pixmap_s(struct fz_colorspace_s *,int,int,PyObject *,int)\n" - " fz_pixmap_s::fz_pixmap_s(char *)\n" - " fz_pixmap_s::fz_pixmap_s(PyObject *)\n" - " fz_pixmap_s::fz_pixmap_s(struct fz_document_s *,int)\n"); - return 0; + return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_shrink(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_y_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; int arg2 ; @@ -16700,18 +17223,18 @@ SWIGINTERN PyObject *_wrap_Pixmap_shrink(PyObject *SWIGUNUSEDPARM(self), PyObjec PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_shrink",&obj0,&obj1)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_y_set",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_shrink" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_y_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_shrink" "', argument " "2"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_y_set" "', argument " "2"" of type '" "int""'"); } arg2 = (int)(val2); - fz_pixmap_s_shrink(arg1,arg2); + if (arg1) (arg1)->y = arg2; resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -16719,77 +17242,51 @@ SWIGINTERN PyObject *_wrap_Pixmap_shrink(PyObject *SWIGUNUSEDPARM(self), PyObjec } -SWIGINTERN PyObject *_wrap_Pixmap_gammaWith(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_y_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - float arg2 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_gammaWith",&obj0,&obj1)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_y_get",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_gammaWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_y_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_gammaWith" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - fz_pixmap_s_gammaWith(arg1,arg2); - resultobj = SWIG_Py_Void(); + result = (int) ((arg1)->y); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_tintWith(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_w_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; int arg2 ; - int arg3 ; - int arg4 ; void *argp1 = 0 ; int res1 = 0 ; int val2 ; int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; - int val4 ; - int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOOO:Pixmap_tintWith",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_w_set",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_tintWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_w_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_tintWith" "', argument " "2"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_w_set" "', argument " "2"" of type '" "int""'"); } arg2 = (int)(val2); - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Pixmap_tintWith" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Pixmap_tintWith" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); - fz_pixmap_s_tintWith(arg1,arg2,arg3,arg4); + if (arg1) (arg1)->w = arg2; resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -16797,28 +17294,29 @@ SWIGINTERN PyObject *_wrap_Pixmap_tintWith(PyObject *SWIGUNUSEDPARM(self), PyObj } -SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_w_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_clearWith",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_w_get",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_clearWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_w_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - fz_pixmap_s_clearWith__SWIG_0(arg1); - resultobj = SWIG_Py_Void(); + result = (int) ((arg1)->w); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_h_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; int arg2 ; @@ -16829,18 +17327,18 @@ SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_1(PyObject *SWIGUNUSEDPARM(sel PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_clearWith",&obj0,&obj1)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_h_set",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_clearWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_h_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_clearWith" "', argument " "2"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_h_set" "', argument " "2"" of type '" "int""'"); } arg2 = (int)(val2); - fz_pixmap_s_clearWith__SWIG_1(arg1,arg2); + if (arg1) (arg1)->h = arg2; resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -16848,38 +17346,51 @@ SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_1(PyObject *SWIGUNUSEDPARM(sel } -SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_h_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + int result; + + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_h_get",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_h_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + } + arg1 = (struct fz_pixmap_s *)(argp1); + result = (int) ((arg1)->h); + resultobj = SWIG_From_int((int)(result)); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Pixmap_n_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; int arg2 ; - struct fz_irect_s *arg3 = (struct fz_irect_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; int val2 ; int ecode2 = 0 ; - void *argp3 = 0 ; - int res3 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO:Pixmap_clearWith",&obj0,&obj1,&obj2)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_n_set",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_clearWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_n_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_clearWith" "', argument " "2"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_n_set" "', argument " "2"" of type '" "int""'"); } arg2 = (int)(val2); - res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Pixmap_clearWith" "', argument " "3"" of type '" "struct fz_irect_s const *""'"); - } - arg3 = (struct fz_irect_s *)(argp3); - fz_pixmap_s_clearWith__SWIG_2(arg1,arg2,(struct fz_irect_s const *)arg3); + if (arg1) (arg1)->n = arg2; resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -16887,143 +17398,59 @@ SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_2(PyObject *SWIGUNUSEDPARM(sel } -SWIGINTERN PyObject *_wrap_Pixmap_clearWith(PyObject *self, PyObject *args) { - Py_ssize_t argc; - PyObject *argv[4] = { - 0 - }; - Py_ssize_t ii; - - if (!PyTuple_Check(args)) SWIG_fail; - argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 3) && (ii < argc); ii++) { - argv[ii] = PyTuple_GET_ITEM(args,ii); - } - if (argc == 1) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_Pixmap_clearWith__SWIG_0(self, args); - } - } - if (argc == 2) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - { - int res = SWIG_AsVal_int(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_Pixmap_clearWith__SWIG_1(self, args); - } - } - } - if (argc == 3) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - { - int res = SWIG_AsVal_int(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[2], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_Pixmap_clearWith__SWIG_2(self, args); - } - } - } - } - -fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'Pixmap_clearWith'.\n" - " Possible C/C++ prototypes are:\n" - " fz_pixmap_s::clearWith()\n" - " fz_pixmap_s::clearWith(int)\n" - " fz_pixmap_s::clearWith(int,struct fz_irect_s const *)\n"); - return 0; -} - - -SWIGINTERN PyObject *_wrap_Pixmap_copyPixmap(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_n_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - struct fz_pixmap_s *arg2 = (struct fz_pixmap_s *) 0 ; - struct fz_irect_s *arg3 = (struct fz_irect_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - void *argp3 = 0 ; - int res3 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject *result = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OOO:Pixmap_copyPixmap",&obj0,&obj1,&obj2)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_n_get",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_copyPixmap" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_n_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Pixmap_copyPixmap" "', argument " "2"" of type '" "struct fz_pixmap_s *""'"); - } - arg2 = (struct fz_pixmap_s *)(argp2); - res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Pixmap_copyPixmap" "', argument " "3"" of type '" "struct fz_irect_s const *""'"); - } - arg3 = (struct fz_irect_s *)(argp3); - { - result = (PyObject *)fz_pixmap_s_copyPixmap(arg1,arg2,(struct fz_irect_s const *)arg3); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; + result = (int) ((arg1)->n); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_stride(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_xres_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + int arg2 ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; - int result; + PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_stride",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_xres_set",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_stride" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_xres_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - result = (int)fz_pixmap_s_stride(arg1); - resultobj = SWIG_From_int((int)(result)); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_xres_set" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + if (arg1) (arg1)->xres = arg2; + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_alpha(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_xres_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; @@ -17031,13 +17458,13 @@ SWIGINTERN PyObject *_wrap_Pixmap_alpha(PyObject *SWIGUNUSEDPARM(self), PyObject PyObject * obj0 = 0 ; int result; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_alpha",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_xres_get",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_alpha" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_xres_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - result = (int)fz_pixmap_s_alpha(arg1); + result = (int) ((arg1)->xres); resultobj = SWIG_From_int((int)(result)); return resultobj; fail: @@ -17045,547 +17472,600 @@ SWIGINTERN PyObject *_wrap_Pixmap_alpha(PyObject *SWIGUNUSEDPARM(self), PyObject } -SWIGINTERN PyObject *_wrap_Pixmap_colorspace(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_yres_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + int arg2 ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; - struct fz_colorspace_s *result = 0 ; + PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_colorspace",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_yres_set",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_colorspace" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_yres_set" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - result = (struct fz_colorspace_s *)fz_pixmap_s_colorspace(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_yres_set" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + if (arg1) (arg1)->yres = arg2; + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_irect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_yres_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_irect_s *result = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_irect",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_yres_get",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_irect" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_yres_get" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - result = (struct fz_irect_s *)fz_pixmap_s_irect(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_irect_s, 0 | 0 ); + result = (int) ((arg1)->yres); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_size(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_delete_Pixmap(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - int result; - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_size",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:delete_Pixmap",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_DISOWN | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_size" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Pixmap" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - result = (int)fz_pixmap_s_size(arg1); - resultobj = SWIG_From_int((int)(result)); + delete_fz_pixmap_s(arg1); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_setAlpha(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - PyObject *arg2 = (PyObject *) NULL ; + struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + int arg3 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int val3 ; + int ecode3 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject *result = 0 ; + PyObject * obj2 = 0 ; + struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O|O:Pixmap_setAlpha",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO|O:new_Pixmap",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_setAlpha" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - if (obj1) { - arg2 = obj1; + arg1 = (struct fz_colorspace_s *)(argp1); + arg2 = obj1; + if (obj2) { + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Pixmap" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); } { - result = (PyObject *)fz_pixmap_s_setAlpha(arg1,arg2); + result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_0(arg1,arg2,arg3); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = result; + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_getPNGData(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - int arg2 = (int) -1 ; + struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; + struct fz_pixmap_s *arg2 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - int val2 ; - int ecode2 = 0 ; + void *argp2 = 0 ; + int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject *result = 0 ; + struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O|O:Pixmap_getPNGData",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:new_Pixmap",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_getPNGData" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); } - arg1 = (struct fz_pixmap_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_getPNGData" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); + arg1 = (struct fz_colorspace_s *)(argp1); + res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "struct fz_pixmap_s *""'"); } + arg2 = (struct fz_pixmap_s *)(argp2); { - result = (PyObject *)fz_pixmap_s_getPNGData(arg1,arg2); + result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_1(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = result; + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap__writeIMG(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - char *arg2 = (char *) 0 ; - int arg3 ; - int arg4 = (int) -1 ; + float arg2 ; + float arg3 ; + PyObject *arg4 = (PyObject *) NULL ; void *argp1 = 0 ; int res1 = 0 ; - int res2 ; - char *buf2 = 0 ; - int alloc2 = 0 ; - int val3 ; + float val2 ; + int ecode2 = 0 ; + float val3 ; int ecode3 = 0 ; - int val4 ; - int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; PyObject * obj3 = 0 ; - PyObject *result = 0 ; + struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO|O:Pixmap__writeIMG",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OOO|O:new_Pixmap",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap__writeIMG" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); - res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Pixmap__writeIMG" "', argument " "2"" of type '" "char *""'"); - } - arg2 = (char *)(buf2); - ecode3 = SWIG_AsVal_int(obj2, &val3); + ecode2 = SWIG_AsVal_float(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "float""'"); + } + arg2 = (float)(val2); + ecode3 = SWIG_AsVal_float(obj2, &val3); if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Pixmap__writeIMG" "', argument " "3"" of type '" "int""'"); + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Pixmap" "', argument " "3"" of type '" "float""'"); } - arg3 = (int)(val3); + arg3 = (float)(val3); if (obj3) { - ecode4 = SWIG_AsVal_int(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Pixmap__writeIMG" "', argument " "4"" of type '" "int""'"); - } - arg4 = (int)(val4); + arg4 = obj3; } { - result = (PyObject *)fz_pixmap_s__writeIMG(arg1,arg2,arg3,arg4); + result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_2(arg1,arg2,arg3,arg4); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = result; - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); return resultobj; fail: - if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } -SWIGINTERN PyObject *_wrap_Pixmap_invertIRect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_3(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - struct fz_irect_s *arg2 = (struct fz_irect_s *) NULL ; + int arg2 = (int) 1 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O|O:Pixmap_invertIRect",&obj0,&obj1)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O|O:new_Pixmap",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_invertIRect" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } arg1 = (struct fz_pixmap_s *)(argp1); if (obj1) { - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Pixmap_invertIRect" "', argument " "2"" of type '" "struct fz_irect_s const *""'"); - } - arg2 = (struct fz_irect_s *)(argp2); - } - fz_pixmap_s_invertIRect(arg1,(struct fz_irect_s const *)arg2); - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Pixmap_samples(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_samples",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_samples" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); - } - arg1 = (struct fz_pixmap_s *)(argp1); - result = (PyObject *)fz_pixmap_s_samples(arg1); - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *Pixmap_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_pixmap_s, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); -} - -SWIGINTERN PyObject *_wrap_delete_Colorspace(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:delete_Colorspace",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, SWIG_POINTER_DISOWN | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Colorspace" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); } - arg1 = (struct fz_colorspace_s *)(argp1); - delete_fz_colorspace_s(arg1); - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Colorspace(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - int arg1 ; - int val1 ; - int ecode1 = 0 ; - PyObject * obj0 = 0 ; - struct fz_colorspace_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:new_Colorspace",&obj0)) SWIG_fail; - ecode1 = SWIG_AsVal_int(obj0, &val1); - if (!SWIG_IsOK(ecode1)) { - SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_Colorspace" "', argument " "1"" of type '" "int""'"); - } - arg1 = (int)(val1); - result = (struct fz_colorspace_s *)new_fz_colorspace_s(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_colorspace_s, SWIG_POINTER_NEW | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Colorspace_n(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"O:Colorspace_n",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Colorspace_n" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); + { + result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_3(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } } - arg1 = (struct fz_colorspace_s *)(argp1); - result = (int)fz_colorspace_s_n(arg1); - resultobj = SWIG_From_int((int)(result)); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Colorspace__name(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_4(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; + int arg2 ; + int arg3 ; + PyObject *arg4 = (PyObject *) 0 ; + int arg5 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; + int val3 ; + int ecode3 = 0 ; + int val5 ; + int ecode5 = 0 ; PyObject * obj0 = 0 ; - char *result = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Colorspace__name",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OOOO|O:new_Pixmap",&obj0,&obj1,&obj2,&obj3,&obj4)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Colorspace__name" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); } arg1 = (struct fz_colorspace_s *)(argp1); - result = (char *)fz_colorspace_s__name(arg1); - resultobj = SWIG_FromCharPtr((const char *)result); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Pixmap" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + arg4 = obj3; + if (obj4) { + ecode5 = SWIG_AsVal_int(obj4, &val5); + if (!SWIG_IsOK(ecode5)) { + SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "new_Pixmap" "', argument " "5"" of type '" "int""'"); + } + arg5 = (int)(val5); + } + { + result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_4(arg1,arg2,arg3,arg4,arg5); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *Colorspace_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_colorspace_s, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); -} - -SWIGINTERN PyObject *_wrap_new_Device__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_5(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; - struct fz_irect_s *arg2 = (struct fz_irect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; + char *arg1 = (char *) 0 ; + int res1 ; + char *buf1 = 0 ; + int alloc1 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct DeviceWrapper *result = 0 ; + struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:new_Device",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:new_Pixmap",&obj0)) SWIG_fail; + res1 = SWIG_AsCharPtrAndSize(obj0, &buf1, NULL, &alloc1); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Device" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); - } - arg1 = (struct fz_pixmap_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_irect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "new_Device" "', argument " "2"" of type '" "struct fz_irect_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "char *""'"); } - arg2 = (struct fz_irect_s *)(argp2); + arg1 = (char *)(buf1); { - result = (struct DeviceWrapper *)new_DeviceWrapper__SWIG_0(arg1,arg2); + result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_5(arg1); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_DeviceWrapper, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); + if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); return resultobj; fail: + if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); return NULL; } -SWIGINTERN PyObject *_wrap_new_Device__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_6(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_display_list_s *arg1 = (struct fz_display_list_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; + PyObject *arg1 = (PyObject *) 0 ; PyObject * obj0 = 0 ; - struct DeviceWrapper *result = 0 ; + struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:new_Device",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_display_list_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Device" "', argument " "1"" of type '" "struct fz_display_list_s *""'"); - } - arg1 = (struct fz_display_list_s *)(argp1); + if (!PyArg_ParseTuple(args,(char *)"O:new_Pixmap",&obj0)) SWIG_fail; + arg1 = obj0; { - result = (struct DeviceWrapper *)new_DeviceWrapper__SWIG_1(arg1); + result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_6(arg1); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_DeviceWrapper, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Device__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap__SWIG_7(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_stext_page_s *arg1 = (struct fz_stext_page_s *) 0 ; - int arg2 = (int) 0 ; + struct fz_document_s *arg1 = (struct fz_document_s *) 0 ; + int arg2 ; void *argp1 = 0 ; int res1 = 0 ; int val2 ; int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - struct DeviceWrapper *result = 0 ; + struct fz_pixmap_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O|O:new_Device",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_stext_page_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:new_Pixmap",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_document_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Device" "', argument " "1"" of type '" "struct fz_stext_page_s *""'"); - } - arg1 = (struct fz_stext_page_s *)(argp1); - if (obj1) { - ecode2 = SWIG_AsVal_int(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Device" "', argument " "2"" of type '" "int""'"); - } - arg2 = (int)(val2); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Pixmap" "', argument " "1"" of type '" "struct fz_document_s *""'"); } + arg1 = (struct fz_document_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Pixmap" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); { - result = (struct DeviceWrapper *)new_DeviceWrapper__SWIG_2(arg1,arg2); + result = (struct fz_pixmap_s *)new_fz_pixmap_s__SWIG_7(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_DeviceWrapper, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Device(PyObject *self, PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Pixmap(PyObject *self, PyObject *args) { Py_ssize_t argc; - PyObject *argv[3] = { + PyObject *argv[6] = { 0 }; Py_ssize_t ii; if (!PyTuple_Check(args)) SWIG_fail; argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 2) && (ii < argc); ii++) { + for (ii = 0; (ii < 5) && (ii < argc); ii++) { argv[ii] = PyTuple_GET_ITEM(args,ii); } + if ((argc >= 1) && (argc <= 2)) { + int _v; + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); + _v = SWIG_CheckState(res); + if (_v) { + if (argc <= 1) { + return _wrap_new_Pixmap__SWIG_3(self, args); + } + { + int res = SWIG_AsVal_int(argv[1], NULL); + _v = SWIG_CheckState(res); + } + if (_v) { + return _wrap_new_Pixmap__SWIG_3(self, args); + } + } + } + if (argc == 1) { + int _v; + int res = SWIG_AsCharPtrAndSize(argv[0], 0, NULL, 0); + _v = SWIG_CheckState(res); + if (_v) { + return _wrap_new_Pixmap__SWIG_5(self, args); + } + } if (argc == 1) { + int _v; + _v = (argv[0] != 0); + if (_v) { + return _wrap_new_Pixmap__SWIG_6(self, args); + } + } + if (argc == 2) { int _v; void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_display_list_s, 0); + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_colorspace_s, 0); _v = SWIG_CheckState(res); if (_v) { - return _wrap_new_Device__SWIG_1(self, args); + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); + _v = SWIG_CheckState(res); + if (_v) { + return _wrap_new_Pixmap__SWIG_1(self, args); + } } } - if ((argc >= 1) && (argc <= 2)) { + if ((argc >= 2) && (argc <= 3)) { int _v; void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_stext_page_s, 0); + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_colorspace_s, 0); _v = SWIG_CheckState(res); if (_v) { - if (argc <= 1) { - return _wrap_new_Device__SWIG_2(self, args); + _v = (argv[1] != 0); + if (_v) { + if (argc <= 2) { + return _wrap_new_Pixmap__SWIG_0(self, args); + } + { + int res = SWIG_AsVal_int(argv[2], NULL); + _v = SWIG_CheckState(res); + } + if (_v) { + return _wrap_new_Pixmap__SWIG_0(self, args); + } } + } + } + if (argc == 2) { + int _v; + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_document_s, 0); + _v = SWIG_CheckState(res); + if (_v) { { int res = SWIG_AsVal_int(argv[1], NULL); _v = SWIG_CheckState(res); } if (_v) { - return _wrap_new_Device__SWIG_2(self, args); + return _wrap_new_Pixmap__SWIG_7(self, args); } } } - if (argc == 2) { + if ((argc >= 3) && (argc <= 4)) { int _v; void *vptr = 0; int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); _v = SWIG_CheckState(res); if (_v) { - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[1], &vptr, SWIGTYPE_p_fz_irect_s, 0); - _v = SWIG_CheckState(res); + { + int res = SWIG_AsVal_float(argv[1], NULL); + _v = SWIG_CheckState(res); + } if (_v) { - return _wrap_new_Device__SWIG_0(self, args); + { + int res = SWIG_AsVal_float(argv[2], NULL); + _v = SWIG_CheckState(res); + } + if (_v) { + if (argc <= 3) { + return _wrap_new_Pixmap__SWIG_2(self, args); + } + _v = (argv[3] != 0); + if (_v) { + return _wrap_new_Pixmap__SWIG_2(self, args); + } + } + } + } + } + if ((argc >= 4) && (argc <= 5)) { + int _v; + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_colorspace_s, 0); + _v = SWIG_CheckState(res); + if (_v) { + { + int res = SWIG_AsVal_int(argv[1], NULL); + _v = SWIG_CheckState(res); + } + if (_v) { + { + int res = SWIG_AsVal_int(argv[2], NULL); + _v = SWIG_CheckState(res); + } + if (_v) { + _v = (argv[3] != 0); + if (_v) { + if (argc <= 4) { + return _wrap_new_Pixmap__SWIG_4(self, args); + } + { + int res = SWIG_AsVal_int(argv[4], NULL); + _v = SWIG_CheckState(res); + } + if (_v) { + return _wrap_new_Pixmap__SWIG_4(self, args); + } + } + } } } } fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'new_Device'.\n" + SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'new_Pixmap'.\n" " Possible C/C++ prototypes are:\n" - " DeviceWrapper::DeviceWrapper(struct fz_pixmap_s *,struct fz_irect_s *)\n" - " DeviceWrapper::DeviceWrapper(struct fz_display_list_s *)\n" - " DeviceWrapper::DeviceWrapper(struct fz_stext_page_s *,int)\n"); + " fz_pixmap_s::fz_pixmap_s(struct fz_colorspace_s *,PyObject *,int)\n" + " fz_pixmap_s::fz_pixmap_s(struct fz_colorspace_s *,struct fz_pixmap_s *)\n" + " fz_pixmap_s::fz_pixmap_s(struct fz_pixmap_s *,float,float,PyObject *)\n" + " fz_pixmap_s::fz_pixmap_s(struct fz_pixmap_s *,int)\n" + " fz_pixmap_s::fz_pixmap_s(struct fz_colorspace_s *,int,int,PyObject *,int)\n" + " fz_pixmap_s::fz_pixmap_s(char *)\n" + " fz_pixmap_s::fz_pixmap_s(PyObject *)\n" + " fz_pixmap_s::fz_pixmap_s(struct fz_document_s *,int)\n"); return 0; } -SWIGINTERN PyObject *_wrap_delete_Device(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_shrink(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct DeviceWrapper *arg1 = (struct DeviceWrapper *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + int arg2 ; void *argp1 = 0 ; int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:delete_Device",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_DeviceWrapper, SWIG_POINTER_DISOWN | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_shrink",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Device" "', argument " "1"" of type '" "struct DeviceWrapper *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_shrink" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct DeviceWrapper *)(argp1); - delete_DeviceWrapper(arg1); + arg1 = (struct fz_pixmap_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_shrink" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + fz_pixmap_s_shrink(arg1,arg2); resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -17593,147 +18073,161 @@ SWIGINTERN PyObject *_wrap_delete_Device(PyObject *SWIGUNUSEDPARM(self), PyObjec } -SWIGINTERN PyObject *Device_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_DeviceWrapper, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); -} - -SWIGINTERN PyObject *_wrap__fz_pre_scale(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_gammaWith(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; float arg2 ; - float arg3 ; void *argp1 = 0 ; int res1 = 0 ; float val2 ; int ecode2 = 0 ; - float val3 ; - int ecode3 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_matrix_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO:_fz_pre_scale",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_gammaWith",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "_fz_pre_scale" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_gammaWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); + arg1 = (struct fz_pixmap_s *)(argp1); ecode2 = SWIG_AsVal_float(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "_fz_pre_scale" "', argument " "2"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_gammaWith" "', argument " "2"" of type '" "float""'"); } arg2 = (float)(val2); - ecode3 = SWIG_AsVal_float(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "_fz_pre_scale" "', argument " "3"" of type '" "float""'"); - } - arg3 = (float)(val3); - result = (struct fz_matrix_s *)fz_pre_scale(arg1,arg2,arg3); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + fz_pixmap_s_gammaWith(arg1,arg2); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap__fz_pre_shear(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_tintWith(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; - float arg3 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + int arg2 ; + int arg3 ; + int arg4 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; + int val2 ; int ecode2 = 0 ; - float val3 ; + int val3 ; int ecode3 = 0 ; + int val4 ; + int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; - struct fz_matrix_s *result = 0 ; + PyObject * obj3 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO:_fz_pre_shear",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOOO:Pixmap_tintWith",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "_fz_pre_shear" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_tintWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); + arg1 = (struct fz_pixmap_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "_fz_pre_shear" "', argument " "2"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_tintWith" "', argument " "2"" of type '" "int""'"); } - arg2 = (float)(val2); - ecode3 = SWIG_AsVal_float(obj2, &val3); + arg2 = (int)(val2); + ecode3 = SWIG_AsVal_int(obj2, &val3); if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "_fz_pre_shear" "', argument " "3"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Pixmap_tintWith" "', argument " "3"" of type '" "int""'"); } - arg3 = (float)(val3); - result = (struct fz_matrix_s *)fz_pre_shear(arg1,arg2,arg3); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + arg3 = (int)(val3); + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Pixmap_tintWith" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + fz_pixmap_s_tintWith(arg1,arg2,arg3,arg4); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap__fz_pre_rotate(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; + PyObject * obj0 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_clearWith",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_clearWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + } + arg1 = (struct fz_pixmap_s *)(argp1); + fz_pixmap_s_clearWith__SWIG_0(arg1); + resultobj = SWIG_Py_Void(); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + int arg2 ; + void *argp1 = 0 ; + int res1 = 0 ; + int val2 ; int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - struct fz_matrix_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:_fz_pre_rotate",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OO:Pixmap_clearWith",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "_fz_pre_rotate" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_clearWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); + arg1 = (struct fz_pixmap_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "_fz_pre_rotate" "', argument " "2"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_clearWith" "', argument " "2"" of type '" "int""'"); } - arg2 = (float)(val2); - result = (struct fz_matrix_s *)fz_pre_rotate(arg1,arg2); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + arg2 = (int)(val2); + fz_pixmap_s_clearWith__SWIG_1(arg1,arg2); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_a_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_clearWith__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + int arg2 ; + PyObject *arg3 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; + int val2 ; int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Matrix_a_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO:Pixmap_clearWith",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_a_set" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_clearWith" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); + arg1 = (struct fz_pixmap_s *)(argp1); + ecode2 = SWIG_AsVal_int(obj1, &val2); if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Matrix_a_set" "', argument " "2"" of type '" "float""'"); + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_clearWith" "', argument " "2"" of type '" "int""'"); } - arg2 = (float)(val2); - if (arg1) (arg1)->a = arg2; + arg2 = (int)(val2); + arg3 = obj2; + fz_pixmap_s_clearWith__SWIG_2(arg1,arg2,arg3); resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -17741,259 +18235,376 @@ SWIGINTERN PyObject *_wrap_Matrix_a_set(PyObject *SWIGUNUSEDPARM(self), PyObject } -SWIGINTERN PyObject *_wrap_Matrix_a_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_clearWith(PyObject *self, PyObject *args) { + Py_ssize_t argc; + PyObject *argv[4] = { + 0 + }; + Py_ssize_t ii; + + if (!PyTuple_Check(args)) SWIG_fail; + argc = args ? PyObject_Length(args) : 0; + for (ii = 0; (ii < 3) && (ii < argc); ii++) { + argv[ii] = PyTuple_GET_ITEM(args,ii); + } + if (argc == 1) { + int _v; + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); + _v = SWIG_CheckState(res); + if (_v) { + return _wrap_Pixmap_clearWith__SWIG_0(self, args); + } + } + if (argc == 2) { + int _v; + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); + _v = SWIG_CheckState(res); + if (_v) { + { + int res = SWIG_AsVal_int(argv[1], NULL); + _v = SWIG_CheckState(res); + } + if (_v) { + return _wrap_Pixmap_clearWith__SWIG_1(self, args); + } + } + } + if (argc == 3) { + int _v; + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); + _v = SWIG_CheckState(res); + if (_v) { + { + int res = SWIG_AsVal_int(argv[1], NULL); + _v = SWIG_CheckState(res); + } + if (_v) { + _v = (argv[2] != 0); + if (_v) { + return _wrap_Pixmap_clearWith__SWIG_2(self, args); + } + } + } + } + +fail: + SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'Pixmap_clearWith'.\n" + " Possible C/C++ prototypes are:\n" + " fz_pixmap_s::clearWith()\n" + " fz_pixmap_s::clearWith(int)\n" + " fz_pixmap_s::clearWith(int,PyObject *)\n"); + return 0; +} + + +SWIGINTERN PyObject *_wrap_Pixmap_copyPixmap(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + struct fz_pixmap_s *arg2 = (struct fz_pixmap_s *) 0 ; + PyObject *arg3 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; + void *argp2 = 0 ; + int res2 = 0 ; PyObject * obj0 = 0 ; - float result; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Matrix_a_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO:Pixmap_copyPixmap",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_a_get" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_copyPixmap" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - result = (float) ((arg1)->a); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_pixmap_s *)(argp1); + res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Pixmap_copyPixmap" "', argument " "2"" of type '" "struct fz_pixmap_s *""'"); + } + arg2 = (struct fz_pixmap_s *)(argp2); + arg3 = obj2; + { + result = (PyObject *)fz_pixmap_s_copyPixmap(arg1,arg2,arg3); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_b_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_stride(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OO:Matrix_b_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_stride",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_b_set" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_stride" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Matrix_b_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->b = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_pixmap_s *)(argp1); + result = (int)fz_pixmap_s_stride(arg1); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_b_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_alpha(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - float result; + int result; - if (!PyArg_ParseTuple(args,(char *)"O:Matrix_b_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_alpha",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_b_get" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_alpha" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - result = (float) ((arg1)->b); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_pixmap_s *)(argp1); + result = (int)fz_pixmap_s_alpha(arg1); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_c_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_colorspace(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; + struct fz_colorspace_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Matrix_c_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_colorspace",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_c_set" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_colorspace" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Matrix_c_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->c = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_pixmap_s *)(argp1); + result = (struct fz_colorspace_s *)fz_pixmap_s_colorspace(arg1); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_c_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_irect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - float result; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Matrix_c_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_irect",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_c_get" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_irect" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - result = (float) ((arg1)->c); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_pixmap_s *)(argp1); + result = (PyObject *)fz_pixmap_s_irect(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_d_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_size(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"OO:Matrix_d_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_size",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_d_set" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_size" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Matrix_d_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->d = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_pixmap_s *)(argp1); + result = (int)fz_pixmap_s_size(arg1); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_d_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_setAlpha(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + PyObject *arg2 = (PyObject *) NULL ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - float result; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Matrix_d_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|O:Pixmap_setAlpha",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_d_get" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_setAlpha" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - result = (float) ((arg1)->d); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_pixmap_s *)(argp1); + if (obj1) { + arg2 = obj1; + } + { + result = (PyObject *)fz_pixmap_s_setAlpha(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_e_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_getPNGData(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + int arg2 = (int) -1 ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; + int val2 ; int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Matrix_e_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|O:Pixmap_getPNGData",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_e_set" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_getPNGData" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Matrix_e_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->e = arg2; - resultobj = SWIG_Py_Void(); + arg1 = (struct fz_pixmap_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Pixmap_getPNGData" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + } + { + result = (PyObject *)fz_pixmap_s_getPNGData(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_e_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap__writeIMG(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + char *arg2 = (char *) 0 ; + int arg3 ; + int arg4 = (int) -1 ; void *argp1 = 0 ; int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + int val3 ; + int ecode3 = 0 ; + int val4 ; + int ecode4 = 0 ; PyObject * obj0 = 0 ; - float result; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Matrix_e_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO|O:Pixmap__writeIMG",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_e_get" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap__writeIMG" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - result = (float) ((arg1)->e); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_pixmap_s *)(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Pixmap__writeIMG" "', argument " "2"" of type '" "char *""'"); + } + arg2 = (char *)(buf2); + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Pixmap__writeIMG" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + if (obj3) { + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Pixmap__writeIMG" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + } + { + result = (PyObject *)fz_pixmap_s__writeIMG(arg1,arg2,arg3,arg4); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return NULL; } -SWIGINTERN PyObject *_wrap_Matrix_f_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_invertIRect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + PyObject *arg2 = (PyObject *) NULL ; void *argp1 = 0 ; int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Matrix_f_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O|O:Pixmap_invertIRect",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_f_set" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_invertIRect" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Matrix_f_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->f = arg2; + arg1 = (struct fz_pixmap_s *)(argp1); + if (obj1) { + arg2 = obj1; + } + fz_pixmap_s_invertIRect(arg1,arg2); resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -18001,501 +18612,317 @@ SWIGINTERN PyObject *_wrap_Matrix_f_set(PyObject *SWIGUNUSEDPARM(self), PyObject } -SWIGINTERN PyObject *_wrap_Matrix_f_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Pixmap_samples(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - float result; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Matrix_f_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Pixmap_samples",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_f_get" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Pixmap_samples" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); } - arg1 = (struct fz_matrix_s *)(argp1); - result = (float) ((arg1)->f); - resultobj = SWIG_From_float((float)(result)); + arg1 = (struct fz_pixmap_s *)(argp1); + result = (PyObject *)fz_pixmap_s_samples(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Matrix__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *Pixmap_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *obj; + if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; + SWIG_TypeNewClientData(SWIGTYPE_p_fz_pixmap_s, SWIG_NewClientData(obj)); + return SWIG_Py_Void(); +} + +SWIGINTERN PyObject *_wrap_delete_Colorspace(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *result = 0 ; + struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; - if (!PyArg_ParseTuple(args,(char *)":new_Matrix")) SWIG_fail; - result = (struct fz_matrix_s *)calloc(1, sizeof(struct fz_matrix_s)); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, SWIG_POINTER_NEW | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:delete_Colorspace",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, SWIG_POINTER_DISOWN | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Colorspace" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); + } + arg1 = (struct fz_colorspace_s *)(argp1); + delete_fz_colorspace_s(arg1); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_delete_Matrix(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Colorspace(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; + int arg1 ; + int val1 ; + int ecode1 = 0 ; PyObject * obj0 = 0 ; + struct fz_colorspace_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:delete_Matrix",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, SWIG_POINTER_DISOWN | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Matrix" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); - } - arg1 = (struct fz_matrix_s *)(argp1); - delete_fz_matrix_s(arg1); - resultobj = SWIG_Py_Void(); + if (!PyArg_ParseTuple(args,(char *)"O:new_Colorspace",&obj0)) SWIG_fail; + ecode1 = SWIG_AsVal_int(obj0, &val1); + if (!SWIG_IsOK(ecode1)) { + SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_Colorspace" "', argument " "1"" of type '" "int""'"); + } + arg1 = (int)(val1); + result = (struct fz_colorspace_s *)new_fz_colorspace_s(arg1); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_colorspace_s, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Matrix__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Colorspace_n(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; + struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_matrix_s *result = 0 ; + int result; - if (!PyArg_ParseTuple(args,(char *)"O:new_Matrix",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:Colorspace_n",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Matrix" "', argument " "1"" of type '" "struct fz_matrix_s const *""'"); - } - arg1 = (struct fz_matrix_s *)(argp1); - { - result = (struct fz_matrix_s *)new_fz_matrix_s__SWIG_1((struct fz_matrix_s const *)arg1); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Colorspace_n" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, SWIG_POINTER_NEW | 0 ); + arg1 = (struct fz_colorspace_s *)(argp1); + result = (int)fz_colorspace_s_n(arg1); + resultobj = SWIG_From_int((int)(result)); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Matrix__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - float arg1 ; - float arg2 ; - int arg3 = (int) 0 ; - float val1 ; - int ecode1 = 0 ; - float val2 ; - int ecode2 = 0 ; - int val3 ; - int ecode3 = 0 ; +SWIGINTERN PyObject *_wrap_Colorspace__name(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_colorspace_s *arg1 = (struct fz_colorspace_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_matrix_s *result = 0 ; + char *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO|O:new_Matrix",&obj0,&obj1,&obj2)) SWIG_fail; - ecode1 = SWIG_AsVal_float(obj0, &val1); - if (!SWIG_IsOK(ecode1)) { - SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_Matrix" "', argument " "1"" of type '" "float""'"); - } - arg1 = (float)(val1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Matrix" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (obj2) { - ecode3 = SWIG_AsVal_int(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Matrix" "', argument " "3"" of type '" "int""'"); - } - arg3 = (int)(val3); - } - { - result = (struct fz_matrix_s *)new_fz_matrix_s__SWIG_2(arg1,arg2,arg3); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } + if (!PyArg_ParseTuple(args,(char *)"O:Colorspace__name",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Colorspace__name" "', argument " "1"" of type '" "struct fz_colorspace_s *""'"); } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, SWIG_POINTER_NEW | 0 ); + arg1 = (struct fz_colorspace_s *)(argp1); + result = (char *)fz_colorspace_s__name(arg1); + resultobj = SWIG_FromCharPtr((const char *)result); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Matrix__SWIG_3(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *Colorspace_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *obj; + if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; + SWIG_TypeNewClientData(SWIGTYPE_p_fz_colorspace_s, SWIG_NewClientData(obj)); + return SWIG_Py_Void(); +} + +SWIGINTERN PyObject *_wrap_new_Device__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - float arg1 ; - float arg2 ; - float arg3 ; - float arg4 ; - float arg5 ; - float arg6 ; - float val1 ; - int ecode1 = 0 ; - float val2 ; - int ecode2 = 0 ; - float val3 ; - int ecode3 = 0 ; - float val4 ; - int ecode4 = 0 ; - float val5 ; - int ecode5 = 0 ; - float val6 ; - int ecode6 = 0 ; + struct fz_pixmap_s *arg1 = (struct fz_pixmap_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject * obj3 = 0 ; - PyObject * obj4 = 0 ; - PyObject * obj5 = 0 ; - struct fz_matrix_s *result = 0 ; + struct DeviceWrapper *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOOOOO:new_Matrix",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5)) SWIG_fail; - ecode1 = SWIG_AsVal_float(obj0, &val1); - if (!SWIG_IsOK(ecode1)) { - SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_Matrix" "', argument " "1"" of type '" "float""'"); - } - arg1 = (float)(val1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Matrix" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - ecode3 = SWIG_AsVal_float(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "new_Matrix" "', argument " "3"" of type '" "float""'"); - } - arg3 = (float)(val3); - ecode4 = SWIG_AsVal_float(obj3, &val4); - if (!SWIG_IsOK(ecode4)) { - SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "new_Matrix" "', argument " "4"" of type '" "float""'"); - } - arg4 = (float)(val4); - ecode5 = SWIG_AsVal_float(obj4, &val5); - if (!SWIG_IsOK(ecode5)) { - SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "new_Matrix" "', argument " "5"" of type '" "float""'"); - } - arg5 = (float)(val5); - ecode6 = SWIG_AsVal_float(obj5, &val6); - if (!SWIG_IsOK(ecode6)) { - SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "new_Matrix" "', argument " "6"" of type '" "float""'"); - } - arg6 = (float)(val6); + if (!PyArg_ParseTuple(args,(char *)"OO:new_Device",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Device" "', argument " "1"" of type '" "struct fz_pixmap_s *""'"); + } + arg1 = (struct fz_pixmap_s *)(argp1); + arg2 = obj1; { - result = (struct fz_matrix_s *)new_fz_matrix_s__SWIG_3(arg1,arg2,arg3,arg4,arg5,arg6); + result = (struct DeviceWrapper *)new_DeviceWrapper__SWIG_0(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_DeviceWrapper, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Matrix__SWIG_4(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Device__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - float arg1 ; - float val1 ; - int ecode1 = 0 ; + struct fz_display_list_s *arg1 = (struct fz_display_list_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_matrix_s *result = 0 ; + struct DeviceWrapper *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:new_Matrix",&obj0)) SWIG_fail; - ecode1 = SWIG_AsVal_float(obj0, &val1); - if (!SWIG_IsOK(ecode1)) { - SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_Matrix" "', argument " "1"" of type '" "float""'"); - } - arg1 = (float)(val1); + if (!PyArg_ParseTuple(args,(char *)"O:new_Device",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_display_list_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Device" "', argument " "1"" of type '" "struct fz_display_list_s *""'"); + } + arg1 = (struct fz_display_list_s *)(argp1); { - result = (struct fz_matrix_s *)new_fz_matrix_s__SWIG_4(arg1); + result = (struct DeviceWrapper *)new_DeviceWrapper__SWIG_1(arg1); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_DeviceWrapper, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Matrix__SWIG_5(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Device__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - PyObject *arg1 = (PyObject *) 0 ; + struct fz_stext_page_s *arg1 = (struct fz_stext_page_s *) 0 ; + int arg2 = (int) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; PyObject * obj0 = 0 ; - struct fz_matrix_s *result = 0 ; + PyObject * obj1 = 0 ; + struct DeviceWrapper *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:new_Matrix",&obj0)) SWIG_fail; - arg1 = obj0; + if (!PyArg_ParseTuple(args,(char *)"O|O:new_Device",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_stext_page_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Device" "', argument " "1"" of type '" "struct fz_stext_page_s *""'"); + } + arg1 = (struct fz_stext_page_s *)(argp1); + if (obj1) { + ecode2 = SWIG_AsVal_int(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Device" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + } { - result = (struct fz_matrix_s *)new_fz_matrix_s__SWIG_5(arg1); + result = (struct DeviceWrapper *)new_DeviceWrapper__SWIG_2(arg1,arg2); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); return NULL; } } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, SWIG_POINTER_NEW | 0 ); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_DeviceWrapper, SWIG_POINTER_NEW | 0 ); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_new_Matrix(PyObject *self, PyObject *args) { +SWIGINTERN PyObject *_wrap_new_Device(PyObject *self, PyObject *args) { Py_ssize_t argc; - PyObject *argv[7] = { + PyObject *argv[3] = { 0 }; Py_ssize_t ii; if (!PyTuple_Check(args)) SWIG_fail; argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 6) && (ii < argc); ii++) { + for (ii = 0; (ii < 2) && (ii < argc); ii++) { argv[ii] = PyTuple_GET_ITEM(args,ii); } - if (argc == 0) { - return _wrap_new_Matrix__SWIG_0(self, args); - } if (argc == 1) { int _v; void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_matrix_s, 0); + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_display_list_s, 0); _v = SWIG_CheckState(res); if (_v) { - return _wrap_new_Matrix__SWIG_1(self, args); - } - } - if (argc == 1) { - int _v; - { - int res = SWIG_AsVal_float(argv[0], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Matrix__SWIG_4(self, args); - } - } - if (argc == 1) { - int _v; - _v = (argv[0] != 0); - if (_v) { - return _wrap_new_Matrix__SWIG_5(self, args); + return _wrap_new_Device__SWIG_1(self, args); } } - if ((argc >= 2) && (argc <= 3)) { + if ((argc >= 1) && (argc <= 2)) { int _v; - { - int res = SWIG_AsVal_float(argv[0], NULL); - _v = SWIG_CheckState(res); - } + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_stext_page_s, 0); + _v = SWIG_CheckState(res); if (_v) { + if (argc <= 1) { + return _wrap_new_Device__SWIG_2(self, args); + } { - int res = SWIG_AsVal_float(argv[1], NULL); + int res = SWIG_AsVal_int(argv[1], NULL); _v = SWIG_CheckState(res); } if (_v) { - if (argc <= 2) { - return _wrap_new_Matrix__SWIG_2(self, args); - } - { - int res = SWIG_AsVal_int(argv[2], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Matrix__SWIG_2(self, args); - } + return _wrap_new_Device__SWIG_2(self, args); } } } - if (argc == 6) { + if (argc == 2) { int _v; - { - int res = SWIG_AsVal_float(argv[0], NULL); - _v = SWIG_CheckState(res); - } + void *vptr = 0; + int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_pixmap_s, 0); + _v = SWIG_CheckState(res); if (_v) { - { - int res = SWIG_AsVal_float(argv[1], NULL); - _v = SWIG_CheckState(res); - } + _v = (argv[1] != 0); if (_v) { - { - int res = SWIG_AsVal_float(argv[2], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[3], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[4], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[5], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Matrix__SWIG_3(self, args); - } - } - } - } + return _wrap_new_Device__SWIG_0(self, args); } } } fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'new_Matrix'.\n" + SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'new_Device'.\n" " Possible C/C++ prototypes are:\n" - " fz_matrix_s::fz_matrix_s()\n" - " fz_matrix_s::fz_matrix_s(struct fz_matrix_s const *)\n" - " fz_matrix_s::fz_matrix_s(float,float,int)\n" - " fz_matrix_s::fz_matrix_s(float,float,float,float,float,float)\n" - " fz_matrix_s::fz_matrix_s(float)\n" - " fz_matrix_s::fz_matrix_s(PyObject *)\n"); + " DeviceWrapper::DeviceWrapper(struct fz_pixmap_s *,PyObject *)\n" + " DeviceWrapper::DeviceWrapper(struct fz_display_list_s *)\n" + " DeviceWrapper::DeviceWrapper(struct fz_stext_page_s *,int)\n"); return 0; } -SWIGINTERN PyObject *_wrap_Matrix_invert(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - struct fz_matrix_s *arg2 = (struct fz_matrix_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - int result; - - if (!PyArg_ParseTuple(args,(char *)"OO:Matrix_invert",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_invert" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); - } - arg1 = (struct fz_matrix_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Matrix_invert" "', argument " "2"" of type '" "struct fz_matrix_s const *""'"); - } - arg2 = (struct fz_matrix_s *)(argp2); - result = (int)fz_matrix_s_invert(arg1,(struct fz_matrix_s const *)arg2); - resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Matrix_preTranslate(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - float arg2 ; - float arg3 ; - void *argp1 = 0 ; - int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; - float val3 ; - int ecode3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_matrix_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO:Matrix_preTranslate",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_preTranslate" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); - } - arg1 = (struct fz_matrix_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Matrix_preTranslate" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - ecode3 = SWIG_AsVal_float(obj2, &val3); - if (!SWIG_IsOK(ecode3)) { - SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Matrix_preTranslate" "', argument " "3"" of type '" "float""'"); - } - arg3 = (float)(val3); - result = (struct fz_matrix_s *)fz_matrix_s_preTranslate(arg1,arg2,arg3); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Matrix_concat(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_delete_Device(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_matrix_s *arg1 = (struct fz_matrix_s *) 0 ; - struct fz_matrix_s *arg2 = (struct fz_matrix_s *) 0 ; - struct fz_matrix_s *arg3 = (struct fz_matrix_s *) 0 ; + struct DeviceWrapper *arg1 = (struct DeviceWrapper *) 0 ; void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - void *argp3 = 0 ; - int res3 = 0 ; + int res1 = 0 ; PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - struct fz_matrix_s *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OOO:Matrix_concat",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:delete_Device",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_DeviceWrapper, SWIG_POINTER_DISOWN | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Matrix_concat" "', argument " "1"" of type '" "struct fz_matrix_s *""'"); - } - arg1 = (struct fz_matrix_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Matrix_concat" "', argument " "2"" of type '" "struct fz_matrix_s *""'"); - } - arg2 = (struct fz_matrix_s *)(argp2); - res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Matrix_concat" "', argument " "3"" of type '" "struct fz_matrix_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Device" "', argument " "1"" of type '" "struct DeviceWrapper *""'"); } - arg3 = (struct fz_matrix_s *)(argp3); - result = (struct fz_matrix_s *)fz_matrix_s_concat(arg1,arg2,arg3); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_matrix_s, 0 | 0 ); + arg1 = (struct DeviceWrapper *)(argp1); + delete_DeviceWrapper(arg1); + resultobj = SWIG_Py_Void(); return resultobj; fail: return NULL; } -SWIGINTERN PyObject *Matrix_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *Device_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *obj; if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_matrix_s, SWIG_NewClientData(obj)); + SWIG_TypeNewClientData(SWIGTYPE_p_DeviceWrapper, SWIG_NewClientData(obj)); return SWIG_Py_Void(); } @@ -18647,351 +19074,26 @@ SWIGINTERN PyObject *_wrap_Outline_isExternal(PyObject *SWIGUNUSEDPARM(self), Py arg1 = (struct fz_outline_s *)(argp1); result = (int)fz_outline_s_isExternal(arg1); resultobj = SWIG_From_int((int)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_delete_Outline(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_outline_s *arg1 = (struct fz_outline_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:delete_Outline",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_outline_s, SWIG_POINTER_DISOWN | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Outline" "', argument " "1"" of type '" "struct fz_outline_s *""'"); - } - arg1 = (struct fz_outline_s *)(argp1); - free((char *) arg1); - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *Outline_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_outline_s, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); -} - -SWIGINTERN PyObject *_wrap__fz_transform_point(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; - struct fz_matrix_s *arg2 = (struct fz_matrix_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_point_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:_fz_transform_point",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "_fz_transform_point" "', argument " "1"" of type '" "struct fz_point_s *""'"); - } - arg1 = (struct fz_point_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "_fz_transform_point" "', argument " "2"" of type '" "struct fz_matrix_s const *""'"); - } - arg2 = (struct fz_matrix_s *)(argp2); - result = (struct fz_point_s *)fz_transform_point(arg1,(struct fz_matrix_s const *)arg2); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_point_s, 0 | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Point_x_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; - float arg2 ; - void *argp1 = 0 ; - int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Point_x_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Point_x_set" "', argument " "1"" of type '" "struct fz_point_s *""'"); - } - arg1 = (struct fz_point_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Point_x_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->x = arg2; - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Point_x_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - float result; - - if (!PyArg_ParseTuple(args,(char *)"O:Point_x_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Point_x_get" "', argument " "1"" of type '" "struct fz_point_s *""'"); - } - arg1 = (struct fz_point_s *)(argp1); - result = (float) ((arg1)->x); - resultobj = SWIG_From_float((float)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Point_y_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; - float arg2 ; - void *argp1 = 0 ; - int res1 = 0 ; - float val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Point_y_set",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Point_y_set" "', argument " "1"" of type '" "struct fz_point_s *""'"); - } - arg1 = (struct fz_point_s *)(argp1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Point_y_set" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - if (arg1) (arg1)->y = arg2; - resultobj = SWIG_Py_Void(); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Point_y_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - float result; - - if (!PyArg_ParseTuple(args,(char *)"O:Point_y_get",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Point_y_get" "', argument " "1"" of type '" "struct fz_point_s *""'"); - } - arg1 = (struct fz_point_s *)(argp1); - result = (float) ((arg1)->y); - resultobj = SWIG_From_float((float)(result)); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Point__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_point_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)":new_Point")) SWIG_fail; - result = (struct fz_point_s *)calloc(1, sizeof(struct fz_point_s)); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_point_s, SWIG_POINTER_NEW | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Point__SWIG_1(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - struct fz_point_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:new_Point",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Point" "', argument " "1"" of type '" "struct fz_point_s const *""'"); - } - arg1 = (struct fz_point_s *)(argp1); - { - result = (struct fz_point_s *)new_fz_point_s__SWIG_1((struct fz_point_s const *)arg1); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_point_s, SWIG_POINTER_NEW | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Point__SWIG_2(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - float arg1 ; - float arg2 ; - float val1 ; - int ecode1 = 0 ; - float val2 ; - int ecode2 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - struct fz_point_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:new_Point",&obj0,&obj1)) SWIG_fail; - ecode1 = SWIG_AsVal_float(obj0, &val1); - if (!SWIG_IsOK(ecode1)) { - SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "new_Point" "', argument " "1"" of type '" "float""'"); - } - arg1 = (float)(val1); - ecode2 = SWIG_AsVal_float(obj1, &val2); - if (!SWIG_IsOK(ecode2)) { - SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "new_Point" "', argument " "2"" of type '" "float""'"); - } - arg2 = (float)(val2); - { - result = (struct fz_point_s *)new_fz_point_s__SWIG_2(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_point_s, SWIG_POINTER_NEW | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Point__SWIG_3(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - PyObject *arg1 = (PyObject *) 0 ; - PyObject * obj0 = 0 ; - struct fz_point_s *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"O:new_Point",&obj0)) SWIG_fail; - arg1 = obj0; - { - result = (struct fz_point_s *)new_fz_point_s__SWIG_3(arg1); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_point_s, SWIG_POINTER_NEW | 0 ); - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_new_Point(PyObject *self, PyObject *args) { - Py_ssize_t argc; - PyObject *argv[3] = { - 0 - }; - Py_ssize_t ii; - - if (!PyTuple_Check(args)) SWIG_fail; - argc = args ? PyObject_Length(args) : 0; - for (ii = 0; (ii < 2) && (ii < argc); ii++) { - argv[ii] = PyTuple_GET_ITEM(args,ii); - } - if (argc == 0) { - return _wrap_new_Point__SWIG_0(self, args); - } - if (argc == 1) { - int _v; - void *vptr = 0; - int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_fz_point_s, 0); - _v = SWIG_CheckState(res); - if (_v) { - return _wrap_new_Point__SWIG_1(self, args); - } - } - if (argc == 1) { - int _v; - _v = (argv[0] != 0); - if (_v) { - return _wrap_new_Point__SWIG_3(self, args); - } - } - if (argc == 2) { - int _v; - { - int res = SWIG_AsVal_float(argv[0], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - { - int res = SWIG_AsVal_float(argv[1], NULL); - _v = SWIG_CheckState(res); - } - if (_v) { - return _wrap_new_Point__SWIG_2(self, args); - } - } - } - + return resultobj; fail: - SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number or type of arguments for overloaded function 'new_Point'.\n" - " Possible C/C++ prototypes are:\n" - " fz_point_s::fz_point_s()\n" - " fz_point_s::fz_point_s(struct fz_point_s const *)\n" - " fz_point_s::fz_point_s(float,float)\n" - " fz_point_s::fz_point_s(PyObject *)\n"); - return 0; + return NULL; } -SWIGINTERN PyObject *_wrap_delete_Point(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_delete_Outline(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_point_s *arg1 = (struct fz_point_s *) 0 ; + struct fz_outline_s *arg1 = (struct fz_outline_s *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:delete_Point",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_point_s, SWIG_POINTER_DISOWN | 0 ); + if (!PyArg_ParseTuple(args,(char *)"O:delete_Outline",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_outline_s, SWIG_POINTER_DISOWN | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Point" "', argument " "1"" of type '" "struct fz_point_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Outline" "', argument " "1"" of type '" "struct fz_outline_s *""'"); } - arg1 = (struct fz_point_s *)(argp1); - delete_fz_point_s(arg1); + arg1 = (struct fz_outline_s *)(argp1); + free((char *) arg1); resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -18999,10 +19101,10 @@ SWIGINTERN PyObject *_wrap_delete_Point(PyObject *SWIGUNUSEDPARM(self), PyObject } -SWIGINTERN PyObject *Point_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *Outline_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *obj; if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_point_s, SWIG_NewClientData(obj)); + SWIG_TypeNewClientData(SWIGTYPE_p_fz_outline_s, SWIG_NewClientData(obj)); return SWIG_Py_Void(); } @@ -19033,7 +19135,7 @@ SWIGINTERN PyObject *_wrap_Annot_rect(PyObject *SWIGUNUSEDPARM(self), PyObject * void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; if (!PyArg_ParseTuple(args,(char *)"O:Annot_rect",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); @@ -19041,15 +19143,15 @@ SWIGINTERN PyObject *_wrap_Annot_rect(PyObject *SWIGUNUSEDPARM(self), PyObject * SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot_rect" "', argument " "1"" of type '" "struct fz_annot_s *""'"); } arg1 = (struct fz_annot_s *)(argp1); - result = (struct fz_rect_s *)fz_annot_s_rect(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); + result = (PyObject *)fz_annot_s_rect(arg1); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Annot__getXref(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Annot_xref(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; void *argp1 = 0 ; @@ -19057,13 +19159,13 @@ SWIGINTERN PyObject *_wrap_Annot__getXref(PyObject *SWIGUNUSEDPARM(self), PyObje PyObject * obj0 = 0 ; int result; - if (!PyArg_ParseTuple(args,(char *)"O:Annot__getXref",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O:Annot_xref",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__getXref" "', argument " "1"" of type '" "struct fz_annot_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot_xref" "', argument " "1"" of type '" "struct fz_annot_s *""'"); } arg1 = (struct fz_annot_s *)(argp1); - result = (int)fz_annot_s__getXref(arg1); + result = (int)fz_annot_s_xref(arg1); resultobj = SWIG_From_int((int)(result)); return resultobj; fail: @@ -19104,69 +19206,32 @@ SWIGINTERN PyObject *_wrap_Annot__setAP(PyObject *SWIGUNUSEDPARM(self), PyObject PyObject *resultobj = 0; struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; PyObject *arg2 = (PyObject *) 0 ; + int arg3 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; + int val3 ; + int ecode3 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Annot__setAP",&obj0,&obj1)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO|O:Annot__setAP",&obj0,&obj1,&obj2)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__setAP" "', argument " "1"" of type '" "struct fz_annot_s *""'"); } arg1 = (struct fz_annot_s *)(argp1); arg2 = obj1; - { - result = (PyObject *)fz_annot_s__setAP(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - - -SWIGINTERN PyObject *_wrap_Annot__checkAP(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; - char *arg3 = (char *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject * obj2 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OOO:Annot__checkAP",&obj0,&obj1,&obj2)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__checkAP" "', argument " "1"" of type '" "struct fz_annot_s *""'"); - } - arg1 = (struct fz_annot_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Annot__checkAP" "', argument " "2"" of type '" "struct fz_rect_s *""'"); - } - arg2 = (struct fz_rect_s *)(argp2); - res3 = SWIG_AsCharPtrAndSize(obj2, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Annot__checkAP" "', argument " "3"" of type '" "char *""'"); + if (obj2) { + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Annot__setAP" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); } - arg3 = (char *)(buf3); { - result = (PyObject *)fz_annot_s__checkAP(arg1,arg2,arg3); + result = (PyObject *)fz_annot_s__setAP(arg1,arg2,arg3); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -19174,37 +19239,29 @@ SWIGINTERN PyObject *_wrap_Annot__checkAP(PyObject *SWIGUNUSEDPARM(self), PyObje } } resultobj = result; - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return resultobj; fail: - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); return NULL; } -SWIGINTERN PyObject *_wrap_Annot__setRect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Annot_setRect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; - struct fz_rect_s *arg2 = (struct fz_rect_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Annot__setRect",&obj0,&obj1)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO:Annot_setRect",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__setRect" "', argument " "1"" of type '" "struct fz_annot_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot_setRect" "', argument " "1"" of type '" "struct fz_annot_s *""'"); } arg1 = (struct fz_annot_s *)(argp1); - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Annot__setRect" "', argument " "2"" of type '" "struct fz_rect_s *""'"); - } - arg2 = (struct fz_rect_s *)(argp2); - fz_annot_s__setRect(arg1,arg2); + arg2 = obj1; + fz_annot_s_setRect(arg1,arg2); resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -19234,38 +19291,6 @@ SWIGINTERN PyObject *_wrap_Annot_vertices(PyObject *SWIGUNUSEDPARM(self), PyObje } -SWIGINTERN PyObject *_wrap_Annot__setVertices(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - PyObject *result = 0 ; - - if (!PyArg_ParseTuple(args,(char *)"OO:Annot__setVertices",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__setVertices" "', argument " "1"" of type '" "struct fz_annot_s *""'"); - } - arg1 = (struct fz_annot_s *)(argp1); - arg2 = obj1; - { - result = (PyObject *)fz_annot_s__setVertices(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } - } - resultobj = result; - return resultobj; -fail: - return NULL; -} - - SWIGINTERN PyObject *_wrap_Annot_colors(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; @@ -19288,21 +19313,58 @@ SWIGINTERN PyObject *_wrap_Annot_colors(PyObject *SWIGUNUSEDPARM(self), PyObject } -SWIGINTERN PyObject *_wrap_Annot_updateAppearance(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Annot_update(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; + float arg2 = (float) 0.0 ; + PyObject *arg3 = (PyObject *) NULL ; + PyObject *arg4 = (PyObject *) NULL ; + PyObject *arg5 = (PyObject *) NULL ; + int arg6 = (int) -1 ; void *argp1 = 0 ; int res1 = 0 ; + float val2 ; + int ecode2 = 0 ; + int val6 ; + int ecode6 = 0 ; PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject * obj4 = 0 ; + PyObject * obj5 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Annot_updateAppearance",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O|OOOOO:Annot_update",&obj0,&obj1,&obj2,&obj3,&obj4,&obj5)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot_updateAppearance" "', argument " "1"" of type '" "struct fz_annot_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot_update" "', argument " "1"" of type '" "struct fz_annot_s *""'"); } arg1 = (struct fz_annot_s *)(argp1); - result = (PyObject *)fz_annot_s_updateAppearance(arg1); + if (obj1) { + ecode2 = SWIG_AsVal_float(obj1, &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Annot_update" "', argument " "2"" of type '" "float""'"); + } + arg2 = (float)(val2); + } + if (obj2) { + arg3 = obj2; + } + if (obj3) { + arg4 = obj3; + } + if (obj4) { + arg5 = obj4; + } + if (obj5) { + ecode6 = SWIG_AsVal_int(obj5, &val6); + if (!SWIG_IsOK(ecode6)) { + SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "Annot_update" "', argument " "6"" of type '" "int""'"); + } + arg6 = (int)(val6); + } + result = (PyObject *)fz_annot_s_update(arg1,arg2,arg3,arg4,arg5,arg6); resultobj = result; return resultobj; fail: @@ -19898,13 +19960,11 @@ SWIGINTERN PyObject *_wrap_Annot_next(PyObject *SWIGUNUSEDPARM(self), PyObject * SWIGINTERN PyObject *_wrap_Annot_getPixmap(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; - struct fz_matrix_s *arg2 = (struct fz_matrix_s *) NULL ; + PyObject *arg2 = (PyObject *) NULL ; struct fz_colorspace_s *arg3 = (struct fz_colorspace_s *) NULL ; int arg4 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; void *argp3 = 0 ; int res3 = 0 ; int val4 ; @@ -19922,11 +19982,7 @@ SWIGINTERN PyObject *_wrap_Annot_getPixmap(PyObject *SWIGUNUSEDPARM(self), PyObj } arg1 = (struct fz_annot_s *)(argp1); if (obj1) { - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Annot_getPixmap" "', argument " "2"" of type '" "struct fz_matrix_s *""'"); - } - arg2 = (struct fz_matrix_s *)(argp2); + arg2 = obj1; } if (obj2) { res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); @@ -19942,46 +19998,189 @@ SWIGINTERN PyObject *_wrap_Annot_getPixmap(PyObject *SWIGUNUSEDPARM(self), PyObj } arg4 = (int)(val4); } - { - result = (struct fz_pixmap_s *)fz_annot_s_getPixmap(arg1,arg2,arg3,arg4); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } + { + result = (struct fz_pixmap_s *)fz_annot_s_getPixmap(arg1,arg2,arg3,arg4); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Annot__updateWidget(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:Annot__updateWidget",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__updateWidget" "', argument " "1"" of type '" "struct fz_annot_s *""'"); + } + arg1 = (struct fz_annot_s *)(argp1); + arg2 = obj1; + { + result = (PyObject *)fz_annot_s__updateWidget(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Annot__getWidget(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:Annot__getWidget",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__getWidget" "', argument " "1"" of type '" "struct fz_annot_s *""'"); + } + arg1 = (struct fz_annot_s *)(argp1); + arg2 = obj1; + { + result = (PyObject *)fz_annot_s__getWidget(arg1,arg2); + if(!result) + { + PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); + return NULL; + } + } + resultobj = result; + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *Annot_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *obj; + if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; + SWIG_TypeNewClientData(SWIGTYPE_p_fz_annot_s, SWIG_NewClientData(obj)); + return SWIG_Py_Void(); +} + +SWIGINTERN PyObject *_wrap_delete_Link(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_link_s *arg1 = (struct fz_link_s *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:delete_Link",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_link_s, SWIG_POINTER_DISOWN | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Link" "', argument " "1"" of type '" "struct fz_link_s *""'"); + } + arg1 = (struct fz_link_s *)(argp1); + delete_fz_link_s(arg1); + resultobj = SWIG_Py_Void(); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Link__border(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct fz_link_s *arg1 = (struct fz_link_s *) 0 ; + struct fz_document_s *arg2 = (struct fz_document_s *) 0 ; + int arg3 ; + void *argp1 = 0 ; + int res1 = 0 ; + void *argp2 = 0 ; + int res2 = 0 ; + int val3 ; + int ecode3 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OOO:Link__border",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_link_s, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Link__border" "', argument " "1"" of type '" "struct fz_link_s *""'"); + } + arg1 = (struct fz_link_s *)(argp1); + res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Link__border" "', argument " "2"" of type '" "struct fz_document_s *""'"); } - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_pixmap_s, 0 | 0 ); + arg2 = (struct fz_document_s *)(argp2); + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Link__border" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + result = (PyObject *)fz_link_s__border(arg1,arg2,arg3); + resultobj = result; return resultobj; fail: return NULL; } -SWIGINTERN PyObject *_wrap_Annot__updateWidget(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Link__setBorder(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; + struct fz_link_s *arg1 = (struct fz_link_s *) 0 ; PyObject *arg2 = (PyObject *) 0 ; + struct fz_document_s *arg3 = (struct fz_document_s *) 0 ; + int arg4 ; void *argp1 = 0 ; int res1 = 0 ; + void *argp3 = 0 ; + int res3 = 0 ; + int val4 ; + int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Annot__updateWidget",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOOO:Link__setBorder",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_link_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__updateWidget" "', argument " "1"" of type '" "struct fz_annot_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Link__setBorder" "', argument " "1"" of type '" "struct fz_link_s *""'"); } - arg1 = (struct fz_annot_s *)(argp1); + arg1 = (struct fz_link_s *)(argp1); arg2 = obj1; - { - result = (PyObject *)fz_annot_s__updateWidget(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } + res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Link__setBorder" "', argument " "3"" of type '" "struct fz_document_s *""'"); } + arg3 = (struct fz_document_s *)(argp3); + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Link__setBorder" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + result = (PyObject *)fz_link_s__setBorder(arg1,arg2,arg3,arg4); resultobj = result; return resultobj; fail: @@ -19989,31 +20188,39 @@ SWIGINTERN PyObject *_wrap_Annot__updateWidget(PyObject *SWIGUNUSEDPARM(self), P } -SWIGINTERN PyObject *_wrap_Annot__getWidget(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Link__colors(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_annot_s *arg1 = (struct fz_annot_s *) 0 ; - PyObject *arg2 = (PyObject *) 0 ; + struct fz_link_s *arg1 = (struct fz_link_s *) 0 ; + struct fz_document_s *arg2 = (struct fz_document_s *) 0 ; + int arg3 ; void *argp1 = 0 ; int res1 = 0 ; + void *argp2 = 0 ; + int res2 = 0 ; + int val3 ; + int ecode3 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO:Annot__getWidget",&obj0,&obj1)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_annot_s, 0 | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOO:Link__colors",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_link_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Annot__getWidget" "', argument " "1"" of type '" "struct fz_annot_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Link__colors" "', argument " "1"" of type '" "struct fz_link_s *""'"); } - arg1 = (struct fz_annot_s *)(argp1); - arg2 = obj1; - { - result = (PyObject *)fz_annot_s__getWidget(arg1,arg2); - if(!result) - { - PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); - return NULL; - } + arg1 = (struct fz_link_s *)(argp1); + res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Link__colors" "', argument " "2"" of type '" "struct fz_document_s *""'"); } + arg2 = (struct fz_document_s *)(argp2); + ecode3 = SWIG_AsVal_int(obj2, &val3); + if (!SWIG_IsOK(ecode3)) { + SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "Link__colors" "', argument " "3"" of type '" "int""'"); + } + arg3 = (int)(val3); + result = (PyObject *)fz_link_s__colors(arg1,arg2,arg3); resultobj = result; return resultobj; fail: @@ -20021,28 +20228,43 @@ SWIGINTERN PyObject *_wrap_Annot__getWidget(PyObject *SWIGUNUSEDPARM(self), PyOb } -SWIGINTERN PyObject *Annot_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *obj; - if (!PyArg_ParseTuple(args,(char *)"O:swigregister", &obj)) return NULL; - SWIG_TypeNewClientData(SWIGTYPE_p_fz_annot_s, SWIG_NewClientData(obj)); - return SWIG_Py_Void(); -} - -SWIGINTERN PyObject *_wrap_delete_Link(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Link__setColors(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_link_s *arg1 = (struct fz_link_s *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + struct fz_document_s *arg3 = (struct fz_document_s *) 0 ; + int arg4 ; void *argp1 = 0 ; int res1 = 0 ; + void *argp3 = 0 ; + int res3 = 0 ; + int val4 ; + int ecode4 = 0 ; PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:delete_Link",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_link_s, SWIG_POINTER_DISOWN | 0 ); + if (!PyArg_ParseTuple(args,(char *)"OOOO:Link__setColors",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_link_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_Link" "', argument " "1"" of type '" "struct fz_link_s *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Link__setColors" "', argument " "1"" of type '" "struct fz_link_s *""'"); } arg1 = (struct fz_link_s *)(argp1); - delete_fz_link_s(arg1); - resultobj = SWIG_Py_Void(); + arg2 = obj1; + res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_document_s, 0 | 0 ); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "Link__setColors" "', argument " "3"" of type '" "struct fz_document_s *""'"); + } + arg3 = (struct fz_document_s *)(argp3); + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "Link__setColors" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + result = (PyObject *)fz_link_s__setColors(arg1,arg2,arg3,arg4); + resultobj = result; return resultobj; fail: return NULL; @@ -20099,7 +20321,7 @@ SWIGINTERN PyObject *_wrap_Link_rect(PyObject *SWIGUNUSEDPARM(self), PyObject *a void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; if (!PyArg_ParseTuple(args,(char *)"O:Link_rect",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_link_s, 0 | 0 ); @@ -20107,8 +20329,8 @@ SWIGINTERN PyObject *_wrap_Link_rect(PyObject *SWIGUNUSEDPARM(self), PyObject *a SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Link_rect" "', argument " "1"" of type '" "struct fz_link_s *""'"); } arg1 = (struct fz_link_s *)(argp1); - result = (struct fz_rect_s *)fz_link_s_rect(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); + result = (PyObject *)fz_link_s_rect(arg1); + resultobj = result; return resultobj; fail: return NULL; @@ -20167,18 +20389,12 @@ SWIGINTERN PyObject *_wrap_delete_DisplayList(PyObject *SWIGUNUSEDPARM(self), Py SWIGINTERN PyObject *_wrap_new_DisplayList(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; + PyObject *arg1 = (PyObject *) 0 ; PyObject * obj0 = 0 ; struct fz_display_list_s *result = 0 ; if (!PyArg_ParseTuple(args,(char *)"O:new_DisplayList",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_DisplayList" "', argument " "1"" of type '" "struct fz_rect_s *""'"); - } - arg1 = (struct fz_rect_s *)(argp1); + arg1 = obj0; { result = (struct fz_display_list_s *)new_fz_display_list_s(arg1); if(!result) @@ -20198,16 +20414,12 @@ SWIGINTERN PyObject *_wrap_DisplayList_run(PyObject *SWIGUNUSEDPARM(self), PyObj PyObject *resultobj = 0; struct fz_display_list_s *arg1 = (struct fz_display_list_s *) 0 ; struct DeviceWrapper *arg2 = (struct DeviceWrapper *) 0 ; - struct fz_matrix_s *arg3 = (struct fz_matrix_s *) 0 ; - struct fz_rect_s *arg4 = (struct fz_rect_s *) 0 ; + PyObject *arg3 = (PyObject *) 0 ; + PyObject *arg4 = (PyObject *) 0 ; void *argp1 = 0 ; int res1 = 0 ; void *argp2 = 0 ; int res2 = 0 ; - void *argp3 = 0 ; - int res3 = 0 ; - void *argp4 = 0 ; - int res4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; @@ -20225,18 +20437,10 @@ SWIGINTERN PyObject *_wrap_DisplayList_run(PyObject *SWIGUNUSEDPARM(self), PyObj SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "DisplayList_run" "', argument " "2"" of type '" "struct DeviceWrapper *""'"); } arg2 = (struct DeviceWrapper *)(argp2); - res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "DisplayList_run" "', argument " "3"" of type '" "struct fz_matrix_s const *""'"); - } - arg3 = (struct fz_matrix_s *)(argp3); - res4 = SWIG_ConvertPtr(obj3, &argp4,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res4)) { - SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "DisplayList_run" "', argument " "4"" of type '" "struct fz_rect_s const *""'"); - } - arg4 = (struct fz_rect_s *)(argp4); + arg3 = obj2; + arg4 = obj3; { - result = (int)fz_display_list_s_run(arg1,arg2,(struct fz_matrix_s const *)arg3,(struct fz_rect_s const *)arg4); + result = (int)fz_display_list_s_run(arg1,arg2,arg3,arg4); if(result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -20256,7 +20460,7 @@ SWIGINTERN PyObject *_wrap_DisplayList_rect(PyObject *SWIGUNUSEDPARM(self), PyOb void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - struct fz_rect_s *result = 0 ; + PyObject *result = 0 ; if (!PyArg_ParseTuple(args,(char *)"O:DisplayList_rect",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_display_list_s, 0 | 0 ); @@ -20264,8 +20468,8 @@ SWIGINTERN PyObject *_wrap_DisplayList_rect(PyObject *SWIGUNUSEDPARM(self), PyOb SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "DisplayList_rect" "', argument " "1"" of type '" "struct fz_display_list_s *""'"); } arg1 = (struct fz_display_list_s *)(argp1); - result = (struct fz_rect_s *)fz_display_list_s_rect(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_fz_rect_s, 0 | 0 ); + result = (PyObject *)fz_display_list_s_rect(arg1); + resultobj = result; return resultobj; fail: return NULL; @@ -20275,20 +20479,16 @@ SWIGINTERN PyObject *_wrap_DisplayList_rect(PyObject *SWIGUNUSEDPARM(self), PyOb SWIGINTERN PyObject *_wrap_DisplayList_getPixmap(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct fz_display_list_s *arg1 = (struct fz_display_list_s *) 0 ; - struct fz_matrix_s *arg2 = (struct fz_matrix_s *) NULL ; + PyObject *arg2 = (PyObject *) NULL ; struct fz_colorspace_s *arg3 = (struct fz_colorspace_s *) NULL ; int arg4 = (int) 0 ; - struct fz_rect_s *arg5 = (struct fz_rect_s *) NULL ; + PyObject *arg5 = (PyObject *) NULL ; void *argp1 = 0 ; int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; void *argp3 = 0 ; int res3 = 0 ; int val4 ; int ecode4 = 0 ; - void *argp5 = 0 ; - int res5 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; @@ -20303,11 +20503,7 @@ SWIGINTERN PyObject *_wrap_DisplayList_getPixmap(PyObject *SWIGUNUSEDPARM(self), } arg1 = (struct fz_display_list_s *)(argp1); if (obj1) { - res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_fz_matrix_s, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "DisplayList_getPixmap" "', argument " "2"" of type '" "struct fz_matrix_s const *""'"); - } - arg2 = (struct fz_matrix_s *)(argp2); + arg2 = obj1; } if (obj2) { res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_fz_colorspace_s, 0 | 0 ); @@ -20324,14 +20520,10 @@ SWIGINTERN PyObject *_wrap_DisplayList_getPixmap(PyObject *SWIGUNUSEDPARM(self), arg4 = (int)(val4); } if (obj4) { - res5 = SWIG_ConvertPtr(obj4, &argp5,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res5)) { - SWIG_exception_fail(SWIG_ArgError(res5), "in method '" "DisplayList_getPixmap" "', argument " "5"" of type '" "struct fz_rect_s *""'"); - } - arg5 = (struct fz_rect_s *)(argp5); + arg5 = obj4; } { - result = (struct fz_pixmap_s *)fz_display_list_s_getPixmap(arg1,(struct fz_matrix_s const *)arg2,arg3,arg4,arg5); + result = (struct fz_pixmap_s *)fz_display_list_s_getPixmap(arg1,arg2,arg3,arg4,arg5); if(!result) { PyErr_SetString(PyExc_RuntimeError, fz_caught_message(gctx)); @@ -20394,18 +20586,12 @@ SWIGINTERN PyObject *DisplayList_swigregister(PyObject *SWIGUNUSEDPARM(self), Py SWIGINTERN PyObject *_wrap_new_TextPage(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - struct fz_rect_s *arg1 = (struct fz_rect_s *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; + PyObject *arg1 = (PyObject *) 0 ; PyObject * obj0 = 0 ; struct fz_stext_page_s *result = 0 ; if (!PyArg_ParseTuple(args,(char *)"O:new_TextPage",&obj0)) SWIG_fail; - res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_rect_s, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_TextPage" "', argument " "1"" of type '" "struct fz_rect_s *""'"); - } - arg1 = (struct fz_rect_s *)(argp1); + arg1 = obj0; { result = (struct fz_stext_page_s *)new_fz_stext_page_s(arg1); if(!result) @@ -20447,6 +20633,7 @@ SWIGINTERN PyObject *_wrap_TextPage_search(PyObject *SWIGUNUSEDPARM(self), PyObj struct fz_stext_page_s *arg1 = (struct fz_stext_page_s *) 0 ; char *arg2 = (char *) 0 ; int arg3 = (int) 16 ; + int arg4 = (int) 0 ; void *argp1 = 0 ; int res1 = 0 ; int res2 ; @@ -20454,12 +20641,15 @@ SWIGINTERN PyObject *_wrap_TextPage_search(PyObject *SWIGUNUSEDPARM(self), PyObj int alloc2 = 0 ; int val3 ; int ecode3 = 0 ; + int val4 ; + int ecode4 = 0 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; PyObject * obj2 = 0 ; + PyObject * obj3 = 0 ; PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"OO|O:TextPage_search",&obj0,&obj1,&obj2)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"OO|OO:TextPage_search",&obj0,&obj1,&obj2,&obj3)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_fz_stext_page_s, 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "TextPage_search" "', argument " "1"" of type '" "struct fz_stext_page_s *""'"); @@ -20477,7 +20667,14 @@ SWIGINTERN PyObject *_wrap_TextPage_search(PyObject *SWIGUNUSEDPARM(self), PyObj } arg3 = (int)(val3); } - result = (PyObject *)fz_stext_page_s_search(arg1,(char const *)arg2,arg3); + if (obj3) { + ecode4 = SWIG_AsVal_int(obj3, &val4); + if (!SWIG_IsOK(ecode4)) { + SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "TextPage_search" "', argument " "4"" of type '" "int""'"); + } + arg4 = (int)(val4); + } + result = (PyObject *)fz_stext_page_s_search(arg1,(char const *)arg2,arg3,arg4); resultobj = result; if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); return resultobj; @@ -20766,20 +20963,20 @@ SWIGINTERN PyObject *_wrap_Tools_fitz_config(PyObject *SWIGUNUSEDPARM(self), PyO } -SWIGINTERN PyObject *_wrap_Tools__store_debug(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Tools_glyph_cache_empty(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct Tools *arg1 = (struct Tools *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Tools__store_debug",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O:Tools_glyph_cache_empty",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Tools, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Tools__store_debug" "', argument " "1"" of type '" "struct Tools *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Tools_glyph_cache_empty" "', argument " "1"" of type '" "struct Tools *""'"); } arg1 = (struct Tools *)(argp1); - Tools__store_debug(arg1); + Tools_glyph_cache_empty(arg1); resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -20787,20 +20984,42 @@ SWIGINTERN PyObject *_wrap_Tools__store_debug(PyObject *SWIGUNUSEDPARM(self), Py } -SWIGINTERN PyObject *_wrap_Tools_glyph_cache_empty(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_Tools_fitz_stderr(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct Tools *arg1 = (struct Tools *) 0 ; void *argp1 = 0 ; int res1 = 0 ; PyObject * obj0 = 0 ; + PyObject *result = 0 ; - if (!PyArg_ParseTuple(args,(char *)"O:Tools_glyph_cache_empty",&obj0)) SWIG_fail; + if (!PyArg_ParseTuple(args,(char *)"O:Tools_fitz_stderr",&obj0)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Tools, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Tools_glyph_cache_empty" "', argument " "1"" of type '" "struct Tools *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Tools_fitz_stderr" "', argument " "1"" of type '" "struct Tools *""'"); } arg1 = (struct Tools *)(argp1); - Tools_glyph_cache_empty(arg1); + result = (PyObject *)Tools_fitz_stderr(arg1); + resultobj = result; + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Tools_fitz_stderr_reset(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct Tools *arg1 = (struct Tools *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:Tools_fitz_stderr_reset",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Tools, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Tools_fitz_stderr_reset" "', argument " "1"" of type '" "struct Tools *""'"); + } + arg1 = (struct Tools *)(argp1); + Tools_fitz_stderr_reset(arg1); resultobj = SWIG_Py_Void(); return resultobj; fail: @@ -20808,6 +21027,59 @@ SWIGINTERN PyObject *_wrap_Tools_glyph_cache_empty(PyObject *SWIGUNUSEDPARM(self } +SWIGINTERN PyObject *_wrap_Tools_transform_rect(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct Tools *arg1 = (struct Tools *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + PyObject *arg3 = (PyObject *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject * obj2 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OOO:Tools_transform_rect",&obj0,&obj1,&obj2)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Tools, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Tools_transform_rect" "', argument " "1"" of type '" "struct Tools *""'"); + } + arg1 = (struct Tools *)(argp1); + arg2 = obj1; + arg3 = obj2; + result = (PyObject *)Tools_transform_rect(arg1,arg2,arg3); + resultobj = result; + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_Tools_invert_matrix(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct Tools *arg1 = (struct Tools *) 0 ; + PyObject *arg2 = (PyObject *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + PyObject *result = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:Tools_invert_matrix",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Tools, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Tools_invert_matrix" "', argument " "1"" of type '" "struct Tools *""'"); + } + arg1 = (struct Tools *)(argp1); + arg2 = obj1; + result = (PyObject *)Tools_invert_matrix(arg1,arg2); + resultobj = result; + return resultobj; +fail: + return NULL; +} + + SWIGINTERN PyObject *_wrap_new_Tools(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; struct Tools *result = 0 ; @@ -20863,7 +21135,7 @@ static PyMethodDef SwigMethods[] = { { (char *)"Document_embeddedFileUpd", _wrap_Document_embeddedFileUpd, METH_VARARGS, (char *)"Change an embedded file given its entry number or name."}, { (char *)"Document_embeddedFileGet", _wrap_Document_embeddedFileGet, METH_VARARGS, (char *)"Retrieve embedded file content by name or by number."}, { (char *)"Document_embeddedFileAdd", _wrap_Document_embeddedFileAdd, METH_VARARGS, (char *)"Embed a new file."}, - { (char *)"Document_convertToPDF", _wrap_Document_convertToPDF, METH_VARARGS, (char *)"Convert document to PDF selecting copy range and optional rotation. Output bytes object."}, + { (char *)"Document_convertToPDF", _wrap_Document_convertToPDF, METH_VARARGS, (char *)"Convert document to PDF selecting page range and optional rotation. Output bytes object."}, { (char *)"Document_pageCount", _wrap_Document_pageCount, METH_VARARGS, (char *)"Document_pageCount(self) -> int"}, { (char *)"Document__getMetadata", _wrap_Document__getMetadata, METH_VARARGS, (char *)"Document__getMetadata(self, key) -> char *"}, { (char *)"Document_needsPass", _wrap_Document_needsPass, METH_VARARGS, (char *)"Document_needsPass(self) -> int"}, @@ -20875,11 +21147,13 @@ static PyMethodDef SwigMethods[] = { { (char *)"Document__deleteObject", _wrap_Document__deleteObject, METH_VARARGS, (char *)"Delete the object given by its xref"}, { (char *)"Document__getPDFroot", _wrap_Document__getPDFroot, METH_VARARGS, (char *)"PDF catalog xref number"}, { (char *)"Document_isPDF", _wrap_Document_isPDF, METH_VARARGS, (char *)"Document_isPDF(self) -> PyObject *"}, + { (char *)"Document__hasXrefStream", _wrap_Document__hasXrefStream, METH_VARARGS, (char *)"Document__hasXrefStream(self) -> PyObject *"}, + { (char *)"Document__hasXrefOldStyle", _wrap_Document__hasXrefOldStyle, METH_VARARGS, (char *)"Document__hasXrefOldStyle(self) -> PyObject *"}, { (char *)"Document_isDirty", _wrap_Document_isDirty, METH_VARARGS, (char *)"Document_isDirty(self) -> PyObject *"}, { (char *)"Document__getGCTXerrcode", _wrap_Document__getGCTXerrcode, METH_VARARGS, (char *)"Document__getGCTXerrcode(self) -> int"}, { (char *)"Document__getGCTXerrmsg", _wrap_Document__getGCTXerrmsg, METH_VARARGS, (char *)"Document__getGCTXerrmsg(self) -> char const *"}, { (char *)"Document_authenticate", _wrap_Document_authenticate, METH_VARARGS, (char *)"Decrypt document with a password."}, - { (char *)"Document_save", _wrap_Document_save, METH_VARARGS, (char *)"Document_save(self, filename, garbage=0, clean=0, deflate=0, incremental=0, ascii=0, expand=0, linear=0, pretty=0) -> PyObject *"}, + { (char *)"Document_save", _wrap_Document_save, METH_VARARGS, (char *)"Document_save(self, filename, garbage=0, clean=0, deflate=0, incremental=0, ascii=0, expand=0, linear=0, pretty=0, decrypt=1) -> PyObject *"}, { (char *)"Document_write", _wrap_Document_write, METH_VARARGS, (char *)"Write document to a bytes object."}, { (char *)"Document_insertPDF", _wrap_Document_insertPDF, METH_VARARGS, (char *)"Copy page range ['from', 'to'] of source PDF, starting as page number 'start_at'."}, { (char *)"Document_insertPage", _wrap_Document_insertPage, METH_VARARGS, (char *)"Insert a new page in front of 'pno'. Use arguments 'width', 'height' to specify a non-default page size, and optionally text insertion arguments."}, @@ -20906,23 +21180,26 @@ static PyMethodDef SwigMethods[] = { { (char *)"Document__setMetadata", _wrap_Document__setMetadata, METH_VARARGS, (char *)"Document__setMetadata(self, text) -> PyObject *"}, { (char *)"Document_swigregister", Document_swigregister, METH_VARARGS, NULL}, { (char *)"delete_Page", _wrap_delete_Page, METH_VARARGS, (char *)"delete_Page(self)"}, - { (char *)"Page_bound", _wrap_Page_bound, METH_VARARGS, (char *)"Page_bound(self) -> Rect"}, - { (char *)"Page_run", _wrap_Page_run, METH_VARARGS, (char *)"Page_run(self, dw, m) -> int"}, + { (char *)"Page_bound", _wrap_Page_bound, METH_VARARGS, (char *)"Page_bound(self) -> PyObject *"}, + { (char *)"Page_run", _wrap_Page_run, METH_VARARGS, (char *)"Page_run(self, dw, m) -> PyObject *"}, { (char *)"Page_getSVGimage", _wrap_Page_getSVGimage, METH_VARARGS, (char *)"Create an SVG image from the page as a string."}, { (char *)"Page_addLineAnnot", _wrap_Page_addLineAnnot, METH_VARARGS, (char *)"Add 'Line' annot for points p1 and p2."}, { (char *)"Page_addTextAnnot", _wrap_Page_addTextAnnot, METH_VARARGS, (char *)"Add a 'sticky note' at position 'point'."}, - { (char *)"Page_addFileAnnot", _wrap_Page_addFileAnnot, METH_VARARGS, (char *)"Add a 'FileAttachment' annotation."}, - { (char *)"Page_addStrikeoutAnnot", _wrap_Page_addStrikeoutAnnot, METH_VARARGS, (char *)"Strike out content in a rectangle."}, - { (char *)"Page_addUnderlineAnnot", _wrap_Page_addUnderlineAnnot, METH_VARARGS, (char *)"Underline content in a rectangle."}, - { (char *)"Page_addHighlightAnnot", _wrap_Page_addHighlightAnnot, METH_VARARGS, (char *)"Highlight content in a rectangle."}, + { (char *)"Page_addInkAnnot", _wrap_Page_addInkAnnot, METH_VARARGS, (char *)"Add a 'handwriting' as a list of list of point-likes. Each sublist forms an independent stroke."}, + { (char *)"Page_addStampAnnot", _wrap_Page_addStampAnnot, METH_VARARGS, (char *)"Add a 'rubber stamp' in a rectangle."}, + { (char *)"Page_addFileAnnot", _wrap_Page_addFileAnnot, METH_VARARGS, (char *)"Add a 'FileAttachment' annotation at location 'point'."}, + { (char *)"Page_addStrikeoutAnnot", _wrap_Page_addStrikeoutAnnot, METH_VARARGS, (char *)"Strike out content in a rectangle or quadrilateral."}, + { (char *)"Page_addUnderlineAnnot", _wrap_Page_addUnderlineAnnot, METH_VARARGS, (char *)"Underline content in a rectangle or quadrilateral."}, + { (char *)"Page_addSquigglyAnnot", _wrap_Page_addSquigglyAnnot, METH_VARARGS, (char *)"Wavy underline content in a rectangle or quadrilateral."}, + { (char *)"Page_addHighlightAnnot", _wrap_Page_addHighlightAnnot, METH_VARARGS, (char *)"Highlight content in a rectangle or quadrilateral."}, { (char *)"Page_addRectAnnot", _wrap_Page_addRectAnnot, METH_VARARGS, (char *)"Add a 'Rectangle' annotation."}, { (char *)"Page_addCircleAnnot", _wrap_Page_addCircleAnnot, METH_VARARGS, (char *)"Add a 'Circle' annotation."}, { (char *)"Page_addPolylineAnnot", _wrap_Page_addPolylineAnnot, METH_VARARGS, (char *)"Add a 'Polyline' annotation for a sequence of points."}, { (char *)"Page_addPolygonAnnot", _wrap_Page_addPolygonAnnot, METH_VARARGS, (char *)"Add a 'Polygon' annotation for a sequence of points."}, - { (char *)"Page_addFreetextAnnot", _wrap_Page_addFreetextAnnot, METH_VARARGS, (char *)"Add a 'FreeText' annotation at position 'point'."}, + { (char *)"Page_addFreetextAnnot", _wrap_Page_addFreetextAnnot, METH_VARARGS, (char *)"Add a 'FreeText' annotation in rectangle 'rect'."}, { (char *)"Page__addWidget", _wrap_Page__addWidget, METH_VARARGS, (char *)"Page__addWidget(self, Widget) -> Annot"}, { (char *)"Page_getDisplayList", _wrap_Page_getDisplayList, METH_VARARGS, (char *)"Page_getDisplayList(self) -> DisplayList"}, - { (char *)"Page_setCropBox", _wrap_Page_setCropBox, METH_VARARGS, (char *)"Page_setCropBox(self, rect=None) -> PyObject *"}, + { (char *)"Page_setCropBox", _wrap_Page_setCropBox, METH_VARARGS, (char *)"Page_setCropBox(self, rect) -> PyObject *"}, { (char *)"Page_loadLinks", _wrap_Page_loadLinks, METH_VARARGS, (char *)"Page_loadLinks(self) -> Link"}, { (char *)"Page_firstAnnot", _wrap_Page_firstAnnot, METH_VARARGS, (char *)"Points to first annotation on page"}, { (char *)"Page_deleteLink", _wrap_Page_deleteLink, METH_VARARGS, (char *)"Delete link if PDF"}, @@ -20940,64 +21217,6 @@ static PyMethodDef SwigMethods[] = { { (char *)"Page__getContents", _wrap_Page__getContents, METH_VARARGS, (char *)"Page__getContents(self) -> PyObject *"}, { (char *)"Page__setContents", _wrap_Page__setContents, METH_VARARGS, (char *)"Set the /Contents object in page definition"}, { (char *)"Page_swigregister", Page_swigregister, METH_VARARGS, NULL}, - { (char *)"_fz_transform_rect", _wrap__fz_transform_rect, METH_VARARGS, (char *)"_fz_transform_rect(rect, transform) -> Rect"}, - { (char *)"Rect_x0_set", _wrap_Rect_x0_set, METH_VARARGS, (char *)"Rect_x0_set(self, x0)"}, - { (char *)"Rect_x0_get", _wrap_Rect_x0_get, METH_VARARGS, (char *)"Rect_x0_get(self) -> float"}, - { (char *)"Rect_y0_set", _wrap_Rect_y0_set, METH_VARARGS, (char *)"Rect_y0_set(self, y0)"}, - { (char *)"Rect_y0_get", _wrap_Rect_y0_get, METH_VARARGS, (char *)"Rect_y0_get(self) -> float"}, - { (char *)"Rect_x1_set", _wrap_Rect_x1_set, METH_VARARGS, (char *)"Rect_x1_set(self, x1)"}, - { (char *)"Rect_x1_get", _wrap_Rect_x1_get, METH_VARARGS, (char *)"Rect_x1_get(self) -> float"}, - { (char *)"Rect_y1_set", _wrap_Rect_y1_set, METH_VARARGS, (char *)"Rect_y1_set(self, y1)"}, - { (char *)"Rect_y1_get", _wrap_Rect_y1_get, METH_VARARGS, (char *)"Rect_y1_get(self) -> float"}, - { (char *)"delete_Rect", _wrap_delete_Rect, METH_VARARGS, (char *)"delete_Rect(self)"}, - { (char *)"new_Rect", _wrap_new_Rect, METH_VARARGS, (char *)"\n" - "Rect()\n" - "Rect(s)\n" - "Rect(lt, rb)\n" - "Rect(x0, y0, rb)\n" - "Rect(lt, x1, y1)\n" - "Rect(x0, y0, x1, y1)\n" - "new_Rect(list) -> Rect\n" - ""}, - { (char *)"Rect_round", _wrap_Rect_round, METH_VARARGS, (char *)"Create enclosing 'IRect'"}, - { (char *)"Rect_includePoint", _wrap_Rect_includePoint, METH_VARARGS, (char *)"Enlarge to include a 'Point' p"}, - { (char *)"Rect_intersect", _wrap_Rect_intersect, METH_VARARGS, (char *)"Shrink to intersection with another 'Rect' r"}, - { (char *)"Rect_includeRect", _wrap_Rect_includeRect, METH_VARARGS, (char *)"Enlarge to include another 'Rect' r"}, - { (char *)"Rect_normalize", _wrap_Rect_normalize, METH_VARARGS, (char *)"Make rectangle finite"}, - { (char *)"Rect_contains", _wrap_Rect_contains, METH_VARARGS, (char *)"\n" - "contains\n" - "contains\n" - "contains\n" - ""}, - { (char *)"Rect_isEmpty", _wrap_Rect_isEmpty, METH_VARARGS, (char *)"Rect_isEmpty(self) -> PyObject *"}, - { (char *)"Rect_isInfinite", _wrap_Rect_isInfinite, METH_VARARGS, (char *)"Rect_isInfinite(self) -> PyObject *"}, - { (char *)"Rect_swigregister", Rect_swigregister, METH_VARARGS, NULL}, - { (char *)"IRect_x0_set", _wrap_IRect_x0_set, METH_VARARGS, (char *)"IRect_x0_set(self, x0)"}, - { (char *)"IRect_x0_get", _wrap_IRect_x0_get, METH_VARARGS, (char *)"IRect_x0_get(self) -> int"}, - { (char *)"IRect_y0_set", _wrap_IRect_y0_set, METH_VARARGS, (char *)"IRect_y0_set(self, y0)"}, - { (char *)"IRect_y0_get", _wrap_IRect_y0_get, METH_VARARGS, (char *)"IRect_y0_get(self) -> int"}, - { (char *)"IRect_x1_set", _wrap_IRect_x1_set, METH_VARARGS, (char *)"IRect_x1_set(self, x1)"}, - { (char *)"IRect_x1_get", _wrap_IRect_x1_get, METH_VARARGS, (char *)"IRect_x1_get(self) -> int"}, - { (char *)"IRect_y1_set", _wrap_IRect_y1_set, METH_VARARGS, (char *)"IRect_y1_set(self, y1)"}, - { (char *)"IRect_y1_get", _wrap_IRect_y1_get, METH_VARARGS, (char *)"IRect_y1_get(self) -> int"}, - { (char *)"delete_IRect", _wrap_delete_IRect, METH_VARARGS, (char *)"delete_IRect(self)"}, - { (char *)"new_IRect", _wrap_new_IRect, METH_VARARGS, (char *)"\n" - "IRect()\n" - "IRect(s)\n" - "IRect(x0, y0, x1, y1)\n" - "new_IRect(list) -> IRect\n" - ""}, - { (char *)"IRect_isEmpty", _wrap_IRect_isEmpty, METH_VARARGS, (char *)"IRect_isEmpty(self) -> PyObject *"}, - { (char *)"IRect_isInfinite", _wrap_IRect_isInfinite, METH_VARARGS, (char *)"IRect_isInfinite(self) -> PyObject *"}, - { (char *)"IRect_normalize", _wrap_IRect_normalize, METH_VARARGS, (char *)"Make rectangle finite"}, - { (char *)"IRect_contains", _wrap_IRect_contains, METH_VARARGS, (char *)"\n" - "contains\n" - "contains\n" - "contains\n" - ""}, - { (char *)"IRect_translate", _wrap_IRect_translate, METH_VARARGS, (char *)"IRect_translate(self, xoff, yoff) -> IRect"}, - { (char *)"IRect_intersect", _wrap_IRect_intersect, METH_VARARGS, (char *)"IRect_intersect(self, ir) -> IRect"}, - { (char *)"IRect_swigregister", IRect_swigregister, METH_VARARGS, NULL}, { (char *)"Pixmap_x_set", _wrap_Pixmap_x_set, METH_VARARGS, (char *)"Pixmap_x_set(self, x)"}, { (char *)"Pixmap_x_get", _wrap_Pixmap_x_get, METH_VARARGS, (char *)"Pixmap_x_get(self) -> int"}, { (char *)"Pixmap_y_set", _wrap_Pixmap_y_set, METH_VARARGS, (char *)"Pixmap_y_set(self, y)"}, @@ -21035,7 +21254,7 @@ static PyMethodDef SwigMethods[] = { { (char *)"Pixmap_stride", _wrap_Pixmap_stride, METH_VARARGS, (char *)"Pixmap_stride(self) -> int"}, { (char *)"Pixmap_alpha", _wrap_Pixmap_alpha, METH_VARARGS, (char *)"Pixmap_alpha(self) -> int"}, { (char *)"Pixmap_colorspace", _wrap_Pixmap_colorspace, METH_VARARGS, (char *)"Pixmap_colorspace(self) -> Colorspace"}, - { (char *)"Pixmap_irect", _wrap_Pixmap_irect, METH_VARARGS, (char *)"Pixmap_irect(self) -> IRect"}, + { (char *)"Pixmap_irect", _wrap_Pixmap_irect, METH_VARARGS, (char *)"Pixmap_irect(self) -> PyObject *"}, { (char *)"Pixmap_size", _wrap_Pixmap_size, METH_VARARGS, (char *)"Pixmap_size(self) -> int"}, { (char *)"Pixmap_setAlpha", _wrap_Pixmap_setAlpha, METH_VARARGS, (char *)"Pixmap_setAlpha(self, alphavalues=None) -> PyObject *"}, { (char *)"Pixmap_getPNGData", _wrap_Pixmap_getPNGData, METH_VARARGS, (char *)"Pixmap_getPNGData(self, savealpha=-1) -> PyObject *"}, @@ -21055,34 +21274,6 @@ static PyMethodDef SwigMethods[] = { ""}, { (char *)"delete_Device", _wrap_delete_Device, METH_VARARGS, (char *)"delete_Device(self)"}, { (char *)"Device_swigregister", Device_swigregister, METH_VARARGS, NULL}, - { (char *)"_fz_pre_scale", _wrap__fz_pre_scale, METH_VARARGS, (char *)"_fz_pre_scale(m, sx, sy) -> Matrix"}, - { (char *)"_fz_pre_shear", _wrap__fz_pre_shear, METH_VARARGS, (char *)"_fz_pre_shear(m, sx, sy) -> Matrix"}, - { (char *)"_fz_pre_rotate", _wrap__fz_pre_rotate, METH_VARARGS, (char *)"_fz_pre_rotate(m, degree) -> Matrix"}, - { (char *)"Matrix_a_set", _wrap_Matrix_a_set, METH_VARARGS, (char *)"Matrix_a_set(self, a)"}, - { (char *)"Matrix_a_get", _wrap_Matrix_a_get, METH_VARARGS, (char *)"Matrix_a_get(self) -> float"}, - { (char *)"Matrix_b_set", _wrap_Matrix_b_set, METH_VARARGS, (char *)"Matrix_b_set(self, b)"}, - { (char *)"Matrix_b_get", _wrap_Matrix_b_get, METH_VARARGS, (char *)"Matrix_b_get(self) -> float"}, - { (char *)"Matrix_c_set", _wrap_Matrix_c_set, METH_VARARGS, (char *)"Matrix_c_set(self, c)"}, - { (char *)"Matrix_c_get", _wrap_Matrix_c_get, METH_VARARGS, (char *)"Matrix_c_get(self) -> float"}, - { (char *)"Matrix_d_set", _wrap_Matrix_d_set, METH_VARARGS, (char *)"Matrix_d_set(self, d)"}, - { (char *)"Matrix_d_get", _wrap_Matrix_d_get, METH_VARARGS, (char *)"Matrix_d_get(self) -> float"}, - { (char *)"Matrix_e_set", _wrap_Matrix_e_set, METH_VARARGS, (char *)"Matrix_e_set(self, e)"}, - { (char *)"Matrix_e_get", _wrap_Matrix_e_get, METH_VARARGS, (char *)"Matrix_e_get(self) -> float"}, - { (char *)"Matrix_f_set", _wrap_Matrix_f_set, METH_VARARGS, (char *)"Matrix_f_set(self, f)"}, - { (char *)"Matrix_f_get", _wrap_Matrix_f_get, METH_VARARGS, (char *)"Matrix_f_get(self) -> float"}, - { (char *)"delete_Matrix", _wrap_delete_Matrix, METH_VARARGS, (char *)"delete_Matrix(self)"}, - { (char *)"new_Matrix", _wrap_new_Matrix, METH_VARARGS, (char *)"\n" - "Matrix()\n" - "Matrix(n)\n" - "Matrix(sx, sy, shear=0)\n" - "Matrix(r, s, t, u, v, w)\n" - "Matrix(degree)\n" - "new_Matrix(list) -> Matrix\n" - ""}, - { (char *)"Matrix_invert", _wrap_Matrix_invert, METH_VARARGS, (char *)"Matrix_invert(self, m) -> int"}, - { (char *)"Matrix_preTranslate", _wrap_Matrix_preTranslate, METH_VARARGS, (char *)"Matrix_preTranslate(self, sx, sy) -> Matrix"}, - { (char *)"Matrix_concat", _wrap_Matrix_concat, METH_VARARGS, (char *)"Matrix_concat(self, m1, m2) -> Matrix"}, - { (char *)"Matrix_swigregister", Matrix_swigregister, METH_VARARGS, NULL}, { (char *)"Outline_title_get", _wrap_Outline_title_get, METH_VARARGS, (char *)"Outline_title_get(self) -> char *"}, { (char *)"Outline_page_get", _wrap_Outline_page_get, METH_VARARGS, (char *)"Outline_page_get(self) -> int"}, { (char *)"Outline_next_get", _wrap_Outline_next_get, METH_VARARGS, (char *)"Outline_next_get(self) -> Outline"}, @@ -21092,30 +21283,15 @@ static PyMethodDef SwigMethods[] = { { (char *)"Outline_isExternal", _wrap_Outline_isExternal, METH_VARARGS, (char *)"Outline_isExternal(self) -> int"}, { (char *)"delete_Outline", _wrap_delete_Outline, METH_VARARGS, (char *)"delete_Outline(self)"}, { (char *)"Outline_swigregister", Outline_swigregister, METH_VARARGS, NULL}, - { (char *)"_fz_transform_point", _wrap__fz_transform_point, METH_VARARGS, (char *)"_fz_transform_point(point, transform) -> Point"}, - { (char *)"Point_x_set", _wrap_Point_x_set, METH_VARARGS, (char *)"Point_x_set(self, x)"}, - { (char *)"Point_x_get", _wrap_Point_x_get, METH_VARARGS, (char *)"Point_x_get(self) -> float"}, - { (char *)"Point_y_set", _wrap_Point_y_set, METH_VARARGS, (char *)"Point_y_set(self, y)"}, - { (char *)"Point_y_get", _wrap_Point_y_get, METH_VARARGS, (char *)"Point_y_get(self) -> float"}, - { (char *)"new_Point", _wrap_new_Point, METH_VARARGS, (char *)"\n" - "Point()\n" - "Point(q)\n" - "Point(x, y)\n" - "new_Point(list) -> Point\n" - ""}, - { (char *)"delete_Point", _wrap_delete_Point, METH_VARARGS, (char *)"delete_Point(self)"}, - { (char *)"Point_swigregister", Point_swigregister, METH_VARARGS, NULL}, { (char *)"delete_Annot", _wrap_delete_Annot, METH_VARARGS, (char *)"delete_Annot(self)"}, { (char *)"Annot_rect", _wrap_Annot_rect, METH_VARARGS, (char *)"Rectangle containing the annot"}, - { (char *)"Annot__getXref", _wrap_Annot__getXref, METH_VARARGS, (char *)"Xref number of annotation"}, + { (char *)"Annot_xref", _wrap_Annot_xref, METH_VARARGS, (char *)"Annot_xref(self) -> int"}, { (char *)"Annot__getAP", _wrap_Annot__getAP, METH_VARARGS, (char *)"Get contents source of a PDF annot"}, { (char *)"Annot__setAP", _wrap_Annot__setAP, METH_VARARGS, (char *)"Update contents source of a PDF annot"}, - { (char *)"Annot__checkAP", _wrap_Annot__checkAP, METH_VARARGS, (char *)"Check and update /AP object of annot"}, - { (char *)"Annot__setRect", _wrap_Annot__setRect, METH_VARARGS, (char *)"Annot__setRect(self, rect)"}, + { (char *)"Annot_setRect", _wrap_Annot_setRect, METH_VARARGS, (char *)"Annot_setRect(self, rect)"}, { (char *)"Annot_vertices", _wrap_Annot_vertices, METH_VARARGS, (char *)"Point coordinates for various annot types"}, - { (char *)"Annot__setVertices", _wrap_Annot__setVertices, METH_VARARGS, (char *)"Change the annot's vertices. Only for 'Line', 'PolyLine' and 'Polygon' types."}, { (char *)"Annot_colors", _wrap_Annot_colors, METH_VARARGS, (char *)"dictionary of the annot's colors"}, - { (char *)"Annot_updateAppearance", _wrap_Annot_updateAppearance, METH_VARARGS, (char *)"Update the appearance of an annotation."}, + { (char *)"Annot_update", _wrap_Annot_update, METH_VARARGS, (char *)"Update the appearance of an annotation."}, { (char *)"Annot_setColors", _wrap_Annot_setColors, METH_VARARGS, (char *)"\n" "setColors(dict)\n" "Changes the 'stroke' and 'fill' colors of an annotation. If provided, values must be lists of up to 4 floats.\n" @@ -21145,21 +21321,25 @@ static PyMethodDef SwigMethods[] = { { (char *)"Annot__getWidget", _wrap_Annot__getWidget, METH_VARARGS, (char *)"Annot__getWidget(self, Widget) -> PyObject *"}, { (char *)"Annot_swigregister", Annot_swigregister, METH_VARARGS, NULL}, { (char *)"delete_Link", _wrap_delete_Link, METH_VARARGS, (char *)"delete_Link(self)"}, + { (char *)"Link__border", _wrap_Link__border, METH_VARARGS, (char *)"Link__border(self, doc, xref) -> PyObject *"}, + { (char *)"Link__setBorder", _wrap_Link__setBorder, METH_VARARGS, (char *)"Link__setBorder(self, border, doc, xref) -> PyObject *"}, + { (char *)"Link__colors", _wrap_Link__colors, METH_VARARGS, (char *)"Link__colors(self, doc, xref) -> PyObject *"}, + { (char *)"Link__setColors", _wrap_Link__setColors, METH_VARARGS, (char *)"Link__setColors(self, colors, doc, xref) -> PyObject *"}, { (char *)"Link_uri", _wrap_Link_uri, METH_VARARGS, (char *)"Link_uri(self) -> char *"}, { (char *)"Link_isExternal", _wrap_Link_isExternal, METH_VARARGS, (char *)"Link_isExternal(self) -> int"}, - { (char *)"Link_rect", _wrap_Link_rect, METH_VARARGS, (char *)"Link_rect(self) -> Rect"}, + { (char *)"Link_rect", _wrap_Link_rect, METH_VARARGS, (char *)"Link_rect(self) -> PyObject *"}, { (char *)"Link_next", _wrap_Link_next, METH_VARARGS, (char *)"Link_next(self) -> Link"}, { (char *)"Link_swigregister", Link_swigregister, METH_VARARGS, NULL}, { (char *)"delete_DisplayList", _wrap_delete_DisplayList, METH_VARARGS, (char *)"delete_DisplayList(self)"}, { (char *)"new_DisplayList", _wrap_new_DisplayList, METH_VARARGS, (char *)"new_DisplayList(mediabox) -> DisplayList"}, { (char *)"DisplayList_run", _wrap_DisplayList_run, METH_VARARGS, (char *)"DisplayList_run(self, dw, m, area) -> int"}, - { (char *)"DisplayList_rect", _wrap_DisplayList_rect, METH_VARARGS, (char *)"DisplayList_rect(self) -> Rect"}, + { (char *)"DisplayList_rect", _wrap_DisplayList_rect, METH_VARARGS, (char *)"DisplayList_rect(self) -> PyObject *"}, { (char *)"DisplayList_getPixmap", _wrap_DisplayList_getPixmap, METH_VARARGS, (char *)"DisplayList_getPixmap(self, matrix=None, colorspace=None, alpha=0, clip=None) -> Pixmap"}, { (char *)"DisplayList_getTextPage", _wrap_DisplayList_getTextPage, METH_VARARGS, (char *)"DisplayList_getTextPage(self, flags=3) -> TextPage"}, { (char *)"DisplayList_swigregister", DisplayList_swigregister, METH_VARARGS, NULL}, { (char *)"new_TextPage", _wrap_new_TextPage, METH_VARARGS, (char *)"new_TextPage(mediabox) -> TextPage"}, { (char *)"delete_TextPage", _wrap_delete_TextPage, METH_VARARGS, (char *)"delete_TextPage(self)"}, - { (char *)"TextPage_search", _wrap_TextPage_search, METH_VARARGS, (char *)"TextPage_search(self, needle, hit_max=16) -> PyObject *"}, + { (char *)"TextPage_search", _wrap_TextPage_search, METH_VARARGS, (char *)"TextPage_search(self, needle, hit_max=16, quads=0) -> PyObject *"}, { (char *)"TextPage__extractTextBlocks_AsList", _wrap_TextPage__extractTextBlocks_AsList, METH_VARARGS, (char *)"TextPage__extractTextBlocks_AsList(self) -> PyObject *"}, { (char *)"TextPage__extractTextWords_AsList", _wrap_TextPage__extractTextWords_AsList, METH_VARARGS, (char *)"TextPage__extractTextWords_AsList(self) -> PyObject *"}, { (char *)"TextPage__extractText", _wrap_TextPage__extractText, METH_VARARGS, (char *)"TextPage__extractText(self, format) -> PyObject *"}, @@ -21167,13 +21347,16 @@ static PyMethodDef SwigMethods[] = { { (char *)"delete_Graftmap", _wrap_delete_Graftmap, METH_VARARGS, (char *)"delete_Graftmap(self)"}, { (char *)"new_Graftmap", _wrap_new_Graftmap, METH_VARARGS, (char *)"new_Graftmap(doc) -> Graftmap"}, { (char *)"Graftmap_swigregister", Graftmap_swigregister, METH_VARARGS, NULL}, - { (char *)"Tools_gen_id", _wrap_Tools_gen_id, METH_VARARGS, (char *)"Return a globally unique integer."}, + { (char *)"Tools_gen_id", _wrap_Tools_gen_id, METH_VARARGS, (char *)"Return a unique integer."}, { (char *)"Tools_store_shrink", _wrap_Tools_store_shrink, METH_VARARGS, (char *)"Free 'percent' of current store size."}, { (char *)"Tools_store_size", _wrap_Tools_store_size, METH_VARARGS, (char *)"Current store size."}, { (char *)"Tools_store_maxsize", _wrap_Tools_store_maxsize, METH_VARARGS, (char *)"Maximum store size."}, { (char *)"Tools_fitz_config", _wrap_Tools_fitz_config, METH_VARARGS, (char *)"Show configuration data."}, - { (char *)"Tools__store_debug", _wrap_Tools__store_debug, METH_VARARGS, (char *)"Tools__store_debug(self)"}, { (char *)"Tools_glyph_cache_empty", _wrap_Tools_glyph_cache_empty, METH_VARARGS, (char *)"Empty the glyph cache."}, + { (char *)"Tools_fitz_stderr", _wrap_Tools_fitz_stderr, METH_VARARGS, (char *)"Tools_fitz_stderr(self) -> PyObject *"}, + { (char *)"Tools_fitz_stderr_reset", _wrap_Tools_fitz_stderr_reset, METH_VARARGS, (char *)"Tools_fitz_stderr_reset(self)"}, + { (char *)"Tools_transform_rect", _wrap_Tools_transform_rect, METH_VARARGS, (char *)"Tools_transform_rect(self, rect, matrix) -> PyObject *"}, + { (char *)"Tools_invert_matrix", _wrap_Tools_invert_matrix, METH_VARARGS, (char *)"Tools_invert_matrix(self, matrix) -> PyObject *"}, { (char *)"new_Tools", _wrap_new_Tools, METH_VARARGS, (char *)"new_Tools() -> Tools"}, { (char *)"delete_Tools", _wrap_delete_Tools, METH_VARARGS, (char *)"delete_Tools(self)"}, { (char *)"Tools_swigregister", Tools_swigregister, METH_VARARGS, NULL}, @@ -21190,14 +21373,10 @@ static swig_type_info _swigt__p_fz_annot_s = {"_p_fz_annot_s", "struct fz_annot_ static swig_type_info _swigt__p_fz_colorspace_s = {"_p_fz_colorspace_s", "struct fz_colorspace_s *|fz_colorspace_s *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_fz_display_list_s = {"_p_fz_display_list_s", "struct fz_display_list_s *|fz_display_list_s *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_fz_document_s = {"_p_fz_document_s", "struct fz_document_s *|fz_document_s *", 0, 0, (void*)0, 0}; -static swig_type_info _swigt__p_fz_irect_s = {"_p_fz_irect_s", "struct fz_irect_s *|fz_irect_s *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_fz_link_s = {"_p_fz_link_s", "struct fz_link_s *|fz_link_s *", 0, 0, (void*)0, 0}; -static swig_type_info _swigt__p_fz_matrix_s = {"_p_fz_matrix_s", "struct fz_matrix_s *|fz_matrix_s *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_fz_outline_s = {"_p_fz_outline_s", "struct fz_outline_s *|fz_outline_s *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_fz_page_s = {"_p_fz_page_s", "struct fz_page_s *|fz_page_s *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_fz_pixmap_s = {"_p_fz_pixmap_s", "struct fz_pixmap_s *|fz_pixmap_s *", 0, 0, (void*)0, 0}; -static swig_type_info _swigt__p_fz_point_s = {"_p_fz_point_s", "struct fz_point_s *|fz_point_s *", 0, 0, (void*)0, 0}; -static swig_type_info _swigt__p_fz_rect_s = {"_p_fz_rect_s", "struct fz_rect_s *|fz_rect_s *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_fz_stext_page_s = {"_p_fz_stext_page_s", "struct fz_stext_page_s *|fz_stext_page_s *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_pdf_graft_map_s = {"_p_pdf_graft_map_s", "struct pdf_graft_map_s *|pdf_graft_map_s *", 0, 0, (void*)0, 0}; @@ -21209,14 +21388,10 @@ static swig_type_info *swig_type_initial[] = { &_swigt__p_fz_colorspace_s, &_swigt__p_fz_display_list_s, &_swigt__p_fz_document_s, - &_swigt__p_fz_irect_s, &_swigt__p_fz_link_s, - &_swigt__p_fz_matrix_s, &_swigt__p_fz_outline_s, &_swigt__p_fz_page_s, &_swigt__p_fz_pixmap_s, - &_swigt__p_fz_point_s, - &_swigt__p_fz_rect_s, &_swigt__p_fz_stext_page_s, &_swigt__p_pdf_graft_map_s, }; @@ -21228,14 +21403,10 @@ static swig_cast_info _swigc__p_fz_annot_s[] = { {&_swigt__p_fz_annot_s, 0, 0, static swig_cast_info _swigc__p_fz_colorspace_s[] = { {&_swigt__p_fz_colorspace_s, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_fz_display_list_s[] = { {&_swigt__p_fz_display_list_s, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_fz_document_s[] = { {&_swigt__p_fz_document_s, 0, 0, 0},{0, 0, 0, 0}}; -static swig_cast_info _swigc__p_fz_irect_s[] = { {&_swigt__p_fz_irect_s, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_fz_link_s[] = { {&_swigt__p_fz_link_s, 0, 0, 0},{0, 0, 0, 0}}; -static swig_cast_info _swigc__p_fz_matrix_s[] = { {&_swigt__p_fz_matrix_s, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_fz_outline_s[] = { {&_swigt__p_fz_outline_s, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_fz_page_s[] = { {&_swigt__p_fz_page_s, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_fz_pixmap_s[] = { {&_swigt__p_fz_pixmap_s, 0, 0, 0},{0, 0, 0, 0}}; -static swig_cast_info _swigc__p_fz_point_s[] = { {&_swigt__p_fz_point_s, 0, 0, 0},{0, 0, 0, 0}}; -static swig_cast_info _swigc__p_fz_rect_s[] = { {&_swigt__p_fz_rect_s, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_fz_stext_page_s[] = { {&_swigt__p_fz_stext_page_s, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_pdf_graft_map_s[] = { {&_swigt__p_pdf_graft_map_s, 0, 0, 0},{0, 0, 0, 0}}; @@ -21247,14 +21418,10 @@ static swig_cast_info *swig_cast_initial[] = { _swigc__p_fz_colorspace_s, _swigc__p_fz_display_list_s, _swigc__p_fz_document_s, - _swigc__p_fz_irect_s, _swigc__p_fz_link_s, - _swigc__p_fz_matrix_s, _swigc__p_fz_outline_s, _swigc__p_fz_page_s, _swigc__p_fz_pixmap_s, - _swigc__p_fz_point_s, - _swigc__p_fz_rect_s, _swigc__p_fz_stext_page_s, _swigc__p_pdf_graft_map_s, }; @@ -21964,6 +22131,32 @@ SWIG_init(void) { } fz_register_document_handlers(gctx); + //----------------------------------------------------------------------------- + // START redirect stdout/stderr + //----------------------------------------------------------------------------- + + JM_fitz_stdout = fz_new_output(gctx, 0, JM_fitz_stdout, JM_write_stdout, NULL, NULL); + + fz_set_stdout(gctx, JM_fitz_stdout); + + JM_fitz_stderr = fz_new_output(gctx, 0, JM_fitz_stderr, JM_write_stderr, NULL, NULL); + + fz_set_stderr(gctx, JM_fitz_stderr); + + if (JM_fitz_stderr && JM_fitz_stdout) + { + ; + } + else + PySys_WriteStdout("error redirecting stdout/stderr!\n"); + + JM_error_log = PyList_New(0); + JM_output_log = PyList_New(0); + + //----------------------------------------------------------------------------- + // STOP redirect stdout/stderr + //----------------------------------------------------------------------------- + SWIG_Python_SetConstant(d, "CS_RGB",SWIG_From_int((int)(1))); SWIG_Python_SetConstant(d, "CS_GRAY",SWIG_From_int((int)(2))); SWIG_Python_SetConstant(d, "CS_CMYK",SWIG_From_int((int)(3))); diff --git a/fitz/helper-annot.i b/fitz/helper-annot.i index fd669d148..372fb2e4a 100644 --- a/fitz/helper-annot.i +++ b/fitz/helper-annot.i @@ -109,7 +109,7 @@ #define WIDGET_Ff_CommitOnSelCHange 67108864 //---------------------------------------------------------------------------- -// return string for line end style +// return code for line end style string //---------------------------------------------------------------------------- int JM_le_value(fz_context *ctx, char *le) { @@ -122,32 +122,71 @@ int JM_le_value(fz_context *ctx, char *le) //---------------------------------------------------------------------------- pdf_obj *JM_get_border_style(fz_context *ctx, PyObject *style) { - pdf_obj *val = PDF_NAME_S; + pdf_obj *val = PDF_NAME(S); if (!style) return val; char *s = JM_Python_str_AsChar(style); JM_PyErr_Clear; if (!s) return val; - if (!strncmp(s, "b", 1) || !strncmp(s, "B", 1)) val = PDF_NAME_B; - else if (!strncmp(s, "d", 1) || !strncmp(s, "D", 1)) val = PDF_NAME_D; - else if (!strncmp(s, "i", 1) || !strncmp(s, "I", 1)) val = PDF_NAME_I; - else if (!strncmp(s, "u", 1) || !strncmp(s, "U", 1)) val = PDF_NAME_U; + if (!strncmp(s, "b", 1) || !strncmp(s, "B", 1)) val = PDF_NAME(B); + else if (!strncmp(s, "d", 1) || !strncmp(s, "D", 1)) val = PDF_NAME(D); + else if (!strncmp(s, "i", 1) || !strncmp(s, "I", 1)) val = PDF_NAME(I); + else if (!strncmp(s, "u", 1) || !strncmp(s, "U", 1)) val = PDF_NAME(U); JM_Python_str_DelForPy3(s); return val; } +//---------------------------------------------------------------------------- +// Make /DA string of annotation +//---------------------------------------------------------------------------- +const char *JM_expand_fname(const char **name) +{ + if (!*name) return "Helv"; + if (!strncmp(*name, "Co", 2)) return "Cour"; + if (!strncmp(*name, "co", 2)) return "Cour"; + if (!strncmp(*name, "Ti", 2)) return "TiRo"; + if (!strncmp(*name, "ti", 2)) return "TiRo"; + if (!strncmp(*name, "Sy", 2)) return "Symb"; + if (!strncmp(*name, "sy", 2)) return "Symb"; + if (!strncmp(*name, "Za", 2)) return "ZaDb"; + if (!strncmp(*name, "za", 2)) return "ZaDb"; + return "Helv"; +} + +void JM_make_annot_DA(fz_context *ctx, pdf_annot *annot, int ncol, float col[4], const char *fontname, float fontsize) +{ + fz_buffer *buf = NULL; + fz_try(ctx) + { + buf = fz_new_buffer(ctx, 50); + if (ncol == 1) + fz_append_printf(ctx, buf, "%g g ", col[0]); + else if (ncol == 3) + fz_append_printf(ctx, buf, "%g %g %g rg ", col[0], col[1], col[2]); + else + fz_append_printf(ctx, buf, "%g %g %g %g k ", col[0], col[1], col[2], col[3]); + fz_append_printf(ctx, buf, "/%s %g Tf", JM_expand_fname(&fontname), fontsize); + char *da = NULL; + size_t len = fz_buffer_storage(ctx, buf, &da); + pdf_dict_put_string(ctx, annot->obj, PDF_NAME(DA), (const char *)da, len); + } + fz_always(ctx) fz_drop_buffer(ctx, buf); + fz_catch(ctx) fz_rethrow(ctx); + return; +} + //---------------------------------------------------------------------------- // refreshes the link and annotation tables of a page //---------------------------------------------------------------------------- void refresh_link_table(fz_context *ctx, pdf_page *page) { - pdf_obj *annots_arr = pdf_dict_get(ctx, page->obj, PDF_NAME_Annots); + pdf_obj *annots_arr = pdf_dict_get(ctx, page->obj, PDF_NAME(Annots)); if (annots_arr) { fz_rect page_mediabox; fz_matrix page_ctm; pdf_page_transform(ctx, page, &page_mediabox, &page_ctm); page->links = pdf_load_link_annots(ctx, page->doc, annots_arr, - pdf_to_num(ctx, page->obj), &page_ctm); + pdf_to_num(ctx, page->obj), page_ctm); pdf_load_annots(ctx, page, annots_arr); } return; @@ -156,44 +195,28 @@ void refresh_link_table(fz_context *ctx, pdf_page *page) //----------------------------------------------------------------------------- // create a strike-out / underline / highlight annotation //----------------------------------------------------------------------------- -struct fz_annot_s *JM_AnnotTextmarker(fz_context *ctx, pdf_page *page, fz_rect *rect, int type) +struct fz_annot_s *JM_AnnotTextmarker(fz_context *ctx, pdf_page *page, fz_quad q, int type) { pdf_annot *annot = NULL; - float line_thickness = 0.0f; - float line_height = 0.0f; - float alpha = 1.0f; - float h = rect->y1 - rect->y0; - fz_rect bbox = {rect->x0, rect->y0, rect->x1, rect->y1}; + float width = 0; float color[3] = {0,0,0}; switch (type) { case PDF_ANNOT_HIGHLIGHT: - color[0] = 1.0f; - color[1] = 1.0f; - color[2] = 0.0f; - alpha = 0.3f; - line_thickness = 1.0f; - line_height = 0.5f; + color[0] = color[1] = 1; color[2] = 0; + width = 1.0f; break; case PDF_ANNOT_UNDERLINE: - color[0] = 0.0f; - color[1] = 0.0f; - color[2] = 1.0f; - alpha = 1.0f; - line_thickness = 0.07f; - line_height = 0.075f; - bbox.y0 += 0.8f * h; - bbox.y1 += 0.8f * h; + color[0] = color[1] = 0; color[2] = 1; + width = 0.07f; + break; + case PDF_ANNOT_SQUIGGLY: + color[0] = color[1] = 0; color[2] = 1; + width = 0.07f; break; case PDF_ANNOT_STRIKE_OUT: - color[0] = 1.0f; - color[1] = 0.0f; - color[2] = 0.0f; - alpha = 1.0f; - line_thickness = 0.07f; - line_height = 0.375f; - bbox.y0 += 0.17f * h; - bbox.y1 += 0.17f * h; + color[0] = 1; color[1] = color[2] = 0; + width = 0.07f; break; } fz_try(ctx) @@ -201,20 +224,19 @@ struct fz_annot_s *JM_AnnotTextmarker(fz_context *ctx, pdf_page *page, fz_rect * pdf_document *pdf = page->doc; annot = pdf_create_annot(ctx, page, type); pdf_set_annot_color(ctx, annot, 3, color); - pdf_set_annot_border(ctx, annot, line_thickness); - pdf_add_annot_quad_point(ctx, annot, bbox); - pdf_set_annot_rect(ctx, annot, &bbox); - pdf_set_markup_appearance(ctx, pdf, annot, color, alpha, line_thickness, line_height); + pdf_set_annot_border(ctx, annot, width); + pdf_add_annot_quad_point(ctx, annot, q); + pdf_set_annot_rect(ctx, annot, fz_rect_from_quad(q)); + pdf_update_annot(ctx, annot); } fz_catch(ctx) fz_rethrow(ctx); - pdf_dirty_annot(ctx, annot); return (fz_annot *) annot; } //----------------------------------------------------------------------------- // create a circle or rectangle annotation //----------------------------------------------------------------------------- -struct fz_annot_s *JM_AnnotCircleOrRect(fz_context *ctx, pdf_page *page, fz_rect *rect, int type) +struct fz_annot_s *JM_AnnotCircleOrRect(fz_context *ctx, pdf_page *page, PyObject *rect, int type) { pdf_annot *annot; float col[3] = {0,0,0}; @@ -225,10 +247,10 @@ struct fz_annot_s *JM_AnnotCircleOrRect(fz_context *ctx, pdf_page *page, fz_rect annot = pdf_create_annot(ctx, page, type); pdf_set_annot_border(ctx, annot, width); pdf_set_annot_color(ctx, annot, 3, col); - pdf_set_annot_rect(ctx, annot, rect); + pdf_set_annot_rect(ctx, annot, JM_rect_from_py(rect)); + pdf_update_annot(ctx, annot); } fz_catch(ctx) fz_rethrow(ctx); - pdf_dirty_annot(ctx, annot); return (fz_annot *) annot; } @@ -266,119 +288,178 @@ struct fz_annot_s *JM_AnnotMultiline(fz_context *ctx, pdf_page *page, PyObject * rect.y1 = point.y; } else - fz_include_point_in_rect(&rect, &point); + rect = fz_include_point_in_rect(rect, point); } pdf_set_annot_border(ctx, annot, width); // standard: width = 1 pdf_set_annot_color(ctx, annot, 3, col); // standard: black - fz_expand_rect(&rect, 3 * width); - pdf_set_annot_rect(ctx, annot, &rect); + rect = fz_expand_rect(rect, 3 * width); + pdf_set_annot_rect(ctx, annot, rect); + pdf_update_annot(ctx, annot); } fz_catch(ctx) fz_rethrow(ctx); - pdf_dirty_annot(ctx, annot); return (fz_annot *) annot; } -void JM_draw_pushpin1(fz_context *ctx, fz_path *path) +PyObject *JM_annot_border(fz_context *ctx, pdf_obj *annot_obj) { - fz_moveto(ctx, path, 2.8f, 29.0f); - fz_lineto(ctx, path, 17.2f, 29.0f); - fz_lineto(ctx, path, 13.6f, 22.0f); - fz_lineto(ctx, path, 13.6f, 17.8f); - fz_lineto(ctx, path, 19.0f, 10.8f); - fz_lineto(ctx, path, 1.0f, 10.8f); - fz_lineto(ctx, path, 6.4f, 17.8f); - fz_lineto(ctx, path, 6.4f, 22.0f); - fz_lineto(ctx, path, 2.8f, 29.0f); - fz_closepath(ctx, path); -} + PyObject *res = PyDict_New(); + PyObject *dash_py = PyList_New(0); + PyObject *effect_py = PyList_New(0); + int i; + char *effect2 = NULL, *style = NULL; + float width = -1.0f; + int effect1 = -1; -void JM_draw_pushpin2(fz_context *ctx, fz_path *path) -{ - fz_moveto(ctx, path, 13.6f, 22.0f); - fz_lineto(ctx, path, 6.4f, 22.0f); - fz_moveto(ctx, path, 13.6f, 17.8f); - fz_lineto(ctx, path, 6.4f, 17.8f); - fz_closepath(ctx, path); -} + pdf_obj *o = pdf_dict_get(ctx, annot_obj, PDF_NAME(Border)); + if (pdf_is_array(ctx, o)) + { + width = pdf_to_real(ctx, pdf_array_get(ctx, o, 2)); + if (pdf_array_len(ctx, o) == 4) + { + pdf_obj *dash = pdf_array_get(ctx, o, 3); + for (i = 0; i < pdf_array_len(ctx, dash); i++) + PyList_Append(dash_py, Py_BuildValue("i", + pdf_to_int(ctx, pdf_array_get(ctx, dash, i)))); + } + } -void JM_draw_pushpin3(fz_context *ctx, fz_path *path) -{ - fz_moveto(ctx, path, 9.1f, 10.8f); - fz_lineto(ctx, path, 10.0f, 1.0f); - fz_lineto(ctx, path, 10.9f, 10.8f); - fz_closepath(ctx, path); + pdf_obj *bs_o = pdf_dict_get(ctx, annot_obj, PDF_NAME(BS)); + if (bs_o) + { + o = pdf_dict_get(ctx, bs_o, PDF_NAME(W)); + if (o) width = pdf_to_real(ctx, o); + o = pdf_dict_get(ctx, bs_o, PDF_NAME(S)); + if (o) style = (char *) pdf_to_name(ctx, o); + o = pdf_dict_get(ctx, bs_o, PDF_NAME(D)); + if (o) + { + for (i = 0; i < pdf_array_len(ctx, o); i++) + PyList_Append(dash_py, Py_BuildValue("i", + pdf_to_int(ctx, pdf_array_get(ctx, o, i)))); + } + } + + pdf_obj *be_o = pdf_dict_gets(ctx, annot_obj, "BE"); + if (be_o) + { + o = pdf_dict_get(ctx, be_o, PDF_NAME(S)); + if (o) effect2 = (char *) pdf_to_name(ctx, o); + o = pdf_dict_get(ctx, be_o, PDF_NAME(I)); + if (o) effect1 = pdf_to_int(ctx, o); + } + + PyList_Append(effect_py, Py_BuildValue("i", effect1)); + PyList_Append(effect_py, Py_BuildValue("s", effect2)); + + PyDict_SetItemString(res, "width", Py_BuildValue("f", width)); + + PyDict_SetItemString(res, "dashes", dash_py); + + PyDict_SetItemString(res, "style", Py_BuildValue("s", style)); + + if (effect1 > -1) PyDict_SetItemString(res, "effect", effect_py); + Py_CLEAR(effect_py); + Py_CLEAR(dash_py); + return res; } -void JM_update_file_attachment_annot(fz_context *ctx, pdf_document *doc, pdf_annot *annot) +PyObject *JM_annot_set_border(fz_context *ctx, PyObject *border, pdf_document *doc, pdf_obj *annot_obj) { - static float yellow[3] = {1.0f, 1.0f, 0.0f}; - static float blue[3] = {0.0f, 0.0f, 1.0f}; - static float black[3] = {0.0f, 0.0f, 0.0f}; - static float outline_thickness = 0.9f; - fz_display_list *dlist = NULL; - fz_device *dev = NULL; - fz_colorspace *cs = NULL; - fz_path *path = NULL; - fz_stroke_state *stroke = NULL; - fz_matrix page_ctm; - pdf_page_transform(ctx, annot->page, NULL, &page_ctm); - fz_var(path); - fz_var(stroke); - fz_var(dlist); - fz_var(dev); - fz_var(cs); - fz_try(ctx) + if (!PyDict_Check(border)) { - fz_rect rect; + JM_Warning("arg must be a dict"); + return NONE; // not a dict + } + + double nwidth = -1; // new width + double owidth = -1; // old width + PyObject *ndashes = NULL; // new dashes + PyObject *odashes = NULL; // old dashes + PyObject *nstyle = NULL; // new style + PyObject *ostyle = NULL; // old style + + nwidth = PyFloat_AsDouble(PyDict_GetItemString(border, "width")); + ndashes = PyDict_GetItemString(border, "dashes"); + nstyle = PyDict_GetItemString(border, "style"); - pdf_to_rect(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME_Rect), &rect); - dlist = fz_new_display_list(ctx, NULL); - dev = fz_new_list_device(ctx, dlist); - cs = fz_device_rgb(ctx); /* Borrowed reference */ - stroke = fz_new_stroke_state(ctx); - stroke->linewidth = outline_thickness; - stroke->linejoin = FZ_LINEJOIN_ROUND; - - path = fz_new_path(ctx); - JM_draw_pushpin1(ctx, path); - fz_fill_path(ctx, dev, path, 0, &page_ctm, cs, yellow, 1.0f, NULL); - fz_stroke_path(ctx, dev, path, stroke, &page_ctm, cs, black, 1.0f, NULL); - fz_drop_path(ctx, path); - path = NULL; - - path = fz_new_path(ctx); - JM_draw_pushpin2(ctx, path); - fz_stroke_path(ctx, dev, path, stroke, &page_ctm, cs, black, 1.0f, NULL); - fz_drop_path(ctx, path); - path = NULL; - - path = fz_new_path(ctx); - JM_draw_pushpin3(ctx, path); - fz_fill_path(ctx, dev, path, 0, &page_ctm, cs, blue, 1.0f, NULL); - fz_stroke_path(ctx, dev, path, stroke, &page_ctm, cs, black, 1.0f, NULL); - fz_close_device(ctx, dev); - - fz_transform_rect(&rect, &page_ctm); - pdf_set_annot_appearance(ctx, doc, annot, &rect, dlist); - rect.x0 = rect.y0 = 0; - rect.x1 = 20; - rect.y1 = 30; - pdf_obj *ap = pdf_dict_getl(ctx, annot->obj, PDF_NAME_AP, PDF_NAME_N, NULL); - pdf_dict_put_rect(ctx, ap, PDF_NAME_BBox, &rect); - pdf_drop_obj(ctx, annot->ap); - annot->ap = NULL; + // first get old border properties + PyObject *oborder = JM_annot_border(ctx, annot_obj); + owidth = PyFloat_AsDouble(PyDict_GetItemString(oborder, "width")); + odashes = PyDict_GetItemString(oborder, "dashes"); + ostyle = PyDict_GetItemString(oborder, "style"); + + // then delete any relevant entries + pdf_dict_del(ctx, annot_obj, PDF_NAME(BS)); + pdf_dict_del(ctx, annot_obj, PDF_NAME(BE)); + pdf_dict_del(ctx, annot_obj, PDF_NAME(Border)); + + Py_ssize_t i, n; + int d; + // populate new border array + if (nwidth < 0) nwidth = owidth; // no new width: take current + if (nwidth < 0) nwidth = 0.0f; // default if no width given + if (!ndashes) ndashes = odashes; // no new dashes: take old + if (!nstyle) nstyle = ostyle; // no new style: take old + + if (ndashes && PySequence_Check(ndashes) && PySequence_Size(ndashes) > 0) + { + n = PySequence_Size(ndashes); + pdf_obj *darr = pdf_new_array(ctx, doc, n); + for (i = 0; i < n; i++) + { + d = (int) PyInt_AsLong(PySequence_GetItem(ndashes, i)); + pdf_array_push_int(ctx, darr, (int64_t) d); + } + pdf_dict_putl_drop(ctx, annot_obj, darr, PDF_NAME(BS), PDF_NAME(D), NULL); + nstyle = Py_BuildValue("s", "D"); } - fz_always(ctx) + + pdf_dict_putl_drop(ctx, annot_obj, pdf_new_real(ctx, nwidth), + PDF_NAME(BS), PDF_NAME(W), NULL); + + pdf_obj *val = JM_get_border_style(ctx, nstyle); + + pdf_dict_putl_drop(ctx, annot_obj, val, + PDF_NAME(BS), PDF_NAME(S), NULL); + + PyErr_Clear(); + return NONE; +} + +PyObject *JM_annot_colors(fz_context *ctx, pdf_obj *annot_obj) +{ + PyObject *res = PyDict_New(); + PyObject *bc = PyList_New(0); // stroke colors + PyObject *fc = PyList_New(0); // fill colors + + int i; + float col; + pdf_obj *o = pdf_dict_get(ctx, annot_obj, PDF_NAME(C)); + if (pdf_is_array(ctx, o)) { - fz_drop_device(ctx, dev); - fz_drop_display_list(ctx, dlist); - fz_drop_stroke_state(ctx, stroke); - fz_drop_path(ctx, path); + int n = pdf_array_len(ctx, o); + for (i = 0; i < n; i++) + { + col = pdf_to_real(ctx, pdf_array_get(ctx, o, i)); + PyList_Append(bc, Py_BuildValue("f", col)); + } } - fz_catch(ctx) + PyDict_SetItemString(res, "stroke", bc); + + o = pdf_dict_gets(ctx, annot_obj, "IC"); + if (pdf_is_array(ctx, o)) { - fz_rethrow(ctx); + int n = pdf_array_len(ctx, o); + for (i = 0; i < n; i++) + { + col = pdf_to_real(ctx, pdf_array_get(ctx, o, i)); + PyList_Append(fc, Py_BuildValue("f", col)); + } } -} + PyDict_SetItemString(res, "fill", fc); + Py_CLEAR(bc); + Py_CLEAR(fc); + return res; +} %} \ No newline at end of file diff --git a/fitz/helper-convert.i b/fitz/helper-convert.i index b2e675267..17de5ddc0 100644 --- a/fitz/helper-convert.i +++ b/fitz/helper-convert.i @@ -27,13 +27,13 @@ PyObject *JM_convert_to_pdf(fz_context *ctx, fz_document *doc, int fp, int tp, i fz_try(ctx) { page = fz_load_page(ctx, doc, i); - fz_bound_page(ctx, page, &mediabox); - dev = pdf_page_write(ctx, pdfout, &mediabox, &resources, &contents); - fz_run_page(ctx, page, dev, &fz_identity, NULL); + mediabox = fz_bound_page(ctx, page); + dev = pdf_page_write(ctx, pdfout, mediabox, &resources, &contents); + fz_run_page(ctx, page, dev, fz_identity, NULL); fz_close_device(ctx, dev); fz_drop_device(ctx, dev); dev = NULL; - pdf_obj *page_obj = pdf_add_page(ctx, pdfout, &mediabox, rotate, resources, contents); + pdf_obj *page_obj = pdf_add_page(ctx, pdfout, mediabox, rotate, resources, contents); pdf_insert_page(ctx, pdfout, -1, page_obj); pdf_drop_obj(ctx, page_obj); } diff --git a/fitz/helper-fields.i b/fitz/helper-fields.i index 7e39aa4e1..38f535bfb 100644 --- a/fitz/helper-fields.i +++ b/fitz/helper-fields.i @@ -15,18 +15,17 @@ PyObject *JM_pushbtn_state(fz_context *ctx, pdf_annot *annot) //----------------------------------------------------------------------------- PyObject *JM_checkbox_state(fz_context *ctx, pdf_annot *annot) { - pdf_document *pdf = pdf_get_bound_document(ctx, annot->obj); - pdf_obj *leafv = pdf_get_inheritable(ctx, pdf, annot->obj, PDF_NAME_V); - pdf_obj *leafas = pdf_get_inheritable(ctx, pdf, annot->obj, PDF_NAME_AS); + pdf_obj *leafv = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(V)); + pdf_obj *leafas = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(AS)); if (!leafv) Py_RETURN_FALSE; - if (leafv == PDF_NAME_Off) Py_RETURN_FALSE; - if (leafv == pdf_new_name(ctx, NULL, "Yes")) + if (leafv == PDF_NAME(Off)) Py_RETURN_FALSE; + if (leafv == pdf_new_name(ctx, "Yes")) Py_RETURN_TRUE; if (pdf_is_string(ctx, leafv) && !strcmp(pdf_to_str_buf(ctx, leafv), "Off")) Py_RETURN_FALSE; if (pdf_is_string(ctx, leafv) && !strcmp(pdf_to_str_buf(ctx, leafv), "Yes")) Py_RETURN_TRUE; - if (leafas && leafas == PDF_NAME_Off) Py_RETURN_FALSE; + if (leafas && leafas == PDF_NAME(Off)) Py_RETURN_FALSE; Py_RETURN_TRUE; } @@ -56,9 +55,9 @@ PyObject *JM_listbox_value(fz_context *ctx, pdf_annot *annot) { int i = 0, n = 0; // may be single value or array - pdf_obj *optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME_V); + pdf_obj *optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME(V)); if (pdf_is_string(ctx, optarr)) // a single string - return PyString_FromString(pdf_to_utf8(ctx, optarr)); + return PyString_FromString(pdf_to_text_string(ctx, optarr)); // value is an array (may have len 0) n = pdf_array_len(ctx, optarr); @@ -71,7 +70,7 @@ PyObject *JM_listbox_value(fz_context *ctx, pdf_annot *annot) pdf_obj *elem = pdf_array_get(ctx, optarr, i); if (pdf_is_array(ctx, elem)) elem = pdf_array_get(ctx, elem, 1); - PyList_Append(liste, PyString_FromString(pdf_to_utf8(ctx, elem))); + PyList_Append(liste, PyString_FromString(pdf_to_text_string(ctx, elem))); } return liste; } @@ -97,7 +96,7 @@ PyObject *JM_choice_options(fz_context *ctx, pdf_annot *annot) int n = pdf_choice_widget_options(ctx, pdf, (pdf_widget *) annot, 0, NULL); if (n == 0) return NONE; // wrong widget type - pdf_obj *optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME_Opt); + pdf_obj *optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME(Opt)); int i, m; PyObject *liste = PyList_New(0); @@ -107,12 +106,12 @@ PyObject *JM_choice_options(fz_context *ctx, pdf_annot *annot) if (m == 2) { PyList_Append(liste, Py_BuildValue("ss", - pdf_to_utf8(ctx, pdf_array_get(ctx, pdf_array_get(ctx, optarr, i), 0)), - pdf_to_utf8(ctx, pdf_array_get(ctx, pdf_array_get(ctx, optarr, i), 1)))); + pdf_to_text_string(ctx, pdf_array_get(ctx, pdf_array_get(ctx, optarr, i), 0)), + pdf_to_text_string(ctx, pdf_array_get(ctx, pdf_array_get(ctx, optarr, i), 1)))); } else { - PyList_Append(liste, PyString_FromString(pdf_to_utf8(ctx, pdf_array_get(ctx, optarr, i)))); + PyList_Append(liste, PyString_FromString(pdf_to_text_string(ctx, pdf_array_get(ctx, optarr, i)))); } } return liste; @@ -133,7 +132,7 @@ void JM_set_choice_options(fz_context *ctx, pdf_annot *annot, PyObject *liste) JM_Python_str_DelForPy3(opt); } - pdf_dict_put(ctx, annot->obj, PDF_NAME_Opt, optarr); + pdf_dict_put(ctx, annot->obj, PDF_NAME(Opt), optarr); return; } @@ -157,18 +156,18 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge // ensure a font resources dict /DR --- ----------------------------------- pdf_obj *dr = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), - PDF_NAME_Root, PDF_NAME_AcroForm, PDF_NAME_DR, NULL); + PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(DR), NULL); // new /DR using the object prepared in xref if (!dr) { pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), - PDF_NAME_Root, PDF_NAME_AcroForm, NULL); + PDF_NAME(Root), PDF_NAME(AcroForm), NULL); int xref = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, "_dr_xref")); pdf_obj *f = pdf_new_indirect(ctx, pdf, xref, 0); dr = pdf_new_dict(ctx, pdf, 1); - pdf_dict_put(ctx, dr, PDF_NAME_Font, f); - pdf_dict_put_drop(ctx, form, PDF_NAME_DR, dr); + pdf_dict_put(ctx, dr, PDF_NAME(Font), f); + pdf_dict_put_drop(ctx, form, PDF_NAME(DR), dr); JM_PyErr_Clear; } @@ -180,7 +179,7 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge rect.y1 = (float) PyFloat_AsDouble(PySequence_GetItem(value, 3)); Py_CLEAR(value); JM_PyErr_Clear; - pdf_set_annot_rect(ctx, annot, &rect); // set the rect + pdf_set_annot_rect(ctx, annot, rect); // set the rect // fill color ------------------------------------------------------------- value = PyObject_GetAttrString(Widget, "fill_color"); @@ -206,8 +205,8 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge for (i = 0; i < n; i++) pdf_array_push_int(ctx, dashes, PyInt_AsLong(PySequence_GetItem(value, i))); - pdf_dict_putl_drop(ctx, annot->obj, dashes, PDF_NAME_BS, - PDF_NAME_D, NULL); + pdf_dict_putl_drop(ctx, annot->obj, dashes, PDF_NAME(BS), + PDF_NAME(D), NULL); } Py_CLEAR(value); JM_PyErr_Clear; @@ -221,8 +220,8 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge for (i = 0; i < n; i++) pdf_array_push_real(ctx, border_col, PyFloat_AsDouble(PySequence_GetItem(value, i))); - pdf_dict_putl_drop(ctx, annot->obj, border_col, PDF_NAME_MK, - PDF_NAME_BC, NULL); + pdf_dict_putl_drop(ctx, annot->obj, border_col, PDF_NAME(MK), + PDF_NAME(BC), NULL); } Py_CLEAR(value); JM_PyErr_Clear; @@ -240,7 +239,7 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge int text_maxlen = (int) PyInt_AsLong(PyObject_GetAttrString(Widget, "text_maxlen")); if (text_maxlen) - pdf_dict_put_int(ctx, annot->obj, PDF_NAME_MaxLen, text_maxlen); + pdf_dict_put_int(ctx, annot->obj, PDF_NAME(MaxLen), text_maxlen); JM_PyErr_Clear; } @@ -257,20 +256,20 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge // border style ----------------------------------------------------------- pdf_obj *val = JM_get_border_style(ctx, PyObject_GetAttrString(Widget, "border_style")); - pdf_dict_putl_drop(ctx, annot->obj, val, PDF_NAME_BS, PDF_NAME_S, NULL); + pdf_dict_putl_drop(ctx, annot->obj, val, PDF_NAME(BS), PDF_NAME(S), NULL); // border width ----------------------------------------------------------- float border_width = (float) PyFloat_AsDouble(PyObject_GetAttrString(Widget, "border_width")); - pdf_dict_putl_drop(ctx, annot->obj, pdf_new_real(ctx, pdf, - border_width), PDF_NAME_BS, PDF_NAME_W, NULL); + pdf_dict_putl_drop(ctx, annot->obj, pdf_new_real(ctx, border_width), + PDF_NAME(BS), PDF_NAME(W), NULL); JM_PyErr_Clear; // /DA string ------------------------------------------------------------- char *da = JM_Python_str_AsChar(PyObject_GetAttrString(Widget, "_text_da")); if (da) { - pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME_DA, da); + pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(DA), da); JM_Python_str_DelForPy3(da); pdf_dict_dels(ctx, annot->obj, "DS"); // unsupported pdf_dict_dels(ctx, annot->obj, "RC"); // unsupported @@ -290,7 +289,7 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge } JM_PyErr_Clear; } - pdf_dict_put_int(ctx, annot->obj, PDF_NAME_Ff, Ff); + pdf_dict_put_int(ctx, annot->obj, PDF_NAME(Ff), Ff); // button caption --------------------------------------------------------- if (field_type == PDF_WIDGET_TYPE_RADIOBUTTON || @@ -301,8 +300,8 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge "button_caption")); if (ca) { - pdf_dict_putl(ctx, annot->obj, pdf_new_text_string(ctx, NULL, ca), - PDF_NAME_MK, PDF_NAME_CA, NULL); + pdf_dict_putl(ctx, annot->obj, pdf_new_text_string(ctx, ca), + PDF_NAME(MK), PDF_NAME(CA), NULL); JM_Python_str_DelForPy3(ca); } JM_PyErr_Clear; @@ -322,12 +321,12 @@ void JM_set_widget_properties(fz_context *ctx, pdf_annot *annot, PyObject *Widge if (PyObject_RichCompareBool(value, Py_True, Py_EQ)) { result = pdf_field_set_value(ctx, pdf, annot->obj, "Yes"); - pdf_dict_put_name(ctx, annot->obj, PDF_NAME_V, "Yes"); + pdf_dict_put_name(ctx, annot->obj, PDF_NAME(V), "Yes"); } else { result = pdf_field_set_value(ctx, pdf, annot->obj, "Off"); - pdf_dict_put(ctx, annot->obj, PDF_NAME_V, PDF_NAME_Off); + pdf_dict_put(ctx, annot->obj, PDF_NAME(V), PDF_NAME(Off)); } break; default: diff --git a/fitz/helper-geo-c.i b/fitz/helper-geo-c.i new file mode 100644 index 000000000..5f647d038 --- /dev/null +++ b/fitz/helper-geo-c.i @@ -0,0 +1,247 @@ +%{ + +//----------------------------------------------------------------------------- +// Functions converting betwenn PySequences and fitz geometry objects +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// Incomplete - not in use +//----------------------------------------------------------------------------- +/* +int JM_is_valid_quad(fz_quad q) +{ + fz_point b = fz_normalize_vector(fz_make_point(q.ur.x - q.ul.x, q.ur.y - q.ul.y)); + if ((fabs(b.x) + fabs(b.y)) <= JM_EPS) return 0; // empty quad! + + fz_point c = fz_normalize_vector(fz_make_point(q.ll.x - q.ul.x, q.ll.y - q.ul.y)); + if ((fabs(c.x) + fabs(c.y)) <= JM_EPS) return 0; // empty quad! + + if (fabs(b.x * c.x + b.y * c.y) > JM_EPS) + return 0; // angle at UL != 90 deg + + b = fz_normalize_vector(fz_make_point(q.ur.x - q.lr.x, q.ur.y - q.lr.y)); + c = fz_normalize_vector(fz_make_point(q.ll.x - q.lr.x, q.ll.y - q.lr.y)); + if (fabs(b.x * c.x + b.y * c.y) > JM_EPS) + return 0; // angle at LR != 90 deg + + b = fz_normalize_vector(fz_make_point(q.ul.x - q.ll.x, q.ul.y - q.ll.y)); + c = fz_normalize_vector(fz_make_point(q.lr.x - q.ll.x, q.lr.y - q.ll.y)); + if (fabs(b.x * c.x + b.y * c.y) > JM_EPS) + return 0; // angle at LL != 90 deg + return 1; +} +*/ + +//----------------------------------------------------------------------------- +// PySequence to quad. Default: quad of (0, 0) points. +// Four floats are treated as coordinates of a rect, and its corners will +// define the quad. +//----------------------------------------------------------------------------- +fz_quad JM_quad_from_py(PyObject *r) +{ + fz_quad q; + fz_point p[4]; + size_t i; + q.ul = q.ur = q.ll = q.lr = fz_make_point(0,0); + + if (!r || !PySequence_Check(r) || PySequence_Size(r) != 4) + return q; + + double x0 = PyFloat_AsDouble(PySequence_GetItem(r, 0)); + if (!PyErr_Occurred()) // assume case 1: a rect is given + { + double y0 = PyFloat_AsDouble(PySequence_GetItem(r, 1)); + if (PyErr_Occurred()) goto return_simple; + + double x1 = PyFloat_AsDouble(PySequence_GetItem(r, 2)); + if (PyErr_Occurred()) goto return_simple; + + double y1 = PyFloat_AsDouble(PySequence_GetItem(r, 3)); + if (PyErr_Occurred()) goto return_simple; + + q.ul = fz_make_point(x0, y0); + q.ur = fz_make_point(x1, y0); + q.ll = fz_make_point(x0, y1); + q.lr = fz_make_point(x1, y1); + + return_simple: ; + PyErr_Clear(); + return q; + } + + PyErr_Clear(); + for (i = 0; i < 4; i++) + { + PyObject *o = PySequence_GetItem(r, i); + p[i].x = p[i].y = 0; + if (!PySequence_Check(o) || PySequence_Size(o) != 2) + goto weiter; + + p[i].x = PyFloat_AsDouble(PySequence_GetItem(o, 0)); + if (PyErr_Occurred()) + p[i].x = 0; + + p[i].y = PyFloat_AsDouble(PySequence_GetItem(o, 1)); + if (PyErr_Occurred()) + p[i].y = 0; + + PyErr_Clear(); + weiter: ; + Py_CLEAR(o); + } + q.ul = p[0]; + q.ur = p[1]; + q.ll = p[2]; + q.lr = p[3]; + return q; +} + +//----------------------------------------------------------------------------- +// PySequence to rect. Default: infinite rect +//----------------------------------------------------------------------------- +fz_rect JM_rect_from_py(PyObject *r) +{ + if (!r || !PySequence_Check(r) || PySequence_Size(r) != 4) + return fz_infinite_rect; + + double x0 = PyFloat_AsDouble(PySequence_GetItem(r, 0)); + if (PyErr_Occurred()) goto return_empty; + + double y0 = PyFloat_AsDouble(PySequence_GetItem(r, 1)); + if (PyErr_Occurred()) goto return_empty; + + double x1 = PyFloat_AsDouble(PySequence_GetItem(r, 2)); + if (PyErr_Occurred()) goto return_empty; + + double y1 = PyFloat_AsDouble(PySequence_GetItem(r, 3)); + if (PyErr_Occurred()) goto return_empty; + + return fz_make_rect((float) x0, (float) y0, (float) x1, (float) y1); + + return_empty: ; + PyErr_Clear(); + return fz_infinite_rect; +} + +//----------------------------------------------------------------------------- +// fz_rect to PySequence +//----------------------------------------------------------------------------- +PyObject *JM_py_from_rect(fz_rect r) +{ + return Py_BuildValue("ffff", r.x0, r.y0, r.x1, r.y1); +} + +//----------------------------------------------------------------------------- +// PySequence to fz_irect. Default: infinite irect +//----------------------------------------------------------------------------- +fz_irect JM_irect_from_py(PyObject *r) +{ + if (!r || !PySequence_Check(r) || PySequence_Size(r) != 4) + return fz_infinite_irect; + + long x0 = PyLong_AsLong(PySequence_GetItem(r, 0)); + if (PyErr_Occurred()) goto return_empty; + + long y0 = PyLong_AsLong(PySequence_GetItem(r, 1)); + if (PyErr_Occurred()) goto return_empty; + + long x1 = PyLong_AsLong(PySequence_GetItem(r, 2)); + if (PyErr_Occurred()) goto return_empty; + + long y1 = PyLong_AsLong(PySequence_GetItem(r, 3)); + if (PyErr_Occurred()) goto return_empty; + + return fz_make_irect((int) x0, (int) y0, (int) x1, (int) y1); + + return_empty: ; + PyErr_Clear(); + return fz_infinite_irect; +} + +//----------------------------------------------------------------------------- +// fz_irect to PySequence +//----------------------------------------------------------------------------- +PyObject *JM_py_from_irect(fz_irect r) +{ + return Py_BuildValue("iiii", r.x0, r.y0, r.x1, r.y1); +} + + +//----------------------------------------------------------------------------- +// PySequence to fz_point. Default: (0, 0) +//----------------------------------------------------------------------------- +fz_point JM_point_from_py(PyObject *p) +{ + fz_point p0 = {0,0}; + if (!p || !PySequence_Check(p) || PySequence_Size(p) != 2) + return p0; + + double x = PyFloat_AsDouble(PySequence_GetItem(p, 0)); + if (PyErr_Occurred()) goto zero_point; + + double y = PyFloat_AsDouble(PySequence_GetItem(p, 1)); + if (PyErr_Occurred()) goto zero_point; + + return fz_make_point((float) x, (float) y); + + zero_point: ; + PyErr_Clear(); + return p0; +} + +//----------------------------------------------------------------------------- +// fz_point to PySequence +//----------------------------------------------------------------------------- +PyObject *JM_py_from_point(fz_point p) +{ + return Py_BuildValue("ff", p.x, p.y); +} + + +//----------------------------------------------------------------------------- +// PySequence to fz_matrix. Default: fz_identity +//----------------------------------------------------------------------------- +fz_matrix JM_matrix_from_py(PyObject *m) +{ + fz_matrix m0 = fz_identity; + if (!m || !PySequence_Check(m) || PySequence_Size(m) != 6) + return m0; + + double a = PyFloat_AsDouble(PySequence_GetItem(m, 0)); + if (PyErr_Occurred()) goto fertig; + + double b = PyFloat_AsDouble(PySequence_GetItem(m, 1)); + if (PyErr_Occurred()) goto fertig; + + double c = PyFloat_AsDouble(PySequence_GetItem(m, 2)); + if (PyErr_Occurred()) goto fertig; + + double d = PyFloat_AsDouble(PySequence_GetItem(m, 3)); + if (PyErr_Occurred()) goto fertig; + + double e = PyFloat_AsDouble(PySequence_GetItem(m, 4)); + if (PyErr_Occurred()) goto fertig; + + double f = PyFloat_AsDouble(PySequence_GetItem(m, 5)); + if (PyErr_Occurred()) goto fertig; + + m0.a = a; + m0.b = b; + m0.c = c; + m0.d = d; + m0.e = e; + m0.f = f; + + fertig: ; + PyErr_Clear(); + return m0; +} + +//----------------------------------------------------------------------------- +// fz_matrix to PySequence +//----------------------------------------------------------------------------- +PyObject *JM_py_from_matrix(fz_matrix m) +{ + return Py_BuildValue("ffffff", m.a, m.b, m.c, m.d, m.e, m.f); +} + +%} \ No newline at end of file diff --git a/fitz/helper-geo-py.i b/fitz/helper-geo-py.i new file mode 100644 index 000000000..2b91ff079 --- /dev/null +++ b/fitz/helper-geo-py.i @@ -0,0 +1,894 @@ +%pythoncode %{ +class Matrix(): + """Matrix() - all zeros\nMatrix(a, b, c, d, e, f)\nMatrix(zoom-x, zoom-y) - zoom\nMatrix(shear-x, shear-y, 1) - shear\nMatrix(degree) - rotate\nMatrix(Matrix) - new copy\nMatrix(sequence) - from 'sequence'""" + def __init__(self, *args): + if not args: + self.a = self.b = self.c = self.d = self.e = self.f = 0.0 + return None + if len(args) > 6: + raise ValueError("illegal sequ. length") + if len(args) == 6: # 6 numbers + self.a = float(args[0]) + self.b = float(args[1]) + self.c = float(args[2]) + self.d = float(args[3]) + self.e = float(args[4]) + self.f = float(args[5]) + return None + if len(args) == 1: # either an angle or a sequ + if hasattr(args[0], "__float__"): + theta = args[0] * math.pi / 180.0 + c = math.cos(theta) + s = math.sin(theta) + self.a = self.d = c + self.b = s + self.c = -s + self.e = self.f = 0.0 + return None + else: + self.a = float(args[0][0]) + self.b = float(args[0][1]) + self.c = float(args[0][2]) + self.d = float(args[0][3]) + self.e = float(args[0][4]) + self.f = float(args[0][5]) + return None + if len(args) == 2 or len(args) == 3 and args[2] == 0: + self.a, self.b, self.c, self.d, self.e, self.f = float(args[0]), \ + 0.0, 0.0, float(args[1]), 0.0, 0.0 + return None + if len(args) == 3 and args[2] == 1: + self.a, self.b, self.c, self.d, self.e, self.f = 1.0, \ + float(args[1]), float(args[0]), 1.0, 0.0, 0.0 + return None + raise ValueError("illegal Matrix constructor") + + def invert(self, src=None): + """Calculate the inverted matrix. Return 0 if successful and replace + current one. Else return 1 and do nothing. + """ + if src is None: + src = self + dst = TOOLS.invert_matrix(src) + if min(dst) >= max(dst): + return 1 + self.a = dst[0] + self.b = dst[1] + self.c = dst[2] + self.d = dst[3] + self.e = dst[4] + self.f = dst[5] + return 0 + + def preTranslate(self, tx, ty): + """Calculate pre translation and replace current matrix.""" + self.e += tx * self.a + ty * self.c + self.f += tx * self.b + ty * self.d + return self + + def preScale(self, sx, sy): + """Calculate pre scaling and replacing current matrix.""" + self.a *= sx + self.b *= sx + self.c *= sy + self.d *= sy + return self + + def preShear(self, h, v): + """Calculate pre shearing and replace current matrix.""" + a, b = self.a, self.b + self.a += v * self.c + self.b += v * self.d + self.c += h * a + self.d += h * b + return self + + def preRotate(self, theta): + """Calculate pre rotation and replace current matrix.""" + while theta < 0: theta += 360 + while theta >= 360: theta -= 360 + epsilon = 1e-5 + if abs(0 - theta) < epsilon: + pass + + elif abs(90.0 - theta) < epsilon: + a = self.a + b = self.b + self.a = self.c + self.b = self.d + self.c = -a + self.d = -b + + elif abs(180.0 - theta) < epsilon: + self.a = -self.a + self.b = -self.b + self.c = -self.c + self.d = -self.d + + elif abs(270.0 - theta) < epsilon: + a = self.a + b = self.b + self.a = -self.c + self.b = -self.d + self.c = a + self.d = b + + else: + s = math.sin(theta * math.pi / 180.0) + c = math.cos(theta * math.pi / 180.0) + a = self.a + b = self.b + self.a = c * a + s * self.c + self.b = c * b + s * self.d + self.c =-s * a + c * self.c + self.d =-s * b + c * self.d + + return self + + def concat(self, one, two): + """Multiply two matrices and replace current one.""" + dst = Matrix() + dst.a = one[0] * two[0] + one[1] * two[2] + dst.b = one[0] * two[1] + one[1] * two[3] + dst.c = one[2] * two[0] + one[3] * two[2] + dst.d = one[2] * two[1] + one[3] * two[3] + dst.e = one[4] * two[0] + one[5] * two[2] + two[4] + dst.f = one[4] * two[1] + one[5] * two[3] + two[5] + self = dst + return self + + def __getitem__(self, i): + return (self.a, self.b, self.c, self.d, self.e, self.f)[i] + + def __setitem__(self, i, v): + if i == 0: self.a = v + elif i == 1: self.b = v + elif i == 2: self.c = v + elif i == 3: self.d = v + elif i == 4: self.e = v + elif i == 5: self.f = v + else: + raise IndexError("index out of range") + return + + def __len__(self): + return 6 + + def __repr__(self): + return "Matrix" + str(tuple(self)) + + def __invert__(self): + m1 = Matrix() + m1.invert(self) + return m1 + __inv__ = __invert__ + + def __mul__(self, m): + if hasattr(m, "__float__"): + return Matrix(self.a * m, self.b * m, self.c * m, + self.d * m, self.e * m, self.f * m) + a = self.a * m[0] + self.b * m[2] + b = self.a * m[1] + self.b * m[3] + c = self.c * m[0] + self.d * m[2] + d = self.c * m[1] + self.d * m[3] + e = self.e * m[0] + self.f * m[2] + m[4] + f = self.e * m[1] + self.f * m[3] + m[5] + return Matrix(a, b, c, d, e, f) + + def __truediv__(self, m): + if hasattr(m, "__float__"): + return Matrix(self.a * 1./m, self.b * 1./m, self.c * 1./m, + self.d * 1./m, self.e * 1./m, self.f * 1./m) + m1 = TOOLS.invert_matrix(m) + if min(m1) >= max(m1): + raise ZeroDivisionError("op2 is not invertible") + return self.concat(self, m1) + __div__ = __truediv__ + + def __add__(self, m): + if hasattr(m, "__float__"): + return Matrix(self.a + m, self.b + m, self.c + m, + self.d + m, self.e + m, self.f + m) + return Matrix(self.a + m[0], self.b + m[1], self.c + m[2], + self.d + m[3], self.e + m[4], self.f + m[5]) + + def __sub__(self, m): + if hasattr(m, "__float__"): + return Matrix(self.a - m, self.b - m, self.c - m, + self.d - m, self.e - m, self.f - m) + return Matrix(self.a - m[0], self.b - m[1], self.c - m[2], + self.d - m[3], self.e - m[4], self.f - m[5]) + + def __pos__(self): + return Matrix(self) + + def __neg__(self): + return Matrix(-self.a, -self.b, -self.c, -self.d, -self.e, -self.f) + + def __bool__(self): + return max(self) > 0 or min(self) < 0 + + def __nonzero__(self): + return max(self) > 0 or min(self) < 0 + + def __eq__(self, m): + if not hasattr(m, "__len__"): + return False + return len(m) == 6 and bool(self - m) is False + + def __abs__(self): + return math.sqrt(self.a*self.a + self.b*self.b + self.c*self.c + \ + self.d*self.d + self.e*self.e + self.f*self.f) + + @property + def isRectilinear(self): + epsilon = 1e-5 + return (abs(self.b) < epsilon and abs(self.c) < epsilon) or \ + (abs(self.a) < epsilon and abs(self.d) < epsilon); + + def __hash__(self): + return hash(tuple(self)) + + +class IdentityMatrix(Matrix): + """Identity matrix [1, 0, 0, 1, 0, 0]""" + def __init__(self): + Matrix.__init__(self, 1.0, 1.0) + def __setattr__(self, name, value): + if name in "ad": + self.__dict__[name] = 1.0 + elif name in "bcef": + self.__dict__[name] = 0.0 + else: + self.__dict__[name] = value + + def checkargs(*args): + raise NotImplementedError("Identity is readonly") + preRotate = checkargs + preShear = checkargs + preScale = checkargs + preTranslate = checkargs + concat = checkargs + invert = checkargs + def __repr__(self): + return "IdentityMatrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)" + +Identity = IdentityMatrix() + +class Point(): + """Point() - all zeros\nPoint(x, y)\nPoint(Point) - new copy\nPoint(sequence) - from 'sequence'""" + def __init__(self, *args): + if not args: + self.x = 0.0 + self.y = 0.0 + return None + + if len(args) > 2: + raise ValueError("illegal sequ. length") + if len(args) == 2: + self.x = float(args[0]) + self.y = float(args[1]) + return None + if len(args) == 1: + l = args[0] + if hasattr(l, "__getitem__") is False: + raise ValueError("illegal Point constructor") + if len(l) != 2: + raise ValueError("illegal sequ. length") + self.x = float(l[0]) + self.y = float(l[1]) + return None + raise ValueError("illegal Point constructor") + + def transform(self, m): + """Replace point by its transformation with matrix m.""" + x = self.x + self.x = x * m[0] + self.y * m[2] + m[4] + self.y = x * m[1] + self.y * m[3] + m[5] + return self + + @property + def unit(self): + """Return unit vector of a point.""" + s = self.x * self.x + self.y * self.y + if s < 1e-5: + return Point(0,0) + s = math.sqrt(s) + return Point(self.x / s, self.y / s) + + @property + def abs_unit(self): + """Return unit vector of a point with positive coordinates.""" + s = self.x * self.x + self.y * self.y + if s < 1e-5: + return Point(0,0) + s = math.sqrt(s) + return Point(abs(self.x) / s, abs(self.y) / s) + + def distance_to(self, *args): + """Return the distance to a rectangle or another point.""" + assert len(args) > 0, "at least one parameter must be given" + x = args[0] + if len(args) > 1: + unit = args[1] + else: + unit = "px" + u = {"px": (1.,1.), "in": (1.,72.), "cm": (2.54, 72.), "mm": (25.4, 72.)} + f = u[unit][0] / u[unit][1] + if type(x) is Point: + return abs(self - x) * f + + # from here on, x is a rectangle + # as a safeguard, make a finite copy of it + r = Rect(x.top_left, x.top_left) + r = r | x.bottom_right + if self in r: + return 0.0 + if self.x > r.x1: + if self.y >= r.y1: + return self.distance_to(r.bottom_right, unit) + elif self.y <= r.y0: + return self.distance_to(r.top_right, unit) + else: + return (self.x - r.x1) * f + elif r.x0 <= self.x <= r.x1: + if self.y >= r.y1: + return (self.y - r.y1) * f + else: + return (r.y0 - self.y) * f + else: + if self.y >= r.y1: + return self.distance_to(r.bottom_left, unit) + elif self.y <= r.y0: + return self.distance_to(r.top_left, unit) + else: + return (r.x0 - self.x) * f + + def __getitem__(self, i): + return (self.x, self.y)[i] + + def __len__(self): + return 2 + + def __setitem__(self, i, v): + if i == 0: self.x = float(v) + elif i == 1: self.y = float(v) + else: + raise IndexError("index out of range") + return None + + def __repr__(self): + return "Point" + str(tuple(self)) + + def __pos__(self): + return Point(self) + + def __neg__(self): + return Point(-self.x, -self.y) + + def __bool__(self): + return max(self) > 0 or min(self) < 0 + + def __nonzero__(self): + return max(self) > 0 or min(self) < 0 + + def __eq__(self, p): + if not hasattr(p, "__len__"): + return False + return len(p) == 2 and bool(self - p) is False + + def __abs__(self): + return math.sqrt(self.x * self.x + self.y * self.y) + + def __add__(self, p): + if hasattr(p, "__float__"): + return Point(self.x + p, self.y + p) + return Point(self.x + p[0], self.y + p[1]) + + def __sub__(self, p): + if hasattr(p, "__float__"): + return Point(self.x - p, self.y - p) + return Point(self.x - p[0], self.y - p[1]) + + def __mul__(self, m): + if hasattr(m, "__float__"): + return Point(self.x * m, self.y * m) + p = Point(self) + return p.transform(m) + + def __truediv__(self, m): + if hasattr(m, "__float__"): + return Point(self.x * 1./m, self.y * 1./m) + m1 = TOOLS.invert_matrix(m) + if min(m1) >= max(m1): + raise ZeroDivisionError("op2 is not invertible") + p = Point(self) + return p.transform(m1) + + __div__ = __truediv__ + + def __hash__(self): + return hash(tuple(self)) + +class Rect(): + """Rect() - all zeros\nRect(x0, y0, x1, y1)\nRect(top-left, x1, y1)\nRect(x0, y0, bottom-right)\nRect(top-left, bottom-right)\nRect(Rect or IRect) - new copy\nRect(sequence) - from 'sequence'""" + def __init__(self, *args): + if not args: + self.x0 = self.y0 = self.x1 = self.y1 = 0.0 + return None + + if len(args) > 4: + raise ValueError("invalid sequ. length") + if len(args) == 4: + self.x0 = float(args[0]) + self.y0 = float(args[1]) + self.x1 = float(args[2]) + self.y1 = float(args[3]) + return None + if len(args) == 1: + l = args[0] + if hasattr(l, "__getitem__") is False: + raise ValueError("invalid Rect constructor") + if len(l) != 4: + raise ValueError("invalid sequ. length") + self.x0 = float(l[0]) + self.y0 = float(l[1]) + self.x1 = float(l[2]) + self.y1 = float(l[3]) + return None + if len(args) == 2: # 2 Points provided + self.x0 = float(args[0][0]) + self.y0 = float(args[0][1]) + self.x1 = float(args[1][0]) + self.y1 = float(args[1][1]) + return None + if len(args) == 3: # 2 floats and 1 Point provided + a0 = args[0] + a1 = args[1] + a2 = args[2] + if hasattr(a0, "__float__"): # (float, float, Point) provided + self.x0 = float(a0) + self.y0 = float(a1) + self.x1 = float(a2[0]) + self.y1 = float(a2[1]) + return None + self.x0 = float(a0[0]) # (Point, float, float) provided + self.y0 = float(a0[1]) + self.x1 = float(a1) + self.y1 = float(a2) + return None + raise ValueError("invalid Rect constructor") + + def normalize(self): + """Replace rectangle with its finite version.""" + if self.x1 < self.x0: + self.x0, self.x1 = self.x1, self.x0 + if self.y1 < self.y0: + self.y0, self.y1 = self.y1, self.y0 + return self + + @property + def isEmpty(self): + """Check if rectangle area is empty.""" + return self.x0 == self.x1 or self.y0 == self.y1 + + @property + def isInfinite(self): + """Check if rectangle is infinite.""" + return self.x0 > self.x1 or self.y0 > self.y1 + + @property + def top_left(self): + return Point(self.x0, self.y0) + + @property + def top_right(self): + return Point(self.x1, self.y0) + + @property + def bottom_left(self): + return Point(self.x0, self.y1) + + @property + def bottom_right(self): + return Point(self.x1, self.y1) + + tl = top_left + tr = top_right + bl = bottom_left + br = bottom_right + + @property + def quad(self): + return Quad(self.tl, self.tr, self.bl, self.br) + + def round(self): + r = Rect(self).normalize() + ir = IRect(math.floor(r.x0), math.floor(r.y0), math.ceil(r.x1), math.ceil(r.y1)) + return ir + + irect = property(round) + + width = property(lambda self: abs(self.x1 - self.x0)) + height = property(lambda self: abs(self.y1 - self.y0)) + + def includePoint(self, p): + """Extend rectangle to include point p.""" + x0 = min(self.x0, self.x1, p[0]) + x1 = max(self.x0, self.x1, p[0]) + y0 = min(self.y0, self.y1, p[1]) + y1 = max(self.y0, self.y1, p[1]) + self.x0 = x0 + self.y0 = y0 + self.x1 = x1 + self.y1 = y1 + return self + + def includeRect(self, r): + """Extend rectangle to include rectangle r.""" + x0 = min(self.x0, self.x1, r[0], r[2]) + x1 = max(self.x0, self.x1, r[0], r[2]) + y0 = min(self.y0, self.y1, r[1], r[3]) + y1 = max(self.y0, self.y1, r[1], r[3]) + self.x0 = x0 + self.y0 = y0 + self.x1 = x1 + self.y1 = y1 + return self + + def intersect(self, r): + """Restrict rectangle to common area with rectangle r.""" + if self.isEmpty: return Rect() + r1 = Rect(r) + if r1.isEmpty: return Rect() + if r1.isInfinite: return self + if self.isInfinite: return r1 + x0 = max(self.x0, r1.x0) + x1 = min(self.x1, r1.x1) + y0 = max(self.y0, r1.y0) + y1 = min(self.y1, r1.y1) + if x1 < x0 or y1 < y0: + self = Rect() + else: + self = Rect(x0, y0, x1, y1) + return self + + + def __getitem__(self, i): + return (self.x0, self.y0, self.x1, self.y1)[i] + + def __len__(self): + return 4 + + def __setitem__(self, i, v): + if i == 0: self.x0 = float(v) + elif i == 1: self.y0 = float(v) + elif i == 2: self.x1 = float(v) + elif i == 3: self.y1 = float(v) + else: + raise IndexError("index out of range") + return None + + def __repr__(self): + return "Rect" + str(tuple(self)) + + def __pos__(self): + return Rect(self) + + def __neg__(self): + return Rect(-self.x0, -self.y0, -self.x1, -self.y1) + + def __bool__(self): + return max(self) > 0 or min(self) < 0 + + def __nonzero__(self): + return max(self) > 0 or min(self) < 0 + + def __eq__(self, p): + if not hasattr(p, "__len__"): + return False + return len(p) == 4 and bool(self - p) is False + + def __abs__(self): + if self.isEmpty or self.isInfinite: + return 0.0 + return (self.x1 - self.x0) * (self.y1 - self.y0) + + def __add__(self, p): + if hasattr(p, "__float__"): + r = Rect(self.x0 + p, self.y0 + p, self.x1 + p, self.y1 + p) + else: + r = Rect(self.x0 + p[0], self.y0 + p[1], self.x1 + p[2], self.y1 + p[3]) + return r + + def __sub__(self, p): + if hasattr(p, "__float__"): + return Rect(self.x0 - p, self.y0 - p, self.x1 - p, self.y1 - p) + return Rect(self.x0 - p[0], self.y0 - p[1], self.x1 - p[2], self.y1 - p[3]) + + def transform(self, m): + """Replace rectangle with its transformation by matrix m.""" + self = Rect(TOOLS.transform_rect(self, m)) + return self + + def __mul__(self, m): + if hasattr(m, "__float__"): + return Rect(self.x0 * m, self.y0 * m, self.x1 * m, self.y1 * m) + r = Rect(self) + r = r.transform(m) + return r + + def __truediv__(self, m): + if hasattr(m, "__float__"): + return Rect(self.x0 * 1./m, self.y0 * 1./m, self.x1 * 1./m, self.y1 * 1./m) + im = TOOLS.invert_matrix(m) + if min(im) >= max(im): + raise ZeroDivisionError("op2 is not invertible") + r = Rect(self) + r = r.transform(im) + return r + + __div__ = __truediv__ + + def __contains__(self, x): + if hasattr(x, "__float__"): + return x in tuple(self) + l = len(x) + r = Rect(self).normalize() + if l == 4: + if r.isEmpty: return False + xr = Rect(x).normalize() + if xr.isEmpty: return True + if r.x0 <= xr.x0 and r.y0 <= xr.y0 and \ + r.x1 >= xr.x1 and r.y1 >= xr.y1: + return True + return False + if l == 2: + if r.x0 <= x[0] <= r.x1 and \ + r.y0 <= x[1] <= r.y1: + return True + return False + return False + + def __or__(self, x): + if not hasattr(x, "__len__"): + raise ValueError("op2 is an unsupported type") + + r = Rect(self) + if len(x) == 2: + return r.includePoint(x) + if len(x) == 4: + return r.includeRect(x) + raise ValueError("op2 is an unsupported type") + + def __and__(self, x): + if not hasattr(x, "__len__"): + raise ValueError("op2 is an unsupported type") + + r1 = Rect(x) + r = Rect(self) + return r.intersect(r1) + + def intersects(self, x): + """Check if intersection with rectangle x is not empty.""" + r1 = Rect(x) + if self.isEmpty or self.isInfinite or r1.isEmpty: + return False + r = Rect(self) + if r.intersect(r1).isEmpty: + return False + return True + + def __hash__(self): + return hash(tuple(self)) + +class IRect(Rect): + """IRect() - all zeros\nIRect(x0, y0, x1, y1)\nIRect(Rect or IRect) - new copy\nIRect(sequence) - from 'sequence'""" + def __init__(self, *args): + Rect.__init__(self, *args) + self.x0 = math.floor(self.x0) + self.y0 = math.floor(self.y0) + self.x1 = math.ceil(self.x1) + self.y1 = math.ceil(self.y1) + return None + + @property + def round(self): + pass + + irect = round + + @property + def rect(self): + return Rect(self) + + def __repr__(self): + return "IRect" + str(tuple(self)) + + def includePoint(self, p): + """Extend rectangle to include point p.""" + return Rect.includePoint(self, p).round() + + def includeRect(self, r): + """Extend rectangle to include rectangle r.""" + return Rect.includeRect(self, r).round() + + def intersect(self, r): + """Restrict rectangle to intersection with rectangle r.""" + return Rect.intersect(self, r).round() + + def __setitem__(self, i, v): + if i == 0: self.x0 = int(v) + elif i == 1: self.y0 = int(v) + elif i == 2: self.x1 = int(v) + elif i == 3: self.y1 = int(v) + else: + raise IndexError("index out of range") + return None + + def __pos__(self): + return IRect(self) + + def __neg__(self): + return IRect(-self.x0, -self.y0, -self.x1, -self.y1) + + def __add__(self, p): + return Rect.__add__(self, p).round() + + def __sub__(self, p): + return Rect.__sub__(self, p).round() + + def transform(self, m): + return Rect.transform(self, m).round() + + def __mul__(self, m): + return Rect.__mul__(self, m).round() + + def __truediv__(self, m): + return Rect.__truediv__(self, m).round() + + def __or__(self, x): + return Rect.__or__(self, x).round() + + def __and__(self, x): + return Rect.__and__(self, x).round() + +class Quad(): + """Quad() - all zero points\nQuad(ul, ur, ll, lr)\nQuad(quad) - new copy\nQuad(sequence) - from 'sequence'""" + def __init__(self, *args): + if not args: + self.ul = self.ur = self.ll = self.lr = Point() + return None + + if len(args) > 4: + raise ValueError("invalid sequ. length") + if len(args) == 4: + self.ul = Point(args[0]) + self.ur = Point(args[1]) + self.ll = Point(args[2]) + self.lr = Point(args[3]) + return None + if len(args) == 1: + l = args[0] + if hasattr(l, "__getitem__") is False: + raise ValueError("invalid Quad constructor") + if len(l) != 4: + raise ValueError("invalid sequ. length") + self.ul = Point(l[0]) + self.ur = Point(l[1]) + self.ll = Point(l[2]) + self.lr = Point(l[3]) + return None + raise ValueError("invalid Quad constructor") + + @property + def isRectangular(self): + upper = (self.ur - self.ul).unit + if not bool(upper): + return False + right = (self.lr - self.ur).unit + if not bool(right): + return False + left = (self.ll - self.ul).unit + if not bool(left): + return False + lower = (self.lr - self.ll).unit + if not bool(lower): + return False + eps = 1e-5 + + return abs(sum(map(lambda x,y: x*y, upper, right))) <= eps and \ + abs(sum(map(lambda x,y: x*y, upper, left))) <= eps and \ + abs(sum(map(lambda x,y: x*y, left, lower))) <= eps + + @property + def isEmpty(self): + if self.isRectangular: + return False + eps = 1e-5 + ul = Point() + ur = (self.ur - self.ul).abs_unit + lr = (self.lr - self.ul).abs_unit + ll = (self.ll - self.ul).abs_unit + if max(ur.y, lr.y, ll.y) - min(ur.y, lr.y, ll.y) < eps: + return True + return False + + width = property(lambda self: max(abs(self.ul - self.ur), abs(self.ll - self.lr))) + height = property(lambda self: max(abs(self.ul - self.ll), abs(self.ur - self.lr))) + + @property + def rect(self): + return Rect(self.ul, self.ur) | self.ll | self.lr + + def __getitem__(self, i): + return (self.ul, self.ur, self.ll, self.lr)[i] + + def __len__(self): + return 4 + + def __setitem__(self, i, v): + if i == 0: self.ul = Point(v) + elif i == 1: self.ur = Point(v) + elif i == 2: self.ll = Point(v) + elif i == 3: self.lr = Point(v) + else: + raise IndexError("index out of range") + return None + + def __repr__(self): + return "Quad" + str(tuple(self)) + + def __pos__(self): + return Quad(self) + + def __neg__(self): + return Quad(-self.ul, -self.ur, -self.ll, -self.lr) + + def __bool__(self): + return not self.isEmpty + + def __nonzero__(self): + return not self.isEmpty + + def __eq__(self, p): + if not hasattr(p, "__len__"): + return False + return len(p) == 4 and self.ul == p[0] and self.ur == p[1] and \ + self.ll == p[3] and self.lr == p[3] + + def __abs__(self): + if self.isEmpty: + return 0.0 + return abs(self.ul - self.ur) * abs(self.ul - self.ll) + + def transform(self, m): + """Replace quad by its transformation with matrix m.""" + self.ul *= m + self.ur *= m + self.ll *= m + self.lr *= m + return self + + def __mul__(self, m): + r = Quad(self) + r = r.transform(m) + return r + + def __truediv__(self, m): + r = Quad(self) + if hasattr(m, "__float__"): + im = 1. / m + else: + im = TOOLS.invert_matrix(m) + if min(im) >= max(im): + raise ZeroDivisionError("op2 is not invertible") + r = r.transform(im) + return r + + __div__ = __truediv__ + + def __hash__(self): + return hash(tuple(self)) + +%} \ No newline at end of file diff --git a/fitz/helper-other.i b/fitz/helper-other.i index a9056c526..d1cce1451 100644 --- a/fitz/helper-other.i +++ b/fitz/helper-other.i @@ -5,6 +5,13 @@ void JM_TRACE(const char *id) PySys_WriteStdout("%s\n", id); } +// put warnings on Python-stdout +void JM_Warning(const char *id) +{ + PySys_WriteStdout("warning: %s\n", id); +} + +#if JM_MEMORY == 1 //----------------------------------------------------------------------------- // The following 3 functions replace MuPDF standard memory allocation. // This will ensure, that MuPDF memory handling becomes part of Python's @@ -12,18 +19,11 @@ void JM_TRACE(const char *id) //----------------------------------------------------------------------------- static void *JM_Py_Malloc(void *opaque, size_t size) { - //void *p = PyMem_Malloc(size); - //PySys_WriteStdout("%p alloc %u\n", p, size); - //return p; return PyMem_Malloc(size); } static void *JM_Py_Realloc(void *opaque, void *old, size_t size) { - //void *p = PyMem_Realloc(old, size); - //if (old) PySys_WriteStdout("%p free\n", old); - //PySys_WriteStdout("%p alloc %u\n", p, size); - //return p; return PyMem_Realloc(old, size); } @@ -39,8 +39,9 @@ const fz_alloc_context JM_Alloc_Context = JM_Py_Realloc, JM_PY_Free }; +#endif - +// return Python bools for a given integer PyObject *JM_BOOL(int v) { if (v == 0) @@ -110,7 +111,6 @@ PyObject *JM_fitz_config() PyDict_SetItemString(dict, "svg", JM_BOOL(FZ_ENABLE_SVG)); PyDict_SetItemString(dict, "cbz", JM_BOOL(FZ_ENABLE_CBZ)); PyDict_SetItemString(dict, "img", JM_BOOL(FZ_ENABLE_IMG)); - PyDict_SetItemString(dict, "tiff", JM_BOOL(FZ_ENABLE_TIFF)); PyDict_SetItemString(dict, "html", JM_BOOL(FZ_ENABLE_HTML)); PyDict_SetItemString(dict, "epub", JM_BOOL(FZ_ENABLE_EPUB)); PyDict_SetItemString(dict, "gprf", JM_BOOL(FZ_ENABLE_GPRF)); @@ -131,20 +131,90 @@ PyObject *JM_fitz_config() } //---------------------------------------------------------------------------- -// Return a Python bytes object for an fz_buffer +// Update a color float array with values from a Python sequence. +// Any error condition is treated as a no-op. +//---------------------------------------------------------------------------- +void JM_color_FromSequence(PyObject *color, int *n, float col[4]) +{ + if (!color) return; + if (PyFloat_Check(color)) // maybe just a single float + { + float c = (float) PyFloat_AsDouble(color); + if (!INRANGE(c, 0.0f, 1.0f)) return; + col[0] = c; + *n = 1; + return; + } + if (!PySequence_Check(color)) return; + int len = PySequence_Size(color), i; + if (!INRANGE(len, 1, 4) || len == 2) return; + + float mcol[4] = {0,0,0,0}; // local color storage + for (i = 0; i < len; i++) + { + mcol[i] = (float) PyFloat_AsDouble(PySequence_GetItem(color, i)); + if (PyErr_Occurred()) + { + PyErr_Clear(); // reset Py error indicator + return; + } + if (!INRANGE(mcol[i], 0.0f, 1.0f)) return; + } + + *n = len; + for (i = 0; i < len; i++) + col[i] = mcol[i]; + return; +} + +//---------------------------------------------------------------------------- +// Turn fz_buffer into a Python bytes object //---------------------------------------------------------------------------- PyObject *JM_BinFromBuffer(fz_context *ctx, fz_buffer *buffer) { - PyObject *bytes = NULL; + PyObject *bytes = PyBytes_FromString(""); char *c = NULL; if (buffer) { size_t len = fz_buffer_storage(gctx, buffer, &c); bytes = PyBytes_FromStringAndSize(c, (Py_ssize_t) len); } - else + return bytes; +} + +//---------------------------------------------------------------------------- +// Turn fz_buffer into a Python bytearray object +//---------------------------------------------------------------------------- +PyObject *JM_BArrayFromBuffer(fz_context *ctx, fz_buffer *buffer) +{ + PyObject *bytes = PyByteArray_FromObject(Py_BuildValue("s", "")); + char *c = NULL; + if (buffer) + { + size_t len = fz_buffer_storage(ctx, buffer, &c); + bytes = PyByteArray_FromStringAndSize(c, (Py_ssize_t) len); + } + return bytes; +} + +//---------------------------------------------------------------------------- +// Turn fz_buffer to a base64 encoded bytes object +//---------------------------------------------------------------------------- +PyObject *JM_B64FromBuffer(fz_context *ctx, fz_buffer *buffer) +{ + PyObject *bytes = PyBytes_FromString(""); + char *c = NULL; + char *b64 = NULL; + if (buffer) { - bytes = PyBytes_FromString(""); + size_t len = fz_buffer_storage(ctx, buffer, &c); + fz_buffer *res = fz_new_buffer(ctx, len); + fz_output *out = fz_new_output_with_buffer(ctx, res); + fz_write_base64(ctx, out, (const unsigned char *) c, (int) len, 0); + size_t nlen = fz_buffer_storage(ctx, res, &b64); + bytes = PyBytes_FromStringAndSize(b64, (Py_ssize_t) nlen); + fz_drop_buffer(ctx, res); + fz_drop_output(ctx, out); } return bytes; } @@ -182,7 +252,7 @@ fz_buffer *JM_deflatebuf(fz_context *ctx, unsigned char *p, size_t n) //---------------------------------------------------------------------------- // update a stream object -// compress stream where beneficial +// compress stream when beneficial //---------------------------------------------------------------------------- void JM_update_stream(fz_context *ctx, pdf_document *doc, pdf_obj *obj, fz_buffer *buffer) { @@ -198,7 +268,7 @@ void JM_update_stream(fz_context *ctx, pdf_document *doc, pdf_obj *obj, fz_buffe } if (nlen < len) // was it worth the effort? { - pdf_dict_put(ctx, obj, PDF_NAME_Filter, PDF_NAME_FlateDecode); + pdf_dict_put(ctx, obj, PDF_NAME(Filter), PDF_NAME(FlateDecode)); pdf_update_stream(ctx, doc, obj, nres, 1); } else @@ -213,21 +283,22 @@ void JM_update_stream(fz_context *ctx, pdf_document *doc, pdf_obj *obj, fz_buffe // of only the 'clip' part of the displaylist rectangle //----------------------------------------------------------------------------- fz_pixmap * -JM_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, const fz_matrix *ctm, fz_colorspace *cs, int alpha, const fz_rect *clip) +JM_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, PyObject *ctm, fz_colorspace *cs, int alpha, PyObject *clip) { - fz_rect rect; - fz_irect irect; + fz_rect rect = fz_bound_display_list(ctx, list); + fz_matrix matrix = JM_matrix_from_py(ctm); fz_pixmap *pix = NULL; fz_var(pix); fz_device *dev = NULL; fz_var(dev); fz_separations *seps = NULL; - fz_bound_display_list(ctx, list, &rect); - if (clip) fz_intersect_rect(&rect, clip); - fz_transform_rect(&rect, ctm); - fz_round_rect(&irect, &rect); + fz_rect rclip = JM_rect_from_py(clip); + rect = fz_intersect_rect(rect, rclip); // no-op if clip is not given + + rect = fz_transform_rect(rect, matrix); + fz_irect irect = fz_round_rect(rect); - pix = fz_new_pixmap_with_bbox(ctx, cs, &irect, seps, alpha); + pix = fz_new_pixmap_with_bbox(ctx, cs, irect, seps, alpha); if (alpha) fz_clear_pixmap(ctx, pix); else @@ -235,11 +306,17 @@ JM_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, const fz_mat fz_try(ctx) { - if (clip) - dev = fz_new_draw_device_with_bbox(ctx, ctm, pix, &irect); + if (!fz_is_infinite_rect(rclip)) + { + dev = fz_new_draw_device_with_bbox(ctx, matrix, pix, &irect); + fz_run_display_list(ctx, list, dev, fz_identity, rclip, NULL); + } else - dev = fz_new_draw_device(ctx, ctm, pix); - fz_run_display_list(ctx, list, dev, &fz_identity, clip, NULL); + { + dev = fz_new_draw_device(ctx, matrix, pix); + fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL); + } + fz_close_device(ctx, dev); } fz_always(ctx) @@ -295,6 +372,7 @@ size_t JM_CharFromBytesOrArray(PyObject *stream, char **data) //---------------------------------------------------------------------------- // Return fz_buffer from a PyBytes or PyByteArray object +// Attention: must be freed by caller! //---------------------------------------------------------------------------- fz_buffer *JM_BufferFromBytes(fz_context *ctx, PyObject *stream) { @@ -363,9 +441,9 @@ void page_merge(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, i pdf_obj *obj = NULL, *ref = NULL, *subt = NULL; // list of object types (per page) we want to copy - pdf_obj *known_page_objs[] = {PDF_NAME_Contents, PDF_NAME_Resources, - PDF_NAME_MediaBox, PDF_NAME_CropBox, PDF_NAME_BleedBox, PDF_NAME_Annots, - PDF_NAME_TrimBox, PDF_NAME_ArtBox, PDF_NAME_Rotate, PDF_NAME_UserUnit}; + pdf_obj *known_page_objs[] = {PDF_NAME(Contents), PDF_NAME(Resources), + PDF_NAME(MediaBox), PDF_NAME(CropBox), PDF_NAME(BleedBox), PDF_NAME(Annots), + PDF_NAME(TrimBox), PDF_NAME(ArtBox), PDF_NAME(Rotate), PDF_NAME(UserUnit)}; int n = nelem(known_page_objs); // number of list elements int i; int num, j; @@ -378,7 +456,7 @@ void page_merge(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, i pdf_flatten_inheritable_page_items(ctx, pageref); // make a new page page_dict = pdf_new_dict(ctx, doc_des, 4); - pdf_dict_put_drop(ctx, page_dict, PDF_NAME_Type, PDF_NAME_Page); + pdf_dict_put_drop(ctx, page_dict, PDF_NAME(Type), PDF_NAME(Page)); // copy objects of source page into it for (i = 0; i < n; i++) @@ -388,12 +466,12 @@ void page_merge(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, i pdf_dict_put_drop(ctx, page_dict, known_page_objs[i], pdf_graft_mapped_object(ctx, graft_map, obj)); } // remove any links from annots array - pdf_obj *annots = pdf_dict_get(ctx, page_dict, PDF_NAME_Annots); + pdf_obj *annots = pdf_dict_get(ctx, page_dict, PDF_NAME(Annots)); int len = pdf_array_len(ctx, annots); for (j = 0; j < len; j++) { pdf_obj *o = pdf_array_get(ctx, annots, j); - if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME_Subtype), PDF_NAME_Link)) + if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME(Subtype)), PDF_NAME(Link))) continue; // remove the link annotation pdf_array_delete(ctx, annots, j); @@ -403,8 +481,8 @@ void page_merge(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, i // rotate the page as requested if (rotate != -1) { - pdf_obj *rotateobj = pdf_new_int(ctx, doc_des, rotate); - pdf_dict_put_drop(ctx, page_dict, PDF_NAME_Rotate, rotateobj); + pdf_obj *rotateobj = pdf_new_int(ctx, (int64_t) rotate); + pdf_dict_put_drop(ctx, page_dict, PDF_NAME(Rotate), rotateobj); } // Now add the page dictionary to dest PDF obj = pdf_add_object_drop(ctx, doc_des, page_dict); @@ -462,49 +540,25 @@ void merge_range(fz_context *ctx, pdf_document *doc_des, pdf_document *doc_src, } //---------------------------------------------------------------------------- -// Fills table 'res' with outline xref numbers -// 'res' must be a correctly pre-allocated table of integers -// 'obj' must be the first OL item -// returns (int) number of filled-in outline item xref numbers. -//---------------------------------------------------------------------------- -int fillOLNumbers(fz_context *ctx, int *res, pdf_obj *obj, int oc, int argc) -{ - int onum; - pdf_obj *first, *parent, *thisobj; - if (!obj) return oc; - if (oc >= argc) return oc; - thisobj = obj; - while (thisobj) { - onum = pdf_to_num(ctx, thisobj); - res[oc] = onum; - oc += 1; - first = pdf_dict_get(ctx, thisobj, PDF_NAME_First); /* try go down */ - if (first) oc = fillOLNumbers(ctx, res, first, oc, argc); /* recurse */ - thisobj = pdf_dict_get(ctx, thisobj, PDF_NAME_Next); /* try go next */ - parent = pdf_dict_get(ctx, thisobj, PDF_NAME_Parent); /* get parent */ - if (!thisobj) thisobj = parent; /* goto parent if no next obj */ - } - return oc; -} - -//---------------------------------------------------------------------------- -// Returns number of outlines -// 'obj' must be first OL item +// Return list of outline xref numbers. Recursive function. Arguments: +// 'obj' first OL item +// 'xrefs' empty Python list //---------------------------------------------------------------------------- -int countOutlines(fz_context *ctx, pdf_obj *obj, int oc) +PyObject *JM_outline_xrefs(fz_context *ctx, pdf_obj *obj, PyObject *xrefs) { pdf_obj *first, *parent, *thisobj; - if (!obj) return oc; + if (!obj) return xrefs; thisobj = obj; - while (thisobj) { - oc += 1; - first = pdf_dict_get(ctx, thisobj, PDF_NAME_First); /* try go down */ - if (first) oc = countOutlines(ctx, first, oc); - thisobj = pdf_dict_get(ctx, thisobj, PDF_NAME_Next); /* try go next */ - parent = pdf_dict_get(ctx, thisobj, PDF_NAME_Parent); /* get parent */ + while (thisobj) + { + PyList_Append(xrefs, Py_BuildValue("i", pdf_to_num(ctx, thisobj))); + first = pdf_dict_get(ctx, thisobj, PDF_NAME(First)); // try go down + if (first) xrefs = JM_outline_xrefs(ctx, first, xrefs); + thisobj = pdf_dict_get(ctx, thisobj, PDF_NAME(Next)); // try go next + parent = pdf_dict_get(ctx, thisobj, PDF_NAME(Parent)); // get parent if (!thisobj) thisobj = parent; /* goto parent if no next exists */ } - return oc; + return xrefs; } //----------------------------------------------------------------------------- @@ -516,14 +570,14 @@ fz_buffer *fontbuffer(fz_context *ctx, pdf_document *doc, int xref) pdf_obj *o, *obj = NULL, *desft, *stream = NULL; char *ext = ""; o = pdf_load_object(ctx, doc, xref); - desft = pdf_dict_get(ctx, o, PDF_NAME_DescendantFonts); + desft = pdf_dict_get(ctx, o, PDF_NAME(DescendantFonts)); if (desft) { obj = pdf_resolve_indirect(ctx, pdf_array_get(ctx, desft, 0)); - obj = pdf_dict_get(ctx, obj, PDF_NAME_FontDescriptor); + obj = pdf_dict_get(ctx, obj, PDF_NAME(FontDescriptor)); } else - obj = pdf_dict_get(ctx, o, PDF_NAME_FontDescriptor); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontDescriptor)); if (!obj) { @@ -534,29 +588,29 @@ fz_buffer *fontbuffer(fz_context *ctx, pdf_document *doc, int xref) pdf_drop_obj(ctx, o); o = obj; - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile)); if (obj) stream = obj; // ext = "pfa" - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile2); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile2)); if (obj) stream = obj; // ext = "ttf" - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile3); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile3)); if (obj) { stream = obj; - obj = pdf_dict_get(ctx, obj, PDF_NAME_Subtype); + obj = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)); if (obj && !pdf_is_name(ctx, obj)) { PySys_WriteStdout("invalid font descriptor subtype"); return NULL; } - if (pdf_name_eq(ctx, obj, PDF_NAME_Type1C)) + if (pdf_name_eq(ctx, obj, PDF_NAME(Type1C))) ext = "cff"; - else if (pdf_name_eq(ctx, obj, PDF_NAME_CIDFontType0C)) + else if (pdf_name_eq(ctx, obj, PDF_NAME(CIDFontType0C))) ext = "cid"; - else if (pdf_name_eq(ctx, obj, PDF_NAME_OpenType)) + else if (pdf_name_eq(ctx, obj, PDF_NAME(OpenType))) ext = "otf"; else PySys_WriteStdout("warning: unhandled font type '%s'", pdf_to_name(ctx, obj)); @@ -579,40 +633,40 @@ char *fontextension(fz_context *ctx, pdf_document *doc, int xref) if (xref < 1) return "n/a"; pdf_obj *o, *obj = NULL, *desft; o = pdf_load_object(ctx, doc, xref); - desft = pdf_dict_get(ctx, o, PDF_NAME_DescendantFonts); + desft = pdf_dict_get(ctx, o, PDF_NAME(DescendantFonts)); if (desft) { obj = pdf_resolve_indirect(ctx, pdf_array_get(ctx, desft, 0)); - obj = pdf_dict_get(ctx, obj, PDF_NAME_FontDescriptor); + obj = pdf_dict_get(ctx, obj, PDF_NAME(FontDescriptor)); } else - obj = pdf_dict_get(ctx, o, PDF_NAME_FontDescriptor); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontDescriptor)); pdf_drop_obj(ctx, o); if (!obj) return "n/a"; // this is a base-14 font o = obj; // we have the FontDescriptor - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile)); if (obj) return "pfa"; - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile2); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile2)); if (obj) return "ttf"; - obj = pdf_dict_get(ctx, o, PDF_NAME_FontFile3); + obj = pdf_dict_get(ctx, o, PDF_NAME(FontFile3)); if (obj) { - obj = pdf_dict_get(ctx, obj, PDF_NAME_Subtype); + obj = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)); if (obj && !pdf_is_name(ctx, obj)) { PySys_WriteStdout("invalid font descriptor subtype"); return "n/a"; } - if (pdf_name_eq(ctx, obj, PDF_NAME_Type1C)) + if (pdf_name_eq(ctx, obj, PDF_NAME(Type1C))) return "cff"; - else if (pdf_name_eq(ctx, obj, PDF_NAME_CIDFontType0C)) + else if (pdf_name_eq(ctx, obj, PDF_NAME(CIDFontType0C))) return "cid"; - else if (pdf_name_eq(ctx, obj, PDF_NAME_OpenType)) + else if (pdf_name_eq(ctx, obj, PDF_NAME(OpenType))) return "otf"; else PySys_WriteStdout("unhandled font type '%s'", pdf_to_name(ctx, obj)); @@ -621,6 +675,33 @@ char *fontextension(fz_context *ctx, pdf_document *doc, int xref) return "n/a"; } +//----------------------------------------------------------------------------- +// create PDF object from given string (new in v1.14.0: MuPDF dropped it) +//----------------------------------------------------------------------------- +pdf_obj *JM_pdf_obj_from_str(fz_context *ctx, pdf_document *doc, char *src) +{ + pdf_obj *result = NULL; + pdf_lexbuf lexbuf; + fz_stream *stream = fz_open_memory(ctx, (unsigned char *)src, strlen(src)); + + pdf_lexbuf_init(ctx, &lexbuf, PDF_LEXBUF_SMALL); + + fz_try(ctx) + result = pdf_parse_stm_obj(ctx, doc, stream, &lexbuf); + + fz_always(ctx) + { + pdf_lexbuf_fin(ctx, &lexbuf); + fz_drop_stream(ctx, stream); + } + + fz_catch(ctx) + fz_rethrow(ctx); + + return result; + +} + //----------------------------------------------------------------------------- // dummy structure for various tools and utilities //----------------------------------------------------------------------------- @@ -660,4 +741,38 @@ struct fz_store_s int needs_reaping; }; +//----------------------------------------------------------------------------- +// START redirect stdout/stderr +//----------------------------------------------------------------------------- +PyObject *JM_error_log; +PyObject *JM_output_log; + +static void +JM_write_stdout(fz_context *ctx, void *opaque, const void *buffer, size_t count) +{ + if (!buffer || !count) return; + PyObject *c = Py_BuildValue("s#", (const char *) buffer, (Py_ssize_t) count); + if (!c || c == NONE) return; + PyList_Append(JM_output_log, c); + Py_CLEAR(c); + return; +} + +static void +JM_write_stderr(fz_context *ctx, void *opaque, const void *buffer, size_t count) +{ + if (!buffer || !count) return; + PyObject *c = Py_BuildValue("s#", (const char *) buffer, (Py_ssize_t) count); + if (!c || c == NONE) return; + PyList_Append(JM_error_log, c); + Py_CLEAR(c); + return; +} + +fz_output *JM_fitz_stdout; +fz_output *JM_fitz_stderr; +//----------------------------------------------------------------------------- +// STOP redirect stdout/stderr +//----------------------------------------------------------------------------- + %} \ No newline at end of file diff --git a/fitz/helper-pdfinfo.i b/fitz/helper-pdfinfo.i index 6de541396..f99cc34ab 100644 --- a/fitz/helper-pdfinfo.i +++ b/fitz/helper-pdfinfo.i @@ -58,15 +58,15 @@ void JM_gather_fonts(fz_context *ctx, pdf_document *pdf, pdf_obj *dict, continue; } refname = pdf_dict_get_key(ctx, dict, i); - subtype = pdf_dict_get(ctx, fontdict, PDF_NAME_Subtype); - basefont = pdf_dict_get(ctx, fontdict, PDF_NAME_BaseFont); + subtype = pdf_dict_get(ctx, fontdict, PDF_NAME(Subtype)); + basefont = pdf_dict_get(ctx, fontdict, PDF_NAME(BaseFont)); if (!basefont || pdf_is_null(ctx, basefont)) - name = pdf_dict_get(ctx, fontdict, PDF_NAME_Name); + name = pdf_dict_get(ctx, fontdict, PDF_NAME(Name)); else name = basefont; - encoding = pdf_dict_get(ctx, fontdict, PDF_NAME_Encoding); + encoding = pdf_dict_get(ctx, fontdict, PDF_NAME(Encoding)); if (pdf_is_dict(ctx, encoding)) - encoding = pdf_dict_get(ctx, encoding, PDF_NAME_BaseEncoding); + encoding = pdf_dict_get(ctx, encoding, PDF_NAME(BaseEncoding)); int xref = pdf_to_num(ctx, fontdict); char *ext = "n/a"; if (xref) ext = fontextension(ctx, pdf, xref); @@ -111,25 +111,25 @@ void JM_gather_images(fz_context *ctx, pdf_document *doc, pdf_obj *dict, } refname = pdf_dict_get_key(ctx, dict, i); - type = pdf_dict_get(ctx, imagedict, PDF_NAME_Subtype); - if (!pdf_name_eq(ctx, type, PDF_NAME_Image)) + type = pdf_dict_get(ctx, imagedict, PDF_NAME(Subtype)); + if (!pdf_name_eq(ctx, type, PDF_NAME(Image))) continue; int xref = pdf_to_num(ctx, imagedict); int gen = 0; - smask = pdf_dict_get(ctx, imagedict, PDF_NAME_SMask); + smask = pdf_dict_get(ctx, imagedict, PDF_NAME(SMask)); if (smask) gen = pdf_to_num(ctx, smask); - filter = pdf_dict_get(ctx, imagedict, PDF_NAME_Filter); + filter = pdf_dict_get(ctx, imagedict, PDF_NAME(Filter)); altcs = NULL; - cs = pdf_dict_get(ctx, imagedict, PDF_NAME_ColorSpace); + cs = pdf_dict_get(ctx, imagedict, PDF_NAME(ColorSpace)); if (pdf_is_array(ctx, cs)) { pdf_obj *cses = cs; cs = pdf_array_get(ctx, cses, 0); - if (pdf_name_eq(ctx, cs, PDF_NAME_DeviceN) || - pdf_name_eq(ctx, cs, PDF_NAME_Separation)) + if (pdf_name_eq(ctx, cs, PDF_NAME(DeviceN)) || + pdf_name_eq(ctx, cs, PDF_NAME(Separation))) { altcs = pdf_array_get(ctx, cses, 2); if (pdf_is_array(ctx, altcs)) @@ -137,9 +137,9 @@ void JM_gather_images(fz_context *ctx, pdf_document *doc, pdf_obj *dict, } } - width = pdf_dict_get(ctx, imagedict, PDF_NAME_Width); - height = pdf_dict_get(ctx, imagedict, PDF_NAME_Height); - bpc = pdf_dict_get(ctx, imagedict, PDF_NAME_BitsPerComponent); + width = pdf_dict_get(ctx, imagedict, PDF_NAME(Width)); + height = pdf_dict_get(ctx, imagedict, PDF_NAME(Height)); + bpc = pdf_dict_get(ctx, imagedict, PDF_NAME(BitsPerComponent)); PyObject *entry = PyList_New(0); PyList_Append(entry, Py_BuildValue("i", xref)); @@ -179,8 +179,8 @@ void JM_gather_forms(fz_context *ctx, pdf_document *doc, pdf_obj *dict, } refname = pdf_dict_get_key(ctx, dict, i); - type = pdf_dict_get(ctx, imagedict, PDF_NAME_Subtype); - if (!pdf_name_eq(ctx, type, PDF_NAME_Form)) + type = pdf_dict_get(ctx, imagedict, PDF_NAME(Subtype)); + if (!pdf_name_eq(ctx, type, PDF_NAME(Form))) continue; int xref = pdf_to_num(ctx, imagedict); @@ -206,19 +206,19 @@ void JM_scan_resources(fz_context *ctx, pdf_document *pdf, pdf_obj *rsrc, { if (what == 1) // look up fonts { - font = pdf_dict_get(ctx, rsrc, PDF_NAME_Font); + font = pdf_dict_get(ctx, rsrc, PDF_NAME(Font)); JM_gather_fonts(ctx, pdf, font, liste); n = pdf_dict_len(ctx, font); for (i = 0; i < n; i++) { pdf_obj *obj = pdf_dict_get_val(ctx, font, i); - subrsrc = pdf_dict_get(ctx, obj, PDF_NAME_Resources); + subrsrc = pdf_dict_get(ctx, obj, PDF_NAME(Resources)); if (subrsrc) JM_scan_resources(ctx, pdf, subrsrc, liste, what); } } - xobj = pdf_dict_get(ctx, rsrc, PDF_NAME_XObject); + xobj = pdf_dict_get(ctx, rsrc, PDF_NAME(XObject)); if (what == 2) // look up images { @@ -234,7 +234,7 @@ void JM_scan_resources(fz_context *ctx, pdf_document *pdf, pdf_obj *rsrc, for (i = 0; i < n; i++) { pdf_obj *obj = pdf_dict_get_val(ctx, xobj, i); - subrsrc = pdf_dict_get(ctx, obj, PDF_NAME_Resources); + subrsrc = pdf_dict_get(ctx, obj, PDF_NAME(Resources)); if (subrsrc) JM_scan_resources(ctx, pdf, subrsrc, liste, what); } diff --git a/fitz/helper-portfolio.i b/fitz/helper-portfolio.i index d3348018d..26b225b83 100644 --- a/fitz/helper-portfolio.i +++ b/fitz/helper-portfolio.i @@ -34,7 +34,7 @@ int JM_find_embedded(fz_context *ctx, PyObject *id, pdf_document *pdf) if (!name || strlen(name) == 0) return -1; for (i = 0; i < count; i++) { - tname = pdf_to_utf8(ctx, pdf_portfolio_entry_name(ctx, pdf, i)); + tname = (char *) pdf_to_text_string(ctx, pdf_portfolio_entry_name(ctx, pdf, i)); if (!strcmp(tname, name)) { JM_Python_str_DelForPy3(name); @@ -52,14 +52,14 @@ pdf_obj *JM_embedded_names(fz_context *ctx, pdf_document *pdf) { pdf_obj *names = NULL, *kids = NULL, *o = NULL; int i, n; - names = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), PDF_NAME_Root, PDF_NAME_Names, PDF_NAME_EmbeddedFiles, - PDF_NAME_Names, NULL); + names = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), PDF_NAME(Root), PDF_NAME(Names), PDF_NAME(EmbeddedFiles), + PDF_NAME(Names), NULL); if (names) return names; // not found, therefore a /Kids object contains the /Names - kids = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), PDF_NAME_Root, - PDF_NAME_Names, PDF_NAME_EmbeddedFiles, - PDF_NAME_Kids, NULL); + kids = pdf_dict_getl(ctx, pdf_trailer(ctx, pdf), PDF_NAME(Root), + PDF_NAME(Names), PDF_NAME(EmbeddedFiles), + PDF_NAME(Kids), NULL); //------------------------------------------------------------------------- // 'kids' is an array of indirect references pointing to dictionaries. // Only /Limits and /Names can occur in those dictionaries @@ -71,7 +71,7 @@ pdf_obj *JM_embedded_names(fz_context *ctx, pdf_document *pdf) for (i = 0; i < n; i++) { o = pdf_resolve_indirect(ctx, pdf_array_get(ctx, kids, i)); - names = pdf_dict_get(ctx, o, PDF_NAME_Names); + names = pdf_dict_get(ctx, o, PDF_NAME(Names)); if (names) return names; } return NULL; // should never execute @@ -85,23 +85,23 @@ pdf_obj *JM_embedded_names(fz_context *ctx, pdf_document *pdf) //----------------------------------------------------------------------------- void JM_embedded_clean(fz_context *ctx, pdf_document *pdf) { - pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, pdf), PDF_NAME_Root); + pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, pdf), PDF_NAME(Root)); // remove any empty /Collection entry - pdf_obj *coll = pdf_dict_get(ctx, root, PDF_NAME_Collection); + pdf_obj *coll = pdf_dict_get(ctx, root, PDF_NAME(Collection)); if (coll && pdf_dict_len(ctx, coll) == 0) - pdf_dict_del(ctx, root, PDF_NAME_Collection); + pdf_dict_del(ctx, root, PDF_NAME(Collection)); if (!pdf_count_portfolio_entries(ctx, pdf)) return; - pdf_obj *efiles = pdf_dict_getl(ctx, root, PDF_NAME_Names, - PDF_NAME_EmbeddedFiles, NULL); + pdf_obj *efiles = pdf_dict_getl(ctx, root, PDF_NAME(Names), + PDF_NAME(EmbeddedFiles), NULL); if (efiles) // we have embedded files { // make sure they are displayed - pdf_dict_put_name(ctx, root, PDF_NAME_PageMode, "UseAttachments"); + pdf_dict_put_name(ctx, root, PDF_NAME(PageMode), "UseAttachments"); // remove the limits entry: seems to be a MuPDF bug - pdf_dict_del(ctx, efiles, PDF_NAME_Limits); + pdf_dict_del(ctx, efiles, PDF_NAME(Limits)); } return; @@ -122,24 +122,24 @@ pdf_obj *JM_embed_file(fz_context *ctx, pdf_document *pdf, fz_buffer *buf, fz_try(ctx) { val = pdf_new_dict(ctx, pdf, 6); - pdf_dict_put_dict(ctx, val, PDF_NAME_CI, 4); - ef = pdf_dict_put_dict(ctx, val, PDF_NAME_EF, 4); - pdf_dict_put_text_string(ctx, val, PDF_NAME_F, filename); - pdf_dict_put_text_string(ctx, val, PDF_NAME_UF, filename); - pdf_dict_put_text_string(ctx, val, PDF_NAME_Desc, desc); - pdf_dict_put(ctx, val, PDF_NAME_Type, PDF_NAME_Filespec); + pdf_dict_put_dict(ctx, val, PDF_NAME(CI), 4); + ef = pdf_dict_put_dict(ctx, val, PDF_NAME(EF), 4); + pdf_dict_put_text_string(ctx, val, PDF_NAME(F), filename); + pdf_dict_put_text_string(ctx, val, PDF_NAME(UF), filename); + pdf_dict_put_text_string(ctx, val, PDF_NAME(Desc), desc); + pdf_dict_put(ctx, val, PDF_NAME(Type), PDF_NAME(Filespec)); tbuf = fz_new_buffer(ctx, strlen(filename)+1); fz_append_string(ctx, tbuf, filename); fz_terminate_buffer(ctx, tbuf); - pdf_dict_put_drop(ctx, ef, PDF_NAME_F, + pdf_dict_put_drop(ctx, ef, PDF_NAME(F), (f = pdf_add_stream(ctx, pdf, tbuf, NULL, 0))); fz_drop_buffer(ctx, tbuf); JM_update_stream(ctx, pdf, f, buf); len = fz_buffer_storage(ctx, buf, NULL); - pdf_dict_put_int(ctx, f, PDF_NAME_DL, len); - pdf_dict_put_int(ctx, f, PDF_NAME_Length, len); - params = pdf_dict_put_dict(ctx, f, PDF_NAME_Params, 4); - pdf_dict_put_int(ctx, params, PDF_NAME_Size, len); + pdf_dict_put_int(ctx, f, PDF_NAME(DL), len); + pdf_dict_put_int(ctx, f, PDF_NAME(Length), len); + params = pdf_dict_put_dict(ctx, f, PDF_NAME(Params), 4); + pdf_dict_put_int(ctx, params, PDF_NAME(Size), len); } fz_always(ctx) { diff --git a/fitz/helper-python.i b/fitz/helper-python.i index fb4ab0ed2..58f3d2979 100644 --- a/fitz/helper-python.i +++ b/fitz/helper-python.i @@ -34,6 +34,24 @@ TEXT_PRESERVE_LIGATURES = 1 TEXT_PRESERVE_WHITESPACE = 2 TEXT_PRESERVE_IMAGES = 4 +#------------------------------------------------------------------------------ +# Stamp annotation icon numbers +#------------------------------------------------------------------------------ +STAMP_Approved = 0 +STAMP_AsIs = 1 +STAMP_Confidential = 2 +STAMP_Departmental = 3 +STAMP_Experimental = 4 +STAMP_Expired = 5 +STAMP_Final = 6 +STAMP_ForComment = 7 +STAMP_ForPublicRelease = 8 +STAMP_NotApproved = 9 +STAMP_NotForPublicRelease = 10 +STAMP_Sold = 11 +STAMP_TopSecret = 12 +STAMP_Draft = 13 + #------------------------------------------------------------------------------ # Base 14 font names #------------------------------------------------------------------------------ @@ -48,7 +66,7 @@ Base14_fontnames = ("Courier", "Courier-Oblique", "Courier-Bold", # Emulate old linkDest class #------------------------------------------------------------------------------ class linkDest(): - '''link or outline destination details''' + """link or outline destination details""" def __init__(self, obj, rlink): isExt = obj.isExternal isInt = not isExt @@ -263,18 +281,18 @@ def CheckParent(o): def CheckColor(c): if c is not None: - if type(c) not in (list, tuple) or len(c) != 3 or \ + if type(c) not in (list, tuple) or len(c) not in (1, 3, 4) or \ min(c) < 0 or max(c) > 1: - raise ValueError("need 3 color components in range 0 to 1") + raise ValueError("need 1, 3 or 4 color components in range 0 to 1") def CheckMorph(o): if not bool(o): return False if not (type(o) in (list, tuple) and len(o) == 2): raise ValueError("morph must be a sequence of length 2") if not (type(o[0]) == Point and issubclass(type(o[1]), Matrix)): - raise ValueError("invalid morph parameter") + raise ValueError("invalid morph parm 0") if not o[1].e == o[1].f == 0: - raise ValueError("invalid morph parameter") + raise ValueError("invalid morph parm 1") return True def CheckFont(page, fontname): @@ -377,386 +395,4 @@ def ConversionTrailer(i): return r -#------------------------------------------------------------------------------ -# -#------------------------------------------------------------------------------ -def _hor_matrix(C, P): - """Given two points C, P calculate matrix that rotates the line C -> P parallel to the x-axis. - """ - S = P - C # vector C -> P - try: - alfa = math.asin(abs(S.y) / abs(S)) # absolute angle from horizontal - except ZeroDivisionError: - print("points are too close:") - return Matrix() - if S.x < 0: # make arcsin result unique - if S.y <= 0: # bottom-left - alfa = -(math.pi - alfa) - else: # top-left - alfa = math.pi - alfa - else: - if S.y >= 0: # top-right - pass - else: # bottom-right - alfa = - alfa - ca = math.cos(alfa) - sa = math.sin(alfa) - m = Matrix(ca, -sa, sa, ca, -C.x, -C.y) - return m - -def _make_rect_AP(annot): - """ Create /AP stream for rectangle annotation. - """ - w = annot.border["width"] # get line width - sc = annot.colors["stroke"] # get stroke color - fc = annot.colors["fill"] # get fill color - ca = annot.opacity # get opacity value - Alp0 = "/Alp0 gs\n" if ca >= 0 else "" - scol = "%g %g %g RG " % (sc[0], sc[1], sc[2]) if sc else "0 0 0 RG " - fcol = "%g %g %g rg " % (fc[0], fc[1], fc[2]) if fc else "" - dt = annot.border.get("dashes") # get annot dashes - dtab = [] - if dt: - dtab = ["[", "]0 d"] - for n in dt: - dtab[0] += "%i " % n - dtab = "".join(dtab) # make dashes command - r = annot.rect # annot rectangle - r1 = r2 = w/2. # rect starts bottom-left here - r3 = r.width - w # rect width reduced by line width - r4 = r.height - w # rect height reduced by line with - ap = "q\n%s%g %g %g %g re %g w 1 J 1 j\n" % (Alp0, r1, r2, r3, r4, w) - ap += scol + fcol + dtab # appearance stream so far - if fcol: # have fill color? - ap += "\nb\nQ\n" - else: - ap += "\ns\nQ\n" - return ap - -def _le_annot_parms(annot, p1, p2): - """Get common parameters for making line end symbols. - """ - w = annot.border["width"] # line width - sc = annot.colors["stroke"] # stroke color - scol = "%g %g %g RG\n" % (sc[0], sc[1], sc[2]) if sc else "0 0 0 RG\n" - fc = annot.colors["fill"] # fill color - fcol = "%g %g %g rg\n" % (fc[0], fc[1], fc[2]) if fc else "1 1 1 rg\n" - delta = Point(annot.rect.x0, annot.rect.y0) - nr = annot.rect - Rect(delta, delta) - h = nr.height - np1 = p1 - delta # point coord relative to annot rect - np2 = p2 - delta # point coord relative to annot rect - m = _hor_matrix(np1, np2) # matrix makes the line horizontal - im = ~m # inverted matrix - L = np1 * m # converted start (left) point - R = np2 * m # converted end (right) point - return m, im, L, R, w, h, scol, fcol - -def _make_circle_AP(annot): - """Create /AP stream for circle annotation - """ - sc = annot.colors["stroke"] # stroke color - scol = "%g %g %g RG " % (sc[0], sc[1], sc[2]) if sc else "0 0 0 RG " - fc = annot.colors["fill"] # fill color - ca = annot.opacity # get opacity value - Alp0 = "/Alp0 gs\n" if ca >= 0 else "" - fcol = "%g %g %g rg " % (fc[0], fc[1], fc[2]) if fc else "" - dt = annot.border.get("dashes") # get annot dashes - dtab = [] - if dt: - dtab = ["[", "]0 d\n"] - for n in dt: - dtab[0] += "%i " % n - dtab = "".join(dtab) # the dashes command - lw = annot.border["width"] # line width - lw2 = lw / 2. - h = annot.rect.height - r = Rect(lw2, lw2, annot.rect.width - lw2, h - lw2) - - ap = "q\n" + Alp0 + _oval_string(h, r.tl, r.tr, r.br, r.bl) - ap += "%g w 1 J 1 j\n" % lw - ap += scol + fcol + dtab # ap stream so far - if fcol: # have fill color? - ap += "\nb\nQ\n" - else: - ap += "\ns\nQ\n" - return ap - -def _oval_string(h, p1, p2, p3, p4): - """Return /AP string defining an oval within a 4-polygon provided as points - """ - def bezier(p, q, r): - f = "%g %g %g %g %g %g c\n" - return f % (p.x, h - p.y, q.x, h - q.y, r.x, h - r.y) - - kappa = 0.55228474983 # magic number - ml = p1 + (p4 - p1) * 0.5 # middle points ... - mo = p1 + (p2 - p1) * 0.5 # for each ... - mr = p2 + (p3 - p2) * 0.5 # polygon ... - mu = p4 + (p3 - p4) * 0.5 # edge - ol1 = ml + (p1 - ml) * kappa # the 8 bezier - ol2 = mo + (p1 - mo) * kappa # helper points - or1 = mo + (p2 - mo) * kappa - or2 = mr + (p2 - mr) * kappa - ur1 = mr + (p3 - mr) * kappa - ur2 = mu + (p3 - mu) * kappa - ul1 = mu + (p4 - mu) * kappa - ul2 = ml + (p4 - ml) * kappa - # now draw, starting from middle point of left edge - ap = "%g %g m\n" % (ml.x, h - ml.y) - ap += bezier(ol1, ol2, mo) - ap += bezier(or1, or2, mr) - ap += bezier(ur1, ur2, mu) - ap += bezier(ul1, ul2, ml) - return ap - -def _le_diamond(annot, p1, p2, lr): - """Make stream commands for diamond line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - shift = 1.75 # 2*shift*width = length of square edge - d = w * shift - M = R - (w, 0) if lr else L + (w, 0) - r = Rect(M, M) + (-d, -d, d, d) # the square - # the square makes line longer by (2*shift - 1)*width - p = (r.tl + (r.bl - r.tl) * 0.5) * im - ap = "q\n%g %g m\n" % (p.x, h - p.y) - p = (r.tl + (r.tr - r.tl) * 0.5) * im - ap += "%g %g l\n" % (p.x, h - p.y) - p = (r.tr + (r.br - r.tr) * 0.5) * im - ap += "%g %g l\n" % (p.x, h - p.y) - p = (r.br + (r.bl - r.br) * 0.5) * im - ap += "%g %g l\n" % (p.x, h - p.y) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _le_square(annot, p1, p2, lr): - """Make stream commands for square line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - shift = 1.25 # 2*shift*width = length of square edge - d = w * shift - M = R - (w, 0) if lr else L + (w, 0) - r = Rect(M, M) + (-d, -d, d, d) # the square - # the square makes line longer by (2*shift - 1)*width - p = r.tl * im - ap = "q\n%g %g m\n" % (p.x, h - p.y) - p = r.tr * im - ap += "%g %g l\n" % (p.x, h - p.y) - p = r.br * im - ap += "%g %g l\n" % (p.x, h - p.y) - p = r.bl * im - ap += "%g %g l\n" % (p.x, h - p.y) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _le_circle(annot, p1, p2, lr): - """Make stream commands for circle line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - shift = 1.50 # 2*shift*width = length of square edge - d = w * shift - M = R - (w, 0) if lr else L + (w, 0) - r = Rect(M, M) + (-d, -d, d, d) # the square - ap = "q\n" + _oval_string(h, r.tl * im, r.tr * im, r.br * im, r.bl * im) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _le_butt(annot, p1, p2, lr): - """Make stream commands for butt line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - M = R if lr else L - top = (M + (0, -2 * w)) * im - bot = (M + (0, 2 * w)) * im - ap = "\nq\n%g %g m\n" % (top.x, h - top.y) - ap += "%g %g l\n" % (bot.x, h - bot.y) - ap += "%g w\n" % w - ap += scol + "s\nQ\n" - return ap - -def _le_slash(annot, p1, p2, lr): - """Make stream commands for slash line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - rw = 1.1547 * w * 0.5 # makes rect diagonal a 30 deg inclination - M = R if lr else L - r = Rect(M.x - rw, M.y - 2 * w, M.x + rw, M.y + 2 * w) - top = r.tl * im - bot = r.br * im - ap = "\nq\n%g %g m\n" % (top.x, h - top.y) - ap += "%g %g l\n" % (bot.x, h - bot.y) - ap += "%g w\n" % w - ap += scol + "s\nQ\n" - return ap - -def _le_openarrow(annot, p1, p2, lr): - """Make stream commands for open arrow line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - p2 = R + (1.5 * w, 0) if lr else L - (1.5 * w, 0) - p1 = p2 + (-3 * w, -1.5 * w) if lr else p2 + (3 * w, -1.5 * w) - p3 = p2 + (-3 * w, 1.5 * w) if lr else p2 + (3 * w, 1.5 * w) - p1 *= im - p2 *= im - p3 *= im - ap = "\nq\n%g %g m\n" % (p1.x, h - p1.y) - ap += "%g %g l\n" % (p2.x, h - p2.y) - ap += "%g %g l\n" % (p3.x, h - p3.y) - ap += "%g w\n" % w - ap += scol + "S\nQ\n" - return ap - -def _le_closedarrow(annot, p1, p2, lr): - """Make stream commands for closed arrow line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - p2 = R + (1.5 * w, 0) if lr else L - (1.5 * w, 0) - p1 = p2 + (-3 * w, -1.5 * w) if lr else p2 + (3 * w, -1.5 * w) - p3 = p2 + (-3 * w, 1.5 * w) if lr else p2 + (3 * w, 1.5 * w) - p1 *= im - p2 *= im - p3 *= im - ap = "\nq\n%g %g m\n" % (p1.x, h - p1.y) - ap += "%g %g l\n" % (p2.x, h - p2.y) - ap += "%g %g l\n" % (p3.x, h - p3.y) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _le_ropenarrow(annot, p1, p2, lr): - """Make stream commands for right open arrow line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - p2 = R - (0.5 * w, 0) if lr else L + (0.5 * w, 0) - p1 = p2 + (3 * w, -1.5 * w) if lr else p2 + (-3 * w, -1.5 * w) - p3 = p2 + (3 * w, 1.5 * w) if lr else p2 + (-3 * w, 1.5 * w) - p1 *= im - p2 *= im - p3 *= im - ap = "\nq\n%g %g m\n" % (p1.x, h - p1.y) - ap += "%g %g l\n" % (p2.x, h - p2.y) - ap += "%g %g l\n" % (p3.x, h - p3.y) - ap += "%g w\n" % w - ap += scol + fcol + "S\nQ\n" - return ap - -def _le_rclosedarrow(annot, p1, p2, lr): - """Make stream commands for right closed arrow line end symbol. "lr" denotes left (False) or right point. - """ - m, im, L, R, w, h, scol, fcol = _le_annot_parms(annot, p1, p2) - p2 = R - (3.0 * w, 0) if lr else L + (3.0 * w, 0) - p1 = p2 + (3 * w, -1.5 * w) if lr else p2 + (-3 * w, -1.5 * w) - p3 = p2 + (3 * w, 1.5 * w) if lr else p2 + (-3 * w, 1.5 * w) - p1 *= im - p2 *= im - p3 *= im - ap = "\nq\n%g %g m\n" % (p1.x, h - p1.y) - ap += "%g %g l\n" % (p2.x, h - p2.y) - ap += "%g %g l\n" % (p3.x, h - p3.y) - ap += "%g w\n" % w - ap += scol + fcol + "b\nQ\n" - return ap - -def _make_line_AP(annot, nv = None, r0 = None): - """ Create the /AP stream for 'Line', 'PolyLine' and 'Polygon' annotations. - """ - w = annot.border["width"] # get line width - sc = annot.colors["stroke"] # get stroke color - fc = annot.colors["fill"] # get fill color - ca = annot.opacity # get opacity value - Alp0 = "/Alp0 gs\n" if ca >= 0 else "" - vert = nv if nv else annot.vertices # get list of points - rn = r0 if r0 else annot.rect - h = rn.height # annot rectangle height - r = Rect(0, 0, rn.width, h) # this is the /BBox of the /AP - x0 = rn.x0 # annot rect origin x - y0 = rn.y0 # annot rect origin y - scol = "%g %g %g RG\n" % (sc[0], sc[1], sc[2]) if sc else "0 0 0 RG\n" - fcol = "%g %g %g rg\n" % (fc[0], fc[1], fc[2]) if fc else "" - - dt = annot.border.get("dashes") # get annot dashes - dtab = [] - if dt: - dtab = ["[", "]0 d\n"] - for n in dt: - dtab[0] += "%i " % n - dtab = "".join(dtab) # dashes command - - # start /AP string with a goto command - ap = "q\n%s%g %g m\n" % (Alp0, vert[0][0] - x0, h - (vert[0][1] - y0)) - - # add line commands for all subsequent points - for v in vert[1:]: - ap += "%g %g l\n" % (v[0] - x0, h - (v[1] - y0)) - - # add color triples and other stuff commands - ap += scol + fcol + dtab + "%g w 1 J 1 j\n" % w - - # add stroke / fill & stroke command depending on type - if fcol and annot.type[0] == ANNOT_POLYGON: - ap += "b" - else: - ap += "S" - ap += "\nQ\n" - - # function names for creating line end symbols - _le_func = (None, _le_square, _le_circle, _le_diamond, _le_openarrow, - _le_closedarrow, _le_butt, _le_ropenarrow, - _le_rclosedarrow, _le_slash) - valid_range = range(1, len(_le_func)) - - le_left, le_right = annot.lineEnds # get line end symbol numbers - - if le_left in valid_range: # uses points 1 and 2 - func = _le_func[le_left] # function for left symbol - ap += func(annot, Point(vert[0]), Point(vert[1]), False) - - if le_right in valid_range: # uses last and second to last points - func = _le_func[le_right] # function for right symbol - ap += func(annot, Point(vert[-2]), Point(vert[-1]), True) - - return ap - -def _upd_my_AP(annot): - """Update /AP stream for annotation types we are handling. - """ - if annot.type[0] not in range(2, 8): # not our business - return - - # this is the /AP rect for circle or square - r = Rect(0, 0, annot.rect.width, annot.rect.height) - - if annot.type[0] == ANNOT_CIRCLE: - ap = _make_circle_AP(annot) # stream for circle annot - annot._checkAP(r, ap) - return - - if annot.type[0] == ANNOT_SQUARE: - ap = _make_rect_AP(annot) # stream for square annot - annot._checkAP(r, ap) - return - - #-------------------------------------------------------------------------- - # we have one of the line annotations - #-------------------------------------------------------------------------- - - # first calculate the rect that contains all the points, including small - # extra rects around first and last point for any line symbols. - w = 3 * annot.border["width"] # 3 times the line width - ov = annot.vertices # get all the points - rect = Rect(Point(ov[0]), Point(ov[0])) + (-w, -w, w, w) - for v in ov[1:-1]: # include remaining points - rect |= v - rect |= Rect(Point(ov[-1]), Point(ov[-1])) + (-w, -w, w, w) - - annot._setRect(rect) # set this as annot rect - r = Rect(0, 0, rect.width, rect.height) # derive the /AP rect - - ap = _make_line_AP(annot) # make the /AP stream - annot._checkAP(r, ap) # create or update /AP object - return %} \ No newline at end of file diff --git a/fitz/helper-select.i b/fitz/helper-select.i index a446bc770..3c01bfeb5 100644 --- a/fitz/helper-select.i +++ b/fitz/helper-select.i @@ -33,7 +33,7 @@ void retainpage(fz_context *ctx, pdf_document *doc, pdf_obj *parent, pdf_obj *ki pdf_flatten_inheritable_page_items(ctx, pageref); - pdf_dict_put(ctx, pageref, PDF_NAME_Parent, parent); + pdf_dict_put(ctx, pageref, PDF_NAME(Parent), parent); /* Store page object in new kids array */ pdf_array_push(ctx, kids, pageref); @@ -58,12 +58,12 @@ int dest_is_valid(fz_context *ctx, pdf_obj *o, int page_count, int *page_object_ { pdf_obj *p; - p = pdf_dict_get(ctx, o, PDF_NAME_A); - if (pdf_name_eq(ctx, pdf_dict_get(ctx, p, PDF_NAME_S), PDF_NAME_GoTo) && - !string_in_names_list(ctx, pdf_dict_get(ctx, p, PDF_NAME_D), names_list)) + p = pdf_dict_get(ctx, o, PDF_NAME(A)); + if (pdf_name_eq(ctx, pdf_dict_get(ctx, p, PDF_NAME(S)), PDF_NAME(GoTo)) && + !string_in_names_list(ctx, pdf_dict_get(ctx, p, PDF_NAME(D)), names_list)) return 0; - p = pdf_dict_get(ctx, o, PDF_NAME_Dest); + p = pdf_dict_get(ctx, o, PDF_NAME(Dest)); if (p == NULL) {} else if (pdf_is_string(ctx, p)) @@ -103,30 +103,30 @@ int strip_outline(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int pag // Outline with invalid dest and no children. Drop it by // pulling the next one in here. /*************************************************************/ - pdf_obj *next = pdf_dict_get(ctx, current, PDF_NAME_Next); + pdf_obj *next = pdf_dict_get(ctx, current, PDF_NAME(Next)); if (next == NULL) { // There is no next one to pull in if (prev != NULL) - pdf_dict_del(ctx, prev, PDF_NAME_Next); + pdf_dict_del(ctx, prev, PDF_NAME(Next)); } else if (prev != NULL) { - pdf_dict_put(ctx, prev, PDF_NAME_Next, next); - pdf_dict_put(ctx, next, PDF_NAME_Prev, prev); + pdf_dict_put(ctx, prev, PDF_NAME(Next), next); + pdf_dict_put(ctx, next, PDF_NAME(Prev), prev); } else { - pdf_dict_del(ctx, next, PDF_NAME_Prev); + pdf_dict_del(ctx, next, PDF_NAME(Prev)); } current = next; } else { // Outline with invalid dest, but children. Just drop the dest. - pdf_dict_del(ctx, current, PDF_NAME_Dest); - pdf_dict_del(ctx, current, PDF_NAME_A); - current = pdf_dict_get(ctx, current, PDF_NAME_Next); + pdf_dict_del(ctx, current, PDF_NAME(Dest)); + pdf_dict_del(ctx, current, PDF_NAME(A)); + current = pdf_dict_get(ctx, current, PDF_NAME(Next)); } } else @@ -135,7 +135,7 @@ int strip_outline(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int pag if (first == NULL) first = current; prev = current; - current = pdf_dict_get(ctx, current, PDF_NAME_Next); + current = pdf_dict_get(ctx, current, PDF_NAME(Next)); count++; } } @@ -155,7 +155,7 @@ int strip_outlines(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int pa if (outlines == NULL) return 0; - first = pdf_dict_get(ctx, outlines, PDF_NAME_First); + first = pdf_dict_get(ctx, outlines, PDF_NAME(First)); if (first == NULL) nc = 0; else @@ -164,16 +164,16 @@ int strip_outlines(fz_context *ctx, pdf_document *doc, pdf_obj *outlines, int pa if (nc == 0) { - pdf_dict_del(ctx, outlines, PDF_NAME_First); - pdf_dict_del(ctx, outlines, PDF_NAME_Last); - pdf_dict_del(ctx, outlines, PDF_NAME_Count); + pdf_dict_del(ctx, outlines, PDF_NAME(First)); + pdf_dict_del(ctx, outlines, PDF_NAME(Last)); + pdf_dict_del(ctx, outlines, PDF_NAME(Count)); } else { - int old_count = pdf_to_int(ctx, pdf_dict_get(ctx, outlines, PDF_NAME_Count)); - pdf_dict_put(ctx, outlines, PDF_NAME_First, first); - pdf_dict_put(ctx, outlines, PDF_NAME_Last, last); - pdf_dict_put_drop(ctx, outlines, PDF_NAME_Count, pdf_new_int(ctx, doc, old_count > 0 ? nc : -nc)); + int old_count = pdf_to_int(ctx, pdf_dict_get(ctx, outlines, PDF_NAME(Count))); + pdf_dict_put(ctx, outlines, PDF_NAME(First), first); + pdf_dict_put(ctx, outlines, PDF_NAME(Last), last); + pdf_dict_put_drop(ctx, outlines, PDF_NAME(Count), pdf_new_int(ctx, old_count > 0 ? nc : -nc)); } return nc; } @@ -199,19 +199,19 @@ void retainpages(fz_context *ctx, globals *glo, PyObject *liste) // Keep only pages/type and (reduced) dest entries to avoid // references to dropped pages /******************************************************************************/ - oldroot = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME_Root); - pages = pdf_dict_get(ctx, oldroot, PDF_NAME_Pages); - olddests = pdf_load_name_tree(ctx, doc, PDF_NAME_Dests); - outlines = pdf_dict_get(ctx, oldroot, PDF_NAME_Outlines); - ocproperties = pdf_dict_get(ctx, oldroot, PDF_NAME_OCProperties); + oldroot = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); + pages = pdf_dict_get(ctx, oldroot, PDF_NAME(Pages)); + olddests = pdf_load_name_tree(ctx, doc, PDF_NAME(Dests)); + outlines = pdf_dict_get(ctx, oldroot, PDF_NAME(Outlines)); + ocproperties = pdf_dict_get(ctx, oldroot, PDF_NAME(OCProperties)); root = pdf_new_dict(ctx, doc, 3); - pdf_dict_put(ctx, root, PDF_NAME_Type, pdf_dict_get(ctx, oldroot, PDF_NAME_Type)); - pdf_dict_put(ctx, root, PDF_NAME_Pages, pdf_dict_get(ctx, oldroot, PDF_NAME_Pages)); + pdf_dict_put(ctx, root, PDF_NAME(Type), pdf_dict_get(ctx, oldroot, PDF_NAME(Type))); + pdf_dict_put(ctx, root, PDF_NAME(Pages), pdf_dict_get(ctx, oldroot, PDF_NAME(Pages))); if (outlines) - pdf_dict_put(ctx, root, PDF_NAME_Outlines, outlines); + pdf_dict_put(ctx, root, PDF_NAME(Outlines), outlines); if (ocproperties) - pdf_dict_put(ctx, root, PDF_NAME_OCProperties, ocproperties); + pdf_dict_put(ctx, root, PDF_NAME(OCProperties), ocproperties); pdf_update_object(ctx, doc, pdf_to_num(ctx, oldroot), root); @@ -233,9 +233,9 @@ void retainpages(fz_context *ctx, globals *glo, PyObject *liste) fz_catch(ctx) fz_rethrow(ctx); // Update page count and kids array - countobj = pdf_new_int(ctx, doc, pdf_array_len(ctx, kids)); - pdf_dict_put_drop(ctx, pages, PDF_NAME_Count, countobj); - pdf_dict_put_drop(ctx, pages, PDF_NAME_Kids, kids); + countobj = pdf_new_int(ctx, pdf_array_len(ctx, kids)); + pdf_dict_put_drop(ctx, pages, PDF_NAME(Count), countobj); + pdf_dict_put_drop(ctx, pages, PDF_NAME(Kids), kids); pagecount = pdf_count_pages(ctx, doc); page_object_nums = fz_calloc(ctx, pagecount, sizeof(*page_object_nums)); @@ -262,20 +262,20 @@ void retainpages(fz_context *ctx, globals *glo, PyObject *liste) { pdf_obj *key = pdf_dict_get_key(ctx, olddests, i); pdf_obj *val = pdf_dict_get_val(ctx, olddests, i); - pdf_obj *dest = pdf_dict_get(ctx, val, PDF_NAME_D); + pdf_obj *dest = pdf_dict_get(ctx, val, PDF_NAME(D)); dest = pdf_array_get(ctx, dest ? dest : val, 0); if (dest_is_valid_page(ctx, dest, page_object_nums, pagecount)) { - pdf_obj *key_str = pdf_new_string(ctx, doc, pdf_to_name(ctx, key), strlen(pdf_to_name(ctx, key))); + pdf_obj *key_str = pdf_new_string(ctx, pdf_to_name(ctx, key), strlen(pdf_to_name(ctx, key))); pdf_array_push_drop(ctx, names_list, key_str); pdf_array_push(ctx, names_list, val); } } - pdf_dict_put(ctx, dests, PDF_NAME_Names, names_list); - pdf_dict_put(ctx, names, PDF_NAME_Dests, dests); - pdf_dict_put(ctx, root, PDF_NAME_Names, names); + pdf_dict_put(ctx, dests, PDF_NAME(Names), names_list); + pdf_dict_put(ctx, names, PDF_NAME(Dests), dests); + pdf_dict_put(ctx, root, PDF_NAME(Names), names); pdf_drop_obj(ctx, names); pdf_drop_obj(ctx, dests); @@ -289,7 +289,7 @@ void retainpages(fz_context *ctx, globals *glo, PyObject *liste) { pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, i); - pdf_obj *annots = pdf_dict_get(ctx, pageref, PDF_NAME_Annots); + pdf_obj *annots = pdf_dict_get(ctx, pageref, PDF_NAME(Annots)); int len = pdf_array_len(ctx, annots); int j; @@ -298,7 +298,7 @@ void retainpages(fz_context *ctx, globals *glo, PyObject *liste) { pdf_obj *o = pdf_array_get(ctx, annots, j); - if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME_Subtype), PDF_NAME_Link)) + if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME(Subtype)), PDF_NAME(Link))) continue; if (!dest_is_valid(ctx, o, pagecount, page_object_nums, names_list)) @@ -313,7 +313,7 @@ void retainpages(fz_context *ctx, globals *glo, PyObject *liste) if (strip_outlines(ctx, doc, outlines, pagecount, page_object_nums, names_list) == 0) { - pdf_dict_del(ctx, root, PDF_NAME_Outlines); + pdf_dict_del(ctx, root, PDF_NAME(Outlines)); } fz_free(ctx, page_object_nums); diff --git a/fitz/helper-stext.i b/fitz/helper-stext.i index eb18d704a..088b01697 100644 --- a/fitz/helper-stext.i +++ b/fitz/helper-stext.i @@ -12,7 +12,7 @@ JM_print_stext_page_as_text(fz_context *ctx, fz_output *out, fz_stext_page *page fz_stext_line *line; fz_stext_char *ch; char utf[10]; - int i, n; + int i, n, last_char; for (block = page->first_block; block; block = block->next) { @@ -21,13 +21,14 @@ JM_print_stext_page_as_text(fz_context *ctx, fz_output *out, fz_stext_page *page int line_n = 0; for (line = block->u.t.first_line; line; line = line->next) { - if (line_n > 0) fz_write_string(ctx, out, " "); + if (line_n > 0 && last_char != 10) fz_write_string(ctx, out, "\n"); line_n++; for (ch = line->first_char; ch; ch = ch->next) { n = fz_runetochar(utf, ch->c); for (i = 0; i < n; i++) fz_write_byte(ctx, out, utf[i]); + last_char = ch->c; } } fz_write_string(ctx, out, "\n"); @@ -54,38 +55,17 @@ int JM_append_word(fz_context *ctx, PyObject *lines, fz_buffer *buff, fz_rect *w // Functions for dictionary output //----------------------------------------------------------------------------- -// create an empty rectangle -------------------------------------------------- -fz_rect *JM_empty_rect() +// create the char rectangle from the char quad +fz_rect JM_char_bbox(fz_stext_line *line, fz_stext_char *ch) { - fz_rect *r = JM_Alloc(fz_rect, 1); - r->x0 = r->y0 = r->x1 = r->y1 = 0; + fz_rect r = fz_rect_from_quad(ch->quad); + if (!fz_is_empty_rect(r)) return r; + // we need to correct erroneous font! + if ((r.y1 - r.y0) <= JM_EPS) r.y0 = r.y1 - ch->size; + if ((r.x1 - r.x0) <= JM_EPS) r.x0 = r.x1 - ch->size; return r; } -// enlarge rect r1 by r2. modify r2-height by size -void JM_join_rect(fz_rect *r1, fz_rect *r2, float size) -{ - fz_rect r = {MIN(r2->x0, r2->x1), MIN(r2->y0, r2->y1), - MAX(r2->x0, r2->x1), MAX(r2->y0, r2->y1)}; - if (abs(r.x1 - r.x0) < 0.00001f) r.x0 = r.x1 - size; - if (abs(r.y1 - r.y0) < 0.00001f) r.y0 = r.y1 - size; - - if (fz_is_empty_rect(r1)) - { - r1->x0 = r.x0; - r1->y0 = r.y0; - r1->x1 = r.x1; - r1->y1 = r.y1; - } - else - { - r1->x0 = MIN(r1->x0, r.x0); - r1->y0 = MIN(r1->y0, r.y0); - r1->x1 = MAX(r1->x1, r.x1); - r1->y1 = MAX(r1->y1, r.y1); - } -} - static int detect_super_script(fz_stext_line *line, fz_stext_char *ch) { if (line->wmode == 0 && line->dir.x == 1 && line->dir.y == 0) @@ -134,7 +114,7 @@ JM_style_end_dict(fz_context *ctx, fz_buffer *buff, PyObject *span, PyObject *sp } PyObject * -JM_extract_stext_textchar_as_dict(fz_context *ctx, fz_stext_char *ch) +JM_extract_stext_textchar_as_dict(fz_context *ctx, fz_stext_char *ch, fz_rect *ch_bbox) { //JM_TRACE("entering JM_extract_stext_textchar_as_dict"); char data[10]; @@ -143,8 +123,8 @@ JM_extract_stext_textchar_as_dict(fz_context *ctx, fz_stext_char *ch) PyDict_SetItemString(chardict, "c", PyUnicode_FromStringAndSize(data, len)); PyDict_SetItemString(chardict, "origin", Py_BuildValue("ff", ch->origin.x, ch->origin.y)); PyDict_SetItemString(chardict, "bbox", Py_BuildValue("ffff", - ch->bbox.x0, ch->bbox.y0, - ch->bbox.x1, ch->bbox.y1)); + ch_bbox->x0, ch_bbox->y0, + ch_bbox->x1, ch_bbox->y1)); //JM_TRACE("leaving JM_extract_stext_textchar_as_dict"); return chardict; } @@ -163,13 +143,13 @@ JM_extract_stext_textblock_as_dict(fz_context *ctx, fz_stext_block *block, int r PyObject *charlist = NULL, *chardict = NULL; linelist = PyList_New(0); PyObject *dict = PyDict_New(); - fz_rect *blockrect = JM_empty_rect(); + fz_rect blockrect = block->bbox, linerect, ch_bbox; PyDict_SetItemString(dict, "type", PyInt_FromLong(FZ_STEXT_BLOCK_TEXT)); //JM_TRACE("before line loop"); for (line = block->u.t.first_line; line; line = line->next) { linedict = PyDict_New(); - fz_rect *linerect = JM_empty_rect(); + linerect = line->bbox; PyDict_SetItemString(linedict, "wmode", Py_BuildValue("i", line->wmode)); PyDict_SetItemString(linedict, "dir", Py_BuildValue("ff", line->dir.x, line->dir.y)); spanlist = PyList_New(0); @@ -179,7 +159,8 @@ JM_extract_stext_textblock_as_dict(fz_context *ctx, fz_stext_block *block, int r //JM_TRACE("before character loop"); for (ch = line->first_char; ch; ch = ch->next) { - JM_join_rect(linerect, &ch->bbox, ch->size); + ch_bbox = JM_char_bbox(line, ch); + linerect = fz_union_rect(linerect, ch_bbox); //JM_TRACE("joined char bbox to linerect"); int ch_sup = detect_super_script(line, ch); if (ch->font != font || ch->size != size) @@ -214,7 +195,7 @@ JM_extract_stext_textblock_as_dict(fz_context *ctx, fz_stext_block *block, int r fz_append_rune(ctx, buff, ch->c); else { - chardict = JM_extract_stext_textchar_as_dict(ctx, ch); + chardict = JM_extract_stext_textchar_as_dict(ctx, ch, &ch_bbox); PyList_Append(charlist, chardict); Py_CLEAR(chardict); } @@ -238,12 +219,11 @@ JM_extract_stext_textblock_as_dict(fz_context *ctx, fz_stext_block *block, int r PyDict_SetItemString(linedict, "spans", spanlist); Py_CLEAR(spanlist); PyDict_SetItemString(linedict, "bbox", Py_BuildValue("ffff", - linerect->x0, linerect->y0, - linerect->x1, linerect->y1)); + linerect.x0, linerect.y0, + linerect.x1, linerect.y1)); - JM_join_rect(blockrect, linerect, 0.0f); + blockrect = fz_union_rect(blockrect, linerect); - JM_Free(linerect); PyList_Append(linelist, linedict); Py_CLEAR(linedict); } @@ -251,9 +231,8 @@ JM_extract_stext_textblock_as_dict(fz_context *ctx, fz_stext_block *block, int r PyDict_SetItemString(dict, "lines", linelist); Py_CLEAR(linelist); PyDict_SetItemString(dict, "bbox", Py_BuildValue("ffff", - blockrect->x0, blockrect->y0, - blockrect->x1, blockrect->y1)); - JM_Free(blockrect); + blockrect.x0, blockrect.y0, + blockrect.x1, blockrect.y1)); //JM_TRACE("leaving JM_extract_stext_textblock_as_dict"); return dict; } @@ -310,7 +289,10 @@ JM_extract_stext_imageblock_as_dict(fz_context *ctx, fz_stext_block *block) buf = freebuf = fz_new_buffer_from_image_as_png(ctx, image, NULL); strcpy(ext, "png"); } - bytes = JM_BinFromBuffer(ctx, buf); + if (PY_MAJOR_VERSION > 2) + bytes = JM_BinFromBuffer(ctx, buf); + else + bytes = JM_BArrayFromBuffer(ctx, buf); } fz_always(ctx) { diff --git a/fitz/helper-write-c.i b/fitz/helper-write-c.i new file mode 100644 index 000000000..7d5202a4a --- /dev/null +++ b/fitz/helper-write-c.i @@ -0,0 +1,2981 @@ +%{ +void pdf_dict_put_val_null(fz_context *ctx, pdf_obj *obj, int idx); + +#include +#include +#include + +#define SIG_EXTRAS_SIZE (1024) + +typedef struct pdf_write_state_s pdf_write_state; + +/* + As part of linearization, we need to keep a list of what objects are used + by what page. We do this by recording the objects used in a given page + in a page_objects structure. We have a list of these structures (one per + page) in the page_objects_list structure. + + The page_objects structure maintains a heap in the object array, so + insertion takes log n time, and we can heapsort and dedupe at the end for + a total worse case n log n time. + + The magic heap invariant is that: + entry[n] >= entry[(n+1)*2-1] & entry[n] >= entry[(n+1)*2] + or equivalently: + entry[(n-1)>>1] >= entry[n] + + For a discussion of the heap data structure (and heapsort) see Kingston, + "Algorithms and Data Structures". +*/ + +typedef struct { + int num_shared; + int page_object_number; + int num_objects; + int min_ofs; + int max_ofs; + /* Extensible list of objects used on this page */ + int cap; + int len; + int object[1]; +} page_objects; + +typedef struct { + int cap; + int len; + page_objects *page[1]; +} page_objects_list; + +struct pdf_write_state_s +{ + fz_output *out; + + int do_incremental; + int do_tight; + int do_ascii; + int do_expand; + int do_compress; + int do_compress_images; + int do_compress_fonts; + int do_garbage; + int do_linear; + int do_clean; + + int list_len; + int *use_list; + int64_t *ofs_list; + int *gen_list; + int *renumber_map; + int continue_on_error; + int *errors; + /* The following extras are required for linearization */ + int *rev_renumber_map; + int start; + int64_t first_xref_offset; + int64_t main_xref_offset; + int64_t first_xref_entry_offset; + int64_t file_len; + int hints_shared_offset; + int hintstream_len; + pdf_obj *linear_l; + pdf_obj *linear_h0; + pdf_obj *linear_h1; + pdf_obj *linear_o; + pdf_obj *linear_e; + pdf_obj *linear_n; + pdf_obj *linear_t; + pdf_obj *hints_s; + pdf_obj *hints_length; + int page_count; + page_objects_list *page_object_lists; + int crypt_object_number; +}; + +/* + * Constants for use with use_list. + * + * If use_list[num] = 0, then object num is unused. + * If use_list[num] & PARAMS, then object num is the linearisation params obj. + * If use_list[num] & CATALOGUE, then object num is used by the catalogue. + * If use_list[num] & PAGE1, then object num is used by page 1. + * If use_list[num] & SHARED, then object num is shared between pages. + * If use_list[num] & PAGE_OBJECT then this must be the first object in a page. + * If use_list[num] & OTHER_OBJECTS then this must should appear in section 9. + * Otherwise object num is used by page (use_list[num]>>USE_PAGE_SHIFT). + */ +enum +{ + USE_CATALOGUE = 2, + USE_PAGE1 = 4, + USE_SHARED = 8, + USE_PARAMS = 16, + USE_HINTS = 32, + USE_PAGE_OBJECT = 64, + USE_OTHER_OBJECTS = 128, + USE_PAGE_MASK = ~255, + USE_PAGE_SHIFT = 8 +}; + +/* + * page_objects and page_object_list handling functions + */ +static page_objects_list * +page_objects_list_create(fz_context *ctx) +{ + page_objects_list *pol = fz_calloc(ctx, 1, sizeof(*pol)); + + pol->cap = 1; + pol->len = 0; + return pol; +} + +static void +page_objects_list_destroy(fz_context *ctx, page_objects_list *pol) +{ + int i; + + if (!pol) + return; + for (i = 0; i < pol->len; i++) + { + fz_free(ctx, pol->page[i]); + } + fz_free(ctx, pol); +} + +static void +page_objects_list_ensure(fz_context *ctx, page_objects_list **pol, int newcap) +{ + int oldcap = (*pol)->cap; + if (newcap <= oldcap) + return; + *pol = fz_resize_array(ctx, *pol, 1, sizeof(page_objects_list) + (newcap-1)*sizeof(page_objects *)); + memset(&(*pol)->page[oldcap], 0, (newcap-oldcap)*sizeof(page_objects *)); + (*pol)->cap = newcap; +} + +static page_objects * +page_objects_create(fz_context *ctx) +{ + int initial_cap = 8; + page_objects *po = fz_calloc(ctx, 1, sizeof(*po) + (initial_cap-1) * sizeof(int)); + + po->cap = initial_cap; + po->len = 0; + return po; +} + +static void +page_objects_insert(fz_context *ctx, page_objects **ppo, int i) +{ + page_objects *po; + + /* Make a page_objects if we don't have one */ + if (*ppo == NULL) + *ppo = page_objects_create(ctx); + + po = *ppo; + /* page_objects insertion: extend the page_objects by 1, and put us on the end */ + if (po->len == po->cap) + { + po = fz_resize_array(ctx, po, 1, sizeof(page_objects) + (po->cap*2 - 1)*sizeof(int)); + po->cap *= 2; + *ppo = po; + } + po->object[po->len++] = i; +} + +static void +page_objects_list_insert(fz_context *ctx, pdf_write_state *opts, int page, int object) +{ + page_objects_list_ensure(ctx, &opts->page_object_lists, page+1); + if (opts->page_object_lists->len < page+1) + opts->page_object_lists->len = page+1; + page_objects_insert(ctx, &opts->page_object_lists->page[page], object); +} + +static void +page_objects_list_set_page_object(fz_context *ctx, pdf_write_state *opts, int page, int object) +{ + page_objects_list_ensure(ctx, &opts->page_object_lists, page+1); + opts->page_object_lists->page[page]->page_object_number = object; +} + +static void +page_objects_sort(fz_context *ctx, page_objects *po) +{ + int i, j; + int n = po->len; + + /* Step 1: Make a heap */ + /* Invariant: Valid heap in [0..i), unsorted elements in [i..n) */ + for (i = 1; i < n; i++) + { + /* Now bubble backwards to maintain heap invariant */ + j = i; + while (j != 0) + { + int tmp; + int k = (j-1)>>1; + if (po->object[k] >= po->object[j]) + break; + tmp = po->object[k]; + po->object[k] = po->object[j]; + po->object[j] = tmp; + j = k; + } + } + + /* Step 2: Heap sort */ + /* Invariant: valid heap in [0..i), sorted list in [i..n) */ + /* Initially: i = n */ + for (i = n-1; i > 0; i--) + { + /* Swap the maximum (0th) element from the page_objects into its place + * in the sorted list (position i). */ + int tmp = po->object[0]; + po->object[0] = po->object[i]; + po->object[i] = tmp; + /* Now, the page_objects is invalid because the 0th element is out + * of place. Bubble it until the page_objects is valid. */ + j = 0; + while (1) + { + /* Children are k and k+1 */ + int k = (j+1)*2-1; + /* If both children out of the page_objects, we're done */ + if (k > i-1) + break; + /* If both are in the page_objects, pick the larger one */ + if (k < i-1 && po->object[k] < po->object[k+1]) + k++; + /* If j is bigger than k (i.e. both of its children), + * we're done */ + if (po->object[j] > po->object[k]) + break; + tmp = po->object[k]; + po->object[k] = po->object[j]; + po->object[j] = tmp; + j = k; + } + } +} + +static int +order_ge(int ui, int uj) +{ + /* + For linearization, we need to order the sections as follows: + + Remaining pages (Part 7) + Shared objects (Part 8) + Objects not associated with any page (Part 9) + Any "other" objects + (Header)(Part 1) + (Linearization params) (Part 2) + (1st page Xref/Trailer) (Part 3) + Catalogue (and other document level objects) (Part 4) + First page (Part 6) + (Primary Hint stream) (*) (Part 5) + Any free objects + + Note, this is NOT the same order they appear in + the final file! + + (*) The PDF reference gives us the option of putting the hint stream + after the first page, and we take it, for simplicity. + */ + + /* If the 2 objects are in the same section, then page object comes first. */ + if (((ui ^ uj) & ~USE_PAGE_OBJECT) == 0) + return ((ui & USE_PAGE_OBJECT) == 0); + /* Put unused objects last */ + else if (ui == 0) + return 1; + else if (uj == 0) + return 0; + /* Put the hint stream before that... */ + else if (ui & USE_HINTS) + return 1; + else if (uj & USE_HINTS) + return 0; + /* Put page 1 before that... */ + else if (ui & USE_PAGE1) + return 1; + else if (uj & USE_PAGE1) + return 0; + /* Put the catalogue before that... */ + else if (ui & USE_CATALOGUE) + return 1; + else if (uj & USE_CATALOGUE) + return 0; + /* Put the linearization params before that... */ + else if (ui & USE_PARAMS) + return 1; + else if (uj & USE_PARAMS) + return 0; + /* Put other objects before that */ + else if (ui & USE_OTHER_OBJECTS) + return 1; + else if (uj & USE_OTHER_OBJECTS) + return 0; + /* Put shared objects before that... */ + else if (ui & USE_SHARED) + return 1; + else if (uj & USE_SHARED) + return 0; + /* And otherwise, order by the page number on which + * they are used. */ + return (ui>>USE_PAGE_SHIFT) >= (uj>>USE_PAGE_SHIFT); +} + +static void +heap_sort(int *list, int n, const int *val, int (*ge)(int, int)) +{ + int i, j; + + /* Step 1: Make a heap */ + /* Invariant: Valid heap in [0..i), unsorted elements in [i..n) */ + for (i = 1; i < n; i++) + { + /* Now bubble backwards to maintain heap invariant */ + j = i; + while (j != 0) + { + int tmp; + int k = (j-1)>>1; + if (ge(val[list[k]], val[list[j]])) + break; + tmp = list[k]; + list[k] = list[j]; + list[j] = tmp; + j = k; + } + } + + /* Step 2: Heap sort */ + /* Invariant: valid heap in [0..i), sorted list in [i..n) */ + /* Initially: i = n */ + for (i = n-1; i > 0; i--) + { + /* Swap the maximum (0th) element from the page_objects into its place + * in the sorted list (position i). */ + int tmp = list[0]; + list[0] = list[i]; + list[i] = tmp; + /* Now, the page_objects is invalid because the 0th element is out + * of place. Bubble it until the page_objects is valid. */ + j = 0; + while (1) + { + /* Children are k and k+1 */ + int k = (j+1)*2-1; + /* If both children out of the page_objects, we're done */ + if (k > i-1) + break; + /* If both are in the page_objects, pick the larger one */ + if (k < i-1 && ge(val[list[k+1]], val[list[k]])) + k++; + /* If j is bigger than k (i.e. both of its children), + * we're done */ + if (ge(val[list[j]], val[list[k]])) + break; + tmp = list[k]; + list[k] = list[j]; + list[j] = tmp; + j = k; + } + } +} + +static void +page_objects_dedupe(fz_context *ctx, page_objects *po) +{ + int i, j; + int n = po->len-1; + + for (i = 0; i < n; i++) + { + if (po->object[i] == po->object[i+1]) + break; + } + j = i; /* j points to the last valid one */ + i++; /* i points to the first one we haven't looked at */ + for (; i < n; i++) + { + if (po->object[j] != po->object[i]) + po->object[++j] = po->object[i]; + } + po->len = j+1; +} + +static void +page_objects_list_sort_and_dedupe(fz_context *ctx, page_objects_list *pol) +{ + int i; + int n = pol->len; + + for (i = 0; i < n; i++) + { + page_objects_sort(ctx, pol->page[i]); + page_objects_dedupe(ctx, pol->page[i]); + } +} + +/* + * Garbage collect objects not reachable from the trailer. + */ + +/* Mark a reference. If it's been marked already, return NULL (as no further + * processing is required). If it's not, return the resolved object so + * that we can continue our recursive marking. If it's a duff reference + * return the fact so that we can remove the reference at source. + */ +static pdf_obj *markref(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj, int *duff) +{ + int num = pdf_to_num(ctx, obj); + + if (num <= 0 || num >= pdf_xref_len(ctx, doc)) + { + *duff = 1; + return NULL; + } + *duff = 0; + if (opts->use_list[num]) + return NULL; + + opts->use_list[num] = 1; + + /* Bake in /Length in stream objects */ + fz_try(ctx) + { + if (pdf_obj_num_is_stream(ctx, doc, num)) + { + pdf_obj *len = pdf_dict_get(ctx, obj, PDF_NAME(Length)); + if (pdf_is_indirect(ctx, len)) + { + opts->use_list[pdf_to_num(ctx, len)] = 0; + len = pdf_resolve_indirect(ctx, len); + pdf_dict_put(ctx, obj, PDF_NAME(Length), len); + } + } + } + fz_catch(ctx) + { + fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); + /* Leave broken */ + } + + obj = pdf_resolve_indirect(ctx, obj); + if (obj == NULL || pdf_is_null(ctx, obj)) + { + *duff = 1; + opts->use_list[num] = 0; + } + + return obj; +} + +#ifdef DEBUG_MARK_AND_SWEEP +static int depth = 0; + +static +void indent() +{ + while (depth > 0) + { + int d = depth; + if (d > 16) + d = 16; + printf("%s", &" "[16-d]); + depth -= d; + } +} +#define DEBUGGING_MARKING(A) do { A; } while (0) +#else +#define DEBUGGING_MARKING(A) do { } while (0) +#endif + +/* Recursively mark an object. If any references found are duff, then + * replace them with nulls. */ +static int markobj(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj) +{ + int i; + + DEBUGGING_MARKING(depth++); + + while (pdf_is_indirect(ctx, obj)) + { + int duff; + DEBUGGING_MARKING(indent(); printf("Marking object %d\n", pdf_to_num(ctx, obj))); + obj = markref(ctx, doc, opts, obj, &duff); + if (duff) + { + DEBUGGING_MARKING(depth--); + return 1; + } + } + + if (pdf_is_dict(ctx, obj)) + { + int n = pdf_dict_len(ctx, obj); + for (i = 0; i < n; i++) + { + DEBUGGING_MARKING(indent(); printf("DICT[%d/%d] = %s\n", i, n, pdf_to_name(ctx, pdf_dict_get_key(ctx, obj, i)))); + if (markobj(ctx, doc, opts, pdf_dict_get_val(ctx, obj, i))) + pdf_dict_put_val_null(ctx, obj, i); + } + } + + else if (pdf_is_array(ctx, obj)) + { + int n = pdf_array_len(ctx, obj); + for (i = 0; i < n; i++) + { + DEBUGGING_MARKING(indent(); printf("ARRAY[%d/%d]\n", i, n)); + if (markobj(ctx, doc, opts, pdf_array_get(ctx, obj, i))) + pdf_array_put(ctx, obj, i, PDF_NULL); + } + } + + DEBUGGING_MARKING(depth--); + + return 0; +} + +static void +expand_lists(fz_context *ctx, pdf_write_state *opts, int num) +{ + int i; + + /* objects are numbered 0..num and maybe two additional objects for linearization */ + num += 3; + opts->use_list = fz_resize_array(ctx, opts->use_list, num, sizeof(*opts->use_list)); + opts->ofs_list = fz_resize_array(ctx, opts->ofs_list, num, sizeof(*opts->ofs_list)); + opts->gen_list = fz_resize_array(ctx, opts->gen_list, num, sizeof(*opts->gen_list)); + opts->renumber_map = fz_resize_array(ctx, opts->renumber_map, num, sizeof(*opts->renumber_map)); + opts->rev_renumber_map = fz_resize_array(ctx, opts->rev_renumber_map, num, sizeof(*opts->rev_renumber_map)); + + for (i = opts->list_len; i < num; i++) + { + opts->use_list[i] = 0; + opts->ofs_list[i] = 0; + opts->gen_list[i] = 0; + opts->renumber_map[i] = i; + opts->rev_renumber_map[i] = i; + } + opts->list_len = num; +} + +/* + * Scan for and remove duplicate objects (slow) + */ + +static void removeduplicateobjs(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + int num, other, max_num; + int xref_len = pdf_xref_len(ctx, doc); + + for (num = 1; num < xref_len; num++) + { + /* Only compare an object to objects preceding it */ + for (other = 1; other < num; other++) + { + pdf_obj *a, *b; + int newnum, streama = 0, streamb = 0, differ = 0; + + if (num == other || !opts->use_list[num] || !opts->use_list[other]) + continue; + + /* TODO: resolve indirect references to see if we can omit them */ + + /* + * Comparing stream objects data contents would take too long. + * + * pdf_obj_num_is_stream calls pdf_cache_object and ensures + * that the xref table has the objects loaded. + */ + fz_try(ctx) + { + streama = pdf_obj_num_is_stream(ctx, doc, num); + streamb = pdf_obj_num_is_stream(ctx, doc, other); + differ = streama || streamb; + if (streama && streamb && opts->do_garbage >= 4) + differ = 0; + } + fz_catch(ctx) + { + /* Assume different */ + differ = 1; + } + if (differ) + continue; + + a = pdf_get_xref_entry(ctx, doc, num)->obj; + b = pdf_get_xref_entry(ctx, doc, other)->obj; + + if (pdf_objcmp(ctx, a, b)) + continue; + + if (streama && streamb) + { + /* Check to see if streams match too. */ + fz_buffer *sa = NULL; + fz_buffer *sb = NULL; + + fz_var(sa); + fz_var(sb); + + differ = 1; + fz_try(ctx) + { + unsigned char *dataa, *datab; + size_t lena, lenb; + sa = pdf_load_raw_stream_number(ctx, doc, num); + sb = pdf_load_raw_stream_number(ctx, doc, other); + lena = fz_buffer_storage(ctx, sa, &dataa); + lenb = fz_buffer_storage(ctx, sb, &datab); + if (lena == lenb && memcmp(dataa, datab, lena) == 0) + differ = 0; + } + fz_always(ctx) + { + fz_drop_buffer(ctx, sa); + fz_drop_buffer(ctx, sb); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + if (differ) + continue; + } + + /* Keep the lowest numbered object */ + newnum = fz_mini(num, other); + max_num = fz_maxi(num, other); + if (max_num >= opts->list_len) + expand_lists(ctx, opts, max_num); + opts->renumber_map[num] = newnum; + opts->renumber_map[other] = newnum; + opts->rev_renumber_map[newnum] = num; /* Either will do */ + opts->use_list[fz_maxi(num, other)] = 0; + + /* One duplicate was found, do not look for another */ + break; + } + } +} + +/* + * Renumber objects sequentially so the xref is more compact + * + * This code assumes that any opts->renumber_map[n] <= n for all n. + */ + +static void compactxref(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + int num, newnum; + int xref_len = pdf_xref_len(ctx, doc); + + /* + * Update renumber_map in-place, clustering all used + * objects together at low object ids. Objects that + * already should be renumbered will have their new + * object ids be updated to reflect the compaction. + */ + + if (xref_len > opts->list_len) + expand_lists(ctx, opts, xref_len-1); + + newnum = 1; + for (num = 1; num < xref_len; num++) + { + /* If it's not used, map it to zero */ + if (!opts->use_list[opts->renumber_map[num]]) + { + opts->renumber_map[num] = 0; + } + /* If it's not moved, compact it. */ + else if (opts->renumber_map[num] == num) + { + opts->rev_renumber_map[newnum] = opts->rev_renumber_map[num]; + opts->renumber_map[num] = newnum++; + } + /* Otherwise it's used, and moved. We know that it must have + * moved down, so the place it's moved to will be in the right + * place already. */ + else + { + opts->renumber_map[num] = opts->renumber_map[opts->renumber_map[num]]; + } + } +} + +/* + * Update indirect objects according to renumbering established when + * removing duplicate objects and compacting the xref. + */ + +static void renumberobj(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj) +{ + int i; + int xref_len = pdf_xref_len(ctx, doc); + + if (pdf_is_dict(ctx, obj)) + { + int n = pdf_dict_len(ctx, obj); + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, obj, i); + pdf_obj *val = pdf_dict_get_val(ctx, obj, i); + if (pdf_is_indirect(ctx, val)) + { + int o = pdf_to_num(ctx, val); + if (o >= xref_len || o <= 0 || opts->renumber_map[o] == 0) + val = PDF_NULL; + else + val = pdf_new_indirect(ctx, doc, opts->renumber_map[o], 0); + pdf_dict_put_drop(ctx, obj, key, val); + } + else + { + renumberobj(ctx, doc, opts, val); + } + } + } + + else if (pdf_is_array(ctx, obj)) + { + int n = pdf_array_len(ctx, obj); + for (i = 0; i < n; i++) + { + pdf_obj *val = pdf_array_get(ctx, obj, i); + if (pdf_is_indirect(ctx, val)) + { + int o = pdf_to_num(ctx, val); + if (o >= xref_len || o <= 0 || opts->renumber_map[o] == 0) + val = PDF_NULL; + else + val = pdf_new_indirect(ctx, doc, opts->renumber_map[o], 0); + pdf_array_put_drop(ctx, obj, i, val); + } + else + { + renumberobj(ctx, doc, opts, val); + } + } + } +} + +static void renumberobjs(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + pdf_xref_entry *newxref = NULL; + int newlen; + int num; + int *new_use_list; + int xref_len = pdf_xref_len(ctx, doc); + + new_use_list = fz_calloc(ctx, pdf_xref_len(ctx, doc)+3, sizeof(int)); + + fz_var(newxref); + fz_try(ctx) + { + /* Apply renumber map to indirect references in all objects in xref */ + renumberobj(ctx, doc, opts, pdf_trailer(ctx, doc)); + for (num = 0; num < xref_len; num++) + { + pdf_obj *obj; + int to = opts->renumber_map[num]; + + /* If object is going to be dropped, don't bother renumbering */ + if (to == 0) + continue; + + obj = pdf_get_xref_entry(ctx, doc, num)->obj; + + if (pdf_is_indirect(ctx, obj)) + { + obj = pdf_new_indirect(ctx, doc, to, 0); + fz_try(ctx) + pdf_update_object(ctx, doc, num, obj); + fz_always(ctx) + pdf_drop_obj(ctx, obj); + fz_catch(ctx) + fz_rethrow(ctx); + } + else + { + renumberobj(ctx, doc, opts, obj); + } + } + + /* Create new table for the reordered, compacted xref */ + newxref = fz_malloc_array(ctx, xref_len + 3, sizeof(pdf_xref_entry)); + newxref[0] = *pdf_get_xref_entry(ctx, doc, 0); + + /* Move used objects into the new compacted xref */ + newlen = 0; + for (num = 1; num < xref_len; num++) + { + if (opts->use_list[num]) + { + pdf_xref_entry *e; + if (newlen < opts->renumber_map[num]) + newlen = opts->renumber_map[num]; + e = pdf_get_xref_entry(ctx, doc, num); + newxref[opts->renumber_map[num]] = *e; + if (e->obj) + { + pdf_set_obj_parent(ctx, e->obj, opts->renumber_map[num]); + e->obj = NULL; + } + new_use_list[opts->renumber_map[num]] = opts->use_list[num]; + } + else + { + pdf_xref_entry *e = pdf_get_xref_entry(ctx, doc, num); + pdf_drop_obj(ctx, e->obj); + e->obj = NULL; + fz_drop_buffer(ctx, e->stm_buf); + e->stm_buf = NULL; + } + } + + pdf_replace_xref(ctx, doc, newxref, newlen + 1); + newxref = NULL; + } + fz_catch(ctx) + { + fz_free(ctx, newxref); + fz_free(ctx, new_use_list); + fz_rethrow(ctx); + } + fz_free(ctx, opts->use_list); + opts->use_list = new_use_list; + + for (num = 1; num < xref_len; num++) + { + opts->renumber_map[num] = num; + } +} + +static void page_objects_list_renumber(pdf_write_state *opts) +{ + int i, j; + + for (i = 0; i < opts->page_object_lists->len; i++) + { + page_objects *po = opts->page_object_lists->page[i]; + for (j = 0; j < po->len; j++) + { + po->object[j] = opts->renumber_map[po->object[j]]; + } + po->page_object_number = opts->renumber_map[po->page_object_number]; + } +} + +static void +mark_all(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *val, int flag, int page) +{ + if (pdf_mark_obj(ctx, val)) + return; + + fz_try(ctx) + { + if (pdf_is_indirect(ctx, val)) + { + int num = pdf_to_num(ctx, val); + if (opts->use_list[num] & USE_PAGE_MASK) + /* Already used */ + opts->use_list[num] |= USE_SHARED; + else + opts->use_list[num] |= flag; + if (page >= 0) + page_objects_list_insert(ctx, opts, page, num); + } + + if (pdf_is_dict(ctx, val)) + { + int i, n = pdf_dict_len(ctx, val); + + for (i = 0; i < n; i++) + { + mark_all(ctx, doc, opts, pdf_dict_get_val(ctx, val, i), flag, page); + } + } + else if (pdf_is_array(ctx, val)) + { + int i, n = pdf_array_len(ctx, val); + + for (i = 0; i < n; i++) + { + mark_all(ctx, doc, opts, pdf_array_get(ctx, val, i), flag, page); + } + } + } + fz_always(ctx) + { + pdf_unmark_obj(ctx, val); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } +} + +static int +mark_pages(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *val, int pagenum) +{ + if (pdf_mark_obj(ctx, val)) + return pagenum; + + fz_try(ctx) + { + if (pdf_is_dict(ctx, val)) + { + if (pdf_name_eq(ctx, PDF_NAME(Page), pdf_dict_get(ctx, val, PDF_NAME(Type)))) + { + int num = pdf_to_num(ctx, val); + pdf_unmark_obj(ctx, val); + mark_all(ctx, doc, opts, val, pagenum == 0 ? USE_PAGE1 : (pagenum<use_list[num] |= USE_PAGE_OBJECT; + } + else + { + int i, n = pdf_dict_len(ctx, val); + + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, val, i); + pdf_obj *obj = pdf_dict_get_val(ctx, val, i); + + if (pdf_name_eq(ctx, PDF_NAME(Kids), key)) + pagenum = mark_pages(ctx, doc, opts, obj, pagenum); + else + mark_all(ctx, doc, opts, obj, USE_CATALOGUE, -1); + } + + if (pdf_is_indirect(ctx, val)) + { + int num = pdf_to_num(ctx, val); + opts->use_list[num] |= USE_CATALOGUE; + } + } + } + else if (pdf_is_array(ctx, val)) + { + int i, n = pdf_array_len(ctx, val); + + for (i = 0; i < n; i++) + { + pagenum = mark_pages(ctx, doc, opts, pdf_array_get(ctx, val, i), pagenum); + } + if (pdf_is_indirect(ctx, val)) + { + int num = pdf_to_num(ctx, val); + opts->use_list[num] |= USE_CATALOGUE; + } + } + } + fz_always(ctx) + { + pdf_unmark_obj(ctx, val); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + return pagenum; +} + +static void +mark_root(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *dict) +{ + int i, n = pdf_dict_len(ctx, dict); + + if (pdf_mark_obj(ctx, dict)) + return; + + fz_try(ctx) + { + if (pdf_is_indirect(ctx, dict)) + { + int num = pdf_to_num(ctx, dict); + opts->use_list[num] |= USE_CATALOGUE; + } + + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, dict, i); + pdf_obj *val = pdf_dict_get_val(ctx, dict, i); + + if (pdf_name_eq(ctx, PDF_NAME(Pages), key)) + opts->page_count = mark_pages(ctx, doc, opts, val, 0); + else if (pdf_name_eq(ctx, PDF_NAME(Names), key)) + mark_all(ctx, doc, opts, val, USE_OTHER_OBJECTS, -1); + else if (pdf_name_eq(ctx, PDF_NAME(Dests), key)) + mark_all(ctx, doc, opts, val, USE_OTHER_OBJECTS, -1); + else if (pdf_name_eq(ctx, PDF_NAME(Outlines), key)) + { + int section; + /* Look at PageMode to decide whether to + * USE_OTHER_OBJECTS or USE_PAGE1 here. */ + if (pdf_name_eq(ctx, pdf_dict_get(ctx, dict, PDF_NAME(PageMode)), PDF_NAME(UseOutlines))) + section = USE_PAGE1; + else + section = USE_OTHER_OBJECTS; + mark_all(ctx, doc, opts, val, section, -1); + } + else + mark_all(ctx, doc, opts, val, USE_CATALOGUE, -1); + } + } + fz_always(ctx) + { + pdf_unmark_obj(ctx, dict); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } +} + +static void +mark_trailer(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *dict) +{ + int i, n = pdf_dict_len(ctx, dict); + + if (pdf_mark_obj(ctx, dict)) + return; + + fz_try(ctx) + { + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, dict, i); + pdf_obj *val = pdf_dict_get_val(ctx, dict, i); + + if (pdf_name_eq(ctx, PDF_NAME(Root), key)) + mark_root(ctx, doc, opts, val); + else + mark_all(ctx, doc, opts, val, USE_CATALOGUE, -1); + } + } + fz_always(ctx) + { + pdf_unmark_obj(ctx, dict); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } +} + +static void +add_linearization_objs(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + pdf_obj *params_obj = NULL; + pdf_obj *params_ref = NULL; + pdf_obj *hint_obj = NULL; + pdf_obj *hint_ref = NULL; + pdf_obj *o; + int params_num, hint_num; + + fz_var(params_obj); + fz_var(params_ref); + fz_var(hint_obj); + fz_var(hint_ref); + + fz_try(ctx) + { + /* Linearization params */ + params_obj = pdf_new_dict(ctx, doc, 10); + params_ref = pdf_add_object(ctx, doc, params_obj); + params_num = pdf_to_num(ctx, params_ref); + + opts->use_list[params_num] = USE_PARAMS; + opts->renumber_map[params_num] = params_num; + opts->rev_renumber_map[params_num] = params_num; + opts->gen_list[params_num] = 0; + pdf_dict_put_real(ctx, params_obj, PDF_NAME(Linearized), 1.0f); + opts->linear_l = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(L), opts->linear_l); + opts->linear_h0 = pdf_new_int(ctx, INT_MIN); + o = pdf_new_array(ctx, doc, 2); + pdf_dict_put_drop(ctx, params_obj, PDF_NAME(H), o); + pdf_array_push(ctx, o, opts->linear_h0); + opts->linear_h1 = pdf_new_int(ctx, INT_MIN); + pdf_array_push(ctx, o, opts->linear_h1); + opts->linear_o = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(O), opts->linear_o); + opts->linear_e = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(E), opts->linear_e); + opts->linear_n = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(N), opts->linear_n); + opts->linear_t = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, params_obj, PDF_NAME(T), opts->linear_t); + + /* Primary hint stream */ + hint_obj = pdf_new_dict(ctx, doc, 10); + hint_ref = pdf_add_object(ctx, doc, hint_obj); + hint_num = pdf_to_num(ctx, hint_ref); + + opts->use_list[hint_num] = USE_HINTS; + opts->renumber_map[hint_num] = hint_num; + opts->rev_renumber_map[hint_num] = hint_num; + opts->gen_list[hint_num] = 0; + pdf_dict_put_int(ctx, hint_obj, PDF_NAME(P), 0); + opts->hints_s = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, hint_obj, PDF_NAME(S), opts->hints_s); + /* FIXME: Do we have thumbnails? Do a T entry */ + /* FIXME: Do we have outlines? Do an O entry */ + /* FIXME: Do we have article threads? Do an A entry */ + /* FIXME: Do we have named destinations? Do a E entry */ + /* FIXME: Do we have interactive forms? Do a V entry */ + /* FIXME: Do we have document information? Do an I entry */ + /* FIXME: Do we have logical structure hierarchy? Do a C entry */ + /* FIXME: Do L, Page Label hint table */ + pdf_dict_put(ctx, hint_obj, PDF_NAME(Filter), PDF_NAME(FlateDecode)); + opts->hints_length = pdf_new_int(ctx, INT_MIN); + pdf_dict_put(ctx, hint_obj, PDF_NAME(Length), opts->hints_length); + pdf_get_xref_entry(ctx, doc, hint_num)->stm_ofs = -1; + } + fz_always(ctx) + { + pdf_drop_obj(ctx, params_obj); + pdf_drop_obj(ctx, params_ref); + pdf_drop_obj(ctx, hint_ref); + pdf_drop_obj(ctx, hint_obj); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } +} + +static void +lpr_inherit_res_contents(fz_context *ctx, pdf_obj *res, pdf_obj *dict, pdf_obj *text) +{ + pdf_obj *o, *r; + int i, n; + + /* If the parent node doesn't have an entry of this type, give up. */ + o = pdf_dict_get(ctx, dict, text); + if (!o) + return; + + /* If the resources dict we are building doesn't have an entry of this + * type yet, then just copy it (ensuring it's not a reference) */ + r = pdf_dict_get(ctx, res, text); + if (r == NULL) + { + o = pdf_resolve_indirect(ctx, o); + if (pdf_is_dict(ctx, o)) + o = pdf_copy_dict(ctx, o); + else if (pdf_is_array(ctx, o)) + o = pdf_copy_array(ctx, o); + else + o = NULL; + if (o) + pdf_dict_put_drop(ctx, res, text, o); + return; + } + + /* Otherwise we need to merge o into r */ + if (pdf_is_dict(ctx, o)) + { + n = pdf_dict_len(ctx, o); + for (i = 0; i < n; i++) + { + pdf_obj *key = pdf_dict_get_key(ctx, o, i); + pdf_obj *val = pdf_dict_get_val(ctx, o, i); + + if (pdf_dict_get(ctx, res, key)) + continue; + pdf_dict_put(ctx, res, key, val); + } + } +} + +static void +lpr_inherit_res(fz_context *ctx, pdf_obj *node, int depth, pdf_obj *dict) +{ + while (1) + { + pdf_obj *o; + + node = pdf_dict_get(ctx, node, PDF_NAME(Parent)); + depth--; + if (!node || depth < 0) + break; + + o = pdf_dict_get(ctx, node, PDF_NAME(Resources)); + if (o) + { + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(ExtGState)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(ColorSpace)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(Pattern)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(Shading)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(XObject)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(Font)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(ProcSet)); + lpr_inherit_res_contents(ctx, dict, o, PDF_NAME(Properties)); + } + } +} + +static pdf_obj * +lpr_inherit(fz_context *ctx, pdf_obj *node, char *text, int depth) +{ + do + { + pdf_obj *o = pdf_dict_gets(ctx, node, text); + + if (o) + return pdf_resolve_indirect(ctx, o); + node = pdf_dict_get(ctx, node, PDF_NAME(Parent)); + depth--; + } + while (depth >= 0 && node); + + return NULL; +} + +static int +lpr(fz_context *ctx, pdf_document *doc, pdf_obj *node, int depth, int page) +{ + pdf_obj *kids; + pdf_obj *o = NULL; + int i, n; + + if (pdf_mark_obj(ctx, node)) + return page; + + fz_var(o); + + fz_try(ctx) + { + if (pdf_name_eq(ctx, PDF_NAME(Page), pdf_dict_get(ctx, node, PDF_NAME(Type)))) + { + pdf_obj *r; /* r is deliberately not cleaned up */ + + /* Copy resources down to the child */ + o = pdf_keep_obj(ctx, pdf_dict_get(ctx, node, PDF_NAME(Resources))); + if (!o) + { + o = pdf_keep_obj(ctx, pdf_new_dict(ctx, doc, 2)); + pdf_dict_put(ctx, node, PDF_NAME(Resources), o); + } + lpr_inherit_res(ctx, node, depth, o); + r = lpr_inherit(ctx, node, "MediaBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(MediaBox), r); + r = lpr_inherit(ctx, node, "CropBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(CropBox), r); + r = lpr_inherit(ctx, node, "BleedBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(BleedBox), r); + r = lpr_inherit(ctx, node, "TrimBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(TrimBox), r); + r = lpr_inherit(ctx, node, "ArtBox", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(ArtBox), r); + r = lpr_inherit(ctx, node, "Rotate", depth); + if (r) + pdf_dict_put(ctx, node, PDF_NAME(Rotate), r); + page++; + } + else + { + kids = pdf_dict_get(ctx, node, PDF_NAME(Kids)); + n = pdf_array_len(ctx, kids); + for(i = 0; i < n; i++) + { + page = lpr(ctx, doc, pdf_array_get(ctx, kids, i), depth+1, page); + } + pdf_dict_del(ctx, node, PDF_NAME(Resources)); + pdf_dict_del(ctx, node, PDF_NAME(MediaBox)); + pdf_dict_del(ctx, node, PDF_NAME(CropBox)); + pdf_dict_del(ctx, node, PDF_NAME(BleedBox)); + pdf_dict_del(ctx, node, PDF_NAME(TrimBox)); + pdf_dict_del(ctx, node, PDF_NAME(ArtBox)); + pdf_dict_del(ctx, node, PDF_NAME(Rotate)); + } + } + fz_always(ctx) + { + pdf_drop_obj(ctx, o); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + + pdf_unmark_obj(ctx, node); + + return page; +} + +static void +linearize(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + int i; + int n = pdf_xref_len(ctx, doc) + 2; + int *reorder; + int *rev_renumber_map; + + opts->page_object_lists = page_objects_list_create(ctx); + + /* Ensure that every page has local references of its resources */ + /* FIXME: We could 'thin' the resources according to what is actually + * required for each page, but this would require us to run the page + * content streams. */ + pdf_localise_page_resources(ctx, doc); + + /* Walk the objects for each page, marking which ones are used, where */ + memset(opts->use_list, 0, n * sizeof(int)); + mark_trailer(ctx, doc, opts, pdf_trailer(ctx, doc)); + + /* Add new objects required for linearization */ + add_linearization_objs(ctx, doc, opts); + + /* Allocate/init the structures used for renumbering the objects */ + reorder = fz_calloc(ctx, n, sizeof(int)); + rev_renumber_map = fz_calloc(ctx, n, sizeof(int)); + for (i = 0; i < n; i++) + { + reorder[i] = i; + } + + /* Heap sort the reordering */ + heap_sort(reorder+1, n-1, opts->use_list, &order_ge); + + /* Find the split point */ + for (i = 1; (opts->use_list[reorder[i]] & USE_PARAMS) == 0; i++) {} + opts->start = i; + + /* Roll the reordering into the renumber_map */ + for (i = 0; i < n; i++) + { + opts->renumber_map[reorder[i]] = i; + rev_renumber_map[i] = opts->rev_renumber_map[reorder[i]]; + } + fz_free(ctx, opts->rev_renumber_map); + opts->rev_renumber_map = rev_renumber_map; + fz_free(ctx, reorder); + + /* Apply the renumber_map */ + page_objects_list_renumber(opts); + renumberobjs(ctx, doc, opts); + + page_objects_list_sort_and_dedupe(ctx, opts->page_object_lists); +} + +static void +update_linearization_params(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + int64_t offset; + pdf_set_int(ctx, opts->linear_l, opts->file_len); + /* Primary hint stream offset (of object, not stream!) */ + pdf_set_int(ctx, opts->linear_h0, opts->ofs_list[pdf_xref_len(ctx, doc)-1]); + /* Primary hint stream length (of object, not stream!) */ + offset = (opts->start == 1 ? opts->main_xref_offset : opts->ofs_list[1] + opts->hintstream_len); + pdf_set_int(ctx, opts->linear_h1, offset - opts->ofs_list[pdf_xref_len(ctx, doc)-1]); + /* Object number of first pages page object (the first object of page 0) */ + pdf_set_int(ctx, opts->linear_o, opts->page_object_lists->page[0]->object[0]); + /* Offset of end of first page (first page is followed by primary + * hint stream (object n-1) then remaining pages (object 1...). The + * primary hint stream counts as part of the first pages data, I think. + */ + offset = (opts->start == 1 ? opts->main_xref_offset : opts->ofs_list[1] + opts->hintstream_len); + pdf_set_int(ctx, opts->linear_e, offset); + /* Number of pages in document */ + pdf_set_int(ctx, opts->linear_n, opts->page_count); + /* Offset of first entry in main xref table */ + pdf_set_int(ctx, opts->linear_t, opts->first_xref_entry_offset + opts->hintstream_len); + /* Offset of shared objects hint table in the primary hint stream */ + pdf_set_int(ctx, opts->hints_s, opts->hints_shared_offset); + /* Primary hint stream length */ + pdf_set_int(ctx, opts->hints_length, opts->hintstream_len); +} + +/* + * Make sure we have loaded objects from object streams. + */ + +static void preloadobjstms(fz_context *ctx, pdf_document *doc) +{ + pdf_obj *obj; + int num; + + /* xref_len may change due to repair, so check it every iteration */ + for (num = 0; num < pdf_xref_len(ctx, doc); num++) + { + if (pdf_get_xref_entry(ctx, doc, num)->type == 'o') + { + obj = pdf_load_object(ctx, doc, num); + pdf_drop_obj(ctx, obj); + } + } +} + +/* + * Save streams and objects to the output + */ + +static inline int isbinary(int c) +{ + if (c == '\n' || c == '\r' || c == '\t') + return 0; + return c < 32 || c > 127; +} + +static int isbinarystream(fz_context *ctx, fz_buffer *buf) +{ + unsigned char *data; + size_t len = fz_buffer_storage(ctx, buf, &data); + size_t i; + for (i = 0; i < len; i++) + if (isbinary(data[i])) + return 1; + return 0; +} + +static fz_buffer *hexbuf(fz_context *ctx, const unsigned char *p, size_t n) +{ + static const char hex[17] = "0123456789abcdef"; + int x = 0; + size_t len = n * 2 + (n / 32) + 1; + unsigned char *data = fz_malloc(ctx, len); + fz_buffer *buf = fz_new_buffer_from_data(ctx, data, len); + + while (n--) + { + *data++ = hex[*p >> 4]; + *data++ = hex[*p & 15]; + if (++x == 32) + { + *data++ = '\n'; + x = 0; + } + p++; + } + + *data++ = '>'; + + return buf; +} + +static void addhexfilter(fz_context *ctx, pdf_document *doc, pdf_obj *dict) +{ + pdf_obj *f, *dp, *newf, *newdp; + + newf = newdp = NULL; + f = pdf_dict_get(ctx, dict, PDF_NAME(Filter)); + dp = pdf_dict_get(ctx, dict, PDF_NAME(DecodeParms)); + + fz_var(newf); + fz_var(newdp); + + fz_try(ctx) + { + if (pdf_is_name(ctx, f)) + { + newf = pdf_new_array(ctx, doc, 2); + pdf_array_push(ctx, newf, PDF_NAME(ASCIIHexDecode)); + pdf_array_push(ctx, newf, f); + f = newf; + if (pdf_is_dict(ctx, dp)) + { + newdp = pdf_new_array(ctx, doc, 2); + pdf_array_push(ctx, newdp, PDF_NULL); + pdf_array_push(ctx, newdp, dp); + dp = newdp; + } + } + else if (pdf_is_array(ctx, f)) + { + pdf_array_insert(ctx, f, PDF_NAME(ASCIIHexDecode), 0); + if (pdf_is_array(ctx, dp)) + pdf_array_insert(ctx, dp, PDF_NULL, 0); + } + else + f = PDF_NAME(ASCIIHexDecode); + + pdf_dict_put(ctx, dict, PDF_NAME(Filter), f); + if (dp) + pdf_dict_put(ctx, dict, PDF_NAME(DecodeParms), dp); + } + fz_always(ctx) + { + pdf_drop_obj(ctx, newf); + pdf_drop_obj(ctx, newdp); + } + fz_catch(ctx) + fz_rethrow(ctx); +} + +static fz_buffer *deflatebuf(fz_context *ctx, const unsigned char *p, size_t n) +{ + fz_buffer *buf; + uLongf csize; + int t; + uLong longN = (uLong)n; + unsigned char *data; + size_t cap; + + if (n != (size_t)longN) + fz_throw(ctx, FZ_ERROR_GENERIC, "Buffer too large to deflate"); + + cap = compressBound(longN); + data = fz_malloc(ctx, cap); + buf = fz_new_buffer_from_data(ctx, data, cap); + csize = (uLongf)cap; + t = compress(data, &csize, p, longN); + if (t != Z_OK) + { + fz_drop_buffer(ctx, buf); + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot deflate buffer"); + } + fz_resize_buffer(ctx, buf, csize); + return buf; +} + +static void write_data(fz_context *ctx, void *arg, const unsigned char *data, int len) +{ + fz_write_data(ctx, (fz_output *)arg, data, len); +} + +static void copystream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj_orig, int num, int gen, int do_deflate) +{ + fz_buffer *buf, *tmp; + pdf_obj *obj; + size_t len; + unsigned char *data; + + buf = pdf_load_raw_stream_number(ctx, doc, num); + + obj = pdf_copy_dict(ctx, obj_orig); + + len = fz_buffer_storage(ctx, buf, &data); + if (do_deflate && !pdf_dict_get(ctx, obj, PDF_NAME(Filter))) + { + size_t clen; + unsigned char *cdata; + tmp = deflatebuf(ctx, data, len); + clen = fz_buffer_storage(ctx, tmp, &cdata); + if (clen >= len) + { + /* Don't bother compressing, as we gain nothing. */ + fz_drop_buffer(ctx, tmp); + } + else + { + len = clen; + data = cdata; + pdf_dict_put(ctx, obj, PDF_NAME(Filter), PDF_NAME(FlateDecode)); + fz_drop_buffer(ctx, buf); + buf = tmp; + } + } + + if (opts->do_ascii && isbinarystream(ctx, buf)) + { + tmp = hexbuf(ctx, data, len); + fz_drop_buffer(ctx, buf); + buf = tmp; + len = fz_buffer_storage(ctx, buf, &data); + + addhexfilter(ctx, doc, obj); + } + + pdf_dict_put_int(ctx, obj, PDF_NAME(Length), pdf_encrypted_len(ctx, NULL, num, gen, (int)len)); + + fz_write_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_encrypted_obj(ctx, opts->out, obj, opts->do_tight, NULL, num, gen); + fz_write_string(ctx, opts->out, "\nstream\n"); + pdf_encrypt_data(ctx, NULL, num, gen, write_data, opts->out, data, (int) len); + fz_write_string(ctx, opts->out, "\nendstream\nendobj\n\n"); + + fz_drop_buffer(ctx, buf); + pdf_drop_obj(ctx, obj); +} + +static void expandstream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *obj_orig, int num, int gen, int do_deflate) +{ + fz_buffer *buf, *tmp; + pdf_obj *obj; + int truncated = 0; + size_t len; + unsigned char *data; + + buf = pdf_load_stream_truncated(ctx, doc, num, (opts->continue_on_error ? &truncated : NULL)); + if (truncated && opts->errors) + (*opts->errors)++; + + obj = pdf_copy_dict(ctx, obj_orig); + pdf_dict_del(ctx, obj, PDF_NAME(Filter)); + pdf_dict_del(ctx, obj, PDF_NAME(DecodeParms)); + + len = fz_buffer_storage(ctx, buf, &data); + if (do_deflate) + { + unsigned char *cdata; + size_t clen; + tmp = deflatebuf(ctx, data, len); + clen = fz_buffer_storage(ctx, tmp, &cdata); + if (clen >= len) + { + /* Don't bother compressing, as we gain nothing. */ + fz_drop_buffer(ctx, tmp); + } + else + { + len = clen; + data = cdata; + pdf_dict_put(ctx, obj, PDF_NAME(Filter), PDF_NAME(FlateDecode)); + fz_drop_buffer(ctx, buf); + buf = tmp; + } + } + + if (opts->do_ascii && isbinarystream(ctx, buf)) + { + tmp = hexbuf(ctx, data, len); + fz_drop_buffer(ctx, buf); + buf = tmp; + len = fz_buffer_storage(ctx, buf, &data); + + addhexfilter(ctx, doc, obj); + } + + pdf_dict_put_int(ctx, obj, PDF_NAME(Length), len); + + fz_write_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_encrypted_obj(ctx, opts->out, obj, opts->do_tight, NULL, num, gen); + fz_write_string(ctx, opts->out, "\nstream\n"); + fz_write_data(ctx, opts->out, data, len); + fz_write_string(ctx, opts->out, "\nendstream\nendobj\n\n"); + + fz_drop_buffer(ctx, buf); + pdf_drop_obj(ctx, obj); +} + +static int is_image_filter(const char *s) +{ + if (!strcmp(s, "CCITTFaxDecode") || !strcmp(s, "CCF") || + !strcmp(s, "DCTDecode") || !strcmp(s, "DCT") || + !strcmp(s, "RunLengthDecode") || !strcmp(s, "RL") || + !strcmp(s, "JBIG2Decode") || + !strcmp(s, "JPXDecode")) + return 1; + return 0; +} + +static int filter_implies_image(fz_context *ctx, pdf_obj *o) +{ + if (!o) + return 0; + if (pdf_is_name(ctx, o)) + return is_image_filter(pdf_to_name(ctx, o)); + if (pdf_is_array(ctx, o)) + { + int i, len; + len = pdf_array_len(ctx, o); + for (i = 0; i < len; i++) + if (is_image_filter(pdf_to_name(ctx, pdf_array_get(ctx, o, i)))) + return 1; + } + return 0; +} + +static int is_image_stream(fz_context *ctx, pdf_obj *obj) +{ + pdf_obj *o; + if ((o = pdf_dict_get(ctx, obj, PDF_NAME(Type)), pdf_name_eq(ctx, o, PDF_NAME(XObject)))) + if ((o = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)), pdf_name_eq(ctx, o, PDF_NAME(Image)))) + return 1; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Filter)), filter_implies_image(ctx, o)) + return 1; + if (pdf_dict_get(ctx, obj, PDF_NAME(Width)) != NULL && pdf_dict_get(ctx, obj, PDF_NAME(Height)) != NULL) + return 1; + return 0; +} + +static int is_font_stream(fz_context *ctx, pdf_obj *obj) +{ + pdf_obj *o; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Type)), pdf_name_eq(ctx, o, PDF_NAME(Font))) + return 1; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Type)), pdf_name_eq(ctx, o, PDF_NAME(FontDescriptor))) + return 1; + if (pdf_dict_get(ctx, obj, PDF_NAME(Length1)) != NULL) + return 1; + if (pdf_dict_get(ctx, obj, PDF_NAME(Length2)) != NULL) + return 1; + if (pdf_dict_get(ctx, obj, PDF_NAME(Length3)) != NULL) + return 1; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)), pdf_name_eq(ctx, o, PDF_NAME(Type1C))) + return 1; + if (o = pdf_dict_get(ctx, obj, PDF_NAME(Subtype)), pdf_name_eq(ctx, o, PDF_NAME(CIDFontType0C))) + return 1; + return 0; +} + +static int is_xml_metadata(fz_context *ctx, pdf_obj *obj) +{ + if (pdf_name_eq(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Type)), PDF_NAME(Metadata))) + if (pdf_name_eq(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Subtype)), PDF_NAME(XML))) + return 1; + return 0; +} + +static void writeobject(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int num, int gen, int skip_xrefs, int unenc) +{ + if (unenc) return; + pdf_xref_entry *entry; + pdf_obj *obj; + pdf_obj *type; + + fz_try(ctx) + { + obj = pdf_load_object(ctx, doc, num); + } + fz_catch(ctx) + { + fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); + if (opts->continue_on_error) + { + fz_write_printf(ctx, opts->out, "%d %d obj\nnull\nendobj\n", num, gen); + if (opts->errors) + (*opts->errors)++; + fz_warn(ctx, "%s", fz_caught_message(ctx)); + return; + } + else + fz_rethrow(ctx); + } + + /* skip ObjStm and XRef objects */ + if (pdf_is_dict(ctx, obj)) + { + type = pdf_dict_get(ctx, obj, PDF_NAME(Type)); + if (pdf_name_eq(ctx, type, PDF_NAME(ObjStm))) + { + opts->use_list[num] = 0; + pdf_drop_obj(ctx, obj); + return; + } + if (skip_xrefs && pdf_name_eq(ctx, type, PDF_NAME(XRef))) + { + opts->use_list[num] = 0; + pdf_drop_obj(ctx, obj); + return; + } + } + + entry = pdf_get_xref_entry(ctx, doc, num); + if (!pdf_obj_num_is_stream(ctx, doc, num)) + { + fz_write_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_encrypted_obj(ctx, opts->out, obj, opts->do_tight, NULL, num, gen); + fz_write_string(ctx, opts->out, "\nendobj\n\n"); + } + else if (entry->stm_ofs < 0 && entry->stm_buf == NULL) + { + fz_write_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_encrypted_obj(ctx, opts->out, obj, opts->do_tight, NULL, num, gen); + fz_write_string(ctx, opts->out, "\nstream\nendstream\nendobj\n\n"); + } + else + { + fz_try(ctx) + { + int do_deflate = opts->do_compress; + int do_expand = opts->do_expand; + if (opts->do_compress_images && is_image_stream(ctx, obj)) + do_deflate = 1, do_expand = 0; + if (opts->do_compress_fonts && is_font_stream(ctx, obj)) + do_deflate = 1, do_expand = 0; + if (is_xml_metadata(ctx, obj)) + do_deflate = 0, do_expand = 0; + if (do_expand) + expandstream(ctx, doc, opts, obj, num, gen, do_deflate); + else + copystream(ctx, doc, opts, obj, num, gen, do_deflate); + } + fz_catch(ctx) + { + fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); + if (opts->continue_on_error) + { + fz_write_printf(ctx, opts->out, "%d %d obj\nnull\nendobj\n", num, gen); + if (opts->errors) + (*opts->errors)++; + fz_warn(ctx, "%s", fz_caught_message(ctx)); + } + else + { + pdf_drop_obj(ctx, obj); + fz_rethrow(ctx); + } + } + } + + pdf_drop_obj(ctx, obj); +} + +static void writexrefsubsect(fz_context *ctx, pdf_write_state *opts, int from, int to) +{ + int num; + + fz_write_printf(ctx, opts->out, "%d %d\n", from, to - from); + for (num = from; num < to; num++) + { + if (opts->use_list[num]) + fz_write_printf(ctx, opts->out, "%010lu %05d n \n", opts->ofs_list[num], opts->gen_list[num]); + else + fz_write_printf(ctx, opts->out, "%010lu %05d f \n", opts->ofs_list[num], opts->gen_list[num]); + } +} + +static void writexref(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int from, int to, int first, int64_t main_xref_offset, int64_t startxref) +{ + pdf_obj *trailer = NULL; + pdf_obj *obj; + pdf_obj *nobj = NULL; + + fz_write_string(ctx, opts->out, "xref\n"); + opts->first_xref_entry_offset = fz_tell_output(ctx, opts->out); + + if (opts->do_incremental) + { + int subfrom = from; + int subto; + + while (subfrom < to) + { + while (subfrom < to && !pdf_xref_is_incremental(ctx, doc, subfrom)) + subfrom++; + + subto = subfrom; + while (subto < to && pdf_xref_is_incremental(ctx, doc, subto)) + subto++; + + if (subfrom < subto) + writexrefsubsect(ctx, opts, subfrom, subto); + + subfrom = subto; + } + } + else + { + writexrefsubsect(ctx, opts, from, to); + } + + fz_write_string(ctx, opts->out, "\n"); + + fz_var(trailer); + + if (opts->do_incremental) + { + trailer = pdf_keep_obj(ctx, pdf_trailer(ctx, doc)); + pdf_dict_put_int(ctx, trailer, PDF_NAME(Size), pdf_xref_len(ctx, doc)); + pdf_dict_put_int(ctx, trailer, PDF_NAME(Prev), doc->startxref); + doc->startxref = startxref; + } + else + { + trailer = pdf_new_dict(ctx, doc, 5); + + nobj = pdf_new_int(ctx, to); + pdf_dict_put_drop(ctx, trailer, PDF_NAME(Size), nobj); + + if (first) + { + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Info)); + if (obj) + pdf_dict_put(ctx, trailer, PDF_NAME(Info), obj); + + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); + if (obj) + pdf_dict_put(ctx, trailer, PDF_NAME(Root), obj); + + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(ID)); + if (obj) + pdf_dict_put(ctx, trailer, PDF_NAME(ID), obj); + } + if (main_xref_offset != 0) + { + nobj = pdf_new_int(ctx, main_xref_offset); + pdf_dict_put_drop(ctx, trailer, PDF_NAME(Prev), nobj); + } + } + + fz_write_string(ctx, opts->out, "trailer\n"); + /* Trailer is NOT encrypted */ + pdf_print_obj(ctx, opts->out, trailer, opts->do_tight); + fz_write_string(ctx, opts->out, "\n"); + + pdf_drop_obj(ctx, trailer); + + fz_write_printf(ctx, opts->out, "startxref\n%lu\n%%%%EOF\n", startxref); + + doc->has_xref_streams = 0; +} + +static void writexrefstreamsubsect(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_obj *index, fz_buffer *fzbuf, int from, int to) +{ + int num; + + pdf_array_push_int(ctx, index, from); + pdf_array_push_int(ctx, index, to - from); + for (num = from; num < to; num++) + { + fz_append_byte(ctx, fzbuf, opts->use_list[num] ? 1 : 0); + fz_append_byte(ctx, fzbuf, opts->ofs_list[num]>>24); + fz_append_byte(ctx, fzbuf, opts->ofs_list[num]>>16); + fz_append_byte(ctx, fzbuf, opts->ofs_list[num]>>8); + fz_append_byte(ctx, fzbuf, opts->ofs_list[num]); + fz_append_byte(ctx, fzbuf, opts->gen_list[num]); + } +} + +static void writexrefstream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int from, int to, int first, int64_t main_xref_offset, int64_t startxref) +{ + int num; + pdf_obj *dict = NULL; + pdf_obj *obj; + pdf_obj *w = NULL; + pdf_obj *index; + fz_buffer *fzbuf = NULL; + + fz_var(dict); + fz_var(w); + fz_var(fzbuf); + fz_try(ctx) + { + num = pdf_create_object(ctx, doc); + dict = pdf_new_dict(ctx, doc, 6); + pdf_update_object(ctx, doc, num, dict); + + opts->first_xref_entry_offset = fz_tell_output(ctx, opts->out); + + to++; + + if (first) + { + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Info)); + if (obj) + pdf_dict_put(ctx, dict, PDF_NAME(Info), obj); + + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); + if (obj) + pdf_dict_put(ctx, dict, PDF_NAME(Root), obj); + + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(ID)); + if (obj) + pdf_dict_put(ctx, dict, PDF_NAME(ID), obj); + + if (opts->do_incremental) + { + obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Encrypt)); + if (obj) + pdf_dict_put(ctx, dict, PDF_NAME(Encrypt), obj); + } + } + + pdf_dict_put_int(ctx, dict, PDF_NAME(Size), to); + + if (opts->do_incremental) + { + pdf_dict_put_int(ctx, dict, PDF_NAME(Prev), doc->startxref); + doc->startxref = startxref; + } + else + { + if (main_xref_offset != 0) + pdf_dict_put_int(ctx, dict, PDF_NAME(Prev), main_xref_offset); + } + + pdf_dict_put(ctx, dict, PDF_NAME(Type), PDF_NAME(XRef)); + + w = pdf_new_array(ctx, doc, 3); + pdf_dict_put(ctx, dict, PDF_NAME(W), w); + pdf_array_push_int(ctx, w, 1); + pdf_array_push_int(ctx, w, 4); + pdf_array_push_int(ctx, w, 1); + + index = pdf_new_array(ctx, doc, 2); + pdf_dict_put_drop(ctx, dict, PDF_NAME(Index), index); + + /* opts->gen_list[num] is already initialized by fz_calloc. */ + opts->use_list[num] = 1; + opts->ofs_list[num] = opts->first_xref_entry_offset; + + fzbuf = fz_new_buffer(ctx, (1 + 4 + 1) * (to-from)); + + if (opts->do_incremental) + { + int subfrom = from; + int subto; + + while (subfrom < to) + { + while (subfrom < to && !pdf_xref_is_incremental(ctx, doc, subfrom)) + subfrom++; + + subto = subfrom; + while (subto < to && pdf_xref_is_incremental(ctx, doc, subto)) + subto++; + + if (subfrom < subto) + writexrefstreamsubsect(ctx, doc, opts, index, fzbuf, subfrom, subto); + + subfrom = subto; + } + } + else + { + writexrefstreamsubsect(ctx, doc, opts, index, fzbuf, from, to); + } + + pdf_update_stream(ctx, doc, dict, fzbuf, 0); + + writeobject(ctx, doc, opts, num, 0, 0, 0); + fz_write_printf(ctx, opts->out, "startxref\n%lu\n%%%%EOF\n", startxref); + } + fz_always(ctx) + { + pdf_drop_obj(ctx, dict); + pdf_drop_obj(ctx, w); + fz_drop_buffer(ctx, fzbuf); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + + doc->has_old_style_xrefs = 0; +} + +static void +padto(fz_context *ctx, fz_output *out, int64_t target) +{ + int64_t pos = fz_tell_output(ctx, out); + + assert(pos <= target); + while (pos < target) + { + fz_write_byte(ctx, out, '\n'); + pos++; + } +} + +static void +dowriteobject(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int num, int pass) +{ + pdf_xref_entry *entry = pdf_get_xref_entry(ctx, doc, num); + if (entry->type == 'f') + opts->gen_list[num] = entry->gen; + if (entry->type == 'n') + opts->gen_list[num] = entry->gen; + if (entry->type == 'o') + opts->gen_list[num] = 0; + + /* If we are renumbering, then make sure all generation numbers are + * zero (except object 0 which must be free, and have a gen number of + * 65535). Changing the generation numbers (and indeed object numbers) + * will break encryption - so only do this if we are renumbering + * anyway. */ + if (opts->do_garbage >= 2) + opts->gen_list[num] = (num == 0 ? 65535 : 0); + + if (opts->do_garbage && !opts->use_list[num]) + return; + + if (entry->type == 'n' || entry->type == 'o') + { + if (pass > 0) + padto(ctx, opts->out, opts->ofs_list[num]); + if (!opts->do_incremental || pdf_xref_is_incremental(ctx, doc, num)) + { + opts->ofs_list[num] = fz_tell_output(ctx, opts->out); + writeobject(ctx, doc, opts, num, opts->gen_list[num], 1, num == opts->crypt_object_number); + } + } + else + opts->use_list[num] = 0; +} + +static void +writeobjects(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, int pass) +{ + int num; + int xref_len = pdf_xref_len(ctx, doc); + + if (!opts->do_incremental) + { + fz_write_printf(ctx, opts->out, "%%PDF-%d.%d\n", doc->version / 10, doc->version % 10); + fz_write_string(ctx, opts->out, "%\xC2\xB5\xC2\xB6\n\n"); + } + + dowriteobject(ctx, doc, opts, opts->start, pass); + + if (opts->do_linear) + { + /* Write first xref */ + if (pass == 0) + opts->first_xref_offset = fz_tell_output(ctx, opts->out); + else + padto(ctx, opts->out, opts->first_xref_offset); + writexref(ctx, doc, opts, opts->start, pdf_xref_len(ctx, doc), 1, opts->main_xref_offset, 0); + } + + for (num = opts->start+1; num < xref_len; num++) + dowriteobject(ctx, doc, opts, num, pass); + if (opts->do_linear && pass == 1) + { + int64_t offset = (opts->start == 1 ? opts->main_xref_offset : opts->ofs_list[1] + opts->hintstream_len); + padto(ctx, opts->out, offset); + } + for (num = 1; num < opts->start; num++) + { + if (pass == 1) + opts->ofs_list[num] += opts->hintstream_len; + dowriteobject(ctx, doc, opts, num, pass); + } +} + +static int +my_log2(int x) +{ + int i = 0; + + if (x <= 0) + return 0; + + while ((1< 0) + i++; + + if ((1<page_object_lists->page[0]; + int page_len_bits, shared_object_bits, shared_object_id_bits; + int shared_length_bits; + int xref_len = pdf_xref_len(ctx, doc); + + min_shared_object = pdf_xref_len(ctx, doc); + max_shared_object = 1; + min_shared_length = opts->file_len; + max_shared_length = 0; + for (i=1; i < xref_len; i++) + { + int min, max, page; + + min = opts->ofs_list[i]; + if (i == opts->start-1 || (opts->start == 1 && i == xref_len-1)) + max = opts->main_xref_offset; + else if (i == xref_len-1) + max = opts->ofs_list[1]; + else + max = opts->ofs_list[i+1]; + + assert(max > min); + + if (opts->use_list[i] & USE_SHARED) + { + page = -1; + if (i < min_shared_object) + min_shared_object = i; + if (i > max_shared_object) + max_shared_object = i; + if (min_shared_length > max - min) + min_shared_length = max - min; + if (max_shared_length < max - min) + max_shared_length = max - min; + } + else if (opts->use_list[i] & (USE_CATALOGUE | USE_HINTS | USE_PARAMS)) + page = -1; + else if (opts->use_list[i] & USE_PAGE1) + { + page = 0; + if (min_shared_length > max - min) + min_shared_length = max - min; + if (max_shared_length < max - min) + max_shared_length = max - min; + } + else if (opts->use_list[i] == 0) + page = -1; + else + page = opts->use_list[i]>>USE_PAGE_SHIFT; + + if (page >= 0) + { + pop[page]->num_objects++; + if (pop[page]->min_ofs > min) + pop[page]->min_ofs = min; + if (pop[page]->max_ofs < max) + pop[page]->max_ofs = max; + } + } + + min_objs_per_page = max_objs_per_page = pop[0]->num_objects; + min_page_length = max_page_length = pop[0]->max_ofs - pop[0]->min_ofs; + for (i=1; i < opts->page_count; i++) + { + int tmp; + if (min_objs_per_page > pop[i]->num_objects) + min_objs_per_page = pop[i]->num_objects; + if (max_objs_per_page < pop[i]->num_objects) + max_objs_per_page = pop[i]->num_objects; + tmp = pop[i]->max_ofs - pop[i]->min_ofs; + if (tmp < min_page_length) + min_page_length = tmp; + if (tmp > max_page_length) + max_page_length = tmp; + } + + for (i=0; i < opts->page_count; i++) + { + int count = 0; + page_objects *po = opts->page_object_lists->page[i]; + for (j = 0; j < po->len; j++) + { + if (i == 0 && opts->use_list[po->object[j]] & USE_PAGE1) + count++; + else if (i != 0 && opts->use_list[po->object[j]] & USE_SHARED) + count++; + } + po->num_shared = count; + if (i == 0 || count > max_shared_object_refs) + max_shared_object_refs = count; + } + if (min_shared_object > max_shared_object) + min_shared_object = max_shared_object = 0; + + /* Table F.3 - Header */ + /* Header Item 1: Least number of objects in a page */ + fz_append_bits(ctx, buf, min_objs_per_page, 32); + /* Header Item 2: Location of first pages page object */ + fz_append_bits(ctx, buf, opts->ofs_list[pop[0]->page_object_number], 32); + /* Header Item 3: Number of bits required to represent the difference + * between the greatest and least number of objects in a page. */ + objs_per_page_bits = my_log2(max_objs_per_page - min_objs_per_page); + fz_append_bits(ctx, buf, objs_per_page_bits, 16); + /* Header Item 4: Least length of a page. */ + fz_append_bits(ctx, buf, min_page_length, 32); + /* Header Item 5: Number of bits needed to represent the difference + * between the greatest and least length of a page. */ + page_len_bits = my_log2(max_page_length - min_page_length); + fz_append_bits(ctx, buf, page_len_bits, 16); + /* Header Item 6: Least offset to start of content stream (Acrobat + * sets this to always be 0) */ + fz_append_bits(ctx, buf, 0, 32); + /* Header Item 7: Number of bits needed to represent the difference + * between the greatest and least offset to content stream (Acrobat + * sets this to always be 0) */ + fz_append_bits(ctx, buf, 0, 16); + /* Header Item 8: Least content stream length. (Acrobat + * sets this to always be 0) */ + fz_append_bits(ctx, buf, 0, 32); + /* Header Item 9: Number of bits needed to represent the difference + * between the greatest and least content stream length (Acrobat + * sets this to always be the same as item 5) */ + fz_append_bits(ctx, buf, page_len_bits, 16); + /* Header Item 10: Number of bits needed to represent the greatest + * number of shared object references. */ + shared_object_bits = my_log2(max_shared_object_refs); + fz_append_bits(ctx, buf, shared_object_bits, 16); + /* Header Item 11: Number of bits needed to represent the greatest + * shared object identifier. */ + shared_object_id_bits = my_log2(max_shared_object - min_shared_object + pop[0]->num_shared); + fz_append_bits(ctx, buf, shared_object_id_bits, 16); + /* Header Item 12: Number of bits needed to represent the numerator + * of the fractions. We always send 0. */ + fz_append_bits(ctx, buf, 0, 16); + /* Header Item 13: Number of bits needed to represent the denominator + * of the fractions. We always send 0. */ + fz_append_bits(ctx, buf, 0, 16); + + /* Table F.4 - Page offset hint table (per page) */ + /* Item 1: A number that, when added to the least number of objects + * on a page, gives the number of objects in the page. */ + for (i = 0; i < opts->page_count; i++) + { + fz_append_bits(ctx, buf, pop[i]->num_objects - min_objs_per_page, objs_per_page_bits); + } + fz_append_bits_pad(ctx, buf); + /* Item 2: A number that, when added to the least page length, gives + * the length of the page in bytes. */ + for (i = 0; i < opts->page_count; i++) + { + fz_append_bits(ctx, buf, pop[i]->max_ofs - pop[i]->min_ofs - min_page_length, page_len_bits); + } + fz_append_bits_pad(ctx, buf); + /* Item 3: The number of shared objects referenced from the page. */ + for (i = 0; i < opts->page_count; i++) + { + fz_append_bits(ctx, buf, pop[i]->num_shared, shared_object_bits); + } + fz_append_bits_pad(ctx, buf); + /* Item 4: Shared object id for each shared object ref in every page. + * Spec says "not for page 1", but acrobat does send page 1's - all + * as zeros. */ + for (i = 0; i < opts->page_count; i++) + { + for (j = 0; j < pop[i]->len; j++) + { + int o = pop[i]->object[j]; + if (i == 0 && opts->use_list[o] & USE_PAGE1) + fz_append_bits(ctx, buf, 0 /* o - pop[0]->page_object_number */, shared_object_id_bits); + if (i != 0 && opts->use_list[o] & USE_SHARED) + fz_append_bits(ctx, buf, o - min_shared_object + pop[0]->num_shared, shared_object_id_bits); + } + } + fz_append_bits_pad(ctx, buf); + /* Item 5: Numerator of fractional position for each shared object reference. */ + /* We always send 0 in 0 bits */ + /* Item 6: A number that, when added to the least offset to the start + * of the content stream (F.3 Item 6), gives the offset in bytes of + * start of the pages content stream object relative to the beginning + * of the page. Always 0 in 0 bits. */ + /* Item 7: A number that, when added to the least content stream length + * (F.3 Item 8), gives the length of the pages content stream object. + * Always == Item 2 as least content stream length = least page stream + * length. + */ + for (i = 0; i < opts->page_count; i++) + { + fz_append_bits(ctx, buf, pop[i]->max_ofs - pop[i]->min_ofs - min_page_length, page_len_bits); + } + + /* Pad, and then do shared object hint table */ + fz_append_bits_pad(ctx, buf); + opts->hints_shared_offset = (int)fz_buffer_storage(ctx, buf, NULL); + + /* Table F.5: */ + /* Header Item 1: Object number of the first object in the shared + * objects section. */ + fz_append_bits(ctx, buf, min_shared_object, 32); + /* Header Item 2: Location of first object in the shared objects + * section. */ + fz_append_bits(ctx, buf, opts->ofs_list[min_shared_object], 32); + /* Header Item 3: The number of shared object entries for the first + * page. */ + fz_append_bits(ctx, buf, pop[0]->num_shared, 32); + /* Header Item 4: The number of shared object entries for the shared + * objects section + first page. */ + fz_append_bits(ctx, buf, max_shared_object - min_shared_object + pop[0]->num_shared, 32); + /* Header Item 5: The number of bits needed to represent the greatest + * number of objects in a shared object group (Always 0). */ + fz_append_bits(ctx, buf, 0, 16); + /* Header Item 6: The least length of a shared object group in bytes. */ + fz_append_bits(ctx, buf, min_shared_length, 32); + /* Header Item 7: The number of bits required to represent the + * difference between the greatest and least length of a shared object + * group. */ + shared_length_bits = my_log2(max_shared_length - min_shared_length); + fz_append_bits(ctx, buf, shared_length_bits, 16); + + /* Table F.6 */ + /* Item 1: Shared object group length (page 1 objects) */ + for (j = 0; j < pop[0]->len; j++) + { + int o = pop[0]->object[j]; + int64_t min, max; + min = opts->ofs_list[o]; + if (o == opts->start-1) + max = opts->main_xref_offset; + else if (o < xref_len-1) + max = opts->ofs_list[o+1]; + else + max = opts->ofs_list[1]; + if (opts->use_list[o] & USE_PAGE1) + fz_append_bits(ctx, buf, max - min - min_shared_length, shared_length_bits); + } + /* Item 1: Shared object group length (shared objects) */ + for (i = min_shared_object; i <= max_shared_object; i++) + { + int min, max; + min = opts->ofs_list[i]; + if (i == opts->start-1) + max = opts->main_xref_offset; + else if (i < xref_len-1) + max = opts->ofs_list[i+1]; + else + max = opts->ofs_list[1]; + fz_append_bits(ctx, buf, max - min - min_shared_length, shared_length_bits); + } + fz_append_bits_pad(ctx, buf); + + /* Item 2: MD5 presence flags */ + for (i = max_shared_object - min_shared_object + pop[0]->num_shared; i > 0; i--) + { + fz_append_bits(ctx, buf, 0, 1); + } + fz_append_bits_pad(ctx, buf); + /* Item 3: MD5 sums (not present) */ + fz_append_bits_pad(ctx, buf); + /* Item 4: Number of objects in the group (not present) */ +} + +static void +make_hint_stream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + fz_buffer *buf = fz_new_buffer(ctx, 100); + + fz_try(ctx) + { + make_page_offset_hints(ctx, doc, opts, buf); + pdf_update_stream(ctx, doc, pdf_load_object(ctx, doc, pdf_xref_len(ctx, doc)-1), buf, 0); + opts->hintstream_len = (int)fz_buffer_storage(ctx, buf, NULL); + fz_drop_buffer(ctx, buf); + } + fz_catch(ctx) + { + fz_drop_buffer(ctx, buf); + fz_rethrow(ctx); + } +} + +static void presize_unsaved_signature_byteranges(fz_context *ctx, pdf_document *doc) +{ + int s; + + for (s = 0; s < doc->num_incremental_sections; s++) + { + pdf_xref *xref = &doc->xref_sections[s]; + + if (xref->unsaved_sigs) + { + /* The ByteRange objects of signatures are initially written out with + * dummy values, and then overwritten later. We need to make sure their + * initial form at least takes enough sufficient file space */ + pdf_unsaved_sig *usig; + int n = 0; + + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + n++; + + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + /* There will be segments of bytes at the beginning, at + * the end and between each consecutive pair of signatures, + * hence n + 1 */ + int i; + pdf_obj *byte_range = pdf_dict_getl(ctx, usig->field, PDF_NAME(V), PDF_NAME(ByteRange), NULL); + + for (i = 0; i < n+1; i++) + { + pdf_array_push_int(ctx, byte_range, INT_MAX); + pdf_array_push_int(ctx, byte_range, INT_MAX); + } + } + } + } +} + +static void complete_signatures(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) +{ + pdf_unsaved_sig *usig; + char *buf = NULL; + int buf_size; + int s; + int i; + int last_end; + fz_stream *stm = NULL; + fz_var(stm); + fz_var(buf); + + fz_try(ctx) + { + for (s = 0; s < doc->num_incremental_sections; s++) + { + pdf_xref *xref = &doc->xref_sections[doc->num_incremental_sections - s - 1]; + + if (xref->unsaved_sigs) + { + pdf_obj *byte_range; + buf_size = 0; + + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + int size = usig->signer->max_digest_size(usig->signer); + + buf_size = fz_maxi(buf_size, size); + } + + buf_size = buf_size * 2 + SIG_EXTRAS_SIZE; + + buf = fz_calloc(ctx, buf_size, 1); + + stm = fz_stream_from_output(ctx, opts->out); + /* Locate the byte ranges and contents in the saved file */ + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + char *bstr, *cstr, *fstr; + int pnum = pdf_obj_parent_num(ctx, pdf_dict_getl(ctx, usig->field, PDF_NAME(V), PDF_NAME(ByteRange), NULL)); + fz_seek(ctx, stm, opts->ofs_list[pnum], SEEK_SET); + (void)fz_read(ctx, stm, (unsigned char *)buf, buf_size); + buf[buf_size-1] = 0; + + bstr = strstr(buf, "/ByteRange"); + cstr = strstr(buf, "/Contents"); + fstr = strstr(buf, "/Filter"); + + if (bstr && cstr && fstr && bstr < cstr && cstr < fstr) + { + usig->byte_range_start = bstr - buf + 10 + opts->ofs_list[pnum]; + usig->byte_range_end = cstr - buf + opts->ofs_list[pnum]; + usig->contents_start = cstr - buf + 9 + opts->ofs_list[pnum]; + usig->contents_end = fstr - buf + opts->ofs_list[pnum]; + } + } + + fz_drop_stream(ctx, stm); + stm = NULL; + + /* Recreate ByteRange with correct values. Initially store the + * recreated object in the first of the unsaved signatures */ + byte_range = pdf_new_array(ctx, doc, 4); + pdf_dict_putl_drop(ctx, xref->unsaved_sigs->field, byte_range, PDF_NAME(V), PDF_NAME(ByteRange), NULL); + + last_end = 0; + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + pdf_array_push_int(ctx, byte_range, last_end); + pdf_array_push_int(ctx, byte_range, usig->contents_start - last_end); + last_end = usig->contents_end; + } + pdf_array_push_int(ctx, byte_range, last_end); + pdf_array_push_int(ctx, byte_range, xref->end_ofs - last_end); + + /* Copy the new ByteRange to the other unsaved signatures */ + for (usig = xref->unsaved_sigs->next; usig; usig = usig->next) + pdf_dict_putl_drop(ctx, usig->field, pdf_copy_array(ctx, byte_range), PDF_NAME(V), PDF_NAME(ByteRange), NULL); + + /* Write the byte range into buf, padding with spaces*/ + i = pdf_sprint_obj(ctx, buf, buf_size, byte_range, 1); + memset(buf+i, ' ', buf_size-i); + + /* Write the byte range to the file */ + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + fz_seek_output(ctx, opts->out, usig->byte_range_start, SEEK_SET); + fz_write_data(ctx, opts->out, buf, usig->byte_range_end - usig->byte_range_start); + } + + /* Write the digests into the file */ + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + pdf_write_digest(ctx, opts->out, byte_range, usig->contents_start, usig->contents_end - usig->contents_start, usig->signer); + + /* delete the unsaved_sigs records */ + while ((usig = xref->unsaved_sigs) != NULL) + { + xref->unsaved_sigs = usig->next; + pdf_drop_obj(ctx, usig->field); + usig->signer->drop(usig->signer); + fz_free(ctx, usig); + } + + xref->unsaved_sigs_end = NULL; + + fz_free(ctx, buf); + buf = NULL; + } + } + } + fz_catch(ctx) + { + fz_drop_stream(ctx, stm); + fz_free(ctx, buf); + fz_rethrow(ctx); + } +} + +static void clean_content_streams(fz_context *ctx, pdf_document *doc, int sanitize, int ascii) +{ + int n = pdf_count_pages(ctx, doc); + int i; + + for (i = 0; i < n; i++) + { + pdf_annot *annot; + pdf_page *page = pdf_load_page(ctx, doc, i); + pdf_clean_page_contents(ctx, doc, page, NULL, NULL, NULL, sanitize, ascii); + + for (annot = pdf_first_annot(ctx, page); annot != NULL; annot = pdf_next_annot(ctx, annot)) + { + pdf_clean_annot_contents(ctx, doc, annot, NULL, NULL, NULL, sanitize, ascii); + } + + fz_drop_page(ctx, &page->super); + } +} + +/* Initialise the pdf_write_state, used dynamically during the write, from the static + * pdf_write_options, passed into pdf_save_document */ +static void initialise_write_state(fz_context *ctx, pdf_document *doc, const pdf_write_options *in_opts, pdf_write_state *opts) +{ + int xref_len = pdf_xref_len(ctx, doc); + + opts->do_incremental = in_opts->do_incremental; + opts->do_ascii = in_opts->do_ascii; + opts->do_tight = !in_opts->do_pretty; + opts->do_expand = in_opts->do_decompress; + opts->do_compress = in_opts->do_compress; + opts->do_compress_images = in_opts->do_compress_images; + opts->do_compress_fonts = in_opts->do_compress_fonts; + + opts->do_garbage = in_opts->do_garbage; + opts->do_linear = in_opts->do_linear; + opts->do_clean = in_opts->do_clean; + opts->start = 0; + opts->main_xref_offset = INT_MIN; + + /* We deliberately make these arrays long enough to cope with + * 1 to n access rather than 0..n-1, and add space for 2 new + * extra entries that may be required for linearization. */ + opts->list_len = 0; + opts->use_list = NULL; + opts->ofs_list = NULL; + opts->gen_list = NULL; + opts->renumber_map = NULL; + opts->rev_renumber_map = NULL; + opts->continue_on_error = in_opts->continue_on_error; + opts->errors = in_opts->errors; + + expand_lists(ctx, opts, xref_len); +} + +/* Free the resources held by the dynamic write options */ +static void finalise_write_state(fz_context *ctx, pdf_write_state *opts) +{ + fz_free(ctx, opts->use_list); + fz_free(ctx, opts->ofs_list); + fz_free(ctx, opts->gen_list); + fz_free(ctx, opts->renumber_map); + fz_free(ctx, opts->rev_renumber_map); + pdf_drop_obj(ctx, opts->linear_l); + pdf_drop_obj(ctx, opts->linear_h0); + pdf_drop_obj(ctx, opts->linear_h1); + pdf_drop_obj(ctx, opts->linear_o); + pdf_drop_obj(ctx, opts->linear_e); + pdf_drop_obj(ctx, opts->linear_n); + pdf_drop_obj(ctx, opts->linear_t); + pdf_drop_obj(ctx, opts->hints_s); + pdf_drop_obj(ctx, opts->hints_length); + page_objects_list_destroy(ctx, opts->page_object_lists); +} + +static void +prepare_for_save(fz_context *ctx, pdf_document *doc, pdf_write_options *in_opts) +{ + doc->freeze_updates = 1; + + /* Rewrite (and possibly sanitize) the operator streams */ + if (in_opts->do_clean || in_opts->do_sanitize) + clean_content_streams(ctx, doc, in_opts->do_sanitize, in_opts->do_ascii); + + pdf_finish_edit(ctx, doc); + presize_unsaved_signature_byteranges(ctx, doc); +} + +static void +change_identity(fz_context *ctx, pdf_document *doc) +{ + pdf_obj *identity = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(ID)); + pdf_obj *str; + unsigned char rnd[16]; + + if (pdf_array_len(ctx, identity) < 2) + return; + + /* Maybe recalculate this in future. For now, just change the second one. */ + fz_memrnd(ctx, rnd, 16); + str = pdf_new_string(ctx, (char *)rnd, 16); + pdf_array_put_drop(ctx, identity, 1, str); + +} + +static void +do_pdf_save_document(fz_context *ctx, pdf_document *doc, pdf_write_state *opts, pdf_write_options *in_opts) +{ + int lastfree; + int num; + int xref_len; + + if (in_opts->do_incremental) + { + /* If no changes, nothing to write */ + if (doc->num_incremental_sections == 0) + return; + if (opts->out) + { + fz_seek_output(ctx, opts->out, 0, SEEK_END); + fz_write_string(ctx, opts->out, "\n"); + } + } + + xref_len = pdf_xref_len(ctx, doc); + + fz_try(ctx) + { + initialise_write_state(ctx, doc, in_opts, opts); + + /* Make sure any objects hidden in compressed streams have been loaded */ + if (!opts->do_incremental) + { + pdf_ensure_solid_xref(ctx, doc, xref_len); + preloadobjstms(ctx, doc); + change_identity(ctx, doc); + xref_len = pdf_xref_len(ctx, doc); /* May have changed due to repair */ + expand_lists(ctx, opts, xref_len); + } + + /* Sweep & mark objects from the trailer */ + if (opts->do_garbage >= 1 || opts->do_linear) + (void)markobj(ctx, doc, opts, pdf_trailer(ctx, doc)); + else + { + xref_len = pdf_xref_len(ctx, doc); /* May have changed due to repair */ + expand_lists(ctx, opts, xref_len); + for (num = 0; num < xref_len; num++) + opts->use_list[num] = 1; + } + + /* Coalesce and renumber duplicate objects */ + if (opts->do_garbage >= 3) + removeduplicateobjs(ctx, doc, opts); + + /* Compact xref by renumbering and removing unused objects */ + if (opts->do_garbage >= 2 || opts->do_linear) + compactxref(ctx, doc, opts); + + opts->crypt_object_number = 0; + + if (doc->crypt) + { + pdf_obj *crypt = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Encrypt)); + int crypt_num = pdf_to_num(ctx, crypt); + opts->crypt_object_number = opts->renumber_map[crypt_num]; + } + + /* Make renumbering affect all indirect references and update xref */ + if (opts->do_garbage >= 2 || opts->do_linear) + renumberobjs(ctx, doc, opts); + + /* Truncate the xref after compacting and renumbering */ + if ((opts->do_garbage >= 2 || opts->do_linear) && !opts->do_incremental) + { + xref_len = pdf_xref_len(ctx, doc); /* May have changed due to repair */ + expand_lists(ctx, opts, xref_len); + while (xref_len > 0 && !opts->use_list[xref_len-1]) + xref_len--; + } + + if (opts->do_linear) + linearize(ctx, doc, opts); + + if (opts->do_incremental) + { + int i; + + doc->disallow_new_increments = 1; + + for (i = 0; i < doc->num_incremental_sections; i++) + { + doc->xref_base = doc->num_incremental_sections - i - 1; + + writeobjects(ctx, doc, opts, 0); + + for (num = 0; num < xref_len; num++) + { + if (!opts->use_list[num] && pdf_xref_is_incremental(ctx, doc, num)) + { + /* Make unreusable. FIXME: would be better to link to existing free list */ + opts->gen_list[num] = 65535; + opts->ofs_list[num] = 0; + } + } + + opts->first_xref_offset = fz_tell_output(ctx, opts->out); + if (doc->has_xref_streams) + writexrefstream(ctx, doc, opts, 0, xref_len, 1, 0, opts->first_xref_offset); + else + writexref(ctx, doc, opts, 0, xref_len, 1, 0, opts->first_xref_offset); + + doc->xref_sections[doc->xref_base].end_ofs = fz_tell_output(ctx, opts->out); + } + + doc->xref_base = 0; + doc->disallow_new_increments = 0; + } + else + { + writeobjects(ctx, doc, opts, 0); + + /* Construct linked list of free object slots */ + lastfree = 0; + for (num = 0; num < xref_len; num++) + { + if (!opts->use_list[num]) + { + opts->gen_list[num]++; + opts->ofs_list[lastfree] = num; + lastfree = num; + } + } + + if (opts->do_linear && opts->page_count > 0) + { + opts->main_xref_offset = fz_tell_output(ctx, opts->out); + writexref(ctx, doc, opts, 0, opts->start, 0, 0, opts->first_xref_offset); + opts->file_len = fz_tell_output(ctx, opts->out); + + make_hint_stream(ctx, doc, opts); + if (opts->do_ascii) + { + opts->hintstream_len *= 2; + opts->hintstream_len += 1 + ((opts->hintstream_len+63)>>6); + } + opts->file_len += opts->hintstream_len; + opts->main_xref_offset += opts->hintstream_len; + update_linearization_params(ctx, doc, opts); + fz_seek_output(ctx, opts->out, 0, 0); + writeobjects(ctx, doc, opts, 1); + + padto(ctx, opts->out, opts->main_xref_offset); + writexref(ctx, doc, opts, 0, opts->start, 0, 0, opts->first_xref_offset); + } + else + { + opts->first_xref_offset = fz_tell_output(ctx, opts->out); + writexref(ctx, doc, opts, 0, xref_len, 1, 0, opts->first_xref_offset); + } + + doc->xref_sections[0].end_ofs = fz_tell_output(ctx, opts->out); + } + + complete_signatures(ctx, doc, opts); + + doc->dirty = 0; + } + fz_always(ctx) + { + finalise_write_state(ctx, opts); + + doc->freeze_updates = 0; + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } +} + +void JM_write_document(fz_context *ctx, pdf_document *doc, fz_output *out, pdf_write_options *in_opts, int decrypt) +{ + if (decrypt == 0) + { + pdf_write_document(ctx, doc, out, in_opts); + return; + } + pdf_write_options opts_defaults = { 0 }; + pdf_write_state opts = { 0 }; + + if (!doc) + return; + + if (!in_opts) + in_opts = &opts_defaults; + + if (in_opts->do_incremental && doc->repair_attempted) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes on a repaired file"); + if (in_opts->do_incremental && in_opts->do_garbage) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes with garbage collection"); + if (in_opts->do_incremental && in_opts->do_linear) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes with linearisation"); + if (pdf_has_unsaved_sigs(ctx, doc) && !out->as_stream) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't write pdf that has unsaved sigs to a fz_output unless it supports fz_stream_from_output!"); + + prepare_for_save(ctx, doc, in_opts); + + opts.out = out; + + do_pdf_save_document(ctx, doc, &opts, in_opts); +} + +void JM_save_document(fz_context *ctx, pdf_document *doc, const char *filename, pdf_write_options *in_opts, int decrypt) +{ + if (decrypt == 0) + { + pdf_save_document(ctx, doc, filename, in_opts); + return; + } + pdf_write_options opts_defaults = { 0 }; + pdf_write_state opts = { 0 }; + + if (!doc) + return; + + if (!in_opts) + in_opts = &opts_defaults; + + if (in_opts->do_incremental && !doc->file) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes on a new document"); + if (in_opts->do_incremental && doc->repair_attempted) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes on a repaired file"); + if (in_opts->do_incremental && in_opts->do_garbage) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes with garbage collection"); + if (in_opts->do_incremental && in_opts->do_linear) + fz_throw(ctx, FZ_ERROR_GENERIC, "Can't do incremental writes with linearisation"); + + prepare_for_save(ctx, doc, in_opts); + + if (in_opts->do_incremental) + { + /* If no changes, nothing to write */ + if (doc->num_incremental_sections == 0) + return; + opts.out = fz_new_output_with_path(ctx, filename, 1); + } + else + { + opts.out = fz_new_output_with_path(ctx, filename, 0); + } + fz_try(ctx) + { + do_pdf_save_document(ctx, doc, &opts, in_opts); + fz_close_output(ctx, opts.out); + } + fz_always(ctx) + { + fz_drop_output(ctx, opts.out); + opts.out = NULL; + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } +} +%} \ No newline at end of file diff --git a/fitz/helper-xobject.i b/fitz/helper-xobject.i index 4fc7540b0..833415a75 100644 --- a/fitz/helper-xobject.i +++ b/fitz/helper-xobject.i @@ -6,19 +6,19 @@ pdf_obj *JM_xobject_from_page(fz_context *ctx, pdf_document *pdfout, pdf_document *pdfsrc, int pno, fz_rect *mediabox, fz_rect *cropbox, int xref, pdf_graft_map *gmap) { fz_buffer *nres = NULL, *res = NULL; - pdf_obj *xobj1, *contents, *resources, *o, *spageref; + pdf_obj *xobj1, *contents = NULL, *resources = NULL, *o, *spageref; int i; fz_try(ctx) { if (pno < 0 || pno >= pdf_count_pages(ctx, pdfsrc)) THROWMSG("invalid page number(s)"); spageref = pdf_lookup_page_obj(ctx, pdfsrc, pno); - pdf_obj *mb = pdf_dict_get(ctx, spageref, PDF_NAME_MediaBox); + pdf_obj *mb = pdf_dict_get(ctx, spageref, PDF_NAME(MediaBox)); if (mb) - pdf_to_rect(ctx, mb, mediabox); + *mediabox = pdf_to_rect(ctx, mb); else - pdf_bound_page(ctx, pdf_load_page(ctx, pdfsrc, pno), mediabox); - o = pdf_dict_get(ctx, spageref, PDF_NAME_CropBox); + *mediabox = pdf_bound_page(ctx, pdf_load_page(ctx, pdfsrc, pno)); + o = pdf_dict_get(ctx, spageref, PDF_NAME(CropBox)); if (!o) { cropbox->x0 = mediabox->x0; @@ -27,7 +27,7 @@ pdf_obj *JM_xobject_from_page(fz_context *ctx, pdf_document *pdfout, pdf_documen cropbox->y1 = mediabox->y1; } else - pdf_to_rect(ctx, o, cropbox); + *cropbox = pdf_to_rect(ctx, o); if (xref > 0) // we can reuse an XObject! { @@ -38,14 +38,14 @@ pdf_obj *JM_xobject_from_page(fz_context *ctx, pdf_document *pdfout, pdf_documen else // need to create new XObject { // Deep-copy resources object of source page - o = pdf_dict_get(ctx, spageref, PDF_NAME_Resources); + o = pdf_dict_get(ctx, spageref, PDF_NAME(Resources)); if (gmap) // use graftmap when possible resources = pdf_graft_mapped_object(ctx, gmap, o); else resources = pdf_graft_object(ctx, pdfout, o); // get spgage contents source; combine when several objects - contents = pdf_dict_get(ctx, spageref, PDF_NAME_Contents); + contents = pdf_dict_get(ctx, spageref, PDF_NAME(Contents)); if (pdf_is_array(ctx, contents)) // more than one! { res = fz_new_buffer(ctx, 1024); @@ -64,13 +64,13 @@ pdf_obj *JM_xobject_from_page(fz_context *ctx, pdf_document *pdfout, pdf_documen //------------------------------------------------------------- // create XObject representing the source page //------------------------------------------------------------- - xobj1 = pdf_new_xobject(ctx, pdfout, mediabox, &fz_identity); + xobj1 = pdf_new_xobject(ctx, pdfout, *mediabox, fz_identity, NULL, res); // store spage contents JM_update_stream(ctx, pdfout, xobj1, res); fz_drop_buffer(ctx, res); // store spage resources - pdf_dict_put_drop(ctx, xobj1, PDF_NAME_Resources, resources); + pdf_dict_put_drop(ctx, xobj1, PDF_NAME(Resources), resources); } } fz_catch(ctx) fz_rethrow(ctx); @@ -85,7 +85,7 @@ void JM_insert_contents(fz_context *ctx, pdf_document *pdf, { fz_try(ctx) { - pdf_obj *contents = pdf_dict_get(ctx, pageref, PDF_NAME_Contents); + pdf_obj *contents = pdf_dict_get(ctx, pageref, PDF_NAME(Contents)); pdf_obj *newconts = pdf_add_stream(ctx, pdf, newcont, NULL, 0); if (pdf_is_array(ctx, contents)) { @@ -107,7 +107,7 @@ void JM_insert_contents(fz_context *ctx, pdf_document *pdf, pdf_array_push_drop(ctx, carr, newconts); pdf_array_push(ctx, carr, contents); } - pdf_dict_put_drop(ctx, pageref, PDF_NAME_Contents, carr); + pdf_dict_put_drop(ctx, pageref, PDF_NAME(Contents), carr); } } fz_catch(ctx) fz_rethrow(ctx); @@ -122,7 +122,7 @@ void JM_extend_contents(fz_context *ctx, pdf_document *pdfout, { int i; fz_buffer *oldcont = NULL, *endcont = NULL; - pdf_obj *contents = pdf_dict_get(ctx, pageref, PDF_NAME_Contents); + pdf_obj *contents = pdf_dict_get(ctx, pageref, PDF_NAME(Contents)); fz_try(ctx) { if (pdf_is_array(ctx, contents)) // multiple contents objects! @@ -161,50 +161,5 @@ void JM_extend_contents(fz_context *ctx, pdf_document *pdfout, return; } -----------------------------------------------------------------------------*/ -//----------------------------------------------------------------------------- -// Create / check AP object for the annotation -// Always reset /BBOX to rect and /Matrix to the identity matrix. -//----------------------------------------------------------------------------- -void JM_make_ap_object(fz_context *ctx, fz_annot *fzannot, fz_rect *rect, char *c) -{ - pdf_annot *annot = pdf_annot_from_fz_annot(ctx, fzannot); - pdf_document *pdf = annot->page->doc; - pdf_obj *xobj, *ca = NULL, *extgstate = NULL; - fz_buffer *contbuffer = NULL; - fz_try(ctx) - { - xobj = pdf_dict_getl(ctx, annot->obj, PDF_NAME_AP, PDF_NAME_N, NULL); - if (xobj) - { - pdf_dict_put_rect(ctx, xobj, PDF_NAME_BBox, rect); - pdf_dict_put_matrix(ctx, xobj, PDF_NAME_Matrix, &fz_identity); - } - else - xobj = pdf_new_xobject(ctx, pdf, rect, &fz_identity); - - ca = pdf_dict_get(ctx, annot->obj, PDF_NAME_CA); - if (ca) - { - pdf_obj *fcao = pdf_new_real(ctx, pdf, pdf_to_real(ctx, ca)); - pdf_obj *alp0 = pdf_new_dict(ctx, pdf, 2); - pdf_dict_put(ctx, alp0, PDF_NAME_CA, fcao); - pdf_dict_put(ctx, alp0, PDF_NAME_ca, fcao); - pdf_obj *extg = pdf_new_dict(ctx, pdf, 1); - pdf_dict_puts_drop(ctx, extg, "Alp0", alp0); - pdf_dict_putl_drop(ctx, xobj, extg, PDF_NAME_Resources, PDF_NAME_ExtGState, NULL); - } - - contbuffer = fz_new_buffer_from_copied_data(ctx, c, strlen(c)); - JM_update_stream(ctx, pdf, xobj, contbuffer); - pdf_dict_putl_drop(ctx, annot->obj, xobj, PDF_NAME_AP, PDF_NAME_N, NULL); - annot->ap = NULL; - } - fz_always(ctx) - { - fz_drop_buffer(ctx, contbuffer); - } - fz_catch(ctx) fz_rethrow(ctx); - return; -} %} \ No newline at end of file diff --git a/fitz/utils.py b/fitz/utils.py index aa4e862de..0265129a4 100644 --- a/fitz/utils.py +++ b/fitz/utils.py @@ -1,4 +1,4 @@ -from . import fitz +from fitz import * import math ''' The following is a collection of functions to extend PyMupdf. @@ -10,7 +10,7 @@ def showPDFpage(page, rect, src, pno, overlay = True, keep_proportion = True, reuse_xref=0, clip = None): """Show page number 'pno' of PDF 'src' in rectangle 'rect'. """ - fitz.CheckParent(page) + CheckParent(page) doc = page.parent # list of existing /Form /XObjects xobjlist = doc._getPageInfo(page.number, 3) @@ -31,7 +31,7 @@ def showPDFpage(page, rect, src, pno, overlay = True, keep_proportion = True, if isrc in doc.Graftmaps: # yes: use the old graftmap gmap = doc.Graftmaps[isrc] else: # no: make a new graftmap - gmap = fitz.Graftmap(doc) + gmap = Graftmap(doc) doc.Graftmaps[isrc] = gmap return page._showPDFpage(rect, src, pno, overlay = overlay, @@ -42,17 +42,17 @@ def showPDFpage(page, rect, src, pno, overlay = True, keep_proportion = True, #============================================================================== # A function for searching string occurrences on a page. #============================================================================== -def searchFor(page, text, hit_max = 16): +def searchFor(page, text, hit_max = 16, quads = False): '''Search for a string on a page. Parameters: text: string to be searched for hit_max: maximum hits Returns a list of rectangles, each containing an occurrence. ''' - fitz.CheckParent(page) + CheckParent(page) dl = page.getDisplayList() # create DisplayList tp = dl.getTextPage() # create TextPage # return list of hitting reactangles - rlist = tp.search(text, hit_max = hit_max) + rlist = tp.search(text, hit_max = hit_max, quads = quads) dl = None tp = None return rlist @@ -60,22 +60,22 @@ def searchFor(page, text, hit_max = 16): #============================================================================== # A function for searching string occurrences on a page. #============================================================================== -def searchPageFor(doc, pno, text, hit_max=16): - """Search for a string on a page. Parameters:\npno: integer page number\ntext: string to be searched for\nhit_max: maximum hits.\nReturns a list of rectangles, each of which surrounds a found occurrence.""" - return doc[pno].searchFor(text, hit_max = hit_max) +def searchPageFor(doc, pno, text, hit_max=16, quads=False): + """Search for a string on a page. Parameters:\npno: integer page number\ntext: string to be searched for\nhit_max: maximum hits.\nReturns a list of rectangles or quads, each of which surrounds a found occurrence.""" + return doc[pno].searchFor(text, hit_max = hit_max, quads = quads) #============================================================================== # A function for extracting a text blocks list #============================================================================== -def getTextBlocks(page, images = False): +def getTextBlocks(page, images=False): """Return the text blocks as a list of concatenated lines with their bbox per block. """ - fitz.CheckParent(page) + CheckParent(page) dl = page.getDisplayList() - flags = fitz.TEXT_PRESERVE_LIGATURES | fitz.TEXT_PRESERVE_WHITESPACE + flags = TEXT_PRESERVE_LIGATURES | TEXT_PRESERVE_WHITESPACE if images: - flags |= fitz.TEXT_PRESERVE_IMAGES + flags |= TEXT_PRESERVE_IMAGES tp = dl.getTextPage(flags) l = tp._extractTextBlocks_AsList() del tp @@ -88,7 +88,7 @@ def getTextBlocks(page, images = False): def getTextWords(page): """Return the text words as a list with the bbox for each word. """ - fitz.CheckParent(page) + CheckParent(page) dl = page.getDisplayList() tp = dl.getTextPage() l = tp._extractTextWords_AsList() @@ -99,7 +99,7 @@ def getTextWords(page): def getTextUWords(page): """Return the text words as a list with the bbox for each word. """ - fitz.CheckParent(page) + CheckParent(page) dl = page.getDisplayList() tp = dl.getTextPage() l = tp._extractUniWords_AsList() @@ -112,7 +112,7 @@ def getTextUWords(page): #============================================================================== def getText(page, output = "text"): '''Extract a PDF page's text. Parameters:\noutput option: text, html, dict, json, rawdict, xhtml or xml.\nReturns the output of TextPage methods extractText, extractHTML, extractDICT, extractJSON, extractRAWDICT, extractXHTML or etractXML respectively. Default and misspelling choice is "text".''' - fitz.CheckParent(page) + CheckParent(page) dl = page.getDisplayList() # available output types formats = ("text", "html", "json", "xml", "xhtml", "dict", "rawdict") @@ -122,9 +122,9 @@ def getText(page, output = "text"): f = formats.index(output.lower()) except: f = 0 - flags = fitz.TEXT_PRESERVE_LIGATURES | fitz.TEXT_PRESERVE_WHITESPACE + flags = TEXT_PRESERVE_LIGATURES | TEXT_PRESERVE_WHITESPACE if images[f] : - flags |= fitz.TEXT_PRESERVE_IMAGES + flags |= TEXT_PRESERVE_IMAGES tp = dl.getTextPage(flags) # TextPage with / without images t = tp._extractText(f) del dl @@ -148,29 +148,29 @@ def getPageText(doc, pno, output = "text"): # Requires a page object. #============================================================================== -def getPixmap(page, matrix = fitz.Identity, colorspace = fitz.csRGB, clip = None, +def getPixmap(page, matrix = None, colorspace = csRGB, clip = None, alpha = True): """Create pixmap of page. - matrix: fitz.Matrix for transformation (default: fitz.Identity). - colorspace: text string / fitz.Colorspace (rgb, rgb, gray - case ignored), default fitz.csRGB. - clip: a fitz.IRect to restrict rendering to this area. + matrix: Matrix for transformation (default: Identity). + colorspace: text string / Colorspace (rgb, rgb, gray - case ignored), default csRGB. + clip: a IRect to restrict rendering to this area. """ - fitz.CheckParent(page) + CheckParent(page) # determine required colorspace cs = colorspace if type(colorspace) is str: if colorspace.upper() == "GRAY": - cs = fitz.csGRAY + cs = csGRAY elif colorspace.upper() == "CMYK": - cs = fitz.csCMYK + cs = csCMYK else: - cs = fitz.csRGB + cs = csRGB assert cs.n in (1,3,4), "unsupported colorspace" dl = page.getDisplayList() # create DisplayList - if type(clip) is fitz.IRect: + if type(clip) is IRect: scissor = clip.rect else: scissor = clip @@ -184,10 +184,10 @@ def getPixmap(page, matrix = fitz.Identity, colorspace = fitz.csRGB, clip = None #============================================================================== # A function for rendering a page by its number #============================================================================== -# getPagePixmap(doc, pno, matrix = fitz.Identity, colorspace = "RGB", clip = None, alpha = False): -def getPagePixmap(doc, pno, matrix = fitz.Identity, colorspace = fitz.csRGB, +# getPagePixmap(doc, pno, matrix = Identity, colorspace = "RGB", clip = None, alpha = False): +def getPagePixmap(doc, pno, matrix = None, colorspace = csRGB, clip = None, alpha = True): - '''Create pixmap of page number.\nmatrix: fitz.Matrix for transformation (default: fitz.Identity).\ncolorspace: text string / fitz.Colorspace (rgb, rgb, gray - case ignored), default fitz.csRGB.\nclip: a fitz.IRect to restrict rendering to this area.''' + '''Create pixmap of page number.\nmatrix: Matrix for transformation (default: Identity).\ncolorspace: text string / Colorspace (rgb, rgb, gray - case ignored), default csRGB.\nclip: a IRect to restrict rendering to this area.''' return doc[pno].getPixmap(matrix = matrix, colorspace = colorspace, clip = clip, alpha = alpha) @@ -200,39 +200,39 @@ def getLinkDict(ln): nl["from"] = ln.rect except: pass - pnt = fitz.Point(0, 0) - if ln.dest.flags & fitz.LINK_FLAG_L_VALID: + pnt = Point(0, 0) + if ln.dest.flags & LINK_FLAG_L_VALID: pnt.x = ln.dest.lt.x - if ln.dest.flags & fitz.LINK_FLAG_T_VALID: + if ln.dest.flags & LINK_FLAG_T_VALID: pnt.y = ln.dest.lt.y - if ln.dest.kind == fitz.LINK_URI: + if ln.dest.kind == LINK_URI: nl["uri"] = ln.dest.uri - elif ln.dest.kind == fitz.LINK_GOTO: + elif ln.dest.kind == LINK_GOTO: nl["page"] = ln.dest.page nl["to"] = pnt - if ln.dest.flags & fitz.LINK_FLAG_R_IS_ZOOM: + if ln.dest.flags & LINK_FLAG_R_IS_ZOOM: nl["zoom"] = ln.dest.rb.x else: nl["zoom"] = 0.0 - elif ln.dest.kind == fitz.LINK_GOTOR: + elif ln.dest.kind == LINK_GOTOR: nl["file"] = ln.dest.fileSpec.replace("\\", "/") nl["page"] = ln.dest.page if ln.dest.page < 0: nl["to"] = ln.dest.dest else: nl["to"] = pnt - if ln.dest.flags & fitz.LINK_FLAG_R_IS_ZOOM: + if ln.dest.flags & LINK_FLAG_R_IS_ZOOM: nl["zoom"] = ln.dest.rb.x else: nl["zoom"] = 0.0 - elif ln.dest.kind == fitz.LINK_LAUNCH: + elif ln.dest.kind == LINK_LAUNCH: nl["file"] = ln.dest.fileSpec.replace("\\", "/") - elif ln.dest.kind == fitz.LINK_NAMED: + elif ln.dest.kind == LINK_NAMED: nl["name"] = ln.dest.named else: @@ -248,7 +248,7 @@ def getLinkDict(ln): def getLinks(page): '''Create a list of all links contained in a PDF page as dictionaries - see PyMuPDF ducmentation for details.''' - fitz.CheckParent(page) + CheckParent(page) ln = page.firstLink links = [] while ln: @@ -314,8 +314,6 @@ def getRectArea(*args): unit = args[1] else: unit = "px" - if rect.isInfinite or rect.isEmpty: - return 0.0 u = {"px": (1,1), "in": (1.,72.), "cm": (2.54, 72.), "mm": (25.4, 72.)} f = (u[unit][0] / u[unit][1])**2 return f * rect.width * rect.height @@ -363,185 +361,6 @@ def writeImage(*arg, **kw): return rc -#============================================================================== -# arithmetic methods for fitz.Matrix -#============================================================================== -def mat_mult(m1, m2): # __mul__ - if hasattr(m2, "__float__"): - return fitz.Matrix(m1.a * m2, m1.b * m2, m1.c * m2, - m1.d * m2, m1.e * m2, m1.f * m2) - m = fitz.Matrix() - try: - m.concat(m1, fitz.Matrix(m2)) - except: - raise NotImplementedError("op2 must be matrix-like or a number") - return m - -def mat_div(m1, m2): # __mul__ - if hasattr(m2, "__float__"): - return fitz.Matrix(m1.a /m2, m1.b /m2, m1.c /m2, - m1.d /m2, m1.e /m2, m1.f /m2) - m = fitz.Matrix() - mi1 = fitz.Matrix(m2) - mi2 = mat_invert(mi1) - if not bool(mi2): - raise ZeroDivisionError("op2 is not invertible") - m.concat(m1, mi2) - return m - -def mat_invert(me): # __invert__ - m = fitz.Matrix() - m.invert(me) - return m - -def mat_add(m1, m2): # __add__ - if hasattr(m2, "__float__"): - return fitz.Matrix(m1.a + m2, m1.b + m2, m1.c + m2, - m1.d + m2, m1.e + m2, m1.f + m2) - me = fitz.Matrix(m2) - return fitz.Matrix(m1.a + me.a, m1.b + me.b, m1.c + me.c, - m1.d + me.d, m1.e + me.e, m1.f + me.f) - - -def mat_sub(m1, m2): # __sub__ - if hasattr(m2, "__float__"): - return fitz.Matrix(m1.a - m2, m1.b - m2, m1.c - m2, - m1.d - m2, m1.e - m2, m1.f - m2) - me = fitz.Matrix(m2) - return fitz.Matrix(m1.a - me.a, m1.b - me.b, m1.c - me.c, - m1.d - me.d, m1.e - me.e, m1.f - me.f) - -def mat_abs(m): # __abs__ - a = m.a**2 + m.b**2 + m.c**2 + m.d**2 + m.e**2 + m.f**2 - return math.sqrt(a) - -def mat_true(m): # __nonzero__ - return (abs(m.a) + abs(m.b) + abs(m.c) + abs(m.d) + abs(m.e) + abs(m.f)) > 0 - -def mat_equ(m, m2): # __equ__ - return len(m) == len(m2) and mat_true(m - m2) == 0 - -def mat_contains(m, x): - return x in tuple(m) - -#============================================================================== -# arithmetic methods for fitz.Rect -#============================================================================== -def rect_or(r1, r2): # __or__: include point, rect or irect - r = fitz.Rect(r1) - if len(r2) == 2: - r.includePoint(fitz.Point(r2)) - else: - r.includeRect(fitz.Rect(r2)) - return r if type(r1) is fitz.Rect else r.irect - -def rect_and(r1, r2): # __and__: intersection with rect or irect - r = fitz.Rect(r1) - r.intersect(fitz.Rect(r2)) - return r if type(r1) is fitz.Rect else r.irect - -def rect_add(r1, r2): # __add__: add number, rect or irect to rect - r = fitz.Rect(r1) - if hasattr(r2, "__float__"): - a = fitz.Rect(r2, r2, r2, r2) - else: - a = fitz.Rect(r2) - r.x0 += a.x0 - r.y0 += a.y0 - r.x1 += a.x1 - r.y1 += a.y1 - return r if type(r1) is fitz.Rect else r.irect - -def rect_sub(r1, r2): # __sub__: subtract number, rect or irect from rect - r = fitz.Rect(r1) - if hasattr(r2, "__float__"): - a = fitz.Rect(r2, r2, r2, r2) - else: - a = fitz.Rect(r2) - r.x0 -= a.x0 - r.y0 -= a.y0 - r.x1 -= a.x1 - r.y1 -= a.y1 - return r if type(r1) is fitz.Rect else r.irect - -def rect_mul(r, m): # __mul__: transform with matrix - r1 = fitz.Rect(r) - if hasattr(m, "__float__"): - r1 = fitz.Rect(r1.x0 * m, r1.y0 * m, r1.x1 * m, r1.y1 * m) - else: - m1 = fitz.Matrix(m) - r1.transform(m1) - return r1 if type(r) is fitz.Rect else r1.irect - -def rect_div(r, m): # __div__ / __truediv__ - r1 = fitz.Rect(r) - if hasattr(m, "__float__"): - r1 = fitz.Rect(r1.x0 * 1. / m, r1.y0 * 1. / m, r1.x1 * 1. / m, r1.y1 * 1. / m) - else: - m1 = ~fitz.Matrix(m) - if not bool(m1): - raise ZeroDivisionError("op2 is not invertible") - r1.transform(m1) - return r1 if type(r) is fitz.Rect else r1.irect - -def rect_equ(r, r2): # __equ__ - return type(r) == type(r2) and rect_true(r - r2) == 0 - -def rect_true(r): - return (abs(r.x0) + abs(r.y0) + abs(r.x1) + abs(r.y1)) > 0 - -def rect_contains(r, x): - if type(x) in (fitz.Point, fitz.IRect, fitz.Rect): - return r.contains(x) - if hasattr(x, "__float__"): - return x in tuple(r) - if len(x) == 2: - return r.contains(fitz.Point(x)) - return r.contains(fitz.Rect(x)) - -#============================================================================== -# arithmetic methods for fitz.Point -#============================================================================== -def point_add(p1, p2): - if hasattr(p2, "__float__"): - return fitz.Point(p1.x + p2, p1.y + p2) - p = fitz.Point(p2) - return fitz.Point(p1.x + p.x, p1.y + p.y) - -def point_sub(p1, p2): - if hasattr(p2, "__float__"): - return fitz.Point(p1.x - p2, p1.y - p2) - p = fitz.Point(p2) - return fitz.Point(p1.x - p.x, p1.y - p.y) - -def point_mul(p, m): - if hasattr(m, "__float__"): - return fitz.Point(p.x * m, p.y * m) - p1 = fitz.Point(p) - m1 = fitz.Matrix(m) - return p1.transform(m1) - -def point_div(p, m): - if hasattr(m, "__float__"): - return fitz.Point(p.x*1./m, p.y*1./m) - p1 = fitz.Point(p) - m1 = ~fitz.Matrix(m) - if not bool(m1): - raise ZeroDivisionError("op2 is not invertible") - return p1.transform(m1) - -def point_abs(p): - return math.sqrt(p.x**2 + p.y**2) - -def point_true(p): - return (abs(p.x) + abs(p.y)) > 0 - -def point_equ(p, p2): # __equ__ - return type(p) == type(p2) and point_true(p - p2) == 0 - -def point_contains(p, x): - return x in tuple(p) - #============================================================================== # Document method Set Metadata #============================================================================== @@ -557,21 +376,21 @@ def setMetadata(doc, m): "keywords"): raise ValueError("invalid dictionary key: " + k) d = "<= 0: - fspec = fitz.getPDFstr(ddict["file"]) + if ddict["kind"] == LINK_GOTOR and ddict["page"] >= 0: + fspec = getPDFstr(ddict["file"]) dest = str_gotor1 % (ddict["page"], ddict["to"].x, ddict["to"].y, ddict["zoom"], fspec, fspec) return dest @@ -661,6 +480,7 @@ def setToC(doc, toc): # no formal errors in toc -------------------------------------------------- old_xrefs = doc._delToC() # del old outlines, get xref numbers + old_xrefs = [] # force creation of new xrefs # prepare table of xrefs for new bookmarks xref = [0] + old_xrefs xref[0] = doc._getOLRootNumber() # entry zero is outline root xref# @@ -680,7 +500,7 @@ def setToC(doc, toc): for i in range(toclen): o = toc[i] lvl = o[0] # level - title = fitz.getPDFstr(o[1]) # titel + title = getPDFstr(o[1]) # titel pno = min(doc.pageCount - 1, max(0, o[2] - 1)) # page number top = 0 if len(o) < 4: @@ -799,30 +619,30 @@ def cre_annot(lnk, xref_dst, list_src, height): r = lnk["from"] rect = "%g %g %g %g" % (r.x0, height - r.y0, r.x1, height - r.y1) - if lnk["kind"] == fitz.LINK_GOTO: + if lnk["kind"] == LINK_GOTO: txt = annot_goto idx = list_src.index(lnk["page"]) annot = txt % (xref_dst[idx], lnk["to"].x, lnk["to"].y, rect) - elif lnk["kind"] == fitz.LINK_GOTOR: + elif lnk["kind"] == LINK_GOTOR: if lnk["page"] >= 0: txt = annot_gotor - pnt = lnk.get("to", fitz.Point(0, 0)) # destination point - if type(pnt) is not fitz.Point: - pnt = fitz.Point(0, 0) + pnt = lnk.get("to", Point(0, 0)) # destination point + if type(pnt) is not Point: + pnt = Point(0, 0) annot = txt % (lnk["page"], pnt.x, pnt.y, lnk["file"], lnk["file"], rect) else: txt = annot_gotor_n - to = fitz.getPDFstr(lnk["to"]) + to = getPDFstr(lnk["to"]) to = to[1:-1] f = lnk["file"] annot = txt % (to, f, rect) - elif lnk["kind"] == fitz.LINK_LAUNCH: + elif lnk["kind"] == LINK_LAUNCH: txt = annot_launch annot = txt % (lnk["file"], lnk["file"], rect) - elif lnk["kind"] == fitz.LINK_URI: + elif lnk["kind"] == LINK_URI: txt = annot_uri annot = txt % (lnk["uri"], rect) else: @@ -875,7 +695,7 @@ def cre_annot(lnk, xref_dst, list_src, height): page_dst = doc1[list_dst[i]] link_tab = [] for l in links: - if l["kind"] == fitz.LINK_GOTO and (l["page"] not in list_src): + if l["kind"] == LINK_GOTO and (l["page"] not in list_src): continue # target not in copied pages annot_text = cre_annot(l, xref_dst, list_src, height) if not annot_text: @@ -913,38 +733,38 @@ def getLinkText(page, lnk): rect = "%g %g %g %g" % (r.x0, height - r.y0, r.x1, height - r.y1) annot = "" - if lnk["kind"] == fitz.LINK_GOTO: + if lnk["kind"] == LINK_GOTO: if lnk["page"] >= 0: txt = annot_goto pno = lnk["page"] xref = page.parent._getPageXref(pno)[0] - pnt = lnk.get("to", fitz.Point(0, 0)) # destination point + pnt = lnk.get("to", Point(0, 0)) # destination point annot = txt % (xref, pnt.x, pnt.y, rect) else: txt = annot_goto_n - annot = txt % (fitz.getPDFstr(lnk["to"]), rect) + annot = txt % (getPDFstr(lnk["to"]), rect) - elif lnk["kind"] == fitz.LINK_GOTOR: + elif lnk["kind"] == LINK_GOTOR: if lnk["page"] >= 0: txt = annot_gotor - pnt = lnk.get("to", fitz.Point(0, 0)) # destination point - if type(pnt) is not fitz.Point: - pnt = fitz.Point(0, 0) + pnt = lnk.get("to", Point(0, 0)) # destination point + if type(pnt) is not Point: + pnt = Point(0, 0) annot = txt % (lnk["page"], pnt.x, pnt.y, lnk["file"], lnk["file"], rect) else: txt = annot_gotor_n - annot = txt % (fitz.getPDFstr(lnk["to"]), lnk["file"], rect) + annot = txt % (getPDFstr(lnk["to"]), lnk["file"], rect) - elif lnk["kind"] == fitz.LINK_LAUNCH: + elif lnk["kind"] == LINK_LAUNCH: txt = annot_launch annot = txt % (lnk["file"], lnk["file"], rect) - elif lnk["kind"] == fitz.LINK_URI: + elif lnk["kind"] == LINK_URI: txt = annot_uri annot = txt % (lnk["uri"], rect) - elif lnk["kind"] == fitz.LINK_NAMED: + elif lnk["kind"] == LINK_NAMED: txt = annot_named annot = txt % (lnk["name"], rect) @@ -952,7 +772,7 @@ def getLinkText(page, lnk): def updateLink(page, lnk): """ Update a link on the current page. """ - fitz.CheckParent(page) + CheckParent(page) annot = getLinkText(page, lnk) assert annot != "", "link kind not supported" page.parent._updateObject(lnk["xref"], annot, page = page) @@ -960,22 +780,12 @@ def updateLink(page, lnk): def insertLink(page, lnk, mark = True): """ Insert a new link for the current page. """ - fitz.CheckParent(page) + CheckParent(page) annot = getLinkText(page, lnk) assert annot != "", "link kind not supported" page._addAnnot_FromString([annot]) return -def intersects(me, rect): - """ Check whether this rectangle and 'rect' have a non-empty intersection.""" - - if me.isEmpty or me.isInfinite or rect.isEmpty or rect.isInfinite: - return False - r = me & rect - if r.isEmpty or r.isInfinite: - return False - return True - #------------------------------------------------------------------------------- # Page.insertTextbox #------------------------------------------------------------------------------- @@ -1219,134 +1029,6 @@ def drawSector(page, center, point, beta, color = (0, 0, 0), fill = None, return Q - -#------------------------------------------------------------------------------- -# Annot.updateImage -#------------------------------------------------------------------------------- -def updateImage(annot): - '''Update border and color information in the appearance dictionary /AP.''' - fitz.CheckParent(annot) - - def modAP(tab, ctab, ftab, wtab, dtab): - '''replace all occurrences of colors, width and dashes by provided values.''' - ntab = [] - in_text_block = False # if True do nothing - for i in range(len(tab)): - ntab.append(tab[i]) # store in output - if tab[i] == b"BT": # begin of text block - in_text_block = True # switch on - continue - if tab[i] == b"ET": # end of text block - in_text_block = False # switch off - continue - if in_text_block: # next token if in text block - continue - if tab[i] == b"Do": # another XObject invoked - print("warning: skipping nested XObject call") - continue - if ftab and (tab[i] == b"s"): # fill color provided - ntab[-1] = b"b" # make sure it is used - continue - if ctab: # stroke color provided - if tab[i] == b"G": # it is a gray - del ntab[-2:] - ntab.extend(ctab) - continue - elif tab[i] == b"RG": # it is RGB - del ntab[len(ntab)-4:] - ntab.extend(ctab) - continue - elif tab[i] == b"K": # it is CMYK - del ntab[len(ntab)-5:] - ntab.extend(ctab) - continue - if ftab: # fill color provided - if tab[i] == b"g": # it is a gray - del ntab[-2:] - ntab.extend(ftab) - continue - elif tab[i] == b"rg": # it is RGB - del ntab[len(ntab)-4:] - ntab.extend(ftab) - continue - elif tab[i] == b"k": # it is CMYK - del ntab[len(ntab)-5:] - ntab.extend(ftab) - continue - if wtab: # width value provided - if tab[i] == b"w": - ntab[-2] = wtab[0] - continue - if dtab: # dashes provided - if tab[i] == b"d": - j = len(ntab) - 1 - x = b"d" - while not x.startswith(b"["): # search start of array - j -= 1 - x = ntab[j] - del ntab[j:] - ntab.extend(dtab) - return ntab - - # the following annot types are handled elsewhere - if annot.type[0] in range(2, 8): - return - ap = annot._getAP() # get appearance text if present - if not ap: - return - aptab = ap.split() # decompose into a list - - # prepare width, colors and dashes lists - # fill color - cols = annot.colors.get("fill", []) - ftab = [] - for c in cols: - ftab.append(b"%g" % c) - l = len(cols) - if l == 4: ftab.append(b"k") - elif l == 3: ftab.append(b"rg") - elif l == 1: ftab.append(b"g") - - # stroke color - cols = annot.colors.get("stroke", []) - ctab = [] - for c in cols: - ctab.append(b"%g" % c) - l = len(cols) - if l == 4: ctab.append(b"K") - elif l == 3: ctab.append(b"RG") - elif l == 1: ctab.append(b"G") - - # border width - c = annot.border.get("width") - wtab = [] - if c: - wtab = [b"%g" % c, b"w"] - - # dash pattern - c = annot.border.get("dashes") - dtab = [] - if c: - dtab = [b"[", b"]0 d"] - for n in c: - if n == 0: - break - dtab[0] += b"%i " % n - - outlist = ftab + ctab + wtab + dtab - if not outlist: - return - outlist = [b"q"] + outlist - # make sure we insert behind a leading "save graphics state" - while aptab[0] == b"q": - aptab = aptab[1:] - # now change every color, width and dashes spec - aptab = modAP(aptab, ctab, ftab, wtab, dtab) - aptab = outlist + aptab - ap = b" ".join(aptab) - annot._setAP(ap) - return - #---------------------------------------------------------------------- # Name: wx.lib.colourdb.py # Purpose: Adds a bunch of colour names and RGB values to the @@ -1975,7 +1657,7 @@ def getColorHSV(name): # Document.getCharWidths #------------------------------------------------------------------------------ def getCharWidths(doc, xref, limit = 256, idx = 0): - fontinfo = fitz.CheckFontInfo(doc, xref) + fontinfo = CheckFontInfo(doc, xref) if fontinfo is None: name, ext, stype, _ = doc.extractFont(xref, info_only = True) glyphs = None @@ -2009,7 +1691,7 @@ def getCharWidths(doc, xref, limit = 256, idx = 0): glyphs = new_glyphs fontdict["glyphs"] = glyphs fontinfo[1] = fontdict - fitz.UpdateFontInfo(doc, fontinfo) + UpdateFontInfo(doc, fontinfo) return glyphs #------------------------------------------------------------------------------ @@ -2041,7 +1723,7 @@ def horizontal_angle(C, P): return alfa def __init__(self, page): - fitz.CheckParent(page) + CheckParent(page) self.page = page self.doc = page.parent if not self.doc.isPDF: @@ -2057,13 +1739,13 @@ def __init__(self, page): def updateRect(self, x): if self.rect is None: - if type(x) is fitz.Point: - self.rect = fitz.Rect(x, x) + if type(x) is Point: + self.rect = Rect(x, x) else: self.rect = x else: - if type(x) is fitz.Point: + if type(x) is Point: self.rect.x0 = min(self.rect.x0, x.x) self.rect.y0 = min(self.rect.y0, x.y) self.rect.x1 = max(self.rect.x1, x.x) @@ -2147,7 +1829,7 @@ def drawOval(self, rect): def drawCircle(self, center, radius): """Draw a circle given its center and radius. """ - assert radius > 1e-7, "radius must be postive" + assert radius > 1e-5, "radius must be postive" p1 = center - (radius, 0) return self.drawSector(center, p1, 360, fullSector = False) @@ -2175,7 +1857,7 @@ def drawSector(self, center, point, beta, fullSector = True): if not (self.lastPoint == point): self.contents += l3 % (point.x + self.x, h - point.y - self.y) self.lastPoint = point - Q = fitz.Point(0, 0) # just make sure it exists + Q = Point(0, 0) # just make sure it exists C = center P = point S = P - C # vector 'center' -> 'point' @@ -2185,10 +1867,10 @@ def drawSector(self, center, point, beta, fullSector = True): while abs(betar) > abs(w90): # draw 90 degree arcs q1 = C.x + math.cos(alfa + w90) * rad q2 = C.y + math.sin(alfa + w90) * rad - Q = fitz.Point(q1, q2) # the arc's end point + Q = Point(q1, q2) # the arc's end point r1 = C.x + math.cos(alfa + w45) * rad / math.cos(w45) r2 = C.y + math.sin(alfa + w45) * rad / math.cos(w45) - R = fitz.Point(r1, r2) # crossing point of tangents + R = Point(r1, r2) # crossing point of tangents kappah = (1 - math.cos(w45)) * 4 / 3 / abs(R - Q) kappa = kappah * abs(P - Q) cp1 = P + (R - P) * kappa # control point 1 @@ -2204,10 +1886,10 @@ def drawSector(self, center, point, beta, fullSector = True): beta2 = betar / 2 q1 = C.x + math.cos(alfa + betar) * rad q2 = C.y + math.sin(alfa + betar) * rad - Q = fitz.Point(q1, q2) # the arc's end point + Q = Point(q1, q2) # the arc's end point r1 = C.x + math.cos(alfa + beta2) * rad / math.cos(beta2) r2 = C.y + math.sin(alfa + beta2) * rad / math.cos(beta2) - R = fitz.Point(r1, r2) # crossing point of tangents + R = Point(r1, r2) # crossing point of tangents # kappa height is 4/3 of segment height kappah = (1 - math.cos(beta2)) * 4 / 3 / abs(R - Q) # kappa height kappa = kappah * abs(P - Q) / (1 - math.cos(betar)) @@ -2226,12 +1908,13 @@ def drawSector(self, center, point, beta, fullSector = True): def drawRect(self, rect): """Draw a rectangle. """ - self.contents += "%g %g %g %g re\n" % (rect.x0 + self.x, - self.height - rect.y1 - self.y, - rect.width, rect.height) - self.updateRect(rect) - self.lastPoint = rect.tl - return rect.tl + r = +rect + self.contents += "%g %g %g %g re\n" % (r.x0 + self.x, + self.height - r.y1 - self.y, + r.width, r.height) + self.updateRect(r) + self.lastPoint = r.tl + return r.tl def drawZigzag(self, p1, p2, breadth = 2): """Draw a zig-zagged line from p1 to p2. @@ -2248,15 +1931,15 @@ def drawZigzag(self, p1, p2, breadth = 2): points = [] # stores edges for i in range (1, cnt): if i % 4 == 1: # point "above" connection - p = fitz.Point(i, -1) * mb + p = Point(i, -1) * mb elif i % 4 == 3: # point "below" connection - p = fitz.Point(i, 1) * mb + p = Point(i, 1) * mb else: # ignore others continue r = abs(p) p /= r # now p = (cos, sin) # this is the point rotated by alfa - np = fitz.Point((p.x + self.x) * calfa - (p.y + self.y) * salfa, + np = Point((p.x + self.x) * calfa - (p.y + self.y) * salfa, (p.y + self.y) * calfa + (p.x + self.x) * salfa) * r points.append(p1 + np) self.drawPolyline([p1] + points + [p2]) # add start and end points @@ -2278,15 +1961,15 @@ def drawSquiggle(self, p1, p2, breadth = 2): points = [] # stores edges for i in range (1, cnt): if i % 4 == 1: # point "above" connection - p = fitz.Point(i, -k) * mb + p = Point(i, -k) * mb elif i % 4 == 3: # point "below" connection - p = fitz.Point(i, k) * mb + p = Point(i, k) * mb else: # else on connection - p = fitz.Point(i, 0) * mb + p = Point(i, 0) * mb r = abs(p) p /= r # now p = (cos, sin) # this is the point rotated by alfa - np = fitz.Point((p.x + self.x) * calfa - (p.y + self.y) * salfa, + np = Point((p.x + self.x) * calfa - (p.y + self.y) * salfa, (p.y + self.y) * calfa + (p.x + self.x) * salfa) * r points.append(p1 + np) points = [p1] + points + [p2] @@ -2330,14 +2013,14 @@ def insertText(self, point, buffer, fname = "Helvetica" if fname[0] == "/": fname = fname[1:] - f = fitz.CheckFont(self.page, fname) + f = CheckFont(self.page, fname) if f is not None: xref = f[0] else: xref = self.page.insertFont(fontname = fname, fontfile = fontfile, set_simple = set_simple, idx = idx) - f = fitz.CheckFont(self.page, fname) + f = CheckFont(self.page, fname) assert xref > 0, "invalid fontname" basename, ext, stype, _ = self.doc.extractFont(xref, info_only = True) @@ -2351,11 +2034,11 @@ def insertText(self, point, buffer, tab = [] for t in text: - tab.append(fitz.getTJstr(t, glyphs)) + tab.append(getTJstr(t, glyphs)) text = tab - fitz.CheckColor(color) - morphing = fitz.CheckMorph(morph) + CheckColor(color) + morphing = CheckMorph(morph) rot = rotate assert rot % 90 == 0, "rotate not multiple of 90" while rot < 0: rot += 360 @@ -2372,7 +2055,7 @@ def insertText(self, point, buffer, # setting up for standard rotation directions # case rotate = 0 if morphing: - m1 = fitz.Matrix(1, 0, 0, 1, morph[0].x + self.x, + m1 = Matrix(1, 0, 0, 1, morph[0].x + self.x, height - morph[0].y - self.y) mat = ~m1 * morph[1] * m1 cm = "%g %g %g %g %g %g cm\n" % tuple(mat) @@ -2454,7 +2137,7 @@ def insertTextbox(self, rect, buffer, fontname = "Helvetica", fontfile = None, """ if rect.isEmpty or rect.isInfinite: raise ValueError("text box must be finite and not empty") - fitz.CheckColor(color) + CheckColor(color) assert rotate % 90 == 0, "rotate must be multiple of 90" rot = rotate while rot < 0: rot += 360 @@ -2476,7 +2159,7 @@ def insertTextbox(self, rect, buffer, fontname = "Helvetica", fontfile = None, fname = "Helvetica" if fname[0] == "/": fname = fname[1:] - f = fitz.CheckFont(self.page, fname) + f = CheckFont(self.page, fname) if f is None: raise ValueError("invalid font reference " + fname) @@ -2485,7 +2168,7 @@ def insertTextbox(self, rect, buffer, fontname = "Helvetica", fontfile = None, else: xref = self.page.insertFont(fontname = fname, fontfile = fontfile, set_simple = set_simple, idx = idx) - f = fitz.CheckFont(self.page, fname) + f = CheckFont(self.page, fname) assert xref > 0, "invalid fontname" basename, ext, stype, _ = self.doc.extractFont(xref, info_only = True) @@ -2517,8 +2200,8 @@ def pixlen(x): blen = widthtab[32][1] * fontsize # pixel size of space character text = "" # output buffer lheight = fontsize * 1.2 # line height - if fitz.CheckMorph(morph): - m1 = fitz.Matrix(1, 0, 0, 1, morph[0].x + self.x, + if CheckMorph(morph): + m1 = Matrix(1, 0, 0, 1, morph[0].x + self.x, self.height - morph[0].y - self.y) mat = ~m1 * morph[1] * m1 cm = "%g %g %g %g %g %g cm\n" % tuple(mat) @@ -2529,7 +2212,7 @@ def pixlen(x): # adjust for text orientation / rotation #--------------------------------------------------------------------------- progr = 1 # direction of line progress - c_pnt = fitz.Point(0, fontsize) # used for line progress + c_pnt = Point(0, fontsize) # used for line progress if rot == 0: # normal orientation point = rect.tl + c_pnt # line 1 is 'fontsize' below top pos = point.y + self.y # y of first line @@ -2537,7 +2220,7 @@ def pixlen(x): maxpos = rect.y1 + self.y # lines must not be below this elif rot == 90: # rotate counter clockwise - c_pnt = fitz.Point(fontsize, 0) # progress in x-direction + c_pnt = Point(fontsize, 0) # progress in x-direction point = rect.bl + c_pnt # line 1 'fontsize' away from left pos = point.x + self.x # position of first line maxwidth = rect.height # pixels available in one line @@ -2545,7 +2228,7 @@ def pixlen(x): cm += cmp90 elif rot == 180: # text upside down - c_pnt = -fitz.Point(0, fontsize) # progress upwards in y direction + c_pnt = -Point(0, fontsize) # progress upwards in y direction point = rect.br + c_pnt # line 1 'fontsize' above bottom pos = point.y + self.y # position of first line maxwidth = rect.width # pixels available in one line @@ -2554,7 +2237,7 @@ def pixlen(x): cm += cm180 else: # rotate clockwise (270 or -90) - c_pnt = -fitz.Point(fontsize, 0) # progress from right to left + c_pnt = -Point(fontsize, 0) # progress from right to left point = rect.tr + c_pnt # line 1 'fontsize' left of right pos = point.x + self.x # position of first line maxwidth = rect.height # pixels available in one line @@ -2634,14 +2317,14 @@ def pixlen(x): pnt = point + c_pnt * (i * 1.2) # text start of line if align == 1: # center: right shift by half width if rot in (0, 180): - pnt = pnt + fitz.Point(pl / 2, 0) * progr + pnt = pnt + Point(pl / 2, 0) * progr else: - pnt = pnt - fitz.Point(0, pl / 2) * progr + pnt = pnt - Point(0, pl / 2) * progr elif align == 2: # right: right shift by full width if rot in (0, 180): - pnt = pnt + fitz.Point(pl, 0) * progr + pnt = pnt + Point(pl, 0) * progr else: - pnt = pnt - fitz.Point(0, pl) * progr + pnt = pnt - Point(0, pl) * progr elif align == 3: # justify spaces = t.count(" ") # number of spaces in line if spaces > 0 and just_tab[i]: # if any, and we may justify @@ -2660,7 +2343,7 @@ def pixlen(x): left = -pnt.x - self.x top = -height + pnt.y + self.y nres += templ % (left, top, fname, fontsize, - spacing, red, green, blue, fitz.getTJstr(t, glyphs)) + spacing, red, green, blue, getTJstr(t, glyphs)) nres += "ET Q\n" self.totalcont += nres @@ -2679,8 +2362,8 @@ def finish(self, width = 1, """ if self.contents == "": # treat empty contents as no-op return - fitz.CheckColor(color) - fitz.CheckColor(fill) + CheckColor(color) + CheckColor(fill) self.contents += "%g w\n%i J\n%i j\n" % (width, roundCap, roundCap) if dashes is not None and len(dashes) > 0: @@ -2697,8 +2380,8 @@ def finish(self, width = 1, else: self.contents += "S\n" self.totalcont += "\nn q\n" - if fitz.CheckMorph(morph): - m1 = fitz.Matrix(1, 0, 0, 1, morph[0].x + self.x, + if CheckMorph(morph): + m1 = Matrix(1, 0, 0, 1, morph[0].x + self.x, self.height - morph[0].y - self.y) mat = ~m1 * morph[1] * m1 self.totalcont += "%g %g %g %g %g %g cm\n" % tuple(mat) @@ -2710,7 +2393,7 @@ def finish(self, width = 1, def commit(self, overlay = True): """Update the page's /Contents object with Shape data. The argument controls, whether data appear in foreground (True, default) or background. """ - fitz.CheckParent(self.page) # doc may have died meanwhile + CheckParent(self.page) # doc may have died meanwhile if self.totalcont == "": return if not self.totalcont.endswith("Q\n"): diff --git a/fitz/version.i b/fitz/version.i index 58a0afb8a..325c0d6b0 100644 --- a/fitz/version.i +++ b/fitz/version.i @@ -1,6 +1,6 @@ %pythoncode %{ -VersionFitz = "1.13.0" -VersionBind = "1.13.20" -VersionDate = "2018-09-09 09:54:14" -version = (VersionBind, VersionFitz, "20180909095414") +VersionFitz = "1.14.0" +VersionBind = "1.14.0" +VersionDate = "2018-11-16 05:14:22" +version = (VersionBind, VersionFitz, "20181116051422") %} \ No newline at end of file diff --git a/setup.py b/setup.py index 016404d08..86c930050 100644 --- a/setup.py +++ b/setup.py @@ -10,10 +10,10 @@ '/usr/local/include/mupdf', '/usr/local/thirdparty/zlib', ], - #library_dirs=[''], + #library_dirs=['/usr/local/lib'], libraries=[ 'mupdf', - 'mupdfthird', + 'mupdf-third', # 'jbig2dec', 'openjp2', 'jpeg', 'freetype', # 'crypto', #openssl is required by mupdf on archlinux ], # the libraries to link with @@ -26,7 +26,7 @@ '/usr/local/include', '/usr/local/thirdparty/zlib'], library_dirs=['/usr/local/lib'], - libraries=['mupdf', 'mupdfthird'] + libraries=['mupdf', 'mupdf-third'] ) else: @@ -52,7 +52,7 @@ sources=['./fitz/fitz_wrap.c',]) setup(name = 'PyMuPDF', - version = "1.13.20", + version = "1.14.0", description = 'Python bindings for the PDF rendering library MuPDF', classifiers = ['Development Status :: 5 - Production/Stable', 'Environment :: Console',