Skip to content

Commit f337d81

Browse files
committed
Align report renderer SkiaSharp Linux assets with managed runtime
1 parent 9db9a35 commit f337d81

9 files changed

Lines changed: 56 additions & 36 deletions

File tree

‎Directory.Build.props‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<AnalysisMode>Recommended</AnalysisMode>
1313
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1414
<NoWarn>$(NoWarn);CS1591;MAAI001</NoWarn>
15-
<Version>1.0.6</Version>
15+
<Version>1.0.7</Version>
1616
<PackageVersion>$(Version)</PackageVersion>
1717
</PropertyGroup>
1818

‎Directory.Packages.props‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<ItemGroup>
1212
<PackageVersion Include="DocumentFormat.OpenXml" Version="3.5.1" />
1313
<PackageVersion Include="PdfPig" Version="0.1.16" />
14-
<PackageVersion Include="SkiaSharp" Version="3.119.4" />
15-
<PackageVersion Include="SkiaSharp.NativeAssets.Linux" Version="3.119.4" />
14+
<PackageVersion Include="SkiaSharp" Version="4.151.2" />
15+
<PackageVersion Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="4.151.2" />
1616
<PackageVersion Include="coverlet.collector" Version="10.0.1" />
1717
<PackageVersion Include="coverlet.msbuild" Version="10.0.1" />
1818
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="2.0.5" />

‎src/ManagedCode.FileContext/Documents/Reports/FileContextReportChart.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static void DrawCartesian(SKCanvas canvas, SKRect bounds, JsonElement co
114114
}
115115

