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.209 by root, Fri Jun 18 21:58:03 2021 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
517 } 518 }
518 519
519 bool load (const rxvt_fontprop &prop, bool force_prop) 520 bool load (const rxvt_fontprop &prop, bool force_prop)
520 { 521 {
521 width = 1; height = 1; 522 width = 1; height = 1;
522 ascent = 1; descent = 0; 523 ascent = 1;
524 descent = (*fs)[2]->descent;
523 525
524 set_name (strdup ("built-in rendition overflow font")); 526 set_name (strdup ("built-in rendition overflow font"));
525 527
526 return true; 528 return true;
527 } 529 }
935 } 937 }
936#endif 938#endif
937 939
938 width = 1; 940 width = 1;
939 941
940 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++)
941 { 943 {
942 if (FROM_UNICODE (cs, *t) == NOCHAR) 944 if (FROM_UNICODE (cs, *t) == NOCHAR)
943 continue; 945 continue;
944 946
945 // ignore characters we wouldn't use anyways 947 // ignore characters we wouldn't use anyways
1137 1139
1138#if XFT 1140#if XFT
1139 1141
1140struct rxvt_font_xft : rxvt_font 1142struct rxvt_font_xft : rxvt_font
1141{ 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
1142 rxvt_font_xft () 1155 rxvt_font_xft ()
1143 { 1156 {
1144 f = 0; 1157 f = 0;
1145 } 1158 }
1146 1159
1252 break; 1265 break;
1253 } 1266 }
1254 1267
1255 FT_Face face = XftLockFace (f); 1268 FT_Face face = XftLockFace (f);
1256 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
1257 ascent = (face->size->metrics.ascender + 63) >> 6; 1280 ascent = (face->size->metrics.ascender + 63) >> 6;
1258 descent = (-face->size->metrics.descender + 63) >> 6; 1281 descent = (-face->size->metrics.descender + 63) >> 6;
1259 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6); 1282 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1260 width = 0; 1283 width = 0;
1261 1284
1263 1286
1264 XftUnlockFace (f); 1287 XftUnlockFace (f);
1265 1288
1266 int glheight = height; 1289 int glheight = height;
1267 1290
1268 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++)
1269 { 1292 {
1270 FcChar16 ch = *t; 1293 FcChar32 ch = *t;
1271 1294
1272 if (cs != CS_UNICODE 1295 if (cs != CS_UNICODE
1273 && ch > 0x100 1296 && ch > 0x100
1274 && FROM_UNICODE (cs, ch) == NOCHAR) 1297 && FROM_UNICODE (cs, ch) == NOCHAR)
1275 continue; 1298 continue;
1278 bool careful; 1301 bool careful;
1279 if (!has_char (*t, &prop, careful)) 1302 if (!has_char (*t, &prop, careful))
1280 continue; 1303 continue;
1281 1304
1282 XGlyphInfo g; 1305 XGlyphInfo g;
1283 XftTextExtents16 (disp, f, &ch, 1, &g); 1306 XftTextExtents32 (disp, f, &ch, 1, &g);
1284 1307
1285 g.width -= g.x; 1308 g.width -= g.x;
1286 1309
1287 int wcw = WCWIDTH (ch); 1310 int wcw = WCWIDTH (ch);
1288 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw; 1311 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1330 clear (); 1353 clear ();
1331 success = false; 1354 success = false;
1332 } 1355 }
1333#endif 1356#endif
1334 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
1335 return success; 1371 return success;
1336} 1372}
1337 1373
1338bool 1374bool
1339rxvt_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
1340{ 1376{
1341 careful = false; 1377 careful = false;
1342 1378
1343 rxvt_compose_expand_static<FcChar32> exp; 1379 // handle non-bmp chars when text_t is 16 bit
1344 FcChar32 *chrs = exp (unicode); 1380#if ENABLE_COMBINING && !UNICODE_3
1345 int nchrs = exp.length (chrs); 1381 if (ecb_expect_false (IS_COMPOSE (unicode)))
1346 1382 if (compose_char *cc = rxvt_composite [unicode])
1347 // allm chars in sequence must be available 1383 if (cc->c2 == NOCHAR)
1348 for (int i = 0; i < nchrs; ++i) 1384 unicode = cc->c1;
1349 if (!XftCharExists (term->dpy, f, chrs [i])) 1385 else
1386 return false;
1387 else
1350 return false; 1388 return false;
1389#endif
1390
1391 FcChar32 chr = unicode;
1392
1393 if (!XftCharExists (term->dpy, f, chr))
1394 return false;
1351 1395
1352 if (!prop || prop->width == rxvt_fontprop::unset) 1396 if (!prop || prop->width == rxvt_fontprop::unset)
1353 return true; 1397 return true;
1354 1398
1355 int wcw = max (WCWIDTH (chrs [0]), 1); 1399 int wcw = max (WCWIDTH (chr), 1);
1356 1400
1357 // we check against all glyph sizes. this is probably wrong,
1358 // due to the way ->draw positions these glyphs.
1359 for (int i = 0; i < nchrs; ++i)
1360 {
1361 XGlyphInfo g; 1401 XGlyphInfo g;
1362 XftTextExtents32 (term->dpy, f, chrs + i, 1, &g); 1402 XftTextExtents32 (term->dpy, f, &chr, 1, &g);
1363 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;
1409
1364 int w = g.width - g.x; 1410 int w = g.width - g.x;
1365 1411
1366 careful = g.x > 0 || w > prop->width * wcw; 1412 careful = g.x > 0 || w > cwidth;
1367 1413
1368 if (careful && !OVERLAP_OK (w, wcw, prop)) 1414 if (careful && !OVERLAP_OK (w, wcw, prop))
1369 return false; 1415 return false;
1370 1416
1371 // this weeds out _totally_ broken fonts, or glyphs 1417 // this weeds out _totally_ broken fonts, or glyphs
1372 if (!OVERLAP_OK (g.xOff, wcw, prop)) 1418 if (!OVERLAP_OK (g.xOff, wcw, prop))
1373 return false; 1419 return false;
1374 }
1375 1420
1376 return true; 1421 return true;
1377} 1422}
1378 1423
1379void 1424void
1380rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1425rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1381 const text_t *text, int len, 1426 const text_t *text, int len,
1382 int fg, int bg) 1427 int fg, int bg)
1383{ 1428{
1384 //XftGlyphSpec *enc = rxvt_temp_buf<XftGlyphSpec> (len);//D 1429 XftGlyphSpec *enc = rxvt_temp_buf<XftGlyphSpec> (len);
1385 //XftGlyphSpec *ep = enc;//D 1430 XftGlyphSpec *ep = enc;
1386 static vector<XftGlyphSpec> enc; enc.resize (0); // static to avoid malloc, still slow
1387 1431
1388 dTermDisplay; 1432 dTermDisplay;
1389 dTermGC; 1433 dTermGC;
1390 1434
1391 int w = term->fwidth * len; 1435 int w = term->fwidth * len;
1402 int y_ = buffered ? 0 : y; 1446 int y_ = buffered ? 0 : y;
1403 1447
1404 while (len) 1448 while (len)
1405 { 1449 {
1406 int cwidth = term->fwidth; 1450 int cwidth = term->fwidth;
1407
1408 rxvt_compose_expand_static<FcChar32> exp;
1409 FcChar32 *chrs = exp (*text++); len--; 1451 FcChar32 chr = *text++; len--;
1410 int nchrs = exp.length (chrs);
1411 1452
1412 while (len && *text == NOCHAR) 1453 while (len && *text == NOCHAR)
1413 text++, len--, cwidth += term->fwidth; 1454 text++, len--, cwidth += term->fwidth;
1414 1455
1415 if (chrs [0] != ' ') // skip spaces 1456 if (chr != ' ') // skip spaces
1416 { 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
1417 #if 1 1466 #if 0
1418 FT_UInt glyphs [decltype (exp)::max_size]; 1467 FT_UInt glyphs [decltype (exp)::max_size];
1419 1468
1420 for (int i = 0; i < nchrs; ++i) 1469 for (int i = 0; i < nchrs; ++i)
1421 glyphs [i] = XftCharIndex (disp, f, chrs [i]); 1470 glyphs [i] = XftCharIndex (disp, f, chrs [i]);
1422 1471
1426 XftGlyphExtents (disp, f, glyphs+i, 1, &ep); 1475 XftGlyphExtents (disp, f, glyphs+i, 1, &ep);
1427 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); 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);
1428 } 1477 }
1429 #endif 1478 #endif
1430 1479
1431 FT_UInt glyph = XftCharIndex (disp, f, chrs [0]); 1480 FT_UInt glyph;
1432 XGlyphInfo extents0; 1481 int xOff;
1433 XftGlyphExtents (disp, f, &glyph, 1, &extents0);
1434 1482
1435 int cx = x_ + (cwidth - extents0.xOff >> 1); 1483 #if XFT_CHAR_CACHE
1436 int cy = y_ + ascent; 1484 if (ecb_expect_true (IN_RANGE_INC (chr, char_cache_min, char_cache_max)))
1437
1438 XftGlyphSpec ep;
1439 ep.glyph = glyph;
1440 ep.x = cx + (extents0.xOff ? 0 : extents0.xOff);
1441 ep.y = cy;
1442
1443 enc.push_back (ep);
1444
1445 for (int i = 1; ecb_expect_false (i < nchrs); ++i)
1446 { 1485 {
1486 glyph = glyph_cache [chr - char_cache_min];
1487 xOff = xoff_cache [chr - char_cache_min];
1488 }
1489 else
1490 #endif
1491 {
1447 FT_UInt glyph = XftCharIndex (disp, f, chrs [i]); 1492 glyph = XftCharIndex (disp, f, chr);
1448 XGlyphInfo extents; 1493 XGlyphInfo extents;
1449 XftGlyphExtents (disp, f, &glyph, 1, &extents); 1494 XftGlyphExtents (disp, f, &glyph, 1, &extents);
1450 1495 xOff = extents.xOff;
1451 ep.glyph = glyph;
1452 ep.x = cx + (extents.xOff ? 0 : extents0.xOff);
1453
1454 enc.push_back (ep);
1455 } 1496 }
1497
1498 ep->glyph = glyph;
1499 ep->x = x_;
1500 ep->y = y_ + ascent;
1501
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.
1513 ep->x += xOff ? 0 : cwidth;
1514
1515 ++ep;
1456 } 1516 }
1457 1517
1458 x_ += cwidth; 1518 x_ += cwidth;
1459 } 1519 }
1460 1520
1461 if (buffered) 1521 if (buffered)
1462 { 1522 {
1463 if (!enc.empty ()) 1523 if (ep != enc)
1464 { 1524 {
1465 rxvt_drawable &d2 = d.screen->scratch_drawable (w, h); 1525 rxvt_drawable &d2 = d.screen->scratch_drawable (w, h);
1466 1526
1467#ifdef HAVE_IMG 1527#ifdef HAVE_IMG
1468 Picture dst = 0; // the only assignment is done conditionally in the following if condition 1528 Picture dst = 0; // the only assignment is done conditionally in the following if condition
1514 } 1574 }
1515 else 1575 else
1516#endif 1576#endif
1517 XftDrawRect (d2, &term->pix_colors[bg >= 0 ? bg : Color_bg].c, 0, 0, w, h); 1577 XftDrawRect (d2, &term->pix_colors[bg >= 0 ? bg : Color_bg].c, 0, 0, w, h);
1518 1578
1519 XftDrawGlyphSpec (d2, &term->pix_colors[fg].c, f, &enc[0], enc.size ()); 1579 XftDrawGlyphSpec (d2, &term->pix_colors[fg].c, f, enc, ep - enc);
1520 XCopyArea (disp, d2, d, gc, 0, 0, w, h, x, y); 1580 XCopyArea (disp, d2, d, gc, 0, 0, w, h, x, y);
1521 } 1581 }
1522 else 1582 else
1523 clear_rect (d, x, y, w, h, bg); 1583 clear_rect (d, x, y, w, h, bg);
1524 } 1584 }
1525 else 1585 else
1526 { 1586 {
1527 clear_rect (d, x, y, w, h, bg); 1587 clear_rect (d, x, y, w, h, bg);
1528 XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, &enc[0], enc.size ()); 1588 XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, enc, ep - enc);
1529 } 1589 }
1530} 1590}
1531 1591
1532#endif 1592#endif
1533 1593
1729} 1789}
1730 1790
1731int 1791int
1732rxvt_fontset::find_font_idx (unicode_t unicode) 1792rxvt_fontset::find_font_idx (unicode_t unicode)
1733{ 1793{
1734 // this limits fmap size. it has to accomodate COMPOSE_HI when UNICODE_3 1794 // this limits fmap size. it has to accommodate COMPOSE_HI when UNICODE_3
1735 if (unicode > 0x1fffff) 1795 if (unicode > 0x1fffff)
1736 return 0; 1796 return 0;
1737 1797
1738 unicode_t hi = unicode >> 8; 1798 unicode_t hi = unicode >> 8;
1739 1799

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines