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