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