How to use kerning pairs extracted from a TTF file to correctly show glyphs as Path2D in Java?

后端 未结 2 1167
孤城傲影
孤城傲影 2021-01-25 13:38

This question is about recovering glyph font information in Java and it is related to a question posted here. For more details please check the question and ans

相关标签:
2条回答
  • 2021-01-25 14:17

    GNU Classpath contains an example, gnu.classpath.examples.awt.HintingDemo.java, that may help to solve this problem. This example allows you to visualize glyphs. It reads the font and interprets the language for hints given in it. You can choose to show with hints or without them (hinted glyphs are good for small font sizes but not recommended in large sizes). If you are not used to Truetype hints you will understand with this demo that they align the paths within integer boundaries. The program isn't very fancy but it has all the necessary tools to read the glyphs and interpret the hints with the advantage of visualizing the results.

    You don't need the whole package to compile and run this demo. If you are using Eclipse it is easy to create a project for it. First create the packages gnu.classpath.examples.awt and import HintingDemo.java in it. Then you just import all its dependencies, file by file or whole packages at a time. For example, you can import the whole package gnu.java.awt.font and erase OpenTypeFontPeer.java (the demo doesn't need it and it causes an error if you leave it).

    This gives a standalone way to read and display glyphs directly from the font file. Interestingly, it doesn't use any kerning information. This has to be added with Apache FOP library. If reading the file twice is a problem you will need a workaround, either going deeply into GNU Classpath to get the same information, or trying to make Apache FOP "to talk" with GNU Classpath. At this time I am unable to say how difficult this is. I am using it only as tools to copy the information and using it elsewhere, not as a way to really read font files in an actual program. Fonts are very compact but are not the most efficient way to display text, especially where there is interpretation of the font language as in the case of Type 1 and Truetype fonts. Getting rid of this interpretation looks like a good idea if you are willing high quality and speed.

    0 讨论(0)
  • 2021-01-25 14:33

    Problem solved!

    Recalling that to open the file and to obtain the kerning pairs one needs this code, using the library Apache FOP:

    TTFFile file;
    File ttf = new File("C:\\Windows\\Fonts\\calibri.ttf" );
    try { file = TTFFile.open(ttf); }
    catch (IOException e) {e.printStackTrace(); }
    Map<Integer, Map<Integer, Integer>> kerning = file.getKerning();
    

    The following piece of code to vectorize the glyphs is correct now:

    Font font = new Font("Calibri", Font.PLAIN, 2048);
    int size = '}' - ' ' + 1;
    Path2D.Float[] glyphs = new Path2D.Float[size];
    //double[] lens = new double[size];
    String chars[] = new String[size];
    int i; char c; 
    char[] s = { '0' };
    for (i = 0, c = ' '; c <= '}'; c++, i++) { s[0] = c; chars[i] = new String(s); }
    for (i = 0; i < size; i++) {
        vectorize(glyphs[i] = new Path2D.Float(), chars[i]);
        //lens[i] = glyphs[i].getBounds2D().getWidth();
    }
    

    Notice that now the font size is 2048 which is the unitsPerEm for this particular font. This value is given by the HEAD tag in the font file as explained here.

    Notice that the widths cannot be given by the array lens and code commented out above. It has to be read from the file. Using int width = getCharWidthRaw(prev) from Apache FOP, where prev is the previous character, width is the raw width of the character as written in the file. This value has to be added to the kerning pair value that can be obtained in the map kerning.

    The map is used this way: kerning.get(prev) which returns another map containing the characters and kerning values to be added. If the character to be shown next is found in this map, the corresponding value is added to width. If not found, or if null is returned, there is no kerning value for this pair.

    Here it is a text to show the kerning now works.

    0 讨论(0)
提交回复
热议问题