1616
1717class HeadTableFlags :
1818 @staticmethod
19- def from_value (value ) -> 'HeadTableFlags' :
19+ def parse (value ) -> 'HeadTableFlags' :
2020 baseline_at_y0 = value & 0b_0000_0000_0000_0001 > 0
2121 left_sidebearing_at_x0 = value & 0b_0000_0000_0000_0010 > 0
2222 instructions_may_depend_on_point_size = value & 0b_0000_0000_0000_0100 > 0
@@ -93,10 +93,23 @@ def value(self) -> int:
9393 value |= 0b_0100_0000_0000_0000
9494 return value
9595
96+ def copy (self ) -> 'HeadTableFlags' :
97+ return HeadTableFlags (
98+ self .baseline_at_y0 ,
99+ self .left_sidebearing_at_x0 ,
100+ self .instructions_may_depend_on_point_size ,
101+ self .force_ppem_to_integer ,
102+ self .instructions_may_alter_advance_width ,
103+ self .font_data_is_lossless_after_optimization ,
104+ self .font_converted ,
105+ self .font_optimized_for_cleartype ,
106+ self .last_resort_font ,
107+ )
108+
96109
97110class MacStyle :
98111 @staticmethod
99- def from_value (value ) -> 'MacStyle' :
112+ def parse (value ) -> 'MacStyle' :
100113 bold = value & 0b_0000_0000_0000_0001 > 0
101114 italic = value & 0b_0000_0000_0000_0010 > 0
102115 underline = value & 0b_0000_0000_0000_0100 > 0
@@ -159,6 +172,17 @@ def value(self) -> int:
159172 value |= 0b_0000_0000_0100_0000
160173 return value
161174
175+ def copy (self ) -> 'MacStyle' :
176+ return MacStyle (
177+ self .bold ,
178+ self .italic ,
179+ self .underline ,
180+ self .outline ,
181+ self .shadow ,
182+ self .condensed ,
183+ self .extended ,
184+ )
185+
162186
163187class FontDirectionHint (IntEnum ):
164188 FULLY_MIXED = 0
@@ -189,15 +213,15 @@ def parse(data: bytes, container: SfntTableContainer) -> 'HeadTable':
189213 magic_number = stream .read_uint32 ()
190214 if magic_number != _MAGIC_NUMBER :
191215 raise SfntError ('bad magic number' )
192- flags = stream .read_uint16 ()
216+ flags = HeadTableFlags . parse ( stream .read_uint16 () )
193217 units_per_em = stream .read_uint16 ()
194218 created_seconds_since_1904 = stream .read_long_datetime ()
195219 modified_seconds_since_1904 = stream .read_long_datetime ()
196220 x_min = stream .read_int16 ()
197221 y_min = stream .read_int16 ()
198222 x_max = stream .read_int16 ()
199223 y_max = stream .read_int16 ()
200- mac_style = stream .read_uint16 ()
224+ mac_style = MacStyle . parse ( stream .read_uint16 () )
201225 lowest_rec_ppem = stream .read_uint16 ()
202226 font_direction_hint = FontDirectionHint (stream .read_int16 ())
203227 index_to_loc_format = IndexToLocFormat (stream .read_int16 ())
@@ -227,15 +251,15 @@ def parse(data: bytes, container: SfntTableContainer) -> 'HeadTable':
227251 minor_version : int
228252 font_revision : float
229253 checksum_adjustment : int
230- flags : int
254+ flags : HeadTableFlags
231255 units_per_em : int
232256 created_seconds_since_1904 : int
233257 modified_seconds_since_1904 : int
234258 x_min : int
235259 y_min : int
236260 x_max : int
237261 y_max : int
238- mac_style : int
262+ mac_style : MacStyle
239263 lowest_rec_ppem : int
240264 font_direction_hint : FontDirectionHint
241265 index_to_loc_format : IndexToLocFormat
@@ -247,15 +271,15 @@ def __init__(
247271 minor_version : int = 0 ,
248272 font_revision : float = 0 ,
249273 checksum_adjustment : int = 0 ,
250- flags : int = 0 ,
274+ flags : HeadTableFlags | None = None ,
251275 units_per_em : int = UNITS_PER_EM_MIN_VALUE ,
252276 created_seconds_since_1904 : int = 0 ,
253277 modified_seconds_since_1904 : int = 0 ,
254278 x_min : int = 0 ,
255279 y_min : int = 0 ,
256280 x_max : int = 0 ,
257281 y_max : int = 0 ,
258- mac_style : int = 0 ,
282+ mac_style : MacStyle | None = None ,
259283 lowest_rec_ppem : int = 0 ,
260284 font_direction_hint : FontDirectionHint = FontDirectionHint .LEFT_TO_RIGHT_CONTAINS_NEUTRALS ,
261285 index_to_loc_format : IndexToLocFormat = IndexToLocFormat .SHORT ,
@@ -265,24 +289,20 @@ def __init__(
265289 self .minor_version = minor_version
266290 self .font_revision = font_revision
267291 self .checksum_adjustment = checksum_adjustment
268- self .flags = flags
292+ self .flags = HeadTableFlags () if flags is None else flags
269293 self .units_per_em = units_per_em
270294 self .created_seconds_since_1904 = created_seconds_since_1904
271295 self .modified_seconds_since_1904 = modified_seconds_since_1904
272296 self .x_min = x_min
273297 self .y_min = y_min
274298 self .x_max = x_max
275299 self .y_max = y_max
276- self .mac_style = mac_style
300+ self .mac_style = MacStyle () if mac_style is None else mac_style
277301 self .lowest_rec_ppem = lowest_rec_ppem
278302 self .font_direction_hint = font_direction_hint
279303 self .index_to_loc_format = index_to_loc_format
280304 self .glyph_data_format = glyph_data_format
281305
282- @property
283- def flags_obj (self ) -> HeadTableFlags :
284- return HeadTableFlags .from_value (self .flags )
285-
286306 @property
287307 def created_timestamp (self ) -> int :
288308 return seconds_since_1904_to_timestamp (self .created_seconds_since_1904 )
@@ -315,25 +335,21 @@ def modified_datetime(self) -> datetime:
315335 def modified_datetime (self , value : datetime ):
316336 self .modified_timestamp = int (value .timestamp ())
317337
318- @property
319- def mac_style_obj (self ) -> MacStyle :
320- return MacStyle .from_value (self .mac_style )
321-
322338 def copy (self ) -> 'HeadTable' :
323339 return HeadTable (
324340 self .major_version ,
325341 self .minor_version ,
326342 self .font_revision ,
327343 self .checksum_adjustment ,
328- self .flags ,
344+ self .flags . copy () ,
329345 self .units_per_em ,
330346 self .created_seconds_since_1904 ,
331347 self .modified_seconds_since_1904 ,
332348 self .x_min ,
333349 self .y_min ,
334350 self .x_max ,
335351 self .y_max ,
336- self .mac_style ,
352+ self .mac_style . copy () ,
337353 self .lowest_rec_ppem ,
338354 self .font_direction_hint ,
339355 self .index_to_loc_format ,
@@ -349,15 +365,15 @@ def dump(self, container: SfntTableContainer) -> bytes:
349365 stream .write_fixed (self .font_revision )
350366 stream .write_uint32 (self .checksum_adjustment )
351367 stream .write_uint32 (_MAGIC_NUMBER )
352- stream .write_uint16 (self .flags )
368+ stream .write_uint16 (self .flags . value )
353369 stream .write_uint16 (self .units_per_em )
354370 stream .write_long_datetime (self .created_seconds_since_1904 )
355371 stream .write_long_datetime (self .modified_seconds_since_1904 )
356372 stream .write_int16 (self .x_min )
357373 stream .write_int16 (self .y_min )
358374 stream .write_int16 (self .x_max )
359375 stream .write_int16 (self .y_max )
360- stream .write_uint16 (self .mac_style )
376+ stream .write_uint16 (self .mac_style . value )
361377 stream .write_uint16 (self .lowest_rec_ppem )
362378 stream .write_int16 (self .font_direction_hint )
363379 stream .write_int16 (self .index_to_loc_format )
0 commit comments