OpenFontSource.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using FontStashSharp.Interfaces;
  2. using MatterHackers.Agg;
  3. using MatterHackers.Agg.Font;
  4. using MatterHackers.Agg.Image;
  5. using MatterHackers.Agg.VertexSource;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Net.Http.Headers;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using Typography.OpenFont;
  12. namespace Veldrid.FontStashSharp.Typography
  13. {
  14. internal class OpenFontSource : IFontSource
  15. {
  16. private TypeFace _faceType;
  17. private TypeFacePrinter _facePrinter;
  18. public OpenFontSource(Typeface typeface)
  19. {
  20. _faceType = new TypeFace();
  21. _faceType.LoadFormTypeFace(typeface);
  22. _facePrinter = new TypeFacePrinter("",new StyledTypeFace(_faceType,12),new MatterHackers.VectorMath.Vector2(0,1), Justification.Left, Baseline.Text);
  23. }
  24. public string FontName => _faceType.Typeface.Name;
  25. public string FontStyle => _faceType.Typeface.FontSubFamily;
  26. public void Dispose()
  27. {
  28. _facePrinter = null;
  29. }
  30. public int? GetGlyphId(int codepoint)
  31. {
  32. return _faceType.Typeface.GetGlyphIndex(codepoint);
  33. }
  34. public int GetGlyphKernAdvance(int previousGlyphId, int glyphId, float fontSize)
  35. {
  36. return 0;
  37. try
  38. {
  39. _facePrinter.TypeFaceStyle.FontSize = fontSize;
  40. return (int)_facePrinter.TypeFaceStyle.GetAdvanceForCharacter((char)previousGlyphId, (char)glyphId);
  41. }catch
  42. {
  43. return 0;
  44. }
  45. }
  46. public void GetGlyphMetrics(int glyphId, float fontSize, out double advance, out double x0, out double y0, out double x1, out double y1)
  47. {
  48. _facePrinter.Text = new string((char)glyphId, 1);
  49. _facePrinter.TypeFaceStyle.FontSize = fontSize;
  50. advance = _facePrinter.TypeFaceStyle.GetAdvanceForCharacter((char)glyphId);
  51. x0 = _facePrinter.LocalBounds.Left;
  52. x1 = _facePrinter.LocalBounds.Right;
  53. y1 = _facePrinter.TypeFaceStyle.BoundingBoxInPixels.Height;
  54. y0 = 0;
  55. }
  56. public void GetMetricsForSize(float fontSize, out double ascent, out double descent, out double lineHeight)
  57. {
  58. _facePrinter.TypeFaceStyle.FontSize = fontSize;
  59. ascent = (_facePrinter.TypeFaceStyle.AscentInPixels);
  60. descent = _facePrinter.TypeFaceStyle.DescentInPixels;
  61. lineHeight = _facePrinter.TypeFaceStyle.BoundingBoxInPixels.Height;
  62. }
  63. public void RasterizeGlyphBitmap(int glyphId, float fontSize, byte[] buffer, int startIndex, int outWidth, int outHeight, int outStride)
  64. {
  65. _facePrinter.Text = new string((char)glyphId, 1);
  66. _facePrinter.TypeFaceStyle.FontSize = fontSize;
  67. _facePrinter.DrawFromHintedCache = false;
  68. var size = _facePrinter.GetSize();
  69. _facePrinter.Origin = new MatterHackers.VectorMath.Vector2(0, outHeight - size.Y);
  70. using (ImageBuffer imageBuffer = new ImageBuffer(outWidth, outHeight,8,new blenderGrayFromRed(1)))
  71. {
  72. var g = imageBuffer.NewGraphics2D();
  73. g.DrawString(_facePrinter, color: Color.Red);
  74. imageBuffer.FlipY();
  75. var tempbuffer = imageBuffer.GetBuffer();
  76. Unsafe.CopyBlock(ref buffer[startIndex], ref tempbuffer[0], (uint)(outWidth * outHeight));
  77. }
  78. }
  79. }
  80. }