Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2556 #2564

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions fitz/helper-devices.i
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ jm_lineart_path(fz_context *ctx, jm_lineart_device *dev, const fz_path *path)
DICT_SETITEM_DROP(dev_pathdict, dictkey_items, PyList_New(0));
fz_walk_path(ctx, path, &trace_path_walker, dev);
// Check if any items were added ...
if (!PyList_Size(PyDict_GetItem(dev_pathdict, dictkey_items))) {
if (!PyDict_GetItem(dev_pathdict, dictkey_items) || !PyList_Size(PyDict_GetItem(dev_pathdict, dictkey_items))) {
Py_CLEAR(dev_pathdict);
}
}
Expand Down Expand Up @@ -468,6 +468,9 @@ jm_lineart_clip_path(fz_context *ctx, fz_device *dev_, const fz_path *path, int
trace_device_ctm = ctm; //fz_concat(ctm, trace_device_ptm);
path_type = CLIP_PATH;
jm_lineart_path(ctx, dev, path);
if (!dev_pathdict) {
return;
}
DICT_SETITEM_DROP(dev_pathdict, dictkey_type, PyUnicode_FromString("clip"));
DICT_SETITEMSTR_DROP(dev_pathdict, "even_odd", JM_BOOL(even_odd));
if (!PyDict_GetItemString(dev_pathdict, "closePath")) {
Expand All @@ -489,6 +492,9 @@ jm_lineart_clip_stroke_path(fz_context *ctx, fz_device *dev_, const fz_path *pat
trace_device_ctm = ctm; //fz_concat(ctm, trace_device_ptm);
path_type = CLIP_STROKE_PATH;
jm_lineart_path(ctx, dev, path);
if (!dev_pathdict) {
return;
}
DICT_SETITEM_DROP(dev_pathdict, dictkey_type, PyUnicode_FromString("clip"));
DICT_SETITEMSTR_DROP(dev_pathdict, "even_odd", Py_BuildValue("s", NULL));
if (!PyDict_GetItemString(dev_pathdict, "closePath")) {
Expand Down Expand Up @@ -601,9 +607,11 @@ jm_trace_text_span(fz_context *ctx, PyObject *out, fz_text_span *span, int type,
const char *fontname = JM_font_name(ctx, span->font);
float rgb[3];
PyObject *chars = PyTuple_New(span->len);
fz_matrix join = fz_concat(span->trm, ctm);
fz_point dir = fz_transform_vector(fz_make_point(1, 0), join);
double fsize = sqrt(fabs((double) span->trm.a * (double) span->trm.d));
fz_matrix mat = fz_concat(span->trm, ctm); // text transformation matrix
fz_point dir = fz_transform_vector(fz_make_point(1, 0), mat); // writing direction
dir = fz_normalize_vector(dir);

double fsize = sqrt(fabs((double) span->trm.a * (double) span->trm.d)); // font size
double linewidth, adv, asc, dsc;
double space_adv = 0;
float x0, y0, x1, y1;
Expand All @@ -613,35 +621,35 @@ jm_trace_text_span(fz_context *ctx, PyObject *out, fz_text_span *span, int type,
dsc = -0.1;
asc = 0.9;
}

// compute effective ascender / descender
double ascsize = asc * fsize / (asc - dsc);
double dscsize = dsc * fsize / (asc - dsc);
int fflags = 0;

int fflags = 0; // font flags
int mono = fz_font_is_monospaced(ctx, span->font);
fflags += mono * TEXT_FONT_MONOSPACED;
fflags += fz_font_is_italic(ctx, span->font) * TEXT_FONT_ITALIC;
fflags += fz_font_is_serif(ctx, span->font) * TEXT_FONT_SERIFED;
fflags += fz_font_is_bold(ctx, span->font) * TEXT_FONT_BOLD;
fz_matrix mat = trace_device_ptm;
fz_matrix ctm_rot = fz_concat(ctm, trace_device_rot);
mat = fz_concat(mat, ctm_rot);

if (dev_linewidth > 0) {
if (dev_linewidth > 0) { // width of character border
linewidth = (double) dev_linewidth;
} else {
linewidth = fsize * 0.05;
linewidth = fsize * 0.05; // default: 5% of font size
}
fz_point char_orig;
double last_adv = 0;

// walk through characters of span
fz_rect span_bbox;
dir = fz_normalize_vector(dir);
fz_matrix rot = fz_make_matrix(dir.x, dir.y, -dir.y, dir.x, 0, 0);
if (dir.x == -1) { // left-right flip
rot.d = 1;
}

//PySys_WriteStdout("mat: (%g, %g, %g, %g)\n", mat.a, mat.b, mat.c, mat.d);
//PySys_WriteStdout("rot: (%g, %g, %g, %g)\n", rot.a, rot.b, rot.c, rot.d);

for (i = 0; i < span->len; i++) {
adv = 0;
if (span->items[i].gid >= 0) {
Expand All @@ -653,14 +661,14 @@ jm_trace_text_span(fz_context *ctx, PyObject *out, fz_text_span *span, int type,
space_adv = adv;
}
char_orig = fz_make_point(span->items[i].x, span->items[i].y);
char_orig.y = trace_device_ptm.f - char_orig.y;
char_orig = fz_transform_point(char_orig, mat);
char_orig = fz_transform_point(char_orig, ctm);
fz_matrix m1 = fz_make_matrix(1, 0, 0, 1, -char_orig.x, -char_orig.y);
m1 = fz_concat(m1, rot);
m1 = fz_concat(m1, fz_make_matrix(1, 0, 0, 1, char_orig.x, char_orig.y));
x0 = char_orig.x;
x1 = x0 + adv;
if (dir.x == 1 && span->trm.d < 0) { // up-down flip
if (mat.d > 0 && (dir.x == 1 || dir.x == -1) ||
mat.b !=0 && mat.b == -mat.c) { // up-down flip
y0 = char_orig.y + dscsize;
y1 = char_orig.y + ascsize;
} else {
Expand Down Expand Up @@ -688,7 +696,7 @@ jm_trace_text_span(fz_context *ctx, PyObject *out, fz_text_span *span, int type,
space_adv = last_adv;
}
} else {
space_adv = last_adv; // for mono fonts this suffices
space_adv = last_adv; // for mono, any char width suffices
}
}
// make the span dictionary
Expand All @@ -701,24 +709,25 @@ jm_trace_text_span(fz_context *ctx, PyObject *out, fz_text_span *span, int type,
DICT_SETITEMSTR_DROP(span_dict, "bidi_dir", PyLong_FromLong((long) span->markup_dir));
DICT_SETITEM_DROP(span_dict, dictkey_ascender, PyFloat_FromDouble(asc));
DICT_SETITEM_DROP(span_dict, dictkey_descender, PyFloat_FromDouble(dsc));
DICT_SETITEM_DROP(span_dict, dictkey_colorspace, PyLong_FromLong(3));

if (colorspace) {
fz_convert_color(ctx, colorspace, color, fz_device_rgb(ctx),
rgb, NULL, fz_default_color_params);
DICT_SETITEM_DROP(span_dict, dictkey_colorspace, PyLong_FromLong(3));
DICT_SETITEM_DROP(span_dict, dictkey_color, Py_BuildValue("fff", rgb[0], rgb[1], rgb[2]));
} else {
DICT_SETITEM_DROP(span_dict, dictkey_colorspace, PyLong_FromLong(1));
DICT_SETITEM_DROP(span_dict, dictkey_color, PyFloat_FromDouble(1));
rgb[0] = rgb[1] = rgb[2] = 0;
}

DICT_SETITEM_DROP(span_dict, dictkey_color, Py_BuildValue("fff", rgb[0], rgb[1], rgb[2]));
DICT_SETITEM_DROP(span_dict, dictkey_size, PyFloat_FromDouble(fsize));
DICT_SETITEMSTR_DROP(span_dict, "opacity", PyFloat_FromDouble((double) alpha));
DICT_SETITEMSTR_DROP(span_dict, "linewidth", PyFloat_FromDouble((double) linewidth));
DICT_SETITEMSTR_DROP(span_dict, "spacewidth", PyFloat_FromDouble(space_adv));
DICT_SETITEM_DROP(span_dict, dictkey_type, PyLong_FromLong((long) type));
DICT_SETITEM_DROP(span_dict, dictkey_chars, chars);
DICT_SETITEM_DROP(span_dict, dictkey_bbox, JM_py_from_rect(span_bbox));
DICT_SETITEMSTR_DROP(span_dict, "layer", JM_EscapeStrFromStr(layer_name));
DICT_SETITEMSTR_DROP(span_dict, "seqno", PyLong_FromSize_t(seqno));
DICT_SETITEM_DROP(span_dict, dictkey_chars, chars);
LIST_APPEND_DROP(out, span_dict);
}

Expand Down
Binary file added tests/resources/test_2533.pdf
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/test_drawings.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,17 @@ def test_2462():
doc = fitz.open(f"{scriptdir}/resources/test-2462.pdf")
page = doc[0]
vg = page.get_drawings(extended=True)


def test_2556():
"""Ensure that incomplete clip paths will be properly ignored."""
doc = fitz.open() # new empty PDF
page = doc.new_page() # new page
# following contains an incomplete clip
c = b"q 50 697.6 400 100.0 re W n q 0 0 m W n Q "
xref = doc.get_new_xref() # prepare /Contents object for page
doc.update_object(xref,"<<>>") # new xref now is a dictionary
doc.update_stream(xref, c) # store drawing commands
page.set_contents(xref) # give the page this xref as /Contents
# following will bring down interpreter if fix not installed
assert page.get_drawings(extended=True)
18 changes: 18 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,24 @@ def test_texttrace():
print( f'page {i} json:\n{json.dumps(tt, indent=" ")}', file=f)


def test_2533():
"""Assert correct char bbox in page.get_texttrace().

Search for a unique char on page and confirm that page.get_texttrace()
returns the same bbox as the search method.
"""
doc = fitz.open(os.path.join(scriptdir, "resources", "test_2533.pdf"))
page = doc[0]
NEEDLE = "民"
ord_NEEDLE = ord(NEEDLE)
for span in page.get_texttrace():
for char in span["chars"]:
if char[0] == ord_NEEDLE:
bbox = fitz.Rect(char[3])
break
assert page.search_for(NEEDLE)[0] == bbox


def test_2506():
"""Ensure expected font size across text writing angles."""
doc = fitz.open()
Expand Down
Loading