wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / dwrite / gdiinterop.c
blob6a5953a67e9286686df7e4e3afc1d8a4c97253d1
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 {
741 DWORD size;
742 DWORD flags;
743 DWORD cache_num;
744 DWORD instance_id;
745 DWORD unk;
746 WORD face_index;
747 WORD simulations;
750 struct font_fileinfo {
751 FILETIME writetime;
752 LARGE_INTEGER size;
753 WCHAR path[1];
756 /* Undocumented gdi32 exports, used to access actually selected font information */
757 extern BOOL WINAPI GetFontRealizationInfo(HDC hdc, struct font_realization_info *info);
758 extern BOOL WINAPI GetFontFileInfo(DWORD instance_id, DWORD unknown, struct font_fileinfo *info, SIZE_T size, SIZE_T *needed);
759 extern BOOL WINAPI GetFontFileData(DWORD instance_id, DWORD unknown, UINT64 offset, void *buff, DWORD buff_size);
761 static HRESULT WINAPI gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop1 *iface,
762 HDC hdc, IDWriteFontFace **fontface)
764 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
765 struct font_realization_info info;
766 struct font_fileinfo *fileinfo;
767 DWRITE_FONT_FILE_TYPE filetype;
768 DWRITE_FONT_FACE_TYPE facetype;
769 IDWriteFontFile *file;
770 BOOL is_supported;
771 UINT32 facenum;
772 SIZE_T needed;
773 HRESULT hr;
775 TRACE("%p, %p, %p.\n", iface, hdc, fontface);
777 *fontface = NULL;
779 if (!hdc)
780 return E_INVALIDARG;
782 /* get selected font id */
783 info.size = sizeof(info);
784 if (!GetFontRealizationInfo(hdc, &info)) {
785 WARN("failed to get selected font id\n");
786 return E_FAIL;
789 needed = 0;
790 GetFontFileInfo(info.instance_id, 0, NULL, 0, &needed);
791 if (needed == 0) {
792 WARN("failed to get font file info size\n");
793 return E_FAIL;
796 fileinfo = heap_alloc(needed);
797 if (!fileinfo)
798 return E_OUTOFMEMORY;
800 if (!GetFontFileInfo(info.instance_id, 0, fileinfo, needed, &needed)) {
801 heap_free(fileinfo);
802 return E_FAIL;
805 if (*fileinfo->path)
806 hr = IDWriteFactory7_CreateFontFileReference(interop->factory, fileinfo->path, &fileinfo->writetime, &file);
807 else
808 hr = IDWriteFactory7_CreateCustomFontFileReference(interop->factory, &info.instance_id,
809 sizeof(info.instance_id), &interop->IDWriteFontFileLoader_iface, &file);
811 heap_free(fileinfo);
812 if (FAILED(hr))
813 return hr;
815 is_supported = FALSE;
816 hr = IDWriteFontFile_Analyze(file, &is_supported, &filetype, &facetype, &facenum);
817 if (SUCCEEDED(hr)) {
818 if (is_supported)
819 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
820 hr = IDWriteFactory7_CreateFontFace(interop->factory, facetype, 1, &file, info.face_index,
821 info.simulations, fontface);
822 else
823 hr = DWRITE_E_FILEFORMAT;
826 IDWriteFontFile_Release(file);
827 return hr;
830 static HRESULT WINAPI gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop1 *iface,
831 HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
833 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
835 TRACE("%p, %p, %u, %u, %p.\n", iface, hdc, width, height, target);
837 return create_rendertarget(interop->factory, hdc, width, height, target);
840 static HRESULT WINAPI gdiinterop1_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
841 LOGFONTW const *logfont, IDWriteFontCollection *collection, IDWriteFont **font)
843 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
844 IDWriteFontFamily *family;
845 DWRITE_FONT_STYLE style;
846 BOOL exists = FALSE;
847 UINT32 index;
848 HRESULT hr;
850 TRACE("%p, %p, %p, %p.\n", iface, logfont, collection, font);
852 *font = NULL;
854 if (!logfont) return E_INVALIDARG;
856 if (collection)
857 IDWriteFontCollection_AddRef(collection);
858 else {
859 hr = IDWriteFactory5_GetSystemFontCollection((IDWriteFactory5 *)interop->factory, FALSE, (IDWriteFontCollection1 **)&collection, FALSE);
860 if (FAILED(hr)) {
861 ERR("failed to get system font collection: 0x%08x.\n", hr);
862 return hr;
866 hr = IDWriteFontCollection_FindFamilyName(collection, logfont->lfFaceName, &index, &exists);
867 if (FAILED(hr))
868 goto done;
870 if (!exists) {
871 hr = DWRITE_E_NOFONT;
872 goto done;
875 hr = IDWriteFontCollection_GetFontFamily(collection, index, &family);
876 if (FAILED(hr))
877 goto done;
879 style = logfont->lfItalic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
880 hr = IDWriteFontFamily_GetFirstMatchingFont(family, logfont->lfWeight, DWRITE_FONT_STRETCH_NORMAL, style, font);
881 IDWriteFontFamily_Release(family);
883 done:
884 IDWriteFontCollection_Release(collection);
885 return hr;
888 static HRESULT WINAPI gdiinterop1_GetFontSignature_(IDWriteGdiInterop1 *iface, IDWriteFontFace *fontface,
889 FONTSIGNATURE *fontsig)
891 TRACE("%p, %p, %p.\n", iface, fontface, fontsig);
893 return get_fontsig_from_fontface(fontface, fontsig);
896 static HRESULT WINAPI gdiinterop1_GetFontSignature(IDWriteGdiInterop1 *iface, IDWriteFont *font, FONTSIGNATURE *fontsig)
898 TRACE("%p, %p, %p.\n", iface, font, fontsig);
900 if (!font)
901 return E_INVALIDARG;
903 return get_fontsig_from_font(font, fontsig);
906 static HRESULT WINAPI gdiinterop1_GetMatchingFontsByLOGFONT(IDWriteGdiInterop1 *iface, LOGFONTW const *logfont,
907 IDWriteFontSet *fontset, IDWriteFontSet **subset)
909 FIXME("%p, %p, %p, %p: stub.\n", iface, logfont, fontset, subset);
911 return E_NOTIMPL;
914 static const IDWriteGdiInterop1Vtbl gdiinteropvtbl =
916 gdiinterop_QueryInterface,
917 gdiinterop_AddRef,
918 gdiinterop_Release,
919 gdiinterop_CreateFontFromLOGFONT,
920 gdiinterop_ConvertFontToLOGFONT,
921 gdiinterop_ConvertFontFaceToLOGFONT,
922 gdiinterop_CreateFontFaceFromHdc,
923 gdiinterop_CreateBitmapRenderTarget,
924 gdiinterop1_CreateFontFromLOGFONT,
925 gdiinterop1_GetFontSignature_,
926 gdiinterop1_GetFontSignature,
927 gdiinterop1_GetMatchingFontsByLOGFONT
930 static HRESULT WINAPI memresourcestream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **out)
932 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
934 if (IsEqualIID(&IID_IDWriteFontFileStream, riid) || IsEqualIID(&IID_IUnknown, riid)) {
935 *out = iface;
936 IDWriteFontFileStream_AddRef(iface);
937 return S_OK;
940 *out = NULL;
941 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
942 return E_NOINTERFACE;
945 static ULONG WINAPI memresourcestream_AddRef(IDWriteFontFileStream *iface)
947 struct memresource_stream *stream = impl_from_IDWriteFontFileStream(iface);
948 ULONG refcount = InterlockedIncrement(&stream->refcount);
950 TRACE("%p, refcount %d.\n", iface, refcount);
952 return refcount;
955 static ULONG WINAPI memresourcestream_Release(IDWriteFontFileStream *iface)
957 struct memresource_stream *stream = impl_from_IDWriteFontFileStream(iface);
958 ULONG refcount = InterlockedDecrement(&stream->refcount);
960 TRACE("%p, refcount %d.\n", iface, refcount);
962 if (!refcount)
963 heap_free(stream);
965 return refcount;
968 static HRESULT WINAPI memresourcestream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start,
969 UINT64 offset, UINT64 fragment_size, void **fragment_context)
971 struct memresource_stream *stream = impl_from_IDWriteFontFileStream(iface);
972 struct font_fileinfo fileinfo;
973 void *fragment;
975 TRACE("%p, %p, %s, %s, %p.\n", iface, fragment_start, wine_dbgstr_longlong(offset),
976 wine_dbgstr_longlong(fragment_size), fragment_context);
978 *fragment_context = NULL;
979 *fragment_start = NULL;
981 if (!GetFontFileInfo(stream->key, 0, &fileinfo, sizeof(fileinfo), NULL))
982 return E_INVALIDARG;
984 if ((offset >= fileinfo.size.QuadPart - 1) || (fragment_size > fileinfo.size.QuadPart - offset))
985 return E_INVALIDARG;
987 if (!(fragment = heap_alloc(fragment_size)))
988 return E_OUTOFMEMORY;
990 if (!GetFontFileData(stream->key, 0, offset, fragment, fragment_size))
991 return E_FAIL;
993 *fragment_start = *fragment_context = fragment;
994 return S_OK;
997 static void WINAPI memresourcestream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
999 TRACE("%p, %p.\n", iface, fragment_context);
1001 heap_free(fragment_context);
1004 static HRESULT WINAPI memresourcestream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
1006 struct memresource_stream *stream = impl_from_IDWriteFontFileStream(iface);
1007 struct font_fileinfo fileinfo;
1009 TRACE("%p, %p.\n", iface, size);
1011 if (!GetFontFileInfo(stream->key, 0, &fileinfo, sizeof(fileinfo), NULL))
1012 return E_INVALIDARG;
1014 *size = fileinfo.size.QuadPart;
1016 return S_OK;
1019 static HRESULT WINAPI memresourcestream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
1021 TRACE("%p, %p.\n", iface, last_writetime);
1023 return E_NOTIMPL;
1026 static const IDWriteFontFileStreamVtbl memresourcestreamvtbl =
1028 memresourcestream_QueryInterface,
1029 memresourcestream_AddRef,
1030 memresourcestream_Release,
1031 memresourcestream_ReadFileFragment,
1032 memresourcestream_ReleaseFileFragment,
1033 memresourcestream_GetFileSize,
1034 memresourcestream_GetLastWriteTime,
1037 static HRESULT WINAPI memresourceloader_QueryInterface(IDWriteFontFileLoader *iface, REFIID riid, void **out)
1039 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
1041 if (IsEqualIID(&IID_IDWriteFontFileLoader, riid) || IsEqualIID(&IID_IUnknown, riid)) {
1042 *out = iface;
1043 IDWriteFontFileLoader_AddRef(iface);
1044 return S_OK;
1047 *out = NULL;
1048 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
1049 return E_NOINTERFACE;
1052 static ULONG WINAPI memresourceloader_AddRef(IDWriteFontFileLoader *iface)
1054 return 2;
1057 static ULONG WINAPI memresourceloader_Release(IDWriteFontFileLoader *iface)
1059 return 1;
1062 static HRESULT WINAPI memresourceloader_CreateStreamFromKey(IDWriteFontFileLoader *iface, void const *key,
1063 UINT32 key_size, IDWriteFontFileStream **ret)
1065 struct memresource_stream *stream;
1067 TRACE("%p, %p, %u, %p.\n", iface, key, key_size, ret);
1069 *ret = NULL;
1071 if (!key || key_size != sizeof(DWORD))
1072 return E_INVALIDARG;
1074 if (!(stream = heap_alloc(sizeof(*stream))))
1075 return E_OUTOFMEMORY;
1077 stream->IDWriteFontFileStream_iface.lpVtbl = &memresourcestreamvtbl;
1078 stream->refcount = 1;
1079 memcpy(&stream->key, key, sizeof(stream->key));
1081 *ret = &stream->IDWriteFontFileStream_iface;
1083 return S_OK;
1086 static const IDWriteFontFileLoaderVtbl memresourceloadervtbl =
1088 memresourceloader_QueryInterface,
1089 memresourceloader_AddRef,
1090 memresourceloader_Release,
1091 memresourceloader_CreateStreamFromKey,
1094 HRESULT create_gdiinterop(IDWriteFactory7 *factory, IDWriteGdiInterop1 **ret)
1096 struct gdiinterop *interop;
1098 *ret = NULL;
1100 if (!(interop = heap_alloc(sizeof(*interop))))
1101 return E_OUTOFMEMORY;
1103 interop->IDWriteGdiInterop1_iface.lpVtbl = &gdiinteropvtbl;
1104 interop->IDWriteFontFileLoader_iface.lpVtbl = &memresourceloadervtbl;
1105 interop->refcount = 1;
1106 interop->factory = factory;
1107 IDWriteFactory7_AddRef(interop->factory);
1108 IDWriteFactory7_RegisterFontFileLoader(factory, &interop->IDWriteFontFileLoader_iface);
1110 *ret = &interop->IDWriteGdiInterop1_iface;
1111 return S_OK;