dwrite: Fix invalid parameter handling in GetGlyphIndices().
[wine/multimedia.git] / dlls / dwrite / font.c
blobc4b8818df728ae0ad267f4eb7731a5bfa2a4afba
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 UINT32 i;
393 TRACE("(%p)->(%p %u %p)\n", This, codepoints, count, glyph_indices);
395 if (!glyph_indices)
396 return E_INVALIDARG;
398 if (!codepoints) {
399 memset(glyph_indices, 0, count*sizeof(UINT16));
400 return E_INVALIDARG;
403 for (i = 0; i < count; i++)
404 glyph_indices[i] = freetype_get_glyphindex(iface, codepoints[i]);
406 return S_OK;
409 static HRESULT WINAPI dwritefontface_TryGetFontTable(IDWriteFontFace2 *iface, UINT32 table_tag,
410 const void **table_data, UINT32 *table_size, void **context, BOOL *exists)
412 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
414 TRACE("(%p)->(%u %p %p %p %p)\n", This, table_tag, table_data, table_size, context, exists);
416 return opentype_get_font_table(This->streams[0], This->type, This->index, table_tag, table_data, context, table_size, exists);
419 static void WINAPI dwritefontface_ReleaseFontTable(IDWriteFontFace2 *iface, void *table_context)
421 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
423 TRACE("(%p)->(%p)\n", This, table_context);
425 IDWriteFontFileStream_ReleaseFileFragment(This->streams[0], table_context);
428 HRESULT new_glyph_outline(UINT32 count, struct glyph_outline **ret)
430 struct glyph_outline *outline;
431 D2D1_POINT_2F *points;
432 UINT8 *tags;
434 *ret = NULL;
436 outline = heap_alloc(sizeof(*outline));
437 if (!outline)
438 return E_OUTOFMEMORY;
440 points = heap_alloc(count*sizeof(D2D1_POINT_2F));
441 tags = heap_alloc(count*sizeof(UINT8));
442 if (!points || !tags) {
443 heap_free(points);
444 heap_free(tags);
445 heap_free(outline);
446 return E_OUTOFMEMORY;
449 outline->points = points;
450 outline->tags = tags;
451 outline->count = count;
452 outline->advance = 0.0;
454 *ret = outline;
455 return S_OK;
458 static void free_glyph_outline(struct glyph_outline *outline)
460 heap_free(outline->points);
461 heap_free(outline->tags);
462 heap_free(outline);
465 static void report_glyph_outline(const struct glyph_outline *outline, IDWriteGeometrySink *sink)
467 UINT16 p;
469 for (p = 0; p < outline->count; p++) {
470 if (outline->tags[p] & OUTLINE_POINT_START) {
471 ID2D1SimplifiedGeometrySink_BeginFigure(sink, outline->points[p], D2D1_FIGURE_BEGIN_FILLED);
472 continue;
475 if (outline->tags[p] & OUTLINE_POINT_LINE)
476 ID2D1SimplifiedGeometrySink_AddLines(sink, outline->points+p, 1);
477 else if (outline->tags[p] & OUTLINE_POINT_BEZIER) {
478 static const UINT16 segment_length = 3;
479 ID2D1SimplifiedGeometrySink_AddBeziers(sink, (D2D1_BEZIER_SEGMENT*)&outline->points[p], 1);
480 p += segment_length - 1;
483 if (outline->tags[p] & OUTLINE_POINT_END)
484 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
488 static inline void translate_glyph_outline(struct glyph_outline *outline, FLOAT xoffset, FLOAT yoffset)
490 UINT16 p;
492 for (p = 0; p < outline->count; p++) {
493 outline->points[p].x += xoffset;
494 outline->points[p].y += yoffset;
498 static HRESULT WINAPI dwritefontface_GetGlyphRunOutline(IDWriteFontFace2 *iface, FLOAT emSize,
499 UINT16 const *glyphs, FLOAT const* advances, DWRITE_GLYPH_OFFSET const *offsets,
500 UINT32 count, BOOL is_sideways, BOOL is_rtl, IDWriteGeometrySink *sink)
502 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
503 FLOAT advance = 0.0;
504 HRESULT hr;
505 UINT32 g;
507 TRACE("(%p)->(%.2f %p %p %p %u %d %d %p)\n", This, emSize, glyphs, advances, offsets,
508 count, is_sideways, is_rtl, sink);
510 if (!glyphs || !sink)
511 return E_INVALIDARG;
513 if (is_sideways)
514 FIXME("sideways mode is not supported.\n");
516 for (g = 0; g < count; g++) {
517 FLOAT xoffset = 0.0, yoffset = 0.0;
518 struct glyph_outline *outline;
520 /* FIXME: cache outlines */
522 hr = freetype_get_glyph_outline(iface, emSize, glyphs[g], This->simulations, &outline);
523 if (FAILED(hr))
524 return hr;
526 /* glyph offsets act as current glyph adjustment */
527 if (offsets) {
528 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
529 yoffset -= offsets[g].ascenderOffset;
532 if (g == 0)
533 advance = is_rtl ? -outline->advance : 0.0;
535 xoffset += advance;
536 translate_glyph_outline(outline, xoffset, yoffset);
538 /* update advance to next glyph */
539 if (advances)
540 advance += is_rtl ? -advances[g] : advances[g];
541 else
542 advance += is_rtl ? -outline->advance : outline->advance;
544 report_glyph_outline(outline, sink);
545 free_glyph_outline(outline);
548 return S_OK;
551 static HRESULT WINAPI dwritefontface_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT emSize,
552 FLOAT pixels_per_dip, DWRITE_MEASURING_MODE mode, IDWriteRenderingParams* params, DWRITE_RENDERING_MODE* rendering_mode)
554 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
555 FIXME("(%p)->(%f %f %d %p %p): stub\n", This, emSize, pixels_per_dip, mode, params, rendering_mode);
556 return E_NOTIMPL;
559 static HRESULT WINAPI dwritefontface_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
560 DWRITE_MATRIX const *transform, DWRITE_FONT_METRICS *metrics)
562 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
563 DWRITE_FONT_METRICS1 metrics1;
564 HRESULT hr;
566 TRACE("(%p)->(%.2f %.2f %p %p)\n", This, emSize, pixels_per_dip, transform, metrics);
568 hr = IDWriteFontFace2_GetGdiCompatibleMetrics(iface, emSize, pixels_per_dip, transform, &metrics1);
569 if (FAILED(hr))
570 return hr;
572 memcpy(metrics, &metrics1, sizeof(*metrics));
573 return hr;
576 static HRESULT WINAPI dwritefontface_GetGdiCompatibleGlyphMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
577 DWRITE_MATRIX const *transform, BOOL use_gdi_natural, UINT16 const *glyph_indices, UINT32 glyph_count,
578 DWRITE_GLYPH_METRICS *metrics, BOOL is_sideways)
580 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
581 FIXME("(%p)->(%f %f %p %d %p %u %p %d): stub\n", This, emSize, pixels_per_dip, transform, use_gdi_natural, glyph_indices,
582 glyph_count, metrics, is_sideways);
583 return E_NOTIMPL;
586 static void WINAPI dwritefontface1_GetMetrics(IDWriteFontFace2 *iface, DWRITE_FONT_METRICS1 *metrics)
588 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
589 TRACE("(%p)->(%p)\n", This, metrics);
590 *metrics = This->metrics;
593 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT em_size, FLOAT pixels_per_dip,
594 const DWRITE_MATRIX *transform, DWRITE_FONT_METRICS1 *metrics)
596 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
597 FIXME("(%p)->(%f %f %p %p): stub\n", This, em_size, pixels_per_dip, transform, metrics);
598 return E_NOTIMPL;
601 static void WINAPI dwritefontface1_GetCaretMetrics(IDWriteFontFace2 *iface, DWRITE_CARET_METRICS *metrics)
603 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
604 TRACE("(%p)->(%p)\n", This, metrics);
605 *metrics = This->caret;
608 static HRESULT WINAPI dwritefontface1_GetUnicodeRanges(IDWriteFontFace2 *iface, UINT32 max_count,
609 DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
611 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
613 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
615 *count = 0;
616 if (max_count && !ranges)
617 return E_INVALIDARG;
619 return opentype_cmap_get_unicode_ranges(get_fontface_cmap(This), max_count, ranges, count);
622 static BOOL WINAPI dwritefontface1_IsMonospacedFont(IDWriteFontFace2 *iface)
624 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
625 TRACE("(%p)\n", This);
626 return freetype_is_monospaced(iface);
629 static HRESULT WINAPI dwritefontface1_GetDesignGlyphAdvances(IDWriteFontFace2 *iface,
630 UINT32 glyph_count, UINT16 const *glyphs, INT32 *advances, BOOL is_sideways)
632 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
633 UINT32 i;
635 TRACE("(%p)->(%u %p %p %d)\n", This, glyph_count, glyphs, advances, is_sideways);
637 for (i = 0; i < glyph_count; i++) {
638 DWRITE_GLYPH_METRICS metrics = { 0 };
639 HRESULT hr;
641 hr = IDWriteFontFace2_GetDesignGlyphMetrics(iface, glyphs + i, 1, &metrics, is_sideways);
642 if (FAILED(hr))
643 return hr;
645 advances[i] = is_sideways ? metrics.advanceHeight : metrics.advanceWidth;
648 return S_OK;
651 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleGlyphAdvances(IDWriteFontFace2 *iface,
652 FLOAT em_size, FLOAT pixels_per_dip, const DWRITE_MATRIX *transform, BOOL use_gdi_natural,
653 BOOL is_sideways, UINT32 glyph_count, UINT16 const *indices, INT32 *advances)
655 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
656 FIXME("(%p)->(%f %f %p %d %d %u %p %p): stub\n", This, em_size, pixels_per_dip, transform,
657 use_gdi_natural, is_sideways, glyph_count, indices, advances);
658 return E_NOTIMPL;
661 static HRESULT WINAPI dwritefontface1_GetKerningPairAdjustments(IDWriteFontFace2 *iface, UINT32 glyph_count,
662 const UINT16 *indices, INT32 *adjustments)
664 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
665 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, indices, adjustments);
666 return E_NOTIMPL;
669 static BOOL WINAPI dwritefontface1_HasKerningPairs(IDWriteFontFace2 *iface)
671 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
672 FIXME("(%p): stub\n", This);
673 return FALSE;
676 static HRESULT WINAPI dwritefontface1_GetRecommendedRenderingMode(IDWriteFontFace2 *iface,
677 FLOAT font_emsize, FLOAT dpiX, FLOAT dpiY, const DWRITE_MATRIX *transform, BOOL is_sideways,
678 DWRITE_OUTLINE_THRESHOLD threshold, DWRITE_MEASURING_MODE measuring_mode, DWRITE_RENDERING_MODE *rendering_mode)
680 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
681 FIXME("(%p)->(%f %f %f %p %d %d %d %p): stub\n", This, font_emsize, dpiX, dpiY, transform, is_sideways,
682 threshold, measuring_mode, rendering_mode);
683 return E_NOTIMPL;
686 static HRESULT WINAPI dwritefontface1_GetVerticalGlyphVariants(IDWriteFontFace2 *iface, UINT32 glyph_count,
687 const UINT16 *nominal_indices, UINT16 *vertical_indices)
689 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
690 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, nominal_indices, vertical_indices);
691 return E_NOTIMPL;
694 static BOOL WINAPI dwritefontface1_HasVerticalGlyphVariants(IDWriteFontFace2 *iface)
696 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
697 FIXME("(%p): stub\n", This);
698 return FALSE;
701 static BOOL WINAPI dwritefontface2_IsColorFont(IDWriteFontFace2 *iface)
703 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
704 FIXME("(%p): stub\n", This);
705 return FALSE;
708 static UINT32 WINAPI dwritefontface2_GetColorPaletteCount(IDWriteFontFace2 *iface)
710 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
711 FIXME("(%p): stub\n", This);
712 return 0;
715 static UINT32 WINAPI dwritefontface2_GetPaletteEntryCount(IDWriteFontFace2 *iface)
717 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
718 FIXME("(%p): stub\n", This);
719 return 0;
722 static HRESULT WINAPI dwritefontface2_GetPaletteEntries(IDWriteFontFace2 *iface, UINT32 palette_index,
723 UINT32 first_entry_index, UINT32 entry_count, DWRITE_COLOR_F *entries)
725 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
726 FIXME("(%p)->(%u %u %u %p): stub\n", This, palette_index, first_entry_index, entry_count, entries);
727 return E_NOTIMPL;
730 static HRESULT WINAPI dwritefontface2_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT fontEmSize,
731 FLOAT dpiX, FLOAT dpiY, DWRITE_MATRIX const *transform, BOOL is_sideways, DWRITE_OUTLINE_THRESHOLD threshold,
732 DWRITE_MEASURING_MODE measuringmode, IDWriteRenderingParams *params, DWRITE_RENDERING_MODE *renderingmode,
733 DWRITE_GRID_FIT_MODE *gridfitmode)
735 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
736 FIXME("(%p)->(%f %f %f %p %d %d %d %p %p %p): stub\n", This, fontEmSize, dpiX, dpiY, transform, is_sideways, threshold,
737 measuringmode, params, renderingmode, gridfitmode);
738 return E_NOTIMPL;
741 static const IDWriteFontFace2Vtbl dwritefontfacevtbl = {
742 dwritefontface_QueryInterface,
743 dwritefontface_AddRef,
744 dwritefontface_Release,
745 dwritefontface_GetType,
746 dwritefontface_GetFiles,
747 dwritefontface_GetIndex,
748 dwritefontface_GetSimulations,
749 dwritefontface_IsSymbolFont,
750 dwritefontface_GetMetrics,
751 dwritefontface_GetGlyphCount,
752 dwritefontface_GetDesignGlyphMetrics,
753 dwritefontface_GetGlyphIndices,
754 dwritefontface_TryGetFontTable,
755 dwritefontface_ReleaseFontTable,
756 dwritefontface_GetGlyphRunOutline,
757 dwritefontface_GetRecommendedRenderingMode,
758 dwritefontface_GetGdiCompatibleMetrics,
759 dwritefontface_GetGdiCompatibleGlyphMetrics,
760 dwritefontface1_GetMetrics,
761 dwritefontface1_GetGdiCompatibleMetrics,
762 dwritefontface1_GetCaretMetrics,
763 dwritefontface1_GetUnicodeRanges,
764 dwritefontface1_IsMonospacedFont,
765 dwritefontface1_GetDesignGlyphAdvances,
766 dwritefontface1_GetGdiCompatibleGlyphAdvances,
767 dwritefontface1_GetKerningPairAdjustments,
768 dwritefontface1_HasKerningPairs,
769 dwritefontface1_GetRecommendedRenderingMode,
770 dwritefontface1_GetVerticalGlyphVariants,
771 dwritefontface1_HasVerticalGlyphVariants,
772 dwritefontface2_IsColorFont,
773 dwritefontface2_GetColorPaletteCount,
774 dwritefontface2_GetPaletteEntryCount,
775 dwritefontface2_GetPaletteEntries,
776 dwritefontface2_GetRecommendedRenderingMode
779 HRESULT get_family_names_from_stream(IDWriteFontFileStream *stream, UINT32 index, DWRITE_FONT_FACE_TYPE facetype,
780 IDWriteLocalizedStrings **names)
782 const void *name_table = NULL;
783 void *name_context;
784 HRESULT hr = E_FAIL;
786 opentype_get_font_table(stream, facetype, index, MS_NAME_TAG, &name_table, &name_context, NULL, NULL);
787 if (name_table) {
788 hr = opentype_get_font_strings_from_id(name_table, DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES, names);
789 IDWriteFontFileStream_ReleaseFileFragment(stream, name_context);
791 else
792 *names = NULL;
794 return hr;
797 static HRESULT get_fontface_from_font(struct dwrite_font *font, IDWriteFontFace2 **fontface)
799 struct dwrite_font_data *data = font->data;
800 IDWriteFontFace *face;
801 HRESULT hr;
803 *fontface = NULL;
805 hr = IDWriteFactory2_CreateFontFace(data->factory, data->face_type, 1, &data->file,
806 data->face_index, font->simulations, &face);
807 if (FAILED(hr))
808 return hr;
810 hr = IDWriteFontFace_QueryInterface(face, &IID_IDWriteFontFace2, (void**)fontface);
811 IDWriteFontFace_Release(face);
813 return hr;
816 static HRESULT WINAPI dwritefont_QueryInterface(IDWriteFont2 *iface, REFIID riid, void **obj)
818 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
820 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
822 if (IsEqualIID(riid, &IID_IDWriteFont2) ||
823 IsEqualIID(riid, &IID_IDWriteFont1) ||
824 IsEqualIID(riid, &IID_IDWriteFont) ||
825 IsEqualIID(riid, &IID_IUnknown))
827 *obj = iface;
828 IDWriteFont2_AddRef(iface);
829 return S_OK;
832 *obj = NULL;
833 return E_NOINTERFACE;
836 static ULONG WINAPI dwritefont_AddRef(IDWriteFont2 *iface)
838 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
839 ULONG ref = InterlockedIncrement(&This->ref);
840 TRACE("(%p)->(%d)\n", This, ref);
841 return ref;
844 static ULONG WINAPI dwritefont_Release(IDWriteFont2 *iface)
846 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
847 ULONG ref = InterlockedDecrement(&This->ref);
849 TRACE("(%p)->(%d)\n", This, ref);
851 if (!ref) {
852 IDWriteFontFamily_Release(This->family);
853 release_font_data(This->data);
854 heap_free(This);
857 return ref;
860 static HRESULT WINAPI dwritefont_GetFontFamily(IDWriteFont2 *iface, IDWriteFontFamily **family)
862 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
863 TRACE("(%p)->(%p)\n", This, family);
865 *family = This->family;
866 IDWriteFontFamily_AddRef(*family);
867 return S_OK;
870 static DWRITE_FONT_WEIGHT WINAPI dwritefont_GetWeight(IDWriteFont2 *iface)
872 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
873 TRACE("(%p)\n", This);
874 return This->data->weight;
877 static DWRITE_FONT_STRETCH WINAPI dwritefont_GetStretch(IDWriteFont2 *iface)
879 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
880 TRACE("(%p)\n", This);
881 return This->data->stretch;
884 static DWRITE_FONT_STYLE WINAPI dwritefont_GetStyle(IDWriteFont2 *iface)
886 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
887 TRACE("(%p)\n", This);
888 return This->style;
891 static BOOL WINAPI dwritefont_IsSymbolFont(IDWriteFont2 *iface)
893 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
894 IDWriteFontFace2 *fontface;
895 HRESULT hr;
897 TRACE("(%p)\n", This);
899 hr = get_fontface_from_font(This, &fontface);
900 if (FAILED(hr))
901 return hr;
903 return IDWriteFontFace2_IsSymbolFont(fontface);
906 static HRESULT WINAPI dwritefont_GetFaceNames(IDWriteFont2 *iface, IDWriteLocalizedStrings **names)
908 static const WCHAR boldobliqueW[] = {'B','o','l','d',' ','O','b','l','i','q','u','e',0};
909 static const WCHAR obliqueW[] = {'O','b','l','i','q','u','e',0};
910 static const WCHAR boldW[] = {'B','o','l','d',0};
911 static const WCHAR enusW[] = {'e','n','-','u','s',0};
913 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
914 IDWriteLocalizedStrings *strings;
915 const WCHAR *name;
916 HRESULT hr;
918 TRACE("(%p)->(%p)\n", This, names);
920 *names = NULL;
922 if (This->simulations == DWRITE_FONT_SIMULATIONS_NONE) {
923 BOOL exists;
924 return IDWriteFont2_GetInformationalStrings(iface, DWRITE_INFORMATIONAL_STRING_WIN32_SUBFAMILY_NAMES,
925 names, &exists);
928 switch (This->simulations) {
929 case DWRITE_FONT_SIMULATIONS_BOLD|DWRITE_FONT_SIMULATIONS_OBLIQUE:
930 name = boldobliqueW;
931 break;
932 case DWRITE_FONT_SIMULATIONS_BOLD:
933 name = boldW;
934 break;
935 case DWRITE_FONT_SIMULATIONS_OBLIQUE:
936 name = obliqueW;
937 break;
938 default:
939 ERR("unknown simulations %d\n", This->simulations);
940 return E_FAIL;
943 hr = create_localizedstrings(&strings);
944 if (FAILED(hr)) return hr;
946 hr = add_localizedstring(strings, enusW, name);
947 if (FAILED(hr)) {
948 IDWriteLocalizedStrings_Release(strings);
949 return hr;
952 *names = strings;
954 return S_OK;
957 static HRESULT WINAPI dwritefont_GetInformationalStrings(IDWriteFont2 *iface,
958 DWRITE_INFORMATIONAL_STRING_ID stringid, IDWriteLocalizedStrings **strings, BOOL *exists)
960 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
961 struct dwrite_font_data *data = This->data;
962 HRESULT hr;
964 TRACE("(%p)->(%d %p %p)\n", This, stringid, strings, exists);
966 *exists = FALSE;
967 *strings = NULL;
969 if (stringid > DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_CID_NAME || stringid == DWRITE_INFORMATIONAL_STRING_NONE)
970 return S_OK;
972 if (!data->info_strings[stringid]) {
973 IDWriteFontFace2 *fontface;
974 const void *table_data;
975 BOOL table_exists;
976 void *context;
977 UINT32 size;
979 hr = get_fontface_from_font(This, &fontface);
980 if (FAILED(hr))
981 return hr;
983 table_exists = FALSE;
984 hr = IDWriteFontFace2_TryGetFontTable(fontface, MS_NAME_TAG, &table_data, &size, &context, &table_exists);
985 if (FAILED(hr) || !table_exists)
986 WARN("no NAME table found.\n");
988 if (table_exists) {
989 hr = opentype_get_font_strings_from_id(table_data, stringid, &data->info_strings[stringid]);
990 if (FAILED(hr) || !data->info_strings[stringid])
991 return hr;
992 IDWriteFontFace2_ReleaseFontTable(fontface, context);
996 hr = clone_localizedstring(data->info_strings[stringid], strings);
997 if (FAILED(hr))
998 return hr;
1000 *exists = TRUE;
1001 return S_OK;
1004 static DWRITE_FONT_SIMULATIONS WINAPI dwritefont_GetSimulations(IDWriteFont2 *iface)
1006 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1007 TRACE("(%p)\n", This);
1008 return This->simulations;
1011 static void WINAPI dwritefont_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS *metrics)
1013 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1015 TRACE("(%p)->(%p)\n", This, metrics);
1016 memcpy(metrics, &This->data->metrics, sizeof(*metrics));
1019 static HRESULT WINAPI dwritefont_HasCharacter(IDWriteFont2 *iface, UINT32 value, BOOL *exists)
1021 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1022 IDWriteFontFace2 *fontface;
1023 UINT16 index;
1024 HRESULT hr;
1026 TRACE("(%p)->(0x%08x %p)\n", This, value, exists);
1028 *exists = FALSE;
1030 hr = get_fontface_from_font(This, &fontface);
1031 if (FAILED(hr))
1032 return hr;
1034 index = 0;
1035 hr = IDWriteFontFace2_GetGlyphIndices(fontface, &value, 1, &index);
1036 if (FAILED(hr))
1037 return hr;
1039 *exists = index != 0;
1040 return S_OK;
1043 static HRESULT WINAPI dwritefont_CreateFontFace(IDWriteFont2 *iface, IDWriteFontFace **face)
1045 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1046 HRESULT hr;
1048 TRACE("(%p)->(%p)\n", This, face);
1050 hr = get_fontface_from_font(This, (IDWriteFontFace2**)face);
1051 if (hr == S_OK)
1052 IDWriteFontFace_AddRef(*face);
1054 return hr;
1057 static void WINAPI dwritefont1_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS1 *metrics)
1059 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1060 TRACE("(%p)->(%p)\n", This, metrics);
1061 *metrics = This->data->metrics;
1064 static void WINAPI dwritefont1_GetPanose(IDWriteFont2 *iface, DWRITE_PANOSE *panose)
1066 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1067 FIXME("(%p)->(%p): stub\n", This, panose);
1070 static HRESULT WINAPI dwritefont1_GetUnicodeRanges(IDWriteFont2 *iface, UINT32 max_count, DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
1072 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1073 IDWriteFontFace2 *fontface;
1074 HRESULT hr;
1076 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
1078 hr = get_fontface_from_font(This, &fontface);
1079 if (FAILED(hr))
1080 return hr;
1082 return IDWriteFontFace2_GetUnicodeRanges(fontface, max_count, ranges, count);
1085 static BOOL WINAPI dwritefont1_IsMonospacedFont(IDWriteFont2 *iface)
1087 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1088 IDWriteFontFace2 *fontface;
1089 HRESULT hr;
1091 TRACE("(%p)\n", This);
1093 hr = get_fontface_from_font(This, &fontface);
1094 if (FAILED(hr))
1095 return hr;
1097 return IDWriteFontFace2_IsMonospacedFont(fontface);
1100 static HRESULT WINAPI dwritefont2_IsColorFont(IDWriteFont2 *iface)
1102 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1103 IDWriteFontFace2 *fontface;
1104 HRESULT hr;
1106 TRACE("(%p)\n", This);
1108 hr = get_fontface_from_font(This, &fontface);
1109 if (FAILED(hr))
1110 return hr;
1112 return IDWriteFontFace2_IsColorFont(fontface);
1115 static const IDWriteFont2Vtbl dwritefontvtbl = {
1116 dwritefont_QueryInterface,
1117 dwritefont_AddRef,
1118 dwritefont_Release,
1119 dwritefont_GetFontFamily,
1120 dwritefont_GetWeight,
1121 dwritefont_GetStretch,
1122 dwritefont_GetStyle,
1123 dwritefont_IsSymbolFont,
1124 dwritefont_GetFaceNames,
1125 dwritefont_GetInformationalStrings,
1126 dwritefont_GetSimulations,
1127 dwritefont_GetMetrics,
1128 dwritefont_HasCharacter,
1129 dwritefont_CreateFontFace,
1130 dwritefont1_GetMetrics,
1131 dwritefont1_GetPanose,
1132 dwritefont1_GetUnicodeRanges,
1133 dwritefont1_IsMonospacedFont,
1134 dwritefont2_IsColorFont
1137 static HRESULT create_font(struct dwrite_font_data *data, IDWriteFontFamily *family, DWRITE_FONT_SIMULATIONS simulations,
1138 IDWriteFont **font)
1140 struct dwrite_font *This;
1141 *font = NULL;
1143 This = heap_alloc(sizeof(struct dwrite_font));
1144 if (!This) return E_OUTOFMEMORY;
1146 This->IDWriteFont2_iface.lpVtbl = &dwritefontvtbl;
1147 This->ref = 1;
1148 This->family = family;
1149 IDWriteFontFamily_AddRef(family);
1150 This->simulations = simulations;
1151 This->style = data->style;
1152 This->data = data;
1153 InterlockedIncrement(&This->data->ref);
1155 /* set oblique style from requested simulation */
1156 if ((simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) && data->style == DWRITE_FONT_STYLE_NORMAL)
1157 This->style = DWRITE_FONT_STYLE_OBLIQUE;
1159 *font = (IDWriteFont*)&This->IDWriteFont2_iface;
1161 return S_OK;
1164 static HRESULT WINAPI dwritefontfamily_QueryInterface(IDWriteFontFamily *iface, REFIID riid, void **obj)
1166 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1167 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1169 if (IsEqualIID(riid, &IID_IUnknown) ||
1170 IsEqualIID(riid, &IID_IDWriteFontList) ||
1171 IsEqualIID(riid, &IID_IDWriteFontFamily))
1173 *obj = iface;
1174 IDWriteFontFamily_AddRef(iface);
1175 return S_OK;
1178 *obj = NULL;
1179 return E_NOINTERFACE;
1182 static ULONG WINAPI dwritefontfamily_AddRef(IDWriteFontFamily *iface)
1184 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1185 ULONG ref = InterlockedIncrement(&This->ref);
1186 TRACE("(%p)->(%d)\n", This, ref);
1187 return ref;
1190 static ULONG WINAPI dwritefontfamily_Release(IDWriteFontFamily *iface)
1192 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1193 ULONG ref = InterlockedDecrement(&This->ref);
1195 TRACE("(%p)->(%d)\n", This, ref);
1197 if (!ref)
1199 IDWriteFontCollection_Release(This->collection);
1200 release_fontfamily_data(This->data);
1201 heap_free(This);
1204 return ref;
1207 static HRESULT WINAPI dwritefontfamily_GetFontCollection(IDWriteFontFamily *iface, IDWriteFontCollection **collection)
1209 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1210 TRACE("(%p)->(%p)\n", This, collection);
1212 *collection = This->collection;
1213 IDWriteFontCollection_AddRef(This->collection);
1214 return S_OK;
1217 static UINT32 WINAPI dwritefontfamily_GetFontCount(IDWriteFontFamily *iface)
1219 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1220 TRACE("(%p)\n", This);
1221 return This->data->font_count;
1224 static HRESULT WINAPI dwritefontfamily_GetFont(IDWriteFontFamily *iface, UINT32 index, IDWriteFont **font)
1226 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1228 TRACE("(%p)->(%u %p)\n", This, index, font);
1230 *font = NULL;
1232 if (This->data->font_count == 0)
1233 return S_FALSE;
1235 if (index >= This->data->font_count)
1236 return E_INVALIDARG;
1238 return create_font(This->data->fonts[index], iface, DWRITE_FONT_SIMULATIONS_NONE, font);
1241 static HRESULT WINAPI dwritefontfamily_GetFamilyNames(IDWriteFontFamily *iface, IDWriteLocalizedStrings **names)
1243 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1244 return clone_localizedstring(This->data->familyname, names);
1247 static inline BOOL is_matching_font_style(DWRITE_FONT_STYLE style, DWRITE_FONT_STYLE font_style)
1249 if (style == font_style)
1250 return TRUE;
1252 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) && font_style == DWRITE_FONT_STYLE_NORMAL)
1253 return TRUE;
1255 return FALSE;
1258 static HRESULT WINAPI dwritefontfamily_GetFirstMatchingFont(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1259 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFont **font)
1261 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1262 UINT32 min_weight_diff = ~0u;
1263 int found = -1, i;
1265 TRACE("(%p)->(%d %d %d %p)\n", This, weight, stretch, style, font);
1267 for (i = 0; i < This->data->font_count; i++) {
1268 if (is_matching_font_style(style, This->data->fonts[i]->style) && stretch == This->data->fonts[i]->stretch) {
1269 DWRITE_FONT_WEIGHT font_weight = This->data->fonts[i]->weight;
1270 UINT32 weight_diff = abs(font_weight - weight);
1271 if (weight_diff < min_weight_diff) {
1272 min_weight_diff = weight_diff;
1273 found = i;
1278 if (found != -1) {
1279 DWRITE_FONT_SIMULATIONS simulations = DWRITE_FONT_SIMULATIONS_NONE;
1281 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) &&
1282 This->data->fonts[found]->style == DWRITE_FONT_STYLE_NORMAL) {
1283 simulations = DWRITE_FONT_SIMULATIONS_OBLIQUE;
1285 return create_font(This->data->fonts[found], iface, simulations, font);
1287 else {
1288 *font = NULL;
1289 return DWRITE_E_NOFONT;
1293 static HRESULT WINAPI dwritefontfamily_GetMatchingFonts(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1294 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFontList **fonts)
1296 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1297 FIXME("(%p)->(%d %d %d %p): stub\n", This, weight, stretch, style, fonts);
1298 return E_NOTIMPL;
1301 static const IDWriteFontFamilyVtbl fontfamilyvtbl = {
1302 dwritefontfamily_QueryInterface,
1303 dwritefontfamily_AddRef,
1304 dwritefontfamily_Release,
1305 dwritefontfamily_GetFontCollection,
1306 dwritefontfamily_GetFontCount,
1307 dwritefontfamily_GetFont,
1308 dwritefontfamily_GetFamilyNames,
1309 dwritefontfamily_GetFirstMatchingFont,
1310 dwritefontfamily_GetMatchingFonts
1313 static HRESULT create_fontfamily(struct dwrite_fontfamily_data *data, IDWriteFontCollection *collection, IDWriteFontFamily **family)
1315 struct dwrite_fontfamily *This;
1317 *family = NULL;
1319 This = heap_alloc(sizeof(struct dwrite_fontfamily));
1320 if (!This) return E_OUTOFMEMORY;
1322 This->IDWriteFontFamily_iface.lpVtbl = &fontfamilyvtbl;
1323 This->ref = 1;
1324 This->collection = collection;
1325 IDWriteFontCollection_AddRef(collection);
1326 This->data = data;
1327 InterlockedIncrement(&This->data->ref);
1329 *family = &This->IDWriteFontFamily_iface;
1331 return S_OK;
1334 BOOL is_system_collection(IDWriteFontCollection *collection)
1336 void *obj;
1337 return IDWriteFontCollection_QueryInterface(collection, &IID_issystemcollection, (void**)&obj) == S_OK;
1340 static HRESULT WINAPI dwritefontcollection_QueryInterface(IDWriteFontCollection *iface, REFIID riid, void **obj)
1342 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1343 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1345 if (IsEqualIID(riid, &IID_IUnknown) ||
1346 IsEqualIID(riid, &IID_IDWriteFontCollection))
1348 *obj = iface;
1349 IDWriteFontCollection_AddRef(iface);
1350 return S_OK;
1353 *obj = NULL;
1355 if (This->is_system && IsEqualIID(riid, &IID_issystemcollection))
1356 return S_OK;
1358 return E_NOINTERFACE;
1361 static ULONG WINAPI dwritefontcollection_AddRef(IDWriteFontCollection *iface)
1363 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1364 ULONG ref = InterlockedIncrement(&This->ref);
1365 TRACE("(%p)->(%d)\n", This, ref);
1366 return ref;
1369 static ULONG WINAPI dwritefontcollection_Release(IDWriteFontCollection *iface)
1371 unsigned int i;
1372 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1373 ULONG ref = InterlockedDecrement(&This->ref);
1374 TRACE("(%p)->(%d)\n", This, ref);
1376 if (!ref) {
1377 for (i = 0; i < This->family_count; i++)
1378 release_fontfamily_data(This->family_data[i]);
1379 heap_free(This->family_data);
1380 heap_free(This);
1383 return ref;
1386 static UINT32 WINAPI dwritefontcollection_GetFontFamilyCount(IDWriteFontCollection *iface)
1388 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1389 TRACE("(%p)\n", This);
1390 return This->family_count;
1393 static HRESULT WINAPI dwritefontcollection_GetFontFamily(IDWriteFontCollection *iface, UINT32 index, IDWriteFontFamily **family)
1395 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1397 TRACE("(%p)->(%u %p)\n", This, index, family);
1399 if (index >= This->family_count) {
1400 *family = NULL;
1401 return E_FAIL;
1404 return create_fontfamily(This->family_data[index], iface, family);
1407 static UINT32 collection_find_family(struct dwrite_fontcollection *collection, const WCHAR *name)
1409 UINT32 i;
1411 for (i = 0; i < collection->family_count; i++) {
1412 IDWriteLocalizedStrings *family_name = collection->family_data[i]->familyname;
1413 HRESULT hr;
1414 int j;
1416 for (j = 0; j < IDWriteLocalizedStrings_GetCount(family_name); j++) {
1417 WCHAR buffer[255];
1418 hr = IDWriteLocalizedStrings_GetString(family_name, j, buffer, 255);
1419 if (SUCCEEDED(hr) && !strcmpW(buffer, name))
1420 return i;
1424 return ~0u;
1427 static HRESULT WINAPI dwritefontcollection_FindFamilyName(IDWriteFontCollection *iface, const WCHAR *name, UINT32 *index, BOOL *exists)
1429 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1430 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(name), index, exists);
1431 *index = collection_find_family(This, name);
1432 *exists = *index != ~0u;
1433 return S_OK;
1436 static BOOL is_same_fontfile(IDWriteFontFile *left, IDWriteFontFile *right)
1438 UINT32 left_key_size, right_key_size;
1439 const void *left_key, *right_key;
1440 HRESULT hr;
1442 if (left == right)
1443 return TRUE;
1445 hr = IDWriteFontFile_GetReferenceKey(left, &left_key, &left_key_size);
1446 if (FAILED(hr))
1447 return FALSE;
1449 hr = IDWriteFontFile_GetReferenceKey(right, &right_key, &right_key_size);
1450 if (FAILED(hr))
1451 return FALSE;
1453 if (left_key_size != right_key_size)
1454 return FALSE;
1456 return !memcmp(left_key, right_key, left_key_size);
1459 static HRESULT WINAPI dwritefontcollection_GetFontFromFontFace(IDWriteFontCollection *iface, IDWriteFontFace *face, IDWriteFont **font)
1461 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1462 struct dwrite_fontfamily_data *found_family = NULL;
1463 struct dwrite_font_data *found_font = NULL;
1464 DWRITE_FONT_SIMULATIONS simulations;
1465 IDWriteFontFamily *family;
1466 UINT32 i, j, face_index;
1467 IDWriteFontFile *file;
1468 HRESULT hr;
1470 TRACE("(%p)->(%p %p)\n", This, face, font);
1472 *font = NULL;
1474 if (!face)
1475 return E_INVALIDARG;
1477 i = 1;
1478 hr = IDWriteFontFace_GetFiles(face, &i, &file);
1479 if (FAILED(hr))
1480 return hr;
1481 face_index = IDWriteFontFace_GetIndex(face);
1483 for (i = 0; i < This->family_count; i++) {
1484 struct dwrite_fontfamily_data *family_data = This->family_data[i];
1485 for (j = 0; j < family_data->font_count; j++) {
1486 struct dwrite_font_data *font_data = family_data->fonts[j];
1488 if (face_index == font_data->face_index && is_same_fontfile(file, font_data->file)) {
1489 found_font = font_data;
1490 found_family = family_data;
1491 break;
1496 if (!found_font)
1497 return DWRITE_E_NOFONT;
1499 hr = create_fontfamily(found_family, iface, &family);
1500 if (FAILED(hr))
1501 return hr;
1503 simulations = IDWriteFontFace_GetSimulations(face);
1504 hr = create_font(found_font, family, simulations, font);
1505 IDWriteFontFamily_Release(family);
1506 return hr;
1509 static const IDWriteFontCollectionVtbl fontcollectionvtbl = {
1510 dwritefontcollection_QueryInterface,
1511 dwritefontcollection_AddRef,
1512 dwritefontcollection_Release,
1513 dwritefontcollection_GetFontFamilyCount,
1514 dwritefontcollection_GetFontFamily,
1515 dwritefontcollection_FindFamilyName,
1516 dwritefontcollection_GetFontFromFontFace
1519 static HRESULT fontfamily_add_font(struct dwrite_fontfamily_data *family_data, struct dwrite_font_data *font_data)
1521 if (family_data->font_count + 1 >= family_data->font_alloc) {
1522 struct dwrite_font_data **new_list;
1523 UINT32 new_alloc;
1525 new_alloc = family_data->font_alloc * 2;
1526 new_list = heap_realloc(family_data->fonts, sizeof(*family_data->fonts) * new_alloc);
1527 if (!new_list)
1528 return E_OUTOFMEMORY;
1529 family_data->fonts = new_list;
1530 family_data->font_alloc = new_alloc;
1533 family_data->fonts[family_data->font_count] = font_data;
1534 family_data->font_count++;
1535 return S_OK;
1538 static HRESULT fontcollection_add_family(struct dwrite_fontcollection *collection, struct dwrite_fontfamily_data *family)
1540 if (collection->family_alloc < collection->family_count + 1) {
1541 struct dwrite_fontfamily_data **new_list;
1542 UINT32 new_alloc;
1544 new_alloc = collection->family_alloc * 2;
1545 new_list = heap_realloc(collection->family_data, sizeof(*new_list) * new_alloc);
1546 if (!new_list)
1547 return E_OUTOFMEMORY;
1549 collection->family_alloc = new_alloc;
1550 collection->family_data = new_list;
1553 collection->family_data[collection->family_count] = family;
1554 collection->family_count++;
1556 return S_OK;
1559 static HRESULT init_font_collection(struct dwrite_fontcollection *collection, BOOL is_system)
1561 collection->IDWriteFontCollection_iface.lpVtbl = &fontcollectionvtbl;
1562 collection->ref = 1;
1563 collection->family_count = 0;
1564 collection->family_alloc = 2;
1565 collection->is_system = is_system;
1567 collection->family_data = heap_alloc(sizeof(*collection->family_data)*2);
1568 if (!collection->family_data)
1569 return E_OUTOFMEMORY;
1571 return S_OK;
1574 HRESULT get_filestream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
1576 IDWriteFontFileLoader *loader;
1577 const void *key;
1578 UINT32 key_size;
1579 HRESULT hr;
1581 *stream = NULL;
1583 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
1584 if (FAILED(hr))
1585 return hr;
1587 hr = IDWriteFontFile_GetLoader(file, &loader);
1588 if (FAILED(hr))
1589 return hr;
1591 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
1592 IDWriteFontFileLoader_Release(loader);
1593 if (FAILED(hr))
1594 return hr;
1596 return hr;
1599 static HRESULT init_font_data(IDWriteFactory2 *factory, IDWriteFontFile *file, UINT32 face_index, DWRITE_FONT_FACE_TYPE face_type,
1600 IDWriteFontFileStream **stream, struct dwrite_font_data **ret)
1602 void *os2_context, *head_context;
1603 const void *tt_os2 = NULL, *tt_head = NULL;
1604 struct dwrite_font_data *data;
1605 HRESULT hr;
1607 data = heap_alloc_zero(sizeof(*data));
1608 if (!data)
1609 return E_OUTOFMEMORY;
1611 hr = get_filestream_from_file(file, stream);
1612 if (FAILED(hr)) {
1613 heap_free(data);
1614 return hr;
1617 data->ref = 1;
1618 data->factory = factory;
1619 data->file = file;
1620 data->face_index = face_index;
1621 data->face_type = face_type;
1622 IDWriteFontFile_AddRef(file);
1623 IDWriteFactory2_AddRef(factory);
1625 opentype_get_font_table(*stream, face_type, face_index, MS_OS2_TAG, &tt_os2, &os2_context, NULL, NULL);
1626 opentype_get_font_table(*stream, face_type, face_index, MS_HEAD_TAG, &tt_head, &head_context, NULL, NULL);
1628 opentype_get_font_properties(*stream, face_type, face_index, &data->stretch, &data->weight, &data->style);
1629 opentype_get_font_metrics(*stream, face_type, face_index, &data->metrics, NULL);
1631 if (tt_os2)
1632 IDWriteFontFileStream_ReleaseFileFragment(*stream, os2_context);
1633 if (tt_head)
1634 IDWriteFontFileStream_ReleaseFileFragment(*stream, head_context);
1636 *ret = data;
1637 return S_OK;
1640 static HRESULT init_fontfamily_data(IDWriteLocalizedStrings *familyname, struct dwrite_fontfamily_data **ret)
1642 struct dwrite_fontfamily_data *data;
1644 data = heap_alloc(sizeof(*data));
1645 if (!data)
1646 return E_OUTOFMEMORY;
1648 data->ref = 1;
1649 data->font_count = 0;
1650 data->font_alloc = 2;
1652 data->fonts = heap_alloc(sizeof(*data->fonts)*data->font_alloc);
1653 if (!data->fonts) {
1654 heap_free(data);
1655 return E_OUTOFMEMORY;
1658 data->familyname = familyname;
1659 IDWriteLocalizedStrings_AddRef(familyname);
1661 *ret = data;
1662 return S_OK;
1665 HRESULT create_font_collection(IDWriteFactory2* factory, IDWriteFontFileEnumerator *enumerator, BOOL is_system, IDWriteFontCollection **ret)
1667 struct dwrite_fontcollection *collection;
1668 BOOL current = FALSE;
1669 HRESULT hr = S_OK;
1671 *ret = NULL;
1673 collection = heap_alloc(sizeof(struct dwrite_fontcollection));
1674 if (!collection) return E_OUTOFMEMORY;
1676 hr = init_font_collection(collection, is_system);
1677 if (FAILED(hr)) {
1678 heap_free(collection);
1679 return hr;
1682 *ret = &collection->IDWriteFontCollection_iface;
1684 TRACE("building font collection:\n");
1686 while (hr == S_OK) {
1687 DWRITE_FONT_FACE_TYPE face_type;
1688 DWRITE_FONT_FILE_TYPE file_type;
1689 IDWriteFontFile *file;
1690 UINT32 face_count;
1691 BOOL supported;
1692 int i;
1694 current = FALSE;
1695 hr = IDWriteFontFileEnumerator_MoveNext(enumerator, &current);
1696 if (FAILED(hr) || !current)
1697 break;
1699 hr = IDWriteFontFileEnumerator_GetCurrentFontFile(enumerator, &file);
1700 if (FAILED(hr))
1701 break;
1703 hr = IDWriteFontFile_Analyze(file, &supported, &file_type, &face_type, &face_count);
1704 if (FAILED(hr) || !supported || face_count == 0) {
1705 TRACE("unsupported font (0x%08x, %d, %u)\n", hr, supported, face_count);
1706 IDWriteFontFile_Release(file);
1707 continue;
1710 for (i = 0; i < face_count; i++) {
1711 IDWriteLocalizedStrings *family_name = NULL;
1712 struct dwrite_font_data *font_data;
1713 IDWriteFontFileStream *stream;
1714 WCHAR buffer[255];
1715 UINT32 index;
1717 /* alloc and init new font data structure */
1718 hr = init_font_data(factory, file, i, face_type, &stream, &font_data);
1719 if (FAILED(hr))
1720 break;
1722 /* get family name from font file */
1723 hr = get_family_names_from_stream(stream, i, face_type, &family_name);
1724 IDWriteFontFileStream_Release(stream);
1725 if (FAILED(hr)) {
1726 WARN("unable to get family name from font\n");
1727 release_font_data(font_data);
1728 continue;
1731 buffer[0] = 0;
1732 IDWriteLocalizedStrings_GetString(family_name, 0, buffer, sizeof(buffer)/sizeof(WCHAR));
1734 index = collection_find_family(collection, buffer);
1735 if (index != ~0u)
1736 hr = fontfamily_add_font(collection->family_data[index], font_data);
1737 else {
1738 struct dwrite_fontfamily_data *family_data;
1740 /* create and init new family */
1741 hr = init_fontfamily_data(family_name, &family_data);
1742 if (hr == S_OK) {
1743 /* add font to family, family - to collection */
1744 hr = fontfamily_add_font(family_data, font_data);
1745 if (hr == S_OK)
1746 hr = fontcollection_add_family(collection, family_data);
1748 if (FAILED(hr))
1749 release_fontfamily_data(family_data);
1753 IDWriteLocalizedStrings_Release(family_name);
1755 if (FAILED(hr))
1756 break;
1759 IDWriteFontFile_Release(file);
1762 return hr;
1765 struct system_fontfile_enumerator
1767 IDWriteFontFileEnumerator IDWriteFontFileEnumerator_iface;
1768 LONG ref;
1770 IDWriteFactory2 *factory;
1771 HKEY hkey;
1772 int index;
1775 static inline struct system_fontfile_enumerator *impl_from_IDWriteFontFileEnumerator(IDWriteFontFileEnumerator* iface)
1777 return CONTAINING_RECORD(iface, struct system_fontfile_enumerator, IDWriteFontFileEnumerator_iface);
1780 static HRESULT WINAPI systemfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1782 *obj = NULL;
1784 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1785 IDWriteFontFileEnumerator_AddRef(iface);
1786 *obj = iface;
1787 return S_OK;
1790 return E_NOINTERFACE;
1793 static ULONG WINAPI systemfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1795 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1796 return InterlockedIncrement(&enumerator->ref);
1799 static ULONG WINAPI systemfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1801 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1802 ULONG ref = InterlockedDecrement(&enumerator->ref);
1804 if (!ref) {
1805 IDWriteFactory2_Release(enumerator->factory);
1806 RegCloseKey(enumerator->hkey);
1807 heap_free(enumerator);
1810 return ref;
1813 static HRESULT WINAPI systemfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1815 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1816 DWORD ret, type, count;
1817 WCHAR *filename;
1818 HRESULT hr;
1820 *file = NULL;
1822 if (enumerator->index < 0)
1823 return E_FAIL;
1825 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1826 return E_FAIL;
1828 if (!(filename = heap_alloc(count)))
1829 return E_OUTOFMEMORY;
1831 ret = RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, (BYTE*)filename, &count);
1832 if (ret) {
1833 heap_free(filename);
1834 return E_FAIL;
1837 /* Fonts installed in 'Fonts' system dir don't get full path in registry font files cache */
1838 if (!strchrW(filename, '\\')) {
1839 static const WCHAR fontsW[] = {'\\','f','o','n','t','s','\\',0};
1840 WCHAR fullpathW[MAX_PATH];
1842 GetWindowsDirectoryW(fullpathW, sizeof(fullpathW)/sizeof(WCHAR));
1843 strcatW(fullpathW, fontsW);
1844 strcatW(fullpathW, filename);
1846 hr = IDWriteFactory2_CreateFontFileReference(enumerator->factory, fullpathW, NULL, file);
1848 else
1849 hr = IDWriteFactory2_CreateFontFileReference(enumerator->factory, filename, NULL, file);
1851 heap_free(filename);
1852 return hr;
1855 static HRESULT WINAPI systemfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1857 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1859 *current = FALSE;
1860 enumerator->index++;
1862 /* iterate until we find next string value */
1863 while (1) {
1864 DWORD type = 0, count;
1865 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1866 break;
1867 if (type == REG_SZ) {
1868 *current = TRUE;
1869 break;
1871 enumerator->index++;
1874 TRACE("index = %d, current = %d\n", enumerator->index, *current);
1875 return S_OK;
1878 static const struct IDWriteFontFileEnumeratorVtbl systemfontfileenumeratorvtbl =
1880 systemfontfileenumerator_QueryInterface,
1881 systemfontfileenumerator_AddRef,
1882 systemfontfileenumerator_Release,
1883 systemfontfileenumerator_MoveNext,
1884 systemfontfileenumerator_GetCurrentFontFile
1887 static HRESULT create_system_fontfile_enumerator(IDWriteFactory2 *factory, IDWriteFontFileEnumerator **ret)
1889 struct system_fontfile_enumerator *enumerator;
1890 static const WCHAR fontslistW[] = {
1891 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1892 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1893 'F','o','n','t','s',0
1896 *ret = NULL;
1898 enumerator = heap_alloc(sizeof(*enumerator));
1899 if (!enumerator)
1900 return E_OUTOFMEMORY;
1902 enumerator->IDWriteFontFileEnumerator_iface.lpVtbl = &systemfontfileenumeratorvtbl;
1903 enumerator->ref = 1;
1904 enumerator->factory = factory;
1905 enumerator->index = -1;
1906 IDWriteFactory2_AddRef(factory);
1908 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, fontslistW, 0, GENERIC_READ, &enumerator->hkey)) {
1909 ERR("failed to open fonts list key\n");
1910 IDWriteFactory2_Release(factory);
1911 heap_free(enumerator);
1912 return E_FAIL;
1915 *ret = &enumerator->IDWriteFontFileEnumerator_iface;
1917 return S_OK;
1920 HRESULT get_system_fontcollection(IDWriteFactory2 *factory, IDWriteFontCollection **collection)
1922 IDWriteFontFileEnumerator *enumerator;
1923 HRESULT hr;
1925 *collection = NULL;
1927 hr = create_system_fontfile_enumerator(factory, &enumerator);
1928 if (FAILED(hr))
1929 return hr;
1931 TRACE("building system font collection for factory %p\n", factory);
1932 hr = create_font_collection(factory, enumerator, TRUE, collection);
1933 IDWriteFontFileEnumerator_Release(enumerator);
1934 return hr;
1937 static HRESULT WINAPI eudcfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1939 *obj = NULL;
1941 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1942 IDWriteFontFileEnumerator_AddRef(iface);
1943 *obj = iface;
1944 return S_OK;
1947 return E_NOINTERFACE;
1950 static ULONG WINAPI eudcfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1952 return 2;
1955 static ULONG WINAPI eudcfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1957 return 1;
1960 static HRESULT WINAPI eudcfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1962 *file = NULL;
1963 return E_FAIL;
1966 static HRESULT WINAPI eudcfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1968 *current = FALSE;
1969 return S_OK;
1972 static const struct IDWriteFontFileEnumeratorVtbl eudcfontfileenumeratorvtbl =
1974 eudcfontfileenumerator_QueryInterface,
1975 eudcfontfileenumerator_AddRef,
1976 eudcfontfileenumerator_Release,
1977 eudcfontfileenumerator_MoveNext,
1978 eudcfontfileenumerator_GetCurrentFontFile
1981 static IDWriteFontFileEnumerator eudc_fontfile_enumerator = { &eudcfontfileenumeratorvtbl };
1983 HRESULT get_eudc_fontcollection(IDWriteFactory2 *factory, IDWriteFontCollection **collection)
1985 TRACE("building EUDC font collection for factory %p\n", factory);
1986 return create_font_collection(factory, &eudc_fontfile_enumerator, FALSE, collection);
1989 static HRESULT WINAPI dwritefontfile_QueryInterface(IDWriteFontFile *iface, REFIID riid, void **obj)
1991 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1993 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1995 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFile))
1997 *obj = iface;
1998 IDWriteFontFile_AddRef(iface);
1999 return S_OK;
2002 *obj = NULL;
2003 return E_NOINTERFACE;
2006 static ULONG WINAPI dwritefontfile_AddRef(IDWriteFontFile *iface)
2008 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2009 ULONG ref = InterlockedIncrement(&This->ref);
2010 TRACE("(%p)->(%d)\n", This, ref);
2011 return ref;
2014 static ULONG WINAPI dwritefontfile_Release(IDWriteFontFile *iface)
2016 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2017 ULONG ref = InterlockedDecrement(&This->ref);
2019 TRACE("(%p)->(%d)\n", This, ref);
2021 if (!ref)
2023 IDWriteFontFileLoader_Release(This->loader);
2024 if (This->stream) IDWriteFontFileStream_Release(This->stream);
2025 heap_free(This->reference_key);
2026 heap_free(This);
2029 return ref;
2032 static HRESULT WINAPI dwritefontfile_GetReferenceKey(IDWriteFontFile *iface, const void **fontFileReferenceKey, UINT32 *fontFileReferenceKeySize)
2034 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2035 TRACE("(%p)->(%p, %p)\n", This, fontFileReferenceKey, fontFileReferenceKeySize);
2036 *fontFileReferenceKey = This->reference_key;
2037 *fontFileReferenceKeySize = This->key_size;
2039 return S_OK;
2042 static HRESULT WINAPI dwritefontfile_GetLoader(IDWriteFontFile *iface, IDWriteFontFileLoader **fontFileLoader)
2044 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2045 TRACE("(%p)->(%p)\n", This, fontFileLoader);
2046 *fontFileLoader = This->loader;
2047 IDWriteFontFileLoader_AddRef(This->loader);
2049 return S_OK;
2052 static HRESULT WINAPI dwritefontfile_Analyze(IDWriteFontFile *iface, BOOL *isSupportedFontType, DWRITE_FONT_FILE_TYPE *fontFileType, DWRITE_FONT_FACE_TYPE *fontFaceType, UINT32 *numberOfFaces)
2054 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2055 IDWriteFontFileStream *stream;
2056 HRESULT hr;
2058 TRACE("(%p)->(%p, %p, %p, %p)\n", This, isSupportedFontType, fontFileType, fontFaceType, numberOfFaces);
2060 *isSupportedFontType = FALSE;
2061 *fontFileType = DWRITE_FONT_FILE_TYPE_UNKNOWN;
2062 if (fontFaceType)
2063 *fontFaceType = DWRITE_FONT_FACE_TYPE_UNKNOWN;
2064 *numberOfFaces = 0;
2066 hr = IDWriteFontFileLoader_CreateStreamFromKey(This->loader, This->reference_key, This->key_size, &stream);
2067 if (FAILED(hr))
2068 return S_OK;
2070 hr = opentype_analyze_font(stream, numberOfFaces, fontFileType, fontFaceType, isSupportedFontType);
2072 /* TODO: Further Analysis */
2073 IDWriteFontFileStream_Release(stream);
2074 return S_OK;
2077 static const IDWriteFontFileVtbl dwritefontfilevtbl = {
2078 dwritefontfile_QueryInterface,
2079 dwritefontfile_AddRef,
2080 dwritefontfile_Release,
2081 dwritefontfile_GetReferenceKey,
2082 dwritefontfile_GetLoader,
2083 dwritefontfile_Analyze,
2086 HRESULT create_font_file(IDWriteFontFileLoader *loader, const void *reference_key, UINT32 key_size, IDWriteFontFile **font_file)
2088 struct dwrite_fontfile *This;
2090 This = heap_alloc(sizeof(struct dwrite_fontfile));
2091 if (!This) return E_OUTOFMEMORY;
2093 This->IDWriteFontFile_iface.lpVtbl = &dwritefontfilevtbl;
2094 This->ref = 1;
2095 IDWriteFontFileLoader_AddRef(loader);
2096 This->loader = loader;
2097 This->stream = NULL;
2098 This->reference_key = heap_alloc(key_size);
2099 memcpy(This->reference_key, reference_key, key_size);
2100 This->key_size = key_size;
2102 *font_file = &This->IDWriteFontFile_iface;
2104 return S_OK;
2107 static HRESULT get_stream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
2109 IDWriteFontFileLoader *loader;
2110 UINT32 key_size;
2111 const void *key;
2112 HRESULT hr;
2114 *stream = NULL;
2115 hr = IDWriteFontFile_GetLoader(file, &loader);
2116 if (FAILED(hr))
2117 return hr;
2119 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
2120 if (FAILED(hr)) {
2121 IDWriteFontFileLoader_Release(loader);
2122 return hr;
2125 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
2126 IDWriteFontFileLoader_Release(loader);
2128 return hr;
2131 HRESULT create_fontface(DWRITE_FONT_FACE_TYPE facetype, UINT32 files_number, IDWriteFontFile* const* font_files, UINT32 index,
2132 DWRITE_FONT_SIMULATIONS simulations, IDWriteFontFace2 **ret)
2134 struct dwrite_fontface *fontface;
2135 HRESULT hr = S_OK;
2136 int i;
2138 *ret = NULL;
2140 fontface = heap_alloc(sizeof(struct dwrite_fontface));
2141 if (!fontface)
2142 return E_OUTOFMEMORY;
2144 fontface->files = heap_alloc_zero(sizeof(*fontface->files) * files_number);
2145 fontface->streams = heap_alloc_zero(sizeof(*fontface->streams) * files_number);
2147 if (!fontface->files || !fontface->streams) {
2148 heap_free(fontface->files);
2149 heap_free(fontface->streams);
2150 heap_free(fontface);
2151 return E_OUTOFMEMORY;
2154 fontface->IDWriteFontFace2_iface.lpVtbl = &dwritefontfacevtbl;
2155 fontface->ref = 1;
2156 fontface->type = facetype;
2157 fontface->file_count = files_number;
2158 fontface->cmap.data = NULL;
2159 fontface->cmap.context = NULL;
2160 fontface->cmap.size = 0;
2161 fontface->index = index;
2162 fontface->simulations = simulations;
2163 memset(fontface->glyphs, 0, sizeof(fontface->glyphs));
2165 for (i = 0; i < fontface->file_count; i++) {
2166 hr = get_stream_from_file(font_files[i], &fontface->streams[i]);
2167 if (FAILED(hr)) {
2168 IDWriteFontFace2_Release(&fontface->IDWriteFontFace2_iface);
2169 return hr;
2172 fontface->files[i] = font_files[i];
2173 IDWriteFontFile_AddRef(font_files[i]);
2176 opentype_get_font_metrics(fontface->streams[0], facetype, index, &fontface->metrics, &fontface->caret);
2177 if (simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
2178 /* TODO: test what happens if caret is already slanted */
2179 if (fontface->caret.slopeRise == 1) {
2180 fontface->caret.slopeRise = fontface->metrics.designUnitsPerEm;
2181 fontface->caret.slopeRun = fontface->caret.slopeRise / 3;
2185 *ret = &fontface->IDWriteFontFace2_iface;
2186 return S_OK;
2189 /* IDWriteLocalFontFileLoader and its required IDWriteFontFileStream */
2190 struct local_refkey
2192 FILETIME writetime;
2193 WCHAR name[1];
2196 struct local_cached_stream
2198 struct list entry;
2199 IDWriteFontFileStream *stream;
2200 struct local_refkey *key;
2201 UINT32 key_size;
2204 struct dwrite_localfontfilestream
2206 IDWriteFontFileStream IDWriteFontFileStream_iface;
2207 LONG ref;
2209 struct local_cached_stream *entry;
2210 const void *file_ptr;
2211 UINT64 size;
2214 struct dwrite_localfontfileloader {
2215 IDWriteLocalFontFileLoader IDWriteLocalFontFileLoader_iface;
2216 LONG ref;
2218 struct list streams;
2221 static inline struct dwrite_localfontfileloader *impl_from_IDWriteLocalFontFileLoader(IDWriteLocalFontFileLoader *iface)
2223 return CONTAINING_RECORD(iface, struct dwrite_localfontfileloader, IDWriteLocalFontFileLoader_iface);
2226 static inline struct dwrite_localfontfilestream *impl_from_IDWriteFontFileStream(IDWriteFontFileStream *iface)
2228 return CONTAINING_RECORD(iface, struct dwrite_localfontfilestream, IDWriteFontFileStream_iface);
2231 static HRESULT WINAPI localfontfilestream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **obj)
2233 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2234 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2235 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileStream))
2237 *obj = iface;
2238 IDWriteFontFileStream_AddRef(iface);
2239 return S_OK;
2242 *obj = NULL;
2243 return E_NOINTERFACE;
2246 static ULONG WINAPI localfontfilestream_AddRef(IDWriteFontFileStream *iface)
2248 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2249 ULONG ref = InterlockedIncrement(&This->ref);
2250 TRACE("(%p)->(%d)\n", This, ref);
2251 return ref;
2254 static inline void release_cached_stream(struct local_cached_stream *stream)
2256 list_remove(&stream->entry);
2257 heap_free(stream->key);
2258 heap_free(stream);
2261 static ULONG WINAPI localfontfilestream_Release(IDWriteFontFileStream *iface)
2263 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2264 ULONG ref = InterlockedDecrement(&This->ref);
2266 TRACE("(%p)->(%d)\n", This, ref);
2268 if (!ref) {
2269 UnmapViewOfFile(This->file_ptr);
2270 release_cached_stream(This->entry);
2271 heap_free(This);
2274 return ref;
2277 static HRESULT WINAPI localfontfilestream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start, UINT64 offset, UINT64 fragment_size, void **fragment_context)
2279 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2281 TRACE("(%p)->(%p, %s, %s, %p)\n",This, fragment_start,
2282 wine_dbgstr_longlong(offset), wine_dbgstr_longlong(fragment_size), fragment_context);
2284 *fragment_context = NULL;
2286 if ((offset >= This->size - 1) || (fragment_size > This->size - offset)) {
2287 *fragment_start = NULL;
2288 return E_FAIL;
2291 *fragment_start = (char*)This->file_ptr + offset;
2292 return S_OK;
2295 static void WINAPI localfontfilestream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
2297 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2298 TRACE("(%p)->(%p)\n", This, fragment_context);
2301 static HRESULT WINAPI localfontfilestream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
2303 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2304 TRACE("(%p)->(%p)\n", This, size);
2305 *size = This->size;
2306 return S_OK;
2309 static HRESULT WINAPI localfontfilestream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
2311 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2312 ULARGE_INTEGER li;
2314 TRACE("(%p)->(%p)\n", This, last_writetime);
2316 li.u.LowPart = This->entry->key->writetime.dwLowDateTime;
2317 li.u.HighPart = This->entry->key->writetime.dwHighDateTime;
2318 *last_writetime = li.QuadPart;
2320 return S_OK;
2323 static const IDWriteFontFileStreamVtbl localfontfilestreamvtbl =
2325 localfontfilestream_QueryInterface,
2326 localfontfilestream_AddRef,
2327 localfontfilestream_Release,
2328 localfontfilestream_ReadFileFragment,
2329 localfontfilestream_ReleaseFileFragment,
2330 localfontfilestream_GetFileSize,
2331 localfontfilestream_GetLastWriteTime
2334 static HRESULT create_localfontfilestream(const void *file_ptr, UINT64 size, struct local_cached_stream *entry, IDWriteFontFileStream** iface)
2336 struct dwrite_localfontfilestream *This = heap_alloc(sizeof(struct dwrite_localfontfilestream));
2337 if (!This)
2338 return E_OUTOFMEMORY;
2340 This->IDWriteFontFileStream_iface.lpVtbl = &localfontfilestreamvtbl;
2341 This->ref = 1;
2343 This->file_ptr = file_ptr;
2344 This->size = size;
2345 This->entry = entry;
2347 *iface = &This->IDWriteFontFileStream_iface;
2348 return S_OK;
2351 static HRESULT WINAPI localfontfileloader_QueryInterface(IDWriteLocalFontFileLoader *iface, REFIID riid, void **obj)
2353 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2355 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2357 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileLoader) || IsEqualIID(riid, &IID_IDWriteLocalFontFileLoader))
2359 *obj = iface;
2360 IDWriteLocalFontFileLoader_AddRef(iface);
2361 return S_OK;
2364 *obj = NULL;
2365 return E_NOINTERFACE;
2368 static ULONG WINAPI localfontfileloader_AddRef(IDWriteLocalFontFileLoader *iface)
2370 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2371 ULONG ref = InterlockedIncrement(&This->ref);
2372 TRACE("(%p)->(%d)\n", This, ref);
2373 return ref;
2376 static ULONG WINAPI localfontfileloader_Release(IDWriteLocalFontFileLoader *iface)
2378 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2379 ULONG ref = InterlockedDecrement(&This->ref);
2381 TRACE("(%p)->(%d)\n", This, ref);
2383 if (!ref) {
2384 struct local_cached_stream *stream, *stream2;
2386 /* This will detach all entries from cache. Entries are released together with streams,
2387 so stream controls its lifetime. */
2388 LIST_FOR_EACH_ENTRY_SAFE(stream, stream2, &This->streams, struct local_cached_stream, entry)
2389 list_init(&stream->entry);
2391 heap_free(This);
2394 return ref;
2397 static HRESULT WINAPI localfontfileloader_CreateStreamFromKey(IDWriteLocalFontFileLoader *iface, const void *key, UINT32 key_size, IDWriteFontFileStream **ret)
2399 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2400 const struct local_refkey *refkey = key;
2401 struct local_cached_stream *stream;
2402 IDWriteFontFileStream *filestream;
2403 HANDLE file, mapping;
2404 LARGE_INTEGER size;
2405 void *file_ptr;
2406 HRESULT hr;
2408 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, ret);
2409 TRACE("name: %s\n", debugstr_w(refkey->name));
2411 /* search cache first */
2412 LIST_FOR_EACH_ENTRY(stream, &This->streams, struct local_cached_stream, entry) {
2413 if (key_size == stream->key_size && !memcmp(stream->key, key, key_size)) {
2414 *ret = stream->stream;
2415 IDWriteFontFileStream_AddRef(*ret);
2416 return S_OK;
2420 *ret = NULL;
2422 file = CreateFileW(refkey->name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2423 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2424 if (file == INVALID_HANDLE_VALUE)
2425 return E_FAIL;
2427 GetFileSizeEx(file, &size);
2428 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
2429 CloseHandle(file);
2430 if (!mapping)
2431 return E_FAIL;
2433 file_ptr = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
2434 CloseHandle(mapping);
2436 stream = heap_alloc(sizeof(*stream));
2437 if (!stream) {
2438 UnmapViewOfFile(file_ptr);
2439 return E_OUTOFMEMORY;
2442 stream->key = heap_alloc(key_size);
2443 if (!stream->key) {
2444 UnmapViewOfFile(file_ptr);
2445 heap_free(stream);
2446 return E_OUTOFMEMORY;
2449 stream->key_size = key_size;
2450 memcpy(stream->key, key, key_size);
2452 hr = create_localfontfilestream(file_ptr, size.QuadPart, stream, &filestream);
2453 if (FAILED(hr)) {
2454 UnmapViewOfFile(file_ptr);
2455 heap_free(stream->key);
2456 heap_free(stream);
2457 return hr;
2460 stream->stream = filestream;
2461 list_add_head(&This->streams, &stream->entry);
2463 *ret = stream->stream;
2465 return S_OK;
2468 static HRESULT WINAPI localfontfileloader_GetFilePathLengthFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, UINT32 *length)
2470 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2471 const struct local_refkey *refkey = key;
2473 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, length);
2475 *length = strlenW(refkey->name);
2476 return S_OK;
2479 static HRESULT WINAPI localfontfileloader_GetFilePathFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, WCHAR *path, UINT32 length)
2481 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2482 const struct local_refkey *refkey = key;
2484 TRACE("(%p)->(%p, %i, %p, %i)\n", This, key, key_size, path, length);
2486 if (length < strlenW(refkey->name))
2487 return E_INVALIDARG;
2489 strcpyW(path, refkey->name);
2490 return S_OK;
2493 static HRESULT WINAPI localfontfileloader_GetLastWriteTimeFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, FILETIME *writetime)
2495 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2496 const struct local_refkey *refkey = key;
2498 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, writetime);
2500 *writetime = refkey->writetime;
2501 return S_OK;
2504 static const struct IDWriteLocalFontFileLoaderVtbl localfontfileloadervtbl = {
2505 localfontfileloader_QueryInterface,
2506 localfontfileloader_AddRef,
2507 localfontfileloader_Release,
2508 localfontfileloader_CreateStreamFromKey,
2509 localfontfileloader_GetFilePathLengthFromKey,
2510 localfontfileloader_GetFilePathFromKey,
2511 localfontfileloader_GetLastWriteTimeFromKey
2514 HRESULT create_localfontfileloader(IDWriteLocalFontFileLoader** iface)
2516 struct dwrite_localfontfileloader *This = heap_alloc(sizeof(struct dwrite_localfontfileloader));
2517 if (!This)
2518 return E_OUTOFMEMORY;
2520 This->IDWriteLocalFontFileLoader_iface.lpVtbl = &localfontfileloadervtbl;
2521 This->ref = 1;
2522 list_init(&This->streams);
2524 *iface = &This->IDWriteLocalFontFileLoader_iface;
2525 return S_OK;
2528 HRESULT get_local_refkey(const WCHAR *path, const FILETIME *writetime, void **key, UINT32 *size)
2530 struct local_refkey *refkey;
2532 *size = FIELD_OFFSET(struct local_refkey, name) + (strlenW(path)+1)*sizeof(WCHAR);
2533 *key = NULL;
2535 refkey = heap_alloc(*size);
2536 if (!refkey)
2537 return E_OUTOFMEMORY;
2539 if (writetime)
2540 refkey->writetime = *writetime;
2541 else {
2542 WIN32_FILE_ATTRIBUTE_DATA info;
2544 if (GetFileAttributesExW(path, GetFileExInfoStandard, &info))
2545 refkey->writetime = info.ftLastWriteTime;
2546 else
2547 memset(&refkey->writetime, 0, sizeof(refkey->writetime));
2549 strcpyW(refkey->name, path);
2551 *key = refkey;
2553 return S_OK;