BabelDOC 0.1.6rc0__py3-none-any.whl
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.
- babeldoc/__init__.py +1 -0
- babeldoc/asynchronize/__init__.py +51 -0
- babeldoc/const.py +7 -0
- babeldoc/converter.py +510 -0
- babeldoc/document_il/__init__.py +43 -0
- babeldoc/document_il/backend/__init__.py +0 -0
- babeldoc/document_il/backend/pdf_creater.py +402 -0
- babeldoc/document_il/frontend/__init__.py +0 -0
- babeldoc/document_il/frontend/il_creater.py +348 -0
- babeldoc/document_il/il_version_1.py +799 -0
- babeldoc/document_il/il_version_1.rnc +141 -0
- babeldoc/document_il/il_version_1.rng +390 -0
- babeldoc/document_il/il_version_1.xsd +235 -0
- babeldoc/document_il/midend/__init__.py +0 -0
- babeldoc/document_il/midend/add_debug_information.py +138 -0
- babeldoc/document_il/midend/il_translator.py +553 -0
- babeldoc/document_il/midend/layout_parser.py +180 -0
- babeldoc/document_il/midend/paragraph_finder.py +452 -0
- babeldoc/document_il/midend/remove_descent.py +168 -0
- babeldoc/document_il/midend/styles_and_formulas.py +701 -0
- babeldoc/document_il/midend/typesetting.py +761 -0
- babeldoc/document_il/translator/__init__.py +0 -0
- babeldoc/document_il/translator/cache.py +145 -0
- babeldoc/document_il/translator/translator.py +267 -0
- babeldoc/document_il/utils/fontmap.py +227 -0
- babeldoc/document_il/utils/layout_helper.py +467 -0
- babeldoc/document_il/utils/style_helper.py +86 -0
- babeldoc/document_il/xml_converter.py +51 -0
- babeldoc/docvision/README.md +0 -0
- babeldoc/docvision/__init__.py +0 -0
- babeldoc/docvision/doclayout.py +278 -0
- babeldoc/docvision/rpc_doclayout.py +162 -0
- babeldoc/high_level.py +468 -0
- babeldoc/main.py +466 -0
- babeldoc/pdfinterp.py +464 -0
- babeldoc/progress_monitor.py +146 -0
- babeldoc/translation_config.py +156 -0
- babeldoc-0.1.6rc0.dist-info/METADATA +283 -0
- babeldoc-0.1.6rc0.dist-info/RECORD +42 -0
- babeldoc-0.1.6rc0.dist-info/WHEEL +4 -0
- babeldoc-0.1.6rc0.dist-info/entry_points.txt +2 -0
- babeldoc-0.1.6rc0.dist-info/licenses/LICENSE +661 -0
babeldoc/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.6.rc0"
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Args:
|
|
6
|
+
def __init__(self, args, kwargs):
|
|
7
|
+
self.args = args
|
|
8
|
+
self.kwargs = kwargs
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class AsyncCallback:
|
|
12
|
+
def __init__(self):
|
|
13
|
+
self.queue = asyncio.Queue()
|
|
14
|
+
self.finished = False
|
|
15
|
+
self.loop = asyncio.get_event_loop()
|
|
16
|
+
|
|
17
|
+
def step_callback(self, *args, **kwargs):
|
|
18
|
+
# Whenever a step is called, add to the queue but don't set finished to True, so __anext__ will continue
|
|
19
|
+
args = Args(args, kwargs)
|
|
20
|
+
|
|
21
|
+
# We have to use the threadsafe call so that it wakes up the event loop, in case it's sleeping:
|
|
22
|
+
# https://stackoverflow.com/a/49912853/2148718
|
|
23
|
+
self.loop.call_soon_threadsafe(self.queue.put_nowait, args)
|
|
24
|
+
|
|
25
|
+
# Add a small delay to release the GIL, ensuring the event loop has time to process messages
|
|
26
|
+
time.sleep(0.01)
|
|
27
|
+
|
|
28
|
+
def finished_callback(self, *args, **kwargs):
|
|
29
|
+
# Whenever a finished is called, add to the queue as with step, but also set finished to True, so __anext__
|
|
30
|
+
# will terminate after processing the remaining items
|
|
31
|
+
if self.finished:
|
|
32
|
+
return
|
|
33
|
+
self.step_callback(*args, **kwargs)
|
|
34
|
+
self.finished = True
|
|
35
|
+
|
|
36
|
+
def __await__(self):
|
|
37
|
+
# Since this implements __anext__, this can return itself
|
|
38
|
+
return self.queue.get().__await__()
|
|
39
|
+
|
|
40
|
+
def __aiter__(self):
|
|
41
|
+
# Since this implements __anext__, this can return itself
|
|
42
|
+
return self
|
|
43
|
+
|
|
44
|
+
async def __anext__(self):
|
|
45
|
+
# Keep waiting for the queue if a) we haven't finished, or b) if the queue is still full. This lets us finish
|
|
46
|
+
# processing the remaining items even after we've finished
|
|
47
|
+
if self.finished and self.queue.empty():
|
|
48
|
+
raise StopAsyncIteration
|
|
49
|
+
|
|
50
|
+
result = await self.queue.get()
|
|
51
|
+
return result
|
babeldoc/const.py
ADDED
babeldoc/converter.py
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import logging
|
|
3
|
+
import re
|
|
4
|
+
import unicodedata
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from pdfminer.converter import PDFConverter
|
|
8
|
+
from pdfminer.layout import LTChar
|
|
9
|
+
from pdfminer.layout import LTComponent
|
|
10
|
+
from pdfminer.layout import LTFigure
|
|
11
|
+
from pdfminer.layout import LTLine
|
|
12
|
+
from pdfminer.layout import LTPage
|
|
13
|
+
from pdfminer.layout import LTText
|
|
14
|
+
from pdfminer.pdfcolor import PDFColorSpace
|
|
15
|
+
from pdfminer.pdffont import PDFCIDFont
|
|
16
|
+
from pdfminer.pdffont import PDFFont
|
|
17
|
+
from pdfminer.pdffont import PDFUnicodeNotDefined
|
|
18
|
+
from pdfminer.pdfinterp import PDFGraphicState
|
|
19
|
+
from pdfminer.pdfinterp import PDFResourceManager
|
|
20
|
+
from pdfminer.utils import Matrix
|
|
21
|
+
from pdfminer.utils import apply_matrix_pt
|
|
22
|
+
from pdfminer.utils import bbox2str
|
|
23
|
+
from pdfminer.utils import matrix2str
|
|
24
|
+
from pdfminer.utils import mult_matrix
|
|
25
|
+
from pymupdf import Font
|
|
26
|
+
|
|
27
|
+
from babeldoc.document_il.frontend.il_creater import ILCreater
|
|
28
|
+
|
|
29
|
+
log = logging.getLogger(__name__)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class PDFConverterEx(PDFConverter):
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
rsrcmgr: PDFResourceManager,
|
|
36
|
+
il_creater: ILCreater | None = None,
|
|
37
|
+
) -> None:
|
|
38
|
+
PDFConverter.__init__(self, rsrcmgr, None, "utf-8", 1, None)
|
|
39
|
+
self.il_creater = il_creater
|
|
40
|
+
|
|
41
|
+
def begin_page(self, page, ctm) -> None:
|
|
42
|
+
# 重载替换 cropbox
|
|
43
|
+
(x0, y0, x1, y1) = page.cropbox
|
|
44
|
+
(x0, y0) = apply_matrix_pt(ctm, (x0, y0))
|
|
45
|
+
(x1, y1) = apply_matrix_pt(ctm, (x1, y1))
|
|
46
|
+
mediabox = (0, 0, abs(x0 - x1), abs(y0 - y1))
|
|
47
|
+
self.il_creater.on_page_media_box(
|
|
48
|
+
mediabox[0],
|
|
49
|
+
mediabox[1],
|
|
50
|
+
mediabox[2],
|
|
51
|
+
mediabox[3],
|
|
52
|
+
)
|
|
53
|
+
self.il_creater.on_page_number(page.pageno)
|
|
54
|
+
self.cur_item = LTPage(page.pageno, mediabox)
|
|
55
|
+
|
|
56
|
+
def end_page(self, _page) -> None:
|
|
57
|
+
# 重载返回指令流
|
|
58
|
+
return self.receive_layout(self.cur_item)
|
|
59
|
+
|
|
60
|
+
def begin_figure(self, name, bbox, matrix) -> None:
|
|
61
|
+
# 重载设置 pageid
|
|
62
|
+
self._stack.append(self.cur_item)
|
|
63
|
+
self.cur_item = LTFigure(name, bbox, mult_matrix(matrix, self.ctm))
|
|
64
|
+
self.cur_item.pageid = self._stack[-1].pageid
|
|
65
|
+
|
|
66
|
+
def end_figure(self, _: str) -> None:
|
|
67
|
+
# 重载返回指令流
|
|
68
|
+
fig = self.cur_item
|
|
69
|
+
if not isinstance(self.cur_item, LTFigure):
|
|
70
|
+
raise ValueError(f"Unexpected item type: {type(self.cur_item)}")
|
|
71
|
+
self.cur_item = self._stack.pop()
|
|
72
|
+
self.cur_item.add(fig)
|
|
73
|
+
return self.receive_layout(fig)
|
|
74
|
+
|
|
75
|
+
def render_char(
|
|
76
|
+
self,
|
|
77
|
+
matrix,
|
|
78
|
+
font,
|
|
79
|
+
fontsize: float,
|
|
80
|
+
scaling: float,
|
|
81
|
+
rise: float,
|
|
82
|
+
cid: int,
|
|
83
|
+
ncs,
|
|
84
|
+
graphicstate: PDFGraphicState,
|
|
85
|
+
) -> float:
|
|
86
|
+
# 重载设置 cid 和 font
|
|
87
|
+
try:
|
|
88
|
+
text = font.to_unichr(cid)
|
|
89
|
+
if not isinstance(text, str):
|
|
90
|
+
raise TypeError(f"Expected string, got {type(text)}")
|
|
91
|
+
except PDFUnicodeNotDefined:
|
|
92
|
+
text = self.handle_undefined_char(font, cid)
|
|
93
|
+
textwidth = font.char_width(cid)
|
|
94
|
+
textdisp = font.char_disp(cid)
|
|
95
|
+
|
|
96
|
+
font_name = font.fontname
|
|
97
|
+
if isinstance(font_name, bytes):
|
|
98
|
+
try:
|
|
99
|
+
font_name = font_name.decode("utf-8")
|
|
100
|
+
except UnicodeDecodeError:
|
|
101
|
+
font_name = "BASE64:" + base64.b64encode(font_name).decode("utf-8")
|
|
102
|
+
font_id = self.il_creater.current_page_font_name_id_map[font_name]
|
|
103
|
+
|
|
104
|
+
item = AWLTChar(
|
|
105
|
+
matrix,
|
|
106
|
+
font,
|
|
107
|
+
fontsize,
|
|
108
|
+
scaling,
|
|
109
|
+
rise,
|
|
110
|
+
text,
|
|
111
|
+
textwidth,
|
|
112
|
+
textdisp,
|
|
113
|
+
ncs,
|
|
114
|
+
graphicstate,
|
|
115
|
+
self.il_creater.xobj_id,
|
|
116
|
+
font_id,
|
|
117
|
+
)
|
|
118
|
+
self.cur_item.add(item)
|
|
119
|
+
item.cid = cid # hack 插入原字符编码
|
|
120
|
+
item.font = font # hack 插入原字符字体
|
|
121
|
+
return item.adv
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class AWLTChar(LTChar):
|
|
125
|
+
"""Actual letter in the text as a Unicode string."""
|
|
126
|
+
|
|
127
|
+
def __init__(
|
|
128
|
+
self,
|
|
129
|
+
matrix: Matrix,
|
|
130
|
+
font: PDFFont,
|
|
131
|
+
fontsize: float,
|
|
132
|
+
scaling: float,
|
|
133
|
+
rise: float,
|
|
134
|
+
text: str,
|
|
135
|
+
textwidth: float,
|
|
136
|
+
textdisp: float | tuple[float | None, float],
|
|
137
|
+
ncs: PDFColorSpace,
|
|
138
|
+
graphicstate: PDFGraphicState,
|
|
139
|
+
xobj_id: int,
|
|
140
|
+
font_id: str,
|
|
141
|
+
) -> None:
|
|
142
|
+
LTText.__init__(self)
|
|
143
|
+
self._text = text
|
|
144
|
+
self.matrix = matrix
|
|
145
|
+
self.fontname = font.fontname
|
|
146
|
+
self.ncs = ncs
|
|
147
|
+
self.graphicstate = graphicstate
|
|
148
|
+
self.xobj_id = xobj_id
|
|
149
|
+
self.adv = textwidth * fontsize * scaling
|
|
150
|
+
self.aw_font_id = font_id
|
|
151
|
+
# compute the boundary rectangle.
|
|
152
|
+
if font.is_vertical():
|
|
153
|
+
# vertical
|
|
154
|
+
assert isinstance(textdisp, tuple)
|
|
155
|
+
(vx, vy) = textdisp
|
|
156
|
+
if vx is None:
|
|
157
|
+
vx = fontsize * 0.5
|
|
158
|
+
else:
|
|
159
|
+
vx = vx * fontsize * 0.001
|
|
160
|
+
vy = (1000 - vy) * fontsize * 0.001
|
|
161
|
+
bbox_lower_left = (-vx, vy + rise + self.adv)
|
|
162
|
+
bbox_upper_right = (-vx + fontsize, vy + rise)
|
|
163
|
+
else:
|
|
164
|
+
# horizontal
|
|
165
|
+
descent = font.get_descent() * fontsize
|
|
166
|
+
bbox_lower_left = (0, descent + rise)
|
|
167
|
+
bbox_upper_right = (self.adv, descent + rise + fontsize)
|
|
168
|
+
(a, b, c, d, e, f) = self.matrix
|
|
169
|
+
self.upright = a * d * scaling > 0 and b * c <= 0
|
|
170
|
+
(x0, y0) = apply_matrix_pt(self.matrix, bbox_lower_left)
|
|
171
|
+
(x1, y1) = apply_matrix_pt(self.matrix, bbox_upper_right)
|
|
172
|
+
if x1 < x0:
|
|
173
|
+
(x0, x1) = (x1, x0)
|
|
174
|
+
if y1 < y0:
|
|
175
|
+
(y0, y1) = (y1, y0)
|
|
176
|
+
LTComponent.__init__(self, (x0, y0, x1, y1))
|
|
177
|
+
if font.is_vertical() or matrix[0] == 0:
|
|
178
|
+
self.size = self.width
|
|
179
|
+
else:
|
|
180
|
+
self.size = self.height
|
|
181
|
+
return
|
|
182
|
+
|
|
183
|
+
def __repr__(self) -> str:
|
|
184
|
+
return f"<{self.__class__.__name__} {bbox2str(self.bbox)} matrix={matrix2str(self.matrix)} font={self.fontname!r} adv={self.adv} text={self.get_text()!r}>"
|
|
185
|
+
|
|
186
|
+
def get_text(self) -> str:
|
|
187
|
+
return self._text
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class Paragraph:
|
|
191
|
+
def __init__(self, y, x, x0, x1, size, brk):
|
|
192
|
+
self.y: float = y # 初始纵坐标
|
|
193
|
+
self.x: float = x # 初始横坐标
|
|
194
|
+
self.x0: float = x0 # 左边界
|
|
195
|
+
self.x1: float = x1 # 右边界
|
|
196
|
+
self.size: float = size # 字体大小
|
|
197
|
+
self.brk: bool = brk # 换行标记
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
# fmt: off
|
|
201
|
+
class TranslateConverter(PDFConverterEx):
|
|
202
|
+
def __init__(
|
|
203
|
+
self,
|
|
204
|
+
rsrcmgr,
|
|
205
|
+
vfont: str | None = None,
|
|
206
|
+
vchar: str | None = None,
|
|
207
|
+
thread: int = 0,
|
|
208
|
+
layout: dict | None = None,
|
|
209
|
+
lang_in: str = "", # 保留参数但添加未使用标记
|
|
210
|
+
_lang_out: str = "", # 改为未使用参数
|
|
211
|
+
_service: str = "", # 改为未使用参数
|
|
212
|
+
resfont: str = "",
|
|
213
|
+
noto: Font | None = None,
|
|
214
|
+
envs: dict | None = None,
|
|
215
|
+
_prompt: list | None = None, # 改为未使用参数
|
|
216
|
+
il_creater: ILCreater | None = None,
|
|
217
|
+
):
|
|
218
|
+
layout = layout or {}
|
|
219
|
+
super().__init__(rsrcmgr, il_creater)
|
|
220
|
+
self.vfont = vfont
|
|
221
|
+
self.vchar = vchar
|
|
222
|
+
self.thread = thread
|
|
223
|
+
self.layout = layout
|
|
224
|
+
self.resfont = resfont
|
|
225
|
+
self.noto = noto
|
|
226
|
+
|
|
227
|
+
def receive_layout(self, ltpage: LTPage):
|
|
228
|
+
# 段落
|
|
229
|
+
sstk: list[str] = [] # 段落文字栈
|
|
230
|
+
pstk: list[Paragraph] = [] # 段落属性栈
|
|
231
|
+
vbkt: int = 0 # 段落公式括号计数
|
|
232
|
+
# 公式组
|
|
233
|
+
vstk: list[LTChar] = [] # 公式符号组
|
|
234
|
+
vlstk: list[LTLine] = [] # 公式线条组
|
|
235
|
+
vfix: float = 0 # 公式纵向偏移
|
|
236
|
+
# 公式组栈
|
|
237
|
+
var: list[list[LTChar]] = [] # 公式符号组栈
|
|
238
|
+
varl: list[list[LTLine]] = [] # 公式线条组栈
|
|
239
|
+
varf: list[float] = [] # 公式纵向偏移栈
|
|
240
|
+
vlen: list[float] = [] # 公式宽度栈
|
|
241
|
+
# 全局
|
|
242
|
+
lstk: list[LTLine] = [] # 全局线条栈
|
|
243
|
+
xt: LTChar = None # 上一个字符
|
|
244
|
+
xt_cls: int = -1 # 上一个字符所属段落,保证无论第一个字符属于哪个类别都可以触发新段落
|
|
245
|
+
vmax: float = ltpage.width / 4 # 行内公式最大宽度
|
|
246
|
+
ops: str = "" # 渲染结果
|
|
247
|
+
|
|
248
|
+
def vflag(font: str, char: str): # 匹配公式(和角标)字体
|
|
249
|
+
if isinstance(font, bytes): # 不一定能 decode,直接转 str
|
|
250
|
+
font = str(font)
|
|
251
|
+
font = font.split("+")[-1] # 字体名截断
|
|
252
|
+
if re.match(r"\(cid:", char):
|
|
253
|
+
return True
|
|
254
|
+
# 基于字体名规则的判定
|
|
255
|
+
if self.vfont:
|
|
256
|
+
if re.match(self.vfont, font):
|
|
257
|
+
return True
|
|
258
|
+
else:
|
|
259
|
+
if re.match( # latex 字体
|
|
260
|
+
r"(CM[^R]|(MS|XY|MT|BL|RM|EU|LA|RS)[A-Z]|LINE|LCIRCLE|TeX-|rsfs|txsy|wasy|stmary|.*Mono|.*Code|.*Ital|.*Sym|.*Math)",
|
|
261
|
+
font,
|
|
262
|
+
):
|
|
263
|
+
return True
|
|
264
|
+
# 基于字符集规则的判定
|
|
265
|
+
if self.vchar:
|
|
266
|
+
if re.match(self.vchar, char):
|
|
267
|
+
return True
|
|
268
|
+
else:
|
|
269
|
+
if (
|
|
270
|
+
char
|
|
271
|
+
and char != " " # 非空格
|
|
272
|
+
and (
|
|
273
|
+
unicodedata.category(char[0])
|
|
274
|
+
in ["Lm", "Mn", "Sk", "Sm", "Zl", "Zp", "Zs"] # 文字修饰符、数学符号、分隔符号
|
|
275
|
+
or ord(char[0]) in range(0x370, 0x400) # 希腊字母
|
|
276
|
+
)
|
|
277
|
+
):
|
|
278
|
+
return True
|
|
279
|
+
return False
|
|
280
|
+
|
|
281
|
+
############################################################
|
|
282
|
+
# A. 原文档解析
|
|
283
|
+
for child in ltpage:
|
|
284
|
+
if isinstance(child, LTChar):
|
|
285
|
+
self.il_creater.on_lt_char(child)
|
|
286
|
+
continue
|
|
287
|
+
cur_v = False
|
|
288
|
+
layout = self.layout[ltpage.pageid]
|
|
289
|
+
# ltpage.height 可能是 fig 里面的高度,这里统一用 layout.shape
|
|
290
|
+
h, w = layout.shape
|
|
291
|
+
# 读取当前字符在 layout 中的类别
|
|
292
|
+
cx, cy = np.clip(int(child.x0), 0, w - 1), np.clip(int(child.y0), 0, h - 1)
|
|
293
|
+
cls = layout[cy, cx]
|
|
294
|
+
# 锚定文档中 bullet 的位置
|
|
295
|
+
if child.get_text() == "•":
|
|
296
|
+
cls = 0
|
|
297
|
+
# 判定当前字符是否属于公式
|
|
298
|
+
if ( # 判定当前字符是否属于公式
|
|
299
|
+
cls == 0 # 1. 类别为保留区域
|
|
300
|
+
or (cls == xt_cls and len(sstk[-1].strip()) > 1 and child.size < pstk[-1].size * 0.79) # 2. 角标字体,有 0.76 的角标和 0.799 的大写,这里用 0.79 取中,同时考虑首字母放大的情况
|
|
301
|
+
or vflag(child.fontname, child.get_text()) # 3. 公式字体
|
|
302
|
+
or (child.matrix[0] == 0 and child.matrix[3] == 0) # 4. 垂直字体
|
|
303
|
+
):
|
|
304
|
+
cur_v = True
|
|
305
|
+
# 判定括号组是否属于公式
|
|
306
|
+
if not cur_v:
|
|
307
|
+
if vstk and child.get_text() == "(":
|
|
308
|
+
cur_v = True
|
|
309
|
+
vbkt += 1
|
|
310
|
+
if vbkt and child.get_text() == ")":
|
|
311
|
+
cur_v = True
|
|
312
|
+
vbkt -= 1
|
|
313
|
+
if ( # 判定当前公式是否结束
|
|
314
|
+
not cur_v # 1. 当前字符不属于公式
|
|
315
|
+
or cls != xt_cls # 2. 当前字符与前一个字符不属于同一段落
|
|
316
|
+
# or (abs(child.x0 - xt.x0) > vmax and cls != 0) # 3. 段落内换行,可能是一长串斜体的段落,也可能是段内分式换行,这里设个阈值进行区分
|
|
317
|
+
# 禁止纯公式(代码)段落换行,直到文字开始再重开文字段落,保证只存在两种情况
|
|
318
|
+
# A. 纯公式(代码)段落(锚定绝对位置)sstk[-1]=="" -> sstk[-1]=="{v*}"
|
|
319
|
+
# B. 文字开头段落(排版相对位置)sstk[-1]!=""
|
|
320
|
+
or (sstk[-1] != "" and abs(child.x0 - xt.x0) > vmax) # 因为 cls==xt_cls==0 一定有 sstk[-1]=="",所以这里不需要再判定 cls!=0
|
|
321
|
+
):
|
|
322
|
+
if vstk:
|
|
323
|
+
if ( # 根据公式右侧的文字修正公式的纵向偏移
|
|
324
|
+
not cur_v # 1. 当前字符不属于公式
|
|
325
|
+
and cls == xt_cls # 2. 当前字符与前一个字符属于同一段落
|
|
326
|
+
and child.x0 > max([vch.x0 for vch in vstk]) # 3. 当前字符在公式右侧
|
|
327
|
+
):
|
|
328
|
+
vfix = vstk[0].y0 - child.y0
|
|
329
|
+
if sstk[-1] == "":
|
|
330
|
+
xt_cls = -1 # 禁止纯公式段落(sstk[-1]=="{v*}")的后续连接,但是要考虑新字符和后续字符的连接,所以这里修改的是上个字符的类别
|
|
331
|
+
sstk[-1] += f"{{v{len(var)}}}"
|
|
332
|
+
var.append(vstk)
|
|
333
|
+
varl.append(vlstk)
|
|
334
|
+
varf.append(vfix)
|
|
335
|
+
vstk = []
|
|
336
|
+
vlstk = []
|
|
337
|
+
vfix = 0
|
|
338
|
+
# 当前字符不属于公式或当前字符是公式的第一个字符
|
|
339
|
+
if not vstk:
|
|
340
|
+
if cls == xt_cls: # 当前字符与前一个字符属于同一段落
|
|
341
|
+
if child.x0 > xt.x1 + 1: # 添加行内空格
|
|
342
|
+
sstk[-1] += " "
|
|
343
|
+
elif child.x1 < xt.x0: # 添加换行空格并标记原文段落存在换行
|
|
344
|
+
sstk[-1] += " "
|
|
345
|
+
pstk[-1].brk = True
|
|
346
|
+
else: # 根据当前字符构建一个新的段落
|
|
347
|
+
sstk.append("")
|
|
348
|
+
pstk.append(Paragraph(child.y0, child.x0, child.x0, child.x0, child.size, False))
|
|
349
|
+
if not cur_v: # 文字入栈
|
|
350
|
+
if ( # 根据当前字符修正段落属性
|
|
351
|
+
child.size > pstk[-1].size / 0.79 # 1. 当前字符显著比段落字体大
|
|
352
|
+
or len(sstk[-1].strip()) == 1 # 2. 当前字符为段落第二个文字(考虑首字母放大的情况)
|
|
353
|
+
) and child.get_text() != " ": # 3. 当前字符不是空格
|
|
354
|
+
pstk[-1].y -= child.size - pstk[-1].size # 修正段落初始纵坐标,假设两个不同大小字符的上边界对齐
|
|
355
|
+
pstk[-1].size = child.size
|
|
356
|
+
sstk[-1] += child.get_text()
|
|
357
|
+
else: # 公式入栈
|
|
358
|
+
if ( # 根据公式左侧的文字修正公式的纵向偏移
|
|
359
|
+
not vstk # 1. 当前字符是公式的第一个字符
|
|
360
|
+
and cls == xt_cls # 2. 当前字符与前一个字符属于同一段落
|
|
361
|
+
and child.x0 > xt.x0 # 3. 前一个字符在公式左侧
|
|
362
|
+
):
|
|
363
|
+
vfix = child.y0 - xt.y0
|
|
364
|
+
vstk.append(child)
|
|
365
|
+
# 更新段落边界,因为段落内换行之后可能是公式开头,所以要在外边处理
|
|
366
|
+
pstk[-1].x0 = min(pstk[-1].x0, child.x0)
|
|
367
|
+
pstk[-1].x1 = max(pstk[-1].x1, child.x1)
|
|
368
|
+
# 更新上一个字符
|
|
369
|
+
xt = child
|
|
370
|
+
xt_cls = cls
|
|
371
|
+
elif isinstance(child, LTFigure):
|
|
372
|
+
# 图表
|
|
373
|
+
self.il_creater.on_pdf_figure(child)
|
|
374
|
+
pass
|
|
375
|
+
elif isinstance(child, LTLine): # 线条
|
|
376
|
+
continue
|
|
377
|
+
layout = self.layout[ltpage.pageid]
|
|
378
|
+
# ltpage.height 可能是 fig 里面的高度,这里统一用 layout.shape
|
|
379
|
+
h, w = layout.shape
|
|
380
|
+
# 读取当前线条在 layout 中的类别
|
|
381
|
+
cx, cy = np.clip(int(child.x0), 0, w - 1), np.clip(int(child.y0), 0, h - 1)
|
|
382
|
+
cls = layout[cy, cx]
|
|
383
|
+
if vstk and cls == xt_cls: # 公式线条
|
|
384
|
+
vlstk.append(child)
|
|
385
|
+
else: # 全局线条
|
|
386
|
+
lstk.append(child)
|
|
387
|
+
else:
|
|
388
|
+
pass
|
|
389
|
+
return
|
|
390
|
+
# 处理结尾
|
|
391
|
+
if vstk: # 公式出栈
|
|
392
|
+
sstk[-1] += f"{{v{len(var)}}}"
|
|
393
|
+
var.append(vstk)
|
|
394
|
+
varl.append(vlstk)
|
|
395
|
+
varf.append(vfix)
|
|
396
|
+
log.debug("\n==========[VSTACK]==========\n")
|
|
397
|
+
for var_id, v in enumerate(var): # 计算公式宽度
|
|
398
|
+
l = max([vch.x1 for vch in v]) - v[0].x0
|
|
399
|
+
log.debug(f'< {l:.1f} {v[0].x0:.1f} {v[0].y0:.1f} {v[0].cid} {v[0].fontname} {len(varl[var_id])} > v{var_id} = {"".join([ch.get_text() for ch in v])}')
|
|
400
|
+
vlen.append(l)
|
|
401
|
+
|
|
402
|
+
############################################################
|
|
403
|
+
# B. 段落翻译
|
|
404
|
+
log.debug("\n==========[SSTACK]==========\n")
|
|
405
|
+
|
|
406
|
+
news = sstk.copy()
|
|
407
|
+
|
|
408
|
+
############################################################
|
|
409
|
+
# C. 新文档排版
|
|
410
|
+
def raw_string(fcur: str, cstk: str): # 编码字符串
|
|
411
|
+
if fcur == 'noto':
|
|
412
|
+
return "".join([f"{self.noto.has_glyph(ord(c)):04x}" for c in cstk])
|
|
413
|
+
elif isinstance(self.fontmap[fcur], PDFCIDFont): # 判断编码长度
|
|
414
|
+
return "".join([f"{ord(c):04x}" for c in cstk])
|
|
415
|
+
else:
|
|
416
|
+
return "".join([f"{ord(c):02x}" for c in cstk])
|
|
417
|
+
|
|
418
|
+
_x, _y = 0, 0
|
|
419
|
+
for para_id, new in enumerate(news):
|
|
420
|
+
x: float = pstk[para_id].x # 段落初始横坐标
|
|
421
|
+
y: float = pstk[para_id].y # 段落初始纵坐标
|
|
422
|
+
x0: float = pstk[para_id].x0 # 段落左边界
|
|
423
|
+
x1: float = pstk[para_id].x1 # 段落右边界
|
|
424
|
+
size: float = pstk[para_id].size # 段落字体大小
|
|
425
|
+
brk: bool = pstk[para_id].brk # 段落换行标记
|
|
426
|
+
cstk: str = "" # 当前文字栈
|
|
427
|
+
fcur: str = None # 当前字体 ID
|
|
428
|
+
tx = x
|
|
429
|
+
fcur_ = fcur
|
|
430
|
+
ptr = 0
|
|
431
|
+
log.debug(f"< {y} {x} {x0} {x1} {size} {brk} > {sstk[para_id]} | {new}")
|
|
432
|
+
while ptr < len(new):
|
|
433
|
+
vy_regex = re.match(
|
|
434
|
+
r"\{\s*v([\d\s]+)\}", new[ptr:], re.IGNORECASE,
|
|
435
|
+
) # 匹配 {vn} 公式标记
|
|
436
|
+
mod = 0 # 文字修饰符
|
|
437
|
+
if vy_regex: # 加载公式
|
|
438
|
+
ptr += len(vy_regex.group(0))
|
|
439
|
+
try:
|
|
440
|
+
vid = int(vy_regex.group(1).replace(" ", ""))
|
|
441
|
+
adv = vlen[vid]
|
|
442
|
+
except Exception as e:
|
|
443
|
+
log.debug("Skipping formula placeholder due to: %s", e)
|
|
444
|
+
continue # 翻译器可能会自动补个越界的公式标记
|
|
445
|
+
if var[vid][-1].get_text() and unicodedata.category(var[vid][-1].get_text()[0]) in ["Lm", "Mn", "Sk"]: # 文字修饰符
|
|
446
|
+
mod = var[vid][-1].width
|
|
447
|
+
else: # 加载文字
|
|
448
|
+
ch = new[ptr]
|
|
449
|
+
fcur_ = None
|
|
450
|
+
try:
|
|
451
|
+
if fcur_ is None and self.fontmap["tiro"].to_unichr(ord(ch)) == ch:
|
|
452
|
+
fcur_ = "tiro" # 默认拉丁字体
|
|
453
|
+
except Exception:
|
|
454
|
+
pass
|
|
455
|
+
if fcur_ is None:
|
|
456
|
+
fcur_ = self.resfont # 默认非拉丁字体
|
|
457
|
+
if fcur_ == 'noto':
|
|
458
|
+
adv = self.noto.char_lengths(ch, size)[0]
|
|
459
|
+
else:
|
|
460
|
+
adv = self.fontmap[fcur_].char_width(ord(ch)) * size
|
|
461
|
+
ptr += 1
|
|
462
|
+
if ( # 输出文字缓冲区
|
|
463
|
+
fcur_ != fcur # 1. 字体更新
|
|
464
|
+
or vy_regex # 2. 插入公式
|
|
465
|
+
or x + adv > x1 + 0.1 * size # 3. 到达右边界(可能一整行都被符号化,这里需要考虑浮点误差)
|
|
466
|
+
):
|
|
467
|
+
if cstk:
|
|
468
|
+
ops += f"/{fcur} {size:f} Tf 1 0 0 1 {tx:f} {y:f} Tm [<{raw_string(fcur, cstk)}>] TJ "
|
|
469
|
+
cstk = ""
|
|
470
|
+
if brk and x + adv > x1 + 0.1 * size: # 到达右边界且原文段落存在换行
|
|
471
|
+
x = x0
|
|
472
|
+
lang_space = {"zh-cn": 1.4, "zh-tw": 1.4, "zh-hans": 1.4, "zh-hant": 1.4, "zh": 1.4, "ja": 1.1, "ko": 1.2, "en": 1.2, "ar": 1.0, "ru": 0.8, "uk": 0.8, "ta": 0.8}
|
|
473
|
+
# y -= size * lang_space.get(self.translator.lang_out.lower(), 1.1) # 小语种大多适配 1.1
|
|
474
|
+
y -= size * 1.4
|
|
475
|
+
if vy_regex: # 插入公式
|
|
476
|
+
fix = 0
|
|
477
|
+
if fcur is not None: # 段落内公式修正纵向偏移
|
|
478
|
+
fix = varf[vid]
|
|
479
|
+
for vch in var[vid]: # 排版公式字符
|
|
480
|
+
vc = chr(vch.cid)
|
|
481
|
+
ops += f"/{self.fontid[vch.font]} {vch.size:f} Tf 1 0 0 1 {x + vch.x0 - var[vid][0].x0:f} {fix + y + vch.y0 - var[vid][0].y0:f} Tm <{raw_string(self.fontid[vch.font], vc)}> TJ "
|
|
482
|
+
if log.isEnabledFor(logging.DEBUG):
|
|
483
|
+
lstk.append(LTLine(0.1, (_x, _y), (x + vch.x0 - var[vid][0].x0, fix + y + vch.y0 - var[vid][0].y0)))
|
|
484
|
+
_x, _y = x + vch.x0 - var[vid][0].x0, fix + y + vch.y0 - var[vid][0].y0
|
|
485
|
+
for l in varl[vid]: # 排版公式线条
|
|
486
|
+
if l.linewidth < 5: # hack 有的文档会用粗线条当图片背景
|
|
487
|
+
ops += f"ET q 1 0 0 1 {l.pts[0][0] + x - var[vid][0].x0:f} {l.pts[0][1] + fix + y - var[vid][0].y0:f} cm [] 0 d 0 J {l.linewidth:f} w 0 0 m {l.pts[1][0] - l.pts[0][0]:f} {l.pts[1][1] - l.pts[0][1]:f} l S Q BT "
|
|
488
|
+
else: # 插入文字缓冲区
|
|
489
|
+
if not cstk: # 单行开头
|
|
490
|
+
tx = x
|
|
491
|
+
if x == x0 and ch == " ": # 消除段落换行空格
|
|
492
|
+
adv = 0
|
|
493
|
+
else:
|
|
494
|
+
cstk += ch
|
|
495
|
+
else:
|
|
496
|
+
cstk += ch
|
|
497
|
+
adv -= mod # 文字修饰符
|
|
498
|
+
fcur = fcur_
|
|
499
|
+
x += adv
|
|
500
|
+
if log.isEnabledFor(logging.DEBUG):
|
|
501
|
+
lstk.append(LTLine(0.1, (_x, _y), (x, y)))
|
|
502
|
+
_x, _y = x, y
|
|
503
|
+
# 处理结尾
|
|
504
|
+
if cstk:
|
|
505
|
+
ops += f"/{fcur} {size:f} Tf 1 0 0 1 {tx:f} {y:f} Tm <{raw_string(fcur, cstk)}> TJ "
|
|
506
|
+
for l in lstk: # 排版全局线条
|
|
507
|
+
if l.linewidth < 5: # hack 有的文档会用粗线条当图片背景
|
|
508
|
+
ops += f"ET q 1 0 0 1 {l.pts[0][0]:f} {l.pts[0][1]:f} cm [] 0 d 0 J {l.linewidth:f} w 0 0 m {l.pts[1][0] - l.pts[0][0]:f} {l.pts[1][1] - l.pts[0][1]:f} l S Q BT "
|
|
509
|
+
ops = f"BT {ops}ET "
|
|
510
|
+
return ops
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from babeldoc.document_il.il_version_1 import BaseOperations
|
|
2
|
+
from babeldoc.document_il.il_version_1 import Box
|
|
3
|
+
from babeldoc.document_il.il_version_1 import Cropbox
|
|
4
|
+
from babeldoc.document_il.il_version_1 import Document
|
|
5
|
+
from babeldoc.document_il.il_version_1 import GraphicState
|
|
6
|
+
from babeldoc.document_il.il_version_1 import Mediabox
|
|
7
|
+
from babeldoc.document_il.il_version_1 import Page
|
|
8
|
+
from babeldoc.document_il.il_version_1 import PageLayout
|
|
9
|
+
from babeldoc.document_il.il_version_1 import PdfCharacter
|
|
10
|
+
from babeldoc.document_il.il_version_1 import PdfFigure
|
|
11
|
+
from babeldoc.document_il.il_version_1 import PdfFont
|
|
12
|
+
from babeldoc.document_il.il_version_1 import PdfFormula
|
|
13
|
+
from babeldoc.document_il.il_version_1 import PdfLine
|
|
14
|
+
from babeldoc.document_il.il_version_1 import PdfParagraph
|
|
15
|
+
from babeldoc.document_il.il_version_1 import PdfParagraphComposition
|
|
16
|
+
from babeldoc.document_il.il_version_1 import PdfRectangle
|
|
17
|
+
from babeldoc.document_il.il_version_1 import PdfSameStyleCharacters
|
|
18
|
+
from babeldoc.document_il.il_version_1 import PdfSameStyleUnicodeCharacters
|
|
19
|
+
from babeldoc.document_il.il_version_1 import PdfStyle
|
|
20
|
+
from babeldoc.document_il.il_version_1 import PdfXobject
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"BaseOperations",
|
|
24
|
+
"Box",
|
|
25
|
+
"Cropbox",
|
|
26
|
+
"Document",
|
|
27
|
+
"GraphicState",
|
|
28
|
+
"Mediabox",
|
|
29
|
+
"Page",
|
|
30
|
+
"PageLayout",
|
|
31
|
+
"PdfCharacter",
|
|
32
|
+
"PdfFigure",
|
|
33
|
+
"PdfFont",
|
|
34
|
+
"PdfFormula",
|
|
35
|
+
"PdfLine",
|
|
36
|
+
"PdfParagraph",
|
|
37
|
+
"PdfParagraphComposition",
|
|
38
|
+
"PdfRectangle",
|
|
39
|
+
"PdfSameStyleCharacters",
|
|
40
|
+
"PdfSameStyleUnicodeCharacters",
|
|
41
|
+
"PdfStyle",
|
|
42
|
+
"PdfXobject",
|
|
43
|
+
]
|
|
File without changes
|