This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGlyphRunText.cs
More file actions
72 lines (62 loc) · 2.56 KB
/
GlyphRunText.cs
File metadata and controls
72 lines (62 loc) · 2.56 KB
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
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © 2014 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Windows;
using System.Windows.Media;
namespace MapControl
{
/// <summary>
/// Contains helper methods for creating GlyphRun objects.
/// </summary>
public static class GlyphRunText
{
public static GlyphRun Create(string text, Typeface typeface, double emSize, Point baselineOrigin = new Point())
{
GlyphTypeface glyphTypeface;
if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
{
throw new ArgumentException(string.Format("{0}: no GlyphTypeface found", typeface.FontFamily));
}
var glyphIndices = new ushort[text.Length];
var advanceWidths = new double[text.Length];
for (int i = 0; i < text.Length; i++)
{
var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[i]];
glyphIndices[i] = glyphIndex;
advanceWidths[i] = glyphTypeface.AdvanceWidths[glyphIndex] * emSize;
}
return new GlyphRun(glyphTypeface, 0, false, emSize, glyphIndices, baselineOrigin, advanceWidths, null, null, null, null, null, null);
}
public static void DrawGlyphRun(this DrawingContext drawingContext, Brush foreground, GlyphRun glyphRun,
Point position, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
{
var boundingBox = glyphRun.ComputeInkBoundingBox();
switch (horizontalAlignment)
{
case HorizontalAlignment.Center:
position.X -= boundingBox.Width / 2d;
break;
case HorizontalAlignment.Right:
position.X -= boundingBox.Width;
break;
default:
break;
}
switch (verticalAlignment)
{
case VerticalAlignment.Center:
position.Y -= boundingBox.Height / 2d;
break;
case VerticalAlignment.Bottom:
position.Y -= boundingBox.Height;
break;
default:
break;
}
drawingContext.PushTransform(new TranslateTransform(position.X - boundingBox.X, position.Y - boundingBox.Y));
drawingContext.DrawGlyphRun(foreground, glyphRun);
drawingContext.Pop();
}
}
}