-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmunisy
executable file
·87 lines (73 loc) · 3.61 KB
/
rmunisy
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
#!/usr/bin/python3
import fontforge
from pathlib import Path
from sys import exit as abort
class RMUnisy():
def __init__(self, fonts: list, unicode_data: tuple[str|Path, int], output_dir: Path, name_mark: str="") -> None:
# check arguments
for font in fonts:
self.__isfile(font)
self.__isdir(output_dir)
# init object
self.unicode = self.get_unicode(*unicode_data)
self.fonts = fonts
self.output_dir = output_dir
self.name_mark = name_mark
def __isfile(self, path: Path) -> bool:
if path.is_file():
return True
abort(f"{self.__class__.__name__}: {path} is not a file.")
def __isdir(self, path: Path) -> bool:
if path.is_dir():
return True
abort(f"{self.__class__.__name__}: {path} is not a directory.")
def __clean_data(self, raw) -> list:
if isinstance(raw, str):
if raw == "":
abort(f"{self.__class__.__name__}: There is no unicode characters to delete.")
data = raw.strip().split(" ")
else:
self.__isfile(raw)
with open(raw, "r") as file:
data = file.read().strip().split("\n")
return data
def get_unicode(self, raw, base: int=10) -> list:
if base > 36 or base < 2: # int function required base to be with those conditions.
abort(f"{self.__class__.__name__}: Base must be >= 2 and <= 36.")
data = self.__clean_data(raw)
try:
unicodes = []
for u in data:
if "-" in u:
borders = [int(border, base) for border in u.split("-")] # range function need int arguments.
unicodes += list(range(borders[0], borders[1] + 1))
else:
unicodes.append(int(u, base))
except ValueError:
abort(f"{self.__class__.__name__}: Invalid unicode of base {base}.")
return unicodes
def patch(self) -> bool:
for path in self.fonts:
font = fontforge.open(str(path))
font.selection.select(("unicode", None), *self.unicode)
font.clear()
font.generate(f"{self.output_dir}/{path.stem}{self.name_mark}{path.suffix}")
font.close()
return True
def main() -> bool:
# Resolve arguments
parser = ArgumentParser(description="Delete unicode characters from a font using FontForge")
parser.add_argument('fonts_path', type=Path, nargs="+", help="The path to the fonts to patch")
parser.add_argument('-i', '--input', dest="unicode", type=str, default="", help="A string with the unicode characters to remove (separated by a space)")
parser.add_argument('-if', '--input-file', dest="unicode", type=Path, help="The path of the file contain the unicodes to delete")
parser.add_argument('-b', '--base', dest="base", type=int, default=10, help="The number base of the unicodes in input (default is 10)")
parser.add_argument('-o', '--output-dir', dest="output_dir", required=False, type=Path, default=Path("."), help="Output Directory (default is working dir)")
parser.add_argument('-m', '--mark=', dest="mark", required=False, type=str, default="", help="String to add to the patched file's name")
arguments_manager = parser.parse_args()
program = RMUnisy(fonts=arguments_manager.fonts_path, unicode_data=(arguments_manager.unicode, arguments_manager.base),
output_dir=arguments_manager.output_dir, name_mark=arguments_manager.mark)
program.patch()
return True
if __name__ == "__main__":
from argparse import ArgumentParser
main()