using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace GlyphPlayground
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
myCanvas.Background = Brushes.White;
//myCanvas.Children.Add(new test());
var g = new Glyphs();
g.FontUri = new Uri("C:\\WINDOWS\\Fonts\\ARIAL.TTF");
int size = 30;
double totalWidth = 0;
string text = "Ay";
var typeface = getGlyphTypeFace("Arial");
string indices = "";
for (int n = 0; n < text.Length; n++)
{
ushort glyphIndex = typeface.CharacterToGlyphMap[text[n]];
indices += glyphIndex + ";";
double width = typeface.AdvanceWidths[glyphIndex] * size;
totalWidth += width;
}
indices = indices.Substring(0, indices.Length - 1);
g.Fill = Brushes.Black;
g.UnicodeString = text;
g.OriginX = 30;
g.OriginY = 500 + size;
//indices = ",,,,0";// +indices;
//g.Indices = indices;
g.FontRenderingEmSize = size;
myCanvas.Children.Add(g);
var cursor = new Rectangle();
cursor.Height = typeface.Height *size;
cursor.Width = 2;
cursor.Fill = Brushes.Black;
Canvas.SetLeft(cursor, 30 + getCharWidth('A', typeface, size) + getCharWidth('y', typeface, size));
Canvas.SetTop(cursor, g.OriginY - typeface.Baseline * size);
myCanvas.Children.Add(cursor);
var h = myCanvas.Height;
var timer = new DispatcherTimer();
timer.Tick += (obj, args) =>
{
cursor.Fill = (cursor.Fill == Brushes.White) ? Brushes.Black : Brushes.White;
cursor.InvalidateVisual();
};
timer.Interval = new TimeSpan(0, 0, 0, 0, 800);
timer.Start();
this.KeyDown += (sender, args) =>
{
var key = args.Key.ToString();
g.UnicodeString += key;
Canvas.SetLeft(cursor, (double)cursor.GetValue(Canvas.LeftProperty) + getCharWidth(key[0], typeface, size));
g.InvalidateVisual();
cursor.InvalidateVisual();
};
}
private GlyphTypeface getGlyphTypeFace(string fontName)
{
Typeface typeface = new Typeface(fontName);
GlyphTypeface glyphTypeface;
if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
throw new InvalidOperationException("No glyphtypeface found");
return glyphTypeface;
}
private double getCharWidth(char ch, GlyphTypeface typeface, int size)
{
var charIndex = typeface.CharacterToGlyphMap[ch];
return typeface.AdvanceWidths[charIndex] * size;
}
}
class test : UIElement
{
protected override void OnRender(DrawingContext dc)
{
Typeface typeface = new Typeface(new FontFamily("Arial"),
FontStyles.Italic,
FontWeights.Normal,
FontStretches.Normal);
GlyphTypeface glyphTypeface;
if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
throw new InvalidOperationException("No glyphtypeface found");
string text = "Hello, worldy!";
double size = 40;
ushort[] glyphIndexes = new ushort[text.Length];
double[] advanceWidths = new double[text.Length];
double totalWidth = 0;
for (int n = 0; n < text.Length; n++)
{
ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
glyphIndexes[n] = glyphIndex;
double width = glyphTypeface.AdvanceWidths[glyphIndex] * size;
advanceWidths[n] = width;
totalWidth += width;
}
Point origin = new Point(175, 50);
GlyphRun glyphRun = new GlyphRun(glyphTypeface, 0, false, size,
glyphIndexes, origin, advanceWidths, null, null, null, null,
null, null);
dc.DrawGlyphRun(Brushes.Black, glyphRun);
double y = origin.Y;
dc.DrawLine(new Pen(Brushes.Red, 1), new Point(origin.X, y),
new Point(origin.X + totalWidth, y));
y -= (glyphTypeface.Baseline * size);
dc.DrawLine(new Pen(Brushes.Green, 1), new Point(origin.X, y),
new Point(origin.X + totalWidth, y));
y += (glyphTypeface.Height * size);
dc.DrawLine(new Pen(Brushes.Blue, 1), new Point(origin.X, y),
new Point(origin.X + totalWidth, y));
}
}
}