gdi32: Fix arguments for OSMesaMakeCurrent when using 16 bit formats.
[wine.git] / dlls / dwrite / font.c
blob12ce3ca1a243409c40903149cbfea0241acad2c0
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 for (i = 0; i < count; i++)
396 glyph_indices[i] = freetype_get_glyphindex(iface, codepoints[i]);
398 return S_OK;
401 static HRESULT WINAPI dwritefontface_TryGetFontTable(IDWriteFontFace2 *iface, UINT32 table_tag,
402 const void **table_data, UINT32 *table_size, void **context, BOOL *exists)
404 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
406 TRACE("(%p)->(%u %p %p %p %p)\n", This, table_tag, table_data, table_size, context, exists);
408 return opentype_get_font_table(This->streams[0], This->type, This->index, table_tag, table_data, context, table_size, exists);
411 static void WINAPI dwritefontface_ReleaseFontTable(IDWriteFontFace2 *iface, void *table_context)
413 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
415 TRACE("(%p)->(%p)\n", This, table_context);
417 IDWriteFontFileStream_ReleaseFileFragment(This->streams[0], table_context);
420 HRESULT new_glyph_outline(UINT32 count, struct glyph_outline **ret)
422 struct glyph_outline *outline;
423 D2D1_POINT_2F *points;
424 UINT8 *tags;
426 *ret = NULL;
428 outline = heap_alloc(sizeof(*outline));
429 if (!outline)
430 return E_OUTOFMEMORY;
432 points = heap_alloc(count*sizeof(D2D1_POINT_2F));
433 tags = heap_alloc(count*sizeof(UINT8));
434 if (!points || !tags) {
435 heap_free(points);
436 heap_free(tags);
437 heap_free(outline);
438 return E_OUTOFMEMORY;
441 outline->points = points;
442 outline->tags = tags;
443 outline->count = count;
444 outline->advance = 0.0;
446 *ret = outline;
447 return S_OK;
450 static void free_glyph_outline(struct glyph_outline *outline)
452 heap_free(outline->points);
453 heap_free(outline->tags);
454 heap_free(outline);
457 static void report_glyph_outline(const struct glyph_outline *outline, IDWriteGeometrySink *sink)
459 UINT16 p;
461 for (p = 0; p < outline->count; p++) {
462 if (outline->tags[p] & OUTLINE_POINT_START) {
463 ID2D1SimplifiedGeometrySink_BeginFigure(sink, outline->points[p], D2D1_FIGURE_BEGIN_FILLED);
464 continue;
467 if (outline->tags[p] & OUTLINE_POINT_LINE)
468 ID2D1SimplifiedGeometrySink_AddLines(sink, outline->points+p, 1);
469 else if (outline->tags[p] & OUTLINE_POINT_BEZIER) {
470 static const UINT16 segment_length = 3;
471 ID2D1SimplifiedGeometrySink_AddBeziers(sink, (D2D1_BEZIER_SEGMENT*)&outline->points[p], 1);
472 p += segment_length - 1;
475 if (outline->tags[p] & OUTLINE_POINT_END)
476 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
480 static inline void translate_glyph_outline(struct glyph_outline *outline, FLOAT xoffset, FLOAT yoffset)
482 UINT16 p;
484 for (p = 0; p < outline->count; p++) {
485 outline->points[p].x += xoffset;
486 outline->points[p].y += yoffset;
490 static HRESULT WINAPI dwritefontface_GetGlyphRunOutline(IDWriteFontFace2 *iface, FLOAT emSize,
491 UINT16 const *glyphs, FLOAT const* advances, DWRITE_GLYPH_OFFSET const *offsets,
492 UINT32 count, BOOL is_sideways, BOOL is_rtl, IDWriteGeometrySink *sink)
494 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
495 FLOAT advance = 0.0;
496 HRESULT hr;
497 UINT32 g;
499 TRACE("(%p)->(%.2f %p %p %p %u %d %d %p)\n", This, emSize, glyphs, advances, offsets,
500 count, is_sideways, is_rtl, sink);
502 if (!glyphs || !sink)
503 return E_INVALIDARG;
505 if (is_sideways)
506 FIXME("sideways mode is not supported.\n");
508 for (g = 0; g < count; g++) {
509 FLOAT xoffset = 0.0, yoffset = 0.0;
510 struct glyph_outline *outline;
512 /* FIXME: cache outlines */
514 hr = freetype_get_glyph_outline(iface, emSize, glyphs[g], This->simulations, &outline);
515 if (FAILED(hr))
516 return hr;
518 /* glyph offsets act as current glyph adjustment */
519 if (offsets) {
520 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
521 yoffset -= offsets[g].ascenderOffset;
524 if (g == 0)
525 advance = is_rtl ? -outline->advance : 0.0;
527 xoffset += advance;
528 translate_glyph_outline(outline, xoffset, yoffset);
530 /* update advance to next glyph */
531 if (advances)
532 advance += is_rtl ? -advances[g] : advances[g];
533 else
534 advance += is_rtl ? -outline->advance : outline->advance;
536 report_glyph_outline(outline, sink);
537 free_glyph_outline(outline);
540 return S_OK;
543 static HRESULT WINAPI dwritefontface_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT emSize,
544 FLOAT pixels_per_dip, DWRITE_MEASURING_MODE mode, IDWriteRenderingParams* params, DWRITE_RENDERING_MODE* rendering_mode)
546 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
547 FIXME("(%p)->(%f %f %d %p %p): stub\n", This, emSize, pixels_per_dip, mode, params, rendering_mode);
548 return E_NOTIMPL;
551 static HRESULT WINAPI dwritefontface_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
552 DWRITE_MATRIX const *transform, DWRITE_FONT_METRICS *metrics)
554 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
555 DWRITE_FONT_METRICS1 metrics1;
556 HRESULT hr;
558 TRACE("(%p)->(%.2f %.2f %p %p)\n", This, emSize, pixels_per_dip, transform, metrics);
560 hr = IDWriteFontFace2_GetGdiCompatibleMetrics(iface, emSize, pixels_per_dip, transform, &metrics1);
561 if (FAILED(hr))
562 return hr;
564 memcpy(metrics, &metrics1, sizeof(*metrics));
565 return hr;
568 static HRESULT WINAPI dwritefontface_GetGdiCompatibleGlyphMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
569 DWRITE_MATRIX const *transform, BOOL use_gdi_natural, UINT16 const *glyph_indices, UINT32 glyph_count,
570 DWRITE_GLYPH_METRICS *metrics, BOOL is_sideways)
572 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
573 FIXME("(%p)->(%f %f %p %d %p %u %p %d): stub\n", This, emSize, pixels_per_dip, transform, use_gdi_natural, glyph_indices,
574 glyph_count, metrics, is_sideways);
575 return E_NOTIMPL;
578 static void WINAPI dwritefontface1_GetMetrics(IDWriteFontFace2 *iface, DWRITE_FONT_METRICS1 *metrics)
580 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
581 TRACE("(%p)->(%p)\n", This, metrics);
582 *metrics = This->metrics;
585 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT em_size, FLOAT pixels_per_dip,
586 const DWRITE_MATRIX *transform, DWRITE_FONT_METRICS1 *metrics)
588 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
589 FIXME("(%p)->(%f %f %p %p): stub\n", This, em_size, pixels_per_dip, transform, metrics);
590 return E_NOTIMPL;
593 static void WINAPI dwritefontface1_GetCaretMetrics(IDWriteFontFace2 *iface, DWRITE_CARET_METRICS *metrics)
595 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
596 TRACE("(%p)->(%p)\n", This, metrics);
597 *metrics = This->caret;
600 static HRESULT WINAPI dwritefontface1_GetUnicodeRanges(IDWriteFontFace2 *iface, UINT32 max_count,
601 DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
603 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
605 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
607 *count = 0;
608 if (max_count && !ranges)
609 return E_INVALIDARG;
611 return opentype_cmap_get_unicode_ranges(get_fontface_cmap(This), max_count, ranges, count);
614 static BOOL WINAPI dwritefontface1_IsMonospacedFont(IDWriteFontFace2 *iface)
616 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
617 TRACE("(%p)\n", This);
618 return freetype_is_monospaced(iface);
621 static HRESULT WINAPI dwritefontface1_GetDesignGlyphAdvances(IDWriteFontFace2 *iface,
622 UINT32 glyph_count, UINT16 const *glyphs, INT32 *advances, BOOL is_sideways)
624 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
625 UINT32 i;
627 TRACE("(%p)->(%u %p %p %d)\n", This, glyph_count, glyphs, advances, is_sideways);
629 for (i = 0; i < glyph_count; i++) {
630 DWRITE_GLYPH_METRICS metrics = { 0 };
631 HRESULT hr;
633 hr = IDWriteFontFace2_GetDesignGlyphMetrics(iface, glyphs + i, 1, &metrics, is_sideways);
634 if (FAILED(hr))
635 return hr;
637 advances[i] = is_sideways ? metrics.advanceHeight : metrics.advanceWidth;
640 return S_OK;
643 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleGlyphAdvances(IDWriteFontFace2 *iface,
644 FLOAT em_size, FLOAT pixels_per_dip, const DWRITE_MATRIX *transform, BOOL use_gdi_natural,
645 BOOL is_sideways, UINT32 glyph_count, UINT16 const *indices, INT32 *advances)
647 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
648 FIXME("(%p)->(%f %f %p %d %d %u %p %p): stub\n", This, em_size, pixels_per_dip, transform,
649 use_gdi_natural, is_sideways, glyph_count, indices, advances);
650 return E_NOTIMPL;
653 static HRESULT WINAPI dwritefontface1_GetKerningPairAdjustments(IDWriteFontFace2 *iface, UINT32 glyph_count,
654 const UINT16 *indices, INT32 *adjustments)
656 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
657 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, indices, adjustments);
658 return E_NOTIMPL;
661 static BOOL WINAPI dwritefontface1_HasKerningPairs(IDWriteFontFace2 *iface)
663 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
664 FIXME("(%p): stub\n", This);
665 return FALSE;
668 static HRESULT WINAPI dwritefontface1_GetRecommendedRenderingMode(IDWriteFontFace2 *iface,
669 FLOAT font_emsize, FLOAT dpiX, FLOAT dpiY, const DWRITE_MATRIX *transform, BOOL is_sideways,
670 DWRITE_OUTLINE_THRESHOLD threshold, DWRITE_MEASURING_MODE measuring_mode, DWRITE_RENDERING_MODE *rendering_mode)
672 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
673 FIXME("(%p)->(%f %f %f %p %d %d %d %p): stub\n", This, font_emsize, dpiX, dpiY, transform, is_sideways,
674 threshold, measuring_mode, rendering_mode);
675 return E_NOTIMPL;
678 static HRESULT WINAPI dwritefontface1_GetVerticalGlyphVariants(IDWriteFontFace2 *iface, UINT32 glyph_count,
679 const UINT16 *nominal_indices, UINT16 *vertical_indices)
681 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
682 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, nominal_indices, vertical_indices);
683 return E_NOTIMPL;
686 static BOOL WINAPI dwritefontface1_HasVerticalGlyphVariants(IDWriteFontFace2 *iface)
688 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
689 FIXME("(%p): stub\n", This);
690 return FALSE;
693 static BOOL WINAPI dwritefontface2_IsColorFont(IDWriteFontFace2 *iface)
695 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
696 FIXME("(%p): stub\n", This);
697 return FALSE;
700 static UINT32 WINAPI dwritefontface2_GetColorPaletteCount(IDWriteFontFace2 *iface)
702 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
703 FIXME("(%p): stub\n", This);
704 return 0;
707 static UINT32 WINAPI dwritefontface2_GetPaletteEntryCount(IDWriteFontFace2 *iface)
709 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
710 FIXME("(%p): stub\n", This);
711 return 0;
714 static HRESULT WINAPI dwritefontface2_GetPaletteEntries(IDWriteFontFace2 *iface, UINT32 palette_index,
715 UINT32 first_entry_index, UINT32 entry_count, DWRITE_COLOR_F *entries)
717 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
718 FIXME("(%p)->(%u %u %u %p): stub\n", This, palette_index, first_entry_index, entry_count, entries);
719 return E_NOTIMPL;
722 static HRESULT WINAPI dwritefontface2_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT fontEmSize,
723 FLOAT dpiX, FLOAT dpiY, DWRITE_MATRIX const *transform, BOOL is_sideways, DWRITE_OUTLINE_THRESHOLD threshold,
724 DWRITE_MEASURING_MODE measuringmode, IDWriteRenderingParams *params, DWRITE_RENDERING_MODE *renderingmode,
725 DWRITE_GRID_FIT_MODE *gridfitmode)
727 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
728 FIXME("(%p)->(%f %f %f %p %d %d %d %p %p %p): stub\n", This, fontEmSize, dpiX, dpiY, transform, is_sideways, threshold,
729 measuringmode, params, renderingmode, gridfitmode);
730 return E_NOTIMPL;
733 static const IDWriteFontFace2Vtbl dwritefontfacevtbl = {
734 dwritefontface_QueryInterface,
735 dwritefontface_AddRef,
736 dwritefontface_Release,
737 dwritefontface_GetType,
738 dwritefontface_GetFiles,
739 dwritefontface_GetIndex,
740 dwritefontface_GetSimulations,
741 dwritefontface_IsSymbolFont,
742 dwritefontface_GetMetrics,
743 dwritefontface_GetGlyphCount,
744 dwritefontface_GetDesignGlyphMetrics,
745 dwritefontface_GetGlyphIndices,
746 dwritefontface_TryGetFontTable,
747 dwritefontface_ReleaseFontTable,
748 dwritefontface_GetGlyphRunOutline,
749 dwritefontface_GetRecommendedRenderingMode,
750 dwritefontface_GetGdiCompatibleMetrics,
751 dwritefontface_GetGdiCompatibleGlyphMetrics,
752 dwritefontface1_GetMetrics,
753 dwritefontface1_GetGdiCompatibleMetrics,
754 dwritefontface1_GetCaretMetrics,
755 dwritefontface1_GetUnicodeRanges,
756 dwritefontface1_IsMonospacedFont,
757 dwritefontface1_GetDesignGlyphAdvances,
758 dwritefontface1_GetGdiCompatibleGlyphAdvances,
759 dwritefontface1_GetKerningPairAdjustments,
760 dwritefontface1_HasKerningPairs,
761 dwritefontface1_GetRecommendedRenderingMode,
762 dwritefontface1_GetVerticalGlyphVariants,
763 dwritefontface1_HasVerticalGlyphVariants,
764 dwritefontface2_IsColorFont,
765 dwritefontface2_GetColorPaletteCount,
766 dwritefontface2_GetPaletteEntryCount,
767 dwritefontface2_GetPaletteEntries,
768 dwritefontface2_GetRecommendedRenderingMode
771 HRESULT get_family_names_from_stream(IDWriteFontFileStream *stream, UINT32 index, DWRITE_FONT_FACE_TYPE facetype,
772 IDWriteLocalizedStrings **names)
774 const void *name_table = NULL;
775 void *name_context;
776 HRESULT hr = S_OK;
778 opentype_get_font_table(stream, facetype, index, MS_NAME_TAG, &name_table, &name_context, NULL, NULL);
779 if (name_table) {
780 hr = opentype_get_font_strings_from_id(name_table, DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES, names);
781 IDWriteFontFileStream_ReleaseFileFragment(stream, name_context);
783 else
784 names = NULL;
786 return hr;
789 static HRESULT get_fontface_from_font(struct dwrite_font *font, IDWriteFontFace2 **fontface)
791 struct dwrite_font_data *data = font->data;
792 IDWriteFontFace *face;
793 HRESULT hr;
795 *fontface = NULL;
797 hr = IDWriteFactory2_CreateFontFace(data->factory, data->face_type, 1, &data->file,
798 data->face_index, font->simulations, &face);
799 if (FAILED(hr))
800 return hr;
802 hr = IDWriteFontFace_QueryInterface(face, &IID_IDWriteFontFace2, (void**)fontface);
803 IDWriteFontFace_Release(face);
805 return hr;
808 static HRESULT WINAPI dwritefont_QueryInterface(IDWriteFont2 *iface, REFIID riid, void **obj)
810 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
812 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
814 if (IsEqualIID(riid, &IID_IDWriteFont2) ||
815 IsEqualIID(riid, &IID_IDWriteFont1) ||
816 IsEqualIID(riid, &IID_IDWriteFont) ||
817 IsEqualIID(riid, &IID_IUnknown))
819 *obj = iface;
820 IDWriteFont2_AddRef(iface);
821 return S_OK;
824 *obj = NULL;
825 return E_NOINTERFACE;
828 static ULONG WINAPI dwritefont_AddRef(IDWriteFont2 *iface)
830 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
831 ULONG ref = InterlockedIncrement(&This->ref);
832 TRACE("(%p)->(%d)\n", This, ref);
833 return ref;
836 static ULONG WINAPI dwritefont_Release(IDWriteFont2 *iface)
838 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
839 ULONG ref = InterlockedDecrement(&This->ref);
841 TRACE("(%p)->(%d)\n", This, ref);
843 if (!ref) {
844 IDWriteFontFamily_Release(This->family);
845 release_font_data(This->data);
846 heap_free(This);
849 return ref;
852 static HRESULT WINAPI dwritefont_GetFontFamily(IDWriteFont2 *iface, IDWriteFontFamily **family)
854 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
855 TRACE("(%p)->(%p)\n", This, family);
857 *family = This->family;
858 IDWriteFontFamily_AddRef(*family);
859 return S_OK;
862 static DWRITE_FONT_WEIGHT WINAPI dwritefont_GetWeight(IDWriteFont2 *iface)
864 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
865 TRACE("(%p)\n", This);
866 return This->data->weight;
869 static DWRITE_FONT_STRETCH WINAPI dwritefont_GetStretch(IDWriteFont2 *iface)
871 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
872 TRACE("(%p)\n", This);
873 return This->data->stretch;
876 static DWRITE_FONT_STYLE WINAPI dwritefont_GetStyle(IDWriteFont2 *iface)
878 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
879 TRACE("(%p)\n", This);
880 return This->style;
883 static BOOL WINAPI dwritefont_IsSymbolFont(IDWriteFont2 *iface)
885 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
886 IDWriteFontFace2 *fontface;
887 HRESULT hr;
889 TRACE("(%p)\n", This);
891 hr = get_fontface_from_font(This, &fontface);
892 if (FAILED(hr))
893 return hr;
895 return IDWriteFontFace2_IsSymbolFont(fontface);
898 static HRESULT WINAPI dwritefont_GetFaceNames(IDWriteFont2 *iface, IDWriteLocalizedStrings **names)
900 static const WCHAR boldobliqueW[] = {'B','o','l','d',' ','O','b','l','i','q','u','e',0};
901 static const WCHAR obliqueW[] = {'O','b','l','i','q','u','e',0};
902 static const WCHAR boldW[] = {'B','o','l','d',0};
903 static const WCHAR enusW[] = {'e','n','-','u','s',0};
905 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
906 IDWriteLocalizedStrings *strings;
907 const WCHAR *name;
908 HRESULT hr;
910 TRACE("(%p)->(%p)\n", This, names);
912 *names = NULL;
914 if (This->simulations == DWRITE_FONT_SIMULATIONS_NONE) {
915 BOOL exists;
916 return IDWriteFont2_GetInformationalStrings(iface, DWRITE_INFORMATIONAL_STRING_WIN32_SUBFAMILY_NAMES,
917 names, &exists);
920 switch (This->simulations) {
921 case DWRITE_FONT_SIMULATIONS_BOLD|DWRITE_FONT_SIMULATIONS_OBLIQUE:
922 name = boldobliqueW;
923 break;
924 case DWRITE_FONT_SIMULATIONS_BOLD:
925 name = boldW;
926 break;
927 case DWRITE_FONT_SIMULATIONS_OBLIQUE:
928 name = obliqueW;
929 break;
930 default:
931 ERR("unknown simulations %d\n", This->simulations);
932 return E_FAIL;
935 hr = create_localizedstrings(&strings);
936 if (FAILED(hr)) return hr;
938 hr = add_localizedstring(strings, enusW, name);
939 if (FAILED(hr)) {
940 IDWriteLocalizedStrings_Release(strings);
941 return hr;
944 *names = strings;
946 return S_OK;
949 static HRESULT WINAPI dwritefont_GetInformationalStrings(IDWriteFont2 *iface,
950 DWRITE_INFORMATIONAL_STRING_ID stringid, IDWriteLocalizedStrings **strings, BOOL *exists)
952 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
953 struct dwrite_font_data *data = This->data;
954 HRESULT hr;
956 TRACE("(%p)->(%d %p %p)\n", This, stringid, strings, exists);
958 *exists = FALSE;
959 *strings = NULL;
961 if (stringid > DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_CID_NAME || stringid == DWRITE_INFORMATIONAL_STRING_NONE)
962 return S_OK;
964 if (!data->info_strings[stringid]) {
965 IDWriteFontFace2 *fontface;
966 const void *table_data;
967 BOOL table_exists;
968 void *context;
969 UINT32 size;
971 hr = get_fontface_from_font(This, &fontface);
972 if (FAILED(hr))
973 return hr;
975 table_exists = FALSE;
976 hr = IDWriteFontFace2_TryGetFontTable(fontface, MS_NAME_TAG, &table_data, &size, &context, &table_exists);
977 if (FAILED(hr) || !table_exists)
978 WARN("no NAME table found.\n");
980 if (table_exists) {
981 hr = opentype_get_font_strings_from_id(table_data, stringid, &data->info_strings[stringid]);
982 if (FAILED(hr) || !data->info_strings[stringid])
983 return hr;
984 IDWriteFontFace2_ReleaseFontTable(fontface, context);
988 hr = clone_localizedstring(data->info_strings[stringid], strings);
989 if (FAILED(hr))
990 return hr;
992 *exists = TRUE;
993 return S_OK;
996 static DWRITE_FONT_SIMULATIONS WINAPI dwritefont_GetSimulations(IDWriteFont2 *iface)
998 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
999 TRACE("(%p)\n", This);
1000 return This->simulations;
1003 static void WINAPI dwritefont_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS *metrics)
1005 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1007 TRACE("(%p)->(%p)\n", This, metrics);
1008 memcpy(metrics, &This->data->metrics, sizeof(*metrics));
1011 static HRESULT WINAPI dwritefont_HasCharacter(IDWriteFont2 *iface, UINT32 value, BOOL *exists)
1013 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1014 IDWriteFontFace2 *fontface;
1015 UINT16 index;
1016 HRESULT hr;
1018 TRACE("(%p)->(0x%08x %p)\n", This, value, exists);
1020 *exists = FALSE;
1022 hr = get_fontface_from_font(This, &fontface);
1023 if (FAILED(hr))
1024 return hr;
1026 index = 0;
1027 hr = IDWriteFontFace2_GetGlyphIndices(fontface, &value, 1, &index);
1028 if (FAILED(hr))
1029 return hr;
1031 *exists = index != 0;
1032 return S_OK;
1035 static HRESULT WINAPI dwritefont_CreateFontFace(IDWriteFont2 *iface, IDWriteFontFace **face)
1037 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1038 HRESULT hr;
1040 TRACE("(%p)->(%p)\n", This, face);
1042 hr = get_fontface_from_font(This, (IDWriteFontFace2**)face);
1043 if (hr == S_OK)
1044 IDWriteFontFace_AddRef(*face);
1046 return hr;
1049 static void WINAPI dwritefont1_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS1 *metrics)
1051 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1052 TRACE("(%p)->(%p)\n", This, metrics);
1053 *metrics = This->data->metrics;
1056 static void WINAPI dwritefont1_GetPanose(IDWriteFont2 *iface, DWRITE_PANOSE *panose)
1058 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1059 FIXME("(%p)->(%p): stub\n", This, panose);
1062 static HRESULT WINAPI dwritefont1_GetUnicodeRanges(IDWriteFont2 *iface, UINT32 max_count, DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
1064 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1065 IDWriteFontFace2 *fontface;
1066 HRESULT hr;
1068 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
1070 hr = get_fontface_from_font(This, &fontface);
1071 if (FAILED(hr))
1072 return hr;
1074 return IDWriteFontFace2_GetUnicodeRanges(fontface, max_count, ranges, count);
1077 static BOOL WINAPI dwritefont1_IsMonospacedFont(IDWriteFont2 *iface)
1079 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1080 IDWriteFontFace2 *fontface;
1081 HRESULT hr;
1083 TRACE("(%p)\n", This);
1085 hr = get_fontface_from_font(This, &fontface);
1086 if (FAILED(hr))
1087 return hr;
1089 return IDWriteFontFace2_IsMonospacedFont(fontface);
1092 static HRESULT WINAPI dwritefont2_IsColorFont(IDWriteFont2 *iface)
1094 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1095 IDWriteFontFace2 *fontface;
1096 HRESULT hr;
1098 TRACE("(%p)\n", This);
1100 hr = get_fontface_from_font(This, &fontface);
1101 if (FAILED(hr))
1102 return hr;
1104 return IDWriteFontFace2_IsColorFont(fontface);
1107 static const IDWriteFont2Vtbl dwritefontvtbl = {
1108 dwritefont_QueryInterface,
1109 dwritefont_AddRef,
1110 dwritefont_Release,
1111 dwritefont_GetFontFamily,
1112 dwritefont_GetWeight,
1113 dwritefont_GetStretch,
1114 dwritefont_GetStyle,
1115 dwritefont_IsSymbolFont,
1116 dwritefont_GetFaceNames,
1117 dwritefont_GetInformationalStrings,
1118 dwritefont_GetSimulations,
1119 dwritefont_GetMetrics,
1120 dwritefont_HasCharacter,
1121 dwritefont_CreateFontFace,
1122 dwritefont1_GetMetrics,
1123 dwritefont1_GetPanose,
1124 dwritefont1_GetUnicodeRanges,
1125 dwritefont1_IsMonospacedFont,
1126 dwritefont2_IsColorFont
1129 static HRESULT create_font(struct dwrite_font_data *data, IDWriteFontFamily *family, DWRITE_FONT_SIMULATIONS simulations,
1130 IDWriteFont **font)
1132 struct dwrite_font *This;
1133 *font = NULL;
1135 This = heap_alloc(sizeof(struct dwrite_font));
1136 if (!This) return E_OUTOFMEMORY;
1138 This->IDWriteFont2_iface.lpVtbl = &dwritefontvtbl;
1139 This->ref = 1;
1140 This->family = family;
1141 IDWriteFontFamily_AddRef(family);
1142 This->simulations = simulations;
1143 This->style = data->style;
1144 This->data = data;
1145 InterlockedIncrement(&This->data->ref);
1147 /* set oblique style from requested simulation */
1148 if ((simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) && data->style == DWRITE_FONT_STYLE_NORMAL)
1149 This->style = DWRITE_FONT_STYLE_OBLIQUE;
1151 *font = (IDWriteFont*)&This->IDWriteFont2_iface;
1153 return S_OK;
1156 static HRESULT WINAPI dwritefontfamily_QueryInterface(IDWriteFontFamily *iface, REFIID riid, void **obj)
1158 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1159 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1161 if (IsEqualIID(riid, &IID_IUnknown) ||
1162 IsEqualIID(riid, &IID_IDWriteFontList) ||
1163 IsEqualIID(riid, &IID_IDWriteFontFamily))
1165 *obj = iface;
1166 IDWriteFontFamily_AddRef(iface);
1167 return S_OK;
1170 *obj = NULL;
1171 return E_NOINTERFACE;
1174 static ULONG WINAPI dwritefontfamily_AddRef(IDWriteFontFamily *iface)
1176 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1177 ULONG ref = InterlockedIncrement(&This->ref);
1178 TRACE("(%p)->(%d)\n", This, ref);
1179 return ref;
1182 static ULONG WINAPI dwritefontfamily_Release(IDWriteFontFamily *iface)
1184 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1185 ULONG ref = InterlockedDecrement(&This->ref);
1187 TRACE("(%p)->(%d)\n", This, ref);
1189 if (!ref)
1191 IDWriteFontCollection_Release(This->collection);
1192 release_fontfamily_data(This->data);
1193 heap_free(This);
1196 return ref;
1199 static HRESULT WINAPI dwritefontfamily_GetFontCollection(IDWriteFontFamily *iface, IDWriteFontCollection **collection)
1201 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1202 TRACE("(%p)->(%p)\n", This, collection);
1204 *collection = This->collection;
1205 IDWriteFontCollection_AddRef(This->collection);
1206 return S_OK;
1209 static UINT32 WINAPI dwritefontfamily_GetFontCount(IDWriteFontFamily *iface)
1211 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1212 TRACE("(%p)\n", This);
1213 return This->data->font_count;
1216 static HRESULT WINAPI dwritefontfamily_GetFont(IDWriteFontFamily *iface, UINT32 index, IDWriteFont **font)
1218 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1220 TRACE("(%p)->(%u %p)\n", This, index, font);
1222 *font = NULL;
1224 if (This->data->font_count == 0)
1225 return S_FALSE;
1227 if (index >= This->data->font_count)
1228 return E_INVALIDARG;
1230 return create_font(This->data->fonts[index], iface, DWRITE_FONT_SIMULATIONS_NONE, font);
1233 static HRESULT WINAPI dwritefontfamily_GetFamilyNames(IDWriteFontFamily *iface, IDWriteLocalizedStrings **names)
1235 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1236 return clone_localizedstring(This->data->familyname, names);
1239 static inline BOOL is_matching_font_style(DWRITE_FONT_STYLE style, DWRITE_FONT_STYLE font_style)
1241 if (style == font_style)
1242 return TRUE;
1244 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) && font_style == DWRITE_FONT_STYLE_NORMAL)
1245 return TRUE;
1247 return FALSE;
1250 static HRESULT WINAPI dwritefontfamily_GetFirstMatchingFont(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1251 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFont **font)
1253 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1254 UINT32 min_weight_diff = ~0u;
1255 int found = -1, i;
1257 TRACE("(%p)->(%d %d %d %p)\n", This, weight, stretch, style, font);
1259 for (i = 0; i < This->data->font_count; i++) {
1260 if (is_matching_font_style(style, This->data->fonts[i]->style) && stretch == This->data->fonts[i]->stretch) {
1261 DWRITE_FONT_WEIGHT font_weight = This->data->fonts[i]->weight;
1262 UINT32 weight_diff = abs(font_weight - weight);
1263 if (weight_diff < min_weight_diff) {
1264 min_weight_diff = weight_diff;
1265 found = i;
1270 if (found != -1) {
1271 DWRITE_FONT_SIMULATIONS simulations = DWRITE_FONT_SIMULATIONS_NONE;
1273 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) &&
1274 This->data->fonts[found]->style == DWRITE_FONT_STYLE_NORMAL) {
1275 simulations = DWRITE_FONT_SIMULATIONS_OBLIQUE;
1277 return create_font(This->data->fonts[found], iface, simulations, font);
1279 else {
1280 *font = NULL;
1281 return DWRITE_E_NOFONT;
1285 static HRESULT WINAPI dwritefontfamily_GetMatchingFonts(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1286 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFontList **fonts)
1288 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1289 FIXME("(%p)->(%d %d %d %p): stub\n", This, weight, stretch, style, fonts);
1290 return E_NOTIMPL;
1293 static const IDWriteFontFamilyVtbl fontfamilyvtbl = {
1294 dwritefontfamily_QueryInterface,
1295 dwritefontfamily_AddRef,
1296 dwritefontfamily_Release,
1297 dwritefontfamily_GetFontCollection,
1298 dwritefontfamily_GetFontCount,
1299 dwritefontfamily_GetFont,
1300 dwritefontfamily_GetFamilyNames,
1301 dwritefontfamily_GetFirstMatchingFont,
1302 dwritefontfamily_GetMatchingFonts
1305 static HRESULT create_fontfamily(struct dwrite_fontfamily_data *data, IDWriteFontCollection *collection, IDWriteFontFamily **family)
1307 struct dwrite_fontfamily *This;
1309 *family = NULL;
1311 This = heap_alloc(sizeof(struct dwrite_fontfamily));
1312 if (!This) return E_OUTOFMEMORY;
1314 This->IDWriteFontFamily_iface.lpVtbl = &fontfamilyvtbl;
1315 This->ref = 1;
1316 This->collection = collection;
1317 IDWriteFontCollection_AddRef(collection);
1318 This->data = data;
1319 InterlockedIncrement(&This->data->ref);
1321 *family = &This->IDWriteFontFamily_iface;
1323 return S_OK;
1326 BOOL is_system_collection(IDWriteFontCollection *collection)
1328 void *obj;
1329 return IDWriteFontCollection_QueryInterface(collection, &IID_issystemcollection, (void**)&obj) == S_OK;
1332 static HRESULT WINAPI dwritefontcollection_QueryInterface(IDWriteFontCollection *iface, REFIID riid, void **obj)
1334 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1335 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1337 if (IsEqualIID(riid, &IID_IUnknown) ||
1338 IsEqualIID(riid, &IID_IDWriteFontCollection))
1340 *obj = iface;
1341 IDWriteFontCollection_AddRef(iface);
1342 return S_OK;
1345 *obj = NULL;
1347 if (This->is_system && IsEqualIID(riid, &IID_issystemcollection))
1348 return S_OK;
1350 return E_NOINTERFACE;
1353 static ULONG WINAPI dwritefontcollection_AddRef(IDWriteFontCollection *iface)
1355 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1356 ULONG ref = InterlockedIncrement(&This->ref);
1357 TRACE("(%p)->(%d)\n", This, ref);
1358 return ref;
1361 static ULONG WINAPI dwritefontcollection_Release(IDWriteFontCollection *iface)
1363 unsigned int i;
1364 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1365 ULONG ref = InterlockedDecrement(&This->ref);
1366 TRACE("(%p)->(%d)\n", This, ref);
1368 if (!ref) {
1369 for (i = 0; i < This->family_count; i++)
1370 release_fontfamily_data(This->family_data[i]);
1371 heap_free(This->family_data);
1372 heap_free(This);
1375 return ref;
1378 static UINT32 WINAPI dwritefontcollection_GetFontFamilyCount(IDWriteFontCollection *iface)
1380 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1381 TRACE("(%p)\n", This);
1382 return This->family_count;
1385 static HRESULT WINAPI dwritefontcollection_GetFontFamily(IDWriteFontCollection *iface, UINT32 index, IDWriteFontFamily **family)
1387 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1389 TRACE("(%p)->(%u %p)\n", This, index, family);
1391 if (index >= This->family_count) {
1392 *family = NULL;
1393 return E_FAIL;
1396 return create_fontfamily(This->family_data[index], iface, family);
1399 static UINT32 collection_find_family(struct dwrite_fontcollection *collection, const WCHAR *name)
1401 UINT32 i;
1403 for (i = 0; i < collection->family_count; i++) {
1404 IDWriteLocalizedStrings *family_name = collection->family_data[i]->familyname;
1405 HRESULT hr;
1406 int j;
1408 for (j = 0; j < IDWriteLocalizedStrings_GetCount(family_name); j++) {
1409 WCHAR buffer[255];
1410 hr = IDWriteLocalizedStrings_GetString(family_name, j, buffer, 255);
1411 if (SUCCEEDED(hr) && !strcmpW(buffer, name))
1412 return i;
1416 return ~0u;
1419 static HRESULT WINAPI dwritefontcollection_FindFamilyName(IDWriteFontCollection *iface, const WCHAR *name, UINT32 *index, BOOL *exists)
1421 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1422 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(name), index, exists);
1423 *index = collection_find_family(This, name);
1424 *exists = *index != ~0u;
1425 return S_OK;
1428 static BOOL is_same_fontfile(IDWriteFontFile *left, IDWriteFontFile *right)
1430 UINT32 left_key_size, right_key_size;
1431 const void *left_key, *right_key;
1432 HRESULT hr;
1434 if (left == right)
1435 return TRUE;
1437 hr = IDWriteFontFile_GetReferenceKey(left, &left_key, &left_key_size);
1438 if (FAILED(hr))
1439 return FALSE;
1441 hr = IDWriteFontFile_GetReferenceKey(right, &right_key, &right_key_size);
1442 if (FAILED(hr))
1443 return FALSE;
1445 if (left_key_size != right_key_size)
1446 return FALSE;
1448 return !memcmp(left_key, right_key, left_key_size);
1451 static HRESULT WINAPI dwritefontcollection_GetFontFromFontFace(IDWriteFontCollection *iface, IDWriteFontFace *face, IDWriteFont **font)
1453 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1454 struct dwrite_fontfamily_data *found_family = NULL;
1455 struct dwrite_font_data *found_font = NULL;
1456 DWRITE_FONT_SIMULATIONS simulations;
1457 IDWriteFontFamily *family;
1458 UINT32 i, j, face_index;
1459 IDWriteFontFile *file;
1460 HRESULT hr;
1462 TRACE("(%p)->(%p %p)\n", This, face, font);
1464 *font = NULL;
1466 if (!face)
1467 return E_INVALIDARG;
1469 i = 1;
1470 hr = IDWriteFontFace_GetFiles(face, &i, &file);
1471 if (FAILED(hr))
1472 return hr;
1473 face_index = IDWriteFontFace_GetIndex(face);
1475 for (i = 0; i < This->family_count; i++) {
1476 struct dwrite_fontfamily_data *family_data = This->family_data[i];
1477 for (j = 0; j < family_data->font_count; j++) {
1478 struct dwrite_font_data *font_data = family_data->fonts[j];
1480 if (face_index == font_data->face_index && is_same_fontfile(file, font_data->file)) {
1481 found_font = font_data;
1482 found_family = family_data;
1483 break;
1488 if (!found_font)
1489 return DWRITE_E_NOFONT;
1491 hr = create_fontfamily(found_family, iface, &family);
1492 if (FAILED(hr))
1493 return hr;
1495 simulations = IDWriteFontFace_GetSimulations(face);
1496 hr = create_font(found_font, family, simulations, font);
1497 IDWriteFontFamily_Release(family);
1498 return hr;
1501 static const IDWriteFontCollectionVtbl fontcollectionvtbl = {
1502 dwritefontcollection_QueryInterface,
1503 dwritefontcollection_AddRef,
1504 dwritefontcollection_Release,
1505 dwritefontcollection_GetFontFamilyCount,
1506 dwritefontcollection_GetFontFamily,
1507 dwritefontcollection_FindFamilyName,
1508 dwritefontcollection_GetFontFromFontFace
1511 static HRESULT fontfamily_add_font(struct dwrite_fontfamily_data *family_data, struct dwrite_font_data *font_data)
1513 if (family_data->font_count + 1 >= family_data->font_alloc) {
1514 struct dwrite_font_data **new_list;
1515 UINT32 new_alloc;
1517 new_alloc = family_data->font_alloc * 2;
1518 new_list = heap_realloc(family_data->fonts, sizeof(*family_data->fonts) * new_alloc);
1519 if (!new_list)
1520 return E_OUTOFMEMORY;
1521 family_data->fonts = new_list;
1522 family_data->font_alloc = new_alloc;
1525 family_data->fonts[family_data->font_count] = font_data;
1526 family_data->font_count++;
1527 return S_OK;
1530 static HRESULT fontcollection_add_family(struct dwrite_fontcollection *collection, struct dwrite_fontfamily_data *family)
1532 if (collection->family_alloc < collection->family_count + 1) {
1533 struct dwrite_fontfamily_data **new_list;
1534 UINT32 new_alloc;
1536 new_alloc = collection->family_alloc * 2;
1537 new_list = heap_realloc(collection->family_data, sizeof(*new_list) * new_alloc);
1538 if (!new_list)
1539 return E_OUTOFMEMORY;
1541 collection->family_alloc = new_alloc;
1542 collection->family_data = new_list;
1545 collection->family_data[collection->family_count] = family;
1546 collection->family_count++;
1548 return S_OK;
1551 static HRESULT init_font_collection(struct dwrite_fontcollection *collection, BOOL is_system)
1553 collection->IDWriteFontCollection_iface.lpVtbl = &fontcollectionvtbl;
1554 collection->ref = 1;
1555 collection->family_count = 0;
1556 collection->family_alloc = 2;
1557 collection->is_system = is_system;
1559 collection->family_data = heap_alloc(sizeof(*collection->family_data)*2);
1560 if (!collection->family_data)
1561 return E_OUTOFMEMORY;
1563 return S_OK;
1566 HRESULT get_filestream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
1568 IDWriteFontFileLoader *loader;
1569 const void *key;
1570 UINT32 key_size;
1571 HRESULT hr;
1573 *stream = NULL;
1575 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
1576 if (FAILED(hr))
1577 return hr;
1579 hr = IDWriteFontFile_GetLoader(file, &loader);
1580 if (FAILED(hr))
1581 return hr;
1583 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
1584 IDWriteFontFileLoader_Release(loader);
1585 if (FAILED(hr))
1586 return hr;
1588 return hr;
1591 static HRESULT init_font_data(IDWriteFactory2 *factory, IDWriteFontFile *file, UINT32 face_index, DWRITE_FONT_FACE_TYPE face_type,
1592 IDWriteFontFileStream **stream, struct dwrite_font_data **ret)
1594 void *os2_context, *head_context;
1595 const void *tt_os2 = NULL, *tt_head = NULL;
1596 struct dwrite_font_data *data;
1597 HRESULT hr;
1599 data = heap_alloc_zero(sizeof(*data));
1600 if (!data)
1601 return E_OUTOFMEMORY;
1603 hr = get_filestream_from_file(file, stream);
1604 if (FAILED(hr)) {
1605 heap_free(data);
1606 return hr;
1609 data->ref = 1;
1610 data->factory = factory;
1611 data->file = file;
1612 data->face_index = face_index;
1613 data->face_type = face_type;
1614 IDWriteFontFile_AddRef(file);
1615 IDWriteFactory2_AddRef(factory);
1617 opentype_get_font_table(*stream, face_type, face_index, MS_OS2_TAG, &tt_os2, &os2_context, NULL, NULL);
1618 opentype_get_font_table(*stream, face_type, face_index, MS_HEAD_TAG, &tt_head, &head_context, NULL, NULL);
1620 opentype_get_font_properties(*stream, face_type, face_index, &data->stretch, &data->weight, &data->style);
1621 opentype_get_font_metrics(*stream, face_type, face_index, &data->metrics, NULL);
1623 if (tt_os2)
1624 IDWriteFontFileStream_ReleaseFileFragment(*stream, os2_context);
1625 if (tt_head)
1626 IDWriteFontFileStream_ReleaseFileFragment(*stream, head_context);
1628 *ret = data;
1629 return S_OK;
1632 static HRESULT init_fontfamily_data(IDWriteLocalizedStrings *familyname, struct dwrite_fontfamily_data **ret)
1634 struct dwrite_fontfamily_data *data;
1636 data = heap_alloc(sizeof(*data));
1637 if (!data)
1638 return E_OUTOFMEMORY;
1640 data->ref = 1;
1641 data->font_count = 0;
1642 data->font_alloc = 2;
1644 data->fonts = heap_alloc(sizeof(*data->fonts)*data->font_alloc);
1645 if (!data->fonts) {
1646 heap_free(data);
1647 return E_OUTOFMEMORY;
1650 data->familyname = familyname;
1651 IDWriteLocalizedStrings_AddRef(familyname);
1653 *ret = data;
1654 return S_OK;
1657 HRESULT create_font_collection(IDWriteFactory2* factory, IDWriteFontFileEnumerator *enumerator, BOOL is_system, IDWriteFontCollection **ret)
1659 struct dwrite_fontcollection *collection;
1660 BOOL current = FALSE;
1661 HRESULT hr = S_OK;
1663 *ret = NULL;
1665 collection = heap_alloc(sizeof(struct dwrite_fontcollection));
1666 if (!collection) return E_OUTOFMEMORY;
1668 hr = init_font_collection(collection, is_system);
1669 if (FAILED(hr)) {
1670 heap_free(collection);
1671 return hr;
1674 *ret = &collection->IDWriteFontCollection_iface;
1676 TRACE("building font collection:\n");
1678 while (hr == S_OK) {
1679 DWRITE_FONT_FACE_TYPE face_type;
1680 DWRITE_FONT_FILE_TYPE file_type;
1681 IDWriteFontFile *file;
1682 UINT32 face_count;
1683 BOOL supported;
1684 int i;
1686 current = FALSE;
1687 hr = IDWriteFontFileEnumerator_MoveNext(enumerator, &current);
1688 if (FAILED(hr) || !current)
1689 break;
1691 hr = IDWriteFontFileEnumerator_GetCurrentFontFile(enumerator, &file);
1692 if (FAILED(hr))
1693 break;
1695 hr = IDWriteFontFile_Analyze(file, &supported, &file_type, &face_type, &face_count);
1696 if (FAILED(hr) || !supported || face_count == 0) {
1697 TRACE("unsupported font (0x%08x, %d, %u)\n", hr, supported, face_count);
1698 IDWriteFontFile_Release(file);
1699 continue;
1702 for (i = 0; i < face_count; i++) {
1703 IDWriteLocalizedStrings *family_name = NULL;
1704 struct dwrite_font_data *font_data;
1705 IDWriteFontFileStream *stream;
1706 WCHAR buffer[255];
1707 UINT32 index;
1709 /* alloc and init new font data structure */
1710 hr = init_font_data(factory, file, i, face_type, &stream, &font_data);
1711 if (FAILED(hr))
1712 break;
1714 /* get family name from font file */
1715 hr = get_family_names_from_stream(stream, i, face_type, &family_name);
1716 IDWriteFontFileStream_Release(stream);
1717 if (FAILED(hr)) {
1718 WARN("unable to get family name from font\n");
1719 release_font_data(font_data);
1720 continue;
1723 buffer[0] = 0;
1724 IDWriteLocalizedStrings_GetString(family_name, 0, buffer, sizeof(buffer)/sizeof(WCHAR));
1726 index = collection_find_family(collection, buffer);
1727 if (index != ~0u)
1728 hr = fontfamily_add_font(collection->family_data[index], font_data);
1729 else {
1730 struct dwrite_fontfamily_data *family_data;
1732 /* create and init new family */
1733 hr = init_fontfamily_data(family_name, &family_data);
1734 if (hr == S_OK) {
1735 /* add font to family, family - to collection */
1736 hr = fontfamily_add_font(family_data, font_data);
1737 if (hr == S_OK)
1738 hr = fontcollection_add_family(collection, family_data);
1740 if (FAILED(hr))
1741 release_fontfamily_data(family_data);
1745 IDWriteLocalizedStrings_Release(family_name);
1747 if (FAILED(hr))
1748 break;
1751 IDWriteFontFile_Release(file);
1754 return hr;
1757 struct system_fontfile_enumerator
1759 IDWriteFontFileEnumerator IDWriteFontFileEnumerator_iface;
1760 LONG ref;
1762 IDWriteFactory2 *factory;
1763 HKEY hkey;
1764 int index;
1767 static inline struct system_fontfile_enumerator *impl_from_IDWriteFontFileEnumerator(IDWriteFontFileEnumerator* iface)
1769 return CONTAINING_RECORD(iface, struct system_fontfile_enumerator, IDWriteFontFileEnumerator_iface);
1772 static HRESULT WINAPI systemfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1774 *obj = NULL;
1776 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1777 IDWriteFontFileEnumerator_AddRef(iface);
1778 *obj = iface;
1779 return S_OK;
1782 return E_NOINTERFACE;
1785 static ULONG WINAPI systemfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1787 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1788 return InterlockedIncrement(&enumerator->ref);
1791 static ULONG WINAPI systemfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1793 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1794 ULONG ref = InterlockedDecrement(&enumerator->ref);
1796 if (!ref) {
1797 IDWriteFactory2_Release(enumerator->factory);
1798 RegCloseKey(enumerator->hkey);
1799 heap_free(enumerator);
1802 return ref;
1805 static HRESULT WINAPI systemfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1807 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1808 DWORD ret, type, count;
1809 WCHAR *filename;
1810 HRESULT hr;
1812 *file = NULL;
1814 if (enumerator->index < 0)
1815 return E_FAIL;
1817 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1818 return E_FAIL;
1820 if (!(filename = heap_alloc(count)))
1821 return E_OUTOFMEMORY;
1823 ret = RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, (BYTE*)filename, &count);
1824 if (ret) {
1825 heap_free(filename);
1826 return E_FAIL;
1829 /* Fonts installed in 'Fonts' system dir don't get full path in registry font files cache */
1830 if (!strchrW(filename, '\\')) {
1831 static const WCHAR fontsW[] = {'\\','f','o','n','t','s','\\',0};
1832 WCHAR fullpathW[MAX_PATH];
1834 GetWindowsDirectoryW(fullpathW, sizeof(fullpathW)/sizeof(WCHAR));
1835 strcatW(fullpathW, fontsW);
1836 strcatW(fullpathW, filename);
1838 hr = IDWriteFactory2_CreateFontFileReference(enumerator->factory, fullpathW, NULL, file);
1840 else
1841 hr = IDWriteFactory2_CreateFontFileReference(enumerator->factory, filename, NULL, file);
1843 heap_free(filename);
1844 return hr;
1847 static HRESULT WINAPI systemfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1849 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1851 *current = FALSE;
1852 enumerator->index++;
1854 /* iterate until we find next string value */
1855 while (1) {
1856 DWORD type = 0, count;
1857 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1858 break;
1859 if (type == REG_SZ) {
1860 *current = TRUE;
1861 break;
1863 enumerator->index++;
1866 TRACE("index = %d, current = %d\n", enumerator->index, *current);
1867 return S_OK;
1870 static const struct IDWriteFontFileEnumeratorVtbl systemfontfileenumeratorvtbl =
1872 systemfontfileenumerator_QueryInterface,
1873 systemfontfileenumerator_AddRef,
1874 systemfontfileenumerator_Release,
1875 systemfontfileenumerator_MoveNext,
1876 systemfontfileenumerator_GetCurrentFontFile
1879 static HRESULT create_system_fontfile_enumerator(IDWriteFactory2 *factory, IDWriteFontFileEnumerator **ret)
1881 struct system_fontfile_enumerator *enumerator;
1882 static const WCHAR fontslistW[] = {
1883 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1884 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1885 'F','o','n','t','s',0
1888 *ret = NULL;
1890 enumerator = heap_alloc(sizeof(*enumerator));
1891 if (!enumerator)
1892 return E_OUTOFMEMORY;
1894 enumerator->IDWriteFontFileEnumerator_iface.lpVtbl = &systemfontfileenumeratorvtbl;
1895 enumerator->ref = 1;
1896 enumerator->factory = factory;
1897 enumerator->index = -1;
1898 IDWriteFactory2_AddRef(factory);
1900 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, fontslistW, 0, GENERIC_READ, &enumerator->hkey)) {
1901 ERR("failed to open fonts list key\n");
1902 IDWriteFactory2_Release(factory);
1903 heap_free(enumerator);
1904 return E_FAIL;
1907 *ret = &enumerator->IDWriteFontFileEnumerator_iface;
1909 return S_OK;
1912 HRESULT get_system_fontcollection(IDWriteFactory2 *factory, IDWriteFontCollection **collection)
1914 IDWriteFontFileEnumerator *enumerator;
1915 HRESULT hr;
1917 *collection = NULL;
1919 hr = create_system_fontfile_enumerator(factory, &enumerator);
1920 if (FAILED(hr))
1921 return hr;
1923 TRACE("building system font collection for factory %p\n", factory);
1924 hr = create_font_collection(factory, enumerator, TRUE, collection);
1925 IDWriteFontFileEnumerator_Release(enumerator);
1926 return hr;
1929 static HRESULT WINAPI eudcfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1931 *obj = NULL;
1933 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1934 IDWriteFontFileEnumerator_AddRef(iface);
1935 *obj = iface;
1936 return S_OK;
1939 return E_NOINTERFACE;
1942 static ULONG WINAPI eudcfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1944 return 2;
1947 static ULONG WINAPI eudcfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1949 return 1;
1952 static HRESULT WINAPI eudcfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1954 *file = NULL;
1955 return E_FAIL;
1958 static HRESULT WINAPI eudcfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1960 *current = FALSE;
1961 return S_OK;
1964 static const struct IDWriteFontFileEnumeratorVtbl eudcfontfileenumeratorvtbl =
1966 eudcfontfileenumerator_QueryInterface,
1967 eudcfontfileenumerator_AddRef,
1968 eudcfontfileenumerator_Release,
1969 eudcfontfileenumerator_MoveNext,
1970 eudcfontfileenumerator_GetCurrentFontFile
1973 static IDWriteFontFileEnumerator eudc_fontfile_enumerator = { &eudcfontfileenumeratorvtbl };
1975 HRESULT get_eudc_fontcollection(IDWriteFactory2 *factory, IDWriteFontCollection **collection)
1977 TRACE("building EUDC font collection for factory %p\n", factory);
1978 return create_font_collection(factory, &eudc_fontfile_enumerator, FALSE, collection);
1981 static HRESULT WINAPI dwritefontfile_QueryInterface(IDWriteFontFile *iface, REFIID riid, void **obj)
1983 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
1985 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1987 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFile))
1989 *obj = iface;
1990 IDWriteFontFile_AddRef(iface);
1991 return S_OK;
1994 *obj = NULL;
1995 return E_NOINTERFACE;
1998 static ULONG WINAPI dwritefontfile_AddRef(IDWriteFontFile *iface)
2000 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2001 ULONG ref = InterlockedIncrement(&This->ref);
2002 TRACE("(%p)->(%d)\n", This, ref);
2003 return ref;
2006 static ULONG WINAPI dwritefontfile_Release(IDWriteFontFile *iface)
2008 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2009 ULONG ref = InterlockedDecrement(&This->ref);
2011 TRACE("(%p)->(%d)\n", This, ref);
2013 if (!ref)
2015 IDWriteFontFileLoader_Release(This->loader);
2016 if (This->stream) IDWriteFontFileStream_Release(This->stream);
2017 heap_free(This->reference_key);
2018 heap_free(This);
2021 return ref;
2024 static HRESULT WINAPI dwritefontfile_GetReferenceKey(IDWriteFontFile *iface, const void **fontFileReferenceKey, UINT32 *fontFileReferenceKeySize)
2026 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2027 TRACE("(%p)->(%p, %p)\n", This, fontFileReferenceKey, fontFileReferenceKeySize);
2028 *fontFileReferenceKey = This->reference_key;
2029 *fontFileReferenceKeySize = This->key_size;
2031 return S_OK;
2034 static HRESULT WINAPI dwritefontfile_GetLoader(IDWriteFontFile *iface, IDWriteFontFileLoader **fontFileLoader)
2036 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2037 TRACE("(%p)->(%p)\n", This, fontFileLoader);
2038 *fontFileLoader = This->loader;
2039 IDWriteFontFileLoader_AddRef(This->loader);
2041 return S_OK;
2044 static HRESULT WINAPI dwritefontfile_Analyze(IDWriteFontFile *iface, BOOL *isSupportedFontType, DWRITE_FONT_FILE_TYPE *fontFileType, DWRITE_FONT_FACE_TYPE *fontFaceType, UINT32 *numberOfFaces)
2046 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2047 IDWriteFontFileStream *stream;
2048 HRESULT hr;
2050 TRACE("(%p)->(%p, %p, %p, %p)\n", This, isSupportedFontType, fontFileType, fontFaceType, numberOfFaces);
2052 *isSupportedFontType = FALSE;
2053 *fontFileType = DWRITE_FONT_FILE_TYPE_UNKNOWN;
2054 if (fontFaceType)
2055 *fontFaceType = DWRITE_FONT_FACE_TYPE_UNKNOWN;
2056 *numberOfFaces = 0;
2058 hr = IDWriteFontFileLoader_CreateStreamFromKey(This->loader, This->reference_key, This->key_size, &stream);
2059 if (FAILED(hr))
2060 return S_OK;
2062 hr = opentype_analyze_font(stream, numberOfFaces, fontFileType, fontFaceType, isSupportedFontType);
2064 /* TODO: Further Analysis */
2065 IDWriteFontFileStream_Release(stream);
2066 return S_OK;
2069 static const IDWriteFontFileVtbl dwritefontfilevtbl = {
2070 dwritefontfile_QueryInterface,
2071 dwritefontfile_AddRef,
2072 dwritefontfile_Release,
2073 dwritefontfile_GetReferenceKey,
2074 dwritefontfile_GetLoader,
2075 dwritefontfile_Analyze,
2078 HRESULT create_font_file(IDWriteFontFileLoader *loader, const void *reference_key, UINT32 key_size, IDWriteFontFile **font_file)
2080 struct dwrite_fontfile *This;
2082 This = heap_alloc(sizeof(struct dwrite_fontfile));
2083 if (!This) return E_OUTOFMEMORY;
2085 This->IDWriteFontFile_iface.lpVtbl = &dwritefontfilevtbl;
2086 This->ref = 1;
2087 IDWriteFontFileLoader_AddRef(loader);
2088 This->loader = loader;
2089 This->stream = NULL;
2090 This->reference_key = heap_alloc(key_size);
2091 memcpy(This->reference_key, reference_key, key_size);
2092 This->key_size = key_size;
2094 *font_file = &This->IDWriteFontFile_iface;
2096 return S_OK;
2099 static HRESULT get_stream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
2101 IDWriteFontFileLoader *loader;
2102 UINT32 key_size;
2103 const void *key;
2104 HRESULT hr;
2106 *stream = NULL;
2107 hr = IDWriteFontFile_GetLoader(file, &loader);
2108 if (FAILED(hr))
2109 return hr;
2111 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
2112 if (FAILED(hr)) {
2113 IDWriteFontFileLoader_Release(loader);
2114 return hr;
2117 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
2118 IDWriteFontFileLoader_Release(loader);
2120 return hr;
2123 HRESULT create_fontface(DWRITE_FONT_FACE_TYPE facetype, UINT32 files_number, IDWriteFontFile* const* font_files, UINT32 index,
2124 DWRITE_FONT_SIMULATIONS simulations, IDWriteFontFace2 **ret)
2126 struct dwrite_fontface *fontface;
2127 HRESULT hr = S_OK;
2128 int i;
2130 *ret = NULL;
2132 fontface = heap_alloc(sizeof(struct dwrite_fontface));
2133 if (!fontface)
2134 return E_OUTOFMEMORY;
2136 fontface->files = heap_alloc_zero(sizeof(*fontface->files) * files_number);
2137 fontface->streams = heap_alloc_zero(sizeof(*fontface->streams) * files_number);
2139 if (!fontface->files || !fontface->streams) {
2140 heap_free(fontface->files);
2141 heap_free(fontface->streams);
2142 heap_free(fontface);
2143 return E_OUTOFMEMORY;
2146 fontface->IDWriteFontFace2_iface.lpVtbl = &dwritefontfacevtbl;
2147 fontface->ref = 1;
2148 fontface->type = facetype;
2149 fontface->file_count = files_number;
2150 fontface->cmap.data = NULL;
2151 fontface->cmap.context = NULL;
2152 fontface->cmap.size = 0;
2153 fontface->index = index;
2154 fontface->simulations = simulations;
2155 memset(fontface->glyphs, 0, sizeof(fontface->glyphs));
2157 for (i = 0; i < fontface->file_count; i++) {
2158 hr = get_stream_from_file(font_files[i], &fontface->streams[i]);
2159 if (FAILED(hr)) {
2160 IDWriteFontFace2_Release(&fontface->IDWriteFontFace2_iface);
2161 return hr;
2164 fontface->files[i] = font_files[i];
2165 IDWriteFontFile_AddRef(font_files[i]);
2168 opentype_get_font_metrics(fontface->streams[0], facetype, index, &fontface->metrics, &fontface->caret);
2169 if (simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
2170 /* TODO: test what happens if caret is already slanted */
2171 if (fontface->caret.slopeRise == 1) {
2172 fontface->caret.slopeRise = fontface->metrics.designUnitsPerEm;
2173 fontface->caret.slopeRun = fontface->caret.slopeRise / 3;
2177 *ret = &fontface->IDWriteFontFace2_iface;
2178 return S_OK;
2181 /* IDWriteLocalFontFileLoader and its required IDWriteFontFileStream */
2182 struct local_refkey
2184 FILETIME writetime;
2185 WCHAR name[1];
2188 struct local_cached_stream
2190 struct list entry;
2191 IDWriteFontFileStream *stream;
2192 struct local_refkey *key;
2193 UINT32 key_size;
2196 struct dwrite_localfontfilestream
2198 IDWriteFontFileStream IDWriteFontFileStream_iface;
2199 LONG ref;
2201 struct local_cached_stream *entry;
2202 const void *file_ptr;
2203 UINT64 size;
2206 struct dwrite_localfontfileloader {
2207 IDWriteLocalFontFileLoader IDWriteLocalFontFileLoader_iface;
2208 LONG ref;
2210 struct list streams;
2213 static inline struct dwrite_localfontfileloader *impl_from_IDWriteLocalFontFileLoader(IDWriteLocalFontFileLoader *iface)
2215 return CONTAINING_RECORD(iface, struct dwrite_localfontfileloader, IDWriteLocalFontFileLoader_iface);
2218 static inline struct dwrite_localfontfilestream *impl_from_IDWriteFontFileStream(IDWriteFontFileStream *iface)
2220 return CONTAINING_RECORD(iface, struct dwrite_localfontfilestream, IDWriteFontFileStream_iface);
2223 static HRESULT WINAPI localfontfilestream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **obj)
2225 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2226 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2227 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileStream))
2229 *obj = iface;
2230 IDWriteFontFileStream_AddRef(iface);
2231 return S_OK;
2234 *obj = NULL;
2235 return E_NOINTERFACE;
2238 static ULONG WINAPI localfontfilestream_AddRef(IDWriteFontFileStream *iface)
2240 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2241 ULONG ref = InterlockedIncrement(&This->ref);
2242 TRACE("(%p)->(%d)\n", This, ref);
2243 return ref;
2246 static inline void release_cached_stream(struct local_cached_stream *stream)
2248 list_remove(&stream->entry);
2249 heap_free(stream->key);
2250 heap_free(stream);
2253 static ULONG WINAPI localfontfilestream_Release(IDWriteFontFileStream *iface)
2255 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2256 ULONG ref = InterlockedDecrement(&This->ref);
2258 TRACE("(%p)->(%d)\n", This, ref);
2260 if (!ref) {
2261 UnmapViewOfFile(This->file_ptr);
2262 release_cached_stream(This->entry);
2263 heap_free(This);
2266 return ref;
2269 static HRESULT WINAPI localfontfilestream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start, UINT64 offset, UINT64 fragment_size, void **fragment_context)
2271 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2273 TRACE("(%p)->(%p, %s, %s, %p)\n",This, fragment_start,
2274 wine_dbgstr_longlong(offset), wine_dbgstr_longlong(fragment_size), fragment_context);
2276 *fragment_context = NULL;
2278 if ((offset >= This->size - 1) || (fragment_size > This->size - offset)) {
2279 *fragment_start = NULL;
2280 return E_FAIL;
2283 *fragment_start = (char*)This->file_ptr + offset;
2284 return S_OK;
2287 static void WINAPI localfontfilestream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
2289 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2290 TRACE("(%p)->(%p)\n", This, fragment_context);
2293 static HRESULT WINAPI localfontfilestream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
2295 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2296 TRACE("(%p)->(%p)\n", This, size);
2297 *size = This->size;
2298 return S_OK;
2301 static HRESULT WINAPI localfontfilestream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
2303 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2304 ULARGE_INTEGER li;
2306 TRACE("(%p)->(%p)\n", This, last_writetime);
2308 li.u.LowPart = This->entry->key->writetime.dwLowDateTime;
2309 li.u.HighPart = This->entry->key->writetime.dwHighDateTime;
2310 *last_writetime = li.QuadPart;
2312 return S_OK;
2315 static const IDWriteFontFileStreamVtbl localfontfilestreamvtbl =
2317 localfontfilestream_QueryInterface,
2318 localfontfilestream_AddRef,
2319 localfontfilestream_Release,
2320 localfontfilestream_ReadFileFragment,
2321 localfontfilestream_ReleaseFileFragment,
2322 localfontfilestream_GetFileSize,
2323 localfontfilestream_GetLastWriteTime
2326 static HRESULT create_localfontfilestream(const void *file_ptr, UINT64 size, struct local_cached_stream *entry, IDWriteFontFileStream** iface)
2328 struct dwrite_localfontfilestream *This = heap_alloc(sizeof(struct dwrite_localfontfilestream));
2329 if (!This)
2330 return E_OUTOFMEMORY;
2332 This->IDWriteFontFileStream_iface.lpVtbl = &localfontfilestreamvtbl;
2333 This->ref = 1;
2335 This->file_ptr = file_ptr;
2336 This->size = size;
2337 This->entry = entry;
2339 *iface = &This->IDWriteFontFileStream_iface;
2340 return S_OK;
2343 static HRESULT WINAPI localfontfileloader_QueryInterface(IDWriteLocalFontFileLoader *iface, REFIID riid, void **obj)
2345 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2347 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2349 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileLoader) || IsEqualIID(riid, &IID_IDWriteLocalFontFileLoader))
2351 *obj = iface;
2352 IDWriteLocalFontFileLoader_AddRef(iface);
2353 return S_OK;
2356 *obj = NULL;
2357 return E_NOINTERFACE;
2360 static ULONG WINAPI localfontfileloader_AddRef(IDWriteLocalFontFileLoader *iface)
2362 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2363 ULONG ref = InterlockedIncrement(&This->ref);
2364 TRACE("(%p)->(%d)\n", This, ref);
2365 return ref;
2368 static ULONG WINAPI localfontfileloader_Release(IDWriteLocalFontFileLoader *iface)
2370 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2371 ULONG ref = InterlockedDecrement(&This->ref);
2373 TRACE("(%p)->(%d)\n", This, ref);
2375 if (!ref) {
2376 struct local_cached_stream *stream, *stream2;
2378 /* This will detach all entries from cache. Entries are released together with streams,
2379 so stream controls its lifetime. */
2380 LIST_FOR_EACH_ENTRY_SAFE(stream, stream2, &This->streams, struct local_cached_stream, entry)
2381 list_init(&stream->entry);
2383 heap_free(This);
2386 return ref;
2389 static HRESULT WINAPI localfontfileloader_CreateStreamFromKey(IDWriteLocalFontFileLoader *iface, const void *key, UINT32 key_size, IDWriteFontFileStream **ret)
2391 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2392 const struct local_refkey *refkey = key;
2393 struct local_cached_stream *stream;
2394 IDWriteFontFileStream *filestream;
2395 HANDLE file, mapping;
2396 LARGE_INTEGER size;
2397 void *file_ptr;
2398 HRESULT hr;
2400 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, ret);
2401 TRACE("name: %s\n", debugstr_w(refkey->name));
2403 /* search cache first */
2404 LIST_FOR_EACH_ENTRY(stream, &This->streams, struct local_cached_stream, entry) {
2405 if (key_size == stream->key_size && !memcmp(stream->key, key, key_size)) {
2406 *ret = stream->stream;
2407 IDWriteFontFileStream_AddRef(*ret);
2408 return S_OK;
2412 *ret = NULL;
2414 file = CreateFileW(refkey->name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2415 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2416 if (file == INVALID_HANDLE_VALUE)
2417 return E_FAIL;
2419 GetFileSizeEx(file, &size);
2420 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
2421 CloseHandle(file);
2422 if (!mapping)
2423 return E_FAIL;
2425 file_ptr = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
2426 CloseHandle(mapping);
2428 stream = heap_alloc(sizeof(*stream));
2429 if (!stream) {
2430 UnmapViewOfFile(file_ptr);
2431 return E_OUTOFMEMORY;
2434 stream->key = heap_alloc(key_size);
2435 if (!stream->key) {
2436 UnmapViewOfFile(file_ptr);
2437 heap_free(stream);
2438 return E_OUTOFMEMORY;
2441 stream->key_size = key_size;
2442 memcpy(stream->key, key, key_size);
2444 hr = create_localfontfilestream(file_ptr, size.QuadPart, stream, &filestream);
2445 if (FAILED(hr)) {
2446 UnmapViewOfFile(file_ptr);
2447 heap_free(stream->key);
2448 heap_free(stream);
2449 return hr;
2452 stream->stream = filestream;
2453 list_add_head(&This->streams, &stream->entry);
2455 *ret = stream->stream;
2457 return S_OK;
2460 static HRESULT WINAPI localfontfileloader_GetFilePathLengthFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, UINT32 *length)
2462 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2463 const struct local_refkey *refkey = key;
2465 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, length);
2467 *length = strlenW(refkey->name);
2468 return S_OK;
2471 static HRESULT WINAPI localfontfileloader_GetFilePathFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, WCHAR *path, UINT32 length)
2473 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2474 const struct local_refkey *refkey = key;
2476 TRACE("(%p)->(%p, %i, %p, %i)\n", This, key, key_size, path, length);
2478 if (length < strlenW(refkey->name))
2479 return E_INVALIDARG;
2481 strcpyW(path, refkey->name);
2482 return S_OK;
2485 static HRESULT WINAPI localfontfileloader_GetLastWriteTimeFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, FILETIME *writetime)
2487 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2488 const struct local_refkey *refkey = key;
2490 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, writetime);
2492 *writetime = refkey->writetime;
2493 return S_OK;
2496 static const struct IDWriteLocalFontFileLoaderVtbl localfontfileloadervtbl = {
2497 localfontfileloader_QueryInterface,
2498 localfontfileloader_AddRef,
2499 localfontfileloader_Release,
2500 localfontfileloader_CreateStreamFromKey,
2501 localfontfileloader_GetFilePathLengthFromKey,
2502 localfontfileloader_GetFilePathFromKey,
2503 localfontfileloader_GetLastWriteTimeFromKey
2506 HRESULT create_localfontfileloader(IDWriteLocalFontFileLoader** iface)
2508 struct dwrite_localfontfileloader *This = heap_alloc(sizeof(struct dwrite_localfontfileloader));
2509 if (!This)
2510 return E_OUTOFMEMORY;
2512 This->IDWriteLocalFontFileLoader_iface.lpVtbl = &localfontfileloadervtbl;
2513 This->ref = 1;
2514 list_init(&This->streams);
2516 *iface = &This->IDWriteLocalFontFileLoader_iface;
2517 return S_OK;
2520 HRESULT get_local_refkey(const WCHAR *path, const FILETIME *writetime, void **key, UINT32 *size)
2522 struct local_refkey *refkey;
2524 *size = FIELD_OFFSET(struct local_refkey, name) + (strlenW(path)+1)*sizeof(WCHAR);
2525 *key = NULL;
2527 refkey = heap_alloc(*size);
2528 if (!refkey)
2529 return E_OUTOFMEMORY;
2531 if (writetime)
2532 refkey->writetime = *writetime;
2533 else {
2534 WIN32_FILE_ATTRIBUTE_DATA info;
2536 if (GetFileAttributesExW(path, GetFileExInfoStandard, &info))
2537 refkey->writetime = info.ftLastWriteTime;
2538 else
2539 memset(&refkey->writetime, 0, sizeof(refkey->writetime));
2541 strcpyW(refkey->name, path);
2543 *key = refkey;
2545 return S_OK;