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