Repository navigation
Expand file tree
/
Copy pathPdfSharpAdapter.cs
More file actions
164 lines (139 loc) · 5.22 KB
/
Copy pathPdfSharpAdapter.cs
File metadata and controls
164 lines (139 loc) · 5.22 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// "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 PdfSharp.Drawing;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.Adapters;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
using TheArtOfDev.HtmlRenderer.PdfSharp.FontResolution;
using TheArtOfDev.HtmlRenderer.PdfSharp.Utilities;
namespace TheArtOfDev.HtmlRenderer.PdfSharp.Adapters
{
/// <summary>
/// Adapter for PdfSharp library platform.
/// </summary>
internal sealed class PdfSharpAdapter : RAdapter
{
#region Fields and Consts
/// <summary>
/// Singleton instance of global adapter.
/// </summary>
private static readonly PdfSharpAdapter _instance = new PdfSharpAdapter();
/// <summary>
/// Font resolver instance for managing font discovery and resolution.
/// </summary>
private FontResolver _fontResolver;
#endregion
/// <summary>
/// Init color resolve.
/// </summary>
private PdfSharpAdapter()
{
_fontResolver = FontResolver.Register();
AddFontFamilyMapping("monospace", "Courier New");
AddFontFamilyMapping("Helvetica", "Arial");
var fontFamilies = _fontResolver.DiscoverFontFamilies();
foreach (var fontFamily in fontFamilies)
{
AddFontFamily(new FontFamilyAdapter(new XFontFamily(fontFamily)));
}
}
/// <summary>
/// Singleton instance of global adapter.
/// </summary>
public static PdfSharpAdapter Instance
{
get { return _instance; }
}
/// <summary>
/// Get the FontResolver instance for advanced font management.
/// </summary>
internal FontResolver FontResolver
{
get { return _fontResolver; }
}
protected override RColor GetColorInt(string colorName)
{
try
{
var colorResourceManager = new XColorResourceManager();
var knownColors = XColorResourceManager.GetKnownColors(true);
foreach (var knownColor in knownColors)
{
var name = colorResourceManager.ToColorName(knownColor);
if (!string.Equals(name, colorName, StringComparison.OrdinalIgnoreCase))
{
continue;
}
var xColor = XColor.FromKnownColor(knownColor);
return xColor.IsEmpty ? RColor.Empty : Utils.Convert(xColor);
}
return RColor.Empty;
}
catch
{
return RColor.Empty;
}
}
protected override RPen CreatePen(RColor color)
{
return new PenAdapter(new XPen(Utils.Convert(color)));
}
protected override RBrush CreateSolidBrush(RColor color)
{
XBrush solidBrush;
if (color == RColor.White)
solidBrush = XBrushes.White;
else if (color == RColor.Black)
solidBrush = XBrushes.Black;
else if (color.A < 1)
solidBrush = XBrushes.Transparent;
else
solidBrush = new XSolidBrush(Utils.Convert(color));
return new BrushAdapter(solidBrush);
}
protected override RBrush CreateLinearGradientBrush(RRect rect, RColor color1, RColor color2, double angle)
{
XLinearGradientMode mode;
if (angle < 45)
mode = XLinearGradientMode.ForwardDiagonal;
else if (angle < 90)
mode = XLinearGradientMode.Vertical;
else if (angle < 135)
mode = XLinearGradientMode.BackwardDiagonal;
else
mode = XLinearGradientMode.Horizontal;
return new BrushAdapter(new XLinearGradientBrush(Utils.Convert(rect), Utils.Convert(color1), Utils.Convert(color2), mode));
}
protected override RImage ConvertImageInt(object image)
{
return image != null ? new ImageAdapter((XImage)image) : null;
}
protected override RImage ImageFromStreamInt(Stream memoryStream)
{
return new ImageAdapter(XImage.FromStream(memoryStream));
}
protected override RFont CreateFontInt(string family, double size, RFontStyle style)
{
var fontStyle = Utils.Convert(style);
var xFont = new XFont(family, size, fontStyle, new XPdfFontOptions(PdfFontEncoding.Unicode));
return new FontAdapter(xFont);
}
protected override RFont CreateFontInt(RFontFamily family, double size, RFontStyle style)
{
var fontStyle = Utils.Convert(style);
var xFont = new XFont(((FontFamilyAdapter)family).FontFamily.Name, size, fontStyle, new XPdfFontOptions(PdfFontEncoding.Unicode));
return new FontAdapter(xFont);
}
}
}