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

load_glyphs() not working in v1.5.2 (worked in v1.5.1) #51

Closed
anecdata opened this issue Nov 18, 2021 · 8 comments
Closed

load_glyphs() not working in v1.5.2 (worked in v1.5.1) #51

anecdata opened this issue Nov 18, 2021 · 8 comments
Labels
bug Something isn't working

Comments

@anecdata
Copy link
Member

Tested on Espressif, SAMD51, SAMD21, RP2040. PyPortal demo:

Adafruit CircuitPython 7.1.0-beta.0 on 2021-11-12; Adafruit PyPortal with samd51j20
>>> 
>>> from adafruit_bitmap_font import bitmap_font
>>> 
>>> bitmap_font.__version__
'1.5.1'
>>> 
>>> # from https://learn.adafruit.com/pyportal-alarm-clock/code-tour
>>> time_font = bitmap_font.load_font('/fonts/Arial-12.bdf')
>>> time_font.load_glyphs(b'0123456789:')
>>> 

Replace adafruit_bitmap_font v1.5.1 with adafruit_bitmap_font v1.5.2:

Adafruit CircuitPython 7.1.0-beta.0 on 2021-11-12; Adafruit PyPortal with samd51j20
>>> 
>>> from adafruit_bitmap_font import bitmap_font
>>> 
>>> bitmap_font.__version__
'1.5.2'
>>> 
>>> # from https://learn.adafruit.com/pyportal-alarm-clock/code-tour
>>> time_font = bitmap_font.load_font('/fonts/Arial-12.bdf')
>>> time_font.load_glyphs(b'0123456789:')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_bitmap_font/bdf.py", line 142, in load_glyphs
OSError: [Errno 22] Invalid argument
>>> 
@anecdata anecdata added the bug Something isn't working label Nov 18, 2021
@FoamyGuy
Copy link
Contributor

It looks to me like the code at line 142 is assuming that self.file is an opened file instance.

tracing back up a bit in the library the file is coming ultimately from here:

with open(filename, "rb") as font_file:
first_four = font_file.read(4)
if filename.endswith("bdf") and first_four == b"STAR":
from . import bdf
return bdf.BDF(font_file, bitmap)

I'm not super familiar with the lifecycle of things that use the with: context processor. Is it possible that the return and initializer happening on the same line is resulting in font_file getting closed automatically by the end of the with block before it has the chance to be used for reading inside of the load_glyphs() function?

@FoamyGuy
Copy link
Contributor

I do think the issue is that the file is being closed before load_glyphs() can do anything with it. I verified the same error with this minimal reproducer:

>>> f = open("boot_out.txt", "r")
>>> print(f.readline())
Adafruit CircuitPython 7.0.0-alpha.5 on 2021-07-21; Adafruit FunHouse with ESP32S2

>>> f.close()
>>> f.seek(0)
84
>>> print(f.readline())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

@anecdata
Copy link
Member Author

I think you're right, the file diff seemed to point to that too.

@tannewt
Copy link
Member

tannewt commented Nov 18, 2021

We should probably switch this to a filename as well.

@jposada202020
Copy link
Contributor

Is there any work to be done here? or #52 solved the issue. Happy to help if needed :)

@anecdata
Copy link
Member Author

As far as I know, this was fixed in 1.5.3, and versions starting then have been loading fonts and glyphs fine.

@jposada202020
Copy link
Contributor

I think I will test it with https://github.com/adafruit/Adafruit_CircuitPython_Display_Text/blob/main/examples/display_text_advance_example.py , and if everything goes well and you agree Ill close the issue :)

@jposada202020
Copy link
Contributor

You were right, no issues detected. Tested on

Adafruit CircuitPython 8.0.0 on 2023-02-06; Adafruit PyPortal Titano with samd51j20

Using https://github.com/adafruit/Adafruit_CircuitPython_Display_Text/blob/main/examples/display_text_advance_example.py

LGTM thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants