MockTextShaperImpl.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using Avalonia.Media.TextFormatting;
  3. using Avalonia.Media.TextFormatting.Unicode;
  4. using Avalonia.Platform;
  5. namespace Avalonia.UnitTests
  6. {
  7. public class MockTextShaperImpl : ITextShaperImpl
  8. {
  9. public ShapedBuffer ShapeText(ReadOnlyMemory<char> text, TextShaperOptions options)
  10. {
  11. var typeface = options.Typeface;
  12. var fontRenderingEmSize = options.FontRenderingEmSize;
  13. var bidiLevel = options.BidiLevel;
  14. var shapedBuffer = new ShapedBuffer(text, text.Length, typeface, fontRenderingEmSize, bidiLevel);
  15. var textSpan = text.Span;
  16. var textStartIndex = TextTestHelper.GetStartCharIndex(text);
  17. for (var i = 0; i < shapedBuffer.Length;)
  18. {
  19. var glyphCluster = i + textStartIndex;
  20. var codepoint = Codepoint.ReadAt(textSpan, i, out var count);
  21. var glyphIndex = typeface.GetGlyph(codepoint);
  22. for (var j = 0; j < count; ++j)
  23. {
  24. shapedBuffer[i + j] = new GlyphInfo(glyphIndex, glyphCluster, 10);
  25. }
  26. i += count;
  27. }
  28. return shapedBuffer;
  29. }
  30. }
  31. }