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

Method center_rect

src/__init__.py:10683–10715  ·  view source on GitHub ↗

Calculate minimal sub-rectangle for the overlay text. Notes: Because 'insert_textbox' supports no vertical text centering, we calculate an approximate number of lines here and return a sub-rect with smaller height, which should still be su

(annot_rect, new_text, font, fsize)

Source from the content-addressed store, hash-verified

10681 """
10682
10683 def center_rect(annot_rect, new_text, font, fsize):
10684 """Calculate minimal sub-rectangle for the overlay text.
10685
10686 Notes:
10687 Because 'insert_textbox' supports no vertical text centering,
10688 we calculate an approximate number of lines here and return a
10689 sub-rect with smaller height, which should still be sufficient.
10690 Args:
10691 annot_rect: the annotation rectangle
10692 new_text: the text to insert.
10693 font: the fontname. Must be one of the CJK or Base-14 set, else
10694 the rectangle is returned unchanged.
10695 fsize: the fontsize
10696 Returns:
10697 A rectangle to use instead of the annot rectangle.
10698 """
10699 if not new_text or annot_rect.width <= EPSILON:
10700 return annot_rect
10701 try:
10702 text_width = get_text_length(new_text, font, fsize)
10703 except (ValueError, mupdf.FzErrorBase): # unsupported font
10704 if g_exceptions_verbose:
10705 exception_info()
10706 return annot_rect
10707 line_height = fsize * 1.2
10708 limit = annot_rect.width
10709 h = math.ceil(text_width / limit) * line_height # estimate rect height
10710 if h >= annot_rect.height:
10711 return annot_rect
10712 r = annot_rect
10713 y = (annot_rect.tl.y + annot_rect.bl.y - h) * 0.5
10714 r.y0 = y
10715 return r
10716
10717 CheckParent(page)
10718 doc = page.parent

Callers

nothing calls this directly

Calls 2

get_text_lengthFunction · 0.85
exception_infoFunction · 0.85

Tested by

no test coverage detected