查找文本“空格”以像素为单位的值

发布于 2024-11-05 21:13:56 字数 156 浏览 1 评论 0原文

在处理字体时,有没有办法计算出如果是“1 2”,两个字符之间的间距应该是多少?

如果它在 C++ 或 C# 中是已知的,我不介意。如果是 C++ 语言,我会将其封送过来,如果是 C# 语言,我会将其保存到文件中并在 C++ 中加载。我尝试查看 TextMetric 结构,但不存在。

With dealing with fonts, is there a way to figure out what the space between two characters should be if it was "1 2"?

If it is known in C++ or C#, I don't mind. If it is in C++, I will marshal it over, it if is in C#, I will save it to a file and load it in C++. I tried taking a look at the TextMetric structure but it was not there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不打扰别人 2024-11-12 21:13:56

Graphics.MeasureCharacterRanges 在测量单个值时获得非常好的结果通过在前后添加零宽度连接符 \u200D 来形成字形(甚至空格)。然而,由于我认为是零宽度连接器,它确实忽略了所有字偶距调整的内容。

您需要一堆附加参数,但是 new StringFormat(StringFormat.GenericTypgraphic)Rectangle.Empty 对我来说效果很好。

它返回字符串中每个字符的漂亮浮点边界数组。

Graphics.MeasureCharacterRanges gets pretty good results measuring single glyphs (even spaces) by adding a zero-width-joiner character \u200D before and after. It does however ignore all Kerning stuff, due to the zero-width-joiners I think.

You need a bunch of additional parameters, but a new StringFormat(StringFormat.GenericTypographic) and Rectangle.Empty did just fine for me.

It returns an array of nice floating point bounds of each character in the string.

七色彩虹 2024-11-12 21:13:56

如果您想要衡量文本呈现方式的指标,则需要提供文本。您需要提供 ascii 空格字符。现代系统使用字距调整来调整间距并使文本看起来更有吸引力。

If you want metrics of how text will be rendered you need to provide the text. You would need to provide the ascii space character. Modern systems using Kerning to adjust spacing and make text look more attractive.

海风掠过北极光 2024-11-12 21:13:56

您可以使用 Graphics.MeasureString() 并传入字体和 < code>" " 带空格的字符串?

Can you use Graphics.MeasureString() and pass in a font and " " string with space ?

滥情空心 2024-11-12 21:13:56

下面是使用表单的 Paint 事件处理程序的 C# 示例:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    float widthWithSpace = e.Graphics.MeasureString("1 2", new Font("Arial", 12)).Width;
    float widthWithoutSpace = e.Graphics.MeasureString("12", new Font("Arial", 12)).Width;
    float spaceWidthInPixels = widthWithSpace - widthWithoutSpace ;
}

Here is an example in C# using a form's Paint event handler:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    float widthWithSpace = e.Graphics.MeasureString("1 2", new Font("Arial", 12)).Width;
    float widthWithoutSpace = e.Graphics.MeasureString("12", new Font("Arial", 12)).Width;
    float spaceWidthInPixels = widthWithSpace - widthWithoutSpace ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文