| 1 | /* ftfont.c -- FreeType font driver. |
| 2 | Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
| 3 | Copyright (C) 2006, 2007, 2008, 2009 |
| 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 3 of the License, or |
| 12 | (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */ |
| 21 | |
| 22 | #include <config.h> |
| 23 | #include <stdio.h> |
| 24 | #include <setjmp.h> |
| 25 | |
| 26 | #include <fontconfig/fontconfig.h> |
| 27 | #include <fontconfig/fcfreetype.h> |
| 28 | |
| 29 | #include "lisp.h" |
| 30 | #include "dispextern.h" |
| 31 | #include "frame.h" |
| 32 | #include "blockinput.h" |
| 33 | #include "character.h" |
| 34 | #include "charset.h" |
| 35 | #include "coding.h" |
| 36 | #include "composite.h" |
| 37 | #include "fontset.h" |
| 38 | #include "font.h" |
| 39 | #include "ftfont.h" |
| 40 | |
| 41 | /* Symbolic type of this font-driver. */ |
| 42 | Lisp_Object Qfreetype; |
| 43 | |
| 44 | /* Fontconfig's generic families and their aliases. */ |
| 45 | static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif; |
| 46 | |
| 47 | /* Flag to tell if FcInit is already called or not. */ |
| 48 | static int fc_initialized; |
| 49 | |
| 50 | /* Handle to a FreeType library instance. */ |
| 51 | static FT_Library ft_library; |
| 52 | |
| 53 | /* Cache for FreeType fonts. */ |
| 54 | static Lisp_Object freetype_font_cache; |
| 55 | |
| 56 | /* Cache for FT_Face and FcCharSet. */ |
| 57 | static Lisp_Object ft_face_cache; |
| 58 | |
| 59 | /* The actual structure for FreeType font that can be casted to struct |
| 60 | font. */ |
| 61 | |
| 62 | struct ftfont_info |
| 63 | { |
| 64 | struct font font; |
| 65 | #ifdef HAVE_LIBOTF |
| 66 | /* The following four members must be here in this order to be |
| 67 | compatible with struct xftfont_info (in xftfont.c). */ |
| 68 | int maybe_otf; /* Flag to tell if this may be OTF or not. */ |
| 69 | OTF *otf; |
| 70 | #endif /* HAVE_LIBOTF */ |
| 71 | FT_Size ft_size; |
| 72 | int index; |
| 73 | }; |
| 74 | |
| 75 | enum ftfont_cache_for |
| 76 | { |
| 77 | FTFONT_CACHE_FOR_FACE, |
| 78 | FTFONT_CACHE_FOR_CHARSET, |
| 79 | FTFONT_CACHE_FOR_ENTITY |
| 80 | }; |
| 81 | |
| 82 | static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object)); |
| 83 | |
| 84 | static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object, |
| 85 | FcPattern *)); |
| 86 | static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object, |
| 87 | enum ftfont_cache_for)); |
| 88 | |
| 89 | static void ftfont_filter_properties P_ ((Lisp_Object font, Lisp_Object alist)); |
| 90 | |
| 91 | Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object)); |
| 92 | |
| 93 | #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM)) |
| 94 | |
| 95 | static struct |
| 96 | { |
| 97 | /* registry name */ |
| 98 | char *name; |
| 99 | /* characters to distinguish the charset from the others */ |
| 100 | int uniquifier[6]; |
| 101 | /* additional constraint by language */ |
| 102 | char *lang; |
| 103 | /* set on demand */ |
| 104 | FcCharSet *fc_charset; |
| 105 | } fc_charset_table[] = |
| 106 | { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } }, |
| 107 | { "iso8859-2", { 0x00A0, 0x010E }}, |
| 108 | { "iso8859-3", { 0x00A0, 0x0108 }}, |
| 109 | { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }}, |
| 110 | { "iso8859-5", { 0x00A0, 0x0401 }}, |
| 111 | { "iso8859-6", { 0x00A0, 0x060C }}, |
| 112 | { "iso8859-7", { 0x00A0, 0x0384 }}, |
| 113 | { "iso8859-8", { 0x00A0, 0x05D0 }}, |
| 114 | { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }}, |
| 115 | { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }}, |
| 116 | { "iso8859-11", { 0x00A0, 0x0E01 }}, |
| 117 | { "iso8859-13", { 0x00A0, 0x201C }}, |
| 118 | { "iso8859-14", { 0x00A0, 0x0174 }}, |
| 119 | { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }}, |
| 120 | { "iso8859-16", { 0x00A0, 0x0218}}, |
| 121 | { "gb2312.1980-0", { 0x4E13 }, "zh-cn"}, |
| 122 | { "big5-0", { 0xF6B1 }, "zh-tw" }, |
| 123 | { "jisx0208.1983-0", { 0x4E55 }, "ja"}, |
| 124 | { "ksc5601.1985-0", { 0xAC00 }, "ko"}, |
| 125 | { "cns11643.1992-1", { 0xFE32 }, "zh-tw"}, |
| 126 | { "cns11643.1992-2", { 0x4E33, 0x7934 }}, |
| 127 | { "cns11643.1992-3", { 0x201A9 }}, |
| 128 | { "cns11643.1992-4", { 0x20057 }}, |
| 129 | { "cns11643.1992-5", { 0x20000 }}, |
| 130 | { "cns11643.1992-6", { 0x20003 }}, |
| 131 | { "cns11643.1992-7", { 0x20055 }}, |
| 132 | { "gbk-0", { 0x4E06 }, "zh-cn"}, |
| 133 | { "jisx0212.1990-0", { 0x4E44 }}, |
| 134 | { "jisx0213.2000-1", { 0xFA10 }, "ja"}, |
| 135 | { "jisx0213.2000-2", { 0xFA49 }}, |
| 136 | { "jisx0213.2004-1", { 0x20B9F }}, |
| 137 | { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"}, |
| 138 | { "tis620.2529-1", { 0x0E01 }, "th"}, |
| 139 | { "windows-1251", { 0x0401, 0x0490 }, "ru"}, |
| 140 | { "koi8-r", { 0x0401, 0x2219 }, "ru"}, |
| 141 | { "mulelao-1", { 0x0E81 }, "lo"}, |
| 142 | { "unicode-sip", { 0x20000 }}, |
| 143 | { NULL } |
| 144 | }; |
| 145 | |
| 146 | extern Lisp_Object Qc, Qm, Qp, Qd; |
| 147 | |
| 148 | /* Dirty hack for handing ADSTYLE property. |
| 149 | |
| 150 | Fontconfig (actually the underlying FreeType) gives such ADSTYLE |
| 151 | font property of PCF/BDF fonts in FC_STYLE. And, "Bold", |
| 152 | "Oblique", "Italic", or any non-normal SWIDTH property names |
| 153 | (e.g. SemiCondensed) are appended. In addition, if there's no |
| 154 | ADSTYLE property nor non-normal WEIGHT/SLANT/SWIDTH properties, |
| 155 | "Regular" is used for FC_STYLE (see the function |
| 156 | pcf_interpret_style in src/pcf/pcfread.c of FreeType). |
| 157 | |
| 158 | Unfortunately this behavior is not documented, so the following |
| 159 | code may fail if FreeType changes the behavior in the future. */ |
| 160 | |
| 161 | static Lisp_Object |
| 162 | get_adstyle_property (FcPattern *p) |
| 163 | { |
| 164 | char *str, *end; |
| 165 | Lisp_Object adstyle; |
| 166 | |
| 167 | if (FcPatternGetString (p, FC_STYLE, 0, (FcChar8 **) &str) != FcResultMatch) |
| 168 | return Qnil; |
| 169 | for (end = str; *end && *end != ' '; end++); |
| 170 | if (*end) |
| 171 | { |
| 172 | char *p = alloca (end - str + 1); |
| 173 | memcpy (p, str, end - str); |
| 174 | p[end - str] = '\0'; |
| 175 | end = p + (end - str); |
| 176 | str = p; |
| 177 | } |
| 178 | if (xstrcasecmp (str, "Regular") == 0 |
| 179 | || xstrcasecmp (str, "Bold") == 0 |
| 180 | || xstrcasecmp (str, "Oblique") == 0 |
| 181 | || xstrcasecmp (str, "Italic") == 0) |
| 182 | return Qnil; |
| 183 | adstyle = font_intern_prop (str, end - str, 1); |
| 184 | if (font_style_to_value (FONT_WIDTH_INDEX, adstyle, 0) >= 0) |
| 185 | return Qnil; |
| 186 | return adstyle; |
| 187 | } |
| 188 | |
| 189 | static Lisp_Object |
| 190 | ftfont_pattern_entity (p, extra) |
| 191 | FcPattern *p; |
| 192 | Lisp_Object extra; |
| 193 | { |
| 194 | Lisp_Object key, cache, entity; |
| 195 | char *file, *str; |
| 196 | int index; |
| 197 | int numeric; |
| 198 | double dbl; |
| 199 | FcBool b; |
| 200 | |
| 201 | if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch) |
| 202 | return Qnil; |
| 203 | if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch) |
| 204 | return Qnil; |
| 205 | |
| 206 | key = Fcons (make_unibyte_string ((char *) file, strlen ((char *) file)), |
| 207 | make_number (index)); |
| 208 | cache = ftfont_lookup_cache (key, FTFONT_CACHE_FOR_ENTITY); |
| 209 | entity = XCAR (cache); |
| 210 | if (! NILP (entity)) |
| 211 | { |
| 212 | Lisp_Object val = font_make_entity (); |
| 213 | int i; |
| 214 | |
| 215 | for (i = 0; i < FONT_OBJLIST_INDEX; i++) |
| 216 | ASET (val, i, AREF (entity, i)); |
| 217 | return val; |
| 218 | } |
| 219 | entity = font_make_entity (); |
| 220 | XSETCAR (cache, entity); |
| 221 | |
| 222 | ASET (entity, FONT_TYPE_INDEX, Qfreetype); |
| 223 | ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1); |
| 224 | |
| 225 | if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch) |
| 226 | ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1)); |
| 227 | if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch) |
| 228 | ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1)); |
| 229 | if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch) |
| 230 | { |
| 231 | if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM) |
| 232 | numeric = FC_WEIGHT_MEDIUM; |
| 233 | FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric)); |
| 234 | } |
| 235 | if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch) |
| 236 | { |
| 237 | numeric += 100; |
| 238 | FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric)); |
| 239 | } |
| 240 | if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch) |
| 241 | { |
| 242 | FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric)); |
| 243 | } |
| 244 | if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch) |
| 245 | { |
| 246 | ASET (entity, FONT_SIZE_INDEX, make_number (dbl)); |
| 247 | } |
| 248 | else |
| 249 | ASET (entity, FONT_SIZE_INDEX, make_number (0)); |
| 250 | if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch) |
| 251 | ASET (entity, FONT_SPACING_INDEX, make_number (numeric)); |
| 252 | if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch) |
| 253 | { |
| 254 | int dpi = dbl; |
| 255 | ASET (entity, FONT_DPI_INDEX, make_number (dpi)); |
| 256 | } |
| 257 | if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch |
| 258 | && b == FcTrue) |
| 259 | { |
| 260 | ASET (entity, FONT_SIZE_INDEX, make_number (0)); |
| 261 | ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0)); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | /* As this font is not scalable, parhaps this is a BDF or PCF |
| 266 | font. */ |
| 267 | FT_Face ft_face; |
| 268 | |
| 269 | ASET (entity, FONT_ADSTYLE_INDEX, get_adstyle_property (p)); |
| 270 | if ((ft_library || FT_Init_FreeType (&ft_library) == 0) |
| 271 | && FT_New_Face (ft_library, file, index, &ft_face) == 0) |
| 272 | { |
| 273 | BDF_PropertyRec rec; |
| 274 | |
| 275 | if (FT_Get_BDF_Property (ft_face, "AVERAGE_WIDTH", &rec) == 0 |
| 276 | && rec.type == BDF_PROPERTY_TYPE_INTEGER) |
| 277 | ASET (entity, FONT_AVGWIDTH_INDEX, make_number (rec.u.integer)); |
| 278 | FT_Done_Face (ft_face); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra)); |
| 283 | font_put_extra (entity, QCfont_entity, key); |
| 284 | return entity; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | static Lisp_Object ftfont_generic_family_list; |
| 289 | |
| 290 | static Lisp_Object |
| 291 | ftfont_resolve_generic_family (family, pattern) |
| 292 | Lisp_Object family; |
| 293 | FcPattern *pattern; |
| 294 | { |
| 295 | Lisp_Object slot; |
| 296 | FcPattern *match; |
| 297 | FcResult result; |
| 298 | FcLangSet *langset; |
| 299 | |
| 300 | family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil); |
| 301 | if (EQ (family, Qmono)) |
| 302 | family = Qmonospace; |
| 303 | else if (EQ (family, Qsans) || EQ (family, Qsans__serif)) |
| 304 | family = Qsans_serif; |
| 305 | slot = assq_no_quit (family, ftfont_generic_family_list); |
| 306 | if (! CONSP (slot)) |
| 307 | return Qnil; |
| 308 | if (! EQ (XCDR (slot), Qt)) |
| 309 | return XCDR (slot); |
| 310 | pattern = FcPatternDuplicate (pattern); |
| 311 | if (! pattern) |
| 312 | goto err; |
| 313 | FcPatternDel (pattern, FC_FOUNDRY); |
| 314 | FcPatternDel (pattern, FC_FAMILY); |
| 315 | FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family)); |
| 316 | if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch) |
| 317 | { |
| 318 | /* This is to avoid the effect of locale. */ |
| 319 | langset = FcLangSetCreate (); |
| 320 | FcLangSetAdd (langset, "en"); |
| 321 | FcPatternAddLangSet (pattern, FC_LANG, langset); |
| 322 | FcLangSetDestroy (langset); |
| 323 | } |
| 324 | FcConfigSubstitute (NULL, pattern, FcMatchPattern); |
| 325 | FcDefaultSubstitute (pattern); |
| 326 | match = FcFontMatch (NULL, pattern, &result); |
| 327 | if (match) |
| 328 | { |
| 329 | FcChar8 *fam; |
| 330 | |
| 331 | if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch) |
| 332 | family = intern ((char *) fam); |
| 333 | } |
| 334 | else |
| 335 | family = Qnil; |
| 336 | XSETCDR (slot, family); |
| 337 | if (match) FcPatternDestroy (match); |
| 338 | err: |
| 339 | if (pattern) FcPatternDestroy (pattern); |
| 340 | return family; |
| 341 | } |
| 342 | |
| 343 | struct ftfont_cache_data |
| 344 | { |
| 345 | FT_Face ft_face; |
| 346 | FcCharSet *fc_charset; |
| 347 | }; |
| 348 | |
| 349 | static Lisp_Object |
| 350 | ftfont_lookup_cache (key, cache_for) |
| 351 | Lisp_Object key; |
| 352 | enum ftfont_cache_for cache_for; |
| 353 | { |
| 354 | Lisp_Object cache, val, entity; |
| 355 | struct ftfont_cache_data *cache_data; |
| 356 | |
| 357 | if (FONT_ENTITY_P (key)) |
| 358 | { |
| 359 | entity = key; |
| 360 | val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX)); |
| 361 | xassert (CONSP (val)); |
| 362 | key = XCDR (val); |
| 363 | } |
| 364 | else |
| 365 | entity = Qnil; |
| 366 | |
| 367 | if (NILP (ft_face_cache)) |
| 368 | cache = Qnil; |
| 369 | else |
| 370 | cache = Fgethash (key, ft_face_cache, Qnil); |
| 371 | if (NILP (cache)) |
| 372 | { |
| 373 | if (NILP (ft_face_cache)) |
| 374 | { |
| 375 | Lisp_Object args[2]; |
| 376 | |
| 377 | args[0] = QCtest; |
| 378 | args[1] = Qequal; |
| 379 | ft_face_cache = Fmake_hash_table (2, args); |
| 380 | } |
| 381 | cache_data = xmalloc (sizeof (struct ftfont_cache_data)); |
| 382 | cache_data->ft_face = NULL; |
| 383 | cache_data->fc_charset = NULL; |
| 384 | val = make_save_value (NULL, 0); |
| 385 | XSAVE_VALUE (val)->integer = 0; |
| 386 | XSAVE_VALUE (val)->pointer = cache_data; |
| 387 | cache = Fcons (Qnil, val); |
| 388 | Fputhash (key, cache, ft_face_cache); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | val = XCDR (cache); |
| 393 | cache_data = XSAVE_VALUE (val)->pointer; |
| 394 | } |
| 395 | |
| 396 | if (cache_for == FTFONT_CACHE_FOR_ENTITY) |
| 397 | return cache; |
| 398 | |
| 399 | if (cache_for == FTFONT_CACHE_FOR_FACE |
| 400 | ? ! cache_data->ft_face : ! cache_data->fc_charset) |
| 401 | { |
| 402 | char *filename = (char *) SDATA (XCAR (key)); |
| 403 | int index = XINT (XCDR (key)); |
| 404 | |
| 405 | if (cache_for == FTFONT_CACHE_FOR_FACE) |
| 406 | { |
| 407 | if (! ft_library |
| 408 | && FT_Init_FreeType (&ft_library) != 0) |
| 409 | return Qnil; |
| 410 | if (FT_New_Face (ft_library, filename, index, &cache_data->ft_face) |
| 411 | != 0) |
| 412 | return Qnil; |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | FcPattern *pat = NULL; |
| 417 | FcFontSet *fontset = NULL; |
| 418 | FcObjectSet *objset = NULL; |
| 419 | FcCharSet *charset = NULL; |
| 420 | |
| 421 | pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename, |
| 422 | FC_INDEX, FcTypeInteger, index, NULL); |
| 423 | if (! pat) |
| 424 | goto finish; |
| 425 | objset = FcObjectSetBuild (FC_CHARSET, FC_STYLE, NULL); |
| 426 | if (! objset) |
| 427 | goto finish; |
| 428 | fontset = FcFontList (NULL, pat, objset); |
| 429 | if (! fontset) |
| 430 | goto finish; |
| 431 | if (fontset && fontset->nfont > 0 |
| 432 | && (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0, |
| 433 | &charset) |
| 434 | == FcResultMatch)) |
| 435 | cache_data->fc_charset = FcCharSetCopy (charset); |
| 436 | else |
| 437 | cache_data->fc_charset = FcCharSetCreate (); |
| 438 | |
| 439 | finish: |
| 440 | if (fontset) |
| 441 | FcFontSetDestroy (fontset); |
| 442 | if (objset) |
| 443 | FcObjectSetDestroy (objset); |
| 444 | if (pat) |
| 445 | FcPatternDestroy (pat); |
| 446 | } |
| 447 | } |
| 448 | return cache; |
| 449 | } |
| 450 | |
| 451 | FcCharSet * |
| 452 | ftfont_get_fc_charset (entity) |
| 453 | Lisp_Object entity; |
| 454 | { |
| 455 | Lisp_Object val, cache; |
| 456 | struct ftfont_cache_data *cache_data; |
| 457 | |
| 458 | cache = ftfont_lookup_cache (entity, FTFONT_CACHE_FOR_CHARSET); |
| 459 | val = XCDR (cache); |
| 460 | cache_data = XSAVE_VALUE (val)->pointer; |
| 461 | return cache_data->fc_charset; |
| 462 | } |
| 463 | |
| 464 | #ifdef HAVE_LIBOTF |
| 465 | static OTF * |
| 466 | ftfont_get_otf (ftfont_info) |
| 467 | struct ftfont_info *ftfont_info; |
| 468 | { |
| 469 | OTF *otf; |
| 470 | |
| 471 | if (ftfont_info->otf) |
| 472 | return ftfont_info->otf; |
| 473 | if (! ftfont_info->maybe_otf) |
| 474 | return NULL; |
| 475 | otf = OTF_open_ft_face (ftfont_info->ft_size->face); |
| 476 | if (! otf || OTF_get_table (otf, "head") < 0) |
| 477 | { |
| 478 | if (otf) |
| 479 | OTF_close (otf); |
| 480 | ftfont_info->maybe_otf = 0; |
| 481 | return NULL; |
| 482 | } |
| 483 | ftfont_info->otf = otf; |
| 484 | return otf; |
| 485 | } |
| 486 | #endif /* HAVE_LIBOTF */ |
| 487 | |
| 488 | static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR)); |
| 489 | static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object)); |
| 490 | static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object)); |
| 491 | static Lisp_Object ftfont_list_family P_ ((Lisp_Object)); |
| 492 | static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int)); |
| 493 | static void ftfont_close P_ ((FRAME_PTR, struct font *)); |
| 494 | static int ftfont_has_char P_ ((Lisp_Object, int)); |
| 495 | static unsigned ftfont_encode_char P_ ((struct font *, int)); |
| 496 | static int ftfont_text_extents P_ ((struct font *, unsigned *, int, |
| 497 | struct font_metrics *)); |
| 498 | static int ftfont_get_bitmap P_ ((struct font *, unsigned, |
| 499 | struct font_bitmap *, int)); |
| 500 | static int ftfont_anchor_point P_ ((struct font *, unsigned, int, |
| 501 | int *, int *)); |
| 502 | static Lisp_Object ftfont_otf_capability P_ ((struct font *)); |
| 503 | static Lisp_Object ftfont_shape P_ ((Lisp_Object)); |
| 504 | |
| 505 | #ifdef HAVE_OTF_GET_VARIATION_GLYPHS |
| 506 | static int ftfont_variation_glyphs P_ ((struct font *, int c, |
| 507 | unsigned variations[256])); |
| 508 | #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */ |
| 509 | |
| 510 | struct font_driver ftfont_driver = |
| 511 | { |
| 512 | 0, /* Qfreetype */ |
| 513 | 0, /* case insensitive */ |
| 514 | ftfont_get_cache, |
| 515 | ftfont_list, |
| 516 | ftfont_match, |
| 517 | ftfont_list_family, |
| 518 | NULL, /* free_entity */ |
| 519 | ftfont_open, |
| 520 | ftfont_close, |
| 521 | /* We can't draw a text without device dependent functions. */ |
| 522 | NULL, /* prepare_face */ |
| 523 | NULL, /* done_face */ |
| 524 | ftfont_has_char, |
| 525 | ftfont_encode_char, |
| 526 | ftfont_text_extents, |
| 527 | /* We can't draw a text without device dependent functions. */ |
| 528 | NULL, /* draw */ |
| 529 | ftfont_get_bitmap, |
| 530 | NULL, /* get_bitmap */ |
| 531 | NULL, /* free_bitmap */ |
| 532 | NULL, /* get_outline */ |
| 533 | ftfont_anchor_point, |
| 534 | #ifdef HAVE_LIBOTF |
| 535 | ftfont_otf_capability, |
| 536 | #else /* not HAVE_LIBOTF */ |
| 537 | NULL, |
| 538 | #endif /* not HAVE_LIBOTF */ |
| 539 | NULL, /* otf_drive */ |
| 540 | NULL, /* start_for_frame */ |
| 541 | NULL, /* end_for_frame */ |
| 542 | #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF) |
| 543 | ftfont_shape, |
| 544 | #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */ |
| 545 | NULL, |
| 546 | #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */ |
| 547 | NULL, /* check */ |
| 548 | |
| 549 | #ifdef HAVE_OTF_GET_VARIATION_GLYPHS |
| 550 | ftfont_variation_glyphs, |
| 551 | #else |
| 552 | NULL, |
| 553 | #endif |
| 554 | |
| 555 | ftfont_filter_properties, /* filter_properties */ |
| 556 | }; |
| 557 | |
| 558 | extern Lisp_Object QCname; |
| 559 | |
| 560 | static Lisp_Object |
| 561 | ftfont_get_cache (f) |
| 562 | FRAME_PTR f; |
| 563 | { |
| 564 | return freetype_font_cache; |
| 565 | } |
| 566 | |
| 567 | static int |
| 568 | ftfont_get_charset (registry) |
| 569 | Lisp_Object registry; |
| 570 | { |
| 571 | char *str = (char *) SDATA (SYMBOL_NAME (registry)); |
| 572 | char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1); |
| 573 | Lisp_Object regexp; |
| 574 | int i, j; |
| 575 | |
| 576 | for (i = j = 0; i < SBYTES (SYMBOL_NAME (registry)); i++, j++) |
| 577 | { |
| 578 | if (str[i] == '.') |
| 579 | re[j++] = '\\'; |
| 580 | else if (str[i] == '*') |
| 581 | re[j++] = '.'; |
| 582 | re[j] = str[i]; |
| 583 | if (re[j] == '?') |
| 584 | re[j] = '.'; |
| 585 | } |
| 586 | re[j] = '\0'; |
| 587 | regexp = make_unibyte_string (re, j); |
| 588 | for (i = 0; fc_charset_table[i].name; i++) |
| 589 | if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0) |
| 590 | break; |
| 591 | if (! fc_charset_table[i].name) |
| 592 | return -1; |
| 593 | if (! fc_charset_table[i].fc_charset) |
| 594 | { |
| 595 | FcCharSet *charset = FcCharSetCreate (); |
| 596 | int *uniquifier = fc_charset_table[i].uniquifier; |
| 597 | |
| 598 | if (! charset) |
| 599 | return -1; |
| 600 | for (j = 0; uniquifier[j]; j++) |
| 601 | if (! FcCharSetAddChar (charset, uniquifier[j])) |
| 602 | { |
| 603 | FcCharSetDestroy (charset); |
| 604 | return -1; |
| 605 | } |
| 606 | fc_charset_table[i].fc_charset = charset; |
| 607 | } |
| 608 | return i; |
| 609 | } |
| 610 | |
| 611 | struct OpenTypeSpec |
| 612 | { |
| 613 | Lisp_Object script; |
| 614 | unsigned int script_tag, langsys_tag; |
| 615 | int nfeatures[2]; |
| 616 | unsigned int *features[2]; |
| 617 | }; |
| 618 | |
| 619 | #define OTF_SYM_TAG(SYM, TAG) \ |
| 620 | do { \ |
| 621 | unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \ |
| 622 | TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \ |
| 623 | } while (0) |
| 624 | |
| 625 | #define OTF_TAG_STR(TAG, P) \ |
| 626 | do { \ |
| 627 | (P)[0] = (char) (TAG >> 24); \ |
| 628 | (P)[1] = (char) ((TAG >> 16) & 0xFF); \ |
| 629 | (P)[2] = (char) ((TAG >> 8) & 0xFF); \ |
| 630 | (P)[3] = (char) (TAG & 0xFF); \ |
| 631 | (P)[4] = '\0'; \ |
| 632 | } while (0) |
| 633 | |
| 634 | #define OTF_TAG_SYM(SYM, TAG) \ |
| 635 | do { \ |
| 636 | char str[5]; \ |
| 637 | \ |
| 638 | OTF_TAG_STR (TAG, str); \ |
| 639 | (SYM) = font_intern_prop (str, 4, 1); \ |
| 640 | } while (0) |
| 641 | |
| 642 | |
| 643 | static struct OpenTypeSpec * |
| 644 | ftfont_get_open_type_spec (Lisp_Object otf_spec) |
| 645 | { |
| 646 | struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec)); |
| 647 | Lisp_Object val; |
| 648 | int i, j, negative; |
| 649 | |
| 650 | if (! spec) |
| 651 | return NULL; |
| 652 | spec->script = XCAR (otf_spec); |
| 653 | if (! NILP (spec->script)) |
| 654 | { |
| 655 | OTF_SYM_TAG (spec->script, spec->script_tag); |
| 656 | val = assq_no_quit (spec->script, Votf_script_alist); |
| 657 | if (CONSP (val) && SYMBOLP (XCDR (val))) |
| 658 | spec->script = XCDR (val); |
| 659 | else |
| 660 | spec->script = Qnil; |
| 661 | } |
| 662 | else |
| 663 | spec->script_tag = 0x44464C54; /* "DFLT" */ |
| 664 | otf_spec = XCDR (otf_spec); |
| 665 | val = XCAR (otf_spec); |
| 666 | if (! NILP (val)) |
| 667 | OTF_SYM_TAG (val, spec->langsys_tag); |
| 668 | else |
| 669 | spec->langsys_tag = 0; |
| 670 | spec->nfeatures[0] = spec->nfeatures[1] = 0; |
| 671 | for (i = 0; i < 2; i++) |
| 672 | { |
| 673 | Lisp_Object len; |
| 674 | |
| 675 | otf_spec = XCDR (otf_spec); |
| 676 | if (NILP (otf_spec)) |
| 677 | break; |
| 678 | val = XCAR (otf_spec); |
| 679 | if (NILP (val)) |
| 680 | continue; |
| 681 | len = Flength (val); |
| 682 | spec->features[i] = malloc (sizeof (int) * XINT (len)); |
| 683 | if (! spec->features[i]) |
| 684 | { |
| 685 | if (i > 0 && spec->features[0]) |
| 686 | free (spec->features[0]); |
| 687 | free (spec); |
| 688 | return NULL; |
| 689 | } |
| 690 | for (j = 0, negative = 0; CONSP (val); val = XCDR (val)) |
| 691 | { |
| 692 | if (NILP (XCAR (val))) |
| 693 | negative = 1; |
| 694 | else |
| 695 | { |
| 696 | unsigned int tag; |
| 697 | |
| 698 | OTF_SYM_TAG (XCAR (val), tag); |
| 699 | spec->features[i][j++] = negative ? tag & 0x80000000 : tag; |
| 700 | } |
| 701 | } |
| 702 | spec->nfeatures[i] = j; |
| 703 | } |
| 704 | return spec; |
| 705 | } |
| 706 | |
| 707 | static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *, |
| 708 | struct OpenTypeSpec **, |
| 709 | char **langname)); |
| 710 | |
| 711 | static FcPattern * |
| 712 | ftfont_spec_pattern (spec, otlayout, otspec, langname) |
| 713 | Lisp_Object spec; |
| 714 | char *otlayout; |
| 715 | struct OpenTypeSpec **otspec; |
| 716 | char **langname; |
| 717 | { |
| 718 | Lisp_Object tmp, extra; |
| 719 | FcPattern *pattern = NULL; |
| 720 | FcCharSet *charset = NULL; |
| 721 | FcLangSet *langset = NULL; |
| 722 | int n; |
| 723 | int dpi = -1; |
| 724 | int scalable = -1; |
| 725 | Lisp_Object script = Qnil; |
| 726 | Lisp_Object registry; |
| 727 | int fc_charset_idx; |
| 728 | |
| 729 | if ((n = FONT_SLANT_NUMERIC (spec)) >= 0 |
| 730 | && n < 100) |
| 731 | /* Fontconfig doesn't support reverse-italic/obligue. */ |
| 732 | return NULL; |
| 733 | |
| 734 | if (INTEGERP (AREF (spec, FONT_DPI_INDEX))) |
| 735 | dpi = XINT (AREF (spec, FONT_DPI_INDEX)); |
| 736 | if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX)) |
| 737 | && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0) |
| 738 | scalable = 1; |
| 739 | |
| 740 | registry = AREF (spec, FONT_REGISTRY_INDEX); |
| 741 | if (NILP (registry) |
| 742 | || EQ (registry, Qascii_0) |
| 743 | || EQ (registry, Qiso10646_1) |
| 744 | || EQ (registry, Qunicode_bmp)) |
| 745 | fc_charset_idx = -1; |
| 746 | else |
| 747 | { |
| 748 | FcChar8 *lang; |
| 749 | |
| 750 | fc_charset_idx = ftfont_get_charset (registry); |
| 751 | if (fc_charset_idx < 0) |
| 752 | return NULL; |
| 753 | charset = fc_charset_table[fc_charset_idx].fc_charset; |
| 754 | *langname = fc_charset_table[fc_charset_idx].lang; |
| 755 | lang = (FcChar8 *) *langname; |
| 756 | if (lang) |
| 757 | { |
| 758 | langset = FcLangSetCreate (); |
| 759 | if (! langset) |
| 760 | goto err; |
| 761 | FcLangSetAdd (langset, lang); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | otlayout[0] = '\0'; |
| 766 | for (extra = AREF (spec, FONT_EXTRA_INDEX); |
| 767 | CONSP (extra); extra = XCDR (extra)) |
| 768 | { |
| 769 | Lisp_Object key, val; |
| 770 | |
| 771 | key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra)); |
| 772 | if (EQ (key, QCdpi)) |
| 773 | dpi = XINT (val); |
| 774 | else if (EQ (key, QClang)) |
| 775 | { |
| 776 | if (! langset) |
| 777 | langset = FcLangSetCreate (); |
| 778 | if (! langset) |
| 779 | goto err; |
| 780 | if (SYMBOLP (val)) |
| 781 | { |
| 782 | if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val))) |
| 783 | goto err; |
| 784 | } |
| 785 | else |
| 786 | for (; CONSP (val); val = XCDR (val)) |
| 787 | if (SYMBOLP (XCAR (val)) |
| 788 | && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val)))) |
| 789 | goto err; |
| 790 | } |
| 791 | else if (EQ (key, QCotf)) |
| 792 | { |
| 793 | *otspec = ftfont_get_open_type_spec (val); |
| 794 | if (! *otspec) |
| 795 | return NULL; |
| 796 | strcat (otlayout, "otlayout:"); |
| 797 | OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9); |
| 798 | script = (*otspec)->script; |
| 799 | } |
| 800 | else if (EQ (key, QCscript)) |
| 801 | script = val; |
| 802 | else if (EQ (key, QCscalable)) |
| 803 | scalable = ! NILP (val); |
| 804 | } |
| 805 | |
| 806 | if (! NILP (script) && ! charset) |
| 807 | { |
| 808 | Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars); |
| 809 | |
| 810 | if (CONSP (chars) && CONSP (CDR (chars))) |
| 811 | { |
| 812 | charset = FcCharSetCreate (); |
| 813 | if (! charset) |
| 814 | goto err; |
| 815 | for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars)) |
| 816 | if (CHARACTERP (XCAR (chars)) |
| 817 | && ! FcCharSetAddChar (charset, XUINT (XCAR (chars)))) |
| 818 | goto err; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | pattern = FcPatternCreate (); |
| 823 | if (! pattern) |
| 824 | goto err; |
| 825 | tmp = AREF (spec, FONT_FOUNDRY_INDEX); |
| 826 | if (! NILP (tmp) |
| 827 | && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp))) |
| 828 | goto err; |
| 829 | tmp = AREF (spec, FONT_FAMILY_INDEX); |
| 830 | if (! NILP (tmp) |
| 831 | && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp))) |
| 832 | goto err; |
| 833 | if (charset |
| 834 | && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset)) |
| 835 | goto err; |
| 836 | if (langset |
| 837 | && ! FcPatternAddLangSet (pattern, FC_LANG, langset)) |
| 838 | goto err; |
| 839 | if (dpi >= 0 |
| 840 | && ! FcPatternAddDouble (pattern, FC_DPI, dpi)) |
| 841 | goto err; |
| 842 | if (scalable >= 0 |
| 843 | && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse)) |
| 844 | goto err; |
| 845 | |
| 846 | goto finish; |
| 847 | |
| 848 | err: |
| 849 | /* We come here because of unexpected error in fontconfig API call |
| 850 | (usually insufficient memory). */ |
| 851 | if (pattern) |
| 852 | { |
| 853 | FcPatternDestroy (pattern); |
| 854 | pattern = NULL; |
| 855 | } |
| 856 | if (*otspec) |
| 857 | { |
| 858 | if ((*otspec)->nfeatures[0] > 0) |
| 859 | free ((*otspec)->features[0]); |
| 860 | if ((*otspec)->nfeatures[1] > 0) |
| 861 | free ((*otspec)->features[1]); |
| 862 | free (*otspec); |
| 863 | *otspec = NULL; |
| 864 | } |
| 865 | |
| 866 | finish: |
| 867 | if (langset) FcLangSetDestroy (langset); |
| 868 | if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset); |
| 869 | return pattern; |
| 870 | } |
| 871 | |
| 872 | static Lisp_Object |
| 873 | ftfont_list (frame, spec) |
| 874 | Lisp_Object frame, spec; |
| 875 | { |
| 876 | Lisp_Object val = Qnil, family, adstyle; |
| 877 | int i; |
| 878 | FcPattern *pattern; |
| 879 | FcFontSet *fontset = NULL; |
| 880 | FcObjectSet *objset = NULL; |
| 881 | FcCharSet *charset; |
| 882 | Lisp_Object chars = Qnil; |
| 883 | FcResult result; |
| 884 | char otlayout[15]; /* For "otlayout:XXXX" */ |
| 885 | struct OpenTypeSpec *otspec = NULL; |
| 886 | int spacing = -1; |
| 887 | char *langname = NULL; |
| 888 | |
| 889 | if (! fc_initialized) |
| 890 | { |
| 891 | FcInit (); |
| 892 | fc_initialized = 1; |
| 893 | } |
| 894 | |
| 895 | pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname); |
| 896 | if (! pattern) |
| 897 | return Qnil; |
| 898 | if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch) |
| 899 | { |
| 900 | val = assq_no_quit (QCscript, AREF (spec, FONT_EXTRA_INDEX)); |
| 901 | if (! NILP (val)) |
| 902 | { |
| 903 | val = assq_no_quit (XCDR (val), Vscript_representative_chars); |
| 904 | if (CONSP (val) && VECTORP (XCDR (val))) |
| 905 | chars = XCDR (val); |
| 906 | } |
| 907 | val = Qnil; |
| 908 | } |
| 909 | if (INTEGERP (AREF (spec, FONT_SPACING_INDEX))) |
| 910 | spacing = XINT (AREF (spec, FONT_SPACING_INDEX)); |
| 911 | family = AREF (spec, FONT_FAMILY_INDEX); |
| 912 | if (! NILP (family)) |
| 913 | { |
| 914 | Lisp_Object resolved; |
| 915 | |
| 916 | resolved = ftfont_resolve_generic_family (family, pattern); |
| 917 | if (! NILP (resolved)) |
| 918 | { |
| 919 | FcPatternDel (pattern, FC_FAMILY); |
| 920 | if (! FcPatternAddString (pattern, FC_FAMILY, |
| 921 | SYMBOL_FcChar8 (resolved))) |
| 922 | goto err; |
| 923 | } |
| 924 | } |
| 925 | adstyle = AREF (spec, FONT_ADSTYLE_INDEX); |
| 926 | if (! NILP (adstyle) && SBYTES (SYMBOL_NAME (adstyle)) == 0) |
| 927 | adstyle = Qnil; |
| 928 | objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT, |
| 929 | FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE, |
| 930 | FC_STYLE, FC_FILE, FC_INDEX, |
| 931 | #ifdef FC_CAPABILITY |
| 932 | FC_CAPABILITY, |
| 933 | #endif /* FC_CAPABILITY */ |
| 934 | #ifdef FC_FONTFORMAT |
| 935 | FC_FONTFORMAT, |
| 936 | #endif |
| 937 | NULL); |
| 938 | if (! objset) |
| 939 | goto err; |
| 940 | if (! NILP (chars)) |
| 941 | FcObjectSetAdd (objset, FC_CHARSET); |
| 942 | |
| 943 | fontset = FcFontList (NULL, pattern, objset); |
| 944 | if (! fontset || fontset->nfont == 0) |
| 945 | goto finish; |
| 946 | #if 0 |
| 947 | /* Need fix because this finds any fonts. */ |
| 948 | if (fontset->nfont == 0 && ! NILP (family)) |
| 949 | { |
| 950 | /* Try maching with configuration. For instance, the |
| 951 | configuration may specify "Nimbus Mono L" as an alias of |
| 952 | "Courier". */ |
| 953 | FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString, |
| 954 | SYMBOL_FcChar8 (family), NULL); |
| 955 | FcChar8 *fam; |
| 956 | |
| 957 | if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue) |
| 958 | { |
| 959 | for (i = 0; |
| 960 | FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch; |
| 961 | i++) |
| 962 | { |
| 963 | FcPatternDel (pattern, FC_FAMILY); |
| 964 | FcPatternAddString (pattern, FC_FAMILY, fam); |
| 965 | FcFontSetDestroy (fontset); |
| 966 | fontset = FcFontList (NULL, pattern, objset); |
| 967 | if (fontset && fontset->nfont > 0) |
| 968 | break; |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | #endif |
| 973 | for (i = 0; i < fontset->nfont; i++) |
| 974 | { |
| 975 | Lisp_Object entity; |
| 976 | |
| 977 | if (spacing >= 0) |
| 978 | { |
| 979 | int this; |
| 980 | |
| 981 | if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this) |
| 982 | == FcResultMatch) |
| 983 | && spacing != this) |
| 984 | continue; |
| 985 | } |
| 986 | |
| 987 | #ifdef FC_CAPABILITY |
| 988 | if (otlayout[0]) |
| 989 | { |
| 990 | FcChar8 *this; |
| 991 | |
| 992 | if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, &this) |
| 993 | != FcResultMatch |
| 994 | || ! strstr ((char *) this, otlayout)) |
| 995 | continue; |
| 996 | } |
| 997 | #endif /* FC_CAPABILITY */ |
| 998 | #ifdef HAVE_LIBOTF |
| 999 | if (otspec) |
| 1000 | { |
| 1001 | FcChar8 *file; |
| 1002 | OTF *otf; |
| 1003 | |
| 1004 | if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file) |
| 1005 | != FcResultMatch) |
| 1006 | continue; |
| 1007 | otf = OTF_open ((char *) file); |
| 1008 | if (! otf) |
| 1009 | continue; |
| 1010 | if (OTF_check_features (otf, 1, |
| 1011 | otspec->script_tag, otspec->langsys_tag, |
| 1012 | otspec->features[0], |
| 1013 | otspec->nfeatures[0]) != 1 |
| 1014 | || OTF_check_features (otf, 0, |
| 1015 | otspec->script_tag, otspec->langsys_tag, |
| 1016 | otspec->features[1], |
| 1017 | otspec->nfeatures[1]) != 1) |
| 1018 | continue; |
| 1019 | } |
| 1020 | #endif /* HAVE_LIBOTF */ |
| 1021 | if (VECTORP (chars)) |
| 1022 | { |
| 1023 | int j; |
| 1024 | |
| 1025 | if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset) |
| 1026 | != FcResultMatch) |
| 1027 | continue; |
| 1028 | for (j = 0; j < ASIZE (chars); j++) |
| 1029 | if (NATNUMP (AREF (chars, j)) |
| 1030 | && FcCharSetHasChar (charset, XFASTINT (AREF (chars, j)))) |
| 1031 | break; |
| 1032 | if (j == ASIZE (chars)) |
| 1033 | continue; |
| 1034 | } |
| 1035 | if (! NILP (adstyle) || langname) |
| 1036 | { |
| 1037 | Lisp_Object this_adstyle = get_adstyle_property (fontset->fonts[i]); |
| 1038 | |
| 1039 | if (! NILP (adstyle) |
| 1040 | && (NILP (this_adstyle) |
| 1041 | || xstrcasecmp (SDATA (SYMBOL_NAME (adstyle)), |
| 1042 | SDATA (SYMBOL_NAME (this_adstyle))) != 0)) |
| 1043 | continue; |
| 1044 | if (langname |
| 1045 | && ! NILP (this_adstyle) |
| 1046 | && xstrcasecmp (langname, SDATA (SYMBOL_NAME (this_adstyle)))) |
| 1047 | continue; |
| 1048 | } |
| 1049 | entity = ftfont_pattern_entity (fontset->fonts[i], |
| 1050 | AREF (spec, FONT_EXTRA_INDEX)); |
| 1051 | if (! NILP (entity)) |
| 1052 | val = Fcons (entity, val); |
| 1053 | } |
| 1054 | val = Fnreverse (val); |
| 1055 | goto finish; |
| 1056 | |
| 1057 | err: |
| 1058 | /* We come here because of unexpected error in fontconfig API call |
| 1059 | (usually insufficient memory). */ |
| 1060 | val = Qnil; |
| 1061 | |
| 1062 | finish: |
| 1063 | FONT_ADD_LOG ("ftfont-list", spec, val); |
| 1064 | if (objset) FcObjectSetDestroy (objset); |
| 1065 | if (fontset) FcFontSetDestroy (fontset); |
| 1066 | if (pattern) FcPatternDestroy (pattern); |
| 1067 | return val; |
| 1068 | } |
| 1069 | |
| 1070 | static Lisp_Object |
| 1071 | ftfont_match (frame, spec) |
| 1072 | Lisp_Object frame, spec; |
| 1073 | { |
| 1074 | Lisp_Object entity = Qnil; |
| 1075 | FcPattern *pattern, *match = NULL; |
| 1076 | FcResult result; |
| 1077 | char otlayout[15]; /* For "otlayout:XXXX" */ |
| 1078 | struct OpenTypeSpec *otspec = NULL; |
| 1079 | char *langname = NULL; |
| 1080 | |
| 1081 | if (! fc_initialized) |
| 1082 | { |
| 1083 | FcInit (); |
| 1084 | fc_initialized = 1; |
| 1085 | } |
| 1086 | |
| 1087 | pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname); |
| 1088 | if (! pattern) |
| 1089 | return Qnil; |
| 1090 | |
| 1091 | if (INTEGERP (AREF (spec, FONT_SIZE_INDEX))) |
| 1092 | { |
| 1093 | FcValue value; |
| 1094 | |
| 1095 | value.type = FcTypeDouble; |
| 1096 | value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX)); |
| 1097 | FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse); |
| 1098 | } |
| 1099 | if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue) |
| 1100 | { |
| 1101 | FcDefaultSubstitute (pattern); |
| 1102 | match = FcFontMatch (NULL, pattern, &result); |
| 1103 | if (match) |
| 1104 | { |
| 1105 | entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX)); |
| 1106 | FcPatternDestroy (match); |
| 1107 | if (! NILP (AREF (spec, FONT_FAMILY_INDEX)) |
| 1108 | && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX), |
| 1109 | ftfont_generic_family_list)) |
| 1110 | && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX), |
| 1111 | AREF (entity, FONT_FAMILY_INDEX)))) |
| 1112 | entity = Qnil; |
| 1113 | } |
| 1114 | } |
| 1115 | FcPatternDestroy (pattern); |
| 1116 | |
| 1117 | FONT_ADD_LOG ("ftfont-match", spec, entity); |
| 1118 | return entity; |
| 1119 | } |
| 1120 | |
| 1121 | static Lisp_Object |
| 1122 | ftfont_list_family (frame) |
| 1123 | Lisp_Object frame; |
| 1124 | { |
| 1125 | Lisp_Object list = Qnil; |
| 1126 | FcPattern *pattern = NULL; |
| 1127 | FcFontSet *fontset = NULL; |
| 1128 | FcObjectSet *objset = NULL; |
| 1129 | int i; |
| 1130 | |
| 1131 | if (! fc_initialized) |
| 1132 | { |
| 1133 | FcInit (); |
| 1134 | fc_initialized = 1; |
| 1135 | } |
| 1136 | |
| 1137 | pattern = FcPatternCreate (); |
| 1138 | if (! pattern) |
| 1139 | goto finish; |
| 1140 | objset = FcObjectSetBuild (FC_FAMILY, NULL); |
| 1141 | if (! objset) |
| 1142 | goto finish; |
| 1143 | fontset = FcFontList (NULL, pattern, objset); |
| 1144 | if (! fontset) |
| 1145 | goto finish; |
| 1146 | |
| 1147 | for (i = 0; i < fontset->nfont; i++) |
| 1148 | { |
| 1149 | FcPattern *pat = fontset->fonts[i]; |
| 1150 | FcChar8 *str; |
| 1151 | |
| 1152 | if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch) |
| 1153 | list = Fcons (intern ((char *) str), list); |
| 1154 | } |
| 1155 | |
| 1156 | finish: |
| 1157 | if (objset) FcObjectSetDestroy (objset); |
| 1158 | if (fontset) FcFontSetDestroy (fontset); |
| 1159 | if (pattern) FcPatternDestroy (pattern); |
| 1160 | |
| 1161 | return list; |
| 1162 | } |
| 1163 | |
| 1164 | |
| 1165 | static Lisp_Object |
| 1166 | ftfont_open (f, entity, pixel_size) |
| 1167 | FRAME_PTR f; |
| 1168 | Lisp_Object entity; |
| 1169 | int pixel_size; |
| 1170 | { |
| 1171 | struct ftfont_info *ftfont_info; |
| 1172 | struct font *font; |
| 1173 | struct ftfont_cache_data *cache_data; |
| 1174 | FT_Face ft_face; |
| 1175 | FT_Size ft_size; |
| 1176 | FT_UInt size; |
| 1177 | Lisp_Object val, filename, index, cache, font_object; |
| 1178 | int scalable; |
| 1179 | int spacing; |
| 1180 | char name[256]; |
| 1181 | int i, len; |
| 1182 | int upEM; |
| 1183 | |
| 1184 | val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX)); |
| 1185 | if (! CONSP (val)) |
| 1186 | return Qnil; |
| 1187 | val = XCDR (val); |
| 1188 | cache = ftfont_lookup_cache (entity, FTFONT_CACHE_FOR_FACE); |
| 1189 | if (NILP (cache)) |
| 1190 | return Qnil; |
| 1191 | filename = XCAR (val); |
| 1192 | index = XCDR (val); |
| 1193 | val = XCDR (cache); |
| 1194 | cache_data = XSAVE_VALUE (XCDR (cache))->pointer; |
| 1195 | ft_face = cache_data->ft_face; |
| 1196 | if (XSAVE_VALUE (val)->integer > 0) |
| 1197 | { |
| 1198 | /* FT_Face in this cache is already used by the different size. */ |
| 1199 | if (FT_New_Size (ft_face, &ft_size) != 0) |
| 1200 | return Qnil; |
| 1201 | if (FT_Activate_Size (ft_size) != 0) |
| 1202 | { |
| 1203 | FT_Done_Size (ft_size); |
| 1204 | return Qnil; |
| 1205 | } |
| 1206 | } |
| 1207 | XSAVE_VALUE (val)->integer++; |
| 1208 | size = XINT (AREF (entity, FONT_SIZE_INDEX)); |
| 1209 | if (size == 0) |
| 1210 | size = pixel_size; |
| 1211 | if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0) |
| 1212 | { |
| 1213 | if (XSAVE_VALUE (val)->integer == 0) |
| 1214 | FT_Done_Face (ft_face); |
| 1215 | return Qnil; |
| 1216 | } |
| 1217 | |
| 1218 | font_object = font_make_object (VECSIZE (struct ftfont_info), entity, size); |
| 1219 | ASET (font_object, FONT_TYPE_INDEX, Qfreetype); |
| 1220 | len = font_unparse_xlfd (entity, size, name, 256); |
| 1221 | if (len > 0) |
| 1222 | ASET (font_object, FONT_NAME_INDEX, make_string (name, len)); |
| 1223 | len = font_unparse_fcname (entity, size, name, 256); |
| 1224 | if (len > 0) |
| 1225 | ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len)); |
| 1226 | else |
| 1227 | ASET (font_object, FONT_FULLNAME_INDEX, |
| 1228 | AREF (font_object, FONT_NAME_INDEX)); |
| 1229 | ASET (font_object, FONT_FILE_INDEX, filename); |
| 1230 | ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (NULL, filename)); |
| 1231 | font = XFONT_OBJECT (font_object); |
| 1232 | ftfont_info = (struct ftfont_info *) font; |
| 1233 | ftfont_info->ft_size = ft_face->size; |
| 1234 | ftfont_info->index = XINT (index); |
| 1235 | #ifdef HAVE_LIBOTF |
| 1236 | ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT; |
| 1237 | ftfont_info->otf = NULL; |
| 1238 | #endif /* HAVE_LIBOTF */ |
| 1239 | font->pixel_size = size; |
| 1240 | font->driver = &ftfont_driver; |
| 1241 | font->encoding_charset = font->repertory_charset = -1; |
| 1242 | |
| 1243 | upEM = ft_face->units_per_EM; |
| 1244 | scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX)) |
| 1245 | && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0); |
| 1246 | if (scalable) |
| 1247 | { |
| 1248 | font->ascent = ft_face->ascender * size / upEM; |
| 1249 | font->descent = - ft_face->descender * size / upEM; |
| 1250 | font->height = ft_face->height * size / upEM; |
| 1251 | } |
| 1252 | else |
| 1253 | { |
| 1254 | font->ascent = ft_face->size->metrics.ascender >> 6; |
| 1255 | font->descent = - ft_face->size->metrics.descender >> 6; |
| 1256 | font->height = ft_face->size->metrics.height >> 6; |
| 1257 | } |
| 1258 | if (INTEGERP (AREF (entity, FONT_SPACING_INDEX))) |
| 1259 | spacing = XINT (AREF (entity, FONT_SPACING_INDEX)); |
| 1260 | else |
| 1261 | spacing = FC_PROPORTIONAL; |
| 1262 | if (spacing != FC_PROPORTIONAL) |
| 1263 | font->min_width = font->average_width = font->space_width |
| 1264 | = (scalable ? ft_face->max_advance_width * size / upEM |
| 1265 | : ft_face->size->metrics.max_advance >> 6); |
| 1266 | else |
| 1267 | { |
| 1268 | int n; |
| 1269 | |
| 1270 | font->min_width = font->average_width = font->space_width = 0; |
| 1271 | for (i = 32, n = 0; i < 127; i++) |
| 1272 | if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) == 0) |
| 1273 | { |
| 1274 | int this_width = ft_face->glyph->metrics.horiAdvance >> 6; |
| 1275 | |
| 1276 | if (this_width > 0 |
| 1277 | && (! font->min_width || font->min_width > this_width)) |
| 1278 | font->min_width = this_width; |
| 1279 | if (i == 32) |
| 1280 | font->space_width = this_width; |
| 1281 | font->average_width += this_width; |
| 1282 | n++; |
| 1283 | } |
| 1284 | if (n > 0) |
| 1285 | font->average_width /= n; |
| 1286 | } |
| 1287 | |
| 1288 | font->baseline_offset = 0; |
| 1289 | font->relative_compose = 0; |
| 1290 | font->default_ascent = 0; |
| 1291 | font->vertical_centering = 0; |
| 1292 | if (scalable) |
| 1293 | { |
| 1294 | font->underline_position = -ft_face->underline_position * size / upEM; |
| 1295 | font->underline_thickness = ft_face->underline_thickness * size / upEM; |
| 1296 | } |
| 1297 | else |
| 1298 | { |
| 1299 | font->underline_position = -1; |
| 1300 | font->underline_thickness = 0; |
| 1301 | } |
| 1302 | |
| 1303 | return font_object; |
| 1304 | } |
| 1305 | |
| 1306 | static void |
| 1307 | ftfont_close (f, font) |
| 1308 | FRAME_PTR f; |
| 1309 | struct font *font; |
| 1310 | { |
| 1311 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; |
| 1312 | Lisp_Object val, cache; |
| 1313 | |
| 1314 | val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index)); |
| 1315 | cache = ftfont_lookup_cache (val, FTFONT_CACHE_FOR_FACE); |
| 1316 | xassert (CONSP (cache)); |
| 1317 | val = XCDR (cache); |
| 1318 | (XSAVE_VALUE (val)->integer)--; |
| 1319 | if (XSAVE_VALUE (val)->integer == 0) |
| 1320 | { |
| 1321 | struct ftfont_cache_data *cache_data = XSAVE_VALUE (val)->pointer; |
| 1322 | |
| 1323 | FT_Done_Face (cache_data->ft_face); |
| 1324 | #ifdef HAVE_LIBOTF |
| 1325 | if (ftfont_info->otf) |
| 1326 | OTF_close (ftfont_info->otf); |
| 1327 | #endif |
| 1328 | cache_data->ft_face = NULL; |
| 1329 | } |
| 1330 | else |
| 1331 | FT_Done_Size (ftfont_info->ft_size); |
| 1332 | } |
| 1333 | |
| 1334 | static int |
| 1335 | ftfont_has_char (font, c) |
| 1336 | Lisp_Object font; |
| 1337 | int c; |
| 1338 | { |
| 1339 | struct charset *cs = NULL; |
| 1340 | |
| 1341 | if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qja) |
| 1342 | && charset_jisx0208 >= 0) |
| 1343 | cs = CHARSET_FROM_ID (charset_jisx0208); |
| 1344 | else if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qko) |
| 1345 | && charset_ksc5601 >= 0) |
| 1346 | cs = CHARSET_FROM_ID (charset_ksc5601); |
| 1347 | if (cs) |
| 1348 | return (ENCODE_CHAR (cs, c) != CHARSET_INVALID_CODE (cs)); |
| 1349 | |
| 1350 | if (FONT_ENTITY_P (font)) |
| 1351 | { |
| 1352 | FcCharSet *charset = ftfont_get_fc_charset (font); |
| 1353 | |
| 1354 | return (FcCharSetHasChar (charset, c) == FcTrue); |
| 1355 | } |
| 1356 | else |
| 1357 | { |
| 1358 | struct ftfont_info *ftfont_info; |
| 1359 | |
| 1360 | ftfont_info = (struct ftfont_info *) XFONT_OBJECT (font); |
| 1361 | return (FT_Get_Char_Index (ftfont_info->ft_size->face, (FT_ULong) c) |
| 1362 | != 0); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | static unsigned |
| 1367 | ftfont_encode_char (font, c) |
| 1368 | struct font *font; |
| 1369 | int c; |
| 1370 | { |
| 1371 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; |
| 1372 | FT_Face ft_face = ftfont_info->ft_size->face; |
| 1373 | FT_ULong charcode = c; |
| 1374 | FT_UInt code = FT_Get_Char_Index (ft_face, charcode); |
| 1375 | |
| 1376 | return (code > 0 ? code : FONT_INVALID_CODE); |
| 1377 | } |
| 1378 | |
| 1379 | static int |
| 1380 | ftfont_text_extents (font, code, nglyphs, metrics) |
| 1381 | struct font *font; |
| 1382 | unsigned *code; |
| 1383 | int nglyphs; |
| 1384 | struct font_metrics *metrics; |
| 1385 | { |
| 1386 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; |
| 1387 | FT_Face ft_face = ftfont_info->ft_size->face; |
| 1388 | int width = 0; |
| 1389 | int i, first; |
| 1390 | |
| 1391 | if (ftfont_info->ft_size != ft_face->size) |
| 1392 | FT_Activate_Size (ftfont_info->ft_size); |
| 1393 | if (metrics) |
| 1394 | bzero (metrics, sizeof (struct font_metrics)); |
| 1395 | for (i = 0, first = 1; i < nglyphs; i++) |
| 1396 | { |
| 1397 | if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0) |
| 1398 | { |
| 1399 | FT_Glyph_Metrics *m = &ft_face->glyph->metrics; |
| 1400 | |
| 1401 | if (first) |
| 1402 | { |
| 1403 | if (metrics) |
| 1404 | { |
| 1405 | metrics->lbearing = m->horiBearingX >> 6; |
| 1406 | metrics->rbearing = (m->horiBearingX + m->width) >> 6; |
| 1407 | metrics->ascent = m->horiBearingY >> 6; |
| 1408 | metrics->descent = (m->height - m->horiBearingY) >> 6; |
| 1409 | } |
| 1410 | first = 0; |
| 1411 | } |
| 1412 | if (metrics) |
| 1413 | { |
| 1414 | if (metrics->lbearing > width + (m->horiBearingX >> 6)) |
| 1415 | metrics->lbearing = width + (m->horiBearingX >> 6); |
| 1416 | if (metrics->rbearing |
| 1417 | < width + ((m->horiBearingX + m->width) >> 6)) |
| 1418 | metrics->rbearing |
| 1419 | = width + ((m->horiBearingX + m->width) >> 6); |
| 1420 | if (metrics->ascent < (m->horiBearingY >> 6)) |
| 1421 | metrics->ascent = m->horiBearingY >> 6; |
| 1422 | if (metrics->descent > ((m->height - m->horiBearingY) >> 6)) |
| 1423 | metrics->descent = (m->height - m->horiBearingY) >> 6; |
| 1424 | } |
| 1425 | width += m->horiAdvance >> 6; |
| 1426 | } |
| 1427 | else |
| 1428 | { |
| 1429 | width += font->space_width; |
| 1430 | } |
| 1431 | } |
| 1432 | if (metrics) |
| 1433 | metrics->width = width; |
| 1434 | |
| 1435 | return width; |
| 1436 | } |
| 1437 | |
| 1438 | static int |
| 1439 | ftfont_get_bitmap (font, code, bitmap, bits_per_pixel) |
| 1440 | struct font *font; |
| 1441 | unsigned code; |
| 1442 | struct font_bitmap *bitmap; |
| 1443 | int bits_per_pixel; |
| 1444 | { |
| 1445 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; |
| 1446 | FT_Face ft_face = ftfont_info->ft_size->face; |
| 1447 | FT_Int32 load_flags = FT_LOAD_RENDER; |
| 1448 | |
| 1449 | if (ftfont_info->ft_size != ft_face->size) |
| 1450 | FT_Activate_Size (ftfont_info->ft_size); |
| 1451 | if (bits_per_pixel == 1) |
| 1452 | { |
| 1453 | #ifdef FT_LOAD_TARGET_MONO |
| 1454 | load_flags |= FT_LOAD_TARGET_MONO; |
| 1455 | #else |
| 1456 | load_flags |= FT_LOAD_MONOCHROME; |
| 1457 | #endif |
| 1458 | } |
| 1459 | else if (bits_per_pixel != 8) |
| 1460 | /* We don't support such a rendering. */ |
| 1461 | return -1; |
| 1462 | |
| 1463 | if (FT_Load_Glyph (ft_face, code, load_flags) != 0) |
| 1464 | return -1; |
| 1465 | bitmap->bits_per_pixel |
| 1466 | = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1 |
| 1467 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8 |
| 1468 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8 |
| 1469 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8 |
| 1470 | : -1); |
| 1471 | if (bitmap->bits_per_pixel < 0) |
| 1472 | /* We don't suport that kind of pixel mode. */ |
| 1473 | return -1; |
| 1474 | bitmap->rows = ft_face->glyph->bitmap.rows; |
| 1475 | bitmap->width = ft_face->glyph->bitmap.width; |
| 1476 | bitmap->pitch = ft_face->glyph->bitmap.pitch; |
| 1477 | bitmap->buffer = ft_face->glyph->bitmap.buffer; |
| 1478 | bitmap->left = ft_face->glyph->bitmap_left; |
| 1479 | bitmap->top = ft_face->glyph->bitmap_top; |
| 1480 | bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6; |
| 1481 | bitmap->extra = NULL; |
| 1482 | |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | static int |
| 1487 | ftfont_anchor_point (font, code, index, x, y) |
| 1488 | struct font *font; |
| 1489 | unsigned code; |
| 1490 | int index; |
| 1491 | int *x, *y; |
| 1492 | { |
| 1493 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; |
| 1494 | FT_Face ft_face = ftfont_info->ft_size->face; |
| 1495 | |
| 1496 | if (ftfont_info->ft_size != ft_face->size) |
| 1497 | FT_Activate_Size (ftfont_info->ft_size); |
| 1498 | if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0) |
| 1499 | return -1; |
| 1500 | if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE) |
| 1501 | return -1; |
| 1502 | if (index >= ft_face->glyph->outline.n_points) |
| 1503 | return -1; |
| 1504 | *x = ft_face->glyph->outline.points[index].x; |
| 1505 | *y = ft_face->glyph->outline.points[index].y; |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | #ifdef HAVE_LIBOTF |
| 1510 | |
| 1511 | static Lisp_Object |
| 1512 | ftfont_otf_features (gsub_gpos) |
| 1513 | OTF_GSUB_GPOS *gsub_gpos; |
| 1514 | { |
| 1515 | Lisp_Object scripts, langsyses, features, sym; |
| 1516 | int i, j, k, l; |
| 1517 | |
| 1518 | for (scripts = Qnil, i = gsub_gpos->ScriptList.ScriptCount - 1; i >= 0; i--) |
| 1519 | { |
| 1520 | OTF_Script *otf_script = gsub_gpos->ScriptList.Script + i; |
| 1521 | |
| 1522 | for (langsyses = Qnil, j = otf_script->LangSysCount - 1; j >= -1; j--) |
| 1523 | { |
| 1524 | OTF_LangSys *otf_langsys; |
| 1525 | |
| 1526 | if (j >= 0) |
| 1527 | otf_langsys = otf_script->LangSys + j; |
| 1528 | else if (otf_script->DefaultLangSysOffset) |
| 1529 | otf_langsys = &otf_script->DefaultLangSys; |
| 1530 | else |
| 1531 | break; |
| 1532 | |
| 1533 | for (features = Qnil, k = otf_langsys->FeatureCount - 1; k >= 0; k--) |
| 1534 | { |
| 1535 | l = otf_langsys->FeatureIndex[k]; |
| 1536 | if (l >= gsub_gpos->FeatureList.FeatureCount) |
| 1537 | continue; |
| 1538 | OTF_TAG_SYM (sym, gsub_gpos->FeatureList.Feature[l].FeatureTag); |
| 1539 | features = Fcons (sym, features); |
| 1540 | } |
| 1541 | if (j >= 0) |
| 1542 | OTF_TAG_SYM (sym, otf_script->LangSysRecord[j].LangSysTag); |
| 1543 | else |
| 1544 | sym = Qnil; |
| 1545 | langsyses = Fcons (Fcons (sym, features), langsyses); |
| 1546 | } |
| 1547 | |
| 1548 | OTF_TAG_SYM (sym, gsub_gpos->ScriptList.Script[i].ScriptTag); |
| 1549 | scripts = Fcons (Fcons (sym, langsyses), scripts); |
| 1550 | } |
| 1551 | return scripts; |
| 1552 | |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | static Lisp_Object |
| 1557 | ftfont_otf_capability (font) |
| 1558 | struct font *font; |
| 1559 | { |
| 1560 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; |
| 1561 | OTF *otf = ftfont_get_otf (ftfont_info); |
| 1562 | Lisp_Object gsub_gpos; |
| 1563 | |
| 1564 | if (! otf) |
| 1565 | return Qnil; |
| 1566 | gsub_gpos = Fcons (Qnil, Qnil); |
| 1567 | if (OTF_get_table (otf, "GSUB") == 0 |
| 1568 | && otf->gsub->FeatureList.FeatureCount > 0) |
| 1569 | XSETCAR (gsub_gpos, ftfont_otf_features (otf->gsub)); |
| 1570 | if (OTF_get_table (otf, "GPOS") == 0 |
| 1571 | && otf->gpos->FeatureList.FeatureCount > 0) |
| 1572 | XSETCDR (gsub_gpos, ftfont_otf_features (otf->gpos)); |
| 1573 | return gsub_gpos; |
| 1574 | } |
| 1575 | |
| 1576 | #ifdef HAVE_M17N_FLT |
| 1577 | |
| 1578 | struct MFLTFontFT |
| 1579 | { |
| 1580 | MFLTFont flt_font; |
| 1581 | struct font *font; |
| 1582 | FT_Face ft_face; |
| 1583 | OTF *otf; |
| 1584 | }; |
| 1585 | |
| 1586 | static int |
| 1587 | ftfont_get_glyph_id (font, gstring, from, to) |
| 1588 | MFLTFont *font; |
| 1589 | MFLTGlyphString *gstring; |
| 1590 | int from, to; |
| 1591 | { |
| 1592 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; |
| 1593 | FT_Face ft_face = flt_font_ft->ft_face; |
| 1594 | MFLTGlyph *g; |
| 1595 | |
| 1596 | for (g = gstring->glyphs + from; from < to; g++, from++) |
| 1597 | if (! g->encoded) |
| 1598 | { |
| 1599 | FT_UInt code = FT_Get_Char_Index (ft_face, g->code); |
| 1600 | |
| 1601 | g->code = code > 0 ? code : FONT_INVALID_CODE; |
| 1602 | g->encoded = 1; |
| 1603 | } |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
| 1607 | static int |
| 1608 | ftfont_get_metrics (font, gstring, from, to) |
| 1609 | MFLTFont *font; |
| 1610 | MFLTGlyphString *gstring; |
| 1611 | int from, to; |
| 1612 | { |
| 1613 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; |
| 1614 | FT_Face ft_face = flt_font_ft->ft_face; |
| 1615 | MFLTGlyph *g; |
| 1616 | |
| 1617 | for (g = gstring->glyphs + from; from < to; g++, from++) |
| 1618 | if (! g->measured) |
| 1619 | { |
| 1620 | if (g->code != FONT_INVALID_CODE) |
| 1621 | { |
| 1622 | FT_Glyph_Metrics *m; |
| 1623 | |
| 1624 | if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0) |
| 1625 | abort (); |
| 1626 | m = &ft_face->glyph->metrics; |
| 1627 | |
| 1628 | g->lbearing = m->horiBearingX; |
| 1629 | g->rbearing = m->horiBearingX + m->width; |
| 1630 | g->ascent = m->horiBearingY; |
| 1631 | g->descent = m->height - m->horiBearingY; |
| 1632 | g->xadv = m->horiAdvance; |
| 1633 | } |
| 1634 | else |
| 1635 | { |
| 1636 | g->lbearing = 0; |
| 1637 | g->rbearing = g->xadv = flt_font_ft->font->space_width << 6; |
| 1638 | g->ascent = flt_font_ft->font->ascent << 6; |
| 1639 | g->descent = flt_font_ft->font->descent << 6; |
| 1640 | } |
| 1641 | g->yadv = 0; |
| 1642 | g->measured = 1; |
| 1643 | } |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
| 1647 | static int |
| 1648 | ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec) |
| 1649 | { |
| 1650 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; |
| 1651 | OTF *otf = flt_font_ft->otf; |
| 1652 | OTF_Tag *tags; |
| 1653 | int i, n, negative; |
| 1654 | |
| 1655 | for (i = 0; i < 2; i++) |
| 1656 | { |
| 1657 | if (! spec->features[i]) |
| 1658 | continue; |
| 1659 | for (n = 0; spec->features[i][n]; n++); |
| 1660 | tags = alloca (sizeof (OTF_Tag) * n); |
| 1661 | for (n = 0, negative = 0; spec->features[i][n]; n++) |
| 1662 | { |
| 1663 | if (spec->features[i][n] == 0xFFFFFFFF) |
| 1664 | negative = 1; |
| 1665 | else if (negative) |
| 1666 | tags[n - 1] = spec->features[i][n] | 0x80000000; |
| 1667 | else |
| 1668 | tags[n] = spec->features[i][n]; |
| 1669 | } |
| 1670 | if (n - negative > 0 |
| 1671 | && OTF_check_features (otf, i == 0, spec->script, spec->langsys, |
| 1672 | tags, n - negative) != 1) |
| 1673 | return 0; |
| 1674 | } |
| 1675 | return 1; |
| 1676 | } |
| 1677 | |
| 1678 | #define DEVICE_DELTA(table, size) \ |
| 1679 | (((size) >= (table).StartSize && (size) <= (table).EndSize) \ |
| 1680 | ? (table).DeltaValue[(size) - (table).StartSize] << 6 \ |
| 1681 | : 0) |
| 1682 | |
| 1683 | static void |
| 1684 | adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor, |
| 1685 | unsigned code, int x_ppem, int y_ppem, int *x, int *y) |
| 1686 | { |
| 1687 | if (anchor->AnchorFormat == 2) |
| 1688 | { |
| 1689 | FT_Outline *outline; |
| 1690 | int ap = anchor->f.f1.AnchorPoint; |
| 1691 | |
| 1692 | FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME); |
| 1693 | outline = &ft_face->glyph->outline; |
| 1694 | if (ap < outline->n_points) |
| 1695 | { |
| 1696 | *x = outline->points[ap].x << 6; |
| 1697 | *y = outline->points[ap].y << 6; |
| 1698 | } |
| 1699 | } |
| 1700 | else if (anchor->AnchorFormat == 3) |
| 1701 | { |
| 1702 | if (anchor->f.f2.XDeviceTable.offset |
| 1703 | && anchor->f.f2.XDeviceTable.DeltaValue) |
| 1704 | *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem); |
| 1705 | if (anchor->f.f2.YDeviceTable.offset |
| 1706 | && anchor->f.f2.YDeviceTable.DeltaValue) |
| 1707 | *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | static OTF_GlyphString otf_gstring; |
| 1712 | |
| 1713 | static void |
| 1714 | setup_otf_gstring (int size) |
| 1715 | { |
| 1716 | if (otf_gstring.size == 0) |
| 1717 | { |
| 1718 | otf_gstring.glyphs = (OTF_Glyph *) xmalloc (sizeof (OTF_Glyph) * size); |
| 1719 | otf_gstring.size = size; |
| 1720 | } |
| 1721 | else if (otf_gstring.size < size) |
| 1722 | { |
| 1723 | otf_gstring.glyphs = xrealloc (otf_gstring.glyphs, |
| 1724 | sizeof (OTF_Glyph) * size); |
| 1725 | otf_gstring.size = size; |
| 1726 | } |
| 1727 | otf_gstring.used = size; |
| 1728 | memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * size); |
| 1729 | } |
| 1730 | |
| 1731 | |
| 1732 | static int |
| 1733 | ftfont_drive_otf (font, spec, in, from, to, out, adjustment) |
| 1734 | MFLTFont *font; |
| 1735 | MFLTOtfSpec *spec; |
| 1736 | MFLTGlyphString *in; |
| 1737 | int from, to; |
| 1738 | MFLTGlyphString *out; |
| 1739 | MFLTGlyphAdjustment *adjustment; |
| 1740 | { |
| 1741 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; |
| 1742 | FT_Face ft_face = flt_font_ft->ft_face; |
| 1743 | OTF *otf = flt_font_ft->otf; |
| 1744 | int len = to - from; |
| 1745 | int i, j, gidx; |
| 1746 | OTF_Glyph *otfg; |
| 1747 | char script[5], *langsys = NULL; |
| 1748 | char *gsub_features = NULL, *gpos_features = NULL; |
| 1749 | |
| 1750 | if (len == 0) |
| 1751 | return from; |
| 1752 | OTF_tag_name (spec->script, script); |
| 1753 | if (spec->langsys) |
| 1754 | { |
| 1755 | langsys = alloca (5); |
| 1756 | OTF_tag_name (spec->langsys, langsys); |
| 1757 | } |
| 1758 | for (i = 0; i < 2; i++) |
| 1759 | { |
| 1760 | char *p; |
| 1761 | |
| 1762 | if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF) |
| 1763 | { |
| 1764 | for (j = 0; spec->features[i][j]; j++); |
| 1765 | if (i == 0) |
| 1766 | p = gsub_features = alloca (6 * j); |
| 1767 | else |
| 1768 | p = gpos_features = alloca (6 * j); |
| 1769 | for (j = 0; spec->features[i][j]; j++) |
| 1770 | { |
| 1771 | if (spec->features[i][j] == 0xFFFFFFFF) |
| 1772 | *p++ = '*', *p++ = ','; |
| 1773 | else |
| 1774 | { |
| 1775 | OTF_tag_name (spec->features[i][j], p); |
| 1776 | p[4] = ','; |
| 1777 | p += 5; |
| 1778 | } |
| 1779 | } |
| 1780 | *--p = '\0'; |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | setup_otf_gstring (len); |
| 1785 | for (i = 0; i < len; i++) |
| 1786 | { |
| 1787 | otf_gstring.glyphs[i].c = in->glyphs[from + i].c; |
| 1788 | otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code; |
| 1789 | } |
| 1790 | |
| 1791 | OTF_drive_gdef (otf, &otf_gstring); |
| 1792 | gidx = out->used; |
| 1793 | |
| 1794 | if (gsub_features) |
| 1795 | { |
| 1796 | if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features) |
| 1797 | < 0) |
| 1798 | goto simple_copy; |
| 1799 | if (out->allocated < out->used + otf_gstring.used) |
| 1800 | return -2; |
| 1801 | for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; ) |
| 1802 | { |
| 1803 | MFLTGlyph *g; |
| 1804 | int min_from, max_to; |
| 1805 | int j; |
| 1806 | |
| 1807 | g = out->glyphs + out->used; |
| 1808 | *g = in->glyphs[from + otfg->f.index.from]; |
| 1809 | if (g->code != otfg->glyph_id) |
| 1810 | { |
| 1811 | g->c = 0; |
| 1812 | g->code = otfg->glyph_id; |
| 1813 | g->measured = 0; |
| 1814 | } |
| 1815 | out->used++; |
| 1816 | min_from = g->from; |
| 1817 | max_to = g->to; |
| 1818 | if (otfg->f.index.from < otfg->f.index.to) |
| 1819 | { |
| 1820 | /* OTFG substitutes multiple glyphs in IN. */ |
| 1821 | for (j = from + otfg->f.index.from + 1; |
| 1822 | j <= from + otfg->f.index.to; j++) |
| 1823 | { |
| 1824 | if (min_from > in->glyphs[j].from) |
| 1825 | min_from = in->glyphs[j].from; |
| 1826 | if (max_to < in->glyphs[j].to) |
| 1827 | max_to = in->glyphs[j].to; |
| 1828 | } |
| 1829 | g->from = min_from; |
| 1830 | g->to = max_to; |
| 1831 | } |
| 1832 | for (i++, otfg++; (i < otf_gstring.used |
| 1833 | && otfg->f.index.from == otfg[-1].f.index.from); |
| 1834 | i++, otfg++) |
| 1835 | { |
| 1836 | g = out->glyphs + out->used; |
| 1837 | *g = in->glyphs[from + otfg->f.index.to]; |
| 1838 | if (g->code != otfg->glyph_id) |
| 1839 | { |
| 1840 | g->c = 0; |
| 1841 | g->code = otfg->glyph_id; |
| 1842 | g->measured = 0; |
| 1843 | } |
| 1844 | out->used++; |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | else |
| 1849 | { |
| 1850 | if (out->allocated < out->used + len) |
| 1851 | return -2; |
| 1852 | for (i = 0; i < len; i++) |
| 1853 | out->glyphs[out->used++] = in->glyphs[from + i]; |
| 1854 | } |
| 1855 | |
| 1856 | if (gpos_features) |
| 1857 | { |
| 1858 | MFLTGlyph *base = NULL, *mark = NULL, *g; |
| 1859 | int x_ppem, y_ppem, x_scale, y_scale; |
| 1860 | |
| 1861 | if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features) |
| 1862 | < 0) |
| 1863 | return to; |
| 1864 | |
| 1865 | x_ppem = ft_face->size->metrics.x_ppem; |
| 1866 | y_ppem = ft_face->size->metrics.y_ppem; |
| 1867 | x_scale = ft_face->size->metrics.x_scale; |
| 1868 | y_scale = ft_face->size->metrics.y_scale; |
| 1869 | |
| 1870 | for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx; |
| 1871 | i < otf_gstring.used; i++, otfg++, g++) |
| 1872 | { |
| 1873 | MFLTGlyph *prev; |
| 1874 | |
| 1875 | if (! otfg->glyph_id) |
| 1876 | continue; |
| 1877 | switch (otfg->positioning_type) |
| 1878 | { |
| 1879 | case 0: |
| 1880 | break; |
| 1881 | case 1: /* Single */ |
| 1882 | case 2: /* Pair */ |
| 1883 | { |
| 1884 | int format = otfg->f.f1.format; |
| 1885 | |
| 1886 | if (format & OTF_XPlacement) |
| 1887 | adjustment[i].xoff |
| 1888 | = otfg->f.f1.value->XPlacement * x_scale / 0x10000; |
| 1889 | if (format & OTF_XPlaDevice) |
| 1890 | adjustment[i].xoff |
| 1891 | += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem); |
| 1892 | if (format & OTF_YPlacement) |
| 1893 | adjustment[i].yoff |
| 1894 | = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000); |
| 1895 | if (format & OTF_YPlaDevice) |
| 1896 | adjustment[i].yoff |
| 1897 | -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem); |
| 1898 | if (format & OTF_XAdvance) |
| 1899 | adjustment[i].xadv |
| 1900 | += otfg->f.f1.value->XAdvance * x_scale / 0x10000; |
| 1901 | if (format & OTF_XAdvDevice) |
| 1902 | adjustment[i].xadv |
| 1903 | += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem); |
| 1904 | if (format & OTF_YAdvance) |
| 1905 | adjustment[i].yadv |
| 1906 | += otfg->f.f1.value->YAdvance * y_scale / 0x10000; |
| 1907 | if (format & OTF_YAdvDevice) |
| 1908 | adjustment[i].yadv |
| 1909 | += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem); |
| 1910 | adjustment[i].set = 1; |
| 1911 | } |
| 1912 | break; |
| 1913 | case 3: /* Cursive */ |
| 1914 | /* Not yet supported. */ |
| 1915 | break; |
| 1916 | case 4: /* Mark-to-Base */ |
| 1917 | case 5: /* Mark-to-Ligature */ |
| 1918 | if (! base) |
| 1919 | break; |
| 1920 | prev = base; |
| 1921 | goto label_adjust_anchor; |
| 1922 | default: /* i.e. case 6 Mark-to-Mark */ |
| 1923 | if (! mark) |
| 1924 | break; |
| 1925 | prev = mark; |
| 1926 | |
| 1927 | label_adjust_anchor: |
| 1928 | { |
| 1929 | int base_x, base_y, mark_x, mark_y; |
| 1930 | int this_from, this_to; |
| 1931 | |
| 1932 | base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000; |
| 1933 | base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000; |
| 1934 | mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000; |
| 1935 | mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000; |
| 1936 | |
| 1937 | if (otfg->f.f4.base_anchor->AnchorFormat != 1) |
| 1938 | adjust_anchor (ft_face, otfg->f.f4.base_anchor, |
| 1939 | prev->code, x_ppem, y_ppem, &base_x, &base_y); |
| 1940 | if (otfg->f.f4.mark_anchor->AnchorFormat != 1) |
| 1941 | adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code, |
| 1942 | x_ppem, y_ppem, &mark_x, &mark_y); |
| 1943 | adjustment[i].xoff = (base_x - mark_x); |
| 1944 | adjustment[i].yoff = - (base_y - mark_y); |
| 1945 | adjustment[i].back = (g - prev); |
| 1946 | adjustment[i].xadv = 0; |
| 1947 | adjustment[i].advance_is_absolute = 1; |
| 1948 | adjustment[i].set = 1; |
| 1949 | this_from = g->from; |
| 1950 | this_to = g->to; |
| 1951 | for (j = 0; prev + j < g; j++) |
| 1952 | { |
| 1953 | if (this_from > prev[j].from) |
| 1954 | this_from = prev[j].from; |
| 1955 | if (this_to < prev[j].to) |
| 1956 | this_to = prev[j].to; |
| 1957 | } |
| 1958 | for (; prev <= g; prev++) |
| 1959 | { |
| 1960 | prev->from = this_from; |
| 1961 | prev->to = this_to; |
| 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | if (otfg->GlyphClass == OTF_GlyphClass0) |
| 1966 | base = mark = g; |
| 1967 | else if (otfg->GlyphClass == OTF_GlyphClassMark) |
| 1968 | mark = g; |
| 1969 | else |
| 1970 | base = g; |
| 1971 | } |
| 1972 | } |
| 1973 | return to; |
| 1974 | |
| 1975 | simple_copy: |
| 1976 | if (out->allocated < out->used + len) |
| 1977 | return -2; |
| 1978 | font->get_metrics (font, in, from, to); |
| 1979 | memcpy (out->glyphs + out->used, in->glyphs + from, |
| 1980 | sizeof (MFLTGlyph) * len); |
| 1981 | out->used += len; |
| 1982 | return to; |
| 1983 | } |
| 1984 | |
| 1985 | static MFLTGlyphString gstring; |
| 1986 | |
| 1987 | static int m17n_flt_initialized; |
| 1988 | |
| 1989 | extern Lisp_Object QCfamily; |
| 1990 | |
| 1991 | static Lisp_Object |
| 1992 | ftfont_shape_by_flt (lgstring, font, ft_face, otf) |
| 1993 | Lisp_Object lgstring; |
| 1994 | struct font *font; |
| 1995 | FT_Face ft_face; |
| 1996 | OTF *otf; |
| 1997 | { |
| 1998 | EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring); |
| 1999 | EMACS_UINT i; |
| 2000 | struct MFLTFontFT flt_font_ft; |
| 2001 | MFLT *flt = NULL; |
| 2002 | int with_variation_selector = 0; |
| 2003 | |
| 2004 | if (! m17n_flt_initialized) |
| 2005 | { |
| 2006 | M17N_INIT (); |
| 2007 | m17n_flt_initialized = 1; |
| 2008 | } |
| 2009 | |
| 2010 | for (i = 0; i < len; i++) |
| 2011 | { |
| 2012 | Lisp_Object g = LGSTRING_GLYPH (lgstring, i); |
| 2013 | int c; |
| 2014 | |
| 2015 | if (NILP (g)) |
| 2016 | break; |
| 2017 | c = LGLYPH_CHAR (g); |
| 2018 | if (CHAR_VARIATION_SELECTOR_P (c)) |
| 2019 | with_variation_selector++; |
| 2020 | } |
| 2021 | len = i; |
| 2022 | if (with_variation_selector) |
| 2023 | { |
| 2024 | setup_otf_gstring (len); |
| 2025 | for (i = 0; i < len; i++) |
| 2026 | { |
| 2027 | Lisp_Object g = LGSTRING_GLYPH (lgstring, i); |
| 2028 | |
| 2029 | otf_gstring.glyphs[i].c = LGLYPH_CHAR (g); |
| 2030 | otf_gstring.glyphs[i].f.index.from = LGLYPH_FROM (g); |
| 2031 | otf_gstring.glyphs[i].f.index.to = LGLYPH_TO (g); |
| 2032 | } |
| 2033 | OTF_drive_cmap (otf, &otf_gstring); |
| 2034 | for (i = 0; i < otf_gstring.used; i++) |
| 2035 | { |
| 2036 | OTF_Glyph *otfg = otf_gstring.glyphs + i; |
| 2037 | Lisp_Object g0 = LGSTRING_GLYPH (lgstring, otfg->f.index.from); |
| 2038 | Lisp_Object g1 = LGSTRING_GLYPH (lgstring, otfg->f.index.to); |
| 2039 | |
| 2040 | LGLYPH_SET_CODE (g0, otfg->glyph_id); |
| 2041 | LGLYPH_SET_TO (g0, LGLYPH_TO (g1)); |
| 2042 | LGSTRING_SET_GLYPH (lgstring, i, g0); |
| 2043 | } |
| 2044 | if (len > otf_gstring.used) |
| 2045 | { |
| 2046 | len = otf_gstring.used; |
| 2047 | LGSTRING_SET_GLYPH (lgstring, len, Qnil); |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | if (gstring.allocated == 0) |
| 2052 | { |
| 2053 | gstring.allocated = len * 2; |
| 2054 | gstring.glyph_size = sizeof (MFLTGlyph); |
| 2055 | gstring.glyphs = xmalloc (sizeof (MFLTGlyph) * gstring.allocated); |
| 2056 | } |
| 2057 | else if (gstring.allocated < len * 2) |
| 2058 | { |
| 2059 | gstring.allocated = len * 2; |
| 2060 | gstring.glyphs = xrealloc (gstring.glyphs, |
| 2061 | sizeof (MFLTGlyph) * gstring.allocated); |
| 2062 | } |
| 2063 | memset (gstring.glyphs, 0, sizeof (MFLTGlyph) * len); |
| 2064 | for (i = 0; i < len; i++) |
| 2065 | { |
| 2066 | Lisp_Object g = LGSTRING_GLYPH (lgstring, i); |
| 2067 | |
| 2068 | gstring.glyphs[i].c = LGLYPH_CHAR (g); |
| 2069 | if (with_variation_selector) |
| 2070 | { |
| 2071 | gstring.glyphs[i].code = LGLYPH_CODE (g); |
| 2072 | gstring.glyphs[i].encoded = 1; |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | gstring.used = len; |
| 2077 | gstring.r2l = 0; |
| 2078 | |
| 2079 | { |
| 2080 | Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily); |
| 2081 | |
| 2082 | if (NILP (family)) |
| 2083 | flt_font_ft.flt_font.family = Mnil; |
| 2084 | else |
| 2085 | flt_font_ft.flt_font.family |
| 2086 | = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family)))); |
| 2087 | } |
| 2088 | flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem; |
| 2089 | flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem; |
| 2090 | flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id; |
| 2091 | flt_font_ft.flt_font.get_metrics = ftfont_get_metrics; |
| 2092 | flt_font_ft.flt_font.check_otf = ftfont_check_otf; |
| 2093 | flt_font_ft.flt_font.drive_otf = ftfont_drive_otf; |
| 2094 | flt_font_ft.flt_font.internal = NULL; |
| 2095 | flt_font_ft.font = font; |
| 2096 | flt_font_ft.ft_face = ft_face; |
| 2097 | flt_font_ft.otf = otf; |
| 2098 | if (len > 1 |
| 2099 | && gstring.glyphs[1].c >= 0x300 && gstring.glyphs[1].c <= 0x36F) |
| 2100 | /* A little bit ad hoc. Perhaps, shaper must get script and |
| 2101 | language information, and select a proper flt for them |
| 2102 | here. */ |
| 2103 | flt = mflt_get (msymbol ("combining")); |
| 2104 | for (i = 0; i < 3; i++) |
| 2105 | { |
| 2106 | int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, flt); |
| 2107 | if (result != -2) |
| 2108 | break; |
| 2109 | gstring.allocated += gstring.allocated; |
| 2110 | gstring.glyphs = xrealloc (gstring.glyphs, |
| 2111 | sizeof (MFLTGlyph) * gstring.allocated); |
| 2112 | } |
| 2113 | if (gstring.used > LGSTRING_GLYPH_LEN (lgstring)) |
| 2114 | return Qnil; |
| 2115 | for (i = 0; i < gstring.used; i++) |
| 2116 | { |
| 2117 | MFLTGlyph *g = gstring.glyphs + i; |
| 2118 | |
| 2119 | g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from)); |
| 2120 | g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to)); |
| 2121 | } |
| 2122 | |
| 2123 | for (i = 0; i < gstring.used; i++) |
| 2124 | { |
| 2125 | Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i); |
| 2126 | MFLTGlyph *g = gstring.glyphs + i; |
| 2127 | |
| 2128 | if (NILP (lglyph)) |
| 2129 | { |
| 2130 | lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil); |
| 2131 | LGSTRING_SET_GLYPH (lgstring, i, lglyph); |
| 2132 | } |
| 2133 | LGLYPH_SET_FROM (lglyph, g->from); |
| 2134 | LGLYPH_SET_TO (lglyph, g->to); |
| 2135 | LGLYPH_SET_CHAR (lglyph, g->c); |
| 2136 | LGLYPH_SET_CODE (lglyph, g->code); |
| 2137 | LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6); |
| 2138 | LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6); |
| 2139 | LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6); |
| 2140 | LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6); |
| 2141 | LGLYPH_SET_DESCENT (lglyph, g->descent >> 6); |
| 2142 | if (g->adjusted) |
| 2143 | { |
| 2144 | Lisp_Object vec; |
| 2145 | |
| 2146 | vec = Fmake_vector (make_number (3), Qnil); |
| 2147 | ASET (vec, 0, make_number (g->xoff >> 6)); |
| 2148 | ASET (vec, 1, make_number (g->yoff >> 6)); |
| 2149 | ASET (vec, 2, make_number (g->xadv >> 6)); |
| 2150 | LGLYPH_SET_ADJUSTMENT (lglyph, vec); |
| 2151 | } |
| 2152 | } |
| 2153 | return make_number (i); |
| 2154 | } |
| 2155 | |
| 2156 | Lisp_Object |
| 2157 | ftfont_shape (lgstring) |
| 2158 | Lisp_Object lgstring; |
| 2159 | { |
| 2160 | struct font *font; |
| 2161 | struct ftfont_info *ftfont_info; |
| 2162 | OTF *otf; |
| 2163 | |
| 2164 | CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font); |
| 2165 | ftfont_info = (struct ftfont_info *) font; |
| 2166 | otf = ftfont_get_otf (ftfont_info); |
| 2167 | if (! otf) |
| 2168 | return make_number (0); |
| 2169 | return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf); |
| 2170 | } |
| 2171 | |
| 2172 | #endif /* HAVE_M17N_FLT */ |
| 2173 | |
| 2174 | #ifdef HAVE_OTF_GET_VARIATION_GLYPHS |
| 2175 | |
| 2176 | static int |
| 2177 | ftfont_variation_glyphs (font, c, variations) |
| 2178 | struct font *font; |
| 2179 | int c; |
| 2180 | unsigned variations[256]; |
| 2181 | { |
| 2182 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; |
| 2183 | OTF *otf = ftfont_get_otf (ftfont_info); |
| 2184 | |
| 2185 | if (! otf) |
| 2186 | return 0; |
| 2187 | return OTF_get_variation_glyphs (otf, c, variations); |
| 2188 | } |
| 2189 | |
| 2190 | #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */ |
| 2191 | #endif /* HAVE_LIBOTF */ |
| 2192 | |
| 2193 | Lisp_Object |
| 2194 | ftfont_font_format (FcPattern *pattern, Lisp_Object filename) |
| 2195 | { |
| 2196 | FcChar8 *str; |
| 2197 | |
| 2198 | #ifdef FC_FONTFORMAT |
| 2199 | if (pattern) |
| 2200 | { |
| 2201 | if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch) |
| 2202 | return Qnil; |
| 2203 | if (strcmp ((char *) str, "TrueType") == 0) |
| 2204 | return intern ("truetype"); |
| 2205 | if (strcmp ((char *) str, "Type 1") == 0) |
| 2206 | return intern ("type1"); |
| 2207 | if (strcmp ((char *) str, "PCF") == 0) |
| 2208 | return intern ("pcf"); |
| 2209 | if (strcmp ((char *) str, "BDF") == 0) |
| 2210 | return intern ("bdf"); |
| 2211 | } |
| 2212 | #endif /* FC_FONTFORMAT */ |
| 2213 | if (STRINGP (filename)) |
| 2214 | { |
| 2215 | int len = SBYTES (filename); |
| 2216 | |
| 2217 | if (len >= 4) |
| 2218 | { |
| 2219 | str = (FcChar8 *) (SDATA (filename) + len - 4); |
| 2220 | if (xstrcasecmp ((char *) str, ".ttf") == 0) |
| 2221 | return intern ("truetype"); |
| 2222 | if (xstrcasecmp ((char *) str, ".pfb") == 0) |
| 2223 | return intern ("type1"); |
| 2224 | if (xstrcasecmp ((char *) str, ".pcf") == 0) |
| 2225 | return intern ("pcf"); |
| 2226 | if (xstrcasecmp ((char *) str, ".bdf") == 0) |
| 2227 | return intern ("bdf"); |
| 2228 | } |
| 2229 | } |
| 2230 | return intern ("unknown"); |
| 2231 | } |
| 2232 | |
| 2233 | static const char *ftfont_booleans [] = { |
| 2234 | ":antialias", |
| 2235 | ":hinting", |
| 2236 | ":verticallayout", |
| 2237 | ":autohint", |
| 2238 | ":globaladvance", |
| 2239 | ":outline", |
| 2240 | ":scalable", |
| 2241 | ":minspace", |
| 2242 | ":embolden", |
| 2243 | NULL, |
| 2244 | }; |
| 2245 | |
| 2246 | static const char *ftfont_non_booleans [] = { |
| 2247 | ":family", |
| 2248 | ":familylang", |
| 2249 | ":style", |
| 2250 | ":stylelang", |
| 2251 | ":fullname", |
| 2252 | ":fullnamelang", |
| 2253 | ":slant", |
| 2254 | ":weight", |
| 2255 | ":size", |
| 2256 | ":width", |
| 2257 | ":aspect", |
| 2258 | ":pixelsize", |
| 2259 | ":spacing", |
| 2260 | ":foundry", |
| 2261 | ":hintstyle", |
| 2262 | ":file", |
| 2263 | ":index", |
| 2264 | ":ftface", |
| 2265 | ":rasterizer", |
| 2266 | ":scale", |
| 2267 | ":dpi", |
| 2268 | ":rgba", |
| 2269 | ":lcdfilter", |
| 2270 | ":charset", |
| 2271 | ":lang", |
| 2272 | ":fontversion", |
| 2273 | ":capability", |
| 2274 | NULL, |
| 2275 | }; |
| 2276 | |
| 2277 | static void |
| 2278 | ftfont_filter_properties (font, alist) |
| 2279 | Lisp_Object font; |
| 2280 | Lisp_Object alist; |
| 2281 | { |
| 2282 | Lisp_Object it; |
| 2283 | int i; |
| 2284 | |
| 2285 | /* Set boolean values to Qt or Qnil */ |
| 2286 | for (i = 0; ftfont_booleans[i] != NULL; ++i) |
| 2287 | for (it = alist; ! NILP (it); it = XCDR (it)) |
| 2288 | { |
| 2289 | Lisp_Object key = XCAR (XCAR (it)); |
| 2290 | Lisp_Object val = XCDR (XCAR (it)); |
| 2291 | char *keystr = SDATA (SYMBOL_NAME (key)); |
| 2292 | |
| 2293 | if (strcmp (ftfont_booleans[i], keystr) == 0) |
| 2294 | { |
| 2295 | char *str = SYMBOLP (val) ? SDATA (SYMBOL_NAME (val)) : NULL; |
| 2296 | if (INTEGERP (val)) str = XINT (val) != 0 ? "true" : "false"; |
| 2297 | if (str == NULL) str = "true"; |
| 2298 | |
| 2299 | val = Qt; |
| 2300 | if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0 |
| 2301 | || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0 |
| 2302 | || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0 |
| 2303 | || strcmp ("Off", str) == 0) |
| 2304 | val = Qnil; |
| 2305 | Ffont_put (font, key, val); |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | for (i = 0; ftfont_non_booleans[i] != NULL; ++i) |
| 2310 | for (it = alist; ! NILP (it); it = XCDR (it)) |
| 2311 | { |
| 2312 | Lisp_Object key = XCAR (XCAR (it)); |
| 2313 | Lisp_Object val = XCDR (XCAR (it)); |
| 2314 | char *keystr = SDATA (SYMBOL_NAME (key)); |
| 2315 | if (strcmp (ftfont_non_booleans[i], keystr) == 0) |
| 2316 | Ffont_put (font, key, val); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | void |
| 2322 | syms_of_ftfont () |
| 2323 | { |
| 2324 | DEFSYM (Qfreetype, "freetype"); |
| 2325 | DEFSYM (Qmonospace, "monospace"); |
| 2326 | DEFSYM (Qsans_serif, "sans-serif"); |
| 2327 | DEFSYM (Qserif, "serif"); |
| 2328 | DEFSYM (Qmono, "mono"); |
| 2329 | DEFSYM (Qsans, "sans"); |
| 2330 | DEFSYM (Qsans__serif, "sans serif"); |
| 2331 | |
| 2332 | staticpro (&freetype_font_cache); |
| 2333 | freetype_font_cache = Fcons (Qt, Qnil); |
| 2334 | |
| 2335 | staticpro (&ftfont_generic_family_list); |
| 2336 | ftfont_generic_family_list |
| 2337 | = Fcons (Fcons (Qmonospace, Qt), |
| 2338 | Fcons (Fcons (Qsans_serif, Qt), |
| 2339 | Fcons (Fcons (Qsans, Qt), Qnil))); |
| 2340 | |
| 2341 | staticpro (&ft_face_cache); |
| 2342 | ft_face_cache = Qnil; |
| 2343 | |
| 2344 | ftfont_driver.type = Qfreetype; |
| 2345 | register_font_driver (&ftfont_driver, NULL); |
| 2346 | } |
| 2347 | |
| 2348 | /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca |
| 2349 | (do not change this comment) */ |