FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Do a better job of estimating the width of UTF-8 encoded strings · gnuplot/gnuplot@ec8bfa3 · GitHub

Repository navigation

Commit ec8bfa3

Browse files
Ethan A Merritt
committed
Do a better job of estimating the width of UTF-8 encoded strings
1 parent 6a28b58 commit ec8bfa3

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

‎ChangeLog‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
2010-05-20 Ethan A Merritt <merritt@u.washington.edu>
2+
3+
* term/estimate.trm (ENHest_writec strlen_utf8)
4+
src/term.c (estimate_strlen):
5+
6+
Teach estimate_strlen() to handle UTF-8 encoded strings. The estimate
7+
is imperfect, but then again the estimate is already imperfect for any
8+
proportional font. To truly do this more accurately would require
9+
customized implementations for each terminal type. Any unicode code
10+
point below 0x3000 is treated as requiring one character width; code
11+
points above 0x3000 are treated as requiring two character widths.
12+
113
2010-05-16 Ethan A Merritt <merritt@u.washington.edu>
214

315
* src/hidden3d.c (draw_edge): Add support for coloring lines in

‎src/term.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#ifndef lint
2-
static char *RCSid() { return RCSid("$Id: term.c,v 1.202 2010/03/14 22:44:38 sfeam Exp $"); }
2+
static char *RCSid() { return RCSid("$Id: term.c,v 1.203 2010/05/05 18:03:50 sfeam Exp $"); }
33
#endif
44

55
/* GNUPLOT - term.c */
@@ -2789,7 +2789,9 @@ int len;
27892789
FPRINTF((stderr,"Estimating length %d height %g for enhanced text string \"%s\"\n",
27902790
len, (double)(term->ymax)/10., text));
27912791
term = tsave;
2792-
} else
2792+
} else if (encoding == S_ENC_UTF8)
2793+
len = strlen_utf8(text);
2794+
else
27932795
#endif
27942796
len = strlen(text);
27952797

‎term/estimate.trm‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Hello, Emacs, this is -*-C-*-
2-
* $Id: estimate.trm,v 1.7 2008/06/02 19:49:50 sfeam Exp $
2+
* $Id: estimate.trm,v 1.8 2009/03/26 00:49:20 sfeam Exp $
33
*
44
*/
55

@@ -44,6 +44,9 @@ static TBOOLEAN ENHest_widthflag = TRUE;
4444
#define ENHest_font ""
4545
static double ENHest_base;
4646

47+
/* Internal routines for UTF-8 support */
48+
static size_t strlen_utf8 __PROTO((const char *s));
49+
4750
TERM_PUBLIC void
4851
ENHest_OPEN(
4952
char *fontname,
@@ -123,7 +126,10 @@ ENHest_put_text(unsigned int x, unsigned int y, const char *str)
123126

124127
/* If no enhanced text processing is needed, strlen() is sufficient */
125128
if (ignore_enhanced_text || !strpbrk(str, "{}^_@&~\n")) {
126-
term->xmax = strlen(str);
129+
if (encoding == S_ENC_UTF8)
130+
term->xmax = strlen_utf8(str);
131+
else
132+
term->xmax = strlen(str);
127133
term->ymax = 10;
128134
return;
129135
}
@@ -158,7 +164,37 @@ ENHest_writec(int c)
158164
ENHest_x = 0;
159165
}
160166

161-
ENHest_fragment_width += ENHest_fontsize;
167+
if (encoding == S_ENC_UTF8) {
168+
/* Skip all but the first byte of UTF-8 multi-byte characters. */
169+
if ((c & 0xc0) != 0x80) {
170+
ENHest_fragment_width += ENHest_fontsize;
171+
/* [most] characters above 0x3000 are square CJK glyphs, */
172+
/* which are wider than western characters. */
173+
if ((unsigned int)c >= 0xec)
174+
ENHest_fragment_width += ENHest_fontsize;
175+
}
176+
} else
177+
ENHest_fragment_width += ENHest_fontsize;
178+
}
179+
180+
/*
181+
* This routine accounts for multi-byte characters in UTF-8.
182+
* NB: It does not return the _number_ of characters in the string, but
183+
* rather their approximate _width_ in units of typical character width.
184+
* As with the ENHest_writec() routine, it approximates the width of characters
185+
* above unicode 0x3000 as being twice that of western alphabetic characters.
186+
*/
187+
size_t strlen_utf8(const char *s) {
188+
int i = 0, j = 0;
189+
while (s[i]) {
190+
if ((s[i] & 0xc0) != 0x80) {
191+
j++;
192+
if ((unsigned char)(s[i]) >= 0xe3)
193+
j++;
194+
}
195+
i++;
196+
}
197+
return j;
162198
}
163199

164200

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL