BabelDOC 0.1.6__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.6.dist-info/METADATA +283 -0
- babeldoc-0.1.6.dist-info/RECORD +42 -0
- babeldoc-0.1.6.dist-info/WHEEL +4 -0
- babeldoc-0.1.6.dist-info/entry_points.txt +2 -0
- babeldoc-0.1.6.dist-info/licenses/LICENSE +661 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
|
|
3
|
+
<xs:element name="document">
|
|
4
|
+
<xs:complexType>
|
|
5
|
+
<xs:sequence>
|
|
6
|
+
<xs:element maxOccurs="unbounded" ref="page"/>
|
|
7
|
+
</xs:sequence>
|
|
8
|
+
<xs:attribute name="totalPages" use="required" type="xs:int"/>
|
|
9
|
+
</xs:complexType>
|
|
10
|
+
</xs:element>
|
|
11
|
+
<xs:element name="page">
|
|
12
|
+
<xs:complexType>
|
|
13
|
+
<xs:sequence>
|
|
14
|
+
<xs:element ref="mediabox"/>
|
|
15
|
+
<xs:element ref="cropbox"/>
|
|
16
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pdfXobject"/>
|
|
17
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pageLayout"/>
|
|
18
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pdfRectangle"/>
|
|
19
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pdfFont"/>
|
|
20
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pdfParagraph"/>
|
|
21
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pdfFigure"/>
|
|
22
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pdfCharacter"/>
|
|
23
|
+
<xs:element ref="baseOperations"/>
|
|
24
|
+
</xs:sequence>
|
|
25
|
+
<xs:attribute name="pageNumber" use="required" type="xs:int"/>
|
|
26
|
+
<xs:attribute name="Unit" use="required" type="xs:string"/>
|
|
27
|
+
</xs:complexType>
|
|
28
|
+
</xs:element>
|
|
29
|
+
<xs:element name="mediabox">
|
|
30
|
+
<xs:complexType>
|
|
31
|
+
<xs:sequence>
|
|
32
|
+
<xs:element ref="box"/>
|
|
33
|
+
</xs:sequence>
|
|
34
|
+
</xs:complexType>
|
|
35
|
+
</xs:element>
|
|
36
|
+
<xs:element name="cropbox">
|
|
37
|
+
<xs:complexType>
|
|
38
|
+
<xs:sequence>
|
|
39
|
+
<xs:element ref="box"/>
|
|
40
|
+
</xs:sequence>
|
|
41
|
+
</xs:complexType>
|
|
42
|
+
</xs:element>
|
|
43
|
+
<xs:element name="baseOperations" type="xs:string"/>
|
|
44
|
+
<xs:element name="box">
|
|
45
|
+
<xs:complexType>
|
|
46
|
+
<xs:attribute name="x" use="required" type="xs:float"/>
|
|
47
|
+
<xs:attribute name="y" use="required" type="xs:float"/>
|
|
48
|
+
<xs:attribute name="x2" use="required" type="xs:float"/>
|
|
49
|
+
<xs:attribute name="y2" use="required" type="xs:float"/>
|
|
50
|
+
</xs:complexType>
|
|
51
|
+
</xs:element>
|
|
52
|
+
<xs:simpleType name="PDFXrefId">
|
|
53
|
+
<xs:restriction base="xs:int"/>
|
|
54
|
+
</xs:simpleType>
|
|
55
|
+
<xs:element name="pdfFont">
|
|
56
|
+
<xs:complexType>
|
|
57
|
+
<xs:attribute name="name" use="required" type="xs:string"/>
|
|
58
|
+
<xs:attribute name="fontId" use="required" type="xs:string"/>
|
|
59
|
+
<xs:attribute name="xrefId" use="required" type="PDFXrefId"/>
|
|
60
|
+
<xs:attribute name="encodingLength" use="required" type="xs:int"/>
|
|
61
|
+
<xs:attribute name="bold" type="xs:boolean"/>
|
|
62
|
+
<xs:attribute name="italic" type="xs:boolean"/>
|
|
63
|
+
<xs:attribute name="monospace" type="xs:boolean"/>
|
|
64
|
+
<xs:attribute name="serif" type="xs:boolean"/>
|
|
65
|
+
<xs:attribute name="ascent" type="xs:float"/>
|
|
66
|
+
<xs:attribute name="descent" type="xs:float"/>
|
|
67
|
+
</xs:complexType>
|
|
68
|
+
</xs:element>
|
|
69
|
+
<xs:element name="pdfXobject">
|
|
70
|
+
<xs:complexType>
|
|
71
|
+
<xs:sequence>
|
|
72
|
+
<xs:element ref="box"/>
|
|
73
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pdfFont"/>
|
|
74
|
+
<xs:element ref="baseOperations"/>
|
|
75
|
+
</xs:sequence>
|
|
76
|
+
<xs:attribute name="xobjId" use="required" type="xs:int"/>
|
|
77
|
+
<xs:attribute name="xrefId" use="required" type="PDFXrefId"/>
|
|
78
|
+
</xs:complexType>
|
|
79
|
+
</xs:element>
|
|
80
|
+
<xs:element name="pdfCharacter">
|
|
81
|
+
<xs:complexType>
|
|
82
|
+
<xs:sequence>
|
|
83
|
+
<xs:element ref="pdfStyle"/>
|
|
84
|
+
<xs:element ref="box"/>
|
|
85
|
+
</xs:sequence>
|
|
86
|
+
<xs:attribute name="vertical" type="xs:boolean"/>
|
|
87
|
+
<xs:attribute name="scale" type="xs:float"/>
|
|
88
|
+
<xs:attribute name="pdfCharacterId" type="xs:int"/>
|
|
89
|
+
<xs:attribute name="char_unicode" use="required" type="xs:string"/>
|
|
90
|
+
<xs:attribute name="advance" type="xs:float"/>
|
|
91
|
+
<xs:attribute name="xobjId" type="xs:int"/>
|
|
92
|
+
<xs:attribute name="debug_info" type="xs:boolean"/>
|
|
93
|
+
</xs:complexType>
|
|
94
|
+
</xs:element>
|
|
95
|
+
<xs:element name="pageLayout">
|
|
96
|
+
<xs:complexType>
|
|
97
|
+
<xs:sequence>
|
|
98
|
+
<xs:element ref="box"/>
|
|
99
|
+
</xs:sequence>
|
|
100
|
+
<xs:attribute name="id" use="required" type="xs:int"/>
|
|
101
|
+
<xs:attribute name="conf" use="required" type="xs:float"/>
|
|
102
|
+
<xs:attribute name="class_name" use="required" type="xs:string"/>
|
|
103
|
+
</xs:complexType>
|
|
104
|
+
</xs:element>
|
|
105
|
+
<xs:element name="graphicState">
|
|
106
|
+
<xs:complexType>
|
|
107
|
+
<xs:attribute name="linewidth" type="xs:float"/>
|
|
108
|
+
<xs:attribute name="dash">
|
|
109
|
+
<xs:simpleType>
|
|
110
|
+
<xs:restriction>
|
|
111
|
+
<xs:simpleType>
|
|
112
|
+
<xs:list itemType="xs:float"/>
|
|
113
|
+
</xs:simpleType>
|
|
114
|
+
<xs:minLength value="1"/>
|
|
115
|
+
</xs:restriction>
|
|
116
|
+
</xs:simpleType>
|
|
117
|
+
</xs:attribute>
|
|
118
|
+
<xs:attribute name="flatness" type="xs:float"/>
|
|
119
|
+
<xs:attribute name="intent" type="xs:string"/>
|
|
120
|
+
<xs:attribute name="linecap" type="xs:int"/>
|
|
121
|
+
<xs:attribute name="linejoin" type="xs:int"/>
|
|
122
|
+
<xs:attribute name="miterlimit" type="xs:float"/>
|
|
123
|
+
<xs:attribute name="ncolor">
|
|
124
|
+
<xs:simpleType>
|
|
125
|
+
<xs:restriction>
|
|
126
|
+
<xs:simpleType>
|
|
127
|
+
<xs:list itemType="xs:float"/>
|
|
128
|
+
</xs:simpleType>
|
|
129
|
+
<xs:minLength value="1"/>
|
|
130
|
+
</xs:restriction>
|
|
131
|
+
</xs:simpleType>
|
|
132
|
+
</xs:attribute>
|
|
133
|
+
<xs:attribute name="scolor">
|
|
134
|
+
<xs:simpleType>
|
|
135
|
+
<xs:restriction>
|
|
136
|
+
<xs:simpleType>
|
|
137
|
+
<xs:list itemType="xs:float"/>
|
|
138
|
+
</xs:simpleType>
|
|
139
|
+
<xs:minLength value="1"/>
|
|
140
|
+
</xs:restriction>
|
|
141
|
+
</xs:simpleType>
|
|
142
|
+
</xs:attribute>
|
|
143
|
+
<xs:attribute name="strokingColorSpaceName" type="xs:string"/>
|
|
144
|
+
<xs:attribute name="nonStrokingColorSpaceName" type="xs:string"/>
|
|
145
|
+
<xs:attribute name="passthroughPerCharInstruction" type="xs:string"/>
|
|
146
|
+
</xs:complexType>
|
|
147
|
+
</xs:element>
|
|
148
|
+
<xs:element name="pdfStyle">
|
|
149
|
+
<xs:complexType>
|
|
150
|
+
<xs:sequence>
|
|
151
|
+
<xs:element ref="graphicState"/>
|
|
152
|
+
</xs:sequence>
|
|
153
|
+
<xs:attribute name="font_id" use="required" type="xs:string"/>
|
|
154
|
+
<xs:attribute name="font_size" use="required" type="xs:float"/>
|
|
155
|
+
</xs:complexType>
|
|
156
|
+
</xs:element>
|
|
157
|
+
<xs:element name="pdfParagraph">
|
|
158
|
+
<xs:complexType>
|
|
159
|
+
<xs:sequence>
|
|
160
|
+
<xs:element ref="box"/>
|
|
161
|
+
<xs:element ref="pdfStyle"/>
|
|
162
|
+
<xs:element minOccurs="0" maxOccurs="unbounded" ref="pdfParagraphComposition"/>
|
|
163
|
+
</xs:sequence>
|
|
164
|
+
<xs:attribute name="xobjId" type="xs:int"/>
|
|
165
|
+
<xs:attribute name="unicode" use="required" type="xs:string"/>
|
|
166
|
+
<xs:attribute name="scale" type="xs:float"/>
|
|
167
|
+
<xs:attribute name="vertical" type="xs:boolean"/>
|
|
168
|
+
<xs:attribute name="FirstLineIndent" type="xs:boolean"/>
|
|
169
|
+
<xs:attribute name="debug_id" type="xs:string"/>
|
|
170
|
+
</xs:complexType>
|
|
171
|
+
</xs:element>
|
|
172
|
+
<xs:element name="pdfParagraphComposition">
|
|
173
|
+
<xs:complexType>
|
|
174
|
+
<xs:choice>
|
|
175
|
+
<xs:element ref="pdfLine"/>
|
|
176
|
+
<xs:element ref="pdfFormula"/>
|
|
177
|
+
<xs:element ref="pdfSameStyleCharacters"/>
|
|
178
|
+
<xs:element ref="pdfCharacter"/>
|
|
179
|
+
<xs:element ref="pdfSameStyleUnicodeCharacters"/>
|
|
180
|
+
</xs:choice>
|
|
181
|
+
</xs:complexType>
|
|
182
|
+
</xs:element>
|
|
183
|
+
<xs:element name="pdfLine">
|
|
184
|
+
<xs:complexType>
|
|
185
|
+
<xs:sequence>
|
|
186
|
+
<xs:element ref="box"/>
|
|
187
|
+
<xs:element maxOccurs="unbounded" ref="pdfCharacter"/>
|
|
188
|
+
</xs:sequence>
|
|
189
|
+
</xs:complexType>
|
|
190
|
+
</xs:element>
|
|
191
|
+
<xs:element name="pdfSameStyleCharacters">
|
|
192
|
+
<xs:complexType>
|
|
193
|
+
<xs:sequence>
|
|
194
|
+
<xs:element ref="box"/>
|
|
195
|
+
<xs:element ref="pdfStyle"/>
|
|
196
|
+
<xs:element maxOccurs="unbounded" ref="pdfCharacter"/>
|
|
197
|
+
</xs:sequence>
|
|
198
|
+
</xs:complexType>
|
|
199
|
+
</xs:element>
|
|
200
|
+
<xs:element name="pdfSameStyleUnicodeCharacters">
|
|
201
|
+
<xs:complexType>
|
|
202
|
+
<xs:sequence>
|
|
203
|
+
<xs:element minOccurs="0" ref="pdfStyle"/>
|
|
204
|
+
</xs:sequence>
|
|
205
|
+
<xs:attribute name="unicode" use="required" type="xs:string"/>
|
|
206
|
+
<xs:attribute name="debug_info" type="xs:boolean"/>
|
|
207
|
+
</xs:complexType>
|
|
208
|
+
</xs:element>
|
|
209
|
+
<xs:element name="pdfFormula">
|
|
210
|
+
<xs:complexType>
|
|
211
|
+
<xs:sequence>
|
|
212
|
+
<xs:element ref="box"/>
|
|
213
|
+
<xs:element maxOccurs="unbounded" ref="pdfCharacter"/>
|
|
214
|
+
</xs:sequence>
|
|
215
|
+
<xs:attribute name="x_offset" use="required" type="xs:float"/>
|
|
216
|
+
<xs:attribute name="y_offset" use="required" type="xs:float"/>
|
|
217
|
+
</xs:complexType>
|
|
218
|
+
</xs:element>
|
|
219
|
+
<xs:element name="pdfFigure">
|
|
220
|
+
<xs:complexType>
|
|
221
|
+
<xs:sequence>
|
|
222
|
+
<xs:element ref="box"/>
|
|
223
|
+
</xs:sequence>
|
|
224
|
+
</xs:complexType>
|
|
225
|
+
</xs:element>
|
|
226
|
+
<xs:element name="pdfRectangle">
|
|
227
|
+
<xs:complexType>
|
|
228
|
+
<xs:sequence>
|
|
229
|
+
<xs:element ref="box"/>
|
|
230
|
+
<xs:element ref="graphicState"/>
|
|
231
|
+
</xs:sequence>
|
|
232
|
+
<xs:attribute name="debug_info" type="xs:boolean"/>
|
|
233
|
+
</xs:complexType>
|
|
234
|
+
</xs:element>
|
|
235
|
+
</xs:schema>
|
|
File without changes
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import babeldoc.document_il.il_version_1 as il_version_1
|
|
4
|
+
from babeldoc.document_il import GraphicState
|
|
5
|
+
from babeldoc.document_il.utils.style_helper import BLUE
|
|
6
|
+
from babeldoc.document_il.utils.style_helper import ORANGE
|
|
7
|
+
from babeldoc.document_il.utils.style_helper import YELLOW
|
|
8
|
+
from babeldoc.translation_config import TranslationConfig
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AddDebugInformation:
|
|
14
|
+
stage_name = "添加DEBUG信息"
|
|
15
|
+
|
|
16
|
+
def __init__(self, translation_config: TranslationConfig):
|
|
17
|
+
self.translation_config = translation_config
|
|
18
|
+
self.model = translation_config.doc_layout_model
|
|
19
|
+
|
|
20
|
+
def process(self, docs: il_version_1.Document):
|
|
21
|
+
if not self.translation_config.debug:
|
|
22
|
+
return
|
|
23
|
+
|
|
24
|
+
for page in docs.page:
|
|
25
|
+
self.process_page(page)
|
|
26
|
+
|
|
27
|
+
def _create_rectangle(self, box: il_version_1.Box, color: GraphicState):
|
|
28
|
+
rect = il_version_1.PdfRectangle(
|
|
29
|
+
box=box,
|
|
30
|
+
graphic_state=color,
|
|
31
|
+
debug_info=True,
|
|
32
|
+
)
|
|
33
|
+
return rect
|
|
34
|
+
|
|
35
|
+
def _create_text(self, text: str, color: GraphicState, box: il_version_1.Box):
|
|
36
|
+
style = il_version_1.PdfStyle(
|
|
37
|
+
font_id="china-ss",
|
|
38
|
+
font_size=4,
|
|
39
|
+
graphic_state=color,
|
|
40
|
+
)
|
|
41
|
+
return il_version_1.PdfParagraph(
|
|
42
|
+
first_line_indent=False,
|
|
43
|
+
box=il_version_1.Box(
|
|
44
|
+
x=box.x,
|
|
45
|
+
y=box.y2,
|
|
46
|
+
x2=box.x2,
|
|
47
|
+
y2=box.y2 + 5,
|
|
48
|
+
),
|
|
49
|
+
vertical=False,
|
|
50
|
+
pdf_style=style,
|
|
51
|
+
unicode=text,
|
|
52
|
+
pdf_paragraph_composition=[
|
|
53
|
+
il_version_1.PdfParagraphComposition(
|
|
54
|
+
pdf_same_style_unicode_characters=il_version_1.PdfSameStyleUnicodeCharacters(
|
|
55
|
+
unicode=text,
|
|
56
|
+
pdf_style=style,
|
|
57
|
+
debug_info=True,
|
|
58
|
+
),
|
|
59
|
+
),
|
|
60
|
+
],
|
|
61
|
+
xobj_id=-1,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
def process_page(self, page: il_version_1.Page):
|
|
65
|
+
# Add page number text at top-left corner
|
|
66
|
+
page_width = page.cropbox.box.x2 - page.cropbox.box.x
|
|
67
|
+
page_height = page.cropbox.box.y2 - page.cropbox.box.y
|
|
68
|
+
page_number_text = f"pagenumber: {page.page_number}"
|
|
69
|
+
page_number_box = il_version_1.Box(
|
|
70
|
+
x=page.cropbox.box.x + page_width * 0.02,
|
|
71
|
+
y=page.cropbox.box.y,
|
|
72
|
+
x2=page.cropbox.box.x2,
|
|
73
|
+
y2=page.cropbox.box.y2 - page_height * 0.02,
|
|
74
|
+
)
|
|
75
|
+
page_number_paragraph = self._create_text(
|
|
76
|
+
page_number_text,
|
|
77
|
+
BLUE,
|
|
78
|
+
page_number_box,
|
|
79
|
+
)
|
|
80
|
+
page.pdf_paragraph.append(page_number_paragraph)
|
|
81
|
+
|
|
82
|
+
new_paragraphs = []
|
|
83
|
+
|
|
84
|
+
for paragraph in page.pdf_paragraph:
|
|
85
|
+
if not paragraph.pdf_paragraph_composition:
|
|
86
|
+
continue
|
|
87
|
+
if any(
|
|
88
|
+
x.pdf_same_style_unicode_characters.debug_info
|
|
89
|
+
for x in paragraph.pdf_paragraph_composition
|
|
90
|
+
if x.pdf_same_style_unicode_characters
|
|
91
|
+
):
|
|
92
|
+
continue
|
|
93
|
+
# Create a rectangle box
|
|
94
|
+
rect = self._create_rectangle(paragraph.box, BLUE)
|
|
95
|
+
|
|
96
|
+
page.pdf_rectangle.append(rect)
|
|
97
|
+
|
|
98
|
+
# Create text label at top-left corner
|
|
99
|
+
# Note: PDF coordinates are from bottom-left,
|
|
100
|
+
# so we use y2 for top position
|
|
101
|
+
|
|
102
|
+
debug_text = "paragraph"
|
|
103
|
+
if hasattr(paragraph, "debug_id") and paragraph.debug_id:
|
|
104
|
+
debug_text = f"paragraph[{paragraph.debug_id}]"
|
|
105
|
+
new_paragraphs.append(self._create_text(debug_text, BLUE, paragraph.box))
|
|
106
|
+
|
|
107
|
+
for composition in paragraph.pdf_paragraph_composition:
|
|
108
|
+
if composition.pdf_formula:
|
|
109
|
+
new_paragraphs.append(
|
|
110
|
+
self._create_text(
|
|
111
|
+
"formula",
|
|
112
|
+
ORANGE,
|
|
113
|
+
composition.pdf_formula.box,
|
|
114
|
+
),
|
|
115
|
+
)
|
|
116
|
+
page.pdf_rectangle.append(
|
|
117
|
+
self._create_rectangle(
|
|
118
|
+
composition.pdf_formula.box,
|
|
119
|
+
ORANGE,
|
|
120
|
+
),
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
for xobj in page.pdf_xobject:
|
|
124
|
+
new_paragraphs.append(
|
|
125
|
+
self._create_text(
|
|
126
|
+
"xobj",
|
|
127
|
+
YELLOW,
|
|
128
|
+
xobj.box,
|
|
129
|
+
),
|
|
130
|
+
)
|
|
131
|
+
page.pdf_rectangle.append(
|
|
132
|
+
self._create_rectangle(
|
|
133
|
+
xobj.box,
|
|
134
|
+
YELLOW,
|
|
135
|
+
),
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
page.pdf_paragraph.extend(new_paragraphs)
|