Browse Source

Updated to Skia m122

Marcin Ziąbek 1 year ago
parent
commit
e344e4643c

+ 1 - 0
Source/QuestPDF/QuestPDF.csproj

@@ -48,6 +48,7 @@
         
         
         <None Include="Runtimes\**\*.*">
         <None Include="Runtimes\**\*.*">
             <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
             <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+            <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
             <Pack>true</Pack>
             <Pack>true</Pack>
             <PackagePath>runtimes\%(RecursiveDir)%(Filename)%(Extension)</PackagePath>
             <PackagePath>runtimes\%(RecursiveDir)%(Filename)%(Extension)</PackagePath>
         </None>
         </None>

+ 4 - 9
Source/QuestPDF/Skia/SkImage.cs

@@ -9,7 +9,6 @@ internal sealed class SkImage : IDisposable
 
 
     public readonly int Width;
     public readonly int Width;
     public readonly int Height;
     public readonly int Height;
-    
     public readonly int EncodedDataSize;
     public readonly int EncodedDataSize;
     
     
     public SkImage(IntPtr instance)
     public SkImage(IntPtr instance)
@@ -21,7 +20,6 @@ internal sealed class SkImage : IDisposable
         
         
         Width = details.Width;
         Width = details.Width;
         Height = details.Height;
         Height = details.Height;
-        
         EncodedDataSize = details.EncodedDataSize;
         EncodedDataSize = details.EncodedDataSize;
     }
     }
 
 
@@ -80,8 +78,7 @@ internal sealed class SkImage : IDisposable
         
         
         [DllImport(SkiaAPI.LibraryName)]
         [DllImport(SkiaAPI.LibraryName)]
         public static extern void image_delete(IntPtr image);
         public static extern void image_delete(IntPtr image);
-
- 
+        
         [DllImport(SkiaAPI.LibraryName)]
         [DllImport(SkiaAPI.LibraryName)]
         public static extern IntPtr image_resize_and_compress(IntPtr image, int targetImageWidth, int targetImageHeight, int compressionQuality);
         public static extern IntPtr image_resize_and_compress(IntPtr image, int targetImageWidth, int targetImageHeight, int compressionQuality);
 
 
@@ -90,14 +87,12 @@ internal sealed class SkImage : IDisposable
         {
         {
             public int Width;
             public int Width;
             public int Height;
             public int Height;
-        
-            [MarshalAs(UnmanagedType.I1)] public bool IsOpaque;
             public int EncodedDataSize;
             public int EncodedDataSize;
         }
         }
-    
-        [DllImport(SkiaAPI.LibraryName, CallingConvention = CallingConvention.Winapi)]
+
+        [DllImport(SkiaAPI.LibraryName)]
         public static extern SkImageDetails image_get_details(IntPtr image);
         public static extern SkImageDetails image_get_details(IntPtr image);
-    
+
         [DllImport(SkiaAPI.LibraryName)]
         [DllImport(SkiaAPI.LibraryName)]
         public static extern IntPtr image_get_encoded_data(IntPtr image);
         public static extern IntPtr image_get_encoded_data(IntPtr image);
  
  

+ 0 - 8
Source/QuestPDF/Skia/SkImageFormat.cs

@@ -1,8 +0,0 @@
-namespace QuestPDF.Skia;
-
-internal enum SkImageFormat
-{
-    Jpeg,
-    Png,
-    Webp
-}

+ 1 - 1
Source/QuestPDF/Skia/SkNativeDependencyCompatibilityChecker.cs

@@ -3,7 +3,7 @@ using System.Runtime.InteropServices;
 
 
 namespace QuestPDF.Skia;
 namespace QuestPDF.Skia;
 
 
