using System using System Collections Generic using System Linq using

  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
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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));
}
}
}