forked from OpenXmlDev/Open-Xml-PowerTools
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Replace ImageSharp with SkiaSharp #151
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
136143f
Bump System.Configuration.ConfigurationManager from 9.0.7 to 9.0.8
dependabot[bot] 3e191de
Merge pull request #148 from Codeuctivity/dependabot/nuget/OpenXmlPow…
stesee b403208
Bump actions/checkout from 4 to 5
dependabot[bot] 13978b3
Merge pull request #149 from Codeuctivity/dependabot/github_actions/a…
stesee 318fd6a
Replace ImageSharp with SkiaSharp
mapo80 264d273
Add exhaustive SkiaSharp tests
mapo80 3ff8f20
Incremented major version to make move from ImageSharp to SkiaSharp t…
stesee 2588592
Merge pull request #150 from mapo80/main
stesee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,21 @@ | ||
| # Build and Test Instructions | ||
|
|
||
| ## Prerequisites | ||
| - .NET 8 SDK (`dotnet-sdk-8.0`) must be installed. | ||
| ```bash | ||
| apt-get update && apt-get install -y dotnet-sdk-8.0 | ||
| ``` | ||
|
|
||
| ## Build | ||
| From the repository root, run: | ||
| ```bash | ||
| dotnet build | ||
| ``` | ||
| This restores NuGet packages and compiles the library and example projects. | ||
|
|
||
| ## Test | ||
| Execute the unit tests with: | ||
| ```bash | ||
| dotnet test | ||
| ``` | ||
| The command builds required projects and runs the test suite. |
This file contains hidden or 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,56 @@ | ||
| using Codeuctivity.OpenXmlPowerTools; | ||
| using SkiaSharp; | ||
| using Xunit; | ||
|
|
||
| namespace Codeuctivity.Tests | ||
| { | ||
| public class ColorParserTests | ||
| { | ||
| [Theory] | ||
| [InlineData("red", 255, 0, 0)] | ||
| [InlineData("RED", 255, 0, 0)] | ||
| [InlineData("#00FF00", 0, 255, 0)] | ||
| [InlineData("#00ff00", 0, 255, 0)] | ||
| [InlineData("blue", 0, 0, 255)] | ||
| [InlineData("#0000FF", 0, 0, 255)] | ||
| [InlineData("#abc", 170, 187, 204)] | ||
| [InlineData("yellow", 255, 255, 0)] | ||
| [InlineData("black", 0, 0, 0)] | ||
| [InlineData("white", 255, 255, 255)] | ||
| public void ShouldParseColors(string input, byte r, byte g, byte b) | ||
| { | ||
| var result = ColorParser.FromName(input); | ||
| Assert.Equal(r, result.Red); | ||
| Assert.Equal(g, result.Green); | ||
| Assert.Equal(b, result.Blue); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("red", true)] | ||
| [InlineData("#123456", true)] | ||
| [InlineData("notacolor", false)] | ||
| [InlineData("", false)] | ||
| public void ShouldValidateColorNames(string input, bool valid) | ||
| { | ||
| Assert.Equal(valid, ColorParser.IsValidName(input)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FromNameShouldThrowOnInvalid() | ||
| { | ||
| Assert.Throws<System.ArgumentException>(() => ColorParser.FromName("bogus")); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("notacolor")] | ||
| [InlineData("#GGGGGG")] | ||
| [InlineData("")] | ||
| [InlineData(null)] | ||
| [InlineData(" ")] | ||
| public void ShouldRejectInvalidColors(string? input) | ||
| { | ||
| var success = ColorParser.TryFromName(input, out SKColor _); | ||
| Assert.False(success); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,45 @@ | ||
| using Codeuctivity.OpenXmlPowerTools; | ||
| using SkiaSharp; | ||
| using Xunit; | ||
|
|
||
| namespace Codeuctivity.Tests | ||
| { | ||
| public class CssPropertyValueTests | ||
| { | ||
| [Fact] | ||
| public void ShouldRecognizeNamedColor() | ||
| { | ||
| var value = new CssPropertyValue { Type = CssValueType.String, Value = "red" }; | ||
| Assert.True(value.IsColor); | ||
| var color = value.ToColor(); | ||
| Assert.Equal(SKColors.Red, color); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldRecognizeHexColor() | ||
| { | ||
| var value = new CssPropertyValue { Type = CssValueType.Hex, Value = "#0000FF" }; | ||
| Assert.True(value.IsColor); | ||
| var color = value.ToColor(); | ||
| Assert.Equal(SKColors.Blue, color); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldRejectNonColor() | ||
| { | ||
| var value = new CssPropertyValue { Type = CssValueType.String, Value = "1234" }; | ||
| Assert.False(value.IsColor); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldParseHexWithoutHash() | ||
| { | ||
| var value = new CssPropertyValue { Type = CssValueType.Hex, Value = "00FF00" }; | ||
| Assert.True(value.IsColor); | ||
| var color = value.ToColor(); | ||
| Assert.Equal(0u, color.Red); | ||
| Assert.Equal(255u, color.Green); | ||
| Assert.Equal(0u, color.Blue); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,93 @@ | ||
| using System.IO; | ||
| using System.Xml.Linq; | ||
| using Codeuctivity.OpenXmlPowerTools; | ||
| using Codeuctivity.OpenXmlPowerTools.OpenXMLWordprocessingMLToHtmlConverter; | ||
| using SkiaSharp; | ||
| using Xunit; | ||
|
|
||
| namespace Codeuctivity.Tests | ||
| { | ||
| public class ImageHandlerTests | ||
| { | ||
| [Theory] | ||
| [InlineData(SKEncodedImageFormat.Png, "image/png")] | ||
| [InlineData(SKEncodedImageFormat.Jpeg, "image/jpeg")] | ||
| [InlineData(SKEncodedImageFormat.Webp, "image/webp")] | ||
| public void ShouldTransformImagesToDataUri(SKEncodedImageFormat format, string mime) | ||
| { | ||
| using var surface = SKSurface.Create(new SKImageInfo(10, 10)); | ||
| surface.Canvas.Clear(SKColors.Red); | ||
| using var image = surface.Snapshot(); | ||
| using var ms = new MemoryStream(); | ||
| using (var data = image.Encode(format, 100)) | ||
| { | ||
| data.SaveTo(ms); | ||
| } | ||
| ms.Position = 0; | ||
| var info = new ImageInfo { Image = ms, AltText = "alt", ImgStyleAttribute = new XAttribute(NoNamespace.style, "width:10px") }; | ||
| var handler = new ImageHandler(); | ||
| var result = handler.TransformImage(info); | ||
| var srcAttr = result.Attribute(NoNamespace.src); | ||
| Assert.NotNull(srcAttr); | ||
| Assert.StartsWith($"data:{mime};base64,", srcAttr.Value); | ||
| Assert.Equal("width:10px", result.Attribute(NoNamespace.style)?.Value); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldTransformGifToDataUri() | ||
| { | ||
| var gif = System.Convert.FromBase64String("R0lGODlhAQABAPAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=="); | ||
| using var ms = new MemoryStream(gif); | ||
| var info = new ImageInfo { Image = ms }; | ||
| var handler = new ImageHandler(); | ||
| var result = handler.TransformImage(info); | ||
| var srcAttr = result.Attribute(NoNamespace.src); | ||
| Assert.NotNull(srcAttr); | ||
| Assert.StartsWith("data:image/gif;base64,", srcAttr.Value); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldThrowOnInvalidImage() | ||
| { | ||
| using var ms = new MemoryStream(new byte[] { 1, 2, 3, 4 }); | ||
| var handler = new ImageHandler(); | ||
| Assert.ThrowsAny<System.Exception>(() => handler.TransformImage(new ImageInfo { Image = ms })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldIncludeAltText() | ||
| { | ||
| using var surface = SKSurface.Create(new SKImageInfo(5, 5)); | ||
| surface.Canvas.Clear(SKColors.Blue); | ||
| using var image = surface.Snapshot(); | ||
| using var ms = new MemoryStream(); | ||
| using (var data = image.Encode(SKEncodedImageFormat.Png, 100)) | ||
| { | ||
| data.SaveTo(ms); | ||
| } | ||
| ms.Position = 0; | ||
| var info = new ImageInfo { Image = ms, AltText = "demo" }; | ||
| var handler = new ImageHandler(); | ||
| var result = handler.TransformImage(info); | ||
| Assert.Equal("demo", result.Attribute(NoNamespace.alt)?.Value); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldOmitAltTextWhenNotProvided() | ||
| { | ||
| using var surface = SKSurface.Create(new SKImageInfo(5, 5)); | ||
| surface.Canvas.Clear(SKColors.Blue); | ||
| using var image = surface.Snapshot(); | ||
| using var ms = new MemoryStream(); | ||
| using (var data = image.Encode(SKEncodedImageFormat.Png, 100)) | ||
| { | ||
| data.SaveTo(ms); | ||
| } | ||
| ms.Position = 0; | ||
| var info = new ImageInfo { Image = ms }; | ||
| var handler = new ImageHandler(); | ||
| var result = handler.TransformImage(info); | ||
| Assert.Null(result.Attribute(NoNamespace.alt)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,22 +1,44 @@ | ||
| using SixLabors.ImageSharp; | ||
| using SkiaSharp; | ||
| using System; | ||
| using System.Drawing; | ||
|
|
||
| namespace Codeuctivity.OpenXmlPowerTools | ||
| { | ||
| public static class ColorParser | ||
| { | ||
| public static Color FromName(string name) | ||
| public static SKColor FromName(string name) | ||
| { | ||
| return Color.Parse(name); | ||
| if (!TryFromName(name, out var color)) | ||
| { | ||
| throw new ArgumentException("Invalid color name", nameof(name)); | ||
| } | ||
| return color; | ||
| } | ||
|
|
||
| public static bool TryFromName(string? name, out Color color) | ||
| public static bool TryFromName(string? name, out SKColor color) | ||
| { | ||
| return Color.TryParse(name, out color); | ||
| if (string.IsNullOrWhiteSpace(name)) | ||
| { | ||
| color = default; | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var drawingColor = ColorTranslator.FromHtml(name); | ||
| color = new SKColor(drawingColor.R, drawingColor.G, drawingColor.B, drawingColor.A); | ||
| return true; | ||
| } | ||
| catch | ||
| { | ||
| color = default; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static bool IsValidName(string name) | ||
| { | ||
| return TryFromName(name, out _); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a dependency on System.Drawing may introduce platform compatibility issues, as System.Drawing is Windows-specific and not recommended for cross-platform applications. Consider using a more portable color parsing solution or implementing custom color name parsing.