Typophile

How do I get bitmap output of glyphs in FontLab 5?

Soroush Rabiei

Hi there

I'm writing a script for optimizing my kerning algorithm. so I need to get bitmap outputs of glyphs in FontLab. I tried to render it using PIL but FontLab scripting features didn't tell me position of all nodes -> using postscript curves, there is no data about control points (handles) of nodes in the 'nodes' list of a glyph. So images that the script generates are not precise enough to expand kerning scale.

Is there a way to get such image directly? fore example to get an image of a certain glyph in a given em. I know FontLab uses FreeType to render glyph shapes. I have installed sip library. so could I use some API of FontLab to accomplish?

ontLab scripting features didn't tell me position of all nodes -> using postscript curves, there is no data about control points (handles) of nodes in the 'nodes' list of a glyph

No, you have to iterate through each node’s points list to get all the point positions.

Check out the FontLab Python documentation if you haven’t already: http://www.e-font.de/flpydoc/

There is Glyph.Rasterize(), but I'm not sure you can control the ppm size.

Hope this helps,
Jens

Thanks Jens :)

Soroush,

you should find this interesting:
http://forum.fontlab.com/python-scripting/pilrasterizeglyphstofolderttgp...

Best,
Adam

twardoch,

Of course it will be easier than generating image from curves, but I can't use Rasterize(). I just call Rasterize() for generating bacground image :-/ Trying to write some FLtoPIL, I used Glyph.image but it just gives me a binary image that the glyph is tightly bounded into it. So baseline and metrics of a glyph are missed in the final output:

,

I need an image with applied metrics of glyph (left,right,ascender,descender). but can't calculate scale of image to generate such an output...

You need to calculate the offsets yourself, based on the original glyph's bounding box. I'm not sure how you'd expect it to work directly, i.e. how e.g. you'd expect the baseline to be "stored" in the image for a letter such as "p"?

When you're generating a background image in FontLab Studio, the image is rendered at the ppem size determined by FL.Options().BitmapSize. If you set BitmapSize to 1000 then one pixel in your image will correspond to one font unit in a 1000 UPM font (Font().upm). If you set BitmapSize to 200, then one pixel will correspond to 5 font units in such a font. So there you have the scaling factor you need.

By calling Glyph().GetBoundingRect(), you'll get the actual position of the glyph drawing within the design space (in font units). By querying for Glyph().width, you get the advance width. From the difference of GetBoundingRect().ll.x and .ur.x, and Glyph().width, you'll easily get the sidebearings, and from .ll.y, you'll get the position relative to the baseline. All in font units, but you can then convert them to pixel dimensions by comparing Font().upm and Options().BitmapSize.

As for the maximal y dimension to which you should crop your images, there are various scenarios possible. You could crop to the height of the font's bounding box -- which you'd need to calculate yourself by iterating for Glyph().GetBoundingRect() for all glyphs and choosing the maximal value -- or you could crop e.g. to one of the vertical dimensions (such as OS/2.WinAscent and OS/2.WinDescent, but then you're risking that some of your glyphs may be cropped too much.

It all depends on what you really need this information for.

Best,
Adam

Finished up with: 1-setting BitmapSize=1000 2-shifting result of Rasterize().
That was exactly what I needed.
Thanks :)

Good :)

Captured from web.archive.org on 15 Apr 2015. view this capture