dwrite: Added support for oblique simulation in bitmap rendering mode.
[wine.git] / dlls / dwrite / freetype.c
blob35a7ebfd892ccd963ac7e96a6a44f0bdc03dda9f
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_Matrix_Multiply);
78 MAKE_FUNCPTR(FT_New_Memory_Face);
79 MAKE_FUNCPTR(FT_Outline_Copy);
80 MAKE_FUNCPTR(FT_Outline_Decompose);
81 MAKE_FUNCPTR(FT_Outline_Done);
82 MAKE_FUNCPTR(FT_Outline_Get_Bitmap);
83 MAKE_FUNCPTR(FT_Outline_New);
84 MAKE_FUNCPTR(FT_Outline_Transform);
85 MAKE_FUNCPTR(FT_Outline_Translate);
86 MAKE_FUNCPTR(FTC_CMapCache_Lookup);
87 MAKE_FUNCPTR(FTC_CMapCache_New);
88 MAKE_FUNCPTR(FTC_ImageCache_Lookup);
89 MAKE_FUNCPTR(FTC_ImageCache_New);
90 MAKE_FUNCPTR(FTC_Manager_New);
91 MAKE_FUNCPTR(FTC_Manager_Done);
92 MAKE_FUNCPTR(FTC_Manager_LookupFace);
93 MAKE_FUNCPTR(FTC_Manager_LookupSize);
94 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID);
95 #undef MAKE_FUNCPTR
97 struct face_finalizer_data
99 IDWriteFontFileStream *stream;
100 void *context;
103 static void face_finalizer(void *object)
105 FT_Face face = object;
106 struct face_finalizer_data *data = (struct face_finalizer_data *)face->generic.data;
108 IDWriteFontFileStream_ReleaseFileFragment(data->stream, data->context);
109 IDWriteFontFileStream_Release(data->stream);
110 heap_free(data);
113 static FT_Error face_requester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *face)
115 IDWriteFontFace *fontface = (IDWriteFontFace*)face_id;
116 IDWriteFontFileStream *stream;
117 IDWriteFontFile *file;
118 const void *data_ptr;
119 UINT32 index, count;
120 FT_Error fterror;
121 UINT64 data_size;
122 void *context;
123 HRESULT hr;
125 *face = NULL;
127 if (!fontface) {
128 WARN("NULL fontface requested.\n");
129 return FT_Err_Ok;
132 count = 1;
133 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
134 if (FAILED(hr))
135 return FT_Err_Ok;
137 hr = get_filestream_from_file(file, &stream);
138 IDWriteFontFile_Release(file);
139 if (FAILED(hr))
140 return FT_Err_Ok;
142 hr = IDWriteFontFileStream_GetFileSize(stream, &data_size);
143 if (FAILED(hr)) {
144 fterror = FT_Err_Invalid_Stream_Read;
145 goto fail;
148 hr = IDWriteFontFileStream_ReadFileFragment(stream, &data_ptr, 0, data_size, &context);
149 if (FAILED(hr)) {
150 fterror = FT_Err_Invalid_Stream_Read;
151 goto fail;
154 index = IDWriteFontFace_GetIndex(fontface);
155 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
156 if (fterror == FT_Err_Ok) {
157 struct face_finalizer_data *data;
159 data = heap_alloc(sizeof(*data));
160 data->stream = stream;
161 data->context = context;
163 (*face)->generic.data = data;
164 (*face)->generic.finalizer = face_finalizer;
165 return fterror;
167 else
168 IDWriteFontFileStream_ReleaseFileFragment(stream, context);
170 fail:
171 IDWriteFontFileStream_Release(stream);
173 return fterror;
176 BOOL init_freetype(void)
178 FT_Version_t FT_Version;
180 ft_handle = wine_dlopen(SONAME_LIBFREETYPE, RTLD_NOW, NULL, 0);
181 if (!ft_handle) {
182 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
183 return FALSE;
186 #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;}
187 LOAD_FUNCPTR(FT_Done_FreeType)
188 LOAD_FUNCPTR(FT_Done_Glyph)
189 LOAD_FUNCPTR(FT_Get_First_Char)
190 LOAD_FUNCPTR(FT_Get_Kerning)
191 LOAD_FUNCPTR(FT_Get_Sfnt_Table)
192 LOAD_FUNCPTR(FT_Glyph_Copy)
193 LOAD_FUNCPTR(FT_Glyph_Get_CBox)
194 LOAD_FUNCPTR(FT_Glyph_Transform)
195 LOAD_FUNCPTR(FT_Init_FreeType)
196 LOAD_FUNCPTR(FT_Library_Version)
197 LOAD_FUNCPTR(FT_Load_Glyph)
198 LOAD_FUNCPTR(FT_Matrix_Multiply)
199 LOAD_FUNCPTR(FT_New_Memory_Face)
200 LOAD_FUNCPTR(FT_Outline_Copy)
201 LOAD_FUNCPTR(FT_Outline_Decompose)
202 LOAD_FUNCPTR(FT_Outline_Done)
203 LOAD_FUNCPTR(FT_Outline_Get_Bitmap)
204 LOAD_FUNCPTR(FT_Outline_New)
205 LOAD_FUNCPTR(FT_Outline_Transform)
206 LOAD_FUNCPTR(FT_Outline_Translate)
207 LOAD_FUNCPTR(FTC_CMapCache_Lookup)
208 LOAD_FUNCPTR(FTC_CMapCache_New)
209 LOAD_FUNCPTR(FTC_ImageCache_Lookup)
210 LOAD_FUNCPTR(FTC_ImageCache_New)
211 LOAD_FUNCPTR(FTC_Manager_New)
212 LOAD_FUNCPTR(FTC_Manager_Done)
213 LOAD_FUNCPTR(FTC_Manager_LookupFace)
214 LOAD_FUNCPTR(FTC_Manager_LookupSize)
215 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
216 #undef LOAD_FUNCPTR
218 if (pFT_Init_FreeType(&library) != 0) {
219 ERR("Can't init FreeType library\n");
220 wine_dlclose(ft_handle, NULL, 0);
221 ft_handle = NULL;
222 return FALSE;
224 pFT_Library_Version(library, &FT_Version.major, &FT_Version.minor, &FT_Version.patch);
226 /* init cache manager */
227 if (pFTC_Manager_New(library, 0, 0, 0, &face_requester, NULL, &cache_manager) != 0 ||
228 pFTC_CMapCache_New(cache_manager, &cmap_cache) != 0 ||
229 pFTC_ImageCache_New(cache_manager, &image_cache) != 0) {
231 ERR("Failed to init FreeType cache\n");
232 pFTC_Manager_Done(cache_manager);
233 pFT_Done_FreeType(library);
234 wine_dlclose(ft_handle, NULL, 0);
235 ft_handle = NULL;
236 return FALSE;
239 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
240 return TRUE;
242 sym_not_found:
243 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
244 wine_dlclose(ft_handle, NULL, 0);
245 ft_handle = NULL;
246 return FALSE;
249 void release_freetype(void)
251 pFTC_Manager_Done(cache_manager);
252 pFT_Done_FreeType(library);
255 void freetype_notify_cacheremove(IDWriteFontFace4 *fontface)
257 EnterCriticalSection(&freetype_cs);
258 pFTC_Manager_RemoveFaceID(cache_manager, fontface);
259 LeaveCriticalSection(&freetype_cs);
262 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
264 FTC_ScalerRec scaler;
265 FT_Size size;
267 scaler.face_id = fontface;
268 scaler.width = unitsperEm;
269 scaler.height = unitsperEm;
270 scaler.pixel = 1;
271 scaler.x_res = 0;
272 scaler.y_res = 0;
274 EnterCriticalSection(&freetype_cs);
275 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
276 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
277 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
279 ret->leftSideBearing = metrics->horiBearingX;
280 ret->advanceWidth = metrics->horiAdvance;
281 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
282 ret->topSideBearing = metrics->vertBearingY;
283 ret->advanceHeight = metrics->vertAdvance;
284 ret->bottomSideBearing = metrics->vertAdvance - metrics->vertBearingY - metrics->height;
285 ret->verticalOriginY = metrics->height + metrics->vertBearingY;
288 LeaveCriticalSection(&freetype_cs);
290 return S_OK;
293 BOOL freetype_is_monospaced(IDWriteFontFace4 *fontface)
295 BOOL is_monospaced = FALSE;
296 FT_Face face;
298 EnterCriticalSection(&freetype_cs);
299 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
300 is_monospaced = !!FT_IS_FIXED_WIDTH(face);
301 LeaveCriticalSection(&freetype_cs);
303 return is_monospaced;
306 struct decompose_context {
307 IDWriteGeometrySink *sink;
308 FLOAT xoffset;
309 FLOAT yoffset;
310 BOOL figure_started;
311 BOOL figure_closed;
312 BOOL move_to; /* last call was 'move_to' */
313 FT_Vector origin; /* 'pen' position from last call */
316 static inline void ft_vector_to_d2d_point(const FT_Vector *v, FLOAT xoffset, FLOAT yoffset, D2D1_POINT_2F *p)
318 p->x = (v->x / 64.0f) + xoffset;
319 p->y = (v->y / 64.0f) + yoffset;
322 static int decompose_move_to(const FT_Vector *to, void *user)
324 struct decompose_context *ctxt = (struct decompose_context*)user;
325 D2D1_POINT_2F point;
327 if (ctxt->figure_started) {
328 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
329 ctxt->figure_closed = TRUE;
331 else
332 ctxt->figure_closed = FALSE;
333 ctxt->figure_started = TRUE;
335 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
336 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt->sink, point, D2D1_FIGURE_BEGIN_FILLED);
337 ctxt->move_to = TRUE;
338 ctxt->origin = *to;
339 return 0;
342 static int decompose_line_to(const FT_Vector *to, void *user)
344 struct decompose_context *ctxt = (struct decompose_context*)user;
345 /* special case for empty contours, in a way freetype returns them */
346 if (ctxt->move_to && !memcmp(to, &ctxt->origin, sizeof(*to))) {
347 ID2D1SimplifiedGeometrySink_EndFigure(ctxt->sink, D2D1_FIGURE_END_CLOSED);
348 ctxt->figure_closed = TRUE;
350 else {
351 D2D1_POINT_2F point;
352 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, &point);
353 ID2D1SimplifiedGeometrySink_AddLines(ctxt->sink, &point, 1);
354 ctxt->figure_closed = FALSE;
356 ctxt->move_to = FALSE;
357 ctxt->origin = *to;
358 return 0;
361 static int decompose_conic_to(const FT_Vector *control, const FT_Vector *to, void *user)
363 struct decompose_context *ctxt = (struct decompose_context*)user;
364 D2D1_POINT_2F points[3];
365 FT_Vector cubic[3];
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->xoffset, ctxt->yoffset, points);
398 ft_vector_to_d2d_point(cubic + 1, ctxt->xoffset, ctxt->yoffset, points + 1);
399 ft_vector_to_d2d_point(cubic + 2, ctxt->xoffset, ctxt->yoffset, points + 2);
400 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
401 ctxt->figure_closed = FALSE;
402 ctxt->move_to = FALSE;
403 ctxt->origin = *to;
404 return 0;
407 static int decompose_cubic_to(const FT_Vector *control1, const FT_Vector *control2,
408 const FT_Vector *to, void *user)
410 struct decompose_context *ctxt = (struct decompose_context*)user;
411 D2D1_POINT_2F points[3];
413 ft_vector_to_d2d_point(control1, ctxt->xoffset, ctxt->yoffset, points);
414 ft_vector_to_d2d_point(control2, ctxt->xoffset, ctxt->yoffset, points + 1);
415 ft_vector_to_d2d_point(to, ctxt->xoffset, ctxt->yoffset, points + 2);
416 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt->sink, (D2D1_BEZIER_SEGMENT*)points, 1);
417 ctxt->figure_closed = FALSE;
418 ctxt->move_to = FALSE;
419 ctxt->origin = *to;
420 return 0;
423 static void decompose_outline(FT_Outline *outline, FLOAT xoffset, FLOAT yoffset, IDWriteGeometrySink *sink)
425 static const FT_Outline_Funcs decompose_funcs = {
426 decompose_move_to,
427 decompose_line_to,
428 decompose_conic_to,
429 decompose_cubic_to,
433 struct decompose_context context;
435 context.sink = sink;
436 context.xoffset = xoffset;
437 context.yoffset = yoffset;
438 context.figure_started = FALSE;
439 context.figure_closed = FALSE;
440 context.move_to = FALSE;
441 context.origin.x = 0;
442 context.origin.y = 0;
444 pFT_Outline_Decompose(outline, &decompose_funcs, &context);
446 if (!context.figure_closed && outline->n_points)
447 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
450 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
451 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
453 FTC_ScalerRec scaler;
454 USHORT simulations;
455 HRESULT hr = S_OK;
456 FT_Size size;
458 if (!count)
459 return S_OK;
461 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_WINDING);
463 simulations = IDWriteFontFace4_GetSimulations(fontface);
465 scaler.face_id = fontface;
466 scaler.width = emSize;
467 scaler.height = emSize;
468 scaler.pixel = 1;
469 scaler.x_res = 0;
470 scaler.y_res = 0;
472 EnterCriticalSection(&freetype_cs);
473 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
474 FLOAT advance = 0.0f;
475 UINT32 g;
477 for (g = 0; g < count; g++) {
478 if (pFT_Load_Glyph(size->face, glyphs[g], FT_LOAD_NO_BITMAP) == 0) {
479 FLOAT ft_advance = size->face->glyph->metrics.horiAdvance >> 6;
480 FT_Outline *outline = &size->face->glyph->outline;
481 FLOAT xoffset = 0.0f, yoffset = 0.0f;
482 FT_Matrix m;
484 m.xx = 1 << 16;
485 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
486 m.yx = 0;
487 m.yy = -(1 << 16); /* flip Y axis */
489 pFT_Outline_Transform(outline, &m);
491 /* glyph offsets act as current glyph adjustment */
492 if (offsets) {
493 xoffset += is_rtl ? -offsets[g].advanceOffset : offsets[g].advanceOffset;
494 yoffset -= offsets[g].ascenderOffset;
497 if (g == 0 && is_rtl)
498 advance = advances ? -advances[g] : -ft_advance;
500 xoffset += advance;
501 decompose_outline(outline, xoffset, yoffset, sink);
503 /* update advance to next glyph */
504 if (advances)
505 advance += is_rtl ? -advances[g] : advances[g];
506 else
507 advance += is_rtl ? -ft_advance : ft_advance;
511 else
512 hr = E_FAIL;
513 LeaveCriticalSection(&freetype_cs);
515 return hr;
518 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
520 UINT16 count = 0;
521 FT_Face face;
523 EnterCriticalSection(&freetype_cs);
524 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
525 count = face->num_glyphs;
526 LeaveCriticalSection(&freetype_cs);
528 return count;
531 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
532 UINT16 *glyphs)
534 UINT32 i;
536 EnterCriticalSection(&freetype_cs);
537 for (i = 0; i < count; i++) {
538 if (charmap == -1)
539 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoints[i]);
540 else {
541 UINT32 codepoint = codepoints[i];
542 /* special handling for symbol fonts */
543 if (codepoint < 0x100) codepoint += 0xf000;
544 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint);
545 if (!glyphs[i])
546 glyphs[i] = pFTC_CMapCache_Lookup(cmap_cache, fontface, charmap, codepoint - 0xf000);
549 LeaveCriticalSection(&freetype_cs);
552 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
554 BOOL has_kerning_pairs = FALSE;
555 FT_Face face;
557 EnterCriticalSection(&freetype_cs);
558 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
559 has_kerning_pairs = !!FT_HAS_KERNING(face);
560 LeaveCriticalSection(&freetype_cs);
562 return has_kerning_pairs;
565 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
567 INT32 adjustment = 0;
568 FT_Face face;
570 EnterCriticalSection(&freetype_cs);
571 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
572 FT_Vector kern;
573 if (FT_HAS_KERNING(face)) {
574 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
575 adjustment = kern.x;
578 LeaveCriticalSection(&freetype_cs);
580 return adjustment;
583 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX *m, FT_Matrix *ft_matrix)
585 ft_matrix->xx = m->m11 * 0x10000;
586 ft_matrix->xy = -m->m21 * 0x10000;
587 ft_matrix->yx = -m->m12 * 0x10000;
588 ft_matrix->yy = m->m22 * 0x10000;
591 /* Should be used only while holding 'freetype_cs' */
592 static BOOL is_face_scalable(IDWriteFontFace4 *fontface)
594 FT_Face face;
595 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
596 return FT_IS_SCALABLE(face);
597 else
598 return FALSE;
601 static BOOL get_glyph_transform(struct dwrite_glyphbitmap *bitmap, FT_Matrix *ret)
603 USHORT simulations = IDWriteFontFace4_GetSimulations(bitmap->fontface);
604 FT_Matrix m;
606 ret->xx = 1 << 16;
607 ret->xy = 0;
608 ret->yx = 0;
609 ret->yy = 1 << 16;
611 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
612 Disable transform if that's the case. */
613 if (!(is_face_scalable(bitmap->fontface) && (bitmap->m ||
614 (simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE))))
615 return FALSE;
617 if (simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE) {
618 m.xx = 1 << 16;
619 m.xy = (1 << 16) / 3;
620 m.yx = 0;
621 m.yy = 1 << 16;
622 pFT_Matrix_Multiply(&m, ret);
625 if (bitmap->m) {
626 ft_matrix_from_dwrite_matrix(bitmap->m, &m);
627 pFT_Matrix_Multiply(&m, ret);
630 return TRUE;
633 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
635 FTC_ImageTypeRec imagetype;
636 FT_BBox bbox = { 0 };
637 BOOL needs_transform;
638 FT_Glyph glyph;
639 FT_Matrix m;
641 EnterCriticalSection(&freetype_cs);
643 needs_transform = get_glyph_transform(bitmap, &m);
645 imagetype.face_id = bitmap->fontface;
646 imagetype.width = 0;
647 imagetype.height = bitmap->emsize;
648 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
650 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
651 if (needs_transform) {
652 FT_Glyph glyph_copy;
654 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
655 pFT_Glyph_Transform(glyph_copy, &m, NULL);
656 pFT_Glyph_Get_CBox(glyph_copy, FT_GLYPH_BBOX_PIXELS, &bbox);
657 pFT_Done_Glyph(glyph_copy);
660 else
661 pFT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
664 LeaveCriticalSection(&freetype_cs);
666 /* flip Y axis */
667 SetRect(&bitmap->bbox, bbox.xMin, -bbox.yMax, bbox.xMax, -bbox.yMin);
670 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
672 FTC_ScalerRec scaler;
673 FT_Size size;
675 scaler.face_id = fontface;
676 scaler.width = unitsperEm;
677 scaler.height = unitsperEm;
678 scaler.pixel = 1;
679 scaler.x_res = 0;
680 scaler.y_res = 0;
682 EnterCriticalSection(&freetype_cs);
683 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
684 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
685 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
687 bbox->left = metrics->horiBearingX;
688 bbox->right = bbox->left + metrics->horiAdvance;
689 bbox->top = -metrics->horiBearingY;
690 bbox->bottom = bbox->top + metrics->height;
693 LeaveCriticalSection(&freetype_cs);
696 static BOOL freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
698 const RECT *bbox = &bitmap->bbox;
699 int width = bbox->right - bbox->left;
700 int height = bbox->bottom - bbox->top;
702 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
703 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
704 const FT_Outline *src = &outline->outline;
705 FT_Bitmap ft_bitmap;
706 FT_Outline copy;
708 ft_bitmap.width = width;
709 ft_bitmap.rows = height;
710 ft_bitmap.pitch = bitmap->pitch;
711 ft_bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
712 ft_bitmap.buffer = bitmap->buf;
714 /* Note: FreeType will only set 'black' bits for us. */
715 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
716 pFT_Outline_Copy(src, &copy);
717 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
718 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
719 pFT_Outline_Done(library, &copy);
722 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
723 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
724 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
725 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
726 int h = min(height, ft_bitmap->rows);
728 while (h--) {
729 memcpy(dst, src, w);
730 src += ft_bitmap->pitch;
731 dst += bitmap->pitch;
734 else
735 FIXME("format %x not handled\n", glyph->format);
737 return TRUE;
740 static BOOL freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap *bitmap, FT_Glyph glyph)
742 const RECT *bbox = &bitmap->bbox;
743 int width = bbox->right - bbox->left;
744 int height = bbox->bottom - bbox->top;
745 BOOL ret = FALSE;
747 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
748 FT_OutlineGlyph outline = (FT_OutlineGlyph)glyph;
749 const FT_Outline *src = &outline->outline;
750 FT_Bitmap ft_bitmap;
751 FT_Outline copy;
753 ft_bitmap.width = width;
754 ft_bitmap.rows = height;
755 ft_bitmap.pitch = bitmap->pitch;
756 ft_bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
757 ft_bitmap.buffer = bitmap->buf;
759 /* Note: FreeType will only set 'black' bits for us. */
760 if (pFT_Outline_New(library, src->n_points, src->n_contours, &copy) == 0) {
761 pFT_Outline_Copy(src, &copy);
762 pFT_Outline_Translate(&copy, -bbox->left << 6, bbox->bottom << 6);
763 pFT_Outline_Get_Bitmap(library, &copy, &ft_bitmap);
764 pFT_Outline_Done(library, &copy);
767 else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
768 FT_Bitmap *ft_bitmap = &((FT_BitmapGlyph)glyph)->bitmap;
769 BYTE *src = ft_bitmap->buffer, *dst = bitmap->buf;
770 int w = min(bitmap->pitch, (ft_bitmap->width + 7) >> 3);
771 int h = min(height, ft_bitmap->rows);
773 while (h--) {
774 memcpy(dst, src, w);
775 src += ft_bitmap->pitch;
776 dst += bitmap->pitch;
779 ret = TRUE;
781 else
782 FIXME("format %x not handled\n", glyph->format);
784 return ret;
787 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
789 FTC_ImageTypeRec imagetype;
790 BOOL needs_transform;
791 BOOL ret = FALSE;
792 FT_Glyph glyph;
793 FT_Matrix m;
795 EnterCriticalSection(&freetype_cs);
797 needs_transform = get_glyph_transform(bitmap, &m);
799 imagetype.face_id = bitmap->fontface;
800 imagetype.width = 0;
801 imagetype.height = bitmap->emsize;
802 imagetype.flags = needs_transform ? FT_LOAD_NO_BITMAP : FT_LOAD_DEFAULT;
804 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, bitmap->index, &glyph, NULL) == 0) {
805 FT_Glyph glyph_copy;
807 if (needs_transform) {
808 if (pFT_Glyph_Copy(glyph, &glyph_copy) == 0) {
809 pFT_Glyph_Transform(glyph_copy, &m, NULL);
810 glyph = glyph_copy;
813 else
814 glyph_copy = NULL;
816 if (bitmap->type == DWRITE_TEXTURE_CLEARTYPE_3x1)
817 ret = freetype_get_aa_glyph_bitmap(bitmap, glyph);
818 else
819 ret = freetype_get_aliased_glyph_bitmap(bitmap, glyph);
821 if (glyph_copy)
822 pFT_Done_Glyph(glyph_copy);
825 LeaveCriticalSection(&freetype_cs);
827 return ret;
830 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
832 INT charmap_index = -1;
833 FT_Face face;
835 *is_symbol = FALSE;
837 EnterCriticalSection(&freetype_cs);
838 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
839 TT_OS2 *os2 = pFT_Get_Sfnt_Table(face, ft_sfnt_os2);
840 FT_Int i;
842 if (os2) {
843 FT_UInt dummy;
844 if (os2->version == 0)
845 *is_symbol = pFT_Get_First_Char(face, &dummy) >= 0x100;
846 else
847 *is_symbol = !!(os2->ulCodePageRange1 & FS_SYMBOL);
850 for (i = 0; i < face->num_charmaps; i++)
851 if (face->charmaps[i]->encoding == FT_ENCODING_MS_SYMBOL) {
852 *is_symbol = TRUE;
853 charmap_index = i;
854 break;
857 LeaveCriticalSection(&freetype_cs);
859 return charmap_index;
862 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
864 FTC_ImageTypeRec imagetype;
865 FT_Glyph glyph;
866 INT32 advance;
868 imagetype.face_id = fontface;
869 imagetype.width = 0;
870 imagetype.height = emSize;
871 imagetype.flags = FT_LOAD_DEFAULT;
872 if (mode == DWRITE_MEASURING_MODE_NATURAL)
873 imagetype.flags |= FT_LOAD_NO_HINTING;
875 EnterCriticalSection(&freetype_cs);
876 if (pFTC_ImageCache_Lookup(image_cache, &imagetype, index, &glyph, NULL) == 0)
877 advance = glyph->advance.x >> 16;
878 else
879 advance = 0;
880 LeaveCriticalSection(&freetype_cs);
882 return advance;
885 #else /* HAVE_FREETYPE */
887 BOOL init_freetype(void)
889 return FALSE;
892 void release_freetype(void)
896 void freetype_notify_cacheremove(IDWriteFontFace4 *fontface)
900 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
902 return E_NOTIMPL;
905 BOOL freetype_is_monospaced(IDWriteFontFace4 *fontface)
907 return FALSE;
910 HRESULT freetype_get_glyphrun_outline(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 const *glyphs, FLOAT const *advances,
911 DWRITE_GLYPH_OFFSET const *offsets, UINT32 count, BOOL is_rtl, IDWriteGeometrySink *sink)
913 return E_NOTIMPL;
916 UINT16 freetype_get_glyphcount(IDWriteFontFace4 *fontface)
918 return 0;
921 void freetype_get_glyphs(IDWriteFontFace4 *fontface, INT charmap, UINT32 const *codepoints, UINT32 count,
922 UINT16 *glyphs)
924 memset(glyphs, 0, count * sizeof(*glyphs));
927 BOOL freetype_has_kerning_pairs(IDWriteFontFace4 *fontface)
929 return FALSE;
932 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace4 *fontface, UINT16 left, UINT16 right)
934 return 0;
937 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap *bitmap)
939 memset(&bitmap->bbox, 0, sizeof(bitmap->bbox));
942 void freetype_get_design_glyph_bbox(IDWriteFontFace4 *fontface, UINT16 unitsperEm, UINT16 glyph, RECT *bbox)
944 memset(bbox, 0, sizeof(*bbox));
947 BOOL freetype_get_glyph_bitmap(struct dwrite_glyphbitmap *bitmap)
949 return FALSE;
952 INT freetype_get_charmap_index(IDWriteFontFace4 *fontface, BOOL *is_symbol)
954 *is_symbol = FALSE;
955 return -1;
958 INT32 freetype_get_glyph_advance(IDWriteFontFace4 *fontface, FLOAT emSize, UINT16 index, DWRITE_MEASURING_MODE mode)
960 return 0;
963 #endif /* HAVE_FREETYPE */