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
;
323 BOOL move_to
; /* last call was 'move_to' */
324 FT_Vector origin
; /* 'pen' position from last call */
327 static inline void ft_vector_to_d2d_point(const FT_Vector
*v
, FLOAT xoffset
, FLOAT yoffset
, D2D1_POINT_2F
*p
)
329 p
->x
= (v
->x
/ 64.0f
) + xoffset
;
330 p
->y
= (v
->y
/ 64.0f
) + yoffset
;
333 static void decompose_beginfigure(struct decompose_context
*ctxt
)
340 ft_vector_to_d2d_point(&ctxt
->origin
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
341 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt
->sink
, point
, D2D1_FIGURE_BEGIN_FILLED
);
343 ctxt
->figure_started
= TRUE
;
344 ctxt
->move_to
= FALSE
;
347 static int decompose_move_to(const FT_Vector
*to
, void *user
)
349 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
351 if (ctxt
->figure_started
) {
352 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
353 ctxt
->figure_started
= FALSE
;
356 ctxt
->move_to
= TRUE
;
361 static int decompose_line_to(const FT_Vector
*to
, void *user
)
363 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
366 /* Special case for empty contours, in a way freetype returns them. */
367 if (ctxt
->move_to
&& !memcmp(to
, &ctxt
->origin
, sizeof(*to
)))
370 decompose_beginfigure(ctxt
);
372 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
373 ID2D1SimplifiedGeometrySink_AddLines(ctxt
->sink
, &point
, 1);
379 static int decompose_conic_to(const FT_Vector
*control
, const FT_Vector
*to
, void *user
)
381 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
382 D2D1_POINT_2F points
[3];
385 decompose_beginfigure(ctxt
);
387 /* convert from quadratic to cubic */
390 The parametric eqn for a cubic Bezier is, from PLRM:
391 r(t) = at^3 + bt^2 + ct + r0
392 with the control points:
397 A quadratic Bezier has the form:
398 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
400 So equating powers of t leads to:
403 and of course r0 = p0, r3 = p2
406 /* r1 = 1/3 p0 + 2/3 p1
407 r2 = 1/3 p2 + 2/3 p1 */
408 cubic
[0].x
= (2 * control
->x
+ 1) / 3;
409 cubic
[0].y
= (2 * control
->y
+ 1) / 3;
411 cubic
[0].x
+= (ctxt
->origin
.x
+ 1) / 3;
412 cubic
[0].y
+= (ctxt
->origin
.y
+ 1) / 3;
413 cubic
[1].x
+= (to
->x
+ 1) / 3;
414 cubic
[1].y
+= (to
->y
+ 1) / 3;
417 ft_vector_to_d2d_point(cubic
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
418 ft_vector_to_d2d_point(cubic
+ 1, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
419 ft_vector_to_d2d_point(cubic
+ 2, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
420 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
425 static int decompose_cubic_to(const FT_Vector
*control1
, const FT_Vector
*control2
,
426 const FT_Vector
*to
, void *user
)
428 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
429 D2D1_POINT_2F points
[3];
431 decompose_beginfigure(ctxt
);
433 ft_vector_to_d2d_point(control1
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
434 ft_vector_to_d2d_point(control2
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
435 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
436 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
441 static void decompose_outline(FT_Outline
*outline
, FLOAT xoffset
, FLOAT yoffset
, IDWriteGeometrySink
*sink
)
443 static const FT_Outline_Funcs decompose_funcs
= {
451 struct decompose_context context
;
454 context
.xoffset
= xoffset
;
455 context
.yoffset
= yoffset
;
456 context
.figure_started
= FALSE
;
457 context
.move_to
= FALSE
;
458 context
.origin
.x
= 0;
459 context
.origin
.y
= 0;
461 pFT_Outline_Decompose(outline
, &decompose_funcs
, &context
);
463 if (context
.figure_started
)
464 ID2D1SimplifiedGeometrySink_EndFigure(sink
, D2D1_FIGURE_END_CLOSED
);
467 static void embolden_glyph_outline(FT_Outline
*outline
, FLOAT emsize
)
471 strength
= MulDiv(emsize
, 1 << 6, 24);
472 if (pFT_Outline_EmboldenXY
)
473 pFT_Outline_EmboldenXY(outline
, strength
, 0);
475 pFT_Outline_Embolden(outline
, strength
);
478 static void embolden_glyph(FT_Glyph glyph
, FLOAT emsize
)
480 FT_OutlineGlyph outline_glyph
= (FT_OutlineGlyph
)glyph
;
482 if (glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
485 embolden_glyph_outline(&outline_glyph
->outline
, emsize
);
488 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16
const *glyphs
,
489 FLOAT
const *advances
, DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
491 FTC_ScalerRec scaler
;
499 ID2D1SimplifiedGeometrySink_SetFillMode(sink
, D2D1_FILL_MODE_WINDING
);
501 simulations
= IDWriteFontFace4_GetSimulations(fontface
);
503 scaler
.face_id
= fontface
;
504 scaler
.width
= emSize
;
505 scaler
.height
= emSize
;
510 EnterCriticalSection(&freetype_cs
);
511 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
512 FLOAT advance
= 0.0f
;
515 for (g
= 0; g
< count
; g
++) {
516 if (pFT_Load_Glyph(size
->face
, glyphs
[g
], FT_LOAD_NO_BITMAP
) == 0) {
517 FLOAT ft_advance
= size
->face
->glyph
->metrics
.horiAdvance
>> 6;
518 FT_Outline
*outline
= &size
->face
->glyph
->outline
;
519 FLOAT xoffset
= 0.0f
, yoffset
= 0.0f
;
522 if (simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
523 embolden_glyph_outline(outline
, emSize
);
526 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
528 m
.yy
= -(1 << 16); /* flip Y axis */
530 pFT_Outline_Transform(outline
, &m
);
532 /* glyph offsets act as current glyph adjustment */
534 xoffset
+= is_rtl
? -offsets
[g
].advanceOffset
: offsets
[g
].advanceOffset
;
535 yoffset
-= offsets
[g
].ascenderOffset
;
538 if (g
== 0 && is_rtl
)
539 advance
= advances
? -advances
[g
] : -ft_advance
;
542 decompose_outline(outline
, xoffset
, yoffset
, sink
);
544 /* update advance to next glyph */
546 advance
+= is_rtl
? -advances
[g
] : advances
[g
];
548 advance
+= is_rtl
? -ft_advance
: ft_advance
;
554 LeaveCriticalSection(&freetype_cs
);
559 UINT16
freetype_get_glyphcount(IDWriteFontFace4
*fontface
)
564 EnterCriticalSection(&freetype_cs
);
565 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
566 count
= face
->num_glyphs
;
567 LeaveCriticalSection(&freetype_cs
);
572 void freetype_get_glyphs(IDWriteFontFace4
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
577 EnterCriticalSection(&freetype_cs
);
578 for (i
= 0; i
< count
; i
++) {
580 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoints
[i
]);
582 UINT32 codepoint
= codepoints
[i
];
583 /* special handling for symbol fonts */
584 if (codepoint
< 0x100) codepoint
+= 0xf000;
585 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
);
587 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
- 0xf000);
590 LeaveCriticalSection(&freetype_cs
);
593 BOOL
freetype_has_kerning_pairs(IDWriteFontFace4
*fontface
)
595 BOOL has_kerning_pairs
= FALSE
;
598 EnterCriticalSection(&freetype_cs
);
599 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
600 has_kerning_pairs
= !!FT_HAS_KERNING(face
);
601 LeaveCriticalSection(&freetype_cs
);
603 return has_kerning_pairs
;
606 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace4
*fontface
, UINT16 left
, UINT16 right
)
608 INT32 adjustment
= 0;
611 EnterCriticalSection(&freetype_cs
);
612 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
614 if (FT_HAS_KERNING(face
)) {
615 pFT_Get_Kerning(face
, left
, right
, FT_KERNING_UNSCALED
, &kern
);
619 LeaveCriticalSection(&freetype_cs
);
624 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX
*m
, FT_Matrix
*ft_matrix
)
626 ft_matrix
->xx
= m
->m11
* 0x10000;
627 ft_matrix
->xy
= -m
->m21
* 0x10000;
628 ft_matrix
->yx
= -m
->m12
* 0x10000;
629 ft_matrix
->yy
= m
->m22
* 0x10000;
632 /* Should be used only while holding 'freetype_cs' */
633 static BOOL
is_face_scalable(IDWriteFontFace4
*fontface
)
636 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
637 return FT_IS_SCALABLE(face
);
642 static BOOL
get_glyph_transform(struct dwrite_glyphbitmap
*bitmap
, FT_Matrix
*ret
)
651 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
652 Disable transform if that's the case. */
653 if (!is_face_scalable(bitmap
->fontface
) || (!bitmap
->m
&& bitmap
->simulations
== 0))
656 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
) {
658 m
.xy
= (1 << 16) / 3;
661 pFT_Matrix_Multiply(&m
, ret
);
665 ft_matrix_from_dwrite_matrix(bitmap
->m
, &m
);
666 pFT_Matrix_Multiply(&m
, ret
);
672 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
674 FTC_ImageTypeRec imagetype
;
675 FT_BBox bbox
= { 0 };
676 BOOL needs_transform
;
680 EnterCriticalSection(&freetype_cs
);
682 needs_transform
= get_glyph_transform(bitmap
, &m
);
684 imagetype
.face_id
= bitmap
->fontface
;
686 imagetype
.height
= bitmap
->emsize
;
687 imagetype
.flags
= needs_transform
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
689 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
690 if (needs_transform
) {
693 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
694 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
695 embolden_glyph(glyph_copy
, bitmap
->emsize
);
697 /* Includes oblique and user transform. */
698 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
699 pFT_Glyph_Get_CBox(glyph_copy
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
700 pFT_Done_Glyph(glyph_copy
);
704 pFT_Glyph_Get_CBox(glyph
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
707 LeaveCriticalSection(&freetype_cs
);
710 SetRect(&bitmap
->bbox
, bbox
.xMin
, -bbox
.yMax
, bbox
.xMax
, -bbox
.yMin
);
713 void freetype_get_design_glyph_bbox(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, RECT
*bbox
)
715 FTC_ScalerRec scaler
;
718 scaler
.face_id
= fontface
;
719 scaler
.width
= unitsperEm
;
720 scaler
.height
= unitsperEm
;
725 EnterCriticalSection(&freetype_cs
);
726 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
727 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
728 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
730 bbox
->left
= metrics
->horiBearingX
;
731 bbox
->right
= bbox
->left
+ metrics
->horiAdvance
;
732 bbox
->top
= -metrics
->horiBearingY
;
733 bbox
->bottom
= bbox
->top
+ metrics
->height
;
736 LeaveCriticalSection(&freetype_cs
);
739 static BOOL
freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
741 const RECT
*bbox
= &bitmap
->bbox
;
742 int width
= bbox
->right
- bbox
->left
;
743 int height
= bbox
->bottom
- bbox
->top
;
745 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
746 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
747 const FT_Outline
*src
= &outline
->outline
;
751 ft_bitmap
.width
= width
;
752 ft_bitmap
.rows
= height
;
753 ft_bitmap
.pitch
= bitmap
->pitch
;
754 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
755 ft_bitmap
.buffer
= bitmap
->buf
;
757 /* Note: FreeType will only set 'black' bits for us. */
758 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
759 pFT_Outline_Copy(src
, ©
);
760 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
761 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
762 pFT_Outline_Done(library
, ©
);
765 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
766 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
767 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
768 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
769 int h
= min(height
, ft_bitmap
->rows
);
773 src
+= ft_bitmap
->pitch
;
774 dst
+= bitmap
->pitch
;
778 FIXME("format %x not handled\n", glyph
->format
);
783 static BOOL
freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
785 const RECT
*bbox
= &bitmap
->bbox
;
786 int width
= bbox
->right
- bbox
->left
;
787 int height
= bbox
->bottom
- bbox
->top
;
790 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
791 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
792 const FT_Outline
*src
= &outline
->outline
;
796 ft_bitmap
.width
= width
;
797 ft_bitmap
.rows
= height
;
798 ft_bitmap
.pitch
= bitmap
->pitch
;
799 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
800 ft_bitmap
.buffer
= bitmap
->buf
;
802 /* Note: FreeType will only set 'black' bits for us. */
803 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
804 pFT_Outline_Copy(src
, ©
);
805 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
806 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
807 pFT_Outline_Done(library
, ©
);
810 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
811 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
812 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
813 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
814 int h
= min(height
, ft_bitmap
->rows
);
818 src
+= ft_bitmap
->pitch
;
819 dst
+= bitmap
->pitch
;
825 FIXME("format %x not handled\n", glyph
->format
);
830 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
832 FTC_ImageTypeRec imagetype
;
833 BOOL needs_transform
;
838 EnterCriticalSection(&freetype_cs
);
840 needs_transform
= get_glyph_transform(bitmap
, &m
);
842 imagetype
.face_id
= bitmap
->fontface
;
844 imagetype
.height
= bitmap
->emsize
;
845 imagetype
.flags
= needs_transform
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
847 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
850 if (needs_transform
) {
851 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
852 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
853 embolden_glyph(glyph_copy
, bitmap
->emsize
);
855 /* Includes oblique and user transform. */
856 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
864 ret
= freetype_get_aliased_glyph_bitmap(bitmap
, glyph
);
866 ret
= freetype_get_aa_glyph_bitmap(bitmap
, glyph
);
869 pFT_Done_Glyph(glyph_copy
);
872 LeaveCriticalSection(&freetype_cs
);
877 INT
freetype_get_charmap_index(IDWriteFontFace4
*fontface
, BOOL
*is_symbol
)
879 INT charmap_index
= -1;
884 EnterCriticalSection(&freetype_cs
);
885 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
886 TT_OS2
*os2
= pFT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
891 if (os2
->version
== 0)
892 *is_symbol
= pFT_Get_First_Char(face
, &dummy
) >= 0x100;
894 *is_symbol
= !!(os2
->ulCodePageRange1
& FS_SYMBOL
);
897 for (i
= 0; i
< face
->num_charmaps
; i
++)
898 if (face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
) {
904 LeaveCriticalSection(&freetype_cs
);
906 return charmap_index
;
909 INT32
freetype_get_glyph_advance(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
,
912 FTC_ImageTypeRec imagetype
;
916 imagetype
.face_id
= fontface
;
918 imagetype
.height
= emSize
;
919 imagetype
.flags
= FT_LOAD_DEFAULT
;
920 if (mode
== DWRITE_MEASURING_MODE_NATURAL
)
921 imagetype
.flags
|= FT_LOAD_NO_HINTING
;
923 EnterCriticalSection(&freetype_cs
);
924 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, index
, &glyph
, NULL
) == 0) {
925 *has_contours
= glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
&& ((FT_OutlineGlyph
)glyph
)->outline
.n_contours
;
926 advance
= glyph
->advance
.x
>> 16;
929 *has_contours
= FALSE
;
932 LeaveCriticalSection(&freetype_cs
);
937 #else /* HAVE_FREETYPE */
939 BOOL
init_freetype(void)
944 void release_freetype(void)
948 void freetype_notify_cacheremove(IDWriteFontFace4
*fontface
)
952 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
957 BOOL
freetype_is_monospaced(IDWriteFontFace4
*fontface
)
962 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16
const *glyphs
, FLOAT
const *advances
,
963 DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
968 UINT16
freetype_get_glyphcount(IDWriteFontFace4
*fontface
)
973 void freetype_get_glyphs(IDWriteFontFace4
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
976 memset(glyphs
, 0, count
* sizeof(*glyphs
));
979 BOOL
freetype_has_kerning_pairs(IDWriteFontFace4
*fontface
)
984 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace4
*fontface
, UINT16 left
, UINT16 right
)
989 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
991 SetRectEmpty(&bitmap
->bbox
);
994 void freetype_get_design_glyph_bbox(IDWriteFontFace4
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, RECT
*bbox
)
999 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
1004 INT
freetype_get_charmap_index(IDWriteFontFace4
*fontface
, BOOL
*is_symbol
)
1010 INT32
freetype_get_glyph_advance(IDWriteFontFace4
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
,
1013 *has_contours
= FALSE
;
1017 #endif /* HAVE_FREETYPE */