MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / JM_char_quad

Function JM_char_quad

src/__init__.py:18808–18894  ·  view source on GitHub ↗

re-compute char quad if ascender/descender values make no sense

(line, ch)

Source from the content-addressed store, hash-verified

18806
18807
18808def JM_char_quad(line, ch):
18809 '''
18810 re-compute char quad if ascender/descender values make no sense
18811 '''
18812 if 1 and g_use_extra:
18813 # This reduces time taken to extract text from PyMuPDF.pdf from 20s to
18814 # 15s.
18815 return mupdf.FzQuad(extra.JM_char_quad( line.m_internal, ch.m_internal))
18816
18817 assert isinstance(line, mupdf.FzStextLine)
18818 assert isinstance(ch, mupdf.FzStextChar)
18819 if _globals.skip_quad_corrections: # no special handling
18820 return ch.quad
18821 if line.m_internal.wmode: # never touch vertical write mode
18822 return ch.quad
18823 font = mupdf.FzFont(mupdf.ll_fz_keep_font(ch.m_internal.font))
18824 asc = JM_font_ascender(font)
18825 dsc = JM_font_descender(font)
18826 fsize = ch.m_internal.size
18827 asc_dsc = asc - dsc + FLT_EPSILON
18828 if asc_dsc >= 1 and _globals.small_glyph_heights == 0: # no problem
18829 return mupdf.FzQuad(ch.m_internal.quad)
18830
18831 # Re-compute quad with adjusted ascender / descender values:
18832 # Move ch->origin to (0,0) and de-rotate quad, then adjust the corners,
18833 # re-rotate and move back to ch->origin location.
18834 fsize = ch.m_internal.size
18835 bbox = mupdf.fz_font_bbox(font)
18836 fwidth = bbox.x1 - bbox.x0
18837 if asc < 1e-3: # probably Tesseract glyphless font
18838 dsc = -0.1
18839 asc = 0.9
18840 asc_dsc = 1.0
18841
18842 if _globals.small_glyph_heights or asc_dsc < 1:
18843 dsc = dsc / asc_dsc
18844 asc = asc / asc_dsc
18845 asc_dsc = asc - dsc
18846 asc = asc * fsize / asc_dsc
18847 dsc = dsc * fsize / asc_dsc
18848
18849 # Re-compute quad with the adjusted ascender / descender values:
18850 # Move ch->origin to (0,0) and de-rotate quad, then adjust the corners,
18851 # re-rotate and move back to ch->origin location.
18852 c = line.m_internal.dir.x # cosine
18853 s = line.m_internal.dir.y # sine
18854 trm1 = mupdf.fz_make_matrix(c, -s, s, c, 0, 0) # derotate
18855 trm2 = mupdf.fz_make_matrix(c, s, -s, c, 0, 0) # rotate
18856 if (c == -1): # left-right flip
18857 trm1.d = 1
18858 trm2.d = 1
18859 xlate1 = mupdf.fz_make_matrix(1, 0, 0, 1, -ch.m_internal.origin.x, -ch.m_internal.origin.y)
18860 xlate2 = mupdf.fz_make_matrix(1, 0, 0, 1, ch.m_internal.origin.x, ch.m_internal.origin.y)
18861
18862 quad = mupdf.fz_transform_quad(mupdf.FzQuad(ch.m_internal.quad), xlate1) # move origin to (0,0)
18863 quad = mupdf.fz_transform_quad(quad, trm1) # de-rotate corners
18864
18865 # adjust vertical coordinates

Callers 2

JM_char_bboxFunction · 0.85
on_highlight_charFunction · 0.85

Calls 2

JM_font_ascenderFunction · 0.85
JM_font_descenderFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…