Skip to content

Commit 8e26b91

Browse files
committed
实现 MonoBitmap.draw()
1 parent cb8f42a commit 8e26b91

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/pixel_font_knife/mono_bitmap.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import UserList
2+
from io import StringIO
23
from os import PathLike
34
from typing import Any
45

@@ -69,6 +70,16 @@ def resize(self, left: int = 0, right: int = 0, top: int = 0, bottom: int = 0) -
6970
bitmap.append(bitmap_row)
7071
return bitmap
7172

73+
def draw(self, white: str = ' ', black: str = '██', end: str | None = None) -> str:
74+
text = StringIO()
75+
for bitmap_row in self:
76+
for alpha in bitmap_row:
77+
text.write(white if alpha == 0 else black)
78+
if end is not None:
79+
text.write(end)
80+
text.write('\n')
81+
return text.getvalue()
82+
7283
def save_png(
7384
self,
7485
file_path: str | bytes | PathLike[str] | PathLike[bytes],

tests/test_mono_bitmap.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ def test_resize():
104104
])
105105

106106

107+
def test_draw():
108+
bitmap = MonoBitmap([
109+
[1, 1, 0, 0],
110+
[0, 0, 1, 1],
111+
])
112+
text = ('████ *\n'
113+
' ████*\n')
114+
assert bitmap.draw(end='*') == text
115+
116+
107117
def test_load_save(glyphs_dir: Path, tmp_path: Path):
108118
black_load_dir = glyphs_dir.joinpath('black')
109119
black_save_dir = tmp_path.joinpath('black')

0 commit comments

Comments
 (0)