C# 在sharp dx中查找GlyphRun的宽度
我无法使用sharp DX计算glyphRun的宽度。下面是我用来呈现glyphRun的代码,字符串需要呈现为char,如下所示C# 在sharp dx中查找GlyphRun的宽度,c#,text,directx,sharpdx,letter-spacing,C#,Text,Directx,Sharpdx,Letter Spacing,我无法使用sharp DX计算glyphRun的宽度。下面是我用来呈现glyphRun的代码,字符串需要呈现为char,如下所示 private void RenderGlyphRun(FontFace fontFace) { GlyphRun glyphRun = new GlyphRun(); glyphRun.FontFace = fontFace; glyphRun.FontSize = 23; left = 50;
private void RenderGlyphRun(FontFace fontFace)
{
GlyphRun glyphRun = new GlyphRun();
glyphRun.FontFace = fontFace;
glyphRun.FontSize = 23;
left = 50;
top = 50;
string word = "Hello World";
foreach (char letter in word)
{
string stringToBeRendered = letter.ToString();
int[] codePoints = new int[stringToBeRendered.Length];
char[] charsToBeRendered = stringToBeRendered.ToCharArray();
for (int i = 0; i < charsToBeRendered.Length; i++)
{
codePoints[i] = (int)charsToBeRendered[i];
}
short[] glyphIndices = fontFace.GetGlyphIndices(codePoints);
glyphRun.Indices = glyphIndices;
var BrushColor = SharpDX.Color.Black;
SharpDX.DirectWrite.Matrix mtrx = new SharpDX.DirectWrite.Matrix();
mtrx.M11 = 1F;
mtrx.M12 = 0;
mtrx.M21 = 0;
mtrx.M22 = 1F;
mtrx.Dx = 0;
mtrx.Dy = 0;
GlyphMetrics[] metrics = fontFace.GetGdiCompatibleGlyphMetrics(23, 1, mtrx, false, glyphIndices, false);
FontMetrics metr = fontFace.GetGdiCompatibleMetrics(23, 1, new SharpDX.DirectWrite.Matrix());
_pRenderTarget.DrawGlyphRun(new SharpDX.DrawingPointF(left, top), glyphRun, new SharpDX.Direct2D1.SolidColorBrush(_pRenderTarget, BrushColor), MeasuringMode.GdiClassic);
left += (metrics[0].AdvanceWidth - metrics[0].RightSideBearing + metrics[0].LeftSideBearing) / 100;
}
}
private void renderglyprun(FontFace-FontFace)
{
GlyphRun GlyphRun=新GlyphRun();
glyphRun.FontFace=FontFace;
glyphRun.FontSize=23;
左=50;
top=50;
string word=“Hello World”;
foreach(word中的字符字母)
{
string StringToBerndered=letter.ToString();
int[]代码点=新的int[StringToBerndered.Length];
char[]charstoberended=stringToBeRendered.ToCharArray();
for(int i=0;i
渲染字符串中字符的间距非常不同。请帮帮我。@suresh
您可以在glyphRun中计算每个字形的宽度,如下所示
clusterwidth=glyphRun.glyphRun.Advances[Index]
for(int i=0; i<glyphRun.Indices.Length; i++)
{
totalWidth+=glyphRun.Advances[i];
}
基于此,您可以计算总字形运行宽度,如下所示
clusterwidth=glyphRun.glyphRun.Advances[Index]
for(int i=0; i<glyphRun.Indices.Length; i++)
{
totalWidth+=glyphRun.Advances[i];
}
用于(int i=0;i+1)到pashaplus,用于将我指向正确的方向
我需要做同样的事情,我使用一个简单的LINQ表达式来得到这样的和
var glyphRunWidth = drawing.GlyphRun.AdvanceWidths.Sum(a => a);