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
29 #include "dwrite_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dwrite
);
42 IDWriteBitmapRenderTarget1 IDWriteBitmapRenderTarget1_iface
;
43 ID2D1SimplifiedGeometrySink ID2D1SimplifiedGeometrySink_iface
;
46 IDWriteFactory
*factory
;
47 DWRITE_TEXT_ANTIALIAS_MODE antialiasmode
;
56 IDWriteGdiInterop1 IDWriteGdiInterop1_iface
;
58 IDWriteFactory5
*factory
;
61 static inline int get_dib_stride(int width
, int bpp
)
63 return ((width
* bpp
+ 31) >> 3) & ~3;
66 static HRESULT
create_target_dibsection(struct rendertarget
*target
, UINT32 width
, UINT32 height
)
68 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
[256])];
69 BITMAPINFO
*bmi
= (BITMAPINFO
*)bmibuf
;
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);
85 hbm
= CreateBitmap(1, 1, 1, 1, NULL
);
86 target
->dib
.ptr
= NULL
;
87 target
->dib
.stride
= 0;
88 target
->dib
.width
= 0;
91 target
->dib
.stride
= get_dib_stride(width
, 32);
92 target
->dib
.width
= width
;
95 DeleteObject(SelectObject(target
->hdc
, hbm
));
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
))
120 ID2D1SimplifiedGeometrySink_AddRef(iface
);
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
);
162 LineTo(This
->hdc
, points
->x
, points
->y
);
167 static void WINAPI
rendertarget_sink_AddBeziers(ID2D1SimplifiedGeometrySink
*iface
, const D2D1_BEZIER_SEGMENT
*beziers
, UINT32 count
)
169 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
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);
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
)
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
))
220 IDWriteBitmapRenderTarget1_AddRef(iface
);
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
);
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
);
246 IDWriteFactory_Release(This
->factory
);
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
;
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
,
313 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
314 IDWriteGlyphRunAnalysis
*analysis
;
315 DWRITE_RENDERING_MODE rendermode
;
316 DWRITE_TEXTURE_TYPE texturetype
;
317 IDWriteFontFace1
*fontface1
;
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
);
329 hr
= IDWriteFontFace_QueryInterface(run
->fontFace
, &IID_IDWriteFontFace1
, (void**)&fontface1
);
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
,
334 IDWriteFontFace1_Release(fontface1
);
337 hr
= IDWriteFontFace_GetRecommendedRenderingMode(run
->fontFace
, run
->fontEmSize
,
338 This
->ppdip
, measuring_mode
, params
, &rendermode
);
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
;
350 /* target allows any transform to be set, filter it here */
351 if (m
->m11
* m
->m22
== m
->m12
* m
->m21
) {
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
);
377 HBRUSH brush
= CreateSolidBrush(color
);
379 SelectObject(This
->hdc
, brush
);
383 /* FIXME: one way to get affected rectangle bounds is to use region fill */
390 SetWorldTransform(This
->hdc
, &identity
);
395 hr
= IDWriteFactory_CreateGlyphRunAnalysis(This
->factory
,
396 run
, This
->ppdip
, &This
->m
, rendermode
, measuring_mode
,
397 originX
, originY
, &analysis
);
399 WARN("failed to create analysis instance, 0x%08x\n", 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
);
409 WARN("GetAlphaTextureBounds() failed, 0x%08x\n", hr
);
410 IDWriteGlyphRunAnalysis_Release(analysis
);
413 texturetype
= DWRITE_TEXTURE_CLEARTYPE_3x1
;
416 if (IntersectRect(&target
, &target
, &bounds
)) {
417 UINT32 size
= (target
.right
- target
.left
) * (target
.bottom
- target
.top
);
420 color
= colorref_to_pixel_888(color
);
421 if (texturetype
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
423 bitmap
= heap_alloc_zero(size
);
425 IDWriteGlyphRunAnalysis_Release(analysis
);
426 return E_OUTOFMEMORY
;
429 hr
= IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis
, texturetype
, &target
, bitmap
, size
);
431 /* blit to target dib */
432 if (texturetype
== DWRITE_TEXTURE_ALIASED_1x1
)
433 blit_8(&This
->dib
, bitmap
, &target
, color
);
435 blit_subpixel_888(&This
->dib
, This
->size
.cx
, bitmap
, &target
, color
);
437 if (bbox_ret
) *bbox_ret
= target
;
443 IDWriteGlyphRunAnalysis_Release(analysis
);
448 static HDC WINAPI
rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget1
*iface
)
450 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
451 TRACE("(%p)\n", This
);
455 static FLOAT WINAPI
rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget1
*iface
)
457 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
458 TRACE("(%p)\n", This
);
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
);
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
;
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
;
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
);
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
)
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
)
532 This
->antialiasmode
= mode
;
536 static const IDWriteBitmapRenderTarget1Vtbl rendertargetvtbl
= {
537 rendertarget_QueryInterface
,
539 rendertarget_Release
,
540 rendertarget_DrawGlyphRun
,
541 rendertarget_GetMemoryDC
,
542 rendertarget_GetPixelsPerDip
,
543 rendertarget_SetPixelsPerDip
,
544 rendertarget_GetCurrentTransform
,
545 rendertarget_SetCurrentTransform
,
546 rendertarget_GetSize
,
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
;
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
;
566 target
->hdc
= CreateCompatibleDC(hdc
);
567 SetGraphicsMode(target
->hdc
, GM_ADVANCED
);
568 hr
= create_target_dibsection(target
, width
, height
);
570 IDWriteBitmapRenderTarget1_Release(&target
->IDWriteBitmapRenderTarget1_iface
);
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
;
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
))
596 IDWriteGdiInterop1_AddRef(iface
);
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
);
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
);
620 factory_detach_gdiinterop(This
->factory
, iface
);
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
;
645 TRACE("(%p)->(%p %p %p)\n", This
, font
, logfont
, is_systemfont
);
647 *is_systemfont
= FALSE
;
649 memset(logfont
, 0, sizeof(*logfont
));
654 hr
= IDWriteFont_GetFontFamily(font
, &family
);
658 hr
= IDWriteFontFamily_GetFontCollection(family
, &collection
);
659 IDWriteFontFamily_Release(family
);
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
;
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
));
685 get_logfont_from_fontface(fontface
, logfont
);
686 logfont
->lfCharSet
= DEFAULT_CHARSET
;
687 logfont
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
692 struct font_realization_info
{
702 struct font_fileinfo
{
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
;
726 TRACE("(%p)->(%p %p)\n", This
, hdc
, fontface
);
733 /* get selected font id */
734 info
.size
= sizeof(info
);
735 if (!GetFontRealizationInfo(hdc
, &info
)) {
736 WARN("failed to get selected font id\n");
741 GetFontFileInfo(info
.instance_id
, 0, NULL
, 0, &needed
);
743 WARN("failed to get font file info size\n");
747 fileinfo
= heap_alloc(needed
);
749 return E_OUTOFMEMORY
;
751 if (!GetFontFileInfo(info
.instance_id
, 0, fileinfo
, needed
, &needed
)) {
756 hr
= IDWriteFactory5_CreateFontFileReference(This
->factory
, fileinfo
->path
, &fileinfo
->writetime
,
762 is_supported
= FALSE
;
763 hr
= IDWriteFontFile_Analyze(file
, &is_supported
, &filetype
, &facetype
, &facenum
);
766 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
767 hr
= IDWriteFactory5_CreateFontFace(This
->factory
, facetype
, 1, &file
, info
.face_index
,
768 info
.simulations
, fontface
);
770 hr
= DWRITE_E_FILEFORMAT
;
773 IDWriteFontFile_Release(file
);
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
;
795 TRACE("(%p)->(%p %p %p)\n", This
, logfont
, collection
, font
);
799 if (!logfont
) return E_INVALIDARG
;
802 IDWriteFontCollection_AddRef(collection
);
804 hr
= IDWriteFactory5_GetSystemFontCollection(This
->factory
, FALSE
, (IDWriteFontCollection1
**)&collection
, FALSE
);
806 ERR("failed to get system font collection: 0x%08x.\n", hr
);
811 hr
= IDWriteFontCollection_FindFamilyName(collection
, logfont
->lfFaceName
, &index
, &exists
);
816 hr
= DWRITE_E_NOFONT
;
820 hr
= IDWriteFontCollection_GetFontFamily(collection
, index
, &family
);
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
);
829 IDWriteFontCollection_Release(collection
);
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
;
843 TRACE("(%p)->(%p %p)\n", This
, fontface
, fontsig
);
845 memset(fontsig
, 0, sizeof(*fontsig
));
848 hr
= IDWriteFontFace_GetFiles(fontface
, &count
, &file
);
849 hr
= get_filestream_from_file(file
, &stream
);
850 IDWriteFontFile_Release(file
);
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
);
862 static HRESULT WINAPI
gdiinterop1_GetFontSignature(IDWriteGdiInterop1
*iface
, IDWriteFont
*font
, FONTSIGNATURE
*fontsig
)
864 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
865 IDWriteFontFace
*fontface
;
868 TRACE("(%p)->(%p %p)\n", This
, font
, fontsig
);
873 memset(fontsig
, 0, sizeof(*fontsig
));
875 hr
= IDWriteFont_CreateFontFace(font
, &fontface
);
879 hr
= IDWriteGdiInterop1_GetFontSignature_(iface
, fontface
, fontsig
);
880 IDWriteFontFace_Release(fontface
);
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
);
894 static const struct IDWriteGdiInterop1Vtbl gdiinteropvtbl
= {
895 gdiinterop_QueryInterface
,
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(IDWriteFactory5
*factory
, IDWriteGdiInterop1
**ret
)
911 struct gdiinterop
*interop
;
915 if (!(interop
= heap_alloc(sizeof(*interop
))))
916 return E_OUTOFMEMORY
;
918 interop
->IDWriteGdiInterop1_iface
.lpVtbl
= &gdiinteropvtbl
;
920 IDWriteFactory5_AddRef(interop
->factory
= factory
);
922 *ret
= &interop
->IDWriteGdiInterop1_iface
;