Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@ def insert_htmlbox(
oc=0,
opacity=1,
overlay=True,
) -> float:
) -> tuple:
"""Insert text with optional HTML tags and stylings into a rectangle.

Args:
Expand Down Expand Up @@ -2448,16 +2448,21 @@ def insert_htmlbox(
if not fit.big_enough: # there was no fit
return (-1, scale_low)

filled = fit.filled
scale = 1 / fit.parameter # shrink factor

spare_height = fit.rect.y1 - filled[3] # unused room at rectangle bottom
# Note: due to MuPDF's logic this may be negative even for successful fits.
if scale != 1 or spare_height < 0: # if scaling occurred, set spare_height to 0
filled = pymupdf.Rect(fit.filled)
# final adjustment if filled rect is wider than fit rect
if filled.width > fit.rect.width:
h = filled.width / fit.rect.width * fit.rect.height
fit.rect.x1 = filled.x1
fit.rect.y1 = h
fit.parameter = fit.rect.x1 / temp_rect.x1

scale = 1 / fit.parameter
spare_height = max((fit.rect.y1 - filled.y1) / fit.parameter, 0)
if scale != 1: # if scaling occurred, set spare_height to 0
spare_height = 0

def rect_function(*args):
return fit.rect, fit.rect, pymupdf.Identity
return fit.rect, fit.rect, None

# draw story on temp PDF page
doc = story.write_with_links(rect_function)
Expand All @@ -2477,7 +2482,7 @@ def rect_function(*args):
# -------------------------------------------------------------------------
# re-insert links in target rect (show_pdf_page cannot copy annotations)
# -------------------------------------------------------------------------
# scaled center point of fit.rect
# scaled center point of fit rect
mp1 = (fit.rect.tl + fit.rect.br) / 2 * scale

# center point of target rect
Expand Down
14 changes: 14 additions & 0 deletions tests/test_4613.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pymupdf
import string


def test_4613():
text = " ".join([string.ascii_lowercase + " " + string.ascii_uppercase] * 3)
story = pymupdf.Story(text)
doc = pymupdf.open()
page = doc.new_page()
rect = pymupdf.Rect(10, 10, 100, 100)
rc1 = page.insert_htmlbox(rect, story)

new_text = page.get_text("text", clip=rect).replace("\n", " ")
assert text.strip() == new_text.strip()
Loading