Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Horizontal watermark banner is chopped off #208

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Mobile.BuildTools/Drawing/WatermarkTextBanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ private string StripDescenders(string text)
private static (SKPoint, SKPoint) GetBannerLocations(WatermarkPosition watermarkPosition, Size size, float bannerHeight)
=> watermarkPosition switch
{
WatermarkPosition.Top => (new SKPoint(0, 0), new SKPoint(size.Width, 0)),
WatermarkPosition.Bottom => (new SKPoint(0, size.Height), new SKPoint(size.Width, size.Height)),
WatermarkPosition.Top => (new SKPoint(0, bannerHeight / 2), new SKPoint(size.Width, bannerHeight / 2)),
WatermarkPosition.Bottom => (new SKPoint(0, size.Height - (bannerHeight / 2)), new SKPoint(size.Width, size.Height - (bannerHeight / 2))),
WatermarkPosition.TopLeft => (new SKPoint(-bannerHeight, size.Height / 2 + bannerHeight), new SKPoint(size.Width / 2 + bannerHeight, -bannerHeight)),
WatermarkPosition.TopRight => (new SKPoint(size.Width / 2 - bannerHeight, -bannerHeight), new SKPoint(size.Width + bannerHeight, size.Height / 2 + bannerHeight)),
WatermarkPosition.BottomLeft => (new SKPoint(-bannerHeight, size.Height / 2 - bannerHeight), new SKPoint(size.Width / 2 + bannerHeight, size.Height + bannerHeight)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ internal void ProcessImage(OutputImage outputImage)

private SKBitmap ApplyPadding(OutputImage outputImage, ImageBase image, Context context, SKBitmap outputBitmap)
{
if (outputImage.PaddingFactor is null)
if (outputImage.PaddingFactor is null ||
outputImage.PaddingFactor == 0)
{
return outputBitmap;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.IO;
using System.Text;
using System.Text;
using Mobile.BuildTools.Generators.Images;
using Mobile.BuildTools.Models.AppIcons;
using SkiaSharp;
using Xunit;
using Xunit.Abstractions;

namespace Mobile.BuildTools.Tests.Fixtures.Generators
{
public class ImageResizer { }
Expand All @@ -30,7 +30,7 @@ public ImageResizerGeneratorFixture(ITestOutputHelper testOutputHelper)
[InlineData("dotnetbot.svg", "xhdpi", .5)]
public void GeneratesImage(string inputFile, string resourcePath, double scale)
{
var config = GetConfiguration();
var config = GetConfiguration();
config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(inputFile), inputFile), (nameof(scale), scale));
var generator = new ImageResizeGenerator(config);

Expand All @@ -49,8 +49,8 @@ public void GeneratesImage(string inputFile, string resourcePath, double scale)

var ex = Record.Exception(() => generator.ProcessImage(image));

Assert.Null(ex);

Assert.Null(ex);
VerifyImageContents(image);
}

Expand All @@ -63,8 +63,8 @@ public void GeneratesImage(string inputFile, string resourcePath, double scale)
[InlineData("dotnetbot.svg", "xhdpi", 150)]
public void GeneratesImageWithCustomHeightWidth(string inputFile, string resourcePath, int expectedOutput)
{
var config = GetConfiguration();
config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(inputFile), inputFile), (nameof(expectedOutput), expectedOutput));
var config = GetConfiguration();
config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(inputFile), inputFile), (nameof(expectedOutput), expectedOutput));
var generator = new ImageResizeGenerator(config);

var image = new OutputImage
Expand All @@ -82,8 +82,8 @@ public void GeneratesImageWithCustomHeightWidth(string inputFile, string resourc

var ex = Record.Exception(() => generator.ProcessImage(image));

Assert.Null(ex);

Assert.Null(ex);
VerifyImageContents(image);
}

Expand All @@ -94,7 +94,7 @@ public void GeneratesImageWithCustomHeightWidth(string inputFile, string resourc
[InlineData("icon", "beta-version")]
public void AppliesWatermark(string inputImageName, string watermarkImage)
{
var config = GetConfiguration();
var config = GetConfiguration();
config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(inputImageName), inputImageName), (nameof(watermarkImage), watermarkImage));
var generator = new ImageResizeGenerator(config);

Expand Down Expand Up @@ -169,14 +169,34 @@ public void SetsCustomBackground()
}

