(xftfont_open): Genarate a multibyte string if give names are utf-8.
[bpt/emacs.git] / src / ftfont.c
1 /* ftfont.c -- FreeType font driver.
2 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include <config.h>
23 #include <stdio.h>
24
25 #include <fontconfig/fontconfig.h>
26 #include <fontconfig/fcfreetype.h>
27
28 #include "lisp.h"
29 #include "dispextern.h"
30 #include "frame.h"
31 #include "blockinput.h"
32 #include "character.h"
33 #include "charset.h"
34 #include "coding.h"
35 #include "composite.h"
36 #include "fontset.h"
37 #include "font.h"
38 #include "ftfont.h"
39
40 /* Symbolic type of this font-driver. */
41 Lisp_Object Qfreetype;
42
43 /* Fontconfig's generic families and their aliases. */
44 static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
45
46 /* Flag to tell if FcInit is already called or not. */
47 static int fc_initialized;
48
49 /* Handle to a FreeType library instance. */
50 static FT_Library ft_library;
51
52 /* Cache for FreeType fonts. */
53 static Lisp_Object freetype_font_cache;
54
55 /* Cache for FT_Face and FcCharSet. */
56 static Lisp_Object ft_face_cache;
57
58 /* The actual structure for FreeType font that can be casted to struct
59 font. */
60
61 struct ftfont_info
62 {
63 struct font font;
64 #ifdef HAVE_LIBOTF
65 /* The following three members must be here in this order to be
66 compatible with struct xftfont_info (in xftfont.c). */
67 int maybe_otf; /* Flag to tell if this may be OTF or not. */
68 OTF *otf;
69 #endif /* HAVE_LIBOTF */
70 FT_Size ft_size;
71 int index;
72 };
73
74 static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object));
75
76 static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object,
77 FcPattern *));
78 static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object, int));
79
80 Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
81
82 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
83
84 static struct
85 {
86 /* registry name */
87 char *name;
88 /* characters to distinguish the charset from the others */
89 int uniquifier[6];
90 /* additional constraint by language */
91 char *lang;
92 /* set on demand */
93 FcCharSet *fc_charset;
94 } fc_charset_table[] =
95 { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
96 { "iso8859-2", { 0x00A0, 0x010E }},
97 { "iso8859-3", { 0x00A0, 0x0108 }},
98 { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
99 { "iso8859-5", { 0x00A0, 0x0401 }},
100 { "iso8859-6", { 0x00A0, 0x060C }},
101 { "iso8859-7", { 0x00A0, 0x0384 }},
102 { "iso8859-8", { 0x00A0, 0x05D0 }},
103 { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
104 { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
105 { "iso8859-11", { 0x00A0, 0x0E01 }},
106 { "iso8859-13", { 0x00A0, 0x201C }},
107 { "iso8859-14", { 0x00A0, 0x0174 }},
108 { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
109 { "iso8859-16", { 0x00A0, 0x0218}},
110 { "gb2312.1980-0", { 0x4E13 }, "zh-cn"},
111 { "big5-0", { 0xF6B1 }, "zh-tw" },
112 { "jisx0208.1983-0", { 0x4E55 }, "ja"},
113 { "ksc5601.1985-0", { 0xAC00 }, "ko"},
114 { "cns11643.1992-1", { 0xFE32 }, "zh-tw"},
115 { "cns11643.1992-2", { 0x4E33, 0x7934 }},
116 { "cns11643.1992-3", { 0x201A9 }},
117 { "cns11643.1992-4", { 0x20057 }},
118 { "cns11643.1992-5", { 0x20000 }},
119 { "cns11643.1992-6", { 0x20003 }},
120 { "cns11643.1992-7", { 0x20055 }},
121 { "gbk-0", { 0x4E06 }, "zh-cn"},
122 { "jisx0212.1990-0", { 0x4E44 }},
123 { "jisx0213.2000-1", { 0xFA10 }, "ja"},
124 { "jisx0213.2000-2", { 0xFA49 }},
125 { "jisx0213.2004-1", { 0x20B9F }},
126 { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"},
127 { "tis620.2529-1", { 0x0E01 }, "th"},
128 { "windows-1251", { 0x0401, 0x0490 }, "ru"},
129 { "koi8-r", { 0x0401, 0x2219 }, "ru"},
130 { "mulelao-1", { 0x0E81 }, "lo"},
131 { "unicode-sip", { 0x20000 }},
132 { NULL }
133 };
134
135 extern Lisp_Object Qc, Qm, Qp, Qd;
136
137 static Lisp_Object
138 ftfont_pattern_entity (p, extra)
139 FcPattern *p;
140 Lisp_Object extra;
141 {
142 Lisp_Object entity;
143 char *file, *str;
144 int index;
145 int numeric;
146 double dbl;
147 FcBool b;
148
149 if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
150 return Qnil;
151 if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch)
152 return Qnil;
153
154 entity = font_make_entity ();
155
156 ASET (entity, FONT_TYPE_INDEX, Qfreetype);
157 ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
158
159 if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
160 ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
161 if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
162 ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
163 if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
164 {
165 if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
166 numeric = FC_WEIGHT_MEDIUM;
167 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
168 }
169 if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
170 {
171 numeric += 100;
172 FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
173 }
174 if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
175 {
176 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
177 }
178 if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
179 ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
180 else
181 ASET (entity, FONT_SIZE_INDEX, make_number (0));
182 if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch)
183 ASET (entity, FONT_SPACING_INDEX, make_number (numeric));
184 if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch)
185 {
186 int dpi = dbl;
187 ASET (entity, FONT_DPI_INDEX, make_number (dpi));
188 }
189 if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
190 && b == FcTrue)
191 ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
192
193 ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
194 font_put_extra (entity, QCfont_entity,
195 Fcons (make_unibyte_string ((char *) file,
196 strlen ((char *) file)),
197 make_number (index)));
198 return entity;
199 }
200
201
202 static Lisp_Object ftfont_generic_family_list;
203
204 static Lisp_Object
205 ftfont_resolve_generic_family (family, pattern)
206 Lisp_Object family;
207 FcPattern *pattern;
208 {
209 Lisp_Object slot;
210 FcPattern *match;
211 FcResult result;
212 FcLangSet *langset;
213
214 family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
215 if (EQ (family, Qmono))
216 family = Qmonospace;
217 else if (EQ (family, Qsans) || EQ (family, Qsans__serif))
218 family = Qsans_serif;
219 slot = assq_no_quit (family, ftfont_generic_family_list);
220 if (! CONSP (slot))
221 return Qnil;
222 if (! EQ (XCDR (slot), Qt))
223 return XCDR (slot);
224 pattern = FcPatternDuplicate (pattern);
225 if (! pattern)
226 goto err;
227 FcPatternDel (pattern, FC_FOUNDRY);
228 FcPatternDel (pattern, FC_FAMILY);
229 FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family));
230 if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch)
231 {
232 /* This is to avoid the effect of locale. */
233 langset = FcLangSetCreate ();
234 FcLangSetAdd (langset, "en");
235 FcPatternAddLangSet (pattern, FC_LANG, langset);
236 FcLangSetDestroy (langset);
237 }
238 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
239 FcDefaultSubstitute (pattern);
240 match = FcFontMatch (NULL, pattern, &result);
241 if (match)
242 {
243 FcChar8 *fam;
244
245 if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
246 family = intern ((char *) fam);
247 }
248 else
249 family = Qnil;
250 XSETCDR (slot, family);
251 if (match) FcPatternDestroy (match);
252 err:
253 if (pattern) FcPatternDestroy (pattern);
254 return family;
255 }
256
257 struct ftfont_cache_data
258 {
259 FT_Face ft_face;
260 FcCharSet *fc_charset;
261 };
262
263 static Lisp_Object
264 ftfont_lookup_cache (key, for_face)
265 Lisp_Object key;
266 int for_face;
267 {
268 Lisp_Object cache, val;
269 struct ftfont_cache_data *cache_data;
270
271 cache = assoc_no_quit (key, ft_face_cache);
272 if (NILP (cache))
273 {
274 cache_data = xmalloc (sizeof (struct ftfont_cache_data));
275 cache_data->ft_face = NULL;
276 cache_data->fc_charset = NULL;
277 val = make_save_value (NULL, 0);
278 XSAVE_VALUE (val)->integer = 0;
279 XSAVE_VALUE (val)->pointer = cache_data;
280 cache = Fcons (key, val);
281 ft_face_cache = Fcons (cache, ft_face_cache);
282 }
283 else
284 {
285 val = XCDR (cache);
286 cache_data = XSAVE_VALUE (val)->pointer;
287 }
288 if (for_face ? ! cache_data->ft_face : ! cache_data->fc_charset)
289 {
290 char *filename = (char *) SDATA (XCAR (key));
291 int index = XINT (XCDR (key));
292
293 if (for_face)
294 {
295 if (! ft_library
296 && FT_Init_FreeType (&ft_library) != 0)
297 return Qnil;
298 if (FT_New_Face (ft_library, filename, index, &cache_data->ft_face)
299 != 0)
300 return Qnil;
301 }
302 else
303 {
304 FcPattern *pat;
305 FcFontSet *fontset;
306 FcObjectSet *objset;
307 FcCharSet *charset;
308
309 pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename,
310 FC_INDEX, FcTypeInteger, index, NULL);
311 objset = FcObjectSetBuild (FC_CHARSET, NULL);
312 fontset = FcFontList (NULL, pat, objset);
313 xassert (fontset && fontset->nfont > 0);
314 if (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0, &charset)
315 == FcResultMatch)
316 cache_data->fc_charset = FcCharSetCopy (charset);
317 else
318 cache_data->fc_charset = FcCharSetCreate ();
319 FcFontSetDestroy (fontset);
320 FcObjectSetDestroy (objset);
321 FcPatternDestroy (pat);
322 }
323 }
324 return cache;
325 }
326
327 FcCharSet *
328 ftfont_get_fc_charset (entity)
329 Lisp_Object entity;
330 {
331 Lisp_Object val, cache;
332 struct ftfont_cache_data *cache_data;
333
334 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
335 xassert (CONSP (val));
336 val = XCDR (val);
337 cache = ftfont_lookup_cache (val, 0);
338 val = XCDR (cache);
339 cache_data = XSAVE_VALUE (val)->pointer;
340 return cache_data->fc_charset;
341 }
342
343 #ifdef HAVE_LIBOTF
344 static OTF *
345 ftfont_get_otf (ftfont_info)
346 struct ftfont_info *ftfont_info;
347 {
348 OTF *otf;
349
350 if (ftfont_info->otf)
351 return ftfont_info->otf;
352 if (! ftfont_info->maybe_otf)
353 return NULL;
354 otf = OTF_open_ft_face (ftfont_info->ft_size->face);
355 if (! otf || OTF_get_table (otf, "head") < 0)
356 {
357 if (otf)
358 OTF_close (otf);
359 ftfont_info->maybe_otf = 0;
360 return NULL;
361 }
362 ftfont_info->otf = otf;
363 return otf;
364 }
365 #endif /* HAVE_LIBOTF */
366
367 static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
368 static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
369 static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
370 static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
371 static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
372 static void ftfont_close P_ ((FRAME_PTR, struct font *));
373 static int ftfont_has_char P_ ((Lisp_Object, int));
374 static unsigned ftfont_encode_char P_ ((struct font *, int));
375 static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
376 struct font_metrics *));
377 static int ftfont_get_bitmap P_ ((struct font *, unsigned,
378 struct font_bitmap *, int));
379 static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
380 int *, int *));
381 static Lisp_Object ftfont_otf_capability P_ ((struct font *));
382 static Lisp_Object ftfont_shape P_ ((Lisp_Object));
383
384 struct font_driver ftfont_driver =
385 {
386 0, /* Qfreetype */
387 0, /* case insensitive */
388 ftfont_get_cache,
389 ftfont_list,
390 ftfont_match,
391 ftfont_list_family,
392 NULL,
393 ftfont_open,
394 ftfont_close,
395 /* We can't draw a text without device dependent functions. */
396 NULL,
397 NULL,
398 ftfont_has_char,
399 ftfont_encode_char,
400 ftfont_text_extents,
401 /* We can't draw a text without device dependent functions. */
402 NULL,
403 ftfont_get_bitmap,
404 NULL,
405 NULL,
406 NULL,
407 ftfont_anchor_point,
408 #ifdef HAVE_LIBOTF
409 ftfont_otf_capability,
410 #else /* not HAVE_LIBOTF */
411 NULL,
412 #endif /* not HAVE_LIBOTF */
413 NULL,
414 NULL,
415 NULL,
416 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
417 ftfont_shape
418 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
419 NULL
420 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
421 };
422
423 extern Lisp_Object QCname;
424
425 static Lisp_Object
426 ftfont_get_cache (f)
427 FRAME_PTR f;
428 {
429 return freetype_font_cache;
430 }
431
432 static int
433 ftfont_get_charset (registry)
434 Lisp_Object registry;
435 {
436 char *str = (char *) SDATA (SYMBOL_NAME (registry));
437 char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1);
438 Lisp_Object regexp;
439 int i, j;
440
441 for (i = j = 0; i < SBYTES (SYMBOL_NAME (registry)); i++, j++)
442 {
443 if (str[i] == '.')
444 re[j++] = '\\';
445 else if (str[i] == '*')
446 re[j++] = '.';
447 re[j] = str[i];
448 if (re[j] == '?')
449 re[j] = '.';
450 }
451 re[j] = '\0';
452 regexp = make_unibyte_string (re, j);
453 for (i = 0; fc_charset_table[i].name; i++)
454 if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0)
455 break;
456 if (! fc_charset_table[i].name)
457 return -1;
458 if (! fc_charset_table[i].fc_charset)
459 {
460 FcCharSet *charset = FcCharSetCreate ();
461 int *uniquifier = fc_charset_table[i].uniquifier;
462
463 if (! charset)
464 return -1;
465 for (j = 0; uniquifier[j]; j++)
466 if (! FcCharSetAddChar (charset, uniquifier[j]))
467 {
468 FcCharSetDestroy (charset);
469 return -1;
470 }
471 fc_charset_table[i].fc_charset = charset;
472 }
473 return i;
474 }
475
476 struct OpenTypeSpec
477 {
478 Lisp_Object script;
479 unsigned int script_tag, langsys_tag;
480 int nfeatures[2];
481 unsigned int *features[2];
482 };
483
484 #define OTF_SYM_TAG(SYM, TAG) \
485 do { \
486 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
487 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
488 } while (0)
489
490 #define OTF_TAG_STR(TAG, P) \
491 do { \
492 (P)[0] = (char) (TAG >> 24); \
493 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
494 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
495 (P)[3] = (char) (TAG & 0xFF); \
496 (P)[4] = '\0'; \
497 } while (0)
498
499 #define OTF_TAG_SYM(SYM, TAG) \
500 do { \
501 char str[5]; \
502 \
503 OTF_TAG_STR (TAG, str); \
504 (SYM) = font_intern_prop (str, 4, 1); \
505 } while (0)
506
507
508 static struct OpenTypeSpec *
509 ftfont_get_open_type_spec (Lisp_Object otf_spec)
510 {
511 struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec));
512 Lisp_Object val;
513 int i, j, negative;
514
515 if (! spec)
516 return NULL;
517 spec->script = XCAR (otf_spec);
518 if (! NILP (spec->script))
519 {
520 OTF_SYM_TAG (spec->script, spec->script_tag);
521 val = assq_no_quit (spec->script, Votf_script_alist);
522 if (CONSP (val) && SYMBOLP (XCDR (val)))
523 spec->script = XCDR (val);
524 else
525 spec->script = Qnil;
526 }
527 else
528 spec->script_tag = 0x44464C54; /* "DFLT" */
529 otf_spec = XCDR (otf_spec);
530 val = XCAR (otf_spec);
531 if (! NILP (val))
532 OTF_SYM_TAG (val, spec->langsys_tag);
533 else
534 spec->langsys_tag = 0;
535 spec->nfeatures[0] = spec->nfeatures[1] = 0;
536 for (i = 0; i < 2; i++)
537 {
538 Lisp_Object len;
539
540 otf_spec = XCDR (otf_spec);
541 if (NILP (otf_spec))
542 break;
543 val = XCAR (otf_spec);
544 if (NILP (val))
545 continue;
546 len = Flength (val);
547 spec->features[i] = malloc (sizeof (int) * XINT (len));
548 if (! spec->features[i])
549 {
550 if (i > 0 && spec->features[0])
551 free (spec->features[0]);
552 free (spec);
553 return NULL;
554 }
555 for (j = 0, negative = 0; CONSP (val); val = XCDR (val))
556 {
557 if (NILP (XCAR (val)))
558 negative = 1;
559 else
560 {
561 unsigned int tag;
562
563 OTF_SYM_TAG (XCAR (val), tag);
564 spec->features[i][j++] = negative ? tag & 0x80000000 : tag;
565 }
566 }
567 spec->nfeatures[i] = j;
568 }
569 return spec;
570 }
571
572 static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *,
573 struct OpenTypeSpec **));
574
575 static FcPattern *
576 ftfont_spec_pattern (spec, otlayout, otspec)
577 Lisp_Object spec;
578 char *otlayout;
579 struct OpenTypeSpec **otspec;
580 {
581 Lisp_Object tmp, extra;
582 FcPattern *pattern = NULL;
583 FcCharSet *charset = NULL;
584 FcLangSet *langset = NULL;
585 int n;
586 int dpi = -1;
587 int scalable = -1;
588 Lisp_Object script = Qnil;
589 Lisp_Object registry;
590 int fc_charset_idx;
591
592 if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
593 && SBYTES (SYMBOL_NAME (AREF (spec, FONT_ADSTYLE_INDEX))) > 0)
594 /* Fontconfig doesn't support adstyle property. */
595 return NULL;
596 if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
597 && n < 100)
598 /* Fontconfig doesn't support reverse-italic/obligue. */
599 return NULL;
600
601 if (INTEGERP (AREF (spec, FONT_DPI_INDEX)))
602 dpi = XINT (AREF (spec, FONT_DPI_INDEX));
603 if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
604 && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0)
605 scalable = 1;
606
607 registry = AREF (spec, FONT_REGISTRY_INDEX);
608 if (NILP (registry)
609 || EQ (registry, Qascii_0)
610 || EQ (registry, Qiso10646_1)
611 || EQ (registry, Qunicode_bmp))
612 fc_charset_idx = -1;
613 else
614 {
615 FcChar8 *lang;
616
617 fc_charset_idx = ftfont_get_charset (registry);
618 if (fc_charset_idx < 0)
619 return NULL;
620 charset = fc_charset_table[fc_charset_idx].fc_charset;
621 lang = (FcChar8 *) fc_charset_table[fc_charset_idx].lang;
622 if (lang)
623 {
624 langset = FcLangSetCreate ();
625 if (! langset)
626 goto err;
627 FcLangSetAdd (langset, lang);
628 }
629 }
630
631 otlayout[0] = '\0';
632 for (extra = AREF (spec, FONT_EXTRA_INDEX);
633 CONSP (extra); extra = XCDR (extra))
634 {
635 Lisp_Object key, val;
636
637 key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
638 if (EQ (key, QCdpi))
639 dpi = XINT (val);
640 else if (EQ (key, QClang))
641 {
642 if (! langset)
643 langset = FcLangSetCreate ();
644 if (! langset)
645 goto err;
646 if (SYMBOLP (val))
647 {
648 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
649 goto err;
650 }
651 else
652 for (; CONSP (val); val = XCDR (val))
653 if (SYMBOLP (XCAR (val))
654 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val))))
655 goto err;
656 }
657 else if (EQ (key, QCotf))
658 {
659 *otspec = ftfont_get_open_type_spec (val);
660 if (! *otspec)
661 return NULL;
662 strcat (otlayout, "otlayout:");
663 OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9);
664 script = (*otspec)->script;
665 }
666 else if (EQ (key, QCscript))
667 script = val;
668 else if (EQ (key, QCscalable))
669 scalable = ! NILP (val);
670 }
671
672 if (! NILP (script) && ! charset)
673 {
674 Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);
675
676 if (CONSP (chars) && CONSP (CDR (chars)))
677 {
678 charset = FcCharSetCreate ();
679 if (! charset)
680 goto err;
681 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
682 if (CHARACTERP (XCAR (chars))
683 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
684 goto err;
685 }
686 }
687
688 pattern = FcPatternCreate ();
689 if (! pattern)
690 goto err;
691 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
692 if (! NILP (tmp)
693 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
694 goto err;
695 tmp = AREF (spec, FONT_FAMILY_INDEX);
696 if (! NILP (tmp)
697 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
698 goto err;
699 if (charset
700 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
701 goto err;
702 if (langset
703 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
704 goto err;
705 if (dpi >= 0
706 && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
707 goto err;
708 if (scalable >= 0
709 && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
710 goto err;
711
712 goto finish;
713
714 err:
715 /* We come here because of unexpected error in fontconfig API call
716 (usually insufficient memory). */
717 if (pattern)
718 {
719 FcPatternDestroy (pattern);
720 pattern = NULL;
721 }
722 if (*otspec)
723 {
724 if ((*otspec)->nfeatures[0] > 0)
725 free ((*otspec)->features[0]);
726 if ((*otspec)->nfeatures[1] > 0)
727 free ((*otspec)->features[1]);
728 free (*otspec);
729 *otspec = NULL;
730 }
731
732 finish:
733 if (langset) FcLangSetDestroy (langset);
734 if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset);
735 return pattern;
736 }
737
738 static Lisp_Object
739 ftfont_list (frame, spec)
740 Lisp_Object frame, spec;
741 {
742 Lisp_Object val = Qnil, family;
743 int i;
744 FcPattern *pattern;
745 FcFontSet *fontset = NULL;
746 FcObjectSet *objset = NULL;
747 FcCharSet *charset;
748 Lisp_Object chars = Qnil;
749 FcResult result;
750 char otlayout[15]; /* For "otlayout:XXXX" */
751 struct OpenTypeSpec *otspec = NULL;
752 int spacing = -1;
753
754 if (! fc_initialized)
755 {
756 FcInit ();
757 fc_initialized = 1;
758 }
759
760 pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
761 if (! pattern)
762 return Qnil;
763 if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
764 {
765 val = assq_no_quit (QCscript, AREF (spec, FONT_EXTRA_INDEX));
766 if (! NILP (val))
767 {
768 val = assq_no_quit (XCDR (val), Vscript_representative_chars);
769 if (CONSP (val) && VECTORP (XCDR (val)))
770 chars = XCDR (val);
771 }
772 val = Qnil;
773 }
774 if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
775 spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
776 family = AREF (spec, FONT_FAMILY_INDEX);
777 if (! NILP (family))
778 {
779 Lisp_Object resolved;
780
781 resolved = ftfont_resolve_generic_family (family, pattern);
782 if (! NILP (resolved))
783 {
784 FcPatternDel (pattern, FC_FAMILY);
785 if (! FcPatternAddString (pattern, FC_FAMILY,
786 SYMBOL_FcChar8 (resolved)))
787 goto err;
788 }
789 }
790
791 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
792 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
793 FC_FILE, FC_INDEX,
794 #ifdef FC_CAPABILITY
795 FC_CAPABILITY,
796 #endif /* FC_CAPABILITY */
797 #ifdef FC_FONTFORMAT
798 FC_FONTFORMAT,
799 #endif
800 NULL);
801 if (! objset)
802 goto err;
803 if (! NILP (chars))
804 FcObjectSetAdd (objset, FC_CHARSET);
805
806 fontset = FcFontList (NULL, pattern, objset);
807 if (! fontset || fontset->nfont == 0)
808 goto finish;
809 #if 0
810 /* Need fix because this finds any fonts. */
811 if (fontset->nfont == 0 && ! NILP (family))
812 {
813 /* Try maching with configuration. For instance, the
814 configuration may specify "Nimbus Mono L" as an alias of
815 "Courier". */
816 FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString,
817 SYMBOL_FcChar8 (family), NULL);
818 FcChar8 *fam;
819
820 if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue)
821 {
822 for (i = 0;
823 FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch;
824 i++)
825 {
826 FcPatternDel (pattern, FC_FAMILY);
827 FcPatternAddString (pattern, FC_FAMILY, fam);
828 FcFontSetDestroy (fontset);
829 fontset = FcFontList (NULL, pattern, objset);
830 if (fontset->nfont > 0)
831 break;
832 }
833 }
834 }
835 #endif
836 for (i = 0; i < fontset->nfont; i++)
837 {
838 Lisp_Object entity;
839
840 if (spacing >= 0)
841 {
842 int this;
843
844 if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this)
845 == FcResultMatch)
846 && spacing != this)
847 continue;
848 }
849
850 #ifdef FC_CAPABILITY
851 if (otlayout[0])
852 {
853 FcChar8 *this;
854
855 if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, &this)
856 != FcResultMatch
857 || ! strstr ((char *) this, otlayout))
858 continue;
859 }
860 #endif /* FC_CAPABILITY */
861 #ifdef HAVE_LIBOTF
862 if (otspec)
863 {
864 FcChar8 *file;
865 OTF *otf;
866
867 if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file)
868 != FcResultMatch)
869 continue;
870 otf = OTF_open ((char *) file);
871 if (! otf)
872 continue;
873 if (OTF_check_features (otf, 1,
874 otspec->script_tag, otspec->langsys_tag,
875 otspec->features[0],
876 otspec->nfeatures[0]) != 1
877 || OTF_check_features (otf, 0,
878 otspec->script_tag, otspec->langsys_tag,
879 otspec->features[1],
880 otspec->nfeatures[1]) != 1)
881 continue;
882 }
883 #endif /* HAVE_LIBOTF */
884 if (VECTORP (chars))
885 {
886 int j;
887
888 if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset)
889 != FcResultMatch)
890 continue;
891 for (j = 0; j < ASIZE (chars); j++)
892 if (NATNUMP (AREF (chars, j))
893 && FcCharSetHasChar (charset, XFASTINT (AREF (chars, j))))
894 break;
895 if (j == ASIZE (chars))
896 continue;
897 }
898 entity = ftfont_pattern_entity (fontset->fonts[i],
899 AREF (spec, FONT_EXTRA_INDEX));
900 if (! NILP (entity))
901 val = Fcons (entity, val);
902 }
903 val = Fnreverse (val);
904 goto finish;
905
906 err:
907 /* We come here because of unexpected error in fontconfig API call
908 (usually insufficient memory). */
909 val = Qnil;
910
911 finish:
912 font_add_log ("ftfont-list", spec, val);
913 if (objset) FcObjectSetDestroy (objset);
914 if (fontset) FcFontSetDestroy (fontset);
915 if (pattern) FcPatternDestroy (pattern);
916 return val;
917 }
918
919 static Lisp_Object
920 ftfont_match (frame, spec)
921 Lisp_Object frame, spec;
922 {
923 Lisp_Object entity = Qnil;
924 FcPattern *pattern, *match = NULL;
925 FcResult result;
926 char otlayout[15]; /* For "otlayout:XXXX" */
927 struct OpenTypeSpec *otspec = NULL;
928
929 if (! fc_initialized)
930 {
931 FcInit ();
932 fc_initialized = 1;
933 }
934
935 pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
936 if (! pattern)
937 return Qnil;
938
939 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
940 {
941 FcValue value;
942
943 value.type = FcTypeDouble;
944 value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX));
945 FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse);
946 }
947 if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
948 {
949 FcDefaultSubstitute (pattern);
950 match = FcFontMatch (NULL, pattern, &result);
951 if (match)
952 {
953 entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX));
954 FcPatternDestroy (match);
955 if (! NILP (AREF (spec, FONT_FAMILY_INDEX))
956 && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX),
957 ftfont_generic_family_list))
958 && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX),
959 AREF (entity, FONT_FAMILY_INDEX))))
960 entity = Qnil;
961 }
962 }
963 FcPatternDestroy (pattern);
964
965 font_add_log ("ftfont-match", spec, entity);
966 return entity;
967 }
968
969 static Lisp_Object
970 ftfont_list_family (frame)
971 Lisp_Object frame;
972 {
973 Lisp_Object list = Qnil;
974 FcPattern *pattern = NULL;
975 FcFontSet *fontset = NULL;
976 FcObjectSet *objset = NULL;
977 int i;
978
979 if (! fc_initialized)
980 {
981 FcInit ();
982 fc_initialized = 1;
983 }
984
985 pattern = FcPatternCreate ();
986 if (! pattern)
987 goto finish;
988 objset = FcObjectSetBuild (FC_FAMILY, NULL);
989 if (! objset)
990 goto finish;
991 fontset = FcFontList (NULL, pattern, objset);
992 if (! fontset)
993 goto finish;
994
995 for (i = 0; i < fontset->nfont; i++)
996 {
997 FcPattern *pat = fontset->fonts[i];
998 FcChar8 *str;
999
1000 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
1001 list = Fcons (intern ((char *) str), list);
1002 }
1003
1004 finish:
1005 if (objset) FcObjectSetDestroy (objset);
1006 if (fontset) FcFontSetDestroy (fontset);
1007 if (pattern) FcPatternDestroy (pattern);
1008
1009 return list;
1010 }
1011
1012
1013 static Lisp_Object
1014 ftfont_open (f, entity, pixel_size)
1015 FRAME_PTR f;
1016 Lisp_Object entity;
1017 int pixel_size;
1018 {
1019 struct ftfont_info *ftfont_info;
1020 struct font *font;
1021 struct ftfont_cache_data *cache_data;
1022 FT_Face ft_face;
1023 FT_Size ft_size;
1024 FT_UInt size;
1025 Lisp_Object val, filename, index, cache, font_object;
1026 int scalable;
1027 int spacing;
1028 char name[256];
1029 int i, len;
1030 int upEM;
1031
1032 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
1033 if (! CONSP (val))
1034 return Qnil;
1035 val = XCDR (val);
1036 cache = ftfont_lookup_cache (val, 1);
1037 if (NILP (cache))
1038 return Qnil;
1039 filename = XCAR (val);
1040 index = XCDR (val);
1041 val = XCDR (cache);
1042 cache_data = XSAVE_VALUE (XCDR (cache))->pointer;
1043 ft_face = cache_data->ft_face;
1044 if (XSAVE_VALUE (val)->integer > 0)
1045 {
1046 /* FT_Face in this cache is already used by the different size. */
1047 if (FT_New_Size (ft_face, &ft_size) != 0)
1048 return Qnil;
1049 if (FT_Activate_Size (ft_size) != 0)
1050 {
1051 FT_Done_Size (ft_size);
1052 return Qnil;
1053 }
1054 }
1055 XSAVE_VALUE (val)->integer++;
1056 size = XINT (AREF (entity, FONT_SIZE_INDEX));
1057 if (size == 0)
1058 size = pixel_size;
1059 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
1060 {
1061 if (XSAVE_VALUE (val)->integer == 0)
1062 FT_Done_Face (ft_face);
1063 return Qnil;
1064 }
1065
1066 font_object = font_make_object (VECSIZE (struct ftfont_info), entity, size);
1067 ASET (font_object, FONT_TYPE_INDEX, Qfreetype);
1068 len = font_unparse_xlfd (entity, size, name, 256);
1069 if (len > 0)
1070 ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len));
1071 len = font_unparse_fcname (entity, size, name, 256);
1072 if (len > 0)
1073 ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len));
1074 else
1075 ASET (font_object, FONT_FULLNAME_INDEX,
1076 AREF (font_object, FONT_NAME_INDEX));
1077 ASET (font_object, FONT_FILE_INDEX, filename);
1078 ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (NULL, filename));
1079 font = XFONT_OBJECT (font_object);
1080 ftfont_info = (struct ftfont_info *) font;
1081 ftfont_info->ft_size = ft_face->size;
1082 ftfont_info->index = XINT (index);
1083 #ifdef HAVE_LIBOTF
1084 ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
1085 ftfont_info->otf = NULL;
1086 #endif /* HAVE_LIBOTF */
1087 font->pixel_size = size;
1088 font->driver = &ftfont_driver;
1089 font->encoding_charset = font->repertory_charset = -1;
1090
1091 upEM = ft_face->units_per_EM;
1092 scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
1093 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0);
1094 if (scalable)
1095 {
1096 font->ascent = ft_face->ascender * size / upEM;
1097 font->descent = - ft_face->descender * size / upEM;
1098 font->height = ft_face->height * size / upEM;
1099 }
1100 else
1101 {
1102 font->ascent = ft_face->size->metrics.ascender >> 6;
1103 font->descent = - ft_face->size->metrics.descender >> 6;
1104 font->height = ft_face->size->metrics.height >> 6;
1105 }
1106 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
1107 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
1108 else
1109 spacing = FC_PROPORTIONAL;
1110 if (spacing != FC_PROPORTIONAL)
1111 font->min_width = font->average_width = font->space_width
1112 = (scalable ? ft_face->max_advance_width * size / upEM
1113 : ft_face->size->metrics.max_advance >> 6);
1114 else
1115 {
1116 int n;
1117
1118 font->min_width = font->average_width = font->space_width = 0;
1119 for (i = 32, n = 0; i < 127; i++)
1120 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
1121 {
1122 int this_width = ft_face->glyph->metrics.horiAdvance >> 6;
1123
1124 if (this_width > 0
1125 && (! font->min_width || font->min_width > this_width))
1126 font->min_width = this_width;
1127 if (i == 32)
1128 font->space_width = this_width;
1129 font->average_width += this_width;
1130 n++;
1131 }
1132 if (n > 0)
1133 font->average_width /= n;
1134 }
1135
1136 font->baseline_offset = 0;
1137 font->relative_compose = 0;
1138 font->default_ascent = 0;
1139 font->vertical_centering = 0;
1140 if (scalable)
1141 {
1142 font->underline_position = -ft_face->underline_position * size / upEM;
1143 font->underline_thickness = -ft_face->underline_thickness * size / upEM;
1144 }
1145 else
1146 {
1147 font->underline_position = -1;
1148 font->underline_thickness = 0;
1149 }
1150
1151 return font_object;
1152 }
1153
1154 static void
1155 ftfont_close (f, font)
1156 FRAME_PTR f;
1157 struct font *font;
1158 {
1159 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1160 Lisp_Object val, cache;
1161
1162 val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index));
1163 cache = ftfont_lookup_cache (val, 1);
1164 xassert (CONSP (cache));
1165 val = XCDR (cache);
1166 (XSAVE_VALUE (val)->integer)--;
1167 if (XSAVE_VALUE (val)->integer == 0)
1168 {
1169 struct ftfont_cache_data *cache_data = XSAVE_VALUE (val)->pointer;
1170
1171 FT_Done_Face (cache_data->ft_face);
1172 #ifdef HAVE_LIBOTF
1173 if (ftfont_info->otf)
1174 OTF_close (ftfont_info->otf);
1175 #endif
1176 cache_data->ft_face = NULL;
1177 }
1178 else
1179 FT_Done_Size (ftfont_info->ft_size);
1180 }
1181
1182 static int
1183 ftfont_has_char (entity, c)
1184 Lisp_Object entity;
1185 int c;
1186 {
1187 FcCharSet *charset = ftfont_get_fc_charset (entity);
1188
1189 return (FcCharSetHasChar (charset, c) == FcTrue);
1190 }
1191
1192 static unsigned
1193 ftfont_encode_char (font, c)
1194 struct font *font;
1195 int c;
1196 {
1197 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1198 FT_Face ft_face = ftfont_info->ft_size->face;
1199 FT_ULong charcode = c;
1200 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
1201
1202 return (code > 0 ? code : FONT_INVALID_CODE);
1203 }
1204
1205 static int
1206 ftfont_text_extents (font, code, nglyphs, metrics)
1207 struct font *font;
1208 unsigned *code;
1209 int nglyphs;
1210 struct font_metrics *metrics;
1211 {
1212 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1213 FT_Face ft_face = ftfont_info->ft_size->face;
1214 int width = 0;
1215 int i, first;
1216
1217 if (ftfont_info->ft_size != ft_face->size)
1218 FT_Activate_Size (ftfont_info->ft_size);
1219 if (metrics)
1220 bzero (metrics, sizeof (struct font_metrics));
1221 for (i = 0, first = 1; i < nglyphs; i++)
1222 {
1223 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
1224 {
1225 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
1226
1227 if (first)
1228 {
1229 if (metrics)
1230 {
1231 metrics->lbearing = m->horiBearingX >> 6;
1232 metrics->rbearing = (m->horiBearingX + m->width) >> 6;
1233 metrics->ascent = m->horiBearingY >> 6;
1234 metrics->descent = (m->horiBearingY + m->height) >> 6;
1235 }
1236 first = 0;
1237 }
1238 if (metrics)
1239 {
1240 if (metrics->lbearing > width + (m->horiBearingX >> 6))
1241 metrics->lbearing = width + (m->horiBearingX >> 6);
1242 if (metrics->rbearing
1243 < width + ((m->horiBearingX + m->width) >> 6))
1244 metrics->rbearing
1245 = width + ((m->horiBearingX + m->width) >> 6);
1246 if (metrics->ascent < (m->horiBearingY >> 6))
1247 metrics->ascent = m->horiBearingY >> 6;
1248 if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
1249 metrics->descent = (m->horiBearingY + m->height) >> 6;
1250 }
1251 width += m->horiAdvance >> 6;
1252 }
1253 else
1254 {
1255 width += font->space_width;
1256 }
1257 }
1258 if (metrics)
1259 metrics->width = width;
1260
1261 return width;
1262 }
1263
1264 static int
1265 ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
1266 struct font *font;
1267 unsigned code;
1268 struct font_bitmap *bitmap;
1269 int bits_per_pixel;
1270 {
1271 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1272 FT_Face ft_face = ftfont_info->ft_size->face;
1273 FT_Int32 load_flags = FT_LOAD_RENDER;
1274
1275 if (ftfont_info->ft_size != ft_face->size)
1276 FT_Activate_Size (ftfont_info->ft_size);
1277 if (bits_per_pixel == 1)
1278 {
1279 #ifdef FT_LOAD_TARGET_MONO
1280 load_flags |= FT_LOAD_TARGET_MONO;
1281 #else
1282 load_flags |= FT_LOAD_MONOCHROME;
1283 #endif
1284 }
1285 else if (bits_per_pixel != 8)
1286 /* We don't support such a rendering. */
1287 return -1;
1288
1289 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
1290 return -1;
1291 bitmap->bits_per_pixel
1292 = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1
1293 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8
1294 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8
1295 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8
1296 : -1);
1297 if (bitmap->bits_per_pixel < 0)
1298 /* We don't suport that kind of pixel mode. */
1299 return -1;
1300 bitmap->rows = ft_face->glyph->bitmap.rows;
1301 bitmap->width = ft_face->glyph->bitmap.width;
1302 bitmap->pitch = ft_face->glyph->bitmap.pitch;
1303 bitmap->buffer = ft_face->glyph->bitmap.buffer;
1304 bitmap->left = ft_face->glyph->bitmap_left;
1305 bitmap->top = ft_face->glyph->bitmap_top;
1306 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
1307 bitmap->extra = NULL;
1308
1309 return 0;
1310 }
1311
1312 static int
1313 ftfont_anchor_point (font, code, index, x, y)
1314 struct font *font;
1315 unsigned code;
1316 int index;
1317 int *x, *y;
1318 {
1319 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1320 FT_Face ft_face = ftfont_info->ft_size->face;
1321
1322 if (ftfont_info->ft_size != ft_face->size)
1323 FT_Activate_Size (ftfont_info->ft_size);
1324 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
1325 return -1;
1326 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
1327 return -1;
1328 if (index >= ft_face->glyph->outline.n_points)
1329 return -1;
1330 *x = ft_face->glyph->outline.points[index].x;
1331 *y = ft_face->glyph->outline.points[index].y;
1332 return 0;
1333 }
1334
1335 #ifdef HAVE_LIBOTF
1336
1337 static Lisp_Object
1338 ftfont_otf_features (gsub_gpos)
1339 OTF_GSUB_GPOS *gsub_gpos;
1340 {
1341 Lisp_Object scripts, langsyses, features, sym;
1342 int i, j, k, l;
1343
1344 for (scripts = Qnil, i = gsub_gpos->ScriptList.ScriptCount - 1; i >= 0; i--)
1345 {
1346 OTF_Script *otf_script = gsub_gpos->ScriptList.Script + i;
1347
1348 for (langsyses = Qnil, j = otf_script->LangSysCount - 1; j >= -1; j--)
1349 {
1350 OTF_LangSys *otf_langsys;
1351
1352 if (j >= 0)
1353 otf_langsys = otf_script->LangSys + j;
1354 else if (otf_script->DefaultLangSysOffset)
1355 otf_langsys = &otf_script->DefaultLangSys;
1356 else
1357 break;
1358
1359 for (features = Qnil, k = otf_langsys->FeatureCount - 1; k >= 0; k--)
1360 {
1361 l = otf_langsys->FeatureIndex[k];
1362 if (l >= gsub_gpos->FeatureList.FeatureCount)
1363 continue;
1364 OTF_TAG_SYM (sym, gsub_gpos->FeatureList.Feature[l].FeatureTag);
1365 features = Fcons (sym, features);
1366 }
1367 if (j >= 0)
1368 OTF_TAG_SYM (sym, otf_script->LangSysRecord[j].LangSysTag);
1369 else
1370 sym = Qnil;
1371 langsyses = Fcons (Fcons (sym, features), langsyses);
1372 }
1373
1374 OTF_TAG_SYM (sym, gsub_gpos->ScriptList.Script[i].ScriptTag);
1375 scripts = Fcons (Fcons (sym, langsyses), scripts);
1376 }
1377 return scripts;
1378
1379 }
1380
1381
1382 static Lisp_Object
1383 ftfont_otf_capability (font)
1384 struct font *font;
1385 {
1386 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1387 OTF *otf = ftfont_get_otf (ftfont_info);
1388 Lisp_Object gsub_gpos;
1389
1390 if (! otf)
1391 return Qnil;
1392 gsub_gpos = Fcons (Qnil, Qnil);
1393 if (OTF_get_table (otf, "GSUB") == 0
1394 && otf->gsub->FeatureList.FeatureCount > 0)
1395 XSETCAR (gsub_gpos, ftfont_otf_features (otf->gsub));
1396 if (OTF_get_table (otf, "GPOS") == 0
1397 && otf->gpos->FeatureList.FeatureCount > 0)
1398 XSETCDR (gsub_gpos, ftfont_otf_features (otf->gpos));
1399 return gsub_gpos;
1400 }
1401
1402 #ifdef HAVE_M17N_FLT
1403
1404 struct MFLTFontFT
1405 {
1406 MFLTFont flt_font;
1407 struct font *font;
1408 FT_Face ft_face;
1409 OTF *otf;
1410 };
1411
1412 static int
1413 ftfont_get_glyph_id (font, gstring, from, to)
1414 MFLTFont *font;
1415 MFLTGlyphString *gstring;
1416 int from, to;
1417 {
1418 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1419 FT_Face ft_face = flt_font_ft->ft_face;
1420 MFLTGlyph *g;
1421
1422 for (g = gstring->glyphs + from; from < to; g++, from++)
1423 if (! g->encoded)
1424 {
1425 FT_UInt code = FT_Get_Char_Index (ft_face, g->code);
1426
1427 g->code = code > 0 ? code : FONT_INVALID_CODE;
1428 g->encoded = 1;
1429 }
1430 return 0;
1431 }
1432
1433 static int
1434 ftfont_get_metrics (font, gstring, from, to)
1435 MFLTFont *font;
1436 MFLTGlyphString *gstring;
1437 int from, to;
1438 {
1439 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1440 FT_Face ft_face = flt_font_ft->ft_face;
1441 MFLTGlyph *g;
1442
1443 for (g = gstring->glyphs + from; from < to; g++, from++)
1444 if (! g->measured)
1445 {
1446 if (g->code != FONT_INVALID_CODE)
1447 {
1448 FT_Glyph_Metrics *m;
1449
1450 if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
1451 abort ();
1452 m = &ft_face->glyph->metrics;
1453
1454 g->lbearing = m->horiBearingX;
1455 g->rbearing = m->horiBearingX + m->width;
1456 g->ascent = m->horiBearingY;
1457 g->descent = m->height - m->horiBearingY;
1458 g->xadv = m->horiAdvance;
1459 }
1460 else
1461 {
1462 g->lbearing = 0;
1463 g->rbearing = g->xadv = flt_font_ft->font->space_width << 6;
1464 g->ascent = flt_font_ft->font->ascent << 6;
1465 g->descent = flt_font_ft->font->descent << 6;
1466 }
1467 g->yadv = 0;
1468 g->measured = 1;
1469 }
1470 return 0;
1471 }
1472
1473 static int
1474 ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec)
1475 {
1476 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1477 OTF *otf = flt_font_ft->otf;
1478 OTF_Tag *tags;
1479 int i, n, negative;
1480
1481 for (i = 0; i < 2; i++)
1482 {
1483 if (! spec->features[i])
1484 continue;
1485 for (n = 0; spec->features[i][n]; n++);
1486 tags = alloca (sizeof (OTF_Tag) * n);
1487 for (n = 0, negative = 0; spec->features[i][n]; n++)
1488 {
1489 if (spec->features[i][n] == 0xFFFFFFFF)
1490 negative = 1;
1491 else if (negative)
1492 tags[n - 1] = spec->features[i][n] | 0x80000000;
1493 else
1494 tags[n] = spec->features[i][n];
1495 }
1496 if (n - negative > 0
1497 && OTF_check_features (otf, i == 0, spec->script, spec->langsys,
1498 tags, n - negative) != 1)
1499 return 0;
1500 }
1501 return 1;
1502 }
1503
1504 #define DEVICE_DELTA(table, size) \
1505 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1506 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1507 : 0)
1508
1509 static void
1510 adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor,
1511 unsigned code, int x_ppem, int y_ppem, int *x, int *y)
1512 {
1513 if (anchor->AnchorFormat == 2)
1514 {
1515 FT_Outline *outline;
1516 int ap = anchor->f.f1.AnchorPoint;
1517
1518 FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME);
1519 outline = &ft_face->glyph->outline;
1520 if (ap < outline->n_points)
1521 {
1522 *x = outline->points[ap].x << 6;
1523 *y = outline->points[ap].y << 6;
1524 }
1525 }
1526 else if (anchor->AnchorFormat == 3)
1527 {
1528 if (anchor->f.f2.XDeviceTable.offset
1529 && anchor->f.f2.XDeviceTable.DeltaValue)
1530 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
1531 if (anchor->f.f2.YDeviceTable.offset
1532 && anchor->f.f2.YDeviceTable.DeltaValue)
1533 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
1534 }
1535 }
1536
1537 static OTF_GlyphString otf_gstring;
1538
1539 static int
1540 ftfont_drive_otf (font, spec, in, from, to, out, adjustment)
1541 MFLTFont *font;
1542 MFLTOtfSpec *spec;
1543 MFLTGlyphString *in;
1544 int from, to;
1545 MFLTGlyphString *out;
1546 MFLTGlyphAdjustment *adjustment;
1547 {
1548 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1549 FT_Face ft_face = flt_font_ft->ft_face;
1550 OTF *otf = flt_font_ft->otf;
1551 int len = to - from;
1552 int i, j, gidx;
1553 OTF_Glyph *otfg;
1554 char script[5], *langsys = NULL;
1555 char *gsub_features = NULL, *gpos_features = NULL;
1556
1557 if (len == 0)
1558 return from;
1559 OTF_tag_name (spec->script, script);
1560 if (spec->langsys)
1561 {
1562 langsys = alloca (5);
1563 OTF_tag_name (spec->langsys, langsys);
1564 }
1565 for (i = 0; i < 2; i++)
1566 {
1567 char *p;
1568
1569 if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF)
1570 {
1571 for (j = 0; spec->features[i][j]; j++);
1572 if (i == 0)
1573 p = gsub_features = alloca (6 * j);
1574 else
1575 p = gpos_features = alloca (6 * j);
1576 for (j = 0; spec->features[i][j]; j++)
1577 {
1578 if (spec->features[i][j] == 0xFFFFFFFF)
1579 *p++ = '*', *p++ = ',';
1580 else
1581 {
1582 OTF_tag_name (spec->features[i][j], p);
1583 p[4] = ',';
1584 p += 5;
1585 }
1586 }
1587 *--p = '\0';
1588 }
1589 }
1590
1591 if (otf_gstring.size == 0)
1592 {
1593 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
1594 otf_gstring.size = len;
1595 }
1596 else if (otf_gstring.size < len)
1597 {
1598 otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs,
1599 sizeof (OTF_Glyph) * len);
1600 otf_gstring.size = len;
1601 }
1602 otf_gstring.used = len;
1603 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
1604 for (i = 0; i < len; i++)
1605 {
1606 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
1607 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
1608 }
1609
1610 OTF_drive_gdef (otf, &otf_gstring);
1611 gidx = out->used;
1612
1613 if (gsub_features)
1614 {
1615 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
1616 < 0)
1617 goto simple_copy;
1618 if (out->allocated < out->used + otf_gstring.used)
1619 return -2;
1620 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; )
1621 {
1622 MFLTGlyph *g;
1623 int min_from, max_to;
1624 int j;
1625
1626 g = out->glyphs + out->used;
1627 *g = in->glyphs[from + otfg->f.index.from];
1628 if (g->code != otfg->glyph_id)
1629 {
1630 g->c = 0;
1631 g->code = otfg->glyph_id;
1632 g->measured = 0;
1633 }
1634 out->used++;
1635 min_from = g->from;
1636 max_to = g->to;
1637 if (otfg->f.index.from < otfg->f.index.to)
1638 {
1639 /* OTFG substitutes multiple glyphs in IN. */
1640 for (j = from + otfg->f.index.from + 1;
1641 j <= from + otfg->f.index.to; j++)
1642 {
1643 if (min_from > in->glyphs[j].from)
1644 min_from = in->glyphs[j].from;
1645 if (max_to < in->glyphs[j].to)
1646 max_to = in->glyphs[j].to;
1647 }
1648 g->from = min_from;
1649 g->to = max_to;
1650 }
1651 for (i++, otfg++; (i < otf_gstring.used
1652 && otfg->f.index.from == otfg[-1].f.index.from);
1653 i++, otfg++)
1654 {
1655 g = out->glyphs + out->used;
1656 *g = in->glyphs[from + otfg->f.index.to];
1657 if (g->code != otfg->glyph_id)
1658 {
1659 g->c = 0;
1660 g->code = otfg->glyph_id;
1661 g->measured = 0;
1662 }
1663 out->used++;
1664 }
1665 }
1666 }
1667 else
1668 {
1669 if (out->allocated < out->used + len)
1670 return -2;
1671 for (i = 0; i < len; i++)
1672 out->glyphs[out->used++] = in->glyphs[from + i];
1673 }
1674
1675 if (gpos_features)
1676 {
1677 MFLTGlyph *base = NULL, *mark = NULL, *g;
1678 int x_ppem, y_ppem, x_scale, y_scale;
1679
1680 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
1681 < 0)
1682 return to;
1683
1684 x_ppem = ft_face->size->metrics.x_ppem;
1685 y_ppem = ft_face->size->metrics.y_ppem;
1686 x_scale = ft_face->size->metrics.x_scale;
1687 y_scale = ft_face->size->metrics.y_scale;
1688
1689 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
1690 i < otf_gstring.used; i++, otfg++, g++)
1691 {
1692 MFLTGlyph *prev;
1693
1694 if (! otfg->glyph_id)
1695 continue;
1696 switch (otfg->positioning_type)
1697 {
1698 case 0:
1699 break;
1700 case 1: /* Single */
1701 case 2: /* Pair */
1702 {
1703 int format = otfg->f.f1.format;
1704
1705 if (format & OTF_XPlacement)
1706 adjustment[i].xoff
1707 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
1708 if (format & OTF_XPlaDevice)
1709 adjustment[i].xoff
1710 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
1711 if (format & OTF_YPlacement)
1712 adjustment[i].yoff
1713 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
1714 if (format & OTF_YPlaDevice)
1715 adjustment[i].yoff
1716 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
1717 if (format & OTF_XAdvance)
1718 adjustment[i].xadv
1719 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
1720 if (format & OTF_XAdvDevice)
1721 adjustment[i].xadv
1722 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
1723 if (format & OTF_YAdvance)
1724 adjustment[i].yadv
1725 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
1726 if (format & OTF_YAdvDevice)
1727 adjustment[i].yadv
1728 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
1729 adjustment[i].set = 1;
1730 }
1731 break;
1732 case 3: /* Cursive */
1733 /* Not yet supported. */
1734 break;
1735 case 4: /* Mark-to-Base */
1736 case 5: /* Mark-to-Ligature */
1737 if (! base)
1738 break;
1739 prev = base;
1740 goto label_adjust_anchor;
1741 default: /* i.e. case 6 Mark-to-Mark */
1742 if (! mark)
1743 break;
1744 prev = mark;
1745
1746 label_adjust_anchor:
1747 {
1748 int base_x, base_y, mark_x, mark_y;
1749 int this_from, this_to;
1750
1751 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
1752 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
1753 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
1754 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
1755
1756 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
1757 adjust_anchor (ft_face, otfg->f.f4.base_anchor,
1758 prev->code, x_ppem, y_ppem, &base_x, &base_y);
1759 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
1760 adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code,
1761 x_ppem, y_ppem, &mark_x, &mark_y);
1762 adjustment[i].xoff = (base_x - mark_x);
1763 adjustment[i].yoff = - (base_y - mark_y);
1764 adjustment[i].back = (g - prev);
1765 adjustment[i].xadv = 0;
1766 adjustment[i].advance_is_absolute = 1;
1767 adjustment[i].set = 1;
1768 this_from = g->from;
1769 this_to = g->to;
1770 for (j = 0; prev + j < g; j++)
1771 {
1772 if (this_from > prev[j].from)
1773 this_from = prev[j].from;
1774 if (this_to < prev[j].to)
1775 this_to = prev[j].to;
1776 }
1777 for (; prev <= g; prev++)
1778 {
1779 prev->from = this_from;
1780 prev->to = this_to;
1781 }
1782 }
1783 }
1784 if (otfg->GlyphClass == OTF_GlyphClass0)
1785 base = mark = g;
1786 else if (otfg->GlyphClass == OTF_GlyphClassMark)
1787 mark = g;
1788 else
1789 base = g;
1790 }
1791 }
1792 return to;
1793
1794 simple_copy:
1795 if (out->allocated < out->used + len)
1796 return -2;
1797 font->get_metrics (font, in, from, to);
1798 memcpy (out->glyphs + out->used, in->glyphs + from,
1799 sizeof (MFLTGlyph) * len);
1800 out->used += len;
1801 return to;
1802 }
1803
1804 static MFLTGlyphString gstring;
1805
1806 static int m17n_flt_initialized;
1807
1808 extern Lisp_Object QCfamily;
1809
1810 static Lisp_Object
1811 ftfont_shape_by_flt (lgstring, font, ft_face, otf)
1812 Lisp_Object lgstring;
1813 struct font *font;
1814 FT_Face ft_face;
1815 OTF *otf;
1816 {
1817 EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring);
1818 EMACS_UINT i;
1819 struct MFLTFontFT flt_font_ft;
1820 MFLT *flt = NULL;
1821
1822 if (! m17n_flt_initialized)
1823 {
1824 M17N_INIT ();
1825 m17n_flt_initialized = 1;
1826 }
1827
1828 for (i = 0; i < len; i++)
1829 if (NILP (LGSTRING_GLYPH (lgstring, i)))
1830 break;
1831 len = i;
1832
1833 if (gstring.allocated == 0)
1834 {
1835 gstring.allocated = len * 2;
1836 gstring.glyph_size = sizeof (MFLTGlyph);
1837 gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated);
1838 }
1839 else if (gstring.allocated < len * 2)
1840 {
1841 gstring.allocated = len * 2;
1842 gstring.glyphs = realloc (gstring.glyphs,
1843 sizeof (MFLTGlyph) * gstring.allocated);
1844 }
1845 for (i = 0; i < len; i++)
1846 gstring.glyphs[i].c = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i));
1847 gstring.used = len;
1848 gstring.r2l = 0;
1849
1850 {
1851 Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily);
1852
1853 if (NILP (family))
1854 flt_font_ft.flt_font.family = Mnil;
1855 else
1856 flt_font_ft.flt_font.family
1857 = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family))));
1858 }
1859 flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem;
1860 flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem;
1861 flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id;
1862 flt_font_ft.flt_font.get_metrics = ftfont_get_metrics;
1863 flt_font_ft.flt_font.check_otf = ftfont_check_otf;
1864 flt_font_ft.flt_font.drive_otf = ftfont_drive_otf;
1865 flt_font_ft.flt_font.internal = NULL;
1866 flt_font_ft.font = font;
1867 flt_font_ft.ft_face = ft_face;
1868 flt_font_ft.otf = otf;
1869 if (len > 1
1870 && gstring.glyphs[1].c >= 0x300 && gstring.glyphs[1].c <= 0x36F)
1871 /* A little bit ad hoc. Perhaps, shaper must get script and
1872 language information, and select a proper flt for them
1873 here. */
1874 flt = mflt_get (msymbol ("combining"));
1875 for (i = 0; i < 3; i++)
1876 {
1877 int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, flt);
1878 if (result != -2)
1879 break;
1880 gstring.allocated += gstring.allocated;
1881 gstring.glyphs = realloc (gstring.glyphs,
1882 sizeof (MFLTGlyph) * gstring.allocated);
1883 }
1884 if (gstring.used > LGSTRING_GLYPH_LEN (lgstring))
1885 return Qnil;
1886 for (i = 0; i < gstring.used; i++)
1887 {
1888 MFLTGlyph *g = gstring.glyphs + i;
1889
1890 g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from));
1891 g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to));
1892 }
1893
1894 for (i = 0; i < gstring.used; i++)
1895 {
1896 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
1897 MFLTGlyph *g = gstring.glyphs + i;
1898
1899 if (NILP (lglyph))
1900 {
1901 lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
1902 LGSTRING_SET_GLYPH (lgstring, i, lglyph);
1903 }
1904 LGLYPH_SET_FROM (lglyph, g->from);
1905 LGLYPH_SET_TO (lglyph, g->to);
1906 LGLYPH_SET_CHAR (lglyph, g->c);
1907 LGLYPH_SET_CODE (lglyph, g->code);
1908 LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6);
1909 LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6);
1910 LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6);
1911 LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6);
1912 LGLYPH_SET_DESCENT (lglyph, g->descent >> 6);
1913 if (g->adjusted)
1914 {
1915 Lisp_Object vec;
1916
1917 vec = Fmake_vector (make_number (3), Qnil);
1918 ASET (vec, 0, make_number (g->xoff >> 6));
1919 ASET (vec, 1, make_number (g->yoff >> 6));
1920 ASET (vec, 2, make_number (g->xadv >> 6));
1921 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
1922 }
1923 }
1924 return make_number (i);
1925 }
1926
1927 Lisp_Object
1928 ftfont_shape (lgstring)
1929 Lisp_Object lgstring;
1930 {
1931 struct font *font;
1932 struct ftfont_info *ftfont_info;
1933 OTF *otf;
1934
1935 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
1936 ftfont_info = (struct ftfont_info *) font;
1937 otf = ftfont_get_otf (ftfont_info);
1938 if (! otf)
1939 return make_number (0);
1940 return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf);
1941 }
1942
1943 #endif /* HAVE_M17N_FLT */
1944 #endif /* HAVE_LIBOTF */
1945
1946 Lisp_Object
1947 ftfont_font_format (FcPattern *pattern, Lisp_Object filename)
1948 {
1949 FcChar8 *str;
1950
1951 #ifdef FC_FONTFORMAT
1952 if (pattern)
1953 {
1954 if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch)
1955 return Qnil;
1956 if (strcmp ((char *) str, "TrueType") == 0)
1957 return intern ("truetype");
1958 if (strcmp ((char *) str, "Type 1") == 0)
1959 return intern ("type1");
1960 if (strcmp ((char *) str, "PCF") == 0)
1961 return intern ("pcf");
1962 if (strcmp ((char *) str, "BDF") == 0)
1963 return intern ("bdf");
1964 }
1965 #endif /* FC_FONTFORMAT */
1966 if (STRINGP (filename))
1967 {
1968 int len = SBYTES (filename);
1969
1970 if (len >= 4)
1971 {
1972 str = (FcChar8 *) (SDATA (filename) + len - 4);
1973 if (xstrcasecmp ((char *) str, ".ttf") == 0)
1974 return intern ("truetype");
1975 if (xstrcasecmp ((char *) str, ".pfb") == 0)
1976 return intern ("type1");
1977 if (xstrcasecmp ((char *) str, ".pcf") == 0)
1978 return intern ("pcf");
1979 if (xstrcasecmp ((char *) str, ".bdf") == 0)
1980 return intern ("bdf");
1981 }
1982 }
1983 return intern ("unknown");
1984 }
1985
1986 \f
1987 void
1988 syms_of_ftfont ()
1989 {
1990 DEFSYM (Qfreetype, "freetype");
1991 DEFSYM (Qmonospace, "monospace");
1992 DEFSYM (Qsans_serif, "sans-serif");
1993 DEFSYM (Qserif, "serif");
1994 DEFSYM (Qmono, "mono");
1995 DEFSYM (Qsans, "sans");
1996 DEFSYM (Qsans__serif, "sans serif");
1997
1998 staticpro (&freetype_font_cache);
1999 freetype_font_cache = Fcons (Qt, Qnil);
2000
2001 staticpro (&ftfont_generic_family_list);
2002 ftfont_generic_family_list
2003 = Fcons (Fcons (Qmonospace, Qt),
2004 Fcons (Fcons (Qsans_serif, Qt),
2005 Fcons (Fcons (Qsans, Qt), Qnil)));
2006
2007 staticpro (&ft_face_cache);
2008 ft_face_cache = Qnil;
2009
2010 ftfont_driver.type = Qfreetype;
2011 register_font_driver (&ftfont_driver, NULL);
2012 }
2013
2014 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
2015 (do not change this comment) */