po: Update German translation.
[wine.git] / dlls / dwrite / freetype.c
blob934ca80a51d997beec0ede6a635374310b5cebc3
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 FT_Matrix m;
641 ret->xx = 1 << 16;
642 ret->xy = 0;
643 ret->yx = 0;
644 ret->yy = 1 << 16;
646 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
647 Disable transform if that's the case. */
648 if (!is_face_scalable(bitmap->fontface) || (!bitmap->m && bitmap->simulations == 0))
649 return FALSE;
651 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
652 m.xx = 1 << 16;
653 m.xy = (1 << 16) / 3;
654 m.yx = 0;
655 m.yy = 1 << 16;
656 pFT_Matrix_Multiply(&m, ret);
659 if (bitmap->m) {
660 ft_matrix_from_dwrite_matrix(bitmap->m, &m);
661 pFT_Matrix_Multiply(&m, ret);
664 return TRUE;
667 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
669 FTC_ImageTypeRec imagetype;
670 FT_BBox bbox = { 0 };
671 BOOL needs_transform;
672 FT_Glyph glyph;
673 FT_Matrix m;
675 EnterCriticalSection(&freetype_cs);
677 needs_transform = get_glyph_transform(bitmap, &m);
679 imagetype.face_id = bitmap->fontface;
680 imagetype.width = 0;
681 imagetype.height = bitmap->emsize;
682 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
684 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
685 if (needs_transform) {
686 FT_Glyph glyph_copy;
688 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
689 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_BOLD)
690 embolden_glyph(glyph_copy, bitmap->emsize);
692 /* Includes oblique and user transform. */
693 pFT_Glyph_Transform(glyph_copy, &m, NULL);
694 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
695 pFT_Done_Glyph(glyph_copy);
698 else
699 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
702 LeaveCriticalSection(&freetype_cs);
704 /* flip Y axis */
705 SetRect(&bitmap->bbox, bbox.xMin, -bbox.yMax, bbox.xMax, -bbox.yMin);
708 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
710 FTC_ScalerRec scaler;
711 FT_Size size;
713 scaler.face_id = fontface;
714 scaler.width = unitsperEm;
715 scaler.height = unitsperEm;
716 scaler.pixel = 1;
717 scaler.x_res = 0;
718 scaler.y_res = 0;
720 EnterCriticalSection(&freetype_cs);
721 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
722 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
723 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
725 bbox->left = metrics->horiBearingX;
726 bbox->right = bbox->left + metrics->horiAdvance;
727 bbox->top = -metrics->horiBearingY;
728 bbox->bottom = bbox->top + metrics->height;
731 LeaveCriticalSection(&freetype_cs);
734 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
736 const RECT *bbox = &bitmap->bbox;
737 int width = bbox->right - bbox->left;
738 int height = bbox->bottom - bbox->top;
740 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
741 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
742 const FT_Outline *src = &outline->outline;
743 FT_Bitmap ft_bitmap;
744 FT_Outline copy;
746 ft_bitmap.width = width;
747 ft_bitmap.rows = height;
748 ft_bitmap.pitch = bitmap->pitch;
749 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
750 ft_bitmap.buffer = bitmap->buf;
752 /* Note: FreeType will only set 'black' bits for us. */
753 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
754 pFT_Outline_Copy(src, &copy);
755 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
756 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
757 pFT_Outline_Done(library, &copy);
760 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
761 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
762 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
763 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
764 int h = min(height, ft_bitmap->rows);
766 while (h--) {
767 memcpy(dst, src, w);
768 src += ft_bitmap->pitch;
769 dst += bitmap->pitch;
772 else
773 FIXME("format %x not handled\n", glyph->format);
775 return TRUE;
778 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
780 const RECT *bbox = &bitmap->bbox;
781 int width = bbox->right - bbox->left;
782 int height = bbox->bottom - bbox->top;
783 BOOL ret = FALSE;
785 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
786 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
787 const FT_Outline *src = &outline->outline;
788 FT_Bitmap ft_bitmap;
789 FT_Outline copy;
791 ft_bitmap.width = width;
792 ft_bitmap.rows = height;
793 ft_bitmap.pitch = bitmap->pitch;
794 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
795 ft_bitmap.buffer = bitmap->buf;
797 /* Note: FreeType will only set 'black' bits for us. */
798 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
799 pFT_Outline_Copy(src, &copy);
800 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
801 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
802 pFT_Outline_Done(library, &copy);
805 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
806 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
807 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
808 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
809 int h = min(height, ft_bitmap->rows);
811 while (h--) {
812 memcpy(dst, src, w);
813 src += ft_bitmap->pitch;
814 dst += bitmap->pitch;
817 ret = TRUE;
819 else
820 FIXME("format %x not handled\n", glyph->format);
822 return ret;
825 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
827 FTC_ImageTypeRec imagetype;
828 BOOL needs_transform;
829 BOOL ret = FALSE;
830 FT_Glyph glyph;
831 FT_Matrix m;
833 EnterCriticalSection(&freetype_cs);
835 needs_transform = get_glyph_transform(bitmap, &m);
837 imagetype.face_id = bitmap->fontface;
838 imagetype.width = 0;
839 imagetype.height = bitmap->emsize;
840 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
842 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
843 FT_Glyph glyph_copy;
845 if (needs_transform) {
846 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
847 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_BOLD)
848 embolden_glyph(glyph_copy, bitmap->emsize);
850 /* Includes oblique and user transform. */
851 pFT_Glyph_Transform(glyph_copy, &m, NULL);
852 glyph = glyph_copy;
855 else
856 glyph_copy = NULL;
858 if (bitmap->type == DWRITE_TEXTURE_CLEARTYPE_3x1)
859 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
860 else
861 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
863 if (glyph_copy)
864 pFT_Done_Glyph(glyph_copy);
867 LeaveCriticalSection(&freetype_cs);
869 return ret;
872 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
874 INT charmap_index = -1;
875 FT_Face face;
877 *is_symbol = FALSE;
879 EnterCriticalSection(&freetype_cs);
880 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
881 TT_OS2 *os2 = pFT_Get_Sfnt_Table(face, ft_sfnt_os2);
882 FT_Int i;
884 if (os2) {
885 FT_UInt dummy;
886 if (os2->version == 0)
887 *is_symbol = pFT_Get_First_Char(face, &dummy) >= 0x100;
888 else
889 *is_symbol = !!(os2->ulCodePageRange1 & FS_SYMBOL);
892 for (i = 0; i < face->num_charmaps; i++)
893 if (face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL) {
894 *is_symbol = TRUE;
895 charmap_index = i;
896 break;
899 LeaveCriticalSection(&freetype_cs);
901 return charmap_index;
904 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
905 BOOL *has_contours)
907 FTC_ImageTypeRec imagetype;
908 FT_Glyph glyph;
909 INT32 advance;
911 imagetype.face_id = fontface;
912 imagetype.width = 0;
913 imagetype.height = emSize;
914 imagetype.flags = FT_LOAD_DEFAULT;
915 if (mode == DWRITE_MEASURING_MODE_NATURAL)
916 imagetype.flags |= FT_LOAD_NO_HINTING;
918 EnterCriticalSection(&freetype_cs);
919 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0) {
920 *has_contours = glyph->format == FT_GLYPH_FORMAT_OUTLINE && ((FT_OutlineGlyph)glyph)->outline.n_contours;
921 advance = glyph->advance.x >> 16;
923 else {
924 *has_contours = FALSE;
925 advance = 0;
927 LeaveCriticalSection(&freetype_cs);
929 return advance;
932 #else /* HAVE_FREETYPE */
934 BOOL init_freetype(void)
936 return FALSE;
939 void release_freetype(void)
943 void freetype_notify_cacheremove(IDWriteFontFace4 *fontface)
947 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
949 return E_NOTIMPL;
952 BOOL freetype_is_monospaced(IDWriteFontFace4 *fontface)
954 return FALSE;
957 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
958 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
960 return E_NOTIMPL;
963 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
965 return 0;
968 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
969 UINT16 *glyphs)
971 memset(glyphs, 0, count * sizeof(*glyphs));
974 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
976 return FALSE;
979 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
981 return 0;
984 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
986 SetRectEmpty(&bitmap->bbox);
989 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
991 SetRectEmpty(bbox);
994 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
996 return FALSE;
999 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
1001 *is_symbol = FALSE;
1002 return -1;
1005 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
1006 BOOL *has_contours)
1008 *has_contours = FALSE;
1009 return 0;
1012 #endif /* HAVE_FREETYPE */