wevtapi: Add EvtOpenLog stub.
[wine.git] / dlls / dwrite / gdiinterop.c
blobad95e94959e0d93b4a96bd14f467ed371bba48c8
1 /*
2 * GDI Interop
4 * Copyright 2011 Huw Davies
5 * Copyright 2012, 2014-2016 Nikolay Sivov for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "dwrite_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
35 struct dib_data {
36 DWORD *ptr;
37 int stride;
38 int width;
41 struct rendertarget {
42 IDWriteBitmapRenderTarget1 IDWriteBitmapRenderTarget1_iface;
43 ID2D1SimplifiedGeometrySink ID2D1SimplifiedGeometrySink_iface;
44 LONG ref;
46 IDWriteFactory5 *factory;
47 DWRITE_TEXT_ANTIALIAS_MODE antialiasmode;
48 FLOAT ppdip;
49 DWRITE_MATRIX m;
50 SIZE size;
51 HDC hdc;
52 struct dib_data dib;
55 struct gdiinterop {
56 IDWriteGdiInterop1 IDWriteGdiInterop1_iface;
57 LONG ref;
58 IDWriteFactory5 *factory;
61 static inline int get_dib_stride(int width, int bpp)
63 return ((width * bpp + 31) >> 3) & ~3;
66 static HRESULT create_target_dibsection(struct rendertarget *target, UINT32 width, UINT32 height)
68 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
69 BITMAPINFO *bmi = (BITMAPINFO*)bmibuf;
70 HBITMAP hbm;
72 target->size.cx = width;
73 target->size.cy = height;
75 memset(bmi, 0, sizeof(bmibuf));
76 bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
77 bmi->bmiHeader.biHeight = -height;
78 bmi->bmiHeader.biWidth = width;
79 bmi->bmiHeader.biBitCount = 32;
80 bmi->bmiHeader.biPlanes = 1;
81 bmi->bmiHeader.biCompression = BI_RGB;
83 hbm = CreateDIBSection(target->hdc, bmi, DIB_RGB_COLORS, (void**)&target->dib.ptr, NULL, 0);
84 if (!hbm) {
85 hbm = CreateBitmap(1, 1, 1, 1, NULL);
86 target->dib.ptr = NULL;
87 target->dib.stride = 0;
88 target->dib.width = 0;
90 else {
91 target->dib.stride = get_dib_stride(width, 32);
92 target->dib.width = width;
95 DeleteObject(SelectObject(target->hdc, hbm));
96 return S_OK;
99 static inline struct rendertarget *impl_from_IDWriteBitmapRenderTarget1(IDWriteBitmapRenderTarget1 *iface)
101 return CONTAINING_RECORD(iface, struct rendertarget, IDWriteBitmapRenderTarget1_iface);
104 static inline struct rendertarget *impl_from_ID2D1SimplifiedGeometrySink(ID2D1SimplifiedGeometrySink *iface)
106 return CONTAINING_RECORD(iface, struct rendertarget, ID2D1SimplifiedGeometrySink_iface);
109 static inline struct gdiinterop *impl_from_IDWriteGdiInterop1(IDWriteGdiInterop1 *iface)
111 return CONTAINING_RECORD(iface, struct gdiinterop, IDWriteGdiInterop1_iface);
114 static HRESULT WINAPI rendertarget_sink_QueryInterface(ID2D1SimplifiedGeometrySink *iface, REFIID riid, void **obj)
116 if (IsEqualIID(riid, &IID_ID2D1SimplifiedGeometrySink) ||
117 IsEqualIID(riid, &IID_IUnknown))
119 *obj = iface;
120 ID2D1SimplifiedGeometrySink_AddRef(iface);
121 return S_OK;
124 WARN("%s not implemented.\n", debugstr_guid(riid));
126 *obj = NULL;
128 return E_NOINTERFACE;
131 static ULONG WINAPI rendertarget_sink_AddRef(ID2D1SimplifiedGeometrySink *iface)
133 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
134 return IDWriteBitmapRenderTarget1_AddRef(&This->IDWriteBitmapRenderTarget1_iface);
137 static ULONG WINAPI rendertarget_sink_Release(ID2D1SimplifiedGeometrySink *iface)
139 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
140 return IDWriteBitmapRenderTarget1_Release(&This->IDWriteBitmapRenderTarget1_iface);
143 static void WINAPI rendertarget_sink_SetFillMode(ID2D1SimplifiedGeometrySink *iface, D2D1_FILL_MODE mode)
145 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
146 SetPolyFillMode(This->hdc, mode == D2D1_FILL_MODE_ALTERNATE ? ALTERNATE : WINDING);
149 static void WINAPI rendertarget_sink_SetSegmentFlags(ID2D1SimplifiedGeometrySink *iface, D2D1_PATH_SEGMENT vertexFlags)
153 static void WINAPI rendertarget_sink_BeginFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin)
155 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
156 MoveToEx(This->hdc, startPoint.x, startPoint.y, NULL);
159 static void WINAPI rendertarget_sink_AddLines(ID2D1SimplifiedGeometrySink *iface, const D2D1_POINT_2F *points, UINT32 count)
161 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
163 while (count--) {
164 LineTo(This->hdc, points->x, points->y);
165 points++;
169 static void WINAPI rendertarget_sink_AddBeziers(ID2D1SimplifiedGeometrySink *iface, const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
171 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
172 POINT points[3];
174 while (count--) {
175 points[0].x = beziers->point1.x;
176 points[0].y = beziers->point1.y;
177 points[1].x = beziers->point2.x;
178 points[1].y = beziers->point2.y;
179 points[2].x = beziers->point3.x;
180 points[2].y = beziers->point3.y;
182 PolyBezierTo(This->hdc, points, 3);
183 beziers++;
187 static void WINAPI rendertarget_sink_EndFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_FIGURE_END figureEnd)
189 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
190 CloseFigure(This->hdc);
193 static HRESULT WINAPI rendertarget_sink_Close(ID2D1SimplifiedGeometrySink *iface)
195 return S_OK;
198 static const ID2D1SimplifiedGeometrySinkVtbl rendertargetsinkvtbl = {
199 rendertarget_sink_QueryInterface,
200 rendertarget_sink_AddRef,
201 rendertarget_sink_Release,
202 rendertarget_sink_SetFillMode,
203 rendertarget_sink_SetSegmentFlags,
204 rendertarget_sink_BeginFigure,
205 rendertarget_sink_AddLines,
206 rendertarget_sink_AddBeziers,
207 rendertarget_sink_EndFigure,
208 rendertarget_sink_Close
211 static HRESULT WINAPI rendertarget_QueryInterface(IDWriteBitmapRenderTarget1 *iface, REFIID riid, void **obj)
213 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
215 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
217 if (IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget1) ||
218 IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget) ||
219 IsEqualIID(riid, &IID_IUnknown))
221 *obj = iface;
222 IDWriteBitmapRenderTarget1_AddRef(iface);
223 return S_OK;
226 WARN("%s not implemented.\n", debugstr_guid(riid));
228 *obj = NULL;
230 return E_NOINTERFACE;
233 static ULONG WINAPI rendertarget_AddRef(IDWriteBitmapRenderTarget1 *iface)
235 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
236 ULONG ref = InterlockedIncrement(&This->ref);
237 TRACE("(%p)->(%d)\n", This, ref);
238 return ref;
241 static ULONG WINAPI rendertarget_Release(IDWriteBitmapRenderTarget1 *iface)
243 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
244 ULONG ref = InterlockedDecrement(&This->ref);
246 TRACE("(%p)->(%d)\n", This, ref);
248 if (!ref)
250 IDWriteFactory5_Release(This->factory);
251 DeleteDC(This->hdc);
252 heap_free(This);
255 return ref;
258 static inline DWORD *get_pixel_ptr_32(struct dib_data *dib, int x, int y)
260 return (DWORD *)((BYTE*)dib->ptr + y * dib->stride + x * 4);
263 static inline BYTE blend_color(BYTE dst, BYTE src, BYTE alpha)
265 return (src * alpha + dst * (255 - alpha) + 127) / 255;
268 static inline DWORD blend_subpixel(BYTE r, BYTE g, BYTE b, DWORD text, const BYTE *alpha)
270 return blend_color(r, text >> 16, alpha[0]) << 16 |
271 blend_color(g, text >> 8, alpha[1]) << 8 |
272 blend_color(b, text, alpha[2]);
275 static inline DWORD blend_pixel(BYTE r, BYTE g, BYTE b, DWORD text, BYTE alpha)
277 return blend_color(r, text >> 16, alpha) << 16 |
278 blend_color(g, text >> 8, alpha) << 8 |
279 blend_color(b, text, alpha);
282 static void blit_8(struct dib_data *dib, const BYTE *src, const RECT *rect, DWORD text_pixel)
284 DWORD *dst_ptr = get_pixel_ptr_32(dib, rect->left, rect->top);
285 int x, y, src_width = rect->right - rect->left;
287 for (y = rect->top; y < rect->bottom; y++) {
288 for (x = 0; x < src_width; x++) {
289 if (!src[x]) continue;
290 if (src[x] == DWRITE_ALPHA_MAX)
291 dst_ptr[x] = text_pixel;
292 else
293 dst_ptr[x] = blend_pixel(dst_ptr[x] >> 16, dst_ptr[x] >> 8, dst_ptr[x], text_pixel, src[x]);
296 src += src_width;
297 dst_ptr += dib->stride / 4;
301 static void blit_subpixel_888(struct dib_data *dib, int dib_width, const BYTE *src,
302 const RECT *rect, DWORD text_pixel)
304 DWORD *dst_ptr = get_pixel_ptr_32(dib, rect->left, rect->top);
305 int x, y, src_width = rect->right - rect->left;
307 for (y = rect->top; y < rect->bottom; y++) {
308 for (x = 0; x < src_width; x++) {
309 if (src[3*x] == 0 && src[3*x+1] == 0 && src[3*x+2] == 0) continue;
310 dst_ptr[x] = blend_subpixel(dst_ptr[x] >> 16, dst_ptr[x] >> 8, dst_ptr[x], text_pixel, &src[3*x]);
312 dst_ptr += dib->stride / 4;
313 src += src_width * 3;
317 static inline DWORD colorref_to_pixel_888(COLORREF color)
319 return (((color >> 16) & 0xff) | (color & 0xff00) | ((color << 16) & 0xff0000));
322 static HRESULT WINAPI rendertarget_DrawGlyphRun(IDWriteBitmapRenderTarget1 *iface,
323 FLOAT originX, FLOAT originY, DWRITE_MEASURING_MODE measuring_mode,
324 DWRITE_GLYPH_RUN const *run, IDWriteRenderingParams *params, COLORREF color,
325 RECT *bbox_ret)
327 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
328 IDWriteGlyphRunAnalysis *analysis;
329 DWRITE_RENDERING_MODE1 rendermode;
330 DWRITE_GRID_FIT_MODE gridfitmode;
331 DWRITE_TEXTURE_TYPE texturetype;
332 DWRITE_GLYPH_RUN scaled_run;
333 IDWriteFontFace3 *fontface;
334 RECT target, bounds;
335 HRESULT hr;
337 TRACE("(%p)->(%.2f %.2f %d %p %p 0x%08x %p)\n", This, originX, originY,
338 measuring_mode, run, params, color, bbox_ret);
340 SetRectEmpty(bbox_ret);
342 if (!This->dib.ptr)
343 return S_OK;
345 if (!params)
346 return E_INVALIDARG;
348 if (FAILED(hr = IDWriteFontFace_QueryInterface(run->fontFace, &IID_IDWriteFontFace3, (void **)&fontface))) {
349 WARN("Failed to get IDWriteFontFace2 interface, hr %#x.\n", hr);
350 return hr;
353 hr = IDWriteFontFace3_GetRecommendedRenderingMode(fontface, run->fontEmSize, This->ppdip * 96.0f,
354 This->ppdip * 96.0f, NULL /* FIXME */, run->isSideways, DWRITE_OUTLINE_THRESHOLD_ALIASED, measuring_mode,
355 params, &rendermode, &gridfitmode);
356 IDWriteFontFace3_Release(fontface);
357 if (FAILED(hr))
358 return hr;
360 SetRect(&target, 0, 0, This->size.cx, This->size.cy);
362 if (rendermode == DWRITE_RENDERING_MODE1_OUTLINE) {
363 static const XFORM identity = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f };
364 const DWRITE_MATRIX *m = &This->m;
365 XFORM xform;
367 /* target allows any transform to be set, filter it here */
368 if (m->m11 * m->m22 == m->m12 * m->m21) {
369 xform.eM11 = 1.0f;
370 xform.eM12 = 0.0f;
371 xform.eM21 = 0.0f;
372 xform.eM22 = 1.0f;
373 xform.eDx = originX;
374 xform.eDy = originY;
375 } else {
376 xform.eM11 = m->m11;
377 xform.eM12 = m->m12;
378 xform.eM21 = m->m21;
379 xform.eM22 = m->m22;
380 xform.eDx = m->m11 * originX + m->m21 * originY + m->dx;
381 xform.eDy = m->m12 * originX + m->m22 * originY + m->dy;
383 SetWorldTransform(This->hdc, &xform);
385 BeginPath(This->hdc);
387 hr = IDWriteFontFace_GetGlyphRunOutline(run->fontFace, run->fontEmSize * This->ppdip,
388 run->glyphIndices, run->glyphAdvances, run->glyphOffsets, run->glyphCount,
389 run->isSideways, run->bidiLevel & 1, &This->ID2D1SimplifiedGeometrySink_iface);
391 EndPath(This->hdc);
393 if (hr == S_OK) {
394 HBRUSH brush = CreateSolidBrush(color);
396 SelectObject(This->hdc, brush);
398 FillPath(This->hdc);
400 /* FIXME: one way to get affected rectangle bounds is to use region fill */
401 if (bbox_ret)
402 *bbox_ret = target;
404 DeleteObject(brush);
407 SetWorldTransform(This->hdc, &identity);
409 return hr;
412 scaled_run = *run;
413 scaled_run.fontEmSize *= This->ppdip;
414 hr = IDWriteFactory5_CreateGlyphRunAnalysis(This->factory, &scaled_run, &This->m, rendermode, measuring_mode,
415 gridfitmode, This->antialiasmode, originX, originY, &analysis);
416 if (FAILED(hr)) {
417 WARN("failed to create analysis instance, 0x%08x\n", hr);
418 return hr;
421 SetRectEmpty(&bounds);
422 texturetype = DWRITE_TEXTURE_ALIASED_1x1;
423 hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &bounds);
424 if (FAILED(hr) || IsRectEmpty(&bounds)) {
425 hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &bounds);
426 if (FAILED(hr)) {
427 WARN("GetAlphaTextureBounds() failed, 0x%08x\n", hr);
428 IDWriteGlyphRunAnalysis_Release(analysis);
429 return hr;
431 texturetype = DWRITE_TEXTURE_CLEARTYPE_3x1;
434 if (IntersectRect(&target, &target, &bounds)) {
435 UINT32 size = (target.right - target.left) * (target.bottom - target.top);
436 BYTE *bitmap;
438 color = colorref_to_pixel_888(color);
439 if (texturetype == DWRITE_TEXTURE_CLEARTYPE_3x1)
440 size *= 3;
441 bitmap = heap_alloc_zero(size);
442 if (!bitmap) {
443 IDWriteGlyphRunAnalysis_Release(analysis);
444 return E_OUTOFMEMORY;
447 hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, texturetype, &target, bitmap, size);
448 if (hr == S_OK) {
449 /* blit to target dib */
450 if (texturetype == DWRITE_TEXTURE_ALIASED_1x1)
451 blit_8(&This->dib, bitmap, &target, color);
452 else
453 blit_subpixel_888(&This->dib, This->size.cx, bitmap, &target, color);
455 if (bbox_ret) *bbox_ret = target;
458 heap_free(bitmap);
461 IDWriteGlyphRunAnalysis_Release(analysis);
463 return S_OK;
466 static HDC WINAPI rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget1 *iface)
468 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
469 TRACE("(%p)\n", This);
470 return This->hdc;
473 static FLOAT WINAPI rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget1 *iface)
475 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
476 TRACE("(%p)\n", This);
477 return This->ppdip;
480 static HRESULT WINAPI rendertarget_SetPixelsPerDip(IDWriteBitmapRenderTarget1 *iface, FLOAT ppdip)
482 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
484 TRACE("(%p)->(%.2f)\n", This, ppdip);
486 if (ppdip <= 0.0f)
487 return E_INVALIDARG;
489 This->ppdip = ppdip;
490 return S_OK;
493 static HRESULT WINAPI rendertarget_GetCurrentTransform(IDWriteBitmapRenderTarget1 *iface, DWRITE_MATRIX *transform)
495 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
497 TRACE("(%p)->(%p)\n", This, transform);
499 *transform = This->m;
500 return S_OK;
503 static HRESULT WINAPI rendertarget_SetCurrentTransform(IDWriteBitmapRenderTarget1 *iface, DWRITE_MATRIX const *transform)
505 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
507 TRACE("(%p)->(%p)\n", This, transform);
509 This->m = transform ? *transform : identity;
510 return S_OK;
513 static HRESULT WINAPI rendertarget_GetSize(IDWriteBitmapRenderTarget1 *iface, SIZE *size)
515 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
517 TRACE("(%p)->(%p)\n", This, size);
518 *size = This->size;
519 return S_OK;
522 static HRESULT WINAPI rendertarget_Resize(IDWriteBitmapRenderTarget1 *iface, UINT32 width, UINT32 height)
524 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
526 TRACE("(%p)->(%u %u)\n", This, width, height);
528 if (This->size.cx == width && This->size.cy == height)
529 return S_OK;
531 return create_target_dibsection(This, width, height);
534 static DWRITE_TEXT_ANTIALIAS_MODE WINAPI rendertarget_GetTextAntialiasMode(IDWriteBitmapRenderTarget1 *iface)
536 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
537 TRACE("(%p)\n", This);
538 return This->antialiasmode;
541 static HRESULT WINAPI rendertarget_SetTextAntialiasMode(IDWriteBitmapRenderTarget1 *iface, DWRITE_TEXT_ANTIALIAS_MODE mode)
543 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
545 TRACE("(%p)->(%d)\n", This, mode);
547 if ((DWORD)mode > DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE)
548 return E_INVALIDARG;
550 This->antialiasmode = mode;
551 return S_OK;
554 static const IDWriteBitmapRenderTarget1Vtbl rendertargetvtbl = {
555 rendertarget_QueryInterface,
556 rendertarget_AddRef,
557 rendertarget_Release,
558 rendertarget_DrawGlyphRun,
559 rendertarget_GetMemoryDC,
560 rendertarget_GetPixelsPerDip,
561 rendertarget_SetPixelsPerDip,
562 rendertarget_GetCurrentTransform,
563 rendertarget_SetCurrentTransform,
564 rendertarget_GetSize,
565 rendertarget_Resize,
566 rendertarget_GetTextAntialiasMode,
567 rendertarget_SetTextAntialiasMode
570 static HRESULT create_rendertarget(IDWriteFactory5 *factory, HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **ret)
572 struct rendertarget *target;
573 HRESULT hr;
575 *ret = NULL;
577 target = heap_alloc(sizeof(struct rendertarget));
578 if (!target) return E_OUTOFMEMORY;
580 target->IDWriteBitmapRenderTarget1_iface.lpVtbl = &rendertargetvtbl;
581 target->ID2D1SimplifiedGeometrySink_iface.lpVtbl = &rendertargetsinkvtbl;
582 target->ref = 1;
584 target->hdc = CreateCompatibleDC(hdc);
585 SetGraphicsMode(target->hdc, GM_ADVANCED);
586 hr = create_target_dibsection(target, width, height);
587 if (FAILED(hr)) {
588 IDWriteBitmapRenderTarget1_Release(&target->IDWriteBitmapRenderTarget1_iface);
589 return hr;
592 target->m = identity;
593 target->ppdip = GetDeviceCaps(target->hdc, LOGPIXELSX) / 96.0f;
594 target->antialiasmode = DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE;
595 target->factory = factory;
596 IDWriteFactory5_AddRef(factory);
598 *ret = (IDWriteBitmapRenderTarget*)&target->IDWriteBitmapRenderTarget1_iface;
600 return S_OK;
603 static HRESULT WINAPI gdiinterop_QueryInterface(IDWriteGdiInterop1 *iface, REFIID riid, void **obj)
605 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
607 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
609 if (IsEqualIID(riid, &IID_IDWriteGdiInterop1) ||
610 IsEqualIID(riid, &IID_IDWriteGdiInterop) ||
611 IsEqualIID(riid, &IID_IUnknown))
613 *obj = iface;
614 IDWriteGdiInterop1_AddRef(iface);
615 return S_OK;
618 WARN("%s not implemented.\n", debugstr_guid(riid));
620 *obj = NULL;
621 return E_NOINTERFACE;
624 static ULONG WINAPI gdiinterop_AddRef(IDWriteGdiInterop1 *iface)
626 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
627 LONG ref = InterlockedIncrement(&This->ref);
628 TRACE("(%p)->(%d)\n", This, ref);
629 return ref;
632 static ULONG WINAPI gdiinterop_Release(IDWriteGdiInterop1 *iface)
634 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
635 LONG ref = InterlockedDecrement(&This->ref);
637 TRACE("(%p)->(%d)\n", This, ref);
639 if (!ref) {
640 factory_detach_gdiinterop(This->factory, iface);
641 heap_free(This);
644 return ref;
647 static HRESULT WINAPI gdiinterop_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
648 LOGFONTW const *logfont, IDWriteFont **font)
650 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
652 TRACE("(%p)->(%p %p)\n", This, logfont, font);
654 return IDWriteGdiInterop1_CreateFontFromLOGFONT(iface, logfont, NULL, font);
657 static HRESULT WINAPI gdiinterop_ConvertFontToLOGFONT(IDWriteGdiInterop1 *iface,
658 IDWriteFont *font, LOGFONTW *logfont, BOOL *is_systemfont)
660 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
661 IDWriteFontCollection *collection;
662 IDWriteFontFamily *family;
663 HRESULT hr;
665 TRACE("(%p)->(%p %p %p)\n", This, font, logfont, is_systemfont);
667 *is_systemfont = FALSE;
669 memset(logfont, 0, sizeof(*logfont));
671 if (!font)
672 return E_INVALIDARG;
674 hr = IDWriteFont_GetFontFamily(font, &family);
675 if (FAILED(hr))
676 return hr;
678 hr = IDWriteFontFamily_GetFontCollection(family, &collection);
679 IDWriteFontFamily_Release(family);
680 if (FAILED(hr))
681 return hr;
683 *is_systemfont = is_system_collection(collection);
684 IDWriteFontCollection_Release(collection);
686 get_logfont_from_font(font, logfont);
687 logfont->lfCharSet = DEFAULT_CHARSET;
688 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
690 return hr;
693 static HRESULT WINAPI gdiinterop_ConvertFontFaceToLOGFONT(IDWriteGdiInterop1 *iface,
694 IDWriteFontFace *fontface, LOGFONTW *logfont)
696 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
698 TRACE("(%p)->(%p %p)\n", This, fontface, logfont);
700 memset(logfont, 0, sizeof(*logfont));
702 if (!fontface)
703 return E_INVALIDARG;
705 get_logfont_from_fontface(fontface, logfont);
706 logfont->lfCharSet = DEFAULT_CHARSET;
707 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
709 return S_OK;
712 struct font_realization_info {
713 DWORD size;
714 DWORD flags;
715 DWORD cache_num;
716 DWORD instance_id;
717 DWORD unk;
718 WORD face_index;
719 WORD simulations;
722 struct font_fileinfo {
723 FILETIME writetime;
724 LARGE_INTEGER size;
725 WCHAR path[1];
728 /* Undocumented gdi32 exports, used to access actually selected font information */
729 extern BOOL WINAPI GetFontRealizationInfo(HDC hdc, struct font_realization_info *info);
730 extern BOOL WINAPI GetFontFileInfo(DWORD instance_id, DWORD unknown, struct font_fileinfo *info, DWORD size, DWORD *needed);
732 static HRESULT WINAPI gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop1 *iface,
733 HDC hdc, IDWriteFontFace **fontface)
735 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
736 struct font_realization_info info;
737 struct font_fileinfo *fileinfo;
738 DWRITE_FONT_FILE_TYPE filetype;
739 DWRITE_FONT_FACE_TYPE facetype;
740 IDWriteFontFile *file;
741 BOOL is_supported;
742 UINT32 facenum;
743 DWORD needed;
744 HRESULT hr;
746 TRACE("(%p)->(%p %p)\n", This, hdc, fontface);
748 *fontface = NULL;
750 if (!hdc)
751 return E_INVALIDARG;
753 /* get selected font id */
754 info.size = sizeof(info);
755 if (!GetFontRealizationInfo(hdc, &info)) {
756 WARN("failed to get selected font id\n");
757 return E_FAIL;
760 needed = 0;
761 GetFontFileInfo(info.instance_id, 0, NULL, 0, &needed);
762 if (needed == 0) {
763 WARN("failed to get font file info size\n");
764 return E_FAIL;
767 fileinfo = heap_alloc(needed);
768 if (!fileinfo)
769 return E_OUTOFMEMORY;
771 if (!GetFontFileInfo(info.instance_id, 0, fileinfo, needed, &needed)) {
772 heap_free(fileinfo);
773 return E_FAIL;
776 hr = IDWriteFactory5_CreateFontFileReference(This->factory, fileinfo->path, &fileinfo->writetime,
777 &file);
778 heap_free(fileinfo);
779 if (FAILED(hr))
780 return hr;
782 is_supported = FALSE;
783 hr = IDWriteFontFile_Analyze(file, &is_supported, &filetype, &facetype, &facenum);
784 if (SUCCEEDED(hr)) {
785 if (is_supported)
786 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
787 hr = IDWriteFactory5_CreateFontFace(This->factory, facetype, 1, &file, info.face_index,
788 info.simulations, fontface);
789 else
790 hr = DWRITE_E_FILEFORMAT;
793 IDWriteFontFile_Release(file);
794 return hr;
797 static HRESULT WINAPI gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop1 *iface,
798 HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
800 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
801 TRACE("(%p)->(%p %u %u %p)\n", This, hdc, width, height, target);
802 return create_rendertarget(This->factory, hdc, width, height, target);
805 static HRESULT WINAPI gdiinterop1_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
806 LOGFONTW const *logfont, IDWriteFontCollection *collection, IDWriteFont **font)
808 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
809 IDWriteFontFamily *family;
810 DWRITE_FONT_STYLE style;
811 BOOL exists = FALSE;
812 UINT32 index;
813 HRESULT hr;
815 TRACE("(%p)->(%p %p %p)\n", This, logfont, collection, font);
817 *font = NULL;
819 if (!logfont) return E_INVALIDARG;
821 if (collection)
822 IDWriteFontCollection_AddRef(collection);
823 else {
824 hr = IDWriteFactory5_GetSystemFontCollection(This->factory, FALSE, (IDWriteFontCollection1**)&collection, FALSE);
825 if (FAILED(hr)) {
826 ERR("failed to get system font collection: 0x%08x.\n", hr);
827 return hr;
831 hr = IDWriteFontCollection_FindFamilyName(collection, logfont->lfFaceName, &index, &exists);
832 if (FAILED(hr))
833 goto done;
835 if (!exists) {
836 hr = DWRITE_E_NOFONT;
837 goto done;
840 hr = IDWriteFontCollection_GetFontFamily(collection, index, &family);
841 if (FAILED(hr))
842 goto done;
844 style = logfont->lfItalic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
845 hr = IDWriteFontFamily_GetFirstMatchingFont(family, logfont->lfWeight, DWRITE_FONT_STRETCH_NORMAL, style, font);
846 IDWriteFontFamily_Release(family);
848 done:
849 IDWriteFontCollection_Release(collection);
850 return hr;
853 static HRESULT WINAPI gdiinterop1_GetFontSignature_(IDWriteGdiInterop1 *iface, IDWriteFontFace *fontface,
854 FONTSIGNATURE *fontsig)
856 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
858 TRACE("(%p)->(%p %p)\n", This, fontface, fontsig);
860 return get_fontsig_from_fontface(fontface, fontsig);
863 static HRESULT WINAPI gdiinterop1_GetFontSignature(IDWriteGdiInterop1 *iface, IDWriteFont *font, FONTSIGNATURE *fontsig)
865 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
867 TRACE("(%p)->(%p %p)\n", This, font, fontsig);
869 if (!font)
870 return E_INVALIDARG;
872 return get_fontsig_from_font(font, fontsig);
875 static HRESULT WINAPI gdiinterop1_GetMatchingFontsByLOGFONT(IDWriteGdiInterop1 *iface, LOGFONTW const *logfont,
876 IDWriteFontSet *fontset, IDWriteFontSet **subset)
878 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
880 FIXME("(%p)->(%p %p %p): stub\n", This, logfont, fontset, subset);
882 return E_NOTIMPL;
885 static const struct IDWriteGdiInterop1Vtbl gdiinteropvtbl = {
886 gdiinterop_QueryInterface,
887 gdiinterop_AddRef,
888 gdiinterop_Release,
889 gdiinterop_CreateFontFromLOGFONT,
890 gdiinterop_ConvertFontToLOGFONT,
891 gdiinterop_ConvertFontFaceToLOGFONT,
892 gdiinterop_CreateFontFaceFromHdc,
893 gdiinterop_CreateBitmapRenderTarget,
894 gdiinterop1_CreateFontFromLOGFONT,
895 gdiinterop1_GetFontSignature_,
896 gdiinterop1_GetFontSignature,
897 gdiinterop1_GetMatchingFontsByLOGFONT
900 HRESULT create_gdiinterop(IDWriteFactory5 *factory, IDWriteGdiInterop1 **ret)
902 struct gdiinterop *interop;
904 *ret = NULL;
906 if (!(interop = heap_alloc(sizeof(*interop))))
907 return E_OUTOFMEMORY;
909 interop->IDWriteGdiInterop1_iface.lpVtbl = &gdiinteropvtbl;
910 interop->ref = 1;
911 IDWriteFactory5_AddRef(interop->factory = factory);
913 *ret = &interop->IDWriteGdiInterop1_iface;
914 return S_OK;