dwrite: Partially implement GetGlyphImageFormats().
[wine.git] / dlls / dwrite / gdiinterop.c
blob2b3f32ddd7dbd01711422e2a9278ffdd33fb4ac7
1 /*
2 * GDI Interop
4 * Copyright 2011 Huw Davies
5 * Copyright 2012, 2014-2016 Nikolay Sivov for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "dwrite_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
35 struct dib_data {
36 DWORD *ptr;
37 int stride;
38 int width;
41 struct rendertarget {
42 IDWriteBitmapRenderTarget1 IDWriteBitmapRenderTarget1_iface;
43 ID2D1SimplifiedGeometrySink ID2D1SimplifiedGeometrySink_iface;
44 LONG ref;
46 IDWriteFactory *factory;
47 DWRITE_TEXT_ANTIALIAS_MODE antialiasmode;
48 FLOAT ppdip;
49 DWRITE_MATRIX m;
50 SIZE size;
51 HDC hdc;
52 struct dib_data dib;
55 struct gdiinterop {
56 IDWriteGdiInterop1 IDWriteGdiInterop1_iface;
57 LONG ref;
58 IDWriteFactory4 *factory;
61 static inline int get_dib_stride(int width, int bpp)
63 return ((width * bpp + 31) >> 3) & ~3;
66 static HRESULT create_target_dibsection(struct rendertarget *target, UINT32 width, UINT32 height)
68 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
69 BITMAPINFO *bmi = (BITMAPINFO*)bmibuf;
70 HBITMAP hbm;
72 target->size.cx = width;
73 target->size.cy = height;
75 memset(bmi, 0, sizeof(bmibuf));
76 bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
77 bmi->bmiHeader.biHeight = -height;
78 bmi->bmiHeader.biWidth = width;
79 bmi->bmiHeader.biBitCount = 32;
80 bmi->bmiHeader.biPlanes = 1;
81 bmi->bmiHeader.biCompression = BI_RGB;
83 hbm = CreateDIBSection(target->hdc, bmi, DIB_RGB_COLORS, (void**)&target->dib.ptr, NULL, 0);
84 if (!hbm) {
85 hbm = CreateBitmap(1, 1, 1, 1, NULL);
86 target->dib.ptr = NULL;
87 target->dib.stride = 0;
88 target->dib.width = 0;
90 else {
91 target->dib.stride = get_dib_stride(width, 32);
92 target->dib.width = width;
95 DeleteObject(SelectObject(target->hdc, hbm));
96 return S_OK;
99 static inline struct rendertarget *impl_from_IDWriteBitmapRenderTarget1(IDWriteBitmapRenderTarget1 *iface)
101 return CONTAINING_RECORD(iface, struct rendertarget, IDWriteBitmapRenderTarget1_iface);
104 static inline struct rendertarget *impl_from_ID2D1SimplifiedGeometrySink(ID2D1SimplifiedGeometrySink *iface)
106 return CONTAINING_RECORD(iface, struct rendertarget, ID2D1SimplifiedGeometrySink_iface);
109 static inline struct gdiinterop *impl_from_IDWriteGdiInterop1(IDWriteGdiInterop1 *iface)
111 return CONTAINING_RECORD(iface, struct gdiinterop, IDWriteGdiInterop1_iface);
114 static HRESULT WINAPI rendertarget_sink_QueryInterface(ID2D1SimplifiedGeometrySink *iface, REFIID riid, void **obj)
116 if (IsEqualIID(riid, &IID_ID2D1SimplifiedGeometrySink) ||
117 IsEqualIID(riid, &IID_IUnknown))
119 *obj = iface;
120 ID2D1SimplifiedGeometrySink_AddRef(iface);
121 return S_OK;
124 *obj = NULL;
126 return E_NOINTERFACE;
129 static ULONG WINAPI rendertarget_sink_AddRef(ID2D1SimplifiedGeometrySink *iface)
131 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
132 return IDWriteBitmapRenderTarget1_AddRef(&This->IDWriteBitmapRenderTarget1_iface);
135 static ULONG WINAPI rendertarget_sink_Release(ID2D1SimplifiedGeometrySink *iface)
137 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
138 return IDWriteBitmapRenderTarget1_Release(&This->IDWriteBitmapRenderTarget1_iface);
141 static void WINAPI rendertarget_sink_SetFillMode(ID2D1SimplifiedGeometrySink *iface, D2D1_FILL_MODE mode)
143 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
144 SetPolyFillMode(This->hdc, mode == D2D1_FILL_MODE_ALTERNATE ? ALTERNATE : WINDING);
147 static void WINAPI rendertarget_sink_SetSegmentFlags(ID2D1SimplifiedGeometrySink *iface, D2D1_PATH_SEGMENT vertexFlags)
151 static void WINAPI rendertarget_sink_BeginFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin)
153 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
154 MoveToEx(This->hdc, startPoint.x, startPoint.y, NULL);
157 static void WINAPI rendertarget_sink_AddLines(ID2D1SimplifiedGeometrySink *iface, const D2D1_POINT_2F *points, UINT32 count)
159 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
161 while (count--) {
162 LineTo(This->hdc, points->x, points->y);
163 points++;
167 static void WINAPI rendertarget_sink_AddBeziers(ID2D1SimplifiedGeometrySink *iface, const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
169 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
170 POINT points[3];
172 while (count--) {
173 points[0].x = beziers->point1.x;
174 points[0].y = beziers->point1.y;
175 points[1].x = beziers->point2.x;
176 points[1].y = beziers->point2.y;
177 points[2].x = beziers->point3.x;
178 points[2].y = beziers->point3.y;
180 PolyBezierTo(This->hdc, points, 3);
181 beziers++;
185 static void WINAPI rendertarget_sink_EndFigure(ID2D1SimplifiedGeometrySink *iface, D2D1_FIGURE_END figureEnd)
187 struct rendertarget *This = impl_from_ID2D1SimplifiedGeometrySink(iface);
188 CloseFigure(This->hdc);
191 static HRESULT WINAPI rendertarget_sink_Close(ID2D1SimplifiedGeometrySink *iface)
193 return S_OK;
196 static const ID2D1SimplifiedGeometrySinkVtbl rendertargetsinkvtbl = {
197 rendertarget_sink_QueryInterface,
198 rendertarget_sink_AddRef,
199 rendertarget_sink_Release,
200 rendertarget_sink_SetFillMode,
201 rendertarget_sink_SetSegmentFlags,
202 rendertarget_sink_BeginFigure,
203 rendertarget_sink_AddLines,
204 rendertarget_sink_AddBeziers,
205 rendertarget_sink_EndFigure,
206 rendertarget_sink_Close
209 static HRESULT WINAPI rendertarget_QueryInterface(IDWriteBitmapRenderTarget1 *iface, REFIID riid, void **obj)
211 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
213 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
215 if (IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget1) ||
216 IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget) ||
217 IsEqualIID(riid, &IID_IUnknown))
219 *obj = iface;
220 IDWriteBitmapRenderTarget1_AddRef(iface);
221 return S_OK;
224 *obj = NULL;
226 return E_NOINTERFACE;
229 static ULONG WINAPI rendertarget_AddRef(IDWriteBitmapRenderTarget1 *iface)
231 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
232 ULONG ref = InterlockedIncrement(&This->ref);
233 TRACE("(%p)->(%d)\n", This, ref);
234 return ref;
237 static ULONG WINAPI rendertarget_Release(IDWriteBitmapRenderTarget1 *iface)
239 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
240 ULONG ref = InterlockedDecrement(&This->ref);
242 TRACE("(%p)->(%d)\n", This, ref);
244 if (!ref)
246 IDWriteFactory_Release(This->factory);
247 DeleteDC(This->hdc);
248 heap_free(This);
251 return ref;
254 static inline DWORD *get_pixel_ptr_32(struct dib_data *dib, int x, int y)
256 return (DWORD *)((BYTE*)dib->ptr + y * dib->stride + x * 4);
259 static void blit_8(struct dib_data *dib, const BYTE *src, const RECT *rect, DWORD text_pixel)
261 DWORD *dst_ptr = get_pixel_ptr_32(dib, rect->left, rect->top);
262 int x, y, src_width = rect->right - rect->left;
264 for (y = rect->top; y < rect->bottom; y++) {
265 for (x = 0; x < src_width; x++) {
266 if (src[x] < DWRITE_ALPHA_MAX) continue;
267 dst_ptr[x] = text_pixel;
270 src += src_width;
271 dst_ptr += dib->stride / 4;
275 static inline BYTE blend_color(BYTE dst, BYTE src, BYTE alpha)
277 return (src * alpha + dst * (255 - alpha) + 127) / 255;
280 static inline DWORD blend_subpixel(BYTE r, BYTE g, BYTE b, DWORD text, const BYTE *alpha)
282 return blend_color(r, text >> 16, alpha[0]) << 16 |
283 blend_color(g, text >> 8, alpha[1]) << 8 |
284 blend_color(b, text, alpha[2]);
287 static void blit_subpixel_888(struct dib_data *dib, int dib_width, const BYTE *src,
288 const RECT *rect, DWORD text_pixel)
290 DWORD *dst_ptr = get_pixel_ptr_32(dib, rect->left, rect->top);
291 int x, y, src_width = rect->right - rect->left;
293 for (y = rect->top; y < rect->bottom; y++) {
294 for (x = 0; x < src_width; x++) {
295 if (src[3*x] == 0 && src[3*x+1] == 0 && src[3*x+2] == 0) continue;
296 dst_ptr[x] = blend_subpixel(dst_ptr[x] >> 16, dst_ptr[x] >> 8, dst_ptr[x], text_pixel, &src[3*x]);
298 dst_ptr += dib->stride / 4;
299 src += src_width * 3;
303 static inline DWORD colorref_to_pixel_888(COLORREF color)
305 return (((color >> 16) & 0xff) | (color & 0xff00) | ((color << 16) & 0xff0000));
308 static HRESULT WINAPI rendertarget_DrawGlyphRun(IDWriteBitmapRenderTarget1 *iface,
309 FLOAT originX, FLOAT originY, DWRITE_MEASURING_MODE measuring_mode,
310 DWRITE_GLYPH_RUN const *run, IDWriteRenderingParams *params, COLORREF color,
311 RECT *bbox_ret)
313 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
314 IDWriteGlyphRunAnalysis *analysis;
315 DWRITE_RENDERING_MODE rendermode;
316 DWRITE_TEXTURE_TYPE texturetype;
317 IDWriteFontFace1 *fontface1;
318 RECT target, bounds;
319 HRESULT hr;
321 TRACE("(%p)->(%.2f %.2f %d %p %p 0x%08x %p)\n", This, originX, originY,
322 measuring_mode, run, params, color, bbox_ret);
324 SetRectEmpty(bbox_ret);
326 if (!This->dib.ptr)
327 return S_OK;
329 hr = IDWriteFontFace_QueryInterface(run->fontFace, &IID_IDWriteFontFace1, (void**)&fontface1);
330 if (hr == S_OK) {
331 hr = IDWriteFontFace1_GetRecommendedRenderingMode(fontface1, run->fontEmSize, This->ppdip * 96.0f,
332 This->ppdip * 96.0f, NULL, run->isSideways, DWRITE_OUTLINE_THRESHOLD_ALIASED, measuring_mode,
333 &rendermode);
334 IDWriteFontFace1_Release(fontface1);
336 else
337 hr = IDWriteFontFace_GetRecommendedRenderingMode(run->fontFace, run->fontEmSize,
338 This->ppdip, measuring_mode, params, &rendermode);
340 if (FAILED(hr))
341 return hr;
343 SetRect(&target, 0, 0, This->size.cx, This->size.cy);
345 if (rendermode == DWRITE_RENDERING_MODE_OUTLINE) {
346 static const XFORM identity = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f };
347 const DWRITE_MATRIX *m = &This->m;
348 XFORM xform;
350 /* target allows any transform to be set, filter it here */
351 if (m->m11 * m->m22 == m->m12 * m->m21) {
352 xform.eM11 = 1.0f;
353 xform.eM12 = 0.0f;
354 xform.eM21 = 0.0f;
355 xform.eM22 = 1.0f;
356 xform.eDx = originX;
357 xform.eDy = originY;
358 } else {
359 xform.eM11 = m->m11;
360 xform.eM12 = m->m12;
361 xform.eM21 = m->m21;
362 xform.eM22 = m->m22;
363 xform.eDx = m->m11 * originX + m->m21 * originY + m->dx;
364 xform.eDy = m->m12 * originX + m->m22 * originY + m->dy;
366 SetWorldTransform(This->hdc, &xform);
368 BeginPath(This->hdc);
370 hr = IDWriteFontFace_GetGlyphRunOutline(run->fontFace, run->fontEmSize * This->ppdip,
371 run->glyphIndices, run->glyphAdvances, run->glyphOffsets, run->glyphCount,
372 run->isSideways, run->bidiLevel & 1, &This->ID2D1SimplifiedGeometrySink_iface);
374 EndPath(This->hdc);
376 if (hr == S_OK) {
377 HBRUSH brush = CreateSolidBrush(color);
379 SelectObject(This->hdc, brush);
381 FillPath(This->hdc);
383 /* FIXME: one way to get affected rectangle bounds is to use region fill */
384 if (bbox_ret)
385 *bbox_ret = target;
387 DeleteObject(brush);
390 SetWorldTransform(This->hdc, &identity);
392 return hr;
395 hr = IDWriteFactory_CreateGlyphRunAnalysis(This->factory,
396 run, This->ppdip, &This->m, rendermode, measuring_mode,
397 originX, originY, &analysis);
398 if (FAILED(hr)) {
399 WARN("failed to create analysis instance, 0x%08x\n", hr);
400 return hr;
403 SetRectEmpty(&bounds);
404 texturetype = DWRITE_TEXTURE_ALIASED_1x1;
405 hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_ALIASED_1x1, &bounds);
406 if (FAILED(hr) || IsRectEmpty(&bounds)) {
407 hr = IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis, DWRITE_TEXTURE_CLEARTYPE_3x1, &bounds);
408 if (FAILED(hr)) {
409 WARN("GetAlphaTextureBounds() failed, 0x%08x\n", hr);
410 IDWriteGlyphRunAnalysis_Release(analysis);
411 return hr;
413 texturetype = DWRITE_TEXTURE_CLEARTYPE_3x1;
416 if (IntersectRect(&target, &target, &bounds)) {
417 UINT32 size = (target.right - target.left) * (target.bottom - target.top);
418 BYTE *bitmap;
420 color = colorref_to_pixel_888(color);
421 if (texturetype == DWRITE_TEXTURE_CLEARTYPE_3x1)
422 size *= 3;
423 bitmap = heap_alloc_zero(size);
424 if (!bitmap) {
425 IDWriteGlyphRunAnalysis_Release(analysis);
426 return E_OUTOFMEMORY;
429 hr = IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis, texturetype, &target, bitmap, size);
430 if (hr == S_OK) {
431 /* blit to target dib */
432 if (texturetype == DWRITE_TEXTURE_ALIASED_1x1)
433 blit_8(&This->dib, bitmap, &target, color);
434 else
435 blit_subpixel_888(&This->dib, This->size.cx, bitmap, &target, color);
437 if (bbox_ret) *bbox_ret = target;
440 heap_free(bitmap);
443 IDWriteGlyphRunAnalysis_Release(analysis);
445 return S_OK;
448 static HDC WINAPI rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget1 *iface)
450 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
451 TRACE("(%p)\n", This);
452 return This->hdc;
455 static FLOAT WINAPI rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget1 *iface)
457 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
458 TRACE("(%p)\n", This);
459 return This->ppdip;
462 static HRESULT WINAPI rendertarget_SetPixelsPerDip(IDWriteBitmapRenderTarget1 *iface, FLOAT ppdip)
464 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
466 TRACE("(%p)->(%.2f)\n", This, ppdip);
468 if (ppdip <= 0.0f)
469 return E_INVALIDARG;
471 This->ppdip = ppdip;
472 return S_OK;
475 static HRESULT WINAPI rendertarget_GetCurrentTransform(IDWriteBitmapRenderTarget1 *iface, DWRITE_MATRIX *transform)
477 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
479 TRACE("(%p)->(%p)\n", This, transform);
481 *transform = This->m;
482 return S_OK;
485 static HRESULT WINAPI rendertarget_SetCurrentTransform(IDWriteBitmapRenderTarget1 *iface, DWRITE_MATRIX const *transform)
487 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
489 TRACE("(%p)->(%p)\n", This, transform);
491 This->m = transform ? *transform : identity;
492 return S_OK;
495 static HRESULT WINAPI rendertarget_GetSize(IDWriteBitmapRenderTarget1 *iface, SIZE *size)
497 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
499 TRACE("(%p)->(%p)\n", This, size);
500 *size = This->size;
501 return S_OK;
504 static HRESULT WINAPI rendertarget_Resize(IDWriteBitmapRenderTarget1 *iface, UINT32 width, UINT32 height)
506 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
508 TRACE("(%p)->(%u %u)\n", This, width, height);
510 if (This->size.cx == width && This->size.cy == height)
511 return S_OK;
513 return create_target_dibsection(This, width, height);
516 static DWRITE_TEXT_ANTIALIAS_MODE WINAPI rendertarget_GetTextAntialiasMode(IDWriteBitmapRenderTarget1 *iface)
518 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
519 TRACE("(%p)\n", This);
520 return This->antialiasmode;
523 static HRESULT WINAPI rendertarget_SetTextAntialiasMode(IDWriteBitmapRenderTarget1 *iface, DWRITE_TEXT_ANTIALIAS_MODE mode)
525 struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget1(iface);
527 TRACE("(%p)->(%d)\n", This, mode);
529 if ((DWORD)mode > DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE)
530 return E_INVALIDARG;
532 This->antialiasmode = mode;
533 return S_OK;
536 static const IDWriteBitmapRenderTarget1Vtbl rendertargetvtbl = {
537 rendertarget_QueryInterface,
538 rendertarget_AddRef,
539 rendertarget_Release,
540 rendertarget_DrawGlyphRun,
541 rendertarget_GetMemoryDC,
542 rendertarget_GetPixelsPerDip,
543 rendertarget_SetPixelsPerDip,
544 rendertarget_GetCurrentTransform,
545 rendertarget_SetCurrentTransform,
546 rendertarget_GetSize,
547 rendertarget_Resize,
548 rendertarget_GetTextAntialiasMode,
549 rendertarget_SetTextAntialiasMode
552 static HRESULT create_rendertarget(IDWriteFactory *factory, HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **ret)
554 struct rendertarget *target;
555 HRESULT hr;
557 *ret = NULL;
559 target = heap_alloc(sizeof(struct rendertarget));
560 if (!target) return E_OUTOFMEMORY;
562 target->IDWriteBitmapRenderTarget1_iface.lpVtbl = &rendertargetvtbl;
563 target->ID2D1SimplifiedGeometrySink_iface.lpVtbl = &rendertargetsinkvtbl;
564 target->ref = 1;
566 target->hdc = CreateCompatibleDC(hdc);
567 SetGraphicsMode(target->hdc, GM_ADVANCED);
568 hr = create_target_dibsection(target, width, height);
569 if (FAILED(hr)) {
570 IDWriteBitmapRenderTarget1_Release(&target->IDWriteBitmapRenderTarget1_iface);
571 return hr;
574 target->m = identity;
575 target->ppdip = GetDeviceCaps(target->hdc, LOGPIXELSX) / 96.0f;
576 target->antialiasmode = DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE;
577 target->factory = factory;
578 IDWriteFactory_AddRef(factory);
580 *ret = (IDWriteBitmapRenderTarget*)&target->IDWriteBitmapRenderTarget1_iface;
582 return S_OK;
585 static HRESULT WINAPI gdiinterop_QueryInterface(IDWriteGdiInterop1 *iface, REFIID riid, void **obj)
587 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
589 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
591 if (IsEqualIID(riid, &IID_IDWriteGdiInterop1) ||
592 IsEqualIID(riid, &IID_IDWriteGdiInterop) ||
593 IsEqualIID(riid, &IID_IUnknown))
595 *obj = iface;
596 IDWriteGdiInterop1_AddRef(iface);
597 return S_OK;
600 *obj = NULL;
601 return E_NOINTERFACE;
604 static ULONG WINAPI gdiinterop_AddRef(IDWriteGdiInterop1 *iface)
606 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
607 LONG ref = InterlockedIncrement(&This->ref);
608 TRACE("(%p)->(%d)\n", This, ref);
609 return ref;
612 static ULONG WINAPI gdiinterop_Release(IDWriteGdiInterop1 *iface)
614 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
615 LONG ref = InterlockedDecrement(&This->ref);
617 TRACE("(%p)->(%d)\n", This, ref);
619 if (!ref) {
620 factory_detach_gdiinterop(This->factory, iface);
621 heap_free(This);
624 return ref;
627 static HRESULT WINAPI gdiinterop_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
628 LOGFONTW const *logfont, IDWriteFont **font)
630 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
632 TRACE("(%p)->(%p %p)\n", This, logfont, font);
634 return IDWriteGdiInterop1_CreateFontFromLOGFONT(iface, logfont, NULL, font);
637 static HRESULT WINAPI gdiinterop_ConvertFontToLOGFONT(IDWriteGdiInterop1 *iface,
638 IDWriteFont *font, LOGFONTW *logfont, BOOL *is_systemfont)
640 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
641 IDWriteFontCollection *collection;
642 IDWriteFontFamily *family;
643 HRESULT hr;
645 TRACE("(%p)->(%p %p %p)\n", This, font, logfont, is_systemfont);
647 *is_systemfont = FALSE;
649 memset(logfont, 0, sizeof(*logfont));
651 if (!font)
652 return E_INVALIDARG;
654 hr = IDWriteFont_GetFontFamily(font, &family);
655 if (FAILED(hr))
656 return hr;
658 hr = IDWriteFontFamily_GetFontCollection(family, &collection);
659 IDWriteFontFamily_Release(family);
660 if (FAILED(hr))
661 return hr;
663 *is_systemfont = is_system_collection(collection);
664 IDWriteFontCollection_Release(collection);
666 get_logfont_from_font(font, logfont);
667 logfont->lfCharSet = DEFAULT_CHARSET;
668 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
670 return hr;
673 static HRESULT WINAPI gdiinterop_ConvertFontFaceToLOGFONT(IDWriteGdiInterop1 *iface,
674 IDWriteFontFace *fontface, LOGFONTW *logfont)
676 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
678 TRACE("(%p)->(%p %p)\n", This, fontface, logfont);
680 memset(logfont, 0, sizeof(*logfont));
682 if (!fontface)
683 return E_INVALIDARG;
685 get_logfont_from_fontface(fontface, logfont);
686 logfont->lfCharSet = DEFAULT_CHARSET;
687 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
689 return S_OK;
692 struct font_realization_info {
693 DWORD size;
694 DWORD flags;
695 DWORD cache_num;
696 DWORD instance_id;
697 DWORD unk;
698 WORD face_index;
699 WORD simulations;
702 struct font_fileinfo {
703 FILETIME writetime;
704 LARGE_INTEGER size;
705 WCHAR path[1];
708 /* Undocumented gdi32 exports, used to access actually selected font information */
709 extern BOOL WINAPI GetFontRealizationInfo(HDC hdc, struct font_realization_info *info);
710 extern BOOL WINAPI GetFontFileInfo(DWORD instance_id, DWORD unknown, struct font_fileinfo *info, DWORD size, DWORD *needed);
712 static HRESULT WINAPI gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop1 *iface,
713 HDC hdc, IDWriteFontFace **fontface)
715 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
716 struct font_realization_info info;
717 struct font_fileinfo *fileinfo;
718 DWRITE_FONT_FILE_TYPE filetype;
719 DWRITE_FONT_FACE_TYPE facetype;
720 IDWriteFontFile *file;
721 BOOL is_supported;
722 UINT32 facenum;
723 DWORD needed;
724 HRESULT hr;
726 TRACE("(%p)->(%p %p)\n", This, hdc, fontface);
728 *fontface = NULL;
730 if (!hdc)
731 return E_INVALIDARG;
733 /* get selected font id */
734 info.size = sizeof(info);
735 if (!GetFontRealizationInfo(hdc, &info)) {
736 WARN("failed to get selected font id\n");
737 return E_FAIL;
740 needed = 0;
741 GetFontFileInfo(info.instance_id, 0, NULL, 0, &needed);
742 if (needed == 0) {
743 WARN("failed to get font file info size\n");
744 return E_FAIL;
747 fileinfo = heap_alloc(needed);
748 if (!fileinfo)
749 return E_OUTOFMEMORY;
751 if (!GetFontFileInfo(info.instance_id, 0, fileinfo, needed, &needed)) {
752 heap_free(fileinfo);
753 return E_FAIL;
756 hr = IDWriteFactory4_CreateFontFileReference(This->factory, fileinfo->path, &fileinfo->writetime,
757 &file);
758 heap_free(fileinfo);
759 if (FAILED(hr))
760 return hr;
762 is_supported = FALSE;
763 hr = IDWriteFontFile_Analyze(file, &is_supported, &filetype, &facetype, &facenum);
764 if (SUCCEEDED(hr)) {
765 if (is_supported)
766 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
767 hr = IDWriteFactory4_CreateFontFace(This->factory, facetype, 1, &file, info.face_index,
768 info.simulations, fontface);
769 else
770 hr = DWRITE_E_FILEFORMAT;
773 IDWriteFontFile_Release(file);
774 return hr;
777 static HRESULT WINAPI gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop1 *iface,
778 HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
780 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
781 TRACE("(%p)->(%p %u %u %p)\n", This, hdc, width, height, target);
782 return create_rendertarget((IDWriteFactory*)This->factory, hdc, width, height, target);
785 static HRESULT WINAPI gdiinterop1_CreateFontFromLOGFONT(IDWriteGdiInterop1 *iface,
786 LOGFONTW const *logfont, IDWriteFontCollection *collection, IDWriteFont **font)
788 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
789 IDWriteFontFamily *family;
790 DWRITE_FONT_STYLE style;
791 BOOL exists = FALSE;
792 UINT32 index;
793 HRESULT hr;
795 TRACE("(%p)->(%p %p %p)\n", This, logfont, collection, font);
797 *font = NULL;
799 if (!logfont) return E_INVALIDARG;
801 if (collection)
802 IDWriteFontCollection_AddRef(collection);
803 else {
804 hr = IDWriteFactory4_GetSystemFontCollection(This->factory, FALSE, (IDWriteFontCollection1**)&collection, FALSE);
805 if (FAILED(hr)) {
806 ERR("failed to get system font collection: 0x%08x.\n", hr);
807 return hr;
811 hr = IDWriteFontCollection_FindFamilyName(collection, logfont->lfFaceName, &index, &exists);
812 if (FAILED(hr))
813 goto done;
815 if (!exists) {
816 hr = DWRITE_E_NOFONT;
817 goto done;
820 hr = IDWriteFontCollection_GetFontFamily(collection, index, &family);
821 if (FAILED(hr))
822 goto done;
824 style = logfont->lfItalic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
825 hr = IDWriteFontFamily_GetFirstMatchingFont(family, logfont->lfWeight, DWRITE_FONT_STRETCH_NORMAL, style, font);
826 IDWriteFontFamily_Release(family);
828 done:
829 IDWriteFontCollection_Release(collection);
830 return hr;
833 static HRESULT WINAPI gdiinterop1_GetFontSignature_(IDWriteGdiInterop1 *iface, IDWriteFontFace *fontface,
834 FONTSIGNATURE *fontsig)
836 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
837 struct file_stream_desc stream_desc;
838 IDWriteFontFileStream *stream;
839 IDWriteFontFile *file;
840 UINT32 count;
841 HRESULT hr;
843 TRACE("(%p)->(%p %p)\n", This, fontface, fontsig);
845 memset(fontsig, 0, sizeof(*fontsig));
847 count = 1;
848 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
849 hr = get_filestream_from_file(file, &stream);
850 IDWriteFontFile_Release(file);
851 if (FAILED(hr))
852 return hr;
854 stream_desc.stream = stream;
855 stream_desc.face_type = IDWriteFontFace_GetType(fontface);
856 stream_desc.face_index = IDWriteFontFace_GetIndex(fontface);
857 hr = opentype_get_font_signature(&stream_desc, fontsig);
858 IDWriteFontFileStream_Release(stream);
859 return hr;
862 static HRESULT WINAPI gdiinterop1_GetFontSignature(IDWriteGdiInterop1 *iface, IDWriteFont *font, FONTSIGNATURE *fontsig)
864 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
865 IDWriteFontFace *fontface;
866 HRESULT hr;
868 TRACE("(%p)->(%p %p)\n", This, font, fontsig);
870 if (!font)
871 return E_INVALIDARG;
873 memset(fontsig, 0, sizeof(*fontsig));
875 hr = IDWriteFont_CreateFontFace(font, &fontface);
876 if (FAILED(hr))
877 return hr;
879 hr = IDWriteGdiInterop1_GetFontSignature_(iface, fontface, fontsig);
880 IDWriteFontFace_Release(fontface);
881 return hr;
884 static HRESULT WINAPI gdiinterop1_GetMatchingFontsByLOGFONT(IDWriteGdiInterop1 *iface, LOGFONTW const *logfont,
885 IDWriteFontSet *fontset, IDWriteFontSet **subset)
887 struct gdiinterop *This = impl_from_IDWriteGdiInterop1(iface);
889 FIXME("(%p)->(%p %p %p): stub\n", This, logfont, fontset, subset);
891 return E_NOTIMPL;
894 static const struct IDWriteGdiInterop1Vtbl gdiinteropvtbl = {
895 gdiinterop_QueryInterface,
896 gdiinterop_AddRef,
897 gdiinterop_Release,
898 gdiinterop_CreateFontFromLOGFONT,
899 gdiinterop_ConvertFontToLOGFONT,
900 gdiinterop_ConvertFontFaceToLOGFONT,
901 gdiinterop_CreateFontFaceFromHdc,
902 gdiinterop_CreateBitmapRenderTarget,
903 gdiinterop1_CreateFontFromLOGFONT,
904 gdiinterop1_GetFontSignature_,
905 gdiinterop1_GetFontSignature,
906 gdiinterop1_GetMatchingFontsByLOGFONT
909 HRESULT create_gdiinterop(IDWriteFactory4 *factory, IDWriteGdiInterop1 **ret)
911 struct gdiinterop *interop;
913 *ret = NULL;
915 if (!(interop = heap_alloc(sizeof(*interop))))
916 return E_OUTOFMEMORY;
918 interop->IDWriteGdiInterop1_iface.lpVtbl = &gdiinteropvtbl;
919 interop->ref = 1;
920 IDWriteFactory4_AddRef(interop->factory = factory);
922 *ret = &interop->IDWriteGdiInterop1_iface;
923 return S_OK;