-public class SkNativeDependencyCompatibilityChecker
+public static class SkNativeDependencyCompatibilityChecker
 {
 {
     private static bool IsCompatibilityChecked = false;
     private static bool IsCompatibilityChecked = false;
         
         

+ 1 - 6
Source/QuestPDF/Skia/SkSize.cs

@@ -3,15 +3,10 @@ using System.Runtime.InteropServices;
 namespace QuestPDF.Skia;
 namespace QuestPDF.Skia;
 
 
 [StructLayout(LayoutKind.Sequential)]
 [StructLayout(LayoutKind.Sequential)]
-internal class SkSize
+internal struct SkSize
 {
 {
     public float Width;
     public float Width;
     public float Height;
     public float Height;
-
-    public SkSize()
-    {
-        
-    }
     
     
     public SkSize(float width, float height)
     public SkSize(float width, float height)
     {
     {

+ 6 - 0
Source/QuestPDF/Skia/Text/SkFontCollection.cs

@@ -15,6 +15,7 @@ internal sealed class SkFontCollection : IDisposable
     [StructLayout(LayoutKind.Sequential)]
     [StructLayout(LayoutKind.Sequential)]
     private struct CreateCommand
     private struct CreateCommand
     {
     {
+        public IntPtr FontManager;
         public IntPtr TypefaceProvider;
         public IntPtr TypefaceProvider;
         [MarshalAs(UnmanagedType.I1)] public bool UseGlobalFonts;
         [MarshalAs(UnmanagedType.I1)] public bool UseGlobalFonts;
         [MarshalAs(UnmanagedType.I1)] public bool EnableFontFallback;
         [MarshalAs(UnmanagedType.I1)] public bool EnableFontFallback;
@@ -23,9 +24,14 @@ internal sealed class SkFontCollection : IDisposable
     public static SkFontCollection Create(SkTypefaceProvider? typefaceProvider = null, bool useGlobalFonts = false, bool enableFontFallback = false)
     public static SkFontCollection Create(SkTypefaceProvider? typefaceProvider = null, bool useGlobalFonts = false, bool enableFontFallback = false)
     {
     {
         typefaceProvider ??= new SkTypefaceProvider();
         typefaceProvider ??= new SkTypefaceProvider();
+
+        var fontManager = useGlobalFonts
+            ? SkFontManager.Global
+            : SkFontManager.Empty;
         
         
         var command = new CreateCommand
         var command = new CreateCommand
         {
         {
+            FontManager = fontManager.Instance,
             TypefaceProvider = typefaceProvider.Instance,
             TypefaceProvider = typefaceProvider.Instance,
             UseGlobalFonts = useGlobalFonts,
             UseGlobalFonts = useGlobalFonts,
             EnableFontFallback = enableFontFallback
             EnableFontFallback = enableFontFallback

+ 35 - 0
Source/QuestPDF/Skia/Text/SkFontManager.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace QuestPDF.Skia.Text;
+
+internal sealed class SkFontManager
+{
+    public IntPtr Instance { get; }
+    
+    public static SkFontManager Empty { get; } = new(API.font_manager_get_empty());
+    public static SkFontManager Global { get; } = new(API.font_manager_create_default());
+
+    private SkFontManager(IntPtr instance)
+    {
+        Instance = instance;
+    }
+    
+    public SkTypeface CreateTypeface(SkData data)
+    {
+        var instance = API.font_manager_create_typeface(Instance, data.Instance);
+        return new SkTypeface(instance, disposeNativeObject: false);
+    }
+    
+    private static class API
+    {
+        [DllImport(SkiaAPI.LibraryName)]
+        public static extern IntPtr font_manager_get_empty();
+        
+        [DllImport(SkiaAPI.LibraryName)]
+        public static extern IntPtr font_manager_create_default();
+        
+        [DllImport(SkiaAPI.LibraryName)]
+        public static extern IntPtr font_manager_create_typeface(IntPtr fontManager, IntPtr fontData);
+    }
+}

+ 38 - 0
Source/QuestPDF/Skia/Text/SkTypeface.cs

@@ -0,0 +1,38 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace QuestPDF.Skia.Text;
+
+public sealed class SkTypeface : IDisposable
+{
+    public IntPtr Instance { get; private set; }
+    private bool DisposeNativeObject = true;
+    
+    public SkTypeface(IntPtr instance, bool disposeNativeObject)
+    {
+        Instance = instance;
+        DisposeNativeObject = disposeNativeObject;
+    }
+    
+    ~SkTypeface()
+    {
+        Dispose();
+    }
+    
+    public void Dispose()
+    {
+        if (Instance == IntPtr.Zero)
+            return;
+        
+        if (DisposeNativeObject)
+            API.typeface_delete(Instance);
+        
+        Instance = IntPtr.Zero;
+    }
+    
+    private static class API
+    {
+        [DllImport(SkiaAPI.LibraryName)]
+        public static extern void typeface_delete(IntPtr typeface);
+    }
+}

+ 6 - 4
Source/QuestPDF/Skia/Text/SkTypefaceProvider.cs

@@ -14,10 +14,12 @@ internal sealed class SkTypefaceProvider : IDisposable
     
     
     public void AddTypefaceFromData(SkData data, string? alias = null)
     public void AddTypefaceFromData(SkData data, string? alias = null)
     {
     {
+        var typeface = SkFontManager.Global.CreateTypeface(data);
+        
         if (alias == null)
         if (alias == null)
-            API.typeface_font_provider_add_typeface(Instance, data.Instance);
+            API.typeface_font_provider_add_typeface(Instance, typeface.Instance);
         else
         else
-            API.typeface_font_provider_add_typeface_with_custom_alias(Instance, data.Instance, alias);
+            API.typeface_font_provider_add_typeface_with_custom_alias(Instance, typeface.Instance, alias);
     }
     }
     
     
     ~SkTypefaceProvider()
     ~SkTypefaceProvider()
@@ -40,10 +42,10 @@ internal sealed class SkTypefaceProvider : IDisposable
         public static extern IntPtr typeface_font_provider_create();
         public static extern IntPtr typeface_font_provider_create();
         
         
         [DllImport(SkiaAPI.LibraryName)]
         [DllImport(SkiaAPI.LibraryName)]
-        public static extern void typeface_font_provider_add_typeface(IntPtr typefaceProvider, IntPtr data);
+        public static extern void typeface_font_provider_add_typeface(IntPtr typefaceProvider, IntPtr typeface);
         
         
         [DllImport(SkiaAPI.LibraryName)]
         [DllImport(SkiaAPI.LibraryName)]
-        public static extern void typeface_font_provider_add_typeface_with_custom_alias(IntPtr typefaceProvider, IntPtr data, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8StringMarshaller))] string alias);
+        public static extern void typeface_font_provider_add_typeface_with_custom_alias(IntPtr typefaceProvider, IntPtr typeface, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8StringMarshaller))] string alias);
         
         
         [DllImport(SkiaAPI.LibraryName)]
         [DllImport(SkiaAPI.LibraryName)]
         public static extern void typeface_font_provider_delete(IntPtr typefaceProvider);
         public static extern void typeface_font_provider_delete(IntPtr typefaceProvider);