ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtfont.C (file contents):
Revision 1.196 by sf-exg, Thu Jul 14 05:33:26 2016 UTC vs.
Revision 1.224 by root, Thu Jan 4 19:52:37 2024 UTC

134# endif 134# endif
135#endif 135#endif
136 136
137 //{ CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, // this gem of a font has actual dotted circles within the combining character glyphs. 137 //{ CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, // this gem of a font has actual dotted circles within the combining character glyphs.
138#if XFT 138#if XFT
139 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" }, 139 { CS_UNICODE, "xft:DejaVu Sans Mono:antialias=false:autohint=true" },
140 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, 140 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
141 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" }, 141 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
142 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" }, 142 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
143 143
144 // FreeMono is usually uglier than x fonts, so try after the others 144 // FreeMono is usually uglier than x fonts, so try after the others
145 { CS_UNICODE, "xft:FreeMono:autohint=true" }, 145 { CS_UNICODE, "xft:FreeMono:autohint=true" },
146#endif 146#endif
147 147
152 { CS_UNKNOWN, 0 } 152 { CS_UNKNOWN, 0 }
153}; 153};
154 154
155// these characters are used to guess the font height and width 155// these characters are used to guess the font height and width
156// pango uses a similar algorithm and doesn't trust the font either. 156// pango uses a similar algorithm and doesn't trust the font either.
157static uint16_t extent_test_chars[] = { 157static uint32_t extent_test_chars[] = {
158 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', 158 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
159 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ 159 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
160 0x304c, 0x672c, // が本 160 0x304c, 0x672c, // が本
161 0x1f600, // 😀
161}; 162};
162 163
163#define dTermDisplay Display *disp = term->dpy 164#define dTermDisplay Display *disp = term->dpy
164#define dTermGC GC gc = term->gc 165#define dTermGC GC gc = term->gc
165 166
262 } 263 }
263} 264}
264 265
265///////////////////////////////////////////////////////////////////////////// 266/////////////////////////////////////////////////////////////////////////////
266 267
267struct rxvt_font_default : rxvt_font { 268struct rxvt_font_default : rxvt_font
269{
268 struct rxvt_fontset *fs; 270 struct rxvt_fontset *fs;
269 271
270 rxvt_font_default (rxvt_fontset *fs) 272 rxvt_font_default (rxvt_fontset *fs)
271 : rxvt_font () 273 : rxvt_font ()
272 { 274 {
312 if (unicode >= 0x2500 && unicode <= 0x259f && 314 if (unicode >= 0x2500 && unicode <= 0x259f &&
313 !term->option (Opt_skipBuiltinGlyphs)) 315 !term->option (Opt_skipBuiltinGlyphs))
314 return true; 316 return true;
315#endif 317#endif
316 318
317 if (IS_COMPOSE (unicode)) 319 // we do not check for IS_COMPOSE here, as this would
318 return true; 320 // rob other fonts from taking over.
319 321
320 switch (unicode) 322 switch (unicode)
321 { 323 {
322 case ZERO_WIDTH_CHAR: 324 case ZERO_WIDTH_CHAR:
323 case NOCHAR: 325 case NOCHAR:
344 346
345 XSetForeground (disp, gc, term->pix_colors[fg]); 347 XSetForeground (disp, gc, term->pix_colors[fg]);
346 348
347 while (len) 349 while (len)
348 { 350 {
351 const text_t *tp = text;
352 text_t t = *tp;
353
354 while (++text, --len && *text == NOCHAR)
355 ;
356
349#if ENABLE_COMBINING 357#if ENABLE_COMBINING
350 compose_char *cc; 358 compose_char *cc;
351#endif 359#endif
352 const text_t *tp = text;
353 text_t t = *tp;
354
355 while (++text, --len && *text == NOCHAR)
356 ;
357
358 int width = text - tp; 360 int width = text - tp;
359 int fwidth = term->fwidth * width; 361 int fwidth = term->fwidth * width;
360 362
361#ifdef BUILTIN_GLYPHS 363#ifdef BUILTIN_GLYPHS
362 if (0x2500 <= t && t <= 0x259f) 364 if (0x2500 <= t && t <= 0x259f)
444#endif 446#endif
445#if ENABLE_COMBINING 447#if ENABLE_COMBINING
446 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 448 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
447 { 449 {
448 min_it (width, 2); // we only support wcwidth up to 2 450 min_it (width, 2); // we only support wcwidth up to 2
449
450 text_t chrs[2]; 451 text_t chrs[2];
451 chrs [1] = NOCHAR; 452 chrs [1] = NOCHAR;
452 453
453 *chrs = cc->c1; 454 *chrs = cc->c1;
454 rxvt_font *f1 = (*fs)[fs->find_font_idx (cc->c1)]; 455 rxvt_font *f1 = (*fs)[fs->find_font_idx (cc->c1)];
474 case '\t': 475 case '\t':
475 case ZERO_WIDTH_CHAR: 476 case ZERO_WIDTH_CHAR:
476 case NOCHAR: 477 case NOCHAR:
477 break; 478 break;
478 479
480 /*
481 * If the base font does not support variation selectors, treat them as ZWC.
482 * a point could be made to do this for all wcwidth == 0 characters, but I
483 * decided against that until more data is available.
484 */
485 case 0xfe00: case 0xfe01: case 0xfe02: case 0xfe03: case 0xfe04: case 0xfe05: case 0xfe06: case 0xfe07:
486 case 0xfe08: case 0xfe09: case 0xfe0a: case 0xfe0b: case 0xfe0c: case 0xfe0d: case 0xfe0e: case 0xfe0f:
487 break;
488
479 default: 489 default:
480 XDrawRectangle (disp, d, gc, x + 2, y + 2, 490 XDrawRectangle (disp, d, gc, x + 2, y + 2,
481 fwidth - 4, term->fheight - 4); 491 fwidth - 4, term->fheight - 4);
482 } 492 }
483 493
484 x += fwidth; 494 x += fwidth;
485 } 495 }
486} 496}
487 497
488struct rxvt_font_overflow : rxvt_font { 498struct rxvt_font_overflow : rxvt_font
499{
489 struct rxvt_fontset *fs; 500 struct rxvt_fontset *fs;
490 501
491 rxvt_font_overflow (rxvt_fontset *fs) 502 rxvt_font_overflow (rxvt_fontset *fs)
492 : rxvt_font () 503 : rxvt_font ()
493 { 504 {
507 } 518 }
508 519
509 bool load (const rxvt_fontprop &prop, bool force_prop) 520 bool load (const rxvt_fontprop &prop, bool force_prop)
510 { 521 {
511 width = 1; height = 1; 522 width = 1; height = 1;
512 ascent = 1; descent = 0; 523 ascent = 1;
524 descent = (*fs)[2]->descent;
513 525
514 set_name (strdup ("built-in rendition overflow font")); 526 set_name (strdup ("built-in rendition overflow font"));
515 527
516 return true; 528 return true;
517 } 529 }
539 } 551 }
540}; 552};
541 553
542///////////////////////////////////////////////////////////////////////////// 554/////////////////////////////////////////////////////////////////////////////
543 555
544struct rxvt_font_x11 : rxvt_font { 556struct rxvt_font_x11 : rxvt_font
557{
545 rxvt_font_x11 () { f = 0; } 558 rxvt_font_x11 () { f = 0; }
546 559
547 void clear (); 560 void clear ();
548 561
549 rxvt_fontprop properties (); 562 rxvt_fontprop properties ();
924 } 937 }
925#endif 938#endif
926 939
927 width = 1; 940 width = 1;
928 941
929 for (uint16_t *t = extent_test_chars; t < extent_test_chars + ecb_array_length (extent_test_chars); t++) 942 for (uint32_t *t = extent_test_chars; t < extent_test_chars + ecb_array_length (extent_test_chars); t++)
930 { 943 {
931 if (FROM_UNICODE (cs, *t) == NOCHAR) 944 if (FROM_UNICODE (cs, *t) == NOCHAR)
932 continue; 945 continue;
933 946
934 // ignore characters we wouldn't use anyways 947 // ignore characters we wouldn't use anyways
1124 1137
1125///////////////////////////////////////////////////////////////////////////// 1138/////////////////////////////////////////////////////////////////////////////
1126 1139
1127#if XFT 1140#if XFT
1128 1141
1129struct rxvt_font_xft : rxvt_font { 1142struct rxvt_font_xft : rxvt_font
1143{
1144#if XFT_CHAR_CACHE
1145 // we cache the qascii range xoffsets in the name of speed,
1146 // expecting terminals to deal mostly with these characters
1147 // we also assume the xoff always fits into uint8_t,
1148 // which is questionable, but let's see...
1149 // also, it is uncomfortably big, due to the uints.
1150 enum { char_cache_min = 0x20, char_cache_max = 0x7e };
1151 uint8_t xoff_cache [char_cache_max - char_cache_min + 1];
1152 FT_UInt glyph_cache [char_cache_max - char_cache_min + 1];
1153#endif
1154
1130 rxvt_font_xft () { f = 0; } 1155 rxvt_font_xft ()
1156 {
1157 f = 0;
1158 }
1131 1159
1132 void clear (); 1160 void clear ();
1133 1161
1134 rxvt_fontprop properties (); 1162 rxvt_fontprop properties ();
1135 1163
1237 break; 1265 break;
1238 } 1266 }
1239 1267
1240 FT_Face face = XftLockFace (f); 1268 FT_Face face = XftLockFace (f);
1241 1269
1270 // fuck me plenty: XftLockFace can actually return 0. try not to crash.
1271 // we also assume blindly that if the first lock succeeds, then subsequent
1272 // locks will also succeed.
1273 if (!face)
1274 {
1275 XftFontClose (disp, f);
1276 success = false;
1277 break;
1278 }
1279
1242 ascent = (face->size->metrics.ascender + 63) >> 6; 1280 ascent = (face->size->metrics.ascender + 63) >> 6;
1243 descent = (-face->size->metrics.descender + 63) >> 6; 1281 descent = (-face->size->metrics.descender + 63) >> 6;
1244 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6); 1282 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1245 width = 0; 1283 width = 0;
1246 1284
1248 1286
1249 XftUnlockFace (f); 1287 XftUnlockFace (f);
1250 1288
1251 int glheight = height; 1289 int glheight = height;
1252 1290
1253 for (uint16_t *t = extent_test_chars; t < extent_test_chars + ecb_array_length (extent_test_chars); t++) 1291 for (uint32_t *t = extent_test_chars; t < extent_test_chars + ecb_array_length (extent_test_chars); t++)
1254 { 1292 {
1255 FcChar16 ch = *t; 1293 FcChar32 ch = *t;
1256 1294
1257 if (cs != CS_UNICODE 1295 if (cs != CS_UNICODE
1258 && ch > 0x100 1296 && ch > 0x100
1259 && FROM_UNICODE (cs, ch) == NOCHAR) 1297 && FROM_UNICODE (cs, ch) == NOCHAR)
1260 continue; 1298 continue;
1263 bool careful; 1301 bool careful;
1264 if (!has_char (*t, &prop, careful)) 1302 if (!has_char (*t, &prop, careful))
1265 continue; 1303 continue;
1266 1304
1267 XGlyphInfo g; 1305 XGlyphInfo g;
1268 XftTextExtents16 (disp, f, &ch, 1, &g); 1306 XftTextExtents32 (disp, f, &ch, 1, &g);
1269 1307
1270 g.width -= g.x; 1308 g.width -= g.x;
1271 1309
1272 int wcw = WCWIDTH (ch); 1310 int wcw = WCWIDTH (ch);
1273 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw; 1311 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1277 if (glheight < g.height - g.y) glheight = g.height - g.y; 1315 if (glheight < g.height - g.y) glheight = g.height - g.y;
1278 } 1316 }
1279 1317
1280 if (!width) 1318 if (!width)
1281 { 1319 {
1282 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name); 1320 rxvt_warn ("unable to calculate font width for '%s', using max_advance_width.\n", name);
1283 1321 width = f->max_advance_width;
1284 XftFontClose (disp, f);
1285 f = 0;
1286
1287 success = false;
1288 break; 1322 break;
1289 } 1323 }
1290 1324
1291 if (prop.height == rxvt_fontprop::unset 1325 if (prop.height == rxvt_fontprop::unset
1292 || (height <= prop.height && glheight <= prop.height) 1326 || (height <= prop.height && glheight <= prop.height)
1319 clear (); 1353 clear ();
1320 success = false; 1354 success = false;
1321 } 1355 }
1322#endif 1356#endif
1323 1357
1358#if XFT_CHAR_CACHE
1359 // populate char cache
1360 for (FcChar16 ch = char_cache_min; ch <= char_cache_max; ++ch)
1361 {
1362 FT_UInt glyph = XftCharIndex (disp, f, ch);
1363 glyph_cache [ch - char_cache_min] = glyph;
1364
1365 XGlyphInfo g;
1366 XftGlyphExtents (disp, f, &glyph, 1, &g);
1367 xoff_cache [ch - char_cache_min] = g.xOff;
1368 }
1369#endif
1370
1324 return success; 1371 return success;
1325} 1372}
1326 1373
1327bool 1374bool
1328rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const 1375rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
1329{ 1376{
1330 careful = false; 1377 careful = false;
1331 1378
1379 // handle non-bmp chars when text_t is 16 bit
1380#if ENABLE_COMBINING && !UNICODE_3
1381 if (ecb_expect_false (IS_COMPOSE (unicode)))
1382 if (compose_char *cc = rxvt_composite [unicode])
1383 if (cc->c2 == NOCHAR)
1384 unicode = cc->c1;
1385 else
1386 return false;
1387 else
1388 return false;
1389#endif
1390
1391 FcChar32 chr = unicode;
1392
1332 if (!XftCharExists (term->dpy, f, unicode)) 1393 if (!XftCharExists (term->dpy, f, chr))
1333 return false; 1394 return false;
1334 1395
1335 if (!prop || prop->width == rxvt_fontprop::unset) 1396 if (!prop || prop->width == rxvt_fontprop::unset)
1336 return true; 1397 return true;
1337 1398
1338 // check character against base font bounding box 1399 int wcw = max (WCWIDTH (chr), 1);
1339 FcChar32 ch = unicode; 1400
1340 XGlyphInfo g; 1401 XGlyphInfo g;
1341 XftTextExtents32 (term->dpy, f, &ch, 1, &g); 1402 XftTextExtents32 (term->dpy, f, &chr, 1, &g);
1403
1404 int cwidth = prop->width * wcw;
1405
1406 // use same adjustments as in ->draw, see there
1407 g.x += g.xOff ? cwidth - g.xOff >> 1 : 0;
1408 g.x += g.xOff ? 0 : cwidth;
1342 1409
1343 int w = g.width - g.x; 1410 int w = g.width - g.x;
1344 int wcw = max (WCWIDTH (unicode), 1);
1345 1411
1346 careful = g.x > 0 || w > prop->width * wcw; 1412 careful = g.x > 0 || w > cwidth;
1347 1413
1348 if (careful && !OVERLAP_OK (w, wcw, prop)) 1414 if (careful && !OVERLAP_OK (w, wcw, prop))
1349 return false; 1415 return false;
1350 1416
1351 // this weeds out _totally_ broken fonts, or glyphs 1417 // this weeds out _totally_ broken fonts, or glyphs
1358void 1424void
1359rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1425rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1360 const text_t *text, int len, 1426 const text_t *text, int len,
1361 int fg, int bg) 1427 int fg, int bg)
1362{ 1428{
1363 XGlyphInfo extents;
1364 XftGlyphSpec *enc = rxvt_temp_buf<XftGlyphSpec> (len); 1429 XftGlyphSpec *enc = rxvt_temp_buf<XftGlyphSpec> (len);
1365 XftGlyphSpec *ep = enc; 1430 XftGlyphSpec *ep = enc;
1366 1431
1367 dTermDisplay; 1432 dTermDisplay;
1368 dTermGC; 1433 dTermGC;
1381 int y_ = buffered ? 0 : y; 1446 int y_ = buffered ? 0 : y;
1382 1447
1383 while (len) 1448 while (len)
1384 { 1449 {
1385 int cwidth = term->fwidth; 1450 int cwidth = term->fwidth;
1386 FcChar32 fc = *text++; len--; 1451 FcChar32 chr = *text++; len--;
1387 1452
1388 while (len && *text == NOCHAR) 1453 while (len && *text == NOCHAR)
1389 text++, len--, cwidth += term->fwidth; 1454 text++, len--, cwidth += term->fwidth;
1390 1455
1391 if (fc != ' ') // skip spaces 1456 if (chr != ' ') // skip spaces
1457 {
1458 // handle non-bmp chars when text_t is 16 bit
1459 #if ENABLE_COMBINING && !UNICODE_3
1460 if (ecb_expect_false (IS_COMPOSE (chr)))
1461 if (compose_char *cc = rxvt_composite [chr])
1462 if (cc->c2 == NOCHAR)
1463 chr = cc->c1;
1464 #endif
1465
1466 #if 0
1467 FT_UInt glyphs [decltype (exp)::max_size];
1468
1469 for (int i = 0; i < nchrs; ++i)
1470 glyphs [i] = XftCharIndex (disp, f, chrs [i]);
1471
1472 for (int i = 0; i < nchrs; ++i)
1392 { 1473 {
1474 XGlyphInfo ep;
1475 XftGlyphExtents (disp, f, glyphs+i, 1, &ep);
1476 printf ("gs %4x g %4x + %3d,%3d o %3d,%3d wh %3d,%3d\n", chrs[i],glyphs[i],ep.x,ep.y,ep.xOff,ep.yOff,ep.width,ep.height);
1477 }
1478 #endif
1479
1480 FT_UInt glyph;
1481 int xOff;
1482
1483 #if XFT_CHAR_CACHE
1484 if (ecb_expect_true (IN_RANGE_INC (chr, char_cache_min, char_cache_max)))
1485 {
1486 glyph = glyph_cache [chr - char_cache_min];
1487 xOff = xoff_cache [chr - char_cache_min];
1488 }
1489 else
1490 #endif
1491 {
1393 FT_UInt glyph = XftCharIndex (disp, f, fc); 1492 glyph = XftCharIndex (disp, f, chr);
1493 XGlyphInfo extents;
1394 XftGlyphExtents (disp, f, &glyph, 1, &extents); 1494 XftGlyphExtents (disp, f, &glyph, 1, &extents);
1495 xOff = extents.xOff;
1496 }
1395 1497
1396 ep->glyph = glyph; 1498 ep->glyph = glyph;
1397 ep->x = x_ + (cwidth - extents.xOff >> 1); 1499 ep->x = x_;
1398 ep->y = y_ + ascent; 1500 ep->y = y_ + ascent;
1399 1501
1400 if (extents.xOff == 0) 1502 // the xft font cell might differ from the terminal font cell,
1503 // in which case we use the average between the two.
1504 ep->x += xOff ? cwidth - xOff >> 1 : 0;
1505
1506 // xft/freetype represent combining characters as characters with zero
1507 // width rendered over the previous character with some fonts, while
1508 // in other fonts, they are considered normal characters, while yet
1509 // in other fonts, they are shifted all over the place.
1510 // we handle the first two cases by keying off on xOff being 0
1511 // for zero-width chars. normally, we would add extents.xOff
1512 // of the base character here, but we don't have that, so we use cwidth.
1401 ep->x = x_ + cwidth; 1513 ep->x += xOff ? 0 : cwidth;
1402 1514
1403 ep++; 1515 ++ep;
1404 } 1516 }
1405 1517
1406 x_ += cwidth; 1518 x_ += cwidth;
1407 } 1519 }
1408 1520
1677} 1789}
1678 1790
1679int 1791int
1680rxvt_fontset::find_font_idx (unicode_t unicode) 1792rxvt_fontset::find_font_idx (unicode_t unicode)
1681{ 1793{
1682 if (unicode >= 1<<20) 1794 // this limits fmap size. it has to accommodate COMPOSE_HI when UNICODE_3
1795 if (unicode > 0x1fffff)
1683 return 0; 1796 return 0;
1684 1797
1685 unicode_t hi = unicode >> 8; 1798 unicode_t hi = unicode >> 8;
1686 1799
1687 if (hi < fmap.size () 1800 if (hi < fmap.size ())
1688 && fmap[hi] 1801 if (pagemap *pm = fmap[hi])
1689 && (*fmap[hi])[unicode & 0xff] != 0xff) 1802 if ((*pm)[unicode & 0xff] != 0xff)
1690 return (*fmap[hi])[unicode & 0xff]; 1803 return (*pm)[unicode & 0xff];
1691 1804
1692 unsigned int i; 1805 unsigned int i;
1693 1806
1694 for (i = 0; i < fonts.size (); i++) 1807 for (i = 0; i < fonts.size (); i++)
1695 { 1808 {
1719 } 1832 }
1720 1833
1721 next_font: 1834 next_font:
1722 if (i == fonts.size () - 1) 1835 if (i == fonts.size () - 1)
1723 { 1836 {
1837 // compose characters are handled by the default font, unless another font takes over
1838 // we do not go via the fallback list for speed reasons.
1839 if (IS_COMPOSE (unicode))
1840 return 0;
1841
1724 if (fallback->name) 1842 if (fallback->name)
1725 { 1843 {
1726 // search through the fallback list 1844 // search through the fallback list
1727 push_font (new_font (fallback->name, fallback->cs)); 1845 push_font (new_font (fallback->name, fallback->cs));
1728 fallback++; 1846 fallback++;
1734 // way to configure this and xft is easier to hack in, 1852 // way to configure this and xft is easier to hack in,
1735 // while x11 has more framework in place already. 1853 // while x11 has more framework in place already.
1736 // TODO: this is a real resource hog, xft takes ages(?) 1854 // TODO: this is a real resource hog, xft takes ages(?)
1737#if XFT && USE_SLOW_LOOKUP 1855#if XFT && USE_SLOW_LOOKUP
1738 // grab the first xft font that seems suitable 1856 // grab the first xft font that seems suitable
1857 // TOOD: should go first for cellchar (spacing 110) then mono, then else
1739 FcPattern *p = FcPatternCreate (); 1858 FcPattern *p = FcPatternCreate ();
1740 1859
1741 FcCharSet *s = FcCharSetCreate (); 1860 FcCharSet *s = FcCharSetCreate ();
1742 FcCharSetAddChar (s, unicode); 1861 FcCharSetAddChar (s, unicode);
1743 FcPatternAddCharSet (p, FC_CHARSET, s); 1862 FcPatternAddCharSet (p, FC_CHARSET, s);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines