Skip to content

Commit 81e327e

Browse files
committed
enable non-BMP width detection, tweak auto-narrowing character ranges (#1065)
1 parent 706d34f commit 81e327e

4 files changed

Lines changed: 73 additions & 43 deletions

File tree

‎src/term.c‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,22 +2355,25 @@ term_paint(void)
23552355
tattr.attr != (dispchars[j].attr.attr & ~(TATTR_NARROW | DATTR_MASK))
23562356
)
23572357
{
2358+
xchar xch = tchar;
2359+
if ((xch & 0xFC00) == 0xD800 && d->cc_next) {
2360+
termchar * cc = d + d->cc_next;
2361+
if ((cc->chr & 0xFC00) == 0xDC00) {
2362+
xch = ((xchar) (xch - 0xD7C0) << 10) | (cc->chr & 0x03FF);
2363+
}
2364+
}
2365+
23582366
if ((tattr.attr & TATTR_WIDE) == 0
2359-
&& win_char_width(tchar, tattr.attr) == 2
2367+
&& win_char_width(xch, tattr.attr) == 2
23602368
// && !(line->lattr & LATTR_MODE) ? "do not tamper with graphics"
23612369
// && is_ambigwide(tchar) ? but then they will be clipped...
23622370
)
23632371
{
23642372
//printf("[%d:%d] narrow? %04X..%04X\n", i, j, tchar, chars[j + 1].chr);
2365-
xchar ch = tchar;
2366-
if ((ch & 0xFC00) == 0xD800 && d->cc_next) {
2367-
termchar * cc = d + d->cc_next;
2368-
if ((cc->chr & 0xFC00) == 0xDC00) {
2369-
ch = ((xchar) (ch - 0xD7C0) << 10) | (cc->chr & 0x03FF);
2370-
}
2371-
}
2372-
if ((ch >= 0x2190 && ch <= 0x2BFF)
2373-
|| (ch >= 0x1F000 && ch <= 0x1FAFF)
2373+
if (
2374+
// do not narrow various symbol ranges
2375+
(xch >= 0x2190 && xch <= 0x25FF)
2376+
|| (xch >= 0x27C0 && xch <= 0x2BFF)
23742377
)
23752378
{
23762379
//tattr.attr |= TATTR_NARROW1; // ?
@@ -2381,7 +2384,7 @@ term_paint(void)
23812384
#endif
23822385
tattr.attr |= TATTR_NARROW;
23832386
//if (ch != 0x25CC)
2384-
//printf("char %lc U+%04X narrow %d ambig %d\n", ch, ch, !!(tattr.attr & TATTR_NARROW), is_ambigwide(ch));
2387+
//printf("char %lc U+%04X narrow %d ambig %d\n", xch, xch, !!(tattr.attr & TATTR_NARROW), is_ambigwide(xch));
23852388
}
23862389
else if (tattr.attr & TATTR_WIDE
23872390
// guard character expanding properly to avoid
@@ -2396,7 +2399,7 @@ term_paint(void)
23962399
// MS Mincho: wide Greek/Cyrillic but narrow æ, œ, ...
23972400
// SimSun, NSimSun, Yu Gothic
23982401
//&& !font_ambig_wide
2399-
&& win_char_width(tchar, tattr.attr) == 1
2402+
&& win_char_width(xch, tattr.attr) == 1
24002403
// and reassure to apply this only to ambiguous width chars
24012404
&& is_ambigwide(tchar) // is_ambig(tchar) && !is_wide(tchar)
24022405
// do not widen Geometric Shapes

‎src/termout.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,6 +4281,8 @@ term_do_write(const char *buf, uint len)
42814281
width = 2;
42824282
else if (term.wide_extra && wc >= 0x2000 && extrawide(wc)) {
42834283
width = 2;
4284+
// Note: this check is currently not implemented for
4285+
// non-BMP characters (see case if is_low_surrogate(wc) above)
42844286
if (win_char_width(wc, term.curs.attr.attr) < 2)
42854287
term.curs.attr.attr |= TATTR_EXPAND;
42864288
}

‎src/wintext.c‎

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,7 +4131,7 @@ win_check_glyphs(wchar *wcs, uint num, cattrflags attr)
41314131
ReleaseDC(wnd, dc);
41324132
}
41334133

4134-
#define dont_debug_win_char_width
4134+
#define dont_debug_win_char_width 2
41354135

41364136
#ifdef debug_win_char_width
41374137
int
@@ -4214,30 +4214,36 @@ win_char_width(xchar c, cattrflags attr)
42144214
#endif
42154215

42164216
int ibuf = 0;
4217-
bool ok = GetCharWidth32W(dc, c, c, &ibuf);
4217+
4218+
#ifdef use_getcharwidth
4219+
#warning avoid buggy GetCharWidth*
4220+
if (c < 0x10000) {
4221+
bool ok = GetCharWidth32W(dc, c, c, &ibuf);
42184222
#ifdef debug_win_char_width
4219-
printf(" getcharw %04X %dpx/cell %dpx\n", c, ibuf, cell_width);
4223+
printf(" getcharwidth32 %04X %dpx(/cell %dpx)\n", c, ibuf, cell_width);
42204224
#endif
4221-
if (!ok) {
4222-
ReleaseDC(wnd, dc);
4223-
return 0;
4224-
}
4225+
if (!ok) {
4226+
ReleaseDC(wnd, dc);
4227+
return 0;
4228+
}
42254229

4226-
// report char as wide if its width is more than 1½ cells;
4227-
// this is unreliable if font fallback is involved (#615)
4228-
ibuf += cell_width / 2 - 1;
4229-
ibuf /= cell_width;
4230-
if (ibuf > 1) {
4230+
// report char as wide if its width is more than 1½ cells;
4231+
// this is unreliable if font fallback is involved (#615)
4232+
ibuf += cell_width / 2 - 1;
4233+
ibuf /= cell_width;
4234+
if (ibuf > 1) {
42314235
#ifdef debug_win_char_width
4232-
printf(" enquired %04X %dpx/cell %dpx\n", c, ibuf, cell_width);
4236+
printf(" enquired %04X %dpx/cell %dpx\n", c, ibuf, cell_width);
42334237
#endif
4234-
ReleaseDC(wnd, dc);
4235-
//printf(" win_char_width %04X -> %d\n", c, ibuf);
4236-
return ibuf;
4238+
ReleaseDC(wnd, dc);
4239+
//printf(" win_char_width %04X -> %d\n", c, ibuf);
4240+
return ibuf;
4241+
}
42374242
}
4243+
#endif
42384244

42394245
#ifdef measure_width
4240-
int act_char_width(wchar wc)
4246+
int act_char_width(xchar wc)
42414247
{
42424248
HDC wid_dc = CreateCompatibleDC(dc);
42434249
HBITMAP wid_bm = CreateCompatibleBitmap(dc, cell_width * 2, cell_height);
@@ -4249,10 +4255,20 @@ win_char_width(xchar c, cattrflags attr)
42494255
SetBkMode(wid_dc, OPAQUE);
42504256
int dx = 0;
42514257
use_uniscribe = cfg.font_render == FR_UNISCRIBE;
4252-
text_out_start(wid_dc, &wc, 1, &dx);
4253-
text_out(wid_dc, 0, 0, ETO_OPAQUE, null, &wc, 1, &dx);
4258+
wchar wc2[2];
4259+
if (wc < 0x10000) {
4260+
*wc2 = wc;
4261+
text_out_start(wid_dc, wc2, 1, &dx);
4262+
text_out(wid_dc, 0, 0, ETO_OPAQUE, null, wc2, 1, &dx);
4263+
}
4264+
else {
4265+
wc2[0] = high_surrogate(wc);
4266+
wc2[1] = low_surrogate(wc);
4267+
text_out_start(wid_dc, wc2, 2, &dx);
4268+
text_out(wid_dc, 0, 0, ETO_OPAQUE, null, wc2, 2, &dx);
4269+
}
42544270
text_out_end();
4255-
# ifdef debug_win_char_width
4271+
# if defined(debug_win_char_width) && debug_win_char_width > 1
42564272
for (int y = 0; y < cell_height; y++) {
42574273
printf(" %2d|", y);
42584274
for (int x = 0; x < cell_width * 2; x++) {
@@ -4294,23 +4310,28 @@ win_char_width(xchar c, cattrflags attr)
42944310
// although FONT_WIDE is actually activated
42954311
}
42964312

4297-
if ((c >= 0x3000 && c <= 0x303F)
4298-
|| (c >= 0x2460 && c <= 0x24FF) // Enclosed Alphanumerics
4299-
|| (c >= 0x2600 && c <= 0x27BF) // Miscellaneous Symbols, Dingbats
4313+
if ((c >= 0x3000 && c <= 0x303F) // CJK Symbols and Punctuation
4314+
4315+
|| (c >= 0x2460 && c <= 0x24FF) // Enclosed Alphanumerics, to cover:
43004316
//|| (c >= 0x249C && c <= 0x24E9) // parenthesized/circled letters
4301-
|| (c >= 0x3248 && c <= 0x324F) // Enclosed CJK Letters and Months
4317+
4318+
//|| (c >= 0x2600 && c <= 0x27BF) // Miscellaneous Symbols, Dingbats
4319+
|| c == 0x26AC
4320+
4321+
|| (c >= 0x3248 && c <= 0x324F) // circled CJK Numbers
43024322
|| (c >= 0x1F100 && c <= 0x1F1FF) // Enclosed Alphanumeric Supplement
4303-
#ifdef check_more_mostly_covered_below
4304-
|| (c >= 0x2100 && c <= 0x23FF) // Letterlike, Number Forms, Arrows, Math Operators, Misc Technical
4305-
|| (c >= 0x25A0 && c <= 0x25FF) // Geometric Shapes
4306-
|| (c >= 0x2B00 && c <= 0x2BFF) // Miscellaneous Symbols and Arrows
4307-
#endif
4323+
4324+
|| c == 0x2139 // Letterlike: Information Source
4325+
|| (c >= 0x2180 && c <= 0x2182) // Number Forms: combined Roman Numerals
4326+
|| (c >= 0x2187 && c <= 0x2188) // Number Forms: combined Roman Numerals
4327+
43084328
|| (c >= 0xE000 && c < 0xF900) // Private Use Area
4329+
43094330
|| (// check all non-letters with some exceptions
43104331
bidi_class(c) != L // indicates not a letter
4311-
&&
4332+
&& // do not check these non-letters:
43124333
!( (c >= 0x2500 && c <= 0x2588) // Box Drawing, Block Elements
4313-
|| (c >= 0x2592 && c <= 0x2594) // Block Elements
4334+
|| (c >= 0x2591 && c <= 0x2594) // Block Elements
43144335
|| (c >= 0x2160 && c <= 0x2179) // Roman Numerals
43154336
//|| wcschr (W("‐‑‘’‚‛“”„‟‹›"), c) // #712 workaround; now caching
43164337
)

‎wiki/Changelog.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Terminal features
66
* DECTST colour fillings (CSI 4;10..13 y, VT240).
77
* Smart detection of progress indication also inmidst line (mintty/wsltty#202).
88

9+
Font rendering
10+
* Enabled width detection for auto-narrowing non-BMP characters (#1065).
11+
* Tweaked character ranges to consider for auto-narrowing (#1065).
12+
913
Window handling
1014
* Optionally transform Exit to characters, to exit on application-level (#1063).
1115

0 commit comments

Comments
 (0)