1
0
Fork 0
Everything2Everything/src/Everything2Everything.Core/Converters/HtmlProvider.cs
Yun Chan ab126a001a feat(ui): Windows 11 정통 네이티브 리디자인 + Google AdMob 광고 연동
- Mica 베이스 + 왼쪽 위 8px 라운드 콘텐츠 레이어 (탐색기·설정 앱 2계층 구조)
- 시스템 라이트/다크·강조색 자동 추종 (FsThemeService, FsPalette.Light/Dark)
- 강조색은 시스템 강조색에서 WCAG AA 대비를 보정해 파생 (FsAccentPalette)
- Segoe UI Variable 타입 램프, 최소 12px, 반경 4/8px, 카드 중첩 제거
- 네온 시안·더블 베젤·그라디언트 버튼·Inter 서체 폐기
- 전 창 적용: 메인·빠른 옵션/진행·설정·도구 설치·진단, 광고는 중립 카드로
- 결함 수정: 사이드바 체크박스 라벨 잘림, 행 체크박스 잘림, 입력란 이중 여백,
  제목 표시줄 아이콘 검게 렌더, '큐'/'대기열' 용어 혼용
- Google AdMob 광고 단위 연동 (AdHtmlBuilder, 설정 창 AdMob 카드)
- Win11NativeDesignTests 계약 테스트, VisualCaptureHarness 시각 E2E 캡처
2026-10-06 02:45:13 +09:00

317 lines
13 KiB
C#

