dsh-pdf-workbench 1.3.3 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +225 -199
- package/assets/screenshot-attachments.jpg +0 -0
- package/assets/screenshot-canvas.jpg +0 -0
- package/assets/screenshot-questions.jpg +0 -0
- package/assets/screenshot-settings-data.jpg +0 -0
- package/assets/screenshot-settings-mode.jpg +0 -0
- package/assets/screenshot-settings-reading.jpg +0 -0
- package/client.js +5700 -849
- package/docs/DETAIL.md +1 -69
- package/docs/RELEASE.md +119 -119
- package/docs/SIDE-PREVIEW.md +5 -1
- package/docs/preview.svg +86 -0
- package/icon.svg +7 -6
- package/index.js +18 -2
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
- package/screenshots.json +13 -8
- package/src/client/host-ui-entry.js +11 -11
- package/src/client/react-jsx-runtime.cjs +21 -21
- package/src/client/viewer.js +80 -80
- package/src/host/artifacts.js +31 -3
- package/src/host/attachments.js +159 -0
- package/src/host/backup.js +457 -0
- package/src/host/base-session.js +146 -0
- package/src/host/book-import.js +98 -0
- package/src/host/chapter-sessions.js +154 -0
- package/src/host/chapter-text.js +91 -0
- package/src/host/fonts.js +190 -0
- package/src/host/pdf-stream.js +168 -0
- package/src/host/ppt.js +153 -0
- package/src/host/print.js +191 -0
- package/src/host/questions.js +186 -0
- package/src/host/routes.js +671 -25
- package/src/host/state.js +1 -1
- package/src/host/zip.js +186 -0
- package/tools/AGENTS.md +33 -33
- package/tools/README.md +138 -138
- package/tools/auto_chapters.py +122 -122
- package/tools/dsfile.mjs +198 -198
- package/tools/export_chapter.py +108 -108
- package/tools/export_page.py +91 -91
- package/tools/md2overlay.py +200 -200
- package/tools/md2tex.py +422 -422
- package/tools/pdf_blocks.py +110 -110
- package/tools/pdf_fontsize.py +20 -20
- package/tools/pdf_merge.py +15 -15
- package/tools/pdf_pagesizes.py +8 -8
- package/tools/pdf_raster.py +21 -21
- package/tools/pdf_text.py +34 -0
- package/tools/pdfkit.py +896 -896
- package/tools/realign_overlay.py +248 -248
- package/tools/rebuild_overlay.py +55 -55
- package/tools/rename_chapters.py +93 -93
- package/tools//350/247/204/350/214/203/00_/345/205/261/345/220/214/347/272/246/345/256/232.md +142 -142
- package/tools//350/247/204/350/214/203/01_/346/226/207/346/234/254/346/217/220/345/217/226.md +204 -204
- package/tools//350/247/204/350/214/203/02_/347/255/224/347/226/221.md +61 -61
- package/tools//350/247/204/350/214/203/03_/344/271/240/351/242/230/350/247/243/347/255/224.md +77 -77
- package/tools//350/247/204/350/214/203/api/346/217/220/347/244/272/350/257/215_/346/217/220/345/217/226.md +11 -11
- package/vendor/cmaps/LICENSE.txt +36 -36
- package/vendor/fonts/excalifont/OFL.txt +98 -98
- package/vendor/fonts/zhhand/OFL.txt +99 -99
- package/vendor/host-ui.cjs +1719 -1719
- package/vendor/host-ui.css +799 -799
- package/vendor/pdf.min.mjs +28 -28
- package/vendor/pdf.worker.min.mjs +28 -28
- package/vendor/pdfjs-LICENSE.txt +177 -177
- package/vendor/rough/LICENSE +21 -21
- package/vendor/rough/rough.mjs +1 -1
- package/vendor/wasm/LICENSE_JBIG2 +196 -196
- package/vendor/wasm/LICENSE_OPENJPEG +39 -39
- package/vendor/wasm/LICENSE_PDFJS_JBIG2 +13 -13
- package/vendor/wasm/LICENSE_PDFJS_OPENJPEG +22 -22
- package/vendor/wasm/LICENSE_PDFJS_QCMS +19 -19
- package/vendor/wasm/LICENSE_QCMS +21 -21
- package/vendor/wasm/jbig2_nowasm_fallback.js +15 -15
- package/vendor/wasm/openjpeg_nowasm_fallback.js +17 -17
- package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +1 -1
- package/tools/node.cmd +0 -3
- package/tools/py.cmd +0 -3
- package/vendor/fonts/zhhand/Xiaolai-Regular.ttf +0 -0
package/tools/export_chapter.py
CHANGED
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
"""Compose a whole chapter: every page with its revealed translation + annotations.
|
|
2
|
-
|
|
3
|
-
Usage:
|
|
4
|
-
python export_chapter.py <source.pdf> <overlay.pdf|-> <spec.json> <out.pdf>
|
|
5
|
-
|
|
6
|
-
`spec.json` is `{"pages":[{"page":3,"masks":[...],"annots":[...]}, ...]}`; pages not
|
|
7
|
-
listed are exported as the plain original page. Coordinates are normalized.
|
|
8
|
-
"""
|
|
9
|
-
import json
|
|
10
|
-
import os
|
|
11
|
-
import sys
|
|
12
|
-
|
|
13
|
-
import fitz
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def rgb(value, fallback=(0.12, 0.12, 0.12)):
|
|
17
|
-
text = str(value or "").strip().lstrip("#")
|
|
18
|
-
if len(text) == 3:
|
|
19
|
-
text = "".join(char * 2 for char in text)
|
|
20
|
-
if len(text) != 6:
|
|
21
|
-
return fallback
|
|
22
|
-
try:
|
|
23
|
-
return tuple(int(text[index:index + 2], 16) / 255.0 for index in (0, 2, 4))
|
|
24
|
-
except ValueError:
|
|
25
|
-
return fallback
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
source_path, overlay_path, spec_path, out_path = sys.argv[1:5]
|
|
29
|
-
progress_path = sys.argv[5] if len(sys.argv) > 5 else ""
|
|
30
|
-
with open(spec_path, encoding="utf-8-sig") as handle:
|
|
31
|
-
spec = json.load(handle)
|
|
32
|
-
pages = spec.get("pages") if isinstance(spec, dict) else None
|
|
33
|
-
if not isinstance(pages, list):
|
|
34
|
-
pages = []
|
|
35
|
-
by_page = {}
|
|
36
|
-
for item in pages:
|
|
37
|
-
try:
|
|
38
|
-
by_page[int(item.get("page"))] = item
|
|
39
|
-
except (TypeError, ValueError):
|
|
40
|
-
continue
|
|
41
|
-
|
|
42
|
-
source = fitz.open(source_path)
|
|
43
|
-
overlay = fitz.open(overlay_path) if overlay_path != "-" and os.path.exists(overlay_path) else None
|
|
44
|
-
doc = fitz.open()
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
def write_progress(payload):
|
|
48
|
-
if progress_path == "":
|
|
49
|
-
return
|
|
50
|
-
with open(progress_path, "w", encoding="utf-8") as handle:
|
|
51
|
-
json.dump(payload, handle)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
write_progress({"status": "running", "done": 0, "total": source.page_count})
|
|
55
|
-
|
|
56
|
-
for index in range(source.page_count):
|
|
57
|
-
if progress_path != "" and os.path.exists(progress_path + ".cancel"):
|
|
58
|
-
write_progress({"status": "cancelled", "done": index, "total": source.page_count})
|
|
59
|
-
sys.exit(0)
|
|
60
|
-
item = by_page.get(index + 1, {})
|
|
61
|
-
page = doc.new_page(width=source[0].rect.width, height=source[index].rect.height)
|
|
62
|
-
box = page.rect
|
|
63
|
-
page.show_pdf_page(box, source, index)
|
|
64
|
-
|
|
65
|
-
if overlay is not None and index < overlay.page_count:
|
|
66
|
-
for mask in item.get("masks") or []:
|
|
67
|
-
try:
|
|
68
|
-
rect = fitz.Rect(
|
|
69
|
-
float(mask["x"]) * box.width,
|
|
70
|
-
float(mask["y"]) * box.height,
|
|
71
|
-
(float(mask["x"]) + float(mask["w"])) * box.width,
|
|
72
|
-
(float(mask["y"]) + float(mask["h"])) * box.height,
|
|
73
|
-
)
|
|
74
|
-
except (KeyError, TypeError, ValueError):
|
|
75
|
-
continue
|
|
76
|
-
if rect.is_empty or rect.width <= 1 or rect.height <= 1:
|
|
77
|
-
continue
|
|
78
|
-
page.show_pdf_page(rect, overlay, index, clip=rect)
|
|
79
|
-
|
|
80
|
-
for entry in item.get("annots") or []:
|
|
81
|
-
color = rgb(entry.get("color"))
|
|
82
|
-
if entry.get("kind") == "text":
|
|
83
|
-
text = str(entry.get("text") or "")
|
|
84
|
-
if text.strip() == "":
|
|
85
|
-
continue
|
|
86
|
-
x = float(entry.get("x", 0)) * box.width
|
|
87
|
-
y = float(entry.get("y", 0)) * box.height
|
|
88
|
-
for offset, line in enumerate(text.split("\n")):
|
|
89
|
-
page.insert_text((x, y + 14 + offset * 18), line, fontsize=14,
|
|
90
|
-
fontname="china-s", color=color)
|
|
91
|
-
continue
|
|
92
|
-
points = [
|
|
93
|
-
fitz.Point(float(point[0]) * box.width, float(point[1]) * box.height)
|
|
94
|
-
for point in entry.get("points") or []
|
|
95
|
-
if isinstance(point, (list, tuple)) and len(point) >= 2
|
|
96
|
-
]
|
|
97
|
-
if len(points) < 2:
|
|
98
|
-
continue
|
|
99
|
-
page.draw_polyline(points, color=color, width=float(entry.get("width") or 2),
|
|
100
|
-
lineCap=1, lineJoin=1, overlay=True)
|
|
101
|
-
|
|
102
|
-
write_progress({"status": "running", "done": index + 1, "total": source.page_count})
|
|
103
|
-
|
|
104
|
-
tmp = out_path + ".tmp.pdf"
|
|
105
|
-
doc.save(tmp)
|
|
106
|
-
os.replace(tmp, out_path)
|
|
107
|
-
write_progress({"status": "done", "done": source.page_count, "total": source.page_count})
|
|
108
|
-
print(out_path)
|
|
1
|
+
"""Compose a whole chapter: every page with its revealed translation + annotations.
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
python export_chapter.py <source.pdf> <overlay.pdf|-> <spec.json> <out.pdf>
|
|
5
|
+
|
|
6
|
+
`spec.json` is `{"pages":[{"page":3,"masks":[...],"annots":[...]}, ...]}`; pages not
|
|
7
|
+
listed are exported as the plain original page. Coordinates are normalized.
|
|
8
|
+
"""
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
import fitz
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def rgb(value, fallback=(0.12, 0.12, 0.12)):
|
|
17
|
+
text = str(value or "").strip().lstrip("#")
|
|
18
|
+
if len(text) == 3:
|
|
19
|
+
text = "".join(char * 2 for char in text)
|
|
20
|
+
if len(text) != 6:
|
|
21
|
+
return fallback
|
|
22
|
+
try:
|
|
23
|
+
return tuple(int(text[index:index + 2], 16) / 255.0 for index in (0, 2, 4))
|
|
24
|
+
except ValueError:
|
|
25
|
+
return fallback
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
source_path, overlay_path, spec_path, out_path = sys.argv[1:5]
|
|
29
|
+
progress_path = sys.argv[5] if len(sys.argv) > 5 else ""
|
|
30
|
+
with open(spec_path, encoding="utf-8-sig") as handle:
|
|
31
|
+
spec = json.load(handle)
|
|
32
|
+
pages = spec.get("pages") if isinstance(spec, dict) else None
|
|
33
|
+
if not isinstance(pages, list):
|
|
34
|
+
pages = []
|
|
35
|
+
by_page = {}
|
|
36
|
+
for item in pages:
|
|
37
|
+
try:
|
|
38
|
+
by_page[int(item.get("page"))] = item
|
|
39
|
+
except (TypeError, ValueError):
|
|
40
|
+
continue
|
|
41
|
+
|
|
42
|
+
source = fitz.open(source_path)
|
|
43
|
+
overlay = fitz.open(overlay_path) if overlay_path != "-" and os.path.exists(overlay_path) else None
|
|
44
|
+
doc = fitz.open()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def write_progress(payload):
|
|
48
|
+
if progress_path == "":
|
|
49
|
+
return
|
|
50
|
+
with open(progress_path, "w", encoding="utf-8") as handle:
|
|
51
|
+
json.dump(payload, handle)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
write_progress({"status": "running", "done": 0, "total": source.page_count})
|
|
55
|
+
|
|
56
|
+
for index in range(source.page_count):
|
|
57
|
+
if progress_path != "" and os.path.exists(progress_path + ".cancel"):
|
|
58
|
+
write_progress({"status": "cancelled", "done": index, "total": source.page_count})
|
|
59
|
+
sys.exit(0)
|
|
60
|
+
item = by_page.get(index + 1, {})
|
|
61
|
+
page = doc.new_page(width=source[0].rect.width, height=source[index].rect.height)
|
|
62
|
+
box = page.rect
|
|
63
|
+
page.show_pdf_page(box, source, index)
|
|
64
|
+
|
|
65
|
+
if overlay is not None and index < overlay.page_count:
|
|
66
|
+
for mask in item.get("masks") or []:
|
|
67
|
+
try:
|
|
68
|
+
rect = fitz.Rect(
|
|
69
|
+
float(mask["x"]) * box.width,
|
|
70
|
+
float(mask["y"]) * box.height,
|
|
71
|
+
(float(mask["x"]) + float(mask["w"])) * box.width,
|
|
72
|
+
(float(mask["y"]) + float(mask["h"])) * box.height,
|
|
73
|
+
)
|
|
74
|
+
except (KeyError, TypeError, ValueError):
|
|
75
|
+
continue
|
|
76
|
+
if rect.is_empty or rect.width <= 1 or rect.height <= 1:
|
|
77
|
+
continue
|
|
78
|
+
page.show_pdf_page(rect, overlay, index, clip=rect)
|
|
79
|
+
|
|
80
|
+
for entry in item.get("annots") or []:
|
|
81
|
+
color = rgb(entry.get("color"))
|
|
82
|
+
if entry.get("kind") == "text":
|
|
83
|
+
text = str(entry.get("text") or "")
|
|
84
|
+
if text.strip() == "":
|
|
85
|
+
continue
|
|
86
|
+
x = float(entry.get("x", 0)) * box.width
|
|
87
|
+
y = float(entry.get("y", 0)) * box.height
|
|
88
|
+
for offset, line in enumerate(text.split("\n")):
|
|
89
|
+
page.insert_text((x, y + 14 + offset * 18), line, fontsize=14,
|
|
90
|
+
fontname="china-s", color=color)
|
|
91
|
+
continue
|
|
92
|
+
points = [
|
|
93
|
+
fitz.Point(float(point[0]) * box.width, float(point[1]) * box.height)
|
|
94
|
+
for point in entry.get("points") or []
|
|
95
|
+
if isinstance(point, (list, tuple)) and len(point) >= 2
|
|
96
|
+
]
|
|
97
|
+
if len(points) < 2:
|
|
98
|
+
continue
|
|
99
|
+
page.draw_polyline(points, color=color, width=float(entry.get("width") or 2),
|
|
100
|
+
lineCap=1, lineJoin=1, overlay=True)
|
|
101
|
+
|
|
102
|
+
write_progress({"status": "running", "done": index + 1, "total": source.page_count})
|
|
103
|
+
|
|
104
|
+
tmp = out_path + ".tmp.pdf"
|
|
105
|
+
doc.save(tmp)
|
|
106
|
+
os.replace(tmp, out_path)
|
|
107
|
+
write_progress({"status": "done", "done": source.page_count, "total": source.page_count})
|
|
108
|
+
print(out_path)
|
package/tools/export_page.py
CHANGED
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
"""Compose one exported PDF page: original + revealed translation + annotations.
|
|
2
|
-
|
|
3
|
-
Usage:
|
|
4
|
-
python export_page.py <source.pdf> <overlay.pdf|-> <page> <masks.json|-> <annots.json|-> <out.pdf>
|
|
5
|
-
|
|
6
|
-
`overlay.pdf` is the bbox-positioned translation (`<stem>_overlay.pdf`); each mask
|
|
7
|
-
is a normalized `{x,y,w,h}` window that reveals that region of the overlay page.
|
|
8
|
-
Annotations are normalized strokes (`{kind:'stroke', points:[[x,y]...], color,
|
|
9
|
-
width}`) and text notes (`{kind:'text', x, y, text, color}`).
|
|
10
|
-
"""
|
|
11
|
-
import json
|
|
12
|
-
import os
|
|
13
|
-
import sys
|
|
14
|
-
|
|
15
|
-
import fitz
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def rgb(value, fallback=(0.12, 0.12, 0.12)):
|
|
19
|
-
text = str(value or "").strip().lstrip("#")
|
|
20
|
-
if len(text) == 3:
|
|
21
|
-
text = "".join(char * 2 for char in text)
|
|
22
|
-
if len(text) != 6:
|
|
23
|
-
return fallback
|
|
24
|
-
try:
|
|
25
|
-
return tuple(int(text[index:index + 2], 16) / 255.0 for index in (0, 2, 4))
|
|
26
|
-
except ValueError:
|
|
27
|
-
return fallback
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
source_path, overlay_path, page_number, masks_path, annots_path, out_path = sys.argv[1:7]
|
|
31
|
-
index = max(0, int(page_number) - 1)
|
|
32
|
-
masks = []
|
|
33
|
-
if masks_path != "-":
|
|
34
|
-
with open(masks_path, encoding="utf-8") as handle:
|
|
35
|
-
masks = json.load(handle)
|
|
36
|
-
annots = []
|
|
37
|
-
if annots_path != "-":
|
|
38
|
-
with open(annots_path, encoding="utf-8") as handle:
|
|
39
|
-
annots = json.load(handle)
|
|
40
|
-
|
|
41
|
-
source = fitz.open(source_path)
|
|
42
|
-
index = min(index, source.page_count - 1)
|
|
43
|
-
doc = fitz.open()
|
|
44
|
-
page = doc.new_page(width=source[0].rect.width, height=source[index].rect.height)
|
|
45
|
-
box = page.rect
|
|
46
|
-
page.show_pdf_page(box, source, index)
|
|
47
|
-
|
|
48
|
-
if overlay_path != "-" and os.path.exists(overlay_path):
|
|
49
|
-
overlay = fitz.open(overlay_path)
|
|
50
|
-
if index < overlay.page_count:
|
|
51
|
-
for mask in masks:
|
|
52
|
-
try:
|
|
53
|
-
rect = fitz.Rect(
|
|
54
|
-
float(mask["x"]) * box.width,
|
|
55
|
-
float(mask["y"]) * box.height,
|
|
56
|
-
(float(mask["x"]) + float(mask["w"])) * box.width,
|
|
57
|
-
(float(mask["y"]) + float(mask["h"])) * box.height,
|
|
58
|
-
)
|
|
59
|
-
except (KeyError, TypeError, ValueError):
|
|
60
|
-
continue
|
|
61
|
-
if rect.is_empty or rect.width <= 1 or rect.height <= 1:
|
|
62
|
-
continue
|
|
63
|
-
# Same page size on both sides, so the clip equals the target rect.
|
|
64
|
-
page.show_pdf_page(rect, overlay, index, clip=rect)
|
|
65
|
-
|
|
66
|
-
for entry in annots:
|
|
67
|
-
color = rgb(entry.get("color"))
|
|
68
|
-
if entry.get("kind") == "text":
|
|
69
|
-
text = str(entry.get("text") or "")
|
|
70
|
-
if text.strip() == "":
|
|
71
|
-
continue
|
|
72
|
-
x = float(entry.get("x", 0)) * box.width
|
|
73
|
-
y = float(entry.get("y", 0)) * box.height
|
|
74
|
-
for offset, line in enumerate(text.split("\n")):
|
|
75
|
-
page.insert_text((x, y + 14 + offset * 18), line, fontsize=14,
|
|
76
|
-
fontname="china-s", color=color)
|
|
77
|
-
continue
|
|
78
|
-
points = [
|
|
79
|
-
fitz.Point(float(point[0]) * box.width, float(point[1]) * box.height)
|
|
80
|
-
for point in entry.get("points") or []
|
|
81
|
-
if isinstance(point, (list, tuple)) and len(point) >= 2
|
|
82
|
-
]
|
|
83
|
-
if len(points) < 2:
|
|
84
|
-
continue
|
|
85
|
-
page.draw_polyline(points, color=color, width=float(entry.get("width") or 2),
|
|
86
|
-
lineCap=1, lineJoin=1, overlay=True)
|
|
87
|
-
|
|
88
|
-
tmp = out_path + ".tmp.pdf"
|
|
89
|
-
doc.save(tmp)
|
|
90
|
-
os.replace(tmp, out_path)
|
|
91
|
-
print(out_path)
|
|
1
|
+
"""Compose one exported PDF page: original + revealed translation + annotations.
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
python export_page.py <source.pdf> <overlay.pdf|-> <page> <masks.json|-> <annots.json|-> <out.pdf>
|
|
5
|
+
|
|
6
|
+
`overlay.pdf` is the bbox-positioned translation (`<stem>_overlay.pdf`); each mask
|
|
7
|
+
is a normalized `{x,y,w,h}` window that reveals that region of the overlay page.
|
|
8
|
+
Annotations are normalized strokes (`{kind:'stroke', points:[[x,y]...], color,
|
|
9
|
+
width}`) and text notes (`{kind:'text', x, y, text, color}`).
|
|
10
|
+
"""
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
import fitz
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def rgb(value, fallback=(0.12, 0.12, 0.12)):
|
|
19
|
+
text = str(value or "").strip().lstrip("#")
|
|
20
|
+
if len(text) == 3:
|
|
21
|
+
text = "".join(char * 2 for char in text)
|
|
22
|
+
if len(text) != 6:
|
|
23
|
+
return fallback
|
|
24
|
+
try:
|
|
25
|
+
return tuple(int(text[index:index + 2], 16) / 255.0 for index in (0, 2, 4))
|
|
26
|
+
except ValueError:
|
|
27
|
+
return fallback
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
source_path, overlay_path, page_number, masks_path, annots_path, out_path = sys.argv[1:7]
|
|
31
|
+
index = max(0, int(page_number) - 1)
|
|
32
|
+
masks = []
|
|
33
|
+
if masks_path != "-":
|
|
34
|
+
with open(masks_path, encoding="utf-8") as handle:
|
|
35
|
+
masks = json.load(handle)
|
|
36
|
+
annots = []
|
|
37
|
+
if annots_path != "-":
|
|
38
|
+
with open(annots_path, encoding="utf-8") as handle:
|
|
39
|
+
annots = json.load(handle)
|
|
40
|
+
|
|
41
|
+
source = fitz.open(source_path)
|
|
42
|
+
index = min(index, source.page_count - 1)
|
|
43
|
+
doc = fitz.open()
|
|
44
|
+
page = doc.new_page(width=source[0].rect.width, height=source[index].rect.height)
|
|
45
|
+
box = page.rect
|
|
46
|
+
page.show_pdf_page(box, source, index)
|
|
47
|
+
|
|
48
|
+
if overlay_path != "-" and os.path.exists(overlay_path):
|
|
49
|
+
overlay = fitz.open(overlay_path)
|
|
50
|
+
if index < overlay.page_count:
|
|
51
|
+
for mask in masks:
|
|
52
|
+
try:
|
|
53
|
+
rect = fitz.Rect(
|
|
54
|
+
float(mask["x"]) * box.width,
|
|
55
|
+
float(mask["y"]) * box.height,
|
|
56
|
+
(float(mask["x"]) + float(mask["w"])) * box.width,
|
|
57
|
+
(float(mask["y"]) + float(mask["h"])) * box.height,
|
|
58
|
+
)
|
|
59
|
+
except (KeyError, TypeError, ValueError):
|
|
60
|
+
continue
|
|
61
|
+
if rect.is_empty or rect.width <= 1 or rect.height <= 1:
|
|
62
|
+
continue
|
|
63
|
+
# Same page size on both sides, so the clip equals the target rect.
|
|
64
|
+
page.show_pdf_page(rect, overlay, index, clip=rect)
|
|
65
|
+
|
|
66
|
+
for entry in annots:
|
|
67
|
+
color = rgb(entry.get("color"))
|
|
68
|
+
if entry.get("kind") == "text":
|
|
69
|
+
text = str(entry.get("text") or "")
|
|
70
|
+
if text.strip() == "":
|
|
71
|
+
continue
|
|
72
|
+
x = float(entry.get("x", 0)) * box.width
|
|
73
|
+
y = float(entry.get("y", 0)) * box.height
|
|
74
|
+
for offset, line in enumerate(text.split("\n")):
|
|
75
|
+
page.insert_text((x, y + 14 + offset * 18), line, fontsize=14,
|
|
76
|
+
fontname="china-s", color=color)
|
|
77
|
+
continue
|
|
78
|
+
points = [
|
|
79
|
+
fitz.Point(float(point[0]) * box.width, float(point[1]) * box.height)
|
|
80
|
+
for point in entry.get("points") or []
|
|
81
|
+
if isinstance(point, (list, tuple)) and len(point) >= 2
|
|
82
|
+
]
|
|
83
|
+
if len(points) < 2:
|
|
84
|
+
continue
|
|
85
|
+
page.draw_polyline(points, color=color, width=float(entry.get("width") or 2),
|
|
86
|
+
lineCap=1, lineJoin=1, overlay=True)
|
|
87
|
+
|
|
88
|
+
tmp = out_path + ".tmp.pdf"
|
|
89
|
+
doc.save(tmp)
|
|
90
|
+
os.replace(tmp, out_path)
|
|
91
|
+
print(out_path)
|