99from mgtools .constants import EXPORT_FONT_ATLAS_EXTENSION , EXPORT_FONT_METADATA_FILENAME
1010from mgtools .dataclasses .glyph import Glyph
1111from mgtools .enumerators .data_type import DataType
12- from mgtools .file import File
1312from mgtools .mg1 .constants import (
1413 FONT_EXPORT_ATLAS_HEIGHT ,
1514 FONT_EXPORT_ATLAS_WIDTH ,
1817 FONT_MINIMUM_PAGE_SIZE ,
1918 FONT_START_CHAR ,
2019)
21- from mgtools .mgscii import get_mgscii_char
20+ from mgtools .resource .asset import Asset
21+ from mgtools .utilities .mgscii import get_mgscii_char
2222
2323
24- class Font (File ):
24+ class Font (Asset ):
2525 @property
2626 def raw_data (self ) -> bytes :
2727 page_bytes = b""
@@ -50,7 +50,7 @@ def raw_data(self) -> bytes:
5050 )
5151
5252 return (
53- self .data_type .value . to_bytes (2 )
53+ self .data_type .to_bytes (2 )
5454 + b"\0 \0 " # Identifier placeholder
5555 + page_bytes
5656 )
@@ -63,7 +63,7 @@ def __init__(self, data_type: DataType) -> None:
6363 def from_stream (
6464 reader : BufferedReader | BytesIO ,
6565 data_type : DataType | None = None ,
66- ) -> "Font" :
66+ ) -> Asset :
6767 if data_type is None :
6868 data_type = DataType (int .from_bytes (reader .read (2 )))
6969
@@ -82,7 +82,23 @@ def from_stream(
8282 return f
8383
8484 @staticmethod
85- def from_file (file_path : Path , data_type : DataType | None = None ) -> File :
85+ def __encode_glyph (
86+ width : int ,
87+ bitmap_bytes : bytes ,
88+ glyphs_data : bytearray ,
89+ bitmap_data : bytearray ,
90+ ) -> None :
91+ """Append one glyph's header + bitmap to the accumulators."""
92+ glyphs_data .extend (len (bitmap_data ).to_bytes (2 , "little" ))
93+ glyphs_data .extend (
94+ (0 ).to_bytes (2 ) if width == 1 else ((width - 1 ) << 4 ).to_bytes (2 , "little" )
95+ )
96+
97+ if width != 1 and width != 4096 :
98+ bitmap_data .extend (bitmap_bytes )
99+
100+ @staticmethod
101+ def from_file (file_path : Path , data_type : DataType | None = None ) -> Asset :
86102 xmlroot = ET .parse (file_path / EXPORT_FONT_METADATA_FILENAME ).getroot ()
87103 page_elements = xmlroot .findall ("Page" )
88104 page_bytes = b""
@@ -107,29 +123,19 @@ def from_file(file_path: Path, data_type: DataType | None = None) -> File:
107123 glyph_bitmap_data = b""
108124 else :
109125 glyph_image = Image .open (glyph_image_path ).convert ("L" )
110- pixel_data = glyph_image .tobytes ()
111-
112- glyph_bitmap_data = Font .__pack_bitmap_data (pixel_data )
113-
126+ glyph_bitmap_data = Font .__pack_bitmap_data (
127+ glyph_image .tobytes ()
128+ )
114129 width = glyph_image .width
115130
116- glyphs_data .extend (len (bitmap_data ).to_bytes (2 , "little" ))
117-
118- if width == 1 :
119- glyphs_data .extend ((0 ).to_bytes (2 ))
120- else :
121- glyphs_data .extend (((width - 1 ) << 4 ).to_bytes (2 , "little" ))
122-
123- if width != 1 and width != 4096 :
124- bitmap_data .extend (glyph_bitmap_data )
131+ Font .__encode_glyph (
132+ width , glyph_bitmap_data , glyphs_data , bitmap_data
133+ )
125134 else :
126- atlas_image = Image .open (
127- file_path / f"{ page_idx } .{ EXPORT_FONT_ATLAS_EXTENSION } "
128- ).convert ("L" )
135+ atlas_image = Image .open (atlas_path ).convert ("L" )
129136
130137 for glyph_element in page .findall ("Glyph" ):
131138 width = int (glyph_element .get ("width" , "0" ))
132-
133139 x_offset = int (glyph_element .get ("x_offset" , "0" ))
134140 y_offset = int (glyph_element .get ("y_offset" , "0" ))
135141
@@ -142,25 +148,18 @@ def from_file(file_path: Path, data_type: DataType | None = None) -> File:
142148 )
143149 )
144150
145- pixel_data = glyph_image .tobytes ()
146- glyph_bitmap_data = Font .__pack_bitmap_data (pixel_data )
151+ glyph_bitmap_data = Font .__pack_bitmap_data (glyph_image .tobytes ())
147152
148- glyphs_data .extend (len (bitmap_data ).to_bytes (2 , "little" ))
149-
150- if width == 1 :
151- glyphs_data .extend ((0 ).to_bytes (2 ))
152- else :
153- glyphs_data .extend (((width - 1 ) << 4 ).to_bytes (2 , "little" ))
154-
155- if width != 1 and width != 4096 :
156- bitmap_data .extend (glyph_bitmap_data )
153+ Font .__encode_glyph (
154+ width , glyph_bitmap_data , glyphs_data , bitmap_data
155+ )
157156
158157 page_bytes += (
159158 len (glyphs_data + bitmap_data ).to_bytes (4 ) + glyphs_data + bitmap_data
160159 )
161160
162161 return Font .from_stream (
163- BytesIO (DataType .MG1_FONT .value . to_bytes (2 ) + b"\0 \0 " + page_bytes )
162+ BytesIO (DataType .MG1_FONT .to_bytes (2 ) + b"\0 \0 " + page_bytes )
164163 )
165164
166165 def add_data (self , ** kwargs ) -> None :
0 commit comments