Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
HWPX → PDF: replace CFF2 variable fonts rhwp cannot embed with installed ones
KnifeLemon51cf62ae0d38
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 06ae3fd3..5fffc224 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,10 @@ find it, what it does and doesn't do.- **One progress card per conversion** (issue #60). When a long conversion moved from the ring around the donut tothe progress card in the bottom-right corner, the card appeared twice.+- **Blank PDF pages with variable fonts** (issue #61). A Word or 한글 document whose font is installed as a variable+ OpenType font with CFF2 outlines, such as Source Han Serif VF (思源宋体 VF), became a PDF without text. Such a font is+ now replaced by a similar font already on the computer (e.g. SimSun for Simplified Chinese serif text). Filee does+ not download or ship fonts.## 1.10.1diff --git a/src/Filee.Engines/Hwp/RhwpConverter.cs b/src/Filee.Engines/Hwp/RhwpConverter.csindex 65ebd9a9..772d4e09 100644--- a/src/Filee.Engines/Hwp/RhwpConverter.cs+++ b/src/Filee.Engines/Hwp/RhwpConverter.cs@@ -35,9 +35,11 @@ public sealed class RhwpConverter : IConverter{var exe = _exe ?? Locate() ?? throw new InvalidOperationException("rhwp was not found.");var temp = Path.Combine(step.WorkDirectory, $"rhwp-{Guid.NewGuid():N}.{step.To}");+ // Fonts rhwp cannot embed are swapped for installed ones in a copy (a CFF2 variable font left pages blank).+ var input = step.To == "pdf" && step.From == "hwpx" ? RhwpFonts.Prepare(step.InputPath, step.WorkDirectory) ?? step.InputPath : step.InputPath;string[] arguments = step.To switch{- "pdf" => ["export-pdf", step.InputPath, "-o", temp],+ "pdf" => ["export-pdf", input, "-o", temp],"hwpx" => ["export-hwpx", step.InputPath, temp],"hwp" => ["convert", step.InputPath, temp],_ => throw new NotSupportedException(step.To),diff --git a/src/Filee.Engines/Hwp/RhwpFonts.cs b/src/Filee.Engines/Hwp/RhwpFonts.csnew file mode 100644index 00000000..15a73b85--- /dev/null+++ b/src/Filee.Engines/Hwp/RhwpFonts.cs@@ -0,0 +1,151 @@+// Fonts rhwp cannot put into a PDF. rhwp uses the fonts installed on the computer; a variable font with CFF2 outlines+// (Source Han Serif VF, "思源宋体 VF" in Chinese Word documents) fails its font subsetter, and the page came out blank+// (issue #61). Such a font is swapped for an installed font of the same script and style before rhwp runs. Nothing is+// downloaded or shipped: the replacement is always a font the computer already has.++using System.Collections.Concurrent;+using System.IO.Compression;+using System.Xml.Linq;+using SkiaSharp;++namespace Filee.Engines.Hwp;++internal static class RhwpFonts+{+ private static readonly string[] Styles =+ ["Thin", "ExtraLight", "Light", "Normal", "Regular", "Medium", "SemiBold", "Bold", "Heavy", "Black", "ExtraBold"];++ /// <summary>Installed fonts in order of preference, by script, serif first then sans.</summary>+ private static readonly Dictionary<string, (string[] Serif, string[] Sans)> Candidates = new()+ {+ ["zh-Hans"] = (["SimSun", "NSimSun", "Noto Serif SC", "Songti SC", "Noto Serif CJK SC"],+ ["Microsoft YaHei", "DengXian", "Noto Sans SC", "PingFang SC", "Noto Sans CJK SC", "SimHei"]),+ ["zh-Hant"] = (["PMingLiU", "MingLiU", "Noto Serif TC", "Songti TC", "Noto Serif CJK TC"],+ ["Microsoft JhengHei", "Noto Sans TC", "PingFang TC", "Noto Sans CJK TC"]),+ ["ja"] = (["Yu Mincho", "MS Mincho", "Noto Serif JP", "Hiragino Mincho ProN", "Noto Serif CJK JP"],+ ["Yu Gothic", "Meiryo", "MS Gothic", "Noto Sans JP", "Hiragino Sans", "Noto Sans CJK JP"]),+ ["ko"] = (["Batang", "HCR Batang", "Noto Serif KR", "AppleMyungjo", "Noto Serif CJK KR"],+ ["Malgun Gothic", "Noto Sans KR", "Apple SD Gothic Neo", "NanumGothic", "Noto Sans CJK KR"]),+ [""] = (["Times New Roman", "Georgia", "Noto Serif", "DejaVu Serif", "Liberation Serif"],+ ["Arial", "Segoe UI", "Noto Sans", "Helvetica", "DejaVu Sans", "Liberation Sans"]),+ };++ private static readonly ConcurrentDictionary<string, bool?> Usable = new(StringComparer.OrdinalIgnoreCase);++ /// <summary>+ /// For each face that is installed but unusable, an installed replacement. Faces that are fine, or not installed+ /// (rhwp falls back on its own), are left out.+ /// </summary>+ /// <param name="usable">True when rhwp can use the font, false when it can't, null when it is not installed.</param>+ public static Dictionary<string, string> Substitutions(IEnumerable<string> faces, Func<string, bool?>? usable = null)+ {+ usable ??= IsUsable;+ var result = new Dictionary<string, string>(StringComparer.Ordinal);+ foreach (var face in faces.Distinct(StringComparer.Ordinal))+ {+ if (face.Length == 0 || usable(face) != false)+ continue;+ var (serif, sans) = Candidates[Script(face)];+ var order = IsSerif(face) ? serif.Concat(sans) : sans.Concat(serif);+ if (order.FirstOrDefault(c => usable(c) == true) is { } replacement)+ result[face] = replacement;+ }+ return result;+ }++ /// <summary>+ /// A copy of the HWPX in <paramref name="workDirectory"/> with unusable fonts replaced, or null when every font is+ /// fine (or the file is not an HWPX package).+ /// </summary>+ public static string? Prepare(string hwpx, string workDirectory, Func<string, bool?>? usable = null)+ {+ try+ {+ XDocument header;+ using (var zip = ZipFile.OpenRead(hwpx))+ {+ if (zip.GetEntry("Contents/header.xml") is not { } entry)+ return null;+ using var stream = entry.Open();+ header = XDocument.Load(stream);+ }+ var fonts = header.Descendants().Where(e => e.Name.LocalName == "font" && e.Attribute("face") is not null).ToList();+ var substitutions = Substitutions(fonts.Select(f => (string)f.Attribute("face")!), usable);+ if (substitutions.Count == 0)+ return null;+ foreach (var font in fonts)+ if (substitutions.TryGetValue((string)font.Attribute("face")!, out var replacement))+ font.SetAttributeValue("face", replacement);++ var copy = Path.Combine(workDirectory, $"rhwp-fonts-{Guid.NewGuid():N}.hwpx");+ File.Copy(hwpx, copy);+ using (var zip = ZipFile.Open(copy, ZipArchiveMode.Update))+ {+ zip.GetEntry("Contents/header.xml")!.Delete();+ using var stream = zip.CreateEntry("Contents/header.xml", CompressionLevel.Optimal).Open();+ header.Save(stream, SaveOptions.DisableFormatting);+ }+ return copy;+ }+ catch (Exception ex) when (ex is InvalidDataException or System.Xml.XmlException)+ {+ return null; // not a package Filee can read: rhwp reports the problem itself+ }+ }++ /// <summary>+ /// Whether an installed font can be embedded by rhwp: false for variable fonts with CFF2 outlines, null when no+ /// installed font has that name. "思源宋体 VF Medium" names a style of "思源宋体 VF", so trailing style words are tried off too.+ /// </summary>+ internal static bool? IsUsable(string face) => Usable.GetOrAdd(face, static name =>+ {+ foreach (var candidate in Names(name))+ {+ using var typeface = SKFontManager.Default.MatchFamily(candidate);+ // Windows matches localized names ("思源宋体 VF") and returns nothing for unknown ones; fontconfig always+ // returns some font, so elsewhere only a font with the requested name counts.+ if (typeface is not null && (OperatingSystem.IsWindows() || string.Equals(typeface.FamilyName, candidate, StringComparison.OrdinalIgnoreCase)))+ return IsUsable(typeface);+ }+ return null;+ });++ /// <summary>False when the font has CFF2 outlines (variable OpenType fonts made from PostScript outlines).</summary>+ internal static bool IsUsable(SKTypeface typeface) => !typeface.GetTableTags().Contains(Tag("CFF2"));++ private static IEnumerable<string> Names(string face)+ {+ yield return face;+ var words = face.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();+ while (words.Count > 1 && Styles.Contains(words[^1], StringComparer.OrdinalIgnoreCase))+ {+ words.RemoveAt(words.Count - 1);+ yield return string.Join(' ', words);+ }+ }++ /// <summary>The script a font name points to: "思源宋体" and "SC" are Simplified Chinese, "명조" Korean, ….</summary>+ internal static string Script(string face)+ {+ bool Has(params string[] parts) => parts.Any(p => face.Contains(p, StringComparison.OrdinalIgnoreCase));+ bool Word(params string[] words) => face.Split(' ', '-', '_').Any(w => words.Contains(w, StringComparer.OrdinalIgnoreCase));+ if (Word("SC", "CN", "GB") || Has("简", "宋体", "黑体", "仿宋", "楷体"))+ return "zh-Hans";+ if (Word("TC", "HK", "TW") || Has("繁", "明體", "黑體", "宋體"))+ return "zh-Hant";+ if (Word("JP", "J") || Has("明朝", "ゴシック", "游"))+ return "ja";+ if (Word("KR", "K") || face.Any(c => c is >= '가' and <= '힣'))+ return "ko";+ if (face.Any(c => c is >= '一' and <= '鿿'))+ return "zh-Hans";+ return "";+ }++ /// <summary>Serif (Song/Ming/Mincho/Batang) rather than sans (Hei/Gothic/Dotum).</summary>+ internal static bool IsSerif(string face) =>+ new[] { "Serif", "Song", "Ming", "Mincho", "Myeongjo", "Myungjo", "Batang", "Times", "宋", "明", "바탕", "명조", "仿宋", "楷" }+ .Any(p => face.Contains(p, StringComparison.OrdinalIgnoreCase));++ private static uint Tag(string tag) => (uint)tag[0] << 24 | (uint)tag[1] << 16 | (uint)tag[2] << 8 | tag[3];+}diff --git a/tests/Filee.Engines.Tests/RhwpFontsTests.cs b/tests/Filee.Engines.Tests/RhwpFontsTests.csnew file mode 100644index 00000000..8fd0af90--- /dev/null+++ b/tests/Filee.Engines.Tests/RhwpFontsTests.cs@@ -0,0 +1,77 @@+// Fonts rhwp cannot embed (variable fonts with CFF2 outlines) are swapped for installed ones before HWPX → PDF.++using System.IO.Compression;+using System.Xml.Linq;+using Filee.Engines.Hwp;++namespace Filee.Engines.Tests;++public class RhwpFontsTests(EngineFixture fx) : IClassFixture<EngineFixture>+{+ // What the fake computer has: true = usable, false = installed but a CFF2 variable font, missing = not installed.+ private static readonly Dictionary<string, bool> Installed = new()+ {+ ["思源宋体 VF Medium"] = false,+ ["Source Han Sans SC VF"] = false,+ ["Some VF"] = false,+ ["SimSun"] = true,+ ["Microsoft YaHei"] = true,+ ["Malgun Gothic"] = true,+ ["Arial"] = true,+ };++ private static bool? Usable(string face) => Installed.TryGetValue(face, out var usable) ? usable : null;++ [Fact]+ public void Unusable_fonts_get_an_installed_font_of_the_same_script_and_style()+ {+ // Issue #61: a Chinese Word document in 思源宋体 VF (Source Han Serif) came out as blank pages.+ var map = RhwpFonts.Substitutions(["思源宋体 VF Medium", "Source Han Sans SC VF", "Some VF", "SimSun", "Not Installed"], Usable);++ Assert.Equal("SimSun", map["思源宋体 VF Medium"]); // Simplified Chinese serif+ Assert.Equal("Microsoft YaHei", map["Source Han Sans SC VF"]); // Simplified Chinese sans+ Assert.Equal("Arial", map["Some VF"]); // no script in the name: Latin sans+ Assert.Equal(3, map.Count); // usable and missing fonts stay (rhwp falls back itself)+ }++ [Theory]+ [InlineData("思源宋体 VF Medium", "zh-Hans", true)]+ [InlineData("Source Han Sans SC VF", "zh-Hans", false)]+ [InlineData("Noto Serif TC", "zh-Hant", true)]+ [InlineData("Source Han Serif K VF", "ko", true)]+ [InlineData("함초롬돋움", "ko", false)]+ [InlineData("Source Han Sans JP", "ja", false)]+ [InlineData("Inter", "", false)]+ public void Script_and_style_come_from_the_font_name(string face, string script, bool serif)+ {+ Assert.Equal(script, RhwpFonts.Script(face));+ Assert.Equal(serif, RhwpFonts.IsSerif(face));+ }++ [Fact]+ public async Task A_copy_of_the_hwpx_names_the_replacement_and_still_converts()+ {+ var dir = fx.NewFolder();+ var hwpx = await fx.MakeHwpxAsync(dir);+ var faces = Faces(hwpx);+ var broken = faces.First();++ var copy = RhwpFonts.Prepare(hwpx, dir, face => face == broken ? false : face == "Malgun Gothic" ? true : null);++ Assert.NotNull(copy);+ Assert.DoesNotContain(broken, Faces(copy));+ Assert.Contains("Malgun Gothic", Faces(copy));+ Assert.Equal(faces, Faces(hwpx)); // the original is untouched+ using (var zip = ZipFile.OpenRead(copy))+ Assert.Equal("mimetype", zip.Entries[0].FullName); // still a valid HWPX package+ Assert.Null(RhwpFonts.Prepare(hwpx, dir, _ => true)); // nothing to replace: no copy+ }++ private static List<string> Faces(string hwpx)+ {+ using var zip = ZipFile.OpenRead(hwpx);+ using var stream = zip.GetEntry("Contents/header.xml")!.Open();+ return XDocument.Load(stream).Descendants().Where(e => e.Name.LocalName == "font")+ .Select(e => (string?)e.Attribute("face")).OfType<string>().Distinct().ToList();+ }+}