riched20: Initial support for changing font properties.
[wine/multimedia.git] / dlls / dwrite / font.c
blob85b03f69ca4c9b6ffeaf8dfef7ded463c593d060
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 struct dwrite_glyphrunanalysis {
101 IDWriteGlyphRunAnalysis IDWriteGlyphRunAnalysis_iface;
102 LONG ref;
105 #define GLYPH_BLOCK_SHIFT 8
106 #define GLYPH_BLOCK_SIZE (1UL << GLYPH_BLOCK_SHIFT)
107 #define GLYPH_BLOCK_MASK (GLYPH_BLOCK_SIZE - 1)
108 #define GLYPH_MAX 65536
110 struct dwrite_fontface {
111 IDWriteFontFace2 IDWriteFontFace2_iface;
112 LONG ref;
114 IDWriteFontFileStream **streams;
115 IDWriteFontFile **files;
116 UINT32 file_count;
117 UINT32 index;
119 USHORT simulations;
120 DWRITE_FONT_FACE_TYPE type;
121 DWRITE_FONT_METRICS1 metrics;
122 DWRITE_CARET_METRICS caret;
124 struct dwrite_fonttable cmap;
125 DWRITE_GLYPH_METRICS *glyphs[GLYPH_MAX/GLYPH_BLOCK_SIZE];
128 struct dwrite_fontfile {
129 IDWriteFontFile IDWriteFontFile_iface;
130 LONG ref;
132 IDWriteFontFileLoader *loader;
133 void *reference_key;
134 UINT32 key_size;
135 IDWriteFontFileStream *stream;
138 static inline struct dwrite_fontface *impl_from_IDWriteFontFace2(IDWriteFontFace2 *iface)
140 return CONTAINING_RECORD(iface, struct dwrite_fontface, IDWriteFontFace2_iface);
143 static inline struct dwrite_font *impl_from_IDWriteFont2(IDWriteFont2 *iface)
145 return CONTAINING_RECORD(iface, struct dwrite_font, IDWriteFont2_iface);
148 static inline struct dwrite_fontfile *impl_from_IDWriteFontFile(IDWriteFontFile *iface)
150 return CONTAINING_RECORD(iface, struct dwrite_fontfile, IDWriteFontFile_iface);
153 static inline struct dwrite_fontfamily *impl_from_IDWriteFontFamily(IDWriteFontFamily *iface)
155 return CONTAINING_RECORD(iface, struct dwrite_fontfamily, IDWriteFontFamily_iface);
158 static inline struct dwrite_fontcollection *impl_from_IDWriteFontCollection(IDWriteFontCollection *iface)
160 return CONTAINING_RECORD(iface, struct dwrite_fontcollection, IDWriteFontCollection_iface);
163 static inline struct dwrite_glyphrunanalysis *impl_from_IDWriteGlyphRunAnalysis(IDWriteGlyphRunAnalysis *iface)
165 return CONTAINING_RECORD(iface, struct dwrite_glyphrunanalysis, IDWriteGlyphRunAnalysis_iface);
168 static HRESULT get_cached_glyph_metrics(struct dwrite_fontface *fontface, UINT16 glyph, DWRITE_GLYPH_METRICS *metrics)
170 static const DWRITE_GLYPH_METRICS nil;
171 DWRITE_GLYPH_METRICS *block = fontface->glyphs[glyph >> GLYPH_BLOCK_SHIFT];
173 if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(DWRITE_GLYPH_METRICS))) return S_FALSE;
174 memcpy(metrics, &block[glyph & GLYPH_BLOCK_MASK], sizeof(*metrics));
175 return S_OK;
178 static HRESULT set_cached_glyph_metrics(struct dwrite_fontface *fontface, UINT16 glyph, DWRITE_GLYPH_METRICS *metrics)
180 DWRITE_GLYPH_METRICS **block = &fontface->glyphs[glyph >> GLYPH_BLOCK_SHIFT];
182 if (!*block) {
183 /* start new block */
184 *block = heap_alloc_zero(sizeof(*metrics) * GLYPH_BLOCK_SIZE);
185 if (!*block)
186 return E_OUTOFMEMORY;
189 memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], metrics, sizeof(*metrics));
190 return S_OK;
193 static inline void* get_fontface_cmap(struct dwrite_fontface *fontface)
195 BOOL exists = FALSE;
196 HRESULT hr;
198 if (fontface->cmap.data)
199 return fontface->cmap.data;
201 hr = IDWriteFontFace2_TryGetFontTable(&fontface->IDWriteFontFace2_iface, MS_CMAP_TAG, (const void**)&fontface->cmap.data,
202 &fontface->cmap.size, &fontface->cmap.context, &exists);
203 if (FAILED(hr) || !exists) {
204 ERR("Font does not have a CMAP table\n");
205 return NULL;
208 return fontface->cmap.data;
211 static void release_font_data(struct dwrite_font_data *data)
213 int i;
215 if (InterlockedDecrement(&data->ref) > 0)
216 return;
218 for (i = DWRITE_INFORMATIONAL_STRING_NONE; i < sizeof(data->info_strings)/sizeof(data->info_strings[0]); i++) {
219 if (data->info_strings[i])
220 IDWriteLocalizedStrings_Release(data->info_strings[i]);
223 IDWriteFontFile_Release(data->file);
224 IDWriteFactory2_Release(data->factory);
225 heap_free(data->facename);
226 heap_free(data);
229 static void release_fontfamily_data(struct dwrite_fontfamily_data *data)
231 int i;
233 if (InterlockedDecrement(&data->ref) > 0)
234 return;
236 for (i = 0; i < data->font_count; i++)
237 release_font_data(data->fonts[i]);
238 heap_free(data->fonts);
239 IDWriteLocalizedStrings_Release(data->familyname);
240 heap_free(data);
243 static HRESULT WINAPI dwritefontface_QueryInterface(IDWriteFontFace2 *iface, REFIID riid, void **obj)
245 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
247 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
249 if (IsEqualIID(riid, &IID_IDWriteFontFace2) ||
250 IsEqualIID(riid, &IID_IDWriteFontFace1) ||
251 IsEqualIID(riid, &IID_IDWriteFontFace) ||
252 IsEqualIID(riid, &IID_IUnknown))
254 *obj = iface;
255 IDWriteFontFace2_AddRef(iface);
256 return S_OK;
259 *obj = NULL;
260 return E_NOINTERFACE;
263 static ULONG WINAPI dwritefontface_AddRef(IDWriteFontFace2 *iface)
265 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
266 ULONG ref = InterlockedIncrement(&This->ref);
267 TRACE("(%p)->(%d)\n", This, ref);
268 return ref;
271 static ULONG WINAPI dwritefontface_Release(IDWriteFontFace2 *iface)
273 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
274 ULONG ref = InterlockedDecrement(&This->ref);
276 TRACE("(%p)->(%d)\n", This, ref);
278 if (!ref) {
279 UINT32 i;
281 if (This->cmap.context)
282 IDWriteFontFace2_ReleaseFontTable(iface, This->cmap.context);
283 for (i = 0; i < This->file_count; i++) {
284 if (This->streams[i])
285 IDWriteFontFileStream_Release(This->streams[i]);
286 if (This->files[i])
287 IDWriteFontFile_Release(This->files[i]);
290 for (i = 0; i < sizeof(This->glyphs)/sizeof(This->glyphs[0]); i++)
291 heap_free(This->glyphs[i]);
293 freetype_notify_cacheremove(iface);
294 heap_free(This);
297 return ref;
300 static DWRITE_FONT_FACE_TYPE WINAPI dwritefontface_GetType(IDWriteFontFace2 *iface)
302 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
303 TRACE("(%p)\n", This);
304 return This->type;
307 static HRESULT WINAPI dwritefontface_GetFiles(IDWriteFontFace2 *iface, UINT32 *number_of_files,
308 IDWriteFontFile **fontfiles)
310 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
311 int i;
313 TRACE("(%p)->(%p %p)\n", This, number_of_files, fontfiles);
314 if (fontfiles == NULL)
316 *number_of_files = This->file_count;
317 return S_OK;
319 if (*number_of_files < This->file_count)
320 return E_INVALIDARG;
322 for (i = 0; i < This->file_count; i++)
324 IDWriteFontFile_AddRef(This->files[i]);
325 fontfiles[i] = This->files[i];
328 return S_OK;
331 static UINT32 WINAPI dwritefontface_GetIndex(IDWriteFontFace2 *iface)
333 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
334 TRACE("(%p)\n", This);
335 return This->index;
338 static DWRITE_FONT_SIMULATIONS WINAPI dwritefontface_GetSimulations(IDWriteFontFace2 *iface)
340 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
341 TRACE("(%p)\n", This);
342 return This->simulations;
345 static BOOL WINAPI dwritefontface_IsSymbolFont(IDWriteFontFace2 *iface)
347 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
348 FIXME("(%p): stub\n", This);
349 return FALSE;
352 static void WINAPI dwritefontface_GetMetrics(IDWriteFontFace2 *iface, DWRITE_FONT_METRICS *metrics)
354 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
355 TRACE("(%p)->(%p)\n", This, metrics);
356 memcpy(metrics, &This->metrics, sizeof(*metrics));
359 static UINT16 WINAPI dwritefontface_GetGlyphCount(IDWriteFontFace2 *iface)
361 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
362 TRACE("(%p)\n", This);
363 return freetype_get_glyphcount(iface);
366 static HRESULT WINAPI dwritefontface_GetDesignGlyphMetrics(IDWriteFontFace2 *iface,
367 UINT16 const *glyphs, UINT32 glyph_count, DWRITE_GLYPH_METRICS *ret, BOOL is_sideways)
369 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
370 HRESULT hr;
371 UINT32 i;
373 TRACE("(%p)->(%p %u %p %d)\n", This, glyphs, glyph_count, ret, is_sideways);
375 if (!glyphs)
376 return E_INVALIDARG;
378 if (is_sideways)
379 FIXME("sideways metrics are not supported.\n");
381 for (i = 0; i < glyph_count; i++) {
382 DWRITE_GLYPH_METRICS metrics;
384 hr = get_cached_glyph_metrics(This, glyphs[i], &metrics);
385 if (hr != S_OK) {
386 freetype_get_design_glyph_metrics(iface, This->metrics.designUnitsPerEm, glyphs[i], &metrics);
387 hr = set_cached_glyph_metrics(This, glyphs[i], &metrics);
388 if (FAILED(hr))
389 return hr;
391 ret[i] = metrics;
394 return S_OK;
397 static HRESULT WINAPI dwritefontface_GetGlyphIndices(IDWriteFontFace2 *iface, UINT32 const *codepoints,
398 UINT32 count, UINT16 *glyph_indices)
400 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
401 UINT32 i;
403 TRACE("(%p)->(%p %u %p)\n", This, codepoints, count, glyph_indices);
405 if (!glyph_indices)
406 return E_INVALIDARG;
408 if (!codepoints) {
409 memset(glyph_indices, 0, count*sizeof(UINT16));
410 return E_INVALIDARG;
413 for (i = 0; i < count; i++)
414 glyph_indices[i] = freetype_get_glyphindex(iface, codepoints[i]);
416 return S_OK;
419 static HRESULT WINAPI dwritefontface_TryGetFontTable(IDWriteFontFace2 *iface, UINT32 table_tag,
420 const void **table_data, UINT32 *table_size, void **context, BOOL *exists)
422 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
424 TRACE("(%p)->(%u %p %p %p %p)\n", This, table_tag, table_data, table_size, context, exists);
426 return opentype_get_font_table(This->streams[0], This->type, This->index, table_tag, table_data, context, table_size, exists);
429 static void WINAPI dwritefontface_ReleaseFontTable(IDWriteFontFace2 *iface, void *table_context)
431 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
433 TRACE("(%p)->(%p)\n", This, table_context);
435 IDWriteFontFileStream_ReleaseFileFragment(This->streams[0], table_context);
438 HRESULT new_glyph_outline(UINT32 count, struct glyph_outline **ret)
440 struct glyph_outline *outline;
441 D2D1_POINT_2F *points;
442 UINT8 *tags;
444 *ret = NULL;
446 outline = heap_alloc(sizeof(*outline));
447 if (!outline)
448 return E_OUTOFMEMORY;
450 points = heap_alloc(count*sizeof(D2D1_POINT_2F));
451 tags = heap_alloc(count*sizeof(UINT8));
452 if (!points || !tags) {
453 heap_free(points);
454 heap_free(tags);
455 heap_free(outline);
456 return E_OUTOFMEMORY;
459 outline->points = points;
460 outline->tags = tags;
461 outline->count = count;
462 outline->advance = 0.0;
464 *ret = outline;
465 return S_OK;
468 static void free_glyph_outline(struct glyph_outline *outline)
470 heap_free(outline->points);
471 heap_free(outline->tags);
472 heap_free(outline);
475 static void report_glyph_outline(const struct glyph_outline *outline, IDWriteGeometrySink *sink)
477 UINT16 p;
479 for (p = 0; p < outline->count; p++) {
480 if (outline->tags[p] & OUTLINE_POINT_START) {
481 ID2D1SimplifiedGeometrySink_BeginFigure(sink, outline->points[p], D2D1_FIGURE_BEGIN_FILLED);
482 continue;
485 if (outline->tags[p] & OUTLINE_POINT_LINE)
486 ID2D1SimplifiedGeometrySink_AddLines(sink, outline->points+p, 1);
487 else if (outline->tags[p] & OUTLINE_POINT_BEZIER) {
488 static const UINT16 segment_length = 3;
489 ID2D1SimplifiedGeometrySink_AddBeziers(sink, (D2D1_BEZIER_SEGMENT*)&outline->points[p], 1);
490 p += segment_length - 1;
493 if (outline->tags[p] & OUTLINE_POINT_END)
494 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
498 static inline void translate_glyph_outline(struct glyph_outline *outline, FLOAT xoffset, FLOAT yoffset)
500 UINT16 p;
502 for (p = 0; p < outline->count; p++) {
503 outline->points[p].x += xoffset;
504 outline->points[p].y += yoffset;
508 static HRESULT WINAPI dwritefontface_GetGlyphRunOutline(IDWriteFontFace2 *iface, FLOAT emSize,
509 UINT16 const *glyphs, FLOAT const* advances, DWRITE_GLYPH_OFFSET const *offsets,
510 UINT32 count, BOOL is_sideways, BOOL is_rtl, IDWriteGeometrySink *sink)
512 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
513 FLOAT advance = 0.0;
514 HRESULT hr;
515 UINT32 g;
517 TRACE("(%p)->(%.2f %p %p %p %u %d %d %p)\n", This, emSize, glyphs, advances, offsets,
518 count, is_sideways, is_rtl, sink);
520 if (!glyphs || !sink)
521 return E_INVALIDARG;
523 if (is_sideways)
524 FIXME("sideways mode is not supported.\n");
526 for (g = 0; g < count; g++) {
527 FLOAT xoffset = 0.0, yoffset = 0.0;
528 struct glyph_outline *outline;
530 /* FIXME: cache outlines */
532 hr = freetype_get_glyph_outline(iface, emSize, glyphs[g], This->simulations, &outline);
533 if (FAILED(hr))
534 return hr;
536 /* glyph offsets act as current glyph adjustment */
537 if (offsets) {
538 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
539 yoffset -= offsets[g].ascenderOffset;
542 if (g == 0)
543 advance = is_rtl ? -outline->advance : 0.0;
545 xoffset += advance;
546 translate_glyph_outline(outline, xoffset, yoffset);
548 /* update advance to next glyph */
549 if (advances)
550 advance += is_rtl ? -advances[g] : advances[g];
551 else
552 advance += is_rtl ? -outline->advance : outline->advance;
554 report_glyph_outline(outline, sink);
555 free_glyph_outline(outline);
558 return S_OK;
561 static HRESULT WINAPI dwritefontface_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT emSize,
562 FLOAT pixels_per_dip, DWRITE_MEASURING_MODE mode, IDWriteRenderingParams* params, DWRITE_RENDERING_MODE* rendering_mode)
564 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
565 FIXME("(%p)->(%f %f %d %p %p): stub\n", This, emSize, pixels_per_dip, mode, params, rendering_mode);
566 return E_NOTIMPL;
569 static HRESULT WINAPI dwritefontface_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
570 DWRITE_MATRIX const *transform, DWRITE_FONT_METRICS *metrics)
572 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
573 DWRITE_FONT_METRICS1 metrics1;
574 HRESULT hr;
576 TRACE("(%p)->(%.2f %.2f %p %p)\n", This, emSize, pixels_per_dip, transform, metrics);
578 hr = IDWriteFontFace2_GetGdiCompatibleMetrics(iface, emSize, pixels_per_dip, transform, &metrics1);
579 if (FAILED(hr))
580 return hr;
582 memcpy(metrics, &metrics1, sizeof(*metrics));
583 return hr;
586 static HRESULT WINAPI dwritefontface_GetGdiCompatibleGlyphMetrics(IDWriteFontFace2 *iface, FLOAT emSize, FLOAT pixels_per_dip,
587 DWRITE_MATRIX const *transform, BOOL use_gdi_natural, UINT16 const *glyph_indices, UINT32 glyph_count,
588 DWRITE_GLYPH_METRICS *metrics, BOOL is_sideways)
590 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
591 FIXME("(%p)->(%f %f %p %d %p %u %p %d): stub\n", This, emSize, pixels_per_dip, transform, use_gdi_natural, glyph_indices,
592 glyph_count, metrics, is_sideways);
593 return E_NOTIMPL;
596 static void WINAPI dwritefontface1_GetMetrics(IDWriteFontFace2 *iface, DWRITE_FONT_METRICS1 *metrics)
598 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
599 TRACE("(%p)->(%p)\n", This, metrics);
600 *metrics = This->metrics;
603 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleMetrics(IDWriteFontFace2 *iface, FLOAT em_size, FLOAT pixels_per_dip,
604 const DWRITE_MATRIX *transform, DWRITE_FONT_METRICS1 *metrics)
606 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
607 FIXME("(%p)->(%f %f %p %p): stub\n", This, em_size, pixels_per_dip, transform, metrics);
608 return E_NOTIMPL;
611 static void WINAPI dwritefontface1_GetCaretMetrics(IDWriteFontFace2 *iface, DWRITE_CARET_METRICS *metrics)
613 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
614 TRACE("(%p)->(%p)\n", This, metrics);
615 *metrics = This->caret;
618 static HRESULT WINAPI dwritefontface1_GetUnicodeRanges(IDWriteFontFace2 *iface, UINT32 max_count,
619 DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
621 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
623 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
625 *count = 0;
626 if (max_count && !ranges)
627 return E_INVALIDARG;
629 return opentype_cmap_get_unicode_ranges(get_fontface_cmap(This), max_count, ranges, count);
632 static BOOL WINAPI dwritefontface1_IsMonospacedFont(IDWriteFontFace2 *iface)
634 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
635 TRACE("(%p)\n", This);
636 return freetype_is_monospaced(iface);
639 static HRESULT WINAPI dwritefontface1_GetDesignGlyphAdvances(IDWriteFontFace2 *iface,
640 UINT32 glyph_count, UINT16 const *glyphs, INT32 *advances, BOOL is_sideways)
642 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
643 UINT32 i;
645 TRACE("(%p)->(%u %p %p %d)\n", This, glyph_count, glyphs, advances, is_sideways);
647 for (i = 0; i < glyph_count; i++) {
648 DWRITE_GLYPH_METRICS metrics = { 0 };
649 HRESULT hr;
651 hr = IDWriteFontFace2_GetDesignGlyphMetrics(iface, glyphs + i, 1, &metrics, is_sideways);
652 if (FAILED(hr))
653 return hr;
655 advances[i] = is_sideways ? metrics.advanceHeight : metrics.advanceWidth;
658 return S_OK;
661 static HRESULT WINAPI dwritefontface1_GetGdiCompatibleGlyphAdvances(IDWriteFontFace2 *iface,
662 FLOAT em_size, FLOAT pixels_per_dip, const DWRITE_MATRIX *transform, BOOL use_gdi_natural,
663 BOOL is_sideways, UINT32 glyph_count, UINT16 const *indices, INT32 *advances)
665 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
666 FIXME("(%p)->(%f %f %p %d %d %u %p %p): stub\n", This, em_size, pixels_per_dip, transform,
667 use_gdi_natural, is_sideways, glyph_count, indices, advances);
668 return E_NOTIMPL;
671 static HRESULT WINAPI dwritefontface1_GetKerningPairAdjustments(IDWriteFontFace2 *iface, UINT32 count,
672 const UINT16 *indices, INT32 *adjustments)
674 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
675 UINT32 i;
677 TRACE("(%p)->(%u %p %p)\n", This, count, indices, adjustments);
679 if (!(indices || adjustments) || !count)
680 return E_INVALIDARG;
682 if (!indices || count == 1) {
683 memset(adjustments, 0, count*sizeof(INT32));
684 return E_INVALIDARG;
687 for (i = 0; i < count-1; i++)
688 adjustments[i] = freetype_get_kerning_pair_adjustment(iface, indices[i], indices[i+1]);
689 adjustments[count-1] = 0;
691 return S_OK;
694 static BOOL WINAPI dwritefontface1_HasKerningPairs(IDWriteFontFace2 *iface)
696 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
697 TRACE("(%p)\n", This);
698 return freetype_has_kerning_pairs(iface);
701 static HRESULT WINAPI dwritefontface1_GetRecommendedRenderingMode(IDWriteFontFace2 *iface,
702 FLOAT font_emsize, FLOAT dpiX, FLOAT dpiY, const DWRITE_MATRIX *transform, BOOL is_sideways,
703 DWRITE_OUTLINE_THRESHOLD threshold, DWRITE_MEASURING_MODE measuring_mode, DWRITE_RENDERING_MODE *rendering_mode)
705 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
706 FIXME("(%p)->(%f %f %f %p %d %d %d %p): stub\n", This, font_emsize, dpiX, dpiY, transform, is_sideways,
707 threshold, measuring_mode, rendering_mode);
708 return E_NOTIMPL;
711 static HRESULT WINAPI dwritefontface1_GetVerticalGlyphVariants(IDWriteFontFace2 *iface, UINT32 glyph_count,
712 const UINT16 *nominal_indices, UINT16 *vertical_indices)
714 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
715 FIXME("(%p)->(%u %p %p): stub\n", This, glyph_count, nominal_indices, vertical_indices);
716 return E_NOTIMPL;
719 static BOOL WINAPI dwritefontface1_HasVerticalGlyphVariants(IDWriteFontFace2 *iface)
721 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
722 FIXME("(%p): stub\n", This);
723 return FALSE;
726 static BOOL WINAPI dwritefontface2_IsColorFont(IDWriteFontFace2 *iface)
728 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
729 FIXME("(%p): stub\n", This);
730 return FALSE;
733 static UINT32 WINAPI dwritefontface2_GetColorPaletteCount(IDWriteFontFace2 *iface)
735 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
736 FIXME("(%p): stub\n", This);
737 return 0;
740 static UINT32 WINAPI dwritefontface2_GetPaletteEntryCount(IDWriteFontFace2 *iface)
742 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
743 FIXME("(%p): stub\n", This);
744 return 0;
747 static HRESULT WINAPI dwritefontface2_GetPaletteEntries(IDWriteFontFace2 *iface, UINT32 palette_index,
748 UINT32 first_entry_index, UINT32 entry_count, DWRITE_COLOR_F *entries)
750 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
751 FIXME("(%p)->(%u %u %u %p): stub\n", This, palette_index, first_entry_index, entry_count, entries);
752 return E_NOTIMPL;
755 static HRESULT WINAPI dwritefontface2_GetRecommendedRenderingMode(IDWriteFontFace2 *iface, FLOAT fontEmSize,
756 FLOAT dpiX, FLOAT dpiY, DWRITE_MATRIX const *transform, BOOL is_sideways, DWRITE_OUTLINE_THRESHOLD threshold,
757 DWRITE_MEASURING_MODE measuringmode, IDWriteRenderingParams *params, DWRITE_RENDERING_MODE *renderingmode,
758 DWRITE_GRID_FIT_MODE *gridfitmode)
760 struct dwrite_fontface *This = impl_from_IDWriteFontFace2(iface);
761 FIXME("(%p)->(%f %f %f %p %d %d %d %p %p %p): stub\n", This, fontEmSize, dpiX, dpiY, transform, is_sideways, threshold,
762 measuringmode, params, renderingmode, gridfitmode);
763 return E_NOTIMPL;
766 static const IDWriteFontFace2Vtbl dwritefontfacevtbl = {
767 dwritefontface_QueryInterface,
768 dwritefontface_AddRef,
769 dwritefontface_Release,
770 dwritefontface_GetType,
771 dwritefontface_GetFiles,
772 dwritefontface_GetIndex,
773 dwritefontface_GetSimulations,
774 dwritefontface_IsSymbolFont,
775 dwritefontface_GetMetrics,
776 dwritefontface_GetGlyphCount,
777 dwritefontface_GetDesignGlyphMetrics,
778 dwritefontface_GetGlyphIndices,
779 dwritefontface_TryGetFontTable,
780 dwritefontface_ReleaseFontTable,
781 dwritefontface_GetGlyphRunOutline,
782 dwritefontface_GetRecommendedRenderingMode,
783 dwritefontface_GetGdiCompatibleMetrics,
784 dwritefontface_GetGdiCompatibleGlyphMetrics,
785 dwritefontface1_GetMetrics,
786 dwritefontface1_GetGdiCompatibleMetrics,
787 dwritefontface1_GetCaretMetrics,
788 dwritefontface1_GetUnicodeRanges,
789 dwritefontface1_IsMonospacedFont,
790 dwritefontface1_GetDesignGlyphAdvances,
791 dwritefontface1_GetGdiCompatibleGlyphAdvances,
792 dwritefontface1_GetKerningPairAdjustments,
793 dwritefontface1_HasKerningPairs,
794 dwritefontface1_GetRecommendedRenderingMode,
795 dwritefontface1_GetVerticalGlyphVariants,
796 dwritefontface1_HasVerticalGlyphVariants,
797 dwritefontface2_IsColorFont,
798 dwritefontface2_GetColorPaletteCount,
799 dwritefontface2_GetPaletteEntryCount,
800 dwritefontface2_GetPaletteEntries,
801 dwritefontface2_GetRecommendedRenderingMode
804 HRESULT get_family_names_from_stream(IDWriteFontFileStream *stream, UINT32 index, DWRITE_FONT_FACE_TYPE facetype,
805 IDWriteLocalizedStrings **names)
807 const void *name_table = NULL;
808 void *name_context;
809 HRESULT hr = E_FAIL;
811 opentype_get_font_table(stream, facetype, index, MS_NAME_TAG, &name_table, &name_context, NULL, NULL);
812 if (name_table) {
813 hr = opentype_get_font_strings_from_id(name_table, DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES, names);
814 IDWriteFontFileStream_ReleaseFileFragment(stream, name_context);
816 else
817 *names = NULL;
819 return hr;
822 static HRESULT get_fontface_from_font(struct dwrite_font *font, IDWriteFontFace2 **fontface)
824 struct dwrite_font_data *data = font->data;
825 IDWriteFontFace *face;
826 HRESULT hr;
828 *fontface = NULL;
830 hr = IDWriteFactory2_CreateFontFace(data->factory, data->face_type, 1, &data->file,
831 data->face_index, font->simulations, &face);
832 if (FAILED(hr))
833 return hr;
835 hr = IDWriteFontFace_QueryInterface(face, &IID_IDWriteFontFace2, (void**)fontface);
836 IDWriteFontFace_Release(face);
838 return hr;
841 static HRESULT WINAPI dwritefont_QueryInterface(IDWriteFont2 *iface, REFIID riid, void **obj)
843 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
845 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
847 if (IsEqualIID(riid, &IID_IDWriteFont2) ||
848 IsEqualIID(riid, &IID_IDWriteFont1) ||
849 IsEqualIID(riid, &IID_IDWriteFont) ||
850 IsEqualIID(riid, &IID_IUnknown))
852 *obj = iface;
853 IDWriteFont2_AddRef(iface);
854 return S_OK;
857 *obj = NULL;
858 return E_NOINTERFACE;
861 static ULONG WINAPI dwritefont_AddRef(IDWriteFont2 *iface)
863 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
864 ULONG ref = InterlockedIncrement(&This->ref);
865 TRACE("(%p)->(%d)\n", This, ref);
866 return ref;
869 static ULONG WINAPI dwritefont_Release(IDWriteFont2 *iface)
871 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
872 ULONG ref = InterlockedDecrement(&This->ref);
874 TRACE("(%p)->(%d)\n", This, ref);
876 if (!ref) {
877 IDWriteFontFamily_Release(This->family);
878 release_font_data(This->data);
879 heap_free(This);
882 return ref;
885 static HRESULT WINAPI dwritefont_GetFontFamily(IDWriteFont2 *iface, IDWriteFontFamily **family)
887 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
888 TRACE("(%p)->(%p)\n", This, family);
890 *family = This->family;
891 IDWriteFontFamily_AddRef(*family);
892 return S_OK;
895 static DWRITE_FONT_WEIGHT WINAPI dwritefont_GetWeight(IDWriteFont2 *iface)
897 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
898 TRACE("(%p)\n", This);
899 return This->data->weight;
902 static DWRITE_FONT_STRETCH WINAPI dwritefont_GetStretch(IDWriteFont2 *iface)
904 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
905 TRACE("(%p)\n", This);
906 return This->data->stretch;
909 static DWRITE_FONT_STYLE WINAPI dwritefont_GetStyle(IDWriteFont2 *iface)
911 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
912 TRACE("(%p)\n", This);
913 return This->style;
916 static BOOL WINAPI dwritefont_IsSymbolFont(IDWriteFont2 *iface)
918 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
919 IDWriteFontFace2 *fontface;
920 HRESULT hr;
922 TRACE("(%p)\n", This);
924 hr = get_fontface_from_font(This, &fontface);
925 if (FAILED(hr))
926 return hr;
928 return IDWriteFontFace2_IsSymbolFont(fontface);
931 static HRESULT WINAPI dwritefont_GetFaceNames(IDWriteFont2 *iface, IDWriteLocalizedStrings **names)
933 static const WCHAR boldobliqueW[] = {'B','o','l','d',' ','O','b','l','i','q','u','e',0};
934 static const WCHAR obliqueW[] = {'O','b','l','i','q','u','e',0};
935 static const WCHAR boldW[] = {'B','o','l','d',0};
936 static const WCHAR enusW[] = {'e','n','-','u','s',0};
938 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
939 IDWriteLocalizedStrings *strings;
940 const WCHAR *name;
941 HRESULT hr;
943 TRACE("(%p)->(%p)\n", This, names);
945 *names = NULL;
947 if (This->simulations == DWRITE_FONT_SIMULATIONS_NONE) {
948 BOOL exists;
949 return IDWriteFont2_GetInformationalStrings(iface, DWRITE_INFORMATIONAL_STRING_WIN32_SUBFAMILY_NAMES,
950 names, &exists);
953 switch (This->simulations) {
954 case DWRITE_FONT_SIMULATIONS_BOLD|DWRITE_FONT_SIMULATIONS_OBLIQUE:
955 name = boldobliqueW;
956 break;
957 case DWRITE_FONT_SIMULATIONS_BOLD:
958 name = boldW;
959 break;
960 case DWRITE_FONT_SIMULATIONS_OBLIQUE:
961 name = obliqueW;
962 break;
963 default:
964 ERR("unknown simulations %d\n", This->simulations);
965 return E_FAIL;
968 hr = create_localizedstrings(&strings);
969 if (FAILED(hr)) return hr;
971 hr = add_localizedstring(strings, enusW, name);
972 if (FAILED(hr)) {
973 IDWriteLocalizedStrings_Release(strings);
974 return hr;
977 *names = strings;
979 return S_OK;
982 static HRESULT WINAPI dwritefont_GetInformationalStrings(IDWriteFont2 *iface,
983 DWRITE_INFORMATIONAL_STRING_ID stringid, IDWriteLocalizedStrings **strings, BOOL *exists)
985 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
986 struct dwrite_font_data *data = This->data;
987 HRESULT hr;
989 TRACE("(%p)->(%d %p %p)\n", This, stringid, strings, exists);
991 *exists = FALSE;
992 *strings = NULL;
994 if (stringid > DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_CID_NAME || stringid == DWRITE_INFORMATIONAL_STRING_NONE)
995 return S_OK;
997 if (!data->info_strings[stringid]) {
998 IDWriteFontFace2 *fontface;
999 const void *table_data;
1000 BOOL table_exists;
1001 void *context;
1002 UINT32 size;
1004 hr = get_fontface_from_font(This, &fontface);
1005 if (FAILED(hr))
1006 return hr;
1008 table_exists = FALSE;
1009 hr = IDWriteFontFace2_TryGetFontTable(fontface, MS_NAME_TAG, &table_data, &size, &context, &table_exists);
1010 if (FAILED(hr) || !table_exists)
1011 WARN("no NAME table found.\n");
1013 if (table_exists) {
1014 hr = opentype_get_font_strings_from_id(table_data, stringid, &data->info_strings[stringid]);
1015 if (FAILED(hr) || !data->info_strings[stringid])
1016 return hr;
1017 IDWriteFontFace2_ReleaseFontTable(fontface, context);
1021 hr = clone_localizedstring(data->info_strings[stringid], strings);
1022 if (FAILED(hr))
1023 return hr;
1025 *exists = TRUE;
1026 return S_OK;
1029 static DWRITE_FONT_SIMULATIONS WINAPI dwritefont_GetSimulations(IDWriteFont2 *iface)
1031 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1032 TRACE("(%p)\n", This);
1033 return This->simulations;
1036 static void WINAPI dwritefont_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS *metrics)
1038 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1040 TRACE("(%p)->(%p)\n", This, metrics);
1041 memcpy(metrics, &This->data->metrics, sizeof(*metrics));
1044 static HRESULT WINAPI dwritefont_HasCharacter(IDWriteFont2 *iface, UINT32 value, BOOL *exists)
1046 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1047 IDWriteFontFace2 *fontface;
1048 UINT16 index;
1049 HRESULT hr;
1051 TRACE("(%p)->(0x%08x %p)\n", This, value, exists);
1053 *exists = FALSE;
1055 hr = get_fontface_from_font(This, &fontface);
1056 if (FAILED(hr))
1057 return hr;
1059 index = 0;
1060 hr = IDWriteFontFace2_GetGlyphIndices(fontface, &value, 1, &index);
1061 if (FAILED(hr))
1062 return hr;
1064 *exists = index != 0;
1065 return S_OK;
1068 static HRESULT WINAPI dwritefont_CreateFontFace(IDWriteFont2 *iface, IDWriteFontFace **face)
1070 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1071 HRESULT hr;
1073 TRACE("(%p)->(%p)\n", This, face);
1075 hr = get_fontface_from_font(This, (IDWriteFontFace2**)face);
1076 if (hr == S_OK)
1077 IDWriteFontFace_AddRef(*face);
1079 return hr;
1082 static void WINAPI dwritefont1_GetMetrics(IDWriteFont2 *iface, DWRITE_FONT_METRICS1 *metrics)
1084 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1085 TRACE("(%p)->(%p)\n", This, metrics);
1086 *metrics = This->data->metrics;
1089 static void WINAPI dwritefont1_GetPanose(IDWriteFont2 *iface, DWRITE_PANOSE *panose)
1091 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1092 FIXME("(%p)->(%p): stub\n", This, panose);
1095 static HRESULT WINAPI dwritefont1_GetUnicodeRanges(IDWriteFont2 *iface, UINT32 max_count, DWRITE_UNICODE_RANGE *ranges, UINT32 *count)
1097 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1098 IDWriteFontFace2 *fontface;
1099 HRESULT hr;
1101 TRACE("(%p)->(%u %p %p)\n", This, max_count, ranges, count);
1103 hr = get_fontface_from_font(This, &fontface);
1104 if (FAILED(hr))
1105 return hr;
1107 return IDWriteFontFace2_GetUnicodeRanges(fontface, max_count, ranges, count);
1110 static BOOL WINAPI dwritefont1_IsMonospacedFont(IDWriteFont2 *iface)
1112 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1113 IDWriteFontFace2 *fontface;
1114 HRESULT hr;
1116 TRACE("(%p)\n", This);
1118 hr = get_fontface_from_font(This, &fontface);
1119 if (FAILED(hr))
1120 return hr;
1122 return IDWriteFontFace2_IsMonospacedFont(fontface);
1125 static HRESULT WINAPI dwritefont2_IsColorFont(IDWriteFont2 *iface)
1127 struct dwrite_font *This = impl_from_IDWriteFont2(iface);
1128 IDWriteFontFace2 *fontface;
1129 HRESULT hr;
1131 TRACE("(%p)\n", This);
1133 hr = get_fontface_from_font(This, &fontface);
1134 if (FAILED(hr))
1135 return hr;
1137 return IDWriteFontFace2_IsColorFont(fontface);
1140 static const IDWriteFont2Vtbl dwritefontvtbl = {
1141 dwritefont_QueryInterface,
1142 dwritefont_AddRef,
1143 dwritefont_Release,
1144 dwritefont_GetFontFamily,
1145 dwritefont_GetWeight,
1146 dwritefont_GetStretch,
1147 dwritefont_GetStyle,
1148 dwritefont_IsSymbolFont,
1149 dwritefont_GetFaceNames,
1150 dwritefont_GetInformationalStrings,
1151 dwritefont_GetSimulations,
1152 dwritefont_GetMetrics,
1153 dwritefont_HasCharacter,
1154 dwritefont_CreateFontFace,
1155 dwritefont1_GetMetrics,
1156 dwritefont1_GetPanose,
1157 dwritefont1_GetUnicodeRanges,
1158 dwritefont1_IsMonospacedFont,
1159 dwritefont2_IsColorFont
1162 static HRESULT create_font(struct dwrite_font_data *data, IDWriteFontFamily *family, DWRITE_FONT_SIMULATIONS simulations,
1163 IDWriteFont **font)
1165 struct dwrite_font *This;
1166 *font = NULL;
1168 This = heap_alloc(sizeof(struct dwrite_font));
1169 if (!This) return E_OUTOFMEMORY;
1171 This->IDWriteFont2_iface.lpVtbl = &dwritefontvtbl;
1172 This->ref = 1;
1173 This->family = family;
1174 IDWriteFontFamily_AddRef(family);
1175 This->simulations = simulations;
1176 This->style = data->style;
1177 This->data = data;
1178 InterlockedIncrement(&This->data->ref);
1180 /* set oblique style from requested simulation */
1181 if ((simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) && data->style == DWRITE_FONT_STYLE_NORMAL)
1182 This->style = DWRITE_FONT_STYLE_OBLIQUE;
1184 *font = (IDWriteFont*)&This->IDWriteFont2_iface;
1186 return S_OK;
1189 static HRESULT WINAPI dwritefontfamily_QueryInterface(IDWriteFontFamily *iface, REFIID riid, void **obj)
1191 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1192 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1194 if (IsEqualIID(riid, &IID_IUnknown) ||
1195 IsEqualIID(riid, &IID_IDWriteFontList) ||
1196 IsEqualIID(riid, &IID_IDWriteFontFamily))
1198 *obj = iface;
1199 IDWriteFontFamily_AddRef(iface);
1200 return S_OK;
1203 *obj = NULL;
1204 return E_NOINTERFACE;
1207 static ULONG WINAPI dwritefontfamily_AddRef(IDWriteFontFamily *iface)
1209 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1210 ULONG ref = InterlockedIncrement(&This->ref);
1211 TRACE("(%p)->(%d)\n", This, ref);
1212 return ref;
1215 static ULONG WINAPI dwritefontfamily_Release(IDWriteFontFamily *iface)
1217 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1218 ULONG ref = InterlockedDecrement(&This->ref);
1220 TRACE("(%p)->(%d)\n", This, ref);
1222 if (!ref)
1224 IDWriteFontCollection_Release(This->collection);
1225 release_fontfamily_data(This->data);
1226 heap_free(This);
1229 return ref;
1232 static HRESULT WINAPI dwritefontfamily_GetFontCollection(IDWriteFontFamily *iface, IDWriteFontCollection **collection)
1234 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1235 TRACE("(%p)->(%p)\n", This, collection);
1237 *collection = This->collection;
1238 IDWriteFontCollection_AddRef(This->collection);
1239 return S_OK;
1242 static UINT32 WINAPI dwritefontfamily_GetFontCount(IDWriteFontFamily *iface)
1244 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1245 TRACE("(%p)\n", This);
1246 return This->data->font_count;
1249 static HRESULT WINAPI dwritefontfamily_GetFont(IDWriteFontFamily *iface, UINT32 index, IDWriteFont **font)
1251 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1253 TRACE("(%p)->(%u %p)\n", This, index, font);
1255 *font = NULL;
1257 if (This->data->font_count == 0)
1258 return S_FALSE;
1260 if (index >= This->data->font_count)
1261 return E_INVALIDARG;
1263 return create_font(This->data->fonts[index], iface, DWRITE_FONT_SIMULATIONS_NONE, font);
1266 static HRESULT WINAPI dwritefontfamily_GetFamilyNames(IDWriteFontFamily *iface, IDWriteLocalizedStrings **names)
1268 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1269 return clone_localizedstring(This->data->familyname, names);
1272 static inline BOOL is_matching_font_style(DWRITE_FONT_STYLE style, DWRITE_FONT_STYLE font_style)
1274 if (style == font_style)
1275 return TRUE;
1277 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) && font_style == DWRITE_FONT_STYLE_NORMAL)
1278 return TRUE;
1280 return FALSE;
1283 static HRESULT WINAPI dwritefontfamily_GetFirstMatchingFont(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1284 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFont **font)
1286 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1287 UINT32 min_weight_diff = ~0u;
1288 int found = -1, i;
1290 TRACE("(%p)->(%d %d %d %p)\n", This, weight, stretch, style, font);
1292 for (i = 0; i < This->data->font_count; i++) {
1293 if (is_matching_font_style(style, This->data->fonts[i]->style) && stretch == This->data->fonts[i]->stretch) {
1294 DWRITE_FONT_WEIGHT font_weight = This->data->fonts[i]->weight;
1295 UINT32 weight_diff = abs(font_weight - weight);
1296 if (weight_diff < min_weight_diff) {
1297 min_weight_diff = weight_diff;
1298 found = i;
1303 if (found != -1) {
1304 DWRITE_FONT_SIMULATIONS simulations = DWRITE_FONT_SIMULATIONS_NONE;
1306 if (((style == DWRITE_FONT_STYLE_ITALIC) || (style == DWRITE_FONT_STYLE_OBLIQUE)) &&
1307 This->data->fonts[found]->style == DWRITE_FONT_STYLE_NORMAL) {
1308 simulations = DWRITE_FONT_SIMULATIONS_OBLIQUE;
1310 return create_font(This->data->fonts[found], iface, simulations, font);
1312 else {
1313 *font = NULL;
1314 return DWRITE_E_NOFONT;
1318 static HRESULT WINAPI dwritefontfamily_GetMatchingFonts(IDWriteFontFamily *iface, DWRITE_FONT_WEIGHT weight,
1319 DWRITE_FONT_STRETCH stretch, DWRITE_FONT_STYLE style, IDWriteFontList **fonts)
1321 struct dwrite_fontfamily *This = impl_from_IDWriteFontFamily(iface);
1322 FIXME("(%p)->(%d %d %d %p): stub\n", This, weight, stretch, style, fonts);
1323 return E_NOTIMPL;
1326 static const IDWriteFontFamilyVtbl fontfamilyvtbl = {
1327 dwritefontfamily_QueryInterface,
1328 dwritefontfamily_AddRef,
1329 dwritefontfamily_Release,
1330 dwritefontfamily_GetFontCollection,
1331 dwritefontfamily_GetFontCount,
1332 dwritefontfamily_GetFont,
1333 dwritefontfamily_GetFamilyNames,
1334 dwritefontfamily_GetFirstMatchingFont,
1335 dwritefontfamily_GetMatchingFonts
1338 static HRESULT create_fontfamily(struct dwrite_fontfamily_data *data, IDWriteFontCollection *collection, IDWriteFontFamily **family)
1340 struct dwrite_fontfamily *This;
1342 *family = NULL;
1344 This = heap_alloc(sizeof(struct dwrite_fontfamily));
1345 if (!This) return E_OUTOFMEMORY;
1347 This->IDWriteFontFamily_iface.lpVtbl = &fontfamilyvtbl;
1348 This->ref = 1;
1349 This->collection = collection;
1350 IDWriteFontCollection_AddRef(collection);
1351 This->data = data;
1352 InterlockedIncrement(&This->data->ref);
1354 *family = &This->IDWriteFontFamily_iface;
1356 return S_OK;
1359 BOOL is_system_collection(IDWriteFontCollection *collection)
1361 void *obj;
1362 return IDWriteFontCollection_QueryInterface(collection, &IID_issystemcollection, (void**)&obj) == S_OK;
1365 static HRESULT WINAPI dwritefontcollection_QueryInterface(IDWriteFontCollection *iface, REFIID riid, void **obj)
1367 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1368 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1370 if (IsEqualIID(riid, &IID_IUnknown) ||
1371 IsEqualIID(riid, &IID_IDWriteFontCollection))
1373 *obj = iface;
1374 IDWriteFontCollection_AddRef(iface);
1375 return S_OK;
1378 *obj = NULL;
1380 if (This->is_system && IsEqualIID(riid, &IID_issystemcollection))
1381 return S_OK;
1383 return E_NOINTERFACE;
1386 static ULONG WINAPI dwritefontcollection_AddRef(IDWriteFontCollection *iface)
1388 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1389 ULONG ref = InterlockedIncrement(&This->ref);
1390 TRACE("(%p)->(%d)\n", This, ref);
1391 return ref;
1394 static ULONG WINAPI dwritefontcollection_Release(IDWriteFontCollection *iface)
1396 unsigned int i;
1397 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1398 ULONG ref = InterlockedDecrement(&This->ref);
1399 TRACE("(%p)->(%d)\n", This, ref);
1401 if (!ref) {
1402 for (i = 0; i < This->family_count; i++)
1403 release_fontfamily_data(This->family_data[i]);
1404 heap_free(This->family_data);
1405 heap_free(This);
1408 return ref;
1411 static UINT32 WINAPI dwritefontcollection_GetFontFamilyCount(IDWriteFontCollection *iface)
1413 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1414 TRACE("(%p)\n", This);
1415 return This->family_count;
1418 static HRESULT WINAPI dwritefontcollection_GetFontFamily(IDWriteFontCollection *iface, UINT32 index, IDWriteFontFamily **family)
1420 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1422 TRACE("(%p)->(%u %p)\n", This, index, family);
1424 if (index >= This->family_count) {
1425 *family = NULL;
1426 return E_FAIL;
1429 return create_fontfamily(This->family_data[index], iface, family);
1432 static UINT32 collection_find_family(struct dwrite_fontcollection *collection, const WCHAR *name)
1434 UINT32 i;
1436 for (i = 0; i < collection->family_count; i++) {
1437 IDWriteLocalizedStrings *family_name = collection->family_data[i]->familyname;
1438 HRESULT hr;
1439 int j;
1441 for (j = 0; j < IDWriteLocalizedStrings_GetCount(family_name); j++) {
1442 WCHAR buffer[255];
1443 hr = IDWriteLocalizedStrings_GetString(family_name, j, buffer, 255);
1444 if (SUCCEEDED(hr) && !strcmpiW(buffer, name))
1445 return i;
1449 return ~0u;
1452 static HRESULT WINAPI dwritefontcollection_FindFamilyName(IDWriteFontCollection *iface, const WCHAR *name, UINT32 *index, BOOL *exists)
1454 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1455 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(name), index, exists);
1456 *index = collection_find_family(This, name);
1457 *exists = *index != ~0u;
1458 return S_OK;
1461 static BOOL is_same_fontfile(IDWriteFontFile *left, IDWriteFontFile *right)
1463 UINT32 left_key_size, right_key_size;
1464 const void *left_key, *right_key;
1465 HRESULT hr;
1467 if (left == right)
1468 return TRUE;
1470 hr = IDWriteFontFile_GetReferenceKey(left, &left_key, &left_key_size);
1471 if (FAILED(hr))
1472 return FALSE;
1474 hr = IDWriteFontFile_GetReferenceKey(right, &right_key, &right_key_size);
1475 if (FAILED(hr))
1476 return FALSE;
1478 if (left_key_size != right_key_size)
1479 return FALSE;
1481 return !memcmp(left_key, right_key, left_key_size);
1484 static HRESULT WINAPI dwritefontcollection_GetFontFromFontFace(IDWriteFontCollection *iface, IDWriteFontFace *face, IDWriteFont **font)
1486 struct dwrite_fontcollection *This = impl_from_IDWriteFontCollection(iface);
1487 struct dwrite_fontfamily_data *found_family = NULL;
1488 struct dwrite_font_data *found_font = NULL;
1489 DWRITE_FONT_SIMULATIONS simulations;
1490 IDWriteFontFamily *family;
1491 UINT32 i, j, face_index;
1492 IDWriteFontFile *file;
1493 HRESULT hr;
1495 TRACE("(%p)->(%p %p)\n", This, face, font);
1497 *font = NULL;
1499 if (!face)
1500 return E_INVALIDARG;
1502 i = 1;
1503 hr = IDWriteFontFace_GetFiles(face, &i, &file);
1504 if (FAILED(hr))
1505 return hr;
1506 face_index = IDWriteFontFace_GetIndex(face);
1508 for (i = 0; i < This->family_count; i++) {
1509 struct dwrite_fontfamily_data *family_data = This->family_data[i];
1510 for (j = 0; j < family_data->font_count; j++) {
1511 struct dwrite_font_data *font_data = family_data->fonts[j];
1513 if (face_index == font_data->face_index && is_same_fontfile(file, font_data->file)) {
1514 found_font = font_data;
1515 found_family = family_data;
1516 break;
1521 if (!found_font)
1522 return DWRITE_E_NOFONT;
1524 hr = create_fontfamily(found_family, iface, &family);
1525 if (FAILED(hr))
1526 return hr;
1528 simulations = IDWriteFontFace_GetSimulations(face);
1529 hr = create_font(found_font, family, simulations, font);
1530 IDWriteFontFamily_Release(family);
1531 return hr;
1534 static const IDWriteFontCollectionVtbl fontcollectionvtbl = {
1535 dwritefontcollection_QueryInterface,
1536 dwritefontcollection_AddRef,
1537 dwritefontcollection_Release,
1538 dwritefontcollection_GetFontFamilyCount,
1539 dwritefontcollection_GetFontFamily,
1540 dwritefontcollection_FindFamilyName,
1541 dwritefontcollection_GetFontFromFontFace
1544 static HRESULT fontfamily_add_font(struct dwrite_fontfamily_data *family_data, struct dwrite_font_data *font_data)
1546 if (family_data->font_count + 1 >= family_data->font_alloc) {
1547 struct dwrite_font_data **new_list;
1548 UINT32 new_alloc;
1550 new_alloc = family_data->font_alloc * 2;
1551 new_list = heap_realloc(family_data->fonts, sizeof(*family_data->fonts) * new_alloc);
1552 if (!new_list)
1553 return E_OUTOFMEMORY;
1554 family_data->fonts = new_list;
1555 family_data->font_alloc = new_alloc;
1558 family_data->fonts[family_data->font_count] = font_data;
1559 family_data->font_count++;
1560 return S_OK;
1563 static HRESULT fontcollection_add_family(struct dwrite_fontcollection *collection, struct dwrite_fontfamily_data *family)
1565 if (collection->family_alloc < collection->family_count + 1) {
1566 struct dwrite_fontfamily_data **new_list;
1567 UINT32 new_alloc;
1569 new_alloc = collection->family_alloc * 2;
1570 new_list = heap_realloc(collection->family_data, sizeof(*new_list) * new_alloc);
1571 if (!new_list)
1572 return E_OUTOFMEMORY;
1574 collection->family_alloc = new_alloc;
1575 collection->family_data = new_list;
1578 collection->family_data[collection->family_count] = family;
1579 collection->family_count++;
1581 return S_OK;
1584 static HRESULT init_font_collection(struct dwrite_fontcollection *collection, BOOL is_system)
1586 collection->IDWriteFontCollection_iface.lpVtbl = &fontcollectionvtbl;
1587 collection->ref = 1;
1588 collection->family_count = 0;
1589 collection->family_alloc = 2;
1590 collection->is_system = is_system;
1592 collection->family_data = heap_alloc(sizeof(*collection->family_data)*2);
1593 if (!collection->family_data)
1594 return E_OUTOFMEMORY;
1596 return S_OK;
1599 HRESULT get_filestream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
1601 IDWriteFontFileLoader *loader;
1602 const void *key;
1603 UINT32 key_size;
1604 HRESULT hr;
1606 *stream = NULL;
1608 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
1609 if (FAILED(hr))
1610 return hr;
1612 hr = IDWriteFontFile_GetLoader(file, &loader);
1613 if (FAILED(hr))
1614 return hr;
1616 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
1617 IDWriteFontFileLoader_Release(loader);
1618 if (FAILED(hr))
1619 return hr;
1621 return hr;
1624 static HRESULT init_font_data(IDWriteFactory2 *factory, IDWriteFontFile *file, UINT32 face_index, DWRITE_FONT_FACE_TYPE face_type,
1625 IDWriteFontFileStream **stream, struct dwrite_font_data **ret)
1627 void *os2_context, *head_context;
1628 const void *tt_os2 = NULL, *tt_head = NULL;
1629 struct dwrite_font_data *data;
1630 HRESULT hr;
1632 data = heap_alloc_zero(sizeof(*data));
1633 if (!data)
1634 return E_OUTOFMEMORY;
1636 hr = get_filestream_from_file(file, stream);
1637 if (FAILED(hr)) {
1638 heap_free(data);
1639 return hr;
1642 data->ref = 1;
1643 data->factory = factory;
1644 data->file = file;
1645 data->face_index = face_index;
1646 data->face_type = face_type;
1647 IDWriteFontFile_AddRef(file);
1648 IDWriteFactory2_AddRef(factory);
1650 opentype_get_font_table(*stream, face_type, face_index, MS_OS2_TAG, &tt_os2, &os2_context, NULL, NULL);
1651 opentype_get_font_table(*stream, face_type, face_index, MS_HEAD_TAG, &tt_head, &head_context, NULL, NULL);
1653 opentype_get_font_properties(*stream, face_type, face_index, &data->stretch, &data->weight, &data->style);
1654 opentype_get_font_metrics(*stream, face_type, face_index, &data->metrics, NULL);
1656 if (tt_os2)
1657 IDWriteFontFileStream_ReleaseFileFragment(*stream, os2_context);
1658 if (tt_head)
1659 IDWriteFontFileStream_ReleaseFileFragment(*stream, head_context);
1661 *ret = data;
1662 return S_OK;
1665 static HRESULT init_fontfamily_data(IDWriteLocalizedStrings *familyname, struct dwrite_fontfamily_data **ret)
1667 struct dwrite_fontfamily_data *data;
1669 data = heap_alloc(sizeof(*data));
1670 if (!data)
1671 return E_OUTOFMEMORY;
1673 data->ref = 1;
1674 data->font_count = 0;
1675 data->font_alloc = 2;
1677 data->fonts = heap_alloc(sizeof(*data->fonts)*data->font_alloc);
1678 if (!data->fonts) {
1679 heap_free(data);
1680 return E_OUTOFMEMORY;
1683 data->familyname = familyname;
1684 IDWriteLocalizedStrings_AddRef(familyname);
1686 *ret = data;
1687 return S_OK;
1690 HRESULT create_font_collection(IDWriteFactory2* factory, IDWriteFontFileEnumerator *enumerator, BOOL is_system, IDWriteFontCollection **ret)
1692 struct dwrite_fontcollection *collection;
1693 BOOL current = FALSE;
1694 HRESULT hr = S_OK;
1696 *ret = NULL;
1698 collection = heap_alloc(sizeof(struct dwrite_fontcollection));
1699 if (!collection) return E_OUTOFMEMORY;
1701 hr = init_font_collection(collection, is_system);
1702 if (FAILED(hr)) {
1703 heap_free(collection);
1704 return hr;
1707 *ret = &collection->IDWriteFontCollection_iface;
1709 TRACE("building font collection:\n");
1711 while (hr == S_OK) {
1712 DWRITE_FONT_FACE_TYPE face_type;
1713 DWRITE_FONT_FILE_TYPE file_type;
1714 IDWriteFontFile *file;
1715 UINT32 face_count;
1716 BOOL supported;
1717 int i;
1719 current = FALSE;
1720 hr = IDWriteFontFileEnumerator_MoveNext(enumerator, &current);
1721 if (FAILED(hr) || !current)
1722 break;
1724 hr = IDWriteFontFileEnumerator_GetCurrentFontFile(enumerator, &file);
1725 if (FAILED(hr))
1726 break;
1728 /* failed font files are skipped */
1729 hr = IDWriteFontFile_Analyze(file, &supported, &file_type, &face_type, &face_count);
1730 if (FAILED(hr) || !supported || face_count == 0) {
1731 TRACE("unsupported font (%p, 0x%08x, %d, %u)\n", file, hr, supported, face_count);
1732 IDWriteFontFile_Release(file);
1733 hr = S_OK;
1734 continue;
1737 for (i = 0; i < face_count; i++) {
1738 IDWriteLocalizedStrings *family_name = NULL;
1739 struct dwrite_font_data *font_data;
1740 IDWriteFontFileStream *stream;
1741 WCHAR buffer[255];
1742 UINT32 index;
1744 /* alloc and init new font data structure */
1745 hr = init_font_data(factory, file, i, face_type, &stream, &font_data);
1746 if (FAILED(hr))
1747 break;
1749 /* get family name from font file */
1750 hr = get_family_names_from_stream(stream, i, face_type, &family_name);
1751 IDWriteFontFileStream_Release(stream);
1752 if (FAILED(hr)) {
1753 WARN("unable to get family name from font\n");
1754 release_font_data(font_data);
1755 continue;
1758 buffer[0] = 0;
1759 IDWriteLocalizedStrings_GetString(family_name, 0, buffer, sizeof(buffer)/sizeof(WCHAR));
1761 index = collection_find_family(collection, buffer);
1762 if (index != ~0u)
1763 hr = fontfamily_add_font(collection->family_data[index], font_data);
1764 else {
1765 struct dwrite_fontfamily_data *family_data;
1767 /* create and init new family */
1768 hr = init_fontfamily_data(family_name, &family_data);
1769 if (hr == S_OK) {
1770 /* add font to family, family - to collection */
1771 hr = fontfamily_add_font(family_data, font_data);
1772 if (hr == S_OK)
1773 hr = fontcollection_add_family(collection, family_data);
1775 if (FAILED(hr))
1776 release_fontfamily_data(family_data);
1780 IDWriteLocalizedStrings_Release(family_name);
1782 if (FAILED(hr))
1783 break;
1786 IDWriteFontFile_Release(file);
1789 return hr;
1792 struct system_fontfile_enumerator
1794 IDWriteFontFileEnumerator IDWriteFontFileEnumerator_iface;
1795 LONG ref;
1797 IDWriteFactory2 *factory;
1798 HKEY hkey;
1799 int index;
1802 static inline struct system_fontfile_enumerator *impl_from_IDWriteFontFileEnumerator(IDWriteFontFileEnumerator* iface)
1804 return CONTAINING_RECORD(iface, struct system_fontfile_enumerator, IDWriteFontFileEnumerator_iface);
1807 static HRESULT WINAPI systemfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1809 *obj = NULL;
1811 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1812 IDWriteFontFileEnumerator_AddRef(iface);
1813 *obj = iface;
1814 return S_OK;
1817 return E_NOINTERFACE;
1820 static ULONG WINAPI systemfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1822 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1823 return InterlockedIncrement(&enumerator->ref);
1826 static ULONG WINAPI systemfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1828 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1829 ULONG ref = InterlockedDecrement(&enumerator->ref);
1831 if (!ref) {
1832 IDWriteFactory2_Release(enumerator->factory);
1833 RegCloseKey(enumerator->hkey);
1834 heap_free(enumerator);
1837 return ref;
1840 static HRESULT WINAPI systemfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1842 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1843 DWORD ret, type, count;
1844 WCHAR *filename;
1845 HRESULT hr;
1847 *file = NULL;
1849 if (enumerator->index < 0)
1850 return E_FAIL;
1852 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1853 return E_FAIL;
1855 if (!(filename = heap_alloc(count)))
1856 return E_OUTOFMEMORY;
1858 ret = RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, (BYTE*)filename, &count);
1859 if (ret) {
1860 heap_free(filename);
1861 return E_FAIL;
1864 /* Fonts installed in 'Fonts' system dir don't get full path in registry font files cache */
1865 if (!strchrW(filename, '\\')) {
1866 static const WCHAR fontsW[] = {'\\','f','o','n','t','s','\\',0};
1867 WCHAR fullpathW[MAX_PATH];
1869 GetWindowsDirectoryW(fullpathW, sizeof(fullpathW)/sizeof(WCHAR));
1870 strcatW(fullpathW, fontsW);
1871 strcatW(fullpathW, filename);
1873 hr = IDWriteFactory2_CreateFontFileReference(enumerator->factory, fullpathW, NULL, file);
1875 else
1876 hr = IDWriteFactory2_CreateFontFileReference(enumerator->factory, filename, NULL, file);
1878 heap_free(filename);
1879 return hr;
1882 static HRESULT WINAPI systemfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1884 struct system_fontfile_enumerator *enumerator = impl_from_IDWriteFontFileEnumerator(iface);
1886 *current = FALSE;
1887 enumerator->index++;
1889 /* iterate until we find next string value */
1890 while (1) {
1891 DWORD type = 0, count;
1892 if (RegEnumValueW(enumerator->hkey, enumerator->index, NULL, NULL, NULL, &type, NULL, &count))
1893 break;
1894 if (type == REG_SZ) {
1895 *current = TRUE;
1896 break;
1898 enumerator->index++;
1901 TRACE("index = %d, current = %d\n", enumerator->index, *current);
1902 return S_OK;
1905 static const struct IDWriteFontFileEnumeratorVtbl systemfontfileenumeratorvtbl =
1907 systemfontfileenumerator_QueryInterface,
1908 systemfontfileenumerator_AddRef,
1909 systemfontfileenumerator_Release,
1910 systemfontfileenumerator_MoveNext,
1911 systemfontfileenumerator_GetCurrentFontFile
1914 static HRESULT create_system_fontfile_enumerator(IDWriteFactory2 *factory, IDWriteFontFileEnumerator **ret)
1916 struct system_fontfile_enumerator *enumerator;
1917 static const WCHAR fontslistW[] = {
1918 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1919 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1920 'F','o','n','t','s',0
1923 *ret = NULL;
1925 enumerator = heap_alloc(sizeof(*enumerator));
1926 if (!enumerator)
1927 return E_OUTOFMEMORY;
1929 enumerator->IDWriteFontFileEnumerator_iface.lpVtbl = &systemfontfileenumeratorvtbl;
1930 enumerator->ref = 1;
1931 enumerator->factory = factory;
1932 enumerator->index = -1;
1933 IDWriteFactory2_AddRef(factory);
1935 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, fontslistW, 0, GENERIC_READ, &enumerator->hkey)) {
1936 ERR("failed to open fonts list key\n");
1937 IDWriteFactory2_Release(factory);
1938 heap_free(enumerator);
1939 return E_FAIL;
1942 *ret = &enumerator->IDWriteFontFileEnumerator_iface;
1944 return S_OK;
1947 HRESULT get_system_fontcollection(IDWriteFactory2 *factory, IDWriteFontCollection **collection)
1949 IDWriteFontFileEnumerator *enumerator;
1950 HRESULT hr;
1952 *collection = NULL;
1954 hr = create_system_fontfile_enumerator(factory, &enumerator);
1955 if (FAILED(hr))
1956 return hr;
1958 TRACE("building system font collection for factory %p\n", factory);
1959 hr = create_font_collection(factory, enumerator, TRUE, collection);
1960 IDWriteFontFileEnumerator_Release(enumerator);
1961 return hr;
1964 static HRESULT WINAPI eudcfontfileenumerator_QueryInterface(IDWriteFontFileEnumerator *iface, REFIID riid, void **obj)
1966 *obj = NULL;
1968 if (IsEqualIID(riid, &IID_IDWriteFontFileEnumerator) || IsEqualIID(riid, &IID_IUnknown)) {
1969 IDWriteFontFileEnumerator_AddRef(iface);
1970 *obj = iface;
1971 return S_OK;
1974 return E_NOINTERFACE;
1977 static ULONG WINAPI eudcfontfileenumerator_AddRef(IDWriteFontFileEnumerator *iface)
1979 return 2;
1982 static ULONG WINAPI eudcfontfileenumerator_Release(IDWriteFontFileEnumerator *iface)
1984 return 1;
1987 static HRESULT WINAPI eudcfontfileenumerator_GetCurrentFontFile(IDWriteFontFileEnumerator *iface, IDWriteFontFile **file)
1989 *file = NULL;
1990 return E_FAIL;
1993 static HRESULT WINAPI eudcfontfileenumerator_MoveNext(IDWriteFontFileEnumerator *iface, BOOL *current)
1995 *current = FALSE;
1996 return S_OK;
1999 static const struct IDWriteFontFileEnumeratorVtbl eudcfontfileenumeratorvtbl =
2001 eudcfontfileenumerator_QueryInterface,
2002 eudcfontfileenumerator_AddRef,
2003 eudcfontfileenumerator_Release,
2004 eudcfontfileenumerator_MoveNext,
2005 eudcfontfileenumerator_GetCurrentFontFile
2008 static IDWriteFontFileEnumerator eudc_fontfile_enumerator = { &eudcfontfileenumeratorvtbl };
2010 HRESULT get_eudc_fontcollection(IDWriteFactory2 *factory, IDWriteFontCollection **collection)
2012 TRACE("building EUDC font collection for factory %p\n", factory);
2013 return create_font_collection(factory, &eudc_fontfile_enumerator, FALSE, collection);
2016 static HRESULT WINAPI dwritefontfile_QueryInterface(IDWriteFontFile *iface, REFIID riid, void **obj)
2018 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2020 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2022 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFile))
2024 *obj = iface;
2025 IDWriteFontFile_AddRef(iface);
2026 return S_OK;
2029 *obj = NULL;
2030 return E_NOINTERFACE;
2033 static ULONG WINAPI dwritefontfile_AddRef(IDWriteFontFile *iface)
2035 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2036 ULONG ref = InterlockedIncrement(&This->ref);
2037 TRACE("(%p)->(%d)\n", This, ref);
2038 return ref;
2041 static ULONG WINAPI dwritefontfile_Release(IDWriteFontFile *iface)
2043 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2044 ULONG ref = InterlockedDecrement(&This->ref);
2046 TRACE("(%p)->(%d)\n", This, ref);
2048 if (!ref)
2050 IDWriteFontFileLoader_Release(This->loader);
2051 if (This->stream) IDWriteFontFileStream_Release(This->stream);
2052 heap_free(This->reference_key);
2053 heap_free(This);
2056 return ref;
2059 static HRESULT WINAPI dwritefontfile_GetReferenceKey(IDWriteFontFile *iface, const void **fontFileReferenceKey, UINT32 *fontFileReferenceKeySize)
2061 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2062 TRACE("(%p)->(%p, %p)\n", This, fontFileReferenceKey, fontFileReferenceKeySize);
2063 *fontFileReferenceKey = This->reference_key;
2064 *fontFileReferenceKeySize = This->key_size;
2066 return S_OK;
2069 static HRESULT WINAPI dwritefontfile_GetLoader(IDWriteFontFile *iface, IDWriteFontFileLoader **fontFileLoader)
2071 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2072 TRACE("(%p)->(%p)\n", This, fontFileLoader);
2073 *fontFileLoader = This->loader;
2074 IDWriteFontFileLoader_AddRef(This->loader);
2076 return S_OK;
2079 static HRESULT WINAPI dwritefontfile_Analyze(IDWriteFontFile *iface, BOOL *isSupportedFontType, DWRITE_FONT_FILE_TYPE *fontFileType, DWRITE_FONT_FACE_TYPE *fontFaceType, UINT32 *numberOfFaces)
2081 struct dwrite_fontfile *This = impl_from_IDWriteFontFile(iface);
2082 IDWriteFontFileStream *stream;
2083 HRESULT hr;
2085 TRACE("(%p)->(%p, %p, %p, %p)\n", This, isSupportedFontType, fontFileType, fontFaceType, numberOfFaces);
2087 *isSupportedFontType = FALSE;
2088 *fontFileType = DWRITE_FONT_FILE_TYPE_UNKNOWN;
2089 if (fontFaceType)
2090 *fontFaceType = DWRITE_FONT_FACE_TYPE_UNKNOWN;
2091 *numberOfFaces = 0;
2093 hr = IDWriteFontFileLoader_CreateStreamFromKey(This->loader, This->reference_key, This->key_size, &stream);
2094 if (FAILED(hr))
2095 return hr;
2097 hr = opentype_analyze_font(stream, numberOfFaces, fontFileType, fontFaceType, isSupportedFontType);
2099 /* TODO: Further Analysis */
2100 IDWriteFontFileStream_Release(stream);
2101 return S_OK;
2104 static const IDWriteFontFileVtbl dwritefontfilevtbl = {
2105 dwritefontfile_QueryInterface,
2106 dwritefontfile_AddRef,
2107 dwritefontfile_Release,
2108 dwritefontfile_GetReferenceKey,
2109 dwritefontfile_GetLoader,
2110 dwritefontfile_Analyze,
2113 HRESULT create_font_file(IDWriteFontFileLoader *loader, const void *reference_key, UINT32 key_size, IDWriteFontFile **font_file)
2115 struct dwrite_fontfile *This;
2117 This = heap_alloc(sizeof(struct dwrite_fontfile));
2118 if (!This) return E_OUTOFMEMORY;
2120 This->IDWriteFontFile_iface.lpVtbl = &dwritefontfilevtbl;
2121 This->ref = 1;
2122 IDWriteFontFileLoader_AddRef(loader);
2123 This->loader = loader;
2124 This->stream = NULL;
2125 This->reference_key = heap_alloc(key_size);
2126 memcpy(This->reference_key, reference_key, key_size);
2127 This->key_size = key_size;
2129 *font_file = &This->IDWriteFontFile_iface;
2131 return S_OK;
2134 static HRESULT get_stream_from_file(IDWriteFontFile *file, IDWriteFontFileStream **stream)
2136 IDWriteFontFileLoader *loader;
2137 UINT32 key_size;
2138 const void *key;
2139 HRESULT hr;
2141 *stream = NULL;
2142 hr = IDWriteFontFile_GetLoader(file, &loader);
2143 if (FAILED(hr))
2144 return hr;
2146 hr = IDWriteFontFile_GetReferenceKey(file, &key, &key_size);
2147 if (FAILED(hr)) {
2148 IDWriteFontFileLoader_Release(loader);
2149 return hr;
2152 hr = IDWriteFontFileLoader_CreateStreamFromKey(loader, key, key_size, stream);
2153 IDWriteFontFileLoader_Release(loader);
2155 return hr;
2158 HRESULT create_fontface(DWRITE_FONT_FACE_TYPE facetype, UINT32 files_number, IDWriteFontFile* const* font_files, UINT32 index,
2159 DWRITE_FONT_SIMULATIONS simulations, IDWriteFontFace2 **ret)
2161 struct dwrite_fontface *fontface;
2162 HRESULT hr = S_OK;
2163 int i;
2165 *ret = NULL;
2167 fontface = heap_alloc(sizeof(struct dwrite_fontface));
2168 if (!fontface)
2169 return E_OUTOFMEMORY;
2171 fontface->files = heap_alloc_zero(sizeof(*fontface->files) * files_number);
2172 fontface->streams = heap_alloc_zero(sizeof(*fontface->streams) * files_number);
2174 if (!fontface->files || !fontface->streams) {
2175 heap_free(fontface->files);
2176 heap_free(fontface->streams);
2177 heap_free(fontface);
2178 return E_OUTOFMEMORY;
2181 fontface->IDWriteFontFace2_iface.lpVtbl = &dwritefontfacevtbl;
2182 fontface->ref = 1;
2183 fontface->type = facetype;
2184 fontface->file_count = files_number;
2185 fontface->cmap.data = NULL;
2186 fontface->cmap.context = NULL;
2187 fontface->cmap.size = 0;
2188 fontface->index = index;
2189 fontface->simulations = simulations;
2190 memset(fontface->glyphs, 0, sizeof(fontface->glyphs));
2192 for (i = 0; i < fontface->file_count; i++) {
2193 hr = get_stream_from_file(font_files[i], &fontface->streams[i]);
2194 if (FAILED(hr)) {
2195 IDWriteFontFace2_Release(&fontface->IDWriteFontFace2_iface);
2196 return hr;
2199 fontface->files[i] = font_files[i];
2200 IDWriteFontFile_AddRef(font_files[i]);
2203 opentype_get_font_metrics(fontface->streams[0], facetype, index, &fontface->metrics, &fontface->caret);
2204 if (simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
2205 /* TODO: test what happens if caret is already slanted */
2206 if (fontface->caret.slopeRise == 1) {
2207 fontface->caret.slopeRise = fontface->metrics.designUnitsPerEm;
2208 fontface->caret.slopeRun = fontface->caret.slopeRise / 3;
2212 *ret = &fontface->IDWriteFontFace2_iface;
2213 return S_OK;
2216 /* IDWriteLocalFontFileLoader and its required IDWriteFontFileStream */
2217 struct local_refkey
2219 FILETIME writetime;
2220 WCHAR name[1];
2223 struct local_cached_stream
2225 struct list entry;
2226 IDWriteFontFileStream *stream;
2227 struct local_refkey *key;
2228 UINT32 key_size;
2231 struct dwrite_localfontfilestream
2233 IDWriteFontFileStream IDWriteFontFileStream_iface;
2234 LONG ref;
2236 struct local_cached_stream *entry;
2237 const void *file_ptr;
2238 UINT64 size;
2241 struct dwrite_localfontfileloader {
2242 IDWriteLocalFontFileLoader IDWriteLocalFontFileLoader_iface;
2243 LONG ref;
2245 struct list streams;
2248 static inline struct dwrite_localfontfileloader *impl_from_IDWriteLocalFontFileLoader(IDWriteLocalFontFileLoader *iface)
2250 return CONTAINING_RECORD(iface, struct dwrite_localfontfileloader, IDWriteLocalFontFileLoader_iface);
2253 static inline struct dwrite_localfontfilestream *impl_from_IDWriteFontFileStream(IDWriteFontFileStream *iface)
2255 return CONTAINING_RECORD(iface, struct dwrite_localfontfilestream, IDWriteFontFileStream_iface);
2258 static HRESULT WINAPI localfontfilestream_QueryInterface(IDWriteFontFileStream *iface, REFIID riid, void **obj)
2260 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2261 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2262 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileStream))
2264 *obj = iface;
2265 IDWriteFontFileStream_AddRef(iface);
2266 return S_OK;
2269 *obj = NULL;
2270 return E_NOINTERFACE;
2273 static ULONG WINAPI localfontfilestream_AddRef(IDWriteFontFileStream *iface)
2275 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2276 ULONG ref = InterlockedIncrement(&This->ref);
2277 TRACE("(%p)->(%d)\n", This, ref);
2278 return ref;
2281 static inline void release_cached_stream(struct local_cached_stream *stream)
2283 list_remove(&stream->entry);
2284 heap_free(stream->key);
2285 heap_free(stream);
2288 static ULONG WINAPI localfontfilestream_Release(IDWriteFontFileStream *iface)
2290 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2291 ULONG ref = InterlockedDecrement(&This->ref);
2293 TRACE("(%p)->(%d)\n", This, ref);
2295 if (!ref) {
2296 UnmapViewOfFile(This->file_ptr);
2297 release_cached_stream(This->entry);
2298 heap_free(This);
2301 return ref;
2304 static HRESULT WINAPI localfontfilestream_ReadFileFragment(IDWriteFontFileStream *iface, void const **fragment_start, UINT64 offset, UINT64 fragment_size, void **fragment_context)
2306 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2308 TRACE("(%p)->(%p, %s, %s, %p)\n",This, fragment_start,
2309 wine_dbgstr_longlong(offset), wine_dbgstr_longlong(fragment_size), fragment_context);
2311 *fragment_context = NULL;
2313 if ((offset >= This->size - 1) || (fragment_size > This->size - offset)) {
2314 *fragment_start = NULL;
2315 return E_FAIL;
2318 *fragment_start = (char*)This->file_ptr + offset;
2319 return S_OK;
2322 static void WINAPI localfontfilestream_ReleaseFileFragment(IDWriteFontFileStream *iface, void *fragment_context)
2324 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2325 TRACE("(%p)->(%p)\n", This, fragment_context);
2328 static HRESULT WINAPI localfontfilestream_GetFileSize(IDWriteFontFileStream *iface, UINT64 *size)
2330 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2331 TRACE("(%p)->(%p)\n", This, size);
2332 *size = This->size;
2333 return S_OK;
2336 static HRESULT WINAPI localfontfilestream_GetLastWriteTime(IDWriteFontFileStream *iface, UINT64 *last_writetime)
2338 struct dwrite_localfontfilestream *This = impl_from_IDWriteFontFileStream(iface);
2339 ULARGE_INTEGER li;
2341 TRACE("(%p)->(%p)\n", This, last_writetime);
2343 li.u.LowPart = This->entry->key->writetime.dwLowDateTime;
2344 li.u.HighPart = This->entry->key->writetime.dwHighDateTime;
2345 *last_writetime = li.QuadPart;
2347 return S_OK;
2350 static const IDWriteFontFileStreamVtbl localfontfilestreamvtbl =
2352 localfontfilestream_QueryInterface,
2353 localfontfilestream_AddRef,
2354 localfontfilestream_Release,
2355 localfontfilestream_ReadFileFragment,
2356 localfontfilestream_ReleaseFileFragment,
2357 localfontfilestream_GetFileSize,
2358 localfontfilestream_GetLastWriteTime
2361 static HRESULT create_localfontfilestream(const void *file_ptr, UINT64 size, struct local_cached_stream *entry, IDWriteFontFileStream** iface)
2363 struct dwrite_localfontfilestream *This = heap_alloc(sizeof(struct dwrite_localfontfilestream));
2364 if (!This)
2365 return E_OUTOFMEMORY;
2367 This->IDWriteFontFileStream_iface.lpVtbl = &localfontfilestreamvtbl;
2368 This->ref = 1;
2370 This->file_ptr = file_ptr;
2371 This->size = size;
2372 This->entry = entry;
2374 *iface = &This->IDWriteFontFileStream_iface;
2375 return S_OK;
2378 static HRESULT WINAPI localfontfileloader_QueryInterface(IDWriteLocalFontFileLoader *iface, REFIID riid, void **obj)
2380 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2382 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
2384 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteFontFileLoader) || IsEqualIID(riid, &IID_IDWriteLocalFontFileLoader))
2386 *obj = iface;
2387 IDWriteLocalFontFileLoader_AddRef(iface);
2388 return S_OK;
2391 *obj = NULL;
2392 return E_NOINTERFACE;
2395 static ULONG WINAPI localfontfileloader_AddRef(IDWriteLocalFontFileLoader *iface)
2397 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2398 ULONG ref = InterlockedIncrement(&This->ref);
2399 TRACE("(%p)->(%d)\n", This, ref);
2400 return ref;
2403 static ULONG WINAPI localfontfileloader_Release(IDWriteLocalFontFileLoader *iface)
2405 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2406 ULONG ref = InterlockedDecrement(&This->ref);
2408 TRACE("(%p)->(%d)\n", This, ref);
2410 if (!ref) {
2411 struct local_cached_stream *stream, *stream2;
2413 /* This will detach all entries from cache. Entries are released together with streams,
2414 so stream controls its lifetime. */
2415 LIST_FOR_EACH_ENTRY_SAFE(stream, stream2, &This->streams, struct local_cached_stream, entry)
2416 list_init(&stream->entry);
2418 heap_free(This);
2421 return ref;
2424 static HRESULT WINAPI localfontfileloader_CreateStreamFromKey(IDWriteLocalFontFileLoader *iface, const void *key, UINT32 key_size, IDWriteFontFileStream **ret)
2426 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2427 const struct local_refkey *refkey = key;
2428 struct local_cached_stream *stream;
2429 IDWriteFontFileStream *filestream;
2430 HANDLE file, mapping;
2431 LARGE_INTEGER size;
2432 void *file_ptr;
2433 HRESULT hr;
2435 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, ret);
2436 TRACE("name: %s\n", debugstr_w(refkey->name));
2438 /* search cache first */
2439 LIST_FOR_EACH_ENTRY(stream, &This->streams, struct local_cached_stream, entry) {
2440 if (key_size == stream->key_size && !memcmp(stream->key, key, key_size)) {
2441 *ret = stream->stream;
2442 IDWriteFontFileStream_AddRef(*ret);
2443 return S_OK;
2447 *ret = NULL;
2449 file = CreateFileW(refkey->name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
2450 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2451 if (file == INVALID_HANDLE_VALUE)
2452 return E_FAIL;
2454 GetFileSizeEx(file, &size);
2455 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
2456 CloseHandle(file);
2457 if (!mapping)
2458 return E_FAIL;
2460 file_ptr = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
2461 CloseHandle(mapping);
2463 stream = heap_alloc(sizeof(*stream));
2464 if (!stream) {
2465 UnmapViewOfFile(file_ptr);
2466 return E_OUTOFMEMORY;
2469 stream->key = heap_alloc(key_size);
2470 if (!stream->key) {
2471 UnmapViewOfFile(file_ptr);
2472 heap_free(stream);
2473 return E_OUTOFMEMORY;
2476 stream->key_size = key_size;
2477 memcpy(stream->key, key, key_size);
2479 hr = create_localfontfilestream(file_ptr, size.QuadPart, stream, &filestream);
2480 if (FAILED(hr)) {
2481 UnmapViewOfFile(file_ptr);
2482 heap_free(stream->key);
2483 heap_free(stream);
2484 return hr;
2487 stream->stream = filestream;
2488 list_add_head(&This->streams, &stream->entry);
2490 *ret = stream->stream;
2492 return S_OK;
2495 static HRESULT WINAPI localfontfileloader_GetFilePathLengthFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, UINT32 *length)
2497 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2498 const struct local_refkey *refkey = key;
2500 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, length);
2502 *length = strlenW(refkey->name);
2503 return S_OK;
2506 static HRESULT WINAPI localfontfileloader_GetFilePathFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, WCHAR *path, UINT32 length)
2508 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2509 const struct local_refkey *refkey = key;
2511 TRACE("(%p)->(%p, %i, %p, %i)\n", This, key, key_size, path, length);
2513 if (length < strlenW(refkey->name))
2514 return E_INVALIDARG;
2516 strcpyW(path, refkey->name);
2517 return S_OK;
2520 static HRESULT WINAPI localfontfileloader_GetLastWriteTimeFromKey(IDWriteLocalFontFileLoader *iface, void const *key, UINT32 key_size, FILETIME *writetime)
2522 struct dwrite_localfontfileloader *This = impl_from_IDWriteLocalFontFileLoader(iface);
2523 const struct local_refkey *refkey = key;
2525 TRACE("(%p)->(%p, %i, %p)\n", This, key, key_size, writetime);
2527 *writetime = refkey->writetime;
2528 return S_OK;
2531 static const struct IDWriteLocalFontFileLoaderVtbl localfontfileloadervtbl = {
2532 localfontfileloader_QueryInterface,
2533 localfontfileloader_AddRef,
2534 localfontfileloader_Release,
2535 localfontfileloader_CreateStreamFromKey,
2536 localfontfileloader_GetFilePathLengthFromKey,
2537 localfontfileloader_GetFilePathFromKey,
2538 localfontfileloader_GetLastWriteTimeFromKey
2541 HRESULT create_localfontfileloader(IDWriteLocalFontFileLoader** iface)
2543 struct dwrite_localfontfileloader *This = heap_alloc(sizeof(struct dwrite_localfontfileloader));
2544 if (!This)
2545 return E_OUTOFMEMORY;
2547 This->IDWriteLocalFontFileLoader_iface.lpVtbl = &localfontfileloadervtbl;
2548 This->ref = 1;
2549 list_init(&This->streams);
2551 *iface = &This->IDWriteLocalFontFileLoader_iface;
2552 return S_OK;
2555 HRESULT get_local_refkey(const WCHAR *path, const FILETIME *writetime, void **key, UINT32 *size)
2557 struct local_refkey *refkey;
2559 *size = FIELD_OFFSET(struct local_refkey, name) + (strlenW(path)+1)*sizeof(WCHAR);
2560 *key = NULL;
2562 refkey = heap_alloc(*size);
2563 if (!refkey)
2564 return E_OUTOFMEMORY;
2566 if (writetime)
2567 refkey->writetime = *writetime;
2568 else {
2569 WIN32_FILE_ATTRIBUTE_DATA info;
2571 if (GetFileAttributesExW(path, GetFileExInfoStandard, &info))
2572 refkey->writetime = info.ftLastWriteTime;
2573 else
2574 memset(&refkey->writetime, 0, sizeof(refkey->writetime));
2576 strcpyW(refkey->name, path);
2578 *key = refkey;
2580 return S_OK;
2583 /* IDWriteGlyphRunAnalysis */
2584 static HRESULT WINAPI glyphrunanalysis_QueryInterface(IDWriteGlyphRunAnalysis *iface, REFIID riid, void **ppv)
2586 struct dwrite_glyphrunanalysis *This = impl_from_IDWriteGlyphRunAnalysis(iface);
2588 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
2590 if (IsEqualIID(riid, &IID_IDWriteGlyphRunAnalysis) ||
2591 IsEqualIID(riid, &IID_IUnknown))
2593 *ppv = iface;
2594 IDWriteGlyphRunAnalysis_AddRef(iface);
2595 return S_OK;
2598 *ppv = NULL;
2599 return E_NOINTERFACE;
2602 static ULONG WINAPI glyphrunanalysis_AddRef(IDWriteGlyphRunAnalysis *iface)
2604 struct dwrite_glyphrunanalysis *This = impl_from_IDWriteGlyphRunAnalysis(iface);
2605 ULONG ref = InterlockedIncrement(&This->ref);
2606 TRACE("(%p)->(%u)\n", This, ref);
2607 return ref;
2610 static ULONG WINAPI glyphrunanalysis_Release(IDWriteGlyphRunAnalysis *iface)
2612 struct dwrite_glyphrunanalysis *This = impl_from_IDWriteGlyphRunAnalysis(iface);
2613 ULONG ref = InterlockedDecrement(&This->ref);
2615 TRACE("(%p)->(%u)\n", This, ref);
2617 if (!ref) {
2618 heap_free(This);
2621 return ref;
2624 static HRESULT WINAPI glyphrunanalysis_GetAlphaTextureBounds(IDWriteGlyphRunAnalysis *iface, DWRITE_TEXTURE_TYPE type, RECT* bounds)
2626 struct dwrite_glyphrunanalysis *This = impl_from_IDWriteGlyphRunAnalysis(iface);
2627 FIXME("(%p)->(%d %p): stub\n", This, type, bounds);
2628 return E_NOTIMPL;
2631 static HRESULT WINAPI glyphrunanalysis_CreateAlphaTexture(IDWriteGlyphRunAnalysis *iface, DWRITE_TEXTURE_TYPE type,
2632 RECT const* bounds, BYTE* alphaValues, UINT32 bufferSize)
2634 struct dwrite_glyphrunanalysis *This = impl_from_IDWriteGlyphRunAnalysis(iface);
2635 FIXME("(%p)->(%d %p %p %u): stub\n", This, type, bounds, alphaValues, bufferSize);
2636 return E_NOTIMPL;
2639 static HRESULT WINAPI glyphrunanalysis_GetAlphaBlendParams(IDWriteGlyphRunAnalysis *iface, IDWriteRenderingParams *params,
2640 FLOAT *blendGamma, FLOAT *blendEnhancedContrast, FLOAT *blendClearTypeLevel)
2642 struct dwrite_glyphrunanalysis *This = impl_from_IDWriteGlyphRunAnalysis(iface);
2643 FIXME("(%p)->(%p %p %p %p): stub\n", This, params, blendGamma, blendEnhancedContrast, blendClearTypeLevel);
2644 return E_NOTIMPL;
2647 static const struct IDWriteGlyphRunAnalysisVtbl glyphrunanalysisvtbl = {
2648 glyphrunanalysis_QueryInterface,
2649 glyphrunanalysis_AddRef,
2650 glyphrunanalysis_Release,
2651 glyphrunanalysis_GetAlphaTextureBounds,
2652 glyphrunanalysis_CreateAlphaTexture,
2653 glyphrunanalysis_GetAlphaBlendParams
2656 HRESULT create_glyphrunanalysis(DWRITE_RENDERING_MODE rendering_mode, IDWriteGlyphRunAnalysis **ret)
2658 struct dwrite_glyphrunanalysis *analysis;
2660 *ret = NULL;
2662 /* check for valid rendering mode */
2663 if ((UINT32)rendering_mode >= DWRITE_RENDERING_MODE_OUTLINE || rendering_mode == DWRITE_RENDERING_MODE_DEFAULT)
2664 return E_INVALIDARG;
2666 analysis = heap_alloc(sizeof(*analysis));
2667 if (!analysis)
2668 return E_OUTOFMEMORY;
2670 analysis->IDWriteGlyphRunAnalysis_iface.lpVtbl = &glyphrunanalysisvtbl;
2671 analysis->ref = 1;
2673 *ret = &analysis->IDWriteGlyphRunAnalysis_iface;
2674 return S_OK;