-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathGraphicsAdapter.cs
More file actions
330 lines (283 loc) · 11.5 KB
/
GraphicsAdapter.cs
File metadata and controls
330 lines (283 loc) · 11.5 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// "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 System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using TheArtOfDev.HtmlRenderer.Adapters;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
using TheArtOfDev.HtmlRenderer.Core.Utils;
using TheArtOfDev.HtmlRenderer.WPF.Utilities;
namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
{
/// <summary>
/// Adapter for WPF Graphics.
/// </summary>
internal sealed class GraphicsAdapter : RGraphics
{
#region Fields and Consts
/// <summary>
/// The wrapped WPF graphics object
/// </summary>
private readonly DrawingContext _g;
/// <summary>
/// if to release the graphics object on dispose
/// </summary>
private readonly bool _releaseGraphics;
#endregion
/// <summary>
/// Init.
/// </summary>
/// <param name="g">the WPF graphics object to use</param>
/// <param name="initialClip">the initial clip of the graphics</param>
/// <param name="releaseGraphics">optional: if to release the graphics object on dispose (default - false)</param>
public GraphicsAdapter(DrawingContext g, RRect initialClip, bool releaseGraphics = false)
: base(WpfAdapter.Instance, initialClip)
{
ArgChecker.AssertArgNotNull(g, "g");
_g = g;
_releaseGraphics = releaseGraphics;
}
/// <summary>
/// Init.
/// </summary>
public GraphicsAdapter()
: base(WpfAdapter.Instance, RRect.Empty)
{
_g = null;
_releaseGraphics = false;
}
public override void PopClip()
{
_g.Pop();
_clipStack.Pop();
}
public override void PushClip(RRect rect)
{
_clipStack.Push(rect);
_g.PushClip(new RectangleGeometry(Utils.Convert(rect)));
}
public override void PushClipExclude(RRect rect)
{
var geometry = new CombinedGeometry();
geometry.Geometry1 = new RectangleGeometry(Utils.Convert(_clipStack.Peek()));
geometry.Geometry2 = new RectangleGeometry(Utils.Convert(rect));
geometry.GeometryCombineMode = GeometryCombineMode.Exclude;
_clipStack.Push(_clipStack.Peek());
_g.PushClip(geometry);
}
public override Object SetAntiAliasSmoothingMode()
{
return null;
}
public override void ReturnPreviousSmoothingMode(Object prevMode)
{ }
public override RSize MeasureString(string str, RFont font)
{
double width = 0;
GlyphTypeface glyphTypeface = ((FontAdapter)font).GlyphTypeface;
if (glyphTypeface != null)
{
for (int i = 0; i < str.Length; i++)
{
if (glyphTypeface.CharacterToGlyphMap.ContainsKey(str[i]))
{
ushort glyph = glyphTypeface.CharacterToGlyphMap[str[i]];
double advanceWidth = glyphTypeface.AdvanceWidths[glyph];
width += advanceWidth;
}
else
{
width = 0;
break;
}
}
}
if (width <= 0)
{
var formattedText = new FormattedText(str, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, ((FontAdapter)font).Font, 96d / 72d * font.Size, Brushes.Red, 1.0);
return new RSize(formattedText.WidthIncludingTrailingWhitespace, formattedText.Height);
}
return new RSize(width * font.Size * 96d / 72d, font.Height);
}
public override void MeasureString(string str, RFont font, double maxWidth, out int charFit, out double charFitWidth)
{
charFit = 0;
charFitWidth = 0;
bool handled = false;
GlyphTypeface glyphTypeface = ((FontAdapter)font).GlyphTypeface;
if (glyphTypeface != null)
{
handled = true;
double width = 0;
for (int i = 0; i < str.Length; i++)
{
if (glyphTypeface.CharacterToGlyphMap.ContainsKey(str[i]))
{
ushort glyph = glyphTypeface.CharacterToGlyphMap[str[i]];
double advanceWidth = glyphTypeface.AdvanceWidths[glyph] * font.Size * 96d / 72d;
if (!(width + advanceWidth < maxWidth))
{
charFit = i;
charFitWidth = width;
break;
}
width += advanceWidth;
}
else
{
handled = false;
break;
}
}
}
if (!handled)
{
var formattedText = new FormattedText(str, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, ((FontAdapter)font).Font, 96d / 72d * font.Size, Brushes.Red, 1.0);
charFit = str.Length;
charFitWidth = formattedText.WidthIncludingTrailingWhitespace;
}
}
public override void DrawString(string str, RFont font, RColor color, RPoint point, RSize size, bool rtl)
{
var colorConv = ((BrushAdapter)_adapter.GetSolidBrush(color)).Brush;
bool glyphRendered = false;
GlyphTypeface glyphTypeface = ((FontAdapter)font).GlyphTypeface;
if (glyphTypeface != null)
{
double width = 0;
ushort[] glyphs = new ushort[str.Length];
double[] widths = new double[str.Length];
int i = 0;
for (; i < str.Length; i++)
{
ushort glyph;
if (!glyphTypeface.CharacterToGlyphMap.TryGetValue(str[i], out glyph))
break;
glyphs[i] = glyph;
width += glyphTypeface.AdvanceWidths[glyph];
widths[i] = 96d / 72d * font.Size * glyphTypeface.AdvanceWidths[glyph];
}
if (i >= str.Length)
{
point.Y += glyphTypeface.Baseline * font.Size * 96d / 72d;
point.X += rtl ? 96d / 72d * font.Size * width : 0;
glyphRendered = true;
var wpfPoint = Utils.ConvertRound(point);
var glyphRun = new GlyphRun(glyphTypeface, rtl ? 1 : 0,
false, 96d / 72d * font.Size, 1.0f, glyphs,
wpfPoint, widths, null, null, null, null, null, null);
var guidelines = new GuidelineSet();
guidelines.GuidelinesX.Add(wpfPoint.X);
guidelines.GuidelinesY.Add(wpfPoint.Y);
_g.PushGuidelineSet(guidelines);
_g.DrawGlyphRun(colorConv, glyphRun);
_g.Pop();
}
}
if (!glyphRendered)
{
var formattedText = new FormattedText(str, CultureInfo.CurrentCulture, rtl ? FlowDirection.RightToLeft : FlowDirection.LeftToRight, ((FontAdapter)font).Font, 96d / 72d * font.Size, colorConv, 1.0);
point.X += rtl ? formattedText.Width : 0;
_g.DrawText(formattedText, Utils.ConvertRound(point));
}
}
public override RBrush GetTextureBrush(RImage image, RRect dstRect, RPoint translateTransformLocation)
{
var brush = new ImageBrush(((ImageAdapter)image).Image);
brush.Stretch = Stretch.None;
brush.TileMode = TileMode.Tile;
brush.Viewport = Utils.Convert(dstRect);
brush.ViewportUnits = BrushMappingMode.Absolute;
brush.Transform = new TranslateTransform(translateTransformLocation.X, translateTransformLocation.Y);
brush.Freeze();
return new BrushAdapter(brush);
}
public override RGraphicsPath GetGraphicsPath()
{
return new GraphicsPathAdapter();
}
public override void Dispose()
{
if (_releaseGraphics)
_g.Close();
}
#region Delegate graphics methods
public override void DrawLine(RPen pen, double x1, double y1, double x2, double y2)
{
x1 = (int)x1;
x2 = (int)x2;
y1 = (int)y1;
y2 = (int)y2;
var adj = pen.Width;
if (Math.Abs(x1 - x2) < .1 && Math.Abs(adj % 2 - 1) < .1)
{
x1 += .5;
x2 += .5;
}
if (Math.Abs(y1 - y2) < .1 && Math.Abs(adj % 2 - 1) < .1)
{
y1 += .5;
y2 += .5;
}
_g.DrawLine(((PenAdapter)pen).CreatePen(), new Point(x1, y1), new Point(x2, y2));
}
public override void DrawRectangle(RPen pen, double x, double y, double width, double height)
{
var adj = pen.Width;
if (Math.Abs(adj % 2 - 1) < .1)
{
x += .5;
y += .5;
}
_g.DrawRectangle(null, ((PenAdapter)pen).CreatePen(), new Rect(x, y, width, height));
}
public override void DrawRectangle(RBrush brush, double x, double y, double width, double height)
{
_g.DrawRectangle(((BrushAdapter)brush).Brush, null, new Rect(x, y, width, height));
}
public override void DrawImage(RImage image, RRect destRect, RRect srcRect)
{
CroppedBitmap croppedImage = new CroppedBitmap(((ImageAdapter)image).Image, new Int32Rect((int)srcRect.X, (int)srcRect.Y, (int)srcRect.Width, (int)srcRect.Height));
_g.DrawImage(croppedImage, Utils.ConvertRound(destRect));
}
public override void DrawImage(RImage image, RRect destRect)
{
_g.DrawImage(((ImageAdapter)image).Image, Utils.ConvertRound(destRect));
}
public override void DrawPath(RPen pen, RGraphicsPath path)
{
_g.DrawGeometry(null, ((PenAdapter)pen).CreatePen(), ((GraphicsPathAdapter)path).GetClosedGeometry());
}
public override void DrawPath(RBrush brush, RGraphicsPath path)
{
_g.DrawGeometry(((BrushAdapter)brush).Brush, null, ((GraphicsPathAdapter)path).GetClosedGeometry());
}
public override void DrawPolygon(RBrush brush, RPoint[] points)
{
if (points != null && points.Length > 0)
{
var g = new StreamGeometry();
using (var context = g.Open())
{
context.BeginFigure(Utils.Convert(points[0]), true, true);
for (int i = 1; i < points.Length; i++)
context.LineTo(Utils.Convert(points[i]), true, true);
}
g.Freeze();
_g.DrawGeometry(((BrushAdapter)brush).Brush, null, g);
}
}
#endregion
}
}