MockTextShaperImpl.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Globalization;
  3. using Avalonia.Media;
  4. using Avalonia.Media.TextFormatting.Unicode;
  5. using Avalonia.Platform;
  6. using Avalonia.Utilities;
  7. namespace Avalonia.UnitTests
  8. {
  9. public class MockTextShaperImpl : ITextShaperImpl
  10. {
  11. public GlyphRun ShapeText(ReadOnlySlice<char> text, Typeface typeface, double fontRenderingEmSize, CultureInfo culture)
  12. {
  13. var glyphTypeface = typeface.GlyphTypeface;
  14. var glyphIndices = new ushort[text.Length];
  15. var glyphCount = 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. glyphCount++;
  24. }
  25. return new GlyphRun(glyphTypeface, fontRenderingEmSize,
  26. new ReadOnlySlice<ushort>(glyphIndices.AsMemory(0, glyphCount)), characters: text);
  27. }
  28. }
  29. }