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
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_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
);
96 struct face_finalizer_data
98 IDWriteFontFileStream
*stream
;
102 static void face_finalizer(void *object
)
104 FT_Face face
= object
;
105 struct face_finalizer_data
*data
= (struct face_finalizer_data
*)face
->generic
.data
;
107 IDWriteFontFileStream_ReleaseFileFragment(data
->stream
, data
->context
);
108 IDWriteFontFileStream_Release(data
->stream
);
112 static FT_Error
face_requester(FTC_FaceID face_id
, FT_Library library
, FT_Pointer request_data
, FT_Face
*face
)
114 IDWriteFontFace
*fontface
= (IDWriteFontFace
*)face_id
;
115 IDWriteFontFileStream
*stream
;
116 IDWriteFontFile
*file
;
117 const void *data_ptr
;
127 WARN("NULL fontface requested.\n");
132 hr
= IDWriteFontFace_GetFiles(fontface
, &count
, &file
);
136 hr
= get_filestream_from_file(file
, &stream
);
137 IDWriteFontFile_Release(file
);
141 hr
= IDWriteFontFileStream_GetFileSize(stream
, &data_size
);
143 fterror
= FT_Err_Invalid_Stream_Read
;
147 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &data_ptr
, 0, data_size
, &context
);
149 fterror
= FT_Err_Invalid_Stream_Read
;
153 index
= IDWriteFontFace_GetIndex(fontface
);
154 fterror
= pFT_New_Memory_Face(library
, data_ptr
, data_size
, index
, face
);
155 if (fterror
== FT_Err_Ok
) {
156 struct face_finalizer_data
*data
;
158 data
= heap_alloc(sizeof(*data
));
159 data
->stream
= stream
;
160 data
->context
= context
;
162 (*face
)->generic
.data
= data
;
163 (*face
)->generic
.finalizer
= face_finalizer
;
167 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
170 IDWriteFontFileStream_Release(stream
);
175 BOOL
init_freetype(void)
177 FT_Version_t FT_Version
;
179 ft_handle
= wine_dlopen(SONAME_LIBFREETYPE
, RTLD_NOW
, NULL
, 0);
181 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
185 #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;}
186 LOAD_FUNCPTR(FT_Done_FreeType
)
187 LOAD_FUNCPTR(FT_Done_Glyph
)
188 LOAD_FUNCPTR(FT_Get_First_Char
)
189 LOAD_FUNCPTR(FT_Get_Kerning
)
190 LOAD_FUNCPTR(FT_Get_Sfnt_Table
)
191 LOAD_FUNCPTR(FT_Glyph_Copy
)
192 LOAD_FUNCPTR(FT_Glyph_Get_CBox
)
193 LOAD_FUNCPTR(FT_Glyph_Transform
)
194 LOAD_FUNCPTR(FT_Init_FreeType
)
195 LOAD_FUNCPTR(FT_Library_Version
)
196 LOAD_FUNCPTR(FT_Load_Glyph
)
197 LOAD_FUNCPTR(FT_New_Memory_Face
)
198 LOAD_FUNCPTR(FT_Outline_Copy
)
199 LOAD_FUNCPTR(FT_Outline_Decompose
)
200 LOAD_FUNCPTR(FT_Outline_Done
)
201 LOAD_FUNCPTR(FT_Outline_Get_Bitmap
)
202 LOAD_FUNCPTR(FT_Outline_New
)
203 LOAD_FUNCPTR(FT_Outline_Transform
)
204 LOAD_FUNCPTR(FT_Outline_Translate
)
205 LOAD_FUNCPTR(FTC_CMapCache_Lookup
)
206 LOAD_FUNCPTR(FTC_CMapCache_New
)
207 LOAD_FUNCPTR(FTC_ImageCache_Lookup
)
208 LOAD_FUNCPTR(FTC_ImageCache_New
)
209 LOAD_FUNCPTR(FTC_Manager_New
)
210 LOAD_FUNCPTR(FTC_Manager_Done
)
211 LOAD_FUNCPTR(FTC_Manager_LookupFace
)
212 LOAD_FUNCPTR(FTC_Manager_LookupSize
)
213 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID
)
216 if (pFT_Init_FreeType(&library
) != 0) {
217 ERR("Can't init FreeType library\n");
218 wine_dlclose(ft_handle
, NULL
, 0);
222 pFT_Library_Version(library
, &FT_Version
.major
, &FT_Version
.minor
, &FT_Version
.patch
);
224 /* init cache manager */
225 if (pFTC_Manager_New(library
, 0, 0, 0, &face_requester
, NULL
, &cache_manager
) != 0 ||
226 pFTC_CMapCache_New(cache_manager
, &cmap_cache
) != 0 ||
227 pFTC_ImageCache_New(cache_manager
, &image_cache
) != 0) {
229 ERR("Failed to init FreeType cache\n");
230 pFTC_Manager_Done(cache_manager
);
231 pFT_Done_FreeType(library
);
232 wine_dlclose(ft_handle
, NULL
, 0);
237 TRACE("FreeType version is %d.%d.%d\n", FT_Version
.major
, FT_Version
.minor
, FT_Version
.patch
);
241 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
242 wine_dlclose(ft_handle
, NULL
, 0);
247 void release_freetype(void)
249 pFTC_Manager_Done(cache_manager
);
250 pFT_Done_FreeType(library
);
253 void freetype_notify_cacheremove(IDWriteFontFace4
*fontface
)
255 EnterCriticalSection(&freetype_cs
);
256 pFTC_Manager_RemoveFaceID(cache_manager
, fontface
);
257 LeaveCriticalSection(&freetype_cs
);
260 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
262 FTC_ScalerRec scaler
;
265 scaler
.face_id
= fontface
;
266 scaler
.width
= unitsperEm
;
267 scaler
.height
= unitsperEm
;
272 EnterCriticalSection(&freetype_cs
);
273 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
274 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
275 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
277 ret
->leftSideBearing
= metrics
->horiBearingX
;
278 ret
->advanceWidth
= metrics
->horiAdvance
;
279 ret
->rightSideBearing
= metrics
->horiAdvance
- metrics
->horiBearingX
- metrics
->width
;
280 ret
->topSideBearing
= metrics
->vertBearingY
;
281 ret
->advanceHeight
= metrics
->vertAdvance
;
282 ret
->bottomSideBearing
= metrics
->vertAdvance
- metrics
->vertBearingY
- metrics
->height
;
283 ret
->verticalOriginY
= metrics
->height
+ metrics
->vertBearingY
;
286 LeaveCriticalSection(&freetype_cs
);
291 BOOL
freetype_is_monospaced(IDWriteFontFace4
*fontface
)
293 BOOL is_monospaced
= FALSE
;
296 EnterCriticalSection(&freetype_cs
);
297 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
298 is_monospaced
= !!FT_IS_FIXED_WIDTH(face
);
299 LeaveCriticalSection(&freetype_cs
);
301 return is_monospaced
;
304 struct decompose_context
{
305 IDWriteGeometrySink
*sink
;
310 BOOL move_to
; /* last call was 'move_to' */
311 FT_Vector origin
; /* 'pen' position from last call */
314 static inline void ft_vector_to_d2d_point(const FT_Vector
*v
, FLOAT xoffset
, FLOAT yoffset
, D2D1_POINT_2F
*p
)
316 p
->x
= (v
->x
/ 64.0f
) + xoffset
;
317 p
->y
= (v
->y
/ 64.0f
) + yoffset
;
320 static int decompose_move_to(const FT_Vector
*to
, void *user
)
322 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
325 if (ctxt
->figure_started
) {
326 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
327 ctxt
->figure_closed
= TRUE
;
330 ctxt
->figure_closed
= FALSE
;
331 ctxt
->figure_started
= TRUE
;
333 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
334 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt
->sink
, point
, D2D1_FIGURE_BEGIN_FILLED
);
335 ctxt
->move_to
= TRUE
;
340 static int decompose_line_to(const FT_Vector
*to
, void *user
)
342 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
343 /* special case for empty contours, in a way freetype returns them */
344 if (ctxt
->move_to
&& !memcmp(to
, &ctxt
->origin
, sizeof(*to
))) {
345 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
346 ctxt
->figure_closed
= TRUE
;
350 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
351 ID2D1SimplifiedGeometrySink_AddLines(ctxt
->sink
, &point
, 1);
352 ctxt
->figure_closed
= FALSE
;
354 ctxt
->move_to
= FALSE
;
359 static int decompose_conic_to(const FT_Vector
*control
, const FT_Vector
*to
, void *user
)
361 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
362 D2D1_POINT_2F points
[3];
365 /* convert from quadratic to cubic */
368 The parametric eqn for a cubic Bezier is, from PLRM:
369 r(t) = at^3 + bt^2 + ct + r0
370 with the control points:
375 A quadratic Bezier has the form:
376 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
378 So equating powers of t leads to:
381 and of course r0 = p0, r3 = p2
384 /* r1 = 1/3 p0 + 2/3 p1
385 r2 = 1/3 p2 + 2/3 p1 */
386 cubic
[0].x
= (2 * control
->x
+ 1) / 3;
387 cubic
[0].y
= (2 * control
->y
+ 1) / 3;
389 cubic
[0].x
+= (ctxt
->origin
.x
+ 1) / 3;
390 cubic
[0].y
+= (ctxt
->origin
.y
+ 1) / 3;
391 cubic
[1].x
+= (to
->x
+ 1) / 3;
392 cubic
[1].y
+= (to
->y
+ 1) / 3;
395 ft_vector_to_d2d_point(cubic
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
396 ft_vector_to_d2d_point(cubic
+ 1, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
397 ft_vector_to_d2d_point(cubic
+ 2, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
398 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
399 ctxt
->figure_closed
= FALSE
;
400 ctxt
->move_to
= FALSE
;
405 static int decompose_cubic_to(const FT_Vector
*control1
, const FT_Vector
*control2
,
406 const FT_Vector
*to
, void *user
)
408 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
409 D2D1_POINT_2F points
[3];
411 ft_vector_to_d2d_point(control1
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
412 ft_vector_to_d2d_point(control2
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
413 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
414 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
415 ctxt
->figure_closed
= FALSE
;
416 ctxt
->move_to
= FALSE
;
421 static void decompose_outline(FT_Outline
*outline
, FLOAT xoffset
, FLOAT yoffset
, IDWriteGeometrySink
*sink
)
423 static const FT_Outline_Funcs decompose_funcs
= {
431 struct decompose_context context
;
434 context
.xoffset
= xoffset
;
435 context
.yoffset
= yoffset
;
436 context
.figure_started
= FALSE
;
437 context
.figure_closed
= FALSE
;
438 context
.move_to
= FALSE
;
439 context
.origin
.x
= 0;
440 context
.origin
.y
= 0;
442 pFT_Outline_Decompose(outline
, &decompose_funcs
, &context
);
444 if (!context
.figure_closed
&& outline
->n_points
)
445 ID2D1SimplifiedGeometrySink_EndFigure(sink
, D2D1_FIGURE_END_CLOSED
);
448 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16
const *glyphs
, FLOAT
const *advances
,
449 DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
451 FTC_ScalerRec scaler
;
459 ID2D1SimplifiedGeometrySink_SetFillMode(sink
, D2D1_FILL_MODE_WINDING
);
461 simulations
= IDWriteFontFace4_GetSimulations(fontface
);
463 scaler
.face_id
= fontface
;
464 scaler
.width
= emSize
;
465 scaler
.height
= emSize
;
470 EnterCriticalSection(&freetype_cs
);
471 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
472 FLOAT advance
= 0.0f
;
475 for (g
= 0; g
< count
; g
++) {
476 if (pFT_Load_Glyph(size
->face
, glyphs
[g
], FT_LOAD_NO_BITMAP
) == 0) {
477 FLOAT ft_advance
= size
->face
->glyph
->metrics
.horiAdvance
>> 6;
478 FT_Outline
*outline
= &size
->face
->glyph
->outline
;
479 FLOAT xoffset
= 0.0f
, yoffset
= 0.0f
;
483 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
485 m
.yy
= -(1 << 16); /* flip Y axis */
487 pFT_Outline_Transform(outline
, &m
);
489 /* glyph offsets act as current glyph adjustment */
491 xoffset
+= is_rtl
? -offsets
[g
].advanceOffset
: offsets
[g
].advanceOffset
;
492 yoffset
-= offsets
[g
].ascenderOffset
;
495 if (g
== 0 && is_rtl
)
496 advance
= advances
? -advances
[g
] : -ft_advance
;
499 decompose_outline(outline
, xoffset
, yoffset
, sink
);
501 /* update advance to next glyph */
503 advance
+= is_rtl
? -advances
[g
] : advances
[g
];
505 advance
+= is_rtl
? -ft_advance
: ft_advance
;
511 LeaveCriticalSection(&freetype_cs
);
516 UINT16
freetype_get_glyphcount(IDWriteFontFace4
*fontface
)
521 EnterCriticalSection(&freetype_cs
);
522 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
523 count
= face
->num_glyphs
;
524 LeaveCriticalSection(&freetype_cs
);
529 void freetype_get_glyphs(IDWriteFontFace4
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
534 EnterCriticalSection(&freetype_cs
);
535 for (i
= 0; i
< count
; i
++) {
537 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoints
[i
]);
539 UINT32 codepoint
= codepoints
[i
];
540 /* special handling for symbol fonts */
541 if (codepoint
< 0x100) codepoint
+= 0xf000;
542 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
);
544 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
- 0xf000);
547 LeaveCriticalSection(&freetype_cs
);
550 BOOL
freetype_has_kerning_pairs(IDWriteFontFace4
*fontface
)
552 BOOL has_kerning_pairs
= FALSE
;
555 EnterCriticalSection(&freetype_cs
);
556 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
557 has_kerning_pairs
= !!FT_HAS_KERNING(face
);
558 LeaveCriticalSection(&freetype_cs
);
560 return has_kerning_pairs
;
563 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace4
*fontface
, UINT16 left
, UINT16 right
)
565 INT32 adjustment
= 0;
568 EnterCriticalSection(&freetype_cs
);
569 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
571 if (FT_HAS_KERNING(face
)) {
572 pFT_Get_Kerning(face
, left
, right
, FT_KERNING_UNSCALED
, &kern
);
576 LeaveCriticalSection(&freetype_cs
);
581 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX
*m
, FT_Matrix
*ft_matrix
)
583 ft_matrix
->xx
= m
->m11
* 0x10000;
584 ft_matrix
->xy
= -m
->m21
* 0x10000;
585 ft_matrix
->yx
= -m
->m12
* 0x10000;
586 ft_matrix
->yy
= m
->m22
* 0x10000;
589 /* Should be used only while holding 'freetype_cs' */
590 static BOOL
is_face_scalable(IDWriteFontFace4
*fontface
)
593 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
594 return FT_IS_SCALABLE(face
);
599 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
601 FTC_ImageTypeRec imagetype
;
602 FT_BBox bbox
= { 0 };
605 EnterCriticalSection(&freetype_cs
);
607 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef,
608 disable transform if that's the case. */
610 if (!is_face_scalable(bitmap
->fontface
))
614 imagetype
.face_id
= bitmap
->fontface
;
616 imagetype
.height
= bitmap
->emsize
;
617 imagetype
.flags
= bitmap
->m
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
619 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
623 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
626 ft_matrix_from_dwrite_matrix(bitmap
->m
, &ft_matrix
);
627 pFT_Glyph_Transform(glyph_copy
, &ft_matrix
, NULL
);
628 pFT_Glyph_Get_CBox(glyph_copy
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
629 pFT_Done_Glyph(glyph_copy
);
633 pFT_Glyph_Get_CBox(glyph
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
636 LeaveCriticalSection(&freetype_cs
);
639 SetRect(&bitmap
->bbox
, bbox
.xMin
, -bbox
.yMax
, bbox
.xMax
, -bbox
.yMin
);
642 void freetype_get_design_glyph_bbox(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, RECT
*bbox
)
644 FTC_ScalerRec scaler
;
647 scaler
.face_id
= fontface
;
648 scaler
.width
= unitsperEm
;
649 scaler
.height
= unitsperEm
;
654 EnterCriticalSection(&freetype_cs
);
655 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
656 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
657 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
659 bbox
->left
= metrics
->horiBearingX
;
660 bbox
->right
= bbox
->left
+ metrics
->horiAdvance
;
661 bbox
->top
= -metrics
->horiBearingY
;
662 bbox
->bottom
= bbox
->top
+ metrics
->height
;
665 LeaveCriticalSection(&freetype_cs
);
668 static BOOL
freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
670 const RECT
*bbox
= &bitmap
->bbox
;
671 int width
= bbox
->right
- bbox
->left
;
672 int height
= bbox
->bottom
- bbox
->top
;
674 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
675 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
676 const FT_Outline
*src
= &outline
->outline
;
680 ft_bitmap
.width
= width
;
681 ft_bitmap
.rows
= height
;
682 ft_bitmap
.pitch
= bitmap
->pitch
;
683 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
684 ft_bitmap
.buffer
= bitmap
->buf
;
686 /* Note: FreeType will only set 'black' bits for us. */
687 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
688 pFT_Outline_Copy(src
, ©
);
689 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
690 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
691 pFT_Outline_Done(library
, ©
);
694 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
695 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
696 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
697 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
698 int h
= min(height
, ft_bitmap
->rows
);
702 src
+= ft_bitmap
->pitch
;
703 dst
+= bitmap
->pitch
;
707 FIXME("format %x not handled\n", glyph
->format
);
712 static BOOL
freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
714 const RECT
*bbox
= &bitmap
->bbox
;
715 int width
= bbox
->right
- bbox
->left
;
716 int height
= bbox
->bottom
- bbox
->top
;
719 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
720 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
721 const FT_Outline
*src
= &outline
->outline
;
725 ft_bitmap
.width
= width
;
726 ft_bitmap
.rows
= height
;
727 ft_bitmap
.pitch
= bitmap
->pitch
;
728 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
729 ft_bitmap
.buffer
= bitmap
->buf
;
731 /* Note: FreeType will only set 'black' bits for us. */
732 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
733 pFT_Outline_Copy(src
, ©
);
734 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
735 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
736 pFT_Outline_Done(library
, ©
);
739 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
740 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
741 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
742 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
743 int h
= min(height
, ft_bitmap
->rows
);
747 src
+= ft_bitmap
->pitch
;
748 dst
+= bitmap
->pitch
;
754 FIXME("format %x not handled\n", glyph
->format
);
759 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
761 FTC_ImageTypeRec imagetype
;
765 EnterCriticalSection(&freetype_cs
);
768 if (!is_face_scalable(bitmap
->fontface
))
772 imagetype
.face_id
= bitmap
->fontface
;
774 imagetype
.height
= bitmap
->emsize
;
775 imagetype
.flags
= bitmap
->m
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
777 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
781 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
784 ft_matrix_from_dwrite_matrix(bitmap
->m
, &ft_matrix
);
785 pFT_Glyph_Transform(glyph_copy
, &ft_matrix
, NULL
);
792 if (bitmap
->type
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
793 ret
= freetype_get_aa_glyph_bitmap(bitmap
, glyph
);
795 ret
= freetype_get_aliased_glyph_bitmap(bitmap
, glyph
);
798 pFT_Done_Glyph(glyph_copy
);
801 LeaveCriticalSection(&freetype_cs
);
806 INT
freetype_get_charmap_index(IDWriteFontFace4
*fontface
, BOOL
*is_symbol
)
808 INT charmap_index
= -1;
813 EnterCriticalSection(&freetype_cs
);
814 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
815 TT_OS2
*os2
= pFT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
820 if (os2
->version
== 0)
821 *is_symbol
= pFT_Get_First_Char(face
, &dummy
) >= 0x100;
823 *is_symbol
= !!(os2
->ulCodePageRange1
& FS_SYMBOL
);
826 for (i
= 0; i
< face
->num_charmaps
; i
++)
827 if (face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
) {
833 LeaveCriticalSection(&freetype_cs
);
835 return charmap_index
;
838 INT32
freetype_get_glyph_advance(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
)
840 FTC_ImageTypeRec imagetype
;
844 imagetype
.face_id
= fontface
;
846 imagetype
.height
= emSize
;
847 imagetype
.flags
= FT_LOAD_DEFAULT
;
848 if (mode
== DWRITE_MEASURING_MODE_NATURAL
)
849 imagetype
.flags
|= FT_LOAD_NO_HINTING
;
851 EnterCriticalSection(&freetype_cs
);
852 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, index
, &glyph
, NULL
) == 0)
853 advance
= glyph
->advance
.x
>> 16;
856 LeaveCriticalSection(&freetype_cs
);
861 #else /* HAVE_FREETYPE */
863 BOOL
init_freetype(void)
868 void release_freetype(void)
872 void freetype_notify_cacheremove(IDWriteFontFace4
*fontface
)
876 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
881 BOOL
freetype_is_monospaced(IDWriteFontFace4
*fontface
)
886 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16
const *glyphs
, FLOAT
const *advances
,
887 DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
892 UINT16
freetype_get_glyphcount(IDWriteFontFace4
*fontface
)
897 void freetype_get_glyphs(IDWriteFontFace4
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
900 memset(glyphs
, 0, count
* sizeof(*glyphs
));
903 BOOL
freetype_has_kerning_pairs(IDWriteFontFace4
*fontface
)
908 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace4
*fontface
, UINT16 left
, UINT16 right
)
913 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
915 memset(&bitmap
->bbox
, 0, sizeof(bitmap
->bbox
));
918 void freetype_get_design_glyph_bbox(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, RECT
*bbox
)
920 memset(bbox
, 0, sizeof(*bbox
));
923 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
928 INT
freetype_get_charmap_index(IDWriteFontFace4
*fontface
, BOOL
*is_symbol
)
934 INT32
freetype_get_glyph_advance(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
)
939 #endif /* HAVE_FREETYPE */