mfplat: Implement MFCreateMediaBufferFromMediaType() for audio types.
[wine.git] / dlls / dwrite / gdiinterop.c
blob7e88f7490caf02f6ad3a2a7bd30375f74f8247ed
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 {
65 IDWriteFontFileStream IDWriteFontFileStream_iface;
66 LONG ref;
67 DWORD key;
70 static inline int get_dib_stride(int width, int bpp)
72 return ((width * bpp + 31) >> 3) & ~3;
75 static HRESULT create_target_dibsection(struct rendertarget *target, UINT32 width, UINT32 height)
77 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
78 BITMAPINFO *bmi = (BITMAPINFO*)bmibuf;
79 HBITMAP hbm;
81 target->size.cx = width;
82 target->size.cy = height;
84 memset(bmi, 0, sizeof(bmibuf));
85 bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
86 bmi->bmiHeader.biHeight = -height;
87 bmi->bmiHeader.biWidth = width;
88 bmi->bmiHeader.biBitCount = 32;
89 bmi->bmiHeader.biPlanes = 1;
90 bmi->bmiHeader.biCompression = BI_RGB;
92 hbm = CreateDIBSection(target->hdc, bmi, DIB_RGB_COLORS, (void**)&target->dib.ptr, NULL, 0);
93 if (!hbm) {
94 hbm = CreateBitmap(1, 1, 1, 1, NULL);
95 target->dib.ptr = NULL;
96 target->dib.stride = 0;
97 target->dib.width = 0;
99 else {
100 target->dib.stride = get_dib_stride(width, 32);
101 target->dib.width = width;
104 DeleteObject(SelectObject(target->hdc, hbm));
105 return S_OK;
108 static inline struct rendertarget *impl_from_IDWriteBitmapRenderTarget1(IDWriteBitmapRenderTarget1 *iface)
110 return CONTAINING_RECORD(iface, struct rendertarget, IDWriteBitmapRenderTarget1_iface);
113 static inline struct rendertarget *impl_from_ID2D1SimplifiedGeometrySink(ID2D1SimplifiedGeometrySink *iface)
115 return CONTAINING_RECORD(iface, struct rendertarget, ID2D1SimplifiedGeometrySink_iface);
118 static inline struct gdiinterop *impl_from_IDWriteGdiInterop1(IDWriteGdiInterop1 *iface)
120 return CONTAINING_RECORD(iface, struct gdiinterop, IDWriteGdiInterop1_iface);
123 static inline struct gdiinterop *impl_from_IDWriteFontFileLoader(IDWriteFontFileLoader *iface)
125 return CONTAINING_RECORD(iface, struct gdiinterop, IDWriteFontFileLoader_iface);
128 static inline struct memresource_stream *impl_from_IDWriteFontFileStream(IDWriteFontFileStream *iface)
130 return CONTAINING_RECORD(iface, struct memresource_stream, IDWriteFontFileStream_iface);
133 static HRESULT WINAPI rendertarget_sink_QueryInterface(ID2D1SimplifiedGeometrySink *iface, REFIID riid, void **obj)
135 if (IsEqualIID(riid, &IID_ID2D1SimplifiedGeometrySink) ||
136 IsEqualIID(riid, &IID_IUnknown))
138 *obj = iface;
139 ID2D1SimplifiedGeometrySink_AddRef(iface);
140 return S_OK;
143 WARN("%s not implemented.\n", debugstr_guid(riid));
145 *obj = NULL;
147 return E_NOINTERFACE;
150 static ULONG WINAPI rendertarget_sink_AddRef(ID2D1SimplifiedGeometrySink *iface)
152 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
153 return IDWriteBitmapRenderTarget1_AddRef(&This->IDWriteBitmapRenderTarget1_iface);
156 static ULONG WINAPI rendertarget_sink_Release(ID2D1SimplifiedGeometrySink *iface)
158 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
159 return IDWriteBitmapRenderTarget1_Release(&This->IDWriteBitmapRenderTarget1_iface);
162 static void WINAPI rendertarget_sink_SetFillMode(ID2D1SimplifiedGeometrySink *iface, D2D1_FILL_MODE mode)
164 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
165 SetPolyFillMode(This->hdc, mode == D2D1_FILL_MODE_ALTERNATE ? ALTERNATE : WINDING);
168 static void WINAPI rendertarget_sink_SetSegmentFlags(ID2D1SimplifiedGeometrySink *iface, D2D1_PATH_SEGMENT vertexFlags)
172 static void WINAPI rendertarget_sink_BeginFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin)
174 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
175 MoveToEx(This->hdc, startPoint.x, startPoint.y, NULL);
178 static void WINAPI rendertarget_sink_AddLines(ID2D1SimplifiedGeometrySink *iface, const D2D1_POINT_2F *points, UINT32 count)
180 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
182 while (count--) {
183 LineTo(This->hdc, points->x, points->y);
184 points++;
188 static void WINAPI rendertarget_sink_AddBeziers(ID2D1SimplifiedGeometrySink *iface, const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
190 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
191 POINT points[3];
193 while (count--) {
194 points[0].x = beziers->point1.x;
195 points[0].y = beziers->point1.y;
196 points[1].x = beziers->point2.x;
197 points[1].y = beziers->point2.y;
198 points[2].x = beziers->point3.x;
199 points[2].y = beziers->point3.y;
201 PolyBezierTo(This->hdc, points, 3);
202 beziers++;
206 static void WINAPI rendertarget_sink_EndFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_FIGURE_END figureEnd)
208 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
209 CloseFigure(This->hdc);
212 static HRESULT WINAPI rendertarget_sink_Close(ID2D1SimplifiedGeometrySink *iface)
214 return S_OK;
217 static const ID2D1SimplifiedGeometrySinkVtbl rendertargetsinkvtbl = {
218 rendertarget_sink_QueryInterface,
219 rendertarget_sink_AddRef,
220 rendertarget_sink_Release,
221 rendertarget_sink_SetFillMode,
222 rendertarget_sink_SetSegmentFlags,
223 rendertarget_sink_BeginFigure,
224 rendertarget_sink_AddLines,
225 rendertarget_sink_AddBeziers,
226 rendertarget_sink_EndFigure,
227 rendertarget_sink_Close
230 static HRESULT WINAPI rendertarget_QueryInterface(IDWriteBitmapRenderTarget1 *iface, REFIID riid, void **obj)
232 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
234 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
236 if (IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget1) ||
237 IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget) ||
238 IsEqualIID(riid, &IID_IUnknown))
240 *obj = iface;
241 IDWriteBitmapRenderTarget1_AddRef(iface);
242 return S_OK;
245 WARN("%s not implemented.\n", debugstr_guid(riid));
247 *obj = NULL;
249 return E_NOINTERFACE;
252 static ULONG WINAPI rendertarget_AddRef(IDWriteBitmapRenderTarget1 *iface)
254 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
255 ULONG refcount = InterlockedIncrement(&target->refcount);
257 TRACE("%p, refcount %u.\n", iface, refcount);
259 return refcount;
262 static ULONG WINAPI rendertarget_Release(IDWriteBitmapRenderTarget1 *iface)
264 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
265 ULONG refcount = InterlockedDecrement(&target->refcount);
267 TRACE("%p, refcount %u.\n", iface, refcount);
269 if (!refcount)
271 IDWriteFactory7_Release(target->factory);
272 DeleteDC(target->hdc);
273 heap_free(target);
276 return refcount;
279 static inline DWORD *get_pixel_ptr_32(struct dib_data *dib, int x, int y)
281 return (DWORD *)((BYTE*)dib->ptr + y * dib->stride + x * 4);
284 static inline BYTE blend_color(BYTE dst, BYTE src, BYTE alpha)
286 return (src * alpha + dst * (255 - alpha) + 127) / 255;
289 static inline DWORD blend_subpixel(BYTE r, BYTE g, BYTE b, DWORD text, const BYTE *alpha)
291 return blend_color(r, text >> 16, alpha[0]) << 16 |
292 blend_color(g, text >> 8, alpha[1]) << 8 |
293 blend_color(b, text, alpha[2]);
296 static inline DWORD blend_pixel(BYTE r, BYTE g, BYTE b, DWORD text, BYTE alpha)
298 return blend_color(r, text >> 16, alpha) << 16 |
299 blend_color(g, text >> 8, alpha) << 8 |
300 blend_color(b, text, alpha);
303 static void blit_8(struct dib_data *dib, const BYTE *src, const RECT *rect, DWORD text_pixel)
305 DWORD *dst_ptr = get_pixel_ptr_32(dib, rect->left, rect->top);
306 int x, y, src_width = rect->right - rect->left;
308 for (y = rect->top; y < rect->bottom; y++) {
309 for (x = 0; x < src_width; x++) {
310 if (!src[x]) continue;
311 if (src[x] == DWRITE_ALPHA_MAX)
312 dst_ptr[x] = text_pixel;
313 else
314 dst_ptr[x] = blend_pixel(dst_ptr[x] >> 16, dst_ptr[x] >> 8, dst_ptr[x], text_pixel, src[x]);
317 src += src_width;
318 dst_ptr += dib->stride / 4;
322 static void blit_subpixel_888(struct dib_data *dib, int dib_width, const BYTE *src,
323 const RECT *rect, DWORD text_pixel)
325 DWORD *dst_ptr = get_pixel_ptr_32(dib, rect->left, rect->top);
326 int x, y, src_width = rect->right - rect->left;
328 for (y = rect->top; y < rect->bottom; y++) {
329 for (x = 0; x < src_width; x++) {
330 if (src[3*x] == 0 && src[3*x+1] == 0 && src[3*x+2] == 0) continue;
331 dst_ptr[x] = blend_subpixel(dst_ptr[x] >> 16, dst_ptr[x] >> 8, dst_ptr[x], text_pixel, &src[3*x]);
333 dst_ptr += dib->stride / 4;
334 src += src_width * 3;
338 static inline DWORD colorref_to_pixel_888(COLORREF color)
340 return (((color >> 16) & 0xff) | (color & 0xff00) | ((color << 16) & 0xff0000));
343 static HRESULT WINAPI rendertarget_DrawGlyphRun(IDWriteBitmapRenderTarget1 *iface,
344 FLOAT originX, FLOAT originY, DWRITE_MEASURING_MODE measuring_mode,
345 DWRITE_GLYPH_RUN const *run, IDWriteRenderingParams *params, COLORREF color,
346 RECT *bbox_ret)
348 struct rendertarget *target = impl_from_IDWriteBitmapRenderTarget1(iface);
349 IDWriteGlyphRunAnalysis *analysis;
350 DWRITE_RENDERING_MODE1 rendermode;
351 DWRITE_GRID_FIT_MODE gridfitmode;
352 DWRITE_TEXTURE_TYPE texturetype;
353 DWRITE_GLYPH_RUN scaled_run;
354 IDWriteFontFace3 *fontface;
355 RECT target_rect, bounds;
356 HRESULT hr;
358 TRACE("%p, %.8e, %.8e, %d, %p, %p, 0x%08x, %p.\n", iface, originX, originY,
359 measuring_mode, run, params, color, bbox_ret);
361 SetRectEmpty(bbox_ret);
363 if (!target->dib.ptr)
364 return S_OK;
366 if (!params)
367 return E_INVALIDARG;
369 if (FAILED(hr = IDWriteFontFace_QueryInterface(run->fontFace, &IID_IDWriteFontFace3, (void **)&fontface))) {
370 WARN("Failed to get IDWriteFontFace2 interface, hr %#x.\n", hr);
371 return hr;
374 hr = IDWriteFontFace3_GetRecommendedRenderingMode(fontface, run->fontEmSize, target->ppdip * 96.0f,
375 target->ppdip * 96.0f, NULL /* FIXME */, run->isSideways, DWRITE_OUTLINE_THRESHOLD_ALIASED, measuring_mode,
376 params, &rendermode, &gridfitmode);
377 IDWriteFontFace3_Release(fontface);
378 if (FAILED(hr))
379 return hr;
381 SetRect(&target_rect, 0, 0, target->size.cx, target->size.cy);
383 if (rendermode == DWRITE_RENDERING_MODE1_OUTLINE)
385 static const XFORM identity = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f };
386 const DWRITE_MATRIX *m = &target->m;
387 XFORM xform;
389 /* target allows any transform to be set, filter it here */
390 if (m->m11 * m->m22 == m->m12 * m->m21) {
391 xform.eM11 = 1.0f;
392 xform.eM12 = 0.0f;
393 xform.eM21 = 0.0f;
394 xform.eM22 = 1.0f;
395 xform.eDx = originX;
396 xform.eDy = originY;
397 } else {
398 xform.eM11 = m->m11;
399 xform.eM12 = m->m12;
400 xform.eM21 = m->m21;
401 xform.eM22 = m->m22;
402 xform.eDx = m->m11 * originX + m->m21 * originY + m->dx;
403 xform.eDy = m->m12 * originX + m->m22 * originY + m->dy;
405 SetWorldTransform(target->hdc, &xform);
407 BeginPath(target->hdc);
409 hr = IDWriteFontFace_GetGlyphRunOutline(run->fontFace, run->fontEmSize * target->ppdip,
410 run->glyphIndices, run->glyphAdvances, run->glyphOffsets, run->glyphCount,
411 run->isSideways, run->bidiLevel & 1, &target->ID2D1SimplifiedGeometrySink_iface);
413 EndPath(target->hdc);
415 if (hr == S_OK)
417 HBRUSH brush = CreateSolidBrush(color);
419 SelectObject(target->hdc, brush);
421 FillPath(target->hdc);
423 /* FIXME: one way to get affected rectangle bounds is to use region fill */
424 if (bbox_ret)
425 *bbox_ret = target_rect;
427 DeleteObject(brush);
430 SetWorldTransform(target->hdc, &identity);
432 return hr;
435 scaled_run = *run;
436 scaled_run.fontEmSize *= target->ppdip;
437 hr = IDWriteFactory7_CreateGlyphRunAnalysis(target->factory, &scaled_run, &target->m, rendermode, measuring_mode,
438 gridfitmode, target->antialiasmode, originX, originY, &analysis);
439 if (FAILED(hr))
441 WARN("failed to create analysis instance, 0x%08x\n", hr);
442 return hr;
445 SetRectEmpty(&bounds);
446 texturetype = DWRITE_TEXTURE_ALIASED_1x1;
447 hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &bounds);
448 if (FAILED(hr) || IsRectEmpty(&bounds)) {
449 hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &bounds);
450 if (FAILED(hr)) {
451 WARN("GetAlphaTextureBounds() failed, 0x%08x\n", hr);
452 IDWriteGlyphRunAnalysis_Release(analysis);
453 return hr;
455 texturetype = DWRITE_TEXTURE_CLEARTYPE_3x1;
458 if (IntersectRect(&target_rect, &target_rect, &bounds))
460 UINT32 size = (target_rect.right - target_rect.left) * (target_rect.bottom - target_rect.top);
461 BYTE *bitmap;
463 color = colorref_to_pixel_888(color);
464 if (texturetype == DWRITE_TEXTURE_CLEARTYPE_3x1)
465 size *= 3;
466 bitmap = heap_alloc_zero(size);
467 if (!bitmap) {
468 IDWriteGlyphRunAnalysis_Release(analysis);
469 return E_OUTOFMEMORY;
472 hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, texturetype, &target_rect, bitmap, size);
473 if (hr == S_OK) {
474 /* blit to target dib */
475 if (texturetype == DWRITE_TEXTURE_ALIASED_1x1)
476 blit_8(&target->dib, bitmap, &target_rect, color);
477 else
478 blit_subpixel_888(&target->dib, target->size.cx, bitmap, &target_rect, color);
480 if (bbox_ret) *bbox_ret = target_rect;
483 heap_free(bitmap);
486 IDWriteGlyphRunAnalysis_Release(analysis);
488 return S_OK;
491 static HDC WINAPI rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget1 *iface)
493 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
494 TRACE("(%p)\n", This);
495 return This->hdc;
498 static FLOAT WINAPI rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget1 *iface)
500 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
501 TRACE("(%p)\n", This);
502 return This->ppdip;
505 static HRESULT WINAPI rendertarget_SetPixelsPerDip(IDWriteBitmapRenderTarget1 *iface, FLOAT ppdip)
507 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
509 TRACE("(%p)->(%.2f)\n", This, ppdip);
511 if (ppdip <= 0.0f)
512 return E_INVALIDARG;
514 This->ppdip = ppdip;
515 return S_OK;
518 static HRESULT WINAPI rendertarget_GetCurrentTransform(IDWriteBitmapRenderTarget1 *iface, DWRITE_MATRIX *transform)
520 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
522 TRACE("(%p)->(%p)\n", This, transform);
524 *transform = This->m;
525 return S_OK;
528 static HRESULT WINAPI rendertarget_SetCurrentTransform(IDWriteBitmapRenderTarget1 *iface, DWRITE_MATRIX const *transform)
530 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
532 TRACE("(%p)->(%p)\n", This, transform);
534 This->m = transform ? *transform : identity;
535 return S_OK;
538 static HRESULT WINAPI rendertarget_GetSize(IDWriteBitmapRenderTarget1 *iface, SIZE *size)
540 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
542 TRACE("(%p)->(%p)\n", This, size);
543 *size = This->size;
544 return S_OK;
547 static HRESULT WINAPI rendertarget_Resize(IDWriteBitmapRenderTarget1 *iface, UINT32 width, UINT32 height)
549 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
551 TRACE("(%p)->(%u %u)\n", This, width, height);
553 if (This->size.cx == width && This->size.cy == height)
554 return S_OK;
556 return create_target_dibsection(This, width, height);
559 static DWRITE_TEXT_ANTIALIAS_MODE WINAPI rendertarget_GetTextAntialiasMode(IDWriteBitmapRenderTarget1 *iface)
561 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
562 TRACE("(%p)\n", This);
563 return This->antialiasmode;
566 static HRESULT WINAPI rendertarget_SetTextAntialiasMode(IDWriteBitmapRenderTarget1 *iface, DWRITE_TEXT_ANTIALIAS_MODE mode)
568 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
570 TRACE("(%p)->(%d)\n", This, mode);
572 if ((DWORD)mode > DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE)
573 return E_INVALIDARG;
575 This->antialiasmode = mode;
576 return S_OK;
579 static const IDWriteBitmapRenderTarget1Vtbl rendertargetvtbl = {
580 rendertarget_QueryInterface,
581 rendertarget_AddRef,
582 rendertarget_Release,
583 rendertarget_DrawGlyphRun,
584 rendertarget_GetMemoryDC,
585 rendertarget_GetPixelsPerDip,
586 rendertarget_SetPixelsPerDip,
587 rendertarget_GetCurrentTransform,
588 rendertarget_SetCurrentTransform,
589 rendertarget_GetSize,
590 rendertarget_Resize,
591 rendertarget_GetTextAntialiasMode,
592 rendertarget_SetTextAntialiasMode
595 static HRESULT create_rendertarget(IDWriteFactory7 *factory, HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **ret)
597 struct rendertarget *target;
598 HRESULT hr;
600 *ret = NULL;
602 target = heap_alloc(sizeof(struct rendertarget));
603 if (!target) return E_OUTOFMEMORY;
605 target->IDWriteBitmapRenderTarget1_iface.lpVtbl = &rendertargetvtbl;
606 target->ID2D1SimplifiedGeometrySink_iface.lpVtbl = &rendertargetsinkvtbl;
607 target->refcount = 1;
609 target->hdc = CreateCompatibleDC(hdc);
610 SetGraphicsMode(target->hdc, GM_ADVANCED);
611 hr = create_target_dibsection(target, width, height);
612 if (FAILED(hr)) {
613 IDWriteBitmapRenderTarget1_Release(&target->IDWriteBitmapRenderTarget1_iface);
614 return hr;
617 target->m = identity;
618 target->ppdip = GetDeviceCaps(target->hdc, LOGPIXELSX) / 96.0f;
619 target->antialiasmode = DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE;
620 target->factory = factory;
621 IDWriteFactory7_AddRef(factory);
623 *ret = (IDWriteBitmapRenderTarget*)&target->IDWriteBitmapRenderTarget1_iface;
625 return S_OK;
628 static HRESULT WINAPI gdiinterop_QueryInterface(IDWriteGdiInterop1 *iface, REFIID riid, void **obj)
630 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
632 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
634 if (IsEqualIID(riid, &IID_IDWriteGdiInterop1) ||
635 IsEqualIID(riid, &IID_IDWriteGdiInterop) ||
636 IsEqualIID(riid, &IID_IUnknown))
638 *obj = iface;
639 IDWriteGdiInterop1_AddRef(iface);
640 return S_OK;
643 WARN("%s not implemented.\n", debugstr_guid(riid));
645 *obj = NULL;
646 return E_NOINTERFACE;
649 static ULONG WINAPI gdiinterop_AddRef(IDWriteGdiInterop1 *iface)
651 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
652 LONG refcount = InterlockedIncrement(&interop->refcount);
654 TRACE("%p, refcount %u.\n", iface, refcount);
656 return refcount;
659 static ULONG WINAPI gdiinterop_Release(IDWriteGdiInterop1 *iface)
661 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
662 LONG refcount = InterlockedDecrement(&interop->refcount);
664 TRACE("%p, refcount %u.\n", iface, refcount);
666 if (!refcount)
668 IDWriteFactory7_UnregisterFontFileLoader(interop->factory, &interop->IDWriteFontFileLoader_iface);
669 factory_detach_gdiinterop(interop->factory, iface);
670 heap_free(interop);
673 return refcount;
676 static HRESULT WINAPI gdiinterop_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
677 LOGFONTW const *logfont, IDWriteFont **font)
679 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
681 TRACE("(%p)->(%p %p)\n", This, logfont, font);
683 return IDWriteGdiInterop1_CreateFontFromLOGFONT(iface, logfont, NULL, font);
686 static HRESULT WINAPI gdiinterop_ConvertFontToLOGFONT(IDWriteGdiInterop1 *iface,
687 IDWriteFont *font, LOGFONTW *logfont, BOOL *is_systemfont)
689 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
690 IDWriteFontCollection *collection;
691 IDWriteFontFamily *family;
692 HRESULT hr;
694 TRACE("(%p)->(%p %p %p)\n", This, font, logfont, is_systemfont);
696 *is_systemfont = FALSE;
698 memset(logfont, 0, sizeof(*logfont));
700 if (!font)
701 return E_INVALIDARG;
703 hr = IDWriteFont_GetFontFamily(font, &family);
704 if (FAILED(hr))
705 return hr;
707 hr = IDWriteFontFamily_GetFontCollection(family, &collection);
708 IDWriteFontFamily_Release(family);
709 if (FAILED(hr))
710 return hr;
712 *is_systemfont = is_system_collection(collection);
713 IDWriteFontCollection_Release(collection);
715 get_logfont_from_font(font, logfont);
716 logfont->lfCharSet = DEFAULT_CHARSET;
717 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
719 return hr;
722 static HRESULT WINAPI gdiinterop_ConvertFontFaceToLOGFONT(IDWriteGdiInterop1 *iface,
723 IDWriteFontFace *fontface, LOGFONTW *logfont)
725 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
727 TRACE("(%p)->(%p %p)\n", This, fontface, logfont);
729 memset(logfont, 0, sizeof(*logfont));
731 if (!fontface)
732 return E_INVALIDARG;
734 get_logfont_from_fontface(fontface, logfont);
735 logfont->lfCharSet = DEFAULT_CHARSET;
736 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
738 return S_OK;
741 struct font_realization_info {
742 DWORD size;
743 DWORD flags;
744 DWORD cache_num;
745 DWORD instance_id;
746 DWORD unk;
747 WORD face_index;
748 WORD simulations;
751 struct font_fileinfo {
752 FILETIME writetime;
753 LARGE_INTEGER size;
754 WCHAR path[1];
757 /* Undocumented gdi32 exports, used to access actually selected font information */
758 extern BOOL WINAPI GetFontRealizationInfo(HDC hdc, struct font_realization_info *info);
759 extern BOOL WINAPI GetFontFileInfo(DWORD instance_id, DWORD unknown, struct font_fileinfo *info, SIZE_T size, SIZE_T *needed);
760 extern BOOL WINAPI GetFontFileData(DWORD instance_id, DWORD unknown, UINT64 offset, void *buff, DWORD buff_size);
762 static HRESULT WINAPI gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop1 *iface,
763 HDC hdc, IDWriteFontFace **fontface)
765 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
766 struct font_realization_info info;
767 struct font_fileinfo *fileinfo;
768 DWRITE_FONT_FILE_TYPE filetype;
769 DWRITE_FONT_FACE_TYPE facetype;
770 IDWriteFontFile *file;
771 BOOL is_supported;
772 UINT32 facenum;
773 SIZE_T needed;
774 HRESULT hr;
776 TRACE("%p, %p, %p.\n", iface, hdc, fontface);
778 *fontface = NULL;
780 if (!hdc)
781 return E_INVALIDARG;
783 /* get selected font id */
784 info.size = sizeof(info);
785 if (!GetFontRealizationInfo(hdc, &info)) {
786 WARN("failed to get selected font id\n");
787 return E_FAIL;
790 needed = 0;
791 GetFontFileInfo(info.instance_id, 0, NULL, 0, &needed);
792 if (needed == 0) {
793 WARN("failed to get font file info size\n");
794 return E_FAIL;
797 fileinfo = heap_alloc(needed);
798 if (!fileinfo)
799 return E_OUTOFMEMORY;
801 if (!GetFontFileInfo(info.instance_id, 0, fileinfo, needed, &needed)) {
802 heap_free(fileinfo);
803 return E_FAIL;
806 if (*fileinfo->path)
807 hr = IDWriteFactory7_CreateFontFileReference(interop->factory, fileinfo->path, &fileinfo->writetime, &file);
808 else
809 hr = IDWriteFactory7_CreateCustomFontFileReference(interop->factory, &info.instance_id,
810 sizeof(info.instance_id), &interop->IDWriteFontFileLoader_iface, &file);
812 heap_free(fileinfo);
813 if (FAILED(hr))
814 return hr;
816 is_supported = FALSE;
817 hr = IDWriteFontFile_Analyze(file, &is_supported, &filetype, &facetype, &facenum);
818 if (SUCCEEDED(hr)) {
819 if (is_supported)
820 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
821 hr = IDWriteFactory7_CreateFontFace(interop->factory, facetype, 1, &file, info.face_index,
822 info.simulations, fontface);
823 else
824 hr = DWRITE_E_FILEFORMAT;
827 IDWriteFontFile_Release(file);
828 return hr;
831 static HRESULT WINAPI gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop1 *iface,
832 HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
834 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
835 TRACE("(%p)->(%p %u %u %p)\n", This, hdc, width, height, target);
836 return create_rendertarget(This->factory, hdc, width, height, target);
839 static HRESULT WINAPI gdiinterop1_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
840 LOGFONTW const *logfont, IDWriteFontCollection *collection, IDWriteFont **font)
842 struct gdiinterop *interop = impl_from_IDWriteGdiInterop1(iface);
843 IDWriteFontFamily *family;
844 DWRITE_FONT_STYLE style;
845 BOOL exists = FALSE;
846 UINT32 index;
847 HRESULT hr;
849 TRACE("%p, %p, %p, %p.\n", iface, logfont, collection, font);
851 *font = NULL;
853 if (!logfont) return E_INVALIDARG;
855 if (collection)
856 IDWriteFontCollection_AddRef(collection);
857 else {
858 hr = IDWriteFactory5_GetSystemFontCollection((IDWriteFactory5 *)interop->factory, FALSE, (IDWriteFontCollection1 **)&collection, FALSE);
859 if (FAILED(hr)) {
860 ERR("failed to get system font collection: 0x%08x.\n", hr);
861 return hr;
865 hr = IDWriteFontCollection_FindFamilyName(collection, logfont->lfFaceName, &index, &exists);
866 if (FAILED(hr))
867 goto done;
869 if (!exists) {
870 hr = DWRITE_E_NOFONT;
871 goto done;
874 hr = IDWriteFontCollection_GetFontFamily(collection, index, &family);
875 if (FAILED(hr))
876 goto done;
878 style = logfont->lfItalic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
879 hr = IDWriteFontFamily_GetFirstMatchingFont(family, logfont->lfWeight, DWRITE_FONT_STRETCH_NORMAL, style, font);
880 IDWriteFontFamily_Release(family);
882 done:
883 IDWriteFontCollection_Release(collection);
884 return hr;
887 static HRESULT WINAPI gdiinterop1_GetFontSignature_(IDWriteGdiInterop1 *iface, IDWriteFontFace *fontface,
888 FONTSIGNATURE *fontsig)
890 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
892 TRACE("(%p)->(%p %p)\n", This, fontface, fontsig);
894 return get_fontsig_from_fontface(fontface, fontsig);
897 static HRESULT WINAPI gdiinterop1_GetFontSignature(IDWriteGdiInterop1 *iface, IDWriteFont *font, FONTSIGNATURE *fontsig)
899 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
901 TRACE("(%p)->(%p %p)\n", This, font, fontsig);
903 if (!font)
904 return E_INVALIDARG;
906 return get_fontsig_from_font(font, fontsig);
909 static HRESULT WINAPI gdiinterop1_GetMatchingFontsByLOGFONT(IDWriteGdiInterop1 *iface, LOGFONTW const *logfont,
910 IDWriteFontSet *fontset, IDWriteFontSet **subset)
912 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
914 FIXME("(%p)->(%p %p %p): stub\n", This, logfont, fontset, subset);
916 return E_NOTIMPL;
919 static const struct IDWriteGdiInterop1Vtbl gdiinteropvtbl = {
920 gdiinterop_QueryInterface,
921 gdiinterop_AddRef,
922 gdiinterop_Release,
923 gdiinterop_CreateFontFromLOGFONT,
924 gdiinterop_ConvertFontToLOGFONT,
925 gdiinterop_ConvertFontFaceToLOGFONT,
926 gdiinterop_CreateFontFaceFromHdc,
927 gdiinterop_CreateBitmapRenderTarget,
928 gdiinterop1_CreateFontFromLOGFONT,
929 gdiinterop1_GetFontSignature_,
930 gdiinterop1_GetFontSignature,
931 gdiinterop1_GetMatchingFontsByLOGFONT
934 static HRESULT WINAPI memresourcestream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **out)
936 struct memresource_stream *This = impl_from_IDWriteFontFileStream(iface);
938 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), out);
940 if (IsEqualIID(&IID_IDWriteFontFileStream, riid) || IsEqualIID(&IID_IUnknown, riid)) {
941 *out = iface;
942 IDWriteFontFileStream_AddRef(iface);
943 return S_OK;
946 *out = NULL;
947 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
948 return E_NOINTERFACE;
951 static ULONG WINAPI memresourcestream_AddRef(IDWriteFontFileStream *iface)
953 struct memresource_stream *This = impl_from_IDWriteFontFileStream(iface);
954 ULONG ref = InterlockedIncrement(&This->ref);
955 TRACE("(%p)->(%d)\n", This, ref);
956 return ref;
959 static ULONG WINAPI memresourcestream_Release(IDWriteFontFileStream *iface)
961 struct memresource_stream *This = impl_from_IDWriteFontFileStream(iface);
962 ULONG ref = InterlockedDecrement(&This->ref);
964 TRACE("(%p)->(%d)\n", This, ref);
966 if (!ref)
967 heap_free(This);
969 return ref;
972 static HRESULT WINAPI memresourcestream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start,
973 UINT64 offset, UINT64 fragment_size, void **fragment_context)
975 struct memresource_stream *This = impl_from_IDWriteFontFileStream(iface);
976 struct font_fileinfo fileinfo;
977 void *fragment;
979 TRACE("(%p)->(%p %s %s %p)\n", This, fragment_start, wine_dbgstr_longlong(offset),
980 wine_dbgstr_longlong(fragment_size), fragment_context);
982 *fragment_context = NULL;
983 *fragment_start = NULL;
985 if (!GetFontFileInfo(This->key, 0, &fileinfo, sizeof(fileinfo), NULL))
986 return E_INVALIDARG;
988 if ((offset >= fileinfo.size.QuadPart - 1) || (fragment_size > fileinfo.size.QuadPart - offset))
989 return E_INVALIDARG;
991 if (!(fragment = heap_alloc(fragment_size)))
992 return E_OUTOFMEMORY;
994 if (!GetFontFileData(This->key, 0, offset, fragment, fragment_size))
995 return E_FAIL;
997 *fragment_start = *fragment_context = fragment;
998 return S_OK;
1001 static void WINAPI memresourcestream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
1003 struct memresource_stream *This = impl_from_IDWriteFontFileStream(iface);
1005 TRACE("(%p)->(%p)\n", This, fragment_context);
1007 heap_free(fragment_context);
1010 static HRESULT WINAPI memresourcestream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
1012 struct memresource_stream *This = impl_from_IDWriteFontFileStream(iface);
1013 struct font_fileinfo fileinfo;
1015 TRACE("(%p)->(%p)\n", This, size);
1017 if (!GetFontFileInfo(This->key, 0, &fileinfo, sizeof(fileinfo), NULL))
1018 return E_INVALIDARG;
1020 *size = fileinfo.size.QuadPart;
1022 return S_OK;
1025 static HRESULT WINAPI memresourcestream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
1027 struct memresource_stream *This = impl_from_IDWriteFontFileStream(iface);
1029 TRACE("(%p)->(%p)\n", This, last_writetime);
1031 return E_NOTIMPL;
1034 static const struct IDWriteFontFileStreamVtbl memresourcestreamvtbl = {
1035 memresourcestream_QueryInterface,
1036 memresourcestream_AddRef,
1037 memresourcestream_Release,
1038 memresourcestream_ReadFileFragment,
1039 memresourcestream_ReleaseFileFragment,
1040 memresourcestream_GetFileSize,
1041 memresourcestream_GetLastWriteTime,
1044 static HRESULT WINAPI memresourceloader_QueryInterface(IDWriteFontFileLoader *iface, REFIID riid, void **out)
1046 struct gdiinterop *This = impl_from_IDWriteFontFileLoader(iface);
1048 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), out);
1050 if (IsEqualIID(&IID_IDWriteFontFileLoader, riid) || IsEqualIID(&IID_IUnknown, riid)) {
1051 *out = iface;
1052 IDWriteFontFileLoader_AddRef(iface);
1053 return S_OK;
1056 *out = NULL;
1057 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
1058 return E_NOINTERFACE;
1061 static ULONG WINAPI memresourceloader_AddRef(IDWriteFontFileLoader *iface)
1063 return 2;
1066 static ULONG WINAPI memresourceloader_Release(IDWriteFontFileLoader *iface)
1068 return 1;
1071 static HRESULT WINAPI memresourceloader_CreateStreamFromKey(IDWriteFontFileLoader *iface, void const *key,
1072 UINT32 key_size, IDWriteFontFileStream **ret)
1074 struct gdiinterop *This = impl_from_IDWriteFontFileLoader(iface);
1075 struct memresource_stream *stream;
1077 TRACE("(%p)->(%p %u %p)\n", This, key, key_size, ret);
1079 *ret = NULL;
1081 if (!key || key_size != sizeof(DWORD))
1082 return E_INVALIDARG;
1084 if (!(stream = heap_alloc(sizeof(*stream))))
1085 return E_OUTOFMEMORY;
1087 stream->IDWriteFontFileStream_iface.lpVtbl = &memresourcestreamvtbl;
1088 stream->ref = 1;
1089 memcpy(&stream->key, key, sizeof(stream->key));
1091 *ret = &stream->IDWriteFontFileStream_iface;
1093 return S_OK;
1096 static const struct IDWriteFontFileLoaderVtbl memresourceloadervtbl = {
1097 memresourceloader_QueryInterface,
1098 memresourceloader_AddRef,
1099 memresourceloader_Release,
1100 memresourceloader_CreateStreamFromKey,
1103 HRESULT create_gdiinterop(IDWriteFactory7 *factory, IDWriteGdiInterop1 **ret)
1105 struct gdiinterop *interop;
1107 *ret = NULL;
1109 if (!(interop = heap_alloc(sizeof(*interop))))
1110 return E_OUTOFMEMORY;
1112 interop->IDWriteGdiInterop1_iface.lpVtbl = &gdiinteropvtbl;
1113 interop->IDWriteFontFileLoader_iface.lpVtbl = &memresourceloadervtbl;
1114 interop->refcount = 1;
1115 interop->factory = factory;
1116 IDWriteFactory7_AddRef(interop->factory);
1117 IDWriteFactory7_RegisterFontFileLoader(factory, &interop->IDWriteFontFileLoader_iface);
1119 *ret = &interop->IDWriteGdiInterop1_iface;
1120 return S_OK;