aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKenichi Handa2009-05-21 11:23:41 +0000
committerKenichi Handa2009-05-21 11:23:41 +0000
commit5a189ffa3f87c470840752ea8d3a188f217c31c6 (patch)
treecebacc90c89ab887224d451f0d89dbfe27164203 /src
parentd5a8ed105306de407d9a3d7b55730e013ace47b6 (diff)
downloademacs-5a189ffa3f87c470840752ea8d3a188f217c31c6.tar.gz
emacs-5a189ffa3f87c470840752ea8d3a188f217c31c6.zip
(xfont_chars_supported, xfont_supported_scripts): New
functions. (xfont_scripts_cache, xfont_scratch_props): New variables. (Qlatin, Vscalable_fonts_allowed): Extern it. (xfont_list_pattern): Argument changed. Callers changed. Check Vscalable_fonts_allowed. Check the support of a script. (xfont_list): Don't reject a font spec with :script property. (xfont_has_char): Fix setting of encoding. (syms_of_xfont): Staticpro and initialize xfont_scripts_cache and xfont_scratch_props.
Diffstat (limited to 'src')
-rw-r--r--src/xfont.c279
1 files changed, 231 insertions, 48 deletions
diff --git a/src/xfont.c b/src/xfont.c
index afafca89c24..87455bb504a 100644
--- a/src/xfont.c
+++ b/src/xfont.c
@@ -256,20 +256,167 @@ xfont_encode_coding_xlfd (char *xlfd)
256 return len; 256 return len;
257} 257}
258 258
259static Lisp_Object xfont_list_pattern P_ ((Lisp_Object, Display *, char *)); 259/* Check if CHARS (cons or vector) is supported by XFONT whose
260 encoding charset is ENCODING (XFONT is NULL) or by a font whose
261 registry corresponds to ENCODING and REPERTORY.
262 Return 1 if supported, return 0 otherwise. */
263
264static int
265xfont_chars_supported (Lisp_Object chars, XFontStruct *xfont,
266 struct charset *encoding, struct charset *repertory)
267{
268 struct charset *charset = repertory ? repertory : encoding;
269
270 if (CONSP (chars))
271 {
272 for (; CONSP (chars); chars = XCDR (chars))
273 {
274 int c = XINT (XCAR (chars));
275 unsigned code = ENCODE_CHAR (charset, c);
276 XChar2b char2b;
277
278 if (code == CHARSET_INVALID_CODE (charset))
279 break;
280 if (! xfont)
281 continue;
282 if (code >= 0x10000)
283 break;
284 char2b.byte1 = code >> 8;
285 char2b.byte2 = code & 0xFF;
286 if (! xfont_get_pcm (xfont, &char2b))
287 break;
288 }
289 return (NILP (chars));
290 }
291 else if (VECTORP (chars))
292 {
293 int i;
294
295 for (i = ASIZE (chars) - 1; i >= 0; i--)
296 {
297 int c = XINT (AREF (chars, i));
298 unsigned code = ENCODE_CHAR (charset, c);
299 XChar2b char2b;
300
301 if (code == CHARSET_INVALID_CODE (charset))
302 continue;
303 if (! xfont)
304 break;
305 if (code >= 0x10000)
306 continue;
307 char2b.byte1 = code >> 8;
308 char2b.byte2 = code & 0xFF;
309 if (xfont_get_pcm (xfont, &char2b))
310 break;
311 }
312 return (i >= 0);
313 }
314 return 0;
315}
316
317/* A hash table recoding which font supports which scritps. Each key
318 is a vector of characteristic font propertis FOUNDRY to WIDTH and
319 ADDSTYLE, and each value is a list of script symbols.
320
321 We assume that fonts that have the same value in the above
322 properties supports the same set of characters on all displays. */
323
324static Lisp_Object xfont_scripts_cache;
325
326/* Re-usable vector to store characteristic font properites. */
327static Lisp_Object xfont_scratch_props;
328
329extern Lisp_Object Qlatin;
330
331/* Return a list of scripts supported by the font of FONTNAME whose
332 characteristic properties are in PROPS and whose encoding charset
333 is ENCODING. A caller must call BLOCK_INPUT in advance. */
260 334
261static Lisp_Object 335static Lisp_Object
262xfont_list_pattern (frame, display, pattern) 336xfont_supported_scripts (Display *display, char *fontname, Lisp_Object props,
263 Lisp_Object frame; 337 struct charset *encoding)
264 Display *display; 338{
265 char *pattern; 339 Lisp_Object scripts;
340
341 /* Two special cases to avoid opening rather big fonts. */
342 if (EQ (AREF (props, 2), Qja))
343 return Fcons (intern ("kana"), Fcons (intern ("han"), Qnil));
344 if (EQ (AREF (props, 2), Qko))
345 return Fcons (intern ("hangul"), Qnil);
346 scripts = Fgethash (props, xfont_scripts_cache, Qt);
347 if (EQ (scripts, Qt))
348 {
349 XFontStruct *xfont;
350 Lisp_Object val;
351
352 scripts = Qnil;
353 xfont = XLoadQueryFont (display, fontname);
354 if (xfont)
355 {
356 if (xfont->per_char)
357 {
358 for (val = Vscript_representative_chars; CONSP (val);
359 val = XCDR (val))
360 if (CONSP (XCAR (val)) && SYMBOLP (XCAR (XCAR (val))))
361 {
362 Lisp_Object script = XCAR (XCAR (val));
363 Lisp_Object chars = XCDR (XCAR (val));
364
365 if (xfont_chars_supported (chars, xfont, encoding, NULL))
366 scripts = Fcons (script, scripts);
367 }
368 }
369 XFreeFont (display, xfont);
370 }
371 if (EQ (AREF (props, 3), Qiso10646_1)
372 && NILP (Fmemq (Qlatin, scripts)))
373 scripts = Fcons (Qlatin, scripts);
374 Fputhash (Fcopy_sequence (props), scripts, xfont_scripts_cache);
375 }
376 return scripts;
377}
378
379extern Lisp_Object Vscalable_fonts_allowed;
380
381static Lisp_Object
382xfont_list_pattern (Display *display, char *pattern,
383 Lisp_Object registry, Lisp_Object script)
266{ 384{
267 Lisp_Object list = Qnil; 385 Lisp_Object list = Qnil;
386 Lisp_Object chars = Qnil;
387 struct charset *encoding, *repertory = NULL;
268 int i, limit, num_fonts; 388 int i, limit, num_fonts;
269 char **names; 389 char **names;
270 /* Large enough to decode the longest XLFD (255 bytes). */ 390 /* Large enough to decode the longest XLFD (255 bytes). */
271 char buf[512]; 391 char buf[512];
272 392
393 if (! NILP (registry)
394 && font_registry_charsets (registry, &encoding, &repertory) < 0)
395 /* Unknown REGISTRY, not supported. */
396 return Qnil;
397 if (! NILP (script))
398 {
399 chars = assq_no_quit (script, Vscript_representative_chars);
400 if (NILP (chars))
401 /* We can't tell whether or not a font supports SCRIPT. */
402 return Qnil;
403 chars = XCDR (chars);
404 if (repertory)
405 {
406 if (! xfont_chars_supported (chars, NULL, encoding, repertory))
407 return Qnil;
408 script = Qnil;
409 }
410 }
411 if (! repertory && NILP (xfont_scripts_cache))
412 {
413 Lisp_Object args[2];
414
415 args[0] = QCtest;
416 args[1] = Qequal;
417 xfont_scripts_cache = Fmake_hash_table (2, args);
418 }
419
273 BLOCK_INPUT; 420 BLOCK_INPUT;
274 x_catch_errors (display); 421 x_catch_errors (display);
275 422
@@ -292,7 +439,20 @@ xfont_list_pattern (frame, display, pattern)
292 if (num_fonts > 0) 439 if (num_fonts > 0)
293 { 440 {
294 char **indices = alloca (sizeof (char *) * num_fonts); 441 char **indices = alloca (sizeof (char *) * num_fonts);
442 Lisp_Object *props;
443 Lisp_Object scripts = Qnil;
295 444
445 if (NILP (xfont_scratch_props))
446 {
447 xfont_scratch_props = Fmake_vector (make_number (8), Qnil);
448 props = XVECTOR (xfont_scratch_props)->contents;
449 }
450 else
451 {
452 props = XVECTOR (xfont_scratch_props)->contents;
453 for (i = 0; i < 8; i++)
454 props[i] = Qnil;
455 }
296 for (i = 0; i < num_fonts; i++) 456 for (i = 0; i < num_fonts; i++)
297 indices[i] = names[i]; 457 indices[i] = names[i];
298 qsort (indices, num_fonts, sizeof (char *), compare_font_names); 458 qsort (indices, num_fonts, sizeof (char *), compare_font_names);
@@ -300,47 +460,68 @@ xfont_list_pattern (frame, display, pattern)
300 for (i = 0; i < num_fonts; i++) 460 for (i = 0; i < num_fonts; i++)
301 { 461 {
302 Lisp_Object entity; 462 Lisp_Object entity;
303 int result;
304 char *p;
305 463
306 if (i > 0 && xstrcasecmp (indices[i - 1], indices[i]) == 0) 464 if (i > 0 && xstrcasecmp (indices[i - 1], indices[i]) == 0)
307 continue; 465 continue;
308
309 entity = font_make_entity (); 466 entity = font_make_entity ();
310 ASET (entity, FONT_TYPE_INDEX, Qx);
311 xfont_decode_coding_xlfd (indices[i], -1, buf); 467 xfont_decode_coding_xlfd (indices[i], -1, buf);
312 result = font_parse_xlfd (buf, entity); 468 font_parse_xlfd (buf, entity);
313 if (result < 0) 469 ASET (entity, FONT_TYPE_INDEX, Qx);
470 /* Avoid auto-scaled fonts. */
471 if (XINT (AREF (entity, FONT_DPI_INDEX)) != 0
472 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0)
473 continue;
474 /* Avoid not-allowed scalable fonts. */
475 if (NILP (Vscalable_fonts_allowed))
314 { 476 {
315 /* This may be an alias name. Try to get the full XLFD name 477 if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0)
316 from XA_FONT property of the font. */
317 XFontStruct *font = XLoadQueryFont (display, indices[i]);
318 unsigned long value;
319
320 if (! font)
321 continue; 478 continue;
322 if (XGetFontProperty (font, XA_FONT, &value)) 479 }
480 else if (CONSP (Vscalable_fonts_allowed))
481 {
482 Lisp_Object tail, elt;
483
484 for (tail = Vscalable_fonts_allowed; CONSP (tail);
485 tail = XCDR (tail))
323 { 486 {
324 char *name = (char *) XGetAtomName (display, (Atom) value); 487 elt = XCAR (tail);
325 int len = strlen (name); 488 if (STRINGP (elt)
326 489 && fast_c_string_match_ignore_case (elt, indices[i]) >= 0)
327 /* If DXPC (a Differential X Protocol Compressor) 490 break;
328 Ver.3.7 is running, XGetAtomName will return null
329 string. We must avoid such a name. */
330 if (len > 0)
331 {
332 xfont_decode_coding_xlfd (indices[i], -1, buf);
333 result = font_parse_xlfd (buf, entity);
334 }
335 XFree (name);
336 } 491 }
337 XFreeFont (display, font); 492 if (! CONSP (tail))
493 continue;
338 } 494 }
339 495
340 if (result == 0 496 /* Update encoding and repertory if necessary. */
341 /* Avoid auto-scaled fonts. */ 497 if (! EQ (registry, AREF (entity, FONT_REGISTRY_INDEX)))
342 && (XINT (AREF (entity, FONT_DPI_INDEX)) == 0 498 {
343 || XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) > 0)) 499 registry = AREF (entity, FONT_REGISTRY_INDEX);
500 if (font_registry_charsets (registry, &encoding, &repertory) < 0)
501 encoding = NULL;
502 }
503 if (! encoding)
504 /* Unknown REGISTRY, not supported. */
505 continue;
506 if (repertory)
507 {
508 if (NILP (script)
509 || xfont_chars_supported (chars, NULL, encoding, repertory))
510 list = Fcons (entity, list);
511 continue;
512 }
513 if (memcmp (props, &(AREF (entity, FONT_FOUNDRY_INDEX)),
514 sizeof (Lisp_Object) * 7)
515 || ! EQ (AREF (entity, FONT_SPACING_INDEX), props[7]))
516 {
517 memcpy (props, &(AREF (entity, FONT_FOUNDRY_INDEX)),
518 sizeof (Lisp_Object) * 7);
519 props[7] = AREF (entity, FONT_SPACING_INDEX);
520 scripts = xfont_supported_scripts (display, indices[i],
521 xfont_scratch_props, encoding);
522 }
523 if (NILP (script)
524 || ! NILP (Fmemq (script, scripts)))
344 list = Fcons (entity, list); 525 list = Fcons (entity, list);
345 } 526 }
346 XFreeFontNames (names); 527 XFreeFontNames (names);
@@ -359,7 +540,7 @@ xfont_list (frame, spec)
359{ 540{
360 FRAME_PTR f = XFRAME (frame); 541 FRAME_PTR f = XFRAME (frame);
361 Display *display = FRAME_X_DISPLAY_INFO (f)->display; 542 Display *display = FRAME_X_DISPLAY_INFO (f)->display;
362 Lisp_Object registry, list, val, extra; 543 Lisp_Object registry, list, val, extra, script;
363 int len; 544 int len;
364 /* Large enough to contain the longest XLFD (255 bytes) in UTF-8. */ 545 /* Large enough to contain the longest XLFD (255 bytes) in UTF-8. */
365 char name[512]; 546 char name[512];
@@ -370,9 +551,6 @@ xfont_list (frame, spec)
370 val = assq_no_quit (QCotf, extra); 551 val = assq_no_quit (QCotf, extra);
371 if (! NILP (val)) 552 if (! NILP (val))
372 return Qnil; 553 return Qnil;
373 val = assq_no_quit (QCscript, extra);
374 if (! NILP (val))
375 return Qnil;
376 val = assq_no_quit (QClang, extra); 554 val = assq_no_quit (QClang, extra);
377 if (! NILP (val)) 555 if (! NILP (val))
378 return Qnil; 556 return Qnil;
@@ -382,8 +560,10 @@ xfont_list (frame, spec)
382 len = font_unparse_xlfd (spec, 0, name, 512); 560 len = font_unparse_xlfd (spec, 0, name, 512);
383 if (len < 0 || (len = xfont_encode_coding_xlfd (name)) < 0) 561 if (len < 0 || (len = xfont_encode_coding_xlfd (name)) < 0)
384 return Qnil; 562 return Qnil;
385 ASET (spec, FONT_REGISTRY_INDEX, registry); 563
386 list = xfont_list_pattern (frame, display, name); 564 val = assq_no_quit (QCscript, extra);
565 script = CDR (val);
566 list = xfont_list_pattern (display, name, registry, script);
387 if (NILP (list) && NILP (registry)) 567 if (NILP (list) && NILP (registry))
388 { 568 {
389 /* Try iso10646-1 */ 569 /* Try iso10646-1 */
@@ -392,7 +572,7 @@ xfont_list (frame, spec)
392 if (r - name + 10 < 256) /* 10 == strlen (iso10646-1) */ 572 if (r - name + 10 < 256) /* 10 == strlen (iso10646-1) */
393 { 573 {
394 strcpy (r, "iso10646-1"); 574 strcpy (r, "iso10646-1");
395 list = xfont_list_pattern (frame, display, name); 575 list = xfont_list_pattern (display, name, Qiso10646_1, script);
396 } 576 }
397 } 577 }
398 if (NILP (list) && ! NILP (registry)) 578 if (NILP (list) && ! NILP (registry))
@@ -412,7 +592,7 @@ xfont_list (frame, spec)
412 && ((r - name) + SBYTES (XCAR (alter))) < 256) 592 && ((r - name) + SBYTES (XCAR (alter))) < 256)
413 { 593 {
414 strcpy (r, (char *) SDATA (XCAR (alter))); 594 strcpy (r, (char *) SDATA (XCAR (alter)));
415 list = xfont_list_pattern (frame, display, name); 595 list = xfont_list_pattern (display, name, registry, script);
416 if (! NILP (list)) 596 if (! NILP (list))
417 break; 597 break;
418 } 598 }
@@ -427,7 +607,7 @@ xfont_list (frame, spec)
427 bcopy (SDATA (XCDR (val)), name, SBYTES (XCDR (val)) + 1); 607 bcopy (SDATA (XCDR (val)), name, SBYTES (XCDR (val)) + 1);
428 if (xfont_encode_coding_xlfd (name) < 0) 608 if (xfont_encode_coding_xlfd (name) < 0)
429 return Qnil; 609 return Qnil;
430 list = xfont_list_pattern (frame, display, name); 610 list = xfont_list_pattern (display, name, registry, script);
431 } 611 }
432 } 612 }
433 613
@@ -803,16 +983,15 @@ xfont_has_char (font, c)
803 983
804 if (EQ (registry, Qiso10646_1)) 984 if (EQ (registry, Qiso10646_1))
805 { 985 {
986 encoding = CHARSET_FROM_ID (charset_unicode);
806 /* We use a font of `ja' and `ko' adstyle only for a character 987 /* We use a font of `ja' and `ko' adstyle only for a character
807 in JISX0208 and KSC5601 charsets respectively. */ 988 in JISX0208 and KSC5601 charsets respectively. */
808 if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qja) 989 if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qja)
809 && charset_jisx0208 >= 0) 990 && charset_jisx0208 >= 0)
810 encoding = repertory = CHARSET_FROM_ID (charset_jisx0208); 991 repertory = CHARSET_FROM_ID (charset_jisx0208);
811 else if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qko) 992 else if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qko)
812 && charset_ksc5601 >= 0) 993 && charset_ksc5601 >= 0)
813 encoding = repertory = CHARSET_FROM_ID (charset_ksc5601); 994 repertory = CHARSET_FROM_ID (charset_ksc5601);
814 else
815 encoding = CHARSET_FROM_ID (charset_unicode);
816 } 995 }
817 else if (font_registry_charsets (registry, &encoding, &repertory) < 0) 996 else if (font_registry_charsets (registry, &encoding, &repertory) < 0)
818 /* Unknown REGISTRY, not usable. */ 997 /* Unknown REGISTRY, not usable. */
@@ -996,6 +1175,10 @@ xfont_check (f, font)
996void 1175void
997syms_of_xfont () 1176syms_of_xfont ()
998{ 1177{
1178 staticpro (&xfont_scripts_cache);
1179 xfont_scripts_cache = Qnil;
1180 staticpro (&xfont_scratch_props);
1181 xfont_scratch_props = Qnil;;
999 xfont_driver.type = Qx; 1182 xfont_driver.type = Qx;
1000 register_font_driver (&xfont_driver, NULL); 1183 register_font_driver (&xfont_driver, NULL);
1001} 1184}