(Ffont_xlfd_name): EXFUN it.
[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
27#include <ft2build.h>
28#include FT_FREETYPE_H
29#include FT_SIZES_H
30#include <fontconfig/fontconfig.h>
31#include <fontconfig/fcfreetype.h>
32
33#include "lisp.h"
34#include "dispextern.h"
35#include "frame.h"
36#include "blockinput.h"
37#include "character.h"
38#include "charset.h"
39#include "coding.h"
40#include "fontset.h"
41#include "font.h"
42
c2801c99 43/* Symbolic type of this font-driver. */
c2f5bfd6
KH
44Lisp_Object Qfreetype;
45
706b6995
KH
46/* Fontconfig's generic families and their aliases. */
47static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
48
c2801c99 49/* Flag to tell if FcInit is areadly called or not. */
c2f5bfd6 50static int fc_initialized;
c2801c99
KH
51
52/* Handle to a FreeType library instance. */
c2f5bfd6
KH
53static FT_Library ft_library;
54
c2801c99 55/* Cache for FreeType fonts. */
c2f5bfd6
KH
56static Lisp_Object freetype_font_cache;
57
c2801c99
KH
58/* Fontconfig's charset used for finding fonts of registry
59 "iso8859-1". */
c2f5bfd6
KH
60static FcCharSet *cs_iso8859_1;
61
62/* The actual structure for FreeType font that can be casted to struct
63 font. */
64
65struct ftfont_info
66{
67 struct font font;
68 FT_Size ft_size;
69};
70
706b6995
KH
71static int ftfont_build_basic_charsets P_ ((void));
72static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *,
73 Lisp_Object, Lisp_Object));
74static Lisp_Object ftfont_list_generic_family P_ ((Lisp_Object, Lisp_Object,
75 Lisp_Object));
0b966021 76Lisp_Object ftfont_font_format P_ ((FcPattern *));
706b6995
KH
77
78#define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
79
c2f5bfd6
KH
80static int
81ftfont_build_basic_charsets ()
82{
83 FcChar32 c;
84
85 cs_iso8859_1 = FcCharSetCreate ();
86 if (! cs_iso8859_1)
87 return -1;
88 for (c = ' '; c < 127; c++)
89 if (! FcCharSetAddChar (cs_iso8859_1, c))
90 return -1;
260a71fb
KH
91#if 0
92 /* This part is currently disabled. Should be fixed later. */
c2f5bfd6
KH
93 for (c = 192; c < 256; c++)
94 if (! FcCharSetAddChar (cs_iso8859_1, c))
95 return -1;
260a71fb 96#endif
c2f5bfd6
KH
97 return 0;
98}
99
706b6995
KH
100static Lisp_Object
101ftfont_pattern_entity (p, frame, registry)
c2801c99 102 FcPattern *p;
706b6995 103 Lisp_Object frame, registry;
c2801c99
KH
104{
105 Lisp_Object entity;
0b966021 106 FcChar8 *file, *fontformat;
c2801c99
KH
107 FcCharSet *charset;
108 char *str;
109 int numeric;
110 double dbl;
111
112 if (FcPatternGetString (p, FC_FILE, 0, &file) != FcResultMatch)
113 return Qnil;
f0365b6f 114 if (FcPatternGetCharSet (p, FC_CHARSET, 0, &charset) != FcResultMatch)
c2801c99 115 charset = NULL;
e907d979 116#ifdef FC_FONTFORMAT
0b966021 117 if (FcPatternGetString (p, FC_FONTFORMAT, 0, &fontformat) != FcResultMatch)
e907d979 118#endif /* FC_FONTFORMAT */
0b966021 119 fontformat = NULL;
c2801c99
KH
120
121 entity = Fmake_vector (make_number (FONT_ENTITY_MAX), null_string);
122
123 ASET (entity, FONT_TYPE_INDEX, Qfreetype);
124 ASET (entity, FONT_REGISTRY_INDEX, registry);
125 ASET (entity, FONT_FRAME_INDEX, frame);
126 ASET (entity, FONT_OBJLIST_INDEX, Qnil);
127
128 if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
129 ASET (entity, FONT_FOUNDRY_INDEX, intern_downcase (str, strlen (str)));
130 if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
131 ASET (entity, FONT_FAMILY_INDEX, intern_downcase (str, strlen (str)));
132 if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
f63d54dc
KH
133 {
134 if (numeric == FC_WEIGHT_REGULAR)
135 numeric = 100;
136 ASET (entity, FONT_WEIGHT_INDEX, make_number (numeric));
137 }
c2801c99
KH
138 if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
139 ASET (entity, FONT_SLANT_INDEX, make_number (numeric + 100));
140 if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
141 ASET (entity, FONT_WIDTH_INDEX, make_number (numeric));
142 if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
143 ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
144 else
145 ASET (entity, FONT_SIZE_INDEX, make_number (0));
146
147 if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) != FcResultMatch)
f0432f81 148 numeric = -1;
c2801c99
KH
149 file = FcStrCopy (file);
150 if (! file)
151 return Qnil;
152
153 p = FcPatternCreate ();
154 if (! p)
155 return Qnil;
156
157 if (FcPatternAddString (p, FC_FILE, file) == FcFalse
0b966021
KH
158 || (charset
159 && FcPatternAddCharSet (p, FC_CHARSET, charset) == FcFalse)
e907d979 160#ifdef FC_FONTFORMAT
0b966021
KH
161 || (fontformat
162 && FcPatternAddString (p, FC_FONTFORMAT, fontformat) == FcFalse)
e907d979 163#endif /* FC_FONTFORMAT */
f0432f81
KH
164 || (numeric >= 0
165 && FcPatternAddInteger (p, FC_SPACING, numeric) == FcFalse))
c2801c99
KH
166 {
167 FcPatternDestroy (p);
168 return Qnil;
169 }
170 ASET (entity, FONT_EXTRA_INDEX, make_save_value (p, 0));
171 return entity;
172}
173
706b6995
KH
174static Lisp_Object ftfont_generic_family_list;
175
176static Lisp_Object
177ftfont_list_generic_family (spec, frame, registry)
178 Lisp_Object spec, frame, registry;
179{
180 Lisp_Object family = AREF (spec, FONT_FAMILY_INDEX);
181 Lisp_Object slot, list, val;
182
183 if (EQ (family, Qmono))
184 family = Qmonospace;
185 else if (EQ (family, Qsans) || EQ (family, Qsans__serif))
186 family = Qsans_serif;
187 slot = assq_no_quit (family, ftfont_generic_family_list);
188 if (! CONSP (slot))
189 return null_vector;
190 list = XCDR (slot);
191 if (EQ (list, Qt))
192 {
193 /* Not yet listed. */
194 FcObjectSet *objset = NULL;
195 FcPattern *pattern = NULL, *pat = NULL;
196 FcFontSet *fontset = NULL;
197 FcChar8 *fam;
198 int i, j;
199
200 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
201 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING,
e907d979
KH
202 FC_CHARSET, FC_FILE,
203#ifdef FC_FONTFORMAT
204 FC_FONTFORMAT,
205#endif /* FC_FONTFORMAT */
206 NULL);
706b6995
KH
207 if (! objset)
208 goto err;
209 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString,
210 SYMBOL_FcChar8 (family), (char *) 0);
211 if (! pattern)
212 goto err;
213 pat = FcPatternCreate ();
214 if (! pat)
215 goto err;
216 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
217 for (i = 0, val = Qnil;
218 FcPatternGetString (pattern, FC_FAMILY, i, &fam) == FcResultMatch;
219 i++)
220 {
221 if (strcmp ((char *) fam, (char *) SYMBOL_FcChar8 (family)) == 0)
222 continue;
223 if (! FcPatternAddString (pat, FC_FAMILY, fam))
224 goto err;
225 fontset = FcFontList (NULL, pat, objset);
226 if (! fontset)
227 goto err;
228 /* Here we build the list in reverse order so that the last
229 loop in this function build a list in the correct
230 order. */
231 for (j = 0; j < fontset->nfont; j++)
232 {
233 Lisp_Object entity;
234
235 entity = ftfont_pattern_entity (fontset->fonts[j],
236 frame, registry);
237 if (! NILP (entity))
238 val = Fcons (entity, val);
239 }
240 FcFontSetDestroy (fontset);
241 fontset = NULL;
242 FcPatternDel (pat, FC_FAMILY);
243 }
244 list = val;
245 XSETCDR (slot, list);
246 err:
247 if (pat) FcPatternDestroy (pat);
248 if (pattern) FcPatternDestroy (pattern);
249 if (fontset) FcFontSetDestroy (fontset);
250 if (objset) FcObjectSetDestroy (objset);
251 if (EQ (list, Qt))
252 return Qnil;
253 }
254 ASET (spec, FONT_FAMILY_INDEX, Qnil);
255 for (val = Qnil; CONSP (list); list = XCDR (list))
256 if (font_match_p (spec, XCAR (list)))
257 val = Fcons (XCAR (list), val);
258 ASET (spec, FONT_FAMILY_INDEX, family);
259 return Fvconcat (1, &val);
260}
261
c2801c99 262
c2f5bfd6 263static Lisp_Object ftfont_get_cache P_ ((Lisp_Object));
c2f5bfd6 264static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
8daf5667 265static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
c2f5bfd6
KH
266static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
267static void ftfont_free_entity P_ ((Lisp_Object));
268static struct font *ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
269static void ftfont_close P_ ((FRAME_PTR, struct font *));
270static int ftfont_has_char P_ ((Lisp_Object, int));
271static unsigned ftfont_encode_char P_ ((struct font *, int));
272static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
273 struct font_metrics *));
274static int ftfont_get_bitmap P_ ((struct font *, unsigned,
275 struct font_bitmap *, int));
276static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
277 int *, int *));
278
279struct font_driver ftfont_driver =
280 {
281 (Lisp_Object) NULL, /* Qfreetype */
282 ftfont_get_cache,
c2f5bfd6 283 ftfont_list,
8daf5667 284 ftfont_match,
c2f5bfd6
KH
285 ftfont_list_family,
286 ftfont_free_entity,
287 ftfont_open,
288 ftfont_close,
289 /* We can't draw a text without device dependent functions. */
290 NULL,
291 NULL,
292 ftfont_has_char,
293 ftfont_encode_char,
294 ftfont_text_extents,
295 /* We can't draw a text without device dependent functions. */
296 NULL,
297 ftfont_get_bitmap,
298 NULL,
299 NULL,
300 NULL,
301 ftfont_anchor_point,
302#ifdef HAVE_LIBOTF
303 font_otf_capability,
304 font_otf_gsub,
305 font_otf_gpos
306#else
307 NULL,
308 NULL,
309 NULL
310#endif /* HAVE_LIBOTF */
311 };
312
c2f5bfd6
KH
313extern Lisp_Object QCname;
314
315static Lisp_Object
316ftfont_get_cache (frame)
317 Lisp_Object frame;
318{
c2f5bfd6
KH
319 return freetype_font_cache;
320}
321
c2f5bfd6
KH
322static Lisp_Object
323ftfont_list (frame, spec)
324 Lisp_Object frame, spec;
325{
8daf5667 326 Lisp_Object val, tmp, extra;
c2f5bfd6
KH
327 int i;
328 FcPattern *pattern = NULL;
329 FcCharSet *charset = NULL;
330 FcLangSet *langset = NULL;
331 FcFontSet *fontset = NULL;
332 FcObjectSet *objset = NULL;
8daf5667 333 Lisp_Object script;
c2801c99 334 Lisp_Object registry = Qunicode_bmp;
63565713 335 int weight = 0;
bc9a2afe
KH
336 double dpi = -1;
337 int spacing = -1;
338 int scalable = -1;
a85f724a 339 char otf_script[15]; /* For "otlayout\:XXXX" */
c2f5bfd6
KH
340
341 val = null_vector;
342
343 if (! fc_initialized)
344 {
345 FcInit ();
346 fc_initialized = 1;
347 }
348
63565713
KH
349 if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
350 && ! EQ (AREF (spec, FONT_ADSTYLE_INDEX), null_string))
c2f5bfd6 351 return val;
63565713
KH
352 if (! NILP (AREF (spec, FONT_SLANT_INDEX))
353 && XINT (AREF (spec, FONT_SLANT_INDEX)) < 100)
354 /* Fontconfig doesn't support reverse-italic/obligue. */
355 return val;
356
c2f5bfd6
KH
357 if (! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
358 {
359 registry = AREF (spec, FONT_REGISTRY_INDEX);
360 if (EQ (registry, Qiso8859_1))
361 {
362 if (! cs_iso8859_1
363 && ftfont_build_basic_charsets () < 0)
706b6995 364 return Qnil;
c2f5bfd6 365 charset = cs_iso8859_1;
c2f5bfd6 366 }
e1cae3d3
KH
367 else if (! EQ (registry, Qiso10646_1)
368 && ! EQ (registry, Qunicode_bmp)
369 && ! EQ (registry, Qunicode_sip))
706b6995 370 return val;
c2f5bfd6
KH
371 }
372
a85f724a 373 otf_script[0] = '\0';
8daf5667
KH
374 script = Qnil;
375 for (extra = AREF (spec, FONT_EXTRA_INDEX);
376 CONSP (extra); extra = XCDR (extra))
c2f5bfd6 377 {
8daf5667
KH
378 Lisp_Object key, val;
379
380 tmp = XCAR (extra);
381 key = XCAR (tmp), val = XCDR (tmp);
382 if (EQ (key, QCotf))
a85f724a 383 {
8daf5667 384 script = assq_no_quit (val, Votf_script_alist);
a85f724a
KH
385 if (CONSP (script) && SYMBOLP (XCDR (script)))
386 script = XCDR (script);
8daf5667 387 tmp = SYMBOL_NAME (val);
a85f724a
KH
388 sprintf (otf_script, "otlayout:%s", (char *) SDATA (tmp));
389 }
8daf5667 390 else if (EQ (key, QClanguage))
c2f5bfd6
KH
391 {
392 langset = FcLangSetCreate ();
393 if (! langset)
394 goto err;
8daf5667 395 if (SYMBOLP (val))
c2f5bfd6 396 {
8daf5667 397 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
c2f5bfd6
KH
398 goto err;
399 }
400 else
8daf5667
KH
401 for (; CONSP (val); val = XCDR (val))
402 if (SYMBOLP (XCAR (val))
403 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val))))
404 goto err;
c2f5bfd6 405 }
8daf5667
KH
406 else if (EQ (key, QCscript))
407 script = val;
408 else if (EQ (key, QCdpi))
409 dpi = XINT (val);
410 else if (EQ (key, QCspacing))
411 spacing = XINT (val);
412 else if (EQ (key, QCscalable))
413 scalable = ! NILP (val);
414 }
c2f5bfd6 415
8daf5667
KH
416 if (! NILP (script) && ! charset)
417 {
418 Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);
419
420 if (CONSP (chars))
421 {
422 charset = FcCharSetCreate ();
423 if (! charset)
424 goto err;
425 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
426 if (CHARACTERP (XCAR (chars))
427 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
428 goto err;
c2f5bfd6
KH
429 }
430 }
431
8daf5667 432 pattern = FcPatternCreate ();
706b6995
KH
433 if (! pattern)
434 goto err;
706b6995
KH
435 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
436 if (SYMBOLP (tmp) && ! NILP (tmp)
437 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
438 goto err;
439 tmp = AREF (spec, FONT_FAMILY_INDEX);
440 if (SYMBOLP (tmp) && ! NILP (tmp)
441 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
442 goto err;
63565713
KH
443 /* Emacs conventionally doesn't distinguish normal, regular, and
444 medium weight, but fontconfig does. So, we can't restrict font
445 listing by weight. We check it after getting a list. */
706b6995 446 tmp = AREF (spec, FONT_WEIGHT_INDEX);
63565713
KH
447 if (INTEGERP (tmp))
448 weight = XINT (tmp);
706b6995
KH
449 tmp = AREF (spec, FONT_SLANT_INDEX);
450 if (INTEGERP (tmp)
706b6995
KH
451 && ! FcPatternAddInteger (pattern, FC_SLANT, XINT (tmp) - 100))
452 goto err;
453 tmp = AREF (spec, FONT_WIDTH_INDEX);
454 if (INTEGERP (tmp)
455 && ! FcPatternAddInteger (pattern, FC_WIDTH, XINT (tmp)))
456 goto err;
c2f5bfd6
KH
457
458 if (charset
459 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
460 goto err;
461 if (langset
462 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
463 goto err;
bc9a2afe
KH
464 if (dpi >= 0
465 && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
466 goto err;
467 if (spacing >= 0
468 && ! FcPatternAddInteger (pattern, FC_SPACING, spacing))
469 goto err;
470 if (scalable >= 0
25a5d05b 471 && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
bc9a2afe 472 goto err;
c2f5bfd6 473
706b6995
KH
474 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
475 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING,
e907d979
KH
476 FC_CHARSET, FC_FILE,
477#ifdef FC_FONTFORMAT
478 FC_FONTFORMAT,
479#endif /* FC_FONTFORMAT */
480 NULL);
706b6995
KH
481 if (! objset)
482 goto err;
bc5f6c42
KH
483 if (otf_script[0])
484 {
485#ifndef FC_CAPABILITY
486 goto finish;
487#else /* not FC_CAPABILITY */
488 if (! FcObjectSetAdd (objset, FC_CAPABILITY))
489 goto err;
bc5f6c42 490#endif /* not FC_CAPABILITY */
5ed08958 491 }
f63d54dc 492
706b6995
KH
493 fontset = FcFontList (NULL, pattern, objset);
494 if (! fontset)
495 goto err;
c2f5bfd6 496
706b6995
KH
497 if (fontset->nfont > 0)
498 {
f63d54dc
KH
499 double pixel_size;
500
501 if (NILP (AREF (spec, FONT_SIZE_INDEX)))
502 pixel_size = 0;
503 else
504 pixel_size = XINT (AREF (spec, FONT_SIZE_INDEX));
505
706b6995 506 for (i = 0, val = Qnil; i < fontset->nfont; i++)
c2f5bfd6 507 {
f63d54dc
KH
508 Lisp_Object entity;
509
510 if (pixel_size > 0)
511 {
512 double this;
513
514 if (FcPatternGetDouble (fontset->fonts[i], FC_PIXEL_SIZE, 0,
515 &this) == FcResultMatch
63565713
KH
516 && ((this < pixel_size - FONT_PIXEL_SIZE_QUANTUM)
517 || (this > pixel_size + FONT_PIXEL_SIZE_QUANTUM)))
518 continue;
519 }
520 if (weight > 0)
521 {
522 int this;
523
524 if (FcPatternGetInteger (fontset->fonts[i], FC_WEIGHT, 0,
525 &this) != FcResultMatch
526 || (this != weight
527 && (weight != 100
528 || this < FC_WEIGHT_REGULAR
529 || this > FC_WEIGHT_MEDIUM)))
f63d54dc
KH
530 continue;
531 }
bc5f6c42 532#ifdef FC_CAPABILITY
a85f724a
KH
533 if (otf_script[0])
534 {
535 FcChar8 *this;
536
537 if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0,
538 &this) != FcResultMatch
539 || ! strstr ((char *) this, otf_script))
540 continue;
541 }
bc5f6c42 542#endif /* FC_CAPABILITY */
f63d54dc 543 entity = ftfont_pattern_entity (fontset->fonts[i], frame, registry);
c2801c99
KH
544 if (! NILP (entity))
545 val = Fcons (entity, val);
c2f5bfd6 546 }
c2801c99 547 val = Fvconcat (1, &val);
c2f5bfd6 548 }
706b6995
KH
549 else if (! NILP (AREF (spec, FONT_FAMILY_INDEX)))
550 val = ftfont_list_generic_family (spec, frame, registry);
c2f5bfd6
KH
551 goto finish;
552
553 err:
554 /* We come here because of unexpected error in fontconfig API call
706b6995 555 (usually insufficient memory). */
c2f5bfd6
KH
556 val = Qnil;
557
558 finish:
559 if (charset && charset != cs_iso8859_1) FcCharSetDestroy (charset);
560 if (objset) FcObjectSetDestroy (objset);
561 if (fontset) FcFontSetDestroy (fontset);
562 if (langset) FcLangSetDestroy (langset);
563 if (pattern) FcPatternDestroy (pattern);
564
565 return val;
566}
567
8daf5667
KH
568static Lisp_Object
569ftfont_match (frame, spec)
570 Lisp_Object frame, spec;
571{
572 Lisp_Object extra, val, entity;
573 FcPattern *pattern = NULL, *match = NULL;
574 FcResult result;
575
576 if (! fc_initialized)
577 {
578 FcInit ();
579 fc_initialized = 1;
580 }
581
582 extra = AREF (spec, FONT_EXTRA_INDEX);
583 val = assq_no_quit (QCname, extra);
584 if (! CONSP (val) || ! STRINGP (XCDR (val)))
585 return Qnil;
586
587 entity = Qnil;
588 pattern = FcNameParse (SDATA (XCDR (val)));
589 if (pattern)
590 {
591 if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
592 {
593 FcDefaultSubstitute (pattern);
594 match = FcFontMatch (NULL, pattern, &result);
8daf5667
KH
595 if (match)
596 {
597 entity = ftfont_pattern_entity (match, frame, Qunicode_bmp);
598 FcPatternDestroy (match);
599 }
600 }
601 FcPatternDestroy (pattern);
602 }
603
604 return entity;
605}
606
c2f5bfd6
KH
607static Lisp_Object
608ftfont_list_family (frame)
609 Lisp_Object frame;
610{
611 Lisp_Object list;
612 FcPattern *pattern = NULL;
613 FcFontSet *fontset = NULL;
614 FcObjectSet *objset = NULL;
615 int i;
616
617 if (! fc_initialized)
618 {
619 FcInit ();
620 fc_initialized = 1;
621 }
622
623 pattern = FcPatternCreate ();
624 if (! pattern)
625 goto finish;
706b6995 626 objset = FcObjectSetBuild (FC_FAMILY, NULL);
c2f5bfd6
KH
627 if (! objset)
628 goto finish;
629 fontset = FcFontList (NULL, pattern, objset);
630 if (! fontset)
631 goto finish;
632
633 list = Qnil;
634 for (i = 0; i < fontset->nfont; i++)
635 {
636 FcPattern *pat = fontset->fonts[i];
637 FcChar8 *str;
638
639 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
640 list = Fcons (intern_downcase ((char *) str, strlen ((char *) str)),
641 list);
642 }
643
644 finish:
645 if (objset) FcObjectSetDestroy (objset);
646 if (fontset) FcFontSetDestroy (fontset);
647 if (pattern) FcPatternDestroy (pattern);
648
649 return list;
650}
651
652
653static void
654ftfont_free_entity (entity)
655 Lisp_Object entity;
656{
657 Lisp_Object val = AREF (entity, FONT_EXTRA_INDEX);
658 FcPattern *pattern = XSAVE_VALUE (val)->pointer;
659
660 FcPatternDestroy (pattern);
661}
662
663static struct font *
664ftfont_open (f, entity, pixel_size)
665 FRAME_PTR f;
666 Lisp_Object entity;
667 int pixel_size;
668{
669 struct ftfont_info *ftfont_info;
670 struct font *font;
671 FT_Face ft_face;
672 FT_Size ft_size;
673 FT_UInt size;
674 Lisp_Object val;
675 FcPattern *pattern;
676 FcChar8 *file;
677 int spacing;
c9c0c429
KH
678 char *name;
679 int len;
c2f5bfd6
KH
680
681 val = AREF (entity, FONT_EXTRA_INDEX);
682 if (XTYPE (val) != Lisp_Misc
683 || XMISCTYPE (val) != Lisp_Misc_Save_Value)
684 return NULL;
685 pattern = XSAVE_VALUE (val)->pointer;
686 if (XSAVE_VALUE (val)->integer == 0)
687 {
688 /* We have not yet created FT_Face for this font. */
689 if (! ft_library
690 && FT_Init_FreeType (&ft_library) != 0)
691 return NULL;
692 if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
693 return NULL;
694 if (FT_New_Face (ft_library, (char *) file, 0, &ft_face) != 0)
695 return NULL;
696 FcPatternAddFTFace (pattern, FC_FT_FACE, ft_face);
697 ft_size = ft_face->size;
698 }
699 else
700 {
701 if (FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &ft_face)
702 != FcResultMatch)
703 return NULL;
704 if (FT_New_Size (ft_face, &ft_size) != 0)
705 return NULL;
706 if (FT_Activate_Size (ft_size) != 0)
707 {
708 FT_Done_Size (ft_size);
709 return NULL;
710 }
711 }
712
713 size = XINT (AREF (entity, FONT_SIZE_INDEX));
714 if (size == 0)
715 size = pixel_size;
716 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
717 {
718 if (XSAVE_VALUE (val)->integer == 0)
719 FT_Done_Face (ft_face);
720 return NULL;
721 }
722
723 ftfont_info = malloc (sizeof (struct ftfont_info));
724 if (! ftfont_info)
725 return NULL;
726 ftfont_info->ft_size = ft_size;
727
728 font = (struct font *) ftfont_info;
0b966021 729 font->format = ftfont_font_format (pattern);
c2f5bfd6
KH
730 font->entity = entity;
731 font->pixel_size = size;
732 font->driver = &ftfont_driver;
c9c0c429
KH
733 len = 96;
734 name = malloc (len);
735 while (name && font_unparse_fcname (entity, pixel_size, name, len) < 0)
736 {
737 char *new = realloc (name, len += 32);
738
739 if (! new)
740 free (name);
741 name = new;
742 }
743 font->font.full_name = font->font.name = name;
c2f5bfd6
KH
744 font->file_name = (char *) file;
745 font->font.size = ft_face->size->metrics.max_advance >> 6;
c9c0c429
KH
746 if (font->font.size <= 0)
747 font->font.size = size;
a85f724a 748 font->font.charset = font->encoding_charset = font->repertory_charset = -1;
c2f5bfd6
KH
749 font->ascent = ft_face->size->metrics.ascender >> 6;
750 font->descent = - ft_face->size->metrics.descender >> 6;
c9c0c429
KH
751 font->font.height = font->ascent + font->descent;
752 if (FcPatternGetInteger (pattern, FC_SPACING, 0, &spacing) != FcResultMatch)
753 spacing = FC_PROPORTIONAL;
754 if (spacing != FC_PROPORTIONAL)
c2f5bfd6
KH
755 font->font.average_width = font->font.space_width = font->font.size;
756 else
757 {
758 int i;
759
760 for (i = 32; i < 127; i++)
761 {
762 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
763 break;
764 if (i == 32)
765 font->font.space_width = ft_face->glyph->metrics.horiAdvance >> 6;
766 font->font.average_width += ft_face->glyph->metrics.horiAdvance >> 6;
767 }
768 if (i == 127)
769 {
770 /* The font contains all ASCII printable characters. */
771 font->font.average_width /= 95;
772 }
773 else
774 {
775 if (i == 32)
776 font->font.space_width = font->font.size;
777 font->font.average_width = font->font.size;
778 }
779 }
780
c9c0c429
KH
781 /* Unfortunately FreeType doesn't provide a way to get minimum char
782 width. So, we use space_width instead. */
783 font->min_width = font->font.space_width;
784
c2f5bfd6
KH
785 font->font.baseline_offset = 0;
786 font->font.relative_compose = 0;
787 font->font.default_ascent = 0;
788 font->font.vertical_centering = 0;
789
790 (XSAVE_VALUE (val)->integer)++;
791
792 return font;
793}
794
795static void
796ftfont_close (f, font)
797 FRAME_PTR f;
798 struct font *font;
799{
800 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
801 Lisp_Object entity = font->entity;
802 Lisp_Object val = AREF (entity, FONT_EXTRA_INDEX);
803
804 (XSAVE_VALUE (val)->integer)--;
805 if (XSAVE_VALUE (val)->integer == 0)
806 FT_Done_Face (ftfont_info->ft_size->face);
807 else
808 FT_Done_Size (ftfont_info->ft_size);
809
810 free (font);
811}
812
813static int
814ftfont_has_char (entity, c)
815 Lisp_Object entity;
816 int c;
817{
818 Lisp_Object val;
819 FcPattern *pattern;
820 FcCharSet *charset;
821
822 val = AREF (entity, FONT_EXTRA_INDEX);
823 pattern = XSAVE_VALUE (val)->pointer;
c2801c99
KH
824 if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
825 return -1;
c2f5bfd6
KH
826 return (FcCharSetHasChar (charset, (FcChar32) c) == FcTrue);
827}
828
829static unsigned
830ftfont_encode_char (font, c)
831 struct font *font;
832 int c;
833{
834 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
835 FT_Face ft_face = ftfont_info->ft_size->face;
836 FT_ULong charcode = c;
837 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
838
839 return (code > 0 ? code : 0xFFFFFFFF);
840}
841
842static int
843ftfont_text_extents (font, code, nglyphs, metrics)
844 struct font *font;
845 unsigned *code;
846 int nglyphs;
847 struct font_metrics *metrics;
848{
849 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
850 FT_Face ft_face = ftfont_info->ft_size->face;
851 int width = 0;
852 int i;
853
854 if (ftfont_info->ft_size != ft_face->size)
855 FT_Activate_Size (ftfont_info->ft_size);
856 if (metrics)
857 bzero (metrics, sizeof (struct font_metrics));
858 for (i = 0; i < nglyphs; i++)
859 {
860 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
861 {
862 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
863
864 if (metrics)
865 {
866 if (metrics->lbearing > width + (m->horiBearingX >> 6))
867 metrics->lbearing = width + (m->horiBearingX >> 6);
868 if (metrics->rbearing
869 < width + ((m->horiBearingX + m->width) >> 6))
870 metrics->rbearing
871 = width + ((m->horiBearingX + m->width) >> 6);
872 if (metrics->ascent < (m->horiBearingY >> 6))
873 metrics->ascent = m->horiBearingY >> 6;
874 if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
875 metrics->descent = (m->horiBearingY + m->height) >> 6;
876 }
877 width += m->horiAdvance >> 6;
878 }
879 else
880 {
881 width += font->font.space_width;
882 }
883 }
884 if (metrics)
885 metrics->width = width;
886
887 return width;
888}
889
890static int
891ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
892 struct font *font;
893 unsigned code;
894 struct font_bitmap *bitmap;
895 int bits_per_pixel;
896{
897 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
898 FT_Face ft_face = ftfont_info->ft_size->face;
899 FT_Int32 load_flags = FT_LOAD_RENDER;
900
901 if (ftfont_info->ft_size != ft_face->size)
902 FT_Activate_Size (ftfont_info->ft_size);
903 if (bits_per_pixel == 1)
904 {
905#ifdef FT_LOAD_TARGET_MONO
906 load_flags |= FT_LOAD_TARGET_MONO;
907#else
908 load_flags |= FT_LOAD_MONOCHROME;
909#endif
910 }
911 else if (bits_per_pixel != 8)
912 /* We don't support such a rendering. */
913 return -1;
914
915 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
916 return -1;
917 bitmap->rows = ft_face->glyph->bitmap.rows;
918 bitmap->width = ft_face->glyph->bitmap.width;
919 bitmap->pitch = ft_face->glyph->bitmap.pitch;
920 bitmap->buffer = ft_face->glyph->bitmap.buffer;
921 bitmap->left = ft_face->glyph->bitmap_left;
922 bitmap->top = ft_face->glyph->bitmap_top;
923 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
924 bitmap->extra = NULL;
925
926 return 0;
927}
928
929static int
930ftfont_anchor_point (font, code, index, x, y)
931 struct font *font;
932 unsigned code;
933 int index;
934 int *x, *y;
935{
936 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
937 FT_Face ft_face = ftfont_info->ft_size->face;
938
939 if (ftfont_info->ft_size != ft_face->size)
940 FT_Activate_Size (ftfont_info->ft_size);
941 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
942 return -1;
943 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
944 return -1;
945 if (index >= ft_face->glyph->outline.n_points)
946 return -1;
947 *x = ft_face->glyph->outline.points[index].x;
948 *y = ft_face->glyph->outline.points[index].y;
949 return 0;
950}
951
0b966021
KH
952Lisp_Object
953ftfont_font_format (FcPattern *pattern)
954{
e907d979 955 FcChar8 *str;
0b966021 956
e907d979
KH
957#ifdef FC_FONTFORMAT
958 if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch)
0b966021 959 return Qnil;
e907d979 960 if (strcmp ((char *) str, "TrueType") == 0)
0b966021 961 return intern ("truetype");
e907d979 962 if (strcmp ((char *) str, "Tyep 1") == 0)
0b966021 963 return intern ("type1");
e907d979 964 if (strcmp ((char *) str, "PCF") == 0)
0b966021 965 return intern ("pcf");
e907d979 966 if (strcmp ((char *) str, "BDF") == 0)
0b966021 967 return intern ("bdf");
e907d979
KH
968#else /* not FC_FONTFORMAT */
969 if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
970 return Qnil;
971 if (strcasestr ((char *) str, ".ttf") == 0)
972 return intern ("truetype");
973 if (strcasestr ((char *) str, "pfb") == 0)
974 return intern ("type1");
975 if (strcasestr ((char *) str, "pcf") == 0)
976 return intern ("pcf");
977 if (strcasestr ((char *) str, "bdf") == 0)
978 return intern ("bdf");
979#endif /* not FC_FONTFORMAT */
0b966021
KH
980 return intern ("unknown");
981}
982
c2f5bfd6
KH
983\f
984void
985syms_of_ftfont ()
986{
706b6995
KH
987 DEFSYM (Qfreetype, "freetype");
988 DEFSYM (Qmonospace, "monospace");
989 DEFSYM (Qsans_serif, "sans-serif");
990 DEFSYM (Qserif, "serif");
991 DEFSYM (Qmono, "mono");
992 DEFSYM (Qsans, "sans");
993 DEFSYM (Qsans__serif, "sans serif");
994
c2f5bfd6 995 staticpro (&freetype_font_cache);
c2801c99 996 freetype_font_cache = Fcons (Qt, Qnil);
c2f5bfd6 997
706b6995
KH
998 staticpro (&ftfont_generic_family_list);
999 ftfont_generic_family_list
1000 = Fcons (Fcons (Qmonospace, Qt),
1001 Fcons (Fcons (Qsans_serif, Qt),
1002 Fcons (Fcons (Qsans, Qt), Qnil)));
c2f5bfd6
KH
1003
1004 ftfont_driver.type = Qfreetype;
1005 register_font_driver (&ftfont_driver, NULL);
1006}
885b7d09
MB
1007
1008/* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
1009 (do not change this comment) */