-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonts.py
239 lines (180 loc) · 5.56 KB
/
fonts.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from interfaces import AbstractTag, TagRenderFormat
class FontTag(AbstractTag):
def render(self, text: str) -> str:
rendered_string = None
text = self.pre_processing(text)
if self.render_format == TagRenderFormat.BOTH_TAGS:
rendered_string = f"{self.open_tag}{text}{self.close_tag}"
elif self.render_format == TagRenderFormat.LEFT_TAG_ONLY:
rendered_string = f"{self.open_tag}{text}"
elif self.render_format == TagRenderFormat.RIGHT_TAG_ONLY:
rendered_string = f"{text}{self.close_tag}"
elif self.render_format == TagRenderFormat.NO_TAGS:
rendered_string = text
return self.post_processing(rendered_string)
def pre_processing(self, text: str):
return text
def post_processing(self, rendered_string: str):
if "#" in rendered_string:
rendered_string = rendered_string.replace('*', '')
return f"\n\n{rendered_string}\n\n"
return rendered_string
class Image(FontTag):
open_tag = None
close_tag = None
render_format = TagRenderFormat.NO_TAGS
def pre_processing(self, text: str):
if not str(text[0]).isalnum():
text = text[1:]
if not str(text[-1]).isalnum():
text = text[:-1]
return f"\n{text}\n"
class Blockquote(FontTag):
open_tag = '>'
close_tag = None
render_format = TagRenderFormat.LEFT_TAG_ONLY
class SubscriptItalicBold(FontTag):
open_tag = '***~'
close_tag = '~***'
render_format = TagRenderFormat.BOTH_TAGS
class SubscriptItalic(FontTag):
open_tag = '*~'
close_tag = '~*'
render_format = TagRenderFormat.BOTH_TAGS
class Subscript(FontTag):
open_tag = '~'
close_tag = '~'
render_format = TagRenderFormat.BOTH_TAGS
class SuperscriptItalicBold(FontTag):
open_tag = '***^'
close_tag = '^***'
render_format = TagRenderFormat.BOTH_TAGS
class SuperscriptItalic(FontTag):
open_tag = '*^'
close_tag = '^*'
render_format = TagRenderFormat.BOTH_TAGS
class Superscript(FontTag):
open_tag = '^'
close_tag = '^'
render_format = TagRenderFormat.BOTH_TAGS
class ItalicBold(FontTag):
open_tag = '***'
close_tag = '***'
render_format = TagRenderFormat.BOTH_TAGS
class Italic(FontTag):
open_tag = '*'
close_tag = '*'
render_format = TagRenderFormat.BOTH_TAGS
# FIXME its not everytime is subtitles
# def post_processing(self, rendered_string: str):
# if len(rendered_string.split(" ")) > 1:
# rendered_string = f"\n\n{rendered_string}\n\n"
# return rendered_string
class Bold(FontTag):
open_tag = '**'
close_tag = '**'
render_format = TagRenderFormat.BOTH_TAGS
class Title(FontTag):
open_tag = '#'
close_tag = '\n'
render_format = TagRenderFormat.BOTH_TAGS
class ParagraphTheme(FontTag):
open_tag = '##'
close_tag = '\n'
render_format = TagRenderFormat.BOTH_TAGS
class ParagraphSubtheme(FontTag):
open_tag = '###'
close_tag = '\n'
render_format = TagRenderFormat.BOTH_TAGS
class ChapterNumber(FontTag):
open_tag = '####'
close_tag = '\n'
render_format = TagRenderFormat.BOTH_TAGS
class Code(FontTag):
open_tag = '```'
close_tag = '```'
render_format = TagRenderFormat.BOTH_TAGS
keyword_open_tag = '`'
keyword_close_tag = '`'
code_open_tag = '\n```\n'
code_close_tag = '\n```\n'
def pre_processing(self, text: str):
if '\n' not in text:
self.open_tag = self.keyword_open_tag
self.close_tag = self.keyword_close_tag
else:
self.open_tag = self.code_open_tag
self.close_tag = self.code_close_tag
return text
def post_processing(self, rendered_string: str):
rendered_string = rendered_string.replace(' ', ' ')
return rendered_string
class Text(FontTag):
open_tag = None
close_tag = None
render_format = TagRenderFormat.NO_TAGS
FONT_SIZE_SIGNATURE_MAP = {
31: "#",
30: '#',
29: "#",
28: "#",
27: "#",
26: "#",
25: "###",
24: "###",
23: "###",
22: "###",
21: "###",
20: "###",
19: "###",
18: "###",
17: "###",
16: "####",
15: "####",
14: "######",
13: "######",
12: "######",
}
TITLE_SIZE_MIN = min(FONT_SIZE_SIGNATURE_MAP.keys())
TITLE_SIZE_MAX = max(FONT_SIZE_SIGNATURE_MAP.keys())
AVAIlABLE_FONTS_FOR_MAPPING = [
Text, Italic, Bold,
ItalicBold, Code, Image,
Blockquote, ChapterNumber,
Subscript, SubscriptItalic, SubscriptItalicBold,
Superscript, SuperscriptItalic, SuperscriptItalicBold,
Title, ParagraphTheme, ParagraphSubtheme
]
FONT_MAP = {
"sans, proportional": Text,
"serifed, proportional": Text,
"serifed, proportional, bold": Bold,
"serifed, monospaced": Code,
"serifed, monospaced, bold": Code,
"italic, serifed, proportional": Italic,
"italic, serifed, proportional, bold": ItalicBold,
"italic, serifed, monospaced": Code,
"superscript, italic, serifed, proportional": SuperscriptItalic,
"image": Image,
}
def flags_decomposer(flags):
"""
Make font flags human readable.
Useful for PyMuPDF font flags to unwrap them from byte view
"""
l = []
if flags & 2 ** 0:
l.append("superscript")
if flags & 2 ** 1:
l.append("italic")
if flags & 2 ** 2:
l.append("serifed")
else:
l.append("sans")
if flags & 2 ** 3:
l.append("monospaced")
else:
l.append("proportional")
if flags & 2 ** 4:
l.append("bold")
return ", ".join(l)