dwrite: Don't overallocate memory for a BITMAPINFO.
[wine.git] / dlls / dwrite / gdiinterop.c
blob496f099f3dfd9414df67994aecbae004a1e1f316
1 /*
2 * GDI Interop
4 * Copyright 2012 Nikolay Sivov for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "dwrite.h"
29 #include "dwrite_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
35 struct rendertarget {
36 IDWriteBitmapRenderTarget IDWriteBitmapRenderTarget_iface;
37 LONG ref;
39 SIZE size;
40 HDC hdc;
43 static inline struct rendertarget *impl_from_IDWriteBitmapRenderTarget(IDWriteBitmapRenderTarget *iface)
45 return CONTAINING_RECORD(iface, struct rendertarget, IDWriteBitmapRenderTarget_iface);
48 static HRESULT WINAPI rendertarget_QueryInterface(IDWriteBitmapRenderTarget *iface, REFIID riid, void **obj)
50 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
52 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
54 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget))
56 *obj = iface;
57 IDWriteBitmapRenderTarget_AddRef(iface);
58 return S_OK;
61 *obj = NULL;
63 return E_NOINTERFACE;
66 static ULONG WINAPI rendertarget_AddRef(IDWriteBitmapRenderTarget *iface)
68 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
69 ULONG ref = InterlockedIncrement(&This->ref);
70 TRACE("(%p)->(%d)\n", This, ref);
71 return ref;
74 static ULONG WINAPI rendertarget_Release(IDWriteBitmapRenderTarget *iface)
76 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
77 ULONG ref = InterlockedDecrement(&This->ref);
79 TRACE("(%p)->(%d)\n", This, ref);
81 if (!ref)
83 DeleteDC(This->hdc);
84 heap_free(This);
87 return ref;
90 static HRESULT WINAPI rendertarget_DrawGlyphRun(IDWriteBitmapRenderTarget *iface,
91 FLOAT baselineOriginX, FLOAT baselineOriginY, DWRITE_MEASURING_MODE measuring_mode,
92 DWRITE_GLYPH_RUN const* glyph_run, IDWriteRenderingParams* params, COLORREF textColor,
93 RECT *blackbox_rect)
95 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
96 FIXME("(%p)->(%f %f %d %p %p 0x%08x %p): stub\n", This, baselineOriginX, baselineOriginY,
97 measuring_mode, glyph_run, params, textColor, blackbox_rect);
98 return E_NOTIMPL;
101 static HDC WINAPI rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget *iface)
103 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
104 TRACE("(%p)\n", This);
105 return This->hdc;
108 static FLOAT WINAPI rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget *iface)
110 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
111 FIXME("(%p): stub\n", This);
112 return 1.0;
115 static HRESULT WINAPI rendertarget_SetPixelsPerDip(IDWriteBitmapRenderTarget *iface, FLOAT pixels_per_dip)
117 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
118 FIXME("(%p)->(%f): stub\n", This, pixels_per_dip);
119 return E_NOTIMPL;
122 static HRESULT WINAPI rendertarget_GetCurrentTransform(IDWriteBitmapRenderTarget *iface, DWRITE_MATRIX *transform)
124 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
125 FIXME("(%p)->(%p): stub\n", This, transform);
126 return E_NOTIMPL;
129 static HRESULT WINAPI rendertarget_SetCurrentTransform(IDWriteBitmapRenderTarget *iface, DWRITE_MATRIX const *transform)
131 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
132 FIXME("(%p)->(%p): stub\n", This, transform);
133 return E_NOTIMPL;
136 static HRESULT WINAPI rendertarget_GetSize(IDWriteBitmapRenderTarget *iface, SIZE *size)
138 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
140 TRACE("(%p)->(%p)\n", This, size);
141 *size = This->size;
142 return S_OK;
145 static HRESULT WINAPI rendertarget_Resize(IDWriteBitmapRenderTarget *iface, UINT32 width, UINT32 height)
147 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
148 FIXME("(%p)->(%u %u): stub\n", This, width, height);
149 return E_NOTIMPL;
152 static const IDWriteBitmapRenderTargetVtbl rendertargetvtbl = {
153 rendertarget_QueryInterface,
154 rendertarget_AddRef,
155 rendertarget_Release,
156 rendertarget_DrawGlyphRun,
157 rendertarget_GetMemoryDC,
158 rendertarget_GetPixelsPerDip,
159 rendertarget_SetPixelsPerDip,
160 rendertarget_GetCurrentTransform,
161 rendertarget_SetCurrentTransform,
162 rendertarget_GetSize,
163 rendertarget_Resize
166 static HRESULT create_rendertarget(HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
168 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
169 BITMAPINFO *bmi = (BITMAPINFO*)bmibuf;
170 struct rendertarget *This;
171 HBITMAP dib;
173 *target = NULL;
175 This = heap_alloc(sizeof(struct rendertarget));
176 if (!This) return E_OUTOFMEMORY;
178 This->IDWriteBitmapRenderTarget_iface.lpVtbl = &rendertargetvtbl;
179 This->ref = 1;
181 This->size.cx = width;
182 This->size.cy = height;
184 This->hdc = CreateCompatibleDC(hdc);
186 memset(bmi, 0, sizeof(bmibuf));
187 bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
188 bmi->bmiHeader.biHeight = height;
189 bmi->bmiHeader.biWidth = width;
190 bmi->bmiHeader.biBitCount = 32;
191 bmi->bmiHeader.biPlanes = 1;
192 bmi->bmiHeader.biCompression = BI_RGB;
194 dib = CreateDIBSection(This->hdc, bmi, DIB_RGB_COLORS, NULL, NULL, 0);
195 SelectObject(This->hdc, dib);
197 *target = &This->IDWriteBitmapRenderTarget_iface;
199 return S_OK;
202 static HRESULT WINAPI gdiinterop_QueryInterface(IDWriteGdiInterop *iface, REFIID riid, void **obj)
204 TRACE("(%s %p)\n", debugstr_guid(riid), obj);
206 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteGdiInterop))
208 *obj = iface;
209 return S_OK;
212 *obj = NULL;
214 return E_NOINTERFACE;
217 static ULONG WINAPI gdiinterop_AddRef(IDWriteGdiInterop *iface)
219 return 2;
222 static ULONG WINAPI gdiinterop_Release(IDWriteGdiInterop *iface)
224 return 1;
227 static HRESULT WINAPI gdiinterop_CreateFontFromLOGFONT(IDWriteGdiInterop *iface,
228 LOGFONTW const *logfont, IDWriteFont **font)
230 TRACE("(%p %p)\n", logfont, font);
232 if (!logfont) return E_INVALIDARG;
234 return create_font_from_logfont(logfont, font);
237 static HRESULT WINAPI gdiinterop_ConvertFontToLOGFONT(IDWriteGdiInterop *iface,
238 IDWriteFont *font, LOGFONTW *logfont, BOOL *is_systemfont)
240 FIXME("(%p %p %p): stub\n", font, logfont, is_systemfont);
241 return E_NOTIMPL;
244 static HRESULT WINAPI gdiinterop_ConvertFontFaceToLOGFONT(IDWriteGdiInterop *iface,
245 IDWriteFontFace *font, LOGFONTW *logfont)
247 FIXME("(%p %p): stub\n", font, logfont);
248 return E_NOTIMPL;
251 static HRESULT WINAPI gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop *iface,
252 HDC hdc, IDWriteFontFace **fontface)
254 FIXME("(%p %p): stub\n", hdc, fontface);
255 return E_NOTIMPL;
258 static HRESULT WINAPI gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop *iface,
259 HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
261 TRACE("(%p %u %u %p)\n", hdc, width, height, target);
262 return create_rendertarget(hdc, width, height, target);
265 static const struct IDWriteGdiInteropVtbl gdiinteropvtbl = {
266 gdiinterop_QueryInterface,
267 gdiinterop_AddRef,
268 gdiinterop_Release,
269 gdiinterop_CreateFontFromLOGFONT,
270 gdiinterop_ConvertFontToLOGFONT,
271 gdiinterop_ConvertFontFaceToLOGFONT,
272 gdiinterop_CreateFontFaceFromHdc,
273 gdiinterop_CreateBitmapRenderTarget
276 static IDWriteGdiInterop gdiinterop = { &gdiinteropvtbl };
278 HRESULT get_gdiinterop(IDWriteGdiInterop **ret)
280 *ret = &gdiinterop;
281 return S_OK;