Debian Codegraph @

texk/web2c/xetexdir/XeTeXFontInst.cpp

raw
1 /****************************************************************************\
2 Part of the XeTeX typesetting system
3 Copyright (c) 1994-2008 by SIL International
4 Copyright (c) 2009 by Jonathan Kew
5
6 SIL Author(s): Jonathan Kew
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
23 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 Except as contained in this notice, the name of the copyright holders
28 shall not be used in advertising or otherwise to promote the sale,
29 use or other dealings in this Software without prior written
30 authorization from the copyright holders.
31 \****************************************************************************/
32
33 /*
34 * file name: XeTeXFontInst.cpp
35 *
36 * created on: 2005-10-22
37 * created by: Jonathan Kew
38 *
39 * originally based on PortableFontInstance.cpp from ICU
40 */
41
42 #include <w2c/config.h>
43 #include <kpathsea/kpathsea.h>
44
45 #include "XeTeXFontInst.h"
46 #include "XeTeXLayoutInterface.h"
47 #include "XeTeX_ext.h"
48
49 #include <string.h>
50 #include FT_GLYPH_H
51 #include FT_ADVANCES_H
52
53 FT_Library gFreeTypeLibrary = 0;
54
55 static hb_font_funcs_t* hbFontFuncs = NULL;
56
57 XeTeXFontInst::XeTeXFontInst(const char* pathname, int index, float pointSize, int &status)
58 : m_unitsPerEM(0)
59 , m_pointSize(pointSize)
60 , m_ascent(0)
61 , m_descent(0)
62 , m_capHeight(0)
63 , m_xHeight(0)
64 , m_italicAngle(0)
65 , m_vertical(false)
66 , m_filename(NULL)
67 , m_index(0)
68 , m_ftFace(0)
69 , m_hbFont(NULL)
70 {
71 if (pathname != NULL)
72 initialize(pathname, index, status);
73 }
74
75 XeTeXFontInst::~XeTeXFontInst()
76 {
77 if (m_ftFace != 0) {
78 FT_Done_Face(m_ftFace);
79 m_ftFace = 0;
80 }
81 hb_font_destroy(m_hbFont);
82 delete[] m_filename;
83 }
84
85 /* HarfBuzz font functions */
86
87 static hb_bool_t
88 _get_glyph(hb_font_t*, void *font_data, hb_codepoint_t ch, hb_codepoint_t vs, hb_codepoint_t *gid, void*)
89 {
90 FT_Face face = (FT_Face) font_data;
91 *gid = 0;
92
93 if (vs)
94 *gid = FT_Face_GetCharVariantIndex (face, ch, vs);
95
96 if (*gid == 0)
97 *gid = FT_Get_Char_Index (face, ch);
98
99 return *gid != 0;
100 }
101
102 static FT_Fixed
103 _get_glyph_advance(FT_Face face, FT_UInt gid, bool vertical)
104 {
105 FT_Error error;
106 FT_Fixed advance;
107 int flags = FT_LOAD_NO_SCALE;
108
109 if (vertical)
110 flags |= FT_LOAD_VERTICAL_LAYOUT;
111
112 error = FT_Get_Advance(face, gid, flags, &advance);
113 if (error)
114 advance = 0;
115 else
116 advance = advance;
117
118 /* FreeType's vertical metrics grows downward */
119 if (vertical)
120 advance = -advance;
121
122 return advance;
123 }
124
125 static hb_position_t
126 _get_glyph_h_advance(hb_font_t*, void *font_data, hb_codepoint_t gid, void*)
127 {
128 return _get_glyph_advance((FT_Face) font_data, gid, false);
129 }
130
131 static hb_position_t
132 _get_glyph_v_advance(hb_font_t*, void *font_data, hb_codepoint_t gid, void*)
133 {
134 return _get_glyph_advance((FT_Face) font_data, gid, true);
135 }
136
137 static hb_bool_t
138 _get_glyph_h_origin(hb_font_t*, void *font_data, hb_codepoint_t gid, hb_position_t *x, hb_position_t *y, void*)
139 {
140 // horizontal origin is (0, 0)
141 return true;
142 }
143
144 static hb_bool_t
145 _get_glyph_v_origin(hb_font_t*, void *font_data, hb_codepoint_t gid, hb_position_t *x, hb_position_t *y, void*)
146 {
147 // vertical origin is (0, 0) for now
148 return true;
149
150 // TODO
151 // Keep the code below for reference, for now we want to keep vertical
152 // origin at (0, 0) for compatibility with pre-0.9999.
153 // Reconsider this (e.g. using BASE table) when we get around overhauling
154 // the text directionality model and implementing real vertical typesetting.
155
156 FT_Face face = (FT_Face) font_data;
157 FT_Error error;
158
159 error = FT_Load_Glyph (face, gid, FT_LOAD_NO_SCALE);
160 if (!error) {
161 *x = face->glyph->metrics.horiBearingX - face->glyph->metrics.vertBearingX;
162 *y = face->glyph->metrics.horiBearingY - (-face->glyph->metrics.vertBearingY);
163 }
164
165 return !error;
166 }
167
168 static hb_position_t
169 _get_glyph_h_kerning(hb_font_t*, void *font_data, hb_codepoint_t gid1, hb_codepoint_t gid2, void*)
170 {
171 FT_Face face = (FT_Face) font_data;
172 FT_Error error;
173 FT_Vector kerning;
174 hb_position_t ret;
175
176 error = FT_Get_Kerning (face, gid1, gid2, FT_KERNING_UNSCALED, &kerning);
177 if (error)
178 ret = 0;
179 else
180 ret = kerning.x;
181 return ret;
182 }
183
184 static hb_position_t
185 _get_glyph_v_kerning(hb_font_t*, void *font_data, hb_codepoint_t gid1, hb_codepoint_t gid2, void*)
186 {
187 /* FreeType does not support vertical kerning */
188 return 0;
189 }
190
191 static hb_bool_t
192 _get_glyph_extents(hb_font_t*, void *font_data, hb_codepoint_t gid, hb_glyph_extents_t *extents, void*)
193 {
194 FT_Face face = (FT_Face) font_data;
195 FT_Error error;
196
197 error = FT_Load_Glyph (face, gid, FT_LOAD_NO_SCALE);
198 if (!error) {
199 extents->x_bearing = face->glyph->metrics.horiBearingX;
200 extents->y_bearing = face->glyph->metrics.horiBearingY;
201 extents->width = face->glyph->metrics.width;
202 extents->height = -face->glyph->metrics.height;
203 }
204
205 return !error;
206 }
207
208 static hb_bool_t
209 _get_glyph_contour_point(hb_font_t*, void *font_data, hb_codepoint_t gid, unsigned int point_index, hb_position_t *x, hb_position_t *y, void*)
210 {
211 FT_Face face = (FT_Face) font_data;
212 FT_Error error;
213 bool ret = false;
214
215 error = FT_Load_Glyph (face, gid, FT_LOAD_NO_SCALE);
216 if (!error) {
217 if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
218 if (point_index < (unsigned int) face->glyph->outline.n_points) {
219 *x = face->glyph->outline.points[point_index].x;
220 *y = face->glyph->outline.points[point_index].y;
221 ret = true;
222 }
223 }
224 }
225
226 return ret;
227 }
228
229 static hb_bool_t
230 _get_glyph_name(hb_font_t *, void *font_data, hb_codepoint_t gid, char *name, unsigned int size, void *)
231 {
232 FT_Face face = (FT_Face) font_data;
233 bool ret = false;
234
235 ret = !FT_Get_Glyph_Name (face, gid, name, size);
236 if (ret && (size && !*name))
237 ret = false;
238
239 return ret;
240 }
241
242 static hb_font_funcs_t *
243 _get_font_funcs(void)
244 {
245 static hb_font_funcs_t* funcs = hb_font_funcs_create();
246
247 hb_font_funcs_set_glyph_func (funcs, _get_glyph, NULL, NULL);
248 hb_font_funcs_set_glyph_h_advance_func (funcs, _get_glyph_h_advance, NULL, NULL);
249 hb_font_funcs_set_glyph_v_advance_func (funcs, _get_glyph_v_advance, NULL, NULL);
250 hb_font_funcs_set_glyph_h_origin_func (funcs, _get_glyph_h_origin, NULL, NULL);
251 hb_font_funcs_set_glyph_v_origin_func (funcs, _get_glyph_v_origin, NULL, NULL);
252 hb_font_funcs_set_glyph_h_kerning_func (funcs, _get_glyph_h_kerning, NULL, NULL);
253 hb_font_funcs_set_glyph_v_kerning_func (funcs, _get_glyph_v_kerning, NULL, NULL);
254 hb_font_funcs_set_glyph_extents_func (funcs, _get_glyph_extents, NULL, NULL);
255 hb_font_funcs_set_glyph_contour_point_func (funcs, _get_glyph_contour_point, NULL, NULL);
256 hb_font_funcs_set_glyph_name_func (funcs, _get_glyph_name, NULL, NULL);
257
258 return funcs;
259 }
260
261 static hb_blob_t *
262 _get_table(hb_face_t *, hb_tag_t tag, void *user_data)
263 {
264 FT_Face face = (FT_Face) user_data;
265 FT_ULong length = 0;
266 FT_Byte *table;
267 FT_Error error;
268 hb_blob_t* blob = NULL;
269
270 error = FT_Load_Sfnt_Table(face, tag, 0, NULL, &length);
271 if (!error) {
272 table = (FT_Byte *) xmalloc(length * sizeof(char));
273 if (table != NULL) {
274 error = FT_Load_Sfnt_Table(face, tag, 0, (FT_Byte*)table, &length);
275 if (!error) {
276 blob = hb_blob_create((const char*) table, length, HB_MEMORY_MODE_WRITABLE, table, free);
277 } else {
278 free(table);
279 }
280 }
281 }
282
283 return blob;
284 }
285
286 void
287 XeTeXFontInst::initialize(const char* pathname, int index, int &status)
288 {
289 TT_Postscript *postTable;
290 TT_OS2* os2Table;
291 FT_Error error;
292 hb_face_t *hbFace;
293
294 if (!gFreeTypeLibrary) {
295 error = FT_Init_FreeType(&gFreeTypeLibrary);
296 if (error) {
297 fprintf(stderr, "FreeType initialization failed! (%d)\n", error);
298 exit(1);
299 }
300 }
301
302 error = FT_New_Face(gFreeTypeLibrary, pathname, index, &m_ftFace);
303 if (error) {
304 status = 1;
305 return;
306 }
307
308 if (!FT_IS_SCALABLE(m_ftFace)) {
309 status = 1;
310 return;
311 }
312
313 /* for non-sfnt-packaged fonts (presumably Type 1), see if there is an AFM file we can attach */
314 if (index == 0 && !FT_IS_SFNT(m_ftFace)) {
315 char* afm = xstrdup (xbasename (pathname));
316 char* p = strrchr (afm, '.');
317 if (p != NULL && strlen(p) == 4 && tolower(*(p+1)) == 'p' &&
318 tolower(*(p+2)) == 'f')
319 strcpy(p, ".afm");
320 char *fullafm = kpse_find_file (afm, kpse_afm_format, 0);
321 free (afm);
322 if (fullafm) {
323 FT_Attach_File(m_ftFace, fullafm);
324 free (fullafm);
325 }
326 }
327
328 m_filename = xstrdup(pathname);
329 m_index = index;
330 m_unitsPerEM = m_ftFace->units_per_EM;
331 m_ascent = unitsToPoints(m_ftFace->ascender);
332 m_descent = unitsToPoints(m_ftFace->descender);
333
334 postTable = (TT_Postscript *) getFontTable(ft_sfnt_post);
335 if (postTable != NULL) {
336 m_italicAngle = Fix2D(postTable->italicAngle);
337 }
338
339 os2Table = (TT_OS2*) getFontTable(ft_sfnt_os2);
340 if (os2Table) {
341 m_capHeight = unitsToPoints(os2Table->sCapHeight);
342 m_xHeight = unitsToPoints(os2Table->sxHeight);
343 }
344
345 // Set up HarfBuzz font
346 hbFace = hb_face_create_for_tables(_get_table, m_ftFace, NULL);
347 hb_face_set_index(hbFace, index);
348 hb_face_set_upem(hbFace, m_unitsPerEM);
349 m_hbFont = hb_font_create(hbFace);
350 hb_face_destroy(hbFace);
351
352 if (hbFontFuncs == NULL)
353 hbFontFuncs = _get_font_funcs();
354
355 hb_font_set_funcs(m_hbFont, hbFontFuncs, m_ftFace, NULL);
356 hb_font_set_scale(m_hbFont, m_unitsPerEM, m_unitsPerEM);
357 // We don’t want device tables adjustments
358 hb_font_set_ppem(m_hbFont, 0, 0);
359
360 return;
361 }
362
363 void
364 XeTeXFontInst::setLayoutDirVertical(bool vertical)
365 {
366 m_vertical = vertical;
367 }
368
369 void *
370 XeTeXFontInst::getFontTable(OTTag tag) const
371 {
372 FT_ULong tmpLength = 0;
373 FT_Error error = FT_Load_Sfnt_Table(m_ftFace, tag, 0, NULL, &tmpLength);
374 if (error)
375 return NULL;
376
377 void* table = xmalloc(tmpLength * sizeof(char));
378 if (table != NULL) {
379 error = FT_Load_Sfnt_Table(m_ftFace, tag, 0, (FT_Byte*)table, &tmpLength);
380 if (error) {
381 free((void *) table);
382 return NULL;
383 }
384 }
385
386 return table;
387 }
388
389 void *
390 XeTeXFontInst::getFontTable(FT_Sfnt_Tag tag) const
391 {
392 return FT_Get_Sfnt_Table(m_ftFace, tag);
393 }
394
395 void
396 XeTeXFontInst::getGlyphBounds(GlyphID gid, GlyphBBox* bbox)
397 {
398 bbox->xMin = bbox->yMin = bbox->xMax = bbox->yMax = 0.0;
399
400 FT_Error error = FT_Load_Glyph(m_ftFace, gid, FT_LOAD_NO_SCALE);
401 if (error)
402 return;
403
404 FT_Glyph glyph;
405 error = FT_Get_Glyph(m_ftFace->glyph, &glyph);
406 if (error == 0) {
407 FT_BBox ft_bbox;
408 FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_UNSCALED, &ft_bbox);
409 bbox->xMin = unitsToPoints(ft_bbox.xMin);
410 bbox->yMin = unitsToPoints(ft_bbox.yMin);
411 bbox->xMax = unitsToPoints(ft_bbox.xMax);
412 bbox->yMax = unitsToPoints(ft_bbox.yMax);
413 FT_Done_Glyph(glyph);
414 }
415 }
416
417 GlyphID
418 XeTeXFontInst::mapCharToGlyph(UChar32 ch) const
419 {
420 return FT_Get_Char_Index(m_ftFace, ch);
421 }
422
423 uint16_t
424 XeTeXFontInst::getNumGlyphs() const
425 {
426 return m_ftFace->num_glyphs;
427 }
428
429 float
430 XeTeXFontInst::getGlyphWidth(GlyphID gid)
431 {
432 return unitsToPoints(_get_glyph_advance(m_ftFace, gid, false));
433 }
434
435 void
436 XeTeXFontInst::getGlyphHeightDepth(GlyphID gid, float* ht, float* dp)
437 {
438 GlyphBBox bbox;
439 getGlyphBounds(gid, &bbox);
440
441 if (ht)
442 *ht = bbox.yMax;
443 if (dp)
444 *dp = -bbox.yMin;
445 }
446
447 void
448 XeTeXFontInst::getGlyphSidebearings(GlyphID gid, float* lsb, float* rsb)
449 {
450 float width = getGlyphWidth(gid);
451
452 GlyphBBox bbox;
453 getGlyphBounds(gid, &bbox);
454
455 if (lsb)
456 *lsb = bbox.xMin;
457 if (rsb)
458 *rsb = width - bbox.xMax;
459 }
460
461 float
462 XeTeXFontInst::getGlyphItalCorr(GlyphID gid)
463 {
464 float rval = 0.0;
465
466 float width = getGlyphWidth(gid);
467
468 GlyphBBox bbox;
469 getGlyphBounds(gid, &bbox);
470
471 if (bbox.xMax > width)
472 rval = bbox.xMax - width;
473
474 return rval;
475 }
476
477 GlyphID
478 XeTeXFontInst::mapGlyphToIndex(const char* glyphName) const
479 {
480 return FT_Get_Name_Index(m_ftFace, const_cast<char*>(glyphName));
481 }
482
483 const char*
484 XeTeXFontInst::getGlyphName(GlyphID gid, int& nameLen)
485 {
486 if (FT_HAS_GLYPH_NAMES(m_ftFace)) {
487 static char buffer[256];
488 FT_Get_Glyph_Name(m_ftFace, gid, buffer, 256);
489 nameLen = strlen(buffer);
490 return &buffer[0];
491 }
492 else {
493 nameLen = 0;
494 return NULL;
495 }
496 }
497
498 UChar32
499 XeTeXFontInst::getFirstCharCode()
500 {
501 FT_UInt gindex;
502 return FT_Get_First_Char(m_ftFace, &gindex);
503 }
504
505 UChar32
506 XeTeXFontInst::getLastCharCode()
507 {
508 FT_UInt gindex;
509 UChar32 ch = FT_Get_First_Char(m_ftFace, &gindex);
510 UChar32 prev = ch;
511 while (gindex != 0) {
512 prev = ch;
513 ch = FT_Get_Next_Char(m_ftFace, ch, &gindex);
514 }
515 return prev;
516 }