From d083bd8fe067d424f5193ea8ff790964f0544161 Mon Sep 17 00:00:00 2001 From: "Steffen (Daode) Nurpmeso" Date: Tue, 3 Jun 2014 15:23:33 +0200 Subject: [PATCH] colalign(): use bidi_info_needed(), bidi_info_create() --- auxlily.c | 95 ++++++++++++++------------------------------------------------- 1 file changed, 20 insertions(+), 75 deletions(-) diff --git a/auxlily.c b/auxlily.c index 9174e83b..08646f9c 100644 --- a/auxlily.c +++ b/auxlily.c @@ -1658,16 +1658,7 @@ field_detect_clip(size_t maxlen, char const *buf, size_t blen)/*TODO mbrtowc()*/ FL char * colalign(char const *cp, int col, int fill, int *cols_decr_used_or_null) { - /* Unicode: how to isolate RIGHT-TO-LEFT scripts via *headline-bidi* - * 1.1 (Jun 1993): U+200E (E2 80 8E) LEFT-TO-RIGHT MARK - * 6.3 (Sep 2013): U+2068 (E2 81 A8) FIRST STRONG ISOLATE, - * U+2069 (E2 81 A9) POP DIRECTIONAL ISOLATE */ -#ifdef HAVE_NATCH_CHAR - char const _bire[2][6] = { "\xE2\x81\xA8" "\xE2\x81\xA9", - "\xE2\x80\x8E" "\xE2\x80\x8E" - /* worse results: U+202D "\xE2\x80\xAD" U+202C "\xE2\x80\xAC" */ - }, *birep = NULL /* old gcc warning */; -#endif + NATCH_CHAR( struct bidi_info bi; ) int col_orig = col, n, sz; bool_t isbidi, isuni, isrepl; char *nb, *np; @@ -1676,73 +1667,29 @@ colalign(char const *cp, int col, int fill, int *cols_decr_used_or_null) /* Bidi only on request and when there is 8-bit data */ isbidi = isuni = FAL0; #ifdef HAVE_NATCH_CHAR - if ((isuni = ((options & OPT_UNICODE) != 0))) { - if ((nb = ok_vlook(headline_bidi)) == NULL) - goto jnobidi; - for (birep = cp;; ++birep) { - if (*birep == '\0') - goto jnobidi; - else if (*birep & (char)0x80) { - /* TODO Checking for BIDI character: use S-CText fromutf8 - * TODO plus isrighttoleft (or whatever there will be)! */ - ui32_t c, x = (ui8_t)*birep; - if ((x & 0xE0) == 0xC0) { - c = x & ~0xC0; - if ((x = (ui8_t)*++birep) == '\0') - goto jnobidi; - } else if ((x & 0xF0) == 0xE0) { - c = x & ~0xE0; - if ((x = (ui8_t)*++birep) == '\0' || *++birep == '\0') - goto jnobidi; - c <<= 6; - c |= x & 0x7F; - x = (ui8_t)*birep; - } else { - c = x & ~0xF0; - if ((x = (ui8_t)*++birep) == '\0' || - *++birep == '\0' || birep[1] == '\0') - goto jnobidi; - c <<= 6; - c |= x & 0x7F; - c <<= 6; - c |= (x = (ui8_t)*birep) & 0x7F; - x = (ui8_t)*++birep; - } - c <<= 6; - c |= x & 0x7F; - /* (Very very fuzzy, awaiting S-CText for good) */ - if ((c >= 0x05BE && c <= 0x08E3) || - (c >= 0xFB1D && c <= 0xFEFC) || - (c >= 0x10800 && c <= 0x10C48) || - (c >= 0x1EE00 && c <= 0x1EEF1)) - break; - } - } - - isbidi = TRU1; - birep = _bire[0]; - switch (*nb) { - case '3': col -= 2; - case '2': birep = _bire[1]; - break; - case '1': col -= 2; - default: break; - } - if (col < 0) - col = 0; - } + isuni = ((options & OPT_UNICODE) != 0); + bidi_info_create(&bi); + if (bi.bi_start.l == 0) + goto jnobidi; + if (!(isbidi = bidi_info_needed(cp, strlen(cp)))) + goto jnobidi; + + if ((size_t)col >= bi.bi_pad) + col -= bi.bi_pad; + else + col = 0; jnobidi: -#endif /* HAVE_NATCH_CHAR */ +#endif np = nb = salloc(mb_cur_max * strlen(cp) + - ((fill ? col : 0) + (isbidi ? sizeof(_bire[0]) : 0) +1)); + ((fill ? col : 0) + NATCH_CHAR( + (isbidi ? bi.bi_start.l + bi.bi_end.l : 0) ) + +1)); #ifdef HAVE_NATCH_CHAR if (isbidi) { - np[0] = birep[0]; - np[1] = birep[1]; - np[2] = birep[2]; - np += 3; + memcpy(np, bi.bi_start.s, bi.bi_start.l); + np += bi.bi_start.l; } #endif @@ -1803,10 +1750,8 @@ jnobidi: #ifdef HAVE_NATCH_CHAR if (isbidi) { - np[0] = birep[3]; - np[1] = birep[4]; - np[2] = birep[5]; - np += 3; + memcpy(np, bi.bi_end.s, bi.bi_end.l); + np += bi.bi_end.l; } #endif -- 2.11.4.GIT