473,387 Members | 1,664 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,387 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 3749
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.