]> O.S.I.I.S - jp/crow.git/commitdiff
nsvg
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 1 May 2019 21:56:58 +0000 (23:56 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 1 May 2019 21:56:58 +0000 (23:56 +0200)
Crow/src/BmpPicture.cs
Crow/src/SvgPicture.cs
Crow/src/vkvg/Context.cs
Crow/src/vkvg/Device.cs
Crow/src/vkvg/NativeMethods.cs
Crow/src/vkvg/Surface.cs

index 349d7d6490b44c225186cb7ea7e62cd22c730ca4..51bf89697b423f01f7f0638aef31ab96e2653b96 100644 (file)
@@ -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 ();
                }
        }
 }
index 96d42ac8b9479831070d5269f8f79f795dbcd22f..b4b6bbda7f3ba327ad881dd953a75469a8f6ca41 100644 (file)
@@ -35,7 +35,7 @@ namespace Crow
        /// </summary>
        public class SvgPicture : Picture
        {
-               Rsvg.Handle hSVG;
+               IntPtr nsvgImage;
 
                #region CTOR
                /// <summary>
@@ -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
                /// <param name="subPart">limit rendering to svg part named 'subPart'</param>
                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 ();
                }
        }
 }
index 0f709c68b90f0b977c50f94fece18c5a73f780d4..b3cea1528ba82c13f0a3f898c02fce1ae5eaf8f6 100644 (file)
@@ -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
index 1a1b4b56aa7deb1208ff0b8fc96d3f96ff4fc1f4..8d3c7f55ccd78996216922b230aa214269245e4d 100644 (file)
@@ -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 ()
                {
index 7b6dfdf0abce4796bffbfe96dbf539a8e5348b47..dd1757e9c376fc90b864a12178964f802180f29c 100644 (file)
@@ -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
        }
 }
 
index c1c25594ca256d02ed1b55459830758a70006ab7..f88b19baaed76b93a1123ee9fc0bc0d2f30dcf9f 100644 (file)
@@ -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)