1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
Typeface typeface = new Typeface(new FontFamily("Lucida Console"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
GlyphTypeface glyphTypeface;
if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
throw new Exception("No GlyphTypeface found");
// Calcul de la largeur d'un caractère en fonction du Typeface et de la taille de la police
double fontSize = 14;
FormattedText formattedText = new FormattedText("A", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black);
double charWidth = formattedText.Width;
string text = "Mon texte à afficher";
ushort[] glyphIndexes = new ushort[text.Length];
double[] advanceWidths = new double[text.Length];
for (int i = 0; i < text.Length; i++)
{
glyphIndexes[i] = (ushort)(text[i] - 29);
advanceWidths[i] = (double)charWidth;
}
GlyphRun glyphRun = new GlyphRun(glyphTypeface, 0, false, fontSize, glyphIndexes, new Point(0, 0), advanceWidths, null, null, null, null, null, null);
drawingContext.DrawGlyphRun(Brushes.Black, glyphRun);
} |
Partager