widthRatio = heightRatio;
}
- Matrix savedMat = gr.Matrix;
+ gr.Save ();
gr.Translate (rect.Left,rect.Top);
gr.Scale (widthRatio, heightRatio);
gr.SetSourceSurface (imgSurface, 0,0);
gr.Paint ();
- gr.Matrix = savedMat;
+ gr.Restore ();
}
}
}
/// </summary>
public class SvgPicture : Picture
{
- Rsvg.Handle hSVG;
+ IntPtr nsvgImage;
#region CTOR
/// <summary>
#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){
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
/// <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;
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 ();
}
}
}
}
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 {
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
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 ()
{
#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)]
[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
}
}
}
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)