dwrite: Forward more font methods to fontface.
[wine/multimedia.git] / dlls / dwrite / font.c
blobcef4a2776ca2b2eca5b1bc114d7205e8c44b84ae
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_POST_TAG DWRITE_MAKE_OPENTYPE_TAG('p','o','s','t')
32 #define MS_CMAP_TAG DWRITE_MAKE_OPENTYPE_TAG('c','m','a','p')
33 #define MS_NAME_TAG DWRITE_MAKE_OPENTYPE_TAG('n','a','m','e')
35 static const IID IID_issystemcollection = {0x14d88047,0x331f,0x4cd3,{0xbc,0xa8,0x3e,0x67,0x99,0xaf,0x34,0x75}};
37 struct dwrite_font_data {
38 LONG ref;
40 DWRITE_FONT_STYLE style;
41 DWRITE_FONT_STRETCH stretch;
42 DWRITE_FONT_WEIGHT weight;
43 DWRITE_FONT_METRICS1 metrics;
44 IDWriteLocalizedStrings *info_strings[DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_CID_NAME+1];
46 /* data needed to create fontface instance */
47 IDWriteFactory *factory;
48 DWRITE_FONT_FACE_TYPE face_type;
49 IDWriteFontFile *file;
50 UINT32 face_index;
52 WCHAR *facename;
55 struct dwrite_fontfamily_data {
56 LONG ref;
58 IDWriteLocalizedStrings *familyname;
60 struct dwrite_font_data **fonts;
61 UINT32 font_count;
62 UINT32 font_alloc;
65 struct dwrite_fontcollection {
66 IDWriteFontCollection IDWriteFontCollection_iface;
67 LONG ref;
69 struct dwrite_fontfamily_data **family_data;
70 UINT32 family_count;
71 UINT32 family_alloc;
72 BOOL is_system;
75 struct dwrite_fontfamily {
76 IDWriteFontFamily IDWriteFontFamily_iface;
77 LONG ref;
79 struct dwrite_fontfamily_data *data;
81 IDWriteFontCollection* collection;
84 struct dwrite_font {
85 IDWriteFont2 IDWriteFont2_iface;
86 LONG ref;
88 IDWriteFontFamily *family;
90 USHORT simulations;
91 struct dwrite_font_data *data;
94 #define DWRITE_FONTTABLE_MAGIC 0xededfafa
96 struct dwrite_fonttablecontext {
97 UINT32 magic;
98 void *context;
99 UINT32 file_index;
102 struct dwrite_fonttable {
103 void *data;
104 void *context;
105 UINT32 size;
108 struct dwrite_fontface {
109 IDWriteFontFace2 IDWriteFontFace2_iface;
110 LONG ref;
112 IDWriteFontFileStream **streams;
113 IDWriteFontFile **files;
114 UINT32 file_count;
115 UINT32 index;
117 USHORT simulations;
118 DWRITE_FONT_FACE_TYPE type;
119 DWRITE_FONT_METRICS1 metrics;
121 struct dwrite_fonttable cmap;
122 struct ft_fontface *ft;
125 struct dwrite_fontfile {
126 IDWriteFontFile IDWriteFontFile_iface;
127 LONG ref;
129 IDWriteFontFileLoader *loader;
130 void *reference_key;
131 UINT32 key_size;
132 IDWriteFontFileStream *stream;
135 static HRESULT get_filestream_from_file(IDWriteFontFile*,IDWriteFontFileStream**);
137 static inline struct dwrite_fontface *impl_from_IDWriteFontFace2(IDWriteFontFace2 *iface)
139 return CONTAINING_RECORD(iface, struct dwrite_fontface, IDWriteFontFace2_iface);
142 static inline struct dwrite_font *impl_from_IDWriteFont2(IDWriteFont2 *iface)
144 return CONTAINING_RECORD(iface, struct dwrite_font, IDWriteFont2_iface);
147 static inline struct dwrite_fontfile *impl_from_IDWriteFontFile(IDWriteFontFile *iface)
149 return CONTAINING_RECORD(iface, struct dwrite_fontfile, IDWriteFontFile_iface);
152 static inline struct dwrite_fontfamily *impl_from_IDWriteFontFamily(IDWriteFontFamily *iface)
154 return CONTAINING_RECORD(iface, struct dwrite_fontfamily, IDWriteFontFamily_iface);
157 static inline struct dwrite_fontcollection *impl_from_IDWriteFontCollection(IDWriteFontCollection *iface)
159 return CONTAINING_RECORD(iface, struct dwrite_fontcollection, IDWriteFontCollection_iface);
162 static inline void* get_fontface_cmap(struct dwrite_fontface *fontface)
164 BOOL exists = FALSE;
165 HRESULT hr;
167 if (fontface->cmap.data)
168 return fontface->cmap.data;
170 hr = IDWriteFontFace2_TryGetFontTable(&fontface->IDWriteFontFace2_iface, MS_CMAP_TAG, (const void**)&fontface->cmap.data,
171 &fontface->cmap.size, &fontface->cmap.context, &exists);
172 if (FAILED(hr) || !exists) {
173 ERR("Font does not have a CMAP table\n");
174 return NULL;
177 return fontface->cmap.data;
180 static void release_font_data(struct dwrite_font_data *data)
182 int i;
184 if (InterlockedDecrement(&data->ref) > 0)
185 return;
187 for (i = DWRITE_INFORMATIONAL_STRING_NONE; i < sizeof(data->info_strings)/sizeof(data->info_strings[0]); i++) {
188 if (data->info_strings[i])
189 IDWriteLocalizedStrings_Release(data->info_strings[i]);
192 IDWriteFontFile_Release(data->file);
193 IDWriteFactory_Release(data->factory);
194 heap_free(data->facename);
195 heap_free(data);
198 static void release_fontfamily_data(struct dwrite_fontfamily_data *data)
200 int i;
202 if (InterlockedDecrement(&data->ref) > 0)
203 return;
205 for (i = 0; i < data->font_count; i++)
206 release_font_data(data->fonts[i]);
207 heap_free(data->fonts);
208 IDWriteLocalizedStrings_Release(data->familyname);
209 heap_free(data);
212 static HRESULT WINAPI dwritefontface_QueryInterface(IDWriteFontFace2 *iface, REFIID riid, void **obj)
214 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
216 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
218 if (IsEqualIID(riid, &IID_IDWriteFontFace2) ||
219 IsEqualIID(riid, &IID_IDWriteFontFace1) ||
220 IsEqualIID(riid, &IID_IDWriteFontFace) ||
221 IsEqualIID(riid, &IID_IUnknown))
223 *obj = iface;
224 IDWriteFontFace2_AddRef(iface);
225 return S_OK;
228 *obj = NULL;
229 return E_NOINTERFACE;
232 static ULONG WINAPI dwritefontface_AddRef(IDWriteFontFace2 *iface)
234 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
235 ULONG ref = InterlockedIncrement(&This->ref);
236 TRACE("(%p)->(%d)\n", This, ref);
237 return ref;
240 static ULONG WINAPI dwritefontface_Release(IDWriteFontFace2 *iface)
242 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
243 ULONG ref = InterlockedDecrement(&This->ref);
245 TRACE("(%p)->(%d)\n", This, ref);
247 if (!ref) {
248 UINT32 i;
250 if (This->cmap.context)
251 IDWriteFontFace2_ReleaseFontTable(iface, This->cmap.context);
252 for (i = 0; i < This->file_count; i++) {
253 if (This->streams[i])
254 IDWriteFontFileStream_Release(This->streams[i]);
255 if (This->files[i])
256 IDWriteFontFile_Release(This->files[i]);
258 release_ft_fontface(This->ft);
259 heap_free(This);
262 return ref;
265 static DWRITE_FONT_FACE_TYPE WINAPI dwritefontface_GetType(IDWriteFontFace2 *iface)
267 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
268 TRACE("(%p)\n", This);
269 return This->type;
272 static HRESULT WINAPI dwritefontface_GetFiles(IDWriteFontFace2 *iface, UINT32 *number_of_files,
273 IDWriteFontFile **fontfiles)
275 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
276 int i;
278 TRACE("(%p)->(%p %p)\n", This, number_of_files, fontfiles);
279 if (fontfiles == NULL)
281 *number_of_files = This->file_count;
282 return S_OK;
284 if (*number_of_files < This->file_count)
285 return E_INVALIDARG;
287 for (i = 0; i < This->file_count; i++)
289 IDWriteFontFile_AddRef(This->files[i]);
290 fontfiles[i] = This->files[i];
293 return S_OK;
296 static UINT32 WINAPI dwritefontface_GetIndex(IDWriteFontFace2 *iface)
298 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
299 TRACE("(%p)\n", This);
300 return This->index;
303 static DWRITE_FONT_SIMULATIONS WINAPI dwritefontface_GetSimulations(IDWriteFontFace2 *iface)
305 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
306 TRACE("(%p)\n", This);
307 return This->simulations;
310 static BOOL WINAPI dwritefontface_IsSymbolFont(IDWriteFontFace2 *iface)
312 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
313 FIXME("(%p): stub\n", This);
314 return FALSE;
317 static void WINAPI dwritefontface_GetMetrics(IDWriteFontFace2 *iface, DWRITE_FONT_METRICS *metrics)
319 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
320 TRACE("(%p)->(%p)\n", This, metrics);
321 memcpy(metrics, &This->metrics, sizeof(*metrics));
324 static UINT16 WINAPI dwritefontface_GetGlyphCount(IDWriteFontFace2 *iface)
326 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
327 FIXME("(%p): stub\n", This);
328 return 0;
331 static HRESULT WINAPI dwritefontface_GetDesignGlyphMetrics(IDWriteFontFace2 *iface,
332 UINT16 const *glyph_indices, UINT32 glyph_count, DWRITE_GLYPH_METRICS *metrics, BOOL is_sideways)
334 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
335 FIXME("(%p)->(%p %u %p %d): stub\n", This, glyph_indices, glyph_count, metrics, is_sideways);
336 return E_NOTIMPL;
339 static HRESULT WINAPI dwritefontface_GetGlyphIndices(IDWriteFontFace2 *iface, UINT32 const *codepoints,
340 UINT32 count, UINT16 *glyph_indices)
342 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
343 unsigned int i;
344 void *data;
346 TRACE("(%p)->(%p %u %p)\n", This, codepoints, count, glyph_indices);
348 data = get_fontface_cmap(This);
349 if (!data)
350 return E_FAIL;
352 for (i = 0; i < count; i++)
353 opentype_cmap_get_glyphindex(data, codepoints[i], &glyph_indices[i]);
355 return S_OK;
358 static HRESULT WINAPI dwritefontface_TryGetFontTable(IDWriteFontFace2 *iface, UINT32 table_tag,
359 const void **table_data, UINT32 *table_size, void **context, BOOL *exists)
361 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
362 struct dwrite_fonttablecontext *tablecontext;
363 HRESULT hr = S_OK;
364 int i;
366 TRACE("(%p)->(%u %p %p %p %p)\n", This, table_tag, table_data, table_size, context, exists);
368 tablecontext = heap_alloc(sizeof(struct dwrite_fonttablecontext));
369 if (!tablecontext)
370 return E_OUTOFMEMORY;
371 tablecontext->magic = DWRITE_FONTTABLE_MAGIC;
373 *exists = FALSE;
374 for (i = 0; i < This->file_count && !(*exists); i++) {
375 hr = opentype_get_font_table(This->streams[i], This->type, This->index, table_tag, table_data, &tablecontext->context, table_size, exists);
376 tablecontext->file_index = i;
378 if (FAILED(hr) && !*exists)
379 heap_free(tablecontext);
380 else
381 *context = (void*)tablecontext;
383 return hr;
386 static void WINAPI dwritefontface_ReleaseFontTable(IDWriteFontFace2 *iface, void *table_context)
388 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
389 struct dwrite_fonttablecontext *tablecontext = (struct dwrite_fonttablecontext*)table_context;
391 TRACE("(%p)->(%p)\n", This, table_context);
393 if (tablecontext->magic != DWRITE_FONTTABLE_MAGIC)
395 TRACE("Invalid table magic\n");
396 return;
399 IDWriteFontFileStream_ReleaseFileFragment(This->streams[tablecontext->file_index], tablecontext->context);
400 heap_free(tablecontext);
403 static HRESULT WINAPI dwritefontface_GetGlyphRunOutline(IDWriteFontFace2 *iface, FLOAT emSize,
404 UINT16 const *glyph_indices, FLOAT const* glyph_advances, DWRITE_GLYPH_OFFSET const *glyph_offsets,
405 UINT32 glyph_count, BOOL is_sideways, BOOL is_rtl, IDWriteGeometrySink *geometrysink)
407 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
408 FIXME("(%p)->(%f %p %p %p %u %d %d %p): stub\n", This, emSize, glyph_indices, glyph_advances, glyph_offsets,
409 glyph_count, is_sideways, is_rtl, geometrysink);
410 return E_NOTIMPL;
413 static HRESULT WINAPI dwritefontface_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT emSize,
414 FLOAT pixels_per_dip, DWRITE_MEASURING_MODE mode, IDWriteRenderingParams* params, DWRITE_RENDERING_MODE* rendering_mode)
416 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
417 FIXME("(%p)->(%f %f %d %p %p): stub\n", This, emSize, pixels_per_dip, mode, params, rendering_mode);
418 return E_NOTIMPL;
421 static HRESULT WINAPI dwritefontface_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
422 DWRITE_MATRIX const *transform, DWRITE_FONT_METRICS *metrics)
424 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
425 FIXME("(%p)->(%f %f %p %p): stub\n", This, emSize, pixels_per_dip, transform, metrics);
426 return E_NOTIMPL;
429 static HRESULT WINAPI dwritefontface_GetGdiCompatibleGlyphMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
430 DWRITE_MATRIX const *transform, BOOL use_gdi_natural, UINT16 const *glyph_indices, UINT32 glyph_count,
431 DWRITE_GLYPH_METRICS *metrics, BOOL is_sideways)
433 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
434 FIXME("(%p)->(%f %f %p %d %p %u %p %d): stub\n", This, emSize, pixels_per_dip, transform, use_gdi_natural, glyph_indices,
435 glyph_count, metrics, is_sideways);
436 return E_NOTIMPL;
439 static void WINAPI dwritefontface1_GetMetrics(IDWriteFontFace2 *iface, DWRITE_FONT_METRICS1 *metrics)
441 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
442 TRACE("(%p)->(%p)\n", This, metrics);
443 *metrics = This->metrics;
446 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT em_size, FLOAT pixels_per_dip,
447 const DWRITE_MATRIX *transform, DWRITE_FONT_METRICS1 *metrics)
449 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
450 FIXME("(%p)->(%f %f %p %p): stub\n", This, em_size, pixels_per_dip, transform, metrics);
451 return E_NOTIMPL;
454 static void WINAPI dwritefontface1_GetCaretMetrics(IDWriteFontFace2 *iface, DWRITE_CARET_METRICS *metrics)
456 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
457 FIXME("(%p)->(%p): stub\n", This, metrics);
460 static HRESULT WINAPI dwritefontface1_GetUnicodeRanges(IDWriteFontFace2 *iface, UINT32 max_count,
461 DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
463 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
465 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
467 *count = 0;
468 if (max_count && !ranges)
469 return E_INVALIDARG;
471 return opentype_cmap_get_unicode_ranges(get_fontface_cmap(This), max_count, ranges, count);
474 static BOOL WINAPI dwritefontface1_IsMonospacedFont(IDWriteFontFace2 *iface)
476 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
477 FIXME("(%p): stub\n", This);
478 return FALSE;
481 static HRESULT WINAPI dwritefontface1_GetDesignGlyphAdvances(IDWriteFontFace2 *iface,
482 UINT32 glyph_count, UINT16 const *indices, INT32 *advances, BOOL is_sideways)
484 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
485 FIXME("(%p)->(%u %p %p %d): stub\n", This, glyph_count, indices, advances, is_sideways);
486 return E_NOTIMPL;
489 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleGlyphAdvances(IDWriteFontFace2 *iface,
490 FLOAT em_size, FLOAT pixels_per_dip, const DWRITE_MATRIX *transform, BOOL use_gdi_natural,
491 BOOL is_sideways, UINT32 glyph_count, UINT16 const *indices, INT32 *advances)
493 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
494 FIXME("(%p)->(%f %f %p %d %d %u %p %p): stub\n", This, em_size, pixels_per_dip, transform,
495 use_gdi_natural, is_sideways, glyph_count, indices, advances);
496 return E_NOTIMPL;
499 static HRESULT WINAPI dwritefontface1_GetKerningPairAdjustments(IDWriteFontFace2 *iface, UINT32 glyph_count,
500 const UINT16 *indices, INT32 *adjustments)
502 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
503 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, indices, adjustments);
504 return E_NOTIMPL;
507 static BOOL WINAPI dwritefontface1_HasKerningPairs(IDWriteFontFace2 *iface)
509 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
510 FIXME("(%p): stub\n", This);
511 return FALSE;
514 static HRESULT WINAPI dwritefontface1_GetRecommendedRenderingMode(IDWriteFontFace2 *iface,
515 FLOAT font_emsize, FLOAT dpiX, FLOAT dpiY, const DWRITE_MATRIX *transform, BOOL is_sideways,
516 DWRITE_OUTLINE_THRESHOLD threshold, DWRITE_MEASURING_MODE measuring_mode, DWRITE_RENDERING_MODE *rendering_mode)
518 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
519 FIXME("(%p)->(%f %f %f %p %d %d %d %p): stub\n", This, font_emsize, dpiX, dpiY, transform, is_sideways,
520 threshold, measuring_mode, rendering_mode);
521 return E_NOTIMPL;
524 static HRESULT WINAPI dwritefontface1_GetVerticalGlyphVariants(IDWriteFontFace2 *iface, UINT32 glyph_count,
525 const UINT16 *nominal_indices, UINT16 *vertical_indices)
527 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
528 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, nominal_indices, vertical_indices);
529 return E_NOTIMPL;
532 static BOOL WINAPI dwritefontface1_HasVerticalGlyphVariants(IDWriteFontFace2 *iface)
534 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
535 FIXME("(%p): stub\n", This);
536 return FALSE;
539 static BOOL WINAPI dwritefontface2_IsColorFont(IDWriteFontFace2 *iface)
541 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
542 FIXME("(%p): stub\n", This);
543 return FALSE;
546 static UINT32 WINAPI dwritefontface2_GetColorPaletteCount(IDWriteFontFace2 *iface)
548 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
549 FIXME("(%p): stub\n", This);
550 return 0;
553 static UINT32 WINAPI dwritefontface2_GetPaletteEntryCount(IDWriteFontFace2 *iface)
555 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
556 FIXME("(%p): stub\n", This);
557 return 0;
560 static HRESULT WINAPI dwritefontface2_GetPaletteEntries(IDWriteFontFace2 *iface, UINT32 palette_index,
561 UINT32 first_entry_index, UINT32 entry_count, DWRITE_COLOR_F *entries)
563 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
564 FIXME("(%p)->(%u %u %u %p): stub\n", This, palette_index, first_entry_index, entry_count, entries);
565 return E_NOTIMPL;
568 static HRESULT WINAPI dwritefontface2_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT fontEmSize,
569 FLOAT dpiX, FLOAT dpiY, DWRITE_MATRIX const *transform, BOOL is_sideways, DWRITE_OUTLINE_THRESHOLD threshold,
570 DWRITE_MEASURING_MODE measuringmode, IDWriteRenderingParams *params, DWRITE_RENDERING_MODE *renderingmode,
571 DWRITE_GRID_FIT_MODE *gridfitmode)
573 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
574 FIXME("(%p)->(%f %f %f %p %d %d %d %p %p %p): stub\n", This, fontEmSize, dpiX, dpiY, transform, is_sideways, threshold,
575 measuringmode, params, renderingmode, gridfitmode);
576 return E_NOTIMPL;
579 static const IDWriteFontFace2Vtbl dwritefontfacevtbl = {
580 dwritefontface_QueryInterface,
581 dwritefontface_AddRef,
582 dwritefontface_Release,
583 dwritefontface_GetType,
584 dwritefontface_GetFiles,
585 dwritefontface_GetIndex,
586 dwritefontface_GetSimulations,
587 dwritefontface_IsSymbolFont,
588 dwritefontface_GetMetrics,
589 dwritefontface_GetGlyphCount,
590 dwritefontface_GetDesignGlyphMetrics,
591 dwritefontface_GetGlyphIndices,
592 dwritefontface_TryGetFontTable,
593 dwritefontface_ReleaseFontTable,
594 dwritefontface_GetGlyphRunOutline,
595 dwritefontface_GetRecommendedRenderingMode,
596 dwritefontface_GetGdiCompatibleMetrics,
597 dwritefontface_GetGdiCompatibleGlyphMetrics,
598 dwritefontface1_GetMetrics,
599 dwritefontface1_GetGdiCompatibleMetrics,
600 dwritefontface1_GetCaretMetrics,
601 dwritefontface1_GetUnicodeRanges,
602 dwritefontface1_IsMonospacedFont,
603 dwritefontface1_GetDesignGlyphAdvances,
604 dwritefontface1_GetGdiCompatibleGlyphAdvances,
605 dwritefontface1_GetKerningPairAdjustments,
606 dwritefontface1_HasKerningPairs,
607 dwritefontface1_GetRecommendedRenderingMode,
608 dwritefontface1_GetVerticalGlyphVariants,
609 dwritefontface1_HasVerticalGlyphVariants,
610 dwritefontface2_IsColorFont,
611 dwritefontface2_GetColorPaletteCount,
612 dwritefontface2_GetPaletteEntryCount,
613 dwritefontface2_GetPaletteEntries,
614 dwritefontface2_GetRecommendedRenderingMode
617 static void get_font_properties_from_stream(IDWriteFontFileStream *stream, DWRITE_FONT_FACE_TYPE face_type,
618 UINT32 face_index, DWRITE_FONT_METRICS1 *metrics, DWRITE_FONT_STRETCH *stretch, DWRITE_FONT_WEIGHT *weight,
619 DWRITE_FONT_STYLE *style)
621 const void *tt_os2 = NULL, *tt_head = NULL, *tt_post = NULL;
622 void *os2_context, *head_context, *post_context;
623 DWRITE_FONT_STRETCH fontstretch;
624 DWRITE_FONT_WEIGHT fontweight;
625 DWRITE_FONT_STYLE fontstyle;
627 opentype_get_font_table(stream, face_type, face_index, MS_OS2_TAG, &tt_os2, &os2_context, NULL, NULL);
628 opentype_get_font_table(stream, face_type, face_index, MS_HEAD_TAG, &tt_head, &head_context, NULL, NULL);
629 opentype_get_font_table(stream, face_type, face_index, MS_POST_TAG, &tt_post, &post_context, NULL, NULL);
631 if (!stretch) stretch = &fontstretch;
632 if (!weight) weight = &fontweight;
633 if (!style) style = &fontstyle;
635 opentype_get_font_properties(tt_os2, tt_head, stretch, weight, style);
636 opentype_get_font_metrics(tt_os2, tt_head, tt_post, metrics);
638 if (tt_os2)
639 IDWriteFontFileStream_ReleaseFileFragment(stream, os2_context);
640 if (tt_head)
641 IDWriteFontFileStream_ReleaseFileFragment(stream, head_context);
642 if (tt_post)
643 IDWriteFontFileStream_ReleaseFileFragment(stream, post_context);
646 HRESULT convert_fontface_to_logfont(IDWriteFontFace *face, LOGFONTW *logfont)
648 DWRITE_FONT_SIMULATIONS simulations;
649 DWRITE_FONT_FACE_TYPE face_type;
650 IDWriteFontFileStream *stream;
651 DWRITE_FONT_METRICS1 metrics;
652 DWRITE_FONT_STRETCH stretch;
653 DWRITE_FONT_STYLE style;
654 DWRITE_FONT_WEIGHT weight;
655 IDWriteFontFile *file = NULL;
656 UINT32 index;
657 HRESULT hr;
659 memset(logfont, 0, sizeof(*logfont));
661 index = 1;
662 hr = IDWriteFontFace_GetFiles(face, &index, &file);
663 if (FAILED(hr) || !file)
664 return hr;
666 hr = get_filestream_from_file(file, &stream);
667 if (FAILED(hr)) {
668 IDWriteFontFile_Release(file);
669 return hr;
672 index = IDWriteFontFace_GetIndex(face);
673 face_type = IDWriteFontFace_GetType(face);
674 get_font_properties_from_stream(stream, face_type, index, &metrics, &stretch, &weight, &style);
675 IDWriteFontFileStream_Release(stream);
677 simulations = IDWriteFontFace_GetSimulations(face);
679 logfont->lfCharSet = DEFAULT_CHARSET;
680 logfont->lfWeight = weight;
681 logfont->lfItalic = style == DWRITE_FONT_STYLE_ITALIC || (simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE);
682 logfont->lfOutPrecision = OUT_OUTLINE_PRECIS;
683 /* TODO: set facename */
685 return S_OK;
688 static HRESULT get_fontface_from_font(struct dwrite_font *font, IDWriteFontFace2 **fontface)
690 struct dwrite_font_data *data = font->data;
691 IDWriteFontFace *face;
692 HRESULT hr;
694 *fontface = NULL;
696 hr = IDWriteFactory_CreateFontFace(data->factory, data->face_type, 1, &data->file,
697 data->face_index, font->simulations, &face);
698 if (FAILED(hr))
699 return hr;
701 hr = IDWriteFontFace_QueryInterface(face, &IID_IDWriteFontFace2, (void**)fontface);
702 IDWriteFontFace_Release(face);
704 return hr;
707 static HRESULT WINAPI dwritefont_QueryInterface(IDWriteFont2 *iface, REFIID riid, void **obj)
709 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
711 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
713 if (IsEqualIID(riid, &IID_IDWriteFont2) ||
714 IsEqualIID(riid, &IID_IDWriteFont1) ||
715 IsEqualIID(riid, &IID_IDWriteFont) ||
716 IsEqualIID(riid, &IID_IUnknown))
718 *obj = iface;
719 IDWriteFont2_AddRef(iface);
720 return S_OK;
723 *obj = NULL;
724 return E_NOINTERFACE;
727 static ULONG WINAPI dwritefont_AddRef(IDWriteFont2 *iface)
729 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
730 ULONG ref = InterlockedIncrement(&This->ref);
731 TRACE("(%p)->(%d)\n", This, ref);
732 return ref;
735 static ULONG WINAPI dwritefont_Release(IDWriteFont2 *iface)
737 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
738 ULONG ref = InterlockedDecrement(&This->ref);
740 TRACE("(%p)->(%d)\n", This, ref);
742 if (!ref) {
743 IDWriteFontFamily_Release(This->family);
744 release_font_data(This->data);
745 heap_free(This);
748 return ref;
751 static HRESULT WINAPI dwritefont_GetFontFamily(IDWriteFont2 *iface, IDWriteFontFamily **family)
753 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
754 TRACE("(%p)->(%p)\n", This, family);
756 *family = This->family;
757 IDWriteFontFamily_AddRef(*family);
758 return S_OK;
761 static DWRITE_FONT_WEIGHT WINAPI dwritefont_GetWeight(IDWriteFont2 *iface)
763 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
764 TRACE("(%p)\n", This);
765 return This->data->weight;
768 static DWRITE_FONT_STRETCH WINAPI dwritefont_GetStretch(IDWriteFont2 *iface)
770 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
771 TRACE("(%p)\n", This);
772 return This->data->stretch;
775 static DWRITE_FONT_STYLE WINAPI dwritefont_GetStyle(IDWriteFont2 *iface)
777 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
778 TRACE("(%p)\n", This);
779 return This->data->style;
782 static BOOL WINAPI dwritefont_IsSymbolFont(IDWriteFont2 *iface)
784 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
785 IDWriteFontFace2 *fontface;
786 HRESULT hr;
788 TRACE("(%p)\n", This);
790 hr = get_fontface_from_font(This, &fontface);
791 if (FAILED(hr))
792 return hr;
794 return IDWriteFontFace2_IsSymbolFont(fontface);
797 static HRESULT WINAPI dwritefont_GetFaceNames(IDWriteFont2 *iface, IDWriteLocalizedStrings **names)
799 static const WCHAR boldobliqueW[] = {'B','o','l','d',' ','O','b','l','i','q','u','e',0};
800 static const WCHAR obliqueW[] = {'O','b','l','i','q','u','e',0};
801 static const WCHAR boldW[] = {'B','o','l','d',0};
802 static const WCHAR enusW[] = {'e','n','-','u','s',0};
804 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
805 IDWriteLocalizedStrings *strings;
806 const WCHAR *name;
807 HRESULT hr;
809 TRACE("(%p)->(%p)\n", This, names);
811 *names = NULL;
813 if (This->simulations == DWRITE_FONT_SIMULATIONS_NONE) {
814 BOOL exists;
815 return IDWriteFont2_GetInformationalStrings(iface, DWRITE_INFORMATIONAL_STRING_WIN32_SUBFAMILY_NAMES,
816 names, &exists);
819 switch (This->simulations) {
820 case DWRITE_FONT_SIMULATIONS_BOLD|DWRITE_FONT_SIMULATIONS_OBLIQUE:
821 name = boldobliqueW;
822 break;
823 case DWRITE_FONT_SIMULATIONS_BOLD:
824 name = boldW;
825 break;
826 case DWRITE_FONT_SIMULATIONS_OBLIQUE:
827 name = obliqueW;
828 break;
829 default:
830 ERR("unknown simulations %d\n", This->simulations);
831 return E_FAIL;
834 hr = create_localizedstrings(&strings);
835 if (FAILED(hr)) return hr;
837 hr = add_localizedstring(strings, enusW, name);
838 if (FAILED(hr)) {
839 IDWriteLocalizedStrings_Release(strings);
840 return hr;
843 *names = strings;
845 return S_OK;
848 static HRESULT WINAPI dwritefont_GetInformationalStrings(IDWriteFont2 *iface,
849 DWRITE_INFORMATIONAL_STRING_ID stringid, IDWriteLocalizedStrings **strings, BOOL *exists)
851 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
852 struct dwrite_font_data *data = This->data;
853 HRESULT hr;
855 TRACE("(%p)->(%d %p %p)\n", This, stringid, strings, exists);
857 *exists = FALSE;
858 *strings = NULL;
860 if (stringid > DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_CID_NAME || stringid == DWRITE_INFORMATIONAL_STRING_NONE)
861 return S_OK;
863 if (!data->info_strings[stringid]) {
864 IDWriteFontFace2 *fontface;
865 const void *table_data;
866 BOOL table_exists;
867 void *context;
868 UINT32 size;
870 hr = get_fontface_from_font(This, &fontface);
871 if (FAILED(hr))
872 return hr;
874 table_exists = FALSE;
875 hr = IDWriteFontFace2_TryGetFontTable(fontface, MS_NAME_TAG, &table_data, &size, &context, &table_exists);
876 if (FAILED(hr) || !table_exists)
877 WARN("no NAME table found.\n");
879 if (table_exists) {
880 hr = opentype_get_font_strings_from_id(table_data, stringid, &data->info_strings[stringid]);
881 if (FAILED(hr) || !data->info_strings[stringid])
882 return hr;
883 IDWriteFontFace2_ReleaseFontTable(fontface, context);
887 hr = clone_localizedstring(data->info_strings[stringid], strings);
888 if (FAILED(hr))
889 return hr;
891 *exists = TRUE;
892 return S_OK;
895 static DWRITE_FONT_SIMULATIONS WINAPI dwritefont_GetSimulations(IDWriteFont2 *iface)
897 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
898 TRACE("(%p)\n", This);
899 return This->simulations;
902 static void WINAPI dwritefont_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS *metrics)
904 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
906 TRACE("(%p)->(%p)\n", This, metrics);
907 memcpy(metrics, &This->data->metrics, sizeof(*metrics));
910 static HRESULT WINAPI dwritefont_HasCharacter(IDWriteFont2 *iface, UINT32 value, BOOL *exists)
912 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
913 IDWriteFontFace2 *fontface;
914 UINT16 index;
915 HRESULT hr;
917 TRACE("(%p)->(0x%08x %p)\n", This, value, exists);
919 *exists = FALSE;
921 hr = get_fontface_from_font(This, &fontface);
922 if (FAILED(hr))
923 return hr;
925 index = 0;
926 hr = IDWriteFontFace2_GetGlyphIndices(fontface, &value, 1, &index);
927 if (FAILED(hr))
928 return hr;
930 *exists = index != 0;
931 return S_OK;
934 static HRESULT WINAPI dwritefont_CreateFontFace(IDWriteFont2 *iface, IDWriteFontFace **face)
936 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
937 HRESULT hr;
939 TRACE("(%p)->(%p)\n", This, face);
941 hr = get_fontface_from_font(This, (IDWriteFontFace2**)face);
942 if (hr == S_OK)
943 IDWriteFontFace_AddRef(*face);
945 return hr;
948 static void WINAPI dwritefont1_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS1 *metrics)
950 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
951 TRACE("(%p)->(%p)\n", This, metrics);
952 *metrics = This->data->metrics;
955 static void WINAPI dwritefont1_GetPanose(IDWriteFont2 *iface, DWRITE_PANOSE *panose)
957 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
958 FIXME("(%p)->(%p): stub\n", This, panose);
961 static HRESULT WINAPI dwritefont1_GetUnicodeRanges(IDWriteFont2 *iface, UINT32 max_count, DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
963 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
964 IDWriteFontFace2 *fontface;
965 HRESULT hr;
967 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
969 hr = get_fontface_from_font(This, &fontface);
970 if (FAILED(hr))
971 return hr;
973 return IDWriteFontFace2_GetUnicodeRanges(fontface, max_count, ranges, count);
976 static BOOL WINAPI dwritefont1_IsMonospacedFont(IDWriteFont2 *iface)
978 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
979 IDWriteFontFace2 *fontface;
980 HRESULT hr;
982 TRACE("(%p)\n", This);
984 hr = get_fontface_from_font(This, &fontface);
985 if (FAILED(hr))
986 return hr;
988 return IDWriteFontFace2_IsMonospacedFont(fontface);
991 static HRESULT WINAPI dwritefont2_IsColorFont(IDWriteFont2 *iface)
993 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
994 IDWriteFontFace2 *fontface;
995 HRESULT hr;
997 TRACE("(%p)\n", This);
999 hr = get_fontface_from_font(This, &fontface);
1000 if (FAILED(hr))
1001 return hr;
1003 return IDWriteFontFace2_IsColorFont(fontface);
1006 static const IDWriteFont2Vtbl dwritefontvtbl = {
1007 dwritefont_QueryInterface,
1008 dwritefont_AddRef,
1009 dwritefont_Release,
1010 dwritefont_GetFontFamily,
1011 dwritefont_GetWeight,
1012 dwritefont_GetStretch,
1013 dwritefont_GetStyle,
1014 dwritefont_IsSymbolFont,
1015 dwritefont_GetFaceNames,
1016 dwritefont_GetInformationalStrings,
1017 dwritefont_GetSimulations,
1018 dwritefont_GetMetrics,
1019 dwritefont_HasCharacter,
1020 dwritefont_CreateFontFace,
1021 dwritefont1_GetMetrics,
1022 dwritefont1_GetPanose,
1023 dwritefont1_GetUnicodeRanges,
1024 dwritefont1_IsMonospacedFont,
1025 dwritefont2_IsColorFont
1028 static HRESULT create_font(struct dwrite_font_data *data, IDWriteFontFamily *family, DWRITE_FONT_SIMULATIONS simulations,
1029 IDWriteFont **font)
1031 struct dwrite_font *This;
1032 *font = NULL;
1034 This = heap_alloc(sizeof(struct dwrite_font));
1035 if (!This) return E_OUTOFMEMORY;
1037 This->IDWriteFont2_iface.lpVtbl = &dwritefontvtbl;
1038 This->ref = 1;
1039 This->family = family;
1040 IDWriteFontFamily_AddRef(family);
1041 This->simulations = simulations;
1042 This->data = data;
1043 InterlockedIncrement(&This->data->ref);
1045 *font = (IDWriteFont*)&This->IDWriteFont2_iface;
1047 return S_OK;
1050 static HRESULT WINAPI dwritefontfamily_QueryInterface(IDWriteFontFamily *iface, REFIID riid, void **obj)
1052 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1053 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1055 if (IsEqualIID(riid, &IID_IUnknown) ||
1056 IsEqualIID(riid, &IID_IDWriteFontList) ||
1057 IsEqualIID(riid, &IID_IDWriteFontFamily))
1059 *obj = iface;
1060 IDWriteFontFamily_AddRef(iface);
1061 return S_OK;
1064 *obj = NULL;
1065 return E_NOINTERFACE;
1068 static ULONG WINAPI dwritefontfamily_AddRef(IDWriteFontFamily *iface)
1070 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1071 ULONG ref = InterlockedIncrement(&This->ref);
1072 TRACE("(%p)->(%d)\n", This, ref);
1073 return ref;
1076 static ULONG WINAPI dwritefontfamily_Release(IDWriteFontFamily *iface)
1078 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1079 ULONG ref = InterlockedDecrement(&This->ref);
1081 TRACE("(%p)->(%d)\n", This, ref);
1083 if (!ref)
1085 IDWriteFontCollection_Release(This->collection);
1086 release_fontfamily_data(This->data);
1087 heap_free(This);
1090 return ref;
1093 static HRESULT WINAPI dwritefontfamily_GetFontCollection(IDWriteFontFamily *iface, IDWriteFontCollection **collection)
1095 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1096 TRACE("(%p)->(%p)\n", This, collection);
1098 *collection = This->collection;
1099 IDWriteFontCollection_AddRef(This->collection);
1100 return S_OK;
1103 static UINT32 WINAPI dwritefontfamily_GetFontCount(IDWriteFontFamily *iface)
1105 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1106 TRACE("(%p)\n", This);
1107 return This->data->font_count;
1110 static HRESULT WINAPI dwritefontfamily_GetFont(IDWriteFontFamily *iface, UINT32 index, IDWriteFont **font)
1112 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1114 TRACE("(%p)->(%u %p)\n", This, index, font);
1116 *font = NULL;
1118 if (This->data->font_count == 0)
1119 return S_FALSE;
1121 if (index >= This->data->font_count)
1122 return E_INVALIDARG;
1124 return create_font(This->data->fonts[index], iface, DWRITE_FONT_SIMULATIONS_NONE, font);
1127 static HRESULT WINAPI dwritefontfamily_GetFamilyNames(IDWriteFontFamily *iface, IDWriteLocalizedStrings **names)
1129 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1130 return clone_localizedstring(This->data->familyname, names);
1133 static inline BOOL is_matching_font_style(DWRITE_FONT_STYLE style, DWRITE_FONT_STYLE font_style)
1135 if (style == font_style)
1136 return TRUE;
1138 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) && font_style == DWRITE_FONT_STYLE_NORMAL)
1139 return TRUE;
1141 return FALSE;
1144 static HRESULT WINAPI dwritefontfamily_GetFirstMatchingFont(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1145 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFont **font)
1147 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1148 UINT32 min_weight_diff = ~0u;
1149 int found = -1, i;
1151 TRACE("(%p)->(%d %d %d %p)\n", This, weight, stretch, style, font);
1153 for (i = 0; i < This->data->font_count; i++) {
1154 if (is_matching_font_style(style, This->data->fonts[i]->style) && stretch == This->data->fonts[i]->stretch) {
1155 DWRITE_FONT_WEIGHT font_weight = This->data->fonts[i]->weight;
1156 UINT32 weight_diff = abs(font_weight - weight);
1157 if (weight_diff < min_weight_diff) {
1158 min_weight_diff = weight_diff;
1159 found = i;
1164 if (found != -1) {
1165 DWRITE_FONT_SIMULATIONS simulations = DWRITE_FONT_SIMULATIONS_NONE;
1167 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) &&
1168 This->data->fonts[found]->style == DWRITE_FONT_STYLE_NORMAL) {
1169 simulations = DWRITE_FONT_SIMULATIONS_OBLIQUE;
1171 return create_font(This->data->fonts[found], iface, simulations, font);
1173 else {
1174 *font = NULL;
1175 return DWRITE_E_NOFONT;
1179 static HRESULT WINAPI dwritefontfamily_GetMatchingFonts(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1180 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFontList **fonts)
1182 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1183 FIXME("(%p)->(%d %d %d %p): stub\n", This, weight, stretch, style, fonts);
1184 return E_NOTIMPL;
1187 static const IDWriteFontFamilyVtbl fontfamilyvtbl = {
1188 dwritefontfamily_QueryInterface,
1189 dwritefontfamily_AddRef,
1190 dwritefontfamily_Release,
1191 dwritefontfamily_GetFontCollection,
1192 dwritefontfamily_GetFontCount,
1193 dwritefontfamily_GetFont,
1194 dwritefontfamily_GetFamilyNames,
1195 dwritefontfamily_GetFirstMatchingFont,
1196 dwritefontfamily_GetMatchingFonts
1199 static HRESULT create_fontfamily(struct dwrite_fontfamily_data *data, IDWriteFontCollection *collection, IDWriteFontFamily **family)
1201 struct dwrite_fontfamily *This;
1203 *family = NULL;
1205 This = heap_alloc(sizeof(struct dwrite_fontfamily));
1206 if (!This) return E_OUTOFMEMORY;
1208 This->IDWriteFontFamily_iface.lpVtbl = &fontfamilyvtbl;
1209 This->ref = 1;
1210 This->collection = collection;
1211 IDWriteFontCollection_AddRef(collection);
1212 This->data = data;
1213 InterlockedIncrement(&This->data->ref);
1215 *family = &This->IDWriteFontFamily_iface;
1217 return S_OK;
1220 BOOL is_system_collection(IDWriteFontCollection *collection)
1222 void *obj;
1223 return IDWriteFontCollection_QueryInterface(collection, &IID_issystemcollection, (void**)&obj) == S_OK;
1226 static HRESULT WINAPI dwritefontcollection_QueryInterface(IDWriteFontCollection *iface, REFIID riid, void **obj)
1228 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1229 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1231 if (IsEqualIID(riid, &IID_IUnknown) ||
1232 IsEqualIID(riid, &IID_IDWriteFontCollection))
1234 *obj = iface;
1235 IDWriteFontCollection_AddRef(iface);
1236 return S_OK;
1239 *obj = NULL;
1241 if (This->is_system && IsEqualIID(riid, &IID_issystemcollection))
1242 return S_OK;
1244 return E_NOINTERFACE;
1247 static ULONG WINAPI dwritefontcollection_AddRef(IDWriteFontCollection *iface)
1249 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1250 ULONG ref = InterlockedIncrement(&This->ref);
1251 TRACE("(%p)->(%d)\n", This, ref);
1252 return ref;
1255 static ULONG WINAPI dwritefontcollection_Release(IDWriteFontCollection *iface)
1257 unsigned int i;
1258 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1259 ULONG ref = InterlockedDecrement(&This->ref);
1260 TRACE("(%p)->(%d)\n", This, ref);
1262 if (!ref) {
1263 for (i = 0; i < This->family_count; i++)
1264 release_fontfamily_data(This->family_data[i]);
1265 heap_free(This->family_data);
1266 heap_free(This);
1269 return ref;
1272 static UINT32 WINAPI dwritefontcollection_GetFontFamilyCount(IDWriteFontCollection *iface)
1274 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1275 TRACE("(%p)\n", This);
1276 return This->family_count;
1279 static HRESULT WINAPI dwritefontcollection_GetFontFamily(IDWriteFontCollection *iface, UINT32 index, IDWriteFontFamily **family)
1281 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1283 TRACE("(%p)->(%u %p)\n", This, index, family);
1285 if (index >= This->family_count) {
1286 *family = NULL;
1287 return E_FAIL;
1290 return create_fontfamily(This->family_data[index], iface, family);
1293 static HRESULT collection_find_family(struct dwrite_fontcollection *collection, const WCHAR *name, UINT32 *index, BOOL *exists)
1295 UINT32 i;
1297 if (collection->family_count) {
1298 for (i = 0; i < collection->family_count; i++) {
1299 IDWriteLocalizedStrings *family_name = collection->family_data[i]->familyname;
1300 HRESULT hr;
1301 int j;
1303 for (j = 0; j < IDWriteLocalizedStrings_GetCount(family_name); j++) {
1304 WCHAR buffer[255];
1305 hr = IDWriteLocalizedStrings_GetString(family_name, j, buffer, 255);
1306 if (SUCCEEDED(hr)) {
1307 if (!strcmpW(buffer, name)) {
1308 *index = i;
1309 *exists = TRUE;
1310 return S_OK;
1315 *index = (UINT32)-1;
1316 *exists = FALSE;
1319 return S_OK;
1322 static HRESULT WINAPI dwritefontcollection_FindFamilyName(IDWriteFontCollection *iface, const WCHAR *name, UINT32 *index, BOOL *exists)
1324 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1325 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(name), index, exists);
1326 return collection_find_family(This, name, index, exists);
1329 static BOOL is_same_fontfile(IDWriteFontFile *left, IDWriteFontFile *right)
1331 UINT32 left_key_size, right_key_size;
1332 const void *left_key, *right_key;
1333 HRESULT hr;
1335 if (left == right)
1336 return TRUE;
1338 hr = IDWriteFontFile_GetReferenceKey(left, &left_key, &left_key_size);
1339 if (FAILED(hr))
1340 return FALSE;
1342 hr = IDWriteFontFile_GetReferenceKey(right, &right_key, &right_key_size);
1343 if (FAILED(hr))
1344 return FALSE;
1346 if (left_key_size != right_key_size)
1347 return FALSE;
1349 return !memcmp(left_key, right_key, left_key_size);
1352 static HRESULT WINAPI dwritefontcollection_GetFontFromFontFace(IDWriteFontCollection *iface, IDWriteFontFace *face, IDWriteFont **font)
1354 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1355 struct dwrite_fontfamily_data *found_family = NULL;
1356 struct dwrite_font_data *found_font = NULL;
1357 DWRITE_FONT_SIMULATIONS simulations;
1358 IDWriteFontFamily *family;
1359 UINT32 i, j, face_index;
1360 IDWriteFontFile *file;
1361 HRESULT hr;
1363 TRACE("(%p)->(%p %p)\n", This, face, font);
1365 *font = NULL;
1367 if (!face)
1368 return E_INVALIDARG;
1370 i = 1;
1371 hr = IDWriteFontFace_GetFiles(face, &i, &file);
1372 if (FAILED(hr))
1373 return hr;
1374 face_index = IDWriteFontFace_GetIndex(face);
1376 for (i = 0; i < This->family_count; i++) {
1377 struct dwrite_fontfamily_data *family_data = This->family_data[i];
1378 for (j = 0; j < family_data->font_count; j++) {
1379 struct dwrite_font_data *font_data = family_data->fonts[j];
1381 if (face_index == font_data->face_index && is_same_fontfile(file, font_data->file)) {
1382 found_font = font_data;
1383 found_family = family_data;
1384 break;
1389 if (!found_font)
1390 return E_INVALIDARG;
1392 hr = create_fontfamily(found_family, iface, &family);
1393 if (FAILED(hr))
1394 return hr;
1396 simulations = IDWriteFontFace_GetSimulations(face);
1397 hr = create_font(found_font, family, simulations, font);
1398 IDWriteFontFamily_Release(family);
1399 return hr;
1402 static const IDWriteFontCollectionVtbl fontcollectionvtbl = {
1403 dwritefontcollection_QueryInterface,
1404 dwritefontcollection_AddRef,
1405 dwritefontcollection_Release,
1406 dwritefontcollection_GetFontFamilyCount,
1407 dwritefontcollection_GetFontFamily,
1408 dwritefontcollection_FindFamilyName,
1409 dwritefontcollection_GetFontFromFontFace
1412 static HRESULT fontfamily_add_font(struct dwrite_fontfamily_data *family_data, struct dwrite_font_data *font_data)
1414 if (family_data->font_count + 1 >= family_data->font_alloc) {
1415 struct dwrite_font_data **new_list;
1416 UINT32 new_alloc;
1418 new_alloc = family_data->font_alloc * 2;
1419 new_list = heap_realloc(family_data->fonts, sizeof(*family_data->fonts) * new_alloc);
1420 if (!new_list)
1421 return E_OUTOFMEMORY;
1422 family_data->fonts = new_list;
1423 family_data->font_alloc = new_alloc;
1426 family_data->fonts[family_data->font_count] = font_data;
1427 InterlockedIncrement(&font_data->ref);
1428 family_data->font_count++;
1429 return S_OK;
1432 static HRESULT fontcollection_add_family(struct dwrite_fontcollection *collection, struct dwrite_fontfamily_data *family)
1434 if (collection->family_alloc < collection->family_count + 1) {
1435 struct dwrite_fontfamily_data **new_list;
1436 UINT32 new_alloc;
1438 new_alloc = collection->family_alloc * 2;
1439 new_list = heap_realloc(collection->family_data, sizeof(*new_list) * new_alloc);
1440 if (!new_list)
1441 return E_OUTOFMEMORY;
1443 collection->family_alloc = new_alloc;
1444 collection->family_data = new_list;
1447 collection->family_data[collection->family_count] = family;
1448 collection->family_count++;
1450 return S_OK;
1453 static HRESULT init_font_collection(struct dwrite_fontcollection *collection, BOOL is_system)
1455 collection->IDWriteFontCollection_iface.lpVtbl = &fontcollectionvtbl;
1456 collection->ref = 1;
1457 collection->family_count = 0;
1458 collection->family_alloc = 2;
1459 collection->is_system = is_system;
1461 collection->family_data = heap_alloc(sizeof(*collection->family_data)*2);
1462 if (!collection->family_data)
1463 return E_OUTOFMEMORY;
1465 return S_OK;
1468 static HRESULT get_filestream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
1470 IDWriteFontFileLoader *loader;
1471 const void *key;
1472 UINT32 key_size;
1473 HRESULT hr;
1475 *stream = NULL;
1477 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
1478 if (FAILED(hr))
1479 return hr;
1481 hr = IDWriteFontFile_GetLoader(file, &loader);
1482 if (FAILED(hr))
1483 return hr;
1485 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
1486 IDWriteFontFileLoader_Release(loader);
1487 if (FAILED(hr))
1488 return hr;
1490 return hr;
1493 static HRESULT init_font_data(IDWriteFactory *factory, IDWriteFontFile *file, UINT32 face_index, DWRITE_FONT_FACE_TYPE face_type, struct dwrite_font_data *data)
1495 void *os2_context, *head_context, *post_context;
1496 const void *tt_os2 = NULL, *tt_head = NULL, *tt_post = NULL;
1497 IDWriteFontFileStream *stream;
1498 HRESULT hr;
1500 hr = get_filestream_from_file(file, &stream);
1501 if (FAILED(hr))
1502 return hr;
1504 data->factory = factory;
1505 data->file = file;
1506 data->face_index = face_index;
1507 data->face_type = face_type;
1508 IDWriteFontFile_AddRef(file);
1509 IDWriteFactory_AddRef(factory);
1511 opentype_get_font_table(stream, face_type, face_index, MS_OS2_TAG, &tt_os2, &os2_context, NULL, NULL);
1512 opentype_get_font_table(stream, face_type, face_index, MS_HEAD_TAG, &tt_head, &head_context, NULL, NULL);
1513 opentype_get_font_table(stream, face_type, face_index, MS_POST_TAG, &tt_post, &post_context, NULL, NULL);
1515 opentype_get_font_properties(tt_os2, tt_head, &data->stretch, &data->weight, &data->style);
1516 opentype_get_font_metrics(tt_os2, tt_head, tt_post, &data->metrics);
1518 if (tt_os2)
1519 IDWriteFontFileStream_ReleaseFileFragment(stream, os2_context);
1520 if (tt_head)
1521 IDWriteFontFileStream_ReleaseFileFragment(stream, head_context);
1522 if (tt_post)
1523 IDWriteFontFileStream_ReleaseFileFragment(stream, post_context);
1524 IDWriteFontFileStream_Release(stream);
1526 return S_OK;
1529 static HRESULT init_fontfamily_data(IDWriteLocalizedStrings *familyname, struct dwrite_fontfamily_data *data)
1531 data->ref = 1;
1532 data->font_count = 0;
1533 data->font_alloc = 2;
1535 data->fonts = heap_alloc(sizeof(*data->fonts)*data->font_alloc);
1536 if (!data->fonts) {
1537 heap_free(data);
1538 return E_OUTOFMEMORY;
1541 data->familyname = familyname;
1542 IDWriteLocalizedStrings_AddRef(familyname);
1544 return S_OK;
1547 HRESULT create_font_collection(IDWriteFactory* factory, IDWriteFontFileEnumerator *enumerator, BOOL is_system, IDWriteFontCollection **ret)
1549 struct dwrite_fontcollection *collection;
1550 BOOL current = FALSE;
1551 HRESULT hr;
1553 *ret = NULL;
1555 collection = heap_alloc(sizeof(struct dwrite_fontcollection));
1556 if (!collection) return E_OUTOFMEMORY;
1558 hr = init_font_collection(collection, is_system);
1559 if (FAILED(hr)) {
1560 heap_free(collection);
1561 return hr;
1564 *ret = &collection->IDWriteFontCollection_iface;
1566 TRACE("building font collection:\n");
1568 while (1) {
1569 DWRITE_FONT_FACE_TYPE face_type;
1570 DWRITE_FONT_FILE_TYPE file_type;
1571 IDWriteFontFile *file;
1572 UINT32 face_count;
1573 BOOL supported;
1574 int i;
1576 current = FALSE;
1577 hr = IDWriteFontFileEnumerator_MoveNext(enumerator, &current);
1578 if (FAILED(hr) || !current)
1579 break;
1581 hr = IDWriteFontFileEnumerator_GetCurrentFontFile(enumerator, &file);
1582 if (FAILED(hr))
1583 break;
1585 hr = IDWriteFontFile_Analyze(file, &supported, &file_type, &face_type, &face_count);
1586 if (FAILED(hr) || !supported || face_count == 0) {
1587 TRACE("unsupported font (0x%08x, %d, %u)\n", hr, supported, face_count);
1588 IDWriteFontFile_Release(file);
1589 continue;
1592 for (i = 0; i < face_count; i++) {
1593 IDWriteLocalizedStrings *family_name = NULL;
1594 struct dwrite_font_data *font_data;
1595 const void *name_table;
1596 void *name_context;
1597 IDWriteFontFileStream *stream;
1598 WCHAR buffer[255];
1599 UINT32 index;
1600 BOOL exists;
1602 /* alloc and init new font data structure */
1603 font_data = heap_alloc_zero(sizeof(struct dwrite_font_data));
1604 init_font_data(factory, file, i, face_type, font_data);
1606 hr = get_filestream_from_file(file, &stream);
1607 if (FAILED(hr)) {
1608 heap_free (font_data);
1609 return hr;
1612 /* get family name from font file */
1613 name_table = NULL;
1614 opentype_get_font_table(stream, face_type, i, MS_NAME_TAG, &name_table, &name_context, NULL, NULL);
1615 if (name_table)
1616 hr = opentype_get_font_strings_from_id(name_table, DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES, &family_name);
1617 IDWriteFontFileStream_Release(stream);
1619 if (FAILED(hr) || !family_name) {
1620 WARN("unable to get family name from font\n");
1621 continue;
1624 buffer[0] = 0;
1625 IDWriteLocalizedStrings_GetString(family_name, 0, buffer, sizeof(buffer)/sizeof(WCHAR));
1627 exists = FALSE;
1628 hr = collection_find_family(collection, buffer, &index, &exists);
1629 if (exists)
1630 hr = fontfamily_add_font(collection->family_data[index], font_data);
1631 else {
1632 struct dwrite_fontfamily_data *family_data;
1634 /* create and init new family */
1635 family_data = heap_alloc(sizeof(*family_data));
1636 init_fontfamily_data(family_name, family_data);
1638 /* add font to family, family - to collection */
1639 fontfamily_add_font(family_data, font_data);
1640 fontcollection_add_family(collection, family_data);
1643 IDWriteLocalizedStrings_Release(family_name);
1646 IDWriteFontFile_Release(file);
1649 return S_OK;
1652 struct system_fontfile_enumerator
1654 IDWriteFontFileEnumerator IDWriteFontFileEnumerator_iface;
1655 LONG ref;
1657 IDWriteFactory *factory;
1658 HKEY hkey;
1659 int index;
1662 static inline struct system_fontfile_enumerator *impl_from_IDWriteFontFileEnumerator(IDWriteFontFileEnumerator* iface)
1664 return CONTAINING_RECORD(iface, struct system_fontfile_enumerator, IDWriteFontFileEnumerator_iface);
1667 static HRESULT WINAPI systemfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1669 *obj = NULL;
1671 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1672 IDWriteFontFileEnumerator_AddRef(iface);
1673 *obj = iface;
1674 return S_OK;
1677 return E_NOINTERFACE;
1680 static ULONG WINAPI systemfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1682 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1683 return InterlockedIncrement(&enumerator->ref);
1686 static ULONG WINAPI systemfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1688 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1689 ULONG ref = InterlockedDecrement(&enumerator->ref);
1691 if (!ref) {
1692 IDWriteFactory_Release(enumerator->factory);
1693 RegCloseKey(enumerator->hkey);
1694 heap_free(enumerator);
1697 return ref;
1700 static HRESULT WINAPI systemfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1702 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1703 DWORD ret, type, count;
1704 HRESULT hr;
1705 BYTE *data;
1707 *file = NULL;
1709 if (enumerator->index < 0)
1710 return E_FAIL;
1712 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1713 return E_FAIL;
1715 if (!(data = heap_alloc(count)))
1716 return E_OUTOFMEMORY;
1718 ret = RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, data, &count);
1719 if (ret) {
1720 heap_free(data);
1721 return E_FAIL;
1724 hr = IDWriteFactory_CreateFontFileReference(enumerator->factory, (WCHAR*)data, NULL, file);
1725 heap_free(data);
1726 return hr;
1729 static HRESULT WINAPI systemfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1731 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1733 *current = FALSE;
1734 enumerator->index++;
1736 /* iterate until we find next string value */
1737 while (1) {
1738 DWORD type = 0, count;
1739 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1740 break;
1741 if (type == REG_SZ) {
1742 *current = TRUE;
1743 break;
1745 enumerator->index++;
1748 TRACE("index = %d, current = %d\n", enumerator->index, *current);
1749 return S_OK;
1752 static const struct IDWriteFontFileEnumeratorVtbl systemfontfileenumeratorvtbl =
1754 systemfontfileenumerator_QueryInterface,
1755 systemfontfileenumerator_AddRef,
1756 systemfontfileenumerator_Release,
1757 systemfontfileenumerator_MoveNext,
1758 systemfontfileenumerator_GetCurrentFontFile
1761 static HRESULT create_system_fontfile_enumerator(IDWriteFactory *factory, IDWriteFontFileEnumerator **ret)
1763 struct system_fontfile_enumerator *enumerator;
1764 static const WCHAR fontslistW[] = {
1765 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1766 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1767 'F','o','n','t','s',0
1770 *ret = NULL;
1772 enumerator = heap_alloc(sizeof(*enumerator));
1773 if (!enumerator)
1774 return E_OUTOFMEMORY;
1776 enumerator->IDWriteFontFileEnumerator_iface.lpVtbl = &systemfontfileenumeratorvtbl;
1777 enumerator->ref = 1;
1778 enumerator->factory = factory;
1779 enumerator->index = -1;
1780 IDWriteFactory_AddRef(factory);
1782 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, fontslistW, 0, GENERIC_READ, &enumerator->hkey)) {
1783 ERR("failed to open fonts list key\n");
1784 IDWriteFactory_Release(factory);
1785 heap_free(enumerator);
1786 return E_FAIL;
1789 *ret = &enumerator->IDWriteFontFileEnumerator_iface;
1791 return S_OK;
1794 HRESULT get_system_fontcollection(IDWriteFactory *factory, IDWriteFontCollection **collection)
1796 IDWriteFontFileEnumerator *enumerator;
1797 HRESULT hr;
1799 *collection = NULL;
1801 hr = create_system_fontfile_enumerator(factory, &enumerator);
1802 if (FAILED(hr))
1803 return hr;
1805 TRACE("building system font collection for factory %p\n", factory);
1806 hr = create_font_collection(factory, enumerator, TRUE, collection);
1807 IDWriteFontFileEnumerator_Release(enumerator);
1808 return hr;
1811 static HRESULT WINAPI dwritefontfile_QueryInterface(IDWriteFontFile *iface, REFIID riid, void **obj)
1813 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1815 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1817 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFile))
1819 *obj = iface;
1820 IDWriteFontFile_AddRef(iface);
1821 return S_OK;
1824 *obj = NULL;
1825 return E_NOINTERFACE;
1828 static ULONG WINAPI dwritefontfile_AddRef(IDWriteFontFile *iface)
1830 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1831 ULONG ref = InterlockedIncrement(&This->ref);
1832 TRACE("(%p)->(%d)\n", This, ref);
1833 return ref;
1836 static ULONG WINAPI dwritefontfile_Release(IDWriteFontFile *iface)
1838 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1839 ULONG ref = InterlockedDecrement(&This->ref);
1841 TRACE("(%p)->(%d)\n", This, ref);
1843 if (!ref)
1845 IDWriteFontFileLoader_Release(This->loader);
1846 if (This->stream) IDWriteFontFileStream_Release(This->stream);
1847 heap_free(This->reference_key);
1848 heap_free(This);
1851 return ref;
1854 static HRESULT WINAPI dwritefontfile_GetReferenceKey(IDWriteFontFile *iface, const void **fontFileReferenceKey, UINT32 *fontFileReferenceKeySize)
1856 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1857 TRACE("(%p)->(%p, %p)\n", This, fontFileReferenceKey, fontFileReferenceKeySize);
1858 *fontFileReferenceKey = This->reference_key;
1859 *fontFileReferenceKeySize = This->key_size;
1861 return S_OK;
1864 static HRESULT WINAPI dwritefontfile_GetLoader(IDWriteFontFile *iface, IDWriteFontFileLoader **fontFileLoader)
1866 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1867 TRACE("(%p)->(%p)\n", This, fontFileLoader);
1868 *fontFileLoader = This->loader;
1869 IDWriteFontFileLoader_AddRef(This->loader);
1871 return S_OK;
1874 static HRESULT WINAPI dwritefontfile_Analyze(IDWriteFontFile *iface, BOOL *isSupportedFontType, DWRITE_FONT_FILE_TYPE *fontFileType, DWRITE_FONT_FACE_TYPE *fontFaceType, UINT32 *numberOfFaces)
1876 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1877 IDWriteFontFileStream *stream;
1878 HRESULT hr;
1880 TRACE("(%p)->(%p, %p, %p, %p)\n", This, isSupportedFontType, fontFileType, fontFaceType, numberOfFaces);
1882 *isSupportedFontType = FALSE;
1883 *fontFileType = DWRITE_FONT_FILE_TYPE_UNKNOWN;
1884 if (fontFaceType)
1885 *fontFaceType = DWRITE_FONT_FACE_TYPE_UNKNOWN;
1886 *numberOfFaces = 0;
1888 hr = IDWriteFontFileLoader_CreateStreamFromKey(This->loader, This->reference_key, This->key_size, &stream);
1889 if (FAILED(hr))
1890 return S_OK;
1892 hr = opentype_analyze_font(stream, numberOfFaces, fontFileType, fontFaceType, isSupportedFontType);
1894 /* TODO: Further Analysis */
1895 IDWriteFontFileStream_Release(stream);
1896 return S_OK;
1899 static const IDWriteFontFileVtbl dwritefontfilevtbl = {
1900 dwritefontfile_QueryInterface,
1901 dwritefontfile_AddRef,
1902 dwritefontfile_Release,
1903 dwritefontfile_GetReferenceKey,
1904 dwritefontfile_GetLoader,
1905 dwritefontfile_Analyze,
1908 HRESULT create_font_file(IDWriteFontFileLoader *loader, const void *reference_key, UINT32 key_size, IDWriteFontFile **font_file)
1910 struct dwrite_fontfile *This;
1912 This = heap_alloc(sizeof(struct dwrite_fontfile));
1913 if (!This) return E_OUTOFMEMORY;
1915 This->IDWriteFontFile_iface.lpVtbl = &dwritefontfilevtbl;
1916 This->ref = 1;
1917 IDWriteFontFileLoader_AddRef(loader);
1918 This->loader = loader;
1919 This->stream = NULL;
1920 This->reference_key = heap_alloc(key_size);
1921 memcpy(This->reference_key, reference_key, key_size);
1922 This->key_size = key_size;
1924 *font_file = &This->IDWriteFontFile_iface;
1926 return S_OK;
1929 static HRESULT get_stream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
1931 IDWriteFontFileLoader *loader;
1932 UINT32 key_size;
1933 const void *key;
1934 HRESULT hr;
1936 *stream = NULL;
1937 hr = IDWriteFontFile_GetLoader(file, &loader);
1938 if (FAILED(hr))
1939 return hr;
1941 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
1942 if (FAILED(hr)) {
1943 IDWriteFontFileLoader_Release(loader);
1944 return hr;
1947 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
1948 IDWriteFontFileLoader_Release(loader);
1950 return hr;
1953 HRESULT create_fontface(DWRITE_FONT_FACE_TYPE facetype, UINT32 files_number, IDWriteFontFile* const* font_files, UINT32 index,
1954 DWRITE_FONT_SIMULATIONS simulations, IDWriteFontFace2 **ret)
1956 struct dwrite_fontface *fontface;
1957 const void *data_ptr;
1958 HRESULT hr = S_OK;
1959 void *context;
1960 UINT64 size;
1961 int i;
1963 *ret = NULL;
1965 fontface = heap_alloc(sizeof(struct dwrite_fontface));
1966 if (!fontface)
1967 return E_OUTOFMEMORY;
1969 fontface->files = heap_alloc_zero(sizeof(*fontface->files) * files_number);
1970 fontface->streams = heap_alloc_zero(sizeof(*fontface->streams) * files_number);
1972 if (!fontface->files || !fontface->streams) {
1973 heap_free(fontface->files);
1974 heap_free(fontface->streams);
1975 heap_free(fontface);
1976 return E_OUTOFMEMORY;
1979 fontface->IDWriteFontFace2_iface.lpVtbl = &dwritefontfacevtbl;
1980 fontface->ref = 1;
1981 fontface->type = facetype;
1982 fontface->file_count = files_number;
1983 fontface->cmap.data = NULL;
1984 fontface->cmap.context = NULL;
1985 fontface->cmap.size = 0;
1986 fontface->index = index;
1987 fontface->simulations = simulations;
1988 fontface->ft = NULL;
1990 for (i = 0; i < fontface->file_count; i++) {
1991 hr = get_stream_from_file(font_files[i], &fontface->streams[i]);
1992 if (FAILED(hr)) {
1993 IDWriteFontFace2_Release(&fontface->IDWriteFontFace2_iface);
1994 return hr;
1997 fontface->files[i] = font_files[i];
1998 IDWriteFontFile_AddRef(font_files[i]);
2001 get_font_properties_from_stream(fontface->streams[0], facetype, index, &fontface->metrics, NULL, NULL, NULL);
2003 hr = IDWriteFontFileStream_GetFileSize(fontface->streams[0], &size);
2004 if (FAILED(hr))
2005 goto fail;
2007 hr = IDWriteFontFileStream_ReadFileFragment(fontface->streams[0], &data_ptr, 0, size, &context);
2008 if (FAILED(hr))
2009 goto fail;
2011 hr = alloc_ft_fontface(data_ptr, size, fontface->index, &fontface->ft);
2012 IDWriteFontFileStream_ReleaseFileFragment(fontface->streams[0], context);
2013 if (FAILED(hr))
2014 goto fail;
2016 *ret = &fontface->IDWriteFontFace2_iface;
2017 return S_OK;
2019 fail:
2020 IDWriteFontFace2_Release(&fontface->IDWriteFontFace2_iface);
2021 return hr;
2024 /* IDWriteLocalFontFileLoader and its required IDWriteFontFileStream */
2025 struct local_refkey
2027 FILETIME writetime;
2028 WCHAR name[1];
2031 struct local_cached_stream
2033 struct list entry;
2034 IDWriteFontFileStream *stream;
2035 struct local_refkey *key;
2036 UINT32 key_size;
2039 struct dwrite_localfontfilestream
2041 IDWriteFontFileStream IDWriteFontFileStream_iface;
2042 LONG ref;
2044 struct local_cached_stream *entry;
2045 HANDLE handle;
2048 struct dwrite_localfontfileloader {
2049 IDWriteLocalFontFileLoader IDWriteLocalFontFileLoader_iface;
2050 LONG ref;
2052 struct list streams;
2055 static inline struct dwrite_localfontfileloader *impl_from_IDWriteLocalFontFileLoader(IDWriteLocalFontFileLoader *iface)
2057 return CONTAINING_RECORD(iface, struct dwrite_localfontfileloader, IDWriteLocalFontFileLoader_iface);
2060 static inline struct dwrite_localfontfilestream *impl_from_IDWriteFontFileStream(IDWriteFontFileStream *iface)
2062 return CONTAINING_RECORD(iface, struct dwrite_localfontfilestream, IDWriteFontFileStream_iface);
2065 static HRESULT WINAPI localfontfilestream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **obj)
2067 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2068 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2069 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileStream))
2071 *obj = iface;
2072 IDWriteFontFileStream_AddRef(iface);
2073 return S_OK;
2076 *obj = NULL;
2077 return E_NOINTERFACE;
2080 static ULONG WINAPI localfontfilestream_AddRef(IDWriteFontFileStream *iface)
2082 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2083 ULONG ref = InterlockedIncrement(&This->ref);
2084 TRACE("(%p)->(%d)\n", This, ref);
2085 return ref;
2088 static inline void release_cached_stream(struct local_cached_stream *stream)
2090 list_remove(&stream->entry);
2091 heap_free(stream->key);
2092 heap_free(stream);
2095 static ULONG WINAPI localfontfilestream_Release(IDWriteFontFileStream *iface)
2097 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2098 ULONG ref = InterlockedDecrement(&This->ref);
2100 TRACE("(%p)->(%d)\n", This, ref);
2102 if (!ref) {
2103 if (This->handle != INVALID_HANDLE_VALUE)
2104 CloseHandle(This->handle);
2105 release_cached_stream(This->entry);
2106 heap_free(This);
2109 return ref;
2112 static HRESULT WINAPI localfontfilestream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start, UINT64 offset, UINT64 fragment_size, void **fragment_context)
2114 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2115 LARGE_INTEGER distance;
2116 DWORD bytes = fragment_size;
2117 DWORD read;
2119 TRACE("(%p)->(%p, %s, %s, %p)\n",This, fragment_start,
2120 wine_dbgstr_longlong(offset), wine_dbgstr_longlong(fragment_size), fragment_context);
2122 *fragment_context = NULL;
2123 distance.QuadPart = offset;
2124 if (!SetFilePointerEx(This->handle, distance, NULL, FILE_BEGIN))
2125 return E_FAIL;
2126 *fragment_start = *fragment_context = heap_alloc(bytes);
2127 if (!*fragment_context)
2128 return E_FAIL;
2129 if (!ReadFile(This->handle, *fragment_context, bytes, &read, NULL))
2131 heap_free(*fragment_context);
2132 return E_FAIL;
2135 return S_OK;
2138 static void WINAPI localfontfilestream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
2140 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2141 TRACE("(%p)->(%p)\n", This, fragment_context);
2142 heap_free(fragment_context);
2145 static HRESULT WINAPI localfontfilestream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
2147 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2148 LARGE_INTEGER li;
2149 TRACE("(%p)->(%p)\n",This, size);
2150 GetFileSizeEx(This->handle, &li);
2151 *size = li.QuadPart;
2152 return S_OK;
2155 static HRESULT WINAPI localfontfilestream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
2157 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2158 ULARGE_INTEGER li;
2160 TRACE("(%p)->(%p)\n", This, last_writetime);
2162 li.LowPart = This->entry->key->writetime.dwLowDateTime;
2163 li.HighPart = This->entry->key->writetime.dwHighDateTime;
2164 *last_writetime = li.QuadPart;
2166 return S_OK;
2169 static const IDWriteFontFileStreamVtbl localfontfilestreamvtbl =
2171 localfontfilestream_QueryInterface,
2172 localfontfilestream_AddRef,
2173 localfontfilestream_Release,
2174 localfontfilestream_ReadFileFragment,
2175 localfontfilestream_ReleaseFileFragment,
2176 localfontfilestream_GetFileSize,
2177 localfontfilestream_GetLastWriteTime
2180 static HRESULT create_localfontfilestream(HANDLE handle, struct local_cached_stream *entry, IDWriteFontFileStream** iface)
2182 struct dwrite_localfontfilestream *This = heap_alloc(sizeof(struct dwrite_localfontfilestream));
2183 if (!This)
2184 return E_OUTOFMEMORY;
2186 This->ref = 1;
2187 This->handle = handle;
2188 This->IDWriteFontFileStream_iface.lpVtbl = &localfontfilestreamvtbl;
2189 This->entry = entry;
2191 *iface = &This->IDWriteFontFileStream_iface;
2192 return S_OK;
2195 static HRESULT WINAPI localfontfileloader_QueryInterface(IDWriteLocalFontFileLoader *iface, REFIID riid, void **obj)
2197 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2199 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2201 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileLoader) || IsEqualIID(riid, &IID_IDWriteLocalFontFileLoader))
2203 *obj = iface;
2204 IDWriteLocalFontFileLoader_AddRef(iface);
2205 return S_OK;
2208 *obj = NULL;
2209 return E_NOINTERFACE;
2212 static ULONG WINAPI localfontfileloader_AddRef(IDWriteLocalFontFileLoader *iface)
2214 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2215 ULONG ref = InterlockedIncrement(&This->ref);
2216 TRACE("(%p)->(%d)\n", This, ref);
2217 return ref;
2220 static ULONG WINAPI localfontfileloader_Release(IDWriteLocalFontFileLoader *iface)
2222 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2223 ULONG ref = InterlockedDecrement(&This->ref);
2225 TRACE("(%p)->(%d)\n", This, ref);
2227 if (!ref) {
2228 struct local_cached_stream *stream, *stream2;
2230 /* This will detach all entries from cache. Entries are released together with streams,
2231 so stream controls its lifetime. */
2232 LIST_FOR_EACH_ENTRY_SAFE(stream, stream2, &This->streams, struct local_cached_stream, entry)
2233 list_init(&stream->entry);
2235 heap_free(This);
2238 return ref;
2241 static HRESULT WINAPI localfontfileloader_CreateStreamFromKey(IDWriteLocalFontFileLoader *iface, const void *key, UINT32 key_size, IDWriteFontFileStream **ret)
2243 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2244 const struct local_refkey *refkey = key;
2245 struct local_cached_stream *stream;
2246 IDWriteFontFileStream *filestream;
2247 HANDLE handle;
2248 HRESULT hr;
2250 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, ret);
2252 TRACE("name: %s\n", debugstr_w(refkey->name));
2254 /* search cache first */
2255 LIST_FOR_EACH_ENTRY(stream, &This->streams, struct local_cached_stream, entry) {
2256 if (key_size == stream->key_size && !memcmp(stream->key, key, key_size)) {
2257 *ret = stream->stream;
2258 IDWriteFontFileStream_AddRef(*ret);
2259 return S_OK;
2263 *ret = NULL;
2265 handle = CreateFileW(refkey->name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2266 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2268 if (handle == INVALID_HANDLE_VALUE)
2269 return E_FAIL;
2271 stream = heap_alloc(sizeof(*stream));
2272 if (!stream)
2273 return E_OUTOFMEMORY;
2275 stream->key = heap_alloc(key_size);
2276 if (!stream->key) {
2277 heap_free(stream);
2278 return E_OUTOFMEMORY;
2281 stream->key_size = key_size;
2282 memcpy(stream->key, key, key_size);
2284 hr = create_localfontfilestream(handle, stream, &filestream);
2285 if (FAILED(hr)) {
2286 heap_free(stream->key);
2287 heap_free(stream);
2288 return hr;
2291 stream->stream = filestream;
2292 list_add_head(&This->streams, &stream->entry);
2294 *ret = stream->stream;
2296 return S_OK;
2299 static HRESULT WINAPI localfontfileloader_GetFilePathLengthFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, UINT32 *length)
2301 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2302 const struct local_refkey *refkey = key;
2304 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, length);
2306 *length = strlenW(refkey->name);
2307 return S_OK;
2310 static HRESULT WINAPI localfontfileloader_GetFilePathFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, WCHAR *path, UINT32 length)
2312 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2313 const struct local_refkey *refkey = key;
2315 TRACE("(%p)->(%p, %i, %p, %i)\n", This, key, key_size, path, length);
2317 if (length < strlenW(refkey->name))
2318 return E_INVALIDARG;
2320 strcpyW(path, refkey->name);
2321 return S_OK;
2324 static HRESULT WINAPI localfontfileloader_GetLastWriteTimeFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, FILETIME *writetime)
2326 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2327 const struct local_refkey *refkey = key;
2329 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, writetime);
2331 *writetime = refkey->writetime;
2332 return S_OK;
2335 static const struct IDWriteLocalFontFileLoaderVtbl localfontfileloadervtbl = {
2336 localfontfileloader_QueryInterface,
2337 localfontfileloader_AddRef,
2338 localfontfileloader_Release,
2339 localfontfileloader_CreateStreamFromKey,
2340 localfontfileloader_GetFilePathLengthFromKey,
2341 localfontfileloader_GetFilePathFromKey,
2342 localfontfileloader_GetLastWriteTimeFromKey
2345 HRESULT create_localfontfileloader(IDWriteLocalFontFileLoader** iface)
2347 struct dwrite_localfontfileloader *This = heap_alloc(sizeof(struct dwrite_localfontfileloader));
2348 if (!This)
2349 return E_OUTOFMEMORY;
2351 This->IDWriteLocalFontFileLoader_iface.lpVtbl = &localfontfileloadervtbl;
2352 This->ref = 1;
2353 list_init(&This->streams);
2355 *iface = &This->IDWriteLocalFontFileLoader_iface;
2356 return S_OK;
2359 HRESULT get_local_refkey(const WCHAR *path, const FILETIME *writetime, void **key, UINT32 *size)
2361 struct local_refkey *refkey;
2363 *size = FIELD_OFFSET(struct local_refkey, name) + (strlenW(path)+1)*sizeof(WCHAR);
2364 *key = NULL;
2366 refkey = heap_alloc(*size);
2367 if (!refkey)
2368 return E_OUTOFMEMORY;
2370 if (writetime)
2371 refkey->writetime = *writetime;
2372 else {
2373 WIN32_FILE_ATTRIBUTE_DATA info;
2375 if (GetFileAttributesExW(path, GetFileExInfoStandard, &info))
2376 refkey->writetime = info.ftLastWriteTime;
2377 else
2378 memset(&refkey->writetime, 0, sizeof(refkey->writetime));
2380 strcpyW(refkey->name, path);
2382 *key = refkey;
2384 return S_OK;