@@ -2385,7 +2385,7 @@ def insert_htmlbox(
2385
2385
oc = 0 ,
2386
2386
opacity = 1 ,
2387
2387
overlay = True ,
2388
- ) -> float :
2388
+ ) -> tuple :
2389
2389
"""Insert text with optional HTML tags and stylings into a rectangle.
2390
2390
2391
2391
Args:
@@ -2448,16 +2448,21 @@ def insert_htmlbox(
2448
2448
if not fit .big_enough : # there was no fit
2449
2449
return (- 1 , scale_low )
2450
2450
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
2457
2462
spare_height = 0
2458
2463
2459
2464
def rect_function (* args ):
2460
- return fit .rect , fit .rect , pymupdf . Identity
2465
+ return fit .rect , fit .rect , None
2461
2466
2462
2467
# draw story on temp PDF page
2463
2468
doc = story .write_with_links (rect_function )
@@ -2477,7 +2482,7 @@ def rect_function(*args):
2477
2482
# -------------------------------------------------------------------------
2478
2483
# re-insert links in target rect (show_pdf_page cannot copy annotations)
2479
2484
# -------------------------------------------------------------------------
2480
- # scaled center point of fit. rect
2485
+ # scaled center point of fit rect
2481
2486
mp1 = (fit .rect .tl + fit .rect .br ) / 2 * scale
2482
2487
2483
2488
# center point of target rect
0 commit comments