command line font file transformation tool needed

fanatic font addict's picture

I need something that for example you can pass in a matrix to it as a parameter and it will automatically modify all the glyphs in the font file. I need to both italicize and embolden a font as well.

Actually I did manage to accomplish this with ttx and the FreeType library. FreeType has methods like FT_Outline_Transform and FT_Outline_Embolden, and with ttx its easy to get to all the glyph data and then recompile it all back into a font file. But the process is slow: 5-10 seconds for a single font. The reason its slow is at least because its dealing with that huge ttx text file.

But since matrix transformations and embolding is so trivial and ubiquitous I can't believe there's not a command line tool already somewhere to do this but haven't found it anywhere. (The complicating factor in automating this is of course the byzantine TT/OT format.)

fanatic font addict's picture

Actually I did just now encounter the docs for Adobe's rotateFont so it looks like the italic at least is doable. But emboldening cannot be done properly just with matrices I don't think, so if anyone knows of a command line tool that encompasses emboldening as well...

fanatic font addict's picture

Still looking into this - need to figure out if I can use something like rotateFont to do Bold. In their docs it has the following:

"For example, the argument:
-matrix 2.0 0 0 0.75 100 200
will:
- scale up horizontal coordinates by a factor of 2,
- condense the vertical coordinates by a factor three-quarters,
- move the entire glyph left by 100
- move the entire glyph up by 200."

