dwrite: Set facename field of a LOGFONT in ConvertFontFaceToLOGFONT().
[wine/multimedia.git] / dlls / dwrite / font.c
blob2092576831b42a0bfa417c934c0432bfdbf680e4
1 /*
2 * Font and collections
4 * Copyright 2012, 2014 Nikolay Sivov for CodeWeavers
5 * Copyright 2014 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
24 #include "wine/list.h"
25 #include "dwrite_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
29 #define MS_HEAD_TAG DWRITE_MAKE_OPENTYPE_TAG('h','e','a','d')
30 #define MS_OS2_TAG DWRITE_MAKE_OPENTYPE_TAG('O','S','/','2')
31 #define MS_CMAP_TAG DWRITE_MAKE_OPENTYPE_TAG('c','m','a','p')
32 #define MS_NAME_TAG DWRITE_MAKE_OPENTYPE_TAG('n','a','m','e')
34 static const IID IID_issystemcollection = {0x14d88047,0x331f,0x4cd3,{0xbc,0xa8,0x3e,0x67,0x99,0xaf,0x34,0x75}};
36 struct dwrite_font_data {
37 LONG ref;
39 DWRITE_FONT_STYLE style;
40 DWRITE_FONT_STRETCH stretch;
41 DWRITE_FONT_WEIGHT weight;
42 DWRITE_FONT_METRICS1 metrics;
43 IDWriteLocalizedStrings *info_strings[DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_CID_NAME+1];
45 /* data needed to create fontface instance */
46 IDWriteFactory2 *factory;
47 DWRITE_FONT_FACE_TYPE face_type;
48 IDWriteFontFile *file;
49 UINT32 face_index;
51 WCHAR *facename;
54 struct dwrite_fontfamily_data {
55 LONG ref;
57 IDWriteLocalizedStrings *familyname;
59 struct dwrite_font_data **fonts;
60 UINT32 font_count;
61 UINT32 font_alloc;
64 struct dwrite_fontcollection {
65 IDWriteFontCollection IDWriteFontCollection_iface;
66 LONG ref;
68 struct dwrite_fontfamily_data **family_data;
69 UINT32 family_count;
70 UINT32 family_alloc;
71 BOOL is_system;
74 struct dwrite_fontfamily {
75 IDWriteFontFamily IDWriteFontFamily_iface;
76 LONG ref;
78 struct dwrite_fontfamily_data *data;
80 IDWriteFontCollection* collection;
83 struct dwrite_font {
84 IDWriteFont2 IDWriteFont2_iface;
85 LONG ref;
87 IDWriteFontFamily *family;
89 USHORT simulations;
90 DWRITE_FONT_STYLE style;
91 struct dwrite_font_data *data;
94 struct dwrite_fonttable {
95 void *data;
96 void *context;
97 UINT32 size;
100 #define GLYPH_BLOCK_SHIFT 8
101 #define GLYPH_BLOCK_SIZE (1UL << GLYPH_BLOCK_SHIFT)
102 #define GLYPH_BLOCK_MASK (GLYPH_BLOCK_SIZE - 1)
103 #define GLYPH_MAX 65536
105 struct dwrite_fontface {
106 IDWriteFontFace2 IDWriteFontFace2_iface;
107 LONG ref;
109 IDWriteFontFileStream **streams;
110 IDWriteFontFile **files;
111 UINT32 file_count;
112 UINT32 index;
114 USHORT simulations;
115 DWRITE_FONT_FACE_TYPE type;
116 DWRITE_FONT_METRICS1 metrics;
117 DWRITE_CARET_METRICS caret;
119 struct dwrite_fonttable cmap;
120 DWRITE_GLYPH_METRICS *glyphs[GLYPH_MAX/GLYPH_BLOCK_SIZE];
123 struct dwrite_fontfile {
124 IDWriteFontFile IDWriteFontFile_iface;
125 LONG ref;
127 IDWriteFontFileLoader *loader;
128 void *reference_key;
129 UINT32 key_size;
130 IDWriteFontFileStream *stream;
133 static inline struct dwrite_fontface *impl_from_IDWriteFontFace2(IDWriteFontFace2 *iface)
135 return CONTAINING_RECORD(iface, struct dwrite_fontface, IDWriteFontFace2_iface);
138 static inline struct dwrite_font *impl_from_IDWriteFont2(IDWriteFont2 *iface)
140 return CONTAINING_RECORD(iface, struct dwrite_font, IDWriteFont2_iface);
143 static inline struct dwrite_fontfile *impl_from_IDWriteFontFile(IDWriteFontFile *iface)
145 return CONTAINING_RECORD(iface, struct dwrite_fontfile, IDWriteFontFile_iface);
148 static inline struct dwrite_fontfamily *impl_from_IDWriteFontFamily(IDWriteFontFamily *iface)
150 return CONTAINING_RECORD(iface, struct dwrite_fontfamily, IDWriteFontFamily_iface);
153 static inline struct dwrite_fontcollection *impl_from_IDWriteFontCollection(IDWriteFontCollection *iface)
155 return CONTAINING_RECORD(iface, struct dwrite_fontcollection, IDWriteFontCollection_iface);
158 static HRESULT get_cached_glyph_metrics(struct dwrite_fontface *fontface, UINT16 glyph, DWRITE_GLYPH_METRICS *metrics)
160 static const DWRITE_GLYPH_METRICS nil;
161 DWRITE_GLYPH_METRICS *block = fontface->glyphs[glyph >> GLYPH_BLOCK_SHIFT];
163 if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(DWRITE_GLYPH_METRICS))) return S_FALSE;
164 memcpy(metrics, &block[glyph & GLYPH_BLOCK_MASK], sizeof(*metrics));
165 return S_OK;
168 static HRESULT set_cached_glyph_metrics(struct dwrite_fontface *fontface, UINT16 glyph, DWRITE_GLYPH_METRICS *metrics)
170 DWRITE_GLYPH_METRICS **block = &fontface->glyphs[glyph >> GLYPH_BLOCK_SHIFT];
172 if (!*block) {
173 /* start new block */
174 *block = heap_alloc_zero(sizeof(*metrics) * GLYPH_BLOCK_SIZE);
175 if (!*block)
176 return E_OUTOFMEMORY;
179 memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], metrics, sizeof(*metrics));
180 return S_OK;
183 static inline void* get_fontface_cmap(struct dwrite_fontface *fontface)
185 BOOL exists = FALSE;
186 HRESULT hr;
188 if (fontface->cmap.data)
189 return fontface->cmap.data;
191 hr = IDWriteFontFace2_TryGetFontTable(&fontface->IDWriteFontFace2_iface, MS_CMAP_TAG, (const void**)&fontface->cmap.data,
192 &fontface->cmap.size, &fontface->cmap.context, &exists);
193 if (FAILED(hr) || !exists) {
194 ERR("Font does not have a CMAP table\n");
195 return NULL;
198 return fontface->cmap.data;
201 static void release_font_data(struct dwrite_font_data *data)
203 int i;
205 if (InterlockedDecrement(&data->ref) > 0)
206 return;
208 for (i = DWRITE_INFORMATIONAL_STRING_NONE; i < sizeof(data->info_strings)/sizeof(data->info_strings[0]); i++) {
209 if (data->info_strings[i])
210 IDWriteLocalizedStrings_Release(data->info_strings[i]);
213 IDWriteFontFile_Release(data->file);
214 IDWriteFactory2_Release(data->factory);
215 heap_free(data->facename);
216 heap_free(data);
219 static void release_fontfamily_data(struct dwrite_fontfamily_data *data)
221 int i;
223 if (InterlockedDecrement(&data->ref) > 0)
224 return;
226 for (i = 0; i < data->font_count; i++)
227 release_font_data(data->fonts[i]);
228 heap_free(data->fonts);
229 IDWriteLocalizedStrings_Release(data->familyname);
230 heap_free(data);
233 static HRESULT WINAPI dwritefontface_QueryInterface(IDWriteFontFace2 *iface, REFIID riid, void **obj)
235 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
237 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
239 if (IsEqualIID(riid, &IID_IDWriteFontFace2) ||
240 IsEqualIID(riid, &IID_IDWriteFontFace1) ||
241 IsEqualIID(riid, &IID_IDWriteFontFace) ||
242 IsEqualIID(riid, &IID_IUnknown))
244 *obj = iface;
245 IDWriteFontFace2_AddRef(iface);
246 return S_OK;
249 *obj = NULL;
250 return E_NOINTERFACE;
253 static ULONG WINAPI dwritefontface_AddRef(IDWriteFontFace2 *iface)
255 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
256 ULONG ref = InterlockedIncrement(&This->ref);
257 TRACE("(%p)->(%d)\n", This, ref);
258 return ref;
261 static ULONG WINAPI dwritefontface_Release(IDWriteFontFace2 *iface)
263 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
264 ULONG ref = InterlockedDecrement(&This->ref);
266 TRACE("(%p)->(%d)\n", This, ref);
268 if (!ref) {
269 UINT32 i;
271 if (This->cmap.context)
272 IDWriteFontFace2_ReleaseFontTable(iface, This->cmap.context);
273 for (i = 0; i < This->file_count; i++) {
274 if (This->streams[i])
275 IDWriteFontFileStream_Release(This->streams[i]);
276 if (This->files[i])
277 IDWriteFontFile_Release(This->files[i]);
280 for (i = 0; i < sizeof(This->glyphs)/sizeof(This->glyphs[0]); i++)
281 heap_free(This->glyphs[i]);
283 freetype_notify_cacheremove(iface);
284 heap_free(This);
287 return ref;
290 static DWRITE_FONT_FACE_TYPE WINAPI dwritefontface_GetType(IDWriteFontFace2 *iface)
292 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
293 TRACE("(%p)\n", This);
294 return This->type;
297 static HRESULT WINAPI dwritefontface_GetFiles(IDWriteFontFace2 *iface, UINT32 *number_of_files,
298 IDWriteFontFile **fontfiles)
300 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
301 int i;
303 TRACE("(%p)->(%p %p)\n", This, number_of_files, fontfiles);
304 if (fontfiles == NULL)
306 *number_of_files = This->file_count;
307 return S_OK;
309 if (*number_of_files < This->file_count)
310 return E_INVALIDARG;
312 for (i = 0; i < This->file_count; i++)
314 IDWriteFontFile_AddRef(This->files[i]);
315 fontfiles[i] = This->files[i];
318 return S_OK;
321 static UINT32 WINAPI dwritefontface_GetIndex(IDWriteFontFace2 *iface)
323 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
324 TRACE("(%p)\n", This);
325 return This->index;
328 static DWRITE_FONT_SIMULATIONS WINAPI dwritefontface_GetSimulations(IDWriteFontFace2 *iface)
330 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
331 TRACE("(%p)\n", This);
332 return This->simulations;
335 static BOOL WINAPI dwritefontface_IsSymbolFont(IDWriteFontFace2 *iface)
337 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
338 FIXME("(%p): stub\n", This);
339 return FALSE;
342 static void WINAPI dwritefontface_GetMetrics(IDWriteFontFace2 *iface, DWRITE_FONT_METRICS *metrics)
344 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
345 TRACE("(%p)->(%p)\n", This, metrics);
346 memcpy(metrics, &This->metrics, sizeof(*metrics));
349 static UINT16 WINAPI dwritefontface_GetGlyphCount(IDWriteFontFace2 *iface)
351 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
352 TRACE("(%p)\n", This);
353 return freetype_get_glyphcount(iface);
356 static HRESULT WINAPI dwritefontface_GetDesignGlyphMetrics(IDWriteFontFace2 *iface,
357 UINT16 const *glyphs, UINT32 glyph_count, DWRITE_GLYPH_METRICS *ret, BOOL is_sideways)
359 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
360 HRESULT hr;
361 UINT32 i;
363 TRACE("(%p)->(%p %u %p %d)\n", This, glyphs, glyph_count, ret, is_sideways);
365 if (!glyphs)
366 return E_INVALIDARG;
368 if (is_sideways)
369 FIXME("sideways metrics are not supported.\n");
371 for (i = 0; i < glyph_count; i++) {
372 DWRITE_GLYPH_METRICS metrics;
374 hr = get_cached_glyph_metrics(This, glyphs[i], &metrics);
375 if (hr != S_OK) {
376 freetype_get_design_glyph_metrics(iface, This->metrics.designUnitsPerEm, glyphs[i], &metrics);
377 hr = set_cached_glyph_metrics(This, glyphs[i], &metrics);
378 if (FAILED(hr))
379 return hr;
381 ret[i] = metrics;
384 return S_OK;
387 static HRESULT WINAPI dwritefontface_GetGlyphIndices(IDWriteFontFace2 *iface, UINT32 const *codepoints,
388 UINT32 count, UINT16 *glyph_indices)
390 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
391 unsigned int i;
392 void *data;
394 TRACE("(%p)->(%p %u %p)\n", This, codepoints, count, glyph_indices);
396 data = get_fontface_cmap(This);
397 if (!data)
398 return E_FAIL;
400 for (i = 0; i < count; i++)
401 opentype_cmap_get_glyphindex(data, codepoints[i], &glyph_indices[i]);
403 return S_OK;
406 static HRESULT WINAPI dwritefontface_TryGetFontTable(IDWriteFontFace2 *iface, UINT32 table_tag,
407 const void **table_data, UINT32 *table_size, void **context, BOOL *exists)
409 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
411 TRACE("(%p)->(%u %p %p %p %p)\n", This, table_tag, table_data, table_size, context, exists);
413 return opentype_get_font_table(This->streams[0], This->type, This->index, table_tag, table_data, context, table_size, exists);
416 static void WINAPI dwritefontface_ReleaseFontTable(IDWriteFontFace2 *iface, void *table_context)
418 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
420 TRACE("(%p)->(%p)\n", This, table_context);
422 IDWriteFontFileStream_ReleaseFileFragment(This->streams[0], table_context);
425 HRESULT new_glyph_outline(UINT32 count, struct glyph_outline **ret)
427 struct glyph_outline *outline;
428 D2D1_POINT_2F *points;
429 UINT8 *tags;
431 *ret = NULL;
433 outline = heap_alloc(sizeof(*outline));
434 if (!outline)
435 return E_OUTOFMEMORY;
437 points = heap_alloc(count*sizeof(D2D1_POINT_2F));
438 tags = heap_alloc(count*sizeof(UINT8));
439 if (!points || !tags) {
440 heap_free(points);
441 heap_free(tags);
442 heap_free(outline);
443 return E_OUTOFMEMORY;
446 outline->points = points;
447 outline->tags = tags;
448 outline->count = count;
449 outline->advance = 0.0;
451 *ret = outline;
452 return S_OK;
455 static void free_glyph_outline(struct glyph_outline *outline)
457 heap_free(outline->points);
458 heap_free(outline->tags);
459 heap_free(outline);
462 static void report_glyph_outline(const struct glyph_outline *outline, IDWriteGeometrySink *sink)
464 UINT16 p;
466 for (p = 0; p < outline->count; p++) {
467 if (outline->tags[p] & OUTLINE_POINT_START) {
468 ID2D1SimplifiedGeometrySink_BeginFigure(sink, outline->points[p], D2D1_FIGURE_BEGIN_FILLED);
469 continue;
472 if (outline->tags[p] & OUTLINE_POINT_LINE)
473 ID2D1SimplifiedGeometrySink_AddLines(sink, outline->points+p, 1);
474 else if (outline->tags[p] & OUTLINE_POINT_BEZIER) {
475 static const UINT16 segment_length = 3;
476 ID2D1SimplifiedGeometrySink_AddBeziers(sink, (D2D1_BEZIER_SEGMENT*)&outline->points[p], 1);
477 p += segment_length - 1;
480 if (outline->tags[p] & OUTLINE_POINT_END)
481 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
485 static inline void translate_glyph_outline(struct glyph_outline *outline, FLOAT xoffset, FLOAT yoffset)
487 UINT16 p;
489 for (p = 0; p < outline->count; p++) {
490 outline->points[p].x += xoffset;
491 outline->points[p].y += yoffset;
495 static HRESULT WINAPI dwritefontface_GetGlyphRunOutline(IDWriteFontFace2 *iface, FLOAT emSize,
496 UINT16 const *glyphs, FLOAT const* advances, DWRITE_GLYPH_OFFSET const *offsets,
497 UINT32 count, BOOL is_sideways, BOOL is_rtl, IDWriteGeometrySink *sink)
499 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
500 FLOAT advance = 0.0;
501 HRESULT hr;
502 UINT32 g;
504 TRACE("(%p)->(%.2f %p %p %p %u %d %d %p)\n", This, emSize, glyphs, advances, offsets,
505 count, is_sideways, is_rtl, sink);
507 if (!glyphs || !sink)
508 return E_INVALIDARG;
510 if (is_sideways)
511 FIXME("sideways mode is not supported.\n");
513 for (g = 0; g < count; g++) {
514 FLOAT xoffset = 0.0, yoffset = 0.0;
515 struct glyph_outline *outline;
517 /* FIXME: cache outlines */
519 hr = freetype_get_glyph_outline(iface, emSize, glyphs[g], This->simulations, &outline);
520 if (FAILED(hr))
521 return hr;
523 /* glyph offsets act as current glyph adjustment */
524 if (offsets) {
525 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
526 yoffset -= offsets[g].ascenderOffset;
529 if (g == 0)
530 advance = is_rtl ? -outline->advance : 0.0;
532 xoffset += advance;
533 translate_glyph_outline(outline, xoffset, yoffset);
535 /* update advance to next glyph */
536 if (advances)
537 advance += is_rtl ? -advances[g] : advances[g];
538 else
539 advance += is_rtl ? -outline->advance : outline->advance;
541 report_glyph_outline(outline, sink);
542 free_glyph_outline(outline);
545 return S_OK;
548 static HRESULT WINAPI dwritefontface_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT emSize,
549 FLOAT pixels_per_dip, DWRITE_MEASURING_MODE mode, IDWriteRenderingParams* params, DWRITE_RENDERING_MODE* rendering_mode)
551 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
552 FIXME("(%p)->(%f %f %d %p %p): stub\n", This, emSize, pixels_per_dip, mode, params, rendering_mode);
553 return E_NOTIMPL;
556 static HRESULT WINAPI dwritefontface_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
557 DWRITE_MATRIX const *transform, DWRITE_FONT_METRICS *metrics)
559 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
560 DWRITE_FONT_METRICS1 metrics1;
561 HRESULT hr;
563 TRACE("(%p)->(%.2f %.2f %p %p)\n", This, emSize, pixels_per_dip, transform, metrics);
565 hr = IDWriteFontFace2_GetGdiCompatibleMetrics(iface, emSize, pixels_per_dip, transform, &metrics1);
566 if (FAILED(hr))
567 return hr;
569 memcpy(metrics, &metrics1, sizeof(*metrics));
570 return hr;
573 static HRESULT WINAPI dwritefontface_GetGdiCompatibleGlyphMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
574 DWRITE_MATRIX const *transform, BOOL use_gdi_natural, UINT16 const *glyph_indices, UINT32 glyph_count,
575 DWRITE_GLYPH_METRICS *metrics, BOOL is_sideways)
577 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
578 FIXME("(%p)->(%f %f %p %d %p %u %p %d): stub\n", This, emSize, pixels_per_dip, transform, use_gdi_natural, glyph_indices,
579 glyph_count, metrics, is_sideways);
580 return E_NOTIMPL;
583 static void WINAPI dwritefontface1_GetMetrics(IDWriteFontFace2 *iface, DWRITE_FONT_METRICS1 *metrics)
585 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
586 TRACE("(%p)->(%p)\n", This, metrics);
587 *metrics = This->metrics;
590 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT em_size, FLOAT pixels_per_dip,
591 const DWRITE_MATRIX *transform, DWRITE_FONT_METRICS1 *metrics)
593 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
594 FIXME("(%p)->(%f %f %p %p): stub\n", This, em_size, pixels_per_dip, transform, metrics);
595 return E_NOTIMPL;
598 static void WINAPI dwritefontface1_GetCaretMetrics(IDWriteFontFace2 *iface, DWRITE_CARET_METRICS *metrics)
600 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
601 TRACE("(%p)->(%p)\n", This, metrics);
602 *metrics = This->caret;
605 static HRESULT WINAPI dwritefontface1_GetUnicodeRanges(IDWriteFontFace2 *iface, UINT32 max_count,
606 DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
608 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
610 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
612 *count = 0;
613 if (max_count && !ranges)
614 return E_INVALIDARG;
616 return opentype_cmap_get_unicode_ranges(get_fontface_cmap(This), max_count, ranges, count);
619 static BOOL WINAPI dwritefontface1_IsMonospacedFont(IDWriteFontFace2 *iface)
621 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
622 TRACE("(%p)\n", This);
623 return freetype_is_monospaced(iface);
626 static HRESULT WINAPI dwritefontface1_GetDesignGlyphAdvances(IDWriteFontFace2 *iface,
627 UINT32 glyph_count, UINT16 const *glyphs, INT32 *advances, BOOL is_sideways)
629 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
630 UINT32 i;
632 TRACE("(%p)->(%u %p %p %d)\n", This, glyph_count, glyphs, advances, is_sideways);
634 for (i = 0; i < glyph_count; i++) {
635 DWRITE_GLYPH_METRICS metrics = { 0 };
636 HRESULT hr;
638 hr = IDWriteFontFace2_GetDesignGlyphMetrics(iface, glyphs + i, 1, &metrics, is_sideways);
639 if (FAILED(hr))
640 return hr;
642 advances[i] = is_sideways ? metrics.advanceHeight : metrics.advanceWidth;
645 return S_OK;
648 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleGlyphAdvances(IDWriteFontFace2 *iface,
649 FLOAT em_size, FLOAT pixels_per_dip, const DWRITE_MATRIX *transform, BOOL use_gdi_natural,
650 BOOL is_sideways, UINT32 glyph_count, UINT16 const *indices, INT32 *advances)
652 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
653 FIXME("(%p)->(%f %f %p %d %d %u %p %p): stub\n", This, em_size, pixels_per_dip, transform,
654 use_gdi_natural, is_sideways, glyph_count, indices, advances);
655 return E_NOTIMPL;
658 static HRESULT WINAPI dwritefontface1_GetKerningPairAdjustments(IDWriteFontFace2 *iface, UINT32 glyph_count,
659 const UINT16 *indices, INT32 *adjustments)
661 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
662 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, indices, adjustments);
663 return E_NOTIMPL;
666 static BOOL WINAPI dwritefontface1_HasKerningPairs(IDWriteFontFace2 *iface)
668 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
669 FIXME("(%p): stub\n", This);
670 return FALSE;
673 static HRESULT WINAPI dwritefontface1_GetRecommendedRenderingMode(IDWriteFontFace2 *iface,
674 FLOAT font_emsize, FLOAT dpiX, FLOAT dpiY, const DWRITE_MATRIX *transform, BOOL is_sideways,
675 DWRITE_OUTLINE_THRESHOLD threshold, DWRITE_MEASURING_MODE measuring_mode, DWRITE_RENDERING_MODE *rendering_mode)
677 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
678 FIXME("(%p)->(%f %f %f %p %d %d %d %p): stub\n", This, font_emsize, dpiX, dpiY, transform, is_sideways,
679 threshold, measuring_mode, rendering_mode);
680 return E_NOTIMPL;
683 static HRESULT WINAPI dwritefontface1_GetVerticalGlyphVariants(IDWriteFontFace2 *iface, UINT32 glyph_count,
684 const UINT16 *nominal_indices, UINT16 *vertical_indices)
686 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
687 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, nominal_indices, vertical_indices);
688 return E_NOTIMPL;
691 static BOOL WINAPI dwritefontface1_HasVerticalGlyphVariants(IDWriteFontFace2 *iface)
693 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
694 FIXME("(%p): stub\n", This);
695 return FALSE;
698 static BOOL WINAPI dwritefontface2_IsColorFont(IDWriteFontFace2 *iface)
700 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
701 FIXME("(%p): stub\n", This);
702 return FALSE;
705 static UINT32 WINAPI dwritefontface2_GetColorPaletteCount(IDWriteFontFace2 *iface)
707 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
708 FIXME("(%p): stub\n", This);
709 return 0;
712 static UINT32 WINAPI dwritefontface2_GetPaletteEntryCount(IDWriteFontFace2 *iface)
714 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
715 FIXME("(%p): stub\n", This);
716 return 0;
719 static HRESULT WINAPI dwritefontface2_GetPaletteEntries(IDWriteFontFace2 *iface, UINT32 palette_index,
720 UINT32 first_entry_index, UINT32 entry_count, DWRITE_COLOR_F *entries)
722 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
723 FIXME("(%p)->(%u %u %u %p): stub\n", This, palette_index, first_entry_index, entry_count, entries);
724 return E_NOTIMPL;
727 static HRESULT WINAPI dwritefontface2_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT fontEmSize,
728 FLOAT dpiX, FLOAT dpiY, DWRITE_MATRIX const *transform, BOOL is_sideways, DWRITE_OUTLINE_THRESHOLD threshold,
729 DWRITE_MEASURING_MODE measuringmode, IDWriteRenderingParams *params, DWRITE_RENDERING_MODE *renderingmode,
730 DWRITE_GRID_FIT_MODE *gridfitmode)
732 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
733 FIXME("(%p)->(%f %f %f %p %d %d %d %p %p %p): stub\n", This, fontEmSize, dpiX, dpiY, transform, is_sideways, threshold,
734 measuringmode, params, renderingmode, gridfitmode);
735 return E_NOTIMPL;
738 static const IDWriteFontFace2Vtbl dwritefontfacevtbl = {
739 dwritefontface_QueryInterface,
740 dwritefontface_AddRef,
741 dwritefontface_Release,
742 dwritefontface_GetType,
743 dwritefontface_GetFiles,
744 dwritefontface_GetIndex,
745 dwritefontface_GetSimulations,
746 dwritefontface_IsSymbolFont,
747 dwritefontface_GetMetrics,
748 dwritefontface_GetGlyphCount,
749 dwritefontface_GetDesignGlyphMetrics,
750 dwritefontface_GetGlyphIndices,
751 dwritefontface_TryGetFontTable,
752 dwritefontface_ReleaseFontTable,
753 dwritefontface_GetGlyphRunOutline,
754 dwritefontface_GetRecommendedRenderingMode,
755 dwritefontface_GetGdiCompatibleMetrics,
756 dwritefontface_GetGdiCompatibleGlyphMetrics,
757 dwritefontface1_GetMetrics,
758 dwritefontface1_GetGdiCompatibleMetrics,
759 dwritefontface1_GetCaretMetrics,
760 dwritefontface1_GetUnicodeRanges,
761 dwritefontface1_IsMonospacedFont,
762 dwritefontface1_GetDesignGlyphAdvances,
763 dwritefontface1_GetGdiCompatibleGlyphAdvances,
764 dwritefontface1_GetKerningPairAdjustments,
765 dwritefontface1_HasKerningPairs,
766 dwritefontface1_GetRecommendedRenderingMode,
767 dwritefontface1_GetVerticalGlyphVariants,
768 dwritefontface1_HasVerticalGlyphVariants,
769 dwritefontface2_IsColorFont,
770 dwritefontface2_GetColorPaletteCount,
771 dwritefontface2_GetPaletteEntryCount,
772 dwritefontface2_GetPaletteEntries,
773 dwritefontface2_GetRecommendedRenderingMode
776 HRESULT get_family_names_from_stream(IDWriteFontFileStream *stream, UINT32 index, DWRITE_FONT_FACE_TYPE facetype,
777 IDWriteLocalizedStrings **names)
779 const void *name_table = NULL;
780 void *name_context;
781 HRESULT hr = S_OK;
783 opentype_get_font_table(stream, facetype, index, MS_NAME_TAG, &name_table, &name_context, NULL, NULL);
784 if (name_table) {
785 hr = opentype_get_font_strings_from_id(name_table, DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES, names);
786 IDWriteFontFileStream_ReleaseFileFragment(stream, name_context);
788 else
789 names = NULL;
791 return hr;
794 static HRESULT get_fontface_from_font(struct dwrite_font *font, IDWriteFontFace2 **fontface)
796 struct dwrite_font_data *data = font->data;
797 IDWriteFontFace *face;
798 HRESULT hr;
800 *fontface = NULL;
802 hr = IDWriteFactory2_CreateFontFace(data->factory, data->face_type, 1, &data->file,
803 data->face_index, font->simulations, &face);
804 if (FAILED(hr))
805 return hr;
807 hr = IDWriteFontFace_QueryInterface(face, &IID_IDWriteFontFace2, (void**)fontface);
808 IDWriteFontFace_Release(face);
810 return hr;
813 static HRESULT WINAPI dwritefont_QueryInterface(IDWriteFont2 *iface, REFIID riid, void **obj)
815 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
817 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
819 if (IsEqualIID(riid, &IID_IDWriteFont2) ||
820 IsEqualIID(riid, &IID_IDWriteFont1) ||
821 IsEqualIID(riid, &IID_IDWriteFont) ||
822 IsEqualIID(riid, &IID_IUnknown))
824 *obj = iface;
825 IDWriteFont2_AddRef(iface);
826 return S_OK;
829 *obj = NULL;
830 return E_NOINTERFACE;
833 static ULONG WINAPI dwritefont_AddRef(IDWriteFont2 *iface)
835 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
836 ULONG ref = InterlockedIncrement(&This->ref);
837 TRACE("(%p)->(%d)\n", This, ref);
838 return ref;
841 static ULONG WINAPI dwritefont_Release(IDWriteFont2 *iface)
843 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
844 ULONG ref = InterlockedDecrement(&This->ref);
846 TRACE("(%p)->(%d)\n", This, ref);
848 if (!ref) {
849 IDWriteFontFamily_Release(This->family);
850 release_font_data(This->data);
851 heap_free(This);
854 return ref;
857 static HRESULT WINAPI dwritefont_GetFontFamily(IDWriteFont2 *iface, IDWriteFontFamily **family)
859 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
860 TRACE("(%p)->(%p)\n", This, family);
862 *family = This->family;
863 IDWriteFontFamily_AddRef(*family);
864 return S_OK;
867 static DWRITE_FONT_WEIGHT WINAPI dwritefont_GetWeight(IDWriteFont2 *iface)
869 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
870 TRACE("(%p)\n", This);
871 return This->data->weight;
874 static DWRITE_FONT_STRETCH WINAPI dwritefont_GetStretch(IDWriteFont2 *iface)
876 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
877 TRACE("(%p)\n", This);
878 return This->data->stretch;
881 static DWRITE_FONT_STYLE WINAPI dwritefont_GetStyle(IDWriteFont2 *iface)
883 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
884 TRACE("(%p)\n", This);
885 return This->style;
888 static BOOL WINAPI dwritefont_IsSymbolFont(IDWriteFont2 *iface)
890 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
891 IDWriteFontFace2 *fontface;
892 HRESULT hr;
894 TRACE("(%p)\n", This);
896 hr = get_fontface_from_font(This, &fontface);
897 if (FAILED(hr))
898 return hr;
900 return IDWriteFontFace2_IsSymbolFont(fontface);
903 static HRESULT WINAPI dwritefont_GetFaceNames(IDWriteFont2 *iface, IDWriteLocalizedStrings **names)
905 static const WCHAR boldobliqueW[] = {'B','o','l','d',' ','O','b','l','i','q','u','e',0};
906 static const WCHAR obliqueW[] = {'O','b','l','i','q','u','e',0};
907 static const WCHAR boldW[] = {'B','o','l','d',0};
908 static const WCHAR enusW[] = {'e','n','-','u','s',0};
910 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
911 IDWriteLocalizedStrings *strings;
912 const WCHAR *name;
913 HRESULT hr;
915 TRACE("(%p)->(%p)\n", This, names);
917 *names = NULL;
919 if (This->simulations == DWRITE_FONT_SIMULATIONS_NONE) {
920 BOOL exists;
921 return IDWriteFont2_GetInformationalStrings(iface, DWRITE_INFORMATIONAL_STRING_WIN32_SUBFAMILY_NAMES,
922 names, &exists);
925 switch (This->simulations) {
926 case DWRITE_FONT_SIMULATIONS_BOLD|DWRITE_FONT_SIMULATIONS_OBLIQUE:
927 name = boldobliqueW;
928 break;
929 case DWRITE_FONT_SIMULATIONS_BOLD:
930 name = boldW;
931 break;
932 case DWRITE_FONT_SIMULATIONS_OBLIQUE:
933 name = obliqueW;
934 break;
935 default:
936 ERR("unknown simulations %d\n", This->simulations);
937 return E_FAIL;
940 hr = create_localizedstrings(&strings);
941 if (FAILED(hr)) return hr;
943 hr = add_localizedstring(strings, enusW, name);
944 if (FAILED(hr)) {
945 IDWriteLocalizedStrings_Release(strings);
946 return hr;
949 *names = strings;
951 return S_OK;
954 static HRESULT WINAPI dwritefont_GetInformationalStrings(IDWriteFont2 *iface,
955 DWRITE_INFORMATIONAL_STRING_ID stringid, IDWriteLocalizedStrings **strings, BOOL *exists)
957 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
958 struct dwrite_font_data *data = This->data;
959 HRESULT hr;
961 TRACE("(%p)->(%d %p %p)\n", This, stringid, strings, exists);
963 *exists = FALSE;
964 *strings = NULL;
966 if (stringid > DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_CID_NAME || stringid == DWRITE_INFORMATIONAL_STRING_NONE)
967 return S_OK;
969 if (!data->info_strings[stringid]) {
970 IDWriteFontFace2 *fontface;
971 const void *table_data;
972 BOOL table_exists;
973 void *context;
974 UINT32 size;
976 hr = get_fontface_from_font(This, &fontface);
977 if (FAILED(hr))
978 return hr;
980 table_exists = FALSE;
981 hr = IDWriteFontFace2_TryGetFontTable(fontface, MS_NAME_TAG, &table_data, &size, &context, &table_exists);
982 if (FAILED(hr) || !table_exists)
983 WARN("no NAME table found.\n");
985 if (table_exists) {
986 hr = opentype_get_font_strings_from_id(table_data, stringid, &data->info_strings[stringid]);
987 if (FAILED(hr) || !data->info_strings[stringid])
988 return hr;
989 IDWriteFontFace2_ReleaseFontTable(fontface, context);
993 hr = clone_localizedstring(data->info_strings[stringid], strings);
994 if (FAILED(hr))
995 return hr;
997 *exists = TRUE;
998 return S_OK;
1001 static DWRITE_FONT_SIMULATIONS WINAPI dwritefont_GetSimulations(IDWriteFont2 *iface)
1003 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1004 TRACE("(%p)\n", This);
1005 return This->simulations;
1008 static void WINAPI dwritefont_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS *metrics)
1010 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1012 TRACE("(%p)->(%p)\n", This, metrics);
1013 memcpy(metrics, &This->data->metrics, sizeof(*metrics));
1016 static HRESULT WINAPI dwritefont_HasCharacter(IDWriteFont2 *iface, UINT32 value, BOOL *exists)
1018 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1019 IDWriteFontFace2 *fontface;
1020 UINT16 index;
1021 HRESULT hr;
1023 TRACE("(%p)->(0x%08x %p)\n", This, value, exists);
1025 *exists = FALSE;
1027 hr = get_fontface_from_font(This, &fontface);
1028 if (FAILED(hr))
1029 return hr;
1031 index = 0;
1032 hr = IDWriteFontFace2_GetGlyphIndices(fontface, &value, 1, &index);
1033 if (FAILED(hr))
1034 return hr;
1036 *exists = index != 0;
1037 return S_OK;
1040 static HRESULT WINAPI dwritefont_CreateFontFace(IDWriteFont2 *iface, IDWriteFontFace **face)
1042 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1043 HRESULT hr;
1045 TRACE("(%p)->(%p)\n", This, face);
1047 hr = get_fontface_from_font(This, (IDWriteFontFace2**)face);
1048 if (hr == S_OK)
1049 IDWriteFontFace_AddRef(*face);
1051 return hr;
1054 static void WINAPI dwritefont1_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS1 *metrics)
1056 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1057 TRACE("(%p)->(%p)\n", This, metrics);
1058 *metrics = This->data->metrics;
1061 static void WINAPI dwritefont1_GetPanose(IDWriteFont2 *iface, DWRITE_PANOSE *panose)
1063 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1064 FIXME("(%p)->(%p): stub\n", This, panose);
1067 static HRESULT WINAPI dwritefont1_GetUnicodeRanges(IDWriteFont2 *iface, UINT32 max_count, DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
1069 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1070 IDWriteFontFace2 *fontface;
1071 HRESULT hr;
1073 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
1075 hr = get_fontface_from_font(This, &fontface);
1076 if (FAILED(hr))
1077 return hr;
1079 return IDWriteFontFace2_GetUnicodeRanges(fontface, max_count, ranges, count);
1082 static BOOL WINAPI dwritefont1_IsMonospacedFont(IDWriteFont2 *iface)
1084 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1085 IDWriteFontFace2 *fontface;
1086 HRESULT hr;
1088 TRACE("(%p)\n", This);
1090 hr = get_fontface_from_font(This, &fontface);
1091 if (FAILED(hr))
1092 return hr;
1094 return IDWriteFontFace2_IsMonospacedFont(fontface);
1097 static HRESULT WINAPI dwritefont2_IsColorFont(IDWriteFont2 *iface)
1099 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1100 IDWriteFontFace2 *fontface;
1101 HRESULT hr;
1103 TRACE("(%p)\n", This);
1105 hr = get_fontface_from_font(This, &fontface);
1106 if (FAILED(hr))
1107 return hr;
1109 return IDWriteFontFace2_IsColorFont(fontface);
1112 static const IDWriteFont2Vtbl dwritefontvtbl = {
1113 dwritefont_QueryInterface,
1114 dwritefont_AddRef,
1115 dwritefont_Release,
1116 dwritefont_GetFontFamily,
1117 dwritefont_GetWeight,
1118 dwritefont_GetStretch,
1119 dwritefont_GetStyle,
1120 dwritefont_IsSymbolFont,
1121 dwritefont_GetFaceNames,
1122 dwritefont_GetInformationalStrings,
1123 dwritefont_GetSimulations,
1124 dwritefont_GetMetrics,
1125 dwritefont_HasCharacter,
1126 dwritefont_CreateFontFace,
1127 dwritefont1_GetMetrics,
1128 dwritefont1_GetPanose,
1129 dwritefont1_GetUnicodeRanges,
1130 dwritefont1_IsMonospacedFont,
1131 dwritefont2_IsColorFont
1134 static HRESULT create_font(struct dwrite_font_data *data, IDWriteFontFamily *family, DWRITE_FONT_SIMULATIONS simulations,
1135 IDWriteFont **font)
1137 struct dwrite_font *This;
1138 *font = NULL;
1140 This = heap_alloc(sizeof(struct dwrite_font));
1141 if (!This) return E_OUTOFMEMORY;
1143 This->IDWriteFont2_iface.lpVtbl = &dwritefontvtbl;
1144 This->ref = 1;
1145 This->family = family;
1146 IDWriteFontFamily_AddRef(family);
1147 This->simulations = simulations;
1148 This->style = data->style;
1149 This->data = data;
1150 InterlockedIncrement(&This->data->ref);
1152 /* set oblique style from requested simulation */
1153 if ((simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) && data->style == DWRITE_FONT_STYLE_NORMAL)
1154 This->style = DWRITE_FONT_STYLE_OBLIQUE;
1156 *font = (IDWriteFont*)&This->IDWriteFont2_iface;
1158 return S_OK;
1161 static HRESULT WINAPI dwritefontfamily_QueryInterface(IDWriteFontFamily *iface, REFIID riid, void **obj)
1163 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1164 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1166 if (IsEqualIID(riid, &IID_IUnknown) ||
1167 IsEqualIID(riid, &IID_IDWriteFontList) ||
1168 IsEqualIID(riid, &IID_IDWriteFontFamily))
1170 *obj = iface;
1171 IDWriteFontFamily_AddRef(iface);
1172 return S_OK;
1175 *obj = NULL;
1176 return E_NOINTERFACE;
1179 static ULONG WINAPI dwritefontfamily_AddRef(IDWriteFontFamily *iface)
1181 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1182 ULONG ref = InterlockedIncrement(&This->ref);
1183 TRACE("(%p)->(%d)\n", This, ref);
1184 return ref;
1187 static ULONG WINAPI dwritefontfamily_Release(IDWriteFontFamily *iface)
1189 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1190 ULONG ref = InterlockedDecrement(&This->ref);
1192 TRACE("(%p)->(%d)\n", This, ref);
1194 if (!ref)
1196 IDWriteFontCollection_Release(This->collection);
1197 release_fontfamily_data(This->data);
1198 heap_free(This);
1201 return ref;
1204 static HRESULT WINAPI dwritefontfamily_GetFontCollection(IDWriteFontFamily *iface, IDWriteFontCollection **collection)
1206 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1207 TRACE("(%p)->(%p)\n", This, collection);
1209 *collection = This->collection;
1210 IDWriteFontCollection_AddRef(This->collection);
1211 return S_OK;
1214 static UINT32 WINAPI dwritefontfamily_GetFontCount(IDWriteFontFamily *iface)
1216 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1217 TRACE("(%p)\n", This);
1218 return This->data->font_count;
1221 static HRESULT WINAPI dwritefontfamily_GetFont(IDWriteFontFamily *iface, UINT32 index, IDWriteFont **font)
1223 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1225 TRACE("(%p)->(%u %p)\n", This, index, font);
1227 *font = NULL;
1229 if (This->data->font_count == 0)
1230 return S_FALSE;
1232 if (index >= This->data->font_count)
1233 return E_INVALIDARG;
1235 return create_font(This->data->fonts[index], iface, DWRITE_FONT_SIMULATIONS_NONE, font);
1238 static HRESULT WINAPI dwritefontfamily_GetFamilyNames(IDWriteFontFamily *iface, IDWriteLocalizedStrings **names)
1240 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1241 return clone_localizedstring(This->data->familyname, names);
1244 static inline BOOL is_matching_font_style(DWRITE_FONT_STYLE style, DWRITE_FONT_STYLE font_style)
1246 if (style == font_style)
1247 return TRUE;
1249 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) && font_style == DWRITE_FONT_STYLE_NORMAL)
1250 return TRUE;
1252 return FALSE;
1255 static HRESULT WINAPI dwritefontfamily_GetFirstMatchingFont(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1256 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFont **font)
1258 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1259 UINT32 min_weight_diff = ~0u;
1260 int found = -1, i;
1262 TRACE("(%p)->(%d %d %d %p)\n", This, weight, stretch, style, font);
1264 for (i = 0; i < This->data->font_count; i++) {
1265 if (is_matching_font_style(style, This->data->fonts[i]->style) && stretch == This->data->fonts[i]->stretch) {
1266 DWRITE_FONT_WEIGHT font_weight = This->data->fonts[i]->weight;
1267 UINT32 weight_diff = abs(font_weight - weight);
1268 if (weight_diff < min_weight_diff) {
1269 min_weight_diff = weight_diff;
1270 found = i;
1275 if (found != -1) {
1276 DWRITE_FONT_SIMULATIONS simulations = DWRITE_FONT_SIMULATIONS_NONE;
1278 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) &&
1279 This->data->fonts[found]->style == DWRITE_FONT_STYLE_NORMAL) {
1280 simulations = DWRITE_FONT_SIMULATIONS_OBLIQUE;
1282 return create_font(This->data->fonts[found], iface, simulations, font);
1284 else {
1285 *font = NULL;
1286 return DWRITE_E_NOFONT;
1290 static HRESULT WINAPI dwritefontfamily_GetMatchingFonts(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1291 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFontList **fonts)
1293 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1294 FIXME("(%p)->(%d %d %d %p): stub\n", This, weight, stretch, style, fonts);
1295 return E_NOTIMPL;
1298 static const IDWriteFontFamilyVtbl fontfamilyvtbl = {
1299 dwritefontfamily_QueryInterface,
1300 dwritefontfamily_AddRef,
1301 dwritefontfamily_Release,
1302 dwritefontfamily_GetFontCollection,
1303 dwritefontfamily_GetFontCount,
1304 dwritefontfamily_GetFont,
1305 dwritefontfamily_GetFamilyNames,
1306 dwritefontfamily_GetFirstMatchingFont,
1307 dwritefontfamily_GetMatchingFonts
1310 static HRESULT create_fontfamily(struct dwrite_fontfamily_data *data, IDWriteFontCollection *collection, IDWriteFontFamily **family)
1312 struct dwrite_fontfamily *This;
1314 *family = NULL;
1316 This = heap_alloc(sizeof(struct dwrite_fontfamily));
1317 if (!This) return E_OUTOFMEMORY;
1319 This->IDWriteFontFamily_iface.lpVtbl = &fontfamilyvtbl;
1320 This->ref = 1;
1321 This->collection = collection;
1322 IDWriteFontCollection_AddRef(collection);
1323 This->data = data;
1324 InterlockedIncrement(&This->data->ref);
1326 *family = &This->IDWriteFontFamily_iface;
1328 return S_OK;
1331 BOOL is_system_collection(IDWriteFontCollection *collection)
1333 void *obj;
1334 return IDWriteFontCollection_QueryInterface(collection, &IID_issystemcollection, (void**)&obj) == S_OK;
1337 static HRESULT WINAPI dwritefontcollection_QueryInterface(IDWriteFontCollection *iface, REFIID riid, void **obj)
1339 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1340 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1342 if (IsEqualIID(riid, &IID_IUnknown) ||
1343 IsEqualIID(riid, &IID_IDWriteFontCollection))
1345 *obj = iface;
1346 IDWriteFontCollection_AddRef(iface);
1347 return S_OK;
1350 *obj = NULL;
1352 if (This->is_system && IsEqualIID(riid, &IID_issystemcollection))
1353 return S_OK;
1355 return E_NOINTERFACE;
1358 static ULONG WINAPI dwritefontcollection_AddRef(IDWriteFontCollection *iface)
1360 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1361 ULONG ref = InterlockedIncrement(&This->ref);
1362 TRACE("(%p)->(%d)\n", This, ref);
1363 return ref;
1366 static ULONG WINAPI dwritefontcollection_Release(IDWriteFontCollection *iface)
1368 unsigned int i;
1369 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1370 ULONG ref = InterlockedDecrement(&This->ref);
1371 TRACE("(%p)->(%d)\n", This, ref);
1373 if (!ref) {
1374 for (i = 0; i < This->family_count; i++)
1375 release_fontfamily_data(This->family_data[i]);
1376 heap_free(This->family_data);
1377 heap_free(This);
1380 return ref;
1383 static UINT32 WINAPI dwritefontcollection_GetFontFamilyCount(IDWriteFontCollection *iface)
1385 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1386 TRACE("(%p)\n", This);
1387 return This->family_count;
1390 static HRESULT WINAPI dwritefontcollection_GetFontFamily(IDWriteFontCollection *iface, UINT32 index, IDWriteFontFamily **family)
1392 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1394 TRACE("(%p)->(%u %p)\n", This, index, family);
1396 if (index >= This->family_count) {
1397 *family = NULL;
1398 return E_FAIL;
1401 return create_fontfamily(This->family_data[index], iface, family);
1404 static UINT32 collection_find_family(struct dwrite_fontcollection *collection, const WCHAR *name)
1406 UINT32 i;
1408 for (i = 0; i < collection->family_count; i++) {
1409 IDWriteLocalizedStrings *family_name = collection->family_data[i]->familyname;
1410 HRESULT hr;
1411 int j;
1413 for (j = 0; j < IDWriteLocalizedStrings_GetCount(family_name); j++) {
1414 WCHAR buffer[255];
1415 hr = IDWriteLocalizedStrings_GetString(family_name, j, buffer, 255);
1416 if (SUCCEEDED(hr) && !strcmpW(buffer, name))
1417 return i;
1421 return ~0u;
1424 static HRESULT WINAPI dwritefontcollection_FindFamilyName(IDWriteFontCollection *iface, const WCHAR *name, UINT32 *index, BOOL *exists)
1426 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1427 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(name), index, exists);
1428 *index = collection_find_family(This, name);
1429 *exists = *index != ~0u;
1430 return S_OK;
1433 static BOOL is_same_fontfile(IDWriteFontFile *left, IDWriteFontFile *right)
1435 UINT32 left_key_size, right_key_size;
1436 const void *left_key, *right_key;
1437 HRESULT hr;
1439 if (left == right)
1440 return TRUE;
1442 hr = IDWriteFontFile_GetReferenceKey(left, &left_key, &left_key_size);
1443 if (FAILED(hr))
1444 return FALSE;
1446 hr = IDWriteFontFile_GetReferenceKey(right, &right_key, &right_key_size);
1447 if (FAILED(hr))
1448 return FALSE;
1450 if (left_key_size != right_key_size)
1451 return FALSE;
1453 return !memcmp(left_key, right_key, left_key_size);
1456 static HRESULT WINAPI dwritefontcollection_GetFontFromFontFace(IDWriteFontCollection *iface, IDWriteFontFace *face, IDWriteFont **font)
1458 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1459 struct dwrite_fontfamily_data *found_family = NULL;
1460 struct dwrite_font_data *found_font = NULL;
1461 DWRITE_FONT_SIMULATIONS simulations;
1462 IDWriteFontFamily *family;
1463 UINT32 i, j, face_index;
1464 IDWriteFontFile *file;
1465 HRESULT hr;
1467 TRACE("(%p)->(%p %p)\n", This, face, font);
1469 *font = NULL;
1471 if (!face)
1472 return E_INVALIDARG;
1474 i = 1;
1475 hr = IDWriteFontFace_GetFiles(face, &i, &file);
1476 if (FAILED(hr))
1477 return hr;
1478 face_index = IDWriteFontFace_GetIndex(face);
1480 for (i = 0; i < This->family_count; i++) {
1481 struct dwrite_fontfamily_data *family_data = This->family_data[i];
1482 for (j = 0; j < family_data->font_count; j++) {
1483 struct dwrite_font_data *font_data = family_data->fonts[j];
1485 if (face_index == font_data->face_index && is_same_fontfile(file, font_data->file)) {
1486 found_font = font_data;
1487 found_family = family_data;
1488 break;
1493 if (!found_font)
1494 return DWRITE_E_NOFONT;
1496 hr = create_fontfamily(found_family, iface, &family);
1497 if (FAILED(hr))
1498 return hr;
1500 simulations = IDWriteFontFace_GetSimulations(face);
1501 hr = create_font(found_font, family, simulations, font);
1502 IDWriteFontFamily_Release(family);
1503 return hr;
1506 static const IDWriteFontCollectionVtbl fontcollectionvtbl = {
1507 dwritefontcollection_QueryInterface,
1508 dwritefontcollection_AddRef,
1509 dwritefontcollection_Release,
1510 dwritefontcollection_GetFontFamilyCount,
1511 dwritefontcollection_GetFontFamily,
1512 dwritefontcollection_FindFamilyName,
1513 dwritefontcollection_GetFontFromFontFace
1516 static HRESULT fontfamily_add_font(struct dwrite_fontfamily_data *family_data, struct dwrite_font_data *font_data)
1518 if (family_data->font_count + 1 >= family_data->font_alloc) {
1519 struct dwrite_font_data **new_list;
1520 UINT32 new_alloc;
1522 new_alloc = family_data->font_alloc * 2;
1523 new_list = heap_realloc(family_data->fonts, sizeof(*family_data->fonts) * new_alloc);
1524 if (!new_list)
1525 return E_OUTOFMEMORY;
1526 family_data->fonts = new_list;
1527 family_data->font_alloc = new_alloc;
1530 family_data->fonts[family_data->font_count] = font_data;
1531 family_data->font_count++;
1532 return S_OK;
1535 static HRESULT fontcollection_add_family(struct dwrite_fontcollection *collection, struct dwrite_fontfamily_data *family)
1537 if (collection->family_alloc < collection->family_count + 1) {
1538 struct dwrite_fontfamily_data **new_list;
1539 UINT32 new_alloc;
1541 new_alloc = collection->family_alloc * 2;
1542 new_list = heap_realloc(collection->family_data, sizeof(*new_list) * new_alloc);
1543 if (!new_list)
1544 return E_OUTOFMEMORY;
1546 collection->family_alloc = new_alloc;
1547 collection->family_data = new_list;
1550 collection->family_data[collection->family_count] = family;
1551 collection->family_count++;
1553 return S_OK;
1556 static HRESULT init_font_collection(struct dwrite_fontcollection *collection, BOOL is_system)
1558 collection->IDWriteFontCollection_iface.lpVtbl = &fontcollectionvtbl;
1559 collection->ref = 1;
1560 collection->family_count = 0;
1561 collection->family_alloc = 2;
1562 collection->is_system = is_system;
1564 collection->family_data = heap_alloc(sizeof(*collection->family_data)*2);
1565 if (!collection->family_data)
1566 return E_OUTOFMEMORY;
1568 return S_OK;
1571 HRESULT get_filestream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
1573 IDWriteFontFileLoader *loader;
1574 const void *key;
1575 UINT32 key_size;
1576 HRESULT hr;
1578 *stream = NULL;
1580 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
1581 if (FAILED(hr))
1582 return hr;
1584 hr = IDWriteFontFile_GetLoader(file, &loader);
1585 if (FAILED(hr))
1586 return hr;
1588 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
1589 IDWriteFontFileLoader_Release(loader);
1590 if (FAILED(hr))
1591 return hr;
1593 return hr;
1596 static HRESULT init_font_data(IDWriteFactory2 *factory, IDWriteFontFile *file, UINT32 face_index, DWRITE_FONT_FACE_TYPE face_type,
1597 IDWriteFontFileStream **stream, struct dwrite_font_data **ret)
1599 void *os2_context, *head_context;
1600 const void *tt_os2 = NULL, *tt_head = NULL;
1601 struct dwrite_font_data *data;
1602 HRESULT hr;
1604 data = heap_alloc_zero(sizeof(*data));
1605 if (!data)
1606 return E_OUTOFMEMORY;
1608 hr = get_filestream_from_file(file, stream);
1609 if (FAILED(hr)) {
1610 heap_free(data);
1611 return hr;
1614 data->ref = 1;
1615 data->factory = factory;
1616 data->file = file;
1617 data->face_index = face_index;
1618 data->face_type = face_type;
1619 IDWriteFontFile_AddRef(file);
1620 IDWriteFactory2_AddRef(factory);
1622 opentype_get_font_table(*stream, face_type, face_index, MS_OS2_TAG, &tt_os2, &os2_context, NULL, NULL);
1623 opentype_get_font_table(*stream, face_type, face_index, MS_HEAD_TAG, &tt_head, &head_context, NULL, NULL);
1625 opentype_get_font_properties(*stream, face_type, face_index, &data->stretch, &data->weight, &data->style);
1626 opentype_get_font_metrics(*stream, face_type, face_index, &data->metrics, NULL);
1628 if (tt_os2)
1629 IDWriteFontFileStream_ReleaseFileFragment(*stream, os2_context);
1630 if (tt_head)
1631 IDWriteFontFileStream_ReleaseFileFragment(*stream, head_context);
1633 *ret = data;
1634 return S_OK;
1637 static HRESULT init_fontfamily_data(IDWriteLocalizedStrings *familyname, struct dwrite_fontfamily_data **ret)
1639 struct dwrite_fontfamily_data *data;
1641 data = heap_alloc(sizeof(*data));
1642 if (!data)
1643 return E_OUTOFMEMORY;
1645 data->ref = 1;
1646 data->font_count = 0;
1647 data->font_alloc = 2;
1649 data->fonts = heap_alloc(sizeof(*data->fonts)*data->font_alloc);
1650 if (!data->fonts) {
1651 heap_free(data);
1652 return E_OUTOFMEMORY;
1655 data->familyname = familyname;
1656 IDWriteLocalizedStrings_AddRef(familyname);
1658 *ret = data;
1659 return S_OK;
1662 HRESULT create_font_collection(IDWriteFactory2* factory, IDWriteFontFileEnumerator *enumerator, BOOL is_system, IDWriteFontCollection **ret)
1664 struct dwrite_fontcollection *collection;
1665 BOOL current = FALSE;
1666 HRESULT hr = S_OK;
1668 *ret = NULL;
1670 collection = heap_alloc(sizeof(struct dwrite_fontcollection));
1671 if (!collection) return E_OUTOFMEMORY;
1673 hr = init_font_collection(collection, is_system);
1674 if (FAILED(hr)) {
1675 heap_free(collection);
1676 return hr;
1679 *ret = &collection->IDWriteFontCollection_iface;
1681 TRACE("building font collection:\n");
1683 while (hr == S_OK) {
1684 DWRITE_FONT_FACE_TYPE face_type;
1685 DWRITE_FONT_FILE_TYPE file_type;
1686 IDWriteFontFile *file;
1687 UINT32 face_count;
1688 BOOL supported;
1689 int i;
1691 current = FALSE;
1692 hr = IDWriteFontFileEnumerator_MoveNext(enumerator, &current);
1693 if (FAILED(hr) || !current)
1694 break;
1696 hr = IDWriteFontFileEnumerator_GetCurrentFontFile(enumerator, &file);
1697 if (FAILED(hr))
1698 break;
1700 hr = IDWriteFontFile_Analyze(file, &supported, &file_type, &face_type, &face_count);
1701 if (FAILED(hr) || !supported || face_count == 0) {
1702 TRACE("unsupported font (0x%08x, %d, %u)\n", hr, supported, face_count);
1703 IDWriteFontFile_Release(file);
1704 continue;
1707 for (i = 0; i < face_count; i++) {
1708 IDWriteLocalizedStrings *family_name = NULL;
1709 struct dwrite_font_data *font_data;
1710 IDWriteFontFileStream *stream;
1711 WCHAR buffer[255];
1712 UINT32 index;
1714 /* alloc and init new font data structure */
1715 hr = init_font_data(factory, file, i, face_type, &stream, &font_data);
1716 if (FAILED(hr))
1717 break;
1719 /* get family name from font file */
1720 hr = get_family_names_from_stream(stream, i, face_type, &family_name);
1721 IDWriteFontFileStream_Release(stream);
1722 if (FAILED(hr)) {
1723 WARN("unable to get family name from font\n");
1724 release_font_data(font_data);
1725 continue;
1728 buffer[0] = 0;
1729 IDWriteLocalizedStrings_GetString(family_name, 0, buffer, sizeof(buffer)/sizeof(WCHAR));
1731 index = collection_find_family(collection, buffer);
1732 if (index != ~0u)
1733 hr = fontfamily_add_font(collection->family_data[index], font_data);
1734 else {
1735 struct dwrite_fontfamily_data *family_data;
1737 /* create and init new family */
1738 hr = init_fontfamily_data(family_name, &family_data);
1739 if (hr == S_OK) {
1740 /* add font to family, family - to collection */
1741 hr = fontfamily_add_font(family_data, font_data);
1742 if (hr == S_OK)
1743 hr = fontcollection_add_family(collection, family_data);
1745 if (FAILED(hr))
1746 release_fontfamily_data(family_data);
1750 IDWriteLocalizedStrings_Release(family_name);
1752 if (FAILED(hr))
1753 break;
1756 IDWriteFontFile_Release(file);
1759 return hr;
1762 struct system_fontfile_enumerator
1764 IDWriteFontFileEnumerator IDWriteFontFileEnumerator_iface;
1765 LONG ref;
1767 IDWriteFactory2 *factory;
1768 HKEY hkey;
1769 int index;
1772 static inline struct system_fontfile_enumerator *impl_from_IDWriteFontFileEnumerator(IDWriteFontFileEnumerator* iface)
1774 return CONTAINING_RECORD(iface, struct system_fontfile_enumerator, IDWriteFontFileEnumerator_iface);
1777 static HRESULT WINAPI systemfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1779 *obj = NULL;
1781 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1782 IDWriteFontFileEnumerator_AddRef(iface);
1783 *obj = iface;
1784 return S_OK;
1787 return E_NOINTERFACE;
1790 static ULONG WINAPI systemfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1792 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1793 return InterlockedIncrement(&enumerator->ref);
1796 static ULONG WINAPI systemfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1798 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1799 ULONG ref = InterlockedDecrement(&enumerator->ref);
1801 if (!ref) {
1802 IDWriteFactory2_Release(enumerator->factory);
1803 RegCloseKey(enumerator->hkey);
1804 heap_free(enumerator);
1807 return ref;
1810 static HRESULT WINAPI systemfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1812 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1813 DWORD ret, type, count;
1814 WCHAR *filename;
1815 HRESULT hr;
1817 *file = NULL;
1819 if (enumerator->index < 0)
1820 return E_FAIL;
1822 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1823 return E_FAIL;
1825 if (!(filename = heap_alloc(count)))
1826 return E_OUTOFMEMORY;
1828 ret = RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, (BYTE*)filename, &count);
1829 if (ret) {
1830 heap_free(filename);
1831 return E_FAIL;
1834 /* Fonts installed in 'Fonts' system dir don't get full path in registry font files cache */
1835 if (!strchrW(filename, '\\')) {
1836 static const WCHAR fontsW[] = {'\\','f','o','n','t','s','\\',0};
1837 WCHAR fullpathW[MAX_PATH];
1839 GetWindowsDirectoryW(fullpathW, sizeof(fullpathW)/sizeof(WCHAR));
1840 strcatW(fullpathW, fontsW);
1841 strcatW(fullpathW, filename);
1843 hr = IDWriteFactory2_CreateFontFileReference(enumerator->factory, fullpathW, NULL, file);
1845 else
1846 hr = IDWriteFactory2_CreateFontFileReference(enumerator->factory, filename, NULL, file);
1848 heap_free(filename);
1849 return hr;
1852 static HRESULT WINAPI systemfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1854 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1856 *current = FALSE;
1857 enumerator->index++;
1859 /* iterate until we find next string value */
1860 while (1) {
1861 DWORD type = 0, count;
1862 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1863 break;
1864 if (type == REG_SZ) {
1865 *current = TRUE;
1866 break;
1868 enumerator->index++;
1871 TRACE("index = %d, current = %d\n", enumerator->index, *current);
1872 return S_OK;
1875 static const struct IDWriteFontFileEnumeratorVtbl systemfontfileenumeratorvtbl =
1877 systemfontfileenumerator_QueryInterface,
1878 systemfontfileenumerator_AddRef,
1879 systemfontfileenumerator_Release,
1880 systemfontfileenumerator_MoveNext,
1881 systemfontfileenumerator_GetCurrentFontFile
1884 static HRESULT create_system_fontfile_enumerator(IDWriteFactory2 *factory, IDWriteFontFileEnumerator **ret)
1886 struct system_fontfile_enumerator *enumerator;
1887 static const WCHAR fontslistW[] = {
1888 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1889 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1890 'F','o','n','t','s',0
1893 *ret = NULL;
1895 enumerator = heap_alloc(sizeof(*enumerator));
1896 if (!enumerator)
1897 return E_OUTOFMEMORY;
1899 enumerator->IDWriteFontFileEnumerator_iface.lpVtbl = &systemfontfileenumeratorvtbl;
1900 enumerator->ref = 1;
1901 enumerator->factory = factory;
1902 enumerator->index = -1;
1903 IDWriteFactory2_AddRef(factory);
1905 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, fontslistW, 0, GENERIC_READ, &enumerator->hkey)) {
1906 ERR("failed to open fonts list key\n");
1907 IDWriteFactory2_Release(factory);
1908 heap_free(enumerator);
1909 return E_FAIL;
1912 *ret = &enumerator->IDWriteFontFileEnumerator_iface;
1914 return S_OK;
1917 HRESULT get_system_fontcollection(IDWriteFactory2 *factory, IDWriteFontCollection **collection)
1919 IDWriteFontFileEnumerator *enumerator;
1920 HRESULT hr;
1922 *collection = NULL;
1924 hr = create_system_fontfile_enumerator(factory, &enumerator);
1925 if (FAILED(hr))
1926 return hr;
1928 TRACE("building system font collection for factory %p\n", factory);
1929 hr = create_font_collection(factory, enumerator, TRUE, collection);
1930 IDWriteFontFileEnumerator_Release(enumerator);
1931 return hr;
1934 static HRESULT WINAPI eudcfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1936 *obj = NULL;
1938 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1939 IDWriteFontFileEnumerator_AddRef(iface);
1940 *obj = iface;
1941 return S_OK;
1944 return E_NOINTERFACE;
1947 static ULONG WINAPI eudcfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1949 return 2;
1952 static ULONG WINAPI eudcfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1954 return 1;
1957 static HRESULT WINAPI eudcfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1959 *file = NULL;
1960 return E_FAIL;
1963 static HRESULT WINAPI eudcfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1965 *current = FALSE;
1966 return S_OK;
1969 static const struct IDWriteFontFileEnumeratorVtbl eudcfontfileenumeratorvtbl =
1971 eudcfontfileenumerator_QueryInterface,
1972 eudcfontfileenumerator_AddRef,
1973 eudcfontfileenumerator_Release,
1974 eudcfontfileenumerator_MoveNext,
1975 eudcfontfileenumerator_GetCurrentFontFile
1978 static IDWriteFontFileEnumerator eudc_fontfile_enumerator = { &eudcfontfileenumeratorvtbl };
1980 HRESULT get_eudc_fontcollection(IDWriteFactory2 *factory, IDWriteFontCollection **collection)
1982 TRACE("building EUDC font collection for factory %p\n", factory);
1983 return create_font_collection(factory, &eudc_fontfile_enumerator, FALSE, collection);
1986 static HRESULT WINAPI dwritefontfile_QueryInterface(IDWriteFontFile *iface, REFIID riid, void **obj)
1988 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1990 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1992 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFile))
1994 *obj = iface;
1995 IDWriteFontFile_AddRef(iface);
1996 return S_OK;
1999 *obj = NULL;
2000 return E_NOINTERFACE;
2003 static ULONG WINAPI dwritefontfile_AddRef(IDWriteFontFile *iface)
2005 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2006 ULONG ref = InterlockedIncrement(&This->ref);
2007 TRACE("(%p)->(%d)\n", This, ref);
2008 return ref;
2011 static ULONG WINAPI dwritefontfile_Release(IDWriteFontFile *iface)
2013 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2014 ULONG ref = InterlockedDecrement(&This->ref);
2016 TRACE("(%p)->(%d)\n", This, ref);
2018 if (!ref)
2020 IDWriteFontFileLoader_Release(This->loader);
2021 if (This->stream) IDWriteFontFileStream_Release(This->stream);
2022 heap_free(This->reference_key);
2023 heap_free(This);
2026 return ref;
2029 static HRESULT WINAPI dwritefontfile_GetReferenceKey(IDWriteFontFile *iface, const void **fontFileReferenceKey, UINT32 *fontFileReferenceKeySize)
2031 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2032 TRACE("(%p)->(%p, %p)\n", This, fontFileReferenceKey, fontFileReferenceKeySize);
2033 *fontFileReferenceKey = This->reference_key;
2034 *fontFileReferenceKeySize = This->key_size;
2036 return S_OK;
2039 static HRESULT WINAPI dwritefontfile_GetLoader(IDWriteFontFile *iface, IDWriteFontFileLoader **fontFileLoader)
2041 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2042 TRACE("(%p)->(%p)\n", This, fontFileLoader);
2043 *fontFileLoader = This->loader;
2044 IDWriteFontFileLoader_AddRef(This->loader);
2046 return S_OK;
2049 static HRESULT WINAPI dwritefontfile_Analyze(IDWriteFontFile *iface, BOOL *isSupportedFontType, DWRITE_FONT_FILE_TYPE *fontFileType, DWRITE_FONT_FACE_TYPE *fontFaceType, UINT32 *numberOfFaces)
2051 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2052 IDWriteFontFileStream *stream;
2053 HRESULT hr;
2055 TRACE("(%p)->(%p, %p, %p, %p)\n", This, isSupportedFontType, fontFileType, fontFaceType, numberOfFaces);
2057 *isSupportedFontType = FALSE;
2058 *fontFileType = DWRITE_FONT_FILE_TYPE_UNKNOWN;
2059 if (fontFaceType)
2060 *fontFaceType = DWRITE_FONT_FACE_TYPE_UNKNOWN;
2061 *numberOfFaces = 0;
2063 hr = IDWriteFontFileLoader_CreateStreamFromKey(This->loader, This->reference_key, This->key_size, &stream);
2064 if (FAILED(hr))
2065 return S_OK;
2067 hr = opentype_analyze_font(stream, numberOfFaces, fontFileType, fontFaceType, isSupportedFontType);
2069 /* TODO: Further Analysis */
2070 IDWriteFontFileStream_Release(stream);
2071 return S_OK;
2074 static const IDWriteFontFileVtbl dwritefontfilevtbl = {
2075 dwritefontfile_QueryInterface,
2076 dwritefontfile_AddRef,
2077 dwritefontfile_Release,
2078 dwritefontfile_GetReferenceKey,
2079 dwritefontfile_GetLoader,
2080 dwritefontfile_Analyze,
2083 HRESULT create_font_file(IDWriteFontFileLoader *loader, const void *reference_key, UINT32 key_size, IDWriteFontFile **font_file)
2085 struct dwrite_fontfile *This;
2087 This = heap_alloc(sizeof(struct dwrite_fontfile));
2088 if (!This) return E_OUTOFMEMORY;
2090 This->IDWriteFontFile_iface.lpVtbl = &dwritefontfilevtbl;
2091 This->ref = 1;
2092 IDWriteFontFileLoader_AddRef(loader);
2093 This->loader = loader;
2094 This->stream = NULL;
2095 This->reference_key = heap_alloc(key_size);
2096 memcpy(This->reference_key, reference_key, key_size);
2097 This->key_size = key_size;
2099 *font_file = &This->IDWriteFontFile_iface;
2101 return S_OK;
2104 static HRESULT get_stream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
2106 IDWriteFontFileLoader *loader;
2107 UINT32 key_size;
2108 const void *key;
2109 HRESULT hr;
2111 *stream = NULL;
2112 hr = IDWriteFontFile_GetLoader(file, &loader);
2113 if (FAILED(hr))
2114 return hr;
2116 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
2117 if (FAILED(hr)) {
2118 IDWriteFontFileLoader_Release(loader);
2119 return hr;
2122 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
2123 IDWriteFontFileLoader_Release(loader);
2125 return hr;
2128 HRESULT create_fontface(DWRITE_FONT_FACE_TYPE facetype, UINT32 files_number, IDWriteFontFile* const* font_files, UINT32 index,
2129 DWRITE_FONT_SIMULATIONS simulations, IDWriteFontFace2 **ret)
2131 struct dwrite_fontface *fontface;
2132 HRESULT hr = S_OK;
2133 int i;
2135 *ret = NULL;
2137 fontface = heap_alloc(sizeof(struct dwrite_fontface));
2138 if (!fontface)
2139 return E_OUTOFMEMORY;
2141 fontface->files = heap_alloc_zero(sizeof(*fontface->files) * files_number);
2142 fontface->streams = heap_alloc_zero(sizeof(*fontface->streams) * files_number);
2144 if (!fontface->files || !fontface->streams) {
2145 heap_free(fontface->files);
2146 heap_free(fontface->streams);
2147 heap_free(fontface);
2148 return E_OUTOFMEMORY;
2151 fontface->IDWriteFontFace2_iface.lpVtbl = &dwritefontfacevtbl;
2152 fontface->ref = 1;
2153 fontface->type = facetype;
2154 fontface->file_count = files_number;
2155 fontface->cmap.data = NULL;
2156 fontface->cmap.context = NULL;
2157 fontface->cmap.size = 0;
2158 fontface->index = index;
2159 fontface->simulations = simulations;
2160 memset(fontface->glyphs, 0, sizeof(fontface->glyphs));
2162 for (i = 0; i < fontface->file_count; i++) {
2163 hr = get_stream_from_file(font_files[i], &fontface->streams[i]);
2164 if (FAILED(hr)) {
2165 IDWriteFontFace2_Release(&fontface->IDWriteFontFace2_iface);
2166 return hr;
2169 fontface->files[i] = font_files[i];
2170 IDWriteFontFile_AddRef(font_files[i]);
2173 opentype_get_font_metrics(fontface->streams[0], facetype, index, &fontface->metrics, &fontface->caret);
2174 if (simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
2175 /* TODO: test what happens if caret is already slanted */
2176 if (fontface->caret.slopeRise == 1) {
2177 fontface->caret.slopeRise = fontface->metrics.designUnitsPerEm;
2178 fontface->caret.slopeRun = fontface->caret.slopeRise / 3;
2182 *ret = &fontface->IDWriteFontFace2_iface;
2183 return S_OK;
2186 /* IDWriteLocalFontFileLoader and its required IDWriteFontFileStream */
2187 struct local_refkey
2189 FILETIME writetime;
2190 WCHAR name[1];
2193 struct local_cached_stream
2195 struct list entry;
2196 IDWriteFontFileStream *stream;
2197 struct local_refkey *key;
2198 UINT32 key_size;
2201 struct dwrite_localfontfilestream
2203 IDWriteFontFileStream IDWriteFontFileStream_iface;
2204 LONG ref;
2206 struct local_cached_stream *entry;
2207 const void *file_ptr;
2208 UINT64 size;
2211 struct dwrite_localfontfileloader {
2212 IDWriteLocalFontFileLoader IDWriteLocalFontFileLoader_iface;
2213 LONG ref;
2215 struct list streams;
2218 static inline struct dwrite_localfontfileloader *impl_from_IDWriteLocalFontFileLoader(IDWriteLocalFontFileLoader *iface)
2220 return CONTAINING_RECORD(iface, struct dwrite_localfontfileloader, IDWriteLocalFontFileLoader_iface);
2223 static inline struct dwrite_localfontfilestream *impl_from_IDWriteFontFileStream(IDWriteFontFileStream *iface)
2225 return CONTAINING_RECORD(iface, struct dwrite_localfontfilestream, IDWriteFontFileStream_iface);
2228 static HRESULT WINAPI localfontfilestream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **obj)
2230 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2231 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2232 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileStream))
2234 *obj = iface;
2235 IDWriteFontFileStream_AddRef(iface);
2236 return S_OK;
2239 *obj = NULL;
2240 return E_NOINTERFACE;
2243 static ULONG WINAPI localfontfilestream_AddRef(IDWriteFontFileStream *iface)
2245 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2246 ULONG ref = InterlockedIncrement(&This->ref);
2247 TRACE("(%p)->(%d)\n", This, ref);
2248 return ref;
2251 static inline void release_cached_stream(struct local_cached_stream *stream)
2253 list_remove(&stream->entry);
2254 heap_free(stream->key);
2255 heap_free(stream);
2258 static ULONG WINAPI localfontfilestream_Release(IDWriteFontFileStream *iface)
2260 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2261 ULONG ref = InterlockedDecrement(&This->ref);
2263 TRACE("(%p)->(%d)\n", This, ref);
2265 if (!ref) {
2266 UnmapViewOfFile(This->file_ptr);
2267 release_cached_stream(This->entry);
2268 heap_free(This);
2271 return ref;
2274 static HRESULT WINAPI localfontfilestream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start, UINT64 offset, UINT64 fragment_size, void **fragment_context)
2276 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2278 TRACE("(%p)->(%p, %s, %s, %p)\n",This, fragment_start,
2279 wine_dbgstr_longlong(offset), wine_dbgstr_longlong(fragment_size), fragment_context);
2281 *fragment_context = NULL;
2283 if ((offset >= This->size - 1) || (fragment_size > This->size - offset)) {
2284 *fragment_start = NULL;
2285 return E_FAIL;
2288 *fragment_start = (char*)This->file_ptr + offset;
2289 return S_OK;
2292 static void WINAPI localfontfilestream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
2294 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2295 TRACE("(%p)->(%p)\n", This, fragment_context);
2298 static HRESULT WINAPI localfontfilestream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
2300 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2301 TRACE("(%p)->(%p)\n", This, size);
2302 *size = This->size;
2303 return S_OK;
2306 static HRESULT WINAPI localfontfilestream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
2308 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2309 ULARGE_INTEGER li;
2311 TRACE("(%p)->(%p)\n", This, last_writetime);
2313 li.u.LowPart = This->entry->key->writetime.dwLowDateTime;
2314 li.u.HighPart = This->entry->key->writetime.dwHighDateTime;
2315 *last_writetime = li.QuadPart;
2317 return S_OK;
2320 static const IDWriteFontFileStreamVtbl localfontfilestreamvtbl =
2322 localfontfilestream_QueryInterface,
2323 localfontfilestream_AddRef,
2324 localfontfilestream_Release,
2325 localfontfilestream_ReadFileFragment,
2326 localfontfilestream_ReleaseFileFragment,
2327 localfontfilestream_GetFileSize,
2328 localfontfilestream_GetLastWriteTime
2331 static HRESULT create_localfontfilestream(const void *file_ptr, UINT64 size, struct local_cached_stream *entry, IDWriteFontFileStream** iface)
2333 struct dwrite_localfontfilestream *This = heap_alloc(sizeof(struct dwrite_localfontfilestream));
2334 if (!This)
2335 return E_OUTOFMEMORY;
2337 This->IDWriteFontFileStream_iface.lpVtbl = &localfontfilestreamvtbl;
2338 This->ref = 1;
2340 This->file_ptr = file_ptr;
2341 This->size = size;
2342 This->entry = entry;
2344 *iface = &This->IDWriteFontFileStream_iface;
2345 return S_OK;
2348 static HRESULT WINAPI localfontfileloader_QueryInterface(IDWriteLocalFontFileLoader *iface, REFIID riid, void **obj)
2350 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2352 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2354 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileLoader) || IsEqualIID(riid, &IID_IDWriteLocalFontFileLoader))
2356 *obj = iface;
2357 IDWriteLocalFontFileLoader_AddRef(iface);
2358 return S_OK;
2361 *obj = NULL;
2362 return E_NOINTERFACE;
2365 static ULONG WINAPI localfontfileloader_AddRef(IDWriteLocalFontFileLoader *iface)
2367 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2368 ULONG ref = InterlockedIncrement(&This->ref);
2369 TRACE("(%p)->(%d)\n", This, ref);
2370 return ref;
2373 static ULONG WINAPI localfontfileloader_Release(IDWriteLocalFontFileLoader *iface)
2375 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2376 ULONG ref = InterlockedDecrement(&This->ref);
2378 TRACE("(%p)->(%d)\n", This, ref);
2380 if (!ref) {
2381 struct local_cached_stream *stream, *stream2;
2383 /* This will detach all entries from cache. Entries are released together with streams,
2384 so stream controls its lifetime. */
2385 LIST_FOR_EACH_ENTRY_SAFE(stream, stream2, &This->streams, struct local_cached_stream, entry)
2386 list_init(&stream->entry);
2388 heap_free(This);
2391 return ref;
2394 static HRESULT WINAPI localfontfileloader_CreateStreamFromKey(IDWriteLocalFontFileLoader *iface, const void *key, UINT32 key_size, IDWriteFontFileStream **ret)
2396 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2397 const struct local_refkey *refkey = key;
2398 struct local_cached_stream *stream;
2399 IDWriteFontFileStream *filestream;
2400 HANDLE file, mapping;
2401 LARGE_INTEGER size;
2402 void *file_ptr;
2403 HRESULT hr;
2405 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, ret);
2406 TRACE("name: %s\n", debugstr_w(refkey->name));
2408 /* search cache first */
2409 LIST_FOR_EACH_ENTRY(stream, &This->streams, struct local_cached_stream, entry) {
2410 if (key_size == stream->key_size && !memcmp(stream->key, key, key_size)) {
2411 *ret = stream->stream;
2412 IDWriteFontFileStream_AddRef(*ret);
2413 return S_OK;
2417 *ret = NULL;
2419 file = CreateFileW(refkey->name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2420 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2421 if (file == INVALID_HANDLE_VALUE)
2422 return E_FAIL;
2424 GetFileSizeEx(file, &size);
2425 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
2426 CloseHandle(file);
2427 if (!mapping)
2428 return E_FAIL;
2430 file_ptr = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
2431 CloseHandle(mapping);
2433 stream = heap_alloc(sizeof(*stream));
2434 if (!stream) {
2435 UnmapViewOfFile(file_ptr);
2436 return E_OUTOFMEMORY;
2439 stream->key = heap_alloc(key_size);
2440 if (!stream->key) {
2441 UnmapViewOfFile(file_ptr);
2442 heap_free(stream);
2443 return E_OUTOFMEMORY;
2446 stream->key_size = key_size;
2447 memcpy(stream->key, key, key_size);
2449 hr = create_localfontfilestream(file_ptr, size.QuadPart, stream, &filestream);
2450 if (FAILED(hr)) {
2451 UnmapViewOfFile(file_ptr);
2452 heap_free(stream->key);
2453 heap_free(stream);
2454 return hr;
2457 stream->stream = filestream;
2458 list_add_head(&This->streams, &stream->entry);
2460 *ret = stream->stream;
2462 return S_OK;
2465 static HRESULT WINAPI localfontfileloader_GetFilePathLengthFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, UINT32 *length)
2467 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2468 const struct local_refkey *refkey = key;
2470 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, length);
2472 *length = strlenW(refkey->name);
2473 return S_OK;
2476 static HRESULT WINAPI localfontfileloader_GetFilePathFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, WCHAR *path, UINT32 length)
2478 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2479 const struct local_refkey *refkey = key;
2481 TRACE("(%p)->(%p, %i, %p, %i)\n", This, key, key_size, path, length);
2483 if (length < strlenW(refkey->name))
2484 return E_INVALIDARG;
2486 strcpyW(path, refkey->name);
2487 return S_OK;
2490 static HRESULT WINAPI localfontfileloader_GetLastWriteTimeFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, FILETIME *writetime)
2492 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2493 const struct local_refkey *refkey = key;
2495 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, writetime);
2497 *writetime = refkey->writetime;
2498 return S_OK;
2501 static const struct IDWriteLocalFontFileLoaderVtbl localfontfileloadervtbl = {
2502 localfontfileloader_QueryInterface,
2503 localfontfileloader_AddRef,
2504 localfontfileloader_Release,
2505 localfontfileloader_CreateStreamFromKey,
2506 localfontfileloader_GetFilePathLengthFromKey,
2507 localfontfileloader_GetFilePathFromKey,
2508 localfontfileloader_GetLastWriteTimeFromKey
2511 HRESULT create_localfontfileloader(IDWriteLocalFontFileLoader** iface)
2513 struct dwrite_localfontfileloader *This = heap_alloc(sizeof(struct dwrite_localfontfileloader));
2514 if (!This)
2515 return E_OUTOFMEMORY;
2517 This->IDWriteLocalFontFileLoader_iface.lpVtbl = &localfontfileloadervtbl;
2518 This->ref = 1;
2519 list_init(&This->streams);
2521 *iface = &This->IDWriteLocalFontFileLoader_iface;
2522 return S_OK;
2525 HRESULT get_local_refkey(const WCHAR *path, const FILETIME *writetime, void **key, UINT32 *size)
2527 struct local_refkey *refkey;
2529 *size = FIELD_OFFSET(struct local_refkey, name) + (strlenW(path)+1)*sizeof(WCHAR);
2530 *key = NULL;
2532 refkey = heap_alloc(*size);
2533 if (!refkey)
2534 return E_OUTOFMEMORY;
2536 if (writetime)
2537 refkey->writetime = *writetime;
2538 else {
2539 WIN32_FILE_ATTRIBUTE_DATA info;
2541 if (GetFileAttributesExW(path, GetFileExInfoStandard, &info))
2542 refkey->writetime = info.ftLastWriteTime;
2543 else
2544 memset(&refkey->writetime, 0, sizeof(refkey->writetime));
2546 strcpyW(refkey->name, path);
2548 *key = refkey;
2550 return S_OK;