|
2 | 2 |
|
3 | 3 | This sample shows how to use fonts that are included with your application. This allows you to use fonts that are not installed on the computer.
|
4 | 4 |
|
5 |
| -For tasks running on web servers, private fonts may be the only available fonts. |
6 | 5 |
|
7 |
| -Note: The FontResolver is a global object and applies to all consumers of the PdfSharpCore library. It is also used when the MigraDocCore library creates PDF files. |
| 6 | +## Default font resolver |
8 | 7 |
|
| 8 | +PdfSharpCore comes with a [default font resolver](../../../PdfSharpCore/Utils/FontResolver.cs). |
| 9 | +This resolver uses the fonts from the operating system. |
| 10 | +The font directories depend on the used operating system. |
9 | 11 |
|
10 |
| -## The IFontResolver Interface |
| 12 | +**Windows** |
| 13 | +1. `%SystemRoot%\Fonts` |
| 14 | +1. `%LOCALAPPDATA%\Microsoft\Windows\Fonts` |
11 | 15 |
|
12 |
| -In your application you create a class that implements the IFontResolver interface (it is in the PdfSharpCore.Fonts namespace). |
| 16 | +**Linux** |
| 17 | +1. `/usr/share/fonts` |
| 18 | +1. `/usr/local/share/fonts` |
| 19 | +1. `~/.fonts` |
13 | 20 |
|
14 |
| -There are only two methods you have to implement. The first method returns a FontResolverInfo for every supported font. |
| 21 | +**iOS** |
| 22 | +1. `/Library/Fonts/` |
| 23 | + |
| 24 | + |
| 25 | +## Custom font resolver |
| 26 | + |
| 27 | +When running on web services or servers, the operating system might **not** have the fonts installed you need or you can **not** install the font you need. |
| 28 | +In this scenario you must provide the fonts yourself and therefore implement your own font resolver. |
| 29 | + |
| 30 | +### IFontResolver interface |
| 31 | + |
| 32 | +In your application you create a class that implements the `IFontResolver` interface. |
| 33 | + |
| 34 | +There are only two methods you have to implement. The first method returns a `FontResolverInfo` for every supported font. |
15 | 35 |
|
16 | 36 | ```cs
|
17 | 37 | public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
|
18 | 38 | ```
|
19 | 39 |
|
20 |
| -The other method is called using the FaceName from the FontResolverInfo you previously returned. At this stage, return the font data as a byte array. |
| 40 | +The other method is called using the `FaceName` from the FontResolverInfo you previously returned. At this stage, return the font data as a byte array. |
21 | 41 |
|
22 | 42 | ```cs
|
23 | 43 | public byte[] GetFont(string faceName)
|
24 | 44 | ```
|
25 | 45 |
|
26 |
| -Now you only need one more step: register your font resolver using the global font resolver property. Here SegoeWpFontResolver is the class that implements IFontResolver. |
| 46 | +Now you only need one more step: register your font resolver using the global font resolver property. |
| 47 | +Here MyFontResolver is the class that implements `IFontResolver`. |
27 | 48 |
|
28 | 49 | ```cs
|
29 |
| -// Register font resolver before start using PdfSharpCore. |
30 |
| -GlobalFontSettings.FontResolver = new SegoeWpFontResolver(); |
| 50 | +GlobalFontSettings.FontResolver = new MyFontResolver(); |
31 | 51 | ```
|
32 | 52 |
|
33 |
| -## Additional Information |
| 53 | +Note: The `FontResolver` is a global object and applies to all consumers of the PdfSharpCore library. It is also used when the MigraDocCore library creates PDF files. |
34 | 54 |
|
35 |
| -The font resolver set using GlobalFontSettings.FontResolver will be used by PdfSharpCore. Since MigraDocCore uses PdfSharpCore to create PDF files, the font resolver will also be used when generating PDF files from MigraDocCore. |
| 55 | +### Code |
| 56 | + |
| 57 | +This implementation is obviously not complete. |
| 58 | +But it should be enough for everyone to implement their own. |
| 59 | +For more details have a look at the [default font resolver](../../../PdfSharpCore/Utils/FontResolver.cs). |
| 60 | + |
| 61 | +```cs |
| 62 | +using System; |
| 63 | +using System.IO; |
| 64 | +using PdfSharpCore.Utils; |
| 65 | + |
| 66 | +public class MyFontResolver : IFontResolver |
| 67 | +{ |
| 68 | + public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic) |
| 69 | + { |
| 70 | + if (familyName.Equals("OpenSans", StringComparison.CurrentCultureIgnoreCase)) |
| 71 | + { |
| 72 | + if (isBold && isItalic) |
| 73 | + { |
| 74 | + return new FontResolverInfo("OpenSans-BoldItalic.ttf"); |
| 75 | + } |
| 76 | + else if (isBold) |
| 77 | + { |
| 78 | + return new FontResolverInfo("OpenSans-Bold.ttf"); |
| 79 | + } |
| 80 | + else if (isItalic) |
| 81 | + { |
| 82 | + return new FontResolverInfo("OpenSans-Italic.ttf"); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + return new FontResolverInfo("OpenSans-Regular.ttf"); |
| 87 | + } |
| 88 | + } |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + public byte[] GetFont(string faceName) |
| 93 | + { |
| 94 | + var faceNamePath = Path.Join("my path", faceName); |
| 95 | + using(var ms = new MemoryStream()) |
| 96 | + { |
| 97 | + try |
| 98 | + { |
| 99 | + using(var fs = File.OpenRead(faceNamePath)) |
| 100 | + { |
| 101 | + fs.CopyTo(ms); |
| 102 | + ms.Position = 0; |
| 103 | + return ms.ToArray(); |
| 104 | + } |
| 105 | + } |
| 106 | + catch (Exception e) |
| 107 | + { |
| 108 | + Console.WriteLine(e); |
| 109 | + throw new Exception($"No Font File Found - " + faceNamePath); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
0 commit comments