msvcp90: Remove MSVCP_bool type.
[wine.git] / dlls / dwrite / freetype.c
blob260d87b7ac683fb458d91366cfe73f70d1d54653
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/debug.h"
37 #include "dwrite_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
41 #ifdef HAVE_FREETYPE
43 static CRITICAL_SECTION freetype_cs;
44 static CRITICAL_SECTION_DEBUG critsect_debug =
46 0, 0, &freetype_cs,
47 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
48 0, 0, { (DWORD_PTR)(__FILE__ ": freetype_cs") }
50 static CRITICAL_SECTION freetype_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
52 static void *ft_handle = NULL;
53 static FT_Library library = 0;
54 static FTC_Manager cache_manager = 0;
55 static FTC_ImageCache image_cache = 0;
56 typedef struct
58 FT_Int major;
59 FT_Int minor;
60 FT_Int patch;
61 } FT_Version_t;
63 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
64 MAKE_FUNCPTR(FT_Done_FreeType);
65 MAKE_FUNCPTR(FT_Done_Glyph);
66 MAKE_FUNCPTR(FT_Get_First_Char);
67 MAKE_FUNCPTR(FT_Get_Kerning);
68 MAKE_FUNCPTR(FT_Get_Sfnt_Table);
69 MAKE_FUNCPTR(FT_Glyph_Copy);
70 MAKE_FUNCPTR(FT_Glyph_Get_CBox);
71 MAKE_FUNCPTR(FT_Glyph_Transform);
72 MAKE_FUNCPTR(FT_Init_FreeType);
73 MAKE_FUNCPTR(FT_Library_Version);
74 MAKE_FUNCPTR(FT_Load_Glyph);
75 MAKE_FUNCPTR(FT_Matrix_Multiply);
76 MAKE_FUNCPTR(FT_New_Memory_Face);
77 MAKE_FUNCPTR(FT_Outline_Copy);
78 MAKE_FUNCPTR(FT_Outline_Decompose);
79 MAKE_FUNCPTR(FT_Outline_Done);
80 MAKE_FUNCPTR(FT_Outline_Embolden);
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_ImageCache_Lookup);
86 MAKE_FUNCPTR(FTC_ImageCache_New);
87 MAKE_FUNCPTR(FTC_Manager_New);
88 MAKE_FUNCPTR(FTC_Manager_Done);
89 MAKE_FUNCPTR(FTC_Manager_LookupFace);
90 MAKE_FUNCPTR(FTC_Manager_LookupSize);
91 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID);
92 #undef MAKE_FUNCPTR
93 static FT_Error (*pFT_Outline_EmboldenXY)(FT_Outline *, FT_Pos, FT_Pos);
95 struct face_finalizer_data
97 IDWriteFontFileStream *stream;
98 void *context;
101 static void face_finalizer(void *object)
103 FT_Face face = object;
104 struct face_finalizer_data *data = (struct face_finalizer_data *)face->generic.data;
106 IDWriteFontFileStream_ReleaseFileFragment(data->stream, data->context);
107 IDWriteFontFileStream_Release(data->stream);
108 heap_free(data);
111 static FT_Error face_requester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *face)
113 IDWriteFontFace *fontface = (IDWriteFontFace*)face_id;
114 IDWriteFontFileStream *stream;
115 IDWriteFontFile *file;
116 const void *data_ptr;
117 UINT32 index, count;
118 FT_Error fterror;
119 UINT64 data_size;
120 void *context;
121 HRESULT hr;
123 *face = NULL;
125 if (!fontface) {
126 WARN("NULL fontface requested.\n");
127 return FT_Err_Ok;
130 count = 1;
131 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
132 if (FAILED(hr))
133 return FT_Err_Ok;
135 hr = get_filestream_from_file(file, &stream);
136 IDWriteFontFile_Release(file);
137 if (FAILED(hr))
138 return FT_Err_Ok;
140 hr = IDWriteFontFileStream_GetFileSize(stream, &data_size);
141 if (FAILED(hr)) {
142 fterror = FT_Err_Invalid_Stream_Read;
143 goto fail;
146 hr = IDWriteFontFileStream_ReadFileFragment(stream, &data_ptr, 0, data_size, &context);
147 if (FAILED(hr)) {
148 fterror = FT_Err_Invalid_Stream_Read;
149 goto fail;
152 index = IDWriteFontFace_GetIndex(fontface);
153 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
154 if (fterror == FT_Err_Ok) {
155 struct face_finalizer_data *data;
157 data = heap_alloc(sizeof(*data));
158 data->stream = stream;
159 data->context = context;
161 (*face)->generic.data = data;
162 (*face)->generic.finalizer = face_finalizer;
163 return fterror;
165 else
166 IDWriteFontFileStream_ReleaseFileFragment(stream, context);
168 fail:
169 IDWriteFontFileStream_Release(stream);
171 return fterror;
174 BOOL init_freetype(void)
176 FT_Version_t FT_Version;
178 ft_handle = dlopen(SONAME_LIBFREETYPE, RTLD_NOW);
179 if (!ft_handle) {
180 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
181 return FALSE;
184 #define LOAD_FUNCPTR(f) if((p##f = dlsym(ft_handle, #f)) == NULL){WARN("Can't find symbol %s\n", #f); goto sym_not_found;}
185 LOAD_FUNCPTR(FT_Done_FreeType)
186 LOAD_FUNCPTR(FT_Done_Glyph)
187 LOAD_FUNCPTR(FT_Get_First_Char)
188 LOAD_FUNCPTR(FT_Get_Kerning)
189 LOAD_FUNCPTR(FT_Get_Sfnt_Table)
190 LOAD_FUNCPTR(FT_Glyph_Copy)
191 LOAD_FUNCPTR(FT_Glyph_Get_CBox)
192 LOAD_FUNCPTR(FT_Glyph_Transform)
193 LOAD_FUNCPTR(FT_Init_FreeType)
194 LOAD_FUNCPTR(FT_Library_Version)
195 LOAD_FUNCPTR(FT_Load_Glyph)
196 LOAD_FUNCPTR(FT_Matrix_Multiply)
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_Embolden)
202 LOAD_FUNCPTR(FT_Outline_Get_Bitmap)
203 LOAD_FUNCPTR(FT_Outline_New)
204 LOAD_FUNCPTR(FT_Outline_Transform)
205 LOAD_FUNCPTR(FT_Outline_Translate)
206 LOAD_FUNCPTR(FTC_ImageCache_Lookup)
207 LOAD_FUNCPTR(FTC_ImageCache_New)
208 LOAD_FUNCPTR(FTC_Manager_New)
209 LOAD_FUNCPTR(FTC_Manager_Done)
210 LOAD_FUNCPTR(FTC_Manager_LookupFace)
211 LOAD_FUNCPTR(FTC_Manager_LookupSize)
212 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
213 #undef LOAD_FUNCPTR
214 pFT_Outline_EmboldenXY = dlsym(ft_handle, "FT_Outline_EmboldenXY");
216 if (pFT_Init_FreeType(&library) != 0) {
217 ERR("Can't init FreeType library\n");
218 dlclose(ft_handle);
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_ImageCache_New(cache_manager, &image_cache) != 0) {
228 ERR("Failed to init FreeType cache\n");
229 pFTC_Manager_Done(cache_manager);
230 pFT_Done_FreeType(library);
231 dlclose(ft_handle);
232 ft_handle = NULL;
233 return FALSE;
236 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
237 return TRUE;
239 sym_not_found:
240 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
241 dlclose(ft_handle);
242 ft_handle = NULL;
243 return FALSE;
246 void release_freetype(void)
248 pFTC_Manager_Done(cache_manager);
249 pFT_Done_FreeType(library);
252 void freetype_notify_cacheremove(IDWriteFontFace5 *fontface)
254 EnterCriticalSection(&freetype_cs);
255 pFTC_Manager_RemoveFaceID(cache_manager, fontface);
256 LeaveCriticalSection(&freetype_cs);
259 HRESULT freetype_get_design_glyph_metrics(struct dwrite_fontface *fontface, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
261 FTC_ScalerRec scaler;
262 FT_Size size;
264 scaler.face_id = &fontface->IDWriteFontFace5_iface;
265 scaler.width = fontface->metrics.designUnitsPerEm;
266 scaler.height = fontface->metrics.designUnitsPerEm;
267 scaler.pixel = 1;
268 scaler.x_res = 0;
269 scaler.y_res = 0;
271 EnterCriticalSection(&freetype_cs);
272 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
273 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
274 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
276 ret->leftSideBearing = metrics->horiBearingX;
277 ret->advanceWidth = metrics->horiAdvance;
278 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
280 ret->advanceHeight = metrics->vertAdvance;
281 ret->verticalOriginY = fontface->typo_metrics.ascent;
282 ret->topSideBearing = fontface->typo_metrics.ascent - metrics->horiBearingY;
283 ret->bottomSideBearing = metrics->vertAdvance - metrics->height - ret->topSideBearing;
285 /* Adjust in case of bold simulation, glyphs without contours are ignored. */
286 if (fontface->simulations & DWRITE_FONT_SIMULATIONS_BOLD &&
287 size->face->glyph->format == FT_GLYPH_FORMAT_OUTLINE && size->face->glyph->outline.n_contours)
289 if (ret->advanceWidth)
290 ret->advanceWidth += (fontface->metrics.designUnitsPerEm + 49) / 50;
294 LeaveCriticalSection(&freetype_cs);
296 return S_OK;
299 struct decompose_context {
300 IDWriteGeometrySink *sink;
301 D2D1_POINT_2F offset;
302 BOOL figure_started;
303 BOOL move_to; /* last call was 'move_to' */
304 FT_Vector origin; /* 'pen' position from last call */
307 static inline void ft_vector_to_d2d_point(const FT_Vector *v, D2D1_POINT_2F offset, D2D1_POINT_2F *p)
309 p->x = (v->x / 64.0f) + offset.x;
310 p->y = (v->y / 64.0f) + offset.y;
313 static void decompose_beginfigure(struct decompose_context *ctxt)
315 D2D1_POINT_2F point;
317 if (!ctxt->move_to)
318 return;
320 ft_vector_to_d2d_point(&ctxt->origin, ctxt->offset, &point);
321 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt->sink, point, D2D1_FIGURE_BEGIN_FILLED);
323 ctxt->figure_started = TRUE;
324 ctxt->move_to = FALSE;
327 static int decompose_move_to(const FT_Vector *to, void *user)
329 struct decompose_context *ctxt = (struct decompose_context*)user;
331 if (ctxt->figure_started) {
332 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
333 ctxt->figure_started = FALSE;
336 ctxt->move_to = TRUE;
337 ctxt->origin = *to;
338 return 0;
341 static int decompose_line_to(const FT_Vector *to, void *user)
343 struct decompose_context *ctxt = (struct decompose_context*)user;
344 D2D1_POINT_2F point;
346 /* Special case for empty contours, in a way freetype returns them. */
347 if (ctxt->move_to && !memcmp(to, &ctxt->origin, sizeof(*to)))
348 return 0;
350 decompose_beginfigure(ctxt);
352 ft_vector_to_d2d_point(to, ctxt->offset, &point);
353 ID2D1SimplifiedGeometrySink_AddLines(ctxt->sink, &point, 1);
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 decompose_beginfigure(ctxt);
367 /* convert from quadratic to cubic */
370 The parametric eqn for a cubic Bezier is, from PLRM:
371 r(t) = at^3 + bt^2 + ct + r0
372 with the control points:
373 r1 = r0 + c/3
374 r2 = r1 + (c + b)/3
375 r3 = r0 + c + b + a
377 A quadratic Bezier has the form:
378 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
380 So equating powers of t leads to:
381 r1 = 2/3 p1 + 1/3 p0
382 r2 = 2/3 p1 + 1/3 p2
383 and of course r0 = p0, r3 = p2
386 /* r1 = 1/3 p0 + 2/3 p1
387 r2 = 1/3 p2 + 2/3 p1 */
388 cubic[0].x = (2 * control->x + 1) / 3;
389 cubic[0].y = (2 * control->y + 1) / 3;
390 cubic[1] = cubic[0];
391 cubic[0].x += (ctxt->origin.x + 1) / 3;
392 cubic[0].y += (ctxt->origin.y + 1) / 3;
393 cubic[1].x += (to->x + 1) / 3;
394 cubic[1].y += (to->y + 1) / 3;
395 cubic[2] = *to;
397 ft_vector_to_d2d_point(cubic, ctxt->offset, points);
398 ft_vector_to_d2d_point(cubic + 1, ctxt->offset, points + 1);
399 ft_vector_to_d2d_point(cubic + 2, ctxt->offset, points + 2);
400 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
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 decompose_beginfigure(ctxt);
413 ft_vector_to_d2d_point(control1, ctxt->offset, points);
414 ft_vector_to_d2d_point(control2, ctxt->offset, points + 1);
415 ft_vector_to_d2d_point(to, ctxt->offset, points + 2);
416 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
417 ctxt->origin = *to;
418 return 0;
421 static void decompose_outline(FT_Outline *outline, D2D1_POINT_2F offset, 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.offset = offset;
435 context.figure_started = FALSE;
436 context.move_to = FALSE;
437 context.origin.x = 0;
438 context.origin.y = 0;
440 pFT_Outline_Decompose(outline, &decompose_funcs, &context);
442 if (context.figure_started)
443 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
446 static void embolden_glyph_outline(FT_Outline *outline, FLOAT emsize)
448 FT_Pos strength;
450 strength = MulDiv(emsize, 1 << 6, 24);
451 if (pFT_Outline_EmboldenXY)
452 pFT_Outline_EmboldenXY(outline, strength, 0);
453 else
454 pFT_Outline_Embolden(outline, strength);
457 static void embolden_glyph(FT_Glyph glyph, FLOAT emsize)
459 FT_OutlineGlyph outline_glyph = (FT_OutlineGlyph)glyph;
461 if (glyph->format != FT_GLYPH_FORMAT_OUTLINE)
462 return;
464 embolden_glyph_outline(&outline_glyph->outline, emsize);
467 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace5 *fontface, float emSize, UINT16 const *glyphs,
468 float const *advances, DWRITE_GLYPH_OFFSET const *offsets, unsigned int count, BOOL is_rtl,
469 IDWriteGeometrySink *sink)
471 FTC_ScalerRec scaler;
472 USHORT simulations;
473 HRESULT hr = S_OK;
474 FT_Size size;
476 if (!count)
477 return S_OK;
479 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_WINDING);
481 simulations = IDWriteFontFace5_GetSimulations(fontface);
483 scaler.face_id = fontface;
484 scaler.width = emSize;
485 scaler.height = emSize;
486 scaler.pixel = 1;
487 scaler.x_res = 0;
488 scaler.y_res = 0;
490 EnterCriticalSection(&freetype_cs);
491 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
492 float rtl_factor = is_rtl ? -1.0f : 1.0f;
493 D2D1_POINT_2F origin;
494 unsigned int i;
496 origin.x = origin.y = 0.0f;
497 for (i = 0; i < count; ++i)
499 if (pFT_Load_Glyph(size->face, glyphs[i], FT_LOAD_NO_BITMAP) == 0)
501 FLOAT ft_advance = size->face->glyph->metrics.horiAdvance >> 6;
502 FT_Outline *outline = &size->face->glyph->outline;
503 D2D1_POINT_2F glyph_origin;
504 float advance;
505 FT_Matrix m;
507 if (simulations & DWRITE_FONT_SIMULATIONS_BOLD)
508 embolden_glyph_outline(outline, emSize);
510 m.xx = 1 << 16;
511 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
512 m.yx = 0;
513 m.yy = -(1 << 16); /* flip Y axis */
515 pFT_Outline_Transform(outline, &m);
517 if (advances)
518 advance = rtl_factor * advances[i];
519 else
520 advance = rtl_factor * ft_advance;
522 glyph_origin = origin;
523 if (is_rtl)
524 glyph_origin.x += advance;
526 /* glyph offsets act as current glyph adjustment */
527 if (offsets)
529 glyph_origin.x += rtl_factor * offsets[i].advanceOffset;
530 glyph_origin.y -= offsets[i].ascenderOffset;
533 decompose_outline(outline, glyph_origin, sink);
535 origin.x += advance;
539 else
540 hr = E_FAIL;
541 LeaveCriticalSection(&freetype_cs);
543 return hr;
546 UINT16 freetype_get_glyphcount(IDWriteFontFace5 *fontface)
548 UINT16 count = 0;
549 FT_Face face;
551 EnterCriticalSection(&freetype_cs);
552 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
553 count = face->num_glyphs;
554 LeaveCriticalSection(&freetype_cs);
556 return count;
559 BOOL freetype_has_kerning_pairs(IDWriteFontFace5 *fontface)
561 BOOL has_kerning_pairs = FALSE;
562 FT_Face face;
564 EnterCriticalSection(&freetype_cs);
565 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
566 has_kerning_pairs = !!FT_HAS_KERNING(face);
567 LeaveCriticalSection(&freetype_cs);
569 return has_kerning_pairs;
572 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace5 *fontface, UINT16 left, UINT16 right)
574 INT32 adjustment = 0;
575 FT_Face face;
577 EnterCriticalSection(&freetype_cs);
578 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
579 FT_Vector kern;
580 if (FT_HAS_KERNING(face)) {
581 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
582 adjustment = kern.x;
585 LeaveCriticalSection(&freetype_cs);
587 return adjustment;
590 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
592 ft_matrix->xx = m->m11 * 0x10000;
593 ft_matrix->xy = -m->m21 * 0x10000;
594 ft_matrix->yx = -m->m12 * 0x10000;
595 ft_matrix->yy = m->m22 * 0x10000;
598 /* Should be used only while holding 'freetype_cs' */
599 static BOOL is_face_scalable(IDWriteFontFace4 *fontface)
601 FT_Face face;
602 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
603 return FT_IS_SCALABLE(face);
604 else
605 return FALSE;
608 static BOOL get_glyph_transform(struct dwrite_glyphbitmap *bitmap, FT_Matrix *ret)
610 FT_Matrix m;
612 ret->xx = 1 << 16;
613 ret->xy = 0;
614 ret->yx = 0;
615 ret->yy = 1 << 16;
617 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
618 Disable transform if that's the case. */
619 if (!is_face_scalable(bitmap->fontface) || (!bitmap->m && bitmap->simulations == 0))
620 return FALSE;
622 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
623 m.xx = 1 << 16;
624 m.xy = (1 << 16) / 3;
625 m.yx = 0;
626 m.yy = 1 << 16;
627 pFT_Matrix_Multiply(&m, ret);
630 if (bitmap->m) {
631 ft_matrix_from_dwrite_matrix(bitmap->m, &m);
632 pFT_Matrix_Multiply(&m, ret);
635 return TRUE;
638 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
640 FTC_ImageTypeRec imagetype;
641 FT_BBox bbox = { 0 };
642 BOOL needs_transform;
643 FT_Glyph glyph;
644 FT_Matrix m;
646 EnterCriticalSection(&freetype_cs);
648 needs_transform = get_glyph_transform(bitmap, &m);
650 imagetype.face_id = bitmap->fontface;
651 imagetype.width = 0;
652 imagetype.height = bitmap->emsize;
653 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
655 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->glyph, &glyph, NULL) == 0) {
656 if (needs_transform) {
657 FT_Glyph glyph_copy;
659 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
660 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_BOLD)
661 embolden_glyph(glyph_copy, bitmap->emsize);
663 /* Includes oblique and user transform. */
664 pFT_Glyph_Transform(glyph_copy, &m, NULL);
665 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
666 pFT_Done_Glyph(glyph_copy);
669 else
670 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
673 LeaveCriticalSection(&freetype_cs);
675 /* flip Y axis */
676 SetRect(&bitmap->bbox, bbox.xMin, -bbox.yMax, bbox.xMax, -bbox.yMin);
679 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
681 const RECT *bbox = &bitmap->bbox;
682 int width = bbox->right - bbox->left;
683 int height = bbox->bottom - bbox->top;
685 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
686 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
687 const FT_Outline *src = &outline->outline;
688 FT_Bitmap ft_bitmap;
689 FT_Outline copy;
691 ft_bitmap.width = width;
692 ft_bitmap.rows = height;
693 ft_bitmap.pitch = bitmap->pitch;
694 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
695 ft_bitmap.buffer = bitmap->buf;
697 /* Note: FreeType will only set 'black' bits for us. */
698 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
699 pFT_Outline_Copy(src, &copy);
700 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
701 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
702 pFT_Outline_Done(library, &copy);
705 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
706 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
707 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
708 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
709 int h = min(height, ft_bitmap->rows);
711 while (h--) {
712 memcpy(dst, src, w);
713 src += ft_bitmap->pitch;
714 dst += bitmap->pitch;
717 else
718 FIXME("format %x not handled\n", glyph->format);
720 return TRUE;
723 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
725 const RECT *bbox = &bitmap->bbox;
726 int width = bbox->right - bbox->left;
727 int height = bbox->bottom - bbox->top;
728 BOOL ret = FALSE;
730 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
731 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
732 const FT_Outline *src = &outline->outline;
733 FT_Bitmap ft_bitmap;
734 FT_Outline copy;
736 ft_bitmap.width = width;
737 ft_bitmap.rows = height;
738 ft_bitmap.pitch = bitmap->pitch;
739 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
740 ft_bitmap.buffer = bitmap->buf;
742 /* Note: FreeType will only set 'black' bits for us. */
743 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
744 pFT_Outline_Copy(src, &copy);
745 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
746 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
747 pFT_Outline_Done(library, &copy);
750 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
751 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
752 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
753 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
754 int h = min(height, ft_bitmap->rows);
756 while (h--) {
757 memcpy(dst, src, w);
758 src += ft_bitmap->pitch;
759 dst += bitmap->pitch;
762 ret = TRUE;
764 else
765 FIXME("format %x not handled\n", glyph->format);
767 return ret;
770 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
772 FTC_ImageTypeRec imagetype;
773 BOOL needs_transform;
774 BOOL ret = FALSE;
775 FT_Glyph glyph;
776 FT_Matrix m;
778 EnterCriticalSection(&freetype_cs);
780 needs_transform = get_glyph_transform(bitmap, &m);
782 imagetype.face_id = bitmap->fontface;
783 imagetype.width = 0;
784 imagetype.height = bitmap->emsize;
785 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
787 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->glyph, &glyph, NULL) == 0) {
788 FT_Glyph glyph_copy;
790 if (needs_transform) {
791 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
792 if (bitmap->simulations & DWRITE_FONT_SIMULATIONS_BOLD)
793 embolden_glyph(glyph_copy, bitmap->emsize);
795 /* Includes oblique and user transform. */
796 pFT_Glyph_Transform(glyph_copy, &m, NULL);
797 glyph = glyph_copy;
800 else
801 glyph_copy = NULL;
803 if (bitmap->aliased)
804 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
805 else
806 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
808 if (glyph_copy)
809 pFT_Done_Glyph(glyph_copy);
812 LeaveCriticalSection(&freetype_cs);
814 return ret;
817 INT32 freetype_get_glyph_advance(IDWriteFontFace5 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
818 BOOL *has_contours)
820 FTC_ImageTypeRec imagetype;
821 FT_Glyph glyph;
822 INT32 advance;
824 imagetype.face_id = fontface;
825 imagetype.width = 0;
826 imagetype.height = emSize;
827 imagetype.flags = FT_LOAD_DEFAULT;
828 if (mode == DWRITE_MEASURING_MODE_NATURAL)
829 imagetype.flags |= FT_LOAD_NO_HINTING;
831 EnterCriticalSection(&freetype_cs);
832 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0) {
833 *has_contours = glyph->format == FT_GLYPH_FORMAT_OUTLINE && ((FT_OutlineGlyph)glyph)->outline.n_contours;
834 advance = glyph->advance.x >> 16;
836 else {
837 *has_contours = FALSE;
838 advance = 0;
840 LeaveCriticalSection(&freetype_cs);
842 return advance;
845 #else /* HAVE_FREETYPE */
847 BOOL init_freetype(void)
849 return FALSE;
852 void release_freetype(void)
856 void freetype_notify_cacheremove(IDWriteFontFace5 *fontface)
860 HRESULT freetype_get_design_glyph_metrics(struct dwrite_fontface *fontface, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
862 return E_NOTIMPL;
865 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace5 *fontface, float emSize, UINT16 const *glyphs,
866 float const *advances, DWRITE_GLYPH_OFFSET const *offsets, unsigned int count, BOOL is_rtl,
867 IDWriteGeometrySink *sink)
869 return E_NOTIMPL;
872 UINT16 freetype_get_glyphcount(IDWriteFontFace5 *fontface)
874 return 0;
877 BOOL freetype_has_kerning_pairs(IDWriteFontFace5 *fontface)
879 return FALSE;
882 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace5 *fontface, UINT16 left, UINT16 right)
884 return 0;
887 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
889 SetRectEmpty(&bitmap->bbox);
892 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
894 return FALSE;
897 INT32 freetype_get_glyph_advance(IDWriteFontFace5 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode,
898 BOOL *has_contours)
900 *has_contours = FALSE;
901 return 0;
904 #endif /* HAVE_FREETYPE */