|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| using System;
|
| using System.Collections.Generic;
|
| using System.Linq;
|
| using OxyPlot;
|
| using SkiaSharp;
|
|
|
| namespace DWSIM.Drawing.SkiaSharp.Renderers
|
| {
|
|
|
|
|
|
|
|
|
| public class SKCanvasRenderContext : RenderContextBase
|
| {
|
|
|
|
|
|
|
| private readonly HashSet<OxyImage> imagesInUse = new HashSet<OxyImage>();
|
|
|
|
|
|
|
|
|
| private readonly Dictionary<OxyImage, SKBitmap> imageCache = new Dictionary<OxyImage, SKBitmap>();
|
|
|
|
|
|
|
|
|
| private readonly SKPaint paint;
|
|
|
|
|
|
|
|
|
| private readonly SKPath path;
|
|
|
|
|
|
|
|
|
| private readonly SKRect bounds;
|
|
|
|
|
|
|
|
|
| private readonly List<float> pts;
|
|
|
|
|
|
|
|
|
| private SKCanvas canvas;
|
|
|
|
|
|
|
|
|
|
|
| public SKCanvasRenderContext(double scale)
|
| {
|
| this.paint = new SKPaint();
|
| this.path = new SKPath();
|
| this.bounds = new SKRect();
|
| this.pts = new List<float>();
|
| this.Scale = scale;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| public double Scale { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
| public void SetTarget(SKCanvas c)
|
| {
|
| this.canvas = c;
|
| }
|
|
|
|
|
|
|
|
|
| private void Reset()
|
| {
|
| this.paint.Style = SKPaintStyle.Fill;
|
| this.paint.StrokeCap = SKStrokeCap.Butt;
|
| this.paint.StrokeJoin = SKStrokeJoin.Miter;
|
| this.paint.TextAlign = SKTextAlign.Left;
|
| this.paint.Typeface = null;
|
| this.paint.StrokeWidth = 1.0f;
|
| this.paint.StrokeMiter = 4.0f;
|
| this.paint.TextSize = 20.0f;
|
| this.paint.TextScaleX = 1.0f;
|
| this.paint.TextSkewX = 0.0f;
|
| this.paint.BlendMode = SKBlendMode.SrcOver;
|
| this.paint.ColorFilter = null;
|
| this.paint.Shader = null;
|
| this.paint.PathEffect = null;
|
| this.paint.MaskFilter = null;
|
| this.paint.HintingLevel = SKPaintHinting.Normal;
|
| switch (GlobalSettings.Settings.RunningPlatform())
|
| {
|
| case GlobalSettings.Settings.Platform.Windows:
|
| this.paint.Typeface = SKTypeface.FromFamilyName("Segoe UI", SKTypefaceStyle.Normal);
|
| break;
|
| case GlobalSettings.Settings.Platform.Linux:
|
| this.paint.Typeface = SKTypeface.FromFamilyName("Ubuntu", SKTypefaceStyle.Normal);
|
| break;
|
| case GlobalSettings.Settings.Platform.Mac:
|
| this.paint.Typeface = SKTypeface.FromFamilyName("Helvetica Neue", SKTypefaceStyle.Normal);
|
| break;
|
| }
|
| this.paint.Color = GraphicsSurface.ForegroundColor;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override void DrawEllipse(OxyRect rect, OxyColor fill, OxyColor stroke, double thickness)
|
| {
|
| this.Reset();
|
| {
|
| if (fill.IsVisible())
|
| {
|
| this.SetFill(fill);
|
| this.canvas.DrawOval(this.Convert(rect), this.paint);
|
| }
|
|
|
| if (stroke.IsVisible())
|
| {
|
| this.SetStroke(stroke, thickness);
|
| this.canvas.DrawOval(this.Convert(rect), this.paint);
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override void DrawEllipses(IList<OxyRect> rectangles, OxyColor fill, OxyColor stroke, double thickness)
|
| {
|
| this.Reset();
|
| {
|
| foreach (var rect in rectangles)
|
| {
|
| if (fill.IsVisible())
|
| {
|
| this.SetFill(fill);
|
| this.canvas.DrawOval(this.Convert(rect), this.paint);
|
| }
|
|
|
| if (stroke.IsVisible())
|
| {
|
| this.SetStroke(stroke, thickness);
|
| this.canvas.DrawOval(this.Convert(rect), this.paint);
|
| }
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override void DrawLine(IList<ScreenPoint> points, OxyColor stroke, double thickness, double[] dashArray, LineJoin lineJoin, bool aliased)
|
| {
|
| this.Reset();
|
| {
|
| this.path.Reset();
|
| {
|
| this.SetPath(points, aliased);
|
| this.SetStroke(stroke, thickness, dashArray, lineJoin, aliased);
|
| this.canvas.DrawPath(this.path, this.paint);
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override void DrawLineSegments(IList<ScreenPoint> points, OxyColor stroke, double thickness, double[] dashArray, LineJoin lineJoin, bool aliased)
|
| {
|
| this.Reset();
|
| {
|
| this.SetStroke(stroke, thickness, dashArray, lineJoin, aliased);
|
|
|
| if (aliased)
|
| {
|
| for (int i = 0; i < points.Count; i += 2)
|
| {
|
| var pt0 = points[i];
|
| var pt1 = points[i + 1];
|
|
|
| this.canvas.DrawLine(this.ConvertAliased(pt0.X), this.ConvertAliased(pt0.Y), this.ConvertAliased(pt1.X), this.ConvertAliased(pt1.Y), this.paint);
|
| }
|
| }
|
| else
|
| {
|
| for (int i = 0; i < points.Count; i += 2)
|
| {
|
| var pt0 = points[i];
|
| var pt1 = points[i + 1];
|
|
|
| this.canvas.DrawLine(this.Convert(pt0.X), this.Convert(pt0.Y), this.Convert(pt1.X), this.Convert(pt1.Y), this.paint);
|
| }
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override void DrawPolygon(IList<ScreenPoint> points, OxyColor fill, OxyColor stroke, double thickness, double[] dashArray, LineJoin lineJoin, bool aliased)
|
| {
|
| this.Reset();
|
| {
|
| this.path.Reset();
|
| {
|
| this.SetPath(points, aliased);
|
| this.path.Close();
|
|
|
| if (fill.IsVisible())
|
| {
|
| this.SetFill(fill);
|
| this.canvas.DrawPath(this.path, this.paint);
|
| }
|
|
|
| if (stroke.IsVisible())
|
| {
|
| this.SetStroke(stroke, thickness, dashArray, lineJoin, aliased);
|
| this.canvas.DrawPath(this.path, this.paint);
|
| }
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override void DrawRectangle(OxyRect rect, OxyColor fill, OxyColor stroke, double thickness)
|
| {
|
| this.Reset();
|
| {
|
| if (fill.IsVisible())
|
| {
|
| this.SetFill(fill);
|
| this.canvas.DrawRect(new SKRect(this.ConvertAliased(rect.Left), this.ConvertAliased(rect.Top), this.ConvertAliased(rect.Right), this.ConvertAliased(rect.Bottom)), this.paint);
|
| }
|
|
|
| if (stroke.IsVisible())
|
| {
|
| this.SetStroke(stroke, thickness, aliased: true);
|
| this.canvas.DrawRect(new SKRect(this.ConvertAliased(rect.Left), this.ConvertAliased(rect.Top), this.ConvertAliased(rect.Right), this.ConvertAliased(rect.Bottom)), this.paint);
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalAlignment halign, VerticalAlignment valign, OxySize? maxSize)
|
| {
|
| this.Reset();
|
| {
|
| this.paint.TextSize = this.Convert(fontSize);
|
| this.SetFill(fill);
|
|
|
| float width;
|
| float height;
|
| float lineHeight, delta;
|
| this.GetFontMetrics(this.paint, out lineHeight, out delta);
|
| if (maxSize.HasValue || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Bottom)
|
| {
|
| width = this.paint.MeasureText(text);
|
| height = lineHeight;
|
| }
|
| else
|
| {
|
| width = height = 0f;
|
| }
|
|
|
| if (maxSize.HasValue)
|
| {
|
| var maxWidth = this.Convert(maxSize.Value.Width);
|
| var maxHeight = this.Convert(maxSize.Value.Height);
|
|
|
| if (width > maxWidth)
|
| {
|
| width = maxWidth;
|
| }
|
|
|
| if (height > maxHeight)
|
| {
|
| height = maxHeight;
|
| }
|
| }
|
|
|
| var dx = halign == HorizontalAlignment.Left ? 0d : (halign == HorizontalAlignment.Center ? -width * 0.5 : -width);
|
| var dy = valign == VerticalAlignment.Bottom ? 0d : (valign == VerticalAlignment.Middle ? height * 0.5 : height);
|
| var x0 = -this.bounds.Left;
|
| var y0 = delta;
|
|
|
| this.canvas.Save();
|
| this.canvas.Translate(this.Convert(p.X), this.Convert(p.Y));
|
| this.canvas.RotateDegrees((float)rotate);
|
| this.canvas.Translate((float)dx + x0, (float)dy + y0);
|
|
|
| if (maxSize.HasValue)
|
| {
|
| var x1 = -x0;
|
| var y1 = -height - y0;
|
| this.canvas.ClipRect(new SKRect(x1, y1, x1 + width, y1 + height));
|
| this.canvas.Translate(0, lineHeight - height);
|
| }
|
|
|
| this.canvas.DrawText(text, 0, 0, this.paint);
|
| this.canvas.Restore();
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight)
|
| {
|
| if (string.IsNullOrEmpty(text))
|
| {
|
| return OxySize.Empty;
|
| }
|
|
|
| this.Reset();
|
| {
|
| this.paint.IsAntialias = true;
|
| this.paint.TextSize = this.Convert(fontSize);
|
| float lineHeight, delta;
|
| this.GetFontMetrics(this.paint, out lineHeight, out delta);
|
| var textWidth = this.paint.MeasureText(text);
|
| return new OxySize(textWidth / this.Scale, lineHeight / this.Scale);
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| public override bool SetClip(OxyRect rect)
|
| {
|
| this.canvas.Save();
|
| this.canvas.ClipRect(this.Convert(rect));
|
| return true;
|
| }
|
|
|
|
|
|
|
|
|
| public override void ResetClip()
|
| {
|
| this.canvas.Restore();
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
| {
|
| var image = this.GetImage(source);
|
| if (image == null)
|
| {
|
| return;
|
| }
|
|
|
| var src = new SKRect((int)srcX, (int)srcY, (int)(srcX + srcWidth), (int)(srcY + srcHeight));
|
| var dest = new SKRect(this.Convert(destX), this.Convert(destY), this.Convert(destX + destWidth), this.Convert(destY + destHeight));
|
|
|
| this.Reset();
|
|
|
|
|
| this.canvas.DrawBitmap(image, src, dest, this.paint);
|
| }
|
|
|
|
|
|
|
|
|
|
|
| public override void CleanUp()
|
| {
|
| var imagesToRelease = this.imageCache.Keys.Where(i => !this.imagesInUse.Contains(i)).ToList();
|
| foreach (var i in imagesToRelease)
|
| {
|
| var image = this.GetImage(i);
|
| image.Dispose();
|
| this.imageCache.Remove(i);
|
| }
|
|
|
| this.imagesInUse.Clear();
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private void GetFontMetrics(SKPaint paint, out float defaultLineHeight, out float delta)
|
| {
|
| var metrics = paint.FontMetrics;
|
| var ascent = -metrics.Ascent;
|
| var descent = metrics.Descent;
|
| var leading = metrics.Leading;
|
|
|
|
|
|
|
| leading = leading < 0 ? 0 : (float)Math.Floor(leading + 0.5f);
|
| var lineHeight = (float)Math.Floor(ascent + 0.5f) + (float)Math.Floor(descent + 0.5) + leading;
|
| var ascenderDelta = leading >= 0 ? 0 : (float)Math.Floor((0.2 * lineHeight) + 0.5);
|
| defaultLineHeight = lineHeight + ascenderDelta;
|
| delta = ascenderDelta - descent;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| private float Convert(double x)
|
| {
|
| return (float)(x * this.Scale);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| private SKRect Convert(OxyRect rect)
|
| {
|
| return new SKRect(this.ConvertAliased(rect.Left), this.ConvertAliased(rect.Top), this.ConvertAliased(rect.Right), this.ConvertAliased(rect.Bottom));
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| private float ConvertAliased(double x)
|
| {
|
| return (int)(x * this.Scale) + 0.5f;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| private void SetPath(IList<ScreenPoint> points, bool aliased)
|
| {
|
| if (aliased)
|
| {
|
| this.path.MoveTo(this.ConvertAliased(points[0].X), this.ConvertAliased(points[0].Y));
|
| for (int i = 1; i < points.Count; i++)
|
| {
|
| this.path.LineTo(this.ConvertAliased(points[i].X), this.ConvertAliased(points[i].Y));
|
| }
|
| }
|
| else
|
| {
|
| this.path.MoveTo(this.Convert(points[0].X), this.Convert(points[0].Y));
|
| for (int i = 1; i < points.Count; i++)
|
| {
|
| this.path.LineTo(this.Convert(points[i].X), this.Convert(points[i].Y));
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
| private void SetFill(OxyColor fill)
|
| {
|
| this.paint.Style = SKPaintStyle.Fill;
|
| this.paint.Color = fill.ToSKColor();
|
| if (this.paint.Color == SKColors.Black && GlobalSettings.Settings.DarkMode) this.paint.Color = SKColors.White;
|
| this.paint.IsAntialias = true;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private void SetStroke(OxyColor stroke, double thickness, double[] dashArray = null, LineJoin lineJoin = LineJoin.Miter, bool aliased = false)
|
| {
|
| this.paint.Style = SKPaintStyle.Stroke;
|
| this.paint.Color = stroke.ToSKColor();
|
| if (this.paint.Color == SKColors.Black && GlobalSettings.Settings.DarkMode) this.paint.Color = SKColors.White;
|
| this.paint.StrokeWidth = this.Convert(thickness);
|
| this.paint.StrokeJoin = lineJoin.Convert();
|
| if (dashArray != null)
|
| {
|
| var dashArrayF = dashArray.Select(this.Convert).ToArray();
|
| this.paint.PathEffect = SKPathEffect.CreateDash(dashArrayF, 0f);
|
| }
|
|
|
| this.paint.IsAntialias = !aliased;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| private SKBitmap GetImage(OxyImage source)
|
| {
|
| if (source == null)
|
| {
|
| return null;
|
| }
|
|
|
| if (!this.imagesInUse.Contains(source))
|
| {
|
| this.imagesInUse.Add(source);
|
| }
|
|
|
| SKBitmap bitmap;
|
| if (!this.imageCache.TryGetValue(source, out bitmap))
|
| {
|
| var bytes = source.GetData();
|
| bitmap = SKBitmap.Decode(bytes);
|
| this.imageCache.Add(source, bitmap);
|
| }
|
|
|
| return bitmap;
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| public static class ExtensionMethods
|
| {
|
|
|
|
|
|
|
|
|
|
|
| public static SKColor ToColor(this OxyColor color)
|
| {
|
| return color.IsInvisible() ? SKColors.Transparent : new SKColor(color.R, color.G, color.B, color.A);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| public static SKColor ToSKColor(this OxyColor color)
|
| {
|
| return color.IsInvisible() ? new SKColor(0, 0, 0, 0) : new SKColor(color.R, color.G, color.B, color.A);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| public static SKStrokeJoin Convert(this LineJoin join)
|
| {
|
| switch (join)
|
| {
|
| case LineJoin.Bevel:
|
| return SKStrokeJoin.Bevel;
|
| case LineJoin.Miter:
|
| return SKStrokeJoin.Miter;
|
| case LineJoin.Round:
|
| return SKStrokeJoin.Round;
|
| default:
|
| throw new InvalidOperationException("Invalid join type.");
|
| }
|
| }
|
|
|
| }
|
| } |