SkFontCollection.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace QuestPDF.Skia.Text;
  4. internal sealed class SkFontCollection : IDisposable
  5. {
  6. public IntPtr Instance { get; private set; }
  7. public SkFontCollection(IntPtr instance)
  8. {
  9. Instance = instance;
  10. SkiaAPI.EnsureNotNull(Instance);
  11. }
  12. [StructLayout(LayoutKind.Sequential)]
  13. private struct CreateCommand
  14. {
  15. public IntPtr FontManager;
  16. public IntPtr TypefaceProvider;
  17. }
  18. public static SkFontCollection Create(SkTypefaceProvider typefaceProvider, SkFontManager fontManager)
  19. {
  20. var command = new CreateCommand
  21. {
  22. FontManager = fontManager.Instance,
  23. TypefaceProvider = typefaceProvider.Instance
  24. };
  25. var instance = API.font_collection_create(command);
  26. return new SkFontCollection(instance);
  27. }
  28. ~SkFontCollection()
  29. {
  30. Dispose();
  31. }
  32. public void Dispose()
  33. {
  34. if (Instance == IntPtr.Zero)
  35. return;
  36. API.font_collection_unref(Instance);
  37. Instance = IntPtr.Zero;
  38. }
  39. private static class API
  40. {
  41. [DllImport(SkiaAPI.LibraryName)]
  42. public static extern IntPtr font_collection_create(CreateCommand command);
  43. [DllImport(SkiaAPI.LibraryName)]
  44. public static extern void font_collection_unref(IntPtr fontCollection);
  45. }
  46. }