-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathFontAdapter.cs
More file actions
105 lines (88 loc) · 2.59 KB
/
FontAdapter.cs
File metadata and controls
105 lines (88 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using TheArtOfDev.HtmlRenderer.Adapters;
using PdfSharp.Drawing;
namespace TheArtOfDev.HtmlRenderer.PdfSharp.Adapters
{
/// <summary>
/// Adapter for WinForms Font object for core.
/// </summary>
internal sealed class FontAdapter : RFont
{
#region Fields and Consts
/// <summary>
/// the underline win-forms font.
/// </summary>
private readonly XFont _font;
/// <summary>
/// the vertical offset of the font underline location from the top of the font.
/// </summary>
private double _underlineOffset = -1;
/// <summary>
/// Cached font height.
/// </summary>
private double _height = -1;
/// <summary>
/// Cached font whitespace width.
/// </summary>
private double _whitespaceWidth = -1;
#endregion
/// <summary>
/// Init.
/// </summary>
public FontAdapter(XFont font)
{
_font = font;
}
/// <summary>
/// the underline win-forms font.
/// </summary>
public XFont Font
{
get { return _font; }
}
public override double Size
{
get { return _font.Size; }
}
public override double UnderlineOffset
{
get { return _underlineOffset; }
}
public override double Height
{
get { return _height; }
}
public override double LeftPadding
{
get { return _height / 6f; }
}
public override double GetWhitespaceWidth(RGraphics graphics)
{
if (_whitespaceWidth < 0)
{
_whitespaceWidth = graphics.MeasureString(" ", this).Width;
}
return _whitespaceWidth;
}
/// <summary>
/// Set font metrics to be cached for the font for future use.
/// </summary>
/// <param name="height">the full height of the font</param>
/// <param name="underlineOffset">the vertical offset of the font underline location from the top of the font.</param>
internal void SetMetrics(int height, double underlineOffset)
{
_height = height;
_underlineOffset = underlineOffset;
}
}
}