MockTextShaperImpl.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Avalonia.Media;
  2. using Avalonia.Media.TextFormatting;
  3. using Avalonia.Media.TextFormatting.Unicode;
  4. using Avalonia.Platform;
  5. using Avalonia.Utility;
  6. namespace Avalonia.UnitTests
  7. {
  8. public class MockTextShaperImpl : ITextShaperImpl
  9. {
  10. public GlyphRun ShapeText(ReadOnlySlice<char> text, TextFormat textFormat)
  11. {
  12. var glyphTypeface = textFormat.Typeface.GlyphTypeface;
  13. var glyphIndices = new ushort[text.Length];
  14. var height = textFormat.FontMetrics.LineHeight;
  15. var width = 0.0;
  16. for (var i = 0; i < text.Length;)
  17. {
  18. var index = i;
  19. var codepoint = Codepoint.ReadAt(text, i, out var count);
  20. i += count;
  21. var glyph = glyphTypeface.GetGlyph(codepoint);
  22. glyphIndices[index] = glyph;
  23. width += glyphTypeface.GetGlyphAdvance(glyph);
  24. }
  25. return new GlyphRun(glyphTypeface, textFormat.FontRenderingEmSize, glyphIndices, characters: text,
  26. bounds: new Rect(0, 0, width, height));
  27. }
  28. }
  29. }