逐个字符绘制文字时出现字距问题

编程入门 行业动态 更新时间:2024-10-26 09:20:11
本文介绍了逐个字符绘制文字时出现字距问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

逐个字符绘制文字时出现字距问题

本文发布于:2023-11-23 03:53:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   文字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!