// "Therefore those skilled at the unorthodox // are infinite as heaven and earth, // inexhaustible as the great rivers. // When they come to an end, // they begin again, // like the days and months; // they die and are reborn, // like the four seasons." // // - Sun Tsu, // "The Art of War" using System; using System.Collections.Generic; using TheArtOfDev.HtmlRenderer.Adapters.Entities; using TheArtOfDev.HtmlRenderer.Core.Utils; namespace TheArtOfDev.HtmlRenderer.Core.Entities { /// /// Callback used in to allow setting image externally and async.
/// The callback can provide path to image file path, URL or the actual image to use.
/// If is given (not ) then only the specified rectangle will /// be used from the loaded image and not all of it, also the rectangle will be used for size and not the actual image size.
///
/// the path to the image to load (file path or URL) /// the image to use /// optional: limit to specific rectangle in the loaded image public delegate void HtmlImageLoadCallback(string path, Object image, RRect imageRectangle); /// /// Invoked when an image is about to be loaded by file path, URL or inline data in 'img' element or background-image CSS style.
/// Allows to overwrite the loaded image by providing the image object manually, or different source (file or URL) to load from.
/// Example: image 'src' can be non-valid string that is interpreted in the overwrite delegate by custom logic to resource image object
/// Example: image 'src' in the html is relative - the overwrite intercepts the load and provide full source URL to load the image from
/// Example: image download requires authentication - the overwrite intercepts the load, downloads the image to disk using custom code and /// provide file path to load the image from. Can also use the asynchronous image overwrite not to block HTML rendering is applicable.
/// If no alternative data is provided the original source will be used.
///
public sealed class HtmlImageLoadEventArgs : EventArgs { #region Fields and Consts /// /// use to cancel the image loading by html renderer, the provided image will be used. /// private bool _handled; /// /// the source of the image (file path or uri) /// private readonly string _src; /// /// collection of all the attributes that are defined on the image element /// private readonly Dictionary _attributes; /// /// Callback used to allow setting image externally and async. /// private readonly HtmlImageLoadCallback _callback; #endregion /// /// Init. /// /// the source of the image (file path or Uri) /// collection of all the attributes that are defined on the image element /// Callback used to allow setting image externally and async. internal HtmlImageLoadEventArgs(string src, Dictionary attributes, HtmlImageLoadCallback callback) { _src = src; _attributes = attributes; _callback = callback; } /// /// the source of the image (file path, URL or inline data) /// public string Src { get { return _src; } } /// /// collection of all the attributes that are defined on the image element or CSS style /// public Dictionary Attributes { get { return _attributes; } } /// /// Indicate the image load is handled asynchronously. /// Cancel this image loading and overwrite the image asynchronously using callback method.
///
public bool Handled { get { return _handled; } set { _handled = value; } } /// /// Callback to overwrite the loaded image with error image.
/// Can be called directly from delegate handler or asynchronously after setting to True.
///
public void Callback() { _handled = true; _callback(null, null, new RRect()); } /// /// Callback to overwrite the loaded image with image to load from given URI.
/// Can be called directly from delegate handler or asynchronously after setting to True.
///
/// the path to the image to load (file path or URL) public void Callback(string path) { ArgChecker.AssertArgNotNullOrEmpty(path, "path"); _handled = true; _callback(path, null, RRect.Empty); } /// /// Callback to overwrite the loaded image with image to load from given URI.
/// Can be called directly from delegate handler or asynchronously after setting to True.
/// Only the specified rectangle (x,y,width,height) will be used from the loaded image and not all of it, also /// the rectangle will be used for size and not the actual image size.
///
/// the path to the image to load (file path or URL) /// /// /// /// public void Callback(string path, double x, double y, double width, double height) { ArgChecker.AssertArgNotNullOrEmpty(path, "path"); _handled = true; _callback(path, null, new RRect(x, y, width, height)); } /// /// Callback to overwrite the loaded image with given image object.
/// Can be called directly from delegate handler or asynchronously after setting to True.
/// If imageRectangle is given (not ) then only the specified rectangle will /// be used from the loaded image and not all of it, also the rectangle will be used for size and not the actual image size.
///
/// the image to load public void Callback(Object image) { ArgChecker.AssertArgNotNull(image, "image"); _handled = true; _callback(null, image, RRect.Empty); } /// /// Callback to overwrite the loaded image with given image object.
/// Can be called directly from delegate handler or asynchronously after setting to True.
/// Only the specified rectangle (x,y,width,height) will be used from the loaded image and not all of it, also /// the rectangle will be used for size and not the actual image size.
///
/// the image to load /// /// /// /// public void Callback(Object image, double x, double y, double width, double height) { ArgChecker.AssertArgNotNull(image, "image"); _handled = true; _callback(null, image, new RRect(x, y, width, height)); } } }