diff --git a/fitz/helper-devices.i b/fitz/helper-devices.i index 949a70dac..7d9e92396 100644 --- a/fitz/helper-devices.i +++ b/fitz/helper-devices.i @@ -610,9 +610,9 @@ jm_trace_text_span(fz_context *ctx, PyObject *out, fz_text_span *span, int type, PyObject *chars = PyTuple_New(span->len); 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(dir.x * dir.x + dir.y * dir.y); - double fsize = sqrt(fabs((double) span->trm.a * (double) span->trm.d)); // font size + dir = fz_normalize_vector(dir); double linewidth, adv, asc, dsc; double space_adv = 0; float x0, y0, x1, y1; diff --git a/src/__init__.py b/src/__init__.py index fcda0fbb1..970239096 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -17895,9 +17895,9 @@ def jm_trace_text_span(dev, span, type_, ctm, colorspace, color, alpha, seqno): mat = mupdf.fz_concat(span.trm(), ctm) # text transformation matrix dir = mupdf.fz_transform_vector(mupdf.fz_make_point(1, 0), mat) # writing direction - dir = mupdf.fz_normalize_vector(dir) + fsize = math.sqrt(dir.x * dir.x + dir.y * dir.y) # font size - fsize = math.sqrt(abs(span.trm().a * span.trm().d)) # font size + dir = mupdf.fz_normalize_vector(dir) space_adv = 0; asc = JM_font_ascender( span.font()) diff --git a/src/extra.i b/src/extra.i index 13a756530..f9ce90328 100644 --- a/src/extra.i +++ b/src/extra.i @@ -1997,9 +1997,9 @@ static void jm_trace_text_span( //double fsize = sqrt(fabs((double) span->trm.a * (double) span->trm.d)); fz_matrix mat = mupdf::ll_fz_concat(span->trm, ctm); // text transformation matrix fz_point dir = mupdf::ll_fz_transform_vector(mupdf::ll_fz_make_point(1, 0), mat); // writing direction - dir = mupdf::ll_fz_normalize_vector(dir); + double fsize = sqrt(dir.x * dir.x + dir.y * dir.y); // font size - double fsize = sqrt(fabs((double) span->trm.a * (double) span->trm.d)); // font size + dir = mupdf::ll_fz_normalize_vector(dir); // compute effective ascender / descender double asc = (double) JM_font_ascender(span->font); diff --git a/tests/resources/test_2645_1.pdf b/tests/resources/test_2645_1.pdf new file mode 100644 index 000000000..5c177a094 Binary files /dev/null and b/tests/resources/test_2645_1.pdf differ diff --git a/tests/resources/test_2645_2.pdf b/tests/resources/test_2645_2.pdf new file mode 100644 index 000000000..0a9317a52 Binary files /dev/null and b/tests/resources/test_2645_2.pdf differ diff --git a/tests/resources/test_2645_3.pdf b/tests/resources/test_2645_3.pdf new file mode 100644 index 000000000..199089988 Binary files /dev/null and b/tests/resources/test_2645_3.pdf differ diff --git a/tests/test_general.py b/tests/test_general.py index 76e62241d..e7dcdb517 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -277,6 +277,21 @@ def test_2533(): assert page.search_for(NEEDLE)[0] == bbox +def test_2645(): + """Assert same font size calculation in corner cases. + """ + folder = os.path.join(scriptdir, "resources") + files = ("test_2645_1.pdf", "test_2645_2.pdf", "test_2645_3.pdf") + for f in files: + doc = fitz.open(os.path.join(folder, f)) + page = doc[0] + fontsize0 = page.get_texttrace()[0]["size"] + fontsize1 = page.get_text("dict", flags=fitz.TEXTFLAGS_TEXT)["blocks"][0]["lines"][ + 0 + ]["spans"][0]["size"] + assert abs(fontsize0 - fontsize1) < 1e-5 + + def test_2506(): """Ensure expected font size across text writing angles.""" doc = fitz.open()