Skip to content

Commit

Permalink
-修复替换字体功能不支持加载仅为当前用户安装的字体的问题(fixed #101)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed May 6, 2022
1 parent e50f275 commit f6578e1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
66 changes: 37 additions & 29 deletions App/Common/FontHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using iTextSharp.text.pdf;
using CharSet = System.Runtime.InteropServices.CharSet;
using DllImport = System.Runtime.InteropServices.DllImportAttribute;
using Microsoft.Win32;

namespace PDFPatcher.Common
{
Expand All @@ -16,40 +17,47 @@ static class FontHelper
/// <param name="includeFamilyName">是否包含字体组名称</param>
public static Dictionary<string, string> GetInstalledFonts(bool includeFamilyName) {
var d = new Dictionary<string, string>(50);
using (var k = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts")) {
foreach (var name in k.GetValueNames()) {
var p = k.GetValue(name) as string;
if (String.IsNullOrEmpty(p)) {
continue;
}
if (p.IndexOf('\\') == -1) {
p = FontDirectory + p;
using (var k = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts")) {
GetFontsFromRegistryKey(includeFamilyName, d, k);
}
using (var k = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts")) {
GetFontsFromRegistryKey(includeFamilyName, d, k);
}
return d;
}

static void GetFontsFromRegistryKey(bool includeFamilyName, Dictionary<string, string> d, RegistryKey k) {
foreach (var name in k.GetValueNames()) {
var p = k.GetValue(name) as string;
if (String.IsNullOrEmpty(p)) {
continue;
}
if (p.IndexOf('\\') == -1) {
p = FontDirectory + p;
}
var fp = new FilePath(p);
try {
if (fp.HasExtension(Constants.FileExtensions.Ttf)
|| fp.HasExtension(Constants.FileExtensions.Otf)) {
AddFontNames(d, p, includeFamilyName);
}
var fp = new FilePath(p);
try {
if (fp.HasExtension(Constants.FileExtensions.Ttf)
|| fp.HasExtension(Constants.FileExtensions.Otf)) {
AddFontNames(d, p, includeFamilyName);
else if (fp.HasExtension(Constants.FileExtensions.Ttc)) {
var nl = BaseFont.EnumerateTTCNames(p).Length;
//Tracker.DebugMessage (p);
for (int i = 0; i < nl; i++) {
AddFontNames(d, p + "," + i.ToText(), includeFamilyName);
}
else if (fp.HasExtension(Constants.FileExtensions.Ttc)) {
var nl = BaseFont.EnumerateTTCNames(p).Length;
//Tracker.DebugMessage (p);
for (int i = 0; i < nl; i++) {
AddFontNames(d, p + "," + i.ToText(), includeFamilyName);
}
}
}
catch (System.IO.IOException) {
// ignore
}
catch (NullReferenceException) {
}
catch (iTextSharp.text.DocumentException) {
// ignore
}
}
catch (System.IO.IOException) {
// ignore
}
catch (NullReferenceException) {
}
catch (iTextSharp.text.DocumentException) {
// ignore
}
}
return d;
}

static void AddFontNames(Dictionary<string, string> fontNames, string fontPath, bool includeFamilyName) {
Expand Down
1 change: 1 addition & 0 deletions 更新历史.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
拖放多个文档到程序窗口时,无法正常打开所有文档。
修改文档旋转页面时没有考虑原始页面的旋转角度。
合并部分无损压缩的 TIFF 图像到 PDF 文件时会选择有损 JPEG 格式。
替换字体功能不支持加载仅为当前用户安装的字体。

0.6.2.0 2021年11月9日
新增功能:
Expand Down

0 comments on commit f6578e1

Please sign in to comment.