Skip to content

Commit

Permalink
Fixing #2703 (Discussion #2645)
Browse files Browse the repository at this point in the history
In some cases the matrix concatenation of TRM and CTM must be used to find the right font size.
We previously only looked at TRM.
  • Loading branch information
JorjMcKie committed Oct 2, 2023
1 parent 90d3598 commit c57dc86
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions fitz/helper-devices.i
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions src/extra.i
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Binary file added tests/resources/test_2645_1.pdf
Binary file not shown.
Binary file added tests/resources/test_2645_2.pdf
Binary file not shown.
Binary file added tests/resources/test_2645_3.pdf
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c57dc86

Please sign in to comment.