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
24 #include "wine/port.h"
26 #ifdef HAVE_FT2BUILD_H
29 #include FT_FREETYPE_H
31 #include FT_TRUETYPE_TABLES_H
32 #endif /* HAVE_FT2BUILD_H */
35 #include "wine/library.h"
36 #include "wine/debug.h"
38 #include "dwrite_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(dwrite
);
44 static CRITICAL_SECTION freetype_cs
;
45 static CRITICAL_SECTION_DEBUG critsect_debug
=
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;
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_Embolden
);
83 MAKE_FUNCPTR(FT_Outline_Get_Bitmap
);
84 MAKE_FUNCPTR(FT_Outline_New
);
85 MAKE_FUNCPTR(FT_Outline_Transform
);
86 MAKE_FUNCPTR(FT_Outline_Translate
);
87 MAKE_FUNCPTR(FTC_CMapCache_Lookup
);
88 MAKE_FUNCPTR(FTC_CMapCache_New
);
89 MAKE_FUNCPTR(FTC_ImageCache_Lookup
);
90 MAKE_FUNCPTR(FTC_ImageCache_New
);
91 MAKE_FUNCPTR(FTC_Manager_New
);
92 MAKE_FUNCPTR(FTC_Manager_Done
);
93 MAKE_FUNCPTR(FTC_Manager_LookupFace
);
94 MAKE_FUNCPTR(FTC_Manager_LookupSize
);
95 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID
);
97 static FT_Error (*pFT_Outline_EmboldenXY
)(FT_Outline
*, FT_Pos
, FT_Pos
);
99 struct face_finalizer_data
101 IDWriteFontFileStream
*stream
;
105 static void face_finalizer(void *object
)
107 FT_Face face
= object
;
108 struct face_finalizer_data
*data
= (struct face_finalizer_data
*)face
->generic
.data
;
110 IDWriteFontFileStream_ReleaseFileFragment(data
->stream
, data
->context
);
111 IDWriteFontFileStream_Release(data
->stream
);
115 static FT_Error
face_requester(FTC_FaceID face_id
, FT_Library library
, FT_Pointer request_data
, FT_Face
*face
)
117 IDWriteFontFace
*fontface
= (IDWriteFontFace
*)face_id
;
118 IDWriteFontFileStream
*stream
;
119 IDWriteFontFile
*file
;
120 const void *data_ptr
;
130 WARN("NULL fontface requested.\n");
135 hr
= IDWriteFontFace_GetFiles(fontface
, &count
, &file
);
139 hr
= get_filestream_from_file(file
, &stream
);
140 IDWriteFontFile_Release(file
);
144 hr
= IDWriteFontFileStream_GetFileSize(stream
, &data_size
);
146 fterror
= FT_Err_Invalid_Stream_Read
;
150 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &data_ptr
, 0, data_size
, &context
);
152 fterror
= FT_Err_Invalid_Stream_Read
;
156 index
= IDWriteFontFace_GetIndex(fontface
);
157 fterror
= pFT_New_Memory_Face(library
, data_ptr
, data_size
, index
, face
);
158 if (fterror
== FT_Err_Ok
) {
159 struct face_finalizer_data
*data
;
161 data
= heap_alloc(sizeof(*data
));
162 data
->stream
= stream
;
163 data
->context
= context
;
165 (*face
)->generic
.data
= data
;
166 (*face
)->generic
.finalizer
= face_finalizer
;
170 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
173 IDWriteFontFileStream_Release(stream
);
178 BOOL
init_freetype(void)
180 FT_Version_t FT_Version
;
182 ft_handle
= wine_dlopen(SONAME_LIBFREETYPE
, RTLD_NOW
, NULL
, 0);
184 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
188 #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;}
189 LOAD_FUNCPTR(FT_Done_FreeType
)
190 LOAD_FUNCPTR(FT_Done_Glyph
)
191 LOAD_FUNCPTR(FT_Get_First_Char
)
192 LOAD_FUNCPTR(FT_Get_Kerning
)
193 LOAD_FUNCPTR(FT_Get_Sfnt_Table
)
194 LOAD_FUNCPTR(FT_Glyph_Copy
)
195 LOAD_FUNCPTR(FT_Glyph_Get_CBox
)
196 LOAD_FUNCPTR(FT_Glyph_Transform
)
197 LOAD_FUNCPTR(FT_Init_FreeType
)
198 LOAD_FUNCPTR(FT_Library_Version
)
199 LOAD_FUNCPTR(FT_Load_Glyph
)
200 LOAD_FUNCPTR(FT_Matrix_Multiply
)
201 LOAD_FUNCPTR(FT_New_Memory_Face
)
202 LOAD_FUNCPTR(FT_Outline_Copy
)
203 LOAD_FUNCPTR(FT_Outline_Decompose
)
204 LOAD_FUNCPTR(FT_Outline_Done
)
205 LOAD_FUNCPTR(FT_Outline_Embolden
)
206 LOAD_FUNCPTR(FT_Outline_Get_Bitmap
)
207 LOAD_FUNCPTR(FT_Outline_New
)
208 LOAD_FUNCPTR(FT_Outline_Transform
)
209 LOAD_FUNCPTR(FT_Outline_Translate
)
210 LOAD_FUNCPTR(FTC_CMapCache_Lookup
)
211 LOAD_FUNCPTR(FTC_CMapCache_New
)
212 LOAD_FUNCPTR(FTC_ImageCache_Lookup
)
213 LOAD_FUNCPTR(FTC_ImageCache_New
)
214 LOAD_FUNCPTR(FTC_Manager_New
)
215 LOAD_FUNCPTR(FTC_Manager_Done
)
216 LOAD_FUNCPTR(FTC_Manager_LookupFace
)
217 LOAD_FUNCPTR(FTC_Manager_LookupSize
)
218 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID
)
220 pFT_Outline_EmboldenXY
= wine_dlsym(ft_handle
, "FT_Outline_EmboldenXY", NULL
, 0);
222 if (pFT_Init_FreeType(&library
) != 0) {
223 ERR("Can't init FreeType library\n");
224 wine_dlclose(ft_handle
, NULL
, 0);
228 pFT_Library_Version(library
, &FT_Version
.major
, &FT_Version
.minor
, &FT_Version
.patch
);
230 /* init cache manager */
231 if (pFTC_Manager_New(library
, 0, 0, 0, &face_requester
, NULL
, &cache_manager
) != 0 ||
232 pFTC_CMapCache_New(cache_manager
, &cmap_cache
) != 0 ||
233 pFTC_ImageCache_New(cache_manager
, &image_cache
) != 0) {
235 ERR("Failed to init FreeType cache\n");
236 pFTC_Manager_Done(cache_manager
);
237 pFT_Done_FreeType(library
);
238 wine_dlclose(ft_handle
, NULL
, 0);
243 TRACE("FreeType version is %d.%d.%d\n", FT_Version
.major
, FT_Version
.minor
, FT_Version
.patch
);
247 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
248 wine_dlclose(ft_handle
, NULL
, 0);
253 void release_freetype(void)
255 pFTC_Manager_Done(cache_manager
);
256 pFT_Done_FreeType(library
);
259 void freetype_notify_cacheremove(IDWriteFontFace4
*fontface
)
261 EnterCriticalSection(&freetype_cs
);
262 pFTC_Manager_RemoveFaceID(cache_manager
, fontface
);
263 LeaveCriticalSection(&freetype_cs
);
266 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
268 FTC_ScalerRec scaler
;
271 scaler
.face_id
= fontface
;
272 scaler
.width
= unitsperEm
;
273 scaler
.height
= unitsperEm
;
278 EnterCriticalSection(&freetype_cs
);
279 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
280 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
281 USHORT simulations
= IDWriteFontFace4_GetSimulations(fontface
);
282 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
284 ret
->leftSideBearing
= metrics
->horiBearingX
;
285 ret
->advanceWidth
= metrics
->horiAdvance
;
286 ret
->rightSideBearing
= metrics
->horiAdvance
- metrics
->horiBearingX
- metrics
->width
;
287 ret
->topSideBearing
= metrics
->vertBearingY
;
288 ret
->advanceHeight
= metrics
->vertAdvance
;
289 ret
->bottomSideBearing
= metrics
->vertAdvance
- metrics
->vertBearingY
- metrics
->height
;
290 ret
->verticalOriginY
= metrics
->height
+ metrics
->vertBearingY
;
292 /* Adjust in case of bold simulation, glyphs without contours are ignored. */
293 if (simulations
& DWRITE_FONT_SIMULATIONS_BOLD
&& size
->face
->glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
&&
294 size
->face
->glyph
->outline
.n_contours
!= 0) {
295 if (ret
->advanceWidth
)
296 ret
->advanceWidth
+= (unitsperEm
+ 49) / 50;
300 LeaveCriticalSection(&freetype_cs
);
305 BOOL
freetype_is_monospaced(IDWriteFontFace4
*fontface
)
307 BOOL is_monospaced
= FALSE
;
310 EnterCriticalSection(&freetype_cs
);
311 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
312 is_monospaced
= !!FT_IS_FIXED_WIDTH(face
);
313 LeaveCriticalSection(&freetype_cs
);
315 return is_monospaced
;
318 struct decompose_context
{
319 IDWriteGeometrySink
*sink
;
324 BOOL move_to
; /* last call was 'move_to' */
325 FT_Vector origin
; /* 'pen' position from last call */
328 static inline void ft_vector_to_d2d_point(const FT_Vector
*v
, FLOAT xoffset
, FLOAT yoffset
, D2D1_POINT_2F
*p
)
330 p
->x
= (v
->x
/ 64.0f
) + xoffset
;
331 p
->y
= (v
->y
/ 64.0f
) + yoffset
;
334 static int decompose_move_to(const FT_Vector
*to
, void *user
)
336 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
339 if (ctxt
->figure_started
) {
340 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
341 ctxt
->figure_closed
= TRUE
;
344 ctxt
->figure_closed
= FALSE
;
345 ctxt
->figure_started
= TRUE
;
347 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
348 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt
->sink
, point
, D2D1_FIGURE_BEGIN_FILLED
);
349 ctxt
->move_to
= TRUE
;
354 static int decompose_line_to(const FT_Vector
*to
, void *user
)
356 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
357 /* special case for empty contours, in a way freetype returns them */
358 if (ctxt
->move_to
&& !memcmp(to
, &ctxt
->origin
, sizeof(*to
))) {
359 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
360 ctxt
->figure_closed
= TRUE
;
364 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
365 ID2D1SimplifiedGeometrySink_AddLines(ctxt
->sink
, &point
, 1);
366 ctxt
->figure_closed
= FALSE
;
368 ctxt
->move_to
= FALSE
;
373 static int decompose_conic_to(const FT_Vector
*control
, const FT_Vector
*to
, void *user
)
375 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
376 D2D1_POINT_2F points
[3];
379 /* convert from quadratic to cubic */
382 The parametric eqn for a cubic Bezier is, from PLRM:
383 r(t) = at^3 + bt^2 + ct + r0
384 with the control points:
389 A quadratic Bezier has the form:
390 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
392 So equating powers of t leads to:
395 and of course r0 = p0, r3 = p2
398 /* r1 = 1/3 p0 + 2/3 p1
399 r2 = 1/3 p2 + 2/3 p1 */
400 cubic
[0].x
= (2 * control
->x
+ 1) / 3;
401 cubic
[0].y
= (2 * control
->y
+ 1) / 3;
403 cubic
[0].x
+= (ctxt
->origin
.x
+ 1) / 3;
404 cubic
[0].y
+= (ctxt
->origin
.y
+ 1) / 3;
405 cubic
[1].x
+= (to
->x
+ 1) / 3;
406 cubic
[1].y
+= (to
->y
+ 1) / 3;
409 ft_vector_to_d2d_point(cubic
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
410 ft_vector_to_d2d_point(cubic
+ 1, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
411 ft_vector_to_d2d_point(cubic
+ 2, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
412 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
413 ctxt
->figure_closed
= FALSE
;
414 ctxt
->move_to
= FALSE
;
419 static int decompose_cubic_to(const FT_Vector
*control1
, const FT_Vector
*control2
,
420 const FT_Vector
*to
, void *user
)
422 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
423 D2D1_POINT_2F points
[3];
425 ft_vector_to_d2d_point(control1
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
426 ft_vector_to_d2d_point(control2
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
427 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
428 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
429 ctxt
->figure_closed
= FALSE
;
430 ctxt
->move_to
= FALSE
;
435 static void decompose_outline(FT_Outline
*outline
, FLOAT xoffset
, FLOAT yoffset
, IDWriteGeometrySink
*sink
)
437 static const FT_Outline_Funcs decompose_funcs
= {
445 struct decompose_context context
;
448 context
.xoffset
= xoffset
;
449 context
.yoffset
= yoffset
;
450 context
.figure_started
= FALSE
;
451 context
.figure_closed
= FALSE
;
452 context
.move_to
= FALSE
;
453 context
.origin
.x
= 0;
454 context
.origin
.y
= 0;
456 pFT_Outline_Decompose(outline
, &decompose_funcs
, &context
);
458 if (!context
.figure_closed
&& outline
->n_points
)
459 ID2D1SimplifiedGeometrySink_EndFigure(sink
, D2D1_FIGURE_END_CLOSED
);
462 static void embolden_glyph_outline(FT_Outline
*outline
, FLOAT emsize
)
466 strength
= MulDiv(emsize
, 1 << 6, 24);
467 if (pFT_Outline_EmboldenXY
)
468 pFT_Outline_EmboldenXY(outline
, strength
, 0);
470 pFT_Outline_Embolden(outline
, strength
);
473 static void embolden_glyph(FT_Glyph glyph
, FLOAT emsize
)
475 FT_OutlineGlyph outline_glyph
= (FT_OutlineGlyph
)glyph
;
477 if (glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
480 embolden_glyph_outline(&outline_glyph
->outline
, emsize
);
483 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16
const *glyphs
,
484 FLOAT
const *advances
, DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
486 FTC_ScalerRec scaler
;
494 ID2D1SimplifiedGeometrySink_SetFillMode(sink
, D2D1_FILL_MODE_WINDING
);
496 simulations
= IDWriteFontFace4_GetSimulations(fontface
);
498 scaler
.face_id
= fontface
;
499 scaler
.width
= emSize
;
500 scaler
.height
= emSize
;
505 EnterCriticalSection(&freetype_cs
);
506 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
507 FLOAT advance
= 0.0f
;
510 for (g
= 0; g
< count
; g
++) {
511 if (pFT_Load_Glyph(size
->face
, glyphs
[g
], FT_LOAD_NO_BITMAP
) == 0) {
512 FLOAT ft_advance
= size
->face
->glyph
->metrics
.horiAdvance
>> 6;
513 FT_Outline
*outline
= &size
->face
->glyph
->outline
;
514 FLOAT xoffset
= 0.0f
, yoffset
= 0.0f
;
517 if (simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
518 embolden_glyph_outline(outline
, emSize
);
521 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
523 m
.yy
= -(1 << 16); /* flip Y axis */
525 pFT_Outline_Transform(outline
, &m
);
527 /* glyph offsets act as current glyph adjustment */
529 xoffset
+= is_rtl
? -offsets
[g
].advanceOffset
: offsets
[g
].advanceOffset
;
530 yoffset
-= offsets
[g
].ascenderOffset
;
533 if (g
== 0 && is_rtl
)
534 advance
= advances
? -advances
[g
] : -ft_advance
;
537 decompose_outline(outline
, xoffset
, yoffset
, sink
);
539 /* update advance to next glyph */
541 advance
+= is_rtl
? -advances
[g
] : advances
[g
];
543 advance
+= is_rtl
? -ft_advance
: ft_advance
;
549 LeaveCriticalSection(&freetype_cs
);
554 UINT16
freetype_get_glyphcount(IDWriteFontFace4
*fontface
)
559 EnterCriticalSection(&freetype_cs
);
560 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
561 count
= face
->num_glyphs
;
562 LeaveCriticalSection(&freetype_cs
);
567 void freetype_get_glyphs(IDWriteFontFace4
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
572 EnterCriticalSection(&freetype_cs
);
573 for (i
= 0; i
< count
; i
++) {
575 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoints
[i
]);
577 UINT32 codepoint
= codepoints
[i
];
578 /* special handling for symbol fonts */
579 if (codepoint
< 0x100) codepoint
+= 0xf000;
580 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
);
582 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
- 0xf000);
585 LeaveCriticalSection(&freetype_cs
);
588 BOOL
freetype_has_kerning_pairs(IDWriteFontFace4
*fontface
)
590 BOOL has_kerning_pairs
= FALSE
;
593 EnterCriticalSection(&freetype_cs
);
594 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
595 has_kerning_pairs
= !!FT_HAS_KERNING(face
);
596 LeaveCriticalSection(&freetype_cs
);
598 return has_kerning_pairs
;
601 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace4
*fontface
, UINT16 left
, UINT16 right
)
603 INT32 adjustment
= 0;
606 EnterCriticalSection(&freetype_cs
);
607 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
609 if (FT_HAS_KERNING(face
)) {
610 pFT_Get_Kerning(face
, left
, right
, FT_KERNING_UNSCALED
, &kern
);
614 LeaveCriticalSection(&freetype_cs
);
619 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX
*m
, FT_Matrix
*ft_matrix
)
621 ft_matrix
->xx
= m
->m11
* 0x10000;
622 ft_matrix
->xy
= -m
->m21
* 0x10000;
623 ft_matrix
->yx
= -m
->m12
* 0x10000;
624 ft_matrix
->yy
= m
->m22
* 0x10000;
627 /* Should be used only while holding 'freetype_cs' */
628 static BOOL
is_face_scalable(IDWriteFontFace4
*fontface
)
631 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
632 return FT_IS_SCALABLE(face
);
637 static BOOL
get_glyph_transform(struct dwrite_glyphbitmap
*bitmap
, FT_Matrix
*ret
)
639 USHORT simulations
= IDWriteFontFace4_GetSimulations(bitmap
->fontface
);
647 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
648 Disable transform if that's the case. */
649 if (!is_face_scalable(bitmap
->fontface
) || (!bitmap
->m
&& simulations
== 0))
652 if (simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
) {
654 m
.xy
= (1 << 16) / 3;
657 pFT_Matrix_Multiply(&m
, ret
);
661 ft_matrix_from_dwrite_matrix(bitmap
->m
, &m
);
662 pFT_Matrix_Multiply(&m
, ret
);
668 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
670 USHORT simulations
= IDWriteFontFace4_GetSimulations(bitmap
->fontface
);
671 FTC_ImageTypeRec imagetype
;
672 FT_BBox bbox
= { 0 };
673 BOOL needs_transform
;
677 EnterCriticalSection(&freetype_cs
);
679 needs_transform
= get_glyph_transform(bitmap
, &m
);
681 imagetype
.face_id
= bitmap
->fontface
;
683 imagetype
.height
= bitmap
->emsize
;
684 imagetype
.flags
= needs_transform
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
686 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
687 if (needs_transform
) {
690 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
691 if (simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
692 embolden_glyph(glyph_copy
, bitmap
->emsize
);
694 /* Includes oblique and user transform. */
695 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
696 pFT_Glyph_Get_CBox(glyph_copy
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
697 pFT_Done_Glyph(glyph_copy
);
701 pFT_Glyph_Get_CBox(glyph
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
704 LeaveCriticalSection(&freetype_cs
);
707 SetRect(&bitmap
->bbox
, bbox
.xMin
, -bbox
.yMax
, bbox
.xMax
, -bbox
.yMin
);
710 void freetype_get_design_glyph_bbox(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, RECT
*bbox
)
712 FTC_ScalerRec scaler
;
715 scaler
.face_id
= fontface
;
716 scaler
.width
= unitsperEm
;
717 scaler
.height
= unitsperEm
;
722 EnterCriticalSection(&freetype_cs
);
723 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
724 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
725 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
727 bbox
->left
= metrics
->horiBearingX
;
728 bbox
->right
= bbox
->left
+ metrics
->horiAdvance
;
729 bbox
->top
= -metrics
->horiBearingY
;
730 bbox
->bottom
= bbox
->top
+ metrics
->height
;
733 LeaveCriticalSection(&freetype_cs
);
736 static BOOL
freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
738 const RECT
*bbox
= &bitmap
->bbox
;
739 int width
= bbox
->right
- bbox
->left
;
740 int height
= bbox
->bottom
- bbox
->top
;
742 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
743 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
744 const FT_Outline
*src
= &outline
->outline
;
748 ft_bitmap
.width
= width
;
749 ft_bitmap
.rows
= height
;
750 ft_bitmap
.pitch
= bitmap
->pitch
;
751 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
752 ft_bitmap
.buffer
= bitmap
->buf
;
754 /* Note: FreeType will only set 'black' bits for us. */
755 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
756 pFT_Outline_Copy(src
, ©
);
757 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
758 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
759 pFT_Outline_Done(library
, ©
);
762 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
763 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
764 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
765 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
766 int h
= min(height
, ft_bitmap
->rows
);
770 src
+= ft_bitmap
->pitch
;
771 dst
+= bitmap
->pitch
;
775 FIXME("format %x not handled\n", glyph
->format
);
780 static BOOL
freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
782 const RECT
*bbox
= &bitmap
->bbox
;
783 int width
= bbox
->right
- bbox
->left
;
784 int height
= bbox
->bottom
- bbox
->top
;
787 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
788 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
789 const FT_Outline
*src
= &outline
->outline
;
793 ft_bitmap
.width
= width
;
794 ft_bitmap
.rows
= height
;
795 ft_bitmap
.pitch
= bitmap
->pitch
;
796 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
797 ft_bitmap
.buffer
= bitmap
->buf
;
799 /* Note: FreeType will only set 'black' bits for us. */
800 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
801 pFT_Outline_Copy(src
, ©
);
802 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
803 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
804 pFT_Outline_Done(library
, ©
);
807 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
808 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
809 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
810 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
811 int h
= min(height
, ft_bitmap
->rows
);
815 src
+= ft_bitmap
->pitch
;
816 dst
+= bitmap
->pitch
;
822 FIXME("format %x not handled\n", glyph
->format
);
827 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
829 USHORT simulations
= IDWriteFontFace4_GetSimulations(bitmap
->fontface
);
830 FTC_ImageTypeRec imagetype
;
831 BOOL needs_transform
;
836 EnterCriticalSection(&freetype_cs
);
838 needs_transform
= get_glyph_transform(bitmap
, &m
);
840 imagetype
.face_id
= bitmap
->fontface
;
842 imagetype
.height
= bitmap
->emsize
;
843 imagetype
.flags
= needs_transform
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
845 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
848 if (needs_transform
) {
849 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
850 if (simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
851 embolden_glyph(glyph_copy
, bitmap
->emsize
);
853 /* Includes oblique and user transform. */
854 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
861 if (bitmap
->type
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
862 ret
= freetype_get_aa_glyph_bitmap(bitmap
, glyph
);
864 ret
= freetype_get_aliased_glyph_bitmap(bitmap
, glyph
);
867 pFT_Done_Glyph(glyph_copy
);
870 LeaveCriticalSection(&freetype_cs
);
875 INT
freetype_get_charmap_index(IDWriteFontFace4
*fontface
, BOOL
*is_symbol
)
877 INT charmap_index
= -1;
882 EnterCriticalSection(&freetype_cs
);
883 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
884 TT_OS2
*os2
= pFT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
889 if (os2
->version
== 0)
890 *is_symbol
= pFT_Get_First_Char(face
, &dummy
) >= 0x100;
892 *is_symbol
= !!(os2
->ulCodePageRange1
& FS_SYMBOL
);
895 for (i
= 0; i
< face
->num_charmaps
; i
++)
896 if (face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
) {
902 LeaveCriticalSection(&freetype_cs
);
904 return charmap_index
;
907 INT32
freetype_get_glyph_advance(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
,
910 FTC_ImageTypeRec imagetype
;
914 imagetype
.face_id
= fontface
;
916 imagetype
.height
= emSize
;
917 imagetype
.flags
= FT_LOAD_DEFAULT
;
918 if (mode
== DWRITE_MEASURING_MODE_NATURAL
)
919 imagetype
.flags
|= FT_LOAD_NO_HINTING
;
921 EnterCriticalSection(&freetype_cs
);
922 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, index
, &glyph
, NULL
) == 0) {
923 *has_contours
= glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
&& ((FT_OutlineGlyph
)glyph
)->outline
.n_contours
;
924 advance
= glyph
->advance
.x
>> 16;
927 *has_contours
= FALSE
;
930 LeaveCriticalSection(&freetype_cs
);
935 #else /* HAVE_FREETYPE */
937 BOOL
init_freetype(void)
942 void release_freetype(void)
946 void freetype_notify_cacheremove(IDWriteFontFace4
*fontface
)
950 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
955 BOOL
freetype_is_monospaced(IDWriteFontFace4
*fontface
)
960 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16
const *glyphs
, FLOAT
const *advances
,
961 DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
966 UINT16
freetype_get_glyphcount(IDWriteFontFace4
*fontface
)
971 void freetype_get_glyphs(IDWriteFontFace4
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
974 memset(glyphs
, 0, count
* sizeof(*glyphs
));
977 BOOL
freetype_has_kerning_pairs(IDWriteFontFace4
*fontface
)
982 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace4
*fontface
, UINT16 left
, UINT16 right
)
987 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
989 SetRectEmpty(&bitmap
->bbox
);
992 void freetype_get_design_glyph_bbox(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, RECT
*bbox
)
997 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
1002 INT
freetype_get_charmap_index(IDWriteFontFace4
*fontface
, BOOL
*is_symbol
)
1008 INT32
freetype_get_glyph_advance(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
,
1011 *has_contours
= FALSE
;
1015 #endif /* HAVE_FREETYPE */