From 575953c0edced5f25048f6c6e3d20ba66294283a Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Sun, 6 Jan 2019 10:38:32 +0000 Subject: [PATCH] add multi targeting frameworks in the same project for net 4.5 through net 7.0 and net standard 2.0 --- .../AcroForms/AcroFormFactory.cs | 2 +- .../CrossReference/TrailerDictionary.cs | 2 +- .../Fonts/AdobeStylePrivateDictionary.cs | 13 +- .../CompactFontFormatIndex.cs | 4 +- .../CompactFontFormatIndexReader.cs | 4 +- .../Fonts/SystemFonts/ISystemFontLister.cs | 9 + .../SystemFonts/LinuxSystemFontLister.cs | 68 ++++++ .../Fonts/SystemFonts/MacSystemFontLister.cs | 67 +++++ .../Fonts/SystemFonts/SystemFontFinder.cs | 231 +----------------- .../Fonts/SystemFonts/SystemFontRecord.cs | 52 ++++ .../Fonts/SystemFonts/SystemFontType.cs | 12 + .../SystemFonts/WindowsSystemFontLister.cs | 46 ++++ src/UglyToad.PdfPig/UglyToad.PdfPig.csproj | 12 +- src/UglyToad.PdfPig/Util/EmptyArray.cs | 13 + 14 files changed, 297 insertions(+), 238 deletions(-) create mode 100644 src/UglyToad.PdfPig/Fonts/SystemFonts/ISystemFontLister.cs create mode 100644 src/UglyToad.PdfPig/Fonts/SystemFonts/LinuxSystemFontLister.cs create mode 100644 src/UglyToad.PdfPig/Fonts/SystemFonts/MacSystemFontLister.cs create mode 100644 src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontRecord.cs create mode 100644 src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontType.cs create mode 100644 src/UglyToad.PdfPig/Fonts/SystemFonts/WindowsSystemFontLister.cs create mode 100644 src/UglyToad.PdfPig/Util/EmptyArray.cs diff --git a/src/UglyToad.PdfPig/AcroForms/AcroFormFactory.cs b/src/UglyToad.PdfPig/AcroForms/AcroFormFactory.cs index 02608a68..a88b5841 100644 --- a/src/UglyToad.PdfPig/AcroForms/AcroFormFactory.cs +++ b/src/UglyToad.PdfPig/AcroForms/AcroFormFactory.cs @@ -229,7 +229,7 @@ private AcroFieldBase GetChoiceField(DictionaryToken fieldDictionary, NameToken fieldType, uint fieldFlags, AcroFieldCommonInformation information) { - var selectedOptions = Array.Empty(); + var selectedOptions = EmptyArray.Instance; if (fieldDictionary.TryGet(NameToken.V, out var valueToken)) { if (DirectObjectFinder.TryGet(valueToken, tokenScanner, out StringToken valueString)) diff --git a/src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs b/src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs index 06b5a434..4d60c0c8 100644 --- a/src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs +++ b/src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs @@ -91,7 +91,7 @@ } else { - Identifier = Array.Empty(); + Identifier = EmptyArray.Instance; } if (dictionary.TryGet(NameToken.Encrypt, out var encryptionToken)) diff --git a/src/UglyToad.PdfPig/Fonts/AdobeStylePrivateDictionary.cs b/src/UglyToad.PdfPig/Fonts/AdobeStylePrivateDictionary.cs index 4333e3e9..bd3bfd37 100644 --- a/src/UglyToad.PdfPig/Fonts/AdobeStylePrivateDictionary.cs +++ b/src/UglyToad.PdfPig/Fonts/AdobeStylePrivateDictionary.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using Util; using Util.JetBrains.Annotations; /// @@ -150,17 +151,17 @@ throw new ArgumentNullException(nameof(builder)); } - BlueValues = builder.BlueValues ?? Array.Empty(); - OtherBlues = builder.OtherBlues ?? Array.Empty(); - FamilyBlues = builder.FamilyBlues ?? Array.Empty(); - FamilyOtherBlues = builder.FamilyOtherBlues ?? Array.Empty(); + BlueValues = builder.BlueValues ?? EmptyArray.Instance; + OtherBlues = builder.OtherBlues ?? EmptyArray.Instance; + FamilyBlues = builder.FamilyBlues ?? EmptyArray.Instance; + FamilyOtherBlues = builder.FamilyOtherBlues ?? EmptyArray.Instance; BlueScale = builder.BlueScale ?? DefaultBlueScale; BlueFuzz = builder.BlueFuzz ?? DefaultBlueFuzz; BlueShift = builder.BlueShift ?? DefaultBlueShift; StandardHorizontalWidth = builder.StandardHorizontalWidth; StandardVerticalWidth = builder.StandardVerticalWidth; - StemSnapHorizontalWidths = builder.StemSnapHorizontalWidths ?? Array.Empty(); - StemSnapVerticalWidths = builder.StemSnapVerticalWidths ?? Array.Empty(); + StemSnapHorizontalWidths = builder.StemSnapHorizontalWidths ?? EmptyArray.Instance; + StemSnapVerticalWidths = builder.StemSnapVerticalWidths ?? EmptyArray.Instance; ForceBold = builder.ForceBold ?? false; LanguageGroup = builder.LanguageGroup ?? DefaultLanguageGroup; ExpansionFactor = builder.ExpansionFactor ?? DefaultExpansionFactor; diff --git a/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CompactFontFormatIndex.cs b/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CompactFontFormatIndex.cs index adfd8068..66ea8448 100644 --- a/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CompactFontFormatIndex.cs +++ b/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CompactFontFormatIndex.cs @@ -1,8 +1,8 @@ namespace UglyToad.PdfPig.Fonts.CompactFontFormat { - using System; using System.Collections; using System.Collections.Generic; + using Util; using Util.JetBrains.Annotations; internal class CompactFontFormatIndex : IReadOnlyList> @@ -18,7 +18,7 @@ public CompactFontFormatIndex(byte[][] bytes) { - this.bytes = bytes ?? Array.Empty>(); + this.bytes = bytes ?? EmptyArray>.Instance; } public IEnumerator> GetEnumerator() diff --git a/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CompactFontFormatIndexReader.cs b/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CompactFontFormatIndexReader.cs index ed754ead..986f0a32 100644 --- a/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CompactFontFormatIndexReader.cs +++ b/src/UglyToad.PdfPig/Fonts/CompactFontFormat/CompactFontFormatIndexReader.cs @@ -2,6 +2,8 @@ namespace UglyToad.PdfPig.Fonts.CompactFontFormat { + using Util; + internal class CompactFontFormatIndexReader { public CompactFontFormatIndex ReadDictionaryData(CompactFontFormatData data) @@ -43,7 +45,7 @@ namespace UglyToad.PdfPig.Fonts.CompactFontFormat if (count == 0) { - return Array.Empty(); + return EmptyArray.Instance; } var offsetSize = data.ReadOffsize(); diff --git a/src/UglyToad.PdfPig/Fonts/SystemFonts/ISystemFontLister.cs b/src/UglyToad.PdfPig/Fonts/SystemFonts/ISystemFontLister.cs new file mode 100644 index 00000000..17155dce --- /dev/null +++ b/src/UglyToad.PdfPig/Fonts/SystemFonts/ISystemFontLister.cs @@ -0,0 +1,9 @@ +namespace UglyToad.PdfPig.Fonts.SystemFonts +{ + using System.Collections.Generic; + + internal interface ISystemFontLister + { + IEnumerable GetAllFonts(); + } +} \ No newline at end of file diff --git a/src/UglyToad.PdfPig/Fonts/SystemFonts/LinuxSystemFontLister.cs b/src/UglyToad.PdfPig/Fonts/SystemFonts/LinuxSystemFontLister.cs new file mode 100644 index 00000000..a32f19fa --- /dev/null +++ b/src/UglyToad.PdfPig/Fonts/SystemFonts/LinuxSystemFontLister.cs @@ -0,0 +1,68 @@ +namespace UglyToad.PdfPig.Fonts.SystemFonts +{ + using System; + using System.Collections.Generic; + using System.IO; + + internal class LinuxSystemFontLister : ISystemFontLister + { + public IEnumerable GetAllFonts() + { + var directories = new List + { + "/usr/local/fonts", // local + "/usr/local/share/fonts", // local shared + "/usr/share/fonts", // system + "/usr/X11R6/lib/X11/fonts" // X + }; + + try + { + var folder = Environment.GetEnvironmentVariable("$HOME"); + + if (!string.IsNullOrWhiteSpace(folder)) + { + directories.Add($"{folder}/.fonts"); + } + } + catch + { + // ignored + } + + foreach (var directory in directories) + { + try + { + if (!Directory.Exists(directory)) + { + continue; + } + } + catch + { + continue; + } + + string[] files; + + try + { + files = Directory.GetFiles(directory); + } + catch + { + continue; + } + + foreach (var file in files) + { + if (SystemFontRecord.TryCreate(file, out var record)) + { + yield return record; + } + } + } + } + } +} \ No newline at end of file diff --git a/src/UglyToad.PdfPig/Fonts/SystemFonts/MacSystemFontLister.cs b/src/UglyToad.PdfPig/Fonts/SystemFonts/MacSystemFontLister.cs new file mode 100644 index 00000000..47c22430 --- /dev/null +++ b/src/UglyToad.PdfPig/Fonts/SystemFonts/MacSystemFontLister.cs @@ -0,0 +1,67 @@ +namespace UglyToad.PdfPig.Fonts.SystemFonts +{ + using System; + using System.Collections.Generic; + using System.IO; + + internal class MacSystemFontLister : ISystemFontLister + { + public IEnumerable GetAllFonts() + { + var directories = new List + { + "/Library/Fonts/", // local + "/System/Library/Fonts/", // system + "/Network/Library/Fonts/" // network + }; + + try + { + var folder = Environment.GetEnvironmentVariable("$HOME"); + + if (!string.IsNullOrWhiteSpace(folder)) + { + directories.Add($"{folder}/Library/Fonts"); + } + } + catch + { + // ignored + } + + foreach (var directory in directories) + { + try + { + if (!Directory.Exists(directory)) + { + continue; + } + } + catch + { + continue; + } + + string[] files; + + try + { + files = Directory.GetFiles(directory); + } + catch + { + continue; + } + + foreach (var file in files) + { + if (SystemFontRecord.TryCreate(file, out var record)) + { + yield return record; + } + } + } + } + } +} \ No newline at end of file diff --git a/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontFinder.cs b/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontFinder.cs index 2fdb539c..ba5f025f 100644 --- a/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontFinder.cs +++ b/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontFinder.cs @@ -19,7 +19,9 @@ public SystemFontFinder(TrueTypeFontParser trueTypeFontParser) { this.trueTypeFontParser = trueTypeFontParser; + ISystemFontLister lister; +#if NETSTANDARD2_0 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { lister = new WindowsSystemFontLister(); @@ -36,6 +38,9 @@ { throw new NotSupportedException($"Unsupported operating system: {RuntimeInformation.OSDescription}."); } +#else + lister = new WindowsSystemFontLister(); +#endif availableFonts = new Lazy>(() => lister.GetAllFonts().ToList()); } @@ -75,230 +80,4 @@ return null; } } - - internal interface ISystemFontLister - { - IEnumerable GetAllFonts(); - } - - internal class WindowsSystemFontLister : ISystemFontLister - { - public IEnumerable GetAllFonts() - { - // TODO: Could use System.Drawing InstalledFontCollection to do this? - - var winDir = Environment.GetFolderPath(Environment.SpecialFolder.Windows); - - var fonts = Path.Combine(winDir, "Fonts"); - - if (Directory.Exists(fonts)) - { - var files = Directory.GetFiles(fonts); - - foreach (var file in files) - { - if (SystemFontRecord.TryCreate(file, out var record)) - { - yield return record; - } - } - } - - var psFonts = Path.Combine(winDir, "PSFonts"); - - if (Directory.Exists(psFonts)) - { - var files = Directory.GetFiles(fonts); - - foreach (var file in files) - { - if (SystemFontRecord.TryCreate(file, out var record)) - { - yield return record; - } - } - } - } - } - - internal class LinuxSystemFontLister : ISystemFontLister - { - public IEnumerable GetAllFonts() - { - var directories = new List - { - "/usr/local/fonts", // local - "/usr/local/share/fonts", // local shared - "/usr/share/fonts", // system - "/usr/X11R6/lib/X11/fonts" // X - }; - - try - { - var folder = Environment.GetEnvironmentVariable("$HOME"); - - if (!string.IsNullOrWhiteSpace(folder)) - { - directories.Add($"{folder}/.fonts"); - } - } - catch - { - // ignored - } - - foreach (var directory in directories) - { - try - { - if (!Directory.Exists(directory)) - { - continue; - } - } - catch - { - continue; - } - - string[] files; - - try - { - files = Directory.GetFiles(directory); - } - catch - { - continue; - } - - foreach (var file in files) - { - if (SystemFontRecord.TryCreate(file, out var record)) - { - yield return record; - } - } - } - } - } - - internal class MacSystemFontLister : ISystemFontLister - { - public IEnumerable GetAllFonts() - { - var directories = new List - { - "/Library/Fonts/", // local - "/System/Library/Fonts/", // system - "/Network/Library/Fonts/" // network - }; - - try - { - var folder = Environment.GetEnvironmentVariable("$HOME"); - - if (!string.IsNullOrWhiteSpace(folder)) - { - directories.Add($"{folder}/Library/Fonts"); - } - } - catch - { - // ignored - } - - foreach (var directory in directories) - { - try - { - if (!Directory.Exists(directory)) - { - continue; - } - } - catch - { - continue; - } - - string[] files; - - try - { - files = Directory.GetFiles(directory); - } - catch - { - continue; - } - - foreach (var file in files) - { - if (SystemFontRecord.TryCreate(file, out var record)) - { - yield return record; - } - } - } - } - } - - internal struct SystemFontRecord - { - public string Path { get; } - - public SystemFontType Type { get; } - - public SystemFontRecord(string path, SystemFontType type) - { - Path = path ?? throw new ArgumentNullException(nameof(path)); - Type = type; - } - - public static bool TryCreate(string path, out SystemFontRecord type) - { - type = default(SystemFontRecord); - - SystemFontType fontType; - if (path.EndsWith(".ttf")) - { - fontType = SystemFontType.TrueType; - } - else if (path.EndsWith(".otf")) - { - fontType = SystemFontType.OpenType; - } - else if (path.EndsWith(".ttc")) - { - fontType = SystemFontType.TrueTypeCollection; - } - else if (path.EndsWith(".otc")) - { - fontType = SystemFontType.OpenTypeCollection; - } - else if (path.EndsWith(".pfb")) - { - fontType = SystemFontType.Type1; - } - else - { - return false; - } - - type = new SystemFontRecord(path, fontType); - - return true; - } - } - - internal enum SystemFontType - { - Unknown = 0, - TrueType = 1, - OpenType = 2, - Type1 = 3, - TrueTypeCollection = 4, - OpenTypeCollection = 5 - } } \ No newline at end of file diff --git a/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontRecord.cs b/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontRecord.cs new file mode 100644 index 00000000..e49d3dab --- /dev/null +++ b/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontRecord.cs @@ -0,0 +1,52 @@ +namespace UglyToad.PdfPig.Fonts.SystemFonts +{ + using System; + + internal struct SystemFontRecord + { + public string Path { get; } + + public SystemFontType Type { get; } + + public SystemFontRecord(string path, SystemFontType type) + { + Path = path ?? throw new ArgumentNullException(nameof(path)); + Type = type; + } + + public static bool TryCreate(string path, out SystemFontRecord type) + { + type = default(SystemFontRecord); + + SystemFontType fontType; + if (path.EndsWith(".ttf")) + { + fontType = SystemFontType.TrueType; + } + else if (path.EndsWith(".otf")) + { + fontType = SystemFontType.OpenType; + } + else if (path.EndsWith(".ttc")) + { + fontType = SystemFontType.TrueTypeCollection; + } + else if (path.EndsWith(".otc")) + { + fontType = SystemFontType.OpenTypeCollection; + } + else if (path.EndsWith(".pfb")) + { + fontType = SystemFontType.Type1; + } + else + { + return false; + } + + type = new SystemFontRecord(path, fontType); + + return true; + } + } +} \ No newline at end of file diff --git a/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontType.cs b/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontType.cs new file mode 100644 index 00000000..4193d0fb --- /dev/null +++ b/src/UglyToad.PdfPig/Fonts/SystemFonts/SystemFontType.cs @@ -0,0 +1,12 @@ +namespace UglyToad.PdfPig.Fonts.SystemFonts +{ + internal enum SystemFontType + { + Unknown = 0, + TrueType = 1, + OpenType = 2, + Type1 = 3, + TrueTypeCollection = 4, + OpenTypeCollection = 5 + } +} \ No newline at end of file diff --git a/src/UglyToad.PdfPig/Fonts/SystemFonts/WindowsSystemFontLister.cs b/src/UglyToad.PdfPig/Fonts/SystemFonts/WindowsSystemFontLister.cs new file mode 100644 index 00000000..8740a8ac --- /dev/null +++ b/src/UglyToad.PdfPig/Fonts/SystemFonts/WindowsSystemFontLister.cs @@ -0,0 +1,46 @@ +namespace UglyToad.PdfPig.Fonts.SystemFonts +{ + using System; + using System.Collections.Generic; + using System.IO; + + internal class WindowsSystemFontLister : ISystemFontLister + { + public IEnumerable GetAllFonts() + { + // TODO: Could use System.Drawing InstalledFontCollection to do this? + + var winDir = Environment.GetFolderPath(Environment.SpecialFolder.Windows); + + var fonts = Path.Combine(winDir, "Fonts"); + + if (Directory.Exists(fonts)) + { + var files = Directory.GetFiles(fonts); + + foreach (var file in files) + { + if (SystemFontRecord.TryCreate(file, out var record)) + { + yield return record; + } + } + } + + var psFonts = Path.Combine(winDir, "PSFonts"); + + if (Directory.Exists(psFonts)) + { + var files = Directory.GetFiles(fonts); + + foreach (var file in files) + { + if (SystemFontRecord.TryCreate(file, out var record)) + { + yield return record; + } + } + } + } + } +} \ No newline at end of file diff --git a/src/UglyToad.PdfPig/UglyToad.PdfPig.csproj b/src/UglyToad.PdfPig/UglyToad.PdfPig.csproj index a405bf27..5ab2398a 100644 --- a/src/UglyToad.PdfPig/UglyToad.PdfPig.csproj +++ b/src/UglyToad.PdfPig/UglyToad.PdfPig.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + netstandard2.0;net45;net451;net452;net46;net461;net462;net47 PdfPig full UglyToad @@ -47,5 +47,15 @@ runtime; build; native; contentfiles; analyzers + + + + diff --git a/src/UglyToad.PdfPig/Util/EmptyArray.cs b/src/UglyToad.PdfPig/Util/EmptyArray.cs new file mode 100644 index 00000000..aa848a22 --- /dev/null +++ b/src/UglyToad.PdfPig/Util/EmptyArray.cs @@ -0,0 +1,13 @@ +namespace UglyToad.PdfPig.Util +{ + /// + /// NET 4.5 compatible Array.Empty. + /// + internal static class EmptyArray + { + /// + /// An empty array. + /// + public static T[] Instance { get; } = new T[0]; + } +}