[Theory]
[InlineData("Dev", 0.5)]
[InlineData("Dev", 1.0)]
[InlineData("Stage", 0.5)]
[InlineData("Something long", 0.5)]
public void AppliesTextBanner(string text, double scale)
[InlineData("Dev", 0.5, WatermarkPosition.BottomLeft)]
[InlineData("Dev", 1.0, WatermarkPosition.BottomLeft)]
[InlineData("Stage", 0.5, WatermarkPosition.BottomLeft)]
[InlineData("Something long", 1.0, WatermarkPosition.BottomLeft)]
[InlineData("Dev", 0.5, WatermarkPosition.BottomRight)]
[InlineData("Dev", 1.0, WatermarkPosition.BottomRight)]
[InlineData("Stage", 0.5, WatermarkPosition.BottomRight)]
[InlineData("Something long", 1.0, WatermarkPosition.BottomRight)]
[InlineData("Dev", 0.5, WatermarkPosition.Bottom)]
[InlineData("Dev", 1.0, WatermarkPosition.Bottom)]
[InlineData("Stage", 0.5, WatermarkPosition.Bottom)]
[InlineData("Something long", 1.0, WatermarkPosition.Bottom)]
[InlineData("Dev", 0.5, WatermarkPosition.TopLeft)]
[InlineData("Dev", 1.0, WatermarkPosition.TopLeft)]
[InlineData("Stage", 0.5, WatermarkPosition.TopLeft)]
[InlineData("Something long", 1.0, WatermarkPosition.TopLeft)]
[InlineData("Dev", 0.5, WatermarkPosition.TopRight)]
[InlineData("Dev", 1.0, WatermarkPosition.TopRight)]
[InlineData("Stage", 0.5, WatermarkPosition.TopRight)]
[InlineData("Something long", 1.0, WatermarkPosition.TopRight)]
[InlineData("Dev", 0.5, WatermarkPosition.Top)]
[InlineData("Dev", 1.0, WatermarkPosition.Top)]
[InlineData("Stage", 0.5, WatermarkPosition.Top)]
[InlineData("Something long", 1.0, WatermarkPosition.Top)]
public void AppliesTextBanner(string text, double scale, WatermarkPosition position)
{
var config = GetConfiguration();
config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(text), text), (nameof(scale), scale));
var config = GetConfiguration();
config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(text), text), (nameof(scale), scale), (nameof(position), position));
var generator = new ImageResizeGenerator(config);

var image = new OutputImage
Expand All @@ -191,12 +211,13 @@ public void AppliesTextBanner(string text, double scale)
ShouldBeVisible = true,
Watermark = new WatermarkConfiguration
{
Text = text
Text = text,
Position = position
}
};

generator.ProcessImage(image);

generator.ProcessImage(image);
VerifyImageContents(image);
}

Expand All @@ -209,10 +230,10 @@ public void AppliesTextBanner(string text, double scale)
[InlineData("dotnetbot.svg", .5)]
public void AppliesPadding(string inputFile, double paddingFactor)
{
var config = GetConfiguration();
var config = GetConfiguration();
config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(paddingFactor), paddingFactor), (nameof(inputFile), inputFile));
var generator = new ImageResizeGenerator(config);

var generator = new ImageResizeGenerator(config);
var image = new OutputImage
{
Height = 0,
Expand All @@ -231,50 +252,50 @@ public void AppliesPadding(string inputFile, double paddingFactor)

var ex = Record.Exception(() => generator.ProcessImage(image));

Assert.Null(ex);

Assert.Null(ex);
VerifyImageContents(image);
}

private static string GetOutputDirectorySuffix(params (string, object)[] values)
{
var builder = new StringBuilder();

foreach (var value in values)
{
var prefix = "and";
if (builder.Length == 0)
{
prefix = "-with";
}

builder.Append($"{prefix}-{value.Item1}-of-{value.Item2}-");
}

return builder.ToString();
}

private void VerifyImageContents(OutputImage image)
{
var expectedFilePath = Path.Combine(TestConstants.ExpectedImageDirectory, image.OutputFile);
var outputFilePath = image.OutputFile;

Assert.True(File.Exists(expectedFilePath), $"Expected image file '{expectedFilePath}' does not exist");
Assert.True(File.Exists(outputFilePath), $"Resulting image file '{outputFilePath}' does not exist");

}
private static string GetOutputDirectorySuffix(params (string, object)[] values)
{
var builder = new StringBuilder();
foreach (var value in values)
{
var prefix = "and";
if (builder.Length == 0)
{
prefix = "-with";
}
builder.Append($"-{prefix}-{value.Item1}-of-{value.Item2}");
}
return builder.ToString();
}
private void VerifyImageContents(OutputImage image)
{
var expectedFilePath = Path.Combine(TestConstants.ExpectedImageDirectory, image.OutputFile);
var outputFilePath = image.OutputFile;
Assert.True(File.Exists(expectedFilePath), $"Expected image file '{expectedFilePath}' does not exist");
Assert.True(File.Exists(outputFilePath), $"Resulting image file '{outputFilePath}' does not exist");
using var expectedImage = SKBitmap.Decode(expectedFilePath);
using var outputImage = SKBitmap.Decode(outputFilePath);

Assert.Equal(expectedImage.Width, outputImage.Width);
using var outputImage = SKBitmap.Decode(outputFilePath);
Assert.Equal(expectedImage.Width, outputImage.Width);
Assert.Equal(expectedImage.Height, outputImage.Height);

for (var y = 0; y < expectedImage.Height; ++y)
{
for (var x = 0; x < expectedImage.Width; ++x)
{
{
Assert.Equal(expectedImage.GetPixel(x, y), outputImage.GetPixel(x, y));
}
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Diff not rendered.