473,785 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.truet ype("/windows/fonts/verdanai.ttf", 1)
f.font.famil y
'Verdana'
>>>f.font.sty le
'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 3799
On Sep 18, 7:48*pm, Steve Holden <st...@holdenwe b.comwrote:
Fredrik Lundh wrote:
Steve Holden wrote:
Does anyone have a Python recipe for this?
>>from PIL import ImageFont
f = ImageFont.truet ype("/windows/fonts/verdanai.ttf", 1)
f.font.fami ly
'Verdana'
>>f.font.styl e
'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\\fon ts\\arial.ttf', 'rb' )
from struct import *

#header
shead= Struct( '>IHHHH' )
fhead= f.read( shead.size )
dhead= shead.unpack_fr om( 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_f rom(
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.unpac k_from( fnametable, 0 )

sname= Struct( '>HHHHHH' )
for i in range( dnamehead[1] ): #name table records
dname= sname.unpack_fr om(
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...@holdenwe b.comwrote:
>Fredrik Lundh wrote:
>>Steve Holden wrote:
Does anyone have a Python recipe for this?
>from PIL import ImageFont
>f = ImageFont.truet ype("/windows/fonts/verdanai.ttf", 1)
>f.font.fam ily
'Verdana'
>f.font.sty le
'Italic'
Thanks so much, Fredrik. The reason I asked is because I found the
specificatio n 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\\fon ts\\arial.ttf', 'rb' )
from struct import *

#header
shead= Struct( '>IHHHH' )
fhead= f.read( shead.size )
dhead= shead.unpack_fr om( 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_f rom(
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.unpac k_from( fnametable, 0 )

sname= Struct( '>HHHHHH' )
for i in range( dnamehead[1] ): #name table records
dname= sname.unpack_fr om(
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
5340
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 available truetype font that works with UTF8? -aj
1
2498
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 find anything ... bye
0
1435
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 Font.FromLogFont won't allow non-truetype fonts. Please help. J
1
3448
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 for use any kind of fonts in PyOpengl. The nehe tutorial has some bugs in translation to Python. I need to scale rotate the text.
5
1156
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 IP=127.0.0.1 Port=23 I want to be able to save the color settings in the same way, but the I
5
3146
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 = "C:/Windows/Fonts/comic.TTF". Here is the traceback that results:
24
2841
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 with the printer that i am printing to(Zebra TLP2844). When i open Word and look at the fonts available for the default printer, it does not show the fonts i want. If i cahnge the printer to the printer that
0
1115
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. http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6.html Hope that helps.
0
223
by: Fredrik Lundh | last post by:
Steve Holden wrote: 'Verdana' 'Italic' </F>
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.