Skip to content

Commit

Permalink
Several Bug Fixes
Browse files Browse the repository at this point in the history
Fix #2110 (Discussion item #2111):
File `__main__.py` - also include the font's xref in the generated file name.

Fix #2094:
File `helper-device.i' - also ensure equality of x coordinates of relevant corners before assuming a rectangle.

Fix #2087:
File `fitz.i`- if JPX image format is already known, make sure to read the decoded image stream, instead of raw stream in the other cases.
  • Loading branch information
JorjMcKie authored and julian-smith-artifex-com committed Dec 12, 2022
1 parent 0a5476b commit 53a723f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion fitz/__main__.py
Expand Up @@ -512,7 +512,7 @@ def extract_objects(args):
if ext == "n/a" or not buffer:
continue
outname = os.path.join(
out_dir, fontname.replace(" ", "-") + "." + ext
out_dir, f"{fontname.replace(' ', '-')}-{xref}.{ext}"
)
outfile = open(outname, "wb")
outfile.write(buffer)
Expand Down
12 changes: 9 additions & 3 deletions fitz/fitz.i
Expand Up @@ -2775,14 +2775,16 @@ if len(pyliste) == 0 or min(pyliste) not in range(len(self)) or max(pyliste) not

if (pdf_is_jpx_image(gctx, obj)) {
img_type = FZ_IMAGE_JPX;
res = pdf_load_stream(gctx, obj);
ext = "jpx";
}
if (JM_is_jbig2_image(gctx, obj)) {
img_type = FZ_IMAGE_JBIG2;
res = pdf_load_stream(gctx, obj);
ext = "jb2";
}
res = pdf_load_raw_stream(gctx, obj);
if (img_type == FZ_IMAGE_UNKNOWN) {
res = pdf_load_raw_stream(gctx, obj);
unsigned char *c = NULL;
fz_buffer_storage(gctx, res, &c);
img_type = fz_recognize_image_format(gctx, c);
Expand All @@ -2795,9 +2797,10 @@ if len(pyliste) == 0 or min(pyliste) not in range(len(self)) or max(pyliste) not
res = fz_new_buffer_from_image_as_png(gctx, img,
fz_default_color_params);
ext = "png";
} else /*if (smask == 0)*/ {
} else {
img = fz_new_image_from_buffer(gctx, res);
}

fz_image_resolution(img, &xres, &yres);
width = img->w;
height = img->h;
Expand Down Expand Up @@ -2835,7 +2838,8 @@ if len(pyliste) == 0 or min(pyliste) not in range(len(self)) or max(pyliste) not

fz_catch(gctx) {
Py_CLEAR(rc);
Py_RETURN_NONE;
fz_warn(gctx, fz_caught_message(gctx));
Py_RETURN_FALSE;
}
if (!rc)
Py_RETURN_NONE;
Expand Down Expand Up @@ -12332,6 +12336,7 @@ struct Archive
}
return (struct Archive *) arch;
}

Archive(PyObject *a0=NULL, const char *path=NULL)
{
fz_archive *arch=NULL;
Expand Down Expand Up @@ -13566,6 +13571,7 @@ struct Story
return ret;
}


void draw( struct DeviceWrapper* device, PyObject* matrix=NULL)
{
fz_matrix ctm2 = JM_matrix_from_py( matrix);
Expand Down
44 changes: 15 additions & 29 deletions fitz/helper-devices.i
Expand Up @@ -101,6 +101,7 @@ jm_checkrect()
dev_linecount = 0; // reset line count
long orientation = 0;
fz_point ll, lr, ur, ul;
fz_rect r;
PyObject *rect;
PyObject *line0, *line2;
PyObject *items = PyDict_GetItem(dev_pathdict, dictkey_items);
Expand All @@ -109,50 +110,35 @@ jm_checkrect()
line0 = PyList_GET_ITEM(items, len - 3);
ll = JM_point_from_py(PyTuple_GET_ITEM(line0, 1));
lr = JM_point_from_py(PyTuple_GET_ITEM(line0, 2));

// no need to extract "line1"!
line2 = PyList_GET_ITEM(items, len - 1);
ur = JM_point_from_py(PyTuple_GET_ITEM(line2, 1));
ul = JM_point_from_py(PyTuple_GET_ITEM(line2, 2));

/*
---------------------------------------------------------------------
Three connected lines: at least a quad! Check whether even a rect.
For this, the lines must be parallel to the axes.
Assumption:
For decomposing rects, MuPDF always starts with a horizontal line,
followed by a vertical line, followed by a horizontal line.
We will also check orientation of the enclosed area and add this info
as '+1' for anti-clockwise, '-1' for clockwise orientation.
---------------------------------------------------------------------
*/
if (ll.y != lr.y) { // not horizontal
goto drop_out;
}
if (lr.x != ur.x) { // not vertical
goto drop_out;
}
if (ur.y != ul.y) { // not horizontal
goto drop_out;
if (ll.y != lr.y ||
ll.x != ul.x ||
ur.y != ul.y ||
ur.x != lr.x) {
goto drop_out; // not a rectangle
}
// we have a rect, determine orientation
if (ll.x < lr.x) { // move left to right
if (lr.y > ur.y) { // move upwards
orientation = 1;
} else {
orientation = -1;
}
} else { // move right to left
if (lr.y < ur.y) { // move downwards
orientation = 1;
} else {
orientation = -1;
}

// we have a rect, replace last 3 "l" items by one "re" item.
if (ul.y < lr.y) {
r = fz_make_rect(ul.x, ul.y, lr.x, lr.y);
orientation = 1;
} else {
r = fz_make_rect(ll.x, ll.y, ur.x, ur.y);
orientation = -1;
}
// Replace last 3 "l" items by one "re" item.
fz_rect r = fz_make_rect(ul.x, ul.y, ul.x, ul.y);
r = fz_include_point_in_rect(r, ur);
r = fz_include_point_in_rect(r, ll);
r = fz_include_point_in_rect(r, lr);
rect = PyTuple_New(3);
PyTuple_SET_ITEM(rect, 0, PyUnicode_FromString("re"));
PyTuple_SET_ITEM(rect, 1, JM_py_from_rect(r));
Expand Down

0 comments on commit 53a723f

Please sign in to comment.