4 * Copyright 2014 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 #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;
64 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
65 MAKE_FUNCPTR(FT_Done_FreeType
);
66 MAKE_FUNCPTR(FT_Get_Kerning
);
67 MAKE_FUNCPTR(FT_Init_FreeType
);
68 MAKE_FUNCPTR(FT_Library_Version
);
69 MAKE_FUNCPTR(FT_Load_Glyph
);
70 MAKE_FUNCPTR(FT_New_Memory_Face
);
71 MAKE_FUNCPTR(FT_Outline_Transform
);
72 MAKE_FUNCPTR(FTC_CMapCache_Lookup
);
73 MAKE_FUNCPTR(FTC_CMapCache_New
);
74 MAKE_FUNCPTR(FTC_Manager_New
);
75 MAKE_FUNCPTR(FTC_Manager_Done
);
76 MAKE_FUNCPTR(FTC_Manager_LookupFace
);
77 MAKE_FUNCPTR(FTC_Manager_LookupSize
);
78 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID
);
81 static FT_Error
face_requester(FTC_FaceID face_id
, FT_Library library
, FT_Pointer request_data
, FT_Face
*face
)
83 IDWriteFontFace
*fontface
= (IDWriteFontFace
*)face_id
;
84 IDWriteFontFileStream
*stream
;
85 IDWriteFontFile
*file
;
96 hr
= IDWriteFontFace_GetFiles(fontface
, &count
, &file
);
100 hr
= get_filestream_from_file(file
, &stream
);
101 IDWriteFontFile_Release(file
);
105 hr
= IDWriteFontFileStream_GetFileSize(stream
, &data_size
);
107 fterror
= FT_Err_Invalid_Stream_Read
;
111 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &data_ptr
, 0, data_size
, &context
);
113 fterror
= FT_Err_Invalid_Stream_Read
;
117 index
= IDWriteFontFace_GetIndex(fontface
);
118 fterror
= pFT_New_Memory_Face(library
, data_ptr
, data_size
, index
, face
);
119 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
122 IDWriteFontFileStream_Release(stream
);
127 BOOL
init_freetype(void)
129 FT_Version_t FT_Version
;
131 ft_handle
= wine_dlopen(SONAME_LIBFREETYPE
, RTLD_NOW
, NULL
, 0);
133 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
137 #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;}
138 LOAD_FUNCPTR(FT_Done_FreeType
)
139 LOAD_FUNCPTR(FT_Get_Kerning
)
140 LOAD_FUNCPTR(FT_Init_FreeType
)
141 LOAD_FUNCPTR(FT_Library_Version
)
142 LOAD_FUNCPTR(FT_Load_Glyph
)
143 LOAD_FUNCPTR(FT_New_Memory_Face
)
144 LOAD_FUNCPTR(FT_Outline_Transform
)
145 LOAD_FUNCPTR(FTC_CMapCache_Lookup
)
146 LOAD_FUNCPTR(FTC_CMapCache_New
)
147 LOAD_FUNCPTR(FTC_Manager_New
)
148 LOAD_FUNCPTR(FTC_Manager_Done
)
149 LOAD_FUNCPTR(FTC_Manager_LookupFace
)
150 LOAD_FUNCPTR(FTC_Manager_LookupSize
)
151 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID
)
154 if (pFT_Init_FreeType(&library
) != 0) {
155 ERR("Can't init FreeType library\n");
156 wine_dlclose(ft_handle
, NULL
, 0);
160 pFT_Library_Version(library
, &FT_Version
.major
, &FT_Version
.minor
, &FT_Version
.patch
);
162 /* init cache manager */
163 if (pFTC_Manager_New(library
, 0, 0, 0, &face_requester
, NULL
, &cache_manager
) != 0 ||
164 pFTC_CMapCache_New(cache_manager
, &cmap_cache
) != 0) {
166 ERR("Failed to init FreeType cache\n");
167 pFTC_Manager_Done(cache_manager
);
168 pFT_Done_FreeType(library
);
169 wine_dlclose(ft_handle
, NULL
, 0);
174 TRACE("FreeType version is %d.%d.%d\n", FT_Version
.major
, FT_Version
.minor
, FT_Version
.patch
);
178 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
179 wine_dlclose(ft_handle
, NULL
, 0);
184 void release_freetype(void)
186 pFTC_Manager_Done(cache_manager
);
187 pFT_Done_FreeType(library
);
190 void freetype_notify_cacheremove(IDWriteFontFace2
*fontface
)
192 EnterCriticalSection(&freetype_cs
);
193 pFTC_Manager_RemoveFaceID(cache_manager
, fontface
);
194 LeaveCriticalSection(&freetype_cs
);
197 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace2
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
199 FTC_ScalerRec scaler
;
202 scaler
.face_id
= fontface
;
203 scaler
.width
= unitsperEm
;
204 scaler
.height
= unitsperEm
;
209 EnterCriticalSection(&freetype_cs
);
210 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
211 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
212 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
214 ret
->leftSideBearing
= metrics
->horiBearingX
;
215 ret
->advanceWidth
= metrics
->horiAdvance
;
216 ret
->rightSideBearing
= metrics
->horiAdvance
- metrics
->horiBearingX
- metrics
->width
;
217 ret
->topSideBearing
= metrics
->vertBearingY
;
218 ret
->advanceHeight
= metrics
->vertAdvance
;
219 ret
->bottomSideBearing
= metrics
->vertAdvance
- metrics
->vertBearingY
- metrics
->height
;
220 ret
->verticalOriginY
= metrics
->height
+ metrics
->vertBearingY
;
223 LeaveCriticalSection(&freetype_cs
);
228 BOOL
freetype_is_monospaced(IDWriteFontFace2
*fontface
)
230 BOOL is_monospaced
= FALSE
;
233 EnterCriticalSection(&freetype_cs
);
234 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
235 is_monospaced
= FT_IS_FIXED_WIDTH(face
);
236 LeaveCriticalSection(&freetype_cs
);
238 return is_monospaced
;
241 static inline void ft_vector_to_d2d_point(const FT_Vector
*v
, D2D1_POINT_2F
*p
)
247 /* Convert the quadratic Beziers to cubic Beziers. */
248 static void get_cubic_glyph_outline(const FT_Outline
*outline
, short point
, short first_pt
,
249 short contour
, FT_Vector
*cubic_control
)
252 The parametric eqn for a cubic Bezier is, from PLRM:
253 r(t) = at^3 + bt^2 + ct + r0
254 with the control points:
259 A quadratic Bezier has the form:
260 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
262 So equating powers of t leads to:
265 and of course r0 = p0, r3 = p2
268 /* FIXME: Possible optimization in endpoint calculation
269 if there are two consecutive curves */
270 cubic_control
[0] = outline
->points
[point
-1];
271 if (!(outline
->tags
[point
-1] & FT_Curve_Tag_On
)) {
272 cubic_control
[0].x
+= outline
->points
[point
].x
+ 1;
273 cubic_control
[0].y
+= outline
->points
[point
].y
+ 1;
274 cubic_control
[0].x
>>= 1;
275 cubic_control
[0].y
>>= 1;
277 if (point
+1 > outline
->contours
[contour
])
278 cubic_control
[3] = outline
->points
[first_pt
];
280 cubic_control
[3] = outline
->points
[point
+1];
281 if (!(outline
->tags
[point
+1] & FT_Curve_Tag_On
)) {
282 cubic_control
[3].x
+= outline
->points
[point
].x
+ 1;
283 cubic_control
[3].y
+= outline
->points
[point
].y
+ 1;
284 cubic_control
[3].x
>>= 1;
285 cubic_control
[3].y
>>= 1;
289 /* r1 = 1/3 p0 + 2/3 p1
290 r2 = 1/3 p2 + 2/3 p1 */
291 cubic_control
[1].x
= (2 * outline
->points
[point
].x
+ 1) / 3;
292 cubic_control
[1].y
= (2 * outline
->points
[point
].y
+ 1) / 3;
293 cubic_control
[2] = cubic_control
[1];
294 cubic_control
[1].x
+= (cubic_control
[0].x
+ 1) / 3;
295 cubic_control
[1].y
+= (cubic_control
[0].y
+ 1) / 3;
296 cubic_control
[2].x
+= (cubic_control
[3].x
+ 1) / 3;
297 cubic_control
[2].y
+= (cubic_control
[3].y
+ 1) / 3;
300 static inline void set_outline_end_tag(short point
, short endpoint
, UINT8
*tag
)
302 if (point
== endpoint
)
303 *tag
|= OUTLINE_POINT_END
;
306 static short get_outline_data(const FT_Outline
*outline
, struct glyph_outline
*ret
)
308 short contour
, point
= 0, first_pt
, count
;
310 for (contour
= 0, count
= 0; contour
< outline
->n_contours
; contour
++) {
313 ft_vector_to_d2d_point(&outline
->points
[point
], &ret
->points
[count
]);
314 ret
->tags
[count
] = OUTLINE_POINT_START
;
320 while (point
<= outline
->contours
[contour
]) {
322 if (outline
->tags
[point
] & FT_Curve_Tag_On
) {
324 ft_vector_to_d2d_point(&outline
->points
[point
], &ret
->points
[count
]);
325 ret
->tags
[count
] |= OUTLINE_POINT_LINE
;
326 set_outline_end_tag(point
, outline
->contours
[contour
], &ret
->tags
[count
]);
335 FT_Vector cubic_control
[4];
337 get_cubic_glyph_outline(outline
, point
, first_pt
, contour
, cubic_control
);
338 ft_vector_to_d2d_point(&cubic_control
[1], &ret
->points
[count
]);
339 ft_vector_to_d2d_point(&cubic_control
[2], &ret
->points
[count
+1]);
340 ft_vector_to_d2d_point(&cubic_control
[3], &ret
->points
[count
+2]);
341 ret
->tags
[count
] = OUTLINE_POINT_BEZIER
;
342 ret
->tags
[count
+1] = OUTLINE_POINT_BEZIER
;
343 ret
->tags
[count
+2] = OUTLINE_POINT_BEZIER
;
344 set_outline_end_tag(point
, outline
->contours
[contour
], &ret
->tags
[count
+2]);
350 } while (point
<= outline
->contours
[contour
] &&
351 (outline
->tags
[point
] & FT_Curve_Tag_On
) ==
352 (outline
->tags
[point
-1] & FT_Curve_Tag_On
));
354 if (point
<= outline
->contours
[contour
] &&
355 outline
->tags
[point
] & FT_Curve_Tag_On
)
357 /* This is the closing pt of a bezier, but we've already
358 added it, so just inc point and carry on */
367 HRESULT
freetype_get_glyph_outline(IDWriteFontFace2
*fontface
, FLOAT emSize
, UINT16 index
, USHORT simulations
, struct glyph_outline
**ret
)
369 FTC_ScalerRec scaler
;
373 scaler
.face_id
= fontface
;
374 scaler
.width
= emSize
;
375 scaler
.height
= emSize
;
380 EnterCriticalSection(&freetype_cs
);
381 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
382 if (pFT_Load_Glyph(size
->face
, index
, FT_LOAD_DEFAULT
) == 0) {
383 FT_Outline
*outline
= &size
->face
->glyph
->outline
;
388 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
390 m
.yy
= -(1 << 16); /* flip Y axis */
392 pFT_Outline_Transform(outline
, &m
);
394 count
= get_outline_data(outline
, NULL
);
395 hr
= new_glyph_outline(count
, ret
);
397 get_outline_data(outline
, *ret
);
398 (*ret
)->advance
= size
->face
->glyph
->metrics
.horiAdvance
>> 6;
402 LeaveCriticalSection(&freetype_cs
);
407 UINT16
freetype_get_glyphcount(IDWriteFontFace2
*fontface
)
412 EnterCriticalSection(&freetype_cs
);
413 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
414 count
= face
->num_glyphs
;
415 LeaveCriticalSection(&freetype_cs
);
420 UINT16
freetype_get_glyphindex(IDWriteFontFace2
*fontface
, UINT32 codepoint
)
424 EnterCriticalSection(&freetype_cs
);
425 glyph
= pFTC_CMapCache_Lookup(cmap_cache
, fontface
, -1, codepoint
);
426 LeaveCriticalSection(&freetype_cs
);
431 BOOL
freetype_has_kerning_pairs(IDWriteFontFace2
*fontface
)
433 BOOL has_kerning_pairs
= FALSE
;
436 EnterCriticalSection(&freetype_cs
);
437 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
438 has_kerning_pairs
= FT_HAS_KERNING(face
);
439 LeaveCriticalSection(&freetype_cs
);
441 return has_kerning_pairs
;
444 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace2
*fontface
, UINT16 left
, UINT16 right
)
446 INT32 adjustment
= 0;
449 EnterCriticalSection(&freetype_cs
);
450 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
452 if (FT_HAS_KERNING(face
)) {
453 pFT_Get_Kerning(face
, left
, right
, FT_KERNING_UNSCALED
, &kern
);
457 LeaveCriticalSection(&freetype_cs
);
462 #else /* HAVE_FREETYPE */
464 BOOL
init_freetype(void)
469 void release_freetype(void)
473 void freetype_notify_cacheremove(IDWriteFontFace2
*fontface
)
477 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace2
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
482 BOOL
freetype_is_monospaced(IDWriteFontFace2
*fontface
)
487 HRESULT
freetype_get_glyph_outline(IDWriteFontFace2
*fontface
, FLOAT emSize
, UINT16 index
, USHORT simulations
, struct glyph_outline
**ret
)
493 UINT16
freetype_get_glyphcount(IDWriteFontFace2
*fontface
)
498 UINT16
freetype_get_glyphindex(IDWriteFontFace2
*fontface
, UINT32 codepoint
)
503 BOOL
freetype_has_kerning_pairs(IDWriteFontFace2
*fontface
)
508 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace2
*fontface
, UINT16 left
, UINT16 right
)
513 #endif /* HAVE_FREETYPE */