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

Function center_rect

src_classic/utils.py:4059–4089  ·  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 sufficient.

(annot_rect, text, font, fsize)

Source from the content-addressed store, hash-verified

4057 """
4058
4059 def center_rect(annot_rect, text, font, fsize):
4060 """Calculate minimal sub-rectangle for the overlay text.
4061
4062 Notes:
4063 Because 'insert_textbox' supports no vertical text centering,
4064 we calculate an approximate number of lines here and return a
4065 sub-rect with smaller height, which should still be sufficient.
4066 Args:
4067 annot_rect: the annotation rectangle
4068 text: the text to insert.
4069 font: the fontname. Must be one of the CJK or Base-14 set, else
4070 the rectangle is returned unchanged.
4071 fsize: the fontsize
4072 Returns:
4073 A rectangle to use instead of the annot rectangle.
4074 """
4075 if not text:
4076 return annot_rect
4077 try:
4078 text_width = get_text_length(text, font, fsize)
4079 except ValueError: # unsupported font
4080 return annot_rect
4081 line_height = fsize * 1.2
4082 limit = annot_rect.width
4083 h = math.ceil(text_width / limit) * line_height # estimate rect height
4084 if h >= annot_rect.height:
4085 return annot_rect
4086 r = annot_rect
4087 y = (annot_rect.tl.y + annot_rect.bl.y - h) * 0.5
4088 r.y0 = y
4089 return r
4090
4091 CheckParent(page)
4092 doc = page.parent

Callers 2

apply_redactionsMethod · 0.85
apply_redactionsFunction · 0.85

Calls 1

get_text_lengthFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…