aboutsummaryrefslogtreecommitdiffstatsci
path: root/src
diff options
context:
space:
mode:
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>2018-02-03 06:12:57 +0100
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>2018-02-03 06:19:51 +0100
commit726fe4f0acdee5bce7371c8db6b835668d95df17 (patch)
tree445dfca91f3adac0aa549d4b8a6fdfc6676e6006 /src
parentd5c135d4a8ae66f5c0160c089e9e853a0929e839 (diff)
share Picture resource data among widgets
Diffstat (limited to 'src')
-rw-r--r--src/BmpPicture.cs44
-rw-r--r--src/Picture.cs75
-rw-r--r--src/SvgPicture.cs42
3 files changed, 138 insertions, 23 deletions
diff --git a/src/BmpPicture.cs b/src/BmpPicture.cs
index ea83095c..79302354 100644
--- a/src/BmpPicture.cs
+++ b/src/BmpPicture.cs
@@ -30,20 +30,47 @@ using Cairo;
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
@@ -103,6 +130,13 @@ namespace Crow
}
#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;
diff --git a/src/Picture.cs b/src/Picture.cs
index 0e5212ab..fe7007c7 100644
--- a/src/Picture.cs
+++ b/src/Picture.cs
@@ -27,38 +27,88 @@
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))
@@ -71,7 +121,7 @@ namespace Crow
else
_pic = new BmpPicture ();
- _pic.LoadImage (path);
+ _pic.Load (path);
return _pic;
}
@@ -79,6 +129,7 @@ namespace Crow
{
return _pic == null ? null : _pic.Path;
}
+ #endregion
public static object Parse(string path)
{
@@ -92,7 +143,7 @@ namespace Crow
else
_pic = new BmpPicture ();
- _pic.LoadImage (path);
+ _pic.Load (path);
return _pic;
}
diff --git a/src/SvgPicture.cs b/src/SvgPicture.cs
index b398d523..b4026852 100644
--- a/src/SvgPicture.cs
+++ b/src/SvgPicture.cs
@@ -30,22 +30,45 @@ using Cairo;
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
@@ -79,7 +102,14 @@ namespace Crow
}
}
#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;