wined3d: Get rid of the "render_to_fbo" field from the wined3d_swapchain structure.
[wine.git] / dlls / dwrite / gdiinterop.c
blob8df3f9f97ca52ea1e5d9ba39a612471e181be551
1 /*
2 * GDI Interop
4 * Copyright 2011 Huw Davies
5 * Copyright 2012, 2014-2018 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
43 IDWriteBitmapRenderTarget1 IDWriteBitmapRenderTarget1_iface;
44 ID2D1SimplifiedGeometrySink ID2D1SimplifiedGeometrySink_iface;
45 LONG refcount;
47 IDWriteFactory7 *factory;
48 DWRITE_TEXT_ANTIALIAS_MODE antialiasmode;
49 float ppdip;
50 DWRITE_MATRIX m;
51 SIZE size;
52 HDC hdc;
53 struct dib_data dib;
56 struct gdiinterop
58 IDWriteGdiInterop1 IDWriteGdiInterop1_iface;
59 IDWriteFontFileLoader IDWriteFontFileLoader_iface;
60 LONG refcount;
61 IDWriteFactory7 *factory;
64 struct memresource_stream
66 IDWriteFontFileStream IDWriteFontFileStream_iface;
67 LONG refcount;
68 DWORD key;
71 static inline int get_dib_stride(int width, int bpp)
73 return ((width * bpp + 31) >> 3) & ~3;
76 static HRESULT create_target_dibsection(struct rendertarget *target, UINT32 width, UINT32 height)
78 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
79 BITMAPINFO *bmi = (BITMAPINFO*)bmibuf;
80 HBITMAP hbm;
82 target->size.cx = width;
83 target->size.cy = height;
85 memset(bmi, 0, sizeof(bmibuf));
86 bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
87 bmi->bmiHeader.biHeight = -height;
88 bmi->bmiHeader.biWidth = width;
89 bmi->bmiHeader.biBitCount = 32;
90 bmi->bmiHeader.biPlanes = 1;
91 bmi->bmiHeader.biCompression = BI_RGB;
93 hbm = CreateDIBSection(target->hdc, bmi, DIB_RGB_COLORS, (void**)&target->dib.ptr, NULL, 0);
94 if (!hbm) {
95 hbm = CreateBitmap(1, 1, 1, 1, NULL);
96 target->dib.ptr = NULL;
97 target->dib.stride = 0;
98 target->dib.width = 0;
100 else {
101 target->dib.stride = get_dib_stride(width, 32);
102 target->dib.width = width;
105 DeleteObject(SelectObject(target->hdc, hbm));
106 return S_OK;
109 static inline struct rendertarget *impl_from_IDWriteBitmapRenderTarget1(IDWriteBitmapRenderTarget1 *iface)
111 return CONTAINING_RECORD(iface, struct rendertarget, IDWriteBitmapRenderTarget1_iface);
114 static inline struct rendertarget *impl_from_ID2D1SimplifiedGeometrySink(ID2D1SimplifiedGeometrySink *iface)
116 return CONTAINING_RECORD(iface, struct rendertarget, ID2D1SimplifiedGeometrySink_iface);
119 static inline struct gdiinterop *impl_from_IDWriteGdiInterop1(IDWriteGdiInterop1 *iface)
121 return CONTAINING_RECORD(iface, struct gdiinterop, IDWriteGdiInterop1_iface);
124 static inline struct memresource_stream *impl_from_IDWriteFontFileStream(IDWriteFontFileStream *iface)
126 return CONTAINING_RECORD(iface, struct memresource_stream, IDWriteFontFileStream_iface);
129 static HRESULT WINAPI rendertarget_sink_QueryInterface(ID2D1SimplifiedGeometrySink *iface, REFIID riid, void **obj)
131 if (IsEqualIID(riid, &IID_ID2D1SimplifiedGeometrySink) ||
132 IsEqualIID(riid, &IID_IUnknown))
134 *obj = iface;
135 ID2D1SimplifiedGeometrySink_AddRef(iface);
136 return S_OK;
139 WARN("%s not implemented.\n", debugstr_guid(riid));
141 *obj = NULL;
143 return E_NOINTERFACE;
146 static ULONG WINAPI rendertarget_sink_AddRef(ID2D1SimplifiedGeometrySink *iface)
148 struct rendertarget *target = impl_from_ID2D1SimplifiedGeometrySink(iface);
149 return IDWriteBitmapRenderTarget1_AddRef(&target->IDWriteBitmapRenderTarget1_iface);
152 static ULONG WINAPI rendertarget_sink_Release(ID2D1SimplifiedGeometrySink *iface)
154 struct rendertarget *target = impl_from_ID2D1SimplifiedGeometrySink(iface);
155 return IDWriteBitmapRenderTarget1_Release(&target->IDWriteBitmapRenderTarget1_iface);
158 static void WINAPI rendertarget_sink_SetFillMode(ID2D1SimplifiedGeometrySink *iface, D2D1_FILL_MODE mode)
160 struct rendertarget *target = impl_from_ID2D1SimplifiedGeometrySink(iface);
161 SetPolyFillMode(target->hdc, mode == D2D1_FILL_MODE_ALTERNATE ? ALTERNATE : WINDING);
164 static void WINAPI rendertarget_sink_SetSegmentFlags(ID2D1SimplifiedGeometrySink *iface, D2D1_PATH_SEGMENT vertexFlags)
168 static void WINAPI rendertarget_sink_BeginFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin)
170 struct rendertarget *target = impl_from_ID2D1SimplifiedGeometrySink(iface);
171 MoveToEx(target->hdc, startPoint.x, startPoint.y, NULL);
174 static void WINAPI rendertarget_sink_AddLines(ID2D1SimplifiedGeometrySink *iface, const D2D1_POINT_2F *points, UINT32 count)
176 struct rendertarget *target = impl_from_ID2D1SimplifiedGeometrySink(iface);
178 while (count--)
180 LineTo(target->hdc, points->x, points->y);
181 points++;
185 static void WINAPI rendertarget_sink_AddBeziers(ID2D1SimplifiedGeometrySink *iface, const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
187 struct rendertarget *target = impl_from_ID2D1SimplifiedGeometrySink(iface);
188 POINT points[3];
190 while (count--)
192 points[0].x = beziers->point1.x;
193 points[0].y = beziers->point1.y;
194 points[1].x = beziers->point2.x;
195 points[1].y = beziers->point2.y;
196 points[2].x = beziers->point3.x;
197 points[2].y = beziers->point3.y;
199 PolyBezierTo(target->hdc, points, 3);
200 beziers++;
204 static void WINAPI rendertarget_sink_EndFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_FIGURE_END figureEnd)
206 struct rendertarget *target = impl_from_ID2D1SimplifiedGeometrySink(iface);
207 CloseFigure(target->hdc);
210 static HRESULT WINAPI rendertarget_sink_Close(ID2D1SimplifiedGeometrySink *iface)
212 return S_OK;
215 static const ID2D1SimplifiedGeometrySinkVtbl rendertargetsinkvtbl =
217 rendertarget_sink_QueryInterface,
218 rendertarget_sink_AddRef,
219 rendertarget_sink_Release,
220 rendertarget_sink_SetFillMode,
221 rendertarget_sink_SetSegmentFlags,
222 rendertarget_sink_BeginFigure,
223 rendertarget_sink_AddLines,
224 rendertarget_sink_AddBeziers,
225 rendertarget_sink_EndFigure,
226 rendertarget_sink_Close
229 static HRESULT WINAPI rendertarget_QueryInterface(IDWriteBitmapRenderTarget1 *iface, REFIID riid, void **obj)
231 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
233 if (IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget1) ||
234 IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget) ||
235 IsEqualIID(riid, &IID_IUnknown))
237 *obj = iface;
238 IDWriteBitmapRenderTarget1_AddRef(iface);
239 return S_OK;
242 WARN("%s not implemented.\n", debugstr_guid(riid));
244 *obj = NULL;
246 return E_NOINTERFACE;
249 static ULONG WINAPI rendertarget_AddRef(IDWriteBitmapRenderTarget1 *iface)
251 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
252 ULONG refcount = InterlockedIncrement(&target->refcount);
254 TRACE("%p, refcount %u.\n", iface, refcount);
256 return refcount;
259 static ULONG WINAPI rendertarget_Release(IDWriteBitmapRenderTarget1 *iface)
261 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
262 ULONG refcount = InterlockedDecrement(&target->refcount);
264 TRACE("%p, refcount %u.\n", iface, refcount);
266 if (!refcount)
268 IDWriteFactory7_Release(target->factory);
269 DeleteDC(target->hdc);
270 heap_free(target);
273 return refcount;
276 static inline DWORD *get_pixel_ptr_32(struct dib_data *dib, int x, int y)
278 return (DWORD *)((BYTE*)dib->ptr + y * dib->stride + x * 4);
281 static inline BYTE blend_color(BYTE dst, BYTE src, BYTE alpha)
283 return (src * alpha + dst * (255 - alpha) + 127) / 255;
286 static inline DWORD blend_subpixel(BYTE r, BYTE g, BYTE b, DWORD text, const BYTE *alpha)
288 return blend_color(r, text >> 16, alpha[0]) << 16 |
289 blend_color(g, text >> 8, alpha[1]) << 8 |
290 blend_color(b, text, alpha[2]);
293 static inline DWORD blend_pixel(BYTE r, BYTE g, BYTE b, DWORD text, BYTE alpha)
295 return blend_color(r, text >> 16, alpha) << 16 |
296 blend_color(g, text >> 8, alpha) << 8 |
297 blend_color(b, text, alpha);
300 static void blit_8(struct dib_data *dib, const BYTE *src, const RECT *rect, DWORD text_pixel)
302 DWORD *dst_ptr = get_pixel_ptr_32(dib, rect->left, rect->top);
303 int x, y, src_width = rect->right - rect->left;
305 for (y = rect->top; y < rect->bottom; y++) {
306 for (x = 0; x < src_width; x++) {
307 if (!src[x]) continue;
308 if (src[x] == DWRITE_ALPHA_MAX)
309 dst_ptr[x] = text_pixel;
310 else
311 dst_ptr[x] = blend_pixel(dst_ptr[x] >> 16, dst_ptr[x] >> 8, dst_ptr[x], text_pixel, src[x]);
314 src += src_width;
315 dst_ptr += dib->stride / 4;
319 static void blit_subpixel_888(struct dib_data *dib, int dib_width, const BYTE *src,
320 const RECT *rect, DWORD text_pixel)
322 DWORD *dst_ptr = get_pixel_ptr_32(dib, rect->left, rect->top);
323 int x, y, src_width = rect->right - rect->left;
325 for (y = rect->top; y < rect->bottom; y++) {
326 for (x = 0; x < src_width; x++) {
327 if (src[3*x] == 0 && src[3*x+1] == 0 && src[3*x+2] == 0) continue;
328 dst_ptr[x] = blend_subpixel(dst_ptr[x] >> 16, dst_ptr[x] >> 8, dst_ptr[x], text_pixel, &src[3*x]);
330 dst_ptr += dib->stride / 4;
331 src += src_width * 3;
335 static inline DWORD colorref_to_pixel_888(COLORREF color)
337 return (((color >> 16) & 0xff) | (color & 0xff00) | ((color << 16) & 0xff0000));
340 static HRESULT WINAPI rendertarget_DrawGlyphRun(IDWriteBitmapRenderTarget1 *iface,
341 FLOAT originX, FLOAT originY, DWRITE_MEASURING_MODE measuring_mode,
342 DWRITE_GLYPH_RUN const *run, IDWriteRenderingParams *params, COLORREF color,
343 RECT *bbox_ret)
345 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
346 IDWriteGlyphRunAnalysis *analysis;
347 DWRITE_RENDERING_MODE1 rendermode;
348 DWRITE_GRID_FIT_MODE gridfitmode;
349 DWRITE_TEXTURE_TYPE texturetype;
350 DWRITE_GLYPH_RUN scaled_run;
351 IDWriteFontFace3 *fontface;
352 RECT target_rect, bounds;
353 HRESULT hr;
355 TRACE("%p, %.8e, %.8e, %d, %p, %p, 0x%08x, %p.\n", iface, originX, originY,
356 measuring_mode, run, params, color, bbox_ret);
358 SetRectEmpty(bbox_ret);
360 if (!target->dib.ptr)
361 return S_OK;
363 if (!params)
364 return E_INVALIDARG;
366 if (FAILED(hr = IDWriteFontFace_QueryInterface(run->fontFace, &IID_IDWriteFontFace3, (void **)&fontface))) {
367 WARN("Failed to get IDWriteFontFace2 interface, hr %#x.\n", hr);
368 return hr;
371 hr = IDWriteFontFace3_GetRecommendedRenderingMode(fontface, run->fontEmSize, target->ppdip * 96.0f,
372 target->ppdip * 96.0f, &target->m, run->isSideways, DWRITE_OUTLINE_THRESHOLD_ALIASED, measuring_mode,
373 params, &rendermode, &gridfitmode);
374 IDWriteFontFace3_Release(fontface);
375 if (FAILED(hr))
376 return hr;
378 SetRect(&target_rect, 0, 0, target->size.cx, target->size.cy);
380 if (rendermode == DWRITE_RENDERING_MODE1_OUTLINE)
382 static const XFORM identity = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f };
383 const DWRITE_MATRIX *m = &target->m;
384 XFORM xform;
386 /* target allows any transform to be set, filter it here */
387 if (m->m11 * m->m22 == m->m12 * m->m21) {
388 xform.eM11 = 1.0f;
389 xform.eM12 = 0.0f;
390 xform.eM21 = 0.0f;
391 xform.eM22 = 1.0f;
392 xform.eDx = originX;
393 xform.eDy = originY;
394 } else {
395 xform.eM11 = m->m11;
396 xform.eM12 = m->m12;
397 xform.eM21 = m->m21;
398 xform.eM22 = m->m22;
399 xform.eDx = m->m11 * originX + m->m21 * originY + m->dx;
400 xform.eDy = m->m12 * originX + m->m22 * originY + m->dy;
402 SetWorldTransform(target->hdc, &xform);
404 BeginPath(target->hdc);
406 hr = IDWriteFontFace_GetGlyphRunOutline(run->fontFace, run->fontEmSize * target->ppdip,
407 run->glyphIndices, run->glyphAdvances, run->glyphOffsets, run->glyphCount,
408 run->isSideways, run->bidiLevel & 1, &target->ID2D1SimplifiedGeometrySink_iface);
410 EndPath(target->hdc);
412 if (hr == S_OK)
414 HBRUSH brush = CreateSolidBrush(color);
416 SelectObject(target->hdc, brush);
418 FillPath(target->hdc);
420 /* FIXME: one way to get affected rectangle bounds is to use region fill */
421 if (bbox_ret)
422 *bbox_ret = target_rect;
424 DeleteObject(brush);
427 SetWorldTransform(target->hdc, &identity);
429 return hr;
432 scaled_run = *run;
433 scaled_run.fontEmSize *= target->ppdip;
434 hr = IDWriteFactory7_CreateGlyphRunAnalysis(target->factory, &scaled_run, &target->m, rendermode, measuring_mode,
435 gridfitmode, target->antialiasmode, originX, originY, &analysis);
436 if (FAILED(hr))
438 WARN("failed to create analysis instance, 0x%08x\n", hr);
439 return hr;
442 SetRectEmpty(&bounds);
443 texturetype = DWRITE_TEXTURE_ALIASED_1x1;
444 hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &bounds);
445 if (FAILED(hr) || IsRectEmpty(&bounds)) {
446 hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &bounds);
447 if (FAILED(hr)) {
448 WARN("GetAlphaTextureBounds() failed, 0x%08x\n", hr);
449 IDWriteGlyphRunAnalysis_Release(analysis);
450 return hr;
452 texturetype = DWRITE_TEXTURE_CLEARTYPE_3x1;
455 if (IntersectRect(&target_rect, &target_rect, &bounds))
457 UINT32 size = (target_rect.right - target_rect.left) * (target_rect.bottom - target_rect.top);
458 BYTE *bitmap;
460 color = colorref_to_pixel_888(color);
461 if (texturetype == DWRITE_TEXTURE_CLEARTYPE_3x1)
462 size *= 3;
463 bitmap = heap_alloc_zero(size);
464 if (!bitmap) {
465 IDWriteGlyphRunAnalysis_Release(analysis);
466 return E_OUTOFMEMORY;
469 hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, texturetype, &target_rect, bitmap, size);
470 if (hr == S_OK) {
471 /* blit to target dib */
472 if (texturetype == DWRITE_TEXTURE_ALIASED_1x1)
473 blit_8(&target->dib, bitmap, &target_rect, color);
474 else
475 blit_subpixel_888(&target->dib, target->size.cx, bitmap, &target_rect, color);
477 if (bbox_ret) *bbox_ret = target_rect;
480 heap_free(bitmap);
483 IDWriteGlyphRunAnalysis_Release(analysis);
485 return S_OK;
488 static HDC WINAPI rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget1 *iface)
490 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
492 TRACE("%p.\n", iface);
494 return target->hdc;
497 static float WINAPI rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget1 *iface)
499 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
501 TRACE("%p.\n", iface);
503 return target->ppdip;
506 static HRESULT WINAPI rendertarget_SetPixelsPerDip(IDWriteBitmapRenderTarget1 *iface, float ppdip)
508 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
510 TRACE("%p, %.2f.\n", iface, ppdip);
512 if (ppdip <= 0.0f)
513 return E_INVALIDARG;
515 target->ppdip = ppdip;
516 return S_OK;
519 static HRESULT WINAPI rendertarget_GetCurrentTransform(IDWriteBitmapRenderTarget1 *iface, DWRITE_MATRIX *transform)
521 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
523 TRACE("%p, %p.\n", iface, transform);
525 *transform = target->m;
526 return S_OK;
529 static HRESULT WINAPI rendertarget_SetCurrentTransform(IDWriteBitmapRenderTarget1 *iface, DWRITE_MATRIX const *transform)
531 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
533 TRACE("%p, %p.\n", iface, transform);
535 target->m = transform ? *transform : identity;
536 return S_OK;
539 static HRESULT WINAPI rendertarget_GetSize(IDWriteBitmapRenderTarget1 *iface, SIZE *size)
541 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
543 TRACE("%p, %p.\n", iface, size);
545 *size = target->size;
546 return S_OK;
549 static HRESULT WINAPI rendertarget_Resize(IDWriteBitmapRenderTarget1 *iface, UINT32 width, UINT32 height)
551 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
553 TRACE("%p, %u, %u.\n", iface, width, height);
555 if (target->size.cx == width && target->size.cy == height)
556 return S_OK;
558 return create_target_dibsection(target, width, height);
561 static DWRITE_TEXT_ANTIALIAS_MODE WINAPI rendertarget_GetTextAntialiasMode(IDWriteBitmapRenderTarget1 *iface)
563 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
565 TRACE("%p.\n", iface);
567 return target->antialiasmode;
570 static HRESULT WINAPI rendertarget_SetTextAntialiasMode(IDWriteBitmapRenderTarget1 *iface, DWRITE_TEXT_ANTIALIAS_MODE mode)
572 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
574 TRACE("%p, %d.\n", iface, mode);
576 if ((DWORD)mode > DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE)
577 return E_INVALIDARG;
579 target->antialiasmode = mode;
580 return S_OK;
583 static const IDWriteBitmapRenderTarget1Vtbl rendertargetvtbl =
585 rendertarget_QueryInterface,
586 rendertarget_AddRef,
587 rendertarget_Release,
588 rendertarget_DrawGlyphRun,
589 rendertarget_GetMemoryDC,
590 rendertarget_GetPixelsPerDip,
591 rendertarget_SetPixelsPerDip,
592 rendertarget_GetCurrentTransform,
593 rendertarget_SetCurrentTransform,
594 rendertarget_GetSize,
595 rendertarget_Resize,
596 rendertarget_GetTextAntialiasMode,
597 rendertarget_SetTextAntialiasMode
600 static HRESULT create_rendertarget(IDWriteFactory7 *factory, HDC hdc, UINT32 width, UINT32 height,
601 IDWriteBitmapRenderTarget **ret)
603 struct rendertarget *target;
604 HRESULT hr;
606 *ret = NULL;
608 target = heap_alloc(sizeof(struct rendertarget));
609 if (!target) return E_OUTOFMEMORY;
611 target->IDWriteBitmapRenderTarget1_iface.lpVtbl = &rendertargetvtbl;
612 target->ID2D1SimplifiedGeometrySink_iface.lpVtbl = &rendertargetsinkvtbl;
613 target->refcount = 1;
615 target->hdc = CreateCompatibleDC(hdc);
616 SetGraphicsMode(target->hdc, GM_ADVANCED);
617 hr = create_target_dibsection(target, width, height);
618 if (FAILED(hr)) {
619 IDWriteBitmapRenderTarget1_Release(&target->IDWriteBitmapRenderTarget1_iface);
620 return hr;
623 target->m = identity;
624 target->ppdip = GetDeviceCaps(target->hdc, LOGPIXELSX) / 96.0f;
625 target->antialiasmode = DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE;
626 target->factory = factory;
627 IDWriteFactory7_AddRef(factory);
629 *ret = (IDWriteBitmapRenderTarget*)&target->IDWriteBitmapRenderTarget1_iface;
631 return S_OK;
634 static HRESULT WINAPI gdiinterop_QueryInterface(IDWriteGdiInterop1 *iface, REFIID riid, void **obj)
636 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
638 if (IsEqualIID(riid, &IID_IDWriteGdiInterop1) ||
639 IsEqualIID(riid, &IID_IDWriteGdiInterop) ||
640 IsEqualIID(riid, &IID_IUnknown))
642 *obj = iface;
643 IDWriteGdiInterop1_AddRef(iface);
644 return S_OK;
647 WARN("%s not implemented.\n", debugstr_guid(riid));
649 *obj = NULL;
650 return E_NOINTERFACE;
653 static ULONG WINAPI gdiinterop_AddRef(IDWriteGdiInterop1 *iface)
655 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
656 LONG refcount = InterlockedIncrement(&interop->refcount);
658 TRACE("%p, refcount %u.\n", iface, refcount);
660 return refcount;
663 static ULONG WINAPI gdiinterop_Release(IDWriteGdiInterop1 *iface)
665 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
666 LONG refcount = InterlockedDecrement(&interop->refcount);
668 TRACE("%p, refcount %u.\n", iface, refcount);
670 if (!refcount)
672 IDWriteFactory7_UnregisterFontFileLoader(interop->factory, &interop->IDWriteFontFileLoader_iface);
673 factory_detach_gdiinterop(interop->factory, iface);
674 heap_free(interop);
677 return refcount;
680 static HRESULT WINAPI gdiinterop_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
681 LOGFONTW const *logfont, IDWriteFont **font)
683 TRACE("%p, %p, %p.\n", iface, logfont, font);
685 return IDWriteGdiInterop1_CreateFontFromLOGFONT(iface, logfont, NULL, font);
688 static HRESULT WINAPI gdiinterop_ConvertFontToLOGFONT(IDWriteGdiInterop1 *iface,
689 IDWriteFont *font, LOGFONTW *logfont, BOOL *is_systemfont)
691 IDWriteFontCollection *collection;
692 IDWriteFontFamily *family;
693 HRESULT hr;
695 TRACE("%p, %p, %p, %p.\n", iface, font, logfont, is_systemfont);
697 *is_systemfont = FALSE;
699 memset(logfont, 0, sizeof(*logfont));
701 if (!font)
702 return E_INVALIDARG;
704 hr = IDWriteFont_GetFontFamily(font, &family);
705 if (FAILED(hr))
706 return hr;
708 hr = IDWriteFontFamily_GetFontCollection(family, &collection);
709 IDWriteFontFamily_Release(family);
710 if (FAILED(hr))
711 return hr;
713 *is_systemfont = is_system_collection(collection);
714 IDWriteFontCollection_Release(collection);
716 get_logfont_from_font(font, logfont);
717 logfont->lfCharSet = DEFAULT_CHARSET;
718 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
720 return hr;
723 static HRESULT WINAPI gdiinterop_ConvertFontFaceToLOGFONT(IDWriteGdiInterop1 *iface,
724 IDWriteFontFace *fontface, LOGFONTW *logfont)
726 TRACE("%p, %p, %p.\n", iface, fontface, logfont);
728 memset(logfont, 0, sizeof(*logfont));
730 if (!fontface)
731 return E_INVALIDARG;
733 get_logfont_from_fontface(fontface, logfont);
734 logfont->lfCharSet = DEFAULT_CHARSET;
735 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
737 return S_OK;
740 struct font_realization_info
742 DWORD size;
743 DWORD flags;
744 DWORD cache_num;
745 DWORD instance_id;
746 DWORD file_count;
747 WORD face_index;
748 WORD simulations;
751 struct font_fileinfo
753 FILETIME writetime;
754 LARGE_INTEGER size;
755 WCHAR path[1];
758 /* Undocumented gdi32 exports, used to access actually selected font information */
759 extern BOOL WINAPI GetFontRealizationInfo(HDC hdc, struct font_realization_info *info);
760 extern BOOL WINAPI GetFontFileInfo(DWORD instance_id, DWORD file_index, struct font_fileinfo *info, SIZE_T size, SIZE_T *needed);
761 extern BOOL WINAPI GetFontFileData(DWORD instance_id, DWORD file_index, UINT64 offset, void *buff, DWORD buff_size);
763 static HRESULT WINAPI gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop1 *iface,
764 HDC hdc, IDWriteFontFace **fontface)
766 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
767 struct font_realization_info info;
768 struct font_fileinfo *fileinfo;
769 DWRITE_FONT_FILE_TYPE filetype;
770 DWRITE_FONT_FACE_TYPE facetype;
771 IDWriteFontFile *file;
772 BOOL is_supported;
773 UINT32 facenum;
774 SIZE_T needed;
775 HRESULT hr;
777 TRACE("%p, %p, %p.\n", iface, hdc, fontface);
779 *fontface = NULL;
781 if (!hdc)
782 return E_INVALIDARG;
784 /* get selected font id */
785 info.size = sizeof(info);
786 if (!GetFontRealizationInfo(hdc, &info)) {
787 WARN("failed to get selected font id\n");
788 return E_FAIL;
791 needed = 0;
792 GetFontFileInfo(info.instance_id, 0, NULL, 0, &needed);
793 if (needed == 0) {
794 WARN("failed to get font file info size\n");
795 return E_FAIL;
798 fileinfo = heap_alloc(needed);
799 if (!fileinfo)
800 return E_OUTOFMEMORY;
802 if (!GetFontFileInfo(info.instance_id, 0, fileinfo, needed, &needed)) {
803 heap_free(fileinfo);
804 return E_FAIL;
807 if (*fileinfo->path)
808 hr = IDWriteFactory7_CreateFontFileReference(interop->factory, fileinfo->path, &fileinfo->writetime, &file);
809 else
810 hr = IDWriteFactory7_CreateCustomFontFileReference(interop->factory, &info.instance_id,
811 sizeof(info.instance_id), &interop->IDWriteFontFileLoader_iface, &file);
813 heap_free(fileinfo);
814 if (FAILED(hr))
815 return hr;
817 is_supported = FALSE;
818 hr = IDWriteFontFile_Analyze(file, &is_supported, &filetype, &facetype, &facenum);
819 if (SUCCEEDED(hr)) {
820 if (is_supported)
821 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
822 hr = IDWriteFactory7_CreateFontFace(interop->factory, facetype, 1, &file, info.face_index,
823 info.simulations, fontface);
824 else
825 hr = DWRITE_E_FILEFORMAT;
828 IDWriteFontFile_Release(file);
829 return hr;
832 static HRESULT WINAPI gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop1 *iface,
833 HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
835 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
837 TRACE("%p, %p, %u, %u, %p.\n", iface, hdc, width, height, target);
839 return create_rendertarget(interop->factory, hdc, width, height, target);
842 static HRESULT WINAPI gdiinterop1_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
843 LOGFONTW const *logfont, IDWriteFontCollection *collection, IDWriteFont **font)
845 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
846 IDWriteFontFamily *family;
847 DWRITE_FONT_STYLE style;
848 BOOL exists = FALSE;
849 UINT32 index;
850 HRESULT hr;
852 TRACE("%p, %p, %p, %p.\n", iface, logfont, collection, font);
854 *font = NULL;
856 if (!logfont) return E_INVALIDARG;
858 if (collection)
859 IDWriteFontCollection_AddRef(collection);
860 else {
861 hr = IDWriteFactory5_GetSystemFontCollection((IDWriteFactory5 *)interop->factory, FALSE, (IDWriteFontCollection1 **)&collection, FALSE);
862 if (FAILED(hr)) {
863 ERR("failed to get system font collection: 0x%08x.\n", hr);
864 return hr;
868 hr = IDWriteFontCollection_FindFamilyName(collection, logfont->lfFaceName, &index, &exists);
869 if (FAILED(hr))
870 goto done;
872 if (!exists) {
873 hr = DWRITE_E_NOFONT;
874 goto done;
877 hr = IDWriteFontCollection_GetFontFamily(collection, index, &family);
878 if (FAILED(hr))
879 goto done;
881 style = logfont->lfItalic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
882 hr = IDWriteFontFamily_GetFirstMatchingFont(family, logfont->lfWeight, DWRITE_FONT_STRETCH_NORMAL, style, font);
883 IDWriteFontFamily_Release(family);
885 done:
886 IDWriteFontCollection_Release(collection);
887 return hr;
890 static HRESULT WINAPI gdiinterop1_GetFontSignature_(IDWriteGdiInterop1 *iface, IDWriteFontFace *fontface,
891 FONTSIGNATURE *fontsig)
893 TRACE("%p, %p, %p.\n", iface, fontface, fontsig);
895 return get_fontsig_from_fontface(fontface, fontsig);
898 static HRESULT WINAPI gdiinterop1_GetFontSignature(IDWriteGdiInterop1 *iface, IDWriteFont *font, FONTSIGNATURE *fontsig)
900 TRACE("%p, %p, %p.\n", iface, font, fontsig);
902 if (!font)
903 return E_INVALIDARG;
905 return get_fontsig_from_font(font, fontsig);
908 static HRESULT WINAPI gdiinterop1_GetMatchingFontsByLOGFONT(IDWriteGdiInterop1 *iface, LOGFONTW const *logfont,
909 IDWriteFontSet *fontset, IDWriteFontSet **subset)
911 FIXME("%p, %p, %p, %p: stub.\n", iface, logfont, fontset, subset);
913 return E_NOTIMPL;
916 static const IDWriteGdiInterop1Vtbl gdiinteropvtbl =
918 gdiinterop_QueryInterface,
919 gdiinterop_AddRef,
920 gdiinterop_Release,
921 gdiinterop_CreateFontFromLOGFONT,
922 gdiinterop_ConvertFontToLOGFONT,
923 gdiinterop_ConvertFontFaceToLOGFONT,
924 gdiinterop_CreateFontFaceFromHdc,
925 gdiinterop_CreateBitmapRenderTarget,
926 gdiinterop1_CreateFontFromLOGFONT,
927 gdiinterop1_GetFontSignature_,
928 gdiinterop1_GetFontSignature,
929 gdiinterop1_GetMatchingFontsByLOGFONT
932 static HRESULT WINAPI memresourcestream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **out)
934 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
936 if (IsEqualIID(&IID_IDWriteFontFileStream, riid) || IsEqualIID(&IID_IUnknown, riid)) {
937 *out = iface;
938 IDWriteFontFileStream_AddRef(iface);
939 return S_OK;
942 *out = NULL;
943 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
944 return E_NOINTERFACE;
947 static ULONG WINAPI memresourcestream_AddRef(IDWriteFontFileStream *iface)
949 struct memresource_stream *stream = impl_from_IDWriteFontFileStream(iface);
950 ULONG refcount = InterlockedIncrement(&stream->refcount);
952 TRACE("%p, refcount %d.\n", iface, refcount);
954 return refcount;
957 static ULONG WINAPI memresourcestream_Release(IDWriteFontFileStream *iface)
959 struct memresource_stream *stream = impl_from_IDWriteFontFileStream(iface);
960 ULONG refcount = InterlockedDecrement(&stream->refcount);
962 TRACE("%p, refcount %d.\n", iface, refcount);
964 if (!refcount)
965 heap_free(stream);
967 return refcount;
970 static HRESULT WINAPI memresourcestream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start,
971 UINT64 offset, UINT64 fragment_size, void **fragment_context)
973 struct memresource_stream *stream = impl_from_IDWriteFontFileStream(iface);
974 struct font_fileinfo fileinfo;
975 void *fragment;
977 TRACE("%p, %p, %s, %s, %p.\n", iface, fragment_start, wine_dbgstr_longlong(offset),
978 wine_dbgstr_longlong(fragment_size), fragment_context);
980 *fragment_context = NULL;
981 *fragment_start = NULL;
983 if (!GetFontFileInfo(stream->key, 0, &fileinfo, sizeof(fileinfo), NULL))
984 return E_INVALIDARG;
986 if ((offset >= fileinfo.size.QuadPart - 1) || (fragment_size > fileinfo.size.QuadPart - offset))
987 return E_INVALIDARG;
989 if (!(fragment = heap_alloc(fragment_size)))
990 return E_OUTOFMEMORY;
992 if (!GetFontFileData(stream->key, 0, offset, fragment, fragment_size))
993 return E_FAIL;
995 *fragment_start = *fragment_context = fragment;
996 return S_OK;
999 static void WINAPI memresourcestream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
1001 TRACE("%p, %p.\n", iface, fragment_context);
1003 heap_free(fragment_context);
1006 static HRESULT WINAPI memresourcestream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
1008 struct memresource_stream *stream = impl_from_IDWriteFontFileStream(iface);
1009 struct font_fileinfo fileinfo;
1011 TRACE("%p, %p.\n", iface, size);
1013 if (!GetFontFileInfo(stream->key, 0, &fileinfo, sizeof(fileinfo), NULL))
1014 return E_INVALIDARG;
1016 *size = fileinfo.size.QuadPart;
1018 return S_OK;
1021 static HRESULT WINAPI memresourcestream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
1023 TRACE("%p, %p.\n", iface, last_writetime);
1025 return E_NOTIMPL;
1028 static const IDWriteFontFileStreamVtbl memresourcestreamvtbl =
1030 memresourcestream_QueryInterface,
1031 memresourcestream_AddRef,
1032 memresourcestream_Release,
1033 memresourcestream_ReadFileFragment,
1034 memresourcestream_ReleaseFileFragment,
1035 memresourcestream_GetFileSize,
1036 memresourcestream_GetLastWriteTime,
1039 static HRESULT WINAPI memresourceloader_QueryInterface(IDWriteFontFileLoader *iface, REFIID riid, void **out)
1041 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
1043 if (IsEqualIID(&IID_IDWriteFontFileLoader, riid) || IsEqualIID(&IID_IUnknown, riid)) {
1044 *out = iface;
1045 IDWriteFontFileLoader_AddRef(iface);
1046 return S_OK;
1049 *out = NULL;
1050 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
1051 return E_NOINTERFACE;
1054 static ULONG WINAPI memresourceloader_AddRef(IDWriteFontFileLoader *iface)
1056 return 2;
1059 static ULONG WINAPI memresourceloader_Release(IDWriteFontFileLoader *iface)
1061 return 1;
1064 static HRESULT WINAPI memresourceloader_CreateStreamFromKey(IDWriteFontFileLoader *iface, void const *key,
1065 UINT32 key_size, IDWriteFontFileStream **ret)
1067 struct memresource_stream *stream;
1069 TRACE("%p, %p, %u, %p.\n", iface, key, key_size, ret);
1071 *ret = NULL;
1073 if (!key || key_size != sizeof(DWORD))
1074 return E_INVALIDARG;
1076 if (!(stream = heap_alloc(sizeof(*stream))))
1077 return E_OUTOFMEMORY;
1079 stream->IDWriteFontFileStream_iface.lpVtbl = &memresourcestreamvtbl;
1080 stream->refcount = 1;
1081 memcpy(&stream->key, key, sizeof(stream->key));
1083 *ret = &stream->IDWriteFontFileStream_iface;
1085 return S_OK;
1088 static const IDWriteFontFileLoaderVtbl memresourceloadervtbl =
1090 memresourceloader_QueryInterface,
1091 memresourceloader_AddRef,
1092 memresourceloader_Release,
1093 memresourceloader_CreateStreamFromKey,
1096 HRESULT create_gdiinterop(IDWriteFactory7 *factory, IDWriteGdiInterop1 **ret)
1098 struct gdiinterop *interop;
1100 *ret = NULL;
1102 if (!(interop = heap_alloc(sizeof(*interop))))
1103 return E_OUTOFMEMORY;
1105 interop->IDWriteGdiInterop1_iface.lpVtbl = &gdiinteropvtbl;
1106 interop->IDWriteFontFileLoader_iface.lpVtbl = &memresourceloadervtbl;
1107 interop->refcount = 1;
1108 interop->factory = factory;
1109 IDWriteFactory7_AddRef(interop->factory);
1110 IDWriteFactory7_RegisterFontFileLoader(factory, &interop->IDWriteFontFileLoader_iface);
1112 *ret = &interop->IDWriteGdiInterop1_iface;
1113 return S_OK;