// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) 2014 OxyPlot contributors // // // Provides an implementation of IRenderContext which draws to an ImageSharp Image. // // -------------------------------------------------------------------------------------------------------------------- namespace OxyPlot.ImageSharp { using System; using System.Collections.Generic; using System.IO; using System.Linq; using OxyPlot; using SixLabors.ImageSharp; using SixLabors.Fonts; using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Drawing; using SixLabors.ImageSharp.Processing.Processors; /// /// Provides an implementation of IRenderContext which draws to a . /// public class ImageRenderContext : ClippingRenderContext, IDisposable { /// /// The default font to use when a request font cannot be found. /// private static readonly string FallbackFontFamily = "Arial"; /// /// Image to which the the will render. /// private readonly Image image; /// /// Image to which we will render when clipping. /// private readonly Image clipImage; /// /// Whether or not the ImageRenderContext has been disposed. /// private bool disposedValue = false; /// /// The current clipping rectangle. /// private Rectangle clippingRectangle; /// /// A value indicating whether we are currently clipping. /// private bool clipping; /// /// Initializes a new instance of the class. /// /// The width of the image. /// The height of the image. /// The background color of the image. /// The number of dots per inch (DPI). public ImageRenderContext(int width, int height, OxyColor background, double dpi = 96) { this.image = new Image(width, height); this.clipImage = new Image(width, height); this.image.Metadata.HorizontalResolution = dpi; this.image.Metadata.VerticalResolution = dpi; this.clipImage.Metadata.HorizontalResolution = dpi; this.clipImage.Metadata.VerticalResolution = dpi; this.Dpi = (float)dpi; this.DpiScale = (float)(dpi / 96.0); this.image.Mutate(img => img.BackgroundColor(ToRgba32(background))); this.RendersToScreen = false; this.clipping = false; } /// /// Gets the DPI scaling factor. A value of 1 corresponds to 96 DPI (dots per inch). /// private float DpiScale { get; } /// /// Gets the number of dots per inch (DPI). /// private float Dpi { get; } /// /// Gets the current target image. /// private Image Target => this.clipping ? this.clipImage : this.image; /// /// Gets a copy of the image. /// /// A copy of the internal image. public Image GetImageCopy() { this.EnsureClippedRegion(); return this.image.Clone(); } /// /// Saves the image to the specified stream as a png. /// /// The output stream. public void SaveAsPng(Stream output) { this.EnsureClippedRegion(); this.image.SaveAsPng(output); } /// /// Saves the image to the specified stream as a bmp. /// /// The output stream. public void SaveAsBmp(Stream output) { this.EnsureClippedRegion(); // TODO: investigate bmp encoder options this.image.SaveAsBmp(output); } /// /// Saves the image to the specified stream as a gif. /// /// The output stream. public void SaveAsGif(Stream output) { this.EnsureClippedRegion(); // TODO: investigate gif encoder options this.image.SaveAsGif(output); } /// /// Saves the image to the specified stream as a jpeg. /// /// The output stream. /// The quality of the exported jpeg, a value between 0 and 100. public void SaveAsJpeg(Stream output, int quality = 75) { this.EnsureClippedRegion(); this.image.SaveAsJpeg(output, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder() { Quality = quality }); } /// public override void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily = null, double fontSize = 10, double fontWeight = 400, double rotation = 0, OxyPlot.HorizontalAlignment horizontalAlignment = OxyPlot.HorizontalAlignment.Left, OxyPlot.VerticalAlignment verticalAlignment = OxyPlot.VerticalAlignment.Top, OxySize? maxSize = null) { if (text == null || !fill.IsVisible()) { return; } var font = this.GetFontOrThrow(fontFamily, fontSize, this.ToFontStyle(fontWeight)); var actualFontSize = this.NominalFontSizeToPoints(fontSize); var outputX = this.Convert(p.X); var outputY = this.Convert(p.Y); var outputPosition = new PointF(outputX, outputY); var cos = (float)Math.Cos(rotation * Math.PI / 180.0); var sin = (float)Math.Sin(rotation * Math.PI / 180.0); // measure bounds of the whole text (we only need the height) var bounds = this.MeasureTextLoose(text, fontFamily, fontSize, fontWeight); var boundsHeight = this.Convert(bounds.Height); var offsetHeight = new PointF(boundsHeight * -sin, boundsHeight * cos); // determine the font metrids for this font size at 96 DPI var actualDescent = this.Convert(actualFontSize * this.MilliPointsToNominalResolution(font.FontMetrics.Descender)); var offsetDescent = new PointF(actualDescent * -sin, actualDescent * cos); var actualLineHeight = this.Convert(actualFontSize * this.MilliPointsToNominalResolution(font.FontMetrics.LineHeight)); var offsetLineHeight = new PointF(actualLineHeight * -sin, actualLineHeight * cos); var actualLineGap = this.Convert(actualFontSize * this.MilliPointsToNominalResolution(font.FontMetrics.LineGap)); var offsetLineGap = new PointF(actualLineGap * -sin, actualLineGap * cos); // find top of the whole text var deltaY = verticalAlignment switch { OxyPlot.VerticalAlignment.Top => 1.0f, OxyPlot.VerticalAlignment.Middle => 0.5f, OxyPlot.VerticalAlignment.Bottom => 0.0f, _ => throw new ArgumentOutOfRangeException(nameof(verticalAlignment)), }; // this is the top of the top line var topPosition = outputPosition + (offsetHeight * deltaY) - offsetHeight; // need this later var deltaX = horizontalAlignment switch { OxyPlot.HorizontalAlignment.Left => -0.0f, OxyPlot.HorizontalAlignment.Center => -0.5f, OxyPlot.HorizontalAlignment.Right => -1.0f, _ => throw new ArgumentOutOfRangeException(nameof(horizontalAlignment)), }; var lines = StringHelper.SplitLines(text); for (int li = 0; li < lines.Length; li++) { var line = lines[li]; if (string.IsNullOrWhiteSpace(line)) { continue; } // measure bounds of just the line (we only need the width) var lineBounds = this.MeasureTextLoose(line, fontFamily, fontSize, fontWeight); var lineBoundsWidth = this.Convert(lineBounds.Width); var offsetLineWidth = new PointF(lineBoundsWidth * cos, lineBoundsWidth * sin); // find the left baseline position var lineTop = topPosition + (offsetLineGap * li) + (offsetLineHeight * li); var lineBaseLineLeft = lineTop + offsetLineWidth * deltaX + offsetLineHeight + offsetDescent; // this seems to produce consistent and correct results, but we have to rotate it manually, so render it at the origin for simplicity var textPath = new PathBuilder().AddLine(0f, 0f, lineBoundsWidth, 0).Build(); var glyphsAtOrigin = TextBuilder.GenerateGlyphs(line, textPath, new TextOptions(font) { Dpi = this.Dpi, HorizontalAlignment = SixLabors.Fonts.HorizontalAlignment.Left, VerticalAlignment = SixLabors.Fonts.VerticalAlignment.Bottom, // sit on the line (baseline) KerningMode = KerningMode.Auto, }); // translate and rotate into possition var transform = Matrix3x2Extensions.CreateRotationDegrees((float)rotation); transform.Translation = lineBaseLineLeft; var glyphs = glyphsAtOrigin.Transform(transform); // draw the glyphs this.Target.Mutate(img => { img.Fill(ToRgba32(fill), glyphs); }); } } /// public override OxySize MeasureText(string text, string fontFamily = null, double fontSize = 10, double fontWeight = 500) { return this.MeasureTextLoose(text, fontFamily, fontSize, fontWeight); } /// public override void DrawImage( OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { if (source == null) { return; } var dest = new RectangleF((float)this.Convert(destX), (float)this.Convert(destY), (float)this.Convert(destWidth), (float)this.Convert(destHeight)); var src = new RectangleF((float)srcX, (float)srcY, (float)srcWidth, (float)srcHeight); var scale = new SizeF(dest.Width / src.Width, dest.Height / src.Height); // if we are outside the image, quit now if (dest.Right < 0 || dest.Left >= this.image.Width || dest.Bottom < 0 || dest.Top >= this.image.Height) { return; } // crop the bounds so that they are within the image bounds (this is necessary because we have to create a resized version of the cropped source image) var cropLeft = dest.Left < 0 ? -dest.Left : 0; var cropTop = dest.Top < 0 ? -dest.Top : 0; var cropRight = dest.Right >= this.image.Width ? dest.Right - this.image.Width : 0; var cropBottom = dest.Bottom >= this.image.Height ? dest.Bottom - this.image.Height : 0; dest = RectangleF.FromLTRB(dest.Left + cropLeft, dest.Top + cropTop, dest.Right - cropRight, dest.Bottom - cropBottom); src = RectangleF.FromLTRB(src.Left + (cropLeft / scale.Width), src.Top + (cropTop / scale.Height), src.Right - (cropRight / scale.Width), src.Bottom - (cropBottom / scale.Height)); var bytes = source.GetData(); var sourceImage = Image.Load(bytes); var resampler = interpolate ? KnownResamplers.Triangle : KnownResamplers.NearestNeighbor; /* The idea now is to roughly crop the source before we resize and then precisely crop it, before drawing it onto the target * The steps required are: * - Crop the source image to -1/+2 pixel bounds (may need to increase these bounds depending on the resampler) * - Add a one pixel 'mirror' border, so that we can a clamped edge when interpolating * - Resize the source image by the appropriate scale with the appropriate resampler, simultaneously offseting by the non-integer parts of dest and src * - Crop to exactly what we want * - Draw the source image onto the destination image */ var doPad = interpolate; var srcRough = new Rectangle((int)Math.Floor(src.X), (int)Math.Floor(src.Y), (int)Math.Ceiling(src.Width + 3), (int)Math.Ceiling(src.Height + 3)); srcRough.Intersect(sourceImage.Bounds()); var srcOffset = new PointF(srcRough.X - src.X, srcRough.Y - src.Y); srcOffset.Offset(0.5f, 0.5f); // texel alignment for resampler if (doPad) { srcOffset.Offset(-1f, -1f); // offset from padding } var destOffset = new PointF(dest.X - (float)Math.Floor(dest.X), dest.Y - (float)Math.Floor(dest.Y)); var destRough = new Rectangle(0, 0, (int)Math.Ceiling(dest.Width), (int)Math.Ceiling(dest.Height)); var rescale = new AffineTransformBuilder().AppendTranslation(srcOffset).AppendScale(scale); try { sourceImage.Mutate(img => { img.Crop(srcRough); if (doPad) { img.Pad(srcRough.Width + 2, srcRough.Height + 2); img.ApplyProcessor(new MirrorPadProcessor()); } }); sourceImage.Mutate(img => { img.Transform(rescale, resampler); destRough.Intersect(sourceImage.Bounds()); img.Crop(destRough); }); this.Target.Mutate(img => { img.DrawImage(sourceImage, new Point((int)dest.X, (int)dest.Y), new GraphicsOptions() { Antialias = interpolate, BlendPercentage = (float)opacity }); }); } catch (ImageProcessingException) { // Swallow: it's probably because we are trying to render outside of the image: https://github.com/SixLabors/ImageSharp/pull/877 // TODO: verify that we are trying to render outside of the image... somehow // - I don't think this can be done without having to track the ImageSharp code unhealthily closely } finally { sourceImage.Dispose(); } } /// public override void DrawLine(IList points, OxyColor stroke, double thickness, EdgeRenderingMode edgeRenderingMode, double[] dashArray, LineJoin lineJoin) { if (points.Count < 2 || !stroke.IsVisible() || thickness <= 0) { return; } var actualThickness = this.GetActualThickness(thickness, edgeRenderingMode); var actualDashArray = dashArray != null ? this.ConvertDashArray(dashArray, actualThickness) : null; var pen = actualDashArray != null ? new Pen(ToRgba32(stroke), actualThickness, actualDashArray) : new Pen(ToRgba32(stroke), actualThickness); var actualPoints = this.GetActualPoints(points, thickness, edgeRenderingMode).ToArray(); var options = this.CreateDrawingOptions(this.ShouldUseAntiAliasingForLine(edgeRenderingMode, points)); this.Target.Mutate(img => { img.DrawLines(options, pen, actualPoints); }); } /// public override void DrawPolygon(IList points, OxyColor fill, OxyColor stroke, double thickness, EdgeRenderingMode edgeRenderingMode, double[] dashArray, LineJoin lineJoin) { var fillInvisible = !fill.IsVisible(); var strokeInvisible = !stroke.IsVisible() || thickness <= 0; if ((fillInvisible && strokeInvisible) || points.Count < 2) { return; } var actualThickness = this.GetActualThickness(thickness, edgeRenderingMode); var actualDashArray = dashArray != null ? this.ConvertDashArray(dashArray, actualThickness) : null; var pen = strokeInvisible ? null : actualDashArray != null ? new Pen(ToRgba32(stroke), actualThickness, actualDashArray) : new Pen(ToRgba32(stroke), actualThickness); var actualPoints = this.GetActualPoints(points, thickness, edgeRenderingMode).ToArray(); var options = this.CreateDrawingOptions(this.ShouldUseAntiAliasingForLine(edgeRenderingMode, points)); var brush = fillInvisible ? null : Brushes.Solid(ToRgba32(fill)); this.Target.Mutate(img => { if (brush != null) { img.FillPolygon(options, brush, actualPoints); } if (pen != null) { img.DrawPolygon(options, pen, actualPoints); } }); } /// protected override void ResetClip() { this.EnsureClippedRegion(); this.clipping = false; } /// protected override void SetClip(OxyRect clippingRectangle) { var actualRectangle = this.ConvertSnap(clippingRectangle, 0); this.clippingRectangle = Rectangle.FromLTRB((int)actualRectangle.Left, (int)actualRectangle.Top, (int)actualRectangle.Right, (int)actualRectangle.Bottom); this.EnsureClippedRegion(); this.clipping = true; this.Blit(this.image, this.clipImage, this.clippingRectangle); } /// public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } /// /// Disposes the object. /// /// Whether this method is being called from Dispose. protected virtual void Dispose(bool disposing) { if (!this.disposedValue) { if (disposing) { this.image.Dispose(); this.clipImage.Dispose(); } this.disposedValue = true; } } /// /// Translates an to a . /// /// The . /// The resulting . private static Rgba32 ToRgba32(OxyColor color) { return new Rgba32(color.R, color.G, color.B, color.A); } /// /// Gets the pixel offset that a line with the specified thickness should snap to. /// /// /// This takes into account that lines with even stroke thickness should be snapped to the border between two pixels while lines with odd stroke thickness should be snapped to the middle of a pixel. /// /// The line thickness. /// The snap offset. private static float GetSnapOffset(float thickness) { var mod = thickness % 2; var isOdd = mod >= 0.5 && mod < 1.5; return isOdd ? 0.5f : 0; } /// /// Snaps a value to a pixel with the specified offset. /// /// The value. /// The offset. /// The snapped value. private static float Snap(float value, float offset) { return (float)Math.Round(value + offset, MidpointRounding.AwayFromZero) - offset; } /// /// Counts the number of lines in the text. /// /// The text. /// The number of lines in the text. private static int CountLines(string text) { return StringHelper.SplitLines(text).Length; } /// /// Copies the current clipping rectangle from the to the . /// private void EnsureClippedRegion() { if (this.clipping) { this.Blit(this.clipImage, this.image, this.clippingRectangle); } } /// /// Copies pixel values from one image to another within a given rectangle. /// /// The from which to copy. /// The to which to copy. /// The region to copy. private void Blit(Image source, Image destination, Rectangle rectangle) { rectangle.Intersect(source.Bounds()); for (int i = rectangle.Left; i < rectangle.Right; i++) { for (int j = rectangle.Top; j < rectangle.Bottom; j++) { destination[i, j] = source[i, j]; } } } private Font GetFontOrThrow(string fontFamily, double fontSize, FontStyle fontWeight, bool allowFallback = true) { var family = this.GetFamilyOrFallbackOrThrow(fontFamily, allowFallback); var actualFontSize = this.NominalFontSizeToPoints(fontSize); return new Font(family, (float)actualFontSize, fontWeight); } private FontFamily GetFamilyOrFallbackOrThrow(string fontFamily = null, bool allowFallback = true) { if (fontFamily == null) { allowFallback = false; fontFamily = FallbackFontFamily; } FontFamily family; try { family = SixLabors.Fonts.SystemFonts.Get(fontFamily); } catch (FontFamilyNotFoundException primaryEx) { if (!allowFallback) { throw; } try { family = SystemFonts.Get(FallbackFontFamily); } catch (FontFamilyNotFoundException fallbackEx) { throw new AggregateException(primaryEx, fallbackEx); } } return family; } /// /// Measures the text as it will be arranged out by OxyPlot. /// /// The text to render. /// The font family. /// The font size in points. /// The font weight. /// An . private OxySize MeasureTextLoose(string text, string fontFamily, double fontSize, double fontWeight) { text = text ?? string.Empty; var font = this.GetFontOrThrow(fontFamily, fontSize, this.ToFontStyle(fontWeight)); var actualFontSize = this.NominalFontSizeToPoints(fontSize); var tight = this.MeasureTextTight(text, fontFamily, fontSize, fontWeight); var width = tight.Width; var lineHeight = actualFontSize * this.MilliPointsToNominalResolution(font.FontMetrics.LineHeight); var lineGap = actualFontSize * this.MilliPointsToNominalResolution(font.FontMetrics.LineGap); var lineCount = CountLines(text); var height = (lineHeight * lineCount) + (lineGap * (lineCount - 1)); return new OxySize(width, height); } /// /// Measures the text as it will be rendered by ImageSharp. /// /// The text to render. /// The font family. /// The font size in points. /// The font weight. /// An . private OxySize MeasureTextTight(string text, string fontFamily, double fontSize, double fontWeight) { text = text ?? string.Empty; var font = this.GetFontOrThrow(fontFamily, fontSize, this.ToFontStyle(fontWeight)); var actualFontSize = this.NominalFontSizeToPoints(fontSize); var result = TextMeasurer.Measure(text, new TextOptions(font) { Dpi = this.Dpi }); return new OxySize(this.ConvertBack(result.Width), this.ConvertBack(result.Height)); } /// /// Gets the snapping offset for the specified stroke thickness. /// /// /// This takes into account that lines with even stroke thickness should be snapped to the border between two pixels while lines with odd stroke thickness should be snapped to the middle of a pixel. /// /// The stroke thickness. /// The edge rendering mode. /// The snap offset. private float GetSnapOffset(double thickness, EdgeRenderingMode edgeRenderingMode) { var actualThickness = this.GetActualThickness(thickness, edgeRenderingMode); return GetSnapOffset(actualThickness); } /// /// Converts millipoints (thousanths of 1/72nds of an inch) to pixels at 96 dots per inch. /// /// The number of milliPoints. /// Pixels at the nominal resolution of 96 dots per inch. private double MilliPointsToNominalResolution(int milliPoints) { return milliPoints * (0.75 / 1000); } /// /// Converts nominal font sizes (1/96ths of an inch) to points (1/72nds of an inch). /// /// The nominal font size, in units of 1/96th of an inch. /// The font size in points. private double NominalFontSizeToPoints(double fontSize) { return fontSize * 0.75; } /// /// Determines an appropriate to approximate the given font weight. /// /// The font weight. /// The that approximates the given font weight. private FontStyle ToFontStyle(double fontWeight) { return fontWeight < 700 ? FontStyle.Regular : FontStyle.Bold; } /// /// Converts a to a , taking into account DPI scaling. /// /// The rectangle. /// The converted rectangle. private RectangleF Convert(OxyRect rect) { var left = this.Convert(rect.Left); var right = this.Convert(rect.Right); var top = this.Convert(rect.Top); var bottom = this.Convert(rect.Bottom); return RectangleF.FromLTRB(left, top, right, bottom); } /// /// Converts a to a , taking into account DPI scaling. /// /// The value. /// The converted value. private float Convert(double value) { return (float)value * this.DpiScale; } /// /// Converts to a , taking into account DPI scaling. /// /// The point. /// The converted point. private PointF Convert(ScreenPoint point) { return new PointF(this.Convert(point.X), this.Convert(point.Y)); } /// /// Converts a to a , applying reversed DPI scaling. /// /// The value. /// The converted value. private double ConvertBack(float value) { return value / this.DpiScale; } /// /// Converts dash array to a array, taking into account DPI scaling. /// /// The array of values. /// The stroke thickness. /// The array of converted values. private float[] ConvertDashArray(double[] values, float strokeThickness) { var ret = new float[values.Length]; for (var i = 0; i < values.Length; i++) { ret[i] = this.Convert(values[i]) * strokeThickness; } return ret; } /// /// Converts a to a , taking into account DPI scaling and snapping the corners to pixels. /// /// The rectangle. /// The snapping offset. /// The converted rectangle. private RectangleF ConvertSnap(OxyRect rect, float snapOffset) { var left = this.ConvertSnap(rect.Left, snapOffset); var right = this.ConvertSnap(rect.Right, snapOffset); var top = this.ConvertSnap(rect.Top, snapOffset); var bottom = this.ConvertSnap(rect.Bottom, snapOffset); return RectangleF.FromLTRB(left, top, right, bottom); } /// /// Converts a to a , taking into account DPI scaling and snapping the value to a pixel. /// /// The value. /// The snapping offset. /// The converted value. private float ConvertSnap(double value, float snapOffset) { return Snap(this.Convert(value), snapOffset); } /// /// Converts to a , taking into account DPI scaling and snapping the point to a pixel. /// /// The point. /// The snapping offset. /// The converted point. private PointF ConvertSnap(ScreenPoint point, float snapOffset) { return new PointF(this.ConvertSnap(point.X, snapOffset), this.ConvertSnap(point.Y, snapOffset)); } /// /// Gets the s that should actually be rendered from the list of s, taking into account DPI scaling and snapping if necessary. /// /// The points. /// The stroke thickness. /// The edge rendering mode. /// The actual points. private IEnumerable GetActualPoints(IList screenPoints, double strokeThickness, EdgeRenderingMode edgeRenderingMode) { switch (edgeRenderingMode) { case EdgeRenderingMode.Automatic when RenderContextBase.IsStraightLine(screenPoints): case EdgeRenderingMode.Adaptive when RenderContextBase.IsStraightLine(screenPoints): case EdgeRenderingMode.PreferSharpness: var snapOffset = this.GetSnapOffset(strokeThickness, edgeRenderingMode); return screenPoints.Select(p => this.ConvertSnap(p, snapOffset)); default: return screenPoints.Select(this.Convert); } } /// /// Gets the stroke thickness that should actually be used for rendering, taking into account DPI scaling and snapping if necessary. /// /// The stroke thickness. /// The edge rendering mode. /// The actual stroke thickness. private float GetActualThickness(double strokeThickness, EdgeRenderingMode edgeRenderingMode) { var scaledThickness = this.Convert(strokeThickness); if (edgeRenderingMode == EdgeRenderingMode.PreferSharpness) { scaledThickness = Snap(scaledThickness, 0); } return scaledThickness; } /// /// Creates a object for the given options. /// /// A value indicating whether graphics should be antialised. /// A new private DrawingOptions CreateDrawingOptions(bool antialised) { var options = new DrawingOptions() { GraphicsOptions = new GraphicsOptions() { Antialias = antialised } }; return options; } } }