From 2d48694d71355a142b78c2d99c07a794d461008f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Wed, 1 May 2019 23:56:58 +0200 Subject: [PATCH] nsvg --- Crow/src/BmpPicture.cs | 4 +- Crow/src/SvgPicture.cs | 75 +++++++++++++++------------------- Crow/src/vkvg/Context.cs | 9 ++-- Crow/src/vkvg/Device.cs | 10 +++++ Crow/src/vkvg/NativeMethods.cs | 21 +++++++++- Crow/src/vkvg/Surface.cs | 5 ++- 6 files changed, 75 insertions(+), 49 deletions(-) diff --git a/Crow/src/BmpPicture.cs b/Crow/src/BmpPicture.cs index 349d7d64..51bf8969 100644 --- a/Crow/src/BmpPicture.cs +++ b/Crow/src/BmpPicture.cs @@ -131,7 +131,7 @@ namespace Crow widthRatio = heightRatio; } - Matrix savedMat = gr.Matrix; + gr.Save (); gr.Translate (rect.Left,rect.Top); gr.Scale (widthRatio, heightRatio); @@ -139,7 +139,7 @@ namespace Crow gr.SetSourceSurface (imgSurface, 0,0); gr.Paint (); - gr.Matrix = savedMat; + gr.Restore (); } } } diff --git a/Crow/src/SvgPicture.cs b/Crow/src/SvgPicture.cs index 96d42ac8..b4b6bbda 100644 --- a/Crow/src/SvgPicture.cs +++ b/Crow/src/SvgPicture.cs @@ -35,7 +35,7 @@ namespace Crow /// public class SvgPicture : Picture { - Rsvg.Handle hSVG; + IntPtr nsvgImage; #region CTOR /// @@ -54,34 +54,30 @@ namespace Crow #endregion void Load () - { - if (sharedResources.ContainsKey (Path)) { - sharedPicture sp = sharedResources [Path]; - hSVG = (Rsvg.Handle)sp.Data; - Dimensions = sp.Dims; - return; - } - using (Stream stream = Interface.StaticGetStreamFromPath (Path)) { - using (MemoryStream ms = new MemoryStream ()) { - stream.CopyTo (ms); - - hSVG = new Rsvg.Handle (ms.ToArray ()); - Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height); - } + { + using (StreamReader sr = new StreamReader(Interface.CurrentInterface.GetStreamFromPath (Path))) { + nsvgImage = Interface.CurrentInterface.dev.LoadSvgFragment (sr.ReadToEnd ()); } - sharedResources [Path] = new sharedPicture (hSVG, Dimensions); + int w, h; + NativeMethods.nsvg_get_size (nsvgImage, out w, out h); + Dimensions.Width = w; + Dimensions.Height = h; } - public void LoadSvgFragment (string fragment) { - hSVG = new Rsvg.Handle (System.Text.Encoding.Unicode.GetBytes(fragment)); - Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height); + public void LoadSvgFragment (string fragment) { + throw new NotImplementedException (); + /*hSVG = new Rsvg.Handle (System.Text.Encoding.Unicode.GetBytes(fragment)); + Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);*/ } #region implemented abstract members of Fill public override void SetAsSource (Context ctx, Rectangle bounds = default(Rectangle)) { - float widthRatio = 1f; + if (nsvgImage == IntPtr.Zero) + return; + throw new NotImplementedException (); + /*float widthRatio = 1f; float heightRatio = 1f; if (Scaled){ @@ -96,16 +92,16 @@ namespace Crow widthRatio = heightRatio; } - //using (ImageSurface tmp = new ImageSurface (Format.Argb32, bounds.Width, bounds.Height)) { - // using (Context gr = new Context (tmp)) { - // gr.Translate (bounds.Left, bounds.Top); - // gr.Scale (widthRatio, heightRatio); - // gr.Translate ((bounds.Width/widthRatio - Dimensions.Width)/2, (bounds.Height/heightRatio - Dimensions.Height)/2); - - // hSVG.RenderCairo (gr); - // } - // ctx.SetSource (tmp); - //} + using (Surface tmp = new Surface (Interface.CurrentInterface.dev, bounds.Width, bounds.Height)) { + using (Context gr = new Context (tmp)) { + gr.Translate (bounds.Left, bounds.Top); + gr.Scale (widthRatio, heightRatio); + gr.Translate ((bounds.Width / widthRatio - Dimensions.Width) / 2, (bounds.Height / heightRatio - Dimensions.Height) / 2); + gr.SetSourceSurface (svgSurface, 0, 0); + gr.Paint (); + } + ctx.SetSource (tmp); + }*/ } #endregion @@ -118,8 +114,9 @@ namespace Crow /// limit rendering to svg part named 'subPart' public override void Paint (Context gr, Rectangle rect, string subPart = "") { - if (hSVG == null) + if (nsvgImage == IntPtr.Zero) return; + float widthRatio = 1f; float heightRatio = 1f; @@ -133,19 +130,15 @@ namespace Crow else widthRatio = heightRatio; } - + gr.Save (); - gr.Translate (rect.Left,rect.Top); + gr.Translate (rect.Left, rect.Top); gr.Scale (widthRatio, heightRatio); - gr.Translate (((float)rect.Width/widthRatio - Dimensions.Width)/2f, ((float)rect.Height/heightRatio - Dimensions.Height)/2f); - - /*if (string.IsNullOrEmpty (subPart)) - hSVG.RenderCairo (gr); - else - hSVG.RenderCairoSub (gr, "#" + subPart);*/ - - gr.Restore (); + gr.Translate ((rect.Width / widthRatio - Dimensions.Width) / 2, (rect.Height / heightRatio - Dimensions.Height) / 2); + gr.RenderSvg (nsvgImage); + + gr.Restore (); } } } diff --git a/Crow/src/vkvg/Context.cs b/Crow/src/vkvg/Context.cs index 0f709c68..b3cea152 100644 --- a/Crow/src/vkvg/Context.cs +++ b/Crow/src/vkvg/Context.cs @@ -76,8 +76,9 @@ namespace vkvg } public TextExtents TextExtents(string s) { - TextExtents extents; - NativeMethods.vkvg_text_extents (handle, TerminateUtf8(s), out extents); + TextExtents extents = default(TextExtents); + if (!string.IsNullOrEmpty(s)) + NativeMethods.vkvg_text_extents (handle, TerminateUtf8(s), out extents); return extents; } public Matrix Matrix { @@ -247,7 +248,9 @@ namespace vkvg public void SetSourceSurface (Surface surf, float x = 0f, float y = 0f) { NativeMethods.vkvg_set_source_surface (handle, surf.Handle, x, y); } - + public void RenderSvg (IntPtr nsvgImage) { + NativeMethods.vkvg_render_svg (handle, nsvgImage); + } internal static byte[] TerminateUtf8(string s) { // compute the byte count including the trailing \0 diff --git a/Crow/src/vkvg/Device.cs b/Crow/src/vkvg/Device.cs index 1a1b4b56..8d3c7f55 100644 --- a/Crow/src/vkvg/Device.cs +++ b/Crow/src/vkvg/Device.cs @@ -56,6 +56,16 @@ namespace vkvg public IntPtr Handle { get { return handle; }} + public IntPtr LoadSvg (string path) { + return NativeMethods.nsvg_load_file (handle, path); + } + public void DestroySvg (IntPtr nsvgImage) { + NativeMethods.nsvg_destroy (nsvgImage); + } + public IntPtr LoadSvgFragment (string fragment) { + return NativeMethods.nsvg_load (handle, Context.TerminateUtf8(fragment)); + } + #region IDisposable implementation public void Dispose () { diff --git a/Crow/src/vkvg/NativeMethods.cs b/Crow/src/vkvg/NativeMethods.cs index 7b6dfdf0..dd1757e9 100644 --- a/Crow/src/vkvg/NativeMethods.cs +++ b/Crow/src/vkvg/NativeMethods.cs @@ -244,8 +244,12 @@ namespace vkvg #region Surface [DllImport (libvkvg, CallingConvention=CallingConvention.Cdecl)] internal static extern IntPtr vkvg_surface_create (IntPtr device, uint width, uint height); - [DllImport (libvkvg, CallingConvention=CallingConvention.Cdecl)] - internal static extern IntPtr vkvg_surface_create_from_image (IntPtr dev, string filePath); + [DllImport (libvkvg, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr vkvg_surface_create_from_image (IntPtr dev, string filePath); + [DllImport (libvkvg, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr vkvg_surface_create_from_svg (IntPtr dev, string filePath); + [DllImport (libvkvg, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr vkvg_surface_create_from_svg_fragment (IntPtr dev, byte[] filePath); [DllImport (libvkvg, CallingConvention=CallingConvention.Cdecl)] internal static extern IntPtr vkvg_surface_create_from_bitmap (IntPtr dev, ref byte[] data, uint width, uint height); [DllImport (libvkvg, CallingConvention=CallingConvention.Cdecl)] @@ -265,6 +269,19 @@ namespace vkvg [DllImport (libvkvg, CallingConvention=CallingConvention.Cdecl)] internal static extern void vkvg_surface_write_to_png (IntPtr surf, [MarshalAs(UnmanagedType.LPStr)]string path); #endregion + + #region NSVG + [DllImport (libvkvg, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr nsvg_load_file (IntPtr dev, string filePath); + [DllImport (libvkvg, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr nsvg_load (IntPtr dev, byte[] fragment); + [DllImport (libvkvg, CallingConvention = CallingConvention.Cdecl)] + internal static extern void nsvg_destroy (IntPtr nsvgImage); + [DllImport (libvkvg, CallingConvention = CallingConvention.Cdecl)] + internal static extern void nsvg_get_size (IntPtr nsvgImage, out int width, out int height); + [DllImport (libvkvg, CallingConvention = CallingConvention.Cdecl)] + internal static extern void vkvg_render_svg (IntPtr ctx, IntPtr nsvgImage); + #endregion } } diff --git a/Crow/src/vkvg/Surface.cs b/Crow/src/vkvg/Surface.cs index c1c25594..f88b19ba 100644 --- a/Crow/src/vkvg/Surface.cs +++ b/Crow/src/vkvg/Surface.cs @@ -44,7 +44,10 @@ namespace vkvg } public Surface (Device device, string imgPath) { vkvgDev = device; - handle = NativeMethods.vkvg_surface_create_from_image (device.Handle, imgPath); + if (string.Compare(System.IO.Path.GetExtension(imgPath), ".svg", StringComparison.OrdinalIgnoreCase) == 0) + handle = NativeMethods.vkvg_surface_create_from_svg (device.Handle, imgPath); + else + handle = NativeMethods.vkvg_surface_create_from_image (device.Handle, imgPath); } Surface (IntPtr devHandle, int width, int heigth) -- 2.47.3