Skip to content

Latest commit

 

History

History
86 lines (64 loc) · 6.95 KB

Automatic Vector Fonts Generator Project.md

File metadata and controls

86 lines (64 loc) · 6.95 KB

Automatic Vector Fonts Generator Project – Glyphs, their widths and kerning pairs

This project started with the modification of the distances between certain pairs of characters, which is broadly known as kerning, of Type 1 Fonts in PostScript. This was used to display right justified text using the function BreakIntoLines, a modified and enhanced version of a function with same name shown in the PostScript Cookbook (Blue Book) coded in PostScript. Postscript has been used as a prototype language to develop a much more complex project (π Vector GUI for Java and Android) which this project is part of.  

The PostScript prototype has been used for different applications, but had quite a few inconveniences. First, the kerning had to be done by hand (the font kerning information is encrypted in the font and not available in PostScript). Second, this prototype cannot be used in another context (in a Java program, for example), except for showing static vectorized texts. A vectorized text is a text represented by their graphical shapes, broadly called glyphs. A glyph is a series of commands to draw a character. Long vectorized texts are not a compact way to represent these texts because it will contain several repetitions of the same glyphs, thus wasting considerable space. It is neither a convenient way to represent texts in a program, which is mainly done using sequences of character codes (ASCII or Unicode, for example), broadly called as strings. In text entered by the user, such as in a copy-paste context, for example, it is not possible to substitute the use of strings with their vectorized form. On the other hand, one cannot use PostScript to show messages in a program since PostScript interpreters are extremely complex and heavy.

The solution is to have fonts available to the program in order to show the glyphs corresponding to their characters. But, again, a font is an extremely complex file format that must be read in order to use its information. A program that reads fonts is called a parser, and a font parser is also a complex and heavy piece of software. Thus, it is far more convenient to use a font parser to extract the essential information and to generate a much more compact piece of code to make this information available to the program.

This is what has been accomplished in this project and what follows is a detailed description on how this has been accomplished. The essential information needed to display texts correctly on the screen using fonts is: the glyphs, their widths and the kerning pairs.

Besides glyphs and their widths, after several attempts to use font parsers in Java, a simple kerning pairs table could be acquired only for Truetype fonts. A more complete kerning pairs table may be supplied in the GPOS table, which is the only one available for Postscript fonts in Opentype font files. It is understandable that Adobe had done that to enhance the quality of their fonts. These fonts are generally also more compact and smoother because they use cubic Bezier curves instead of quadric Bezier curves used in Truetype. Fonts are a major business for Adobe and to guarantee that customers would continue to pay premium for them, they must make an extra effort in quality and in security to avoid copies. GPOS table is so complex to access and the documentation so scarce that even hackers get discouraged.

With the need of fonts on the web, this table was cracked by Opentype.js, a very sophisticated and high level font parser. However, this has been an information mainly known by Javascript and web programmers.

Automatic Vector Fonts Generator Project – Major Breakthrough

A huge breakthrough has been acquired by using OpenType.js and the glyph inspector shown in the site https://opentype.js.org. This site provides code to use OpenType.js in Javascript. The glyph inspector was modified and added the feature to generate a Java class with the essential information of the font.

image

Error in Glyph Inspector

The only caveat found was that the code in this site had a bug. It was discovered that the function that returns the commands defining the glyphs were not being interpreted in the correct order when a set of points for cubic bezier curves are asked. This had been debugged using JSON.stringify that showed the correct order.

image

Font Transformed in a Java Class

This is how the embedded font looks like: image

Each glyph of the font is defined as a Path2D.Float path, composed by commands moveTo, lineTo, quadTo, curveTo, and closePath, with the respective point coordinates. These coordinates are assumed as the glyph was positioned at the origin.

Modification of Kerning Pairs

In the PostScript prototype the original kerning pairs could be modified by adding additional kerning information to the font. This is an advanced feature that was added to the BreakIntoLines function. In normal Truetype and Opentype fonts this obviously cannot be done except using specialized software to modify fonts. Modification of this information should be accessible and easy to be done. Unfortunately this is not the case neither in nowadays fonts, neither in programs that use them.

After the automatic generation of a font class, as it has been previously presented, this class has three essential information. One of them is the kerning pairs table. Since this table is declared in Java, the information is directly accessible and modifiable. It is declared as int k[][][]. The first dimension of k is accessed by an index which is the code of the first character subtracted by 32 because all characters prior to blank are not visible, thus they don’t need to be kerned. This indexation returns a table that contains the set of second characters of the pairs. The second character is searched in this table using a binary search. The searched character is in reality another table with two elements, the first containing the character to be searched and the second is an integer with the kerning distance. The example below is for the letter ‘I’, forming pairs with characters ‘f’, ‘t’, ‘v’, ‘w’, ‘y’ and ‘z’ (inexistent pairs have distance zero):

{ { 'I' }, { 'f', 7 }, { 't', 6 }, { 'v', 5 }, { 'w', 5 }, { 'y', 5 }, { 'z', 7 } },

This two elements table can be modified with any desirable distance that will determine the kerning distance between the first character (‘I’ in the example above) and the second. For cases of an inexistent pair the character with the corresponding distance must be added in alphabetic order.