(set-language-info): Fix synching
[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;
a85f724a 318 char otf_script[15]; /* For "otlayout\:XXXX" */
c2f5bfd6
KH
319
320 val = null_vector;
321
322 if (! fc_initialized)
323 {
324 FcInit ();
325 fc_initialized = 1;
326 }
327
63565713
KH
328 if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
329 && ! EQ (AREF (spec, FONT_ADSTYLE_INDEX), null_string))
c2f5bfd6 330 return val;
63565713
KH
331 if (! NILP (AREF (spec, FONT_SLANT_INDEX))
332 && XINT (AREF (spec, FONT_SLANT_INDEX)) < 100)
333 /* Fontconfig doesn't support reverse-italic/obligue. */
334 return val;
335
c2f5bfd6
KH
336 if (! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
337 {
338 registry = AREF (spec, FONT_REGISTRY_INDEX);
339 if (EQ (registry, Qiso8859_1))
340 {
341 if (! cs_iso8859_1
342 && ftfont_build_basic_charsets () < 0)
706b6995 343 return Qnil;
c2f5bfd6 344 charset = cs_iso8859_1;
c2f5bfd6 345 }
c2801c99 346 else if (! EQ (registry, Qiso10646_1) && ! EQ (registry, Qunicode_bmp))
706b6995 347 return val;
c2f5bfd6
KH
348 }
349
350 extra = AREF (spec, FONT_EXTRA_INDEX);
706b6995 351 font_name = Qnil;
a85f724a 352 otf_script[0] = '\0';
c2f5bfd6
KH
353 if (CONSP (extra))
354 {
a85f724a
KH
355 Lisp_Object script = Qnil;
356
bc9a2afe 357 tmp = assq_no_quit (QCname, extra);
5239ccf5
KH
358 if (CONSP (tmp) && STRINGP (XCDR (tmp))
359 && SDATA (XCDR (tmp))[0] == ':')
360 font_name = XCDR (tmp);
63565713 361 tmp = assq_no_quit (QCotf, extra);
a85f724a
KH
362 if (CONSP (tmp) && SYMBOLP (XCDR (tmp)))
363 {
364 tmp = XCDR (tmp);
365 script = assq_no_quit (tmp, Votf_script_alist);
366 if (CONSP (script) && SYMBOLP (XCDR (script)))
367 script = XCDR (script);
368 tmp = SYMBOL_NAME (tmp);
369 sprintf (otf_script, "otlayout:%s", (char *) SDATA (tmp));
370 }
63565713 371 tmp = assq_no_quit (QClanguage, extra);
c2f5bfd6
KH
372 if (CONSP (tmp))
373 {
374 langset = FcLangSetCreate ();
375 if (! langset)
376 goto err;
377 tmp = XCDR (tmp);
378 if (SYMBOLP (tmp))
379 {
380 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (tmp)))
381 goto err;
382 }
383 else
384 while (CONSP (tmp))
385 {
386 if (SYMBOLP (XCAR (tmp))
387 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (tmp))))
388 goto err;
389 tmp = XCDR (tmp);
390 }
391 }
63565713 392 tmp = assq_no_quit (QCscript, extra);
a85f724a
KH
393 if (CONSP (tmp))
394 script = XCDR (tmp);
395 if (! NILP (script) && ! charset)
c2f5bfd6 396 {
a85f724a
KH
397 Lisp_Object chars
398 = assq_no_quit (script, Vscript_representative_chars);
c2f5bfd6
KH
399
400 if (CONSP (chars))
401 {
402 charset = FcCharSetCreate ();
403 if (! charset)
404 goto err;
405 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
406 if (CHARACTERP (XCAR (chars))
407 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
408 goto err;
409 }
410 }
bc9a2afe
KH
411 tmp = assq_no_quit (QCdpi, extra);
412 if (CONSP (tmp))
413 dpi = XINT (XCDR (tmp));
414 tmp = assq_no_quit (QCspacing, extra);
415 if (CONSP (tmp))
416 spacing = XINT (XCDR (tmp));
417 tmp = assq_no_quit (QCscalable, extra);
418 if (CONSP (tmp))
419 spacing = ! NILP (XCDR (tmp));
c2f5bfd6
KH
420 }
421
c2f5bfd6 422 if (STRINGP (font_name))
63565713 423 pattern = FcNameParse (SDATA (font_name));
c2f5bfd6 424 else
706b6995
KH
425 pattern = FcPatternCreate ();
426 if (! pattern)
427 goto err;
c2f5bfd6 428
706b6995
KH
429 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
430 if (SYMBOLP (tmp) && ! NILP (tmp)
431 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
432 goto err;
433 tmp = AREF (spec, FONT_FAMILY_INDEX);
434 if (SYMBOLP (tmp) && ! NILP (tmp)
435 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
436 goto err;
63565713
KH
437 /* Emacs conventionally doesn't distinguish normal, regular, and
438 medium weight, but fontconfig does. So, we can't restrict font
439 listing by weight. We check it after getting a list. */
706b6995 440 tmp = AREF (spec, FONT_WEIGHT_INDEX);
63565713
KH
441 if (INTEGERP (tmp))
442 weight = XINT (tmp);
706b6995
KH
443 tmp = AREF (spec, FONT_SLANT_INDEX);
444 if (INTEGERP (tmp)
706b6995
KH
445 && ! FcPatternAddInteger (pattern, FC_SLANT, XINT (tmp) - 100))
446 goto err;
447 tmp = AREF (spec, FONT_WIDTH_INDEX);
448 if (INTEGERP (tmp)
449 && ! FcPatternAddInteger (pattern, FC_WIDTH, XINT (tmp)))
450 goto err;
c2f5bfd6
KH
451
452 if (charset
453 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
454 goto err;
455 if (langset
456 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
457 goto err;
bc9a2afe
KH
458 if (dpi >= 0
459 && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
460 goto err;
461 if (spacing >= 0
462 && ! FcPatternAddInteger (pattern, FC_SPACING, spacing))
463 goto err;
464 if (scalable >= 0
465 && ! FcPatternAddBool (pattern, FC_SPACING, spacing ? FcTrue : FcFalse))
466 goto err;
c2f5bfd6 467
706b6995
KH
468 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
469 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING,
470 FC_CHARSET, FC_FILE, NULL);
471 if (! objset)
472 goto err;
bc5f6c42
KH
473 if (otf_script[0])
474 {
475#ifndef FC_CAPABILITY
476 goto finish;
477#else /* not FC_CAPABILITY */
478 if (! FcObjectSetAdd (objset, FC_CAPABILITY))
479 goto err;
bc5f6c42 480#endif /* not FC_CAPABILITY */
5ed08958 481 }
f63d54dc 482
706b6995
KH
483 fontset = FcFontList (NULL, pattern, objset);
484 if (! fontset)
485 goto err;
c2f5bfd6 486
706b6995
KH
487 if (fontset->nfont > 0)
488 {
f63d54dc
KH
489 double pixel_size;
490
491 if (NILP (AREF (spec, FONT_SIZE_INDEX)))
492 pixel_size = 0;
493 else
494 pixel_size = XINT (AREF (spec, FONT_SIZE_INDEX));
495
706b6995 496 for (i = 0, val = Qnil; i < fontset->nfont; i++)
c2f5bfd6 497 {
f63d54dc
KH
498 Lisp_Object entity;
499
500 if (pixel_size > 0)
501 {
502 double this;
503
504 if (FcPatternGetDouble (fontset->fonts[i], FC_PIXEL_SIZE, 0,
505 &this) == FcResultMatch
63565713
KH
506 && ((this < pixel_size - FONT_PIXEL_SIZE_QUANTUM)
507 || (this > pixel_size + FONT_PIXEL_SIZE_QUANTUM)))
508 continue;
509 }
510 if (weight > 0)
511 {
512 int this;
513
514 if (FcPatternGetInteger (fontset->fonts[i], FC_WEIGHT, 0,
515 &this) != FcResultMatch
516 || (this != weight
517 && (weight != 100
518 || this < FC_WEIGHT_REGULAR
519 || this > FC_WEIGHT_MEDIUM)))
f63d54dc
KH
520 continue;
521 }
bc5f6c42 522#ifdef FC_CAPABILITY
a85f724a
KH
523 if (otf_script[0])
524 {
525 FcChar8 *this;
526
527 if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0,
528 &this) != FcResultMatch
529 || ! strstr ((char *) this, otf_script))
530 continue;
531 }
bc5f6c42 532#endif /* FC_CAPABILITY */
f63d54dc 533 entity = ftfont_pattern_entity (fontset->fonts[i], frame, registry);
c2801c99
KH
534 if (! NILP (entity))
535 val = Fcons (entity, val);
c2f5bfd6 536 }
c2801c99 537 val = Fvconcat (1, &val);
c2f5bfd6 538 }
706b6995
KH
539 else if (! NILP (AREF (spec, FONT_FAMILY_INDEX)))
540 val = ftfont_list_generic_family (spec, frame, registry);
c2f5bfd6
KH
541 goto finish;
542
543 err:
544 /* We come here because of unexpected error in fontconfig API call
706b6995 545 (usually insufficient memory). */
c2f5bfd6
KH
546 val = Qnil;
547
548 finish:
549 if (charset && charset != cs_iso8859_1) FcCharSetDestroy (charset);
550 if (objset) FcObjectSetDestroy (objset);
551 if (fontset) FcFontSetDestroy (fontset);
552 if (langset) FcLangSetDestroy (langset);
553 if (pattern) FcPatternDestroy (pattern);
554
555 return val;
556}
557
558static Lisp_Object
559ftfont_list_family (frame)
560 Lisp_Object frame;
561{
562 Lisp_Object list;
563 FcPattern *pattern = NULL;
564 FcFontSet *fontset = NULL;
565 FcObjectSet *objset = NULL;
566 int i;
567
568 if (! fc_initialized)
569 {
570 FcInit ();
571 fc_initialized = 1;
572 }
573
574 pattern = FcPatternCreate ();
575 if (! pattern)
576 goto finish;
706b6995 577 objset = FcObjectSetBuild (FC_FAMILY, NULL);
c2f5bfd6
KH
578 if (! objset)
579 goto finish;
580 fontset = FcFontList (NULL, pattern, objset);
581 if (! fontset)
582 goto finish;
583
584 list = Qnil;
585 for (i = 0; i < fontset->nfont; i++)
586 {
587 FcPattern *pat = fontset->fonts[i];
588 FcChar8 *str;
589
590 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
591 list = Fcons (intern_downcase ((char *) str, strlen ((char *) str)),
592 list);
593 }
594
595 finish:
596 if (objset) FcObjectSetDestroy (objset);
597 if (fontset) FcFontSetDestroy (fontset);
598 if (pattern) FcPatternDestroy (pattern);
599
600 return list;
601}
602
603
604static void
605ftfont_free_entity (entity)
606 Lisp_Object entity;
607{
608 Lisp_Object val = AREF (entity, FONT_EXTRA_INDEX);
609 FcPattern *pattern = XSAVE_VALUE (val)->pointer;
610
611 FcPatternDestroy (pattern);
612}
613
614static struct font *
615ftfont_open (f, entity, pixel_size)
616 FRAME_PTR f;
617 Lisp_Object entity;
618 int pixel_size;
619{
620 struct ftfont_info *ftfont_info;
621 struct font *font;
622 FT_Face ft_face;
623 FT_Size ft_size;
624 FT_UInt size;
625 Lisp_Object val;
626 FcPattern *pattern;
627 FcChar8 *file;
628 int spacing;
629
630 val = AREF (entity, FONT_EXTRA_INDEX);
631 if (XTYPE (val) != Lisp_Misc
632 || XMISCTYPE (val) != Lisp_Misc_Save_Value)
633 return NULL;
634 pattern = XSAVE_VALUE (val)->pointer;
635 if (XSAVE_VALUE (val)->integer == 0)
636 {
637 /* We have not yet created FT_Face for this font. */
638 if (! ft_library
639 && FT_Init_FreeType (&ft_library) != 0)
640 return NULL;
641 if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
642 return NULL;
643 if (FT_New_Face (ft_library, (char *) file, 0, &ft_face) != 0)
644 return NULL;
645 FcPatternAddFTFace (pattern, FC_FT_FACE, ft_face);
646 ft_size = ft_face->size;
647 }
648 else
649 {
650 if (FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &ft_face)
651 != FcResultMatch)
652 return NULL;
653 if (FT_New_Size (ft_face, &ft_size) != 0)
654 return NULL;
655 if (FT_Activate_Size (ft_size) != 0)
656 {
657 FT_Done_Size (ft_size);
658 return NULL;
659 }
660 }
661
662 size = XINT (AREF (entity, FONT_SIZE_INDEX));
663 if (size == 0)
664 size = pixel_size;
665 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
666 {
667 if (XSAVE_VALUE (val)->integer == 0)
668 FT_Done_Face (ft_face);
669 return NULL;
670 }
671
672 ftfont_info = malloc (sizeof (struct ftfont_info));
673 if (! ftfont_info)
674 return NULL;
675 ftfont_info->ft_size = ft_size;
676
677 font = (struct font *) ftfont_info;
678 font->entity = entity;
679 font->pixel_size = size;
680 font->driver = &ftfont_driver;
681 font->font.name = font->font.full_name = NULL;
682 font->file_name = (char *) file;
683 font->font.size = ft_face->size->metrics.max_advance >> 6;
a85f724a 684 font->font.charset = font->encoding_charset = font->repertory_charset = -1;
c2f5bfd6
KH
685 font->ascent = ft_face->size->metrics.ascender >> 6;
686 font->descent = - ft_face->size->metrics.descender >> 6;
687 font->font.height = ft_face->size->metrics.height >> 6;
688 if (FcPatternGetInteger (pattern, FC_SPACING, 0, &spacing) != FcResultMatch
689 || spacing != FC_PROPORTIONAL)
690 font->font.average_width = font->font.space_width = font->font.size;
691 else
692 {
693 int i;
694
695 for (i = 32; i < 127; i++)
696 {
697 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
698 break;
699 if (i == 32)
700 font->font.space_width = ft_face->glyph->metrics.horiAdvance >> 6;
701 font->font.average_width += ft_face->glyph->metrics.horiAdvance >> 6;
702 }
703 if (i == 127)
704 {
705 /* The font contains all ASCII printable characters. */
706 font->font.average_width /= 95;
707 }
708 else
709 {
710 if (i == 32)
711 font->font.space_width = font->font.size;
712 font->font.average_width = font->font.size;
713 }
714 }
715
716 font->font.baseline_offset = 0;
717 font->font.relative_compose = 0;
718 font->font.default_ascent = 0;
719 font->font.vertical_centering = 0;
720
721 (XSAVE_VALUE (val)->integer)++;
722
723 return font;
724}
725
726static void
727ftfont_close (f, font)
728 FRAME_PTR f;
729 struct font *font;
730{
731 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
732 Lisp_Object entity = font->entity;
733 Lisp_Object val = AREF (entity, FONT_EXTRA_INDEX);
734
735 (XSAVE_VALUE (val)->integer)--;
736 if (XSAVE_VALUE (val)->integer == 0)
737 FT_Done_Face (ftfont_info->ft_size->face);
738 else
739 FT_Done_Size (ftfont_info->ft_size);
740
741 free (font);
742}
743
744static int
745ftfont_has_char (entity, c)
746 Lisp_Object entity;
747 int c;
748{
749 Lisp_Object val;
750 FcPattern *pattern;
751 FcCharSet *charset;
752
753 val = AREF (entity, FONT_EXTRA_INDEX);
754 pattern = XSAVE_VALUE (val)->pointer;
c2801c99
KH
755 if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
756 return -1;
c2f5bfd6
KH
757 return (FcCharSetHasChar (charset, (FcChar32) c) == FcTrue);
758}
759
760static unsigned
761ftfont_encode_char (font, c)
762 struct font *font;
763 int c;
764{
765 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
766 FT_Face ft_face = ftfont_info->ft_size->face;
767 FT_ULong charcode = c;
768 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
769
770 return (code > 0 ? code : 0xFFFFFFFF);
771}
772
773static int
774ftfont_text_extents (font, code, nglyphs, metrics)
775 struct font *font;
776 unsigned *code;
777 int nglyphs;
778 struct font_metrics *metrics;
779{
780 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
781 FT_Face ft_face = ftfont_info->ft_size->face;
782 int width = 0;
783 int i;
784
785 if (ftfont_info->ft_size != ft_face->size)
786 FT_Activate_Size (ftfont_info->ft_size);
787 if (metrics)
788 bzero (metrics, sizeof (struct font_metrics));
789 for (i = 0; i < nglyphs; i++)
790 {
791 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
792 {
793 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
794
795 if (metrics)
796 {
797 if (metrics->lbearing > width + (m->horiBearingX >> 6))
798 metrics->lbearing = width + (m->horiBearingX >> 6);
799 if (metrics->rbearing
800 < width + ((m->horiBearingX + m->width) >> 6))
801 metrics->rbearing
802 = width + ((m->horiBearingX + m->width) >> 6);
803 if (metrics->ascent < (m->horiBearingY >> 6))
804 metrics->ascent = m->horiBearingY >> 6;
805 if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
806 metrics->descent = (m->horiBearingY + m->height) >> 6;
807 }
808 width += m->horiAdvance >> 6;
809 }
810 else
811 {
812 width += font->font.space_width;
813 }
814 }
815 if (metrics)
816 metrics->width = width;
817
818 return width;
819}
820
821static int
822ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
823 struct font *font;
824 unsigned code;
825 struct font_bitmap *bitmap;
826 int bits_per_pixel;
827{
828 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
829 FT_Face ft_face = ftfont_info->ft_size->face;
830 FT_Int32 load_flags = FT_LOAD_RENDER;
831
832 if (ftfont_info->ft_size != ft_face->size)
833 FT_Activate_Size (ftfont_info->ft_size);
834 if (bits_per_pixel == 1)
835 {
836#ifdef FT_LOAD_TARGET_MONO
837 load_flags |= FT_LOAD_TARGET_MONO;
838#else
839 load_flags |= FT_LOAD_MONOCHROME;
840#endif
841 }
842 else if (bits_per_pixel != 8)
843 /* We don't support such a rendering. */
844 return -1;
845
846 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
847 return -1;
848 bitmap->rows = ft_face->glyph->bitmap.rows;
849 bitmap->width = ft_face->glyph->bitmap.width;
850 bitmap->pitch = ft_face->glyph->bitmap.pitch;
851 bitmap->buffer = ft_face->glyph->bitmap.buffer;
852 bitmap->left = ft_face->glyph->bitmap_left;
853 bitmap->top = ft_face->glyph->bitmap_top;
854 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
855 bitmap->extra = NULL;
856
857 return 0;
858}
859
860static int
861ftfont_anchor_point (font, code, index, x, y)
862 struct font *font;
863 unsigned code;
864 int index;
865 int *x, *y;
866{
867 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
868 FT_Face ft_face = ftfont_info->ft_size->face;
869
870 if (ftfont_info->ft_size != ft_face->size)
871 FT_Activate_Size (ftfont_info->ft_size);
872 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
873 return -1;
874 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
875 return -1;
876 if (index >= ft_face->glyph->outline.n_points)
877 return -1;
878 *x = ft_face->glyph->outline.points[index].x;
879 *y = ft_face->glyph->outline.points[index].y;
880 return 0;
881}
882
883\f
884void
885syms_of_ftfont ()
886{
706b6995
KH
887 DEFSYM (Qfreetype, "freetype");
888 DEFSYM (Qmonospace, "monospace");
889 DEFSYM (Qsans_serif, "sans-serif");
890 DEFSYM (Qserif, "serif");
891 DEFSYM (Qmono, "mono");
892 DEFSYM (Qsans, "sans");
893 DEFSYM (Qsans__serif, "sans serif");
894
c2f5bfd6 895 staticpro (&freetype_font_cache);
c2801c99 896 freetype_font_cache = Fcons (Qt, Qnil);
c2f5bfd6 897
706b6995
KH
898 staticpro (&ftfont_generic_family_list);
899 ftfont_generic_family_list
900 = Fcons (Fcons (Qmonospace, Qt),
901 Fcons (Fcons (Qsans_serif, Qt),
902 Fcons (Fcons (Qsans, Qt), Qnil)));
c2f5bfd6
KH
903
904 ftfont_driver.type = Qfreetype;
905 register_font_driver (&ftfont_driver, NULL);
906}
885b7d09
MB
907
908/* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
909 (do not change this comment) */