SvgFontDefn.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. #pragma warning disable
  8. namespace Svg
  9. {
  10. public class SvgFontDefn : IFontDefn
  11. {
  12. private SvgFont _font;
  13. private float _emScale;
  14. private float _ppi;
  15. private float _size;
  16. private Dictionary<string, SvgGlyph> _glyphs;
  17. private Dictionary<string, SvgKern> _kerning;
  18. public float Size
  19. {
  20. get { return _size; }
  21. }
  22. public float SizeInPoints
  23. {
  24. get { return _size * 72.0f / _ppi; }
  25. }
  26. public SvgFontDefn(SvgFont font, float size, float ppi)
  27. {
  28. _font = font;
  29. _size = size;
  30. _ppi = ppi;
  31. var face = _font.Children.OfType<SvgFontFace>().First();
  32. _emScale = _size / face.UnitsPerEm;
  33. }
  34. public float Ascent(ISvgRenderer renderer)
  35. {
  36. float ascent = _font.Descendants().OfType<SvgFontFace>().First().Ascent;
  37. float baselineOffset = this.SizeInPoints * (_emScale / _size) * ascent;
  38. return renderer.DpiY / 72f * baselineOffset;
  39. }
  40. public IList<System.Drawing.RectangleF> MeasureCharacters(ISvgRenderer renderer, string text)
  41. {
  42. var result = new List<RectangleF>();
  43. using (var path = GetPath(renderer, text, result, false)) { }
  44. return result;
  45. }
  46. public System.Drawing.SizeF MeasureString(ISvgRenderer renderer, string text)
  47. {
  48. var result = new List<RectangleF>();
  49. using (var path = GetPath(renderer, text, result, true)) { }
  50. var nonEmpty = result.Where(r => r != RectangleF.Empty);
  51. if (!nonEmpty.Any()) return SizeF.Empty;
  52. return new SizeF(nonEmpty.Last().Right - nonEmpty.First().Left, Ascent(renderer));
  53. }
  54. public void AddStringToPath(ISvgRenderer renderer, GraphicsPath path, string text, PointF location)
  55. {
  56. var textPath = GetPath(renderer, text, null, false);
  57. if (textPath.PointCount > 0)
  58. {
  59. using (var translate = new Matrix())
  60. {
  61. translate.Translate(location.X, location.Y);
  62. textPath.Transform(translate);
  63. path.AddPath(textPath, false);
  64. }
  65. }
  66. }
  67. private GraphicsPath GetPath(ISvgRenderer renderer, string text, IList<RectangleF> ranges, bool measureSpaces)
  68. {
  69. EnsureDictionaries();
  70. RectangleF bounds;
  71. SvgGlyph glyph;
  72. SvgKern kern;
  73. GraphicsPath path;
  74. SvgGlyph prevGlyph = null;
  75. Matrix scaleMatrix;
  76. float xPos = 0;
  77. var ascent = Ascent(renderer);
  78. var result = new GraphicsPath();
  79. if (string.IsNullOrEmpty(text)) return result;
  80. for (int i = 0; i < text.Length; i++)
  81. {
  82. if (!_glyphs.TryGetValue(text.Substring(i, 1), out glyph)) glyph = _font.Descendants().OfType<SvgMissingGlyph>().First();
  83. if (prevGlyph != null && _kerning.TryGetValue(prevGlyph.GlyphName + "|" + glyph.GlyphName, out kern))
  84. {
  85. xPos -= kern.Kerning * _emScale;
  86. }
  87. path = (GraphicsPath)glyph.Path(renderer).Clone();
  88. scaleMatrix = new Matrix();
  89. scaleMatrix.Scale(_emScale, -1 * _emScale, MatrixOrder.Append);
  90. scaleMatrix.Translate(xPos, ascent, MatrixOrder.Append);
  91. path.Transform(scaleMatrix);
  92. scaleMatrix.Dispose();
  93. bounds = path.GetBounds();
  94. if (ranges != null)
  95. {
  96. if (measureSpaces && bounds == RectangleF.Empty)
  97. {
  98. ranges.Add(new RectangleF(xPos, 0, glyph.HorizAdvX * _emScale, ascent));
  99. }
  100. else
  101. {
  102. ranges.Add(bounds);
  103. }
  104. }
  105. if (path.PointCount > 0) result.AddPath(path, false);
  106. xPos += glyph.HorizAdvX * _emScale;
  107. prevGlyph = glyph;
  108. }
  109. return result;
  110. }
  111. private void EnsureDictionaries()
  112. {
  113. if (_glyphs == null) _glyphs = _font.Descendants().OfType<SvgGlyph>().ToDictionary(g => g.Unicode ?? g.GlyphName ?? g.ID);
  114. if (_kerning == null) _kerning = _font.Descendants().OfType<SvgKern>().ToDictionary(k => k.Glyph1 + "|" + k.Glyph2);
  115. }
  116. public void Dispose()
  117. {
  118. _glyphs = null;
  119. _kerning = null;
  120. }
  121. }
  122. }
  123. #pragma warning restore