wined3d: Inverse logic for applying half float vertex format fixups.
[wine.git] / dlls / dwrite / freetype.c
blobb6d785f5e7fafaa0ac886f7436f173b6adb80874
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 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 if (!fontface) {
111 WARN("NULL fontface requested.\n");
112 return FT_Err_Ok;
115 count = 1;
116 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
117 if (FAILED(hr))
118 return FT_Err_Ok;
120 hr = get_filestream_from_file(file, &stream);
121 IDWriteFontFile_Release(file);
122 if (FAILED(hr))
123 return FT_Err_Ok;
125 hr = IDWriteFontFileStream_GetFileSize(stream, &data_size);
126 if (FAILED(hr)) {
127 fterror = FT_Err_Invalid_Stream_Read;
128 goto fail;
131 hr = IDWriteFontFileStream_ReadFileFragment(stream, &data_ptr, 0, data_size, &context);
132 if (FAILED(hr)) {
133 fterror = FT_Err_Invalid_Stream_Read;
134 goto fail;
137 index = IDWriteFontFace_GetIndex(fontface);
138 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
139 IDWriteFontFileStream_ReleaseFileFragment(stream, context);
141 fail:
142 IDWriteFontFileStream_Release(stream);
144 return fterror;
147 BOOL init_freetype(void)
149 FT_Version_t FT_Version;
151 ft_handle = wine_dlopen(SONAME_LIBFREETYPE, RTLD_NOW, NULL, 0);
152 if (!ft_handle) {
153 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
154 return FALSE;
157 #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;}
158 LOAD_FUNCPTR(FT_Done_FreeType)
159 LOAD_FUNCPTR(FT_Done_Glyph)
160 LOAD_FUNCPTR(FT_Get_First_Char)
161 LOAD_FUNCPTR(FT_Get_Kerning)
162 LOAD_FUNCPTR(FT_Get_Sfnt_Table)
163 LOAD_FUNCPTR(FT_Glyph_Copy)
164 LOAD_FUNCPTR(FT_Glyph_Get_CBox)
165 LOAD_FUNCPTR(FT_Glyph_Transform)
166 LOAD_FUNCPTR(FT_Init_FreeType)
167 LOAD_FUNCPTR(FT_Library_Version)
168 LOAD_FUNCPTR(FT_Load_Glyph)
169 LOAD_FUNCPTR(FT_New_Memory_Face)
170 LOAD_FUNCPTR(FT_Outline_Copy)
171 LOAD_FUNCPTR(FT_Outline_Decompose)
172 LOAD_FUNCPTR(FT_Outline_Done)
173 LOAD_FUNCPTR(FT_Outline_Get_Bitmap)
174 LOAD_FUNCPTR(FT_Outline_New)
175 LOAD_FUNCPTR(FT_Outline_Transform)
176 LOAD_FUNCPTR(FT_Outline_Translate)
177 LOAD_FUNCPTR(FTC_CMapCache_Lookup)
178 LOAD_FUNCPTR(FTC_CMapCache_New)
179 LOAD_FUNCPTR(FTC_ImageCache_Lookup)
180 LOAD_FUNCPTR(FTC_ImageCache_New)
181 LOAD_FUNCPTR(FTC_Manager_New)
182 LOAD_FUNCPTR(FTC_Manager_Done)
183 LOAD_FUNCPTR(FTC_Manager_LookupFace)
184 LOAD_FUNCPTR(FTC_Manager_LookupSize)
185 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
186 #undef LOAD_FUNCPTR
188 if (pFT_Init_FreeType(&library) != 0) {
189 ERR("Can't init FreeType library\n");
190 wine_dlclose(ft_handle, NULL, 0);
191 ft_handle = NULL;
192 return FALSE;
194 pFT_Library_Version(library, &FT_Version.major, &FT_Version.minor, &FT_Version.patch);
196 /* init cache manager */
197 if (pFTC_Manager_New(library, 0, 0, 0, &face_requester, NULL, &cache_manager) != 0 ||
198 pFTC_CMapCache_New(cache_manager, &cmap_cache) != 0 ||
199 pFTC_ImageCache_New(cache_manager, &image_cache) != 0) {
201 ERR("Failed to init FreeType cache\n");
202 pFTC_Manager_Done(cache_manager);
203 pFT_Done_FreeType(library);
204 wine_dlclose(ft_handle, NULL, 0);
205 ft_handle = NULL;
206 return FALSE;
209 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
210 return TRUE;
212 sym_not_found:
213 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
214 wine_dlclose(ft_handle, NULL, 0);
215 ft_handle = NULL;
216 return FALSE;
219 void release_freetype(void)
221 pFTC_Manager_Done(cache_manager);
222 pFT_Done_FreeType(library);
225 void freetype_notify_cacheremove(IDWriteFontFace3 *fontface)
227 EnterCriticalSection(&freetype_cs);
228 pFTC_Manager_RemoveFaceID(cache_manager, fontface);
229 LeaveCriticalSection(&freetype_cs);
232 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace3 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
234 FTC_ScalerRec scaler;
235 FT_Size size;
237 scaler.face_id = fontface;
238 scaler.width = unitsperEm;
239 scaler.height = unitsperEm;
240 scaler.pixel = 1;
241 scaler.x_res = 0;
242 scaler.y_res = 0;
244 EnterCriticalSection(&freetype_cs);
245 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
246 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
247 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
249 ret->leftSideBearing = metrics->horiBearingX;
250 ret->advanceWidth = metrics->horiAdvance;
251 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
252 ret->topSideBearing = metrics->vertBearingY;
253 ret->advanceHeight = metrics->vertAdvance;
254 ret->bottomSideBearing = metrics->vertAdvance - metrics->vertBearingY - metrics->height;
255 ret->verticalOriginY = metrics->height + metrics->vertBearingY;
258 LeaveCriticalSection(&freetype_cs);
260 return S_OK;
263 BOOL freetype_is_monospaced(IDWriteFontFace3 *fontface)
265 BOOL is_monospaced = FALSE;
266 FT_Face face;
268 EnterCriticalSection(&freetype_cs);
269 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
270 is_monospaced = !!FT_IS_FIXED_WIDTH(face);
271 LeaveCriticalSection(&freetype_cs);
273 return is_monospaced;
276 struct decompose_context {
277 IDWriteGeometrySink *sink;
278 FLOAT xoffset;
279 FLOAT yoffset;
280 BOOL figure_started;
281 BOOL figure_closed;
282 BOOL move_to; /* last call was 'move_to' */
283 FT_Vector origin; /* 'pen' position from last call */
286 static inline void ft_vector_to_d2d_point(const FT_Vector *v, FLOAT xoffset, FLOAT yoffset, D2D1_POINT_2F *p)
288 p->x = (v->x / 64.0f) + xoffset;
289 p->y = (v->y / 64.0f) + yoffset;
292 static int decompose_move_to(const FT_Vector *to, void *user)
294 struct decompose_context *ctxt = (struct decompose_context*)user;
295 D2D1_POINT_2F point;
297 if (ctxt->figure_started) {
298 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
299 ctxt->figure_closed = TRUE;
301 else
302 ctxt->figure_closed = FALSE;
303 ctxt->figure_started = TRUE;
305 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
306 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt->sink, point, D2D1_FIGURE_BEGIN_FILLED);
307 ctxt->move_to = TRUE;
308 ctxt->origin = *to;
309 return 0;
312 static int decompose_line_to(const FT_Vector *to, void *user)
314 struct decompose_context *ctxt = (struct decompose_context*)user;
315 /* special case for empty contours, in a way freetype returns them */
316 if (ctxt->move_to && !memcmp(to, &ctxt->origin, sizeof(*to))) {
317 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
318 ctxt->figure_closed = TRUE;
320 else {
321 D2D1_POINT_2F point;
322 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
323 ID2D1SimplifiedGeometrySink_AddLines(ctxt->sink, &point, 1);
324 ctxt->figure_closed = FALSE;
326 ctxt->move_to = FALSE;
327 ctxt->origin = *to;
328 return 0;
331 static int decompose_conic_to(const FT_Vector *control, const FT_Vector *to, void *user)
333 struct decompose_context *ctxt = (struct decompose_context*)user;
334 D2D1_POINT_2F points[3];
335 FT_Vector cubic[3];
337 /* convert from quadratic to cubic */
340 The parametric eqn for a cubic Bezier is, from PLRM:
341 r(t) = at^3 + bt^2 + ct + r0
342 with the control points:
343 r1 = r0 + c/3
344 r2 = r1 + (c + b)/3
345 r3 = r0 + c + b + a
347 A quadratic Bezier has the form:
348 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
350 So equating powers of t leads to:
351 r1 = 2/3 p1 + 1/3 p0
352 r2 = 2/3 p1 + 1/3 p2
353 and of course r0 = p0, r3 = p2
356 /* r1 = 1/3 p0 + 2/3 p1
357 r2 = 1/3 p2 + 2/3 p1 */
358 cubic[0].x = (2 * control->x + 1) / 3;
359 cubic[0].y = (2 * control->y + 1) / 3;
360 cubic[1] = cubic[0];
361 cubic[0].x += (ctxt->origin.x + 1) / 3;
362 cubic[0].y += (ctxt->origin.y + 1) / 3;
363 cubic[1].x += (to->x + 1) / 3;
364 cubic[1].y += (to->y + 1) / 3;
365 cubic[2] = *to;
367 ft_vector_to_d2d_point(cubic, ctxt->xoffset, ctxt->yoffset, points);
368 ft_vector_to_d2d_point(cubic + 1, ctxt->xoffset, ctxt->yoffset, points + 1);
369 ft_vector_to_d2d_point(cubic + 2, ctxt->xoffset, ctxt->yoffset, points + 2);
370 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
371 ctxt->figure_closed = FALSE;
372 ctxt->move_to = FALSE;
373 ctxt->origin = *to;
374 return 0;
377 static int decompose_cubic_to(const FT_Vector *control1, const FT_Vector *control2,
378 const FT_Vector *to, void *user)
380 struct decompose_context *ctxt = (struct decompose_context*)user;
381 D2D1_POINT_2F points[3];
383 ft_vector_to_d2d_point(control1, ctxt->xoffset, ctxt->yoffset, points);
384 ft_vector_to_d2d_point(control2, ctxt->xoffset, ctxt->yoffset, points + 1);
385 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, points + 2);
386 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
387 ctxt->figure_closed = FALSE;
388 ctxt->move_to = FALSE;
389 ctxt->origin = *to;
390 return 0;
393 static void decompose_outline(FT_Outline *outline, FLOAT xoffset, FLOAT yoffset, IDWriteGeometrySink *sink)
395 static const FT_Outline_Funcs decompose_funcs = {
396 decompose_move_to,
397 decompose_line_to,
398 decompose_conic_to,
399 decompose_cubic_to,
403 struct decompose_context context;
405 context.sink = sink;
406 context.xoffset = xoffset;
407 context.yoffset = yoffset;
408 context.figure_started = FALSE;
409 context.figure_closed = FALSE;
410 context.move_to = FALSE;
411 context.origin.x = 0;
412 context.origin.y = 0;
414 pFT_Outline_Decompose(outline, &decompose_funcs, &context);
416 if (!context.figure_closed && outline->n_points)
417 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
420 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace3 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
421 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
423 FTC_ScalerRec scaler;
424 USHORT simulations;
425 HRESULT hr = S_OK;
426 FT_Size size;
428 if (!count)
429 return S_OK;
431 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_WINDING);
433 simulations = IDWriteFontFace3_GetSimulations(fontface);
435 scaler.face_id = fontface;
436 scaler.width = emSize;
437 scaler.height = emSize;
438 scaler.pixel = 1;
439 scaler.x_res = 0;
440 scaler.y_res = 0;
442 EnterCriticalSection(&freetype_cs);
443 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
444 FLOAT advance = 0.0f;
445 UINT32 g;
447 for (g = 0; g < count; g++) {
448 if (pFT_Load_Glyph(size->face, glyphs[g], FT_LOAD_NO_BITMAP) == 0) {
449 FLOAT ft_advance = size->face->glyph->metrics.horiAdvance >> 6;
450 FT_Outline *outline = &size->face->glyph->outline;
451 FLOAT xoffset = 0.0f, yoffset = 0.0f;
452 FT_Matrix m;
454 m.xx = 1 << 16;
455 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
456 m.yx = 0;
457 m.yy = -(1 << 16); /* flip Y axis */
459 pFT_Outline_Transform(outline, &m);
461 /* glyph offsets act as current glyph adjustment */
462 if (offsets) {
463 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
464 yoffset -= offsets[g].ascenderOffset;
467 if (g == 0 && is_rtl)
468 advance = advances ? -advances[g] : -ft_advance;
470 xoffset += advance;
471 decompose_outline(outline, xoffset, yoffset, sink);
473 /* update advance to next glyph */
474 if (advances)
475 advance += is_rtl ? -advances[g] : advances[g];
476 else
477 advance += is_rtl ? -ft_advance : ft_advance;
481 else
482 hr = E_FAIL;
483 LeaveCriticalSection(&freetype_cs);
485 return hr;
488 UINT16 freetype_get_glyphcount(IDWriteFontFace3 *fontface)
490 UINT16 count = 0;
491 FT_Face face;
493 EnterCriticalSection(&freetype_cs);
494 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
495 count = face->num_glyphs;
496 LeaveCriticalSection(&freetype_cs);
498 return count;
501 void freetype_get_glyphs(IDWriteFontFace3 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
502 UINT16 *glyphs)
504 UINT32 i;
506 EnterCriticalSection(&freetype_cs);
507 for (i = 0; i < count; i++) {
508 if (charmap == -1)
509 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoints[i]);
510 else {
511 UINT32 codepoint = codepoints[i];
512 /* special handling for symbol fonts */
513 if (codepoint < 0x100) codepoint += 0xf000;
514 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint);
515 if (!glyphs[i])
516 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint - 0xf000);
519 LeaveCriticalSection(&freetype_cs);
522 BOOL freetype_has_kerning_pairs(IDWriteFontFace3 *fontface)
524 BOOL has_kerning_pairs = FALSE;
525 FT_Face face;
527 EnterCriticalSection(&freetype_cs);
528 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
529 has_kerning_pairs = !!FT_HAS_KERNING(face);
530 LeaveCriticalSection(&freetype_cs);
532 return has_kerning_pairs;
535 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace3 *fontface, UINT16 left, UINT16 right)
537 INT32 adjustment = 0;
538 FT_Face face;
540 EnterCriticalSection(&freetype_cs);
541 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
542 FT_Vector kern;
543 if (FT_HAS_KERNING(face)) {
544 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
545 adjustment = kern.x;
548 LeaveCriticalSection(&freetype_cs);
550 return adjustment;
553 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
555 ft_matrix->xx = m->m11 * 0x10000;
556 ft_matrix->xy = -m->m21 * 0x10000;
557 ft_matrix->yx = -m->m12 * 0x10000;
558 ft_matrix->yy = m->m22 * 0x10000;
561 /* Should be used only while holding 'freetype_cs' */
562 static BOOL is_face_scalable(IDWriteFontFace3 *fontface)
564 FT_Face face;
565 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
566 return FT_IS_SCALABLE(face);
567 else
568 return FALSE;
571 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
573 FTC_ImageTypeRec imagetype;
574 FT_BBox bbox = { 0 };
575 FT_Glyph glyph;
577 EnterCriticalSection(&freetype_cs);
579 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef,
580 disable transform if that's the case. */
581 if (bitmap->m) {
582 if (!is_face_scalable(bitmap->fontface))
583 bitmap->m = NULL;
586 imagetype.face_id = bitmap->fontface;
587 imagetype.width = 0;
588 imagetype.height = bitmap->emsize;
589 imagetype.flags = bitmap->m ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
591 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
592 if (bitmap->m) {
593 FT_Glyph glyph_copy;
595 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
596 FT_Matrix ft_matrix;
598 ft_matrix_from_dwrite_matrix(bitmap->m, &ft_matrix);
599 pFT_Glyph_Transform(glyph_copy, &ft_matrix, NULL);
600 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
601 pFT_Done_Glyph(glyph_copy);
604 else
605 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
608 LeaveCriticalSection(&freetype_cs);
610 /* flip Y axis */
611 SetRect(&bitmap->bbox, bbox.xMin, -bbox.yMax, bbox.xMax, -bbox.yMin);
614 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
616 const RECT *bbox = &bitmap->bbox;
617 int width = bbox->right - bbox->left;
618 int height = bbox->bottom - bbox->top;
620 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
621 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
622 const FT_Outline *src = &outline->outline;
623 FT_Bitmap ft_bitmap;
624 FT_Outline copy;
626 ft_bitmap.width = width;
627 ft_bitmap.rows = height;
628 ft_bitmap.pitch = bitmap->pitch;
629 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
630 ft_bitmap.buffer = bitmap->buf;
632 /* Note: FreeType will only set 'black' bits for us. */
633 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
634 pFT_Outline_Copy(src, &copy);
635 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
636 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
637 pFT_Outline_Done(library, &copy);
640 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
641 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
642 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
643 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
644 int h = min(height, ft_bitmap->rows);
646 while (h--) {
647 memcpy(dst, src, w);
648 src += ft_bitmap->pitch;
649 dst += bitmap->pitch;
652 else
653 FIXME("format %x not handled\n", glyph->format);
655 return TRUE;
658 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
660 const RECT *bbox = &bitmap->bbox;
661 int width = bbox->right - bbox->left;
662 int height = bbox->bottom - bbox->top;
663 BOOL ret = FALSE;
665 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
666 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
667 const FT_Outline *src = &outline->outline;
668 FT_Bitmap ft_bitmap;
669 FT_Outline copy;
671 ft_bitmap.width = width;
672 ft_bitmap.rows = height;
673 ft_bitmap.pitch = bitmap->pitch;
674 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
675 ft_bitmap.buffer = bitmap->buf;
677 /* Note: FreeType will only set 'black' bits for us. */
678 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
679 pFT_Outline_Copy(src, &copy);
680 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
681 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
682 pFT_Outline_Done(library, &copy);
685 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
686 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
687 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
688 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
689 int h = min(height, ft_bitmap->rows);
691 while (h--) {
692 memcpy(dst, src, w);
693 src += ft_bitmap->pitch;
694 dst += bitmap->pitch;
697 ret = TRUE;
699 else
700 FIXME("format %x not handled\n", glyph->format);
702 return ret;
705 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
707 FTC_ImageTypeRec imagetype;
708 BOOL ret = FALSE;
709 FT_Glyph glyph;
711 EnterCriticalSection(&freetype_cs);
713 if (bitmap->m) {
714 if (!is_face_scalable(bitmap->fontface))
715 bitmap->m = NULL;
718 imagetype.face_id = bitmap->fontface;
719 imagetype.width = 0;
720 imagetype.height = bitmap->emsize;
721 imagetype.flags = bitmap->m ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
723 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
724 FT_Glyph glyph_copy;
726 if (bitmap->m) {
727 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
728 FT_Matrix ft_matrix;
730 ft_matrix_from_dwrite_matrix(bitmap->m, &ft_matrix);
731 pFT_Glyph_Transform(glyph_copy, &ft_matrix, NULL);
732 glyph = glyph_copy;
735 else
736 glyph_copy = NULL;
738 if (bitmap->type == DWRITE_TEXTURE_CLEARTYPE_3x1)
739 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
740 else
741 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
743 if (glyph_copy)
744 pFT_Done_Glyph(glyph_copy);
747 LeaveCriticalSection(&freetype_cs);
749 return ret;
752 INT freetype_get_charmap_index(IDWriteFontFace3 *fontface, BOOL *is_symbol)
754 INT charmap_index = -1;
755 FT_Face face;
757 *is_symbol = FALSE;
759 EnterCriticalSection(&freetype_cs);
760 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
761 TT_OS2 *os2 = pFT_Get_Sfnt_Table(face, ft_sfnt_os2);
762 FT_Int i;
764 if (os2) {
765 FT_UInt dummy;
766 if (os2->version == 0)
767 *is_symbol = pFT_Get_First_Char(face, &dummy) >= 0x100;
768 else
769 *is_symbol = !!(os2->ulCodePageRange1 & FS_SYMBOL);
772 for (i = 0; i < face->num_charmaps; i++)
773 if (face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL) {
774 *is_symbol = TRUE;
775 charmap_index = i;
776 break;
779 LeaveCriticalSection(&freetype_cs);
781 return charmap_index;
784 INT32 freetype_get_glyph_advance(IDWriteFontFace3 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
786 FTC_ImageTypeRec imagetype;
787 FT_Glyph glyph;
788 INT32 advance;
790 imagetype.face_id = fontface;
791 imagetype.width = 0;
792 imagetype.height = emSize;
793 imagetype.flags = FT_LOAD_DEFAULT;
794 if (mode == DWRITE_MEASURING_MODE_NATURAL)
795 imagetype.flags |= FT_LOAD_NO_HINTING;
797 EnterCriticalSection(&freetype_cs);
798 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0)
799 advance = glyph->advance.x >> 16;
800 else
801 advance = 0;
802 LeaveCriticalSection(&freetype_cs);
804 return advance;
807 #else /* HAVE_FREETYPE */
809 BOOL init_freetype(void)
811 return FALSE;
814 void release_freetype(void)
818 void freetype_notify_cacheremove(IDWriteFontFace3 *fontface)
822 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace3 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
824 return E_NOTIMPL;
827 BOOL freetype_is_monospaced(IDWriteFontFace3 *fontface)
829 return FALSE;
832 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace3 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
833 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
835 return E_NOTIMPL;
838 UINT16 freetype_get_glyphcount(IDWriteFontFace3 *fontface)
840 return 0;
843 void freetype_get_glyphs(IDWriteFontFace3 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
844 UINT16 *glyphs)
846 memset(glyphs, 0, count * sizeof(*glyphs));
849 BOOL freetype_has_kerning_pairs(IDWriteFontFace3 *fontface)
851 return FALSE;
854 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace3 *fontface, UINT16 left, UINT16 right)
856 return 0;
859 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
861 memset(&bitmap->bbox, 0, sizeof(bitmap->bbox));
864 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
866 return FALSE;
869 INT freetype_get_charmap_index(IDWriteFontFace3 *fontface, BOOL *is_symbol)
871 *is_symbol = FALSE;
872 return -1;
875 INT32 freetype_get_glyph_advance(IDWriteFontFace3 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
877 return 0;
880 #endif /* HAVE_FREETYPE */