Chinese characters cannot be displayed
Closed this issue · 1 comments
zenglei286 commented
zenglei286 commented
TextFragment.cs
public override void OnRender(DrawingContext drawingContext, Rect bounds)
{
if (Length == 0)
return;
GlyphTypeface glyphTypeface = View.Content.GlyphTypeface;
var list = ToString().ToList();
Length = list.Count;
var gg = new ushort[list.Count];
double charWidth = View.Content.CharWidth;
var advanceWidths = new double[Length];
for (int i = 0; i < list.Count; i++)
{
char ch = list[i];
try
{
var glyphIndex = glyphTypeface.CharacterToGlyphMap[ch];
gg[i] = glyphIndex;
if (Encoding.GetByteCount(ch.ToString()) > 1)
{
advanceWidths[i] = charWidth * 2;
}
else
{
advanceWidths[i] = charWidth;
}
}
catch (Exception)
{
gg[i] = ch;
advanceWidths[i] = charWidth;
}
}
// var glyphIndices = View.Content.GetGlyphIndices(Data, Offset, Length);
//byte[] bytes = glyphIndices.Select(m => (byte)m).ToArray();
double fontSize = View.Content.LineHeight - 2; // keep padding
var baseline = new Point(0, View.Content.SnapToPixelsY(glyphTypeface.Baseline * fontSize + 1.5, ceiling: true));
GlyphRun run = new GlyphRun(glyphTypeface,
bidiLevel: 0,
isSideways: false,
renderingEmSize: fontSize,
glyphIndices: gg,
baselineOrigin: baseline,
advanceWidths: advanceWidths,
glyphOffsets: null,
characters: null,
deviceFontName: null,
clusterMap: null,
caretStops: null,
language: null);
drawingContext.PushTransform(new TranslateTransform(bounds.X, bounds.Y));
drawingContext.DrawGlyphRun(Brush, run);
drawingContext.Pop();
}