116116
private static void DrawAxisLabels(SKCanvas canvas, SKRect bounds, SKRect plot,
117-
JsonElement content, SKPaint axis)
117+
JsonElement content, ReportPaint axis)
118118
{
119119
var xAxis = ReportJson.Text(content, "xAxis");
120120
var yAxis = ReportJson.Text(content, "yAxis");

‎src/ManagedCode.FileContext/Documents/Reports/FileContextReportPdf.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal void NewPage()
7070
{
7171
using var logo = SKBitmap.Decode(Convert.FromBase64String(theme.LogoBase64))
7272
?? throw new ArgumentException("Theme logo must be PNG or JPEG.");
73-
canvas.DrawBitmap(logo, new SKRect(Margin, 26, Margin + 31, 57));
73+
canvas.DrawBitmap(logo, new SKRect(Margin, 26, Margin + 31, 57), new SKSamplingOptions(SKFilterMode.Linear));
7474
}
7575
else
7676
{
@@ -170,7 +170,7 @@ private void DrawImage(JsonElement content)
170170
using var caption = ReportCanvas.Paint(ReportCanvas.Parse(theme.Muted), 9);
171171
var lines = ReportCanvas.Wrap(ReportJson.Text(content, "caption"), caption, pageWidth - Margin * 2);
172172
Ensure(height + lines.Count * 14 + 12);
173-
canvas.DrawBitmap(image, new SKRect(Margin, Y, pageWidth - Margin, Y + height));
173+
canvas.DrawBitmap(image, new SKRect(Margin, Y, pageWidth - Margin, Y + height), new SKSamplingOptions(SKFilterMode.Linear));
174174
Y += height + 5;
175175
foreach (var line in lines)
176176
{

‎src/ManagedCode.FileContext/Documents/Reports/ReportCanvas.cs‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
namespace ManagedCode.FileContext;
88

9-
// Skia 3 retains these text overloads; keep their compatibility use in one adapter.
10-
#pragma warning disable CS0618
119
internal static class ReportCanvas
1210
{
1311
private const float BoldStrokeWidth = .35f;
@@ -19,15 +17,14 @@ internal static class ReportCanvas
1917
return SKTypeface.FromStream(stream) ?? throw new InvalidOperationException("The bundled report font cannot be loaded.");
2018
});
2119

22-
public static SKPaint Paint(SKColor color, float size = 0, float stroke = 0, bool bold = false) => new()
23-
{
24-
Color = color,
25-
IsAntialias = true,
26-
Typeface = Typeface.Value,
27-
TextSize = size,
28-
Style = StyleFor(bold, stroke),
29-
StrokeWidth = bold ? BoldStrokeWidth : stroke
30-
};
20+
public static ReportPaint Paint(SKColor color, float size = 0, float stroke = 0, bool bold = false) =>
21+
new(new SKPaint
22+
{
23+
Color = color,
24+
IsAntialias = true,
25+
Style = StyleFor(bold, stroke),
26+
StrokeWidth = bold ? BoldStrokeWidth : stroke
27+
}, new SKFont { Typeface = Typeface.Value, Size = size });
3128

3229
private static SKPaintStyle StyleFor(bool bold, float stroke)
3330
{
@@ -41,18 +38,18 @@ public static SKColor Parse(string value)
4138
return color;
4239
}
4340

44-
public static void RequireFits(string value, SKPaint paint, float width, string field)
41+
public static void RequireFits(string value, ReportPaint paint, float width, string field)
4542
{
4643
if (MeasureText(value, paint) > width)
4744
{ throw new ArgumentException($"{field} does not fit the available chart space; use a shorter label or a wider page."); }
4845
}
4946

50-
public static void Text(SKCanvas canvas, string value, float x, float y, SKPaint paint) =>
51-
canvas.DrawText(value, x, y, paint);
47+
public static void Text(SKCanvas canvas, string value, float x, float y, ReportPaint paint) =>
48+
canvas.DrawText(value, x, y, SKTextAlign.Left, paint.Font, paint.ColorPaint);
5249

53-
public static float MeasureText(string value, SKPaint paint) => paint.MeasureText(value);
50+
public static float MeasureText(string value, ReportPaint paint) => paint.Font.MeasureText(value, paint.ColorPaint);
5451

55-
public static IReadOnlyList<string> Wrap(string value, SKPaint paint, float width)
52+
public static IReadOnlyList<string> Wrap(string value, ReportPaint paint, float width)
5653
{
5754
var lines = new List<string>();
5855
foreach (var paragraph in value.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n').Split('\n'))
@@ -61,7 +58,7 @@ public static IReadOnlyList<string> Wrap(string value, SKPaint paint, float widt
6158
foreach (var rune in paragraph.EnumerateRunes())
6259
{
6360
var candidate = current + rune;
64-
if (paint.MeasureText(candidate) > width && current.Length > 0)
61+
if (MeasureText(candidate, paint) > width && current.Length > 0)
6562
{
6663
lines.Add(current);
6764
current = rune.ToString();
@@ -74,5 +71,3 @@ public static IReadOnlyList<string> Wrap(string value, SKPaint paint, float widt
7471
}
7572

7673
}
77-
78-
#pragma warning restore CS0618
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using SkiaSharp;
2+
3+
namespace ManagedCode.FileContext;
4+
5+
internal sealed class ReportPaint(SKPaint colorPaint, SKFont font) : IDisposable
6+
{
7+
public SKPaint ColorPaint { get; } = colorPaint;
8+
public SKFont Font { get; } = font;
9+
10+
public static implicit operator SKPaint(ReportPaint paint) => paint.ColorPaint;
11+
12+
public void Dispose()
13+
{
14+
Font.Dispose();
15+
ColorPaint.Dispose();
16+
}
17+
}

‎src/ManagedCode.FileContext/Documents/Reports/ReportPdfKpiPainter.cs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,21 @@ private static void DrawTrend(SKCanvas canvas, float x, float y, string indicato
5757
using var paint = ReportCanvas.Paint(color, 9);
5858
if (indicator is "up" or "down")
5959
{
60-
using var triangle = new SKPath();
60+
using var builder = new SKPathBuilder();
6161
if (indicator is "up")
6262
{
63-
triangle.MoveTo(x, y + 8);
64-
triangle.LineTo(x + 5, y);
65-
triangle.LineTo(x + 10, y + 8);
63+
builder.MoveTo(x, y + 8);
64+
builder.LineTo(x + 5, y);
65+
builder.LineTo(x + 10, y + 8);
6666
}
6767
else
6868
{
69-
triangle.MoveTo(x, y);
70-
triangle.LineTo(x + 5, y + 8);
71-
triangle.LineTo(x + 10, y);
69+
builder.MoveTo(x, y);
70+
builder.LineTo(x + 5, y + 8);
71+
builder.LineTo(x + 10, y);
7272
}
73-
triangle.Close();
73+
builder.Close();
74+
using var triangle = builder.Detach();
7475
canvas.DrawPath(triangle, paint);
7576
x += 16;
7677
}

‎src/ManagedCode.FileContext/ManagedCode.FileContext.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageReference Include="DocumentFormat.OpenXml" />
1414
<PackageReference Include="PdfPig" />
1515
<PackageReference Include="SkiaSharp" />
16-
<PackageReference Include="SkiaSharp.NativeAssets.Linux" />
16+
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" />
1717
<PackageReference Include="ManagedCode.Storage.Core" />
1818
<PackageReference Include="Microsoft.Agents.AI" />
1919
<PackageReference Include="Microsoft.Extensions.AI" />

‎tests/ManagedCode.FileContext.Tests/ReportCreationTests.cs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ public async Task All_chart_types_render_as_png(string chartType)
8989
title = "Channels", chartType, labels = new[] { "Search", "Email" },
9090
series = new[] { new { name = "Visits", values = new[] { 2, 4 } } },
9191
xAxis = "Channel", yAxis = "Visits"
92-
})], ["png"]);
92+
})], ["pdf", "png"]);
9393
var output = await new FileContextService(store, options).Documents.CreateReportAsync(report);
94-
output.Files.Single().MediaType.ShouldBe("image/png");
94+
var png = output.Files.Single(file => string.Equals(file.MediaType, "image/png", StringComparison.Ordinal));
95+
await using var pngStream = await store.OpenReadAsync(png.Path);
96+
var signature = new byte[8];
97+
(await pngStream.ReadAsync(signature)).ShouldBe(8);
98+
signature.ShouldBe(new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 });
99+
await using var pdfStream = await store.OpenReadAsync(output.Files.Single(file => string.Equals(file.MediaType, "application/pdf", StringComparison.Ordinal)).Path);
100+
using var pdf = PdfDocument.Open(pdfStream);
101+
pdf.GetPage(1).Text.ShouldContain("Channels");
95102
}
96103

97104
[Fact]

0 commit comments

Comments
 (0)