Skip to content

Commit c374ff0

Browse files
committed
添加 SkiaWmfRenderer 支持进行图元优化
在 `EnhancedGraphicsMetafileOptimization.cs` 中引入 `SkiaWmfRenderer`,实现 WMF 到 PNG 的转换逻辑。优先使用 `SkiaWmfRenderer` 进行转换,失败后回退到 `libwmf`。新增私有方法 `ConvertWithSkiaWmfRenderer` 处理转换并记录日志。同时,在 `MediaConverters.Lib.csproj` 中添加对 `SkiaWmfRenderer` 的项目引用
1 parent 2937876 commit c374ff0

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/EnhancedGraphicsMetafileOptimization.cs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Runtime.InteropServices;
88
using System.Runtime.Versioning;
9+
using SkiaWmfRenderer;
910

1011
namespace DotNetCampus.MediaConverters.Imaging.Optimizations;
1112

@@ -49,7 +50,10 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
4950
{
5051
var file = context.ImageFile;
5152

52-
// 在 Linux 上,先尝试使用 Inkscape 进行转换,如失败,再使用 libwmf 进行转换
53+
// 在 Linux 上,先尝试使用 Inkscape 进行转换,如失败,
54+
// 先尝试 Oxage.Wmf 的 SkiaWmfRenderer 进行转换,如果发现不能很好支持,
55+
// 再使用 libwmf 进行转换
56+
5357
// 调用 Inkscape 进行转换
5458
ImageFileOptimizationResult result = ConvertWithInkscape(context);
5559
if (result.OptimizedImageFile is { } svgFile)
@@ -61,7 +65,7 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
6165
// 失败了,没关系,继续使用 libwmf 进行转换
6266
}
6367

64-
// 继续执行 libwmf 的转换,此时不支持 emf 格式
68+
// 继续执行 Oxage.Wmf 或 libwmf 的转换,此时不支持 emf 格式
6569
if (string.Equals(file.Extension, ".emf"))
6670
{
6771
context.LogMessage($"Convert emf to png is not supported with libwmf. File:'{context.ImageFile.FullName}'");
@@ -73,9 +77,21 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
7377
};
7478
}
7579

80+
// 使用 SkiaWmfRenderer 进行转换
81+
result = ConvertWithSkiaWmfRenderer(context);
82+
if (result.IsSuccess)
83+
{
84+
return result;
85+
}
86+
else
87+
{
88+
// 失败了,继续使用 libwmf 进行转换
89+
// 使用 libwmf 进行转换时,对于文本绘制支持很弱。这就是为什么优先使用 SkiaWmfRenderer 的原因
90+
}
91+
7692
// 使用 libwmf 进行转换
7793

78-
result = ConvertWithInkscapeLibWmf(context);
94+
result = ConvertWithLibWmf(context);
7995
if (result.OptimizedImageFile is { } svgLibWmfFile)
8096
{
8197
return ConvertSvgToPngFile(svgLibWmfFile);
@@ -118,8 +134,42 @@ ImageFileOptimizationResult ConvertSvgToPngFile(FileInfo svgImageFile)
118134
}
119135
}
120136

137+
/// <summary>
138+
/// 使用自己写的基于 Oxage.Wmf 的 SkiaWmfRenderer 进行转换,可以比较好处理公式内容
139+
/// </summary>
140+
/// <param name="context"></param>
141+
/// <returns></returns>
142+
private static ImageFileOptimizationResult ConvertWithSkiaWmfRenderer(ImageFileOptimizationContext context)
143+
{
144+
int requestWidth = context.MaxImageWidth ?? 0;
145+
int requestHeight = context.MaxImageHeight ?? 0;
146+
147+
var outputPngFile = new FileInfo(Path.Join(context.WorkingFolder.FullName, $"WmfRender_{Path.GetRandomFileName()}.png"));
148+
149+
var file = context.ImageFile;
150+
151+
context.LogMessage($"Start convert wmf to png by SkiaWmfRenderer. File:'{file}'");
152+
153+
if (SkiaWmfRenderHelper.TryConvertToPng(file, outputPngFile, requestWidth, requestHeight) && File.Exists(outputPngFile.FullName))
154+
{
155+
context.LogMessage($"Success converted wmf to png by SkiaWmfRenderer. File:'{file}' Output:'{outputPngFile}'");
156+
157+
return new ImageFileOptimizationResult()
158+
{
159+
OptimizedImageFile = outputPngFile
160+
};
161+
}
162+
163+
context.LogMessage($"Fail convert wmf to png by SkiaWmfRenderer. File:'{file}'");
164+
return new ImageFileOptimizationResult()
165+
{
166+
OptimizedImageFile = null,
167+
FailureReason = ImageFileOptimizationFailureReason.NotSupported
168+
};
169+
}
170+
121171
[SupportedOSPlatform("linux")]
122-
private static ImageFileOptimizationResult ConvertWithInkscapeLibWmf(ImageFileOptimizationContext context)
172+
private static ImageFileOptimizationResult ConvertWithLibWmf(ImageFileOptimizationContext context)
123173
{
124174
var file = context.ImageFile;
125175
var workingFolder = context.WorkingFolder;

src/MediaConverters/MediaConverters.Lib/MediaConverters.Lib.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<PackageReference Include="System.Drawing.Common" Version="9.0.7" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<ProjectReference Include="..\SkiaWmfRenderer\src\SkiaWmfRenderer\SkiaWmfRenderer.csproj" />
22+
</ItemGroup>
23+
2024
<PropertyGroup>
2125
<!-- 不要在 debug 开启 EmbedAllSources 或 EmbedUntrackedSources:
2226
1. NuGet 包会提示包含未追踪的源,但实际列出的未追踪的源是空的(所以其实都已经追踪了?)

0 commit comments

Comments
 (0)