msvcrt: Stop at trylevel for ControlPc on target frame for non-consolidate unwinds.
[wine.git] / dlls / dwrite / freetype.c
blob2ad513e537d30e1926d9c16aa1fbe0f8fb0bee61
1 /*
2 * FreeType integration
4 * Copyright 2014-2017 Nikolay Sivov for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include "config.h"
24 #include "wine/port.h"
26 #ifdef HAVE_FT2BUILD_H
27 #include <ft2build.h>
28 #include FT_CACHE_H
29 #include FT_FREETYPE_H
30 #include FT_OUTLINE_H
31 #include FT_TRUETYPE_TABLES_H
32 #endif /* HAVE_FT2BUILD_H */
34 #include "windef.h"
35 #include "wine/library.h"
36 #include "wine/debug.h"
38 #include "dwrite_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
42 #ifdef HAVE_FREETYPE
44 static CRITICAL_SECTION freetype_cs;
45 static CRITICAL_SECTION_DEBUG critsect_debug =
47 0, 0, &freetype_cs,
48 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
49 0, 0, { (DWORD_PTR)(__FILE__ ": freetype_cs") }
51 static CRITICAL_SECTION freetype_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
53 static void *ft_handle = NULL;
54 static FT_Library library = 0;
55 static FTC_Manager cache_manager = 0;
56 static FTC_CMapCache cmap_cache = 0;
57 static FTC_ImageCache image_cache = 0;
58 typedef struct
60 FT_Int major;
61 FT_Int minor;
62 FT_Int patch;
63 } FT_Version_t;
65 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
66 MAKE_FUNCPTR(FT_Done_FreeType);
67 MAKE_FUNCPTR(FT_Done_Glyph);
68 MAKE_FUNCPTR(FT_Get_First_Char);
69 MAKE_FUNCPTR(FT_Get_Kerning);
70 MAKE_FUNCPTR(FT_Get_Sfnt_Table);
71 MAKE_FUNCPTR(FT_Glyph_Copy);
72 MAKE_FUNCPTR(FT_Glyph_Get_CBox);
73 MAKE_FUNCPTR(FT_Glyph_Transform);
74 MAKE_FUNCPTR(FT_Init_FreeType);
75 MAKE_FUNCPTR(FT_Library_Version);
76 MAKE_FUNCPTR(FT_Load_Glyph);
77 MAKE_FUNCPTR(FT_Matrix_Multiply);
78 MAKE_FUNCPTR(FT_New_Memory_Face);
79 MAKE_FUNCPTR(FT_Outline_Copy);
80 MAKE_FUNCPTR(FT_Outline_Decompose);
81 MAKE_FUNCPTR(FT_Outline_Done);
82 MAKE_FUNCPTR(FT_Outline_Embolden);
83 MAKE_FUNCPTR(FT_Outline_Get_Bitmap);
84 MAKE_FUNCPTR(FT_Outline_New);
85 MAKE_FUNCPTR(FT_Outline_Transform);
86 MAKE_FUNCPTR(FT_Outline_Translate);
87 MAKE_FUNCPTR(FTC_CMapCache_Lookup);
88 MAKE_FUNCPTR(FTC_CMapCache_New);
89 MAKE_FUNCPTR(FTC_ImageCache_Lookup);
90 MAKE_FUNCPTR(FTC_ImageCache_New);
91 MAKE_FUNCPTR(FTC_Manager_New);
92 MAKE_FUNCPTR(FTC_Manager_Done);
93 MAKE_FUNCPTR(FTC_Manager_LookupFace);
94 MAKE_FUNCPTR(FTC_Manager_LookupSize);
95 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID);
96 #undef MAKE_FUNCPTR
97 static FT_Error (*pFT_Outline_EmboldenXY)(FT_Outline *, FT_Pos, FT_Pos);
99 struct face_finalizer_data
101 IDWriteFontFileStream *stream;
102 void *context;
105 static void face_finalizer(void *object)
107 FT_Face face = object;
108 struct face_finalizer_data *data = (struct face_finalizer_data *)face->generic.data;
110 IDWriteFontFileStream_ReleaseFileFragment(data->stream, data->context);
111 IDWriteFontFileStream_Release(data->stream);
112 heap_free(data);
115 static FT_Error face_requester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *face)
117 IDWriteFontFace *fontface = (IDWriteFontFace*)face_id;
118 IDWriteFontFileStream *stream;
119 IDWriteFontFile *file;
120 const void *data_ptr;
121 UINT32 index, count;
122 FT_Error fterror;
123 UINT64 data_size;
124 void *context;
125 HRESULT hr;
127 *face = NULL;
129 if (!fontface) {
130 WARN("NULL fontface requested.\n");
131 return FT_Err_Ok;
134 count = 1;
135 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
136 if (FAILED(hr))
137 return FT_Err_Ok;
139 hr = get_filestream_from_file(file, &stream);
140 IDWriteFontFile_Release(file);
141 if (FAILED(hr))
142 return FT_Err_Ok;
144 hr = IDWriteFontFileStream_GetFileSize(stream, &data_size);
145 if (FAILED(hr)) {
146 fterror = FT_Err_Invalid_Stream_Read;
147 goto fail;
150 hr = IDWriteFontFileStream_ReadFileFragment(stream, &data_ptr, 0, data_size, &context);
151 if (FAILED(hr)) {
152 fterror = FT_Err_Invalid_Stream_Read;
153 goto fail;
156 index = IDWriteFontFace_GetIndex(fontface);
157 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
158 if (fterror == FT_Err_Ok) {
159 struct face_finalizer_data *data;
161 data = heap_alloc(sizeof(*data));
162 data->stream = stream;
163 data->context = context;
165 (*face)->generic.data = data;
166 (*face)->generic.finalizer = face_finalizer;
167 return fterror;
169 else
170 IDWriteFontFileStream_ReleaseFileFragment(stream, context);
172 fail:
173 IDWriteFontFileStream_Release(stream);
175 return fterror;
178 BOOL init_freetype(void)
180 FT_Version_t FT_Version;
182 ft_handle = wine_dlopen(SONAME_LIBFREETYPE, RTLD_NOW, NULL, 0);
183 if (!ft_handle) {
184 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
185 return FALSE;
188 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(ft_handle, #f, NULL, 0)) == NULL){WARN("Can't find symbol %s\n", #f); goto sym_not_found;}
189 LOAD_FUNCPTR(FT_Done_FreeType)
190 LOAD_FUNCPTR(FT_Done_Glyph)
191 LOAD_FUNCPTR(FT_Get_First_Char)
192 LOAD_FUNCPTR(FT_Get_Kerning)
193 LOAD_FUNCPTR(FT_Get_Sfnt_Table)
194 LOAD_FUNCPTR(FT_Glyph_Copy)
195 LOAD_FUNCPTR(FT_Glyph_Get_CBox)
196 LOAD_FUNCPTR(FT_Glyph_Transform)
197 LOAD_FUNCPTR(FT_Init_FreeType)
198 LOAD_FUNCPTR(FT_Library_Version)
199 LOAD_FUNCPTR(FT_Load_Glyph)
200 LOAD_FUNCPTR(FT_Matrix_Multiply)
201 LOAD_FUNCPTR(FT_New_Memory_Face)
202 LOAD_FUNCPTR(FT_Outline_Copy)
203 LOAD_FUNCPTR(FT_Outline_Decompose)
204 LOAD_FUNCPTR(FT_Outline_Done)
205 LOAD_FUNCPTR(FT_Outline_Embolden)
206 LOAD_FUNCPTR(FT_Outline_Get_Bitmap)
207 LOAD_FUNCPTR(FT_Outline_New)
208 LOAD_FUNCPTR(FT_Outline_Transform)
209 LOAD_FUNCPTR(FT_Outline_Translate)
210 LOAD_FUNCPTR(FTC_CMapCache_Lookup)
211 LOAD_FUNCPTR(FTC_CMapCache_New)
212 LOAD_FUNCPTR(FTC_ImageCache_Lookup)
213 LOAD_FUNCPTR(FTC_ImageCache_New)
214 LOAD_FUNCPTR(FTC_Manager_New)
215 LOAD_FUNCPTR(FTC_Manager_Done)
216 LOAD_FUNCPTR(FTC_Manager_LookupFace)
217 LOAD_FUNCPTR(FTC_Manager_LookupSize)
218 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
219 #undef LOAD_FUNCPTR
220 pFT_Outline_EmboldenXY = wine_dlsym(ft_handle, "FT_Outline_EmboldenXY", NULL, 0);
222 if (pFT_Init_FreeType(&library) != 0) {
223 ERR("Can't init FreeType library\n");
224 wine_dlclose(ft_handle, NULL, 0);
225 ft_handle = NULL;
226 return FALSE;
228 pFT_Library_Version(library, &FT_Version.major, &FT_Version.minor, &FT_Version.patch);
230 /* init cache manager */
231 if (pFTC_Manager_New(library, 0, 0, 0, &face_requester, NULL, &cache_manager) != 0 ||
232 pFTC_CMapCache_New(cache_manager, &cmap_cache) != 0 ||
233 pFTC_ImageCache_New(cache_manager, &image_cache) != 0) {
235 ERR("Failed to init FreeType cache\n");
236 pFTC_Manager_Done(cache_manager);
237 pFT_Done_FreeType(library);
238 wine_dlclose(ft_handle, NULL, 0);
239 ft_handle = NULL;
240 return FALSE;
243 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
244 return TRUE;
246 sym_not_found:
247 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
248 wine_dlclose(ft_handle, NULL, 0);
249 ft_handle = NULL;
250 return FALSE;
253 void release_freetype(void)
255 pFTC_Manager_Done(cache_manager);
256 pFT_Done_FreeType(library);
259 void freetype_notify_cacheremove(IDWriteFontFace4 *fontface)
261 EnterCriticalSection(&freetype_cs);
262 pFTC_Manager_RemoveFaceID(cache_manager, fontface);
263 LeaveCriticalSection(&freetype_cs);
266 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
268 FTC_ScalerRec scaler;
269 FT_Size size;
271 scaler.face_id = fontface;
272 scaler.width = unitsperEm;
273 scaler.height = unitsperEm;
274 scaler.pixel = 1;
275 scaler.x_res = 0;
276 scaler.y_res = 0;
278 EnterCriticalSection(&freetype_cs);
279 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
280 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
281 USHORT simulations = IDWriteFontFace4_GetSimulations(fontface);
282 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
284 ret->leftSideBearing = metrics->horiBearingX;
285 ret->advanceWidth = metrics->horiAdvance;
286 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
287 ret->topSideBearing = metrics->vertBearingY;
288 ret->advanceHeight = metrics->vertAdvance;
289 ret->bottomSideBearing = metrics->vertAdvance - metrics->vertBearingY - metrics->height;
290 ret->verticalOriginY = metrics->height + metrics->vertBearingY;
292 /* Adjust in case of bold simulation, glyphs without contours are ignored. */
293 if (simulations & DWRITE_FONT_SIMULATIONS_BOLD && size->face->glyph->format == FT_GLYPH_FORMAT_OUTLINE &&
294 size->face->glyph->outline.n_contours != 0) {
295 if (ret->advanceWidth)
296 ret->advanceWidth += (unitsperEm + 49) / 50;
300 LeaveCriticalSection(&freetype_cs);
302 return S_OK;
305 BOOL freetype_is_monospaced(IDWriteFontFace4 *fontface)
307 BOOL is_monospaced = FALSE;
308 FT_Face face;
310 EnterCriticalSection(&freetype_cs);
311 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
312 is_monospaced = !!FT_IS_FIXED_WIDTH(face);
313 LeaveCriticalSection(&freetype_cs);
315 return is_monospaced;
318 struct decompose_context {
319 IDWriteGeometrySink *sink;
320 FLOAT xoffset;
321 FLOAT yoffset;
322 BOOL figure_started;
323 BOOL figure_closed;
324 BOOL move_to; /* last call was 'move_to' */
325 FT_Vector origin; /* 'pen' position from last call */
328 static inline void ft_vector_to_d2d_point(const FT_Vector *v, FLOAT xoffset, FLOAT yoffset, D2D1_POINT_2F *p)
330 p->x = (v->x / 64.0f) + xoffset;
331 p->y = (v->y / 64.0f) + yoffset;
334 static int decompose_move_to(const FT_Vector *to, void *user)
336 struct decompose_context *ctxt = (struct decompose_context*)user;
337 D2D1_POINT_2F point;
339 if (ctxt->figure_started) {
340 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
341 ctxt->figure_closed = TRUE;
343 else
344 ctxt->figure_closed = FALSE;
345 ctxt->figure_started = TRUE;
347 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
348 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt->sink, point, D2D1_FIGURE_BEGIN_FILLED);
349 ctxt->move_to = TRUE;
350 ctxt->origin = *to;
351 return 0;
354 static int decompose_line_to(const FT_Vector *to, void *user)
356 struct decompose_context *ctxt = (struct decompose_context*)user;
357 /* special case for empty contours, in a way freetype returns them */
358 if (ctxt->move_to && !memcmp(to, &ctxt->origin, sizeof(*to))) {
359 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
360 ctxt->figure_closed = TRUE;
362 else {
363 D2D1_POINT_2F point;
364 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
365 ID2D1SimplifiedGeometrySink_AddLines(ctxt->sink, &point, 1);
366 ctxt->figure_closed = FALSE;
368 ctxt->move_to = FALSE;
369 ctxt->origin = *to;
370 return 0;
373 static int decompose_conic_to(const FT_Vector *control, const FT_Vector *to, void *user)
375 struct decompose_context *ctxt = (struct decompose_context*)user;
376 D2D1_POINT_2F points[3];
377 FT_Vector cubic[3];
379 /* convert from quadratic to cubic */
382 The parametric eqn for a cubic Bezier is, from PLRM:
383 r(t) = at^3 + bt^2 + ct + r0
384 with the control points:
385 r1 = r0 + c/3
386 r2 = r1 + (c + b)/3
387 r3 = r0 + c + b + a
389 A quadratic Bezier has the form:
390 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
392 So equating powers of t leads to:
393 r1 = 2/3 p1 + 1/3 p0
394 r2 = 2/3 p1 + 1/3 p2
395 and of course r0 = p0, r3 = p2
398 /* r1 = 1/3 p0 + 2/3 p1
399 r2 = 1/3 p2 + 2/3 p1 */
400 cubic[0].x = (2 * control->x + 1) / 3;
401 cubic[0].y = (2 * control->y + 1) / 3;
402 cubic[1] = cubic[0];
403 cubic[0].x += (ctxt->origin.x + 1) / 3;
404 cubic[0].y += (ctxt->origin.y + 1) / 3;
405 cubic[1].x += (to->x + 1) / 3;
406 cubic[1].y += (to->y + 1) / 3;
407 cubic[2] = *to;
409 ft_vector_to_d2d_point(cubic, ctxt->xoffset, ctxt->yoffset, points);
410 ft_vector_to_d2d_point(cubic + 1, ctxt->xoffset, ctxt->yoffset, points + 1);
411 ft_vector_to_d2d_point(cubic + 2, ctxt->xoffset, ctxt->yoffset, points + 2);
412 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
413 ctxt->figure_closed = FALSE;
414 ctxt->move_to = FALSE;
415 ctxt->origin = *to;
416 return 0;
419 static int decompose_cubic_to(const FT_Vector *control1, const FT_Vector *control2,
420 const FT_Vector *to, void *user)
422 struct decompose_context *ctxt = (struct decompose_context*)user;
423 D2D1_POINT_2F points[3];
425 ft_vector_to_d2d_point(control1, ctxt->xoffset, ctxt->yoffset, points);
426 ft_vector_to_d2d_point(control2, ctxt->xoffset, ctxt->yoffset, points + 1);
427 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, points + 2);
428 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
429 ctxt->figure_closed = FALSE;
430 ctxt->move_to = FALSE;
431 ctxt->origin = *to;
432 return 0;
435 static void decompose_outline(FT_Outline *outline, FLOAT xoffset, FLOAT yoffset, IDWriteGeometrySink *sink)
437 static const FT_Outline_Funcs decompose_funcs = {
438 decompose_move_to,
439 decompose_line_to,
440 decompose_conic_to,
441 decompose_cubic_to,
445 struct decompose_context context;
447 context.sink = sink;
448 context.xoffset = xoffset;
449 context.yoffset = yoffset;
450 context.figure_started = FALSE;
451 context.figure_closed = FALSE;
452 context.move_to = FALSE;
453 context.origin.x = 0;
454 context.origin.y = 0;
456 pFT_Outline_Decompose(outline, &decompose_funcs, &context);
458 if (!context.figure_closed && outline->n_points)
459 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
462 static void embolden_glyph_outline(FT_Outline *outline, FLOAT emsize)
464 FT_Pos strength;
466 strength = MulDiv(emsize, 1 << 6, 24);
467 if (pFT_Outline_EmboldenXY)
468 pFT_Outline_EmboldenXY(outline, strength, 0);
469 else
470 pFT_Outline_Embolden(outline, strength);
473 static void embolden_glyph(FT_Glyph glyph, FLOAT emsize)
475 FT_OutlineGlyph outline_glyph = (FT_OutlineGlyph)glyph;
477 if (glyph->format != FT_GLYPH_FORMAT_OUTLINE)
478 return;
480 embolden_glyph_outline(&outline_glyph->outline, emsize);
483 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs,
484 FLOAT const *advances, DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
486 FTC_ScalerRec scaler;
487 USHORT simulations;
488 HRESULT hr = S_OK;
489 FT_Size size;
491 if (!count)
492 return S_OK;
494 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_WINDING);
496 simulations = IDWriteFontFace4_GetSimulations(fontface);
498 scaler.face_id = fontface;
499 scaler.width = emSize;
500 scaler.height = emSize;
501 scaler.pixel = 1;
502 scaler.x_res = 0;
503 scaler.y_res = 0;
505 EnterCriticalSection(&freetype_cs);
506 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
507 FLOAT advance = 0.0f;
508 UINT32 g;
510 for (g = 0; g < count; g++) {
511 if (pFT_Load_Glyph(size->face, glyphs[g], FT_LOAD_NO_BITMAP) == 0) {
512 FLOAT ft_advance = size->face->glyph->metrics.horiAdvance >> 6;
513 FT_Outline *outline = &size->face->glyph->outline;
514 FLOAT xoffset = 0.0f, yoffset = 0.0f;
515 FT_Matrix m;
517 if (simulations & DWRITE_FONT_SIMULATIONS_BOLD)
518 embolden_glyph_outline(outline, emSize);
520 m.xx = 1 << 16;
521 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
522 m.yx = 0;
523 m.yy = -(1 << 16); /* flip Y axis */
525 pFT_Outline_Transform(outline, &m);
527 /* glyph offsets act as current glyph adjustment */
528 if (offsets) {
529 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
530 yoffset -= offsets[g].ascenderOffset;
533 if (g == 0 && is_rtl)
534 advance = advances ? -advances[g] : -ft_advance;
536 xoffset += advance;
537 decompose_outline(outline, xoffset, yoffset, sink);
539 /* update advance to next glyph */
540 if (advances)
541 advance += is_rtl ? -advances[g] : advances[g];
542 else
543 advance += is_rtl ? -ft_advance : ft_advance;
547 else
548 hr = E_FAIL;
549 LeaveCriticalSection(&freetype_cs);
551 return hr;
554 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
556 UINT16 count = 0;
557 FT_Face face;
559 EnterCriticalSection(&freetype_cs);
560 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
561 count = face->num_glyphs;
562 LeaveCriticalSection(&freetype_cs);
564 return count;
567 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
568 UINT16 *glyphs)
570 UINT32 i;
572 EnterCriticalSection(&freetype_cs);
573 for (i = 0; i < count; i++) {
574 if (charmap == -1)
575 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoints[i]);
576 else {
577 UINT32 codepoint = codepoints[i];
578 /* special handling for symbol fonts */
579 if (codepoint < 0x100) codepoint += 0xf000;
580 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint);
581 if (!glyphs[i])
582 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint - 0xf000);
585 LeaveCriticalSection(&freetype_cs);
588 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
590 BOOL has_kerning_pairs = FALSE;
591 FT_Face face;
593 EnterCriticalSection(&freetype_cs);
594 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
595 has_kerning_pairs = !!FT_HAS_KERNING(face);
596 LeaveCriticalSection(&freetype_cs);
598 return has_kerning_pairs;
601 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
603 INT32 adjustment = 0;
604 FT_Face face;
606 EnterCriticalSection(&freetype_cs);
607 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
608 FT_Vector kern;
609 if (FT_HAS_KERNING(face)) {
610 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
611 adjustment = kern.x;
614 LeaveCriticalSection(&freetype_cs);
616 return adjustment;
619 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
621 ft_matrix->xx = m->m11 * 0x10000;
622 ft_matrix->xy = -m->m21 * 0x10000;
623 ft_matrix->yx = -m->m12 * 0x10000;
624 ft_matrix->yy = m->m22 * 0x10000;
627 /* Should be used only while holding 'freetype_cs' */
628 static BOOL is_face_scalable(IDWriteFontFace4 *fontface)
630 FT_Face face;
631 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
632 return FT_IS_SCALABLE(face);
633 else
634 return FALSE;
637 static BOOL get_glyph_transform(struct dwrite_glyphbitmap *bitmap, FT_Matrix *ret)
639 USHORT simulations = IDWriteFontFace4_GetSimulations(bitmap->fontface);
640 FT_Matrix m;
642 ret->xx = 1 << 16;
643 ret->xy = 0;
644 ret->yx = 0;
645 ret->yy = 1 << 16;
647 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
648 Disable transform if that's the case. */
649 if (!is_face_scalable(bitmap->fontface) || (!bitmap->m && simulations == 0))
650 return FALSE;
652 if (simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
653 m.xx = 1 << 16;
654 m.xy = (1 << 16) / 3;
655 m.yx = 0;
656 m.yy = 1 << 16;
657 pFT_Matrix_Multiply(&m, ret);
660 if (bitmap->m) {
661 ft_matrix_from_dwrite_matrix(bitmap->m, &m);
662 pFT_Matrix_Multiply(&m, ret);
665 return TRUE;
668 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
670 USHORT simulations = IDWriteFontFace4_GetSimulations(bitmap->fontface);
671 FTC_ImageTypeRec imagetype;
672 FT_BBox bbox = { 0 };
673 BOOL needs_transform;
674 FT_Glyph glyph;
675 FT_Matrix m;
677 EnterCriticalSection(&freetype_cs);
679 needs_transform = get_glyph_transform(bitmap, &m);
681 imagetype.face_id = bitmap->fontface;
682 imagetype.width = 0;
683 imagetype.height = bitmap->emsize;
684 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
686 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
687 if (needs_transform) {
688 FT_Glyph glyph_copy;
690 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
691 if (simulations & DWRITE_FONT_SIMULATIONS_BOLD)
692 embolden_glyph(glyph_copy, bitmap->emsize);
694 /* Includes oblique and user transform. */
695 pFT_Glyph_Transform(glyph_copy, &m, NULL);
696 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
697 pFT_Done_Glyph(glyph_copy);
700 else
701 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
704 LeaveCriticalSection(&freetype_cs);
706 /* flip Y axis */
707 SetRect(&bitmap->bbox, bbox.xMin, -bbox.yMax, bbox.xMax, -bbox.yMin);
710 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
712 FTC_ScalerRec scaler;
713 FT_Size size;
715 scaler.face_id = fontface;
716 scaler.width = unitsperEm;
717 scaler.height = unitsperEm;
718 scaler.pixel = 1;
719 scaler.x_res = 0;
720 scaler.y_res = 0;
722 EnterCriticalSection(&freetype_cs);
723 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
724 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
725 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
727 bbox->left = metrics->horiBearingX;
728 bbox->right = bbox->left + metrics->horiAdvance;
729 bbox->top = -metrics->horiBearingY;
730 bbox->bottom = bbox->top + metrics->height;
733 LeaveCriticalSection(&freetype_cs);
736 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
738 const RECT *bbox = &bitmap->bbox;
739 int width = bbox->right - bbox->left;
740 int height = bbox->bottom - bbox->top;
742 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
743 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
744 const FT_Outline *src = &outline->outline;
745 FT_Bitmap ft_bitmap;
746 FT_Outline copy;
748 ft_bitmap.width = width;
749 ft_bitmap.rows = height;
750 ft_bitmap.pitch = bitmap->pitch;
751 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
752 ft_bitmap.buffer = bitmap->buf;
754 /* Note: FreeType will only set 'black' bits for us. */
755 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
756 pFT_Outline_Copy(src, &copy);
757 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
758 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
759 pFT_Outline_Done(library, &copy);
762 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
763 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
764 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
765 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
766 int h = min(height, ft_bitmap->rows);
768 while (h--) {
769 memcpy(dst, src, w);
770 src += ft_bitmap->pitch;
771 dst += bitmap->pitch;
774 else
775 FIXME("format %x not handled\n", glyph->format);
777 return TRUE;
780 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
782 const RECT *bbox = &bitmap->bbox;
783 int width = bbox->right - bbox->left;
784 int height = bbox->bottom - bbox->top;
785 BOOL ret = FALSE;
787 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
788 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
789 const FT_Outline *src = &outline->outline;
790 FT_Bitmap ft_bitmap;
791 FT_Outline copy;
793 ft_bitmap.width = width;
794 ft_bitmap.rows = height;
795 ft_bitmap.pitch = bitmap->pitch;
796 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
797 ft_bitmap.buffer = bitmap->buf;
799 /* Note: FreeType will only set 'black' bits for us. */
800 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
801 pFT_Outline_Copy(src, &copy);
802 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
803 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
804 pFT_Outline_Done(library, &copy);
807 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
808 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
809 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
810 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
811 int h = min(height, ft_bitmap->rows);
813 while (h--) {
814 memcpy(dst, src, w);
815 src += ft_bitmap->pitch;
816 dst += bitmap->pitch;
819 ret = TRUE;
821 else
822 FIXME("format %x not handled\n", glyph->format);
824 return ret;
827 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
829 USHORT simulations = IDWriteFontFace4_GetSimulations(bitmap->fontface);
830 FTC_ImageTypeRec imagetype;
831 BOOL needs_transform;
832 BOOL ret = FALSE;
833 FT_Glyph glyph;
834 FT_Matrix m;
836 EnterCriticalSection(&freetype_cs);
838 needs_transform = get_glyph_transform(bitmap, &m);
840 imagetype.face_id = bitmap->fontface;
841 imagetype.width = 0;
842 imagetype.height = bitmap->emsize;
843 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
845 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
846 FT_Glyph glyph_copy;
848 if (needs_transform) {
849 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
850 if (simulations & DWRITE_FONT_SIMULATIONS_BOLD)
851 embolden_glyph(glyph_copy, bitmap->emsize);
853 /* Includes oblique and user transform. */
854 pFT_Glyph_Transform(glyph_copy, &m, NULL);
855 glyph = glyph_copy;
858 else
859 glyph_copy = NULL;
861 if (bitmap->type == DWRITE_TEXTURE_CLEARTYPE_3x1)
862 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
863 else
864 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
866 if (glyph_copy)
867 pFT_Done_Glyph(glyph_copy);
870 LeaveCriticalSection(&freetype_cs);
872 return ret;
875 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
877 INT charmap_index = -1;
878 FT_Face face;
880 *is_symbol = FALSE;
882 EnterCriticalSection(&freetype_cs);
883 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
884 TT_OS2 *os2 = pFT_Get_Sfnt_Table(face, ft_sfnt_os2);
885 FT_Int i;
887 if (os2) {
888 FT_UInt dummy;
889 if (os2->version == 0)
890 *is_symbol = pFT_Get_First_Char(face, &dummy) >= 0x100;
891 else
892 *is_symbol = !!(os2->ulCodePageRange1 & FS_SYMBOL);
895 for (i = 0; i < face->num_charmaps; i++)
896 if (face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL) {
897 *is_symbol = TRUE;
898 charmap_index = i;
899 break;
902 LeaveCriticalSection(&freetype_cs);
904 return charmap_index;
907 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
908 BOOL *has_contours)
910 FTC_ImageTypeRec imagetype;
911 FT_Glyph glyph;
912 INT32 advance;
914 imagetype.face_id = fontface;
915 imagetype.width = 0;
916 imagetype.height = emSize;
917 imagetype.flags = FT_LOAD_DEFAULT;
918 if (mode == DWRITE_MEASURING_MODE_NATURAL)
919 imagetype.flags |= FT_LOAD_NO_HINTING;
921 EnterCriticalSection(&freetype_cs);
922 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0) {
923 *has_contours = glyph->format == FT_GLYPH_FORMAT_OUTLINE && ((FT_OutlineGlyph)glyph)->outline.n_contours;
924 advance = glyph->advance.x >> 16;
926 else {
927 *has_contours = FALSE;
928 advance = 0;
930 LeaveCriticalSection(&freetype_cs);
932 return advance;
935 #else /* HAVE_FREETYPE */
937 BOOL init_freetype(void)
939 return FALSE;
942 void release_freetype(void)
946 void freetype_notify_cacheremove(IDWriteFontFace4 *fontface)
950 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
952 return E_NOTIMPL;
955 BOOL freetype_is_monospaced(IDWriteFontFace4 *fontface)
957 return FALSE;
960 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
961 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
963 return E_NOTIMPL;
966 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
968 return 0;
971 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
972 UINT16 *glyphs)
974 memset(glyphs, 0, count * sizeof(*glyphs));
977 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
979 return FALSE;
982 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
984 return 0;
987 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
989 SetRectEmpty(&bitmap->bbox);
992 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
994 SetRectEmpty(bbox);
997 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
999 return FALSE;
1002 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
1004 *is_symbol = FALSE;
1005 return -1;
1008 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
1009 BOOL *has_contours)
1011 *has_contours = FALSE;
1012 return 0;
1015 #endif /* HAVE_FREETYPE */