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/debug.h"
37 #include "dwrite_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(dwrite
);
43 static CRITICAL_SECTION freetype_cs
;
44 static CRITICAL_SECTION_DEBUG critsect_debug
=
47 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
48 0, 0, { (DWORD_PTR
)(__FILE__
": freetype_cs") }
50 static CRITICAL_SECTION freetype_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
52 static void *ft_handle
= NULL
;
53 static FT_Library library
= 0;
54 static FTC_Manager cache_manager
= 0;
55 static FTC_ImageCache image_cache
= 0;
63 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
64 MAKE_FUNCPTR(FT_Done_FreeType
);
65 MAKE_FUNCPTR(FT_Done_Glyph
);
66 MAKE_FUNCPTR(FT_Get_First_Char
);
67 MAKE_FUNCPTR(FT_Get_Kerning
);
68 MAKE_FUNCPTR(FT_Get_Sfnt_Table
);
69 MAKE_FUNCPTR(FT_Glyph_Copy
);
70 MAKE_FUNCPTR(FT_Glyph_Get_CBox
);
71 MAKE_FUNCPTR(FT_Glyph_Transform
);
72 MAKE_FUNCPTR(FT_Init_FreeType
);
73 MAKE_FUNCPTR(FT_Library_Version
);
74 MAKE_FUNCPTR(FT_Load_Glyph
);
75 MAKE_FUNCPTR(FT_Matrix_Multiply
);
76 MAKE_FUNCPTR(FT_New_Memory_Face
);
77 MAKE_FUNCPTR(FT_Outline_Copy
);
78 MAKE_FUNCPTR(FT_Outline_Decompose
);
79 MAKE_FUNCPTR(FT_Outline_Done
);
80 MAKE_FUNCPTR(FT_Outline_Embolden
);
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_ImageCache_Lookup
);
86 MAKE_FUNCPTR(FTC_ImageCache_New
);
87 MAKE_FUNCPTR(FTC_Manager_New
);
88 MAKE_FUNCPTR(FTC_Manager_Done
);
89 MAKE_FUNCPTR(FTC_Manager_LookupFace
);
90 MAKE_FUNCPTR(FTC_Manager_LookupSize
);
91 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID
);
93 static FT_Error (*pFT_Outline_EmboldenXY
)(FT_Outline
*, FT_Pos
, FT_Pos
);
95 struct face_finalizer_data
97 IDWriteFontFileStream
*stream
;
101 static void face_finalizer(void *object
)
103 FT_Face face
= object
;
104 struct face_finalizer_data
*data
= (struct face_finalizer_data
*)face
->generic
.data
;
106 IDWriteFontFileStream_ReleaseFileFragment(data
->stream
, data
->context
);
107 IDWriteFontFileStream_Release(data
->stream
);
111 static FT_Error
face_requester(FTC_FaceID face_id
, FT_Library library
, FT_Pointer request_data
, FT_Face
*face
)
113 IDWriteFontFace
*fontface
= (IDWriteFontFace
*)face_id
;
114 IDWriteFontFileStream
*stream
;
115 IDWriteFontFile
*file
;
116 const void *data_ptr
;
126 WARN("NULL fontface requested.\n");
131 hr
= IDWriteFontFace_GetFiles(fontface
, &count
, &file
);
135 hr
= get_filestream_from_file(file
, &stream
);
136 IDWriteFontFile_Release(file
);
140 hr
= IDWriteFontFileStream_GetFileSize(stream
, &data_size
);
142 fterror
= FT_Err_Invalid_Stream_Read
;
146 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &data_ptr
, 0, data_size
, &context
);
148 fterror
= FT_Err_Invalid_Stream_Read
;
152 index
= IDWriteFontFace_GetIndex(fontface
);
153 fterror
= pFT_New_Memory_Face(library
, data_ptr
, data_size
, index
, face
);
154 if (fterror
== FT_Err_Ok
) {
155 struct face_finalizer_data
*data
;
157 data
= heap_alloc(sizeof(*data
));
158 data
->stream
= stream
;
159 data
->context
= context
;
161 (*face
)->generic
.data
= data
;
162 (*face
)->generic
.finalizer
= face_finalizer
;
166 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
169 IDWriteFontFileStream_Release(stream
);
174 BOOL
init_freetype(void)
176 FT_Version_t FT_Version
;
178 ft_handle
= dlopen(SONAME_LIBFREETYPE
, RTLD_NOW
);
180 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
184 #define LOAD_FUNCPTR(f) if((p##f = dlsym(ft_handle, #f)) == NULL){WARN("Can't find symbol %s\n", #f); goto sym_not_found;}
185 LOAD_FUNCPTR(FT_Done_FreeType
)
186 LOAD_FUNCPTR(FT_Done_Glyph
)
187 LOAD_FUNCPTR(FT_Get_First_Char
)
188 LOAD_FUNCPTR(FT_Get_Kerning
)
189 LOAD_FUNCPTR(FT_Get_Sfnt_Table
)
190 LOAD_FUNCPTR(FT_Glyph_Copy
)
191 LOAD_FUNCPTR(FT_Glyph_Get_CBox
)
192 LOAD_FUNCPTR(FT_Glyph_Transform
)
193 LOAD_FUNCPTR(FT_Init_FreeType
)
194 LOAD_FUNCPTR(FT_Library_Version
)
195 LOAD_FUNCPTR(FT_Load_Glyph
)
196 LOAD_FUNCPTR(FT_Matrix_Multiply
)
197 LOAD_FUNCPTR(FT_New_Memory_Face
)
198 LOAD_FUNCPTR(FT_Outline_Copy
)
199 LOAD_FUNCPTR(FT_Outline_Decompose
)
200 LOAD_FUNCPTR(FT_Outline_Done
)
201 LOAD_FUNCPTR(FT_Outline_Embolden
)
202 LOAD_FUNCPTR(FT_Outline_Get_Bitmap
)
203 LOAD_FUNCPTR(FT_Outline_New
)
204 LOAD_FUNCPTR(FT_Outline_Transform
)
205 LOAD_FUNCPTR(FT_Outline_Translate
)
206 LOAD_FUNCPTR(FTC_ImageCache_Lookup
)
207 LOAD_FUNCPTR(FTC_ImageCache_New
)
208 LOAD_FUNCPTR(FTC_Manager_New
)
209 LOAD_FUNCPTR(FTC_Manager_Done
)
210 LOAD_FUNCPTR(FTC_Manager_LookupFace
)
211 LOAD_FUNCPTR(FTC_Manager_LookupSize
)
212 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID
)
214 pFT_Outline_EmboldenXY
= dlsym(ft_handle
, "FT_Outline_EmboldenXY");
216 if (pFT_Init_FreeType(&library
) != 0) {
217 ERR("Can't init FreeType library\n");
222 pFT_Library_Version(library
, &FT_Version
.major
, &FT_Version
.minor
, &FT_Version
.patch
);
224 /* init cache manager */
225 if (pFTC_Manager_New(library
, 0, 0, 0, &face_requester
, NULL
, &cache_manager
) != 0 ||
226 pFTC_ImageCache_New(cache_manager
, &image_cache
) != 0) {
228 ERR("Failed to init FreeType cache\n");
229 pFTC_Manager_Done(cache_manager
);
230 pFT_Done_FreeType(library
);
236 TRACE("FreeType version is %d.%d.%d\n", FT_Version
.major
, FT_Version
.minor
, FT_Version
.patch
);
240 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
246 void release_freetype(void)
248 pFTC_Manager_Done(cache_manager
);
249 pFT_Done_FreeType(library
);
252 void freetype_notify_cacheremove(IDWriteFontFace5
*fontface
)
254 EnterCriticalSection(&freetype_cs
);
255 pFTC_Manager_RemoveFaceID(cache_manager
, fontface
);
256 LeaveCriticalSection(&freetype_cs
);
259 HRESULT
freetype_get_design_glyph_metrics(struct dwrite_fontface
*fontface
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
261 FTC_ScalerRec scaler
;
264 scaler
.face_id
= &fontface
->IDWriteFontFace5_iface
;
265 scaler
.width
= fontface
->metrics
.designUnitsPerEm
;
266 scaler
.height
= fontface
->metrics
.designUnitsPerEm
;
271 EnterCriticalSection(&freetype_cs
);
272 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
273 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
274 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
276 ret
->leftSideBearing
= metrics
->horiBearingX
;
277 ret
->advanceWidth
= metrics
->horiAdvance
;
278 ret
->rightSideBearing
= metrics
->horiAdvance
- metrics
->horiBearingX
- metrics
->width
;
280 ret
->advanceHeight
= metrics
->vertAdvance
;
281 ret
->verticalOriginY
= fontface
->typo_metrics
.ascent
;
282 ret
->topSideBearing
= fontface
->typo_metrics
.ascent
- metrics
->horiBearingY
;
283 ret
->bottomSideBearing
= metrics
->vertAdvance
- metrics
->height
- ret
->topSideBearing
;
285 /* Adjust in case of bold simulation, glyphs without contours are ignored. */
286 if (fontface
->simulations
& DWRITE_FONT_SIMULATIONS_BOLD
&&
287 size
->face
->glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
&& size
->face
->glyph
->outline
.n_contours
)
289 if (ret
->advanceWidth
)
290 ret
->advanceWidth
+= (fontface
->metrics
.designUnitsPerEm
+ 49) / 50;
294 LeaveCriticalSection(&freetype_cs
);
299 struct decompose_context
{
300 IDWriteGeometrySink
*sink
;
301 D2D1_POINT_2F offset
;
303 BOOL move_to
; /* last call was 'move_to' */
304 FT_Vector origin
; /* 'pen' position from last call */
307 static inline void ft_vector_to_d2d_point(const FT_Vector
*v
, D2D1_POINT_2F offset
, D2D1_POINT_2F
*p
)
309 p
->x
= (v
->x
/ 64.0f
) + offset
.x
;
310 p
->y
= (v
->y
/ 64.0f
) + offset
.y
;
313 static void decompose_beginfigure(struct decompose_context
*ctxt
)
320 ft_vector_to_d2d_point(&ctxt
->origin
, ctxt
->offset
, &point
);
321 ID2D1SimplifiedGeometrySink_BeginFigure(ctxt
->sink
, point
, D2D1_FIGURE_BEGIN_FILLED
);
323 ctxt
->figure_started
= TRUE
;
324 ctxt
->move_to
= FALSE
;
327 static int decompose_move_to(const FT_Vector
*to
, void *user
)
329 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
331 if (ctxt
->figure_started
) {
332 ID2D1SimplifiedGeometrySink_EndFigure(ctxt
->sink
, D2D1_FIGURE_END_CLOSED
);
333 ctxt
->figure_started
= FALSE
;
336 ctxt
->move_to
= TRUE
;
341 static int decompose_line_to(const FT_Vector
*to
, void *user
)
343 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
346 /* Special case for empty contours, in a way freetype returns them. */
347 if (ctxt
->move_to
&& !memcmp(to
, &ctxt
->origin
, sizeof(*to
)))
350 decompose_beginfigure(ctxt
);
352 ft_vector_to_d2d_point(to
, ctxt
->offset
, &point
);
353 ID2D1SimplifiedGeometrySink_AddLines(ctxt
->sink
, &point
, 1);
359 static int decompose_conic_to(const FT_Vector
*control
, const FT_Vector
*to
, void *user
)
361 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
362 D2D1_POINT_2F points
[3];
365 decompose_beginfigure(ctxt
);
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
->offset
, points
);
398 ft_vector_to_d2d_point(cubic
+ 1, ctxt
->offset
, points
+ 1);
399 ft_vector_to_d2d_point(cubic
+ 2, ctxt
->offset
, points
+ 2);
400 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
405 static int decompose_cubic_to(const FT_Vector
*control1
, const FT_Vector
*control2
,
406 const FT_Vector
*to
, void *user
)
408 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
409 D2D1_POINT_2F points
[3];
411 decompose_beginfigure(ctxt
);
413 ft_vector_to_d2d_point(control1
, ctxt
->offset
, points
);
414 ft_vector_to_d2d_point(control2
, ctxt
->offset
, points
+ 1);
415 ft_vector_to_d2d_point(to
, ctxt
->offset
, points
+ 2);
416 ID2D1SimplifiedGeometrySink_AddBeziers(ctxt
->sink
, (D2D1_BEZIER_SEGMENT
*)points
, 1);
421 static void decompose_outline(FT_Outline
*outline
, D2D1_POINT_2F offset
, IDWriteGeometrySink
*sink
)
423 static const FT_Outline_Funcs decompose_funcs
= {
431 struct decompose_context context
;
434 context
.offset
= offset
;
435 context
.figure_started
= FALSE
;
436 context
.move_to
= FALSE
;
437 context
.origin
.x
= 0;
438 context
.origin
.y
= 0;
440 pFT_Outline_Decompose(outline
, &decompose_funcs
, &context
);
442 if (context
.figure_started
)
443 ID2D1SimplifiedGeometrySink_EndFigure(sink
, D2D1_FIGURE_END_CLOSED
);
446 static void embolden_glyph_outline(FT_Outline
*outline
, FLOAT emsize
)
450 strength
= MulDiv(emsize
, 1 << 6, 24);
451 if (pFT_Outline_EmboldenXY
)
452 pFT_Outline_EmboldenXY(outline
, strength
, 0);
454 pFT_Outline_Embolden(outline
, strength
);
457 static void embolden_glyph(FT_Glyph glyph
, FLOAT emsize
)
459 FT_OutlineGlyph outline_glyph
= (FT_OutlineGlyph
)glyph
;
461 if (glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
464 embolden_glyph_outline(&outline_glyph
->outline
, emsize
);
467 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace5
*fontface
, float emSize
, UINT16
const *glyphs
,
468 float const *advances
, DWRITE_GLYPH_OFFSET
const *offsets
, unsigned int count
, BOOL is_rtl
,
469 IDWriteGeometrySink
*sink
)
471 FTC_ScalerRec scaler
;
479 ID2D1SimplifiedGeometrySink_SetFillMode(sink
, D2D1_FILL_MODE_WINDING
);
481 simulations
= IDWriteFontFace5_GetSimulations(fontface
);
483 scaler
.face_id
= fontface
;
484 scaler
.width
= emSize
;
485 scaler
.height
= emSize
;
490 EnterCriticalSection(&freetype_cs
);
491 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
492 float rtl_factor
= is_rtl
? -1.0f
: 1.0f
;
493 D2D1_POINT_2F origin
;
496 origin
.x
= origin
.y
= 0.0f
;
497 for (i
= 0; i
< count
; ++i
)
499 if (pFT_Load_Glyph(size
->face
, glyphs
[i
], FT_LOAD_NO_BITMAP
) == 0)
501 FLOAT ft_advance
= size
->face
->glyph
->metrics
.horiAdvance
>> 6;
502 FT_Outline
*outline
= &size
->face
->glyph
->outline
;
503 D2D1_POINT_2F glyph_origin
;
507 if (simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
508 embolden_glyph_outline(outline
, emSize
);
511 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
513 m
.yy
= -(1 << 16); /* flip Y axis */
515 pFT_Outline_Transform(outline
, &m
);
518 advance
= rtl_factor
* advances
[i
];
520 advance
= rtl_factor
* ft_advance
;
522 glyph_origin
= origin
;
524 glyph_origin
.x
+= advance
;
526 /* glyph offsets act as current glyph adjustment */
529 glyph_origin
.x
+= rtl_factor
* offsets
[i
].advanceOffset
;
530 glyph_origin
.y
-= offsets
[i
].ascenderOffset
;
533 decompose_outline(outline
, glyph_origin
, sink
);
541 LeaveCriticalSection(&freetype_cs
);
546 UINT16
freetype_get_glyphcount(IDWriteFontFace5
*fontface
)
551 EnterCriticalSection(&freetype_cs
);
552 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
553 count
= face
->num_glyphs
;
554 LeaveCriticalSection(&freetype_cs
);
559 BOOL
freetype_has_kerning_pairs(IDWriteFontFace5
*fontface
)
561 BOOL has_kerning_pairs
= FALSE
;
564 EnterCriticalSection(&freetype_cs
);
565 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
566 has_kerning_pairs
= !!FT_HAS_KERNING(face
);
567 LeaveCriticalSection(&freetype_cs
);
569 return has_kerning_pairs
;
572 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace5
*fontface
, UINT16 left
, UINT16 right
)
574 INT32 adjustment
= 0;
577 EnterCriticalSection(&freetype_cs
);
578 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0) {
580 if (FT_HAS_KERNING(face
)) {
581 pFT_Get_Kerning(face
, left
, right
, FT_KERNING_UNSCALED
, &kern
);
585 LeaveCriticalSection(&freetype_cs
);
590 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX
*m
, FT_Matrix
*ft_matrix
)
592 ft_matrix
->xx
= m
->m11
* 0x10000;
593 ft_matrix
->xy
= -m
->m21
* 0x10000;
594 ft_matrix
->yx
= -m
->m12
* 0x10000;
595 ft_matrix
->yy
= m
->m22
* 0x10000;
598 /* Should be used only while holding 'freetype_cs' */
599 static BOOL
is_face_scalable(IDWriteFontFace4
*fontface
)
602 if (pFTC_Manager_LookupFace(cache_manager
, fontface
, &face
) == 0)
603 return FT_IS_SCALABLE(face
);
608 static BOOL
get_glyph_transform(struct dwrite_glyphbitmap
*bitmap
, FT_Matrix
*ret
)
617 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
618 Disable transform if that's the case. */
619 if (!is_face_scalable(bitmap
->fontface
) || (!bitmap
->m
&& bitmap
->simulations
== 0))
622 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
) {
624 m
.xy
= (1 << 16) / 3;
627 pFT_Matrix_Multiply(&m
, ret
);
631 ft_matrix_from_dwrite_matrix(bitmap
->m
, &m
);
632 pFT_Matrix_Multiply(&m
, ret
);
638 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
640 FTC_ImageTypeRec imagetype
;
641 FT_BBox bbox
= { 0 };
642 BOOL needs_transform
;
646 EnterCriticalSection(&freetype_cs
);
648 needs_transform
= get_glyph_transform(bitmap
, &m
);
650 imagetype
.face_id
= bitmap
->fontface
;
652 imagetype
.height
= bitmap
->emsize
;
653 imagetype
.flags
= needs_transform
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
655 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->glyph
, &glyph
, NULL
) == 0) {
656 if (needs_transform
) {
659 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
660 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
661 embolden_glyph(glyph_copy
, bitmap
->emsize
);
663 /* Includes oblique and user transform. */
664 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
665 pFT_Glyph_Get_CBox(glyph_copy
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
666 pFT_Done_Glyph(glyph_copy
);
670 pFT_Glyph_Get_CBox(glyph
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
673 LeaveCriticalSection(&freetype_cs
);
676 SetRect(&bitmap
->bbox
, bbox
.xMin
, -bbox
.yMax
, bbox
.xMax
, -bbox
.yMin
);
679 static BOOL
freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
681 const RECT
*bbox
= &bitmap
->bbox
;
682 int width
= bbox
->right
- bbox
->left
;
683 int height
= bbox
->bottom
- bbox
->top
;
685 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
686 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
687 const FT_Outline
*src
= &outline
->outline
;
691 ft_bitmap
.width
= width
;
692 ft_bitmap
.rows
= height
;
693 ft_bitmap
.pitch
= bitmap
->pitch
;
694 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
695 ft_bitmap
.buffer
= bitmap
->buf
;
697 /* Note: FreeType will only set 'black' bits for us. */
698 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
699 pFT_Outline_Copy(src
, ©
);
700 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
701 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
702 pFT_Outline_Done(library
, ©
);
705 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
706 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
707 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
708 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
709 int h
= min(height
, ft_bitmap
->rows
);
713 src
+= ft_bitmap
->pitch
;
714 dst
+= bitmap
->pitch
;
718 FIXME("format %x not handled\n", glyph
->format
);
723 static BOOL
freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
725 const RECT
*bbox
= &bitmap
->bbox
;
726 int width
= bbox
->right
- bbox
->left
;
727 int height
= bbox
->bottom
- bbox
->top
;
730 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
731 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
732 const FT_Outline
*src
= &outline
->outline
;
736 ft_bitmap
.width
= width
;
737 ft_bitmap
.rows
= height
;
738 ft_bitmap
.pitch
= bitmap
->pitch
;
739 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
740 ft_bitmap
.buffer
= bitmap
->buf
;
742 /* Note: FreeType will only set 'black' bits for us. */
743 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
744 pFT_Outline_Copy(src
, ©
);
745 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
746 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
747 pFT_Outline_Done(library
, ©
);
750 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
751 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
752 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
753 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
754 int h
= min(height
, ft_bitmap
->rows
);
758 src
+= ft_bitmap
->pitch
;
759 dst
+= bitmap
->pitch
;
765 FIXME("format %x not handled\n", glyph
->format
);
770 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
772 FTC_ImageTypeRec imagetype
;
773 BOOL needs_transform
;
778 EnterCriticalSection(&freetype_cs
);
780 needs_transform
= get_glyph_transform(bitmap
, &m
);
782 imagetype
.face_id
= bitmap
->fontface
;
784 imagetype
.height
= bitmap
->emsize
;
785 imagetype
.flags
= needs_transform
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
787 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->glyph
, &glyph
, NULL
) == 0) {
790 if (needs_transform
) {
791 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
792 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
793 embolden_glyph(glyph_copy
, bitmap
->emsize
);
795 /* Includes oblique and user transform. */
796 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
804 ret
= freetype_get_aliased_glyph_bitmap(bitmap
, glyph
);
806 ret
= freetype_get_aa_glyph_bitmap(bitmap
, glyph
);
809 pFT_Done_Glyph(glyph_copy
);
812 LeaveCriticalSection(&freetype_cs
);
817 INT32
freetype_get_glyph_advance(IDWriteFontFace5
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
,
820 FTC_ImageTypeRec imagetype
;
824 imagetype
.face_id
= fontface
;
826 imagetype
.height
= emSize
;
827 imagetype
.flags
= FT_LOAD_DEFAULT
;
828 if (mode
== DWRITE_MEASURING_MODE_NATURAL
)
829 imagetype
.flags
|= FT_LOAD_NO_HINTING
;
831 EnterCriticalSection(&freetype_cs
);
832 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, index
, &glyph
, NULL
) == 0) {
833 *has_contours
= glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
&& ((FT_OutlineGlyph
)glyph
)->outline
.n_contours
;
834 advance
= glyph
->advance
.x
>> 16;
837 *has_contours
= FALSE
;
840 LeaveCriticalSection(&freetype_cs
);
845 #else /* HAVE_FREETYPE */
847 BOOL
init_freetype(void)
852 void release_freetype(void)
856 void freetype_notify_cacheremove(IDWriteFontFace5
*fontface
)
860 HRESULT
freetype_get_design_glyph_metrics(struct dwrite_fontface
*fontface
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
865 HRESULT
freetype_get_glyphrun_outline(IDWriteFontFace5
*fontface
, float emSize
, UINT16
const *glyphs
,
866 float const *advances
, DWRITE_GLYPH_OFFSET
const *offsets
, unsigned int count
, BOOL is_rtl
,
867 IDWriteGeometrySink
*sink
)
872 UINT16
freetype_get_glyphcount(IDWriteFontFace5
*fontface
)
877 BOOL
freetype_has_kerning_pairs(IDWriteFontFace5
*fontface
)
882 INT32
freetype_get_kerning_pair_adjustment(IDWriteFontFace5
*fontface
, UINT16 left
, UINT16 right
)
887 void freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
889 SetRectEmpty(&bitmap
->bbox
);
892 BOOL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
897 INT32
freetype_get_glyph_advance(IDWriteFontFace5
*fontface
, FLOAT emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
,
900 *has_contours
= FALSE
;
904 #endif /* HAVE_FREETYPE */