dwrite: Store vertical orientation property.
[wine.git] / dlls / dwrite / freetype.c
blob84c9cad4c9a2135bb705b5bf4e13d281cc773f3b
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_Init_FreeType);
67 MAKE_FUNCPTR(FT_Library_Version);
68 MAKE_FUNCPTR(FT_Load_Glyph);
69 MAKE_FUNCPTR(FT_New_Memory_Face);
70 MAKE_FUNCPTR(FT_Outline_Transform);
71 MAKE_FUNCPTR(FTC_CMapCache_Lookup);
72 MAKE_FUNCPTR(FTC_CMapCache_New);
73 MAKE_FUNCPTR(FTC_Manager_New);
74 MAKE_FUNCPTR(FTC_Manager_Done);
75 MAKE_FUNCPTR(FTC_Manager_LookupFace);
76 MAKE_FUNCPTR(FTC_Manager_LookupSize);
77 MAKE_FUNCPTR(FTC_Manager_RemoveFaceID);
78 #undef MAKE_FUNCPTR
80 static FT_Error face_requester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *face)
82 IDWriteFontFace *fontface = (IDWriteFontFace*)face_id;
83 IDWriteFontFileStream *stream;
84 IDWriteFontFile *file;
85 const void *data_ptr;
86 UINT32 index, count;
87 FT_Error fterror;
88 UINT64 data_size;
89 void *context;
90 HRESULT hr;
92 *face = NULL;
94 count = 1;
95 hr = IDWriteFontFace_GetFiles(fontface, &count, &file);
96 if (FAILED(hr))
97 return FT_Err_Ok;
99 hr = get_filestream_from_file(file, &stream);
100 IDWriteFontFile_Release(file);
101 if (FAILED(hr))
102 return FT_Err_Ok;
104 hr = IDWriteFontFileStream_GetFileSize(stream, &data_size);
105 if (FAILED(hr)) {
106 fterror = FT_Err_Invalid_Stream_Read;
107 goto fail;
110 hr = IDWriteFontFileStream_ReadFileFragment(stream, &data_ptr, 0, data_size, &context);
111 if (FAILED(hr)) {
112 fterror = FT_Err_Invalid_Stream_Read;
113 goto fail;
116 index = IDWriteFontFace_GetIndex(fontface);
117 fterror = pFT_New_Memory_Face(library, data_ptr, data_size, index, face);
118 IDWriteFontFileStream_ReleaseFileFragment(stream, context);
120 fail:
121 IDWriteFontFileStream_Release(stream);
123 return fterror;
126 BOOL init_freetype(void)
128 FT_Version_t FT_Version;
130 ft_handle = wine_dlopen(SONAME_LIBFREETYPE, RTLD_NOW, NULL, 0);
131 if (!ft_handle) {
132 WINE_MESSAGE("Wine cannot find the FreeType font library.\n");
133 return FALSE;
136 #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;}
137 LOAD_FUNCPTR(FT_Done_FreeType)
138 LOAD_FUNCPTR(FT_Init_FreeType)
139 LOAD_FUNCPTR(FT_Library_Version)
140 LOAD_FUNCPTR(FT_Load_Glyph)
141 LOAD_FUNCPTR(FT_New_Memory_Face)
142 LOAD_FUNCPTR(FT_Outline_Transform)
143 LOAD_FUNCPTR(FTC_CMapCache_Lookup)
144 LOAD_FUNCPTR(FTC_CMapCache_New)
145 LOAD_FUNCPTR(FTC_Manager_New)
146 LOAD_FUNCPTR(FTC_Manager_Done)
147 LOAD_FUNCPTR(FTC_Manager_LookupFace)
148 LOAD_FUNCPTR(FTC_Manager_LookupSize)
149 LOAD_FUNCPTR(FTC_Manager_RemoveFaceID)
150 #undef LOAD_FUNCPTR
152 if (pFT_Init_FreeType(&library) != 0) {
153 ERR("Can't init FreeType library\n");
154 wine_dlclose(ft_handle, NULL, 0);
155 ft_handle = NULL;
156 return FALSE;
158 pFT_Library_Version(library, &FT_Version.major, &FT_Version.minor, &FT_Version.patch);
160 /* init cache manager */
161 if (pFTC_Manager_New(library, 0, 0, 0, &face_requester, NULL, &cache_manager) != 0 ||
162 pFTC_CMapCache_New(cache_manager, &cmap_cache) != 0) {
164 ERR("Failed to init FreeType cache\n");
165 pFTC_Manager_Done(cache_manager);
166 pFT_Done_FreeType(library);
167 wine_dlclose(ft_handle, NULL, 0);
168 ft_handle = NULL;
169 return FALSE;
172 TRACE("FreeType version is %d.%d.%d\n", FT_Version.major, FT_Version.minor, FT_Version.patch);
173 return TRUE;
175 sym_not_found:
176 WINE_MESSAGE("Wine cannot find certain functions that it needs from FreeType library.\n");
177 wine_dlclose(ft_handle, NULL, 0);
178 ft_handle = NULL;
179 return FALSE;
182 void release_freetype(void)
184 pFTC_Manager_Done(cache_manager);
185 pFT_Done_FreeType(library);
188 void freetype_notify_cacheremove(IDWriteFontFace2 *fontface)
190 EnterCriticalSection(&freetype_cs);
191 pFTC_Manager_RemoveFaceID(cache_manager, fontface);
192 LeaveCriticalSection(&freetype_cs);
195 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace2 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
197 FTC_ScalerRec scaler;
198 FT_Size size;
200 scaler.face_id = fontface;
201 scaler.width = unitsperEm;
202 scaler.height = unitsperEm;
203 scaler.pixel = 1;
204 scaler.x_res = 0;
205 scaler.y_res = 0;
207 EnterCriticalSection(&freetype_cs);
208 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
209 if (pFT_Load_Glyph(size->face, glyph, FT_LOAD_NO_SCALE) == 0) {
210 FT_Glyph_Metrics *metrics = &size->face->glyph->metrics;
212 ret->leftSideBearing = metrics->horiBearingX;
213 ret->advanceWidth = metrics->horiAdvance;
214 ret->rightSideBearing = metrics->horiAdvance - metrics->horiBearingX - metrics->width;
215 ret->topSideBearing = metrics->vertBearingY;
216 ret->advanceHeight = metrics->vertAdvance;
217 ret->bottomSideBearing = metrics->vertAdvance - metrics->vertBearingY - metrics->height;
218 ret->verticalOriginY = metrics->height + metrics->vertBearingY;
221 LeaveCriticalSection(&freetype_cs);
223 return S_OK;
226 BOOL freetype_is_monospaced(IDWriteFontFace2 *fontface)
228 BOOL is_monospaced = FALSE;
229 FT_Face face;
231 EnterCriticalSection(&freetype_cs);
232 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
233 is_monospaced = FT_IS_FIXED_WIDTH(face);
234 LeaveCriticalSection(&freetype_cs);
236 return is_monospaced;
239 static inline void ft_vector_to_d2d_point(const FT_Vector *v, D2D1_POINT_2F *p)
241 p->x = v->x / 64.0;
242 p->y = v->y / 64.0;
245 static HRESULT get_outline_data(const FT_Outline *outline, struct glyph_outline **ret)
247 short i, j, contour = 0;
248 D2D1_POINT_2F *points;
249 UINT16 count = 0;
250 UINT8 *tags;
251 HRESULT hr;
253 /* we need all curves in cubic format */
254 for (i = 0; i < outline->n_points; i++) {
255 /* control point */
256 if (!(outline->tags[i] & FT_Curve_Tag_On)) {
257 if (!(outline->tags[i] & FT_Curve_Tag_Cubic)) {
258 count++;
261 count++;
264 hr = new_glyph_outline(count, ret);
265 if (FAILED(hr))
266 return hr;
268 points = (*ret)->points;
269 tags = (*ret)->tags;
271 ft_vector_to_d2d_point(outline->points, points);
272 tags[0] = OUTLINE_POINT_START;
274 for (i = 1, j = 1; i < outline->n_points; i++, j++) {
275 /* mark start of new contour */
276 if (tags[j-1] & OUTLINE_POINT_END)
277 tags[j] = OUTLINE_POINT_START;
278 else
279 tags[j] = 0;
281 if (outline->tags[i] & FT_Curve_Tag_On) {
282 ft_vector_to_d2d_point(outline->points+i, points+j);
283 tags[j] |= OUTLINE_POINT_LINE;
285 else {
286 /* third order curve */
287 if (outline->tags[i] & FT_Curve_Tag_Cubic) {
288 /* store 3 points, advance 3 points */
290 ft_vector_to_d2d_point(outline->points+i, points+j);
291 ft_vector_to_d2d_point(outline->points+i+1, points+j+1);
292 ft_vector_to_d2d_point(outline->points+i+2, points+j+2);
294 i += 2;
296 else {
297 FT_Vector vec;
299 /* Convert the quadratic Beziers to cubic Beziers.
300 The parametric eqn for a cubic Bezier is, from PLRM:
301 r(t) = at^3 + bt^2 + ct + r0
302 with the control points:
303 r1 = r0 + c/3
304 r2 = r1 + (c + b)/3
305 r3 = r0 + c + b + a
307 A quadratic Bezier has the form:
308 p(t) = (1-t)^2 p0 + 2(1-t)t p1 + t^2 p2
310 So equating powers of t leads to:
311 r1 = 2/3 p1 + 1/3 p0
312 r2 = 2/3 p1 + 1/3 p2
313 and of course r0 = p0, r3 = p2
316 /* r1 */
317 vec.x = 2 * outline->points[i].x + outline->points[i-1].x;
318 vec.y = 2 * outline->points[i].y + outline->points[i-1].y;
319 ft_vector_to_d2d_point(&vec, points+j);
320 points[j].x /= 3.0;
321 points[j].y /= 3.0;
323 /* r2 */
324 vec.x = 2 * outline->points[i].x + outline->points[i+1].x;
325 vec.y = 2 * outline->points[i].y + outline->points[i+1].y;
326 ft_vector_to_d2d_point(&vec, points+j+1);
327 points[j+1].x /= 3.0;
328 points[j+1].y /= 3.0;
330 /* r3 */
331 ft_vector_to_d2d_point(outline->points+i+1, points+j+2);
333 i++;
336 tags[j] = tags[j+1] = tags[j+2] = OUTLINE_POINT_BEZIER;
337 j += 2;
340 /* mark end point */
341 if (i < outline->n_points && outline->contours[contour] == i) {
342 tags[j] |= OUTLINE_POINT_END;
343 contour++;
347 return S_OK;
350 HRESULT freetype_get_glyph_outline(IDWriteFontFace2 *fontface, FLOAT emSize, UINT16 index, USHORT simulations, struct glyph_outline **ret)
352 FTC_ScalerRec scaler;
353 HRESULT hr = S_OK;
354 FT_Size size;
356 scaler.face_id = fontface;
357 scaler.width = emSize;
358 scaler.height = emSize;
359 scaler.pixel = 1;
360 scaler.x_res = 0;
361 scaler.y_res = 0;
363 EnterCriticalSection(&freetype_cs);
364 if (pFTC_Manager_LookupSize(cache_manager, &scaler, &size) == 0) {
365 if (pFT_Load_Glyph(size->face, index, FT_LOAD_DEFAULT) == 0) {
366 FT_Outline *outline = &size->face->glyph->outline;
367 FT_Matrix m;
369 m.xx = 1 << 16;
370 m.xy = simulations & DWRITE_FONT_SIMULATIONS_OBLIQUE ? (1 << 16) / 3 : 0;
371 m.yx = 0;
372 m.yy = -(1 << 16); /* flip Y axis */
374 pFT_Outline_Transform(outline, &m);
376 hr = get_outline_data(outline, ret);
377 if (hr == S_OK)
378 (*ret)->advance = size->face->glyph->metrics.horiAdvance >> 6;
381 LeaveCriticalSection(&freetype_cs);
383 return hr;
386 UINT16 freetype_get_glyphcount(IDWriteFontFace2 *fontface)
388 UINT16 count = 0;
389 FT_Face face;
391 EnterCriticalSection(&freetype_cs);
392 if (pFTC_Manager_LookupFace(cache_manager, fontface, &face) == 0)
393 count = face->num_glyphs;
394 LeaveCriticalSection(&freetype_cs);
396 return count;
399 UINT16 freetype_get_glyphindex(IDWriteFontFace2 *fontface, UINT32 codepoint)
401 UINT16 glyph;
403 EnterCriticalSection(&freetype_cs);
404 glyph = pFTC_CMapCache_Lookup(cmap_cache, fontface, -1, codepoint);
405 LeaveCriticalSection(&freetype_cs);
407 return glyph;
410 #else /* HAVE_FREETYPE */
412 BOOL init_freetype(void)
414 return FALSE;
417 void release_freetype(void)
421 void freetype_notify_cacheremove(IDWriteFontFace2 *fontface)
425 HRESULT freetype_get_design_glyph_metrics(IDWriteFontFace2 *fontface, UINT16 unitsperEm, UINT16 glyph, DWRITE_GLYPH_METRICS *ret)
427 return E_NOTIMPL;
430 BOOL freetype_is_monospaced(IDWriteFontFace2 *fontface)
432 return FALSE;
435 HRESULT freetype_get_glyph_outline(IDWriteFontFace2 *fontface, FLOAT emSize, UINT16 index, USHORT simulations, struct glyph_outline **ret)
437 *ret = NULL;
438 return E_NOTIMPL;
441 UINT16 freetype_get_glyphcount(IDWriteFontFace2 *fontface)
443 return 0;
446 UINT16 freetype_get_glyphindex(IDWriteFontFace2 *fontface, UINT32 codepoint)
448 return 0;
451 #endif /* HAVE_FREETYPE */