estranged42 / Adafruit-GFX-Library

Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from
https://learn.adafruit.com/adafruit-gfx-graphics-library
Other
0 stars 0 forks source link

Add runtime option to select font face ID #1

Open estranged42 opened 6 years ago

estranged42 commented 6 years ago

I came across a need to select a different font face besides 0 from a particular font file. This comes down to changing line 119:

    if((err = FT_New_Face(library, argv[1], 0, &face))) {

The third argument is the face_index which is described in the FreeType documentation as selecting a specific face variant from the file. I was able to just change this from a 0 to a 3 in order to extract the face for my particular case, and figured it would be useful to add this as an option when calling, rather than having to recompile the tool to change this value.

Unfortunately I can't figure out an elegant way to alter the input parameters, since they are passed in a very specific order:

Usage: ./fontconvert fontfile size [first] [last]

I suppose I could add an additional parameter on the very end, but that would force you to add a first and last parameter when you wanted to use the alternate face, which doesn't seem great? However I'm not sure how hard it would be to add switch-style flags to this (i.e. --face=3) or something.

Ideas or opinions? Thanks for reading!

PaintYourDragon commented 6 years ago

fontconvert was written hasty-like and you're right, there's no elegant way to alter the input parameters.

Equally-hasty approach for now might be to just compile in the face number, since I think this is the first time it's come up as an issue (can you elaborate on the situation and estimate the probability that any other users will encounter the same in the future?).

Slightly-less-hasty would be a switch-style flag for that one specific option, or worse, an environment variable. It's kludgey and inconsistent, but again, situation's never arisen before and might never again, in which case it might not even need to be documented and is just sort of a latent feature for a rare situation.

Non-hasty would be to switchify all the input arguments, which would require both code and documentation changes. Maybe using getopt or whatever's likely to be available across different platforms.

All-the-way would be to reimplement fontconvert in Python so it no longer needs MinGW on Windows. This'll probably happen at some point.

estranged42 commented 6 years ago

Thanks so much for the response!

I'm using a font in an LCD project and for the most part the "medium" face works fine, but in one case I wanted to extract the "Condensed" version. Since the first face in this list was Medium, I took a chance that the fourth face, Condensed, would be index 3 and I was right :)

screen shot 2018-03-27 at 8 02 56 pm

It turns out that this font is actually a *.ttc file, so its a TrueType container, and not a single font. Apparently this is becoming more popular. There do seem to be ways to extract individual ttf files from the container, so maybe I will go that route in the future, although I think for now I will just compile the change as needed.. since its a pretty rare thing, and as you say hasn't come up before.

Thanks for taking time to reply!