riched20: Initial support for changing font properties.
[wine/multimedia.git] / dlls / dwrite / freetype.c
bloba18c5426f33a1fb6205a2cb1bf5dca86593e4c77
1 /*
2 * FreeType integration
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
21 #define COBJMACROS
23 #include "config.h"
24 #include "wine/port.h"
26 #ifdef HAVE_FT2BUILD_H
27 #include <ft2build.h>
28 #include FT_CACHE_H
29 #include FT_FREETYPE_H
30 #include FT_OUTLINE_H
31 #endif /* HAVE_FT2BUILD_H */
33 #include "windef.h"
34 #include "dwrite_2.h"
35 #include "wine/library.h"
36 #include "wine/debug.h"
38 #include "dwrite_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
42 #ifdef HAVE_FREETYPE
44 static CRITICAL_SECTION freetype_cs;
45 static CRITICAL_SECTION_DEBUG critsect_debug =
47 0, 0, &freetype_cs,
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 typedef struct
59 FT_Int major;
60 FT_Int minor;
61 FT_Int patch;
62 } FT_Version_t;
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);
79 #undef MAKE_FUNCPTR
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;
86 const void *data_ptr;
87 UINT32 index, count;
88 FT_Error fterror;
89 UINT64 data_size;
90 void *context;
91 HRESULT hr;
93 *face = NULL;
95 count = 1;
96 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
97 if (FAILED(hr))
98 return FT_Err_Ok;
100 hr = get_filestream_from_file(file, &stream);
101 IDWriteFontFile_Release(file);
102 if (FAILED(hr))
103 return FT_Err_Ok;
105 hr = IDWriteFontFileStream_GetFileSize(stream, &data_size);
106 if (FAILED(hr)) {
107 fterror = FT_Err_Invalid_Stream_Read;
108 goto fail;
111 hr = IDWriteFontFileStream_ReadFileFragment(stream, &data_ptr, 0, data_size, &context);
112 if (FAILED(hr)) {
113 fterror = FT_Err_Invalid_Stream_Read;
114 goto fail;
117 index = IDWriteFontFace_GetIndex(fontface);
118 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
119 IDWriteFontFileStream_ReleaseFileFragment(stream, context);
121 fail:
122 IDWriteFontFileStream_Release(stream);
124 return fterror;
127 BOOL init_freetype(void)
129 FT_Version_t FT_Version;
131 ft_handle = wine_dlopen(SONAME_LIBFREETYPE, RTLD_NOW, NULL, 0);
132 if (!ft_handle) {
133 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
134 return FALSE;
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)
152 #undef LOAD_FUNCPTR
154 if (pFT_Init_FreeType(&library) != 0) {
155 ERR("Can't init FreeType library\n");
156 wine_dlclose(ft_handle, NULL, 0);
157 ft_handle = NULL;
158 return FALSE;
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);
170 ft_handle = NULL;
171 return FALSE;
174 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
175 return TRUE;
177 sym_not_found:
178 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
179 wine_dlclose(ft_handle, NULL, 0);
180 ft_handle = NULL;
181 return FALSE;
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;
200 FT_Size size;
202 scaler.face_id = fontface;
203 scaler.width = unitsperEm;
204 scaler.height = unitsperEm;
205 scaler.pixel = 1;
206 scaler.x_res = 0;
207 scaler.y_res = 0;
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);
225 return S_OK;
228 BOOL freetype_is_monospaced(IDWriteFontFace2 *fontface)
230 BOOL is_monospaced = FALSE;
231 FT_Face face;
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)
243 p->x = v->x / 64.0;
244 p->y = v->y / 64.0;
247 static HRESULT get_outline_data(const FT_Outline *outline, struct glyph_outline **ret)
249 short i, j, contour = 0;
250 D2D1_POINT_2F *points;
251 UINT16 count = 0;
252 UINT8 *tags;
253 HRESULT hr;
255 /* we need all curves in cubic format */
256 for (i = 0; i < outline->n_points; i++) {
257 /* control point */
258 if (!(outline->tags[i] & FT_Curve_Tag_On)) {
259 if (!(outline->tags[i] & FT_Curve_Tag_Cubic)) {
260 count++;
263 count++;
266 hr = new_glyph_outline(count, ret);
267 if (FAILED(hr))
268 return hr;
270 points = (*ret)->points;
271 tags = (*ret)->tags;
273 ft_vector_to_d2d_point(outline->points, points);
274 tags[0] = OUTLINE_POINT_START;
276 for (i = 1, j = 1; i < outline->n_points; i++, j++) {
277 /* mark start of new contour */
278 if (tags[j-1] & OUTLINE_POINT_END)
279 tags[j] = OUTLINE_POINT_START;
280 else
281 tags[j] = 0;
283 if (outline->tags[i] & FT_Curve_Tag_On) {
284 ft_vector_to_d2d_point(outline->points+i, points+j);
285 tags[j] |= OUTLINE_POINT_LINE;
287 else {
288 /* third order curve */
289 if (outline->tags[i] & FT_Curve_Tag_Cubic) {
290 /* store 3 points, advance 3 points */
292 ft_vector_to_d2d_point(outline->points+i, points+j);
293 ft_vector_to_d2d_point(outline->points+i+1, points+j+1);
294 ft_vector_to_d2d_point(outline->points+i+2, points+j+2);
296 i += 2;
298 else {
299 FT_Vector vec;
301 /* Convert the quadratic Beziers to cubic Beziers.
302 The parametric eqn for a cubic Bezier is, from PLRM:
303 r(t) = at^3 + bt^2 + ct + r0
304 with the control points:
305 r1 = r0 + c/3
306 r2 = r1 + (c + b)/3
307 r3 = r0 + c + b + a
309 A quadratic Bezier has the form:
310 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
312 So equating powers of t leads to:
313 r1 = 2/3 p1 + 1/3 p0
314 r2 = 2/3 p1 + 1/3 p2
315 and of course r0 = p0, r3 = p2
318 /* r1 */
319 vec.x = 2 * outline->points[i].x + outline->points[i-1].x;
320 vec.y = 2 * outline->points[i].y + outline->points[i-1].y;
321 ft_vector_to_d2d_point(&vec, points+j);
322 points[j].x /= 3.0;
323 points[j].y /= 3.0;
325 /* r2 */
326 vec.x = 2 * outline->points[i].x + outline->points[i+1].x;
327 vec.y = 2 * outline->points[i].y + outline->points[i+1].y;
328 ft_vector_to_d2d_point(&vec, points+j+1);
329 points[j+1].x /= 3.0;
330 points[j+1].y /= 3.0;
332 /* r3 */
333 ft_vector_to_d2d_point(outline->points+i+1, points+j+2);
335 i++;
338 tags[j] = tags[j+1] = tags[j+2] = OUTLINE_POINT_BEZIER;
339 j += 2;
342 /* mark end point */
343 if (i < outline->n_points && outline->contours[contour] == i) {
344 tags[j] |= OUTLINE_POINT_END;
345 contour++;
349 return S_OK;
352 HRESULT freetype_get_glyph_outline(IDWriteFontFace2 *fontface, FLOAT emSize, UINT16 index, USHORT simulations, struct glyph_outline **ret)
354 FTC_ScalerRec scaler;
355 HRESULT hr = S_OK;
356 FT_Size size;
358 scaler.face_id = fontface;
359 scaler.width = emSize;
360 scaler.height = emSize;
361 scaler.pixel = 1;
362 scaler.x_res = 0;
363 scaler.y_res = 0;
365 EnterCriticalSection(&freetype_cs);
366 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
367 if (pFT_Load_Glyph(size->face, index, FT_LOAD_DEFAULT) == 0) {
368 FT_Outline *outline = &size->face->glyph->outline;
369 FT_Matrix m;
371 m.xx = 1 << 16;
372 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
373 m.yx = 0;
374 m.yy = -(1 << 16); /* flip Y axis */
376 pFT_Outline_Transform(outline, &m);
378 hr = get_outline_data(outline, ret);
379 if (hr == S_OK)
380 (*ret)->advance = size->face->glyph->metrics.horiAdvance >> 6;
383 LeaveCriticalSection(&freetype_cs);
385 return hr;
388 UINT16 freetype_get_glyphcount(IDWriteFontFace2 *fontface)
390 UINT16 count = 0;
391 FT_Face face;
393 EnterCriticalSection(&freetype_cs);
394 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
395 count = face->num_glyphs;
396 LeaveCriticalSection(&freetype_cs);
398 return count;
401 UINT16 freetype_get_glyphindex(IDWriteFontFace2 *fontface, UINT32 codepoint)
403 UINT16 glyph;
405 EnterCriticalSection(&freetype_cs);
406 glyph = pFTC_CMapCache_Lookup(cmap_cache, fontface, -1, codepoint);
407 LeaveCriticalSection(&freetype_cs);
409 return glyph;
412 BOOL freetype_has_kerning_pairs(IDWriteFontFace2 *fontface)
414 BOOL has_kerning_pairs = FALSE;
415 FT_Face face;
417 EnterCriticalSection(&freetype_cs);
418 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
419 has_kerning_pairs = FT_HAS_KERNING(face);
420 LeaveCriticalSection(&freetype_cs);
422 return has_kerning_pairs;
425 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace2 *fontface, UINT16 left, UINT16 right)
427 INT32 adjustment = 0;
428 FT_Face face;
430 EnterCriticalSection(&freetype_cs);
431 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0) {
432 FT_Vector kern;
433 if (FT_HAS_KERNING(face)) {
434 pFT_Get_Kerning(face, left, right, FT_KERNING_UNSCALED, &kern);
435 adjustment = kern.x;
438 LeaveCriticalSection(&freetype_cs);
440 return adjustment;
443 #else /* HAVE_FREETYPE */
445 BOOL init_freetype(void)
447 return FALSE;
450 void release_freetype(void)
454 void freetype_notify_cacheremove(IDWriteFontFace2 *fontface)
458 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace2 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
460 return E_NOTIMPL;
463 BOOL freetype_is_monospaced(IDWriteFontFace2 *fontface)
465 return FALSE;
468 HRESULT freetype_get_glyph_outline(IDWriteFontFace2 *fontface, FLOAT emSize, UINT16 index, USHORT simulations, struct glyph_outline **ret)
470 *ret = NULL;
471 return E_NOTIMPL;
474 UINT16 freetype_get_glyphcount(IDWriteFontFace2 *fontface)
476 return 0;
479 UINT16 freetype_get_glyphindex(IDWriteFontFace2 *fontface, UINT32 codepoint)
481 return 0;
484 BOOL freetype_has_kerning_pairs(IDWriteFontFace2 *fontface)
486 return FALSE;
489 INT32 freetype_get_kerning_pair_adjustment(IDWriteFontFace2 *fontface, UINT16 left, UINT16 right)
491 return 0;
494 #endif /* HAVE_FREETYPE */