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