Skip to content

Commit

Permalink
Merge pull request #1 from dansiegel/master
Browse files Browse the repository at this point in the history
Merge from upstream master
  • Loading branch information
bijington authored Apr 21, 2021
2 parents 99e2a37 + a49413a commit 3c6af70
Show file tree
Hide file tree
Showing 50 changed files with 803 additions and 417 deletions.
16 changes: 11 additions & 5 deletions Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
<PackageReference Update="NuGet.Build.Packaging" Version="0.2.5-dev.10" />
<PackageReference Update="Microsoft.Web.Xdt" Version="3.0.0" />

<PackageReference Update="SixLabors.ImageSharp" Version="1.0.3" />
<PackageReference Update="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta11" />
<PackageReference Update="SixLabors.Fonts" Version="1.0.0-beta0013" />

<PackageReference Update="Fizzler" Version="1.2.0" />
<PackageReference Update="HarfBuzzSharp" Version="2.6.1.7" />
<PackageReference Update="SkiaSharp" Version="2.80.2" />
<PackageReference Update="SkiaSharp.HarfBuzz" Version="2.80.2" />
<PackageReference Update="Svg.Skia" Version="0.4.1" />
<PackageReference Update="Svg.Custom" Version="0.4.1" />
<PackageReference Update="Svg.Picture" Version="0.4.1" />
<PackageReference Update="System.Buffers" Version="4.5.1" />
<PackageReference Update="System.Drawing.Common" Version="4.7.0" />
<PackageReference Update="System.IO.UnmanagedMemoryStream" Version="4.3.0" />
<PackageReference Update="System.Memory" Version="4.5.4" />
<PackageReference Update="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Update="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
<PackageReference Update="System.ObjectModel" Version="4.3.0" />
<PackageReference Update="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 9 additions & 17 deletions src/Mobile.BuildTools/Drawing/ColorUtils.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SkiaSharp;

namespace Mobile.BuildTools.Drawing
{
internal static class ColorUtils
{
private static readonly Dictionary<string, Color> namedColors = typeof(Color).GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.FieldType == typeof(Color))
.ToDictionary(x => x.Name, x => (Color)x.GetValue(null));
private static readonly Dictionary<string, SKColor> namedColors = typeof(SKColors).GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.FieldType == typeof(SKColor))
.ToDictionary(x => x.Name, x => (SKColor)x.GetValue(null), StringComparer.OrdinalIgnoreCase);

public static bool TryParse(string input, out Color result)
public static bool TryParse(string input, out SKColor result)
{
result = default;


if (string.IsNullOrWhiteSpace(input))
{
return false;
Expand All @@ -26,15 +26,7 @@ public static bool TryParse(string input, out Color result)
return true;
}

try
{
result = Rgba32.ParseHex(input);
return true;
}
catch
{
return false;
}
return SKColor.TryParse(input, out result);
}
}
}
36 changes: 36 additions & 0 deletions src/Mobile.BuildTools/Drawing/Context.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Drawing;
using Mobile.BuildTools.Logging;
using SkiaSharp;

namespace Mobile.BuildTools.Drawing
{
internal class Context
{
public SKColor BackgroundColor { get; }

public ILog Log { get; }

public double Opacity { get; } = 1.0;

public PointF Scale { get; }

public Size Size { get; }

public Context(SKColor backgroundColor, ILog log, double opacity, int width, int height, float uniformScale) : this(backgroundColor, log, opacity, width, height, uniformScale, uniformScale)
{
}

public Context(SKColor backgroundColor, ILog log, double opacity, int width, int height, float xScale, float yScale) : this(backgroundColor, log, opacity, new Size(width, height), new PointF(xScale, yScale))
{
}

private Context(SKColor backgroundColor, ILog log, double opacity, Size size, PointF scale)
{
BackgroundColor = backgroundColor;
Opacity = opacity;
Log = log;
Size = size;
Scale = scale;
}
}
}
9 changes: 9 additions & 0 deletions src/Mobile.BuildTools/Drawing/FloatExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Mobile.BuildTools.Drawing
{
public static class FloatExtensions
{
public static bool IsEqualTo(this float value, float comparisonValue) => Math.Abs(value - comparisonValue) <= 0.0001;
}
}
47 changes: 0 additions & 47 deletions src/Mobile.BuildTools/Drawing/IconUtils.cs

