Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify some local variable names #6971

Merged
merged 8 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/PIL/BdfFontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,27 @@ def bdf_char(f):
bitmap.append(s[:-1])
bitmap = b"".join(bitmap)

[x, y, l, d] = [int(p) for p in props["BBX"].split()]
[dx, dy] = [int(p) for p in props["DWIDTH"].split()]

bbox = (dx, dy), (l, -d - y, x + l, -d), (0, 0, x, y)
# The word BBX
# followed by the width in x (BBw), height in y (BBh),
# and x and y displacement (BBxoff0, BByoff0)
# of the lower left corner from the origin of the character.
width, height, x_disp, y_disp = [int(p) for p in props["BBX"].split()]

# The word DWIDTH
# followed by the width in x and y of the character in device pixels.
dwx, dwy = [int(p) for p in props["DWIDTH"].split()]

bbox = (
(dwx, dwy),
(x_disp, -y_disp - height, width + x_disp, -y_disp),
(0, 0, width, height),
)

try:
im = Image.frombytes("1", (x, y), bitmap, "hex", "1")
im = Image.frombytes("1", (width, height), bitmap, "hex", "1")
except ValueError:
# deal with zero-width characters
im = Image.new("1", (x, y))
im = Image.new("1", (width, height))

return id, int(props["ENCODING"]), bbox, im

Expand Down
14 changes: 7 additions & 7 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,17 +765,17 @@ def tobytes(self, encoder_name="raw", *args):

bufsize = max(65536, self.size[0] * 4) # see RawEncode.c

data = []
output = []
while True:
l, s, d = e.encode(bufsize)
data.append(d)
if s:
bytes_consumed, errcode, data = e.encode(bufsize)
output.append(data)
if errcode:
break
if s < 0:
msg = f"encoder error {s} in tobytes"
if errcode < 0:
msg = f"encoder error {errcode} in tobytes"
raise RuntimeError(msg)

return b"".join(data)
return b"".join(output)

def tobitmap(self, name="image"):
"""
Expand Down
14 changes: 7 additions & 7 deletions src/PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,20 +530,20 @@ def _encode_tile(im, fp, tile, bufsize, fh, exc=None):
encoder.setimage(im.im, b)
if encoder.pushes_fd:
encoder.setfd(fp)
l, s = encoder.encode_to_pyfd()
errcode = encoder.encode_to_pyfd()[1]
else:
if exc:
# compress to Python file-compatible object
while True:
l, s, d = encoder.encode(bufsize)
fp.write(d)
if s:
errcode, data = encoder.encode(bufsize)[1:]
fp.write(data)
if errcode:
break
else:
# slight speedup: compress to real file object
s = encoder.encode_to_file(fh, bufsize)
if s < 0:
msg = f"encoder error {s} when writing image file"
errcode = encoder.encode_to_file(fh, bufsize)
if errcode < 0:
msg = f"encoder error {errcode} when writing image file"
raise OSError(msg) from exc
finally:
encoder.cleanup()
Expand Down
27 changes: 21 additions & 6 deletions src/PIL/PcfFontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,22 @@ def __init__(self, fp, charset_encoding="iso8859-1"):

for ch, ix in enumerate(encoding):
if ix is not None:
x, y, l, r, w, a, d, f = metrics[ix]
glyph = (w, 0), (l, d - y, x + l, d), (0, 0, x, y), bitmaps[ix]
self.glyph[ch] = glyph
(
xsize,
ysize,
left,
right,
width,
ascent,
descent,
attributes,
) = metrics[ix]
self.glyph[ch] = (
(width, 0),
(left, descent - ysize, xsize + left, descent),
(0, 0, xsize, ysize),
bitmaps[ix],
)

def _getformat(self, tag):
format, size, offset = self.toc[tag]
Expand Down Expand Up @@ -206,9 +219,11 @@ def _load_bitmaps(self, metrics):
mode = "1"

for i in range(nbitmaps):
x, y, l, r, w, a, d, f = metrics[i]
b, e = offsets[i], offsets[i + 1]
bitmaps.append(Image.frombytes("1", (x, y), data[b:e], "raw", mode, pad(x)))
xsize, ysize = metrics[i][:2]
b, e = offsets[i : i + 2]
bitmaps.append(
Image.frombytes("1", (xsize, ysize), data[b:e], "raw", mode, pad(xsize))
)

return bitmaps

Expand Down
10 changes: 5 additions & 5 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1845,13 +1845,13 @@ def _save(im, fp, filename):
e.setimage(im.im, (0, 0) + im.size)
while True:
# undone, change to self.decodermaxblock:
l, s, d = e.encode(16 * 1024)
errcode, data = e.encode(16 * 1024)[1:]
if not _fp:
fp.write(d)
if s:
fp.write(data)
if errcode:
break
if s < 0:
msg = f"encoder error {s} when writing image file"
if errcode < 0:
msg = f"encoder error {errcode} when writing image file"
raise OSError(msg)

else:
Expand Down