diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6753a42..de32d82 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -33,6 +33,6 @@ jobs: - name: Process test results if: ${{ success() || failure() }} shell: pwsh - run: ./eng/process-test-results.ps1 + run: ./eng/process-test-results.ps1 -Branch "${{ github.head_ref || github.ref_name }}" env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/eng/process-test-results.ps1 b/eng/process-test-results.ps1 index ebcf13e..baf91bb 100644 --- a/eng/process-test-results.ps1 +++ b/eng/process-test-results.ps1 @@ -1,5 +1,6 @@ [CmdletBinding(PositionalBinding=$false)] param ( + $branch = $null, [switch]$dry = $false ) @@ -8,7 +9,8 @@ $ErrorActionPreference = "Stop" # If snapshots are changed, create a pull request. if (git status --porcelain) { - $currentBranch = $(git branch --show-current) + $currentBranch = $branch ?? $(git branch --show-current) + Write-Output "Branch: '$currentBranch'" # Avoid making new PR against snapshot-updating PR. if ($currentBranch.StartsWith("update-snapshots")) { @@ -23,14 +25,10 @@ if (git status --porcelain) { git branch -D update-snapshots/$currentBranch git checkout -b update-snapshots/$currentBranch - try { - git commit -am "Update snapshots" - git push -f origin update-snapshots/$currentBranch - gh pr create --base $currentBranch --title "Update snapshots" ` - --body "Generate automatically by script ``process-test-results.ps1``." - } finally { - git checkout $currentBranch - } + git commit -am "Update snapshots" + git push -f origin update-snapshots/$currentBranch + gh pr create --base $currentBranch --title "Update snapshots" ` + --body "Generated automatically by script ``process-test-results.ps1``." } else { Write-Output "No changes in snapshots" } diff --git a/src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs b/src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs index 3f7b339..ac10789 100644 --- a/src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs +++ b/src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs @@ -12,16 +12,20 @@ public sealed class SkGraphicEngine : IGraphicEngine private readonly SKCanvas canvas; private readonly SKColor defaultColor; private readonly SKPaint textPaint; + private readonly SKTypeface? typeface; + private readonly SKFont font; private readonly WordCloudInput wordCloud; private bool bitmapExtracted; private SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud, - SKPaint textPaint) + SKPaint textPaint, SKTypeface? typeface = null) { Sizer = sizer; this.wordCloud = wordCloud; defaultColor = textPaint.Color; this.textPaint = textPaint; + this.typeface = typeface; + font = typeface is null ? new SKFont() : new SKFont(typeface); Bitmap = new SKBitmap(wordCloud.Width, wordCloud.Height); canvas = new SKCanvas(Bitmap); } @@ -36,9 +40,10 @@ public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud, textPaint = new SKPaint { Color = defaultColor, - Typeface = font, IsAntialias = antialias }; + typeface = font; + this.font = font is null ? new SKFont() : new SKFont(font); this.wordCloud = wordCloud; } @@ -48,9 +53,8 @@ public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud, public RectangleD Measure(string text, int count) { - textPaint.TextSize = (float)Sizer.GetFontSize(count); - SKRect rect = new SKRect(); - textPaint.MeasureText(text, ref rect); + font.Size = (float)Sizer.GetFontSize(count); + font.MeasureText(text, out SKRect rect); var m = wordCloud.ItemMargin; return new RectangleD(rect.Left + m, rect.Top + m, rect.Width + 2 * m, rect.Height + 2 * m); } @@ -59,7 +63,7 @@ public void Draw(PointD location, RectangleD measured, string text, int count, s { // For computation explanation, see // https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/basics/text. - textPaint.TextSize = (float)Sizer.GetFontSize(count); + font.Size = (float)Sizer.GetFontSize(count); if (colorHex != null) { textPaint.Color = SKColor.Parse(colorHex); @@ -69,14 +73,14 @@ public void Draw(PointD location, RectangleD measured, string text, int count, s textPaint.Color = defaultColor; } canvas.DrawText(text, (float)(location.X - measured.Left), - (float)(location.Y - measured.Top), textPaint); + (float)(location.Y - measured.Top), font, textPaint); } public IGraphicEngine Clone() { var clonedTextPaint = textPaint.Clone(); clonedTextPaint.Color = defaultColor; - return new SkGraphicEngine(Sizer, wordCloud, clonedTextPaint); + return new SkGraphicEngine(Sizer, wordCloud, clonedTextPaint, typeface); } public SKBitmap ExtractBitmap() @@ -87,6 +91,7 @@ public SKBitmap ExtractBitmap() public void Dispose() { + font.Dispose(); textPaint.Dispose(); canvas.Dispose(); if (!bitmapExtracted) diff --git a/src/KnowledgePicker.WordCloud/KnowledgePicker.WordCloud.csproj b/src/KnowledgePicker.WordCloud/KnowledgePicker.WordCloud.csproj index 809b232..d6a06d8 100644 --- a/src/KnowledgePicker.WordCloud/KnowledgePicker.WordCloud.csproj +++ b/src/KnowledgePicker.WordCloud/KnowledgePicker.WordCloud.csproj @@ -28,7 +28,7 @@ - + diff --git a/test/KnowledgePicker.WordCloud.Tests/Assets/example.png b/test/KnowledgePicker.WordCloud.Tests/Assets/example.png index 556a1d3..daf57d6 100644 Binary files a/test/KnowledgePicker.WordCloud.Tests/Assets/example.png and b/test/KnowledgePicker.WordCloud.Tests/Assets/example.png differ diff --git a/test/KnowledgePicker.WordCloud.Tests/Assets/random-colorizer.png b/test/KnowledgePicker.WordCloud.Tests/Assets/random-colorizer.png index 1c5ac49..e87fe04 100644 Binary files a/test/KnowledgePicker.WordCloud.Tests/Assets/random-colorizer.png and b/test/KnowledgePicker.WordCloud.Tests/Assets/random-colorizer.png differ diff --git a/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer-purple-fallback.png b/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer-purple-fallback.png index 5cee1ef..2699ceb 100644 Binary files a/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer-purple-fallback.png and b/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer-purple-fallback.png differ diff --git a/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer-random-fallback.png b/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer-random-fallback.png index 625e65a..3e95427 100644 Binary files a/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer-random-fallback.png and b/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer-random-fallback.png differ diff --git a/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer.png b/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer.png index 773da59..6ed98b5 100644 Binary files a/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer.png and b/test/KnowledgePicker.WordCloud.Tests/Assets/specific-colorizer.png differ diff --git a/test/KnowledgePicker.WordCloud.Tests/KnowledgePicker.WordCloud.Tests.csproj b/test/KnowledgePicker.WordCloud.Tests/KnowledgePicker.WordCloud.Tests.csproj index 485efab..e54effc 100644 --- a/test/KnowledgePicker.WordCloud.Tests/KnowledgePicker.WordCloud.Tests.csproj +++ b/test/KnowledgePicker.WordCloud.Tests/KnowledgePicker.WordCloud.Tests.csproj @@ -23,7 +23,7 @@ - +