Skip to content

Commit b4b963a

Browse files
committed
Address 4613
Ensure full width of each line is shown in target rectangle
1 parent 9dc88b7 commit b4b963a

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/utils.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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,18 @@ 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
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
24532458

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)
24582460

24592461
def rect_function(*args):
2460-
return fit.rect, fit.rect, pymupdf.Identity
2462+
return fit.rect, fit.rect, None
24612463

24622464
# draw story on temp PDF page
24632465
doc = story.write_with_links(rect_function)
@@ -2477,7 +2479,8 @@ def rect_function(*args):
24772479
# -------------------------------------------------------------------------
24782480
# re-insert links in target rect (show_pdf_page cannot copy annotations)
24792481
# -------------------------------------------------------------------------
2480-
# scaled center point of fit.rect
2482+
# scaled center point of fit rect
2483+
scale = 1 / fit.parameter
24812484
mp1 = (fit.rect.tl + fit.rect.br) / 2 * scale
24822485

24832486
# center point of target rect

tests/test_4613.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pymupdf
2+
import string
3+
4+
5+
def test_4613():
6+
text = " ".join([string.ascii_lowercase + " " + string.ascii_uppercase] * 3)
7+
story = pymupdf.Story(text)
8+
doc = pymupdf.open()
9+
page = doc.new_page()
10+
rect = pymupdf.Rect(10, 10, 100, 100)
11+
rc1 = page.insert_htmlbox(rect, story)
12+
13+
new_text = page.get_text("text", clip=rect).replace("\n", " ")
14+
assert text.strip() == new_text.strip()

0 commit comments

Comments
 (0)