ntdll: Add ARM64 cpu info.
[wine] / dlls / d3dx9_36 / font.c
1 /*
2  * Copyright (C) 2008 Tony Wasserka
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
25 #include "d3dx9_36_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
28
29 struct d3dx_font
30 {
31     ID3DXFont ID3DXFont_iface;
32     LONG ref;
33
34     IDirect3DDevice9 *device;
35     D3DXFONT_DESCW desc;
36
37     HDC hdc;
38     HFONT hfont;
39 };
40
41 static inline struct d3dx_font *impl_from_ID3DXFont(ID3DXFont *iface)
42 {
43     return CONTAINING_RECORD(iface, struct d3dx_font, ID3DXFont_iface);
44 }
45
46 static HRESULT WINAPI ID3DXFontImpl_QueryInterface(ID3DXFont *iface, REFIID riid, void **out)
47 {
48     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
49
50     if (IsEqualGUID(riid, &IID_ID3DXFont)
51             || IsEqualGUID(riid, &IID_IUnknown))
52     {
53         IUnknown_AddRef(iface);
54         *out = iface;
55         return S_OK;
56     }
57
58     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
59
60     *out = NULL;
61     return E_NOINTERFACE;
62 }
63
64 static ULONG WINAPI ID3DXFontImpl_AddRef(ID3DXFont *iface)
65 {
66     struct d3dx_font *This = impl_from_ID3DXFont(iface);
67     ULONG ref=InterlockedIncrement(&This->ref);
68     TRACE("%p increasing refcount to %u\n", iface, ref);
69     return ref;
70 }
71
72 static ULONG WINAPI ID3DXFontImpl_Release(ID3DXFont *iface)
73 {
74     struct d3dx_font *This = impl_from_ID3DXFont(iface);
75     ULONG ref=InterlockedDecrement(&This->ref);
76
77     TRACE("%p decreasing refcount to %u\n", iface, ref);
78
79     if(ref==0) {
80         DeleteObject(This->hfont);
81         DeleteDC(This->hdc);
82         IDirect3DDevice9_Release(This->device);
83         HeapFree(GetProcessHeap(), 0, This);
84     }
85     return ref;
86 }
87
88 static HRESULT WINAPI ID3DXFontImpl_GetDevice(ID3DXFont *iface, IDirect3DDevice9 **device)
89 {
90     struct d3dx_font *This = impl_from_ID3DXFont(iface);
91
92     TRACE("iface %p, device %p\n", iface, device);
93
94     if( !device ) return D3DERR_INVALIDCALL;
95     *device = This->device;
96     IDirect3DDevice9_AddRef(This->device);
97
98     return D3D_OK;
99 }
100
101 static HRESULT WINAPI ID3DXFontImpl_GetDescA(ID3DXFont *iface, D3DXFONT_DESCA *desc)
102 {
103     struct d3dx_font *This = impl_from_ID3DXFont(iface);
104
105     TRACE("iface %p, desc %p\n", iface, desc);
106
107     if( !desc ) return D3DERR_INVALIDCALL;
108     memcpy(desc, &This->desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
109     WideCharToMultiByte(CP_ACP, 0, This->desc.FaceName, -1, desc->FaceName, sizeof(desc->FaceName) / sizeof(CHAR), NULL, NULL);
110
111     return D3D_OK;
112 }
113
114 static HRESULT WINAPI ID3DXFontImpl_GetDescW(ID3DXFont *iface, D3DXFONT_DESCW *desc)
115 {
116     struct d3dx_font *This = impl_from_ID3DXFont(iface);
117
118     TRACE("iface %p, desc %p\n", iface, desc);
119
120     if( !desc ) return D3DERR_INVALIDCALL;
121     *desc = This->desc;
122
123     return D3D_OK;
124 }
125
126 static BOOL WINAPI ID3DXFontImpl_GetTextMetricsA(ID3DXFont *iface, TEXTMETRICA *metrics)
127 {
128     struct d3dx_font *This = impl_from_ID3DXFont(iface);
129     TRACE("iface %p, metrics %p\n", iface, metrics);
130     return GetTextMetricsA(This->hdc, metrics);
131 }
132
133 static BOOL WINAPI ID3DXFontImpl_GetTextMetricsW(ID3DXFont *iface, TEXTMETRICW *metrics)
134 {
135     struct d3dx_font *This = impl_from_ID3DXFont(iface);
136     TRACE("iface %p, metrics %p\n", iface, metrics);
137     return GetTextMetricsW(This->hdc, metrics);
138 }
139
140 static HDC WINAPI ID3DXFontImpl_GetDC(ID3DXFont *iface)
141 {
142     struct d3dx_font *This = impl_from_ID3DXFont(iface);
143     TRACE("iface %p\n", iface);
144     return This->hdc;
145 }
146
147 static HRESULT WINAPI ID3DXFontImpl_GetGlyphData(ID3DXFont *iface, UINT glyph,
148         IDirect3DTexture9 **texture, RECT *blackbox, POINT *cellinc)
149 {
150     FIXME("iface %p, glyph %#x, texture %p, blackbox %s, cellinc %s stub!\n",
151             iface, glyph, texture, wine_dbgstr_rect(blackbox), wine_dbgstr_point(cellinc));
152     return E_NOTIMPL;
153 }
154
155 static HRESULT WINAPI ID3DXFontImpl_PreloadCharacters(ID3DXFont *iface, UINT first, UINT last)
156 {
157     FIXME("iface %p, first %u, last %u stub!\n", iface, first, last);
158     return E_NOTIMPL;
159 }
160
161 static HRESULT WINAPI ID3DXFontImpl_PreloadGlyphs(ID3DXFont *iface, UINT first, UINT last)
162 {
163     FIXME("iface %p, first %u, last %u stub!\n", iface, first, last);
164     return E_NOTIMPL;
165 }
166
167 static HRESULT WINAPI ID3DXFontImpl_PreloadTextA(ID3DXFont *iface, const char *string, INT count)
168 {
169     FIXME("iface %p, string %s, count %d stub!\n", iface, debugstr_a(string), count);
170     return E_NOTIMPL;
171 }
172
173 static HRESULT WINAPI ID3DXFontImpl_PreloadTextW(ID3DXFont *iface, const WCHAR *string, INT count)
174 {
175     FIXME("iface %p, string %s, count %d stub!\n", iface, debugstr_w(string), count);
176     return E_NOTIMPL;
177 }
178
179 static INT WINAPI ID3DXFontImpl_DrawTextA(ID3DXFont *iface, ID3DXSprite *sprite,
180         const char *string, INT count, RECT *rect, DWORD format, D3DCOLOR color)
181 {
182     FIXME("iface %p, sprite %p, string %s, count %d, rect %s, format %#x, color 0x%08x stub!\n",
183             iface,  sprite, debugstr_a(string), count, wine_dbgstr_rect(rect), format, color);
184     return 1;
185 }
186
187 static INT WINAPI ID3DXFontImpl_DrawTextW(ID3DXFont *iface, ID3DXSprite *sprite,
188         const WCHAR *string, INT count, RECT *rect, DWORD format, D3DCOLOR color)
189 {
190     FIXME("iface %p, sprite %p, string %s, count %d, rect %s, format %#x, color 0x%08x stub!\n",
191             iface,  sprite, debugstr_w(string), count, wine_dbgstr_rect(rect), format, color);
192     return 1;
193 }
194
195 static HRESULT WINAPI ID3DXFontImpl_OnLostDevice(ID3DXFont *iface)
196 {
197     FIXME("iface %p stub!\n", iface);
198     return D3D_OK;
199 }
200
201 static HRESULT WINAPI ID3DXFontImpl_OnResetDevice(ID3DXFont *iface)
202 {
203     FIXME("iface %p stub\n", iface);
204     return D3D_OK;
205 }
206
207 static const ID3DXFontVtbl D3DXFont_Vtbl =
208 {
209     /*** IUnknown methods ***/
210     ID3DXFontImpl_QueryInterface,
211     ID3DXFontImpl_AddRef,
212     ID3DXFontImpl_Release,
213     /*** ID3DXFont methods ***/
214     ID3DXFontImpl_GetDevice,
215     ID3DXFontImpl_GetDescA,
216     ID3DXFontImpl_GetDescW,
217     ID3DXFontImpl_GetTextMetricsA,
218     ID3DXFontImpl_GetTextMetricsW,
219     ID3DXFontImpl_GetDC,
220     ID3DXFontImpl_GetGlyphData,
221     ID3DXFontImpl_PreloadCharacters,
222     ID3DXFontImpl_PreloadGlyphs,
223     ID3DXFontImpl_PreloadTextA,
224     ID3DXFontImpl_PreloadTextW,
225     ID3DXFontImpl_DrawTextA,
226     ID3DXFontImpl_DrawTextW,
227     ID3DXFontImpl_OnLostDevice,
228     ID3DXFontImpl_OnResetDevice
229 };
230
231 HRESULT WINAPI D3DXCreateFontA(struct IDirect3DDevice9 *device, INT height, UINT width,
232         UINT weight, UINT miplevels, BOOL italic, DWORD charset, DWORD precision, DWORD quality,
233         DWORD pitchandfamily, const char *facename, struct ID3DXFont **font)
234 {
235     D3DXFONT_DESCA desc;
236
237     if( !device || !font ) return D3DERR_INVALIDCALL;
238
239     desc.Height=height;
240     desc.Width=width;
241     desc.Weight=weight;
242     desc.MipLevels=miplevels;
243     desc.Italic=italic;
244     desc.CharSet=charset;
245     desc.OutputPrecision=precision;
246     desc.Quality=quality;
247     desc.PitchAndFamily=pitchandfamily;
248     if(facename != NULL) lstrcpyA(desc.FaceName, facename);
249     else desc.FaceName[0] = '\0';
250
251     return D3DXCreateFontIndirectA(device, &desc, font);
252 }
253
254 HRESULT WINAPI D3DXCreateFontW(IDirect3DDevice9 *device, INT height, UINT width, UINT weight, UINT miplevels, BOOL italic, DWORD charset,
255                                DWORD precision, DWORD quality, DWORD pitchandfamily, const WCHAR *facename, ID3DXFont **font)
256 {
257     D3DXFONT_DESCW desc;
258
259     if( !device || !font ) return D3DERR_INVALIDCALL;
260
261     desc.Height=height;
262     desc.Width=width;
263     desc.Weight=weight;
264     desc.MipLevels=miplevels;
265     desc.Italic=italic;
266     desc.CharSet=charset;
267     desc.OutputPrecision=precision;
268     desc.Quality=quality;
269     desc.PitchAndFamily=pitchandfamily;
270     if(facename != NULL) strcpyW(desc.FaceName, facename);
271     else desc.FaceName[0] = '\0';
272
273     return D3DXCreateFontIndirectW(device, &desc, font);
274 }
275
276 /***********************************************************************
277  *           D3DXCreateFontIndirectA    (D3DX9_36.@)
278  */
279 HRESULT WINAPI D3DXCreateFontIndirectA(IDirect3DDevice9 *device, const D3DXFONT_DESCA *desc, ID3DXFont **font)
280 {
281     D3DXFONT_DESCW widedesc;
282
283     if( !device || !desc || !font ) return D3DERR_INVALIDCALL;
284
285     /* Copy everything but the last structure member. This requires the
286        two D3DXFONT_DESC structures to be equal until the FaceName member */
287     memcpy(&widedesc, desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
288     MultiByteToWideChar(CP_ACP, 0, desc->FaceName, -1,
289                         widedesc.FaceName, sizeof(widedesc.FaceName)/sizeof(WCHAR));
290     return D3DXCreateFontIndirectW(device, &widedesc, font);
291 }
292
293 /***********************************************************************
294  *           D3DXCreateFontIndirectW    (D3DX9_36.@)
295  */
296 HRESULT WINAPI D3DXCreateFontIndirectW(IDirect3DDevice9 *device, const D3DXFONT_DESCW *desc, ID3DXFont **font)
297 {
298     D3DDEVICE_CREATION_PARAMETERS cpars;
299     D3DDISPLAYMODE mode;
300     struct d3dx_font *object;
301     IDirect3D9 *d3d;
302     HRESULT hr;
303
304     TRACE("(%p, %p, %p)\n", device, desc, font);
305
306     if( !device || !desc || !font ) return D3DERR_INVALIDCALL;
307
308     /* the device MUST support D3DFMT_A8R8G8B8 */
309     IDirect3DDevice9_GetDirect3D(device, &d3d);
310     IDirect3DDevice9_GetCreationParameters(device, &cpars);
311     IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
312     hr = IDirect3D9_CheckDeviceFormat(d3d, cpars.AdapterOrdinal, cpars.DeviceType, mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8);
313     if(FAILED(hr)) {
314         IDirect3D9_Release(d3d);
315         return D3DXERR_INVALIDDATA;
316     }
317     IDirect3D9_Release(d3d);
318
319     object = HeapAlloc(GetProcessHeap(), 0, sizeof(struct d3dx_font));
320     if(object==NULL) {
321         *font=NULL;
322         return E_OUTOFMEMORY;
323     }
324     object->ID3DXFont_iface.lpVtbl = &D3DXFont_Vtbl;
325     object->ref=1;
326     object->device=device;
327     object->desc=*desc;
328
329     object->hdc = CreateCompatibleDC(NULL);
330     if( !object->hdc ) {
331         HeapFree(GetProcessHeap(), 0, object);
332         return D3DXERR_INVALIDDATA;
333     }
334
335     object->hfont = CreateFontW(desc->Height, desc->Width, 0, 0, desc->Weight, desc->Italic, FALSE, FALSE, desc->CharSet,
336                                 desc->OutputPrecision, CLIP_DEFAULT_PRECIS, desc->Quality, desc->PitchAndFamily, desc->FaceName);
337     if( !object->hfont ) {
338         DeleteDC(object->hdc);
339         HeapFree(GetProcessHeap(), 0, object);
340         return D3DXERR_INVALIDDATA;
341     }
342     SelectObject(object->hdc, object->hfont);
343
344     IDirect3DDevice9_AddRef(device);
345     *font=&object->ID3DXFont_iface;
346
347     return D3D_OK;
348 }