Skip to content

Commit

Permalink
fix broken packs/unpacks in ttf/png (pyglet#819)
Browse files Browse the repository at this point in the history
(cherry picked from commit dbd866f)
  • Loading branch information
kilimnik authored and caffeinepills committed Aug 10, 2023
1 parent 55e8a6e commit d76e637
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 45 deletions.
8 changes: 4 additions & 4 deletions pyglet/extlibs/png.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ def unpack_rows(rows):
to being a sequence of bytes.
"""
for row in rows:
fmt = '!%dH' % len(row)
fmt = f'!{len(row)}H'
yield bytearray(struct.pack(fmt, *row))


Expand Down Expand Up @@ -1733,7 +1733,7 @@ def _process_bKGD(self, data):
"PLTE chunk is required before bKGD chunk.")
self.background = struct.unpack('B', data)
else:
self.background = struct.unpack("!%dH" % self.color_planes,
self.background = struct.unpack(f"!{self.color_planes}H",
data)
except struct.error:
raise FormatError("bKGD chunk has incorrect length.")
Expand All @@ -1756,7 +1756,7 @@ def _process_tRNS(self, data):
self.color_type)
try:
self.transparent = \
struct.unpack("!%dH" % self.color_planes, data)
struct.unpack(f"!{self.color_planes}H", data)
except struct.error:
raise FormatError("tRNS chunk has incorrect length.")

Expand Down Expand Up @@ -1989,7 +1989,7 @@ def itertrns(pixels):
pixels = itertrns(pixels)
targetbitdepth = None
if self.sbit:
sbit = struct.unpack('%dB' % len(self.sbit), self.sbit)
sbit = struct.unpack(f'{len(self.sbit)}B', self.sbit)
targetbitdepth = max(sbit)
if targetbitdepth > info['bitdepth']:
raise Error('sBIT chunk %r exceeds bitdepth %d' %
Expand Down
47 changes: 6 additions & 41 deletions pyglet/font/ttf.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
# ----------------------------------------------------------------------------
# pyglet
# Copyright (c) 2006-2008 Alex Holkner
# Copyright (c) 2008-2022 pyglet contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of pyglet nor the names of its
# contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

"""
Implementation of the Truetype file format.
Expand Down Expand Up @@ -402,16 +367,16 @@ def _get_character_map_format4(self, offset):
# a fuckwit.
header = _read_cmap_format4Header(self._data, offset)
seg_count = header.seg_count_x2 // 2
array_size = struct.calcsize('>%dH' % seg_count)
end_count = self._read_array('>%dH' % seg_count,
array_size = struct.calcsize(f'>{seg_count}H')
end_count = self._read_array(f'>{seg_count}H',
offset + header.size)
start_count = self._read_array('>%dH' % seg_count,
start_count = self._read_array(f'>{seg_count}H',
offset + header.size + array_size + 2)
id_delta = self._read_array('>%dh' % seg_count,
id_delta = self._read_array(f'>{seg_count}H',
offset + header.size + array_size + 2 + array_size)
id_range_offset_address = \
offset + header.size + array_size + 2 + array_size + array_size
id_range_offset = self._read_array('>%dH' % seg_count,
id_range_offset = self._read_array(f'>{seg_count}H',
id_range_offset_address)
character_map = {}
for i in range(0, seg_count):
Expand Down Expand Up @@ -475,7 +440,7 @@ def __init__(self, data, offset):
setattr(self, pname, pvalue)

def __repr__(self):
return '{'+', '.join(['%s = %s' % (pname, pvalue) for pname, pvalue in self.pairs])+'}'
return '{'+', '.join([f'{pname} = {pvalue}' for pname, pvalue in self.pairs])+'}'

@staticmethod
def array(data, offset, count):
Expand Down

0 comments on commit d76e637

Please sign in to comment.