1

我无法使用尖锐的 DX 计算 glyphRun 的宽度。下面是我用来呈现 glyphRun 的代码,字符串需要呈现为 char,如下所示。

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;                
        }
    }

渲染字符串中的字符间距非常不同。请帮我解决一下这个。

4

2 回答 2

2

@suresh,

您可以计算glyphRun中的每个字形宽度,如下所示

clusterwidth=glyphRun.glyphRun.Advances[Index]

基于此,您可以计算总字形运行宽度,如下所示

for(int i=0; i<glyphRun.Indices.Length; i++)
{
totalWidth+=glyphRun.Advances[i];
}
于 2013-01-18T19:01:18.380 回答
1

+1 到 pashaplus 为我指出正确的方向。

我需要做同样的事情,我使用了一个简单的 LINQ 表达式来得到这样的总和。

var glyphRunWidth = drawing.GlyphRun.AdvanceWidths.Sum(a => a);
于 2014-12-23T23:10:15.243 回答