66using System . IO ;
77using System . Runtime . InteropServices ;
88using System . Runtime . Versioning ;
9+ using SkiaWmfRenderer ;
910
1011namespace 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 ;
0 commit comments