Kerning problems when drawing text character by character

爷,独闯天下 提交于 2019-12-23 10:19:48

问题


I'm trying to draw strings character by character to add lighting effects to shapes composed of text.

while (i != line.length()) {
c = line.substring(i, i + 1);

cWidth = g.getFontMetrics().stringWidth(c);

g.drawString(c, xx += cWidth, yy);
i++;
}

The problem is, the width of a character isn't the actual distance it's drawn from another character when those two characters are printed as a string. Is there any way to get the correct distance in graphics2d?


回答1:


The answer by Lukas Baran solves the main problem that was probably causing your output to look bad. However, the more subtle problem that you can't replicate kerning in this way remains. How much of a problem this is may depend on the font you're using. To get the kerning right, too, you could do something like this:

while (i != line.length()) {
  String c = line.substring(i, i + 1);
  String d = line.substring(0, i + 1);
  int cWidth = g.getFontMetrics().stringWidth(c);
  int dWidth = g.getFontMetrics().stringWidth(d);
  g.drawString(c, xx + dWidth - cWidth, yy);
  i++;
}

That should place each character where the kerning would have placed it.




回答2:


The problem is that kerning defines the spacing of pairs of letters, and iterating over a string char-by-char gives the kerning system no chance to kick in. I think you'll have to use either a fixed-width font or rework your lighting effect so it works with full strings instead of single chars.




回答3:


I'm not sure if I understood your problem correctly. However, I have tested your code and indeed some string characters overlapped each other.

The problem was in a way you're incrementing xx value (you were incrementing it before drawing a character).

Here's corrected version of your code:

public void myDrawString(Graphics g, String line, int xx, int yy) {
    int i = 0;
    while (i != line.length()) {
        String c = line.substring(i, i + 1);
        int cWidth = g.getFontMetrics().stringWidth(c);
        g.drawString(c, xx, yy);
        xx += cWidth;
        //xx += 2;
        i++;
    }
}

Uncomment and adjust xx += 2 line to increase space between characters.



来源:https://stackoverflow.com/questions/4582545/kerning-problems-when-drawing-text-character-by-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!