How to Use FontTools Module in Python

Python
Python

Python comes with the great modules which you can use them to improve your application functionalities. Recently I felt in love with Python and trying to implement some of my daily usages with it. I was always interested in Unicode and Fonts so that I had a glance at Python abilities in these fields and I have to say that Python has a lot to say when it comes to these areas. It is really fantastic and powerful and you have access to whatever you want just a a peace of a cake. In this Article I show you how to use python abilities to save a font as an XML document. It may be wierd for you but sometimes it is necessary to check font tables and the rest of its information.

To start working with fonts it’s worth to add FontTools library to Python; it will extend your abilities to work with fonts. You can download it from here.

Each Python module has its own way for installation. Some of them only needs a simple copy to the appropriate folders and some of them should be installed. FontTools uses the second approach so that you have to follow the following instruction to install it in your Python.

  • Open Windows Command Prompt
  • Go to the folder you download FontTools
  • Write Setup.py build and press enter
  • Write Setup.pyinstall and press enter
Installing FontTools with Command Prompt in Python
Installing FontTools with Command Prompt in Python
Important Note: I first Installed My FontTools and Then take the above photo from my comand prompt so that the command reply may be different for the first time and the second. You just use the mentioned commands instead of considering provided replies by Python

Some lines will appear in your command prompt and then installation will complete within a minute. Now, to use this module in your application you have to load ( or  import or use ) this module in your application, so write the following command in your Python application just like other commands

[sourcecode language=”python”]
from fontTools.ttLib import ttfont
[/sourcecode]

Note that commands are case sensitive so write it in the exact mentioned way.

Till now you are able to use FontTools abilities in your application so that to test it we create a font object and then get a simple export from it.

To create a font object in Python write the below command in your application; just note that you have to use for slash instead (/) of back slash (\) for addressing

[sourcecode language=”python”]
font = TTFont("d:/fontname.ttf")
[/sourcecode]

Till now you created a new object of your font in the object named font. It’s time to use it in our application so that I save the font as an XML document just like

[sourcecode language=”python”]
font.saveXML("d:/YourDesireName.xml")
[/sourcecode]

Save a Font As XML with Python FontTools Module
Save a Font As XML with Python FontTools Module

You can find the rest of the FontTools commands in the documentation. Share your experiences with me if you know another good library to work with fonts.

How to Build your Solution with Visual Studio Command Prompt

Working with command prompt always been a nice job, it helps you to schedule your routines and gives you more understanding of what’s going on the background. You will give more and on-time information; you can check the result as they happen. You can create a batch fileto build your solutions one after another and many other advantages.Visual Studio command prompt gives you all of the simple windows command prompt abilities plus some tools to control build and publish your source codesand many other abilities which you can find here. You can run Visual Studio command prompt from

[source code=””xml””]
Start Menu \ Visual Studio [2005|2008|2010] \ Visual Studio Tools \ Visual Studio 2008 Command Prompt
[/source]

You can use other prompts like – Visual Studio 2008 x64 Cross Tools Command Prompt – for general x64 system. None of them are different; the only difference is that they are provided for different platforms. As it is hard to run Visual Studio Command Prompt from Windows Task manager, you have to call it from a simple Command Prompt; it means that you have to run the following command in your Command Prompt. After that Visual Studio tools will include into your simple command prompt.

[source code=””xml””]
call "C:\Program Files (x86) \ Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86_amd64
[/source]

x86_amd64 will call x64 Cross Tools. To build your solutions you can use following commands

[source code=””xml””]
MSBuild YourSolution.sln /p:Configuration=Release
[/source]

Note that you can use a path like MSBuild D:\src\YourSolution.sln for your projects. As you all know .NET provide two different type of output for each project, Debug & Release. To specify the type of output you can use following code

[source code=””xml””]
MSBuild D:\src\YourSolution.sln /p:Configuration=Release
[/source]