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
66 IDWriteFontFileStream IDWriteFontFileStream_iface
;
71 static inline int get_dib_stride(int width
, int bpp
)
73 return ((width
* bpp
+ 31) >> 3) & ~3;
76 static HRESULT
create_target_dibsection(struct rendertarget
*target
, UINT32 width
, UINT32 height
)
78 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
[256])];
79 BITMAPINFO
*bmi
= (BITMAPINFO
*)bmibuf
;
82 target
->size
.cx
= width
;
83 target
->size
.cy
= height
;
85 memset(bmi
, 0, sizeof(bmibuf
));
86 bmi
->bmiHeader
.biSize
= sizeof(bmi
->bmiHeader
);
87 bmi
->bmiHeader
.biHeight
= -height
;
88 bmi
->bmiHeader
.biWidth
= width
;
89 bmi
->bmiHeader
.biBitCount
= 32;
90 bmi
->bmiHeader
.biPlanes
= 1;
91 bmi
->bmiHeader
.biCompression
= BI_RGB
;
93 hbm
= CreateDIBSection(target
->hdc
, bmi
, DIB_RGB_COLORS
, (void**)&target
->dib
.ptr
, NULL
, 0);
95 hbm
= CreateBitmap(1, 1, 1, 1, NULL
);
96 target
->dib
.ptr
= NULL
;
97 target
->dib
.stride
= 0;
98 target
->dib
.width
= 0;
101 target
->dib
.stride
= get_dib_stride(width
, 32);
102 target
->dib
.width
= width
;
105 DeleteObject(SelectObject(target
->hdc
, hbm
));
109 static inline struct rendertarget
*impl_from_IDWriteBitmapRenderTarget1(IDWriteBitmapRenderTarget1
*iface
)
111 return CONTAINING_RECORD(iface
, struct rendertarget
, IDWriteBitmapRenderTarget1_iface
);
114 static inline struct rendertarget
*impl_from_ID2D1SimplifiedGeometrySink(ID2D1SimplifiedGeometrySink
*iface
)
116 return CONTAINING_RECORD(iface
, struct rendertarget
, ID2D1SimplifiedGeometrySink_iface
);
119 static inline struct gdiinterop
*impl_from_IDWriteGdiInterop1(IDWriteGdiInterop1
*iface
)
121 return CONTAINING_RECORD(iface
, struct gdiinterop
, IDWriteGdiInterop1_iface
);
124 static inline struct memresource_stream
*impl_from_IDWriteFontFileStream(IDWriteFontFileStream
*iface
)
126 return CONTAINING_RECORD(iface
, struct memresource_stream
, IDWriteFontFileStream_iface
);
129 static HRESULT WINAPI
rendertarget_sink_QueryInterface(ID2D1SimplifiedGeometrySink
*iface
, REFIID riid
, void **obj
)
131 if (IsEqualIID(riid
, &IID_ID2D1SimplifiedGeometrySink
) ||
132 IsEqualIID(riid
, &IID_IUnknown
))
135 ID2D1SimplifiedGeometrySink_AddRef(iface
);
139 WARN("%s not implemented.\n", debugstr_guid(riid
));
143 return E_NOINTERFACE
;
146 static ULONG WINAPI
rendertarget_sink_AddRef(ID2D1SimplifiedGeometrySink
*iface
)
148 struct rendertarget
*target
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
149 return IDWriteBitmapRenderTarget1_AddRef(&target
->IDWriteBitmapRenderTarget1_iface
);
152 static ULONG WINAPI
rendertarget_sink_Release(ID2D1SimplifiedGeometrySink
*iface
)
154 struct rendertarget
*target
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
155 return IDWriteBitmapRenderTarget1_Release(&target
->IDWriteBitmapRenderTarget1_iface
);
158 static void WINAPI
rendertarget_sink_SetFillMode(ID2D1SimplifiedGeometrySink
*iface
, D2D1_FILL_MODE mode
)
160 struct rendertarget
*target
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
161 SetPolyFillMode(target
->hdc
, mode
== D2D1_FILL_MODE_ALTERNATE
? ALTERNATE
: WINDING
);
164 static void WINAPI
rendertarget_sink_SetSegmentFlags(ID2D1SimplifiedGeometrySink
*iface
, D2D1_PATH_SEGMENT vertexFlags
)
168 static void WINAPI
rendertarget_sink_BeginFigure(ID2D1SimplifiedGeometrySink
*iface
, D2D1_POINT_2F startPoint
, D2D1_FIGURE_BEGIN figureBegin
)
170 struct rendertarget
*target
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
171 MoveToEx(target
->hdc
, startPoint
.x
, startPoint
.y
, NULL
);
174 static void WINAPI
rendertarget_sink_AddLines(ID2D1SimplifiedGeometrySink
*iface
, const D2D1_POINT_2F
*points
, UINT32 count
)
176 struct rendertarget
*target
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
180 LineTo(target
->hdc
, points
->x
, points
->y
);
185 static void WINAPI
rendertarget_sink_AddBeziers(ID2D1SimplifiedGeometrySink
*iface
, const D2D1_BEZIER_SEGMENT
*beziers
, UINT32 count
)
187 struct rendertarget
*target
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
192 points
[0].x
= beziers
->point1
.x
;
193 points
[0].y
= beziers
->point1
.y
;
194 points
[1].x
= beziers
->point2
.x
;
195 points
[1].y
= beziers
->point2
.y
;
196 points
[2].x
= beziers
->point3
.x
;
197 points
[2].y
= beziers
->point3
.y
;
199 PolyBezierTo(target
->hdc
, points
, 3);
204 static void WINAPI
rendertarget_sink_EndFigure(ID2D1SimplifiedGeometrySink
*iface
, D2D1_FIGURE_END figureEnd
)
206 struct rendertarget
*target
= impl_from_ID2D1SimplifiedGeometrySink(iface
);
207 CloseFigure(target
->hdc
);
210 static HRESULT WINAPI
rendertarget_sink_Close(ID2D1SimplifiedGeometrySink
*iface
)
215 static const ID2D1SimplifiedGeometrySinkVtbl rendertargetsinkvtbl
=
217 rendertarget_sink_QueryInterface
,
218 rendertarget_sink_AddRef
,
219 rendertarget_sink_Release
,
220 rendertarget_sink_SetFillMode
,
221 rendertarget_sink_SetSegmentFlags
,
222 rendertarget_sink_BeginFigure
,
223 rendertarget_sink_AddLines
,
224 rendertarget_sink_AddBeziers
,
225 rendertarget_sink_EndFigure
,
226 rendertarget_sink_Close
229 static HRESULT WINAPI
rendertarget_QueryInterface(IDWriteBitmapRenderTarget1
*iface
, REFIID riid
, void **obj
)
231 TRACE("%p, %s, %p.\n", iface
, debugstr_guid(riid
), obj
);
233 if (IsEqualIID(riid
, &IID_IDWriteBitmapRenderTarget1
) ||
234 IsEqualIID(riid
, &IID_IDWriteBitmapRenderTarget
) ||
235 IsEqualIID(riid
, &IID_IUnknown
))
238 IDWriteBitmapRenderTarget1_AddRef(iface
);
242 WARN("%s not implemented.\n", debugstr_guid(riid
));
246 return E_NOINTERFACE
;
249 static ULONG WINAPI
rendertarget_AddRef(IDWriteBitmapRenderTarget1
*iface
)
251 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
252 ULONG refcount
= InterlockedIncrement(&target
->refcount
);
254 TRACE("%p, refcount %u.\n", iface
, refcount
);
259 static ULONG WINAPI
rendertarget_Release(IDWriteBitmapRenderTarget1
*iface
)
261 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
262 ULONG refcount
= InterlockedDecrement(&target
->refcount
);
264 TRACE("%p, refcount %u.\n", iface
, refcount
);
268 IDWriteFactory7_Release(target
->factory
);
269 DeleteDC(target
->hdc
);
276 static inline DWORD
*get_pixel_ptr_32(struct dib_data
*dib
, int x
, int y
)
278 return (DWORD
*)((BYTE
*)dib
->ptr
+ y
* dib
->stride
+ x
* 4);
281 static inline BYTE
blend_color(BYTE dst
, BYTE src
, BYTE alpha
)
283 return (src
* alpha
+ dst
* (255 - alpha
) + 127) / 255;
286 static inline DWORD
blend_subpixel(BYTE r
, BYTE g
, BYTE b
, DWORD text
, const BYTE
*alpha
)
288 return blend_color(r
, text
>> 16, alpha
[0]) << 16 |
289 blend_color(g
, text
>> 8, alpha
[1]) << 8 |
290 blend_color(b
, text
, alpha
[2]);
293 static inline DWORD
blend_pixel(BYTE r
, BYTE g
, BYTE b
, DWORD text
, BYTE alpha
)
295 return blend_color(r
, text
>> 16, alpha
) << 16 |
296 blend_color(g
, text
>> 8, alpha
) << 8 |
297 blend_color(b
, text
, alpha
);
300 static void blit_8(struct dib_data
*dib
, const BYTE
*src
, const RECT
*rect
, DWORD text_pixel
)
302 DWORD
*dst_ptr
= get_pixel_ptr_32(dib
, rect
->left
, rect
->top
);
303 int x
, y
, src_width
= rect
->right
- rect
->left
;
305 for (y
= rect
->top
; y
< rect
->bottom
; y
++) {
306 for (x
= 0; x
< src_width
; x
++) {
307 if (!src
[x
]) continue;
308 if (src
[x
] == DWRITE_ALPHA_MAX
)
309 dst_ptr
[x
] = text_pixel
;
311 dst_ptr
[x
] = blend_pixel(dst_ptr
[x
] >> 16, dst_ptr
[x
] >> 8, dst_ptr
[x
], text_pixel
, src
[x
]);
315 dst_ptr
+= dib
->stride
/ 4;
319 static void blit_subpixel_888(struct dib_data
*dib
, int dib_width
, const BYTE
*src
,
320 const RECT
*rect
, DWORD text_pixel
)
322 DWORD
*dst_ptr
= get_pixel_ptr_32(dib
, rect
->left
, rect
->top
);
323 int x
, y
, src_width
= rect
->right
- rect
->left
;
325 for (y
= rect
->top
; y
< rect
->bottom
; y
++) {
326 for (x
= 0; x
< src_width
; x
++) {
327 if (src
[3*x
] == 0 && src
[3*x
+1] == 0 && src
[3*x
+2] == 0) continue;
328 dst_ptr
[x
] = blend_subpixel(dst_ptr
[x
] >> 16, dst_ptr
[x
] >> 8, dst_ptr
[x
], text_pixel
, &src
[3*x
]);
330 dst_ptr
+= dib
->stride
/ 4;
331 src
+= src_width
* 3;
335 static inline DWORD
colorref_to_pixel_888(COLORREF color
)
337 return (((color
>> 16) & 0xff) | (color
& 0xff00) | ((color
<< 16) & 0xff0000));
340 static HRESULT WINAPI
rendertarget_DrawGlyphRun(IDWriteBitmapRenderTarget1
*iface
,
341 FLOAT originX
, FLOAT originY
, DWRITE_MEASURING_MODE measuring_mode
,
342 DWRITE_GLYPH_RUN
const *run
, IDWriteRenderingParams
*params
, COLORREF color
,
345 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
346 IDWriteGlyphRunAnalysis
*analysis
;
347 DWRITE_RENDERING_MODE1 rendermode
;
348 DWRITE_GRID_FIT_MODE gridfitmode
;
349 DWRITE_TEXTURE_TYPE texturetype
;
350 DWRITE_GLYPH_RUN scaled_run
;
351 IDWriteFontFace3
*fontface
;
352 RECT target_rect
, bounds
;
355 TRACE("%p, %.8e, %.8e, %d, %p, %p, 0x%08x, %p.\n", iface
, originX
, originY
,
356 measuring_mode
, run
, params
, color
, bbox_ret
);
358 SetRectEmpty(bbox_ret
);
360 if (!target
->dib
.ptr
)
366 if (FAILED(hr
= IDWriteFontFace_QueryInterface(run
->fontFace
, &IID_IDWriteFontFace3
, (void **)&fontface
))) {
367 WARN("Failed to get IDWriteFontFace2 interface, hr %#x.\n", hr
);
371 hr
= IDWriteFontFace3_GetRecommendedRenderingMode(fontface
, run
->fontEmSize
, target
->ppdip
* 96.0f
,
372 target
->ppdip
* 96.0f
, &target
->m
, run
->isSideways
, DWRITE_OUTLINE_THRESHOLD_ALIASED
, measuring_mode
,
373 params
, &rendermode
, &gridfitmode
);
374 IDWriteFontFace3_Release(fontface
);
378 SetRect(&target_rect
, 0, 0, target
->size
.cx
, target
->size
.cy
);
380 if (rendermode
== DWRITE_RENDERING_MODE1_OUTLINE
)
382 static const XFORM identity
= { 1.0f
, 0.0f
, 0.0f
, 1.0f
, 0.0f
, 0.0f
};
383 const DWRITE_MATRIX
*m
= &target
->m
;
386 /* target allows any transform to be set, filter it here */
387 if (m
->m11
* m
->m22
== m
->m12
* m
->m21
) {
399 xform
.eDx
= m
->m11
* originX
+ m
->m21
* originY
+ m
->dx
;
400 xform
.eDy
= m
->m12
* originX
+ m
->m22
* originY
+ m
->dy
;
402 SetWorldTransform(target
->hdc
, &xform
);
404 BeginPath(target
->hdc
);
406 hr
= IDWriteFontFace_GetGlyphRunOutline(run
->fontFace
, run
->fontEmSize
* target
->ppdip
,
407 run
->glyphIndices
, run
->glyphAdvances
, run
->glyphOffsets
, run
->glyphCount
,
408 run
->isSideways
, run
->bidiLevel
& 1, &target
->ID2D1SimplifiedGeometrySink_iface
);
410 EndPath(target
->hdc
);
414 HBRUSH brush
= CreateSolidBrush(color
);
416 SelectObject(target
->hdc
, brush
);
418 FillPath(target
->hdc
);
420 /* FIXME: one way to get affected rectangle bounds is to use region fill */
422 *bbox_ret
= target_rect
;
427 SetWorldTransform(target
->hdc
, &identity
);
433 scaled_run
.fontEmSize
*= target
->ppdip
;
434 hr
= IDWriteFactory7_CreateGlyphRunAnalysis(target
->factory
, &scaled_run
, &target
->m
, rendermode
, measuring_mode
,
435 gridfitmode
, target
->antialiasmode
, originX
, originY
, &analysis
);
438 WARN("failed to create analysis instance, 0x%08x\n", hr
);
442 SetRectEmpty(&bounds
);
443 texturetype
= DWRITE_TEXTURE_ALIASED_1x1
;
444 hr
= IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis
, DWRITE_TEXTURE_ALIASED_1x1
, &bounds
);
445 if (FAILED(hr
) || IsRectEmpty(&bounds
)) {
446 hr
= IDWriteGlyphRunAnalysis_GetAlphaTextureBounds(analysis
, DWRITE_TEXTURE_CLEARTYPE_3x1
, &bounds
);
448 WARN("GetAlphaTextureBounds() failed, 0x%08x\n", hr
);
449 IDWriteGlyphRunAnalysis_Release(analysis
);
452 texturetype
= DWRITE_TEXTURE_CLEARTYPE_3x1
;
455 if (IntersectRect(&target_rect
, &target_rect
, &bounds
))
457 UINT32 size
= (target_rect
.right
- target_rect
.left
) * (target_rect
.bottom
- target_rect
.top
);
460 color
= colorref_to_pixel_888(color
);
461 if (texturetype
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
463 bitmap
= heap_alloc_zero(size
);
465 IDWriteGlyphRunAnalysis_Release(analysis
);
466 return E_OUTOFMEMORY
;
469 hr
= IDWriteGlyphRunAnalysis_CreateAlphaTexture(analysis
, texturetype
, &target_rect
, bitmap
, size
);
471 /* blit to target dib */
472 if (texturetype
== DWRITE_TEXTURE_ALIASED_1x1
)
473 blit_8(&target
->dib
, bitmap
, &target_rect
, color
);
475 blit_subpixel_888(&target
->dib
, target
->size
.cx
, bitmap
, &target_rect
, color
);
477 if (bbox_ret
) *bbox_ret
= target_rect
;
483 IDWriteGlyphRunAnalysis_Release(analysis
);
488 static HDC WINAPI
rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget1
*iface
)
490 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
492 TRACE("%p.\n", iface
);
497 static float WINAPI
rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget1
*iface
)
499 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
501 TRACE("%p.\n", iface
);
503 return target
->ppdip
;
506 static HRESULT WINAPI
rendertarget_SetPixelsPerDip(IDWriteBitmapRenderTarget1
*iface
, float ppdip
)
508 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
510 TRACE("%p, %.2f.\n", iface
, ppdip
);
515 target
->ppdip
= ppdip
;
519 static HRESULT WINAPI
rendertarget_GetCurrentTransform(IDWriteBitmapRenderTarget1
*iface
, DWRITE_MATRIX
*transform
)
521 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
523 TRACE("%p, %p.\n", iface
, transform
);
525 *transform
= target
->m
;
529 static HRESULT WINAPI
rendertarget_SetCurrentTransform(IDWriteBitmapRenderTarget1
*iface
, DWRITE_MATRIX
const *transform
)
531 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
533 TRACE("%p, %p.\n", iface
, transform
);
535 target
->m
= transform
? *transform
: identity
;
539 static HRESULT WINAPI
rendertarget_GetSize(IDWriteBitmapRenderTarget1
*iface
, SIZE
*size
)
541 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
543 TRACE("%p, %p.\n", iface
, size
);
545 *size
= target
->size
;
549 static HRESULT WINAPI
rendertarget_Resize(IDWriteBitmapRenderTarget1
*iface
, UINT32 width
, UINT32 height
)
551 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
553 TRACE("%p, %u, %u.\n", iface
, width
, height
);
555 if (target
->size
.cx
== width
&& target
->size
.cy
== height
)
558 return create_target_dibsection(target
, width
, height
);
561 static DWRITE_TEXT_ANTIALIAS_MODE WINAPI
rendertarget_GetTextAntialiasMode(IDWriteBitmapRenderTarget1
*iface
)
563 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
565 TRACE("%p.\n", iface
);
567 return target
->antialiasmode
;
570 static HRESULT WINAPI
rendertarget_SetTextAntialiasMode(IDWriteBitmapRenderTarget1
*iface
, DWRITE_TEXT_ANTIALIAS_MODE mode
)
572 struct rendertarget
*target
= impl_from_IDWriteBitmapRenderTarget1(iface
);
574 TRACE("%p, %d.\n", iface
, mode
);
576 if ((DWORD
)mode
> DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE
)
579 target
->antialiasmode
= mode
;
583 static const IDWriteBitmapRenderTarget1Vtbl rendertargetvtbl
=
585 rendertarget_QueryInterface
,
587 rendertarget_Release
,
588 rendertarget_DrawGlyphRun
,
589 rendertarget_GetMemoryDC
,
590 rendertarget_GetPixelsPerDip
,
591 rendertarget_SetPixelsPerDip
,
592 rendertarget_GetCurrentTransform
,
593 rendertarget_SetCurrentTransform
,
594 rendertarget_GetSize
,
596 rendertarget_GetTextAntialiasMode
,
597 rendertarget_SetTextAntialiasMode
600 static HRESULT
create_rendertarget(IDWriteFactory7
*factory
, HDC hdc
, UINT32 width
, UINT32 height
,
601 IDWriteBitmapRenderTarget
**ret
)
603 struct rendertarget
*target
;
608 target
= heap_alloc(sizeof(struct rendertarget
));
609 if (!target
) return E_OUTOFMEMORY
;
611 target
->IDWriteBitmapRenderTarget1_iface
.lpVtbl
= &rendertargetvtbl
;
612 target
->ID2D1SimplifiedGeometrySink_iface
.lpVtbl
= &rendertargetsinkvtbl
;
613 target
->refcount
= 1;
615 target
->hdc
= CreateCompatibleDC(hdc
);
616 SetGraphicsMode(target
->hdc
, GM_ADVANCED
);
617 hr
= create_target_dibsection(target
, width
, height
);
619 IDWriteBitmapRenderTarget1_Release(&target
->IDWriteBitmapRenderTarget1_iface
);
623 target
->m
= identity
;
624 target
->ppdip
= GetDeviceCaps(target
->hdc
, LOGPIXELSX
) / 96.0f
;
625 target
->antialiasmode
= DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE
;
626 target
->factory
= factory
;
627 IDWriteFactory7_AddRef(factory
);
629 *ret
= (IDWriteBitmapRenderTarget
*)&target
->IDWriteBitmapRenderTarget1_iface
;
634 static HRESULT WINAPI
gdiinterop_QueryInterface(IDWriteGdiInterop1
*iface
, REFIID riid
, void **obj
)
636 TRACE("%p, %s, %p.\n", iface
, debugstr_guid(riid
), obj
);
638 if (IsEqualIID(riid
, &IID_IDWriteGdiInterop1
) ||
639 IsEqualIID(riid
, &IID_IDWriteGdiInterop
) ||
640 IsEqualIID(riid
, &IID_IUnknown
))
643 IDWriteGdiInterop1_AddRef(iface
);
647 WARN("%s not implemented.\n", debugstr_guid(riid
));
650 return E_NOINTERFACE
;
653 static ULONG WINAPI
gdiinterop_AddRef(IDWriteGdiInterop1
*iface
)
655 struct gdiinterop
*interop
= impl_from_IDWriteGdiInterop1(iface
);
656 LONG refcount
= InterlockedIncrement(&interop
->refcount
);
658 TRACE("%p, refcount %u.\n", iface
, refcount
);
663 static ULONG WINAPI
gdiinterop_Release(IDWriteGdiInterop1
*iface
)
665 struct gdiinterop
*interop
= impl_from_IDWriteGdiInterop1(iface
);
666 LONG refcount
= InterlockedDecrement(&interop
->refcount
);
668 TRACE("%p, refcount %u.\n", iface
, refcount
);
672 IDWriteFactory7_UnregisterFontFileLoader(interop
->factory
, &interop
->IDWriteFontFileLoader_iface
);
673 factory_detach_gdiinterop(interop
->factory
, iface
);
680 static HRESULT WINAPI
gdiinterop_CreateFontFromLOGFONT(IDWriteGdiInterop1
*iface
,
681 LOGFONTW
const *logfont
, IDWriteFont
**font
)
683 TRACE("%p, %p, %p.\n", iface
, logfont
, font
);
685 return IDWriteGdiInterop1_CreateFontFromLOGFONT(iface
, logfont
, NULL
, font
);
688 static HRESULT WINAPI
gdiinterop_ConvertFontToLOGFONT(IDWriteGdiInterop1
*iface
,
689 IDWriteFont
*font
, LOGFONTW
*logfont
, BOOL
*is_systemfont
)
691 IDWriteFontCollection
*collection
;
692 IDWriteFontFamily
*family
;
695 TRACE("%p, %p, %p, %p.\n", iface
, font
, logfont
, is_systemfont
);
697 *is_systemfont
= FALSE
;
699 memset(logfont
, 0, sizeof(*logfont
));
704 hr
= IDWriteFont_GetFontFamily(font
, &family
);
708 hr
= IDWriteFontFamily_GetFontCollection(family
, &collection
);
709 IDWriteFontFamily_Release(family
);
713 *is_systemfont
= is_system_collection(collection
);
714 IDWriteFontCollection_Release(collection
);
716 get_logfont_from_font(font
, logfont
);
717 logfont
->lfCharSet
= DEFAULT_CHARSET
;
718 logfont
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
723 static HRESULT WINAPI
gdiinterop_ConvertFontFaceToLOGFONT(IDWriteGdiInterop1
*iface
,
724 IDWriteFontFace
*fontface
, LOGFONTW
*logfont
)
726 TRACE("%p, %p, %p.\n", iface
, fontface
, logfont
);
728 memset(logfont
, 0, sizeof(*logfont
));
733 get_logfont_from_fontface(fontface
, logfont
);
734 logfont
->lfCharSet
= DEFAULT_CHARSET
;
735 logfont
->lfOutPrecision
= OUT_OUTLINE_PRECIS
;
740 struct font_realization_info
{
750 struct font_fileinfo
{
756 /* Undocumented gdi32 exports, used to access actually selected font information */
757 extern BOOL WINAPI
GetFontRealizationInfo(HDC hdc
, struct font_realization_info
*info
);
758 extern BOOL WINAPI
GetFontFileInfo(DWORD instance_id
, DWORD unknown
, struct font_fileinfo
*info
, SIZE_T size
, SIZE_T
*needed
);
759 extern BOOL WINAPI
GetFontFileData(DWORD instance_id
, DWORD unknown
, UINT64 offset
, void *buff
, DWORD buff_size
);
761 static HRESULT WINAPI
gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop1
*iface
,
762 HDC hdc
, IDWriteFontFace
**fontface
)
764 struct gdiinterop
*interop
= impl_from_IDWriteGdiInterop1(iface
);
765 struct font_realization_info info
;
766 struct font_fileinfo
*fileinfo
;
767 DWRITE_FONT_FILE_TYPE filetype
;
768 DWRITE_FONT_FACE_TYPE facetype
;
769 IDWriteFontFile
*file
;
775 TRACE("%p, %p, %p.\n", iface
, hdc
, fontface
);
782 /* get selected font id */
783 info
.size
= sizeof(info
);
784 if (!GetFontRealizationInfo(hdc
, &info
)) {
785 WARN("failed to get selected font id\n");
790 GetFontFileInfo(info
.instance_id
, 0, NULL
, 0, &needed
);
792 WARN("failed to get font file info size\n");
796 fileinfo
= heap_alloc(needed
);
798 return E_OUTOFMEMORY
;
800 if (!GetFontFileInfo(info
.instance_id
, 0, fileinfo
, needed
, &needed
)) {
806 hr
= IDWriteFactory7_CreateFontFileReference(interop
->factory
, fileinfo
->path
, &fileinfo
->writetime
, &file
);
808 hr
= IDWriteFactory7_CreateCustomFontFileReference(interop
->factory
, &info
.instance_id
,
809 sizeof(info
.instance_id
), &interop
->IDWriteFontFileLoader_iface
, &file
);
815 is_supported
= FALSE
;
816 hr
= IDWriteFontFile_Analyze(file
, &is_supported
, &filetype
, &facetype
, &facenum
);
819 /* Simulations flags values match DWRITE_FONT_SIMULATIONS */
820 hr
= IDWriteFactory7_CreateFontFace(interop
->factory
, facetype
, 1, &file
, info
.face_index
,
821 info
.simulations
, fontface
);
823 hr
= DWRITE_E_FILEFORMAT
;
826 IDWriteFontFile_Release(file
);
830 static HRESULT WINAPI
gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop1
*iface
,
831 HDC hdc
, UINT32 width
, UINT32 height
, IDWriteBitmapRenderTarget
**target
)
833 struct gdiinterop
*interop
= impl_from_IDWriteGdiInterop1(iface
);
835 TRACE("%p, %p, %u, %u, %p.\n", iface
, hdc
, width
, height
, target
);
837 return create_rendertarget(interop
->factory
, hdc
, width
, height
, target
);
840 static HRESULT WINAPI
gdiinterop1_CreateFontFromLOGFONT(IDWriteGdiInterop1
*iface
,
841 LOGFONTW
const *logfont
, IDWriteFontCollection
*collection
, IDWriteFont
**font
)
843 struct gdiinterop
*interop
= impl_from_IDWriteGdiInterop1(iface
);
844 IDWriteFontFamily
*family
;
845 DWRITE_FONT_STYLE style
;
850 TRACE("%p, %p, %p, %p.\n", iface
, logfont
, collection
, font
);
854 if (!logfont
) return E_INVALIDARG
;
857 IDWriteFontCollection_AddRef(collection
);
859 hr
= IDWriteFactory5_GetSystemFontCollection((IDWriteFactory5
*)interop
->factory
, FALSE
, (IDWriteFontCollection1
**)&collection
, FALSE
);
861 ERR("failed to get system font collection: 0x%08x.\n", hr
);
866 hr
= IDWriteFontCollection_FindFamilyName(collection
, logfont
->lfFaceName
, &index
, &exists
);
871 hr
= DWRITE_E_NOFONT
;
875 hr
= IDWriteFontCollection_GetFontFamily(collection
, index
, &family
);
879 style
= logfont
->lfItalic
? DWRITE_FONT_STYLE_ITALIC
: DWRITE_FONT_STYLE_NORMAL
;
880 hr
= IDWriteFontFamily_GetFirstMatchingFont(family
, logfont
->lfWeight
, DWRITE_FONT_STRETCH_NORMAL
, style
, font
);
881 IDWriteFontFamily_Release(family
);
884 IDWriteFontCollection_Release(collection
);
888 static HRESULT WINAPI
gdiinterop1_GetFontSignature_(IDWriteGdiInterop1
*iface
, IDWriteFontFace
*fontface
,
889 FONTSIGNATURE
*fontsig
)
891 TRACE("%p, %p, %p.\n", iface
, fontface
, fontsig
);
893 return get_fontsig_from_fontface(fontface
, fontsig
);
896 static HRESULT WINAPI
gdiinterop1_GetFontSignature(IDWriteGdiInterop1
*iface
, IDWriteFont
*font
, FONTSIGNATURE
*fontsig
)
898 TRACE("%p, %p, %p.\n", iface
, font
, fontsig
);
903 return get_fontsig_from_font(font
, fontsig
);
906 static HRESULT WINAPI
gdiinterop1_GetMatchingFontsByLOGFONT(IDWriteGdiInterop1
*iface
, LOGFONTW
const *logfont
,
907 IDWriteFontSet
*fontset
, IDWriteFontSet
**subset
)
909 FIXME("%p, %p, %p, %p: stub.\n", iface
, logfont
, fontset
, subset
);
914 static const IDWriteGdiInterop1Vtbl gdiinteropvtbl
=
916 gdiinterop_QueryInterface
,
919 gdiinterop_CreateFontFromLOGFONT
,
920 gdiinterop_ConvertFontToLOGFONT
,
921 gdiinterop_ConvertFontFaceToLOGFONT
,
922 gdiinterop_CreateFontFaceFromHdc
,
923 gdiinterop_CreateBitmapRenderTarget
,
924 gdiinterop1_CreateFontFromLOGFONT
,
925 gdiinterop1_GetFontSignature_
,
926 gdiinterop1_GetFontSignature
,
927 gdiinterop1_GetMatchingFontsByLOGFONT
930 static HRESULT WINAPI
memresourcestream_QueryInterface(IDWriteFontFileStream
*iface
, REFIID riid
, void **out
)
932 TRACE("%p, %s, %p.\n", iface
, debugstr_guid(riid
), out
);
934 if (IsEqualIID(&IID_IDWriteFontFileStream
, riid
) || IsEqualIID(&IID_IUnknown
, riid
)) {
936 IDWriteFontFileStream_AddRef(iface
);
941 WARN("Unsupported interface %s.\n", debugstr_guid(riid
));
942 return E_NOINTERFACE
;
945 static ULONG WINAPI
memresourcestream_AddRef(IDWriteFontFileStream
*iface
)
947 struct memresource_stream
*stream
= impl_from_IDWriteFontFileStream(iface
);
948 ULONG refcount
= InterlockedIncrement(&stream
->refcount
);
950 TRACE("%p, refcount %d.\n", iface
, refcount
);
955 static ULONG WINAPI
memresourcestream_Release(IDWriteFontFileStream
*iface
)
957 struct memresource_stream
*stream
= impl_from_IDWriteFontFileStream(iface
);
958 ULONG refcount
= InterlockedDecrement(&stream
->refcount
);
960 TRACE("%p, refcount %d.\n", iface
, refcount
);
968 static HRESULT WINAPI
memresourcestream_ReadFileFragment(IDWriteFontFileStream
*iface
, void const **fragment_start
,
969 UINT64 offset
, UINT64 fragment_size
, void **fragment_context
)
971 struct memresource_stream
*stream
= impl_from_IDWriteFontFileStream(iface
);
972 struct font_fileinfo fileinfo
;
975 TRACE("%p, %p, %s, %s, %p.\n", iface
, fragment_start
, wine_dbgstr_longlong(offset
),
976 wine_dbgstr_longlong(fragment_size
), fragment_context
);
978 *fragment_context
= NULL
;
979 *fragment_start
= NULL
;
981 if (!GetFontFileInfo(stream
->key
, 0, &fileinfo
, sizeof(fileinfo
), NULL
))
984 if ((offset
>= fileinfo
.size
.QuadPart
- 1) || (fragment_size
> fileinfo
.size
.QuadPart
- offset
))
987 if (!(fragment
= heap_alloc(fragment_size
)))
988 return E_OUTOFMEMORY
;
990 if (!GetFontFileData(stream
->key
, 0, offset
, fragment
, fragment_size
))
993 *fragment_start
= *fragment_context
= fragment
;
997 static void WINAPI
memresourcestream_ReleaseFileFragment(IDWriteFontFileStream
*iface
, void *fragment_context
)
999 TRACE("%p, %p.\n", iface
, fragment_context
);
1001 heap_free(fragment_context
);
1004 static HRESULT WINAPI
memresourcestream_GetFileSize(IDWriteFontFileStream
*iface
, UINT64
*size
)
1006 struct memresource_stream
*stream
= impl_from_IDWriteFontFileStream(iface
);
1007 struct font_fileinfo fileinfo
;
1009 TRACE("%p, %p.\n", iface
, size
);
1011 if (!GetFontFileInfo(stream
->key
, 0, &fileinfo
, sizeof(fileinfo
), NULL
))
1012 return E_INVALIDARG
;
1014 *size
= fileinfo
.size
.QuadPart
;
1019 static HRESULT WINAPI
memresourcestream_GetLastWriteTime(IDWriteFontFileStream
*iface
, UINT64
*last_writetime
)
1021 TRACE("%p, %p.\n", iface
, last_writetime
);
1026 static const IDWriteFontFileStreamVtbl memresourcestreamvtbl
=
1028 memresourcestream_QueryInterface
,
1029 memresourcestream_AddRef
,
1030 memresourcestream_Release
,
1031 memresourcestream_ReadFileFragment
,
1032 memresourcestream_ReleaseFileFragment
,
1033 memresourcestream_GetFileSize
,
1034 memresourcestream_GetLastWriteTime
,
1037 static HRESULT WINAPI
memresourceloader_QueryInterface(IDWriteFontFileLoader
*iface
, REFIID riid
, void **out
)
1039 TRACE("%p, %s, %p.\n", iface
, debugstr_guid(riid
), out
);
1041 if (IsEqualIID(&IID_IDWriteFontFileLoader
, riid
) || IsEqualIID(&IID_IUnknown
, riid
)) {
1043 IDWriteFontFileLoader_AddRef(iface
);
1048 WARN("Unsupported interface %s.\n", debugstr_guid(riid
));
1049 return E_NOINTERFACE
;
1052 static ULONG WINAPI
memresourceloader_AddRef(IDWriteFontFileLoader
*iface
)
1057 static ULONG WINAPI
memresourceloader_Release(IDWriteFontFileLoader
*iface
)
1062 static HRESULT WINAPI
memresourceloader_CreateStreamFromKey(IDWriteFontFileLoader
*iface
, void const *key
,
1063 UINT32 key_size
, IDWriteFontFileStream
**ret
)
1065 struct memresource_stream
*stream
;
1067 TRACE("%p, %p, %u, %p.\n", iface
, key
, key_size
, ret
);
1071 if (!key
|| key_size
!= sizeof(DWORD
))
1072 return E_INVALIDARG
;
1074 if (!(stream
= heap_alloc(sizeof(*stream
))))
1075 return E_OUTOFMEMORY
;
1077 stream
->IDWriteFontFileStream_iface
.lpVtbl
= &memresourcestreamvtbl
;
1078 stream
->refcount
= 1;
1079 memcpy(&stream
->key
, key
, sizeof(stream
->key
));
1081 *ret
= &stream
->IDWriteFontFileStream_iface
;
1086 static const IDWriteFontFileLoaderVtbl memresourceloadervtbl
=
1088 memresourceloader_QueryInterface
,
1089 memresourceloader_AddRef
,
1090 memresourceloader_Release
,
1091 memresourceloader_CreateStreamFromKey
,
1094 HRESULT
create_gdiinterop(IDWriteFactory7
*factory
, IDWriteGdiInterop1
**ret
)
1096 struct gdiinterop
*interop
;
1100 if (!(interop
= heap_alloc(sizeof(*interop
))))
1101 return E_OUTOFMEMORY
;
1103 interop
->IDWriteGdiInterop1_iface
.lpVtbl
= &gdiinteropvtbl
;
1104 interop
->IDWriteFontFileLoader_iface
.lpVtbl
= &memresourceloadervtbl
;
1105 interop
->refcount
= 1;
1106 interop
->factory
= factory
;
1107 IDWriteFactory7_AddRef(interop
->factory
);
1108 IDWriteFactory7_RegisterFontFileLoader(factory
, &interop
->IDWriteFontFileLoader_iface
);
1110 *ret
= &interop
->IDWriteGdiInterop1_iface
;