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 WARN("NULL fontface requested.\n");
116 hr
= IDWriteFontFace_GetFiles(fontface
, &count
, &file
);
120 hr
= get_filestream_from_file(file
, &stream
);
121 IDWriteFontFile_Release(file
);
125 hr
= IDWriteFontFileStream_GetFileSize(stream
, &data_size
);
127 fterror
= FT_Err_Invalid_Stream_Read
;
131 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &data_ptr
, 0, data_size
, &context
);
133 fterror
= FT_Err_Invalid_Stream_Read
;
137 index
= IDWriteFontFace_GetIndex(fontface
);
138 fterror
= pFT_New_Memory_Face(library
, data_ptr
, data_size
, index
, face
);
139 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
142 IDWriteFontFileStream_Release(stream
);
147 BOOL
init_freetype(void)
149 FT_Version_t FT_Version
;
151 ft_handle
= wine_dlopen(SONAME_LIBFREETYPE
, RTLD_NOW
, NULL
, 0);
153 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
157 #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;}
158 LOAD_FUNCPTR(FT_Done_FreeType
)
159 LOAD_FUNCPTR(FT_Done_Glyph
)
160 LOAD_FUNCPTR(FT_Get_First_Char
)
161 LOAD_FUNCPTR(FT_Get_Kerning
)
162 LOAD_FUNCPTR(FT_Get_Sfnt_Table
)
163 LOAD_FUNCPTR(FT_Glyph_Copy
)
164 LOAD_FUNCPTR(FT_Glyph_Get_CBox
)
165 LOAD_FUNCPTR(FT_Glyph_Transform
)
166 LOAD_FUNCPTR(FT_Init_FreeType
)
167 LOAD_FUNCPTR(FT_Library_Version
)
168 LOAD_FUNCPTR(FT_Load_Glyph
)
169 LOAD_FUNCPTR(FT_New_Memory_Face
)
170 LOAD_FUNCPTR(FT_Outline_Copy
)
171 LOAD_FUNCPTR(FT_Outline_Decompose
)
172 LOAD_FUNCPTR(FT_Outline_Done
)
173 LOAD_FUNCPTR(FT_Outline_Get_Bitmap
)
174 LOAD_FUNCPTR(FT_Outline_New
)
175 LOAD_FUNCPTR(FT_Outline_Transform
)
176 LOAD_FUNCPTR(FT_Outline_Translate
)
177 LOAD_FUNCPTR(FTC_CMapCache_Lookup
)
178 LOAD_FUNCPTR(FTC_CMapCache_New
)
179 LOAD_FUNCPTR(FTC_ImageCache_Lookup
)
180 LOAD_FUNCPTR(FTC_ImageCache_New
)
181 LOAD_FUNCPTR(FTC_Manager_New
)
182 LOAD_FUNCPTR(FTC_Manager_Done
)
183 LOAD_FUNCPTR(FTC_Manager_LookupFace
)
184 LOAD_FUNCPTR(FTC_Manager_LookupSize
)
185 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID
)
188 if (pFT_Init_FreeType(&library
) != 0) {
189 ERR("Can't init FreeType library\n");
190 wine_dlclose(ft_handle
, NULL
, 0);
194 pFT_Library_Version(library
, &FT_Version
.major
, &FT_Version
.minor
, &FT_Version
.patch
);
196 /* init cache manager */
197 if (pFTC_Manager_New(library
, 0, 0, 0, &face_requester
, NULL
, &cache_manager
) != 0 ||
198 pFTC_CMapCache_New(cache_manager
, &cmap_cache
) != 0 ||
199 pFTC_ImageCache_New(cache_manager
, &image_cache
) != 0) {
201 ERR("Failed to init FreeType cache\n");
202 pFTC_Manager_Done(cache_manager
);
203 pFT_Done_FreeType(library
);
204 wine_dlclose(ft_handle
, NULL
, 0);
209 TRACE("FreeType version is %d.%d.%d\n", FT_Version
.major
, FT_Version
.minor
, FT_Version
.patch
);
213 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
214 wine_dlclose(ft_handle
, NULL
, 0);
219 void release_freetype(void)
221 pFTC_Manager_Done(cache_manager
);
222 pFT_Done_FreeType(library
);
225 void freetype_notify_cacheremove(IDWriteFontFace3
*fontface
)
227 EnterCriticalSection(&freetype_cs
);
228 pFTC_Manager_RemoveFaceID(cache_manager
, fontface
);
229 LeaveCriticalSection(&freetype_cs
);
232 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace3
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
234 FTC_ScalerRec scaler
;
237 scaler
.face_id
= fontface
;
238 scaler
.width
= unitsperEm
;
239 scaler
.height
= unitsperEm
;
244 EnterCriticalSection(&freetype_cs
);
245 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
246 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
247 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
249 ret
->leftSideBearing
= metrics
->horiBearingX
;
250 ret
->advanceWidth
= metrics
->horiAdvance
;
251 ret
->rightSideBearing
= metrics
->horiAdvance
- metrics
->horiBearingX
- metrics
->width
;
252 ret
->topSideBearing
= metrics
->vertBearingY
;
253 ret
->advanceHeight
= metrics
->vertAdvance
;
254 ret
->bottomSideBearing
= metrics
->vertAdvance
- metrics
->vertBearingY
- metrics
->height
;
255 ret
->verticalOriginY
= metrics
->height
+ metrics
->vertBearingY
;
258 LeaveCriticalSection(&freetype_cs
);
263 BOOL
freetype_is_monospaced(IDWriteFontFace3
*fontface
)
265 BOOL is_monospaced
= FALSE
;
268 EnterCriticalSection(&freetype_cs
);
269 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
270 is_monospaced
= !!FT_IS_FIXED_WIDTH(face
);
271 LeaveCriticalSection(&freetype_cs
);
273 return is_monospaced
;
276 struct decompose_context
{
277 IDWriteGeometrySink
*sink
;
282 BOOL move_to
; /* last call was 'move_to' */
283 FT_Vector origin
; /* 'pen' position from last call */
286 static inline void ft_vector_to_d2d_point(const FT_Vector
*v
, FLOAT xoffset
, FLOAT yoffset
, D2D1_POINT_2F
*p
)
288 p
->x
= (v
->x
/ 64.0f
) + xoffset
;
289 p
->y
= (v
->y
/ 64.0f
) + yoffset
;
292 static int decompose_move_to(const FT_Vector
*to
, void *user
)
294 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
297 if (ctxt
->figure_started
) {
298 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
299 ctxt
->figure_closed
= TRUE
;
302 ctxt
->figure_closed
= FALSE
;
303 ctxt
->figure_started
= TRUE
;
305 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
306 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt
->sink
, point
, D2D1_FIGURE_BEGIN_FILLED
);
307 ctxt
->move_to
= TRUE
;
312 static int decompose_line_to(const FT_Vector
*to
, void *user
)
314 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
315 /* special case for empty contours, in a way freetype returns them */
316 if (ctxt
->move_to
&& !memcmp(to
, &ctxt
->origin
, sizeof(*to
))) {
317 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
318 ctxt
->figure_closed
= TRUE
;
322 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, &point
);
323 ID2D1SimplifiedGeometrySink_AddLines(ctxt
->sink
, &point
, 1);
324 ctxt
->figure_closed
= FALSE
;
326 ctxt
->move_to
= FALSE
;
331 static int decompose_conic_to(const FT_Vector
*control
, const FT_Vector
*to
, void *user
)
333 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
334 D2D1_POINT_2F points
[3];
337 /* convert from quadratic to cubic */
340 The parametric eqn for a cubic Bezier is, from PLRM:
341 r(t) = at^3 + bt^2 + ct + r0
342 with the control points:
347 A quadratic Bezier has the form:
348 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
350 So equating powers of t leads to:
353 and of course r0 = p0, r3 = p2
356 /* r1 = 1/3 p0 + 2/3 p1
357 r2 = 1/3 p2 + 2/3 p1 */
358 cubic
[0].x
= (2 * control
->x
+ 1) / 3;
359 cubic
[0].y
= (2 * control
->y
+ 1) / 3;
361 cubic
[0].x
+= (ctxt
->origin
.x
+ 1) / 3;
362 cubic
[0].y
+= (ctxt
->origin
.y
+ 1) / 3;
363 cubic
[1].x
+= (to
->x
+ 1) / 3;
364 cubic
[1].y
+= (to
->y
+ 1) / 3;
367 ft_vector_to_d2d_point(cubic
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
368 ft_vector_to_d2d_point(cubic
+ 1, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
369 ft_vector_to_d2d_point(cubic
+ 2, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
370 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
371 ctxt
->figure_closed
= FALSE
;
372 ctxt
->move_to
= FALSE
;
377 static int decompose_cubic_to(const FT_Vector
*control1
, const FT_Vector
*control2
,
378 const FT_Vector
*to
, void *user
)
380 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
381 D2D1_POINT_2F points
[3];
383 ft_vector_to_d2d_point(control1
, ctxt
->xoffset
, ctxt
->yoffset
, points
);
384 ft_vector_to_d2d_point(control2
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 1);
385 ft_vector_to_d2d_point(to
, ctxt
->xoffset
, ctxt
->yoffset
, points
+ 2);
386 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
387 ctxt
->figure_closed
= FALSE
;
388 ctxt
->move_to
= FALSE
;
393 static void decompose_outline(FT_Outline
*outline
, FLOAT xoffset
, FLOAT yoffset
, IDWriteGeometrySink
*sink
)
395 static const FT_Outline_Funcs decompose_funcs
= {
403 struct decompose_context context
;
406 context
.xoffset
= xoffset
;
407 context
.yoffset
= yoffset
;
408 context
.figure_started
= FALSE
;
409 context
.figure_closed
= FALSE
;
410 context
.move_to
= FALSE
;
411 context
.origin
.x
= 0;
412 context
.origin
.y
= 0;
414 pFT_Outline_Decompose(outline
, &decompose_funcs
, &context
);
416 if (!context
.figure_closed
&& outline
->n_points
)
417 ID2D1SimplifiedGeometrySink_EndFigure(sink
, D2D1_FIGURE_END_CLOSED
);
420 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace3
*fontface
, FLOAT emSize
, UINT16
const *glyphs
, FLOAT
const *advances
,
421 DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
423 FTC_ScalerRec scaler
;
431 ID2D1SimplifiedGeometrySink_SetFillMode(sink
, D2D1_FILL_MODE_WINDING
);
433 simulations
= IDWriteFontFace3_GetSimulations(fontface
);
435 scaler
.face_id
= fontface
;
436 scaler
.width
= emSize
;
437 scaler
.height
= emSize
;
442 EnterCriticalSection(&freetype_cs
);
443 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
444 FLOAT advance
= 0.0f
;
447 for (g
= 0; g
< count
; g
++) {
448 if (pFT_Load_Glyph(size
->face
, glyphs
[g
], FT_LOAD_NO_BITMAP
) == 0) {
449 FLOAT ft_advance
= size
->face
->glyph
->metrics
.horiAdvance
>> 6;
450 FT_Outline
*outline
= &size
->face
->glyph
->outline
;
451 FLOAT xoffset
= 0.0f
, yoffset
= 0.0f
;
455 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
457 m
.yy
= -(1 << 16); /* flip Y axis */
459 pFT_Outline_Transform(outline
, &m
);
461 /* glyph offsets act as current glyph adjustment */
463 xoffset
+= is_rtl
? -offsets
[g
].advanceOffset
: offsets
[g
].advanceOffset
;
464 yoffset
-= offsets
[g
].ascenderOffset
;
467 if (g
== 0 && is_rtl
)
468 advance
= advances
? -advances
[g
] : -ft_advance
;
471 decompose_outline(outline
, xoffset
, yoffset
, sink
);
473 /* update advance to next glyph */
475 advance
+= is_rtl
? -advances
[g
] : advances
[g
];
477 advance
+= is_rtl
? -ft_advance
: ft_advance
;
483 LeaveCriticalSection(&freetype_cs
);
488 UINT16
freetype_get_glyphcount(IDWriteFontFace3
*fontface
)
493 EnterCriticalSection(&freetype_cs
);
494 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
495 count
= face
->num_glyphs
;
496 LeaveCriticalSection(&freetype_cs
);
501 void freetype_get_glyphs(IDWriteFontFace3
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
506 EnterCriticalSection(&freetype_cs
);
507 for (i
= 0; i
< count
; i
++) {
509 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoints
[i
]);
511 UINT32 codepoint
= codepoints
[i
];
512 /* special handling for symbol fonts */
513 if (codepoint
< 0x100) codepoint
+= 0xf000;
514 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
);
516 glyphs
[i
] = pFTC_CMapCache_Lookup(cmap_cache
, fontface
, charmap
, codepoint
- 0xf000);
519 LeaveCriticalSection(&freetype_cs
);
522 BOOL
freetype_has_kerning_pairs(IDWriteFontFace3
*fontface
)
524 BOOL has_kerning_pairs
= FALSE
;
527 EnterCriticalSection(&freetype_cs
);
528 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
529 has_kerning_pairs
= !!FT_HAS_KERNING(face
);
530 LeaveCriticalSection(&freetype_cs
);
532 return has_kerning_pairs
;
535 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace3
*fontface
, UINT16 left
, UINT16 right
)
537 INT32 adjustment
= 0;
540 EnterCriticalSection(&freetype_cs
);
541 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
543 if (FT_HAS_KERNING(face
)) {
544 pFT_Get_Kerning(face
, left
, right
, FT_KERNING_UNSCALED
, &kern
);
548 LeaveCriticalSection(&freetype_cs
);
553 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX
*m
, FT_Matrix
*ft_matrix
)
555 ft_matrix
->xx
= m
->m11
* 0x10000;
556 ft_matrix
->xy
= -m
->m21
* 0x10000;
557 ft_matrix
->yx
= -m
->m12
* 0x10000;
558 ft_matrix
->yy
= m
->m22
* 0x10000;
561 /* Should be used only while holding 'freetype_cs' */
562 static BOOL
is_face_scalable(IDWriteFontFace3
*fontface
)
565 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
566 return FT_IS_SCALABLE(face
);
571 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
573 FTC_ImageTypeRec imagetype
;
574 FT_BBox bbox
= { 0 };
577 EnterCriticalSection(&freetype_cs
);
579 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef,
580 disable transform if that's the case. */
582 if (!is_face_scalable(bitmap
->fontface
))
586 imagetype
.face_id
= bitmap
->fontface
;
588 imagetype
.height
= bitmap
->emsize
;
589 imagetype
.flags
= bitmap
->m
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
591 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
595 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
598 ft_matrix_from_dwrite_matrix(bitmap
->m
, &ft_matrix
);
599 pFT_Glyph_Transform(glyph_copy
, &ft_matrix
, NULL
);
600 pFT_Glyph_Get_CBox(glyph_copy
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
601 pFT_Done_Glyph(glyph_copy
);
605 pFT_Glyph_Get_CBox(glyph
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
608 LeaveCriticalSection(&freetype_cs
);
611 SetRect(&bitmap
->bbox
, bbox
.xMin
, -bbox
.yMax
, bbox
.xMax
, -bbox
.yMin
);
614 static BOOL
freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
616 const RECT
*bbox
= &bitmap
->bbox
;
617 int width
= bbox
->right
- bbox
->left
;
618 int height
= bbox
->bottom
- bbox
->top
;
620 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
621 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
622 const FT_Outline
*src
= &outline
->outline
;
626 ft_bitmap
.width
= width
;
627 ft_bitmap
.rows
= height
;
628 ft_bitmap
.pitch
= bitmap
->pitch
;
629 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
630 ft_bitmap
.buffer
= bitmap
->buf
;
632 /* Note: FreeType will only set 'black' bits for us. */
633 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
634 pFT_Outline_Copy(src
, ©
);
635 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
636 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
637 pFT_Outline_Done(library
, ©
);
640 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
641 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
642 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
643 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
644 int h
= min(height
, ft_bitmap
->rows
);
648 src
+= ft_bitmap
->pitch
;
649 dst
+= bitmap
->pitch
;
653 FIXME("format %x not handled\n", glyph
->format
);
658 static BOOL
freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
660 const RECT
*bbox
= &bitmap
->bbox
;
661 int width
= bbox
->right
- bbox
->left
;
662 int height
= bbox
->bottom
- bbox
->top
;
665 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
666 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
667 const FT_Outline
*src
= &outline
->outline
;
671 ft_bitmap
.width
= width
;
672 ft_bitmap
.rows
= height
;
673 ft_bitmap
.pitch
= bitmap
->pitch
;
674 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
675 ft_bitmap
.buffer
= bitmap
->buf
;
677 /* Note: FreeType will only set 'black' bits for us. */
678 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
679 pFT_Outline_Copy(src
, ©
);
680 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
681 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
682 pFT_Outline_Done(library
, ©
);
685 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
686 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
687 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
688 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
689 int h
= min(height
, ft_bitmap
->rows
);
693 src
+= ft_bitmap
->pitch
;
694 dst
+= bitmap
->pitch
;
700 FIXME("format %x not handled\n", glyph
->format
);
705 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
707 FTC_ImageTypeRec imagetype
;
711 EnterCriticalSection(&freetype_cs
);
714 if (!is_face_scalable(bitmap
->fontface
))
718 imagetype
.face_id
= bitmap
->fontface
;
720 imagetype
.height
= bitmap
->emsize
;
721 imagetype
.flags
= bitmap
->m
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
723 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->index
, &glyph
, NULL
) == 0) {
727 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
730 ft_matrix_from_dwrite_matrix(bitmap
->m
, &ft_matrix
);
731 pFT_Glyph_Transform(glyph_copy
, &ft_matrix
, NULL
);
738 if (bitmap
->type
== DWRITE_TEXTURE_CLEARTYPE_3x1
)
739 ret
= freetype_get_aa_glyph_bitmap(bitmap
, glyph
);
741 ret
= freetype_get_aliased_glyph_bitmap(bitmap
, glyph
);
744 pFT_Done_Glyph(glyph_copy
);
747 LeaveCriticalSection(&freetype_cs
);
752 INT
freetype_get_charmap_index(IDWriteFontFace3
*fontface
, BOOL
*is_symbol
)
754 INT charmap_index
= -1;
759 EnterCriticalSection(&freetype_cs
);
760 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
761 TT_OS2
*os2
= pFT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
766 if (os2
->version
== 0)
767 *is_symbol
= pFT_Get_First_Char(face
, &dummy
) >= 0x100;
769 *is_symbol
= !!(os2
->ulCodePageRange1
& FS_SYMBOL
);
772 for (i
= 0; i
< face
->num_charmaps
; i
++)
773 if (face
->charmaps
[i
]->encoding
== FT_ENCODING_MS_SYMBOL
) {
779 LeaveCriticalSection(&freetype_cs
);
781 return charmap_index
;
784 INT32
freetype_get_glyph_advance(IDWriteFontFace3
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
)
786 FTC_ImageTypeRec imagetype
;
790 imagetype
.face_id
= fontface
;
792 imagetype
.height
= emSize
;
793 imagetype
.flags
= FT_LOAD_DEFAULT
;
794 if (mode
== DWRITE_MEASURING_MODE_NATURAL
)
795 imagetype
.flags
|= FT_LOAD_NO_HINTING
;
797 EnterCriticalSection(&freetype_cs
);
798 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, index
, &glyph
, NULL
) == 0)
799 advance
= glyph
->advance
.x
>> 16;
802 LeaveCriticalSection(&freetype_cs
);
807 #else /* HAVE_FREETYPE */
809 BOOL
init_freetype(void)
814 void release_freetype(void)
818 void freetype_notify_cacheremove(IDWriteFontFace3
*fontface
)
822 HRESULT
freetype_get_design_glyph_metrics(IDWriteFontFace3
*fontface
, UINT16 unitsperEm
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
827 BOOL
freetype_is_monospaced(IDWriteFontFace3
*fontface
)
832 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace3
*fontface
, FLOAT emSize
, UINT16
const *glyphs
, FLOAT
const *advances
,
833 DWRITE_GLYPH_OFFSET
const *offsets
, UINT32 count
, BOOL is_rtl
, IDWriteGeometrySink
*sink
)
838 UINT16
freetype_get_glyphcount(IDWriteFontFace3
*fontface
)
843 void freetype_get_glyphs(IDWriteFontFace3
*fontface
, INT charmap
, UINT32
const *codepoints
, UINT32 count
,
846 memset(glyphs
, 0, count
* sizeof(*glyphs
));
849 BOOL
freetype_has_kerning_pairs(IDWriteFontFace3
*fontface
)
854 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace3
*fontface
, UINT16 left
, UINT16 right
)
859 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
861 memset(&bitmap
->bbox
, 0, sizeof(bitmap
->bbox
));
864 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
869 INT
freetype_get_charmap_index(IDWriteFontFace3
*fontface
, BOOL
*is_symbol
)
875 INT32
freetype_get_glyph_advance(IDWriteFontFace3
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
)
880 #endif /* HAVE_FREETYPE */