ddraw: Return early in d3d_device7_DrawIndexedPrimitiveStrided() with a 0 vertex...
[wine.git] / dlls / dwrite / freetype.c
blobec855791eb38974d160455588e8da93025a5a8cb
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 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_Decompose)
167 LOAD_FUNCPTR(FT_Outline_Done)
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_CMapCache_Lookup)
173 LOAD_FUNCPTR(FTC_CMapCache_New)
174 LOAD_FUNCPTR(FTC_ImageCache_Lookup)
175 LOAD_FUNCPTR(FTC_ImageCache_New)
176 LOAD_FUNCPTR(FTC_Manager_New)
177 LOAD_FUNCPTR(FTC_Manager_Done)
178 LOAD_FUNCPTR(FTC_Manager_LookupFace)
179 LOAD_FUNCPTR(FTC_Manager_LookupSize)
180 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
181 #undef LOAD_FUNCPTR
183 if (pFT_Init_FreeType(&library) != 0) {
184 ERR("Can't init FreeType library\n");
185 wine_dlclose(ft_handle, NULL, 0);
186 ft_handle = NULL;
187 return FALSE;
189 pFT_Library_Version(library, &FT_Version.major, &FT_Version.minor, &FT_Version.patch);
191 /* init cache manager */
192 if (pFTC_Manager_New(library, 0, 0, 0, &face_requester, NULL, &cache_manager) != 0 ||
193 pFTC_CMapCache_New(cache_manager, &cmap_cache) != 0 ||
194 pFTC_ImageCache_New(cache_manager, &image_cache) != 0) {
196 ERR("Failed to init FreeType cache\n");
197 pFTC_Manager_Done(cache_manager);
198 pFT_Done_FreeType(library);
199 wine_dlclose(ft_handle, NULL, 0);
200 ft_handle = NULL;
201 return FALSE;
204 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
205 return TRUE;
207 sym_not_found:
208 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
209 wine_dlclose(ft_handle, NULL, 0);
210 ft_handle = NULL;
211 return FALSE;
214 void release_freetype(void)
216 pFTC_Manager_Done(cache_manager);
217 pFT_Done_FreeType(library);
220 void freetype_notify_cacheremove(IDWriteFontFace3 *fontface)
222 EnterCriticalSection(&freetype_cs);
223 pFTC_Manager_RemoveFaceID(cache_manager, fontface);
224 LeaveCriticalSection(&freetype_cs);
227 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace3 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
229 FTC_ScalerRec scaler;
230 FT_Size size;
232 scaler.face_id = fontface;
233 scaler.width = unitsperEm;
234 scaler.height = unitsperEm;
235 scaler.pixel = 1;
236 scaler.x_res = 0;
237 scaler.y_res = 0;
239 EnterCriticalSection(&freetype_cs);
240 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
241 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
242 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
244 ret->leftSideBearing = metrics->horiBearingX;
245 ret->advanceWidth = metrics->horiAdvance;
246 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
247 ret->topSideBearing = metrics->vertBearingY;
248 ret->advanceHeight = metrics->vertAdvance;
249 ret->bottomSideBearing = metrics->vertAdvance - metrics->vertBearingY - metrics->height;
250 ret->verticalOriginY = metrics->height + metrics->vertBearingY;
253 LeaveCriticalSection(&freetype_cs);
255 return S_OK;
258 BOOL freetype_is_monospaced(IDWriteFontFace3 *fontface)
260 BOOL is_monospaced = FALSE;
261 FT_Face face;
263 EnterCriticalSection(&freetype_cs);
264 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
265 is_monospaced = !!FT_IS_FIXED_WIDTH(face);
266 LeaveCriticalSection(&freetype_cs);
268 return is_monospaced;
271 struct decompose_context {
272 IDWriteGeometrySink *sink;
273 FLOAT xoffset;
274 FLOAT yoffset;
275 BOOL figure_started;
276 BOOL figure_closed;
277 BOOL move_to; /* last call was 'move_to' */
278 FT_Vector origin; /* 'pen' position from last call */
281 static inline void ft_vector_to_d2d_point(const FT_Vector *v, FLOAT xoffset, FLOAT yoffset, D2D1_POINT_2F *p)
283 p->x = (v->x / 64.0f) + xoffset;
284 p->y = (v->y / 64.0f) + yoffset;
287 static int decompose_move_to(const FT_Vector *to, void *user)
289 struct decompose_context *ctxt = (struct decompose_context*)user;
290 D2D1_POINT_2F point;
292 if (ctxt->figure_started) {
293 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
294 ctxt->figure_closed = TRUE;
296 else
297 ctxt->figure_closed = FALSE;
298 ctxt->figure_started = TRUE;
300 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
301 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt->sink, point, D2D1_FIGURE_BEGIN_FILLED);
302 ctxt->move_to = TRUE;
303 ctxt->origin = *to;
304 return 0;
307 static int decompose_line_to(const FT_Vector *to, void *user)
309 struct decompose_context *ctxt = (struct decompose_context*)user;
310 /* special case for empty contours, in a way freetype returns them */
311 if (ctxt->move_to && !memcmp(to, &ctxt->origin, sizeof(*to))) {
312 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
313 ctxt->figure_closed = TRUE;
315 else {
316 D2D1_POINT_2F point;
317 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
318 ID2D1SimplifiedGeometrySink_AddLines(ctxt->sink, &point, 1);
319 ctxt->figure_closed = FALSE;
321 ctxt->move_to = FALSE;
322 ctxt->origin = *to;
323 return 0;
326 static int decompose_conic_to(const FT_Vector *control, const FT_Vector *to, void *user)
328 struct decompose_context *ctxt = (struct decompose_context*)user;
329 D2D1_POINT_2F points[3];
330 FT_Vector cubic[3];
332 /* convert from quadratic to cubic */
335 The parametric eqn for a cubic Bezier is, from PLRM:
336 r(t) = at^3 + bt^2 + ct + r0
337 with the control points:
338 r1 = r0 + c/3
339 r2 = r1 + (c + b)/3
340 r3 = r0 + c + b + a
342 A quadratic Bezier has the form:
343 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
345 So equating powers of t leads to:
346 r1 = 2/3 p1 + 1/3 p0
347 r2 = 2/3 p1 + 1/3 p2
348 and of course r0 = p0, r3 = p2
351 /* r1 = 1/3 p0 + 2/3 p1
352 r2 = 1/3 p2 + 2/3 p1 */
353 cubic[0].x = (2 * control->x + 1) / 3;
354 cubic[0].y = (2 * control->y + 1) / 3;
355 cubic[1] = cubic[0];
356 cubic[0].x += (ctxt->origin.x + 1) / 3;
357 cubic[0].y += (ctxt->origin.y + 1) / 3;
358 cubic[1].x += (to->x + 1) / 3;
359 cubic[1].y += (to->y + 1) / 3;
360 cubic[2] = *to;
362 ft_vector_to_d2d_point(cubic, ctxt->xoffset, ctxt->yoffset, points);
363 ft_vector_to_d2d_point(cubic + 1, ctxt->xoffset, ctxt->yoffset, points + 1);
364 ft_vector_to_d2d_point(cubic + 2, ctxt->xoffset, ctxt->yoffset, points + 2);
365 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
366 ctxt->figure_closed = FALSE;
367 ctxt->move_to = FALSE;
368 ctxt->origin = *to;
369 return 0;
372 static int decompose_cubic_to(const FT_Vector *control1, const FT_Vector *control2,
373 const FT_Vector *to, void *user)
375 struct decompose_context *ctxt = (struct decompose_context*)user;
376 D2D1_POINT_2F points[3];
378 ft_vector_to_d2d_point(control1, ctxt->xoffset, ctxt->yoffset, points);
379 ft_vector_to_d2d_point(control2, ctxt->xoffset, ctxt->yoffset, points + 1);
380 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, points + 2);
381 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
382 ctxt->figure_closed = FALSE;
383 ctxt->move_to = FALSE;
384 ctxt->origin = *to;
385 return 0;
388 static void decompose_outline(FT_Outline *outline, FLOAT xoffset, FLOAT yoffset, IDWriteGeometrySink *sink)
390 static const FT_Outline_Funcs decompose_funcs = {
391 decompose_move_to,
392 decompose_line_to,
393 decompose_conic_to,
394 decompose_cubic_to,
398 struct decompose_context context;
400 context.sink = sink;
401 context.xoffset = xoffset;
402 context.yoffset = yoffset;
403 context.figure_started = FALSE;
404 context.figure_closed = FALSE;
405 context.move_to = FALSE;
406 context.origin.x = 0;
407 context.origin.y = 0;
409 pFT_Outline_Decompose(outline, &decompose_funcs, &context);
411 if (!context.figure_closed && outline->n_points)
412 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
415 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace3 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
416 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
418 FTC_ScalerRec scaler;
419 USHORT simulations;
420 HRESULT hr = S_OK;
421 FT_Size size;
423 if (!count)
424 return S_OK;
426 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_WINDING);
428 simulations = IDWriteFontFace3_GetSimulations(fontface);
430 scaler.face_id = fontface;
431 scaler.width = emSize;
432 scaler.height = emSize;
433 scaler.pixel = 1;
434 scaler.x_res = 0;
435 scaler.y_res = 0;
437 EnterCriticalSection(&freetype_cs);
438 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
439 FLOAT advance = 0.0f;
440 UINT32 g;
442 for (g = 0; g < count; g++) {
443 if (pFT_Load_Glyph(size->face, glyphs[g], FT_LOAD_NO_BITMAP) == 0) {
444 FLOAT ft_advance = size->face->glyph->metrics.horiAdvance >> 6;
445 FT_Outline *outline = &size->face->glyph->outline;
446 FLOAT xoffset = 0.0f, yoffset = 0.0f;
447 FT_Matrix m;
449 m.xx = 1 << 16;
450 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
451 m.yx = 0;
452 m.yy = -(1 << 16); /* flip Y axis */
454 pFT_Outline_Transform(outline, &m);
456 /* glyph offsets act as current glyph adjustment */
457 if (offsets) {
458 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
459 yoffset -= offsets[g].ascenderOffset;
462 if (g == 0 && is_rtl)
463 advance = advances ? -advances[g] : -ft_advance;
465 xoffset += advance;
466 decompose_outline(outline, xoffset, yoffset, sink);
468 /* update advance to next glyph */
469 if (advances)
470 advance += is_rtl ? -advances[g] : advances[g];
471 else
472 advance += is_rtl ? -ft_advance : ft_advance;
476 else
477 hr = E_FAIL;
478 LeaveCriticalSection(&freetype_cs);
480 return hr;
483 UINT16 freetype_get_glyphcount(IDWriteFontFace3 *fontface)
485 UINT16 count = 0;
486 FT_Face face;
488 EnterCriticalSection(&freetype_cs);
489 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
490 count = face->num_glyphs;
491 LeaveCriticalSection(&freetype_cs);
493 return count;
496 void freetype_get_glyphs(IDWriteFontFace3 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
497 UINT16 *glyphs)
499 UINT32 i;
501 EnterCriticalSection(&freetype_cs);
502 for (i = 0; i < count; i++) {
503 if (charmap == -1)
504 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoints[i]);
505 else {
506 UINT32 codepoint = codepoints[i];
507 /* special handling for symbol fonts */
508 if (codepoint < 0x100) codepoint += 0xf000;
509 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint);
510 if (!glyphs[i])
511 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint - 0xf000);
514 LeaveCriticalSection(&freetype_cs);
517 BOOL freetype_has_kerning_pairs(IDWriteFontFace3 *fontface)
519 BOOL has_kerning_pairs = FALSE;
520 FT_Face face;
522 EnterCriticalSection(&freetype_cs);
523 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
524 has_kerning_pairs = !!FT_HAS_KERNING(face);
525 LeaveCriticalSection(&freetype_cs);
527 return has_kerning_pairs;
530 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace3 *fontface, UINT16 left, UINT16 right)
532 INT32 adjustment = 0;
533 FT_Face face;
535 EnterCriticalSection(&freetype_cs);
536 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
537 FT_Vector kern;
538 if (FT_HAS_KERNING(face)) {
539 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
540 adjustment = kern.x;
543 LeaveCriticalSection(&freetype_cs);
545 return adjustment;
548 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
550 ft_matrix->xx = m->m11 * 0x10000;
551 ft_matrix->xy = -m->m21 * 0x10000;
552 ft_matrix->yx = -m->m12 * 0x10000;
553 ft_matrix->yy = m->m22 * 0x10000;
556 /* Should be used only while holding 'freetype_cs' */
557 static BOOL is_face_scalable(IDWriteFontFace3 *fontface)
559 FT_Face face;
560 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
561 return FT_IS_SCALABLE(face);
562 else
563 return FALSE;
566 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
568 FTC_ImageTypeRec imagetype;
569 FT_BBox bbox = { 0 };
570 FT_Glyph glyph;
572 EnterCriticalSection(&freetype_cs);
574 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef,
575 disable transform if that's the case. */
576 if (bitmap->m) {
577 if (!is_face_scalable(bitmap->fontface))
578 bitmap->m = NULL;
581 imagetype.face_id = bitmap->fontface;
582 imagetype.width = 0;
583 imagetype.height = bitmap->emsize;
584 imagetype.flags = bitmap->m ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
586 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
587 if (bitmap->m) {
588 FT_Glyph glyph_copy;
590 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
591 FT_Matrix ft_matrix;
593 ft_matrix_from_dwrite_matrix(bitmap->m, &ft_matrix);
594 pFT_Glyph_Transform(glyph_copy, &ft_matrix, NULL);
595 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
596 pFT_Done_Glyph(glyph_copy);
599 else
600 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
603 LeaveCriticalSection(&freetype_cs);
605 /* flip Y axis */
606 bitmap->bbox.left = bbox.xMin;
607 bitmap->bbox.right = bbox.xMax;
608 bitmap->bbox.top = -bbox.yMax;
609 bitmap->bbox.bottom = -bbox.yMin;
612 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
614 const RECT *bbox = &bitmap->bbox;
615 int width = bbox->right - bbox->left;
616 int height = bbox->bottom - bbox->top;
618 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
619 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
620 const FT_Outline *src = &outline->outline;
621 FT_Bitmap ft_bitmap;
622 FT_Outline copy;
624 ft_bitmap.width = width;
625 ft_bitmap.rows = height;
626 ft_bitmap.pitch = bitmap->pitch;
627 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
628 ft_bitmap.buffer = bitmap->buf;
630 /* Note: FreeType will only set 'black' bits for us. */
631 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
632 pFT_Outline_Copy(src, &copy);
633 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
634 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
635 pFT_Outline_Done(library, &copy);
638 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
639 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
640 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
641 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
642 int h = min(height, ft_bitmap->rows);
644 while (h--) {
645 memcpy(dst, src, w);
646 src += ft_bitmap->pitch;
647 dst += bitmap->pitch;
650 else
651 FIXME("format %x not handled\n", glyph->format);
653 return TRUE;
656 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
658 const RECT *bbox = &bitmap->bbox;
659 int width = bbox->right - bbox->left;
660 int height = bbox->bottom - bbox->top;
661 BOOL ret = FALSE;
663 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
664 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
665 const FT_Outline *src = &outline->outline;
666 FT_Bitmap ft_bitmap;
667 FT_Outline copy;
669 ft_bitmap.width = width;
670 ft_bitmap.rows = height;
671 ft_bitmap.pitch = bitmap->pitch;
672 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
673 ft_bitmap.buffer = bitmap->buf;
675 /* Note: FreeType will only set 'black' bits for us. */
676 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
677 pFT_Outline_Copy(src, &copy);
678 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
679 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
680 pFT_Outline_Done(library, &copy);
683 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
684 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
685 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
686 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
687 int h = min(height, ft_bitmap->rows);
689 while (h--) {
690 memcpy(dst, src, w);
691 src += ft_bitmap->pitch;
692 dst += bitmap->pitch;
695 ret = TRUE;
697 else
698 FIXME("format %x not handled\n", glyph->format);
700 return ret;
703 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
705 FTC_ImageTypeRec imagetype;
706 BOOL ret = FALSE;
707 FT_Glyph glyph;
709 EnterCriticalSection(&freetype_cs);
711 if (bitmap->m) {
712 if (!is_face_scalable(bitmap->fontface))
713 bitmap->m = NULL;
716 imagetype.face_id = bitmap->fontface;
717 imagetype.width = 0;
718 imagetype.height = bitmap->emsize;
719 imagetype.flags = bitmap->m ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
721 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
722 FT_Glyph glyph_copy;
724 if (bitmap->m) {
725 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
726 FT_Matrix ft_matrix;
728 ft_matrix_from_dwrite_matrix(bitmap->m, &ft_matrix);
729 pFT_Glyph_Transform(glyph_copy, &ft_matrix, NULL);
730 glyph = glyph_copy;
733 else
734 glyph_copy = NULL;
736 if (bitmap->type == DWRITE_TEXTURE_CLEARTYPE_3x1)
737 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
738 else
739 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
741 if (glyph_copy)
742 pFT_Done_Glyph(glyph_copy);
745 LeaveCriticalSection(&freetype_cs);
747 return ret;
750 INT freetype_get_charmap_index(IDWriteFontFace3 *fontface, BOOL *is_symbol)
752 INT charmap_index = -1;
753 FT_Face face;
755 *is_symbol = FALSE;
757 EnterCriticalSection(&freetype_cs);
758 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
759 TT_OS2 *os2 = pFT_Get_Sfnt_Table(face, ft_sfnt_os2);
760 FT_Int i;
762 if (os2) {
763 FT_UInt dummy;
764 if (os2->version == 0)
765 *is_symbol = pFT_Get_First_Char(face, &dummy) >= 0x100;
766 else
767 *is_symbol = !!(os2->ulCodePageRange1 & FS_SYMBOL);
770 for (i = 0; i < face->num_charmaps; i++)
771 if (face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL) {
772 *is_symbol = TRUE;
773 charmap_index = i;
774 break;
777 LeaveCriticalSection(&freetype_cs);
779 return charmap_index;
782 INT32 freetype_get_glyph_advance(IDWriteFontFace3 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
784 FTC_ImageTypeRec imagetype;
785 FT_Glyph glyph;
786 INT32 advance;
788 imagetype.face_id = fontface;
789 imagetype.width = 0;
790 imagetype.height = emSize;
791 imagetype.flags = FT_LOAD_DEFAULT;
792 if (mode == DWRITE_MEASURING_MODE_NATURAL)
793 imagetype.flags |= FT_LOAD_NO_HINTING;
795 EnterCriticalSection(&freetype_cs);
796 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0)
797 advance = glyph->advance.x >> 16;
798 else
799 advance = 0;
800 LeaveCriticalSection(&freetype_cs);
802 return advance;
805 #else /* HAVE_FREETYPE */
807 BOOL init_freetype(void)
809 return FALSE;
812 void release_freetype(void)
816 void freetype_notify_cacheremove(IDWriteFontFace3 *fontface)
820 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace3 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
822 return E_NOTIMPL;
825 BOOL freetype_is_monospaced(IDWriteFontFace3 *fontface)
827 return FALSE;
830 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace3 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
831 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
833 return E_NOTIMPL;
836 UINT16 freetype_get_glyphcount(IDWriteFontFace3 *fontface)
838 return 0;
841 void freetype_get_glyphs(IDWriteFontFace3 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
842 UINT16 *glyphs)
844 memset(glyphs, 0, count * sizeof(*glyphs));
847 BOOL freetype_has_kerning_pairs(IDWriteFontFace3 *fontface)
849 return FALSE;
852 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace3 *fontface, UINT16 left, UINT16 right)
854 return 0;
857 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
859 memset(&bitmap->bbox, 0, sizeof(bitmap->bbox));
862 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
864 return FALSE;
867 INT freetype_get_charmap_index(IDWriteFontFace3 *fontface, BOOL *is_symbol)
869 *is_symbol = FALSE;
870 return -1;
873 INT32 freetype_get_glyph_advance(IDWriteFontFace3 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
875 return 0;
878 #endif /* HAVE_FREETYPE */