Skip to content

Commit 8a3923c

Browse files
committed
完成拆分 Skia 到独立可库引用的项目
1 parent 369e109 commit 8a3923c

1 file changed

Lines changed: 91 additions & 40 deletions

File tree

src/MediaConverters/SkiaWmfRenderer/src/SkiaWmfRenderer/Optimizations/EnhancedGraphicsMetafileOptimization.cs

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,93 @@
77
using System;
88
using System.ComponentModel;
99
using System.Diagnostics;
10+
using System.Diagnostics.CodeAnalysis;
1011
using System.Drawing;
1112
using System.Drawing.Imaging;
1213
using System.IO;
1314
using System.Runtime.InteropServices;
1415
using System.Runtime.Versioning;
1516
using System.Xml.Linq;
17+
using ImageFileOptimizationContext = DotNetCampus.MediaConverters.Imaging.Optimizations.EnhancedGraphicsMetafileOptimizationContext;
18+
using ImageFileOptimizationResult = DotNetCampus.MediaConverters.Imaging.Optimizations.EnhancedGraphicsMetafileOptimizationResult;
1619

1720
namespace DotNetCampus.MediaConverters.Imaging.Optimizations;
1821

22+
/// <summary>
23+
/// 图片文件优化上下文信息
24+
/// </summary>
25+
public readonly record struct EnhancedGraphicsMetafileOptimizationContext()
26+
{
27+
public string TraceId { get; init; } = Guid.NewGuid().ToString("N");
28+
29+
public required FileInfo ImageFile { get; init; }
30+
public required DirectoryInfo WorkingFolder { get; init; }
31+
public required int? MaxImageWidth { get; init; }
32+
public required int? MaxImageHeight { get; init; } = null;
33+
34+
public bool ShouldLogToConsole { get; init; } = false;
35+
36+
public bool ShouldLogToFile { get; init; } = false;
37+
38+
public string LogFileName { get; init; } = "Log.txt";
39+
40+
public void LogMessage(string message)
41+
{
42+
if (!ShouldLogToConsole && !ShouldLogToFile)
43+
{
44+
return;
45+
}
46+
47+
if (ShouldLogToConsole)
48+
{
49+
Console.WriteLine(message);
50+
}
51+
52+
if (ShouldLogToFile)
53+
{
54+
var logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss,fff}][{TraceId}] {message}";
55+
56+
var logFile = Path.Join(WorkingFolder.FullName, LogFileName ?? "Log.txt");
57+
58+
File.AppendAllLines(logFile, [logMessage]);
59+
}
60+
}
61+
}
62+
63+
/// <summary>
64+
/// 图片文件优化结果
65+
/// </summary>
66+
public readonly record struct EnhancedGraphicsMetafileOptimizationResult()
67+
{
68+
[MemberNotNullWhen(returnValue: true)]
69+
public bool IsSuccess => OptimizedImageFile is not null;
70+
71+
/// <summary>
72+
/// 优化后的图片文件
73+
/// </summary>
74+
public FileInfo? OptimizedImageFile { get; init; }
75+
76+
public bool IsNotSupport { get; init; }
77+
78+
public Exception? Exception { get; init; }
79+
80+
public static ImageFileOptimizationResult NotSupported()
81+
{
82+
return new EnhancedGraphicsMetafileOptimizationResult()
83+
{
84+
IsNotSupport = true,
85+
};
86+
}
87+
88+
public static ImageFileOptimizationResult FailException(Exception exception)
89+
{
90+
return new EnhancedGraphicsMetafileOptimizationResult()
91+
{
92+
Exception = exception,
93+
};
94+
}
95+
}
96+
1997
/// <summary>
2098
/// 增强图元优化方法,用于优化 emf 和 wmf 图片
2199
/// </summary>
@@ -44,11 +122,7 @@ public static ImageFileOptimizationResult ConvertWmfOrEmfToPngFile(ImageFileOpti
44122
}
45123
}
46124

47-
return new ImageFileOptimizationResult()
48-
{
49-
OptimizedImageFile = null,
50-
FailureReason = ImageFileOptimizationFailureReason.NotSupported
51-
};
125+
return ImageFileOptimizationResult.NotSupported();
52126
}
53127

