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
29 #include "dwrite_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dwrite
);
43 IDWriteBitmapRenderTarget1 IDWriteBitmapRenderTarget1_iface
;
44 ID2D1SimplifiedGeometrySink ID2D1SimplifiedGeometrySink_iface
;
47 IDWriteFactory7
*factory
;
48 DWRITE_TEXT_ANTIALIAS_MODE antialiasmode
;
58 IDWriteGdiInterop1 IDWriteGdiInterop1_iface
;
59 IDWriteFontFileLoader IDWriteFontFileLoader_iface
;
61 IDWriteFactory7
*factory
;
64 struct memresource_stream
{
65 IDWriteFontFileStream IDWriteFontFileStream_iface
;
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
;
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);
94 hbm
= CreateBitmap(1, 1, 1, 1, NULL
);
95 target
->dib
.ptr
= NULL
;
96 target
->dib
.stride
= 0;
97 target
->dib
.width
= 0;
100 target
->dib
.stride
= get_dib_stride(width
, 32);
101 target
->dib
.width
= width
;
104 DeleteObject(SelectObject(target
->hdc
, hbm
));
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
))
139 ID2D1SimplifiedGeometrySink_AddRef(iface
);
143 WARN("%s not implemented.\n", debugstr_guid(riid
));
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
);
183 LineTo(This
->hdc
, points
->x
, points
->y
);
188 static void WINAPI
rendertarget_sink_AddBeziers(ID2D1SimplifiedGeometrySink
*iface
, const D2D1_BEZIER_SEGMENT
*beziers
, UINT32 count
)
190 struct rendertarget
*This
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
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);
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
)
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
))
241 IDWriteBitmapRenderTarget1_AddRef(iface
);
245 WARN("%s not implemented.\n", debugstr_guid(riid
));
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
);
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
);
271 IDWriteFactory7_Release(target
->factory
);
272 DeleteDC(target
->hdc
);
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
;
314 dst_ptr
[x
] = blend_pixel(dst_ptr
[x
] >> 16, dst_ptr
[x
] >> 8, dst_ptr
[x
], text_pixel
, src
[x
]);
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
,
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
;
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
)
369 if (FAILED(hr
= IDWriteFontFace_QueryInterface(run
->fontFace
, &IID_IDWriteFontFace3
, (void **)&fontface
))) {
370 WARN("Failed to get IDWriteFontFace2 interface, hr %#x.\n", 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
);
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
;
389 /* target allows any transform to be set, filter it here */
390 if (m
->m11
* m
->m22
== m
->m12
* m
->m21
) {
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
);
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 */
425 *bbox_ret
= target_rect
;
430 SetWorldTransform(target
->hdc
, &identity
);
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
);
441 WARN("failed to create analysis instance, 0x%08x\n", 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
);
451 WARN("GetAlphaTextureBounds() failed, 0x%08x\n", hr
);
452 IDWriteGlyphRunAnalysis_Release(analysis
);
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
);
463 color
= colorref_to_pixel_888(color
);
464 if (texturetype
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
466 bitmap
= heap_alloc_zero(size
);
468 IDWriteGlyphRunAnalysis_Release(analysis
);
469 return E_OUTOFMEMORY
;
472 hr
= IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis
, texturetype
, &target_rect
, bitmap
, size
);
474 /* blit to target dib */
475 if (texturetype
== DWRITE_TEXTURE_ALIASED_1x1
)
476 blit_8(&target
->dib
, bitmap
, &target_rect
, color
);
478 blit_subpixel_888(&target
->dib
, target
->size
.cx
, bitmap
, &target_rect
, color
);
480 if (bbox_ret
) *bbox_ret
= target_rect
;
486 IDWriteGlyphRunAnalysis_Release(analysis
);
491 static HDC WINAPI
rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget1
*iface
)
493 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
494 TRACE("(%p)\n", This
);
498 static FLOAT WINAPI
rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget1
*iface
)
500 struct rendertarget
*This
= impl_from_IDWriteBitmapRenderTarget1(iface
);
501 TRACE("(%p)\n", This
);
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
);
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
;
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
;
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
);
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
)
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
)
575 This
->antialiasmode
= mode
;
579 static const IDWriteBitmapRenderTarget1Vtbl rendertargetvtbl
= {
580 rendertarget_QueryInterface
,
582 rendertarget_Release
,
583 rendertarget_DrawGlyphRun
,
584 rendertarget_GetMemoryDC
,
585 rendertarget_GetPixelsPerDip
,
586 rendertarget_SetPixelsPerDip
,
587 rendertarget_GetCurrentTransform
,
588 rendertarget_SetCurrentTransform
,
589 rendertarget_GetSize
,
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
;
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
);
613 IDWriteBitmapRenderTarget1_Release(&target
->IDWriteBitmapRenderTarget1_iface
);
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
;
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
))
639 IDWriteGdiInterop1_AddRef(iface
);
643 WARN("%s not implemented.\n", debugstr_guid(riid
));
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
);
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
);
668 IDWriteFactory7_UnregisterFontFileLoader(interop
->factory
, &interop
->IDWriteFontFileLoader_iface
);
669 factory_detach_gdiinterop(interop
->factory
, iface
);
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
;
694 TRACE("(%p)->(%p %p %p)\n", This
, font
, logfont
, is_systemfont
);
696 *is_systemfont
= FALSE
;
698 memset(logfont
, 0, sizeof(*logfont
));
703 hr
= IDWriteFont_GetFontFamily(font
, &family
);
707 hr
= IDWriteFontFamily_GetFontCollection(family
, &collection
);
708 IDWriteFontFamily_Release(family
);
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
;
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
));
734 get_logfont_from_fontface(fontface
, logfont
);
735 logfont
->lfCharSet
= DEFAULT_CHARSET
;
736 logfont
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
741 struct font_realization_info
{
751 struct font_fileinfo
{
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
;
776 TRACE("%p, %p, %p.\n", iface
, hdc
, fontface
);
783 /* get selected font id */
784 info
.size
= sizeof(info
);
785 if (!GetFontRealizationInfo(hdc
, &info
)) {
786 WARN("failed to get selected font id\n");
791 GetFontFileInfo(info
.instance_id
, 0, NULL
, 0, &needed
);
793 WARN("failed to get font file info size\n");
797 fileinfo
= heap_alloc(needed
);
799 return E_OUTOFMEMORY
;
801 if (!GetFontFileInfo(info
.instance_id
, 0, fileinfo
, needed
, &needed
)) {
807 hr
= IDWriteFactory7_CreateFontFileReference(interop
->factory
, fileinfo
->path
, &fileinfo
->writetime
, &file
);
809 hr
= IDWriteFactory7_CreateCustomFontFileReference(interop
->factory
, &info
.instance_id
,
810 sizeof(info
.instance_id
), &interop
->IDWriteFontFileLoader_iface
, &file
);
816 is_supported
= FALSE
;
817 hr
= IDWriteFontFile_Analyze(file
, &is_supported
, &filetype
, &facetype
, &facenum
);
820 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
821 hr
= IDWriteFactory7_CreateFontFace(interop
->factory
, facetype
, 1, &file
, info
.face_index
,
822 info
.simulations
, fontface
);
824 hr
= DWRITE_E_FILEFORMAT
;
827 IDWriteFontFile_Release(file
);
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
;
849 TRACE("%p, %p, %p, %p.\n", iface
, logfont
, collection
, font
);
853 if (!logfont
) return E_INVALIDARG
;
856 IDWriteFontCollection_AddRef(collection
);
858 hr
= IDWriteFactory5_GetSystemFontCollection((IDWriteFactory5
*)interop
->factory
, FALSE
, (IDWriteFontCollection1
**)&collection
, FALSE
);
860 ERR("failed to get system font collection: 0x%08x.\n", hr
);
865 hr
= IDWriteFontCollection_FindFamilyName(collection
, logfont
->lfFaceName
, &index
, &exists
);
870 hr
= DWRITE_E_NOFONT
;
874 hr
= IDWriteFontCollection_GetFontFamily(collection
, index
, &family
);
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
);
883 IDWriteFontCollection_Release(collection
);
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
);
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
);
919 static const struct IDWriteGdiInterop1Vtbl gdiinteropvtbl
= {
920 gdiinterop_QueryInterface
,
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
)) {
942 IDWriteFontFileStream_AddRef(iface
);
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
);
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
);
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
;
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
))
988 if ((offset
>= fileinfo
.size
.QuadPart
- 1) || (fragment_size
> fileinfo
.size
.QuadPart
- offset
))
991 if (!(fragment
= heap_alloc(fragment_size
)))
992 return E_OUTOFMEMORY
;
994 if (!GetFontFileData(This
->key
, 0, offset
, fragment
, fragment_size
))
997 *fragment_start
= *fragment_context
= fragment
;
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
;
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
);
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
)) {
1052 IDWriteFontFileLoader_AddRef(iface
);
1057 WARN("Unsupported interface %s.\n", debugstr_guid(riid
));
1058 return E_NOINTERFACE
;
1061 static ULONG WINAPI
memresourceloader_AddRef(IDWriteFontFileLoader
*iface
)
1066 static ULONG WINAPI
memresourceloader_Release(IDWriteFontFileLoader
*iface
)
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
);
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
;
1089 memcpy(&stream
->key
, key
, sizeof(stream
->key
));
1091 *ret
= &stream
->IDWriteFontFileStream_iface
;
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
;
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
;