6
6
using System . IO ;
7
7
using System . Runtime . InteropServices ;
8
8
using System . Runtime . Versioning ;
9
+ using SkiaWmfRenderer ;
9
10
10
11
namespace DotNetCampus . MediaConverters . Imaging . Optimizations ;
11
12
@@ -49,7 +50,10 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
49
50
{
50
51
var file = context . ImageFile ;
51
52
52
- // 在 Linux 上,先尝试使用 Inkscape 进行转换,如失败,再使用 libwmf 进行转换
53
+ // 在 Linux 上,先尝试使用 Inkscape 进行转换,如失败,
54
+ // 先尝试 Oxage.Wmf 的 SkiaWmfRenderer 进行转换,如果发现不能很好支持,
55
+ // 再使用 libwmf 进行转换
56
+
53
57
// 调用 Inkscape 进行转换
54
58
ImageFileOptimizationResult result = ConvertWithInkscape ( context ) ;
55
59
if ( result . OptimizedImageFile is { } svgFile )
@@ -61,7 +65,7 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
61
65
// 失败了,没关系,继续使用 libwmf 进行转换
62
66
}
63
67
64
- // 继续执行 libwmf 的转换,此时不支持 emf 格式
68
+ // 继续执行 Oxage.Wmf 或 libwmf 的转换,此时不支持 emf 格式
65
69
if ( string . Equals ( file . Extension , ".emf" ) )
66
70
{
67
71
context . LogMessage ( $ "Convert emf to png is not supported with libwmf. File:'{ context . ImageFile . FullName } '") ;
@@ -73,9 +77,21 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
73
77
} ;
74
78
}
75
79
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
+
76
92
// 使用 libwmf 进行转换
77
93
78
- result = ConvertWithInkscapeLibWmf ( context ) ;
94
+ result = ConvertWithLibWmf ( context ) ;
79
95
if ( result . OptimizedImageFile is { } svgLibWmfFile )
80
96
{
81
97
return ConvertSvgToPngFile ( svgLibWmfFile ) ;
@@ -118,8 +134,42 @@ ImageFileOptimizationResult ConvertSvgToPngFile(FileInfo svgImageFile)
118
134
}
119
135
}
120
136
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
+
121
171
[ SupportedOSPlatform ( "linux" ) ]
122
- private static ImageFileOptimizationResult ConvertWithInkscapeLibWmf ( ImageFileOptimizationContext context )
172
+ private static ImageFileOptimizationResult ConvertWithLibWmf ( ImageFileOptimizationContext context )
123
173
{
124
174
var file = context . ImageFile ;
125
175
var workingFolder = context . WorkingFolder ;
0 commit comments