(ftfont_pattern_entity): Use the numeric value 100 for
[bpt/emacs.git] / src / ftfont.c
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
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, 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
43 /* Symbolic type of this font-driver. */
44 Lisp_Object Qfreetype;
45
46 /* Fontconfig's generic families and their aliases. */
47 static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
48
49 /* Flag to tell if FcInit is areadly called or not. */
50 static int fc_initialized;
51
52 /* Handle to a FreeType library instance. */
53 static FT_Library ft_library;
54
55 /* Cache for FreeType fonts. */
56 static Lisp_Object freetype_font_cache;
57
58 /* Fontconfig's charset used for finding fonts of registry
59 "iso8859-1". */
60 static FcCharSet *cs_iso8859_1;
61
62 /* The actual structure for FreeType font that can be casted to struct
63 font. */
64
65 struct ftfont_info
66 {
67 struct font font;
68 FT_Size ft_size;
69 };
70
71 static int ftfont_build_basic_charsets P_ ((void));
72 static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *,
73 Lisp_Object, Lisp_Object));
74 static 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
79 static int
80 ftfont_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
96 static Lisp_Object
97 ftfont_pattern_entity (p, frame, registry)
98 FcPattern *p;
99 Lisp_Object frame, registry;
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;
110 if (FcPatternGetCharSet (p, FC_CHARSET, 0, &charset) != FcResultMatch)
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 {
126 if (numeric == FC_WEIGHT_REGULAR)
127 numeric = 100;
128 ASET (entity, FONT_WEIGHT_INDEX, make_number (numeric));
129 }
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)
151 || FcPatternAddInteger (p, FC_SPACING, numeric) == FcFalse)
152 {
153 FcPatternDestroy (p);
154 return Qnil;
155 }
156 ASET (entity, FONT_EXTRA_INDEX, make_save_value (p, 0));
157 return entity;
158 }
159
160 static Lisp_Object ftfont_generic_family_list;
161
162 static Lisp_Object
163 ftfont_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
244
245 static Lisp_Object ftfont_get_cache P_ ((Lisp_Object));
246 static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
247 static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
248 static void ftfont_free_entity P_ ((Lisp_Object));
249 static struct font *ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
250 static void ftfont_close P_ ((FRAME_PTR, struct font *));
251 static int ftfont_has_char P_ ((Lisp_Object, int));
252 static unsigned ftfont_encode_char P_ ((struct font *, int));
253 static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
254 struct font_metrics *));
255 static int ftfont_get_bitmap P_ ((struct font *, unsigned,
256 struct font_bitmap *, int));
257 static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
258 int *, int *));
259
260 struct font_driver ftfont_driver =
261 {
262 (Lisp_Object) NULL, /* Qfreetype */
263 ftfont_get_cache,
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
293 extern Lisp_Object QCname;
294
295 static Lisp_Object
296 ftfont_get_cache (frame)
297 Lisp_Object frame;
298 {
299 return freetype_font_cache;
300 }
301
302 static Lisp_Object
303 ftfont_list (frame, spec)
304 Lisp_Object frame, spec;
305 {
306 Lisp_Object val, tmp, extra, font_name;
307 int i;
308 FcPattern *pattern = NULL;
309 FcCharSet *charset = NULL;
310 FcLangSet *langset = NULL;
311 FcFontSet *fontset = NULL;
312 FcObjectSet *objset = NULL;
313 Lisp_Object registry = Qunicode_bmp;
314
315 val = null_vector;
316
317 if (! fc_initialized)
318 {
319 FcInit ();
320 fc_initialized = 1;
321 }
322
323 if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX)))
324 return val;
325 if (! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
326 {
327 registry = AREF (spec, FONT_REGISTRY_INDEX);
328 if (EQ (registry, Qiso8859_1))
329 {
330 if (! cs_iso8859_1
331 && ftfont_build_basic_charsets () < 0)
332 return Qnil;
333 charset = cs_iso8859_1;
334 }
335 else if (! EQ (registry, Qiso10646_1) && ! EQ (registry, Qunicode_bmp))
336 return val;
337 }
338
339 extra = AREF (spec, FONT_EXTRA_INDEX);
340 font_name = Qnil;
341 if (CONSP (extra))
342 {
343 tmp = Fassq (QCotf, extra);
344 if (! NILP (tmp))
345 return val;
346 tmp = Fassq (QClanguage, extra);
347 if (CONSP (tmp))
348 {
349 langset = FcLangSetCreate ();
350 if (! langset)
351 goto err;
352 tmp = XCDR (tmp);
353 if (SYMBOLP (tmp))
354 {
355 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (tmp)))
356 goto err;
357 }
358 else
359 while (CONSP (tmp))
360 {
361 if (SYMBOLP (XCAR (tmp))
362 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (tmp))))
363 goto err;
364 tmp = XCDR (tmp);
365 }
366 }
367 tmp = Fassq (QCname, extra);
368 if (CONSP (tmp))
369 font_name = XCDR (tmp);
370 tmp = Fassq (QCscript, extra);
371 if (CONSP (tmp) && ! charset)
372 {
373 Lisp_Object script = XCDR (tmp);
374 Lisp_Object chars = assq_no_quit (script,
375 Vscript_representative_chars);
376
377 if (CONSP (chars))
378 {
379 charset = FcCharSetCreate ();
380 if (! charset)
381 goto err;
382 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
383 if (CHARACTERP (XCAR (chars))
384 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
385 goto err;
386 }
387 }
388 }
389
390 if (STRINGP (font_name))
391 {
392 pattern = FcNameParse (SDATA (font_name));
393 /* Ignore these values in listing. */
394 FcPatternDel (pattern, FC_PIXEL_SIZE);
395 FcPatternDel (pattern, FC_SIZE);
396 FcPatternDel (pattern, FC_FAMILY);
397 }
398 else
399 pattern = FcPatternCreate ();
400 if (! pattern)
401 goto err;
402
403 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
404 if (SYMBOLP (tmp) && ! NILP (tmp)
405 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
406 goto err;
407 tmp = AREF (spec, FONT_FAMILY_INDEX);
408 if (SYMBOLP (tmp) && ! NILP (tmp)
409 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
410 goto err;
411 tmp = AREF (spec, FONT_WEIGHT_INDEX);
412 if (INTEGERP (tmp)
413 && ! FcPatternAddInteger (pattern, FC_WEIGHT, XINT (tmp)))
414 goto err;
415 tmp = AREF (spec, FONT_SLANT_INDEX);
416 if (INTEGERP (tmp)
417 && XINT (tmp) >= 100
418 && ! FcPatternAddInteger (pattern, FC_SLANT, XINT (tmp) - 100))
419 goto err;
420 tmp = AREF (spec, FONT_WIDTH_INDEX);
421 if (INTEGERP (tmp)
422 && ! FcPatternAddInteger (pattern, FC_WIDTH, XINT (tmp)))
423 goto err;
424 #if 0
425 if (! FcPatternAddBool (pattern, FC_SCALABLE, FcTrue))
426 goto err;
427 #endif
428
429 if (charset
430 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
431 goto err;
432 if (langset
433 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
434 goto err;
435
436 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
437 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING,
438 FC_CHARSET, FC_FILE, NULL);
439 if (! objset)
440 goto err;
441
442 fontset = FcFontList (NULL, pattern, objset);
443 if (! fontset)
444 goto err;
445
446 if (fontset->nfont > 0)
447 {
448 double pixel_size;
449
450 if (NILP (AREF (spec, FONT_SIZE_INDEX)))
451 pixel_size = 0;
452 else
453 pixel_size = XINT (AREF (spec, FONT_SIZE_INDEX));
454
455 for (i = 0, val = Qnil; i < fontset->nfont; i++)
456 {
457 Lisp_Object entity;
458
459 if (pixel_size > 0)
460 {
461 double this;
462
463 if (FcPatternGetDouble (fontset->fonts[i], FC_PIXEL_SIZE, 0,
464 &this) == FcResultMatch
465 && this != pixel_size)
466 continue;
467 }
468 entity = ftfont_pattern_entity (fontset->fonts[i], frame, registry);
469 if (! NILP (entity))
470 val = Fcons (entity, val);
471 }
472 val = Fvconcat (1, &val);
473 }
474 else if (! NILP (AREF (spec, FONT_FAMILY_INDEX)))
475 val = ftfont_list_generic_family (spec, frame, registry);
476 goto finish;
477
478 err:
479 /* We come here because of unexpected error in fontconfig API call
480 (usually insufficient memory). */
481 val = Qnil;
482
483 finish:
484 if (charset && charset != cs_iso8859_1) FcCharSetDestroy (charset);
485 if (objset) FcObjectSetDestroy (objset);
486 if (fontset) FcFontSetDestroy (fontset);
487 if (langset) FcLangSetDestroy (langset);
488 if (pattern) FcPatternDestroy (pattern);
489
490 return val;
491 }
492
493 static Lisp_Object
494 ftfont_list_family (frame)
495 Lisp_Object frame;
496 {
497 Lisp_Object list;
498 FcPattern *pattern = NULL;
499 FcFontSet *fontset = NULL;
500 FcObjectSet *objset = NULL;
501 int i;
502
503 if (! fc_initialized)
504 {
505 FcInit ();
506 fc_initialized = 1;
507 }
508
509 pattern = FcPatternCreate ();
510 if (! pattern)
511 goto finish;
512 objset = FcObjectSetBuild (FC_FAMILY, NULL);
513 if (! objset)
514 goto finish;
515 fontset = FcFontList (NULL, pattern, objset);
516 if (! fontset)
517 goto finish;
518
519 list = Qnil;
520 for (i = 0; i < fontset->nfont; i++)
521 {
522 FcPattern *pat = fontset->fonts[i];
523 FcChar8 *str;
524
525 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
526 list = Fcons (intern_downcase ((char *) str, strlen ((char *) str)),
527 list);
528 }
529
530 finish:
531 if (objset) FcObjectSetDestroy (objset);
532 if (fontset) FcFontSetDestroy (fontset);
533 if (pattern) FcPatternDestroy (pattern);
534
535 return list;
536 }
537
538
539 static void
540 ftfont_free_entity (entity)
541 Lisp_Object entity;
542 {
543 Lisp_Object val = AREF (entity, FONT_EXTRA_INDEX);
544 FcPattern *pattern = XSAVE_VALUE (val)->pointer;
545
546 FcPatternDestroy (pattern);
547 }
548
549 static struct font *
550 ftfont_open (f, entity, pixel_size)
551 FRAME_PTR f;
552 Lisp_Object entity;
553 int pixel_size;
554 {
555 struct ftfont_info *ftfont_info;
556 struct font *font;
557 FT_Face ft_face;
558 FT_Size ft_size;
559 FT_UInt size;
560 Lisp_Object val;
561 FcPattern *pattern;
562 FcChar8 *file;
563 int spacing;
564
565 val = AREF (entity, FONT_EXTRA_INDEX);
566 if (XTYPE (val) != Lisp_Misc
567 || XMISCTYPE (val) != Lisp_Misc_Save_Value)
568 return NULL;
569 pattern = XSAVE_VALUE (val)->pointer;
570 if (XSAVE_VALUE (val)->integer == 0)
571 {
572 /* We have not yet created FT_Face for this font. */
573 if (! ft_library
574 && FT_Init_FreeType (&ft_library) != 0)
575 return NULL;
576 if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
577 return NULL;
578 if (FT_New_Face (ft_library, (char *) file, 0, &ft_face) != 0)
579 return NULL;
580 FcPatternAddFTFace (pattern, FC_FT_FACE, ft_face);
581 ft_size = ft_face->size;
582 }
583 else
584 {
585 if (FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &ft_face)
586 != FcResultMatch)
587 return NULL;
588 if (FT_New_Size (ft_face, &ft_size) != 0)
589 return NULL;
590 if (FT_Activate_Size (ft_size) != 0)
591 {
592 FT_Done_Size (ft_size);
593 return NULL;
594 }
595 }
596
597 size = XINT (AREF (entity, FONT_SIZE_INDEX));
598 if (size == 0)
599 size = pixel_size;
600 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
601 {
602 if (XSAVE_VALUE (val)->integer == 0)
603 FT_Done_Face (ft_face);
604 return NULL;
605 }
606
607 ftfont_info = malloc (sizeof (struct ftfont_info));
608 if (! ftfont_info)
609 return NULL;
610 ftfont_info->ft_size = ft_size;
611
612 font = (struct font *) ftfont_info;
613 font->entity = entity;
614 font->pixel_size = size;
615 font->driver = &ftfont_driver;
616 font->font.name = font->font.full_name = NULL;
617 font->file_name = (char *) file;
618 font->font.size = ft_face->size->metrics.max_advance >> 6;
619 font->ascent = ft_face->size->metrics.ascender >> 6;
620 font->descent = - ft_face->size->metrics.descender >> 6;
621 font->font.height = ft_face->size->metrics.height >> 6;
622 if (FcPatternGetInteger (pattern, FC_SPACING, 0, &spacing) != FcResultMatch
623 || spacing != FC_PROPORTIONAL)
624 font->font.average_width = font->font.space_width = font->font.size;
625 else
626 {
627 int i;
628
629 for (i = 32; i < 127; i++)
630 {
631 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
632 break;
633 if (i == 32)
634 font->font.space_width = ft_face->glyph->metrics.horiAdvance >> 6;
635 font->font.average_width += ft_face->glyph->metrics.horiAdvance >> 6;
636 }
637 if (i == 127)
638 {
639 /* The font contains all ASCII printable characters. */
640 font->font.average_width /= 95;
641 }
642 else
643 {
644 if (i == 32)
645 font->font.space_width = font->font.size;
646 font->font.average_width = font->font.size;
647 }
648 }
649
650 font->font.baseline_offset = 0;
651 font->font.relative_compose = 0;
652 font->font.default_ascent = 0;
653 font->font.vertical_centering = 0;
654
655 (XSAVE_VALUE (val)->integer)++;
656
657 return font;
658 }
659
660 static void
661 ftfont_close (f, font)
662 FRAME_PTR f;
663 struct font *font;
664 {
665 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
666 Lisp_Object entity = font->entity;
667 Lisp_Object val = AREF (entity, FONT_EXTRA_INDEX);
668
669 (XSAVE_VALUE (val)->integer)--;
670 if (XSAVE_VALUE (val)->integer == 0)
671 FT_Done_Face (ftfont_info->ft_size->face);
672 else
673 FT_Done_Size (ftfont_info->ft_size);
674
675 free (font);
676 }
677
678 static int
679 ftfont_has_char (entity, c)
680 Lisp_Object entity;
681 int c;
682 {
683 Lisp_Object val;
684 FcPattern *pattern;
685 FcCharSet *charset;
686
687 val = AREF (entity, FONT_EXTRA_INDEX);
688 pattern = XSAVE_VALUE (val)->pointer;
689 if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
690 return -1;
691 return (FcCharSetHasChar (charset, (FcChar32) c) == FcTrue);
692 }
693
694 static unsigned
695 ftfont_encode_char (font, c)
696 struct font *font;
697 int c;
698 {
699 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
700 FT_Face ft_face = ftfont_info->ft_size->face;
701 FT_ULong charcode = c;
702 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
703
704 return (code > 0 ? code : 0xFFFFFFFF);
705 }
706
707 static int
708 ftfont_text_extents (font, code, nglyphs, metrics)
709 struct font *font;
710 unsigned *code;
711 int nglyphs;
712 struct font_metrics *metrics;
713 {
714 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
715 FT_Face ft_face = ftfont_info->ft_size->face;
716 int width = 0;
717 int i;
718
719 if (ftfont_info->ft_size != ft_face->size)
720 FT_Activate_Size (ftfont_info->ft_size);
721 if (metrics)
722 bzero (metrics, sizeof (struct font_metrics));
723 for (i = 0; i < nglyphs; i++)
724 {
725 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
726 {
727 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
728
729 if (metrics)
730 {
731 if (metrics->lbearing > width + (m->horiBearingX >> 6))
732 metrics->lbearing = width + (m->horiBearingX >> 6);
733 if (metrics->rbearing
734 < width + ((m->horiBearingX + m->width) >> 6))
735 metrics->rbearing
736 = width + ((m->horiBearingX + m->width) >> 6);
737 if (metrics->ascent < (m->horiBearingY >> 6))
738 metrics->ascent = m->horiBearingY >> 6;
739 if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
740 metrics->descent = (m->horiBearingY + m->height) >> 6;
741 }
742 width += m->horiAdvance >> 6;
743 }
744 else
745 {
746 width += font->font.space_width;
747 }
748 }
749 if (metrics)
750 metrics->width = width;
751
752 return width;
753 }
754
755 static int
756 ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
757 struct font *font;
758 unsigned code;
759 struct font_bitmap *bitmap;
760 int bits_per_pixel;
761 {
762 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
763 FT_Face ft_face = ftfont_info->ft_size->face;
764 FT_Int32 load_flags = FT_LOAD_RENDER;
765
766 if (ftfont_info->ft_size != ft_face->size)
767 FT_Activate_Size (ftfont_info->ft_size);
768 if (bits_per_pixel == 1)
769 {
770 #ifdef FT_LOAD_TARGET_MONO
771 load_flags |= FT_LOAD_TARGET_MONO;
772 #else
773 load_flags |= FT_LOAD_MONOCHROME;
774 #endif
775 }
776 else if (bits_per_pixel != 8)
777 /* We don't support such a rendering. */
778 return -1;
779
780 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
781 return -1;
782 bitmap->rows = ft_face->glyph->bitmap.rows;
783 bitmap->width = ft_face->glyph->bitmap.width;
784 bitmap->pitch = ft_face->glyph->bitmap.pitch;
785 bitmap->buffer = ft_face->glyph->bitmap.buffer;
786 bitmap->left = ft_face->glyph->bitmap_left;
787 bitmap->top = ft_face->glyph->bitmap_top;
788 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
789 bitmap->extra = NULL;
790
791 return 0;
792 }
793
794 static int
795 ftfont_anchor_point (font, code, index, x, y)
796 struct font *font;
797 unsigned code;
798 int index;
799 int *x, *y;
800 {
801 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
802 FT_Face ft_face = ftfont_info->ft_size->face;
803
804 if (ftfont_info->ft_size != ft_face->size)
805 FT_Activate_Size (ftfont_info->ft_size);
806 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
807 return -1;
808 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
809 return -1;
810 if (index >= ft_face->glyph->outline.n_points)
811 return -1;
812 *x = ft_face->glyph->outline.points[index].x;
813 *y = ft_face->glyph->outline.points[index].y;
814 return 0;
815 }
816
817 \f
818 void
819 syms_of_ftfont ()
820 {
821 DEFSYM (Qfreetype, "freetype");
822 DEFSYM (Qmonospace, "monospace");
823 DEFSYM (Qsans_serif, "sans-serif");
824 DEFSYM (Qserif, "serif");
825 DEFSYM (Qmono, "mono");
826 DEFSYM (Qsans, "sans");
827 DEFSYM (Qsans__serif, "sans serif");
828
829 staticpro (&freetype_font_cache);
830 freetype_font_cache = Fcons (Qt, Qnil);
831
832 staticpro (&ftfont_generic_family_list);
833 ftfont_generic_family_list
834 = Fcons (Fcons (Qmonospace, Qt),
835 Fcons (Fcons (Qsans_serif, Qt),
836 Fcons (Fcons (Qsans, Qt), Qnil)));
837
838 ftfont_driver.type = Qfreetype;
839 register_font_driver (&ftfont_driver, NULL);
840 }
841
842 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
843 (do not change this comment) */