-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dansiegel/master
Merge from upstream master
- Loading branch information
Showing
50 changed files
with
803 additions
and
417 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.