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_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
);
97 struct face_finalizer_data
99 IDWriteFontFileStream
*stream
;
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
);
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
;
128 WARN("NULL fontface requested.\n");
133 hr
= IDWriteFontFace_GetFiles(fontface
, &count
, &file
);
137 hr
= get_filestream_from_file(file
, &stream
);
138 IDWriteFontFile_Release(file
);
142 hr
= IDWriteFontFileStream_GetFileSize(stream
, &data_size
);
144 fterror
= FT_Err_Invalid_Stream_Read
;
148 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &data_ptr
, 0, data_size
, &context
);
150 fterror
= FT_Err_Invalid_Stream_Read
;
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
;
168 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
171 IDWriteFontFileStream_Release(stream
);
176 BOOL
init_freetype(void)
178 FT_Version_t FT_Version
;
180 ft_handle
= wine_dlopen(SONAME_LIBFREETYPE
, RTLD_NOW
, NULL
, 0);
182 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
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
)
218 if (pFT_Init_FreeType(&library
) != 0) {
219 ERR("Can't init FreeType library\n");
220 wine_dlclose(ft_handle
, NULL
, 0);
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);
239 TRACE("FreeType version is %d.%d.%d\n", FT_Version
.major
, FT_Version
.minor
, FT_Version
.patch
);
243 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
244 wine_dlclose(ft_handle
, NULL
, 0);
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
;
267 scaler
.face_id
= fontface
;
268 scaler
.width
= unitsperEm
;
269 scaler
.height
= unitsperEm
;
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
);
293 BOOL
freetype_is_monospaced(IDWriteFontFace4
*fontface
)
295 BOOL is_monospaced
= FALSE
;
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
;
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
;
327 if (ctxt
->figure_started
) {
328 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
329 ctxt
->figure_closed
= TRUE
;
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
;
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
;
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
;
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];
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:
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:
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;
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;
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
;
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
;
423 static void decompose_outline(FT_Outline
*outline
, FLOAT xoffset
, FLOAT yoffset
, IDWriteGeometrySink
*sink
)
425 static const FT_Outline_Funcs decompose_funcs
= {
433 struct decompose_context context
;
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
;
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
;
472 EnterCriticalSection(&freetype_cs
);
473 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
474 FLOAT advance
= 0.0f
;
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
;
485 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
487 m
.yy
= -(1 << 16); /* flip Y axis */
489 pFT_Outline_Transform(outline
, &m
);
491 /* glyph offsets act as current glyph adjustment */
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
;
501 decompose_outline(outline
, xoffset
, yoffset
, sink
);
503 /* update advance to next glyph */
505 advance
+= is_rtl
? -advances
[g
] : advances
[g
];
507 advance
+= is_rtl
? -ft_advance
: ft_advance
;
513 LeaveCriticalSection(&freetype_cs
);
518 UINT16
freetype_get_glyphcount(IDWriteFontFace4
*fontface
)
523 EnterCriticalSection(&freetype_cs
);
524 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
525 count
= face
->num_glyphs
;
526 LeaveCriticalSection(&freetype_cs
);
531 void freetype_get_glyphs(IDWriteFontFace4
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
536 EnterCriticalSection(&freetype_cs
);
537 for (i
= 0; i
< count
; i
++) {
539 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoints
[i
]);
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
);
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
;
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;
570 EnterCriticalSection(&freetype_cs
);
571 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
573 if (FT_HAS_KERNING(face
)) {
574 pFT_Get_Kerning(face
, left
, right
, FT_KERNING_UNSCALED
, &kern
);
578 LeaveCriticalSection(&freetype_cs
);
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
)
595 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
596 return FT_IS_SCALABLE(face
);
601 static BOOL
get_glyph_transform(struct dwrite_glyphbitmap
*bitmap
, FT_Matrix
*ret
)
603 USHORT simulations
= IDWriteFontFace4_GetSimulations(bitmap
->fontface
);
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
))))
617 if (simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
) {
619 m
.xy
= (1 << 16) / 3;
622 pFT_Matrix_Multiply(&m
, ret
);
626 ft_matrix_from_dwrite_matrix(bitmap
->m
, &m
);
627 pFT_Matrix_Multiply(&m
, ret
);
633 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
635 FTC_ImageTypeRec imagetype
;
636 FT_BBox bbox
= { 0 };
637 BOOL needs_transform
;
641 EnterCriticalSection(&freetype_cs
);
643 needs_transform
= get_glyph_transform(bitmap
, &m
);
645 imagetype
.face_id
= bitmap
->fontface
;
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
) {
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
);
661 pFT_Glyph_Get_CBox(glyph
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
664 LeaveCriticalSection(&freetype_cs
);
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
;
675 scaler
.face_id
= fontface
;
676 scaler
.width
= unitsperEm
;
677 scaler
.height
= unitsperEm
;
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
;
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
, ©
) == 0) {
716 pFT_Outline_Copy(src
, ©
);
717 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
718 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
719 pFT_Outline_Done(library
, ©
);
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
);
730 src
+= ft_bitmap
->pitch
;
731 dst
+= bitmap
->pitch
;
735 FIXME("format %x not handled\n", glyph
->format
);
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
;
747 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
748 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
749 const FT_Outline
*src
= &outline
->outline
;
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
, ©
) == 0) {
761 pFT_Outline_Copy(src
, ©
);
762 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
763 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
764 pFT_Outline_Done(library
, ©
);
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
);
775 src
+= ft_bitmap
->pitch
;
776 dst
+= bitmap
->pitch
;
782 FIXME("format %x not handled\n", glyph
->format
);
787 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
789 FTC_ImageTypeRec imagetype
;
790 BOOL needs_transform
;
795 EnterCriticalSection(&freetype_cs
);
797 needs_transform
= get_glyph_transform(bitmap
, &m
);
799 imagetype
.face_id
= bitmap
->fontface
;
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) {
807 if (needs_transform
) {
808 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
809 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
816 if (bitmap
->type
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
817 ret
= freetype_get_aa_glyph_bitmap(bitmap
, glyph
);
819 ret
= freetype_get_aliased_glyph_bitmap(bitmap
, glyph
);
822 pFT_Done_Glyph(glyph_copy
);
825 LeaveCriticalSection(&freetype_cs
);
830 INT
freetype_get_charmap_index(IDWriteFontFace4
*fontface
, BOOL
*is_symbol
)
832 INT charmap_index
= -1;
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
);
844 if (os2
->version
== 0)
845 *is_symbol
= pFT_Get_First_Char(face
, &dummy
) >= 0x100;
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
) {
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
;
868 imagetype
.face_id
= fontface
;
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;
880 LeaveCriticalSection(&freetype_cs
);
885 #else /* HAVE_FREETYPE */
887 BOOL
init_freetype(void)
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
)
905 BOOL
freetype_is_monospaced(IDWriteFontFace4
*fontface
)
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
)
916 UINT16
freetype_get_glyphcount(IDWriteFontFace4
*fontface
)
921 void freetype_get_glyphs(IDWriteFontFace4
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
924 memset(glyphs
, 0, count
* sizeof(*glyphs
));
927 BOOL
freetype_has_kerning_pairs(IDWriteFontFace4
*fontface
)
932 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace4
*fontface
, UINT16 left
, UINT16 right
)
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
)
952 INT
freetype_get_charmap_index(IDWriteFontFace4
*fontface
, BOOL
*is_symbol
)
958 INT32
freetype_get_glyph_advance(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
)
963 #endif /* HAVE_FREETYPE */