How can I use and extract kerning pairs from from GPOS table in Opentype fonts to correctly show glyphs as Path2D in Java?

后端 未结 1 1851
轮回少年
轮回少年 2020-12-04 03:55

This question is related to a few questions that have been asked long time ago. I saw comments that Opentype fonts were not supported in Java, but this was

相关标签:
1条回答
  • 2020-12-04 04:46

    Problem solved!

    An advice: if you are trying to do this in Java you are losing your time. This problem was solved simply using Opentype.js and the site https://opentype.js.org, but particularly glyph inspector and font inspector.

    I downloaded both code by copying and pasting from page source. I then modified glyph inspector to do the job. You need to go deeply into Opentype.js to get the kerning pairs, though, but it is all there (check code below).

    I ended up porting the entire program to JavaScript. I didn't program JavaScript too much before. Therefore, it must be extremely easy for whom already programs in JavaScript. This program just generates a Java class with the glyphs, the kerning pairs and widths (the advanceWidth for each glyph).

    Here, an image to show the result:

    The code below in JavaScript dumps the GPOS kerning table into the string text, presenting a list of sets containing the second character and the kern value in each line, while the first character of the pair appears as a Java character at the beginning of the line. Notice, that glyph indexes are avoided by using the ASCII codes of the characters.

    This only dumps ' '(space) to '}', which is what is useful for English language. To expand to other characters just use Unicode.

    <!-- indoc: true -> writes in HTML on this page  -->
    const indoc = true;
    const nl = (indoc) ? "<br>" : "\n";
    var chars = font.tables.cmap.glyphIndexMap;
    var g1,g2;
    var i, j, kern;
    var text = "";
    for ( i = 32; i < 126; i++ ) {
        g1 = font.glyphs.get(chars[i]);
        text += "'" +
        (( i == 39 || i == 92) ? "\\" + String.fromCharCode(i) : String.fromCharCode(i) )+ "'";
        for ( j = 32; j < 126; j++ ) {
            g2 = font.glyphs.get(chars[j]);
            if ( (kern = font.getKerningValue(g1,g2)) !== 0 ) {
                text += ", { '" +
                (( j == 39 || j == 92) ? "\\" + String.fromCharCode(j) : String.fromCharCode(j) )+
                "', " + kern + " }";
            }
        }
        text += nl;
    }
    
    0 讨论(0)
提交回复
热议问题