简体   繁体   English

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

[英]Finding the text “space” value in pixels

With dealing with fonts, is there a way to figure out what the space between two characters should be if it was "1 2"? 处理字体时,是否有办法弄清楚两个字符之间的空格应该是“ 1 2”?

If it is known in C++ or C#, I don't mind. 如果它在C ++或C#中是已知的,我不在乎。 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++. 如果使用C ++,我将封送它,如果使用C#,则将其保存到文件中并用C ++加载。 I tried taking a look at the TextMetric structure but it was not there. 我尝试看一下TextMetric结构,但它不存在。

Graphics.MeasureCharacterRanges gets pretty good results measuring single glyphs (even spaces) by adding a zero-width-joiner character \‍ before and after. 通过在前后添加一个零宽度的连接符\‍Graphics.MeasureCharacterRanges可以很好地测量单个字形(偶数空格)。 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. 您需要一堆其他参数,但是new StringFormat(StringFormat.GenericTypographic)Rectangle.Empty对我来说很好。

It returns an array of nice floating point bounds of each character in the string. 它返回字符串中每个字符的精确浮点范围的数组。

你可以使用Graphics.MeasureString() ,并通过在字体和" "字符串空间?

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. 您需要提供ascii空格字符。 Modern systems using Kerning to adjust spacing and make text look more attractive. 现代系统使用字距调整来调整间距并使文本看起来更具吸引力。

Here is an example in C# using a form's Paint event handler: 这是使用窗体的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 ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM