|
1 | 1 | import string
|
2 | 2 | from dataclasses import dataclass
|
| 3 | +from importlib.resources import as_file |
3 | 4 | from itertools import product
|
4 |
| -from typing import NamedTuple |
| 5 | +from pathlib import Path |
| 6 | +from typing import NamedTuple, ContextManager |
5 | 7 |
|
6 | 8 | from PIL import Image
|
7 | 9 |
|
8 |
| -from box import Box |
9 |
| -from pixels import Pixel |
10 |
| -from simple_image import SimpleImage |
| 10 | +from parse_qwantz.box import Box |
| 11 | +from parse_qwantz.pixels import Pixel |
| 12 | +from parse_qwantz.simple_image import SimpleImage |
11 | 13 |
|
12 | 14 | PRINTABLE = string.printable.strip()
|
13 | 15 | FORBIDDEN_CHARS = '\\_`|~'
|
14 | 16 |
|
15 |
| -REGULAR13_SHAPE_FILE = 'img/regular13.png' |
16 |
| -REGULAR12_SHAPE_FILE = 'img/regular12.png' |
17 |
| -REGULAR11_SHAPE_FILE = 'img/regular11.png' |
18 |
| -REGULAR9_SHAPE_FILE = 'img/regular9.png' |
19 |
| -REGULAR8_SHAPE_FILE = 'img/regular8.png' |
| 17 | +FONT_SIZES = [(13, 'Regular'), (12, 'Condensed'), (11, 'Small'), (9, 'Mini'), (8, 'Tiny')] |
| 18 | +SHIFTED_VARIANTS = { |
| 19 | + 13: {',': 1, ':': 1, '.': -1}, |
| 20 | +} |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class CharBox(NamedTuple):
|
@@ -90,9 +91,10 @@ def __repr__(self):
|
90 | 91 |
|
91 | 92 | @classmethod
|
92 | 93 | def from_file(
|
93 |
| - cls, file_path: str, name: str, shifted_variants: dict[str, int] | None = None |
| 94 | + cls, file_path_context_manager: ContextManager[Path], name: str, shifted_variants: dict[str, int] | None = None |
94 | 95 | ) -> "Font":
|
95 |
| - image = SimpleImage.from_image(Image.open(file_path)) |
| 96 | + with file_path_context_manager as file_path: |
| 97 | + image = SimpleImage.from_image(Image.open(file_path)) |
96 | 98 | width = image.width // len(PRINTABLE)
|
97 | 99 | height = image.height
|
98 | 100 | shapes = {}
|
@@ -150,10 +152,11 @@ def get_bitmask(pixel: Pixel, image: SimpleImage, width: int, height: int) -> in
|
150 | 152 | return bitmask
|
151 | 153 |
|
152 | 154 |
|
153 |
| -REGULAR_FONT = Font.from_file(REGULAR13_SHAPE_FILE, 'Regular', shifted_variants={',': 1, ':': 1, '.': -1}) |
154 |
| -CONDENSED_FONT = Font.from_file(REGULAR12_SHAPE_FILE, 'Condensed') |
155 |
| -SMALL_FONT = Font.from_file(REGULAR11_SHAPE_FILE, 'Small') |
156 |
| -MINI_FONT = Font.from_file(REGULAR9_SHAPE_FILE, 'Mini') |
157 |
| -TINY_FONT = Font.from_file(REGULAR8_SHAPE_FILE, 'Tiny') |
158 |
| - |
159 |
| -ALL_FONTS = [REGULAR_FONT, SMALL_FONT, TINY_FONT, CONDENSED_FONT, MINI_FONT] |
| 155 | +ALL_FONTS = [ |
| 156 | + Font.from_file( |
| 157 | + file_path_context_manager=as_file(Path('parse_qwantz', 'img', f'regular{size}.png')), |
| 158 | + name=name, |
| 159 | + shifted_variants=SHIFTED_VARIANTS.get(size, {}), |
| 160 | + ) |
| 161 | + for size, name in FONT_SIZES |
| 162 | +] |
0 commit comments