Release 6.15.
[wine.git] / dlls / dwrite / freetype.c
blobca29b0d97bd6faa322010f7c3981f5cac263e642
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 #if 0
22 #pragma makedep unix
23 #endif
25 #include "config.h"
26 #include "wine/port.h"
28 #ifdef HAVE_FT2BUILD_H
29 #include <ft2build.h>
30 #include FT_CACHE_H
31 #include FT_FREETYPE_H
32 #include FT_OUTLINE_H
33 #include FT_TRUETYPE_TABLES_H
34 #endif /* HAVE_FT2BUILD_H */
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "windef.h"
39 #include "wine/debug.h"
41 #include "dwrite_private.h"
43 #ifdef HAVE_FREETYPE
45 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
47 static RTL_CRITICAL_SECTION freetype_cs;
48 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
50 0, 0, &freetype_cs,
51 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
52 0, 0, { (DWORD_PTR)(__FILE__ ": freetype_cs") }
54 static RTL_CRITICAL_SECTION freetype_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
56 static void *ft_handle = NULL;
57 static FT_Library library = 0;
58 static FTC_Manager cache_manager = 0;
59 static FTC_ImageCache image_cache = 0;
60 typedef struct
62 FT_Int major;
63 FT_Int minor;
64 FT_Int patch;
65 } FT_Version_t;
67 static const struct font_callback_funcs *callback_funcs;
69 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
70 MAKE_FUNCPTR(FT_Done_FreeType);
71 MAKE_FUNCPTR(FT_Done_Glyph);
72 MAKE_FUNCPTR(FT_Get_First_Char);
73 MAKE_FUNCPTR(FT_Get_Kerning);
74 MAKE_FUNCPTR(FT_Get_Sfnt_Table);
75 MAKE_FUNCPTR(FT_Glyph_Copy);
76 MAKE_FUNCPTR(FT_Glyph_Get_CBox);
77 MAKE_FUNCPTR(FT_Glyph_Transform);
78 MAKE_FUNCPTR(FT_Init_FreeType);
79 MAKE_FUNCPTR(FT_Library_Version);
80 MAKE_FUNCPTR(FT_Load_Glyph);
81 MAKE_FUNCPTR(FT_Matrix_Multiply);
82 MAKE_FUNCPTR(FT_MulDiv);
83 MAKE_FUNCPTR(FT_New_Memory_Face);
84 MAKE_FUNCPTR(FT_Outline_Copy);
85 MAKE_FUNCPTR(FT_Outline_Decompose);
86 MAKE_FUNCPTR(FT_Outline_Done);
87 MAKE_FUNCPTR(FT_Outline_Embolden);
88 MAKE_FUNCPTR(FT_Outline_Get_Bitmap);
89 MAKE_FUNCPTR(FT_Outline_New);
90 MAKE_FUNCPTR(FT_Outline_Transform);
91 MAKE_FUNCPTR(FT_Outline_Translate);
92 MAKE_FUNCPTR(FTC_ImageCache_Lookup);
93 MAKE_FUNCPTR(FTC_ImageCache_New);
94 MAKE_FUNCPTR(FTC_Manager_New);
95 MAKE_FUNCPTR(FTC_Manager_Done);
96 MAKE_FUNCPTR(FTC_Manager_LookupFace);
97 MAKE_FUNCPTR(FTC_Manager_LookupSize);
98 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID);
99 #undef MAKE_FUNCPTR
100 static FT_Error (*pFT_Outline_EmboldenXY)(FT_Outline *, FT_Pos, FT_Pos);
102 static void face_finalizer(void *object)
104 FT_Face face = object;
105 callback_funcs->release_font_data((struct font_data_context *)face->generic.data);
108 static FT_Error face_requester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *face)
110 struct font_data_context *context;
111 const void *data_ptr;
112 FT_Error fterror;
113 UINT64 data_size;
114 UINT32 index;
116 *face = NULL;
118 if (!face_id)
120 WARN("NULL fontface requested.\n");
121 return FT_Err_Ok;
124 if (callback_funcs->get_font_data(face_id, &data_ptr, &data_size, &index, &context))
125 return FT_Err_Ok;
127 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
128 if (fterror == FT_Err_Ok)
130 (*face)->generic.data = context;
131 (*face)->generic.finalizer = face_finalizer;
133 else
134 callback_funcs->release_font_data(context);
136 return fterror;
139 static BOOL init_freetype(void)
141 FT_Version_t FT_Version;
143 ft_handle = dlopen(SONAME_LIBFREETYPE, RTLD_NOW);
144 if (!ft_handle) {
145 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
146 return FALSE;
149 #define LOAD_FUNCPTR(f) if((p##f = dlsym(ft_handle, #f)) == NULL){WARN("Can't find symbol %s\n", #f); goto sym_not_found;}
150 LOAD_FUNCPTR(FT_Done_FreeType)
151 LOAD_FUNCPTR(FT_Done_Glyph)
152 LOAD_FUNCPTR(FT_Get_First_Char)
153 LOAD_FUNCPTR(FT_Get_Kerning)
154 LOAD_FUNCPTR(FT_Get_Sfnt_Table)
155 LOAD_FUNCPTR(FT_Glyph_Copy)
156 LOAD_FUNCPTR(FT_Glyph_Get_CBox)
157 LOAD_FUNCPTR(FT_Glyph_Transform)
158 LOAD_FUNCPTR(FT_Init_FreeType)
159 LOAD_FUNCPTR(FT_Library_Version)
160 LOAD_FUNCPTR(FT_Load_Glyph)
161 LOAD_FUNCPTR(FT_Matrix_Multiply)
162 LOAD_FUNCPTR(FT_MulDiv)
163 LOAD_FUNCPTR(FT_New_Memory_Face)
164 LOAD_FUNCPTR(FT_Outline_Copy)
165 LOAD_FUNCPTR(FT_Outline_Decompose)
166 LOAD_FUNCPTR(FT_Outline_Done)
167 LOAD_FUNCPTR(FT_Outline_Embolden)
168 LOAD_FUNCPTR(FT_Outline_Get_Bitmap)
169 LOAD_FUNCPTR(FT_Outline_New)
170 LOAD_FUNCPTR(FT_Outline_Transform)
171 LOAD_FUNCPTR(FT_Outline_Translate)
172 LOAD_FUNCPTR(FTC_ImageCache_Lookup)
173 LOAD_FUNCPTR(FTC_ImageCache_New)
174 LOAD_FUNCPTR(FTC_Manager_New)
175 LOAD_FUNCPTR(FTC_Manager_Done)
176 LOAD_FUNCPTR(FTC_Manager_LookupFace)
177 LOAD_FUNCPTR(FTC_Manager_LookupSize)
178 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
179 #undef LOAD_FUNCPTR
180 pFT_Outline_EmboldenXY = dlsym(ft_handle, "FT_Outline_EmboldenXY");
182 if (pFT_Init_FreeType(&library) != 0) {
183 ERR("Can't init FreeType library\n");
184 dlclose(ft_handle);
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_ImageCache_New(cache_manager, &image_cache) != 0) {
194 ERR("Failed to init FreeType cache\n");
195 pFTC_Manager_Done(cache_manager);
196 pFT_Done_FreeType(library);
197 dlclose(ft_handle);
198 ft_handle = NULL;
199 return FALSE;
202 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
203 return TRUE;
205 sym_not_found:
206 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
207 dlclose(ft_handle);
208 ft_handle = NULL;
209 return FALSE;
212 static void CDECL freetype_notify_release(void *key)
214 RtlEnterCriticalSection(&freetype_cs);
215 pFTC_Manager_RemoveFaceID(cache_manager, key);
216 RtlLeaveCriticalSection(&freetype_cs);
219 static void CDECL freetype_get_design_glyph_metrics(void *key, UINT16 upem, UINT16 ascent,
220 unsigned int simulations, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
222 FTC_ScalerRec scaler;
223 FT_Size size;
225 scaler.face_id = key;
226 scaler.width = upem;
227 scaler.height = upem;
228 scaler.pixel = 1;
229 scaler.x_res = 0;
230 scaler.y_res = 0;
232 RtlEnterCriticalSection(&freetype_cs);
233 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
234 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
235 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
237 ret->leftSideBearing = metrics->horiBearingX;
238 ret->advanceWidth = metrics->horiAdvance;
239 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
241 ret->advanceHeight = metrics->vertAdvance;
242 ret->verticalOriginY = ascent;
243 ret->topSideBearing = ascent - metrics->horiBearingY;
244 ret->bottomSideBearing = metrics->vertAdvance - metrics->height - ret->topSideBearing;
246 /* Adjust in case of bold simulation, glyphs without contours are ignored. */
247 if (simulations & DWRITE_FONT_SIMULATIONS_BOLD &&
248 size->face->glyph->format == FT_GLYPH_FORMAT_OUTLINE && size->face->glyph->outline.n_contours)
250 if (ret->advanceWidth)
251 ret->advanceWidth += (upem + 49) / 50;
255 RtlLeaveCriticalSection(&freetype_cs);
258 struct decompose_context
260 struct dwrite_outline *outline;
261 BOOL figure_started;
262 BOOL move_to; /* last call was 'move_to' */
263 FT_Vector origin; /* 'pen' position from last call */
266 static inline void ft_vector_to_d2d_point(const FT_Vector *v, D2D1_POINT_2F *p)
268 p->x = v->x / 64.0f;
269 p->y = v->y / 64.0f;
272 static int dwrite_outline_push_tag(struct dwrite_outline *outline, unsigned char tag)
274 if (!dwrite_array_reserve((void **)&outline->tags.values, &outline->tags.size, outline->tags.count + 1,
275 sizeof(*outline->tags.values)))
277 return 1;
280 outline->tags.values[outline->tags.count++] = tag;
282 return 0;
285 static int dwrite_outline_push_points(struct dwrite_outline *outline, const D2D1_POINT_2F *points, unsigned int count)
287 if (!dwrite_array_reserve((void **)&outline->points.values, &outline->points.size, outline->points.count + count,
288 sizeof(*outline->points.values)))
290 return 1;
293 memcpy(&outline->points.values[outline->points.count], points, sizeof(*points) * count);
294 outline->points.count += count;
296 return 0;
299 static int decompose_beginfigure(struct decompose_context *ctxt)
301 D2D1_POINT_2F point;
302 int ret;
304 if (!ctxt->move_to)
305 return 0;
307 ft_vector_to_d2d_point(&ctxt->origin, &point);
308 if ((ret = dwrite_outline_push_tag(ctxt->outline, OUTLINE_BEGIN_FIGURE))) return ret;
309 if ((ret = dwrite_outline_push_points(ctxt->outline, &point, 1))) return ret;
311 ctxt->figure_started = TRUE;
312 ctxt->move_to = FALSE;
314 return 0;
317 static int decompose_move_to(const FT_Vector *to, void *user)
319 struct decompose_context *ctxt = (struct decompose_context *)user;
320 int ret;
322 if (ctxt->figure_started)
324 if ((ret = dwrite_outline_push_tag(ctxt->outline, OUTLINE_END_FIGURE))) return ret;
325 ctxt->figure_started = FALSE;
328 ctxt->move_to = TRUE;
329 ctxt->origin = *to;
330 return 0;
333 static int decompose_line_to(const FT_Vector *to, void *user)
335 struct decompose_context *ctxt = (struct decompose_context *)user;
336 D2D1_POINT_2F point;
337 int ret;
339 /* Special case for empty contours, in a way freetype returns them. */
340 if (ctxt->move_to && !memcmp(to, &ctxt->origin, sizeof(*to)))
341 return 0;
343 ft_vector_to_d2d_point(to, &point);
345 if ((ret = decompose_beginfigure(ctxt))) return ret;
346 if ((ret = dwrite_outline_push_points(ctxt->outline, &point, 1))) return ret;
347 if ((ret = dwrite_outline_push_tag(ctxt->outline, OUTLINE_LINE))) return ret;
349 ctxt->origin = *to;
350 return 0;
353 static int decompose_conic_to(const FT_Vector *control, const FT_Vector *to, void *user)
355 struct decompose_context *ctxt = (struct decompose_context *)user;
356 D2D1_POINT_2F points[3];
357 FT_Vector cubic[3];
358 int ret;
360 if ((ret = decompose_beginfigure(ctxt)))
361 return ret;
363 /* convert from quadratic to cubic */
366 The parametric eqn for a cubic Bezier is, from PLRM:
367 r(t) = at^3 + bt^2 + ct + r0
368 with the control points:
369 r1 = r0 + c/3
370 r2 = r1 + (c + b)/3
371 r3 = r0 + c + b + a
373 A quadratic Bezier has the form:
374 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
376 So equating powers of t leads to:
377 r1 = 2/3 p1 + 1/3 p0
378 r2 = 2/3 p1 + 1/3 p2
379 and of course r0 = p0, r3 = p2
382 /* r1 = 1/3 p0 + 2/3 p1
383 r2 = 1/3 p2 + 2/3 p1 */
384 cubic[0].x = (2 * control->x + 1) / 3;
385 cubic[0].y = (2 * control->y + 1) / 3;
386 cubic[1] = cubic[0];
387 cubic[0].x += (ctxt->origin.x + 1) / 3;
388 cubic[0].y += (ctxt->origin.y + 1) / 3;
389 cubic[1].x += (to->x + 1) / 3;
390 cubic[1].y += (to->y + 1) / 3;
391 cubic[2] = *to;
393 ft_vector_to_d2d_point(cubic, points);
394 ft_vector_to_d2d_point(cubic + 1, points + 1);
395 ft_vector_to_d2d_point(cubic + 2, points + 2);
396 if ((ret = dwrite_outline_push_points(ctxt->outline, points, 3))) return ret;
397 if ((ret = dwrite_outline_push_tag(ctxt->outline, OUTLINE_BEZIER))) return ret;
398 ctxt->origin = *to;
399 return 0;
402 static int decompose_cubic_to(const FT_Vector *control1, const FT_Vector *control2,
403 const FT_Vector *to, void *user)
405 struct decompose_context *ctxt = (struct decompose_context *)user;
406 D2D1_POINT_2F points[3];
407 int ret;
409 if ((ret = decompose_beginfigure(ctxt)))
410 return ret;
412 ft_vector_to_d2d_point(control1, points);
413 ft_vector_to_d2d_point(control2, points + 1);
414 ft_vector_to_d2d_point(to, points + 2);
415 ctxt->origin = *to;
417 if ((ret = dwrite_outline_push_points(ctxt->outline, points, 3))) return ret;
418 if ((ret = dwrite_outline_push_tag(ctxt->outline, OUTLINE_BEZIER))) return ret;
420 return 0;
423 static int decompose_outline(FT_Outline *ft_outline, struct dwrite_outline *outline)
425 static const FT_Outline_Funcs decompose_funcs =
427 decompose_move_to,
428 decompose_line_to,
429 decompose_conic_to,
430 decompose_cubic_to,
434 struct decompose_context context = { 0 };
435 int ret;
437 context.outline = outline;
439 ret = pFT_Outline_Decompose(ft_outline, &decompose_funcs, &context);
441 if (!ret && context.figure_started)
442 ret = dwrite_outline_push_tag(outline, OUTLINE_END_FIGURE);
444 return ret;
447 static void embolden_glyph_outline(FT_Outline *outline, FLOAT emsize)
449 FT_Pos strength;
451 strength = pFT_MulDiv(emsize, 1 << 6, 24);
452 if (pFT_Outline_EmboldenXY)
453 pFT_Outline_EmboldenXY(outline, strength, 0);
454 else
455 pFT_Outline_Embolden(outline, strength);
458 static void embolden_glyph(FT_Glyph glyph, FLOAT emsize)
460 FT_OutlineGlyph outline_glyph = (FT_OutlineGlyph)glyph;
462 if (glyph->format != FT_GLYPH_FORMAT_OUTLINE)
463 return;
465 embolden_glyph_outline(&outline_glyph->outline, emsize);
468 static int CDECL freetype_get_glyph_outline(void *key, float emSize, unsigned int simulations,
469 UINT16 glyph, struct dwrite_outline *outline)
471 FTC_ScalerRec scaler;
472 FT_Size size;
473 int ret;
475 scaler.face_id = key;
476 scaler.width = emSize;
477 scaler.height = emSize;
478 scaler.pixel = 1;
479 scaler.x_res = 0;
480 scaler.y_res = 0;
482 RtlEnterCriticalSection(&freetype_cs);
483 if (!(ret = pFTC_Manager_LookupSize(cache_manager, &scaler, &size)))
485 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_BITMAP) == 0)
487 FT_Outline *ft_outline = &size->face->glyph->outline;
488 FT_Matrix m;
490 if (simulations & DWRITE_FONT_SIMULATIONS_BOLD)
491 embolden_glyph_outline(ft_outline, emSize);
493 m.xx = 1 << 16;
494 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
495 m.yx = 0;
496 m.yy = -(1 << 16); /* flip Y axis */
498 pFT_Outline_Transform(ft_outline, &m);
500 ret = decompose_outline(ft_outline, outline);
503 RtlLeaveCriticalSection(&freetype_cs);
505 return ret;
508 static UINT16 CDECL freetype_get_glyph_count(void *key)
510 UINT16 count = 0;
511 FT_Face face;
513 RtlEnterCriticalSection(&freetype_cs);
514 if (pFTC_Manager_LookupFace(cache_manager, key, &face) == 0)
515 count = face->num_glyphs;
516 RtlLeaveCriticalSection(&freetype_cs);
518 return count;
521 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
523 ft_matrix->xx = m->m11 * 0x10000;
524 ft_matrix->xy = -m->m21 * 0x10000;
525 ft_matrix->yx = -m->m12 * 0x10000;
526 ft_matrix->yy = m->m22 * 0x10000;
529 /* Should be used only while holding 'freetype_cs' */
530 static BOOL is_face_scalable(void *key)
532 FT_Face face;
533 if (pFTC_Manager_LookupFace(cache_manager, key, &face) == 0)
534 return FT_IS_SCALABLE(face);
535 else
536 return FALSE;
539 static BOOL get_glyph_transform(struct dwrite_glyphbitmap *bitmap, FT_Matrix *ret)
541 FT_Matrix m;
543 ret->xx = 1 << 16;
544 ret->xy = 0;
545 ret->yx = 0;
546 ret->yy = 1 << 16;
548 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
549 Disable transform if that's the case. */
550 if (!is_face_scalable(bitmap->key) || (!bitmap->m && bitmap->simulations == 0))
551 return FALSE;
553 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
554 m.xx = 1 << 16;
555 m.xy = (1 << 16) / 3;
556 m.yx = 0;
557 m.yy = 1 << 16;
558 pFT_Matrix_Multiply(&m, ret);
561 if (bitmap->m) {
562 ft_matrix_from_dwrite_matrix(bitmap->m, &m);
563 pFT_Matrix_Multiply(&m, ret);
566 return TRUE;
569 static void CDECL freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
571 FTC_ImageTypeRec imagetype;
572 FT_BBox bbox = { 0 };
573 BOOL needs_transform;
574 FT_Glyph glyph;
575 FT_Matrix m;
577 RtlEnterCriticalSection(&freetype_cs);
579 needs_transform = get_glyph_transform(bitmap, &m);
581 imagetype.face_id = bitmap->key;
582 imagetype.width = 0;
583 imagetype.height = bitmap->emsize;
584 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
586 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->glyph, &glyph, NULL) == 0) {
587 if (needs_transform) {
588 FT_Glyph glyph_copy;
590 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
591 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_BOLD)
592 embolden_glyph(glyph_copy, bitmap->emsize);
594 /* Includes oblique and user transform. */
595 pFT_Glyph_Transform(glyph_copy, &m, NULL);
596 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
597 pFT_Done_Glyph(glyph_copy);
600 else
601 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
604 RtlLeaveCriticalSection(&freetype_cs);
606 /* flip Y axis */
607 SetRect(&bitmap->bbox, bbox.xMin, -bbox.yMax, bbox.xMax, -bbox.yMin);
610 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
612 const RECT *bbox = &bitmap->bbox;
613 int width = bbox->right - bbox->left;
614 int height = bbox->bottom - bbox->top;
616 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
617 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
618 const FT_Outline *src = &outline->outline;
619 FT_Bitmap ft_bitmap;
620 FT_Outline copy;
622 ft_bitmap.width = width;
623 ft_bitmap.rows = height;
624 ft_bitmap.pitch = bitmap->pitch;
625 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
626 ft_bitmap.buffer = bitmap->buf;
628 /* Note: FreeType will only set 'black' bits for us. */
629 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
630 pFT_Outline_Copy(src, &copy);
631 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
632 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
633 pFT_Outline_Done(library, &copy);
636 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
637 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
638 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
639 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
640 int h = min(height, ft_bitmap->rows);
642 while (h--) {
643 memcpy(dst, src, w);
644 src += ft_bitmap->pitch;
645 dst += bitmap->pitch;
648 else
649 FIXME("format %x not handled\n", glyph->format);
651 return TRUE;
654 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
656 const RECT *bbox = &bitmap->bbox;
657 int width = bbox->right - bbox->left;
658 int height = bbox->bottom - bbox->top;
659 BOOL ret = FALSE;
661 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
662 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
663 const FT_Outline *src = &outline->outline;
664 FT_Bitmap ft_bitmap;
665 FT_Outline copy;
667 ft_bitmap.width = width;
668 ft_bitmap.rows = height;
669 ft_bitmap.pitch = bitmap->pitch;
670 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
671 ft_bitmap.buffer = bitmap->buf;
673 /* Note: FreeType will only set 'black' bits for us. */
674 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
675 pFT_Outline_Copy(src, &copy);
676 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
677 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
678 pFT_Outline_Done(library, &copy);
681 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
682 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
683 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
684 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
685 int h = min(height, ft_bitmap->rows);
687 while (h--) {
688 memcpy(dst, src, w);
689 src += ft_bitmap->pitch;
690 dst += bitmap->pitch;
693 ret = TRUE;
695 else
696 FIXME("format %x not handled\n", glyph->format);
698 return ret;
701 static BOOL CDECL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
703 FTC_ImageTypeRec imagetype;
704 BOOL needs_transform;
705 BOOL ret = FALSE;
706 FT_Glyph glyph;
707 FT_Matrix m;
709 RtlEnterCriticalSection(&freetype_cs);
711 needs_transform = get_glyph_transform(bitmap, &m);
713 imagetype.face_id = bitmap->key;
714 imagetype.width = 0;
715 imagetype.height = bitmap->emsize;
716 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
718 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->glyph, &glyph, NULL) == 0) {
719 FT_Glyph glyph_copy;
721 if (needs_transform) {
722 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
723 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_BOLD)
724 embolden_glyph(glyph_copy, bitmap->emsize);
726 /* Includes oblique and user transform. */
727 pFT_Glyph_Transform(glyph_copy, &m, NULL);
728 glyph = glyph_copy;
731 else
732 glyph_copy = NULL;
734 if (bitmap->aliased)
735 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
736 else
737 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
739 if (glyph_copy)
740 pFT_Done_Glyph(glyph_copy);
743 RtlLeaveCriticalSection(&freetype_cs);
745 return ret;
748 static INT32 CDECL freetype_get_glyph_advance(void *key, float emSize, UINT16 index,
749 DWRITE_MEASURING_MODE mode, BOOL *has_contours)
751 FTC_ImageTypeRec imagetype;
752 FT_Glyph glyph;
753 INT32 advance;
755 imagetype.face_id = key;
756 imagetype.width = 0;
757 imagetype.height = emSize;
758 imagetype.flags = FT_LOAD_DEFAULT;
759 if (mode == DWRITE_MEASURING_MODE_NATURAL)
760 imagetype.flags |= FT_LOAD_NO_HINTING;
762 RtlEnterCriticalSection(&freetype_cs);
763 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0) {
764 *has_contours = glyph->format == FT_GLYPH_FORMAT_OUTLINE && ((FT_OutlineGlyph)glyph)->outline.n_contours;
765 advance = glyph->advance.x >> 16;
767 else {
768 *has_contours = FALSE;
769 advance = 0;
771 RtlLeaveCriticalSection(&freetype_cs);
773 return advance;
776 const static struct font_backend_funcs freetype_funcs =
778 freetype_notify_release,
779 freetype_get_glyph_outline,
780 freetype_get_glyph_count,
781 freetype_get_glyph_advance,
782 freetype_get_glyph_bbox,
783 freetype_get_glyph_bitmap,
784 freetype_get_design_glyph_metrics,
787 static NTSTATUS init_freetype_lib(HMODULE module, DWORD reason, const void *ptr_in, void *ptr_out)
789 callback_funcs = ptr_in;
790 if (!init_freetype()) return STATUS_DLL_NOT_FOUND;
791 *(const struct font_backend_funcs **)ptr_out = &freetype_funcs;
792 return STATUS_SUCCESS;
795 static NTSTATUS release_freetype_lib(void)
797 pFTC_Manager_Done(cache_manager);
798 pFT_Done_FreeType(library);
799 return STATUS_SUCCESS;
802 #else /* HAVE_FREETYPE */
804 static void CDECL null_notify_release(void *key)
808 static int CDECL null_get_glyph_outline(void *key, float emSize, unsigned int simulations,
809 UINT16 glyph, struct dwrite_outline *outline)
811 return 1;
814 static UINT16 CDECL null_get_glyph_count(void *key)
816 return 0;
819 static INT32 CDECL null_get_glyph_advance(void *key, float emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
820 BOOL *has_contours)
822 *has_contours = FALSE;
823 return 0;
826 static void CDECL null_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
828 SetRectEmpty(&bitmap->bbox);
831 static BOOL CDECL null_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
833 return FALSE;
836 static void CDECL null_get_design_glyph_metrics(void *key, UINT16 upem, UINT16 ascent, unsigned int simulations,
837 UINT16 glyph, DWRITE_GLYPH_METRICS *metrics)
841 const static struct font_backend_funcs null_funcs =
843 null_notify_release,
844 null_get_glyph_outline,
845 null_get_glyph_count,
846 null_get_glyph_advance,
847 null_get_glyph_bbox,
848 null_get_glyph_bitmap,
849 null_get_design_glyph_metrics,
852 static NTSTATUS init_freetype_lib(HMODULE module, DWORD reason, const void *ptr_in, void *ptr_out)
854 *(const struct font_backend_funcs **)ptr_out = &null_funcs;
855 return STATUS_DLL_NOT_FOUND;
858 static NTSTATUS release_freetype_lib(void)
860 return STATUS_DLL_NOT_FOUND;
863 #endif /* HAVE_FREETYPE */
865 NTSTATUS CDECL __wine_init_unix_lib(HMODULE module, DWORD reason, const void *ptr_in, void *ptr_out)
867 if (reason == DLL_PROCESS_ATTACH)
868 return init_freetype_lib(module, reason, ptr_in, ptr_out);
869 else if (reason == DLL_PROCESS_DETACH)
870 return release_freetype_lib();
871 return STATUS_SUCCESS;