54128
[SupportedOSPlatform("linux")]
@@ -76,11 +150,7 @@ private static ImageFileOptimizationResult ConvertInLinux(ImageFileOptimizationC
76150
{
77151
context.LogMessage($"Convert emf to png is not supported with libwmf. File:'{context.ImageFile.FullName}'");
78152

79-
return new ImageFileOptimizationResult()
80-
{
81-
OptimizedImageFile = null,
82-
FailureReason = ImageFileOptimizationFailureReason.NotSupported
83-
};
153+
return ImageFileOptimizationResult.NotSupported();
84154
}
85155

86156
// 使用 SkiaWmfRenderer 进行转换
@@ -111,7 +181,7 @@ ImageFileOptimizationResult ConvertSvgToPngFile(FileInfo svgImageFile)
111181
{
112182
try
113183
{
114-
var convertSvgToPngFile = ImageFileOptimization.ConvertSvgToPngFile(context with
184+
var convertSvgToPngFile = SvgFileOptimization.ConvertSvgToPngFile(context with
115185
{
116186
ImageFile = svgImageFile
117187
});
@@ -124,11 +194,7 @@ ImageFileOptimizationResult ConvertSvgToPngFile(FileInfo svgImageFile)
124194
}
125195
else
126196
{
127-
return new ImageFileOptimizationResult()
128-
{
129-
OptimizedImageFile = null,
130-
FailureReason = ImageFileOptimizationFailureReason.NotSupported
131-
};
197+
return ImageFileOptimizationResult.NotSupported();
132198
}
133199
}
134200
catch (Exception e)
@@ -173,11 +239,7 @@ private static ImageFileOptimizationResult ConvertWithSkiaWmfRenderer(ImageFileO
173239
}
174240

175241
context.LogMessage($"Fail convert wmf to png by SkiaWmfRenderer. File:'{file}'");
176-
return new ImageFileOptimizationResult()
177-
{
178-
OptimizedImageFile = null,
179-
FailureReason = ImageFileOptimizationFailureReason.NotSupported
180-
};
242+
return ImageFileOptimizationResult.NotSupported();
181243
}
182244

183245
[SupportedOSPlatform("linux")]
@@ -193,6 +255,7 @@ private static ImageFileOptimizationResult ConvertWithLibWmf(ImageFileOptimizati
193255

194256
context.LogMessage($"Start convert wmf to svg by libwmf. File:'{file}' wmf2svg='{wmf2svgFile}'");
195257

258+
#if NET7_0_OR_GREATER
196259
try
197260
{
198261
File.SetUnixFileMode(wmf2svgFile, UnixFileMode.UserExecute);
@@ -201,6 +264,7 @@ private static ImageFileOptimizationResult ConvertWithLibWmf(ImageFileOptimizati
201264
{
202265
context.LogMessage($"File.SetUnixFileMode +x Fail. wmf2svgFile='{wmf2svgFile}'. Exception: {e}");
203266
}
267+
#endif
204268

205269
// ./wmf2svg -o 1.svg image.wmf
206270
var processStartInfo = new ProcessStartInfo(wmf2svgFile)
@@ -226,12 +290,12 @@ private static ImageFileOptimizationResult ConvertWithLibWmf(ImageFileOptimizati
226290
try
227291
{
228292
using var process = Process.Start(processStartInfo);
229-
process?.WaitForExit(TimeSpan.FromSeconds(5));
293+
process?.WaitForExit(5000);
230294
if (process?.ExitCode == 0 && File.Exists(svgFile))
231295
{
232296
// 转换成功,再次执行 SVG 转 PNG 的转换
233297
// 由于可能存在 SVG 文件中包含无效字符的问题,因此需要修复一下
234-
var convertedFile = ImageFileOptimization.FixSvgInvalidCharacter(context with
298+
var convertedFile = SvgFileOptimization.FixSvgInvalidCharacter(context with
235299
{
236300
ImageFile = new FileInfo(svgFile)
237301
});
@@ -253,11 +317,7 @@ private static ImageFileOptimizationResult ConvertWithLibWmf(ImageFileOptimizati
253317
return ImageFileOptimizationResult.FailException(e);
254318
}
255319

256-
return new ImageFileOptimizationResult()
257-
{
258-
OptimizedImageFile = null,
259-
FailureReason = ImageFileOptimizationFailureReason.NotSupported,
260-
};
320+
return ImageFileOptimizationResult.NotSupported();
261321
}
262322

263323
[SupportedOSPlatform("linux")]
@@ -282,7 +342,7 @@ private static ImageFileOptimizationResult ConvertWithInkscape(ImageFileOptimiza
282342
try
283343
{
284344
using var process = Process.Start(processStartInfo);
285-
process?.WaitForExit(TimeSpan.FromSeconds(5));
345+
process?.WaitForExit(5000);
286346
if (process?.ExitCode == 0 && File.Exists(svgFile))
287347
{
288348
// 转换成功,再次执行 SVG 转 PNG 的转换
@@ -314,11 +374,7 @@ private static ImageFileOptimizationResult ConvertWithInkscape(ImageFileOptimiza
314374
return ImageFileOptimizationResult.FailException(e);
315375
}
316376

317-
return new ImageFileOptimizationResult()
318-
{
319-
OptimizedImageFile = null,
320-
FailureReason = ImageFileOptimizationFailureReason.NotSupported,
321-
};
377+
return ImageFileOptimizationResult.NotSupported();
322378
}
323379

324380
[SupportedOSPlatform("windows6.1")]
@@ -346,12 +402,7 @@ private static ImageFileOptimizationResult ConvertInWindows(ImageFileOptimizatio
346402
{
347403
context.LogMessage($"Fail convert emf or wmf to png by GDI. File:'{file}' Exception:{e}");
348404

349-
return new ImageFileOptimizationResult
350-
{
351-
OptimizedImageFile = null,
352-
Exception = e,
353-
FailureReason = ImageFileOptimizationFailureReason.GdiException
354-
};
405+
return ImageFileOptimizationResult.FailException(e);
355406
}
356407
}
357408
}

0 commit comments

Comments
 (0)