// "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.Adapters { /// /// Adapter for platform specific graphics rendering object - used to render graphics and text in platform specific context.
/// The core HTML Renderer components use this class for rendering logic, extending this /// class in different platform: WinForms, WPF, Metro, PDF, etc. ///
public abstract class RGraphics : IDisposable { #region Fields/Consts /// /// the global adapter /// protected readonly RAdapter _adapter; /// /// Te clipping bound stack as clips are pushed/poped to/from the graphics /// protected readonly Stack _clipStack = new Stack(); #endregion /// /// Init. /// protected RGraphics(RAdapter adapter, RRect initialClip) { ArgChecker.AssertArgNotNull(adapter, "global"); _adapter = adapter; _clipStack.Push(initialClip); } /// /// Get color pen. /// /// the color to get the pen for /// pen instance public RPen GetPen(RColor color) { return _adapter.GetPen(color); } /// /// Get solid color brush. /// /// the color to get the brush for /// solid color brush instance public RBrush GetSolidBrush(RColor color) { return _adapter.GetSolidBrush(color); } /// /// Get linear gradient color brush from to . /// /// the rectangle to get the brush for /// the start color of the gradient /// the end color of the gradient /// the angle to move the gradient from start color to end color in the rectangle /// linear gradient color brush instance public RBrush GetLinearGradientBrush(RRect rect, RColor color1, RColor color2, double angle) { return _adapter.GetLinearGradientBrush(rect, color1, color2, angle); } /// /// Gets a Rectangle structure that bounds the clipping region of this Graphics. /// /// A rectangle structure that represents a bounding rectangle for the clipping region of this Graphics. public RRect GetClip() { return _clipStack.Peek(); } /// /// Pop the latest clip push. /// public abstract void PopClip(); /// /// Push the clipping region of this Graphics to interception of current clipping rectangle and the given rectangle. /// /// Rectangle to clip to. public abstract void PushClip(RRect rect); /// /// Push the clipping region of this Graphics to exclude the given rectangle from the current clipping rectangle. /// /// Rectangle to exclude clipping in. public abstract void PushClipExclude(RRect rect); /// /// Set the graphics smooth mode to use anti-alias.
/// Use to return back the mode used. ///
/// the previous smooth mode before the change public abstract Object SetAntiAliasSmoothingMode(); /// /// Return to previous smooth mode before anti-alias was set as returned from . /// /// the previous mode to set public abstract void ReturnPreviousSmoothingMode(Object prevMode); /// /// Get TextureBrush object that uses the specified image and bounding rectangle. /// /// The Image object with which this TextureBrush object fills interiors. /// A Rectangle structure that represents the bounding rectangle for this TextureBrush object. /// The dimension by which to translate the transformation public abstract RBrush GetTextureBrush(RImage image, RRect dstRect, RPoint translateTransformLocation); /// /// Get GraphicsPath object. /// /// graphics path instance public abstract RGraphicsPath GetGraphicsPath(); /// /// Measure the width and height of string when drawn on device context HDC /// using the given font . /// /// the string to measure /// the font to measure string with /// the size of the string public abstract RSize MeasureString(string str, RFont font); /// /// Measure the width of string under max width restriction calculating the number of characters that can fit and the width those characters take.
/// Not relevant for platforms that don't render HTML on UI element. ///
/// the string to measure /// the font to measure string with /// the max width to calculate fit characters /// the number of characters that will fit under maxWidth restriction /// the width that only the characters that fit into max width take public abstract void MeasureString(string str, RFont font, double maxWidth, out int charFit, out double charFitWidth); /// /// Draw the given string using the given font and foreground color at given location. /// /// the string to draw /// the font to use to draw the string /// the text color to set /// the location to start string draw (top-left) /// used to know the size of the rendered text for transparent text support /// is to render the string right-to-left (true - RTL, false - LTR) public abstract void DrawString(String str, RFont font, RColor color, RPoint point, RSize size, bool rtl); /// /// Draws a line connecting the two points specified by the coordinate pairs. /// /// Pen that determines the color, width, and style of the line. /// The x-coordinate of the first point. /// The y-coordinate of the first point. /// The x-coordinate of the second point. /// The y-coordinate of the second point. public abstract void DrawLine(RPen pen, double x1, double y1, double x2, double y2); /// /// Draws a rectangle specified by a coordinate pair, a width, and a height. /// /// A Pen that determines the color, width, and style of the rectangle. /// The x-coordinate of the upper-left corner of the rectangle to draw. /// The y-coordinate of the upper-left corner of the rectangle to draw. /// The width of the rectangle to draw. /// The height of the rectangle to draw. public abstract void DrawRectangle(RPen pen, double x, double y, double width, double height); /// /// Fills the interior of a rectangle specified by a pair of coordinates, a width, and a height. /// /// Brush that determines the characteristics of the fill. /// The x-coordinate of the upper-left corner of the rectangle to fill. /// The y-coordinate of the upper-left corner of the rectangle to fill. /// Width of the rectangle to fill. /// Height of the rectangle to fill. public abstract void DrawRectangle(RBrush brush, double x, double y, double width, double height); /// /// Draws the specified portion of the specified at the specified location and with the specified size. /// /// Image to draw. /// Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle. /// Rectangle structure that specifies the portion of the object to draw. public abstract void DrawImage(RImage image, RRect destRect, RRect srcRect); /// /// Draws the specified Image at the specified location and with the specified size. /// /// Image to draw. /// Rectangle structure that specifies the location and size of the drawn image. public abstract void DrawImage(RImage image, RRect destRect); /// /// Draws a GraphicsPath. /// /// Pen that determines the color, width, and style of the path. /// GraphicsPath to draw. public abstract void DrawPath(RPen pen, RGraphicsPath path); /// /// Fills the interior of a GraphicsPath. /// /// Brush that determines the characteristics of the fill. /// GraphicsPath that represents the path to fill. public abstract void DrawPath(RBrush brush, RGraphicsPath path); /// /// Fills the interior of a polygon defined by an array of points specified by Point structures. /// /// Brush that determines the characteristics of the fill. /// Array of Point structures that represent the vertices of the polygon to fill. public abstract void DrawPolygon(RBrush brush, RPoint[] points); /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public abstract void Dispose(); } }