Skip to content

Commit

Permalink
实现 MonoBitmap.scale()
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 4, 2024
1 parent 8e26b91 commit bf60417
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 0 deletions.
Binary file added assets/glyphs/x1.5/4E2D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/glyphs/x1.5/6DF9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/glyphs/x1.5/7C23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/glyphs/x1.5/8A57.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/glyphs/x3/4E2D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/glyphs/x3/6DF9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/glyphs/x3/7C23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/glyphs/x3/8A57.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/pixel_font_knife/mono_bitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ def resize(self, left: int = 0, right: int = 0, top: int = 0, bottom: int = 0) -
bitmap.append(bitmap_row)
return bitmap

def scale(self, scale_x: float = 1, scale_y: float = 1) -> 'MonoBitmap':
bitmap = MonoBitmap()
bitmap.width = int(self.width * scale_x)
bitmap.height = int(self.height * scale_y)
for y in range(bitmap.height):
sy = int(y / scale_y)
bitmap_row = []
for x in range(bitmap.width):
sx = int(x / scale_x)
bitmap_row.append(self[sy][sx])
bitmap.append(bitmap_row)
return bitmap

def draw(self, white: str = ' ', black: str = '██', end: str | None = None) -> str:
text = StringIO()
for bitmap_row in self:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_mono_bitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ def test_resize():
])


def test_scale(glyphs_dir: Path):
for file_path in glyphs_dir.joinpath('black').iterdir():
x1_bitmap = MonoBitmap.load_png(file_path)
x3_bitmap = MonoBitmap.load_png(glyphs_dir.joinpath('x3', file_path.name))
x1_5_bitmap = MonoBitmap.load_png(glyphs_dir.joinpath('x1.5', file_path.name))
assert x1_bitmap.scale(3, 3) == x3_bitmap
assert x1_bitmap.scale(1.5, 1.5) == x1_5_bitmap
assert x3_bitmap.scale(1 / 3, 1 / 3) == x1_bitmap
assert x3_bitmap.scale(0.5, 0.5) == x1_5_bitmap


def test_draw():
bitmap = MonoBitmap([
[1, 1, 0, 0],
Expand Down

0 comments on commit bf60417

Please sign in to comment.