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