diff options
author | rom <romangraef@gmail.com> | 2021-04-24 01:56:51 +0200 |
---|---|---|
committer | rom <romangraef@gmail.com> | 2021-04-24 01:56:51 +0200 |
commit | 156114359f6bb26a3cca198ac7a31bddc44381f7 (patch) | |
tree | a830599ffe4bc502d766861da8787c063b7a1a47 /txtgameengine/fonts.py | |
parent | 79a6c6b13f42a6a2cd4a1b0dd4a93e25756e1b87 (diff) | |
download | txtgameengine-156114359f6bb26a3cca198ac7a31bddc44381f7.tar.gz txtgameengine-156114359f6bb26a3cca198ac7a31bddc44381f7.tar.bz2 txtgameengine-156114359f6bb26a3cca198ac7a31bddc44381f7.zip |
fonts
Diffstat (limited to 'txtgameengine/fonts.py')
-rw-r--r-- | txtgameengine/fonts.py | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/txtgameengine/fonts.py b/txtgameengine/fonts.py index 60660d6..fcbf8b5 100644 --- a/txtgameengine/fonts.py +++ b/txtgameengine/fonts.py @@ -1,15 +1,21 @@ +import os from abc import ABC from dataclasses import dataclass from typing import Optional +import typing +import xml.dom.minidom as minidom + +if typing.TYPE_CHECKING: + from .twod import Texture + from .app import TxtGameApp class Font(ABC): - def get_glyph(self, char: str) -> Optional['Glyph']: - raise NotImplementedError() + def __init__(self): + self.glyphs = dict() - @property - def font(self): - raise NotImplementedError() + def get_glyph(self, char: str) -> Optional['Glyph']: + return self.glyphs.get(char) @dataclass @@ -17,10 +23,35 @@ class Glyph: font: 'Font' x_texture_offset: int y_texture_offset: int - x_width: int - y_width: int + x_texture_width: int + y_texture_width: int + x_render_offset: int + y_render_offset: int + x_advance: int + + +class BitmapFont(Font): + def __init__(self, texture: Texture, xml_path: str): + self.texture = texture + dom = minidom.parse(xml_path) + for char in dom.getElementsByTagName('Char'): + code = char.attributes['code'].value + width = char.attributes['width'].value + x_render_offset, y_render_offset = char.attributes['offset'].value.split(' ') + x_texture_offset, y_texture_offset, x_texture_width, y_texture_width = char.attributes['rect'].split(' ') + self.glyphs[code] = \ + Glyph(self, int(x_texture_offset), int(y_texture_offset), int(x_texture_width), int(y_texture_width), + int(x_render_offset), int(y_render_offset), int(width)) + + @classmethod + def load(cls, app: 'TxtGameApp', image_path: os.PathLike, xml_path: os.PathLike): + from .twod import Texture + return cls(Texture(app, image_path), xml_path) + +class TextRenderer: + def __init__(self, app: 'TxtGameApp'): + self.app = app -class MonospacedFont(Font): - def get_glyph(self, char: str): - pass + def use_font(self, font: Font): + self.font = font |