Closed supcik closed 8 months ago
I don't understand the problem that this seeks to solve. As far as I can see you can already pass a font as a function arg, as in this fragment:
import font10 as font
def foo(f):
print(f.heigt())
foo(font) # Prints the height
I am wary of any change to the font file format: a lot of testing was done to prove that, when frozen, the data was not copied to RAM. It's easy to inadvertently create a format where the MicroPython runtime does this.
Thank you for your feedback. I never passed "module objects" as parameter to a function. Actually, I did not know that this was possible.
To me, it feels more natural to pass an object to a function (a bit like a font structure is used in Adafruit GFX).
A small benefits of having an object is that you can use f.height
(without the parenthesis) instead of f.height()
... but I agree that this might be a detail.
Anyhow, my proposal adds an alternative file format without impacting the current users and without changing the default. I already used it in a small project, and it seems to work well.
Passing the font as an arg is routine: it is one of the constructor args for Writer
and CWriter
classes. From my point of view this extra file format is added complexity with no benefit. Further, it introduces an unknown regarding performance as frozen bytecode. This would require detailed assessment.
I'm afraid this is a definite no from where I stand.
OK, I understand. I will keep this variant in my own repository and continue the development there.
Just FYI there is a reason why height is not accessed with f.height
. This could easily be achieved by creating a module variable height
. However it causes problems when the bytecode resides in Flash. The runtime has no way of knowing that you won't write
import font10 as f
f.height = 42
It may be nonsense but it's valid Python. So the runtime copies the bytecode to RAM. I think it copies the entire module, negating the purpose of freezing it.
A possible solution is to use a property
to create a read-only variable, but I'm unsure if the runtime is intelligent enough not to copy to RAM. There is also a general convention against properties in MicroPython on performance grounds. Using a function is therefore preferable and has the merit of making the read-only attribute self-evident.
A final observation. The API for Python font files has been fixed for many years and a great deal of code relies on it.
This change offers an alternative ".py" file writer, where the font is stored in an object. The resulting files look like this :
The benefit of this solution is that you can easily pass a font as a parameter to a function (for example a
print_str()
function or aget_bounding_box()
function).The alternative format is selected by the
--alt
option