// // Picture.cs // // Author: // Jean-Philippe Bruyère // // Copyright (c) 2013-2017 Jean-Philippe Bruyère // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.IO; using Cairo; using System.Collections.Generic; namespace Crow { /// /// store data and dimensions for resource sharing /// 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; } } /// /// 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 /// public abstract class Picture : Fill { /// /// share a single store for picture resources among usage in different controls /// internal static Dictionary sharedResources = new Dictionary(); /// /// path of the picture /// public string Path; /// /// unscaled dimensions fetched on loading /// public Size Dimensions; /// /// if true and image has to be scalled, it will be scaled in both direction /// equaly /// public bool KeepProportions = false; /// /// allow or not the picture to be scalled on request by the painter /// public bool Scaled = true; #region CTOR /// /// Initializes a new instance of Picture. /// public Picture () { } /// /// Initializes a new instance of Picture by loading the image pointed by the path argument /// /// image path, may be embedded public Picture (string path) { Path = path; } #endregion #region Image Loading /// /// load the image for rendering from the stream given as argument /// /// picture stream public abstract void Load(Interface iface, string path); #endregion /// /// abstract method to paint the image in the rectangle given in arguments according /// to the Scale and keepProportion parameters. /// /// drawing Backend context /// bounds of the target surface to paint /// used for svg only public abstract void Paint(Context ctx, Rectangle rect, string subPart = ""); #region Operators public static implicit operator Picture(string path) { if (string.IsNullOrEmpty (path)) return null; Picture _pic = null; if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) _pic = new SvgPicture (path); else _pic = new BmpPicture (path); return _pic; } public static implicit operator string(Picture _pic) { return _pic == null ? null : _pic.Path; } #endregion public static object Parse(string path) { if (string.IsNullOrEmpty (path)) return null; Picture _pic = null; if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) _pic = new SvgPicture (path); else _pic = new BmpPicture (path); return _pic; } public override string ToString () { return Path; } } }