[Image-SIG] PIL method for loading TrueType/OpenType fonts fromfont names?

Pascal Charbonneau r.charbonneau4 at sympatico.ca
Sun Aug 28 22:54:08 CEST 2005



I am learning python as I go and ended up creating an XML 
typeface/mapping that I load into a dictionnary. Marshalling the dict 
would probably make it faster to load, thanks for the tip. At least I 
learned to use cElementTree in the meantime, so not a complete waste of 
time on my part hehe.

Thanks for the reply :)

Pascal

Fredrik Lundh wrote:

>Pascal Charbonneau wrote:
>
>  
>
>>One thing that is definately missing from the PIL ImageFont module is a
>>way to load fonts by font names. I have not found any easy way to
>>associate the file name (needed to load a TrueType font in PIL) with the
>>actual font typeface name.
>>    
>>
>
>that's because there's no portable way to do that, without actually looking
>inside all available truetype files.
>
>  
>
>>i.e "Times New Roman Bold (TrueType)" = "TIMESDB.TTF"
>>
>>Every standard "font" dialog lets the user choose a Typeface, Point
>>Size, etc and associating them to be used with PIL seems to be an issue.
>>Unless I am being dumb and havent found the obvious easy solution :)
>>
>>Anyone has encountered this issue and can suggest a solution?
>>    
>>
>
>you have to maintain your own "name to file" mapping, either by querying
>a system font manager of some kind, or by scanning the available files, and
>extracting the font name and style from the font object:
>
>    for file in glob.glob("*.ttf"):
>        font = ImageFont.truetype(file, 0)
>        name, style = font.getname()
>        print name, style, "=>", file
>
>(to avoid spending lots of time on this every time you start the program, 
>you
>probably want to maintain a persistent font cache of some kind.  storing the
>data in a marshalled dict should be good enough)
>
></F> 
>
>
>
>_______________________________________________
>Image-SIG maillist  -  Image-SIG at python.org
>http://mail.python.org/mailman/listinfo/image-sig
>
>  
>



More information about the Image-SIG mailing list