using System.Text.Json;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
using Everything2Everything.Core.Providers;
using ImageMagick;
using Microsoft.Web.WebView2.Core;
namespace Everything2Everything.Core.Converters;
public sealed class HtmlProvider : IConverterProvider
{
private static readonly string[] HtmlInputs = { ".html", ".htm" };
private static readonly string[] HtmlImageOutputs =
{ ".png", ".jpg", ".jpeg", ".webp", ".avif", ".bmp", ".tif", ".tiff" };
public ProviderCapability Capability { get; } = new(
Id: "html",
DisplayName: "HTML / 웹 페이지",
SupportedConversions: ProviderCapability.PairsFromMatrix(HtmlInputs, new[] { ".pdf" }, LossClass.Recode)
.Concat(ProviderCapability.PairsFromMatrix(HtmlInputs, HtmlImageOutputs, LossClass.Rasterize)).ToList(),
Status: ProviderStatus.Available,
Summary: "HTML/HTM을 WebView2로 헤드리스 렌더링하여 이미지 또는 PDF로 저장합니다.",
ExternalDependencies: new[]
{
new ExternalDependency(
Name: "Microsoft Edge WebView2 Runtime",
Description: "Windows 11에는 기본 포함되어 있습니다.",
DownloadUrl: "https://developer.microsoft.com/microsoft-edge/webview2/",
IsRequired: true),
},
RoadmapNote: null);
public Task<ProviderAvailability> CheckAvailabilityAsync(CancellationToken cancellationToken = default)
{
try
{
var version = CoreWebView2Environment.GetAvailableBrowserVersionString();
if (string.IsNullOrEmpty(version))
return Task.FromResult(ProviderAvailability.NotReady(
"WebView2 Runtime이 설치되어 있지 않습니다.",
Capability.ExternalDependencies));
return Task.FromResult(ProviderAvailability.Ready);
}
catch (Exception ex)
{
return Task.FromResult(ProviderAvailability.NotReady(
"WebView2 감지 실패: " + ex.Message,
Capability.ExternalDependencies));
}
}
public async Task<ConvertResult> ConvertAsync(
string sourcePath,
string outputDirectory,
string outputExtension,
ConvertOptions options,
IProgress<double>? progress,
CancellationToken cancellationToken)
{
var outExt = ConversionPair.Normalize(outputExtension);
var baseName = Path.GetFileNameWithoutExtension(sourcePath);
var path = OutputPathHelper.ResolveOutputPath(outputDirectory, baseName, null, outExt, options.OnCollision);
if (OutputPathHelper.ShouldSkip(path, options.OnCollision))
return ConvertResult.Skip(sourcePath, "기존 파일이 있어 건너뜁니다.");
if (outExt == ".pdf")
{
var pdfBytes = await CapturePdfAsync(sourcePath, options, progress, cancellationToken)
.ConfigureAwait(false);
await File.WriteAllBytesAsync(path, pdfBytes, cancellationToken).ConfigureAwait(false);
progress?.Report(1.0);
return ConvertResult.Ok(sourcePath, new[] { path });
}
var pngBytes = await CapturePngAsync(sourcePath, options, progress, cancellationToken)
.ConfigureAwait(false);
progress?.Report(0.85);
await Task.Run(() =>
{
using var image = new MagickImage(pngBytes);
var alphaCapable = outExt is ".png" or ".webp" or ".avif" or ".tif" or ".tiff";
if ((!alphaCapable || options.FlattenTransparency) && image.HasAlpha)
{
image.BackgroundColor = new MagickColor(options.TransparencyBackground);
image.Alpha(AlphaOption.Remove);
image.Alpha(AlphaOption.Off);
}
if (options.MaxLongEdgePixels is int maxLong && maxLong > 0
&& (image.Width > (uint)maxLong || image.Height > (uint)maxLong))
{
image.Resize(new MagickGeometry((uint)maxLong, (uint)maxLong) { IgnoreAspectRatio = false });
}
ApplyEncoding(image, outExt, options);
image.Write(path);
}, cancellationToken).ConfigureAwait(false);
progress?.Report(1.0);
return ConvertResult.Ok(sourcePath, new[] { path });
}
private static void ApplyEncoding(IMagickImage<ushort> image, string outputExtension, ConvertOptions options)
{
switch (outputExtension)
{
case ".jpg":
case ".jpeg":
image.Quality = (uint)Math.Clamp(options.Jpeg.Quality, 1, 100);
image.Format = MagickFormat.Jpeg;
break;
case ".png":
image.Format = MagickFormat.Png;
break;
case ".webp":
image.Quality = (uint)Math.Clamp(options.Webp.Quality, 1, 100);
if (options.Webp.Lossless)
image.Settings.SetDefine(MagickFormat.WebP, "lossless", "true");
image.Format = MagickFormat.WebP;
break;
case ".avif":
image.Quality = (uint)Math.Clamp(options.Avif.Quality, 1, 100);
image.Settings.SetDefine(MagickFormat.Avif, "speed", Math.Clamp(options.Avif.Speed, 0, 10).ToString());
image.Format = MagickFormat.Avif;
break;
case ".bmp":
image.Format = MagickFormat.Bmp;
break;
case ".tif":
case ".tiff":
if (!string.IsNullOrWhiteSpace(options.Tiff.Compression))
image.Settings.SetDefine(MagickFormat.Tiff, "compression", options.Tiff.Compression);
image.Format = MagickFormat.Tiff;
break;
}
}
private static Task<byte[]> CapturePngAsync(
string sourcePath,
ConvertOptions options,
IProgress<double>? progress,
CancellationToken cancellationToken)
=> RunOnDispatcherThreadAsync((web, p) => CaptureScreenshotAsync(web, options, p), sourcePath, options, progress, cancellationToken);
private static Task<byte[]> CapturePdfAsync(
string sourcePath,
ConvertOptions options,
IProgress<double>? progress,
CancellationToken cancellationToken)
=> RunOnDispatcherThreadAsync((web, p) => PrintToPdfAsync(web, options, p), sourcePath, options, progress, cancellationToken);
private static Task<byte[]> RunOnDispatcherThreadAsync(
Func<CoreWebView2, IProgress<double>?, Task<byte[]>> capture,
string sourcePath,
ConvertOptions options,
IProgress<double>? progress,
CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<byte[]>(TaskCreationOptions.RunContinuationsAsynchronously);
var thread = new Thread(() =>
{
try
{
var dispatcher = Dispatcher.CurrentDispatcher;
SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(dispatcher));
dispatcher.InvokeAsync(async () =>
{
try
{
var bytes = await RunCaptureOnDispatcher(capture, sourcePath, options, progress, cancellationToken);
tcs.TrySetResult(bytes);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
finally
{
dispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
}
});
Dispatcher.Run();
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Name = "Everything2Everything.HtmlCapture";
thread.Start();
return tcs.Task;
}
private static async Task<byte[]> RunCaptureOnDispatcher(
Func<CoreWebView2, IProgress<double>?, Task<byte[]>> capture,
string sourcePath,
ConvertOptions options,
IProgress<double>? progress,
CancellationToken cancellationToken)
{
CoreWebView2Controller? controller = null;
HwndSource? hwndSource = null;
string? userDataFolder = null;
try
{
userDataFolder = Path.Combine(Path.GetTempPath(), "E2E_WV2_" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(userDataFolder);
var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
progress?.Report(0.15);
int width = options.HtmlRender.ViewportWidth > 0 ? options.HtmlRender.ViewportWidth : 1280;
int height = options.HtmlRender.ViewportHeight ?? 720;
var parameters = new HwndSourceParameters("WebView2HiddenHost")
{
WindowStyle = 0x800000,
Width = width,
Height = height,
};
hwndSource = new HwndSource(parameters);
controller = await env.CreateCoreWebView2ControllerAsync(hwndSource.Handle);
controller.Bounds = new System.Drawing.Rectangle(0, 0, width, height);
controller.IsVisible = true;
var web = controller.CoreWebView2;
progress?.Report(0.3);
var navTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
EventHandler<CoreWebView2NavigationCompletedEventArgs>? navHandler = null;
navHandler = (_, e) =>
{
web.NavigationCompleted -= navHandler!;
if (e.IsSuccess) navTcs.TrySetResult(true);
else navTcs.TrySetException(new InvalidOperationException($"내비게이션 실패: {e.WebErrorStatus}"));
};
web.NavigationCompleted += navHandler;
var fileUri = new Uri(Path.GetFullPath(sourcePath)).AbsoluteUri;
web.Navigate(fileUri);
using (cancellationToken.Register(() => navTcs.TrySetCanceled()))
{
await navTcs.Task;
}
progress?.Report(0.5);
if (options.HtmlRender.WaitMilliseconds > 0)
await Task.Delay(options.HtmlRender.WaitMilliseconds, cancellationToken);
progress?.Report(0.65);
var bytes = await capture(web, progress);
return bytes;
}
finally
{
try { controller?.Close(); } catch { }
try { hwndSource?.Dispose(); } catch { }
if (userDataFolder != null)
{
_ = Task.Run(async () =>
{
await Task.Delay(1500).ConfigureAwait(false);
try
{
if (Directory.Exists(userDataFolder))
{
Directory.Delete(userDataFolder, true);
}
}
catch { }
});
}
}
}
private static async Task<byte[]> CaptureScreenshotAsync(CoreWebView2 web, ConvertOptions options, IProgress<double>? progress)
{
var captureParams = options.HtmlRender.FullPage
? "{\"captureBeyondViewport\":true,\"format\":\"png\"}"
: "{\"format\":\"png\"}";
var resultJson = await web
.CallDevToolsProtocolMethodAsync("Page.captureScreenshot", captureParams)
.ConfigureAwait(true);
progress?.Report(0.8);
using var doc = JsonDocument.Parse(resultJson);
var b64 = doc.RootElement.GetProperty("data").GetString()
?? throw new InvalidOperationException("CDP captureScreenshot이 빈 결과를 반환했습니다.");
return Convert.FromBase64String(b64);
}
private static async Task<byte[]> PrintToPdfAsync(CoreWebView2 web, ConvertOptions options, IProgress<double>? progress)
{
var resultJson = await web
.CallDevToolsProtocolMethodAsync("Page.printToPDF", "{\"printBackground\":true,\"preferCSSPageSize\":true}")
.ConfigureAwait(true);
progress?.Report(0.8);
using var doc = JsonDocument.Parse(resultJson);
var b64 = doc.RootElement.GetProperty("data").GetString()
?? throw new InvalidOperationException("CDP printToPDF가 빈 결과를 반환했습니다.");
return Convert.FromBase64String(b64);
}
}