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 static FT_Error
face_requester(FTC_FaceID face_id
, FT_Library library
, FT_Pointer request_data
, FT_Face
*face
)
98 IDWriteFontFace
*fontface
= (IDWriteFontFace
*)face_id
;
99 IDWriteFontFileStream
*stream
;
100 IDWriteFontFile
*file
;
101 const void *data_ptr
;
111 hr
= IDWriteFontFace_GetFiles(fontface
, &count
, &file
);
115 hr
= get_filestream_from_file(file
, &stream
);
116 IDWriteFontFile_Release(file
);
120 hr
= IDWriteFontFileStream_GetFileSize(stream
, &data_size
);
122 fterror
= FT_Err_Invalid_Stream_Read
;
126 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &data_ptr
, 0, data_size
, &context
);
128 fterror
= FT_Err_Invalid_Stream_Read
;
132 index
= IDWriteFontFace_GetIndex(fontface
);
133 fterror
= pFT_New_Memory_Face(library
, data_ptr
, data_size
, index
, face
);
134 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
137 IDWriteFontFileStream_Release(stream
);
142 BOOL
init_freetype(void)
144 FT_Version_t FT_Version
;
146 ft_handle
= wine_dlopen(SONAME_LIBFREETYPE
, RTLD_NOW
, NULL
, 0);
148 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
152 #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;}
153 LOAD_FUNCPTR(FT_Done_FreeType
)
154 LOAD_FUNCPTR(FT_Done_Glyph
)
155 LOAD_FUNCPTR(FT_Get_First_Char
)
156 LOAD_FUNCPTR(FT_Get_Kerning
)
157 LOAD_FUNCPTR(FT_Get_Sfnt_Table
)
158 LOAD_FUNCPTR(FT_Glyph_Copy
)
159 LOAD_FUNCPTR(FT_Glyph_Get_CBox
)
160 LOAD_FUNCPTR(FT_Glyph_Transform
)
161 LOAD_FUNCPTR(FT_Init_FreeType
)
162 LOAD_FUNCPTR(FT_Library_Version
)
163 LOAD_FUNCPTR(FT_Load_Glyph
)
164 LOAD_FUNCPTR(FT_New_Memory_Face
)
165 LOAD_FUNCPTR(FT_Outline_Copy
)
166 LOAD_FUNCPTR(FT_Outline_Decompose
)
167 LOAD_FUNCPTR(FT_Outline_Done
)
168 LOAD_FUNCPTR(FT_Outline_Get_Bitmap
)
169 LOAD_FUNCPTR(FT_Outline_New
)
170 LOAD_FUNCPTR(FT_Outline_Transform
)
171 LOAD_FUNCPTR(FT_Outline_Translate
)
172 LOAD_FUNCPTR(FTC_CMapCache_Lookup
)
173 LOAD_FUNCPTR(FTC_CMapCache_New
)
174 LOAD_FUNCPTR(FTC_ImageCache_Lookup
)
175 LOAD_FUNCPTR(FTC_ImageCache_New
)
176 LOAD_FUNCPTR(FTC_Manager_New
)
177 LOAD_FUNCPTR(FTC_Manager_Done
)
178 LOAD_FUNCPTR(FTC_Manager_LookupFace
)
179 LOAD_FUNCPTR(FTC_Manager_LookupSize
)
180 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID
)
183 if (pFT_Init_FreeType(&library
) != 0) {
184 ERR("Can't init FreeType library\n");
185 wine_dlclose(ft_handle
, NULL
, 0);
189 pFT_Library_Version(library
, &FT_Version
.major
, &FT_Version
.minor
, &FT_Version
.patch
);
191 /* init cache manager */
192 if (pFTC_Manager_New(library
, 0, 0, 0, &face_requester
, NULL
, &cache_manager
) != 0 ||
193 pFTC_CMapCache_New(cache_manager
, &cmap_cache
) != 0 ||
194 pFTC_ImageCache_New(cache_manager
, &image_cache
) != 0) {
196 ERR("Failed to init FreeType cache\n");
197 pFTC_Manager_Done(cache_manager
);
198 pFT_Done_FreeType(library
);
199 wine_dlclose(ft_handle
, NULL
, 0);
204 TRACE("FreeType version is %d.%d.%d\n", FT_Version
.major
, FT_Version
.minor
, FT_Version
.patch
);
208 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
209 wine_dlclose(ft_handle
, NULL
, 0);
214 void release_freetype(void)
216 pFTC_Manager_Done(cache_manager
);
217 pFT_Done_FreeType(library
);
220 void freetype_notify_cacheremove(IDWriteFontFace3
*fontface
)
222 EnterCriticalSection(&freetype_cs
);
223 pFTC_Manager_RemoveFaceID(cache_manager
, fontface
);
224 LeaveCriticalSection(&freetype_cs
);
227 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace3
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
229 FTC_ScalerRec scaler
;
232 scaler
.face_id
= fontface
;
233 scaler
.width
= unitsperEm
;
234 scaler
.height
= unitsperEm
;
239 EnterCriticalSection(&freetype_cs
);
240 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
241 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
242 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
244 ret
->leftSideBearing
= metrics
->horiBearingX
;
245 ret
->advanceWidth
= metrics
->horiAdvance
;
246 ret
->rightSideBearing
= metrics
->horiAdvance
- metrics
->horiBearingX
- metrics
->width
;
247 ret
->topSideBearing
= metrics
->vertBearingY
;
248 ret
->advanceHeight
= metrics
->vertAdvance
;
249 ret
->bottomSideBearing
= metrics
->vertAdvance
- metrics
->vertBearingY
- metrics
->height
;
250 ret
->verticalOriginY
= metrics
->height
+ metrics
->vertBearingY
;
253 LeaveCriticalSection(&freetype_cs
);
258 BOOL
freetype_is_monospaced(IDWriteFontFace3
*fontface
)
260 BOOL is_monospaced
= FALSE
;
263 EnterCriticalSection(&freetype_cs
);
264 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
265 is_monospaced
= !!FT_IS_FIXED_WIDTH(face
);
266 LeaveCriticalSection(&freetype_cs
);
268 return is_monospaced
;
271 struct decompose_context
{
272 IDWriteGeometrySink
*sink
;
277 BOOL move_to
; /* last call was 'move_to' */
278 FT_Vector origin
; /* 'pen' position from last call */
281 static inline void ft_vector_to_d2d_point(const FT_Vector
*v
, FLOAT xoffset
, FLOAT yoffset
, D2D1_POINT_2F
*p
)
283 p
->x
= (v
->x
/ 64.0f
) + xoffset
;
284 p
->y
= (v
->y
/ 64.0f
) + yoffset
;
287 static int decompose_move_to(const FT_Vector
*to
, void *user
)
289 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
292 if (ctxt
->figure_started
) {
293 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
294 ctxt
->figure_closed
= TRUE
;
297 ctxt
->figure_closed
= FALSE
;
298 ctxt
->figure_started
= TRUE
;
300 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
301 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt
->sink
, point
, D2D1_FIGURE_BEGIN_FILLED
);
302 ctxt
->move_to
= TRUE
;
307 static int decompose_line_to(const FT_Vector
*to
, void *user
)
309 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
310 /* special case for empty contours, in a way freetype returns them */
311 if (ctxt
->move_to
&& !memcmp(to
, &ctxt
->origin
, sizeof(*to
))) {
312 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
313 ctxt
->figure_closed
= TRUE
;
317 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
318 ID2D1SimplifiedGeometrySink_AddLines(ctxt
->sink
, &point
, 1);
319 ctxt
->figure_closed
= FALSE
;
321 ctxt
->move_to
= FALSE
;
326 static int decompose_conic_to(const FT_Vector
*control
, const FT_Vector
*to
, void *user
)
328 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
329 D2D1_POINT_2F points
[3];
332 /* convert from quadratic to cubic */
335 The parametric eqn for a cubic Bezier is, from PLRM:
336 r(t) = at^3 + bt^2 + ct + r0
337 with the control points:
342 A quadratic Bezier has the form:
343 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
345 So equating powers of t leads to:
348 and of course r0 = p0, r3 = p2
351 /* r1 = 1/3 p0 + 2/3 p1
352 r2 = 1/3 p2 + 2/3 p1 */
353 cubic
[0].x
= (2 * control
->x
+ 1) / 3;
354 cubic
[0].y
= (2 * control
->y
+ 1) / 3;
356 cubic
[0].x
+= (ctxt
->origin
.x
+ 1) / 3;
357 cubic
[0].y
+= (ctxt
->origin
.y
+ 1) / 3;
358 cubic
[1].x
+= (to
->x
+ 1) / 3;
359 cubic
[1].y
+= (to
->y
+ 1) / 3;
362 ft_vector_to_d2d_point(cubic
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
363 ft_vector_to_d2d_point(cubic
+ 1, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
364 ft_vector_to_d2d_point(cubic
+ 2, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
365 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
366 ctxt
->figure_closed
= FALSE
;
367 ctxt
->move_to
= FALSE
;
372 static int decompose_cubic_to(const FT_Vector
*control1
, const FT_Vector
*control2
,
373 const FT_Vector
*to
, void *user
)
375 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
376 D2D1_POINT_2F points
[3];
378 ft_vector_to_d2d_point(control1
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
379 ft_vector_to_d2d_point(control2
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
380 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
381 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
382 ctxt
->figure_closed
= FALSE
;
383 ctxt
->move_to
= FALSE
;
388 static void decompose_outline(FT_Outline
*outline
, FLOAT xoffset
, FLOAT yoffset
, IDWriteGeometrySink
*sink
)
390 static const FT_Outline_Funcs decompose_funcs
= {
398 struct decompose_context context
;
401 context
.xoffset
= xoffset
;
402 context
.yoffset
= yoffset
;
403 context
.figure_started
= FALSE
;
404 context
.figure_closed
= FALSE
;
405 context
.move_to
= FALSE
;
406 context
.origin
.x
= 0;
407 context
.origin
.y
= 0;
409 pFT_Outline_Decompose(outline
, &decompose_funcs
, &context
);
411 if (!context
.figure_closed
&& outline
->n_points
)
412 ID2D1SimplifiedGeometrySink_EndFigure(sink
, D2D1_FIGURE_END_CLOSED
);
415 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace3
*fontface
, FLOAT emSize
, UINT16
const *glyphs
, FLOAT
const *advances
,
416 DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
418 FTC_ScalerRec scaler
;
426 ID2D1SimplifiedGeometrySink_SetFillMode(sink
, D2D1_FILL_MODE_WINDING
);
428 simulations
= IDWriteFontFace3_GetSimulations(fontface
);
430 scaler
.face_id
= fontface
;
431 scaler
.width
= emSize
;
432 scaler
.height
= emSize
;
437 EnterCriticalSection(&freetype_cs
);
438 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
439 FLOAT advance
= 0.0f
;
442 for (g
= 0; g
< count
; g
++) {
443 if (pFT_Load_Glyph(size
->face
, glyphs
[g
], FT_LOAD_NO_BITMAP
) == 0) {
444 FLOAT ft_advance
= size
->face
->glyph
->metrics
.horiAdvance
>> 6;
445 FT_Outline
*outline
= &size
->face
->glyph
->outline
;
446 FLOAT xoffset
= 0.0f
, yoffset
= 0.0f
;
450 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
452 m
.yy
= -(1 << 16); /* flip Y axis */
454 pFT_Outline_Transform(outline
, &m
);
456 /* glyph offsets act as current glyph adjustment */
458 xoffset
+= is_rtl
? -offsets
[g
].advanceOffset
: offsets
[g
].advanceOffset
;
459 yoffset
-= offsets
[g
].ascenderOffset
;
462 if (g
== 0 && is_rtl
)
463 advance
= advances
? -advances
[g
] : -ft_advance
;
466 decompose_outline(outline
, xoffset
, yoffset
, sink
);
468 /* update advance to next glyph */
470 advance
+= is_rtl
? -advances
[g
] : advances
[g
];
472 advance
+= is_rtl
? -ft_advance
: ft_advance
;
478 LeaveCriticalSection(&freetype_cs
);
483 UINT16
freetype_get_glyphcount(IDWriteFontFace3
*fontface
)
488 EnterCriticalSection(&freetype_cs
);
489 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
490 count
= face
->num_glyphs
;
491 LeaveCriticalSection(&freetype_cs
);
496 void freetype_get_glyphs(IDWriteFontFace3
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
501 EnterCriticalSection(&freetype_cs
);
502 for (i
= 0; i
< count
; i
++) {
504 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoints
[i
]);
506 UINT32 codepoint
= codepoints
[i
];
507 /* special handling for symbol fonts */
508 if (codepoint
< 0x100) codepoint
+= 0xf000;
509 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
);
511 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
- 0xf000);
514 LeaveCriticalSection(&freetype_cs
);
517 BOOL
freetype_has_kerning_pairs(IDWriteFontFace3
*fontface
)
519 BOOL has_kerning_pairs
= FALSE
;
522 EnterCriticalSection(&freetype_cs
);
523 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
524 has_kerning_pairs
= !!FT_HAS_KERNING(face
);
525 LeaveCriticalSection(&freetype_cs
);
527 return has_kerning_pairs
;
530 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace3
*fontface
, UINT16 left
, UINT16 right
)
532 INT32 adjustment
= 0;
535 EnterCriticalSection(&freetype_cs
);
536 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
538 if (FT_HAS_KERNING(face
)) {
539 pFT_Get_Kerning(face
, left
, right
, FT_KERNING_UNSCALED
, &kern
);
543 LeaveCriticalSection(&freetype_cs
);
548 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX
*m
, FT_Matrix
*ft_matrix
)
550 ft_matrix
->xx
= m
->m11
* 0x10000;
551 ft_matrix
->xy
= -m
->m21
* 0x10000;
552 ft_matrix
->yx
= -m
->m12
* 0x10000;
553 ft_matrix
->yy
= m
->m22
* 0x10000;
556 /* Should be used only while holding 'freetype_cs' */
557 static BOOL
is_face_scalable(IDWriteFontFace3
*fontface
)
560 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
561 return FT_IS_SCALABLE(face
);
566 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
568 FTC_ImageTypeRec imagetype
;
569 FT_BBox bbox
= { 0 };
572 EnterCriticalSection(&freetype_cs
);
574 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef,
575 disable transform if that's the case. */
577 if (!is_face_scalable(bitmap
->fontface
))
581 imagetype
.face_id
= bitmap
->fontface
;
583 imagetype
.height
= bitmap
->emsize
;
584 imagetype
.flags
= bitmap
->m
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
586 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
590 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
593 ft_matrix_from_dwrite_matrix(bitmap
->m
, &ft_matrix
);
594 pFT_Glyph_Transform(glyph_copy
, &ft_matrix
, NULL
);
595 pFT_Glyph_Get_CBox(glyph_copy
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
596 pFT_Done_Glyph(glyph_copy
);
600 pFT_Glyph_Get_CBox(glyph
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
603 LeaveCriticalSection(&freetype_cs
);
606 bitmap
->bbox
.left
= bbox
.xMin
;
607 bitmap
->bbox
.right
= bbox
.xMax
;
608 bitmap
->bbox
.top
= -bbox
.yMax
;
609 bitmap
->bbox
.bottom
= -bbox
.yMin
;
612 static BOOL
freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
614 const RECT
*bbox
= &bitmap
->bbox
;
615 int width
= bbox
->right
- bbox
->left
;
616 int height
= bbox
->bottom
- bbox
->top
;
618 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
619 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
620 const FT_Outline
*src
= &outline
->outline
;
624 ft_bitmap
.width
= width
;
625 ft_bitmap
.rows
= height
;
626 ft_bitmap
.pitch
= bitmap
->pitch
;
627 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
628 ft_bitmap
.buffer
= bitmap
->buf
;
630 /* Note: FreeType will only set 'black' bits for us. */
631 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
632 pFT_Outline_Copy(src
, ©
);
633 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
634 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
635 pFT_Outline_Done(library
, ©
);
638 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
639 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
640 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
641 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
642 int h
= min(height
, ft_bitmap
->rows
);
646 src
+= ft_bitmap
->pitch
;
647 dst
+= bitmap
->pitch
;
651 FIXME("format %x not handled\n", glyph
->format
);
656 static BOOL
freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
658 const RECT
*bbox
= &bitmap
->bbox
;
659 int width
= bbox
->right
- bbox
->left
;
660 int height
= bbox
->bottom
- bbox
->top
;
663 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
664 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
665 const FT_Outline
*src
= &outline
->outline
;
669 ft_bitmap
.width
= width
;
670 ft_bitmap
.rows
= height
;
671 ft_bitmap
.pitch
= bitmap
->pitch
;
672 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
673 ft_bitmap
.buffer
= bitmap
->buf
;
675 /* Note: FreeType will only set 'black' bits for us. */
676 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
677 pFT_Outline_Copy(src
, ©
);
678 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
679 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
680 pFT_Outline_Done(library
, ©
);
683 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
684 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
685 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
686 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
687 int h
= min(height
, ft_bitmap
->rows
);
691 src
+= ft_bitmap
->pitch
;
692 dst
+= bitmap
->pitch
;
698 FIXME("format %x not handled\n", glyph
->format
);
703 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
705 FTC_ImageTypeRec imagetype
;
709 EnterCriticalSection(&freetype_cs
);
712 if (!is_face_scalable(bitmap
->fontface
))
716 imagetype
.face_id
= bitmap
->fontface
;
718 imagetype
.height
= bitmap
->emsize
;
719 imagetype
.flags
= bitmap
->m
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
721 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
725 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
728 ft_matrix_from_dwrite_matrix(bitmap
->m
, &ft_matrix
);
729 pFT_Glyph_Transform(glyph_copy
, &ft_matrix
, NULL
);
736 if (bitmap
->type
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
737 ret
= freetype_get_aa_glyph_bitmap(bitmap
, glyph
);
739 ret
= freetype_get_aliased_glyph_bitmap(bitmap
, glyph
);
742 pFT_Done_Glyph(glyph_copy
);
745 LeaveCriticalSection(&freetype_cs
);
750 INT
freetype_get_charmap_index(IDWriteFontFace3
*fontface
, BOOL
*is_symbol
)
752 INT charmap_index
= -1;
757 EnterCriticalSection(&freetype_cs
);
758 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
759 TT_OS2
*os2
= pFT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
764 if (os2
->version
== 0)
765 *is_symbol
= pFT_Get_First_Char(face
, &dummy
) >= 0x100;
767 *is_symbol
= !!(os2
->ulCodePageRange1
& FS_SYMBOL
);
770 for (i
= 0; i
< face
->num_charmaps
; i
++)
771 if (face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
) {
777 LeaveCriticalSection(&freetype_cs
);
779 return charmap_index
;
782 INT32
freetype_get_glyph_advance(IDWriteFontFace3
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
)
784 FTC_ImageTypeRec imagetype
;
788 imagetype
.face_id
= fontface
;
790 imagetype
.height
= emSize
;
791 imagetype
.flags
= FT_LOAD_DEFAULT
;
792 if (mode
== DWRITE_MEASURING_MODE_NATURAL
)
793 imagetype
.flags
|= FT_LOAD_NO_HINTING
;
795 EnterCriticalSection(&freetype_cs
);
796 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, index
, &glyph
, NULL
) == 0)
797 advance
= glyph
->advance
.x
>> 16;
800 LeaveCriticalSection(&freetype_cs
);
805 #else /* HAVE_FREETYPE */
807 BOOL
init_freetype(void)
812 void release_freetype(void)
816 void freetype_notify_cacheremove(IDWriteFontFace3
*fontface
)
820 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace3
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
825 BOOL
freetype_is_monospaced(IDWriteFontFace3
*fontface
)
830 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace3
*fontface
, FLOAT emSize
, UINT16
const *glyphs
, FLOAT
const *advances
,
831 DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
836 UINT16
freetype_get_glyphcount(IDWriteFontFace3
*fontface
)
841 void freetype_get_glyphs(IDWriteFontFace3
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
844 memset(glyphs
, 0, count
* sizeof(*glyphs
));
847 BOOL
freetype_has_kerning_pairs(IDWriteFontFace3
*fontface
)
852 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace3
*fontface
, UINT16 left
, UINT16 right
)
857 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
859 memset(&bitmap
->bbox
, 0, sizeof(bitmap
->bbox
));
862 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
867 INT
freetype_get_charmap_index(IDWriteFontFace3
*fontface
, BOOL
*is_symbol
)
873 INT32
freetype_get_glyph_advance(IDWriteFontFace3
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
)
878 #endif /* HAVE_FREETYPE */