namespace Crow
{
+
+ /// <summary>
+ /// Derived from FILL for loading and drawing bitmaps in the interface
+ /// </summary>
public class BmpPicture : Picture
{
- byte[] image;
+ byte[] image = null;
+ #region CTOR
+ /// <summary>
+ /// Initializes a new instance of BmpPicture.
+ /// </summary>
public BmpPicture ()
{}
+ /// <summary>
+ /// Initializes a new instance of BmpPicture by loading the image pointed by the path argument
+ /// </summary>
+ /// <param name="path">image path, may be embedded</param>
public BmpPicture (string path) : base(path)
{}
- protected override void loadFromStream (Stream stream)
+ #endregion
+ /// <summary>
+ /// load the image for rendering from the path given as argument
+ /// </summary>
+ /// <param name="path">image path, may be embedded</param>
+ public override void Load (string path)
{
- using (MemoryStream ms = new MemoryStream ()) {
- stream.CopyTo (ms);
- loadBitmap (new System.Drawing.Bitmap (ms));
+ Path = path;
+ if (sharedResources.ContainsKey (path)) {
+ sharedPicture sp = sharedResources [path];
+ image = (byte[])sp.Data;
+ Dimensions = sp.Dims;
+ return;
+ }
+ using (Stream stream = Interface.GetStreamFromPath (path)) {
+ using (MemoryStream ms = new MemoryStream ()) {
+ stream.CopyTo (ms);
+ loadBitmap (new System.Drawing.Bitmap (ms));
+ }
}
+ sharedResources [path] = new sharedPicture (image, Dimensions);
}
//load image via System.Drawing.Bitmap, cairo load png only
}
#endregion
+ /// <summary>
+ /// paint the image in the rectangle given in arguments according
+ /// to the Scale and keepProportion parameters.
+ /// </summary>
+ /// <param name="gr">drawing Backend context</param>
+ /// <param name="rect">bounds of the target surface to paint</param>
+ /// <param name="subPart">used for svg only</param>
public override void Paint (Cairo.Context gr, Rectangle rect, string subPart = "")
{
float widthRatio = 1f;
using System;
using System.IO;
using Cairo;
+using System.Collections.Generic;
namespace Crow
{
+ /// <summary>
+ /// store data and dimensions for resource sharing
+ /// </summary>
+ internal class sharedPicture {
+ //TODO: restructure this whith clever conceptual classes
+ public object Data;
+ public Size Dims;
+ public sharedPicture (object _data, Size _dims){
+ Data = _data;
+ Dims = _dims;
+ }
+ }
+ /// <summary>
+ /// virtual class for loading and drawing picture in the interface
+ ///
+ /// Every loaded resources are stored in a dictonary with their path as key and shared
+ /// among interface elements
+ /// </summary>
public abstract class Picture : Fill
{
+ /// <summary>
+ /// share a single store for picture resources among usage in different controls
+ /// </summary>
+ internal static Dictionary<string, sharedPicture> sharedResources = new Dictionary<string, sharedPicture>();
+
+ /// <summary>
+ /// path of the picture
+ /// </summary>
public string Path;
+ /// <summary>
+ /// unscaled dimensions fetched on loading
+ /// </summary>
public Size Dimensions;
+ /// <summary>
+ /// if true and image has to be scalled, it will be scaled in both direction
+ /// equaly
+ /// </summary>
public bool KeepProportions = false;
+ /// <summary>
+ /// allow or not the picture to be scalled on request by the painter
+ /// </summary>
public bool Scaled = true;
+ #region CTOR
+ /// <summary>
+ /// Initializes a new instance of Picture.
+ /// </summary>
public Picture ()
{
}
+ /// <summary>
+ /// Initializes a new instance of Picture by loading the image pointed by the path argument
+ /// </summary>
+ /// <param name="path">image path, may be embedded</param>
public Picture (string path)
{
- LoadImage (path);
+ Load (path);
}
+ #endregion
#region Image Loading
- public void LoadImage (string path)
- {
- loadFromStream (Interface.GetStreamFromPath (path));
-
- Path = path;
- }
-
- protected abstract void loadFromStream(Stream stream);
+ /// <summary>
+ /// load the image for rendering from the stream given as argument
+ /// </summary>
+ /// <param name="stream">picture stream</param>
+ public abstract void Load(string path);
#endregion
+ /// <summary>
+ /// abstract method to paint the image in the rectangle given in arguments according
+ /// to the Scale and keepProportion parameters.
+ /// </summary>
+ /// <param name="gr">drawing Backend context</param>
+ /// <param name="rect">bounds of the target surface to paint</param>
+ /// <param name="subPart">used for svg only</param>
public abstract void Paint(Context ctx, Rectangle rect, string subPart = "");
-
+ #region Operators
public static implicit operator Picture(string path)
{
if (string.IsNullOrEmpty (path))
else
_pic = new BmpPicture ();
- _pic.LoadImage (path);
+ _pic.Load (path);
return _pic;
}
{
return _pic == null ? null : _pic.Path;
}
+ #endregion
public static object Parse(string path)
{
else
_pic = new BmpPicture ();
- _pic.LoadImage (path);
+ _pic.Load (path);
return _pic;
}
namespace Crow
{
+ /// <summary>
+ /// Derived from FILL for loading and drawing SVG images in the interface
+ /// </summary>
public class SvgPicture : Picture
{
Rsvg.Handle hSVG;
+ #region CTOR
+ /// <summary>
+ /// Initializes a new instance of SvgPicture.
+ /// </summary>
public SvgPicture ()
{}
+ /// <summary>
+ /// Initializes a new instance of SvgPicture by loading the SVG file pointed by the path argument
+ /// </summary>
+ /// <param name="path">image path, may be embedded</param>
public SvgPicture (string path) : base(path)
{}
- protected override void loadFromStream (Stream stream)
+ #endregion
+
+ public override void Load (string path)
{
- using (MemoryStream ms = new MemoryStream ()) {
- stream.CopyTo (ms);
+ Path = path;
+ if (sharedResources.ContainsKey (path)) {
+ sharedPicture sp = sharedResources [path];
+ hSVG = (Rsvg.Handle)sp.Data;
+ Dimensions = sp.Dims;
+ return;
+ }
+ using (Stream stream = Interface.GetStreamFromPath (path)) {
+ using (MemoryStream ms = new MemoryStream ()) {
+ stream.CopyTo (ms);
- hSVG = new Rsvg.Handle (ms.ToArray ());
- Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);
+ hSVG = new Rsvg.Handle (ms.ToArray ());
+ Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);
+ }
}
+ sharedResources [path] = new sharedPicture (hSVG, Dimensions);
}
#region implemented abstract members of Fill
}
}
#endregion
-
+
+ /// <summary>
+ /// paint the image in the rectangle given in arguments according
+ /// to the Scale and keepProportion parameters.
+ /// </summary>
+ /// <param name="gr">drawing Backend context</param>
+ /// <param name="rect">bounds of the target surface to paint</param>
+ /// <param name="subPart">limit rendering to svg part named 'subPart'</param>
public override void Paint (Cairo.Context gr, Rectangle rect, string subPart = "")
{
float widthRatio = 1f;