Skip to content

Commit f478823

Browse files
authored
Move GDI+ initialization and pointer defs to Core assembly (#13114)
If the initialization is in the core shared assembly this will allow targetted access to GDI+ functionality without pulling in the entire set of imports. This will enable WPF to drop the full reference to System.Drawing.Common for OLE scenarios.
1 parent 6be9429 commit f478823

File tree

13 files changed

+114
-109
lines changed

13 files changed

+114
-109
lines changed

src/System.Drawing.Common/src/System/Drawing/Font.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace System.Drawing;
1616
[TypeConverter(typeof(FontConverter))]
1717
[Serializable]
1818
[Runtime.CompilerServices.TypeForwardedFrom(AssemblyRef.SystemDrawing)]
19-
public sealed unsafe class Font : MarshalByRefObject, ICloneable, IDisposable, ISerializable
19+
public sealed unsafe class Font : MarshalByRefObject, ICloneable, IDisposable, ISerializable, IPointer<GpFont>
2020
{
2121
[NonSerialized]
2222
private GpFont* _nativeFont;
@@ -29,9 +29,10 @@ public sealed unsafe class Font : MarshalByRefObject, ICloneable, IDisposable, I
2929
private string _systemFontName = string.Empty;
3030
private string? _originalFontName;
3131

32-
// Return value is in Unit (the unit the font was created in)
32+
nint IPointer<GpFont>.Pointer => (nint)_nativeFont;
33+
3334
/// <summary>
34-
/// Gets the size of this <see cref='Font'/>.
35+
/// Gets the em-size of this <see cref="Font"/> measured in the units specified by the <see cref="Unit"/> property.
3536
/// </summary>
3637
public float Size => _fontSize;
3738

src/System.Drawing.Common/src/System/Drawing/Image.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public void Save(string filename, ImageFormat format)
230230
Guid encoder = format.Encoder;
231231
if (encoder == Guid.Empty)
232232
{
233-
encoder = ImageCodecInfoHelper.GetEncoderClsid(PInvokeGdiPlus.ImageFormatPNG);
233+
encoder = ImageCodecInfoHelper.GetEncoderClsid(PInvokeCore.ImageFormatPNG);
234234
}
235235

236236
Save(filename, encoder, null);

src/System.Drawing.Common/src/System/Drawing/Imaging/ImageFormat.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ public sealed class ImageFormat
1414
{
1515
// Format IDs
1616
// private static ImageFormat undefined = new ImageFormat(new Guid("{b96b3ca9-0728-11d3-9d7b-0000f81ef32e}"));
17-
private static readonly ImageFormat s_memoryBMP = new(PInvokeGdiPlus.ImageFormatMemoryBMP);
18-
private static readonly ImageFormat s_bmp = new(PInvokeGdiPlus.ImageFormatBMP);
19-
private static readonly ImageFormat s_emf = new(PInvokeGdiPlus.ImageFormatEMF);
20-
private static readonly ImageFormat s_wmf = new(PInvokeGdiPlus.ImageFormatWMF);
21-
private static readonly ImageFormat s_jpeg = new(PInvokeGdiPlus.ImageFormatJPEG);
22-
private static readonly ImageFormat s_png = new(PInvokeGdiPlus.ImageFormatPNG);
23-
private static readonly ImageFormat s_gif = new(PInvokeGdiPlus.ImageFormatGIF);
24-
private static readonly ImageFormat s_tiff = new(PInvokeGdiPlus.ImageFormatTIFF);
25-
private static readonly ImageFormat s_exif = new(PInvokeGdiPlus.ImageFormatEXIF);
26-
private static readonly ImageFormat s_icon = new(PInvokeGdiPlus.ImageFormatIcon);
27-
private static readonly ImageFormat s_heif = new(PInvokeGdiPlus.ImageFormatHEIF);
28-
private static readonly ImageFormat s_webp = new(PInvokeGdiPlus.ImageFormatWEBP);
17+
private static readonly ImageFormat s_memoryBMP = new(PInvokeCore.ImageFormatMemoryBMP);
18+
private static readonly ImageFormat s_bmp = new(PInvokeCore.ImageFormatBMP);
19+
private static readonly ImageFormat s_emf = new(PInvokeCore.ImageFormatEMF);
20+
private static readonly ImageFormat s_wmf = new(PInvokeCore.ImageFormatWMF);
21+
private static readonly ImageFormat s_jpeg = new(PInvokeCore.ImageFormatJPEG);
22+
private static readonly ImageFormat s_png = new(PInvokeCore.ImageFormatPNG);
23+
private static readonly ImageFormat s_gif = new(PInvokeCore.ImageFormatGIF);
24+
private static readonly ImageFormat s_tiff = new(PInvokeCore.ImageFormatTIFF);
25+
private static readonly ImageFormat s_exif = new(PInvokeCore.ImageFormatEXIF);
26+
private static readonly ImageFormat s_icon = new(PInvokeCore.ImageFormatIcon);
27+
private static readonly ImageFormat s_heif = new(PInvokeCore.ImageFormatHEIF);
28+
private static readonly ImageFormat s_webp = new(PInvokeCore.ImageFormatWEBP);
2929

3030
private readonly Guid _guid;
3131

@@ -128,62 +128,62 @@ public sealed class ImageFormat
128128
/// </summary>
129129
public override string ToString()
130130
{
131-
if (Guid == PInvokeGdiPlus.ImageFormatMemoryBMP)
131+
if (Guid == PInvokeCore.ImageFormatMemoryBMP)
132132
{
133133
return "MemoryBMP";
134134
}
135135

136-
if (Guid == PInvokeGdiPlus.ImageFormatBMP)
136+
if (Guid == PInvokeCore.ImageFormatBMP)
137137
{
138138
return "Bmp";
139139
}
140140

141-
if (Guid == PInvokeGdiPlus.ImageFormatEMF)
141+
if (Guid == PInvokeCore.ImageFormatEMF)
142142
{
143143
return "Emf";
144144
}
145145

146-
if (Guid == PInvokeGdiPlus.ImageFormatWMF)
146+
if (Guid == PInvokeCore.ImageFormatWMF)
147147
{
148148
return "Wmf";
149149
}
150150

151-
if (Guid == PInvokeGdiPlus.ImageFormatGIF)
151+
if (Guid == PInvokeCore.ImageFormatGIF)
152152
{
153153
return "Gif";
154154
}
155155

156-
if (Guid == PInvokeGdiPlus.ImageFormatJPEG)
156+
if (Guid == PInvokeCore.ImageFormatJPEG)
157157
{
158158
return "Jpeg";
159159
}
160160

161-
if (Guid == PInvokeGdiPlus.ImageFormatPNG)
161+
if (Guid == PInvokeCore.ImageFormatPNG)
162162
{
163163
return "Png";
164164
}
165165

166-
if (Guid == PInvokeGdiPlus.ImageFormatTIFF)
166+
if (Guid == PInvokeCore.ImageFormatTIFF)
167167
{
168168
return "Tiff";
169169
}
170170

171-
if (Guid == PInvokeGdiPlus.ImageFormatEXIF)
171+
if (Guid == PInvokeCore.ImageFormatEXIF)
172172
{
173173
return "Exif";
174174
}
175175

176-
if (Guid == PInvokeGdiPlus.ImageFormatIcon)
176+
if (Guid == PInvokeCore.ImageFormatIcon)
177177
{
178178
return "Icon";
179179
}
180180

181-
if (Guid == PInvokeGdiPlus.ImageFormatHEIF)
181+
if (Guid == PInvokeCore.ImageFormatHEIF)
182182
{
183183
return "Heif";
184184
}
185185

186-
if (Guid == PInvokeGdiPlus.ImageFormatWEBP)
186+
if (Guid == PInvokeCore.ImageFormatWEBP)
187187
{
188188
return "Webp";
189189
}

src/System.Private.Windows.Core/src/NativeMethods.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ FACILITY_CODE
9090
fdex*
9191
FDEX_PROP_FLAGS
9292
FILETIME
93+
GdiplusStartup
94+
GdiplusStartupInputEx
9395
GET_CLASS_LONG_INDEX
9496
GetClientRect
9597
GetClipboardFormatName
@@ -126,6 +128,12 @@ GlobalLock
126128
GlobalReAlloc
127129
GlobalSize
128130
GlobalUnlock
131+
GpBitmap
132+
GpFont
133+
GpGraphics
134+
GpImage
135+
GpMetafile
136+
GpRegion
129137
HBITMAP
130138
HBMMENU_*
131139
HBRUSH
@@ -152,6 +160,7 @@ IDropTargetHelper
152160
IEnumFORMATETC
153161
IEnumUnknown
154162
IGlobalInterfaceTable
163+
ImageFormat*
155164
ImageLockMode
156165
INK_SERIALIZED_FORMAT
157166
INPLACE_E_NOTOOLSPACE
@@ -201,6 +210,7 @@ OleInitialize
201210
OleSetClipboard
202211
OleUninitialize
203212
PeekMessage
213+
PixelFormat*
204214
POINTS
205215
PostMessage
206216
PRINTDLGEX_FLAGS

src/System.Private.Windows.Core/src/Resources/SR.resx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,64 @@
176176
<data name="Clipboard_InvalidPath" xml:space="preserve">
177177
<value>Path "{0}" in the argument "{1}" is not valid.</value>
178178
</data>
179+
<data name="GdiplusAborted" xml:space="preserve">
180+
<value>Function was ended.</value>
181+
</data>
182+
<data name="GdiplusAccessDenied" xml:space="preserve">
183+
<value>File access is denied.</value>
184+
</data>
185+
<data name="GdiplusFileNotFound" xml:space="preserve">
186+
<value>File not found.</value>
187+
</data>
188+
<data name="GdiplusGenericError" xml:space="preserve">
189+
<value>A generic error occurred in GDI+.</value>
190+
</data>
191+
<data name="GdiplusInsufficientBuffer" xml:space="preserve">
192+
<value>Buffer is too small (internal GDI+ error).</value>
193+
</data>
194+
<data name="GdiplusInvalidParameter" xml:space="preserve">
195+
<value>Parameter is not valid.</value>
196+
</data>
197+
<data name="GdiplusOutOfMemory" xml:space="preserve">
198+
<value>An object could not be created, possibly due to a lack of memory, but most likely due to invalid input.</value>
199+
</data>
200+
<data name="GdiplusNotImplemented" xml:space="preserve">
201+
<value>Not implemented.</value>
202+
</data>
203+
<data name="GdiplusOverflow" xml:space="preserve">
204+
<value>Overflow error.</value>
205+
</data>
206+
<data name="GdiplusPropertyNotFoundError" xml:space="preserve">
207+
<value>Property cannot be found.</value>
208+
</data>
209+
<data name="GdiplusPropertyNotSupportedError" xml:space="preserve">
210+
<value>Property is not supported.</value>
211+
</data>
212+
<data name="GdiplusUnknown" xml:space="preserve">
213+
<value>Unknown GDI+ error occurred.</value>
214+
</data>
215+
<data name="GdiplusWrongState" xml:space="preserve">
216+
<value>Bitmap region is already locked.</value>
217+
</data>
218+
<data name="GdiplusUnknownImageFormat" xml:space="preserve">
219+
<value>Image format is unknown.</value>
220+
</data>
221+
<data name="GdiplusUnsupportedGdiplusVersion" xml:space="preserve">
222+
<value>Current version of GDI+ does not support this feature.</value>
223+
</data>
224+
<data name="GdiplusFontFamilyNotFound" xml:space="preserve">
225+
<value>Font '{0}' cannot be found.</value>
226+
</data>
227+
<data name="GdiplusFontStyleNotFound" xml:space="preserve">
228+
<value>Font '{0}' does not support style '{1}'.</value>
229+
</data>
230+
<data name="GdiplusNotInitialized" xml:space="preserve">
231+
<value>GDI+ is not properly initialized (internal GDI+ error).</value>
232+
</data>
233+
<data name="GdiplusObjectBusy" xml:space="preserve">
234+
<value>Object is currently in use elsewhere.</value>
235+
</data>
236+
<data name="GdiplusNotTrueTypeFont_NoName" xml:space="preserve">
237+
<value>Only TrueType fonts are supported. This is not a TrueType font.</value>
238+
</data>
179239
</root>

src/System.Private.Windows.GdiPlus/Windows/Win32/Graphics/GdiPlus/GdiPlusInitialization.cs renamed to src/System.Private.Windows.Core/src/Windows/Win32/Graphics/GdiPlus/GdiPlusInitialization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private static unsafe nuint Init()
1919

2020
nuint token;
2121
GdiplusStartupInputEx startup = GdiplusStartupInputEx.GetDefault();
22-
PInvokeGdiPlus.GdiplusStartup(&token, (GdiplusStartupInput*)&startup, null).ThrowIfFailed();
22+
PInvokeCore.GdiplusStartup(&token, (GdiplusStartupInput*)&startup, null).ThrowIfFailed();
2323
return token;
2424
}
2525

src/System.Private.Windows.GdiPlus/Windows/Win32/Graphics/GdiPlus/StatusExtensions.cs renamed to src/System.Private.Windows.Core/src/Windows/Win32/Graphics/GdiPlus/StatusExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.InteropServices;
5-
using System.Private.Windows.GdiPlus.Resources;
65

76
namespace Windows.Win32.Graphics.GdiPlus;
87

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GDI+ interop should primarily live in System.Private.Windows.Core.GdiPlus. Only initialization and handles
2+
should be defined here to allow WPF to use targeted GDI+ functionality without having to take the entirety of
3+
System.Private.Windows.Core.GdiPlus.

src/System.Private.Windows.GdiPlus/NativeMethods.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,6 @@ GdipLoadImageFromFile
360360
GdipLoadImageFromFileICM
361361
GdipLoadImageFromStream
362362
GdipLoadImageFromStreamICM
363-
GdiplusStartup
364-
GdiplusStartupInputEx
365363
GdipMeasureCharacterRanges
366364
GdipMeasureString
367365
GdipMultiplyLineTransform
@@ -527,9 +525,3 @@ GdipVectorTransformMatrixPoints
527525
GdipVectorTransformMatrixPointsI
528526
GdipWarpPath
529527
GdipWidenPath
530-
GpBitmap
531-
GpGraphics
532-
GpImage
533-
GpRegion
534-
ImageFormat*
535-
PixelFormat*

0 commit comments

Comments
 (0)