TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / dwrite / freetype.c
blob543e74c8eb12cb29970ea4992a1f0b9ceecb18fe
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 "dwrite_2.h"
36 #include "wine/library.h"
37 #include "wine/debug.h"
39 #include "dwrite_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
43 #ifdef HAVE_FREETYPE
45 static CRITICAL_SECTION freetype_cs;
46 static CRITICAL_SECTION_DEBUG critsect_debug =
48 0, 0, &freetype_cs,
49 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
50 0, 0, { (DWORD_PTR)(__FILE__ ": freetype_cs") }
52 static CRITICAL_SECTION freetype_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
54 static void *ft_handle = NULL;
55 static FT_Library library = 0;
56 static FTC_Manager cache_manager = 0;
57 static FTC_CMapCache cmap_cache = 0;
58 static FTC_ImageCache image_cache = 0;
59 typedef struct
61 FT_Int major;
62 FT_Int minor;
63 FT_Int patch;
64 } FT_Version_t;
66 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
67 MAKE_FUNCPTR(FT_Done_FreeType);
68 MAKE_FUNCPTR(FT_Done_Glyph);
69 MAKE_FUNCPTR(FT_Get_First_Char);
70 MAKE_FUNCPTR(FT_Get_Kerning);
71 MAKE_FUNCPTR(FT_Get_Sfnt_Table);
72 MAKE_FUNCPTR(FT_Glyph_Copy);
73 MAKE_FUNCPTR(FT_Glyph_Get_CBox);
74 MAKE_FUNCPTR(FT_Glyph_Transform);
75 MAKE_FUNCPTR(FT_Init_FreeType);
76 MAKE_FUNCPTR(FT_Library_Version);
77 MAKE_FUNCPTR(FT_Load_Glyph);
78 MAKE_FUNCPTR(FT_New_Memory_Face);
79 MAKE_FUNCPTR(FT_Outline_Copy);
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 static FT_Error face_requester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *face)
98 IDWriteFontFace *fontface = (IDWriteFontFace*)face_id;
99 IDWriteFontFileStream *stream;
100 IDWriteFontFile *file;
101 const void *data_ptr;
102 UINT32 index, count;
103 FT_Error fterror;
104 UINT64 data_size;
105 void *context;
106 HRESULT hr;
108 *face = NULL;
110 count = 1;
111 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
112 if (FAILED(hr))
113 return FT_Err_Ok;
115 hr = get_filestream_from_file(file, &stream);
116 IDWriteFontFile_Release(file);
117 if (FAILED(hr))
118 return FT_Err_Ok;
120 hr = IDWriteFontFileStream_GetFileSize(stream, &data_size);
121 if (FAILED(hr)) {
122 fterror = FT_Err_Invalid_Stream_Read;
123 goto fail;
126 hr = IDWriteFontFileStream_ReadFileFragment(stream, &data_ptr, 0, data_size, &context);
127 if (FAILED(hr)) {
128 fterror = FT_Err_Invalid_Stream_Read;
129 goto fail;
132 index = IDWriteFontFace_GetIndex(fontface);
133 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
134 IDWriteFontFileStream_ReleaseFileFragment(stream, context);
136 fail:
137 IDWriteFontFileStream_Release(stream);
139 return fterror;
142 BOOL init_freetype(void)
144 FT_Version_t FT_Version;
146 ft_handle = wine_dlopen(SONAME_LIBFREETYPE, RTLD_NOW, NULL, 0);
147 if (!ft_handle) {
148 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
149 return FALSE;
152 #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;}
153 LOAD_FUNCPTR(FT_Done_FreeType)
154 LOAD_FUNCPTR(FT_Done_Glyph)
155 LOAD_FUNCPTR(FT_Get_First_Char)
156 LOAD_FUNCPTR(FT_Get_Kerning)
157 LOAD_FUNCPTR(FT_Get_Sfnt_Table)
158 LOAD_FUNCPTR(FT_Glyph_Copy)
159 LOAD_FUNCPTR(FT_Glyph_Get_CBox)
160 LOAD_FUNCPTR(FT_Glyph_Transform)
161 LOAD_FUNCPTR(FT_Init_FreeType)
162 LOAD_FUNCPTR(FT_Library_Version)
163 LOAD_FUNCPTR(FT_Load_Glyph)
164 LOAD_FUNCPTR(FT_New_Memory_Face)
165 LOAD_FUNCPTR(FT_Outline_Copy)
166 LOAD_FUNCPTR(FT_Outline_Done)
167 LOAD_FUNCPTR(FT_Outline_Get_Bitmap)
168 LOAD_FUNCPTR(FT_Outline_New)
169 LOAD_FUNCPTR(FT_Outline_Transform)
170 LOAD_FUNCPTR(FT_Outline_Translate)
171 LOAD_FUNCPTR(FTC_CMapCache_Lookup)
172 LOAD_FUNCPTR(FTC_CMapCache_New)
173 LOAD_FUNCPTR(FTC_ImageCache_Lookup)
174 LOAD_FUNCPTR(FTC_ImageCache_New)
175 LOAD_FUNCPTR(FTC_Manager_New)
176 LOAD_FUNCPTR(FTC_Manager_Done)
177 LOAD_FUNCPTR(FTC_Manager_LookupFace)
178 LOAD_FUNCPTR(FTC_Manager_LookupSize)
179 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
180 #undef LOAD_FUNCPTR
182 if (pFT_Init_FreeType(&library) != 0) {
183 ERR("Can't init FreeType library\n");
184 wine_dlclose(ft_handle, NULL, 0);
185 ft_handle = NULL;
186 return FALSE;
188 pFT_Library_Version(library, &FT_Version.major, &FT_Version.minor, &FT_Version.patch);
190 /* init cache manager */
191 if (pFTC_Manager_New(library, 0, 0, 0, &face_requester, NULL, &cache_manager) != 0 ||
192 pFTC_CMapCache_New(cache_manager, &cmap_cache) != 0 ||
193 pFTC_ImageCache_New(cache_manager, &image_cache) != 0) {
195 ERR("Failed to init FreeType cache\n");
196 pFTC_Manager_Done(cache_manager);
197 pFT_Done_FreeType(library);
198 wine_dlclose(ft_handle, NULL, 0);
199 ft_handle = NULL;
200 return FALSE;
203 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
204 return TRUE;
206 sym_not_found:
207 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
208 wine_dlclose(ft_handle, NULL, 0);
209 ft_handle = NULL;
210 return FALSE;
213 void release_freetype(void)
215 pFTC_Manager_Done(cache_manager);
216 pFT_Done_FreeType(library);
219 void freetype_notify_cacheremove(IDWriteFontFace2 *fontface)
221 EnterCriticalSection(&freetype_cs);
222 pFTC_Manager_RemoveFaceID(cache_manager, fontface);
223 LeaveCriticalSection(&freetype_cs);
226 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace2 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
228 FTC_ScalerRec scaler;
229 FT_Size size;
231 scaler.face_id = fontface;
232 scaler.width = unitsperEm;
233 scaler.height = unitsperEm;
234 scaler.pixel = 1;
235 scaler.x_res = 0;
236 scaler.y_res = 0;
238 EnterCriticalSection(&freetype_cs);
239 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
240 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
241 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
243 ret->leftSideBearing = metrics->horiBearingX;
244 ret->advanceWidth = metrics->horiAdvance;
245 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
246 ret->topSideBearing = metrics->vertBearingY;
247 ret->advanceHeight = metrics->vertAdvance;
248 ret->bottomSideBearing = metrics->vertAdvance - metrics->vertBearingY - metrics->height;
249 ret->verticalOriginY = metrics->height + metrics->vertBearingY;
252 LeaveCriticalSection(&freetype_cs);
254 return S_OK;
257 BOOL freetype_is_monospaced(IDWriteFontFace2 *fontface)
259 BOOL is_monospaced = FALSE;
260 FT_Face face;
262 EnterCriticalSection(&freetype_cs);
263 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
264 is_monospaced = FT_IS_FIXED_WIDTH(face);
265 LeaveCriticalSection(&freetype_cs);
267 return is_monospaced;
270 static inline void ft_vector_to_d2d_point(const FT_Vector *v, D2D1_POINT_2F *p)
272 p->x = v->x / 64.0;
273 p->y = v->y / 64.0;
276 /* Convert the quadratic Beziers to cubic Beziers. */
277 static void get_cubic_glyph_outline(const FT_Outline *outline, short point, short first_pt,
278 short contour, FT_Vector *cubic_control)
281 The parametric eqn for a cubic Bezier is, from PLRM:
282 r(t) = at^3 + bt^2 + ct + r0
283 with the control points:
284 r1 = r0 + c/3
285 r2 = r1 + (c + b)/3
286 r3 = r0 + c + b + a
288 A quadratic Bezier has the form:
289 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
291 So equating powers of t leads to:
292 r1 = 2/3 p1 + 1/3 p0
293 r2 = 2/3 p1 + 1/3 p2
294 and of course r0 = p0, r3 = p2
297 /* FIXME: Possible optimization in endpoint calculation
298 if there are two consecutive curves */
299 cubic_control[0] = outline->points[point-1];
300 if (!(outline->tags[point-1] & FT_Curve_Tag_On)) {
301 cubic_control[0].x += outline->points[point].x + 1;
302 cubic_control[0].y += outline->points[point].y + 1;
303 cubic_control[0].x >>= 1;
304 cubic_control[0].y >>= 1;
306 if (point+1 > outline->contours[contour])
307 cubic_control[3] = outline->points[first_pt];
308 else {
309 cubic_control[3] = outline->points[point+1];
310 if (!(outline->tags[point+1] & FT_Curve_Tag_On)) {
311 cubic_control[3].x += outline->points[point].x + 1;
312 cubic_control[3].y += outline->points[point].y + 1;
313 cubic_control[3].x >>= 1;
314 cubic_control[3].y >>= 1;
318 /* r1 = 1/3 p0 + 2/3 p1
319 r2 = 1/3 p2 + 2/3 p1 */
320 cubic_control[1].x = (2 * outline->points[point].x + 1) / 3;
321 cubic_control[1].y = (2 * outline->points[point].y + 1) / 3;
322 cubic_control[2] = cubic_control[1];
323 cubic_control[1].x += (cubic_control[0].x + 1) / 3;
324 cubic_control[1].y += (cubic_control[0].y + 1) / 3;
325 cubic_control[2].x += (cubic_control[3].x + 1) / 3;
326 cubic_control[2].y += (cubic_control[3].y + 1) / 3;
329 static short get_outline_data(const FT_Outline *outline, struct glyph_outline *ret)
331 short contour, point = 0, first_pt, count;
333 for (contour = 0, count = 0; contour < outline->n_contours; contour++) {
334 first_pt = point;
335 if (ret) {
336 ft_vector_to_d2d_point(&outline->points[point], &ret->points[count]);
337 ret->tags[count] = OUTLINE_POINT_START;
338 if (count)
339 ret->tags[count-1] |= OUTLINE_POINT_END;
342 point++;
343 count++;
345 while (point <= outline->contours[contour]) {
346 do {
347 if (outline->tags[point] & FT_Curve_Tag_On) {
348 if (ret) {
349 ft_vector_to_d2d_point(&outline->points[point], &ret->points[count]);
350 ret->tags[count] |= OUTLINE_POINT_LINE;
353 point++;
354 count++;
356 else {
358 if (ret) {
359 FT_Vector cubic_control[4];
361 get_cubic_glyph_outline(outline, point, first_pt, contour, cubic_control);
362 ft_vector_to_d2d_point(&cubic_control[1], &ret->points[count]);
363 ft_vector_to_d2d_point(&cubic_control[2], &ret->points[count+1]);
364 ft_vector_to_d2d_point(&cubic_control[3], &ret->points[count+2]);
365 ret->tags[count] = OUTLINE_POINT_BEZIER;
366 ret->tags[count+1] = OUTLINE_POINT_BEZIER;
367 ret->tags[count+2] = OUTLINE_POINT_BEZIER;
370 count += 3;
371 point++;
373 } while (point <= outline->contours[contour] &&
374 (outline->tags[point] & FT_Curve_Tag_On) ==
375 (outline->tags[point-1] & FT_Curve_Tag_On));
377 if (point <= outline->contours[contour] &&
378 outline->tags[point] & FT_Curve_Tag_On)
380 /* This is the closing pt of a bezier, but we've already
381 added it, so just inc point and carry on */
382 point++;
387 if (ret)
388 ret->tags[count-1] |= OUTLINE_POINT_END;
390 return count;
393 HRESULT freetype_get_glyph_outline(IDWriteFontFace2 *fontface, FLOAT emSize, UINT16 index, USHORT simulations, struct glyph_outline **ret)
395 FTC_ScalerRec scaler;
396 HRESULT hr = S_OK;
397 FT_Size size;
399 scaler.face_id = fontface;
400 scaler.width = emSize;
401 scaler.height = emSize;
402 scaler.pixel = 1;
403 scaler.x_res = 0;
404 scaler.y_res = 0;
406 EnterCriticalSection(&freetype_cs);
407 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
408 if (pFT_Load_Glyph(size->face, index, FT_LOAD_NO_BITMAP) == 0) {
409 FT_Outline *outline = &size->face->glyph->outline;
410 short count;
411 FT_Matrix m;
413 m.xx = 1 << 16;
414 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
415 m.yx = 0;
416 m.yy = -(1 << 16); /* flip Y axis */
418 pFT_Outline_Transform(outline, &m);
420 count = get_outline_data(outline, NULL);
421 hr = new_glyph_outline(count, ret);
422 if (hr == S_OK) {
423 get_outline_data(outline, *ret);
424 (*ret)->advance = size->face->glyph->metrics.horiAdvance >> 6;
428 LeaveCriticalSection(&freetype_cs);
430 return hr;
433 UINT16 freetype_get_glyphcount(IDWriteFontFace2 *fontface)
435 UINT16 count = 0;
436 FT_Face face;
438 EnterCriticalSection(&freetype_cs);
439 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
440 count = face->num_glyphs;
441 LeaveCriticalSection(&freetype_cs);
443 return count;
446 UINT16 freetype_get_glyphindex(IDWriteFontFace2 *fontface, UINT32 codepoint, INT charmap)
448 UINT16 glyph;
450 EnterCriticalSection(&freetype_cs);
451 if (charmap == -1)
452 glyph = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint);
453 else {
454 /* special handling for symbol fonts */
455 if (codepoint < 0x100) codepoint += 0xf000;
456 glyph = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint);
457 if (!glyph)
458 glyph = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint - 0xf000);
460 LeaveCriticalSection(&freetype_cs);
462 return glyph;
465 BOOL freetype_has_kerning_pairs(IDWriteFontFace2 *fontface)
467 BOOL has_kerning_pairs = FALSE;
468 FT_Face face;
470 EnterCriticalSection(&freetype_cs);
471 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
472 has_kerning_pairs = FT_HAS_KERNING(face);
473 LeaveCriticalSection(&freetype_cs);
475 return has_kerning_pairs;
478 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace2 *fontface, UINT16 left, UINT16 right)
480 INT32 adjustment = 0;
481 FT_Face face;
483 EnterCriticalSection(&freetype_cs);
484 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
485 FT_Vector kern;
486 if (FT_HAS_KERNING(face)) {
487 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
488 adjustment = kern.x;
491 LeaveCriticalSection(&freetype_cs);
493 return adjustment;
496 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
498 ft_matrix->xx = m->m11 * 0x10000;
499 ft_matrix->xy = -m->m21 * 0x10000;
500 ft_matrix->yx = -m->m12 * 0x10000;
501 ft_matrix->yy = m->m22 * 0x10000;
504 /* Should be used only while holding 'freetype_cs' */
505 static BOOL is_face_scalable(IDWriteFontFace2 *fontface)
507 FT_Face face;
508 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
509 return FT_IS_SCALABLE(face);
510 else
511 return FALSE;
514 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
516 FTC_ImageTypeRec imagetype;
517 FT_BBox bbox = { 0 };
518 FT_Glyph glyph;
520 EnterCriticalSection(&freetype_cs);
522 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef,
523 disable transform if that's the case. */
524 if (bitmap->m) {
525 if (!is_face_scalable(bitmap->fontface))
526 bitmap->m = NULL;
529 imagetype.face_id = bitmap->fontface;
530 imagetype.width = 0;
531 imagetype.height = bitmap->emsize;
532 imagetype.flags = bitmap->m ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
534 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
535 if (bitmap->m) {
536 FT_Glyph glyph_copy;
538 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
539 FT_Matrix ft_matrix;
541 ft_matrix_from_dwrite_matrix(bitmap->m, &ft_matrix);
542 pFT_Glyph_Transform(glyph_copy, &ft_matrix, NULL);
543 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
544 pFT_Done_Glyph(glyph_copy);
547 else
548 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
551 LeaveCriticalSection(&freetype_cs);
553 /* flip Y axis */
554 bitmap->bbox.left = bbox.xMin;
555 bitmap->bbox.right = bbox.xMax;
556 bitmap->bbox.top = -bbox.yMax;
557 bitmap->bbox.bottom = -bbox.yMin;
560 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
562 const RECT *bbox = &bitmap->bbox;
563 int width = bbox->right - bbox->left;
564 int height = bbox->bottom - bbox->top;
566 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
567 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
568 const FT_Outline *src = &outline->outline;
569 FT_Bitmap ft_bitmap;
570 FT_Outline copy;
572 ft_bitmap.width = width;
573 ft_bitmap.rows = height;
574 ft_bitmap.pitch = bitmap->pitch;
575 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
576 ft_bitmap.buffer = bitmap->buf;
578 /* Note: FreeType will only set 'black' bits for us. */
579 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
580 pFT_Outline_Copy(src, &copy);
581 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
582 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
583 pFT_Outline_Done(library, &copy);
586 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
587 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
588 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
589 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
590 int h = min(height, ft_bitmap->rows);
592 while (h--) {
593 memcpy(dst, src, w);
594 src += ft_bitmap->pitch;
595 dst += bitmap->pitch;
598 else
599 FIXME("format %x not handled\n", glyph->format);
601 return TRUE;
604 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
606 const RECT *bbox = &bitmap->bbox;
607 int width = bbox->right - bbox->left;
608 int height = bbox->bottom - bbox->top;
609 BOOL ret = FALSE;
611 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
612 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
613 const FT_Outline *src = &outline->outline;
614 FT_Bitmap ft_bitmap;
615 FT_Outline copy;
617 ft_bitmap.width = width;
618 ft_bitmap.rows = height;
619 ft_bitmap.pitch = bitmap->pitch;
620 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
621 ft_bitmap.buffer = bitmap->buf;
623 /* Note: FreeType will only set 'black' bits for us. */
624 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
625 pFT_Outline_Copy(src, &copy);
626 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
627 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
628 pFT_Outline_Done(library, &copy);
631 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
632 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
633 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
634 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
635 int h = min(height, ft_bitmap->rows);
637 while (h--) {
638 memcpy(dst, src, w);
639 src += ft_bitmap->pitch;
640 dst += bitmap->pitch;
643 ret = TRUE;
645 else
646 FIXME("format %x not handled\n", glyph->format);
648 return ret;
651 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
653 FTC_ImageTypeRec imagetype;
654 BOOL ret = FALSE;
655 FT_Glyph glyph;
657 EnterCriticalSection(&freetype_cs);
659 if (bitmap->m) {
660 if (!is_face_scalable(bitmap->fontface))
661 bitmap->m = NULL;
664 imagetype.face_id = bitmap->fontface;
665 imagetype.width = 0;
666 imagetype.height = bitmap->emsize;
667 imagetype.flags = bitmap->m ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
669 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
670 FT_Glyph glyph_copy;
672 if (bitmap->m) {
673 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
674 FT_Matrix ft_matrix;
676 ft_matrix_from_dwrite_matrix(bitmap->m, &ft_matrix);
677 pFT_Glyph_Transform(glyph_copy, &ft_matrix, NULL);
678 glyph = glyph_copy;
681 else
682 glyph_copy = NULL;
684 if (bitmap->type == DWRITE_TEXTURE_CLEARTYPE_3x1)
685 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
686 else
687 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
689 if (glyph_copy)
690 pFT_Done_Glyph(glyph_copy);
693 LeaveCriticalSection(&freetype_cs);
695 return ret;
698 INT freetype_get_charmap_index(IDWriteFontFace2 *fontface, BOOL *is_symbol)
700 INT charmap_index = -1;
701 FT_Face face;
703 *is_symbol = FALSE;
705 EnterCriticalSection(&freetype_cs);
706 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
707 TT_OS2 *os2 = pFT_Get_Sfnt_Table(face, ft_sfnt_os2);
708 FT_Int i;
710 if (os2) {
711 FT_UInt dummy;
712 if (os2->version == 0)
713 *is_symbol = pFT_Get_First_Char(face, &dummy) >= 0x100;
714 else
715 *is_symbol = !!(os2->ulCodePageRange1 & FS_SYMBOL);
718 for (i = 0; i < face->num_charmaps; i++)
719 if (face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL) {
720 *is_symbol = TRUE;
721 charmap_index = i;
722 break;
725 LeaveCriticalSection(&freetype_cs);
727 return charmap_index;
730 INT32 freetype_get_glyph_advance(IDWriteFontFace2 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
732 FTC_ImageTypeRec imagetype;
733 FT_Glyph glyph;
734 INT32 advance;
736 imagetype.face_id = fontface;
737 imagetype.width = 0;
738 imagetype.height = emSize;
739 imagetype.flags = FT_LOAD_DEFAULT;
740 if (mode == DWRITE_MEASURING_MODE_NATURAL)
741 imagetype.flags |= FT_LOAD_NO_HINTING;
743 EnterCriticalSection(&freetype_cs);
744 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0)
745 advance = glyph->advance.x >> 16;
746 else
747 advance = 0;
748 LeaveCriticalSection(&freetype_cs);
750 return advance;
753 #else /* HAVE_FREETYPE */
755 BOOL init_freetype(void)
757 return FALSE;
760 void release_freetype(void)
764 void freetype_notify_cacheremove(IDWriteFontFace2 *fontface)
768 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace2 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
770 return E_NOTIMPL;
773 BOOL freetype_is_monospaced(IDWriteFontFace2 *fontface)
775 return FALSE;
778 HRESULT freetype_get_glyph_outline(IDWriteFontFace2 *fontface, FLOAT emSize, UINT16 index, USHORT simulations, struct glyph_outline **ret)
780 *ret = NULL;
781 return E_NOTIMPL;
784 UINT16 freetype_get_glyphcount(IDWriteFontFace2 *fontface)
786 return 0;
789 UINT16 freetype_get_glyphindex(IDWriteFontFace2 *fontface, UINT32 codepoint, INT charmap)
791 return 0;
794 BOOL freetype_has_kerning_pairs(IDWriteFontFace2 *fontface)
796 return FALSE;
799 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace2 *fontface, UINT16 left, UINT16 right)
801 return 0;
804 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
806 memset(&bitmap->bbox, 0, sizeof(bitmap->bbox));
809 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
811 return FALSE;
814 INT freetype_get_charmap_index(IDWriteFontFace2 *fontface, BOOL *is_symbol)
816 *is_symbol = FALSE;
817 return -1;
820 INT32 freetype_get_glyph_advance(IDWriteFontFace2 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
822 return 0;
825 #endif /* HAVE_FREETYPE */