Skip to content

Commit 20aa387

Browse files
committed
j
1 parent cd475c3 commit 20aa387

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

src/sfnttools/tables/dsig/__init__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class DsigPermissionFlags:
1212
@staticmethod
13-
def from_value(value) -> 'DsigPermissionFlags':
13+
def parse(value) -> 'DsigPermissionFlags':
1414
cannot_be_resigned = value & 0b_0000_0000_0000_0001 > 0
1515
return DsigPermissionFlags(cannot_be_resigned)
1616

@@ -29,6 +29,9 @@ def value(self) -> int:
2929
value |= 0b_0000_0000_0000_0001
3030
return value
3131

32+
def copy(self) -> 'DsigPermissionFlags':
33+
return DsigPermissionFlags(self.cannot_be_resigned)
34+
3235

3336
@runtime_checkable
3437
class SignatureBlock(Protocol):
@@ -99,7 +102,7 @@ def parse(data: bytes, container: SfntTableContainer | None = None) -> 'DsigTabl
99102

100103
version = stream.read_uint32()
101104
num_signatures = stream.read_uint16()
102-
flags = stream.read_uint16()
105+
flags = DsigPermissionFlags.parse(stream.read_uint16())
103106
signature_blocks = []
104107
signature_records = [SignatureRecord.parse(stream) for _ in range(num_signatures)]
105108
for signature_record in signature_records:
@@ -116,31 +119,27 @@ def parse(data: bytes, container: SfntTableContainer | None = None) -> 'DsigTabl
116119
)
117120

118121
version: int
119-
flags: int
122+
flags: DsigPermissionFlags
120123
signature_blocks: list[SignatureBlock]
121124

122125
def __init__(
123126
self,
124127
version: int = 1,
125-
flags: int = 0,
128+
flags: DsigPermissionFlags | None = None,
126129
signature_blocks: list[SignatureBlock] | None = None,
127130
):
128131
self.version = version
129-
self.flags = flags
132+
self.flags = DsigPermissionFlags() if flags is None else flags
130133
self.signature_blocks = [] if signature_blocks is None else signature_blocks
131134

132-
@property
133-
def flags_obj(self) -> DsigPermissionFlags:
134-
return DsigPermissionFlags.from_value(self.flags)
135-
136135
@property
137136
def num_signatures(self) -> int:
138137
return len(self.signature_blocks)
139138

140139
def copy(self) -> 'DsigTable':
141140
return DsigTable(
142141
self.version,
143-
self.flags,
142+
self.flags.copy(),
144143
[signature_block.copy() for signature_block in self.signature_blocks],
145144
)
146145

@@ -162,7 +161,7 @@ def dump(self, container: SfntTableContainer) -> bytes:
162161
stream.seek(0)
163162
stream.write_uint32(self.version)
164163
stream.write_uint16(self.num_signatures)
165-
stream.write_uint16(self.flags)
164+
stream.write_uint16(self.flags.value)
166165
for signature_record in signature_records:
167166
signature_record.dump(stream)
168167

src/sfnttools/tables/head/__init__.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class 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

97110
class 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

163187
class 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

Comments
 (0)