add multi targeting frameworks in the same project for net 4.5 through net 7.0 and net standard 2.0

This commit is contained in:
Eliot Jones
2019-01-06 10:38:32 +00:00
parent 16b0260d68
commit 575953c0ed
14 changed files with 297 additions and 238 deletions

View File

@@ -229,7 +229,7 @@
private AcroFieldBase GetChoiceField(DictionaryToken fieldDictionary, NameToken fieldType, uint fieldFlags, AcroFieldCommonInformation information)
{
var selectedOptions = Array.Empty<string>();
var selectedOptions = EmptyArray<string>.Instance;
if (fieldDictionary.TryGet(NameToken.V, out var valueToken))
{
if (DirectObjectFinder.TryGet(valueToken, tokenScanner, out StringToken valueString))

View File

@@ -91,7 +91,7 @@
}
else
{
Identifier = Array.Empty<string>();
Identifier = EmptyArray<string>.Instance;
}
if (dictionary.TryGet(NameToken.Encrypt, out var encryptionToken))

View File

@@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using Util;
using Util.JetBrains.Annotations;
/// <summary>
@@ -150,17 +151,17 @@
throw new ArgumentNullException(nameof(builder));
}
BlueValues = builder.BlueValues ?? Array.Empty<int>();
OtherBlues = builder.OtherBlues ?? Array.Empty<int>();
FamilyBlues = builder.FamilyBlues ?? Array.Empty<int>();
FamilyOtherBlues = builder.FamilyOtherBlues ?? Array.Empty<int>();
BlueValues = builder.BlueValues ?? EmptyArray<int>.Instance;
OtherBlues = builder.OtherBlues ?? EmptyArray<int>.Instance;
FamilyBlues = builder.FamilyBlues ?? EmptyArray<int>.Instance;
FamilyOtherBlues = builder.FamilyOtherBlues ?? EmptyArray<int>.Instance;
BlueScale = builder.BlueScale ?? DefaultBlueScale;
BlueFuzz = builder.BlueFuzz ?? DefaultBlueFuzz;
BlueShift = builder.BlueShift ?? DefaultBlueShift;
StandardHorizontalWidth = builder.StandardHorizontalWidth;
StandardVerticalWidth = builder.StandardVerticalWidth;
StemSnapHorizontalWidths = builder.StemSnapHorizontalWidths ?? Array.Empty<decimal>();
StemSnapVerticalWidths = builder.StemSnapVerticalWidths ?? Array.Empty<decimal>();
StemSnapHorizontalWidths = builder.StemSnapHorizontalWidths ?? EmptyArray<decimal>.Instance;
StemSnapVerticalWidths = builder.StemSnapVerticalWidths ?? EmptyArray<decimal>.Instance;
ForceBold = builder.ForceBold ?? false;
LanguageGroup = builder.LanguageGroup ?? DefaultLanguageGroup;
ExpansionFactor = builder.ExpansionFactor ?? DefaultExpansionFactor;

View File

@@ -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<IReadOnlyList<byte>>
@@ -18,7 +18,7 @@
public CompactFontFormatIndex(byte[][] bytes)
{
this.bytes = bytes ?? Array.Empty<IReadOnlyList<byte>>();
this.bytes = bytes ?? EmptyArray<IReadOnlyList<byte>>.Instance;
}
public IEnumerator<IReadOnlyList<byte>> GetEnumerator()

View File

@@ -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<int>();
return EmptyArray<int>.Instance;
}
var offsetSize = data.ReadOffsize();

View File

@@ -0,0 +1,9 @@
namespace UglyToad.PdfPig.Fonts.SystemFonts
{
using System.Collections.Generic;
internal interface ISystemFontLister
{
IEnumerable<SystemFontRecord> GetAllFonts();
}
}

View File

@@ -0,0 +1,68 @@
namespace UglyToad.PdfPig.Fonts.SystemFonts
{
using System;
using System.Collections.Generic;
using System.IO;
internal class LinuxSystemFontLister : ISystemFontLister
{
public IEnumerable<SystemFontRecord> GetAllFonts()
{
var directories = new List<string>
{
"/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;
}
}
}
}
}
}

View File

@@ -0,0 +1,67 @@
namespace UglyToad.PdfPig.Fonts.SystemFonts
{
using System;
using System.Collections.Generic;
using System.IO;
internal class MacSystemFontLister : ISystemFontLister
{
public IEnumerable<SystemFontRecord> GetAllFonts()
{
var directories = new List<string>
{
"/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;
}
}
}
}
}
}

View File

@@ -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<IReadOnlyList<SystemFontRecord>>(() => lister.GetAllFonts().ToList());
}
@@ -75,230 +80,4 @@
return null;
}
}
internal interface ISystemFontLister
{
IEnumerable<SystemFontRecord> GetAllFonts();
}
internal class WindowsSystemFontLister : ISystemFontLister
{
public IEnumerable<SystemFontRecord> 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<SystemFontRecord> GetAllFonts()
{
var directories = new List<string>
{
"/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<SystemFontRecord> GetAllFonts()
{
var directories = new List<string>
{
"/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
}
}

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,12 @@
namespace UglyToad.PdfPig.Fonts.SystemFonts
{
internal enum SystemFontType
{
Unknown = 0,
TrueType = 1,
OpenType = 2,
Type1 = 3,
TrueTypeCollection = 4,
OpenTypeCollection = 5
}
}

View File

@@ -0,0 +1,46 @@
namespace UglyToad.PdfPig.Fonts.SystemFonts
{
using System;
using System.Collections.Generic;
using System.IO;
internal class WindowsSystemFontLister : ISystemFontLister
{
public IEnumerable<SystemFontRecord> 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;
}
}
}
}
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net45;net451;net452;net46;net461;net462;net47</TargetFrameworks>
<PackageId>PdfPig</PackageId>
<DebugType>full</DebugType>
<Authors>UglyToad</Authors>
@@ -47,5 +47,15 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net45'
OR '$(TargetFramework)'=='net451'
OR '$(TargetFramework)'=='net452'
OR '$(TargetFramework)'=='net46'
OR '$(TargetFramework)'=='net461'
OR '$(TargetFramework)'=='net462'
OR '$(TargetFramework)'=='net47'">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,13 @@
namespace UglyToad.PdfPig.Util
{
/// <summary>
/// NET 4.5 compatible Array.Empty.
/// </summary>
internal static class EmptyArray<T>
{
/// <summary>
/// An empty array.
/// </summary>
public static T[] Instance { get; } = new T[0];
}
}