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