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
26 #include "wine/port.h"
28 #ifdef HAVE_FT2BUILD_H
31 #include FT_FREETYPE_H
33 #include FT_TRUETYPE_TABLES_H
34 #endif /* HAVE_FT2BUILD_H */
37 #define WIN32_NO_STATUS
39 #include "wine/debug.h"
41 #include "dwrite_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dwrite
);
47 static RTL_CRITICAL_SECTION freetype_cs
;
48 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
51 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
52 0, 0, { (DWORD_PTR
)(__FILE__
": freetype_cs") }
54 static RTL_CRITICAL_SECTION freetype_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
56 static void *ft_handle
= NULL
;
57 static FT_Library library
= 0;
58 static FTC_Manager cache_manager
= 0;
59 static FTC_ImageCache image_cache
= 0;
67 static const struct font_callback_funcs
*callback_funcs
;
69 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
70 MAKE_FUNCPTR(FT_Done_FreeType
);
71 MAKE_FUNCPTR(FT_Done_Glyph
);
72 MAKE_FUNCPTR(FT_Get_First_Char
);
73 MAKE_FUNCPTR(FT_Get_Kerning
);
74 MAKE_FUNCPTR(FT_Get_Sfnt_Table
);
75 MAKE_FUNCPTR(FT_Glyph_Copy
);
76 MAKE_FUNCPTR(FT_Glyph_Get_CBox
);
77 MAKE_FUNCPTR(FT_Glyph_Transform
);
78 MAKE_FUNCPTR(FT_Init_FreeType
);
79 MAKE_FUNCPTR(FT_Library_Version
);
80 MAKE_FUNCPTR(FT_Load_Glyph
);
81 MAKE_FUNCPTR(FT_Matrix_Multiply
);
82 MAKE_FUNCPTR(FT_MulDiv
);
83 MAKE_FUNCPTR(FT_New_Memory_Face
);
84 MAKE_FUNCPTR(FT_Outline_Copy
);
85 MAKE_FUNCPTR(FT_Outline_Decompose
);
86 MAKE_FUNCPTR(FT_Outline_Done
);
87 MAKE_FUNCPTR(FT_Outline_Embolden
);
88 MAKE_FUNCPTR(FT_Outline_Get_Bitmap
);
89 MAKE_FUNCPTR(FT_Outline_New
);
90 MAKE_FUNCPTR(FT_Outline_Transform
);
91 MAKE_FUNCPTR(FT_Outline_Translate
);
92 MAKE_FUNCPTR(FTC_ImageCache_Lookup
);
93 MAKE_FUNCPTR(FTC_ImageCache_New
);
94 MAKE_FUNCPTR(FTC_Manager_New
);
95 MAKE_FUNCPTR(FTC_Manager_Done
);
96 MAKE_FUNCPTR(FTC_Manager_LookupFace
);
97 MAKE_FUNCPTR(FTC_Manager_LookupSize
);
98 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID
);
100 static FT_Error (*pFT_Outline_EmboldenXY
)(FT_Outline
*, FT_Pos
, FT_Pos
);
102 static void face_finalizer(void *object
)
104 FT_Face face
= object
;
105 callback_funcs
->release_font_data((struct font_data_context
*)face
->generic
.data
);
108 static FT_Error
face_requester(FTC_FaceID face_id
, FT_Library library
, FT_Pointer request_data
, FT_Face
*face
)
110 struct font_data_context
*context
;
111 const void *data_ptr
;
120 WARN("NULL fontface requested.\n");
124 if (callback_funcs
->get_font_data(face_id
, &data_ptr
, &data_size
, &index
, &context
))
127 fterror
= pFT_New_Memory_Face(library
, data_ptr
, data_size
, index
, face
);
128 if (fterror
== FT_Err_Ok
)
130 (*face
)->generic
.data
= context
;
131 (*face
)->generic
.finalizer
= face_finalizer
;
134 callback_funcs
->release_font_data(context
);
139 static BOOL
init_freetype(void)
141 FT_Version_t FT_Version
;
143 ft_handle
= dlopen(SONAME_LIBFREETYPE
, RTLD_NOW
);
145 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
149 #define LOAD_FUNCPTR(f) if((p##f = dlsym(ft_handle, #f)) == NULL){WARN("Can't find symbol %s\n", #f); goto sym_not_found;}
150 LOAD_FUNCPTR(FT_Done_FreeType
)
151 LOAD_FUNCPTR(FT_Done_Glyph
)
152 LOAD_FUNCPTR(FT_Get_First_Char
)
153 LOAD_FUNCPTR(FT_Get_Kerning
)
154 LOAD_FUNCPTR(FT_Get_Sfnt_Table
)
155 LOAD_FUNCPTR(FT_Glyph_Copy
)
156 LOAD_FUNCPTR(FT_Glyph_Get_CBox
)
157 LOAD_FUNCPTR(FT_Glyph_Transform
)
158 LOAD_FUNCPTR(FT_Init_FreeType
)
159 LOAD_FUNCPTR(FT_Library_Version
)
160 LOAD_FUNCPTR(FT_Load_Glyph
)
161 LOAD_FUNCPTR(FT_Matrix_Multiply
)
162 LOAD_FUNCPTR(FT_MulDiv
)
163 LOAD_FUNCPTR(FT_New_Memory_Face
)
164 LOAD_FUNCPTR(FT_Outline_Copy
)
165 LOAD_FUNCPTR(FT_Outline_Decompose
)
166 LOAD_FUNCPTR(FT_Outline_Done
)
167 LOAD_FUNCPTR(FT_Outline_Embolden
)
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_ImageCache_Lookup
)
173 LOAD_FUNCPTR(FTC_ImageCache_New
)
174 LOAD_FUNCPTR(FTC_Manager_New
)
175 LOAD_FUNCPTR(FTC_Manager_Done
)
176 LOAD_FUNCPTR(FTC_Manager_LookupFace
)
177 LOAD_FUNCPTR(FTC_Manager_LookupSize
)
178 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID
)
180 pFT_Outline_EmboldenXY
= dlsym(ft_handle
, "FT_Outline_EmboldenXY");
182 if (pFT_Init_FreeType(&library
) != 0) {
183 ERR("Can't init FreeType library\n");
188 pFT_Library_Version(library
, &FT_Version
.major
, &FT_Version
.minor
, &FT_Version
.patch
);
190 /* init cache manager */
191 if (pFTC_Manager_New(library
, 0, 0, 0, &face_requester
, NULL
, &cache_manager
) != 0 ||
192 pFTC_ImageCache_New(cache_manager
, &image_cache
) != 0) {
194 ERR("Failed to init FreeType cache\n");
195 pFTC_Manager_Done(cache_manager
);
196 pFT_Done_FreeType(library
);
202 TRACE("FreeType version is %d.%d.%d\n", FT_Version
.major
, FT_Version
.minor
, FT_Version
.patch
);
206 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
212 static void CDECL
freetype_notify_release(void *key
)
214 RtlEnterCriticalSection(&freetype_cs
);
215 pFTC_Manager_RemoveFaceID(cache_manager
, key
);
216 RtlLeaveCriticalSection(&freetype_cs
);
219 static void CDECL
freetype_get_design_glyph_metrics(void *key
, UINT16 upem
, UINT16 ascent
,
220 unsigned int simulations
, UINT16 glyph
, DWRITE_GLYPH_METRICS
*ret
)
222 FTC_ScalerRec scaler
;
225 scaler
.face_id
= key
;
227 scaler
.height
= upem
;
232 RtlEnterCriticalSection(&freetype_cs
);
233 if (pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
) == 0) {
234 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_SCALE
) == 0) {
235 FT_Glyph_Metrics
*metrics
= &size
->face
->glyph
->metrics
;
237 ret
->leftSideBearing
= metrics
->horiBearingX
;
238 ret
->advanceWidth
= metrics
->horiAdvance
;
239 ret
->rightSideBearing
= metrics
->horiAdvance
- metrics
->horiBearingX
- metrics
->width
;
241 ret
->advanceHeight
= metrics
->vertAdvance
;
242 ret
->verticalOriginY
= ascent
;
243 ret
->topSideBearing
= ascent
- metrics
->horiBearingY
;
244 ret
->bottomSideBearing
= metrics
->vertAdvance
- metrics
->height
- ret
->topSideBearing
;
246 /* Adjust in case of bold simulation, glyphs without contours are ignored. */
247 if (simulations
& DWRITE_FONT_SIMULATIONS_BOLD
&&
248 size
->face
->glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
&& size
->face
->glyph
->outline
.n_contours
)
250 if (ret
->advanceWidth
)
251 ret
->advanceWidth
+= (upem
+ 49) / 50;
255 RtlLeaveCriticalSection(&freetype_cs
);
258 struct decompose_context
260 struct dwrite_outline
*outline
;
262 BOOL move_to
; /* last call was 'move_to' */
263 FT_Vector origin
; /* 'pen' position from last call */
266 static inline void ft_vector_to_d2d_point(const FT_Vector
*v
, D2D1_POINT_2F
*p
)
272 static int dwrite_outline_push_tag(struct dwrite_outline
*outline
, unsigned char tag
)
274 if (!dwrite_array_reserve((void **)&outline
->tags
.values
, &outline
->tags
.size
, outline
->tags
.count
+ 1,
275 sizeof(*outline
->tags
.values
)))
280 outline
->tags
.values
[outline
->tags
.count
++] = tag
;
285 static int dwrite_outline_push_points(struct dwrite_outline
*outline
, const D2D1_POINT_2F
*points
, unsigned int count
)
287 if (!dwrite_array_reserve((void **)&outline
->points
.values
, &outline
->points
.size
, outline
->points
.count
+ count
,
288 sizeof(*outline
->points
.values
)))
293 memcpy(&outline
->points
.values
[outline
->points
.count
], points
, sizeof(*points
) * count
);
294 outline
->points
.count
+= count
;
299 static int decompose_beginfigure(struct decompose_context
*ctxt
)
307 ft_vector_to_d2d_point(&ctxt
->origin
, &point
);
308 if ((ret
= dwrite_outline_push_tag(ctxt
->outline
, OUTLINE_BEGIN_FIGURE
))) return ret
;
309 if ((ret
= dwrite_outline_push_points(ctxt
->outline
, &point
, 1))) return ret
;
311 ctxt
->figure_started
= TRUE
;
312 ctxt
->move_to
= FALSE
;
317 static int decompose_move_to(const FT_Vector
*to
, void *user
)
319 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
322 if (ctxt
->figure_started
)
324 if ((ret
= dwrite_outline_push_tag(ctxt
->outline
, OUTLINE_END_FIGURE
))) return ret
;
325 ctxt
->figure_started
= FALSE
;
328 ctxt
->move_to
= TRUE
;
333 static int decompose_line_to(const FT_Vector
*to
, void *user
)
335 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
339 /* Special case for empty contours, in a way freetype returns them. */
340 if (ctxt
->move_to
&& !memcmp(to
, &ctxt
->origin
, sizeof(*to
)))
343 ft_vector_to_d2d_point(to
, &point
);
345 if ((ret
= decompose_beginfigure(ctxt
))) return ret
;
346 if ((ret
= dwrite_outline_push_points(ctxt
->outline
, &point
, 1))) return ret
;
347 if ((ret
= dwrite_outline_push_tag(ctxt
->outline
, OUTLINE_LINE
))) return ret
;
353 static int decompose_conic_to(const FT_Vector
*control
, const FT_Vector
*to
, void *user
)
355 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
356 D2D1_POINT_2F points
[3];
360 if ((ret
= decompose_beginfigure(ctxt
)))
363 /* convert from quadratic to cubic */
366 The parametric eqn for a cubic Bezier is, from PLRM:
367 r(t) = at^3 + bt^2 + ct + r0
368 with the control points:
373 A quadratic Bezier has the form:
374 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
376 So equating powers of t leads to:
379 and of course r0 = p0, r3 = p2
382 /* r1 = 1/3 p0 + 2/3 p1
383 r2 = 1/3 p2 + 2/3 p1 */
384 cubic
[0].x
= (2 * control
->x
+ 1) / 3;
385 cubic
[0].y
= (2 * control
->y
+ 1) / 3;
387 cubic
[0].x
+= (ctxt
->origin
.x
+ 1) / 3;
388 cubic
[0].y
+= (ctxt
->origin
.y
+ 1) / 3;
389 cubic
[1].x
+= (to
->x
+ 1) / 3;
390 cubic
[1].y
+= (to
->y
+ 1) / 3;
393 ft_vector_to_d2d_point(cubic
, points
);
394 ft_vector_to_d2d_point(cubic
+ 1, points
+ 1);
395 ft_vector_to_d2d_point(cubic
+ 2, points
+ 2);
396 if ((ret
= dwrite_outline_push_points(ctxt
->outline
, points
, 3))) return ret
;
397 if ((ret
= dwrite_outline_push_tag(ctxt
->outline
, OUTLINE_BEZIER
))) return ret
;
402 static int decompose_cubic_to(const FT_Vector
*control1
, const FT_Vector
*control2
,
403 const FT_Vector
*to
, void *user
)
405 struct decompose_context
*ctxt
= (struct decompose_context
*)user
;
406 D2D1_POINT_2F points
[3];
409 if ((ret
= decompose_beginfigure(ctxt
)))
412 ft_vector_to_d2d_point(control1
, points
);
413 ft_vector_to_d2d_point(control2
, points
+ 1);
414 ft_vector_to_d2d_point(to
, points
+ 2);
417 if ((ret
= dwrite_outline_push_points(ctxt
->outline
, points
, 3))) return ret
;
418 if ((ret
= dwrite_outline_push_tag(ctxt
->outline
, OUTLINE_BEZIER
))) return ret
;
423 static int decompose_outline(FT_Outline
*ft_outline
, struct dwrite_outline
*outline
)
425 static const FT_Outline_Funcs decompose_funcs
=
434 struct decompose_context context
= { 0 };
437 context
.outline
= outline
;
439 ret
= pFT_Outline_Decompose(ft_outline
, &decompose_funcs
, &context
);
441 if (!ret
&& context
.figure_started
)
442 ret
= dwrite_outline_push_tag(outline
, OUTLINE_END_FIGURE
);
447 static void embolden_glyph_outline(FT_Outline
*outline
, FLOAT emsize
)
451 strength
= pFT_MulDiv(emsize
, 1 << 6, 24);
452 if (pFT_Outline_EmboldenXY
)
453 pFT_Outline_EmboldenXY(outline
, strength
, 0);
455 pFT_Outline_Embolden(outline
, strength
);
458 static void embolden_glyph(FT_Glyph glyph
, FLOAT emsize
)
460 FT_OutlineGlyph outline_glyph
= (FT_OutlineGlyph
)glyph
;
462 if (glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
465 embolden_glyph_outline(&outline_glyph
->outline
, emsize
);
468 static int CDECL
freetype_get_glyph_outline(void *key
, float emSize
, unsigned int simulations
,
469 UINT16 glyph
, struct dwrite_outline
*outline
)
471 FTC_ScalerRec scaler
;
475 scaler
.face_id
= key
;
476 scaler
.width
= emSize
;
477 scaler
.height
= emSize
;
482 RtlEnterCriticalSection(&freetype_cs
);
483 if (!(ret
= pFTC_Manager_LookupSize(cache_manager
, &scaler
, &size
)))
485 if (pFT_Load_Glyph(size
->face
, glyph
, FT_LOAD_NO_BITMAP
) == 0)
487 FT_Outline
*ft_outline
= &size
->face
->glyph
->outline
;
490 if (simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
491 embolden_glyph_outline(ft_outline
, emSize
);
494 m
.xy
= simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
? (1 << 16) / 3 : 0;
496 m
.yy
= -(1 << 16); /* flip Y axis */
498 pFT_Outline_Transform(ft_outline
, &m
);
500 ret
= decompose_outline(ft_outline
, outline
);
503 RtlLeaveCriticalSection(&freetype_cs
);
508 static UINT16 CDECL
freetype_get_glyph_count(void *key
)
513 RtlEnterCriticalSection(&freetype_cs
);
514 if (pFTC_Manager_LookupFace(cache_manager
, key
, &face
) == 0)
515 count
= face
->num_glyphs
;
516 RtlLeaveCriticalSection(&freetype_cs
);
521 static inline void ft_matrix_from_dwrite_matrix(const DWRITE_MATRIX
*m
, FT_Matrix
*ft_matrix
)
523 ft_matrix
->xx
= m
->m11
* 0x10000;
524 ft_matrix
->xy
= -m
->m21
* 0x10000;
525 ft_matrix
->yx
= -m
->m12
* 0x10000;
526 ft_matrix
->yy
= m
->m22
* 0x10000;
529 /* Should be used only while holding 'freetype_cs' */
530 static BOOL
is_face_scalable(void *key
)
533 if (pFTC_Manager_LookupFace(cache_manager
, key
, &face
) == 0)
534 return FT_IS_SCALABLE(face
);
539 static BOOL
get_glyph_transform(struct dwrite_glyphbitmap
*bitmap
, FT_Matrix
*ret
)
548 /* Some fonts provide mostly bitmaps and very few outlines, for example for .notdef.
549 Disable transform if that's the case. */
550 if (!is_face_scalable(bitmap
->key
) || (!bitmap
->m
&& bitmap
->simulations
== 0))
553 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_OBLIQUE
) {
555 m
.xy
= (1 << 16) / 3;
558 pFT_Matrix_Multiply(&m
, ret
);
562 ft_matrix_from_dwrite_matrix(bitmap
->m
, &m
);
563 pFT_Matrix_Multiply(&m
, ret
);
569 static void CDECL
freetype_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
571 FTC_ImageTypeRec imagetype
;
572 FT_BBox bbox
= { 0 };
573 BOOL needs_transform
;
577 RtlEnterCriticalSection(&freetype_cs
);
579 needs_transform
= get_glyph_transform(bitmap
, &m
);
581 imagetype
.face_id
= bitmap
->key
;
583 imagetype
.height
= bitmap
->emsize
;
584 imagetype
.flags
= needs_transform
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
586 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->glyph
, &glyph
, NULL
) == 0) {
587 if (needs_transform
) {
590 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
591 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
592 embolden_glyph(glyph_copy
, bitmap
->emsize
);
594 /* Includes oblique and user transform. */
595 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
596 pFT_Glyph_Get_CBox(glyph_copy
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
597 pFT_Done_Glyph(glyph_copy
);
601 pFT_Glyph_Get_CBox(glyph
, FT_GLYPH_BBOX_PIXELS
, &bbox
);
604 RtlLeaveCriticalSection(&freetype_cs
);
607 SetRect(&bitmap
->bbox
, bbox
.xMin
, -bbox
.yMax
, bbox
.xMax
, -bbox
.yMin
);
610 static BOOL
freetype_get_aliased_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
612 const RECT
*bbox
= &bitmap
->bbox
;
613 int width
= bbox
->right
- bbox
->left
;
614 int height
= bbox
->bottom
- bbox
->top
;
616 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
617 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
618 const FT_Outline
*src
= &outline
->outline
;
622 ft_bitmap
.width
= width
;
623 ft_bitmap
.rows
= height
;
624 ft_bitmap
.pitch
= bitmap
->pitch
;
625 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_MONO
;
626 ft_bitmap
.buffer
= bitmap
->buf
;
628 /* Note: FreeType will only set 'black' bits for us. */
629 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
630 pFT_Outline_Copy(src
, ©
);
631 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
632 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
633 pFT_Outline_Done(library
, ©
);
636 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
637 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
638 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
639 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
640 int h
= min(height
, ft_bitmap
->rows
);
644 src
+= ft_bitmap
->pitch
;
645 dst
+= bitmap
->pitch
;
649 FIXME("format %x not handled\n", glyph
->format
);
654 static BOOL
freetype_get_aa_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
, FT_Glyph glyph
)
656 const RECT
*bbox
= &bitmap
->bbox
;
657 int width
= bbox
->right
- bbox
->left
;
658 int height
= bbox
->bottom
- bbox
->top
;
661 if (glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
) {
662 FT_OutlineGlyph outline
= (FT_OutlineGlyph
)glyph
;
663 const FT_Outline
*src
= &outline
->outline
;
667 ft_bitmap
.width
= width
;
668 ft_bitmap
.rows
= height
;
669 ft_bitmap
.pitch
= bitmap
->pitch
;
670 ft_bitmap
.pixel_mode
= FT_PIXEL_MODE_GRAY
;
671 ft_bitmap
.buffer
= bitmap
->buf
;
673 /* Note: FreeType will only set 'black' bits for us. */
674 if (pFT_Outline_New(library
, src
->n_points
, src
->n_contours
, ©
) == 0) {
675 pFT_Outline_Copy(src
, ©
);
676 pFT_Outline_Translate(©
, -bbox
->left
<< 6, bbox
->bottom
<< 6);
677 pFT_Outline_Get_Bitmap(library
, ©
, &ft_bitmap
);
678 pFT_Outline_Done(library
, ©
);
681 else if (glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
682 FT_Bitmap
*ft_bitmap
= &((FT_BitmapGlyph
)glyph
)->bitmap
;
683 BYTE
*src
= ft_bitmap
->buffer
, *dst
= bitmap
->buf
;
684 int w
= min(bitmap
->pitch
, (ft_bitmap
->width
+ 7) >> 3);
685 int h
= min(height
, ft_bitmap
->rows
);
689 src
+= ft_bitmap
->pitch
;
690 dst
+= bitmap
->pitch
;
696 FIXME("format %x not handled\n", glyph
->format
);
701 static BOOL CDECL
freetype_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
703 FTC_ImageTypeRec imagetype
;
704 BOOL needs_transform
;
709 RtlEnterCriticalSection(&freetype_cs
);
711 needs_transform
= get_glyph_transform(bitmap
, &m
);
713 imagetype
.face_id
= bitmap
->key
;
715 imagetype
.height
= bitmap
->emsize
;
716 imagetype
.flags
= needs_transform
? FT_LOAD_NO_BITMAP
: FT_LOAD_DEFAULT
;
718 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, bitmap
->glyph
, &glyph
, NULL
) == 0) {
721 if (needs_transform
) {
722 if (pFT_Glyph_Copy(glyph
, &glyph_copy
) == 0) {
723 if (bitmap
->simulations
& DWRITE_FONT_SIMULATIONS_BOLD
)
724 embolden_glyph(glyph_copy
, bitmap
->emsize
);
726 /* Includes oblique and user transform. */
727 pFT_Glyph_Transform(glyph_copy
, &m
, NULL
);
735 ret
= freetype_get_aliased_glyph_bitmap(bitmap
, glyph
);
737 ret
= freetype_get_aa_glyph_bitmap(bitmap
, glyph
);
740 pFT_Done_Glyph(glyph_copy
);
743 RtlLeaveCriticalSection(&freetype_cs
);
748 static INT32 CDECL
freetype_get_glyph_advance(void *key
, float emSize
, UINT16 index
,
749 DWRITE_MEASURING_MODE mode
, BOOL
*has_contours
)
751 FTC_ImageTypeRec imagetype
;
755 imagetype
.face_id
= key
;
757 imagetype
.height
= emSize
;
758 imagetype
.flags
= FT_LOAD_DEFAULT
;
759 if (mode
== DWRITE_MEASURING_MODE_NATURAL
)
760 imagetype
.flags
|= FT_LOAD_NO_HINTING
;
762 RtlEnterCriticalSection(&freetype_cs
);
763 if (pFTC_ImageCache_Lookup(image_cache
, &imagetype
, index
, &glyph
, NULL
) == 0) {
764 *has_contours
= glyph
->format
== FT_GLYPH_FORMAT_OUTLINE
&& ((FT_OutlineGlyph
)glyph
)->outline
.n_contours
;
765 advance
= glyph
->advance
.x
>> 16;
768 *has_contours
= FALSE
;
771 RtlLeaveCriticalSection(&freetype_cs
);
776 const static struct font_backend_funcs freetype_funcs
=
778 freetype_notify_release
,
779 freetype_get_glyph_outline
,
780 freetype_get_glyph_count
,
781 freetype_get_glyph_advance
,
782 freetype_get_glyph_bbox
,
783 freetype_get_glyph_bitmap
,
784 freetype_get_design_glyph_metrics
,
787 static NTSTATUS
init_freetype_lib(HMODULE module
, DWORD reason
, const void *ptr_in
, void *ptr_out
)
789 callback_funcs
= ptr_in
;
790 if (!init_freetype()) return STATUS_DLL_NOT_FOUND
;
791 *(const struct font_backend_funcs
**)ptr_out
= &freetype_funcs
;
792 return STATUS_SUCCESS
;
795 static NTSTATUS
release_freetype_lib(void)
797 pFTC_Manager_Done(cache_manager
);
798 pFT_Done_FreeType(library
);
799 return STATUS_SUCCESS
;
802 #else /* HAVE_FREETYPE */
804 static void CDECL
null_notify_release(void *key
)
808 static int CDECL
null_get_glyph_outline(void *key
, float emSize
, unsigned int simulations
,
809 UINT16 glyph
, struct dwrite_outline
*outline
)
814 static UINT16 CDECL
null_get_glyph_count(void *key
)
819 static INT32 CDECL
null_get_glyph_advance(void *key
, float emSize
, UINT16 index
, DWRITE_MEASURING_MODE mode
,
822 *has_contours
= FALSE
;
826 static void CDECL
null_get_glyph_bbox(struct dwrite_glyphbitmap
*bitmap
)
828 SetRectEmpty(&bitmap
->bbox
);
831 static BOOL CDECL
null_get_glyph_bitmap(struct dwrite_glyphbitmap
*bitmap
)
836 static void CDECL
null_get_design_glyph_metrics(void *key
, UINT16 upem
, UINT16 ascent
, unsigned int simulations
,
837 UINT16 glyph
, DWRITE_GLYPH_METRICS
*metrics
)
841 const static struct font_backend_funcs null_funcs
=
844 null_get_glyph_outline
,
845 null_get_glyph_count
,
846 null_get_glyph_advance
,
848 null_get_glyph_bitmap
,
849 null_get_design_glyph_metrics
,
852 static NTSTATUS
init_freetype_lib(HMODULE module
, DWORD reason
, const void *ptr_in
, void *ptr_out
)
854 *(const struct font_backend_funcs
**)ptr_out
= &null_funcs
;
855 return STATUS_DLL_NOT_FOUND
;
858 static NTSTATUS
release_freetype_lib(void)
860 return STATUS_DLL_NOT_FOUND
;
863 #endif /* HAVE_FREETYPE */
865 NTSTATUS CDECL
__wine_init_unix_lib(HMODULE module
, DWORD reason
, const void *ptr_in
, void *ptr_out
)
867 if (reason
== DLL_PROCESS_ATTACH
)
868 return init_freetype_lib(module
, reason
, ptr_in
, ptr_out
);
869 else if (reason
== DLL_PROCESS_DETACH
)
870 return release_freetype_lib();
871 return STATUS_SUCCESS
;