reg/tests: Use a helper function to delete registry values.
[wine.git] / dlls / d3dx9_36 / font.c
blob5522dea552706e58a6f0ad5fb13194d968b7a198
1 /*
2 * Copyright (C) 2008 Tony Wasserka
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.
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.
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
20 #include "config.h"
21 #include "wine/port.h"
23 #include "d3dx9_private.h"
24 #include "wine/unicode.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
28 struct d3dx_font
30 ID3DXFont ID3DXFont_iface;
31 LONG ref;
33 IDirect3DDevice9 *device;
34 D3DXFONT_DESCW desc;
36 HDC hdc;
37 HFONT hfont;
40 static inline struct d3dx_font *impl_from_ID3DXFont(ID3DXFont *iface)
42 return CONTAINING_RECORD(iface, struct d3dx_font, ID3DXFont_iface);
45 static HRESULT WINAPI ID3DXFontImpl_QueryInterface(ID3DXFont *iface, REFIID riid, void **out)
47 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
49 if (IsEqualGUID(riid, &IID_ID3DXFont)
50 || IsEqualGUID(riid, &IID_IUnknown))
52 IUnknown_AddRef(iface);
53 *out = iface;
54 return S_OK;
57 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
59 *out = NULL;
60 return E_NOINTERFACE;
63 static ULONG WINAPI ID3DXFontImpl_AddRef(ID3DXFont *iface)
65 struct d3dx_font *This = impl_from_ID3DXFont(iface);
66 ULONG ref=InterlockedIncrement(&This->ref);
67 TRACE("%p increasing refcount to %u\n", iface, ref);
68 return ref;
71 static ULONG WINAPI ID3DXFontImpl_Release(ID3DXFont *iface)
73 struct d3dx_font *This = impl_from_ID3DXFont(iface);
74 ULONG ref=InterlockedDecrement(&This->ref);
76 TRACE("%p decreasing refcount to %u\n", iface, ref);
78 if(ref==0) {
79 DeleteObject(This->hfont);
80 DeleteDC(This->hdc);
81 IDirect3DDevice9_Release(This->device);
82 HeapFree(GetProcessHeap(), 0, This);
84 return ref;
87 static HRESULT WINAPI ID3DXFontImpl_GetDevice(ID3DXFont *iface, IDirect3DDevice9 **device)
89 struct d3dx_font *This = impl_from_ID3DXFont(iface);
91 TRACE("iface %p, device %p\n", iface, device);
93 if( !device ) return D3DERR_INVALIDCALL;
94 *device = This->device;
95 IDirect3DDevice9_AddRef(This->device);
97 return D3D_OK;
100 static HRESULT WINAPI ID3DXFontImpl_GetDescA(ID3DXFont *iface, D3DXFONT_DESCA *desc)
102 struct d3dx_font *This = impl_from_ID3DXFont(iface);
104 TRACE("iface %p, desc %p\n", iface, desc);
106 if( !desc ) return D3DERR_INVALIDCALL;
107 memcpy(desc, &This->desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
108 WideCharToMultiByte(CP_ACP, 0, This->desc.FaceName, -1, desc->FaceName, sizeof(desc->FaceName) / sizeof(CHAR), NULL, NULL);
110 return D3D_OK;
113 static HRESULT WINAPI ID3DXFontImpl_GetDescW(ID3DXFont *iface, D3DXFONT_DESCW *desc)
115 struct d3dx_font *This = impl_from_ID3DXFont(iface);
117 TRACE("iface %p, desc %p\n", iface, desc);
119 if( !desc ) return D3DERR_INVALIDCALL;
120 *desc = This->desc;
122 return D3D_OK;
125 static BOOL WINAPI ID3DXFontImpl_GetTextMetricsA(ID3DXFont *iface, TEXTMETRICA *metrics)
127 struct d3dx_font *This = impl_from_ID3DXFont(iface);
128 TRACE("iface %p, metrics %p\n", iface, metrics);
129 return GetTextMetricsA(This->hdc, metrics);
132 static BOOL WINAPI ID3DXFontImpl_GetTextMetricsW(ID3DXFont *iface, TEXTMETRICW *metrics)
134 struct d3dx_font *This = impl_from_ID3DXFont(iface);
135 TRACE("iface %p, metrics %p\n", iface, metrics);
136 return GetTextMetricsW(This->hdc, metrics);
139 static HDC WINAPI ID3DXFontImpl_GetDC(ID3DXFont *iface)
141 struct d3dx_font *This = impl_from_ID3DXFont(iface);
142 TRACE("iface %p\n", iface);
143 return This->hdc;
146 static HRESULT WINAPI ID3DXFontImpl_GetGlyphData(ID3DXFont *iface, UINT glyph,
147 IDirect3DTexture9 **texture, RECT *blackbox, POINT *cellinc)
149 FIXME("iface %p, glyph %#x, texture %p, blackbox %p, cellinc %p stub!\n",
150 iface, glyph, texture, blackbox, cellinc);
151 return E_NOTIMPL;
154 static HRESULT WINAPI ID3DXFontImpl_PreloadCharacters(ID3DXFont *iface, UINT first, UINT last)
156 FIXME("iface %p, first %u, last %u stub!\n", iface, first, last);
157 return S_OK;
160 static HRESULT WINAPI ID3DXFontImpl_PreloadGlyphs(ID3DXFont *iface, UINT first, UINT last)
162 FIXME("iface %p, first %u, last %u stub!\n", iface, first, last);
163 return E_NOTIMPL;
166 static HRESULT WINAPI ID3DXFontImpl_PreloadTextA(ID3DXFont *iface, const char *string, INT count)
168 FIXME("iface %p, string %s, count %d stub!\n", iface, debugstr_a(string), count);
169 return E_NOTIMPL;
172 static HRESULT WINAPI ID3DXFontImpl_PreloadTextW(ID3DXFont *iface, const WCHAR *string, INT count)
174 FIXME("iface %p, string %s, count %d stub!\n", iface, debugstr_w(string), count);
175 return E_NOTIMPL;
178 static INT WINAPI ID3DXFontImpl_DrawTextA(ID3DXFont *iface, ID3DXSprite *sprite,
179 const char *string, INT count, RECT *rect, DWORD format, D3DCOLOR color)
181 FIXME("iface %p, sprite %p, string %s, count %d, rect %s, format %#x, color 0x%08x stub!\n",
182 iface, sprite, debugstr_a(string), count, wine_dbgstr_rect(rect), format, color);
183 return 1;
186 static INT WINAPI ID3DXFontImpl_DrawTextW(ID3DXFont *iface, ID3DXSprite *sprite,
187 const WCHAR *string, INT count, RECT *rect, DWORD format, D3DCOLOR color)
189 FIXME("iface %p, sprite %p, string %s, count %d, rect %s, format %#x, color 0x%08x stub!\n",
190 iface, sprite, debugstr_w(string), count, wine_dbgstr_rect(rect), format, color);
191 return 1;
194 static HRESULT WINAPI ID3DXFontImpl_OnLostDevice(ID3DXFont *iface)
196 FIXME("iface %p stub!\n", iface);
197 return D3D_OK;
200 static HRESULT WINAPI ID3DXFontImpl_OnResetDevice(ID3DXFont *iface)
202 FIXME("iface %p stub\n", iface);
203 return D3D_OK;
206 static const ID3DXFontVtbl D3DXFont_Vtbl =
208 /*** IUnknown methods ***/
209 ID3DXFontImpl_QueryInterface,
210 ID3DXFontImpl_AddRef,
211 ID3DXFontImpl_Release,
212 /*** ID3DXFont methods ***/
213 ID3DXFontImpl_GetDevice,
214 ID3DXFontImpl_GetDescA,
215 ID3DXFontImpl_GetDescW,
216 ID3DXFontImpl_GetTextMetricsA,
217 ID3DXFontImpl_GetTextMetricsW,
218 ID3DXFontImpl_GetDC,
219 ID3DXFontImpl_GetGlyphData,
220 ID3DXFontImpl_PreloadCharacters,
221 ID3DXFontImpl_PreloadGlyphs,
222 ID3DXFontImpl_PreloadTextA,
223 ID3DXFontImpl_PreloadTextW,
224 ID3DXFontImpl_DrawTextA,
225 ID3DXFontImpl_DrawTextW,
226 ID3DXFontImpl_OnLostDevice,
227 ID3DXFontImpl_OnResetDevice
230 HRESULT WINAPI D3DXCreateFontA(struct IDirect3DDevice9 *device, INT height, UINT width,
231 UINT weight, UINT miplevels, BOOL italic, DWORD charset, DWORD precision, DWORD quality,
232 DWORD pitchandfamily, const char *facename, struct ID3DXFont **font)
234 D3DXFONT_DESCA desc;
236 if( !device || !font ) return D3DERR_INVALIDCALL;
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) lstrcpyA(desc.FaceName, facename);
248 else desc.FaceName[0] = '\0';
250 return D3DXCreateFontIndirectA(device, &desc, font);
253 HRESULT WINAPI D3DXCreateFontW(IDirect3DDevice9 *device, INT height, UINT width, UINT weight, UINT miplevels, BOOL italic, DWORD charset,
254 DWORD precision, DWORD quality, DWORD pitchandfamily, const WCHAR *facename, ID3DXFont **font)
256 D3DXFONT_DESCW desc;
258 if( !device || !font ) return D3DERR_INVALIDCALL;
260 desc.Height=height;
261 desc.Width=width;
262 desc.Weight=weight;
263 desc.MipLevels=miplevels;
264 desc.Italic=italic;
265 desc.CharSet=charset;
266 desc.OutputPrecision=precision;
267 desc.Quality=quality;
268 desc.PitchAndFamily=pitchandfamily;
269 if(facename != NULL) strcpyW(desc.FaceName, facename);
270 else desc.FaceName[0] = '\0';
272 return D3DXCreateFontIndirectW(device, &desc, font);
275 /***********************************************************************
276 * D3DXCreateFontIndirectA (D3DX9_36.@)
278 HRESULT WINAPI D3DXCreateFontIndirectA(IDirect3DDevice9 *device, const D3DXFONT_DESCA *desc, ID3DXFont **font)
280 D3DXFONT_DESCW widedesc;
282 if( !device || !desc || !font ) return D3DERR_INVALIDCALL;
284 /* Copy everything but the last structure member. This requires the
285 two D3DXFONT_DESC structures to be equal until the FaceName member */
286 memcpy(&widedesc, desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
287 MultiByteToWideChar(CP_ACP, 0, desc->FaceName, -1,
288 widedesc.FaceName, sizeof(widedesc.FaceName)/sizeof(WCHAR));
289 return D3DXCreateFontIndirectW(device, &widedesc, font);
292 /***********************************************************************
293 * D3DXCreateFontIndirectW (D3DX9_36.@)
295 HRESULT WINAPI D3DXCreateFontIndirectW(IDirect3DDevice9 *device, const D3DXFONT_DESCW *desc, ID3DXFont **font)
297 D3DDEVICE_CREATION_PARAMETERS cpars;
298 D3DDISPLAYMODE mode;
299 struct d3dx_font *object;
300 IDirect3D9 *d3d;
301 HRESULT hr;
303 TRACE("(%p, %p, %p)\n", device, desc, font);
305 if( !device || !desc || !font ) return D3DERR_INVALIDCALL;
307 /* the device MUST support D3DFMT_A8R8G8B8 */
308 IDirect3DDevice9_GetDirect3D(device, &d3d);
309 IDirect3DDevice9_GetCreationParameters(device, &cpars);
310 IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
311 hr = IDirect3D9_CheckDeviceFormat(d3d, cpars.AdapterOrdinal, cpars.DeviceType, mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8);
312 if(FAILED(hr)) {
313 IDirect3D9_Release(d3d);
314 return D3DXERR_INVALIDDATA;
316 IDirect3D9_Release(d3d);
318 object = HeapAlloc(GetProcessHeap(), 0, sizeof(struct d3dx_font));
319 if(object==NULL) {
320 *font=NULL;
321 return E_OUTOFMEMORY;
323 object->ID3DXFont_iface.lpVtbl = &D3DXFont_Vtbl;
324 object->ref=1;
325 object->device=device;
326 object->desc=*desc;
328 object->hdc = CreateCompatibleDC(NULL);
329 if( !object->hdc ) {
330 HeapFree(GetProcessHeap(), 0, object);
331 return D3DXERR_INVALIDDATA;
334 object->hfont = CreateFontW(desc->Height, desc->Width, 0, 0, desc->Weight, desc->Italic, FALSE, FALSE, desc->CharSet,
335 desc->OutputPrecision, CLIP_DEFAULT_PRECIS, desc->Quality, desc->PitchAndFamily, desc->FaceName);
336 if( !object->hfont ) {
337 DeleteDC(object->hdc);
338 HeapFree(GetProcessHeap(), 0, object);
339 return D3DXERR_INVALIDDATA;
341 SelectObject(object->hdc, object->hfont);
343 IDirect3DDevice9_AddRef(device);
344 *font=&object->ID3DXFont_iface;
346 return D3D_OK;