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