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.70 by root, Tue Apr 26 00:35:00 2005 UTC vs.
Revision 1.76 by root, Tue Jan 3 23:40:58 2006 UTC

27#include <cstdlib> 27#include <cstdlib>
28#include <wchar.h> 28#include <wchar.h>
29#include <inttypes.h> 29#include <inttypes.h>
30 30
31#define DISPLAY r->display->display 31#define DISPLAY r->display->display
32#define TGC r->TermWin.gc 32#define TGC r->gc
33 33
34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width 34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
35 35
36const struct rxvt_fallback_font { 36const struct rxvt_fallback_font {
37 codeset cs; 37 codeset cs;
276 rxvt_fontprop properties () 276 rxvt_fontprop properties ()
277 { 277 {
278 rxvt_fontprop p; 278 rxvt_fontprop p;
279 279
280 p.width = p.height = 1; 280 p.width = p.height = 1;
281 p.ascent = rxvt_fontprop::unset;
281 p.weight = rxvt_fontprop::medium; 282 p.weight = rxvt_fontprop::medium;
282 p.slant = rxvt_fontprop::roman; 283 p.slant = rxvt_fontprop::roman;
283 284
284 return p; 285 return p;
285 } 286 }
333 const text_t *text, int len, 334 const text_t *text, int len,
334 int fg, int bg) 335 int fg, int bg)
335{ 336{
336 Display *disp = d.display->display; 337 Display *disp = d.display->display;
337 338
338 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 339 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
339 340
340 XSetForeground (disp, TGC, r->pix_colors[fg]); 341 XSetForeground (disp, TGC, r->pix_colors[fg]);
341 342
342 while (len) 343 while (len)
343 { 344 {
349 350
350 while (++text, --len && *text == NOCHAR) 351 while (++text, --len && *text == NOCHAR)
351 ; 352 ;
352 353
353 int width = text - tp; 354 int width = text - tp;
354 int fwidth = r->TermWin.fwidth * width; 355 int fwidth = r->fwidth * width;
355 356
356 if (0x2500 <= t && t <= 0x259f) 357 if (0x2500 <= t && t <= 0x259f)
357 { 358 {
358 uint16_t offs = linedraw_offs[t - 0x2500]; 359 uint16_t offs = linedraw_offs[t - 0x2500];
359 uint32_t *a = linedraw_command + (offs >> 4); 360 uint32_t *a = linedraw_command + (offs >> 4);
360 uint32_t *b = a + (offs & 15); 361 uint32_t *b = a + (offs & 15);
361 362
362 int W = fwidth; 363 int W = fwidth;
363 int H = r->TermWin.fheight; 364 int H = r->fheight;
364 365
365 int x_[16]; 366 int x_[16];
366 int y_[16]; 367 int y_[16];
367 368
368 for (int i = 0; i <= 8; i++) 369 for (int i = 0; i <= 8; i++)
464 case NOCHAR: 465 case NOCHAR:
465 break; 466 break;
466 467
467 default: 468 default:
468 XDrawRectangle (disp, d, TGC, x + 2, y + 2, 469 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
469 fwidth - 4, r->TermWin.fheight - 4); 470 fwidth - 4, r->fheight - 4);
470 } 471 }
471 472
472 x += fwidth; 473 x += fwidth;
473 } 474 }
474} 475}
523bool 524bool
524rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 525rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
525{ 526{
526 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 527 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
527 p.height = height; 528 p.height = height;
529 p.ascent = rxvt_fontprop::unset;
528 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 530 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
529 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; 531 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
530 532
531 return true; 533 return true;
532} 534}
552 554
553 set_properties (p, height, weight, slant, avgwidth); 555 set_properties (p, height, weight, slant, avgwidth);
554 556
555 free (weight); 557 free (weight);
556 free (slant); 558 free (slant);
559
560 p.ascent = f->ascent;
557 561
558 return true; 562 return true;
559} 563}
560 564
561bool 565bool
851 855
852 XCharStruct g; 856 XCharStruct g;
853 int dir_ret, asc_ret, des_ret; 857 int dir_ret, asc_ret, des_ret;
854 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 858 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
855 859
856 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 860 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
857 861
858 if (width < g.width) width = g.width; 862 if (width < g.width) width = g.width;
859 } 863 }
860 864
861 if (cs == CS_UNKNOWN) 865 if (cs == CS_UNKNOWN)
936 return true; 940 return true;
937 941
938 // check character against base font bounding box 942 // check character against base font bounding box
939 int w = xcs->width; 943 int w = xcs->width;
940 int wcw = wcwidth (unicode); 944 int wcw = wcwidth (unicode);
941 if (wcw > 0) w /= wcw; 945 if (wcw > 0) w = (w + wcw - 1) / wcw;
942 946
943 careful = w > prop->width; 947 careful = w > prop->width;
944 if (careful && w > prop->width * MAX_OVERLAP >> 2) 948 if (careful && w > prop->width * MAX_OVERLAP >> 2)
945 return false; 949 return false;
946 950
956 // and it is a mess /. 960 // and it is a mess /.
957 // yet we are trying to be perfect /. 961 // yet we are trying to be perfect /.
958 // but the result still isn't perfect /. 962 // but the result still isn't perfect /.
959 963
960 bool slow = this->slow 964 bool slow = this->slow
961 || width != r->TermWin.fwidth 965 || width != r->fwidth
962 || height != r->TermWin.fheight; 966 || height != r->fheight;
963 967
964 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 968 int base = ascent; // sorry, incorrect: r->fbase;
965 969
966 XGCValues v; 970 XGCValues v;
967 v.foreground = r->pix_colors[fg]; 971 v.foreground = r->pix_colors[fg];
968 v.font = f->fid; 972 v.font = f->fid;
969 973
977 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 981 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
978 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 982 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
979 } 983 }
980 else 984 else
981 { 985 {
982 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 986 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
983 987
984 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 988 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
985 989
986 if (slow) 990 if (slow)
987 { 991 {
988 do 992 do
989 { 993 {
990 if (xc->byte1 || xc->byte2) 994 if (xc->byte1 || xc->byte2)
991 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 995 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
992 996
993 x += r->TermWin.fwidth; 997 x += r->fwidth;
994 xc++; len--; 998 xc++; len--;
995 } 999 }
996 while (len); 1000 while (len);
997 } 1001 }
998 else 1002 else
1009 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1013 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
1010 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1014 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
1011 } 1015 }
1012 else 1016 else
1013 { 1017 {
1014 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1018 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1015 1019
1016 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1020 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
1017 1021
1018 if (slow) 1022 if (slow)
1019 { 1023 {
1020 do 1024 do
1021 { 1025 {
1022 if (*xc) 1026 if (*xc)
1023 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1027 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
1024 1028
1025 x += r->TermWin.fwidth; 1029 x += r->fwidth;
1026 xc++; len--; 1030 xc++; len--;
1027 } 1031 }
1028 while (len); 1032 while (len);
1029 } 1033 }
1030 else 1034 else
1073 1077
1074 FT_Face face = XftLockFace (f); 1078 FT_Face face = XftLockFace (f);
1075 1079
1076 p.width = width; 1080 p.width = width;
1077 p.height = height; 1081 p.height = height;
1082 p.ascent = ascent;
1078 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1083 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1079 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1084 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1080 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1085 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1081 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1086 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1082 1087
1154 1159
1155 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1160 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1156 1161
1157 XftUnlockFace (f); 1162 XftUnlockFace (f);
1158 1163
1164 int glheight = height;
1165
1159 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1166 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1160 { 1167 {
1161 FcChar16 ch = *t; 1168 FcChar16 ch = *t;
1162 1169
1163 if (cs != CS_UNICODE 1170 if (cs != CS_UNICODE
1171 continue; 1178 continue;
1172 1179
1173 XGlyphInfo g; 1180 XGlyphInfo g;
1174 XftTextExtents16 (disp, f, &ch, 1, &g); 1181 XftTextExtents16 (disp, f, &ch, 1, &g);
1175 1182
1183 g.width -= g.x;
1184
1176 int wcw = wcwidth (ch); 1185 int wcw = wcwidth (ch);
1177 if (wcw > 0) g.width = g.width / wcw; 1186 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1178 1187
1179 if (width < g.width) width = g.width; 1188 if (width < g.width ) width = g.width;
1180 if (height < g.height) height = g.height; 1189 if (height < g.height ) height = g.height;
1190 if (glheight < g.height - g.y) glheight = g.height - g.y;
1191 }
1192
1193 if (!width)
1194 {
1195 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1196
1197 XftFontClose (disp, f);
1198 f = 0;
1199
1200 success = false;
1201 break;
1181 } 1202 }
1182 1203
1183 if (prop.height == rxvt_fontprop::unset 1204 if (prop.height == rxvt_fontprop::unset
1184 || height <= prop.height 1205 || (height <= prop.height && glheight <= prop.height)
1185 || height <= 2 1206 || height <= 2
1186 || !scalable) 1207 || !scalable)
1187 break; 1208 break;
1188 1209
1189 if (ftheight) 1210 if (ftheight)
1196 ftheight -= height - prop.height; 1217 ftheight -= height - prop.height;
1197 } 1218 }
1198 else 1219 else
1199 ftheight = prop.height - 1; 1220 ftheight = prop.height - 1;
1200 1221
1201 XftFontClose (disp, f); 1222 XftFontClose (disp, f);
1202 FcPatternDel (match, FC_PIXEL_SIZE); 1223 FcPatternDel (match, FC_PIXEL_SIZE);
1203 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1224 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1204 } 1225 }
1205 1226
1206 FcPatternDestroy (match); 1227 FcPatternDestroy (match);
1207 1228
1208#if 0 // do it per-character 1229#if 0 // do it per-character
1230 // check character against base font bounding box 1251 // check character against base font bounding box
1231 FcChar32 ch = unicode; 1252 FcChar32 ch = unicode;
1232 XGlyphInfo g; 1253 XGlyphInfo g;
1233 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1254 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1234 1255
1235 int w = g.width; 1256 int w = g.width - g.x;
1236 int wcw = wcwidth (unicode); 1257 int wcw = wcwidth (unicode);
1237 if (wcw > 0) w /= wcw; 1258 if (wcw > 0) w = (w + wcw - 1) / wcw;
1238 1259
1239 careful = w > prop->width; 1260 careful = w > prop->width;
1240 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1261 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1241 return false; 1262 return false;
1242 1263
1246void 1267void
1247rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1268rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1248 const text_t *text, int len, 1269 const text_t *text, int len,
1249 int fg, int bg) 1270 int fg, int bg)
1250{ 1271{
1251 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1272 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1252 1273
1253 int base = ascent; // should be fbase, but that is incorrect 1274 int base = ascent; // should be fbase, but that is incorrect
1254 1275
1255 XGlyphInfo extents; 1276 XGlyphInfo extents;
1256 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1277 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1257 FcChar32 *ep = enc; 1278 FcChar32 *ep = enc;
1258 int ewidth = 0; 1279 int ewidth = 0;
1259 int xoff = 0;
1260 1280
1261 while (len) 1281 while (len)
1262 { 1282 {
1263 int cwidth = r->TermWin.fwidth; 1283 int cwidth = r->fwidth;
1264 FcChar32 fc = *text++; len--; 1284 FcChar32 fc = *text++; len--;
1265 FT_UInt gl;
1266 1285
1267 while (len && *text == NOCHAR) 1286 while (len && *text == NOCHAR)
1268 text++, len--, cwidth += r->TermWin.fwidth; 1287 text++, len--, cwidth += r->fwidth;
1269 1288
1270 gl = XftCharIndex (d.display->display, f, fc);
1271 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1272
1273 if (extents.xOff != cwidth && ep != enc)
1274 {
1275 if (xoff > ewidth) xoff = ewidth;
1276 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1277 x + (ewidth - xoff >> 1),
1278 y + base, enc, ep - enc);
1279 x += ewidth;
1280
1281 ep = enc;
1282 ewidth = 0;
1283 xoff = 0;
1284 }
1285
1286 if (fc == ' ' && ep == enc) // skip leading spaces 1289 if (fc == ' ' && ep == enc) // skip leading spaces
1287 {
1288 x += cwidth; 1290 x += cwidth;
1289 continue;
1290 }
1291
1292 else 1291 else
1293 { 1292 {
1293 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1294 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1295
1296 if (extents.xOff != cwidth)
1297 {
1298 if (ewidth)
1299 {
1300 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1301 x, y + base, enc, ep - enc);
1302 x += ewidth;
1303
1304 ep = enc;
1305 ewidth = 0;
1306 }
1307
1308 if (extents.xOff > cwidth)
1309 extents.xOff = cwidth;
1310
1311 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1312 x + (cwidth - extents.xOff >> 1),
1313 y + base, &gl, 1);
1314 x += cwidth;
1315 }
1316 else
1317 {
1294 *ep++ = gl; 1318 *ep++ = gl;
1295 ewidth += cwidth; 1319 ewidth += cwidth;
1296 xoff += extents.xOff; 1320 }
1297 } 1321 }
1298 } 1322 }
1299 1323
1300 if (ep != enc) 1324 if (ep != enc)
1301 {
1302 if (xoff > ewidth) xoff = ewidth;
1303 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1325 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1304 x + (ewidth - xoff >> 1),
1305 y + base, enc, ep - enc); 1326 x, y + base, enc, ep - enc);
1306 }
1307} 1327}
1308#endif 1328#endif
1309 1329
1310///////////////////////////////////////////////////////////////////////////// 1330/////////////////////////////////////////////////////////////////////////////
1311 1331
1312rxvt_fontset::rxvt_fontset (rxvt_t r) 1332rxvt_fontset::rxvt_fontset (rxvt_t r)
1313: r (r), fontdesc (0) 1333: fontdesc (0), r (r)
1314{ 1334{
1315 clear (); 1335 clear ();
1316} 1336}
1317 1337
1318rxvt_fontset::~rxvt_fontset () 1338rxvt_fontset::~rxvt_fontset ()
1321} 1341}
1322 1342
1323void 1343void
1324rxvt_fontset::clear () 1344rxvt_fontset::clear ()
1325{ 1345{
1326 prop.width = prop.height = prop.weight = prop.slant 1346 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1327 = rxvt_fontprop::unset; 1347 = rxvt_fontprop::unset;
1328 1348
1329 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1349 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1330 FONT_UNREF (*i); 1350 FONT_UNREF (*i);
1331 1351
1504 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1524 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1505 goto next_font; 1525 goto next_font;
1506 1526
1507 if (!realize_font (i)) 1527 if (!realize_font (i))
1508 goto next_font; 1528 goto next_font;
1529
1530 if (prop.ascent != rxvt_fontprop::unset)
1531 max_it (f->ascent, prop.ascent);
1509 } 1532 }
1510 1533
1511 if (f->cs == CS_UNKNOWN) 1534 if (f->cs == CS_UNKNOWN)
1512 goto next_font; 1535 goto next_font;
1513 1536

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines