Skip to content

Commit 1ab9add

Browse files
authored
Merge pull request ststeiger#272 from pamapa/doc-font-resolver
doc: improve font resolver section
2 parents 98ee2e9 + ed5de34 commit 1ab9add

File tree

1 file changed

+90
-11
lines changed

1 file changed

+90
-11
lines changed

docs/PdfSharpCore/samples/FontResolver.md

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,113 @@
22

33
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.
44

5-
For tasks running on web servers, private fonts may be the only available fonts.
65

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
87

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.
911

10-
## The IFontResolver Interface
12+
**Windows**
13+
1. `%SystemRoot%\Fonts`
14+
1. `%LOCALAPPDATA%\Microsoft\Windows\Fonts`
1115

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`
1320

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.
1535

1636
```cs
1737
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
1838
```
1939

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.
2141

2242
```cs
2343
public byte[] GetFont(string faceName)
2444
```
2545

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`.
2748

2849
```cs
29-
// Register font resolver before start using PdfSharpCore.
30-
GlobalFontSettings.FontResolver = new SegoeWpFontResolver();
50+
GlobalFontSettings.FontResolver = new MyFontResolver();
3151
```
3252

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.
3454

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

Comments
 (0)