Repository navigation
Expand file tree
/
Copy pathgen_source_pdf.py
More file actions
132 lines (116 loc) · 3.93 KB
/
Copy pathgen_source_pdf.py
File metadata and controls
132 lines (116 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
"""
软著源程序 PDF 生成器
- 拼接项目源代码(按 index.html 加载顺序)
- 删除空行
- 前 30 页 + 后 30 页,每页 50 行
- 页眉:左 蛇途求生网页游戏软件 V1.0,右 第 X 页 共 60 页
"""
import os
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
ROOT = r"d:\code2\AI\SnakeSurvivalPath"
SOFTWARE_NAME = "蛇途求生网页游戏软件 V1.0"
LINES_PER_PAGE = 50
PAGES_FRONT = 30
PAGES_BACK = 30
# 按 index.html 加载顺序 + 相关支撑文件
FILES = [
"index.html",
"js/app-config.js",
"js/config.js",
"js/rng.js",
"js/logic/grid.js",
"js/logic/snake.js",
"js/logic/scoring.js",
"js/logic/items.js",
"js/logic/events.js",
"js/logic/gamble.js",
"js/audio.js",
"js/bgm.js",
"js/feedback.js",
"js/render/particles.js",
"js/render/renderer.js",
"js/leaderboard.js",
"js/ui/main.js",
"js/ui/controls.js",
"js/ui/screens.js",
"js/main.js",
"css/style.css",
"sql/init.sql",
"sw.js",
"manifest.json",
]
def collect_lines():
lines = []
for rel in FILES:
path = os.path.join(ROOT, rel)
if not os.path.exists(path):
continue
with open(path, "r", encoding="utf-8", errors="replace") as f:
file_lines = f.readlines()
# 文件分隔标记
lines.append("// ===== %s =====" % rel.replace("\\", "/"))
for ln in file_lines:
ln = ln.rstrip("\n").rstrip("\r")
# 跳过纯空白行
if ln.strip() == "":
continue
lines.append(ln)
return lines
def main():
pdfmetrics.registerFont(TTFont("SimSun", r"C:\Windows\Fonts\simsun.ttc"))
all_lines = collect_lines()
total = len(all_lines)
front_lines = all_lines[: PAGES_FRONT * LINES_PER_PAGE]
back_lines = all_lines[-PAGES_BACK * LINES_PER_PAGE:] if total > PAGES_BACK * LINES_PER_PAGE else []
# 若总行数不足 60 页则全交
if total <= (PAGES_FRONT + PAGES_BACK) * LINES_PER_PAGE:
pages_lines = [all_lines[i:i + LINES_PER_PAGE] for i in range(0, total, LINES_PER_PAGE)]
else:
front_pages = [front_lines[i:i + LINES_PER_PAGE] for i in range(0, len(front_lines), LINES_PER_PAGE)]
back_pages = [back_lines[i:i + LINES_PER_PAGE] for i in range(0, len(back_lines), LINES_PER_PAGE)]
pages_lines = front_pages + back_pages
total_pages = len(pages_lines)
out_path = os.path.join(ROOT, "源程序_蛇途求生网页游戏软件_V1.0.pdf")
c = canvas.Canvas(out_path, pagesize=A4)
page_w, page_h = A4 # 595.27 x 841.89
# 页边距
left = 28
top = 30
bottom = 28
header_h = 18
content_top = page_h - top - header_h
content_h = content_top - bottom
# 字体:宋体等宽,9pt,行距 = content_h / LINES_PER_PAGE
font_size = 9
line_h = content_h / LINES_PER_PAGE
c.setFont("SimSun", font_size)
for page_idx, page_lines in enumerate(pages_lines, start=1):
# 页眉
c.setFont("SimSun", 9)
c.setFillGray(0.2)
c.drawString(left, page_h - top - 12, SOFTWARE_NAME)
c.drawRightString(page_w - left, page_h - top - 12, "第 %d 页 共 %d 页" % (page_idx, total_pages))
# 页眉下划线
c.setLineWidth(0.5)
c.setStrokeGray(0.5)
c.line(left, page_h - top - 15, page_w - left, page_h - top - 15)
c.setFillGray(0)
# 正文
c.setFont("SimSun", font_size)
y = content_top
for ln in page_lines:
c.drawString(left, y, ln)
y -= line_h
c.showPage()
c.save()
print("总行数(去空行):", total)
print("前30页行数:", len(front_lines))
print("后30页行数:", len(back_lines))
print("总页数:", total_pages)
print("输出:", out_path)
if __name__ == "__main__":
main()