summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthias Melcher <fltk@matthiasm.com>2010-12-06 18:22:22 +0000
committerMatthias Melcher <fltk@matthiasm.com>2010-12-06 18:22:22 +0000
commit1bac8a0ccae1f8993714e795d7da2e78245182d2 (patch)
tree9bde4789126d3e19b4baa98c76b9268c7c896624 /src
parent06e5a163cd6fffa89e5e941fbbc8f9d5ee9fe72d (diff)
Fixed crashes when Fl_Text_* detects illegal UTF 8 sequences. Widgets will not do any further processing but just jump over the character. Screen representation depends largely on whatever the underlying OS does with those sequences, but I feel that this is out of the scope of this library. (STR 2348)
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@7965 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
-rw-r--r--src/Fl_Text_Buffer.cxx6
-rw-r--r--src/Fl_Text_Display.cxx10
-rw-r--r--src/fl_utf8.cxx43
3 files changed, 40 insertions, 19 deletions
diff --git a/src/Fl_Text_Buffer.cxx b/src/Fl_Text_Buffer.cxx
index c70ae1692..d1aea36f1 100644
--- a/src/Fl_Text_Buffer.cxx
+++ b/src/Fl_Text_Buffer.cxx
@@ -1025,7 +1025,7 @@ int Fl_Text_Buffer::search_forward(int startPos, const char *searchString,
*foundPos = startPos;
return 1;
}
- int l = fl_utf8len(c);
+ int l = fl_utf8len1(c);
if (memcmp(sp, address(bp), l))
break;
sp += l; bp += l;
@@ -1077,7 +1077,7 @@ int Fl_Text_Buffer::search_backward(int startPos, const char *searchString,
*foundPos = startPos;
return 1;
}
- int l = fl_utf8len(c);
+ int l = fl_utf8len1(c);
if (memcmp(sp, address(bp), l))
break;
sp += l; bp += l;
@@ -1602,7 +1602,7 @@ int Fl_Text_Buffer::prev_char(int pos) const
int Fl_Text_Buffer::next_char(int pos) const
{
IS_UTF8_ALIGNED2(this, (pos))
- int n = fl_utf8len(byte_at(pos));
+ int n = fl_utf8len1(byte_at(pos));
pos += n;
if (pos>=mLength)
return mLength;
diff --git a/src/Fl_Text_Display.cxx b/src/Fl_Text_Display.cxx
index d54bbcca9..c55a0d17c 100644
--- a/src/Fl_Text_Display.cxx
+++ b/src/Fl_Text_Display.cxx
@@ -753,7 +753,7 @@ void Fl_Text_Display::overstrike(const char* text) {
/* determine how many displayed character positions are covered */
startIndent = mBuffer->count_displayed_characters( lineStart, startPos );
indent = startIndent;
- for ( c = text; *c != '\0'; c += fl_utf8len(*c) )
+ for ( c = text; *c != '\0'; c += fl_utf8len1(*c) )
indent++;
endIndent = indent;
@@ -1735,7 +1735,7 @@ int Fl_Text_Display::handle_vline(
style = position_style(lineStartPos, lineLen, 0);
for (i=0; i<lineLen; ) {
currChar = lineStr[i]; // one byte is enough to handele tabs and other cases
- int len = fl_utf8len(currChar);
+ int len = fl_utf8len1(currChar);
if (len<=0) len = 1; // OUCH!
charStyle = position_style(lineStartPos, lineLen, i);
if (charStyle!=style || currChar=='\t' || prevChar=='\t') {
@@ -1829,7 +1829,7 @@ int Fl_Text_Display::find_x(const char *s, int len, int style, int x) const {
// TODO: use binary search which may be quicker.
int i = 0;
while (i<len) {
- int cl = fl_utf8len(s[i]);
+ int cl = fl_utf8len1(s[i]);
int w = int( string_width(s, i+cl, style) );
if (w>x)
return i;
@@ -3204,7 +3204,7 @@ double Fl_Text_Display::measure_proportional_character(const char *s, int xPix,
return (((xPix/tab)+1)*tab) - xPix;
}
- int charLen = fl_utf8len(*s), style = 0;
+ int charLen = fl_utf8len1(*s), style = 0;
if (mStyleBuffer) {
style = mStyleBuffer->byte_at(pos);
}
@@ -3284,7 +3284,7 @@ int Fl_Text_Display::wrap_uses_character(int lineEndPos) const {
c = buffer()->char_at(lineEndPos);
return c == '\n' || ((c == '\t' || c == ' ') &&
- lineEndPos + fl_utf8len(c) < buffer()->length());
+ lineEndPos + fl_utf8len1(c) < buffer()->length());
}
diff --git a/src/fl_utf8.cxx b/src/fl_utf8.cxx
index 94aff0fb8..ccbe98e95 100644
--- a/src/fl_utf8.cxx
+++ b/src/fl_utf8.cxx
@@ -112,9 +112,11 @@ Toupper(
}
/**
- return the byte length of the UTF-8 sequence with first byte \p c,
- or -1 if \p c is not valid.
- */
+ return the byte length of the UTF-8 sequence with first byte \p c,
+ or -1 if \p c is not valid.
+ This function is helpful for finding faulty UTF8 sequences.
+ \see fl_utf8len1
+ */
int fl_utf8len(char c)
{
if (!(c & 0x80)) return 1;
@@ -137,15 +139,34 @@ int fl_utf8len(char c)
} // fl_utf8len
-#if 0
-int fl_utflen(
- const unsigned char *buf,
- int len)
+/**
+ Return the byte length of the UTF-8 sequence with first byte \p c,
+ or 1 if \p c is not valid.
+ This function can be used to scan faulty UTF8 sequence, albeit ignoring invalid
+ codes.
+ \see fl_utf8len
+ */
+int fl_utf8len1(char c)
{
- unsigned int ucs;
- return fl_utf2ucs(buf, len, &ucs);
-}
-#endif
+ if (!(c & 0x80)) return 1;
+ if (c & 0x40) {
+ if (c & 0x20) {
+ if (c & 0x10) {
+ if (c & 0x08) {
+ if (c & 0x04) {
+ return 6;
+ }
+ return 5;
+ }
+ return 4;
+ }
+ return 3;
+ }
+ return 2;
+ }
+ return 1;
+} // fl_utf8len1
+
/**
returns the number of Unicode chars in the UTF-8 string