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 IDWriteFactory5
*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
);
124 WARN("%s not implemented.\n", debugstr_guid(riid
));
128 return E_NOINTERFACE
;
131 static ULONG WINAPI
rendertarget_sink_AddRef(ID2D1SimplifiedGeometrySink
*iface
)
133 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
134 return IDWriteBitmapRenderTarget1_AddRef(&This
->IDWriteBitmapRenderTarget1_iface
);
137 static ULONG WINAPI
rendertarget_sink_Release(ID2D1SimplifiedGeometrySink
*iface
)
139 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
140 return IDWriteBitmapRenderTarget1_Release(&This
->IDWriteBitmapRenderTarget1_iface
);
143 static void WINAPI
rendertarget_sink_SetFillMode(ID2D1SimplifiedGeometrySink
*iface
, D2D1_FILL_MODE mode
)
145 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
146 SetPolyFillMode(This
->hdc
, mode
== D2D1_FILL_MODE_ALTERNATE
? ALTERNATE
: WINDING
);
149 static void WINAPI
rendertarget_sink_SetSegmentFlags(ID2D1SimplifiedGeometrySink
*iface
, D2D1_PATH_SEGMENT vertexFlags
)
153 static void WINAPI
rendertarget_sink_BeginFigure(ID2D1SimplifiedGeometrySink
*iface
, D2D1_POINT_2F startPoint
, D2D1_FIGURE_BEGIN figureBegin
)
155 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
156 MoveToEx(This
->hdc
, startPoint
.x
, startPoint
.y
, NULL
);
159 static void WINAPI
rendertarget_sink_AddLines(ID2D1SimplifiedGeometrySink
*iface
, const D2D1_POINT_2F
*points
, UINT32 count
)
161 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
164 LineTo(This
->hdc
, points
->x
, points
->y
);
169 static void WINAPI
rendertarget_sink_AddBeziers(ID2D1SimplifiedGeometrySink
*iface
, const D2D1_BEZIER_SEGMENT
*beziers
, UINT32 count
)
171 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
175 points
[0].x
= beziers
->point1
.x
;
176 points
[0].y
= beziers
->point1
.y
;
177 points
[1].x
= beziers
->point2
.x
;
178 points
[1].y
= beziers
->point2
.y
;
179 points
[2].x
= beziers
->point3
.x
;
180 points
[2].y
= beziers
->point3
.y
;
182 PolyBezierTo(This
->hdc
, points
, 3);
187 static void WINAPI
rendertarget_sink_EndFigure(ID2D1SimplifiedGeometrySink
*iface
, D2D1_FIGURE_END figureEnd
)
189 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
190 CloseFigure(This
->hdc
);
193 static HRESULT WINAPI
rendertarget_sink_Close(ID2D1SimplifiedGeometrySink
*iface
)
198 static const ID2D1SimplifiedGeometrySinkVtbl rendertargetsinkvtbl
= {
199 rendertarget_sink_QueryInterface
,
200 rendertarget_sink_AddRef
,
201 rendertarget_sink_Release
,
202 rendertarget_sink_SetFillMode
,
203 rendertarget_sink_SetSegmentFlags
,
204 rendertarget_sink_BeginFigure
,
205 rendertarget_sink_AddLines
,
206 rendertarget_sink_AddBeziers
,
207 rendertarget_sink_EndFigure
,
208 rendertarget_sink_Close
211 static HRESULT WINAPI
rendertarget_QueryInterface(IDWriteBitmapRenderTarget1
*iface
, REFIID riid
, void **obj
)
213 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
215 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), obj
);
217 if (IsEqualIID(riid
, &IID_IDWriteBitmapRenderTarget1
) ||
218 IsEqualIID(riid
, &IID_IDWriteBitmapRenderTarget
) ||
219 IsEqualIID(riid
, &IID_IUnknown
))
222 IDWriteBitmapRenderTarget1_AddRef(iface
);
226 WARN("%s not implemented.\n", debugstr_guid(riid
));
230 return E_NOINTERFACE
;
233 static ULONG WINAPI
rendertarget_AddRef(IDWriteBitmapRenderTarget1
*iface
)
235 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
236 ULONG ref
= InterlockedIncrement(&This
->ref
);
237 TRACE("(%p)->(%d)\n", This
, ref
);
241 static ULONG WINAPI
rendertarget_Release(IDWriteBitmapRenderTarget1
*iface
)
243 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
244 ULONG ref
= InterlockedDecrement(&This
->ref
);
246 TRACE("(%p)->(%d)\n", This
, ref
);
250 IDWriteFactory5_Release(This
->factory
);
258 static inline DWORD
*get_pixel_ptr_32(struct dib_data
*dib
, int x
, int y
)
260 return (DWORD
*)((BYTE
*)dib
->ptr
+ y
* dib
->stride
+ x
* 4);
263 static inline BYTE
blend_color(BYTE dst
, BYTE src
, BYTE alpha
)
265 return (src
* alpha
+ dst
* (255 - alpha
) + 127) / 255;
268 static inline DWORD
blend_subpixel(BYTE r
, BYTE g
, BYTE b
, DWORD text
, const BYTE
*alpha
)
270 return blend_color(r
, text
>> 16, alpha
[0]) << 16 |
271 blend_color(g
, text
>> 8, alpha
[1]) << 8 |
272 blend_color(b
, text
, alpha
[2]);
275 static inline DWORD
blend_pixel(BYTE r
, BYTE g
, BYTE b
, DWORD text
, BYTE alpha
)
277 return blend_color(r
, text
>> 16, alpha
) << 16 |
278 blend_color(g
, text
>> 8, alpha
) << 8 |
279 blend_color(b
, text
, alpha
);
282 static void blit_8(struct dib_data
*dib
, const BYTE
*src
, const RECT
*rect
, DWORD text_pixel
)
284 DWORD
*dst_ptr
= get_pixel_ptr_32(dib
, rect
->left
, rect
->top
);
285 int x
, y
, src_width
= rect
->right
- rect
->left
;
287 for (y
= rect
->top
; y
< rect
->bottom
; y
++) {
288 for (x
= 0; x
< src_width
; x
++) {
289 if (!src
[x
]) continue;
290 if (src
[x
] == DWRITE_ALPHA_MAX
)
291 dst_ptr
[x
] = text_pixel
;
293 dst_ptr
[x
] = blend_pixel(dst_ptr
[x
] >> 16, dst_ptr
[x
] >> 8, dst_ptr
[x
], text_pixel
, src
[x
]);
297 dst_ptr
+= dib
->stride
/ 4;
301 static void blit_subpixel_888(struct dib_data
*dib
, int dib_width
, const BYTE
*src
,
302 const RECT
*rect
, DWORD text_pixel
)
304 DWORD
*dst_ptr
= get_pixel_ptr_32(dib
, rect
->left
, rect
->top
);
305 int x
, y
, src_width
= rect
->right
- rect
->left
;
307 for (y
= rect
->top
; y
< rect
->bottom
; y
++) {
308 for (x
= 0; x
< src_width
; x
++) {
309 if (src
[3*x
] == 0 && src
[3*x
+1] == 0 && src
[3*x
+2] == 0) continue;
310 dst_ptr
[x
] = blend_subpixel(dst_ptr
[x
] >> 16, dst_ptr
[x
] >> 8, dst_ptr
[x
], text_pixel
, &src
[3*x
]);
312 dst_ptr
+= dib
->stride
/ 4;
313 src
+= src_width
* 3;
317 static inline DWORD
colorref_to_pixel_888(COLORREF color
)
319 return (((color
>> 16) & 0xff) | (color
& 0xff00) | ((color
<< 16) & 0xff0000));
322 static HRESULT WINAPI
rendertarget_DrawGlyphRun(IDWriteBitmapRenderTarget1
*iface
,
323 FLOAT originX
, FLOAT originY
, DWRITE_MEASURING_MODE measuring_mode
,
324 DWRITE_GLYPH_RUN
const *run
, IDWriteRenderingParams
*params
, COLORREF color
,
327 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
328 IDWriteGlyphRunAnalysis
*analysis
;
329 DWRITE_RENDERING_MODE1 rendermode
;
330 DWRITE_GRID_FIT_MODE gridfitmode
;
331 DWRITE_TEXTURE_TYPE texturetype
;
332 DWRITE_GLYPH_RUN scaled_run
;
333 IDWriteFontFace3
*fontface
;
337 TRACE("(%p)->(%.2f %.2f %d %p %p 0x%08x %p)\n", This
, originX
, originY
,
338 measuring_mode
, run
, params
, color
, bbox_ret
);
340 SetRectEmpty(bbox_ret
);
348 if (FAILED(hr
= IDWriteFontFace_QueryInterface(run
->fontFace
, &IID_IDWriteFontFace3
, (void **)&fontface
))) {
349 WARN("Failed to get IDWriteFontFace2 interface, hr %#x.\n", hr
);
353 hr
= IDWriteFontFace3_GetRecommendedRenderingMode(fontface
, run
->fontEmSize
, This
->ppdip
* 96.0f
,
354 This
->ppdip
* 96.0f
, NULL
/* FIXME */, run
->isSideways
, DWRITE_OUTLINE_THRESHOLD_ALIASED
, measuring_mode
,
355 params
, &rendermode
, &gridfitmode
);
356 IDWriteFontFace3_Release(fontface
);
360 SetRect(&target
, 0, 0, This
->size
.cx
, This
->size
.cy
);
362 if (rendermode
== DWRITE_RENDERING_MODE1_OUTLINE
) {
363 static const XFORM identity
= { 1.0f
, 0.0f
, 0.0f
, 1.0f
, 0.0f
, 0.0f
};
364 const DWRITE_MATRIX
*m
= &This
->m
;
367 /* target allows any transform to be set, filter it here */
368 if (m
->m11
* m
->m22
== m
->m12
* m
->m21
) {
380 xform
.eDx
= m
->m11
* originX
+ m
->m21
* originY
+ m
->dx
;
381 xform
.eDy
= m
->m12
* originX
+ m
->m22
* originY
+ m
->dy
;
383 SetWorldTransform(This
->hdc
, &xform
);
385 BeginPath(This
->hdc
);
387 hr
= IDWriteFontFace_GetGlyphRunOutline(run
->fontFace
, run
->fontEmSize
* This
->ppdip
,
388 run
->glyphIndices
, run
->glyphAdvances
, run
->glyphOffsets
, run
->glyphCount
,
389 run
->isSideways
, run
->bidiLevel
& 1, &This
->ID2D1SimplifiedGeometrySink_iface
);
394 HBRUSH brush
= CreateSolidBrush(color
);
396 SelectObject(This
->hdc
, brush
);
400 /* FIXME: one way to get affected rectangle bounds is to use region fill */
407 SetWorldTransform(This
->hdc
, &identity
);
413 scaled_run
.fontEmSize
*= This
->ppdip
;
414 hr
= IDWriteFactory5_CreateGlyphRunAnalysis(This
->factory
, &scaled_run
, &This
->m
, rendermode
, measuring_mode
,
415 gridfitmode
, This
->antialiasmode
, originX
, originY
, &analysis
);
417 WARN("failed to create analysis instance, 0x%08x\n", hr
);
421 SetRectEmpty(&bounds
);
422 texturetype
= DWRITE_TEXTURE_ALIASED_1x1
;
423 hr
= IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis
, DWRITE_TEXTURE_ALIASED_1x1
, &bounds
);
424 if (FAILED(hr
) || IsRectEmpty(&bounds
)) {
425 hr
= IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis
, DWRITE_TEXTURE_CLEARTYPE_3x1
, &bounds
);
427 WARN("GetAlphaTextureBounds() failed, 0x%08x\n", hr
);
428 IDWriteGlyphRunAnalysis_Release(analysis
);
431 texturetype
= DWRITE_TEXTURE_CLEARTYPE_3x1
;
434 if (IntersectRect(&target
, &target
, &bounds
)) {
435 UINT32 size
= (target
.right
- target
.left
) * (target
.bottom
- target
.top
);
438 color
= colorref_to_pixel_888(color
);
439 if (texturetype
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
441 bitmap
= heap_alloc_zero(size
);
443 IDWriteGlyphRunAnalysis_Release(analysis
);
444 return E_OUTOFMEMORY
;
447 hr
= IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis
, texturetype
, &target
, bitmap
, size
);
449 /* blit to target dib */
450 if (texturetype
== DWRITE_TEXTURE_ALIASED_1x1
)
451 blit_8(&This
->dib
, bitmap
, &target
, color
);
453 blit_subpixel_888(&This
->dib
, This
->size
.cx
, bitmap
, &target
, color
);
455 if (bbox_ret
) *bbox_ret
= target
;
461 IDWriteGlyphRunAnalysis_Release(analysis
);
466 static HDC WINAPI
rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget1
*iface
)
468 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
469 TRACE("(%p)\n", This
);
473 static FLOAT WINAPI
rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget1
*iface
)
475 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
476 TRACE("(%p)\n", This
);
480 static HRESULT WINAPI
rendertarget_SetPixelsPerDip(IDWriteBitmapRenderTarget1
*iface
, FLOAT ppdip
)
482 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
484 TRACE("(%p)->(%.2f)\n", This
, ppdip
);
493 static HRESULT WINAPI
rendertarget_GetCurrentTransform(IDWriteBitmapRenderTarget1
*iface
, DWRITE_MATRIX
*transform
)
495 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
497 TRACE("(%p)->(%p)\n", This
, transform
);
499 *transform
= This
->m
;
503 static HRESULT WINAPI
rendertarget_SetCurrentTransform(IDWriteBitmapRenderTarget1
*iface
, DWRITE_MATRIX
const *transform
)
505 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
507 TRACE("(%p)->(%p)\n", This
, transform
);
509 This
->m
= transform
? *transform
: identity
;
513 static HRESULT WINAPI
rendertarget_GetSize(IDWriteBitmapRenderTarget1
*iface
, SIZE
*size
)
515 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
517 TRACE("(%p)->(%p)\n", This
, size
);
522 static HRESULT WINAPI
rendertarget_Resize(IDWriteBitmapRenderTarget1
*iface
, UINT32 width
, UINT32 height
)
524 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
526 TRACE("(%p)->(%u %u)\n", This
, width
, height
);
528 if (This
->size
.cx
== width
&& This
->size
.cy
== height
)
531 return create_target_dibsection(This
, width
, height
);
534 static DWRITE_TEXT_ANTIALIAS_MODE WINAPI
rendertarget_GetTextAntialiasMode(IDWriteBitmapRenderTarget1
*iface
)
536 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
537 TRACE("(%p)\n", This
);
538 return This
->antialiasmode
;
541 static HRESULT WINAPI
rendertarget_SetTextAntialiasMode(IDWriteBitmapRenderTarget1
*iface
, DWRITE_TEXT_ANTIALIAS_MODE mode
)
543 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
545 TRACE("(%p)->(%d)\n", This
, mode
);
547 if ((DWORD
)mode
> DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE
)
550 This
->antialiasmode
= mode
;
554 static const IDWriteBitmapRenderTarget1Vtbl rendertargetvtbl
= {
555 rendertarget_QueryInterface
,
557 rendertarget_Release
,
558 rendertarget_DrawGlyphRun
,
559 rendertarget_GetMemoryDC
,
560 rendertarget_GetPixelsPerDip
,
561 rendertarget_SetPixelsPerDip
,
562 rendertarget_GetCurrentTransform
,
563 rendertarget_SetCurrentTransform
,
564 rendertarget_GetSize
,
566 rendertarget_GetTextAntialiasMode
,
567 rendertarget_SetTextAntialiasMode
570 static HRESULT
create_rendertarget(IDWriteFactory5
*factory
, HDC hdc
, UINT32 width
, UINT32 height
, IDWriteBitmapRenderTarget
**ret
)
572 struct rendertarget
*target
;
577 target
= heap_alloc(sizeof(struct rendertarget
));
578 if (!target
) return E_OUTOFMEMORY
;
580 target
->IDWriteBitmapRenderTarget1_iface
.lpVtbl
= &rendertargetvtbl
;
581 target
->ID2D1SimplifiedGeometrySink_iface
.lpVtbl
= &rendertargetsinkvtbl
;
584 target
->hdc
= CreateCompatibleDC(hdc
);
585 SetGraphicsMode(target
->hdc
, GM_ADVANCED
);
586 hr
= create_target_dibsection(target
, width
, height
);
588 IDWriteBitmapRenderTarget1_Release(&target
->IDWriteBitmapRenderTarget1_iface
);
592 target
->m
= identity
;
593 target
->ppdip
= GetDeviceCaps(target
->hdc
, LOGPIXELSX
) / 96.0f
;
594 target
->antialiasmode
= DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE
;
595 target
->factory
= factory
;
596 IDWriteFactory5_AddRef(factory
);
598 *ret
= (IDWriteBitmapRenderTarget
*)&target
->IDWriteBitmapRenderTarget1_iface
;
603 static HRESULT WINAPI
gdiinterop_QueryInterface(IDWriteGdiInterop1
*iface
, REFIID riid
, void **obj
)
605 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
607 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), obj
);
609 if (IsEqualIID(riid
, &IID_IDWriteGdiInterop1
) ||
610 IsEqualIID(riid
, &IID_IDWriteGdiInterop
) ||
611 IsEqualIID(riid
, &IID_IUnknown
))
614 IDWriteGdiInterop1_AddRef(iface
);
618 WARN("%s not implemented.\n", debugstr_guid(riid
));
621 return E_NOINTERFACE
;
624 static ULONG WINAPI
gdiinterop_AddRef(IDWriteGdiInterop1
*iface
)
626 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
627 LONG ref
= InterlockedIncrement(&This
->ref
);
628 TRACE("(%p)->(%d)\n", This
, ref
);
632 static ULONG WINAPI
gdiinterop_Release(IDWriteGdiInterop1
*iface
)
634 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
635 LONG ref
= InterlockedDecrement(&This
->ref
);
637 TRACE("(%p)->(%d)\n", This
, ref
);
640 factory_detach_gdiinterop(This
->factory
, iface
);
647 static HRESULT WINAPI
gdiinterop_CreateFontFromLOGFONT(IDWriteGdiInterop1
*iface
,
648 LOGFONTW
const *logfont
, IDWriteFont
**font
)
650 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
652 TRACE("(%p)->(%p %p)\n", This
, logfont
, font
);
654 return IDWriteGdiInterop1_CreateFontFromLOGFONT(iface
, logfont
, NULL
, font
);
657 static HRESULT WINAPI
gdiinterop_ConvertFontToLOGFONT(IDWriteGdiInterop1
*iface
,
658 IDWriteFont
*font
, LOGFONTW
*logfont
, BOOL
*is_systemfont
)
660 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
661 IDWriteFontCollection
*collection
;
662 IDWriteFontFamily
*family
;
665 TRACE("(%p)->(%p %p %p)\n", This
, font
, logfont
, is_systemfont
);
667 *is_systemfont
= FALSE
;
669 memset(logfont
, 0, sizeof(*logfont
));
674 hr
= IDWriteFont_GetFontFamily(font
, &family
);
678 hr
= IDWriteFontFamily_GetFontCollection(family
, &collection
);
679 IDWriteFontFamily_Release(family
);
683 *is_systemfont
= is_system_collection(collection
);
684 IDWriteFontCollection_Release(collection
);
686 get_logfont_from_font(font
, logfont
);
687 logfont
->lfCharSet
= DEFAULT_CHARSET
;
688 logfont
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
693 static HRESULT WINAPI
gdiinterop_ConvertFontFaceToLOGFONT(IDWriteGdiInterop1
*iface
,
694 IDWriteFontFace
*fontface
, LOGFONTW
*logfont
)
696 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
698 TRACE("(%p)->(%p %p)\n", This
, fontface
, logfont
);
700 memset(logfont
, 0, sizeof(*logfont
));
705 get_logfont_from_fontface(fontface
, logfont
);
706 logfont
->lfCharSet
= DEFAULT_CHARSET
;
707 logfont
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
712 struct font_realization_info
{
722 struct font_fileinfo
{
728 /* Undocumented gdi32 exports, used to access actually selected font information */
729 extern BOOL WINAPI
GetFontRealizationInfo(HDC hdc
, struct font_realization_info
*info
);
730 extern BOOL WINAPI
GetFontFileInfo(DWORD instance_id
, DWORD unknown
, struct font_fileinfo
*info
, DWORD size
, DWORD
*needed
);
732 static HRESULT WINAPI
gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop1
*iface
,
733 HDC hdc
, IDWriteFontFace
**fontface
)
735 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
736 struct font_realization_info info
;
737 struct font_fileinfo
*fileinfo
;
738 DWRITE_FONT_FILE_TYPE filetype
;
739 DWRITE_FONT_FACE_TYPE facetype
;
740 IDWriteFontFile
*file
;
746 TRACE("(%p)->(%p %p)\n", This
, hdc
, fontface
);
753 /* get selected font id */
754 info
.size
= sizeof(info
);
755 if (!GetFontRealizationInfo(hdc
, &info
)) {
756 WARN("failed to get selected font id\n");
761 GetFontFileInfo(info
.instance_id
, 0, NULL
, 0, &needed
);
763 WARN("failed to get font file info size\n");
767 fileinfo
= heap_alloc(needed
);
769 return E_OUTOFMEMORY
;
771 if (!GetFontFileInfo(info
.instance_id
, 0, fileinfo
, needed
, &needed
)) {
776 hr
= IDWriteFactory5_CreateFontFileReference(This
->factory
, fileinfo
->path
, &fileinfo
->writetime
,
782 is_supported
= FALSE
;
783 hr
= IDWriteFontFile_Analyze(file
, &is_supported
, &filetype
, &facetype
, &facenum
);
786 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
787 hr
= IDWriteFactory5_CreateFontFace(This
->factory
, facetype
, 1, &file
, info
.face_index
,
788 info
.simulations
, fontface
);
790 hr
= DWRITE_E_FILEFORMAT
;
793 IDWriteFontFile_Release(file
);
797 static HRESULT WINAPI
gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop1
*iface
,
798 HDC hdc
, UINT32 width
, UINT32 height
, IDWriteBitmapRenderTarget
**target
)
800 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
801 TRACE("(%p)->(%p %u %u %p)\n", This
, hdc
, width
, height
, target
);
802 return create_rendertarget(This
->factory
, hdc
, width
, height
, target
);
805 static HRESULT WINAPI
gdiinterop1_CreateFontFromLOGFONT(IDWriteGdiInterop1
*iface
,
806 LOGFONTW
const *logfont
, IDWriteFontCollection
*collection
, IDWriteFont
**font
)
808 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
809 IDWriteFontFamily
*family
;
810 DWRITE_FONT_STYLE style
;
815 TRACE("(%p)->(%p %p %p)\n", This
, logfont
, collection
, font
);
819 if (!logfont
) return E_INVALIDARG
;
822 IDWriteFontCollection_AddRef(collection
);
824 hr
= IDWriteFactory5_GetSystemFontCollection(This
->factory
, FALSE
, (IDWriteFontCollection1
**)&collection
, FALSE
);
826 ERR("failed to get system font collection: 0x%08x.\n", hr
);
831 hr
= IDWriteFontCollection_FindFamilyName(collection
, logfont
->lfFaceName
, &index
, &exists
);
836 hr
= DWRITE_E_NOFONT
;
840 hr
= IDWriteFontCollection_GetFontFamily(collection
, index
, &family
);
844 style
= logfont
->lfItalic
? DWRITE_FONT_STYLE_ITALIC
: DWRITE_FONT_STYLE_NORMAL
;
845 hr
= IDWriteFontFamily_GetFirstMatchingFont(family
, logfont
->lfWeight
, DWRITE_FONT_STRETCH_NORMAL
, style
, font
);
846 IDWriteFontFamily_Release(family
);
849 IDWriteFontCollection_Release(collection
);
853 static HRESULT WINAPI
gdiinterop1_GetFontSignature_(IDWriteGdiInterop1
*iface
, IDWriteFontFace
*fontface
,
854 FONTSIGNATURE
*fontsig
)
856 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
858 TRACE("(%p)->(%p %p)\n", This
, fontface
, fontsig
);
860 return get_fontsig_from_fontface(fontface
, fontsig
);
863 static HRESULT WINAPI
gdiinterop1_GetFontSignature(IDWriteGdiInterop1
*iface
, IDWriteFont
*font
, FONTSIGNATURE
*fontsig
)
865 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
867 TRACE("(%p)->(%p %p)\n", This
, font
, fontsig
);
872 return get_fontsig_from_font(font
, fontsig
);
875 static HRESULT WINAPI
gdiinterop1_GetMatchingFontsByLOGFONT(IDWriteGdiInterop1
*iface
, LOGFONTW
const *logfont
,
876 IDWriteFontSet
*fontset
, IDWriteFontSet
**subset
)
878 struct gdiinterop
*This
= impl_from_IDWriteGdiInterop1(iface
);
880 FIXME("(%p)->(%p %p %p): stub\n", This
, logfont
, fontset
, subset
);
885 static const struct IDWriteGdiInterop1Vtbl gdiinteropvtbl
= {
886 gdiinterop_QueryInterface
,
889 gdiinterop_CreateFontFromLOGFONT
,
890 gdiinterop_ConvertFontToLOGFONT
,
891 gdiinterop_ConvertFontFaceToLOGFONT
,
892 gdiinterop_CreateFontFaceFromHdc
,
893 gdiinterop_CreateBitmapRenderTarget
,
894 gdiinterop1_CreateFontFromLOGFONT
,
895 gdiinterop1_GetFontSignature_
,
896 gdiinterop1_GetFontSignature
,
897 gdiinterop1_GetMatchingFontsByLOGFONT
900 HRESULT
create_gdiinterop(IDWriteFactory5
*factory
, IDWriteGdiInterop1
**ret
)
902 struct gdiinterop
*interop
;
906 if (!(interop
= heap_alloc(sizeof(*interop
))))
907 return E_OUTOFMEMORY
;
909 interop
->IDWriteGdiInterop1_iface
.lpVtbl
= &gdiinteropvtbl
;
911 IDWriteFactory5_AddRef(interop
->factory
= factory
);
913 *ret
= &interop
->IDWriteGdiInterop1_iface
;