It doesn't call the above Bold though. Is there some sort of matrix transform that will in fact approximate Bold. The routine from FreeType i mentioned in the OP, "FT_Outline_Embold", is pretty short, but doesn't seem to be using matrices as such (still haven't completely figured it out though.)
Anyway, was hoping there was some knowledge to be found here, as algorithmic bolding of text (for example in software when a font doesn't have a bold variant defined) seems fairly commonplace.

blank's picture

Anyway, was hoping there was some knowledge to be found here, as algorithmic bolding of text…

Just because it’s commonplace doesn’t mean that we like it. Most type designers are doing stuff like this with specialized GUI tools that allow type to be manipulated without screwing up the proportions and slanting the bezier handles. You might get better feedback wherever the Fontforge users hang out.

fanatic font addict's picture

to JP: I wouldn't fault anyone for not knowing this. I realize that programmatic font- related issues may be its own special little niche. But things like the ttf file format and so on are quite complex and its all too specialized to find significant information in general programming-related forums. Even stackoverflow.com where you can usually find answers to anything has very little. So if anyone knows of a forum that covers all this that would be great.

Michel Boyer's picture

FontForge can be used for batch processing with scripts. You could try the changeWeight Python Script method for glyphs; its behavior and optional parameters are described here. Here is a chngwt script for dummies:

---
#!/usr/bin/python2.5


import fontforge,sys
fnt=fontforge.open(sys.argv[1])
weighttoadd=int(sys.argv[2])
outname=sys.argv[3]
for g in fnt.glyphs():
   g.changeWeight(weighttoadd)
fnt.generate(outname)
---

The call
chngwt infile.otf 50 outfile.otf

gives a file outfile.otf that is bolder than infile.otf by 50 ÷ em units (you can also use with .pfb or .ttf files)

For some characters, the output does not look too bad. Processing is however slow and the features that FontForge does not know or does not handle completely will be lost or partially lost in outfile.otf

fanatic font addict's picture

Michael Boyer:

Thanks - it would have to be something embeddable in my own application I've written, but I'll check the page for FontForge again and see if the source code is accessible or otherwise if its invokable somehow from the comand line or something.

Of course algorithmic bolding needs to be done somehow if a font hasn't defined it. Lot's of them don't in my experience.

I'm currently looking at just using FreeType and accessing the OTF/TTF file directly as opposed to going through ttx (which is the speed bottleneck here, though its convenient.) But now I'm having a problem deciphering something in the OTF specication but I will post that in a seperate thread.

Michel Boyer's picture

if its invokable somehow from the comand line or something.

The above script is to be invoked from the command line on unix (for instance on a mac). I have never tried with cygwin on a PC.

fanatic font addict's picture

that's right - I think you have to install cygwin to run fontforge on a PC. If that's the case it won't be an option for me.

Theunis de Jong's picture

.. algorithmic bolding of text (for example in software when a font doesn't have a bold variant defined) seems fairly commonplace.

The Word solution: place the text twice (or thrice, or even more often) with a small offset. Yuck.
The usually recommended InDesign solution: give the text a small outline (although I should mention that this also usually is followed by "Butt ugly, so don't do it and purchase a real bold font").

Doing the Word thing in memory, on a glyph, would mean you'd have to intersect the character with itself at an offset. But it will, generally speaking, result in extremely ugly characters.
Doing the InD thing in memory should also be possible but also needs a full CSG operation -- first, you have to create an outline of the glyph, then expand the stroke into a fill form, then intersect it with the original.

If you can get hold of the original outline coordinates and vectors, I think a rather good way would be to move left-pointing lines a bit to the left and right-pointing lines a bit to the right. (And probably something similar if you want to change the height as well.) You cannot use a single transformation on each separate coordinate, as that would amount to simply stretching the character -- the matrix you posted does that.

fanatic font addict's picture

Thanks, theunis.

That method how Word does it is actually useful to know. Also the other methods you proposed for example regarding left and right pointing lines. Looking at the code for Freetype's FT_Outline_Embolden, where they're looking at angles and doing trig, maybe they're doing something similiar. (But really I should just bite the bullet and figure out what they're doing - I could post the code here - its not long.)

But just to reiterate, all I want at this point actually is some command line app that will go through a font and embold everything. I don't care how it does it really, unless its comparable to what is already typically done to algorithmically embolden fonts.

It has to be command line so I could invoke it from my own app. There are command line tools like RotateFont which will do that for italic (basically just give it a slant.)

The problem with doing this myself is not the algorithm (as for example I could use ft_outline_embolden). Rather, the problem is parsing the TTF file to get at the glyphs which is ridiculously complex. I could use ttx which reformats everything in a clean accessible way, but its slow. SO I just want a command line app that emboldens everything in the font, and considering that there are things like RotateFont, was hoping there was something comparable to embold.

fanatic font addict's picture

edit

"I don't care how it does it really, unless its comparable to what is already typically done to algorithmically embolden fonts."

should read

"I don't care how it does it really, as long as its comparable to what is already typically done to algorithmically embolden fonts."

fanatic font addict's picture

To Michael Boyer:

I discovered there are versions of fontforge not requiring cygwin. Didn't realize the significance of that scripting feature, so depending on the speed it may work for me. Just wanted to acknowledge that because my original response to you was slightly oblivious. So thanks to you and James Puckett for mentioning FontForge.

Khaled Hosny's picture

If you are concerned about speed, then you better off playing with FreeType. FontForge is quite slow and overkill for your case, as, I think, it trays to analyse the glyph trying to produce better result.

fanatic font addict's picture

Khaled:

Yes, I concluded that Fontforge is unacceptable. The exe in whatever version I downloaded is 14 mb. So that's nothing I can distribute. Its a big bloated behemoth with overlapping confusing documentation and features. Glad some people have been able to make use of it.

Just to review my experience fwiw (for anyone else's benefit) as I said in the OP I had what I needed working with FreeType and ttx. Just to elaborate, FreeType can perform various transforming algorithms on glyphs but for whatever reason is not set up to write that all back to the ttf file. THat is where ttx comes in - a well known utility that will decompile a ttf (and other formats) into XML and then recompile it back into a ttf. So what I needed working I had working with those two (FreeType and ttx). But the problem is ttx is in python (an interpreted, and slow system) and also its generates this humongous XML file that is dealing with. So the time penalty for all that is about 5 seconds, which in the context of user software is a little excessive.

But there truly for whatever reason just doesn't seem to be any command line utilities that will perform straightforwatrd glyph transformations on a font file. Its ridiculous. I mentioned 'rotateFont' previously, which is part of something Adobe released - some set of tools their developers ostensibly use that they're magnanimously making available to us all. But after struggling with RotateFont for half and hour I gave up. I defy anyone to make it do what its purported to do.

So in the last couple of days I did write my own parser to get the glyph data from a ttf/otf file and then put it back after making changes (thus avoiding the time penalty of ttx involving python and that huge text file). And as I expected it immediately shaved off all that excess time. However thus far it correctly parses the font file only about 2/3's of the time. So there are just a lot of nuances in the ttf/otf format that you have to nail down. I'm not going to reinvent the wheel there, so have just decided to go with my original setup of ttx and Freetype.

Not to knock ttx at all, because it is quite accurate and reliable as far as dealing with the font file - its just kind of slow. So why no one ported its functionality to an efficient (c++ based) executable is beyond me. (You don't need Python installed to run ttx, but its still accessing it via a dll or something so still running as an interpreted program.) Actually the ttx version I'm using is from 2002, because that was what was in the win binary distributable from their own website, but there are a score of other ttx versions floating around as well from different years, so my next step will be to maybe compile the lastest ttx source code. So anyway, in case anyone googles this topic in the future maybe the above will be of use.

Note: In those Adobe utilities is something called sfntedit, and that is actually quite good (the only thing in it that's good, other than ttx which they include with their stuff as well.) But sfntedit will extract a font table to a file and put it back, but its very useful and reliable.

Khaled Hosny's picture

@fanatic font addict
Yes, I concluded that Fontforge is unacceptable. The exe in whatever version I downloaded is 14 mb. So that's nothing I can distribute. Its a big bloated behemoth with overlapping confusing documentation and features. Glad some people have been able to make use of it.

It is a font editor with support for more than a dozen of font formates and technologies, so the complexity is very acceptable, I also find the documentation very extensive and helpful. If you don't understand it, then it is because type design is complex, not any fault of FontForge, you are just using the wrong tool for what you want.

Michel Boyer's picture

The exe in whatever version I downloaded is 14 mb.

Concerning size (you may have other good reasons not to use FontForge) the python library that can be used for scripting (that I used above and that of course requires Python to work) is just 68K on my macintosh. More precisely, fontforge.so is 32K and psMat.so is 32K. Those files are installed in /Library/Python/2.5/site-packages by the postflight in the package FontForge_macunivX.5-20090923.pkg that you can find at the url http://sourceforge.net/projects/fontforge/files/fontforge-executables/.

Michel

PS Of course, the files above are for the macintosh. I have never looked at what is available for other architectures.

Michel Boyer's picture

Of course 32+32 is not equal to 68. There is a tiny info file that takes 4K in the file system though it is just 10 short lines of text.

Michel Boyer's picture

Sorry, the 68K count makes no sense. Those packages rely on files in /usr/local/lib that are quite big and that come with FontForge (and I knew, and I can't believe it slipped my mind!)

fanatic font addict's picture

Thanks, Michel Boyer. Just as an update as I haven't visited this thread in while - I did eventually just write my own C++ code (about 600 lines) to modify font file glyf tables, and did eventually get it all working correctly. It still uses the FreeType library to do the matrix conversions, but I bypass the bottleneck of ttx, by accessing the tables directly myself. I still use ttx to change the font name table (appending 'bold' or 'italic' where appropriate), but the glyf table is huge, and converting it all to a xml text file as ttx does is a huge bottleneck. What has to be done to directly modify the glyf table (and also the loca table) is all very arcane though.

(I also invoked sfntedit extensively in my code as well. Its very useful.)

Read Roberts's picture

About rotateFont: when you say "after struggling with RotateFont for half and hour I gave up", can you tell me what it was you were trying to do, and whether it was under Windows or Mac OSX? As the person who maintains it, I like to know about any bugs. The one issue I know about is that none of the FDK tools produce TTF glyph data: although many can read TTF data, they output CFF glyph data.

- Read Roberts

fanatic font addict's picture

Read Roberts

"About rotateFont: when you say "after struggling with RotateFont for half and hour I gave up", can you tell me what it was you were trying to do..."

Just apply a matrix transformation to all the glyfs in the font file, to give them all an 'italic' slant angle.

Of course, if people customarily use it for this purpose with little or no problem then don't let my experience discourage you. But I couldn't figure it out. Although I may have been sleep-deprived at the time.

I also wanted to do an artificial bold for the font as well. At the time I didn't know how I was going to do bold algorithmically, and my decision against RotateFont may have been partially influenced by the conclusion that I would still need some other tool to do bold.

Also RotateFont seemed to have a whole slough of features which made me concerned about efficiency speed, etc. But the main thing was I never even figured out how to do italic.

fanatic font addict's picture

(cont.)

[RR:]
"whether it was under Windows or Mac OSX"

Windows

"The one issue I know about is that none of the FDK tools produce TTF glyph data: although many can read TTF data, they output CFF glyph data."

Well, both ttx and sfntedit will output ttf (and are both included with the fdk). Didn't know that RotateFont would not.

Syndicate content Syndicate content