d3dx9: Fix constant table trace.
[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 typedef struct ID3DXFontImpl
30 {
31     ID3DXFont ID3DXFont_iface;
32     LONG ref;
33
34     IDirect3DDevice9 *device;
35     D3DXFONT_DESCW desc;
36
37     HDC hdc;
38     HFONT hfont;
39 } ID3DXFontImpl;
40
41 static inline ID3DXFontImpl *impl_from_ID3DXFont(ID3DXFont *iface)
42 {
43     return CONTAINING_RECORD(iface, ID3DXFontImpl, ID3DXFont_iface);
44 }
45
46 static HRESULT WINAPI ID3DXFontImpl_QueryInterface(ID3DXFont *iface, REFIID riid, void **object)
47 {
48     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
49
50     TRACE("(%p): QueryInterface from %s\n", This, debugstr_guid(riid));
51     if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXFont)) {
52         IUnknown_AddRef(iface);
53         *object=This;
54         return S_OK;
55     }
56     WARN("(%p)->(%s, %p): not found\n", iface, debugstr_guid(riid), *object);
57     return E_NOINTERFACE;
58 }
59
60 static ULONG WINAPI ID3DXFontImpl_AddRef(ID3DXFont *iface)
61 {
62     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
63     ULONG ref=InterlockedIncrement(&This->ref);
64     TRACE("(%p)->(): AddRef from %d\n", This, ref-1);
65     return ref;
66 }
67
68 static ULONG WINAPI ID3DXFontImpl_Release(ID3DXFont *iface)
69 {
70     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
71     ULONG ref=InterlockedDecrement(&This->ref);
72
73     TRACE("(%p)->(): ReleaseRef to %d\n", This, ref);
74
75     if(ref==0) {
76         DeleteObject(This->hfont);
77         DeleteDC(This->hdc);
78         IDirect3DDevice9_Release(This->device);
79         HeapFree(GetProcessHeap(), 0, This);
80     }
81     return ref;
82 }
83
84 static HRESULT WINAPI ID3DXFontImpl_GetDevice(ID3DXFont *iface, LPDIRECT3DDEVICE9 *device)
85 {
86     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
87
88     TRACE("(%p)->(%p)\n", This, device);
89
90     if( !device ) return D3DERR_INVALIDCALL;
91     *device = This->device;
92     IDirect3DDevice9_AddRef(This->device);
93
94     return D3D_OK;
95 }
96
97 static HRESULT WINAPI ID3DXFontImpl_GetDescA(ID3DXFont *iface, D3DXFONT_DESCA *desc)
98 {
99     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
100
101     TRACE("(%p)->(%p)\n", This, desc);
102
103     if( !desc ) return D3DERR_INVALIDCALL;
104     memcpy(desc, &This->desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
105     WideCharToMultiByte(CP_ACP, 0, This->desc.FaceName, -1, desc->FaceName, sizeof(desc->FaceName) / sizeof(CHAR), NULL, NULL);
106
107     return D3D_OK;
108 }
109
110 static HRESULT WINAPI ID3DXFontImpl_GetDescW(ID3DXFont *iface, D3DXFONT_DESCW *desc)
111 {
112     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
113
114     TRACE("(%p)->(%p)\n", This, desc);
115
116     if( !desc ) return D3DERR_INVALIDCALL;
117     *desc = This->desc;
118
119     return D3D_OK;
120 }
121
122 static BOOL WINAPI ID3DXFontImpl_GetTextMetricsA(ID3DXFont *iface, TEXTMETRICA *metrics)
123 {
124     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
125     TRACE("(%p)->(%p)\n", This, metrics);
126     return GetTextMetricsA(This->hdc, metrics);
127 }
128
129 static BOOL WINAPI ID3DXFontImpl_GetTextMetricsW(ID3DXFont *iface, TEXTMETRICW *metrics)
130 {
131     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
132     TRACE("(%p)->(%p)\n", This, metrics);
133     return GetTextMetricsW(This->hdc, metrics);
134 }
135
136 static HDC WINAPI ID3DXFontImpl_GetDC(ID3DXFont *iface)
137 {
138     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
139     TRACE("(%p)->()\n", This);
140     return This->hdc;
141 }
142
143 static HRESULT WINAPI ID3DXFontImpl_GetGlyphData(ID3DXFont *iface, UINT glyph,
144         LPDIRECT3DTEXTURE9 *texture, RECT *blackbox, POINT *cellinc)
145 {
146     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
147     FIXME("(%p)->(%u, %p, %p, %p): stub\n", This, glyph, texture, blackbox, cellinc);
148     return D3D_OK;
149 }
150
151 static HRESULT WINAPI ID3DXFontImpl_PreloadCharacters(ID3DXFont *iface, UINT first, UINT last)
152 {
153     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
154     FIXME("(%p)->(%u, %u): stub\n", This, first, last);
155     return D3D_OK;
156 }
157
158 static HRESULT WINAPI ID3DXFontImpl_PreloadGlyphs(ID3DXFont *iface, UINT first, UINT last)
159 {
160     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
161     FIXME("(%p)->(%u, %u): stub\n", This, first, last);
162     return D3D_OK;
163 }
164
165 static HRESULT WINAPI ID3DXFontImpl_PreloadTextA(ID3DXFont *iface, LPCSTR string, INT count)
166 {
167     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
168     FIXME("(%p)->(%s, %d): stub\n", This, string, count);
169     return D3D_OK;
170 }
171
172 static HRESULT WINAPI ID3DXFontImpl_PreloadTextW(ID3DXFont *iface, LPCWSTR string, INT count)
173 {
174     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
175     FIXME("(%p)->(%s, %d): stub\n", This, debugstr_w(string), count);
176     return D3D_OK;
177 }
178
179 static INT WINAPI ID3DXFontImpl_DrawTextA(ID3DXFont *iface, LPD3DXSPRITE sprite, LPCSTR string,
180         INT count, LPRECT rect, DWORD format, D3DCOLOR color)
181 {
182     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
183     FIXME("(%p)->(%p, %s, %d, %p, %d, %#x): stub\n", This, sprite, string, count, rect,  format, color);
184     return 1;
185 }
186
187 static INT WINAPI ID3DXFontImpl_DrawTextW(ID3DXFont *iface, LPD3DXSPRITE sprite, LPCWSTR string,
188         INT count, LPRECT rect, DWORD format, D3DCOLOR color)
189 {
190     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
191     FIXME("(%p)->(%p, %s, %d, %p, %d, %#x): stub\n", This, sprite, debugstr_w(string), count, rect,  format, color);
192     return 1;
193 }
194
195 static HRESULT WINAPI ID3DXFontImpl_OnLostDevice(ID3DXFont *iface)
196 {
197     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
198     FIXME("(%p)->(): stub\n", This);
199     return D3D_OK;
200 }
201
202 static HRESULT WINAPI ID3DXFontImpl_OnResetDevice(ID3DXFont *iface)
203 {
204     ID3DXFontImpl *This=impl_from_ID3DXFont(iface);
205     FIXME("(%p)->(): stub\n", This);
206     return D3D_OK;
207 }
208
209 static const ID3DXFontVtbl D3DXFont_Vtbl =
210 {
211     /*** IUnknown methods ***/
212     ID3DXFontImpl_QueryInterface,
213     ID3DXFontImpl_AddRef,
214     ID3DXFontImpl_Release,
215     /*** ID3DXFont methods ***/
216     ID3DXFontImpl_GetDevice,
217     ID3DXFontImpl_GetDescA,
218     ID3DXFontImpl_GetDescW,
219     ID3DXFontImpl_GetTextMetricsA,
220     ID3DXFontImpl_GetTextMetricsW,
221     ID3DXFontImpl_GetDC,
222     ID3DXFontImpl_GetGlyphData,
223     ID3DXFontImpl_PreloadCharacters,
224     ID3DXFontImpl_PreloadGlyphs,
225     ID3DXFontImpl_PreloadTextA,
226     ID3DXFontImpl_PreloadTextW,
227     ID3DXFontImpl_DrawTextA,
228     ID3DXFontImpl_DrawTextW,
229     ID3DXFontImpl_OnLostDevice,
230     ID3DXFontImpl_OnResetDevice
231 };
232
233 HRESULT WINAPI D3DXCreateFontA(LPDIRECT3DDEVICE9 device, INT height, UINT width, UINT weight, UINT miplevels, BOOL italic, DWORD charset,
234                                DWORD precision, DWORD quality, DWORD pitchandfamily, LPCSTR facename, LPD3DXFONT *font)
235 {
236     D3DXFONT_DESCA desc;
237
238     if( !device || !font ) return D3DERR_INVALIDCALL;
239
240     desc.Height=height;
241     desc.Width=width;
242     desc.Weight=weight;
243     desc.MipLevels=miplevels;
244     desc.Italic=italic;
245     desc.CharSet=charset;
246     desc.OutputPrecision=precision;
247     desc.Quality=quality;
248     desc.PitchAndFamily=pitchandfamily;
249     if(facename != NULL) lstrcpyA(desc.FaceName, facename);
250     else desc.FaceName[0] = '\0';
251
252     return D3DXCreateFontIndirectA(device, &desc, font);
253 }
254
255 HRESULT WINAPI D3DXCreateFontW(LPDIRECT3DDEVICE9 device, INT height, UINT width, UINT weight, UINT miplevels, BOOL italic, DWORD charset,
256                                DWORD precision, DWORD quality, DWORD pitchandfamily, LPCWSTR facename, LPD3DXFONT *font)
257 {
258     D3DXFONT_DESCW desc;
259
260     if( !device || !font ) return D3DERR_INVALIDCALL;
261
262     desc.Height=height;
263     desc.Width=width;
264     desc.Weight=weight;
265     desc.MipLevels=miplevels;
266     desc.Italic=italic;
267     desc.CharSet=charset;
268     desc.OutputPrecision=precision;
269     desc.Quality=quality;
270     desc.PitchAndFamily=pitchandfamily;
271     if(facename != NULL) strcpyW(desc.FaceName, facename);
272     else desc.FaceName[0] = '\0';
273
274     return D3DXCreateFontIndirectW(device, &desc, font);
275 }
276
277 /***********************************************************************
278  *           D3DXCreateFontIndirectA    (D3DX9_36.@)
279  */
280 HRESULT WINAPI D3DXCreateFontIndirectA(LPDIRECT3DDEVICE9 device, CONST D3DXFONT_DESCA *desc, LPD3DXFONT *font)
281 {
282     D3DXFONT_DESCW widedesc;
283
284     if( !device || !desc || !font ) return D3DERR_INVALIDCALL;
285
286     /* Copy everything but the last structure member. This requires the
287        two D3DXFONT_DESC structures to be equal until the FaceName member */
288     memcpy(&widedesc, desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
289     MultiByteToWideChar(CP_ACP, 0, desc->FaceName, -1,
290                         widedesc.FaceName, sizeof(widedesc.FaceName)/sizeof(WCHAR));
291     return D3DXCreateFontIndirectW(device, &widedesc, font);
292 }
293
294 /***********************************************************************
295  *           D3DXCreateFontIndirectW    (D3DX9_36.@)
296  */
297 HRESULT WINAPI D3DXCreateFontIndirectW(LPDIRECT3DDEVICE9 device, CONST D3DXFONT_DESCW *desc, LPD3DXFONT *font)
298 {
299     D3DDEVICE_CREATION_PARAMETERS cpars;
300     D3DDISPLAYMODE mode;
301     ID3DXFontImpl *object;
302     IDirect3D9 *d3d;
303     HRESULT hr;
304
305     FIXME("(%p, %p, %p): stub\n", device, desc, font);
306
307     if( !device || !desc || !font ) return D3DERR_INVALIDCALL;
308
309     /* the device MUST support D3DFMT_A8R8G8B8 */
310     IDirect3DDevice9_GetDirect3D(device, &d3d);
311     IDirect3DDevice9_GetCreationParameters(device, &cpars);
312     IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
313     hr = IDirect3D9_CheckDeviceFormat(d3d, cpars.AdapterOrdinal, cpars.DeviceType, mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8);
314     if(FAILED(hr)) {
315         IDirect3D9_Release(d3d);
316         return D3DXERR_INVALIDDATA;
317     }
318     IDirect3D9_Release(d3d);
319
320     object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXFontImpl));
321     if(object==NULL) {
322         *font=NULL;
323         return E_OUTOFMEMORY;
324     }
325     object->ID3DXFont_iface.lpVtbl = &D3DXFont_Vtbl;
326     object->ref=1;
327     object->device=device;
328     object->desc=*desc;
329
330     object->hdc = CreateCompatibleDC(NULL);
331     if( !object->hdc ) {
332         HeapFree(GetProcessHeap(), 0, object);
333         return D3DXERR_INVALIDDATA;
334     }
335
336     object->hfont = CreateFontW(desc->Height, desc->Width, 0, 0, desc->Weight, desc->Italic, FALSE, FALSE, desc->CharSet,
337                                 desc->OutputPrecision, CLIP_DEFAULT_PRECIS, desc->Quality, desc->PitchAndFamily, desc->FaceName);
338     if( !object->hfont ) {
339         DeleteDC(object->hdc);
340         HeapFree(GetProcessHeap(), 0, object);
341         return D3DXERR_INVALIDDATA;
342     }
343     SelectObject(object->hdc, object->hfont);
344
345     IDirect3DDevice9_AddRef(device);
346     *font=&object->ID3DXFont_iface;
347
348     return D3D_OK;
349 }