forked from ArthurHub/HTML-Renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinGraphics.cs
More file actions
465 lines (408 loc) · 20.1 KB
/
Copy pathWinGraphics.cs
File metadata and controls
465 lines (408 loc) · 20.1 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// "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.Drawing;
using System.Drawing.Drawing2D;
using HtmlRenderer.Utils;
namespace HtmlRenderer
{
/// <summary>
///
/// </summary>
internal sealed class WinGraphics : IGraphics
{
#region Fields and Consts
/// <summary>
/// used for <see cref="MeasureString(string,System.Drawing.Font,float,out int,out int)"/> calculation.
/// </summary>
private static readonly int[] _charFit = new int[1];
/// <summary>
/// used for <see cref="MeasureString(string,System.Drawing.Font,float,out int,out int)"/> calculation.
/// </summary>
private static readonly int[] _charFitWidth = new int[1000];
/// <summary>
/// Used for GDI+ measure string.
/// </summary>
private static readonly CharacterRange[] _characterRanges = new CharacterRange[1];
/// <summary>
/// The string format to use for measuring strings for GDI+ text rendering
/// </summary>
private static readonly StringFormat _stringFormat;
/// <summary>
/// The string format to use for rendering strings for GDI+ text rendering
/// </summary>
private static readonly StringFormat _stringFormat2;
/// <summary>
/// The wrapped WinForms graphics object
/// </summary>
private readonly Graphics _g;
/// <summary>
/// Use GDI+ text rendering to measure/draw text.
/// </summary>
private readonly bool _useGdiPlusTextRendering;
/// <summary>
/// the initialized HDC used
/// </summary>
private IntPtr _hdc;
/// <summary>
/// If text alignment was set to RTL
/// </summary>
private bool _setRtl;
#endregion
/// <summary>
/// Init static resources.
/// </summary>
static WinGraphics()
{
_stringFormat = new StringFormat(StringFormat.GenericTypographic);
_stringFormat.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.MeasureTrailingSpaces;
_stringFormat2 = new StringFormat(StringFormat.GenericTypographic);
}
/// <summary>
/// Init.
/// </summary>
/// <param name="g">the real graphics to use</param>
/// <param name="useGdiPlusTextRendering">Use GDI+ text rendering to measure/draw text.</param>
public WinGraphics(Graphics g, bool useGdiPlusTextRendering)
{
_g = g;
_useGdiPlusTextRendering = useGdiPlusTextRendering;
}
/// <summary>
/// Gets the bounding clipping region of this graphics.
/// </summary>
/// <returns>The bounding rectangle for the clipping region</returns>
public RectangleF GetClip()
{
if( _hdc == IntPtr.Zero )
{
return _g.ClipBounds;
}
else
{
Rectangle lprc;
Win32Utils.GetClipBox(_hdc, out lprc);
return lprc;
}
}
/// <summary>
/// Sets the clipping region of this <see cref="T:System.Drawing.Graphics"/> to the result of the specified operation combining the current clip region and the rectangle specified by a <see cref="T:System.Drawing.RectangleF"/> structure.
/// </summary>
/// <param name="rect"><see cref="T:System.Drawing.RectangleF"/> structure to combine. </param>
/// <param name="combineMode">Member of the <see cref="T:System.Drawing.Drawing2D.CombineMode"/> enumeration that specifies the combining operation to use. </param>
public void SetClip(RectangleF rect, CombineMode combineMode = CombineMode.Replace)
{
ReleaseHdc();
_g.SetClip(rect, combineMode);
}
/// <summary>
/// Measure the width and height of string <paramref name="str"/> when drawn on device context HDC
/// using the given font <paramref name="font"/>.
/// </summary>
/// <param name="str">the string to measure</param>
/// <param name="font">the font to measure string with</param>
/// <returns>the size of the string</returns>
public Size MeasureString(string str, Font font)
{
if( _useGdiPlusTextRendering )
{
ReleaseHdc();
_characterRanges[0] = new CharacterRange(0, str.Length);
_stringFormat.SetMeasurableCharacterRanges(_characterRanges);
var size = _g.MeasureCharacterRanges(str, font, RectangleF.Empty, _stringFormat)[0].GetBounds(_g).Size;
return new Size((int)Math.Round(size.Width), (int)Math.Round(size.Height));
}
else
{
SetFont(font);
var size = new Size();
Win32Utils.GetTextExtentPoint32(_hdc, str, str.Length, ref size);
return size;
}
}
/// <summary>
/// Measure the width and height of string <paramref name="str"/> when drawn on device context HDC
/// using the given font <paramref name="font"/>.<br/>
/// Restrict the width of the string and get the number of characters able to fit in the restriction and
/// the width those characters take.
/// </summary>
/// <param name="str">the string to measure</param>
/// <param name="font">the font to measure string with</param>
/// <param name="maxWidth">the max width to render the string in</param>
/// <param name="charFit">the number of characters that will fit under <see cref="maxWidth"/> restriction</param>
/// <param name="charFitWidth">the width that only the fitted characters take</param>
/// <returns>the size of the string</returns>
public Size MeasureString(string str, Font font, float maxWidth, out int charFit, out int charFitWidth)
{
charFit = 0;
charFitWidth = 0;
if (_useGdiPlusTextRendering)
{
ReleaseHdc();
var size = MeasureString(str, font);
for(int i = 1; i <= str.Length; i++)
{
charFit = i - 1;
Size pSize = MeasureString(str.Substring(0, i), font);
if (pSize.Height <= size.Height && pSize.Width < maxWidth)
charFitWidth = pSize.Width;
else
break;
}
return size;
}
else
{
SetFont(font);
var size = new Size();
Win32Utils.GetTextExtentExPoint(_hdc, str, str.Length, (int)Math.Round(maxWidth), _charFit, _charFitWidth, ref size);
charFit = _charFit[0];
charFitWidth = charFit > 0 ? _charFitWidth[charFit - 1] : 0;
return size;
}
}
/// <summary>
/// Draw the given string using the given font and foreground color at given location.
/// </summary>
/// <param name="str">the string to draw</param>
/// <param name="font">the font to use to draw the string</param>
/// <param name="color">the text color to set</param>
/// <param name="point">the location to start string draw (top-left)</param>
/// <param name="size">used to know the size of the rendered text for transparent text support</param>
/// <param name="rtl">is to render the string right-to-left (true - RTL, false - LTR)</param>
public void DrawString(String str, Font font, Color color, PointF point, SizeF size, bool rtl)
{
if( _useGdiPlusTextRendering )
{
ReleaseHdc();
SetRtlAlign(rtl);
_g.DrawString(str, font, RenderUtils.GetSolidBrush(color), (int)Math.Round(point.X) + ( rtl ? size.Width : 0 ), (int)Math.Round(point.Y), _stringFormat2);
}
else
{
if( color.A == 255 )
{
SetFont(font);
SetTextColor(color);
SetRtlAlign(rtl);
Win32Utils.TextOut(_hdc, (int)Math.Round(point.X), (int)Math.Round(point.Y), str, str.Length);
}
else
{
InitHdc();
SetRtlAlign(rtl);
DrawTransparentText(_hdc, str, font, new Point((int)Math.Round(point.X), (int)Math.Round(point.Y)), Size.Round(size), color);
}
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
ReleaseHdc();
if(_useGdiPlusTextRendering && _setRtl)
_stringFormat2.FormatFlags ^= StringFormatFlags.DirectionRightToLeft;
}
#region Delegate graphics methods
/// <summary>
/// Gets or sets the rendering quality for this <see cref="T:System.Drawing.Graphics"/>.
/// </summary>
/// <returns>
/// One of the <see cref="T:System.Drawing.Drawing2D.SmoothingMode"/> values.
/// </returns>
/// <PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/></PermissionSet>
public SmoothingMode SmoothingMode
{
get
{
ReleaseHdc();
return _g.SmoothingMode;
}
set
{
ReleaseHdc();
_g.SmoothingMode = value;
}
}
/// <summary>
/// Draws a line connecting the two points specified by the coordinate pairs.
/// </summary>
/// <param name="pen"><see cref="T:System.Drawing.Pen"/> that determines the color, width, and style of the line. </param><param name="x1">The x-coordinate of the first point. </param><param name="y1">The y-coordinate of the first point. </param><param name="x2">The x-coordinate of the second point. </param><param name="y2">The y-coordinate of the second point. </param><exception cref="T:System.ArgumentNullException"><paramref name="pen"/> is null.</exception>
public void DrawLine(Pen pen, float x1, float y1, float x2, float y2)
{
ReleaseHdc();
_g.DrawLine(pen, x1, y1, x2, y2);
}
/// <summary>
/// Draws a rectangle specified by a coordinate pair, a width, and a height.
/// </summary>
/// <param name="pen">A <see cref="T:System.Drawing.Pen"/> that determines the color, width, and style of the rectangle. </param><param name="x">The x-coordinate of the upper-left corner of the rectangle to draw. </param><param name="y">The y-coordinate of the upper-left corner of the rectangle to draw. </param><param name="width">The width of the rectangle to draw. </param><param name="height">The height of the rectangle to draw. </param><exception cref="T:System.ArgumentNullException"><paramref name="pen"/> is null.</exception>
public void DrawRectangle(Pen pen, float x, float y, float width, float height)
{
ReleaseHdc();
_g.DrawRectangle(pen, x, y, width, height);
}
public void FillRectangle(Brush getSolidBrush, float left, float top, float width, float height)
{
ReleaseHdc();
_g.FillRectangle(getSolidBrush, left, top, width, height);
}
/// <summary>
/// Draws the specified portion of the specified <see cref="T:System.Drawing.Image"/> at the specified location and with the specified size.
/// </summary>
/// <param name="image"><see cref="T:System.Drawing.Image"/> to draw. </param>
/// <param name="destRect"><see cref="T:System.Drawing.RectangleF"/> structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle. </param>
/// <param name="srcRect"><see cref="T:System.Drawing.RectangleF"/> structure that specifies the portion of the <paramref name="image"/> object to draw. </param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="image"/> is null.</exception>
public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect)
{
ReleaseHdc();
_g.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
}
/// <summary>
/// Draws the specified <see cref="T:System.Drawing.Image"/> at the specified location and with the specified size.
/// </summary>
/// <param name="image"><see cref="T:System.Drawing.Image"/> to draw. </param><param name="destRect"><see cref="T:System.Drawing.Rectangle"/> structure that specifies the location and size of the drawn image. </param><exception cref="T:System.ArgumentNullException"><paramref name="image"/> is null.</exception><PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/></PermissionSet>
public void DrawImage(Image image, RectangleF destRect)
{
ReleaseHdc();
_g.DrawImage(image, destRect);
}
/// <summary>
/// Draws a <see cref="T:System.Drawing.Drawing2D.GraphicsPath"/>.
/// </summary>
/// <param name="pen"><see cref="T:System.Drawing.Pen"/> that determines the color, width, and style of the path. </param><param name="path"><see cref="T:System.Drawing.Drawing2D.GraphicsPath"/> to draw. </param><exception cref="T:System.ArgumentNullException"><paramref name="pen"/> is null.-or-<paramref name="path"/> is null.</exception><PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/></PermissionSet>
public void DrawPath(Pen pen, GraphicsPath path)
{
_g.DrawPath(pen, path);
}
/// <summary>
/// Fills the interior of a <see cref="T:System.Drawing.Drawing2D.GraphicsPath"/>.
/// </summary>
/// <param name="brush"><see cref="T:System.Drawing.Brush"/> that determines the characteristics of the fill. </param><param name="path"><see cref="T:System.Drawing.Drawing2D.GraphicsPath"/> that represents the path to fill. </param><exception cref="T:System.ArgumentNullException"><paramref name="brush"/> is null.-or-<paramref name="path"/> is null.</exception><PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/></PermissionSet>
public void FillPath(Brush brush, GraphicsPath path)
{
ReleaseHdc();
_g.FillPath(brush, path);
}
/// <summary>
/// Fills the interior of a polygon defined by an array of points specified by <see cref="T:System.Drawing.PointF"/> structures.
/// </summary>
/// <param name="brush"><see cref="T:System.Drawing.Brush"/> that determines the characteristics of the fill. </param><param name="points">Array of <see cref="T:System.Drawing.PointF"/> structures that represent the vertices of the polygon to fill. </param><exception cref="T:System.ArgumentNullException"><paramref name="brush"/> is null.-or-<paramref name="points"/> is null.</exception>
public void FillPolygon(Brush brush, PointF[] points)
{
ReleaseHdc();
_g.FillPolygon(brush, points);
}
#endregion
#region Private methods
/// <summary>
/// Init HDC for the current graphics object to be used to call GDI directly.
/// </summary>
private void InitHdc()
{
if(_hdc == IntPtr.Zero)
{
var clip = _g.Clip.GetHrgn(_g);
_hdc = _g.GetHdc();
_setRtl = false;
Win32Utils.SetBkMode(_hdc, 1);
Win32Utils.SelectClipRgn(_hdc, clip);
Win32Utils.DeleteObject(clip);
}
}
/// <summary>
/// Release current HDC to be able to use <see cref="Graphics"/> methods.
/// </summary>
private void ReleaseHdc()
{
if (_hdc != IntPtr.Zero)
{
Win32Utils.SelectClipRgn(_hdc, IntPtr.Zero);
_g.ReleaseHdc(_hdc);
_hdc = IntPtr.Zero;
}
}
/// <summary>
/// Set a resource (e.g. a font) for the specified device context.
/// WARNING: Calling Font.ToHfont() many times without releasing the font handle crashes the app.
/// </summary>
private void SetFont(Font font)
{
InitHdc();
Win32Utils.SelectObject(_hdc, FontsUtils.GetCachedHFont(font));
}
/// <summary>
/// Set the text color of the device context.
/// </summary>
private void SetTextColor(Color color)
{
InitHdc();
int rgb = ( color.B & 0xFF ) << 16 | ( color.G & 0xFF ) << 8 | color.R;
Win32Utils.SetTextColor(_hdc, rgb);
}
/// <summary>
/// Change text align to Left-to-Right or Right-to-Left if required.
/// </summary>
private void SetRtlAlign(bool rtl)
{
if( _setRtl )
{
if( !rtl )
{
if( _useGdiPlusTextRendering )
_stringFormat2.FormatFlags ^= StringFormatFlags.DirectionRightToLeft;
else
Win32Utils.SetTextAlign(_hdc, Win32Utils.TextAlignDefault);
}
}
else if( rtl )
{
if( _useGdiPlusTextRendering )
_stringFormat2.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
else
Win32Utils.SetTextAlign(_hdc, Win32Utils.TextAlignRtl);
}
_setRtl = rtl;
}
/// <summary>
/// Special draw logic to draw transparent text using GDI.<br/>
/// 1. Create in-memory DC<br/>
/// 2. Copy background to in-memory DC<br/>
/// 3. Draw the text to in-memory DC<br/>
/// 4. Copy the in-memory DC to the proper location with alpha blend<br/>
/// </summary>
private static void DrawTransparentText(IntPtr hdc, string str, Font font, Point point, Size size, Color color)
{
IntPtr dib;
var memoryHdc = Win32Utils.CreateMemoryHdc(hdc, size.Width, size.Height, out dib);
try
{
// copy target background to memory HDC so when copied back it will have the proper background
Win32Utils.BitBlt(memoryHdc, 0, 0, size.Width, size.Height, hdc, point.X, point.Y, Win32Utils.BitBltCopy);
// Create and select font
Win32Utils.SelectObject(memoryHdc, FontsUtils.GetCachedHFont(font));
Win32Utils.SetTextColor(memoryHdc, (color.B & 0xFF) << 16 | (color.G & 0xFF) << 8 | color.R);
// Draw text to memory HDC
Win32Utils.TextOut(memoryHdc, 0, 0, str, str.Length);
// copy from memory HDC to normal HDC with alpha blend so achieve the transparent text
Win32Utils.AlphaBlend(hdc, point.X, point.Y, size.Width, size.Height, memoryHdc, 0, 0, size.Width, size.Height, new BlendFunction(color.A));
}
finally
{
Win32Utils.ReleaseMemoryHdc(memoryHdc, dib);
}
}
#endregion
}
}