Skip to content

Commit

Permalink
Update SkiaSharp to v3 (major) (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz authored Dec 3, 2024
2 parents 9ba6dfb + 2011db5 commit d5fb935
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
16 changes: 7 additions & 9 deletions eng/process-test-results.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[CmdletBinding(PositionalBinding=$false)]
param (
$branch = $null,
[switch]$dry = $false
)

Expand All @@ -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")) {
Expand All @@ -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"
}
21 changes: 13 additions & 8 deletions src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ public sealed class SkGraphicEngine : IGraphicEngine<SKBitmap>
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);
}
Expand All @@ -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;
}

Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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<SKBitmap> Clone()
{
var clonedTextPaint = textPaint.Clone();
clonedTextPaint.Color = defaultColor;
return new SkGraphicEngine(Sizer, wordCloud, clonedTextPaint);
return new SkGraphicEngine(Sizer, wordCloud, clonedTextPaint, typeface);
}

public SKBitmap ExtractBitmap()
Expand All @@ -87,6 +91,7 @@ public SKBitmap ExtractBitmap()

public void Dispose()
{
font.Dispose();
textPaint.Dispose();
canvas.Dispose();
if (!bitmapExtracted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.9" />
<PackageReference Include="SkiaSharp" Version="3.116.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Binary file modified test/KnowledgePicker.WordCloud.Tests/Assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/KnowledgePicker.WordCloud.Tests/Assets/random-colorizer.png
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.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\KnowledgePicker.WordCloud\KnowledgePicker.WordCloud.csproj" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.9" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.116.0" />
</ItemGroup>

</Project>

0 comments on commit d5fb935

Please sign in to comment.