ddraw: Don't crash if writing out a new ddraw4 surface segfaults.
[wine.git] / dlls / dwrite / freetype.c
blob596e8a55cc72faad820b7d38b831472edadc61eb
1 /*
2 * FreeType integration
4 * Copyright 2014-2015 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_New_Memory_Face);
78 MAKE_FUNCPTR(FT_Outline_Copy);
79 MAKE_FUNCPTR(FT_Outline_Decompose);
80 MAKE_FUNCPTR(FT_Outline_Done);
81 MAKE_FUNCPTR(FT_Outline_Get_Bitmap);
82 MAKE_FUNCPTR(FT_Outline_New);
83 MAKE_FUNCPTR(FT_Outline_Transform);
84 MAKE_FUNCPTR(FT_Outline_Translate);
85 MAKE_FUNCPTR(FTC_CMapCache_Lookup);
86 MAKE_FUNCPTR(FTC_CMapCache_New);
87 MAKE_FUNCPTR(FTC_ImageCache_Lookup);
88 MAKE_FUNCPTR(FTC_ImageCache_New);
89 MAKE_FUNCPTR(FTC_Manager_New);
90 MAKE_FUNCPTR(FTC_Manager_Done);
91 MAKE_FUNCPTR(FTC_Manager_LookupFace);
92 MAKE_FUNCPTR(FTC_Manager_LookupSize);
93 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID);
94 #undef MAKE_FUNCPTR
96 struct face_finalizer_data
98 IDWriteFontFileStream *stream;
99 void *context;
102 static void face_finalizer(void *object)
104 FT_Face face = object;
105 struct face_finalizer_data *data = (struct face_finalizer_data *)face->generic.data;
107 IDWriteFontFileStream_ReleaseFileFragment(data->stream, data->context);
108 IDWriteFontFileStream_Release(data->stream);
109 heap_free(data);
112 static FT_Error face_requester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *face)
114 IDWriteFontFace *fontface = (IDWriteFontFace*)face_id;
115 IDWriteFontFileStream *stream;
116 IDWriteFontFile *file;
117 const void *data_ptr;
118 UINT32 index, count;
119 FT_Error fterror;
120 UINT64 data_size;
121 void *context;
122 HRESULT hr;
124 *face = NULL;
126 if (!fontface) {
127 WARN("NULL fontface requested.\n");
128 return FT_Err_Ok;
131 count = 1;
132 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
133 if (FAILED(hr))
134 return FT_Err_Ok;
136 hr = get_filestream_from_file(file, &stream);
137 IDWriteFontFile_Release(file);
138 if (FAILED(hr))
139 return FT_Err_Ok;
141 hr = IDWriteFontFileStream_GetFileSize(stream, &data_size);
142 if (FAILED(hr)) {
143 fterror = FT_Err_Invalid_Stream_Read;
144 goto fail;
147 hr = IDWriteFontFileStream_ReadFileFragment(stream, &data_ptr, 0, data_size, &context);
148 if (FAILED(hr)) {
149 fterror = FT_Err_Invalid_Stream_Read;
150 goto fail;
153 index = IDWriteFontFace_GetIndex(fontface);
154 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
155 if (fterror == FT_Err_Ok) {
156 struct face_finalizer_data *data;
158 data = heap_alloc(sizeof(*data));
159 data->stream = stream;
160 data->context = context;
162 (*face)->generic.data = data;
163 (*face)->generic.finalizer = face_finalizer;
164 return fterror;
166 else
167 IDWriteFontFileStream_ReleaseFileFragment(stream, context);
169 fail:
170 IDWriteFontFileStream_Release(stream);
172 return fterror;
175 BOOL init_freetype(void)
177 FT_Version_t FT_Version;
179 ft_handle = wine_dlopen(SONAME_LIBFREETYPE, RTLD_NOW, NULL, 0);
180 if (!ft_handle) {
181 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
182 return FALSE;
185 #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;}
186 LOAD_FUNCPTR(FT_Done_FreeType)
187 LOAD_FUNCPTR(FT_Done_Glyph)
188 LOAD_FUNCPTR(FT_Get_First_Char)
189 LOAD_FUNCPTR(FT_Get_Kerning)
190 LOAD_FUNCPTR(FT_Get_Sfnt_Table)
191 LOAD_FUNCPTR(FT_Glyph_Copy)
192 LOAD_FUNCPTR(FT_Glyph_Get_CBox)
193 LOAD_FUNCPTR(FT_Glyph_Transform)
194 LOAD_FUNCPTR(FT_Init_FreeType)
195 LOAD_FUNCPTR(FT_Library_Version)
196 LOAD_FUNCPTR(FT_Load_Glyph)
197 LOAD_FUNCPTR(FT_New_Memory_Face)
198 LOAD_FUNCPTR(FT_Outline_Copy)
199 LOAD_FUNCPTR(FT_Outline_Decompose)
200 LOAD_FUNCPTR(FT_Outline_Done)
201 LOAD_FUNCPTR(FT_Outline_Get_Bitmap)
202 LOAD_FUNCPTR(FT_Outline_New)
203 LOAD_FUNCPTR(FT_Outline_Transform)
204 LOAD_FUNCPTR(FT_Outline_Translate)
205 LOAD_FUNCPTR(FTC_CMapCache_Lookup)
206 LOAD_FUNCPTR(FTC_CMapCache_New)
207 LOAD_FUNCPTR(FTC_ImageCache_Lookup)
208 LOAD_FUNCPTR(FTC_ImageCache_New)
209 LOAD_FUNCPTR(FTC_Manager_New)
210 LOAD_FUNCPTR(FTC_Manager_Done)
211 LOAD_FUNCPTR(FTC_Manager_LookupFace)
212 LOAD_FUNCPTR(FTC_Manager_LookupSize)
213 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
214 #undef LOAD_FUNCPTR
216 if (pFT_Init_FreeType(&library) != 0) {
217 ERR("Can't init FreeType library\n");
218 wine_dlclose(ft_handle, NULL, 0);
219 ft_handle = NULL;
220 return FALSE;
222 pFT_Library_Version(library, &FT_Version.major, &FT_Version.minor, &FT_Version.patch);
224 /* init cache manager */
225 if (pFTC_Manager_New(library, 0, 0, 0, &face_requester, NULL, &cache_manager) != 0 ||
226 pFTC_CMapCache_New(cache_manager, &cmap_cache) != 0 ||
227 pFTC_ImageCache_New(cache_manager, &image_cache) != 0) {
229 ERR("Failed to init FreeType cache\n");
230 pFTC_Manager_Done(cache_manager);
231 pFT_Done_FreeType(library);
232 wine_dlclose(ft_handle, NULL, 0);
233 ft_handle = NULL;
234 return FALSE;
237 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
238 return TRUE;
240 sym_not_found:
241 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
242 wine_dlclose(ft_handle, NULL, 0);
243 ft_handle = NULL;
244 return FALSE;
247 void release_freetype(void)
249 pFTC_Manager_Done(cache_manager);
250 pFT_Done_FreeType(library);
253 void freetype_notify_cacheremove(IDWriteFontFace4 *fontface)
255 EnterCriticalSection(&freetype_cs);
256 pFTC_Manager_RemoveFaceID(cache_manager, fontface);
257 LeaveCriticalSection(&freetype_cs);
260 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
262 FTC_ScalerRec scaler;
263 FT_Size size;
265 scaler.face_id = fontface;
266 scaler.width = unitsperEm;
267 scaler.height = unitsperEm;
268 scaler.pixel = 1;
269 scaler.x_res = 0;
270 scaler.y_res = 0;
272 EnterCriticalSection(&freetype_cs);
273 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
274 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
275 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
277 ret->leftSideBearing = metrics->horiBearingX;
278 ret->advanceWidth = metrics->horiAdvance;
279 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
280 ret->topSideBearing = metrics->vertBearingY;
281 ret->advanceHeight = metrics->vertAdvance;
282 ret->bottomSideBearing = metrics->vertAdvance - metrics->vertBearingY - metrics->height;
283 ret->verticalOriginY = metrics->height + metrics->vertBearingY;
286 LeaveCriticalSection(&freetype_cs);
288 return S_OK;
291 BOOL freetype_is_monospaced(IDWriteFontFace4 *fontface)
293 BOOL is_monospaced = FALSE;
294 FT_Face face;
296 EnterCriticalSection(&freetype_cs);
297 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
298 is_monospaced = !!FT_IS_FIXED_WIDTH(face);
299 LeaveCriticalSection(&freetype_cs);
301 return is_monospaced;
304 struct decompose_context {
305 IDWriteGeometrySink *sink;
306 FLOAT xoffset;
307 FLOAT yoffset;
308 BOOL figure_started;
309 BOOL figure_closed;
310 BOOL move_to; /* last call was 'move_to' */
311 FT_Vector origin; /* 'pen' position from last call */
314 static inline void ft_vector_to_d2d_point(const FT_Vector *v, FLOAT xoffset, FLOAT yoffset, D2D1_POINT_2F *p)
316 p->x = (v->x / 64.0f) + xoffset;
317 p->y = (v->y / 64.0f) + yoffset;
320 static int decompose_move_to(const FT_Vector *to, void *user)
322 struct decompose_context *ctxt = (struct decompose_context*)user;
323 D2D1_POINT_2F point;
325 if (ctxt->figure_started) {
326 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
327 ctxt->figure_closed = TRUE;
329 else
330 ctxt->figure_closed = FALSE;
331 ctxt->figure_started = TRUE;
333 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
334 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt->sink, point, D2D1_FIGURE_BEGIN_FILLED);
335 ctxt->move_to = TRUE;
336 ctxt->origin = *to;
337 return 0;
340 static int decompose_line_to(const FT_Vector *to, void *user)
342 struct decompose_context *ctxt = (struct decompose_context*)user;
343 /* special case for empty contours, in a way freetype returns them */
344 if (ctxt->move_to && !memcmp(to, &ctxt->origin, sizeof(*to))) {
345 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
346 ctxt->figure_closed = TRUE;
348 else {
349 D2D1_POINT_2F point;
350 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
351 ID2D1SimplifiedGeometrySink_AddLines(ctxt->sink, &point, 1);
352 ctxt->figure_closed = FALSE;
354 ctxt->move_to = FALSE;
355 ctxt->origin = *to;
356 return 0;
359 static int decompose_conic_to(const FT_Vector *control, const FT_Vector *to, void *user)
361 struct decompose_context *ctxt = (struct decompose_context*)user;
362 D2D1_POINT_2F points[3];
363 FT_Vector cubic[3];
365 /* convert from quadratic to cubic */
368 The parametric eqn for a cubic Bezier is, from PLRM:
369 r(t) = at^3 + bt^2 + ct + r0
370 with the control points:
371 r1 = r0 + c/3
372 r2 = r1 + (c + b)/3
373 r3 = r0 + c + b + a
375 A quadratic Bezier has the form:
376 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
378 So equating powers of t leads to:
379 r1 = 2/3 p1 + 1/3 p0
380 r2 = 2/3 p1 + 1/3 p2
381 and of course r0 = p0, r3 = p2
384 /* r1 = 1/3 p0 + 2/3 p1
385 r2 = 1/3 p2 + 2/3 p1 */
386 cubic[0].x = (2 * control->x + 1) / 3;
387 cubic[0].y = (2 * control->y + 1) / 3;
388 cubic[1] = cubic[0];
389 cubic[0].x += (ctxt->origin.x + 1) / 3;
390 cubic[0].y += (ctxt->origin.y + 1) / 3;
391 cubic[1].x += (to->x + 1) / 3;
392 cubic[1].y += (to->y + 1) / 3;
393 cubic[2] = *to;
395 ft_vector_to_d2d_point(cubic, ctxt->xoffset, ctxt->yoffset, points);
396 ft_vector_to_d2d_point(cubic + 1, ctxt->xoffset, ctxt->yoffset, points + 1);
397 ft_vector_to_d2d_point(cubic + 2, ctxt->xoffset, ctxt->yoffset, points + 2);
398 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
399 ctxt->figure_closed = FALSE;
400 ctxt->move_to = FALSE;
401 ctxt->origin = *to;
402 return 0;
405 static int decompose_cubic_to(const FT_Vector *control1, const FT_Vector *control2,
406 const FT_Vector *to, void *user)
408 struct decompose_context *ctxt = (struct decompose_context*)user;
409 D2D1_POINT_2F points[3];
411 ft_vector_to_d2d_point(control1, ctxt->xoffset, ctxt->yoffset, points);
412 ft_vector_to_d2d_point(control2, ctxt->xoffset, ctxt->yoffset, points + 1);
413 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, points + 2);
414 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
415 ctxt->figure_closed = FALSE;
416 ctxt->move_to = FALSE;
417 ctxt->origin = *to;
418 return 0;
421 static void decompose_outline(FT_Outline *outline, FLOAT xoffset, FLOAT yoffset, IDWriteGeometrySink *sink)
423 static const FT_Outline_Funcs decompose_funcs = {
424 decompose_move_to,
425 decompose_line_to,
426 decompose_conic_to,
427 decompose_cubic_to,
431 struct decompose_context context;
433 context.sink = sink;
434 context.xoffset = xoffset;
435 context.yoffset = yoffset;
436 context.figure_started = FALSE;
437 context.figure_closed = FALSE;
438 context.move_to = FALSE;
439 context.origin.x = 0;
440 context.origin.y = 0;
442 pFT_Outline_Decompose(outline, &decompose_funcs, &context);
444 if (!context.figure_closed && outline->n_points)
445 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
448 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
449 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
451 FTC_ScalerRec scaler;
452 USHORT simulations;
453 HRESULT hr = S_OK;
454 FT_Size size;
456 if (!count)
457 return S_OK;
459 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_WINDING);
461 simulations = IDWriteFontFace4_GetSimulations(fontface);
463 scaler.face_id = fontface;
464 scaler.width = emSize;
465 scaler.height = emSize;
466 scaler.pixel = 1;
467 scaler.x_res = 0;
468 scaler.y_res = 0;
470 EnterCriticalSection(&freetype_cs);
471 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
472 FLOAT advance = 0.0f;
473 UINT32 g;
475 for (g = 0; g < count; g++) {
476 if (pFT_Load_Glyph(size->face, glyphs[g], FT_LOAD_NO_BITMAP) == 0) {
477 FLOAT ft_advance = size->face->glyph->metrics.horiAdvance >> 6;
478 FT_Outline *outline = &size->face->glyph->outline;
479 FLOAT xoffset = 0.0f, yoffset = 0.0f;
480 FT_Matrix m;
482 m.xx = 1 << 16;
483 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
484 m.yx = 0;
485 m.yy = -(1 << 16); /* flip Y axis */
487 pFT_Outline_Transform(outline, &m);
489 /* glyph offsets act as current glyph adjustment */
490 if (offsets) {
491 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
492 yoffset -= offsets[g].ascenderOffset;
495 if (g == 0 && is_rtl)
496 advance = advances ? -advances[g] : -ft_advance;
498 xoffset += advance;
499 decompose_outline(outline, xoffset, yoffset, sink);
501 /* update advance to next glyph */
502 if (advances)
503 advance += is_rtl ? -advances[g] : advances[g];
504 else
505 advance += is_rtl ? -ft_advance : ft_advance;
509 else
510 hr = E_FAIL;
511 LeaveCriticalSection(&freetype_cs);
513 return hr;
516 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
518 UINT16 count = 0;
519 FT_Face face;
521 EnterCriticalSection(&freetype_cs);
522 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
523 count = face->num_glyphs;
524 LeaveCriticalSection(&freetype_cs);
526 return count;
529 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
530 UINT16 *glyphs)
532 UINT32 i;
534 EnterCriticalSection(&freetype_cs);
535 for (i = 0; i < count; i++) {
536 if (charmap == -1)
537 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoints[i]);
538 else {
539 UINT32 codepoint = codepoints[i];
540 /* special handling for symbol fonts */
541 if (codepoint < 0x100) codepoint += 0xf000;
542 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint);
543 if (!glyphs[i])
544 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint - 0xf000);
547 LeaveCriticalSection(&freetype_cs);
550 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
552 BOOL has_kerning_pairs = FALSE;
553 FT_Face face;
555 EnterCriticalSection(&freetype_cs);
556 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
557 has_kerning_pairs = !!FT_HAS_KERNING(face);
558 LeaveCriticalSection(&freetype_cs);
560 return has_kerning_pairs;
563 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
565 INT32 adjustment = 0;
566 FT_Face face;
568 EnterCriticalSection(&freetype_cs);
569 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
570 FT_Vector kern;
571 if (FT_HAS_KERNING(face)) {
572 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
573 adjustment = kern.x;
576 LeaveCriticalSection(&freetype_cs);
578 return adjustment;
581 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
583 ft_matrix->xx = m->m11 * 0x10000;
584 ft_matrix->xy = -m->m21 * 0x10000;
585 ft_matrix->yx = -m->m12 * 0x10000;
586 ft_matrix->yy = m->m22 * 0x10000;
589 /* Should be used only while holding 'freetype_cs' */
590 static BOOL is_face_scalable(IDWriteFontFace4 *fontface)
592 FT_Face face;
593 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
594 return FT_IS_SCALABLE(face);
595 else
596 return FALSE;
599 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
601 FTC_ImageTypeRec imagetype;
602 FT_BBox bbox = { 0 };
603 FT_Glyph glyph;
605 EnterCriticalSection(&freetype_cs);
607 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef,
608 disable transform if that's the case. */
609 if (bitmap->m) {
610 if (!is_face_scalable(bitmap->fontface))
611 bitmap->m = NULL;
614 imagetype.face_id = bitmap->fontface;
615 imagetype.width = 0;
616 imagetype.height = bitmap->emsize;
617 imagetype.flags = bitmap->m ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
619 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
620 if (bitmap->m) {
621 FT_Glyph glyph_copy;
623 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
624 FT_Matrix ft_matrix;
626 ft_matrix_from_dwrite_matrix(bitmap->m, &ft_matrix);
627 pFT_Glyph_Transform(glyph_copy, &ft_matrix, NULL);
628 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
629 pFT_Done_Glyph(glyph_copy);
632 else
633 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
636 LeaveCriticalSection(&freetype_cs);
638 /* flip Y axis */
639 SetRect(&bitmap->bbox, bbox.xMin, -bbox.yMax, bbox.xMax, -bbox.yMin);
642 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
644 FTC_ScalerRec scaler;
645 FT_Size size;
647 scaler.face_id = fontface;
648 scaler.width = unitsperEm;
649 scaler.height = unitsperEm;
650 scaler.pixel = 1;
651 scaler.x_res = 0;
652 scaler.y_res = 0;
654 EnterCriticalSection(&freetype_cs);
655 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
656 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
657 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
659 bbox->left = metrics->horiBearingX;
660 bbox->right = bbox->left + metrics->horiAdvance;
661 bbox->top = -metrics->horiBearingY;
662 bbox->bottom = bbox->top + metrics->height;
665 LeaveCriticalSection(&freetype_cs);
668 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
670 const RECT *bbox = &bitmap->bbox;
671 int width = bbox->right - bbox->left;
672 int height = bbox->bottom - bbox->top;
674 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
675 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
676 const FT_Outline *src = &outline->outline;
677 FT_Bitmap ft_bitmap;
678 FT_Outline copy;
680 ft_bitmap.width = width;
681 ft_bitmap.rows = height;
682 ft_bitmap.pitch = bitmap->pitch;
683 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
684 ft_bitmap.buffer = bitmap->buf;
686 /* Note: FreeType will only set 'black' bits for us. */
687 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
688 pFT_Outline_Copy(src, &copy);
689 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
690 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
691 pFT_Outline_Done(library, &copy);
694 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
695 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
696 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
697 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
698 int h = min(height, ft_bitmap->rows);
700 while (h--) {
701 memcpy(dst, src, w);
702 src += ft_bitmap->pitch;
703 dst += bitmap->pitch;
706 else
707 FIXME("format %x not handled\n", glyph->format);
709 return TRUE;
712 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
714 const RECT *bbox = &bitmap->bbox;
715 int width = bbox->right - bbox->left;
716 int height = bbox->bottom - bbox->top;
717 BOOL ret = FALSE;
719 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
720 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
721 const FT_Outline *src = &outline->outline;
722 FT_Bitmap ft_bitmap;
723 FT_Outline copy;
725 ft_bitmap.width = width;
726 ft_bitmap.rows = height;
727 ft_bitmap.pitch = bitmap->pitch;
728 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
729 ft_bitmap.buffer = bitmap->buf;
731 /* Note: FreeType will only set 'black' bits for us. */
732 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
733 pFT_Outline_Copy(src, &copy);
734 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
735 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
736 pFT_Outline_Done(library, &copy);
739 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
740 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
741 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
742 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
743 int h = min(height, ft_bitmap->rows);
745 while (h--) {
746 memcpy(dst, src, w);
747 src += ft_bitmap->pitch;
748 dst += bitmap->pitch;
751 ret = TRUE;
753 else
754 FIXME("format %x not handled\n", glyph->format);
756 return ret;
759 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
761 FTC_ImageTypeRec imagetype;
762 BOOL ret = FALSE;
763 FT_Glyph glyph;
765 EnterCriticalSection(&freetype_cs);
767 if (bitmap->m) {
768 if (!is_face_scalable(bitmap->fontface))
769 bitmap->m = NULL;
772 imagetype.face_id = bitmap->fontface;
773 imagetype.width = 0;
774 imagetype.height = bitmap->emsize;
775 imagetype.flags = bitmap->m ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
777 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
778 FT_Glyph glyph_copy;
780 if (bitmap->m) {
781 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
782 FT_Matrix ft_matrix;
784 ft_matrix_from_dwrite_matrix(bitmap->m, &ft_matrix);
785 pFT_Glyph_Transform(glyph_copy, &ft_matrix, NULL);
786 glyph = glyph_copy;
789 else
790 glyph_copy = NULL;
792 if (bitmap->type == DWRITE_TEXTURE_CLEARTYPE_3x1)
793 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
794 else
795 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
797 if (glyph_copy)
798 pFT_Done_Glyph(glyph_copy);
801 LeaveCriticalSection(&freetype_cs);
803 return ret;
806 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
808 INT charmap_index = -1;
809 FT_Face face;
811 *is_symbol = FALSE;
813 EnterCriticalSection(&freetype_cs);
814 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
815 TT_OS2 *os2 = pFT_Get_Sfnt_Table(face, ft_sfnt_os2);
816 FT_Int i;
818 if (os2) {
819 FT_UInt dummy;
820 if (os2->version == 0)
821 *is_symbol = pFT_Get_First_Char(face, &dummy) >= 0x100;
822 else
823 *is_symbol = !!(os2->ulCodePageRange1 & FS_SYMBOL);
826 for (i = 0; i < face->num_charmaps; i++)
827 if (face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL) {
828 *is_symbol = TRUE;
829 charmap_index = i;
830 break;
833 LeaveCriticalSection(&freetype_cs);
835 return charmap_index;
838 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
840 FTC_ImageTypeRec imagetype;
841 FT_Glyph glyph;
842 INT32 advance;
844 imagetype.face_id = fontface;
845 imagetype.width = 0;
846 imagetype.height = emSize;
847 imagetype.flags = FT_LOAD_DEFAULT;
848 if (mode == DWRITE_MEASURING_MODE_NATURAL)
849 imagetype.flags |= FT_LOAD_NO_HINTING;
851 EnterCriticalSection(&freetype_cs);
852 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0)
853 advance = glyph->advance.x >> 16;
854 else
855 advance = 0;
856 LeaveCriticalSection(&freetype_cs);
858 return advance;
861 #else /* HAVE_FREETYPE */
863 BOOL init_freetype(void)
865 return FALSE;
868 void release_freetype(void)
872 void freetype_notify_cacheremove(IDWriteFontFace4 *fontface)
876 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
878 return E_NOTIMPL;
881 BOOL freetype_is_monospaced(IDWriteFontFace4 *fontface)
883 return FALSE;
886 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
887 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
889 return E_NOTIMPL;
892 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
894 return 0;
897 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
898 UINT16 *glyphs)
900 memset(glyphs, 0, count * sizeof(*glyphs));
903 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
905 return FALSE;
908 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
910 return 0;
913 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
915 memset(&bitmap->bbox, 0, sizeof(bitmap->bbox));
918 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
920 memset(bbox, 0, sizeof(*bbox));
923 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
925 return FALSE;
928 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
930 *is_symbol = FALSE;
931 return -1;
934 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
936 return 0;
939 #endif /* HAVE_FREETYPE */