-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGdip.cs
executable file
·375 lines (316 loc) · 15.1 KB
/
Gdip.cs
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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using engenious.Helper;
namespace engenious.Pipeline
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal unsafe struct LOGFONT
{
private const int LF_FACESIZE = 32;
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
private fixed char _lfFaceName[LF_FACESIZE];
public Span<char> lfFaceName => MemoryMarshal.CreateSpan(ref _lfFaceName[0], LF_FACESIZE);
public override string ToString()
{
return
"lfHeight=" + lfHeight + ", " +
"lfWidth=" + lfWidth + ", " +
"lfEscapement=" + lfEscapement + ", " +
"lfOrientation=" + lfOrientation + ", " +
"lfWeight=" + lfWeight + ", " +
"lfItalic=" + lfItalic + ", " +
"lfUnderline=" + lfUnderline + ", " +
"lfStrikeOut=" + lfStrikeOut + ", " +
"lfCharSet=" + lfCharSet + ", " +
"lfOutPrecision=" + lfOutPrecision + ", " +
"lfClipPrecision=" + lfClipPrecision + ", " +
"lfQuality=" + lfQuality + ", " +
"lfPitchAndFamily=" + lfPitchAndFamily + ", " +
"lfFaceName=" + lfFaceName.ToString();
}
}
internal partial class Gdip
{
/// <summary>
/// Simple wrapper to create a screen HDC within a using statement.
/// </summary>
internal struct ScreenDC : IDisposable
{
private const string User32LibraryName = "user32.dll";
private IntPtr _handle;
public static ScreenDC Create() => new ScreenDC
{
_handle = GetDC(IntPtr.Zero)
};
public static implicit operator IntPtr(ScreenDC screenDC) => screenDC._handle;
public void Dispose() => ReleaseDC(IntPtr.Zero, _handle);
[DllImport(User32LibraryName, ExactSpelling = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport(User32LibraryName, ExactSpelling = true)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
}
[StructLayout(LayoutKind.Sequential)]
internal struct StartupInputEx
{
public int GdiplusVersion; // Must be 1 or 2
public IntPtr DebugEventCallback;
public bool SuppressBackgroundThread; // FALSE unless you're prepared to call
// the hook/unhook functions properly
public bool SuppressExternalCodecs; // FALSE unless you want GDI+ only to use
// its internal image codecs.
public int StartupParameters;
public static StartupInputEx GetDefault()
{
OperatingSystem os = Environment.OSVersion;
StartupInputEx result = default;
// In Windows 7 GDI+1.1 story is different as there are different binaries per GDI+ version.
bool isWindows7 = os.Platform == PlatformID.Win32NT && os.Version.Major == 6 && os.Version.Minor == 1;
result.GdiplusVersion = isWindows7 ? 1 : 2;
result.SuppressBackgroundThread = false;
result.SuppressExternalCodecs = false;
result.StartupParameters = 0;
return result;
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct StartupOutput
{
// The following 2 fields won't be used. They were originally intended
// for getting GDI+ to run on our thread - however there are marshalling
// dealing with function *'s and what not - so we make explicit calls
// to gdi+ after the fact, via the GdiplusNotificationHook and
// GdiplusNotificationUnhook methods.
public IntPtr hook; //not used
public IntPtr unhook; //not used.
}
internal enum GraphicsUnit
{
/// <summary>Specifies the world coordinate system unit as the unit of measure.</summary>
World,
/// <summary>Specifies the unit of measure of the display device. Typically pixels for video displays, and 1/100 inch for printers.</summary>
Display,
/// <summary>Specifies a device pixel as the unit of measure.</summary>
Pixel,
/// <summary>Specifies a printer's point (1/72 inch) as the unit of measure.</summary>
Point,
/// <summary>Specifies the inch as the unit of measure.</summary>
Inch,
/// <summary>Specifies the document unit (1/300 inch) as the unit of measure.</summary>
Document,
/// <summary>Specifies the millimeter as the unit of measure.</summary>
Millimeter,
}
private static readonly IntPtr s_initToken;
static Gdip()
{
if (PlatformHelper.RunningPlatform() != Platform.Windows)
return;
Debug.Assert(s_initToken == IntPtr.Zero, "GdiplusInitialization: Initialize should not be called more than once in the same domain!");
int status = GdiplusStartup(out s_initToken, StartupInputEx.GetDefault(), out _);
CheckStatus(status);
}
private const string GdipLibraryName = "gdiplus.dll";
private const string GdiLibraryName = "gdi32.dll";
[DllImport(GdipLibraryName)]
internal static extern int GdiplusStartup(out IntPtr token, in StartupInputEx input, out StartupOutput output);
[DllImport(GdipLibraryName, CharSet = CharSet.Unicode)]
internal static extern int GdipCreateFontFamilyFromName(string name, IntPtr fontCollection,
out IntPtr FontFamily);
[DllImport(GdipLibraryName)]
internal static extern int GdipGetGenericFontFamilySansSerif(out IntPtr fontfamily);
[DllImport(GdipLibraryName)]
internal static extern int GdipCreateFont(IntPtr fontFamily, float emSize, FontStyle style, GraphicsUnit unit,
out IntPtr font);
[DllImport(GdipLibraryName)]
internal static extern int GdipGetLogFontW(IntPtr font, IntPtr graphics, ref LOGFONT lf);
[DllImport(GdipLibraryName)]
internal static extern int GdipCreateFromHDC(IntPtr hdc, out IntPtr graphics);
[DllImport(GdipLibraryName)]
internal static extern int GdipDeleteGraphics(IntPtr graphics);
[DllImport(GdiLibraryName, ExactSpelling = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr CreateFontIndirectW(ref LOGFONT lplf);
private static IntPtr GetGdipGenericSansSerif()
{
IntPtr nativeFamily;
int status = Gdip.GdipGetGenericFontFamilySansSerif(out nativeFamily);
Gdip.CheckStatus(status);
return nativeFamily;
}
public static bool TryCreateFontFamily(string name, out IntPtr fontfamily)
{
IntPtr nativeFontCollection = IntPtr.Zero;
int status = Gdip.GdipCreateFontFamilyFromName(name, nativeFontCollection, out fontfamily);
if (status != Gdip.Ok)
{
fontfamily = GetGdipGenericSansSerif(); // This throws if failed.
return false;
}
return true;
}
public static IntPtr CreateHFont(IntPtr fontFamily, float emSize, FontStyle fontStyle)
{
if (float.IsNaN(emSize) || float.IsInfinity(emSize) || emSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(emSize));
}
var gdiFont = CreateNativeFont(fontFamily, emSize, fontStyle);
return ToHfont(gdiFont);
}
private static IntPtr CreateNativeFont(IntPtr fontFamily, float fontSize, FontStyle fontStyle)
{
// Note: GDI+ creates singleton font family objects (from the corresponding font file) and reference count them so
// if creating the font object from an external FontFamily, this object's FontFamily will share the same native object.
int status = Gdip.GdipCreateFont(
fontFamily,
fontSize,
fontStyle,
GraphicsUnit.Point,
out var nativeFont);
// Special case this common error message to give more information
if (status == Gdip.FontStyleNotFound)
{
throw new ArgumentException("Font style not found!");
}
else if (status != Gdip.Ok)
{
throw Gdip.StatusException(status);
}
return nativeFont;
}
private static unsafe LOGFONT ToLogFontInternal(IntPtr nativeFont, IntPtr nativeGraphics)
{
LOGFONT logFont = default;
Gdip.CheckStatus(Gdip.GdipGetLogFontW(nativeFont, nativeGraphics, ref logFont));
if (logFont.lfCharSet == 0)
{
logFont.lfCharSet = DEFAULT_CHARSET;
}
return logFont;
}
public static IntPtr ToHfont(IntPtr nativeFont)
{
IntPtr nativeGraphics = IntPtr.Zero;
using var dc = ScreenDC.Create();
try
{
var status = Gdip.GdipCreateFromHDC(dc, out nativeGraphics);
Gdip.CheckStatus(status);
LOGFONT lf = ToLogFontInternal(nativeFont, nativeGraphics);
IntPtr handle = CreateFontIndirectW(ref lf);
if (handle == IntPtr.Zero)
{
throw new Win32Exception();
}
return handle;
}
finally
{
if (nativeGraphics != IntPtr.Zero)
Gdip.GdipDeleteGraphics(nativeGraphics);
}
}
//----------------------------------------------------------------------------------------
// Status codes
//----------------------------------------------------------------------------------------
internal const int Ok = 0;
internal const int GenericError = 1;
internal const int InvalidParameter = 2;
internal const int OutOfMemory = 3;
internal const int ObjectBusy = 4;
internal const int InsufficientBuffer = 5;
internal const int NotImplemented = 6;
internal const int Win32Error = 7;
internal const int WrongState = 8;
internal const int Aborted = 9;
internal const int FileNotFound = 10;
internal const int ValueOverflow = 11;
internal const int AccessDenied = 12;
internal const int UnknownImageFormat = 13;
internal const int FontFamilyNotFound = 14;
internal const int FontStyleNotFound = 15;
internal const int NotTrueTypeFont = 16;
internal const int UnsupportedGdiplusVersion = 17;
internal const int GdiplusNotInitialized = 18;
internal const int PropertyNotFound = 19;
internal const int PropertyNotSupported = 20;
internal const int DEFAULT_CHARSET = 1;
internal static void CheckStatus(int status)
{
if (status != Ok)
throw StatusException(status);
}
internal static Exception StatusException(int status)
{
Debug.Assert(status != Ok, "Throwing an exception for an 'Ok' return code");
// switch (status)
// {
// case GenericError:
// return new ExternalException(SR.GdiplusGenericError, E_FAIL);
// case InvalidParameter:
// return new ArgumentException(SR.GdiplusInvalidParameter);
// case OutOfMemory:
// return new OutOfMemoryException(SR.GdiplusOutOfMemory);
// case ObjectBusy:
// return new InvalidOperationException(SR.GdiplusObjectBusy);
// case InsufficientBuffer:
// return new OutOfMemoryException(SR.GdiplusInsufficientBuffer);
// case NotImplemented:
// return new NotImplementedException(SR.GdiplusNotImplemented);
// case Win32Error:
// return new ExternalException(SR.GdiplusGenericError, E_FAIL);
// case WrongState:
// return new InvalidOperationException(SR.GdiplusWrongState);
// case Aborted:
// return new ExternalException(SR.GdiplusAborted, E_ABORT);
// case FileNotFound:
// return new FileNotFoundException(SR.GdiplusFileNotFound);
// case ValueOverflow:
// return new OverflowException(SR.GdiplusOverflow);
// case AccessDenied:
// return new ExternalException(SR.GdiplusAccessDenied, E_ACCESSDENIED);
// case UnknownImageFormat:
// return new ArgumentException(SR.GdiplusUnknownImageFormat);
// case PropertyNotFound:
// return new ArgumentException(SR.GdiplusPropertyNotFoundError);
// case PropertyNotSupported:
// return new ArgumentException(SR.GdiplusPropertyNotSupportedError);
//
// case FontFamilyNotFound:
// Debug.Fail("We should be special casing FontFamilyNotFound so we can provide the font name");
// return new ArgumentException(SR.Format(SR.GdiplusFontFamilyNotFound, "?"));
//
// case FontStyleNotFound:
// Debug.Fail("We should be special casing FontStyleNotFound so we can provide the font name");
// return new ArgumentException(SR.Format(SR.GdiplusFontStyleNotFound, "?", "?"));
//
// case NotTrueTypeFont:
// Debug.Fail("We should be special casing NotTrueTypeFont so we can provide the font name");
// return new ArgumentException(SR.GdiplusNotTrueTypeFont_NoName);
//
// case UnsupportedGdiplusVersion:
// return new ExternalException(SR.GdiplusUnsupportedGdiplusVersion, E_FAIL);
//
// case GdiplusNotInitialized:
// return new ExternalException(SR.GdiplusNotInitialized, E_FAIL);
// }
//
// return new ExternalException($"{SR.GdiplusUnknown} [{status}]", E_UNEXPECTED);
return new Exception();
}
}
}