-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathGraphicsAdapter.cs
More file actions
239 lines (198 loc) · 8 KB
/
GraphicsAdapter.cs
File metadata and controls
239 lines (198 loc) · 8 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// "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 System;
using TheArtOfDev.HtmlRenderer.Adapters;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
using TheArtOfDev.HtmlRenderer.Core.Utils;
using TheArtOfDev.HtmlRenderer.PdfSharp.Utilities;
namespace TheArtOfDev.HtmlRenderer.PdfSharp.Adapters
{
/// <summary>
/// Adapter for WinForms Graphics for core.
/// </summary>
internal sealed class GraphicsAdapter : RGraphics
{
#region Fields and Consts
/// <summary>
/// The wrapped WinForms graphics object
/// </summary>
private readonly XGraphics _g;
/// <summary>
/// if to release the graphics object on dispose
/// </summary>
private readonly bool _releaseGraphics;
/// <summary>
/// Used to measure and draw strings
/// </summary>
private static readonly XStringFormat _stringFormat;
#endregion
static GraphicsAdapter()
{
_stringFormat = new XStringFormat();
_stringFormat.Alignment = XStringAlignment.Near;
_stringFormat.LineAlignment = XLineAlignment.Near;
}
/// <summary>
/// Init.
/// </summary>
/// <param name="g">the win forms graphics object to use</param>
/// <param name="releaseGraphics">optional: if to release the graphics object on dispose (default - false)</param>
public GraphicsAdapter(XGraphics g, bool releaseGraphics = false)
: base(PdfSharpAdapter.Instance, new RRect(0, 0, double.MaxValue, double.MaxValue))
{
ArgChecker.AssertArgNotNull(g, "g");
_g = g;
_releaseGraphics = releaseGraphics;
}
public override void PopClip()
{
_clipStack.Pop();
_g.Restore();
}
public override void PushClip(RRect rect)
{
_clipStack.Push(rect);
_g.Save();
_g.IntersectClip(Utils.Convert(rect));
}
public override void PushClipExclude(RRect rect)
{ }
public override Object SetAntiAliasSmoothingMode()
{
var prevMode = _g.SmoothingMode;
_g.SmoothingMode = XSmoothingMode.AntiAlias;
return prevMode;
}
public override void ReturnPreviousSmoothingMode(Object prevMode)
{
if (prevMode != null)
{
_g.SmoothingMode = (XSmoothingMode)prevMode;
}
}
public override RSize MeasureString(string str, RFont font)
{
var fontAdapter = (FontAdapter)font;
var realFont = fontAdapter.Font;
var size = _g.MeasureString(str, realFont, _stringFormat);
if (font.Height < 0)
{
var height = realFont.Height;
var descent = realFont.Size * realFont.FontFamily.GetCellDescent(realFont.Style) / realFont.FontFamily.GetEmHeight(realFont.Style);
fontAdapter.SetMetrics(height, height - descent + 1f);
}
return Utils.Convert(size);
}
public override void MeasureString(string str, RFont font, double maxWidth, out int charFit, out double charFitWidth)
{
// there is no need for it - used for text selection
throw new NotSupportedException();
}
public override void DrawString(string str, RFont font, RColor color, RPoint point, RSize size, bool rtl)
{
var xBrush = ((BrushAdapter)_adapter.GetSolidBrush(color)).Brush;
_g.DrawString(str, ((FontAdapter)font).Font, (XBrush)xBrush, point.X, point.Y, _stringFormat);
}
public override void DrawSmallCapString(string str, RFont regularFont, RFont reducedFont, RColor color, RPoint point, bool rtl)
{
var xBrush = ((BrushAdapter)_adapter.GetSolidBrush(color)).Brush;
double additionalOffsetForSmallCaps = regularFont.UnderlineOffset - reducedFont.UnderlineOffset;
int i = 0;
int len = str.Length;
while (i < len)
{
int j = i;
while (j < len && !char.IsLower(str, j))
j++;
if (j != i)
{
_g.DrawString(str.Substring(i, j - i), ((FontAdapter)regularFont).Font, (XBrush)xBrush, point.X, point.Y, _stringFormat);
point.X += MeasureString(str.Substring(i, j - i), regularFont).Width;
i = j;
}
while (j < len && char.IsLower(str, j))
j++;
if (j != i)
{
point.Y += additionalOffsetForSmallCaps;
_g.DrawString(str.Substring(i, j - i).ToUpper(), ((FontAdapter)reducedFont).Font, (XBrush)xBrush, point.X, point.Y, _stringFormat);
point.X += MeasureString(str.Substring(i, j - i).ToUpper(), reducedFont).Width;
point.Y -= additionalOffsetForSmallCaps;
i = j;
}
}
}
public override RBrush GetTextureBrush(RImage image, RRect dstRect, RPoint translateTransformLocation)
{
return new BrushAdapter(new XTextureBrush(((ImageAdapter)image).Image, Utils.Convert(dstRect), Utils.Convert(translateTransformLocation)));
}
public override RGraphicsPath GetGraphicsPath()
{
return new GraphicsPathAdapter();
}
public override void Dispose()
{
if (_releaseGraphics)
_g.Dispose();
}
#region Delegate graphics methods
public override void DrawLine(RPen pen, double x1, double y1, double x2, double y2)
{
_g.DrawLine(((PenAdapter)pen).Pen, x1, y1, x2, y2);
}
public override void DrawRectangle(RPen pen, double x, double y, double width, double height)
{
_g.DrawRectangle(((PenAdapter)pen).Pen, x, y, width, height);
}
public override void DrawRectangle(RBrush brush, double x, double y, double width, double height)
{
var xBrush = ((BrushAdapter)brush).Brush;
var xTextureBrush = xBrush as XTextureBrush;
if (xTextureBrush != null)
{
xTextureBrush.DrawRectangle(_g, x, y, width, height);
}
else
{
_g.DrawRectangle((XBrush)xBrush, x, y, width, height);
// handle bug in PdfSharp that keeps the brush color for next string draw
if (xBrush is XLinearGradientBrush)
_g.DrawRectangle(XBrushes.White, 0, 0, 0.1, 0.1);
}
}
public override void DrawImage(RImage image, RRect destRect, RRect srcRect)
{
_g.DrawImage(((ImageAdapter)image).Image, Utils.Convert(destRect), Utils.Convert(srcRect), XGraphicsUnit.Point);
}
public override void DrawImage(RImage image, RRect destRect)
{
_g.DrawImage(((ImageAdapter)image).Image, Utils.Convert(destRect));
}
public override void DrawPath(RPen pen, RGraphicsPath path)
{
_g.DrawPath(((PenAdapter)pen).Pen, ((GraphicsPathAdapter)path).GraphicsPath);
}
public override void DrawPath(RBrush brush, RGraphicsPath path)
{
_g.DrawPath((XBrush)((BrushAdapter)brush).Brush, ((GraphicsPathAdapter)path).GraphicsPath);
}
public override void DrawPolygon(RBrush brush, RPoint[] points)
{
if (points != null && points.Length > 0)
{
_g.DrawPolygon((XBrush)((BrushAdapter)brush).Brush, Utils.Convert(points), XFillMode.Winding);
}
}
#endregion
}
}