1
我無法設法使用銳利的DX來計算glyphRun的寬度。以下是我用來渲染glyphRun的代碼,並且需要將字符串呈現爲char,如下所示。以銳利的DX查找GlyphRun的寬度
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;
}
}
呈現的字符串中字符的間距非常不同。請幫我解決一下這個。