include: Use the hard-float calling convention for Windows APIs on ARM
[wine.git] / dlls / dwrite / freetype.c
blobffdd8a8f1e8de7e618458e15eedcea9519017240
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 move_to; /* last call was 'move_to' */
324 FT_Vector origin; /* 'pen' position from last call */
327 static inline void ft_vector_to_d2d_point(const FT_Vector *v, FLOAT xoffset, FLOAT yoffset, D2D1_POINT_2F *p)
329 p->x = (v->x / 64.0f) + xoffset;
330 p->y = (v->y / 64.0f) + yoffset;
333 static void decompose_beginfigure(struct decompose_context *ctxt)
335 D2D1_POINT_2F point;
337 if (!ctxt->move_to)
338 return;
340 ft_vector_to_d2d_point(&ctxt->origin, ctxt->xoffset, ctxt->yoffset, &point);
341 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt->sink, point, D2D1_FIGURE_BEGIN_FILLED);
343 ctxt->figure_started = TRUE;
344 ctxt->move_to = FALSE;
347 static int decompose_move_to(const FT_Vector *to, void *user)
349 struct decompose_context *ctxt = (struct decompose_context*)user;
351 if (ctxt->figure_started) {
352 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
353 ctxt->figure_started = FALSE;
356 ctxt->move_to = TRUE;
357 ctxt->origin = *to;
358 return 0;
361 static int decompose_line_to(const FT_Vector *to, void *user)
363 struct decompose_context *ctxt = (struct decompose_context*)user;
364 D2D1_POINT_2F point;
366 /* Special case for empty contours, in a way freetype returns them. */
367 if (ctxt->move_to && !memcmp(to, &ctxt->origin, sizeof(*to)))
368 return 0;
370 decompose_beginfigure(ctxt);
372 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
373 ID2D1SimplifiedGeometrySink_AddLines(ctxt->sink, &point, 1);
375 ctxt->origin = *to;
376 return 0;
379 static int decompose_conic_to(const FT_Vector *control, const FT_Vector *to, void *user)
381 struct decompose_context *ctxt = (struct decompose_context*)user;
382 D2D1_POINT_2F points[3];
383 FT_Vector cubic[3];
385 decompose_beginfigure(ctxt);
387 /* convert from quadratic to cubic */
390 The parametric eqn for a cubic Bezier is, from PLRM:
391 r(t) = at^3 + bt^2 + ct + r0
392 with the control points:
393 r1 = r0 + c/3
394 r2 = r1 + (c + b)/3
395 r3 = r0 + c + b + a
397 A quadratic Bezier has the form:
398 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
400 So equating powers of t leads to:
401 r1 = 2/3 p1 + 1/3 p0
402 r2 = 2/3 p1 + 1/3 p2
403 and of course r0 = p0, r3 = p2
406 /* r1 = 1/3 p0 + 2/3 p1
407 r2 = 1/3 p2 + 2/3 p1 */
408 cubic[0].x = (2 * control->x + 1) / 3;
409 cubic[0].y = (2 * control->y + 1) / 3;
410 cubic[1] = cubic[0];
411 cubic[0].x += (ctxt->origin.x + 1) / 3;
412 cubic[0].y += (ctxt->origin.y + 1) / 3;
413 cubic[1].x += (to->x + 1) / 3;
414 cubic[1].y += (to->y + 1) / 3;
415 cubic[2] = *to;
417 ft_vector_to_d2d_point(cubic, ctxt->xoffset, ctxt->yoffset, points);
418 ft_vector_to_d2d_point(cubic + 1, ctxt->xoffset, ctxt->yoffset, points + 1);
419 ft_vector_to_d2d_point(cubic + 2, ctxt->xoffset, ctxt->yoffset, points + 2);
420 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
421 ctxt->origin = *to;
422 return 0;
425 static int decompose_cubic_to(const FT_Vector *control1, const FT_Vector *control2,
426 const FT_Vector *to, void *user)
428 struct decompose_context *ctxt = (struct decompose_context*)user;
429 D2D1_POINT_2F points[3];
431 decompose_beginfigure(ctxt);
433 ft_vector_to_d2d_point(control1, ctxt->xoffset, ctxt->yoffset, points);
434 ft_vector_to_d2d_point(control2, ctxt->xoffset, ctxt->yoffset, points + 1);
435 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, points + 2);
436 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
437 ctxt->origin = *to;
438 return 0;
441 static void decompose_outline(FT_Outline *outline, FLOAT xoffset, FLOAT yoffset, IDWriteGeometrySink *sink)
443 static const FT_Outline_Funcs decompose_funcs = {
444 decompose_move_to,
445 decompose_line_to,
446 decompose_conic_to,
447 decompose_cubic_to,
451 struct decompose_context context;
453 context.sink = sink;
454 context.xoffset = xoffset;
455 context.yoffset = yoffset;
456 context.figure_started = FALSE;
457 context.move_to = FALSE;
458 context.origin.x = 0;
459 context.origin.y = 0;
461 pFT_Outline_Decompose(outline, &decompose_funcs, &context);
463 if (context.figure_started)
464 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
467 static void embolden_glyph_outline(FT_Outline *outline, FLOAT emsize)
469 FT_Pos strength;
471 strength = MulDiv(emsize, 1 << 6, 24);
472 if (pFT_Outline_EmboldenXY)
473 pFT_Outline_EmboldenXY(outline, strength, 0);
474 else
475 pFT_Outline_Embolden(outline, strength);
478 static void embolden_glyph(FT_Glyph glyph, FLOAT emsize)
480 FT_OutlineGlyph outline_glyph = (FT_OutlineGlyph)glyph;
482 if (glyph->format != FT_GLYPH_FORMAT_OUTLINE)
483 return;
485 embolden_glyph_outline(&outline_glyph->outline, emsize);
488 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs,
489 FLOAT const *advances, DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
491 FTC_ScalerRec scaler;
492 USHORT simulations;
493 HRESULT hr = S_OK;
494 FT_Size size;
496 if (!count)
497 return S_OK;
499 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_WINDING);
501 simulations = IDWriteFontFace4_GetSimulations(fontface);
503 scaler.face_id = fontface;
504 scaler.width = emSize;
505 scaler.height = emSize;
506 scaler.pixel = 1;
507 scaler.x_res = 0;
508 scaler.y_res = 0;
510 EnterCriticalSection(&freetype_cs);
511 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
512 FLOAT advance = 0.0f;
513 UINT32 g;
515 for (g = 0; g < count; g++) {
516 if (pFT_Load_Glyph(size->face, glyphs[g], FT_LOAD_NO_BITMAP) == 0) {
517 FLOAT ft_advance = size->face->glyph->metrics.horiAdvance >> 6;
518 FT_Outline *outline = &size->face->glyph->outline;
519 FLOAT xoffset = 0.0f, yoffset = 0.0f;
520 FT_Matrix m;
522 if (simulations & DWRITE_FONT_SIMULATIONS_BOLD)
523 embolden_glyph_outline(outline, emSize);
525 m.xx = 1 << 16;
526 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
527 m.yx = 0;
528 m.yy = -(1 << 16); /* flip Y axis */
530 pFT_Outline_Transform(outline, &m);
532 /* glyph offsets act as current glyph adjustment */
533 if (offsets) {
534 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
535 yoffset -= offsets[g].ascenderOffset;
538 if (g == 0 && is_rtl)
539 advance = advances ? -advances[g] : -ft_advance;
541 xoffset += advance;
542 decompose_outline(outline, xoffset, yoffset, sink);
544 /* update advance to next glyph */
545 if (advances)
546 advance += is_rtl ? -advances[g] : advances[g];
547 else
548 advance += is_rtl ? -ft_advance : ft_advance;
552 else
553 hr = E_FAIL;
554 LeaveCriticalSection(&freetype_cs);
556 return hr;
559 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
561 UINT16 count = 0;
562 FT_Face face;
564 EnterCriticalSection(&freetype_cs);
565 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
566 count = face->num_glyphs;
567 LeaveCriticalSection(&freetype_cs);
569 return count;
572 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
573 UINT16 *glyphs)
575 UINT32 i;
577 EnterCriticalSection(&freetype_cs);
578 for (i = 0; i < count; i++) {
579 if (charmap == -1)
580 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoints[i]);
581 else {
582 UINT32 codepoint = codepoints[i];
583 /* special handling for symbol fonts */
584 if (codepoint < 0x100) codepoint += 0xf000;
585 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint);
586 if (!glyphs[i])
587 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint - 0xf000);
590 LeaveCriticalSection(&freetype_cs);
593 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
595 BOOL has_kerning_pairs = FALSE;
596 FT_Face face;
598 EnterCriticalSection(&freetype_cs);
599 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
600 has_kerning_pairs = !!FT_HAS_KERNING(face);
601 LeaveCriticalSection(&freetype_cs);
603 return has_kerning_pairs;
606 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
608 INT32 adjustment = 0;
609 FT_Face face;
611 EnterCriticalSection(&freetype_cs);
612 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
613 FT_Vector kern;
614 if (FT_HAS_KERNING(face)) {
615 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
616 adjustment = kern.x;
619 LeaveCriticalSection(&freetype_cs);
621 return adjustment;
624 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
626 ft_matrix->xx = m->m11 * 0x10000;
627 ft_matrix->xy = -m->m21 * 0x10000;
628 ft_matrix->yx = -m->m12 * 0x10000;
629 ft_matrix->yy = m->m22 * 0x10000;
632 /* Should be used only while holding 'freetype_cs' */
633 static BOOL is_face_scalable(IDWriteFontFace4 *fontface)
635 FT_Face face;
636 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
637 return FT_IS_SCALABLE(face);
638 else
639 return FALSE;
642 static BOOL get_glyph_transform(struct dwrite_glyphbitmap *bitmap, FT_Matrix *ret)
644 FT_Matrix m;
646 ret->xx = 1 << 16;
647 ret->xy = 0;
648 ret->yx = 0;
649 ret->yy = 1 << 16;
651 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
652 Disable transform if that's the case. */
653 if (!is_face_scalable(bitmap->fontface) || (!bitmap->m && bitmap->simulations == 0))
654 return FALSE;
656 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
657 m.xx = 1 << 16;
658 m.xy = (1 << 16) / 3;
659 m.yx = 0;
660 m.yy = 1 << 16;
661 pFT_Matrix_Multiply(&m, ret);
664 if (bitmap->m) {
665 ft_matrix_from_dwrite_matrix(bitmap->m, &m);
666 pFT_Matrix_Multiply(&m, ret);
669 return TRUE;
672 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
674 FTC_ImageTypeRec imagetype;
675 FT_BBox bbox = { 0 };
676 BOOL needs_transform;
677 FT_Glyph glyph;
678 FT_Matrix m;
680 EnterCriticalSection(&freetype_cs);
682 needs_transform = get_glyph_transform(bitmap, &m);
684 imagetype.face_id = bitmap->fontface;
685 imagetype.width = 0;
686 imagetype.height = bitmap->emsize;
687 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
689 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
690 if (needs_transform) {
691 FT_Glyph glyph_copy;
693 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
694 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_BOLD)
695 embolden_glyph(glyph_copy, bitmap->emsize);
697 /* Includes oblique and user transform. */
698 pFT_Glyph_Transform(glyph_copy, &m, NULL);
699 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
700 pFT_Done_Glyph(glyph_copy);
703 else
704 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
707 LeaveCriticalSection(&freetype_cs);
709 /* flip Y axis */
710 SetRect(&bitmap->bbox, bbox.xMin, -bbox.yMax, bbox.xMax, -bbox.yMin);
713 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
715 FTC_ScalerRec scaler;
716 FT_Size size;
718 scaler.face_id = fontface;
719 scaler.width = unitsperEm;
720 scaler.height = unitsperEm;
721 scaler.pixel = 1;
722 scaler.x_res = 0;
723 scaler.y_res = 0;
725 EnterCriticalSection(&freetype_cs);
726 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
727 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
728 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
730 bbox->left = metrics->horiBearingX;
731 bbox->right = bbox->left + metrics->horiAdvance;
732 bbox->top = -metrics->horiBearingY;
733 bbox->bottom = bbox->top + metrics->height;
736 LeaveCriticalSection(&freetype_cs);
739 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
741 const RECT *bbox = &bitmap->bbox;
742 int width = bbox->right - bbox->left;
743 int height = bbox->bottom - bbox->top;
745 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
746 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
747 const FT_Outline *src = &outline->outline;
748 FT_Bitmap ft_bitmap;
749 FT_Outline copy;
751 ft_bitmap.width = width;
752 ft_bitmap.rows = height;
753 ft_bitmap.pitch = bitmap->pitch;
754 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
755 ft_bitmap.buffer = bitmap->buf;
757 /* Note: FreeType will only set 'black' bits for us. */
758 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
759 pFT_Outline_Copy(src, &copy);
760 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
761 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
762 pFT_Outline_Done(library, &copy);
765 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
766 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
767 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
768 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
769 int h = min(height, ft_bitmap->rows);
771 while (h--) {
772 memcpy(dst, src, w);
773 src += ft_bitmap->pitch;
774 dst += bitmap->pitch;
777 else
778 FIXME("format %x not handled\n", glyph->format);
780 return TRUE;
783 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
785 const RECT *bbox = &bitmap->bbox;
786 int width = bbox->right - bbox->left;
787 int height = bbox->bottom - bbox->top;
788 BOOL ret = FALSE;
790 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
791 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
792 const FT_Outline *src = &outline->outline;
793 FT_Bitmap ft_bitmap;
794 FT_Outline copy;
796 ft_bitmap.width = width;
797 ft_bitmap.rows = height;
798 ft_bitmap.pitch = bitmap->pitch;
799 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
800 ft_bitmap.buffer = bitmap->buf;
802 /* Note: FreeType will only set 'black' bits for us. */
803 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
804 pFT_Outline_Copy(src, &copy);
805 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
806 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
807 pFT_Outline_Done(library, &copy);
810 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
811 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
812 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
813 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
814 int h = min(height, ft_bitmap->rows);
816 while (h--) {
817 memcpy(dst, src, w);
818 src += ft_bitmap->pitch;
819 dst += bitmap->pitch;
822 ret = TRUE;
824 else
825 FIXME("format %x not handled\n", glyph->format);
827 return ret;
830 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
832 FTC_ImageTypeRec imagetype;
833 BOOL needs_transform;
834 BOOL ret = FALSE;
835 FT_Glyph glyph;
836 FT_Matrix m;
838 EnterCriticalSection(&freetype_cs);
840 needs_transform = get_glyph_transform(bitmap, &m);
842 imagetype.face_id = bitmap->fontface;
843 imagetype.width = 0;
844 imagetype.height = bitmap->emsize;
845 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
847 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
848 FT_Glyph glyph_copy;
850 if (needs_transform) {
851 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
852 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_BOLD)
853 embolden_glyph(glyph_copy, bitmap->emsize);
855 /* Includes oblique and user transform. */
856 pFT_Glyph_Transform(glyph_copy, &m, NULL);
857 glyph = glyph_copy;
860 else
861 glyph_copy = NULL;
863 if (bitmap->aliased)
864 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
865 else
866 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
868 if (glyph_copy)
869 pFT_Done_Glyph(glyph_copy);
872 LeaveCriticalSection(&freetype_cs);
874 return ret;
877 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
879 INT charmap_index = -1;
880 FT_Face face;
882 *is_symbol = FALSE;
884 EnterCriticalSection(&freetype_cs);
885 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
886 TT_OS2 *os2 = pFT_Get_Sfnt_Table(face, ft_sfnt_os2);
887 FT_Int i;
889 if (os2) {
890 FT_UInt dummy;
891 if (os2->version == 0)
892 *is_symbol = pFT_Get_First_Char(face, &dummy) >= 0x100;
893 else
894 *is_symbol = !!(os2->ulCodePageRange1 & FS_SYMBOL);
897 for (i = 0; i < face->num_charmaps; i++)
898 if (face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL) {
899 *is_symbol = TRUE;
900 charmap_index = i;
901 break;
904 LeaveCriticalSection(&freetype_cs);
906 return charmap_index;
909 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
910 BOOL *has_contours)
912 FTC_ImageTypeRec imagetype;
913 FT_Glyph glyph;
914 INT32 advance;
916 imagetype.face_id = fontface;
917 imagetype.width = 0;
918 imagetype.height = emSize;
919 imagetype.flags = FT_LOAD_DEFAULT;
920 if (mode == DWRITE_MEASURING_MODE_NATURAL)
921 imagetype.flags |= FT_LOAD_NO_HINTING;
923 EnterCriticalSection(&freetype_cs);
924 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0) {
925 *has_contours = glyph->format == FT_GLYPH_FORMAT_OUTLINE && ((FT_OutlineGlyph)glyph)->outline.n_contours;
926 advance = glyph->advance.x >> 16;
928 else {
929 *has_contours = FALSE;
930 advance = 0;
932 LeaveCriticalSection(&freetype_cs);
934 return advance;
937 #else /* HAVE_FREETYPE */
939 BOOL init_freetype(void)
941 return FALSE;
944 void release_freetype(void)
948 void freetype_notify_cacheremove(IDWriteFontFace4 *fontface)
952 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
954 return E_NOTIMPL;
957 BOOL freetype_is_monospaced(IDWriteFontFace4 *fontface)
959 return FALSE;
962 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
963 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
965 return E_NOTIMPL;
968 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
970 return 0;
973 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
974 UINT16 *glyphs)
976 memset(glyphs, 0, count * sizeof(*glyphs));
979 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
981 return FALSE;
984 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
986 return 0;
989 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
991 SetRectEmpty(&bitmap->bbox);
994 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
996 SetRectEmpty(bbox);
999 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
1001 return FALSE;
1004 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
1006 *is_symbol = FALSE;
1007 return -1;
1010 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
1011 BOOL *has_contours)
1013 *has_contours = FALSE;
1014 return 0;
1017 #endif /* HAVE_FREETYPE */