This file was deleted.

62 changes: 62 additions & 0 deletions src/Mobile.BuildTools/Drawing/Image.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Drawing;
using SkiaSharp;

namespace Mobile.BuildTools.Drawing
{
internal class Image : ImageBase
{
private SKBitmap bitmap;

public Image(string filename) : base(filename)
{
bitmap = SKBitmap.Decode(filename);
}

public override bool HasTransparentBackground
{
get
{
var imageWidth = GetOriginalSize().Width;
var imageHeight = GetOriginalSize().Height;

for (var x = 0; x < imageWidth; x++)
{
for (var y = 0; y < imageHeight; y++)
{
if (bitmap.GetPixel(x, y).Alpha == 0)
{
return true;
}
}
}

return false;
}
}

public override Size GetOriginalSize() =>
new Size(bitmap.Info.Width, bitmap.Info.Height);

public override void Draw(SKCanvas canvas, Context context)
{
SKPaint paint = null;
if (context.Opacity != 1d)
{
paint = new SKPaint
{
Color = SKColors.Transparent.WithAlpha((byte)(0xFF * context.Opacity))
};
}

canvas.DrawBitmap(bitmap, 0, 0, paint);

paint?.Dispose();
}

public override void Dispose()
{
bitmap?.Dispose();
bitmap = null;
}
}
}
40 changes: 40 additions & 0 deletions src/Mobile.BuildTools/Drawing/ImageBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Drawing;
using System.IO;
using SkiaSharp;

namespace Mobile.BuildTools.Drawing
{
internal abstract class ImageBase : IDisposable
{
public ImageBase(string filename)
{
Filename = filename;
}

public string Filename { get; }

public abstract bool HasTransparentBackground { get; }

public int Height => GetOriginalSize().Height;

public int Width => GetOriginalSize().Width;

public abstract Size GetOriginalSize();

public abstract void Draw(SKCanvas canvas, Context context);

public static ImageBase Load(string filename)
=> IsVector(filename)
? new VectorImage(filename)
: new Image(filename);

private static bool IsVector(string filename) =>
Path.GetExtension(filename)?.Equals(".svg", StringComparison.OrdinalIgnoreCase) ?? false;

public virtual void Dispose()
{

}
}
}
75 changes: 75 additions & 0 deletions src/Mobile.BuildTools/Drawing/VectorImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Drawing;
using SkiaSharp;
using Svg.Skia;

namespace Mobile.BuildTools.Drawing
{
internal class VectorImage : ImageBase
{
private SKSvg svg;

public VectorImage(string filename) : base(filename)
{
svg = new SKSvg();
svg.Load(filename);
}

public override bool HasTransparentBackground => false;

public override Size GetOriginalSize() =>
new Size((int)svg.Picture.CullRect.Size.Width, (int)svg.Picture.CullRect.Size.Height);

public override void Draw(SKCanvas canvas, Context context)
{
SKPaint opacityPaint = null;
if (context.Opacity != 1d)
{
opacityPaint = new SKPaint
{
Color = SKColors.Transparent.WithAlpha((byte)(0xFF * context.Opacity))
};
}

if (context.Scale.X >= 1)
{
// draw using default scaling
canvas.DrawPicture(svg.Picture, opacityPaint);
}
else
{
// draw using raster downscaling
var size = GetOriginalSize();

// TODO: Try and apply a sensible default of 1024x1024 if no sizing information exists.
// Log warning out if reverting to defaults.

// vector scaling has rounding issues, so first draw as intended
var info = new SKImageInfo(size.Width, size.Height);
using var bmp = new SKBitmap(info);
using var cvn = new SKCanvas(bmp);

// draw to a larger canvas first
cvn.Clear(context.BackgroundColor);
cvn.DrawPicture(svg.Picture, opacityPaint);

// set the paint to be the highest quality it can find
var paint = new SKPaint
{
IsAntialias = true,
FilterQuality = SKFilterQuality.High
};

// draw to the main canvas using the correct quality settings
canvas.DrawBitmap(bmp, 0, 0, paint);
}

opacityPaint?.Dispose();
}

public override void Dispose()
{
svg?.Dispose();
svg = null;
}
}
}
Loading

0 comments on commit 3c6af70

Please sign in to comment.