Files
pdf/tests/edits/_hyphen_check.py
T

75 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Does cross-page MIGRATION preserve hyphens/dashes? Grow the page-0 Technologies paragraph so it
pushes the 'Architected ... high-scale e-commerce' paragraph onto page 2 (migration), then check the
migrated text still has its hyphens.
Run: gateway/.venv/Scripts/python.exe tests/edits/_hyphen_check.py
"""
from __future__ import annotations
import json, sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, r"C:\Users\furqa\pdfeng-build\win-local-pdfium\lib")
sys.path.insert(1, str(ROOT / "gateway"))
import pdfengine
PDF = ROOT / "Mo-Faishal-Qureshi.pdf"
def ptext(p):
return "".join(r.text for ln in p.lines for r in ln.runs)
def find(doc, page, needle):
for p in doc.get_page(page).extract_document_model().paragraphs:
if needle in ptext(p):
return p
return None
def alltext(doc):
out = []
for i in range(doc.page_count):
for p in doc.get_page(i).extract_document_model().paragraphs:
out.append(ptext(p))
return " ".join(out)
def main():
doc = pdfengine.PdfDocument.load_from_memory(PDF.read_bytes(), "")
before = alltext(doc)
print("BEFORE migration:")
print(" 'high' + U+002D + 'scale' :", ("high-scale" in before))
print(" 'high' + U+2011 + 'scale' :", ("highscale" in before))
print(" 'e' + U+002D + 'commerce' :", ("e-commerce" in before))
print(" 'e' + U+2011 + 'commerce' :", ("ecommerce" in before))
tech = find(doc, 0, "Technologies")
oi = [i for ln in tech.lines for r in ln.runs for i in r.object_indices]
cl = min(ln.x for ln in tech.lines); cr = max(ln.x + ln.w for ln in tech.lines)
fb = max(ln.baseline_y for ln in tech.lines)
bls = sorted({round(ln.baseline_y, 1) for ln in tech.lines}, reverse=True)
lead = abs(bls[0]-bls[1]) if len(bls) > 1 else 14.0
big = ptext(tech).strip() + " " + ("filler " * 60)
op = {"version": "1.0", "operations": [{"id": "g", "type": "reflow_paragraph", "pageIndex": 0, "data": {
"objectIndices": oi, "runs": [{"text": big, "internalFontId": tech.lines[0].runs[0].internal_font_id,
"fontSize": 11.0, "color": "#000000"}], "columnLeft": cl, "columnRight": cr, "firstBaselineY": fb,
"leading": lead, "oldLineCount": len(tech.lines), "align": "left", "paraId": "TECHPARA"}}]}
doc.apply_edits(json.dumps(op))
r = pdfengine.PdfDocument.load_from_memory(doc.save_full(), "")
after = alltext(r)
print(f"\nAFTER migration (pages={r.page_count}):")
print(" 'high' + U+002D + 'scale' :", ("high-scale" in after))
print(" 'high' + U+2011 + 'scale' :", ("highscale" in after))
print(" 'e' + U+002D + 'commerce' :", ("e-commerce" in after))
print(" 'e' + U+2011 + 'commerce' :", ("ecommerce" in after))
for i in range(r.page_count):
a = find(r, i, "Architected")
if a:
safe = ptext(a)[:120].encode("ascii", "replace").decode("ascii")
print(f"\n Architected (page {i}): {safe!r}")
break
if __name__ == "__main__":
main()