473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Re: Extracting hte font name from a TrueType font file

Fredrik Lundh wrote:
Steve Holden wrote:
>Does anyone have a Python recipe for this?
>>>from PIL import ImageFont
f = ImageFont.truetype("/windows/fonts/verdanai.ttf", 1)
f.font.family
'Verdana'
>>>f.font.style
'Italic'
Thanks so much, Fredrik. The reason I asked is because I found the
specification completely opaque ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 19 '08 #1
2 3738
On Sep 18, 7:48*pm, Steve Holden <st...@holdenweb.comwrote:
Fredrik Lundh wrote:
Steve Holden wrote:
Does anyone have a Python recipe for this?
>>from PIL import ImageFont
f = ImageFont.truetype("/windows/fonts/verdanai.ttf", 1)
f.font.family
'Verdana'
>>f.font.style
'Italic'

Thanks so much, Fredrik. The reason I asked is because I found the
specification completely opaque ...

regards
*Steve
--
Steve Holden * * * *+1 571 484 6266 * +1 800 494 3119
Holden Web LLC * * * * * * *http://www.holdenweb.com/
Here's the code to parse the spec.

#customize path
f= open( '\\windows\\fonts\\arial.ttf', 'rb' )
from struct import *

#header
shead= Struct( '>IHHHH' )
fhead= f.read( shead.size )
dhead= shead.unpack_from( fhead, 0 )

#font directory
stable= Struct( '>4sIII' )
ftable= f.read( stable.size* dhead[ 1 ] )
for i in range( dhead[1] ): #directory records
dtable= stable.unpack_from(
ftable, i* stable.size )
if dtable[0]== 'name': break
assert dtable[0]== 'name'

#name table
f.seek( dtable[2] ) #at offset
fnametable= f.read( dtable[3] ) #length
snamehead= Struct( '>HHH' ) #name table head
dnamehead= snamehead.unpack_from( fnametable, 0 )

sname= Struct( '>HHHHHH' )
for i in range( dnamehead[1] ): #name table records
dname= sname.unpack_from(
fnametable, snamehead.size+ i* sname.size )
if dname[3]== 4: #key == 4: "full name of font"
s= unpack_from(
'%is'% dname[4], fnametable,
dnamehead[2]+ dname[5] )[0]
print dname, s

This outputs:

(0, 3, 0, 4, 10, 318) A r i a l
(1, 0, 0, 4, 5, 4081) Arial
(3, 1, 1033, 4, 10, 318) A r i a l

First 3 fields:

0, 3, 0= Unicode, Unicode 2.0, English
1, 0, 0= Macintosh, Default, English
3, 1, 1033= Windows, Version 1.1, Language "1033"
Sep 19 '08 #2
Aaron "Castironpi" Brady wrote:
On Sep 18, 7:48 pm, Steve Holden <st...@holdenweb.comwrote:
>Fredrik Lundh wrote:
>>Steve Holden wrote:
Does anyone have a Python recipe for this?
>from PIL import ImageFont
>f = ImageFont.truetype("/windows/fonts/verdanai.ttf", 1)
>f.font.family
'Verdana'
>f.font.style
'Italic'
Thanks so much, Fredrik. The reason I asked is because I found the
specification completely opaque ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Here's the code to parse the spec.

#customize path
f= open( '\\windows\\fonts\\arial.ttf', 'rb' )
from struct import *

#header
shead= Struct( '>IHHHH' )
fhead= f.read( shead.size )
dhead= shead.unpack_from( fhead, 0 )

#font directory
stable= Struct( '>4sIII' )
ftable= f.read( stable.size* dhead[ 1 ] )
for i in range( dhead[1] ): #directory records
dtable= stable.unpack_from(
ftable, i* stable.size )
if dtable[0]== 'name': break
assert dtable[0]== 'name'

#name table
f.seek( dtable[2] ) #at offset
fnametable= f.read( dtable[3] ) #length
snamehead= Struct( '>HHH' ) #name table head
dnamehead= snamehead.unpack_from( fnametable, 0 )

sname= Struct( '>HHHHHH' )
for i in range( dnamehead[1] ): #name table records
dname= sname.unpack_from(
fnametable, snamehead.size+ i* sname.size )
if dname[3]== 4: #key == 4: "full name of font"
s= unpack_from(
'%is'% dname[4], fnametable,
dnamehead[2]+ dname[5] )[0]
print dname, s

This outputs:

(0, 3, 0, 4, 10, 318) A r i a l
(1, 0, 0, 4, 5, 4081) Arial
(3, 1, 1033, 4, 10, 318) A r i a l

First 3 fields:

0, 3, 0= Unicode, Unicode 2.0, English
1, 0, 0= Macintosh, Default, English
3, 1, 1033= Windows, Version 1.1, Language "1033"
Well you clearly understood it better than *I* did!

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 19 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas Jung | last post by:
Reportlab has some problems with creating PDFs from UTF8 encoded text. For this reason they are using a truetype font rina.ttf which looks *very ugly*. Does anyone know of a suitable free...
1
by: Gabriele *Darkbard* Farina | last post by:
Hi, there is a Python library that makes me able to extract outline informations from font files? I'd like to manage TrueType and FreeType fonts ... I searched for som wrappers, but I didn't...
0
by: Joshua T. Moore | last post by:
I must print with a non-TrueType font for the printer to do specific features. I P/Invoked CreateFont to get the (IntPtr) handle of the font, but System.Drawing.Graphics.DrawString's...
1
by: Marco | last post by:
I must use text in Opengl, Python This text is for architecture design then I need to use AutoCad fonts (extension .shx). I have two troubles: I don't find a guide, a good program, something...
5
by: Terry Olsen | last post by:
I want to be able to save the user color settings in my app and restore them when the app is re-opened. I currently have a config file with different settings such as: UID=jsmith...
5
by: Christian Stapfer | last post by:
After switching from Python 2.3 to 2.4 (Enought), PIL throws an exception that did not occur formerly (under Python 2.3) when executing ImageFont.truetype(font, size) where font =...
24
by: Tony Girgenti | last post by:
Hello. Developing a Windows Form program in VS.NET VB, .NET Framework 1.1.4322 on a windows XP Pro, SP2. Before printing a document, i want to set the font to a font that is only available...
0
by: Andreas Tawn | last post by:
-----Original Message----- g] On Behalf Of Steve Holden Can't help with a recipe, but here's the formal spec if want to figure it out yourself. ...
0
by: Fredrik Lundh | last post by:
Steve Holden wrote: 'Verdana' 'Italic' </F>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.