@@ -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,18 @@ 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
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
2453
2458
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
2457
- spare_height = 0
2459
+ spare_height = max ((fit .rect .y1 - filled .y1 ) / fit .parameter , 0 )
2458
2460
2459
2461
def rect_function (* args ):
2460
- return fit .rect , fit .rect , pymupdf . Identity
2462
+ return fit .rect , fit .rect , None
2461
2463
2462
2464
# draw story on temp PDF page
2463
2465
doc = story .write_with_links (rect_function )
@@ -2477,7 +2479,8 @@ def rect_function(*args):
2477
2479
# -------------------------------------------------------------------------
2478
2480
# re-insert links in target rect (show_pdf_page cannot copy annotations)
2479
2481
# -------------------------------------------------------------------------
2480
- # scaled center point of fit.rect
2482
+ # scaled center point of fit rect
2483
+ scale = 1 / fit .parameter
2481
2484
mp1 = (fit .rect .tl + fit .rect .br ) / 2 * scale
2482
2485
2483
2486
# center point of target rect
0 commit comments