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.203 by root, Thu Jun 17 11:21:52 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
212} 213}
213 214
214///////////////////////////////////////////////////////////////////////////// 215/////////////////////////////////////////////////////////////////////////////
215 216
216rxvt_font::rxvt_font () 217rxvt_font::rxvt_font ()
217: name(0), width(rxvt_fontprop::unset), height(rxvt_fontprop::unset), can_compose(false) 218: name(0), width(rxvt_fontprop::unset), height(rxvt_fontprop::unset)
218{ 219{
219} 220}
220 221
221void 222void
222rxvt_font::set_name (char *name_) 223rxvt_font::set_name (char *name_)
445#endif 446#endif
446#if ENABLE_COMBINING 447#if ENABLE_COMBINING
447 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 448 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
448 { 449 {
449 min_it (width, 2); // we only support wcwidth up to 2 450 min_it (width, 2); // we only support wcwidth up to 2
450
451 #if NOT_YET
452 vector<text_t> chrs;
453 chrs.reserve (rxvt_composite.expand (t));
454 rxvt_composite.expand (t, &chrs[0]);
455
456 while (!chrs.empty ())
457 {
458 rxvt_font *f1 = fs->find_font_idx (chrs[0])
459
460 int i = 0;
461 while (i < chrs.size () - 1
462 && f1->can_combine
463 && f1->has-char (chrs[i + 1], careful)
464 && !careful
465 ++i;
466
467 (*fs)[f1]->draw (d, x, y, &chrs[0], width, fg, bg);
468 chrs.erase (&chrs[0], &chrs[i]);
469 }
470 #endif
471
472 #if 1
473 text_t chrs[2]; 451 text_t chrs[2];
474 chrs [1] = NOCHAR; 452 chrs [1] = NOCHAR;
475 453
476 *chrs = cc->c1; 454 *chrs = cc->c1;
477 rxvt_font *f1 = (*fs)[fs->find_font_idx (cc->c1)]; 455 rxvt_font *f1 = (*fs)[fs->find_font_idx (cc->c1)];
487 ? f1 465 ? f1
488 : (*fs)[fs->find_font_idx (cc->c2)]; 466 : (*fs)[fs->find_font_idx (cc->c2)];
489 467
490 f2->draw (d, x, y, chrs, width, fg, Color_none); 468 f2->draw (d, x, y, chrs, width, fg, Color_none);
491 } 469 }
492 #endif
493 } 470 }
494#endif 471#endif
495 else 472 else
496 switch (t) 473 switch (t)
497 { 474 {
541 } 518 }
542 519
543 bool load (const rxvt_fontprop &prop, bool force_prop) 520 bool load (const rxvt_fontprop &prop, bool force_prop)
544 { 521 {
545 width = 1; height = 1; 522 width = 1; height = 1;
546 ascent = 1; descent = 0; 523 ascent = 1;
524 descent = (*fs)[2]->descent;
547 525
548 set_name (strdup ("built-in rendition overflow font")); 526 set_name (strdup ("built-in rendition overflow font"));
549 527
550 return true; 528 return true;
551 } 529 }
959 } 937 }
960#endif 938#endif
961 939
962 width = 1; 940 width = 1;
963 941
964 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++)
965 { 943 {
966 if (FROM_UNICODE (cs, *t) == NOCHAR) 944 if (FROM_UNICODE (cs, *t) == NOCHAR)
967 continue; 945 continue;
968 946
969 // ignore characters we wouldn't use anyways 947 // ignore characters we wouldn't use anyways
1161 1139
1162#if XFT 1140#if XFT
1163 1141
1164struct rxvt_font_xft : rxvt_font 1142struct rxvt_font_xft : rxvt_font
1165{ 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
1166 rxvt_font_xft () 1155 rxvt_font_xft ()
1167 { 1156 {
1168 can_compose = true;
1169 f = 0; 1157 f = 0;
1170 } 1158 }
1171 1159
1172 void clear (); 1160 void clear ();
1173 1161
1277 break; 1265 break;
1278 } 1266 }
1279 1267
1280 FT_Face face = XftLockFace (f); 1268 FT_Face face = XftLockFace (f);
1281 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
1282 ascent = (face->size->metrics.ascender + 63) >> 6; 1280 ascent = (face->size->metrics.ascender + 63) >> 6;
1283 descent = (-face->size->metrics.descender + 63) >> 6; 1281 descent = (-face->size->metrics.descender + 63) >> 6;
1284 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6); 1282 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1285 width = 0; 1283 width = 0;
1286 1284
1288 1286
1289 XftUnlockFace (f); 1287 XftUnlockFace (f);
1290 1288
1291 int glheight = height; 1289 int glheight = height;
1292 1290
1293 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++)
1294 { 1292 {
1295 FcChar16 ch = *t; 1293 FcChar32 ch = *t;
1296 1294
1297 if (cs != CS_UNICODE 1295 if (cs != CS_UNICODE
1298 && ch > 0x100 1296 && ch > 0x100
1299 && FROM_UNICODE (cs, ch) == NOCHAR) 1297 && FROM_UNICODE (cs, ch) == NOCHAR)
1300 continue; 1298 continue;
1303 bool careful; 1301 bool careful;
1304 if (!has_char (*t, &prop, careful)) 1302 if (!has_char (*t, &prop, careful))
1305 continue; 1303 continue;
1306 1304
1307 XGlyphInfo g; 1305 XGlyphInfo g;
1308 XftTextExtents16 (disp, f, &ch, 1, &g); 1306 XftTextExtents32 (disp, f, &ch, 1, &g);
1309 1307
1310 g.width -= g.x; 1308 g.width -= g.x;
1311 1309
1312 int wcw = WCWIDTH (ch); 1310 int wcw = WCWIDTH (ch);
1313 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw; 1311 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1355 clear (); 1353 clear ();
1356 success = false; 1354 success = false;
1357 } 1355 }
1358#endif 1356#endif
1359 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
1360 return success; 1371 return success;
1361} 1372}
1362 1373
1363bool 1374bool
1364rxvt_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
1365{ 1376{
1366 careful = false; 1377 careful = false;
1367 1378
1379 // handle non-bmp chars when text_t is 16 bit
1368#if ENABLE_COMBINING && !UNICODE_3 1380#if ENABLE_COMBINING && !UNICODE_3
1369 if (ecb_expect_false (IS_COMPOSE (unicode))) 1381 if (ecb_expect_false (IS_COMPOSE (unicode)))
1370 if (compose_char *cc = rxvt_composite[unicode]) 1382 if (compose_char *cc = rxvt_composite [unicode])
1371 if (cc->c2 == NOCHAR) 1383 if (cc->c2 == NOCHAR)
1372 unicode = cc->c1; 1384 unicode = cc->c1;
1385 else
1386 return false;
1387 else
1388 return false;
1373#endif 1389#endif
1374 1390
1391 FcChar32 chr = unicode;
1392
1375 if (!XftCharExists (term->dpy, f, unicode)) 1393 if (!XftCharExists (term->dpy, f, chr))
1376 return false; 1394 return false;
1377 1395
1378 if (!prop || prop->width == rxvt_fontprop::unset) 1396 if (!prop || prop->width == rxvt_fontprop::unset)
1379 return true; 1397 return true;
1380 1398
1381 // check character against base font bounding box 1399 int wcw = max (WCWIDTH (chr), 1);
1382 FcChar32 ch = unicode; 1400
1383 XGlyphInfo g; 1401 XGlyphInfo g;
1384 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;
1385 1409
1386 int w = g.width - g.x; 1410 int w = g.width - g.x;
1387 int wcw = max (WCWIDTH (unicode), 1);
1388 1411
1389 careful = g.x > 0 || w > prop->width * wcw; 1412 careful = g.x > 0 || w > cwidth;
1390 1413
1391 if (careful && !OVERLAP_OK (w, wcw, prop)) 1414 if (careful && !OVERLAP_OK (w, wcw, prop))
1392 return false; 1415 return false;
1393 1416
1394 // this weeds out _totally_ broken fonts, or glyphs 1417 // this weeds out _totally_ broken fonts, or glyphs
1401void 1424void
1402rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1425rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1403 const text_t *text, int len, 1426 const text_t *text, int len,
1404 int fg, int bg) 1427 int fg, int bg)
1405{ 1428{
1406 XGlyphInfo extents;
1407 XftGlyphSpec *enc = rxvt_temp_buf<XftGlyphSpec> (len); 1429 XftGlyphSpec *enc = rxvt_temp_buf<XftGlyphSpec> (len);
1408 XftGlyphSpec *ep = enc; 1430 XftGlyphSpec *ep = enc;
1409 1431
1410 dTermDisplay; 1432 dTermDisplay;
1411 dTermGC; 1433 dTermGC;
1424 int y_ = buffered ? 0 : y; 1446 int y_ = buffered ? 0 : y;
1425 1447
1426 while (len) 1448 while (len)
1427 { 1449 {
1428 int cwidth = term->fwidth; 1450 int cwidth = term->fwidth;
1429 FcChar32 fc = *text++; len--; 1451 FcChar32 chr = *text++; len--;
1430
1431#if ENABLE_COMBINING && !UNICODE_3
1432 if (ecb_expect_false (IS_COMPOSE (fc)))
1433 if (compose_char *cc = rxvt_composite[fc]) // should always be true, but better be safe than sorry
1434 fc = cc->c1; // c2 must be NOCHAR, as has_char handles it that way
1435#endif
1436 1452
1437 while (len && *text == NOCHAR) 1453 while (len && *text == NOCHAR)
1438 text++, len--, cwidth += term->fwidth; 1454 text++, len--, cwidth += term->fwidth;
1439 1455
1440 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)
1441 { 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 {
1442 FT_UInt glyph = XftCharIndex (disp, f, fc); 1492 glyph = XftCharIndex (disp, f, chr);
1493 XGlyphInfo extents;
1443 XftGlyphExtents (disp, f, &glyph, 1, &extents); 1494 XftGlyphExtents (disp, f, &glyph, 1, &extents);
1495 xOff = extents.xOff;
1496 }
1444 1497
1445 ep->glyph = glyph; 1498 ep->glyph = glyph;
1446 ep->x = x_ + (cwidth - extents.xOff >> 1); 1499 ep->x = x_;
1447 ep->y = y_ + ascent; 1500 ep->y = y_ + ascent;
1448 1501
1449 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.
1450 ep->x = x_ + cwidth; 1513 ep->x += xOff ? 0 : cwidth;
1451 1514
1452 ep++; 1515 ++ep;
1453 } 1516 }
1454 1517
1455 x_ += cwidth; 1518 x_ += cwidth;
1456 } 1519 }
1457 1520
1726} 1789}
1727 1790
1728int 1791int
1729rxvt_fontset::find_font_idx (unicode_t unicode) 1792rxvt_fontset::find_font_idx (unicode_t unicode)
1730{ 1793{
1731 // 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
1732 if (unicode > 0x1fffff) 1795 if (unicode > 0x1fffff)
1733 return 0; 1796 return 0;
1734 1797
1735 unicode_t hi = unicode >> 8; 1798 unicode_t hi = unicode >> 8;
1736 1799

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines