2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2014-01-10 20:11:39 +01:00
|
|
|
|
// Copyright © 2014 Clemens Fischer
|
2012-05-04 12:52:20 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Contains helper methods for creating GlyphRun objects.
|
|
|
|
|
|
/// </summary>
|
2012-04-25 22:02:53 +02:00
|
|
|
|
public static class GlyphRunText
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
public static GlyphRun Create(string text, Typeface typeface, double emSize, Point baselineOrigin = new Point())
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
GlyphTypeface glyphTypeface;
|
|
|
|
|
|
|
|
|
|
|
|
if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException(string.Format("{0}: no GlyphTypeface found", typeface.FontFamily));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var glyphIndices = new ushort[text.Length];
|
|
|
|
|
|
var advanceWidths = new double[text.Length];
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < text.Length; i++)
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[i]];
|
2012-04-25 22:02:53 +02:00
|
|
|
|
glyphIndices[i] = glyphIndex;
|
|
|
|
|
|
advanceWidths[i] = glyphTypeface.AdvanceWidths[glyphIndex] * emSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
return new GlyphRun(glyphTypeface, 0, false, emSize, glyphIndices, baselineOrigin, advanceWidths, null, null, null, null, null, null);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
public static void DrawGlyphRun(this DrawingContext drawingContext, Brush foreground, GlyphRun glyphRun,
|
|
|
|
|
|
Point position, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var bbox = glyphRun.ComputeInkBoundingBox();
|
|
|
|
|
|
var transform = new TranslateTransform(position.X - bbox.X, position.Y - bbox.Y);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
switch (horizontalAlignment)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
case HorizontalAlignment.Center:
|
|
|
|
|
|
transform.X -= bbox.Width / 2d;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case HorizontalAlignment.Right:
|
|
|
|
|
|
transform.X -= bbox.Width;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
switch (verticalAlignment)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
case VerticalAlignment.Center:
|
|
|
|
|
|
transform.Y -= bbox.Height / 2d;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case VerticalAlignment.Bottom:
|
|
|
|
|
|
transform.Y -= bbox.Height;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
drawingContext.PushTransform(transform);
|
|
|
|
|
|
drawingContext.DrawGlyphRun(foreground, glyphRun);
|
|
|
|
|
|
drawingContext.Pop();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|