逐个字符绘制文字时出现字距问题 [英] Kerning problems when drawing text character by character

查看:68
本文介绍了逐个字符绘制文字时出现字距问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图逐个字符地绘制字符串,以将照明效果添加到由文本组成的形状中.

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++;
}

问题是,当这两个字符打印为字符串时,字符的宽度不是它与另一个字符的实际距离.有什么方法可以在graphics2d中获得正确的距离?

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?

推荐答案

Lukas Baran的答案解决了可能导致您的输出看起来很糟糕的主要问题.但是,您无法以这种方式复制字距调整的更细微的问题仍然存在.这到底有多少问题,可能取决于您使用的字体.为了正确调整字距,您可以执行以下操作:

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.

这篇关于逐个字符绘制文字时出现字距问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