@@ -2385,7 +2385,7 @@ def insert_htmlbox(
23852385 oc = 0 ,
23862386 opacity = 1 ,
23872387 overlay = True ,
2388- ) -> float :
2388+ ) -> tuple :
23892389 """Insert text with optional HTML tags and stylings into a rectangle.
23902390
23912391 Args:
@@ -2448,16 +2448,21 @@ def insert_htmlbox(
24482448 if not fit .big_enough : # there was no fit
24492449 return (- 1 , scale_low )
24502450
2451- filled = fit .filled
2452- scale = 1 / fit .parameter # shrink factor
2453-
2454- spare_height = fit .rect .y1 - filled [3 ] # unused room at rectangle bottom
2455- # Note: due to MuPDF's logic this may be negative even for successful fits.
2456- if scale != 1 or spare_height < 0 : # if scaling occurred, set spare_height to 0
2451+ filled = pymupdf .Rect (fit .filled )
2452+ # final adjustment if filled rect is wider than fit rect
2453+ if filled .width > fit .rect .width :
2454+ h = filled .width / fit .rect .width * fit .rect .height
2455+ fit .rect .x1 = filled .x1
2456+ fit .rect .y1 = h
2457+ fit .parameter = fit .rect .x1 / temp_rect .x1
2458+
2459+ scale = 1 / fit .parameter
2460+ spare_height = max ((fit .rect .y1 - filled .y1 ) / fit .parameter , 0 )
2461+ if scale != 1 : # if scaling occurred, set spare_height to 0
24572462 spare_height = 0
24582463
24592464 def rect_function (* args ):
2460- return fit .rect , fit .rect , pymupdf . Identity
2465+ return fit .rect , fit .rect , None
24612466
24622467 # draw story on temp PDF page
24632468 doc = story .write_with_links (rect_function )
@@ -2477,7 +2482,7 @@ def rect_function(*args):
24772482 # -------------------------------------------------------------------------
24782483 # re-insert links in target rect (show_pdf_page cannot copy annotations)
24792484 # -------------------------------------------------------------------------
2480- # scaled center point of fit. rect
2485+ # scaled center point of fit rect
24812486 mp1 = (fit .rect .tl + fit .rect .br ) / 2 * scale
24822487
24832488 # center point of target rect
0 commit comments