1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: rxvtfont.C |
2 | * File: rxvtfont.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
4 | * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com> |
5 | * - original version. |
5 | * - original version. |
6 | * |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
9 | * the Free Software Foundation; either version 2 of the License, or |
… | |
… | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | *---------------------------------------------------------------------*/ |
20 | *---------------------------------------------------------------------*/ |
21 | |
21 | |
22 | #include "../config.h" |
22 | #include "../config.h" |
23 | #include "rxvt.h" |
23 | #include "rxvt.h" |
|
|
24 | #include "rxvtlib.h" |
24 | #include "rxvtutil.h" |
25 | #include "rxvtutil.h" |
25 | #include "rxvtfont.h" |
26 | #include "rxvtfont.h" |
26 | |
27 | |
27 | #include <cstdlib> |
28 | #include <cstdlib> |
28 | #include <wchar.h> |
29 | #include <wchar.h> |
29 | #include <inttypes.h> |
30 | #include <inttypes.h> |
30 | |
31 | |
31 | #define DISPLAY r->display->display |
32 | #define DISPLAY r->display->display |
32 | #define TGC r->TermWin.gc |
33 | #define TGC r->gc |
33 | |
34 | |
34 | #define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width |
35 | #define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width |
35 | |
36 | |
36 | const struct rxvt_fallback_font { |
37 | const struct rxvt_fallback_font { |
37 | codeset cs; |
38 | codeset cs; |
… | |
… | |
276 | rxvt_fontprop properties () |
277 | rxvt_fontprop properties () |
277 | { |
278 | { |
278 | rxvt_fontprop p; |
279 | rxvt_fontprop p; |
279 | |
280 | |
280 | p.width = p.height = 1; |
281 | p.width = p.height = 1; |
|
|
282 | p.ascent = rxvt_fontprop::unset; |
281 | p.weight = rxvt_fontprop::medium; |
283 | p.weight = rxvt_fontprop::medium; |
282 | p.slant = rxvt_fontprop::roman; |
284 | p.slant = rxvt_fontprop::roman; |
283 | |
285 | |
284 | return p; |
286 | return p; |
285 | } |
287 | } |
… | |
… | |
305 | return false; |
307 | return false; |
306 | |
308 | |
307 | if (unicode <= 0x009f) |
309 | if (unicode <= 0x009f) |
308 | return true; |
310 | return true; |
309 | |
311 | |
310 | if (unicode >= 0x2500 && unicode <= 0x259f) |
312 | if (unicode >= 0x2500 && unicode <= 0x259f && |
|
|
313 | !r->option (Opt_skipBuiltinGlyphs)) |
311 | return true; |
314 | return true; |
312 | |
315 | |
313 | if (IS_COMPOSE (unicode)) |
316 | if (IS_COMPOSE (unicode)) |
314 | return true; |
317 | return true; |
315 | |
318 | |
… | |
… | |
333 | const text_t *text, int len, |
336 | const text_t *text, int len, |
334 | int fg, int bg) |
337 | int fg, int bg) |
335 | { |
338 | { |
336 | Display *disp = d.display->display; |
339 | Display *disp = d.display->display; |
337 | |
340 | |
338 | clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
341 | clear_rect (d, x, y, r->fwidth * len, r->fheight, bg); |
339 | |
342 | |
340 | XSetForeground (disp, TGC, r->pix_colors[fg]); |
343 | XSetForeground (disp, TGC, r->pix_colors[fg]); |
341 | |
344 | |
342 | while (len) |
345 | while (len) |
343 | { |
346 | { |
… | |
… | |
349 | |
352 | |
350 | while (++text, --len && *text == NOCHAR) |
353 | while (++text, --len && *text == NOCHAR) |
351 | ; |
354 | ; |
352 | |
355 | |
353 | int width = text - tp; |
356 | int width = text - tp; |
354 | int fwidth = r->TermWin.fwidth * width; |
357 | int fwidth = r->fwidth * width; |
355 | |
358 | |
356 | if (0x2500 <= t && t <= 0x259f) |
359 | if (0x2500 <= t && t <= 0x259f) |
357 | { |
360 | { |
358 | uint16_t offs = linedraw_offs[t - 0x2500]; |
361 | uint16_t offs = linedraw_offs[t - 0x2500]; |
359 | uint32_t *a = linedraw_command + (offs >> 4); |
362 | uint32_t *a = linedraw_command + (offs >> 4); |
360 | uint32_t *b = a + (offs & 15); |
363 | uint32_t *b = a + (offs & 15); |
361 | |
364 | |
362 | int W = fwidth; |
365 | int W = fwidth; |
363 | int H = r->TermWin.fheight; |
366 | int H = r->fheight; |
364 | |
367 | |
365 | int x_[16]; |
368 | int x_[16]; |
366 | int y_[16]; |
369 | int y_[16]; |
367 | |
370 | |
368 | for (int i = 0; i <= 8; i++) |
371 | for (int i = 0; i <= 8; i++) |
… | |
… | |
464 | case NOCHAR: |
467 | case NOCHAR: |
465 | break; |
468 | break; |
466 | |
469 | |
467 | default: |
470 | default: |
468 | XDrawRectangle (disp, d, TGC, x + 2, y + 2, |
471 | XDrawRectangle (disp, d, TGC, x + 2, y + 2, |
469 | fwidth - 4, r->TermWin.fheight - 4); |
472 | fwidth - 4, r->fheight - 4); |
470 | } |
473 | } |
471 | |
474 | |
472 | x += fwidth; |
475 | x += fwidth; |
473 | } |
476 | } |
474 | } |
477 | } |
… | |
… | |
523 | bool |
526 | bool |
524 | rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) |
527 | rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) |
525 | { |
528 | { |
526 | p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; |
529 | p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; |
527 | p.height = height; |
530 | p.height = height; |
|
|
531 | p.ascent = rxvt_fontprop::unset; |
528 | p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
532 | 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; |
533 | p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; |
530 | |
534 | |
531 | return true; |
535 | return true; |
532 | } |
536 | } |
… | |
… | |
552 | |
556 | |
553 | set_properties (p, height, weight, slant, avgwidth); |
557 | set_properties (p, height, weight, slant, avgwidth); |
554 | |
558 | |
555 | free (weight); |
559 | free (weight); |
556 | free (slant); |
560 | free (slant); |
|
|
561 | |
|
|
562 | p.ascent = f->ascent; |
557 | |
563 | |
558 | return true; |
564 | return true; |
559 | } |
565 | } |
560 | |
566 | |
561 | bool |
567 | bool |
… | |
… | |
956 | // and it is a mess /. |
962 | // and it is a mess /. |
957 | // yet we are trying to be perfect /. |
963 | // yet we are trying to be perfect /. |
958 | // but the result still isn't perfect /. |
964 | // but the result still isn't perfect /. |
959 | |
965 | |
960 | bool slow = this->slow |
966 | bool slow = this->slow |
961 | || width != r->TermWin.fwidth |
967 | || width != r->fwidth |
962 | || height != r->TermWin.fheight; |
968 | || height != r->fheight; |
963 | |
969 | |
964 | int base = ascent; // sorry, incorrect: r->TermWin.fbase; |
970 | int base = ascent; // sorry, incorrect: r->fbase; |
965 | |
971 | |
966 | XGCValues v; |
972 | XGCValues v; |
967 | v.foreground = r->pix_colors[fg]; |
973 | v.foreground = r->pix_colors[fg]; |
968 | v.font = f->fid; |
974 | v.font = f->fid; |
969 | |
975 | |
… | |
… | |
977 | XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); |
983 | XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); |
978 | XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); |
984 | XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); |
979 | } |
985 | } |
980 | else |
986 | else |
981 | { |
987 | { |
982 | clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
988 | clear_rect (d, x, y, r->fwidth * len, r->fheight, bg); |
983 | |
989 | |
984 | XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); |
990 | XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); |
985 | |
991 | |
986 | if (slow) |
992 | if (slow) |
987 | { |
993 | { |
988 | do |
994 | do |
989 | { |
995 | { |
990 | if (xc->byte1 || xc->byte2) |
996 | if (xc->byte1 || xc->byte2) |
991 | XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); |
997 | XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); |
992 | |
998 | |
993 | x += r->TermWin.fwidth; |
999 | x += r->fwidth; |
994 | xc++; len--; |
1000 | xc++; len--; |
995 | } |
1001 | } |
996 | while (len); |
1002 | while (len); |
997 | } |
1003 | } |
998 | else |
1004 | else |
… | |
… | |
1009 | XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); |
1015 | XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); |
1010 | XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); |
1016 | XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); |
1011 | } |
1017 | } |
1012 | else |
1018 | else |
1013 | { |
1019 | { |
1014 | clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
1020 | clear_rect (d, x, y, r->fwidth * len, r->fheight, bg); |
1015 | |
1021 | |
1016 | XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); |
1022 | XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); |
1017 | |
1023 | |
1018 | if (slow) |
1024 | if (slow) |
1019 | { |
1025 | { |
1020 | do |
1026 | do |
1021 | { |
1027 | { |
1022 | if (*xc) |
1028 | if (*xc) |
1023 | XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); |
1029 | XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); |
1024 | |
1030 | |
1025 | x += r->TermWin.fwidth; |
1031 | x += r->fwidth; |
1026 | xc++; len--; |
1032 | xc++; len--; |
1027 | } |
1033 | } |
1028 | while (len); |
1034 | while (len); |
1029 | } |
1035 | } |
1030 | else |
1036 | else |
… | |
… | |
1073 | |
1079 | |
1074 | FT_Face face = XftLockFace (f); |
1080 | FT_Face face = XftLockFace (f); |
1075 | |
1081 | |
1076 | p.width = width; |
1082 | p.width = width; |
1077 | p.height = height; |
1083 | p.height = height; |
|
|
1084 | p.ascent = ascent; |
1078 | p.weight = face->style_flags & FT_STYLE_FLAG_BOLD |
1085 | p.weight = face->style_flags & FT_STYLE_FLAG_BOLD |
1079 | ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
1086 | ? rxvt_fontprop::bold : rxvt_fontprop::medium; |
1080 | p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC |
1087 | p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC |
1081 | ? rxvt_fontprop::italic : rxvt_fontprop::roman; |
1088 | ? rxvt_fontprop::italic : rxvt_fontprop::roman; |
1082 | |
1089 | |
… | |
… | |
1183 | if (width < g.width ) width = g.width; |
1190 | if (width < g.width ) width = g.width; |
1184 | if (height < g.height ) height = g.height; |
1191 | if (height < g.height ) height = g.height; |
1185 | if (glheight < g.height - g.y) glheight = g.height - g.y; |
1192 | if (glheight < g.height - g.y) glheight = g.height - g.y; |
1186 | } |
1193 | } |
1187 | |
1194 | |
|
|
1195 | if (!width) |
|
|
1196 | { |
|
|
1197 | rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name); |
|
|
1198 | |
|
|
1199 | XftFontClose (disp, f); |
|
|
1200 | f = 0; |
|
|
1201 | |
|
|
1202 | success = false; |
|
|
1203 | break; |
|
|
1204 | } |
|
|
1205 | |
1188 | if (prop.height == rxvt_fontprop::unset |
1206 | if (prop.height == rxvt_fontprop::unset |
1189 | || (height <= prop.height && glheight <= prop.height) |
1207 | || (height <= prop.height && glheight <= prop.height) |
1190 | || height <= 2 |
1208 | || height <= 2 |
1191 | || !scalable) |
1209 | || !scalable) |
1192 | break; |
1210 | break; |
… | |
… | |
1201 | ftheight -= height - prop.height; |
1219 | ftheight -= height - prop.height; |
1202 | } |
1220 | } |
1203 | else |
1221 | else |
1204 | ftheight = prop.height - 1; |
1222 | ftheight = prop.height - 1; |
1205 | |
1223 | |
1206 | XftFontClose (disp, f); |
1224 | XftFontClose (disp, f); |
1207 | FcPatternDel (match, FC_PIXEL_SIZE); |
1225 | FcPatternDel (match, FC_PIXEL_SIZE); |
1208 | FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); |
1226 | FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); |
1209 | } |
1227 | } |
1210 | |
1228 | |
1211 | FcPatternDestroy (match); |
1229 | FcPatternDestroy (match); |
1212 | |
1230 | |
1213 | #if 0 // do it per-character |
1231 | #if 0 // do it per-character |
… | |
… | |
1251 | void |
1269 | void |
1252 | rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, |
1270 | rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, |
1253 | const text_t *text, int len, |
1271 | const text_t *text, int len, |
1254 | int fg, int bg) |
1272 | int fg, int bg) |
1255 | { |
1273 | { |
1256 | clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); |
1274 | clear_rect (d, x, y, r->fwidth * len, r->fheight, bg); |
1257 | |
|
|
1258 | int base = ascent; // should be fbase, but that is incorrect |
|
|
1259 | |
1275 | |
1260 | XGlyphInfo extents; |
1276 | XGlyphInfo extents; |
1261 | FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); |
1277 | XftGlyphSpec *enc = (XftGlyphSpec *)get_enc_buf (len * sizeof (XftGlyphSpec)); |
1262 | FcChar32 *ep = enc; |
1278 | XftGlyphSpec *ep = enc; |
1263 | int ewidth = 0; |
1279 | |
|
|
1280 | // cut trailing spaces |
|
|
1281 | while (len && text [len - 1] == ' ') |
|
|
1282 | len--; |
1264 | |
1283 | |
1265 | while (len) |
1284 | while (len) |
1266 | { |
1285 | { |
1267 | int cwidth = r->TermWin.fwidth; |
1286 | int cwidth = r->fwidth; |
1268 | FcChar32 fc = *text++; len--; |
1287 | FcChar32 fc = *text++; len--; |
1269 | |
1288 | |
1270 | while (len && *text == NOCHAR) |
1289 | while (len && *text == NOCHAR) |
1271 | text++, len--, cwidth += r->TermWin.fwidth; |
1290 | text++, len--, cwidth += r->fwidth; |
1272 | |
1291 | |
1273 | if (fc == ' ' && ep == enc) // skip leading spaces |
1292 | if (fc != ' ') // skip spaces |
1274 | x += cwidth; |
|
|
1275 | else |
|
|
1276 | { |
1293 | { |
1277 | FT_UInt gl = XftCharIndex (d.display->display, f, fc); |
1294 | FT_UInt glyph = XftCharIndex (d.display->display, f, fc); |
1278 | XftGlyphExtents (d.display->display, f, &gl, 1, &extents); |
1295 | XftGlyphExtents (d.display->display, f, &glyph, 1, &extents); |
1279 | |
1296 | |
1280 | if (extents.xOff != cwidth) |
1297 | ep->glyph = glyph; |
1281 | { |
1298 | ep->x = x + (cwidth - extents.xOff >> 1); |
1282 | if (ewidth) |
1299 | ep->y = y + ascent; |
1283 | { |
1300 | ep++; |
1284 | XftDrawGlyphs (d, &r->pix_colors[fg].c, f, |
|
|
1285 | x, y + base, enc, ep - enc); |
|
|
1286 | x += ewidth; |
|
|
1287 | |
|
|
1288 | ep = enc; |
|
|
1289 | ewidth = 0; |
|
|
1290 | } |
|
|
1291 | |
|
|
1292 | if (extents.xOff > cwidth) |
|
|
1293 | extents.xOff = cwidth; |
|
|
1294 | |
|
|
1295 | XftDrawGlyphs (d, &r->pix_colors[fg].c, f, |
|
|
1296 | x + (cwidth - extents.xOff >> 1), |
|
|
1297 | y + base, &gl, 1); |
|
|
1298 | x += cwidth; |
|
|
1299 | } |
1301 | } |
1300 | else |
1302 | |
1301 | { |
1303 | x += cwidth; |
1302 | *ep++ = gl; |
|
|
1303 | ewidth += cwidth; |
|
|
1304 | } |
|
|
1305 | } |
|
|
1306 | } |
1304 | } |
1307 | |
1305 | |
1308 | if (ep != enc) |
1306 | if (ep != enc) |
1309 | XftDrawGlyphs (d, &r->pix_colors[fg].c, f, |
1307 | XftDrawGlyphSpec (d, &r->pix_colors[fg].c, f, enc, ep - enc); |
1310 | x, y + base, enc, ep - enc); |
|
|
1311 | } |
1308 | } |
1312 | #endif |
1309 | #endif |
1313 | |
1310 | |
1314 | ///////////////////////////////////////////////////////////////////////////// |
1311 | ///////////////////////////////////////////////////////////////////////////// |
1315 | |
1312 | |
1316 | rxvt_fontset::rxvt_fontset (rxvt_t r) |
1313 | rxvt_fontset::rxvt_fontset (rxvt_t r) |
1317 | : r (r), fontdesc (0) |
1314 | : fontdesc (0), r (r) |
1318 | { |
1315 | { |
1319 | clear (); |
1316 | clear (); |
1320 | } |
1317 | } |
1321 | |
1318 | |
1322 | rxvt_fontset::~rxvt_fontset () |
1319 | rxvt_fontset::~rxvt_fontset () |
… | |
… | |
1325 | } |
1322 | } |
1326 | |
1323 | |
1327 | void |
1324 | void |
1328 | rxvt_fontset::clear () |
1325 | rxvt_fontset::clear () |
1329 | { |
1326 | { |
1330 | prop.width = prop.height = prop.weight = prop.slant |
1327 | prop.width = prop.height = prop.ascent = prop.weight = prop.slant |
1331 | = rxvt_fontprop::unset; |
1328 | = rxvt_fontprop::unset; |
1332 | |
1329 | |
1333 | for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) |
1330 | for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) |
1334 | FONT_UNREF (*i); |
1331 | FONT_UNREF (*i); |
1335 | |
1332 | |
… | |
… | |
1508 | if (FROM_UNICODE (f->cs, unicode) == NOCHAR) |
1505 | if (FROM_UNICODE (f->cs, unicode) == NOCHAR) |
1509 | goto next_font; |
1506 | goto next_font; |
1510 | |
1507 | |
1511 | if (!realize_font (i)) |
1508 | if (!realize_font (i)) |
1512 | goto next_font; |
1509 | goto next_font; |
|
|
1510 | |
|
|
1511 | if (prop.ascent != rxvt_fontprop::unset) |
|
|
1512 | max_it (f->ascent, prop.ascent); |
1513 | } |
1513 | } |
1514 | |
1514 | |
1515 | if (f->cs == CS_UNKNOWN) |
1515 | if (f->cs == CS_UNKNOWN) |
1516 | goto next_font; |
1516 | goto next_font; |
1517 | |
1517 | |