diff options
author | Zoolooc <lucianm@users.sourceforge.net> | 2011-03-07 22:12:13 +0100 |
---|---|---|
committer | Zoolooc <lucianm@users.sourceforge.net> | 2011-03-07 22:12:13 +0100 |
commit | aacb78d4335e498417835d518c64b33acb58c749 (patch) | |
tree | 463ecd627f9eaa55035d8777a9c5317668a3347f | |
parent | 379c2fefdeb4ae8d55add2d9ae44ea274cb7937b (diff) | |
download | graphlcd-base-aacb78d4335e498417835d518c64b33acb58c749.tar.gz graphlcd-base-aacb78d4335e498417835d518c64b33acb58c749.tar.bz2 |
consolidate Uf8 sequence detection in Utf8CodeAdjustCounter
-rw-r--r-- | glcdgraphics/bitmap.c | 30 | ||||
-rw-r--r-- | glcdgraphics/font.c | 49 | ||||
-rw-r--r-- | glcdgraphics/font.h | 2 |
3 files changed, 24 insertions, 57 deletions
diff --git a/glcdgraphics/bitmap.c b/glcdgraphics/bitmap.c index 8570da5..fe4b497 100644 --- a/glcdgraphics/bitmap.c +++ b/glcdgraphics/bitmap.c @@ -506,12 +506,8 @@ int cBitmap::DrawText(int x, int y, int xmax, const std::string & text, const cF { int xt; int yt; - int i; + unsigned int i; int c; - int c0; - int c1; - int c2; - int c3; int start; clip(x, 0, width - 1); @@ -551,28 +547,7 @@ int cBitmap::DrawText(int x, int y, int xmax, const std::string & text, const cF } for (i = start; i < (int) text.length(); i++) { - c = text[i]; - c0 = text[i]; - c1 = (i+1 < text.length()) ? text[i+1] : 0; - c2 = (i+2 < text.length()) ? text[i+2] : 0; - c3 = (i+3 < text.length()) ? text[i+3] : 0; - c0 &=0xff; c1 &=0xff; c2 &=0xff; c3 &=0xff; - - if( c0 >= 0xc2 && c0 <= 0xdf && c1 >= 0x80 && c1 <= 0xbf ){ //2 byte UTF-8 sequence found - i+=1; - c = ((c0&0x1f)<<6) | (c1&0x3f); - }else if( (c0 == 0xE0 && c1 >= 0xA0 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf) - || (c0 >= 0xE1 && c1 <= 0xEC && c1 >= 0x80 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf) - || (c0 == 0xED && c1 >= 0x80 && c1 <= 0x9f && c2 >= 0x80 && c2 <= 0xbf) - || (c0 >= 0xEE && c0 <= 0xEF && c1 >= 0x80 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf) ){ //3 byte UTF-8 sequence found - c = ((c0&0x0f)<<4) | ((c1&0x3f)<<6) | (c2&0x3f); - i+=2; - }else if( (c0 == 0xF0 && c1 >= 0x90 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf && c3 >= 0x80 && c3 <= 0xbf) - || (c0 >= 0xF1 && c0 >= 0xF3 && c1 >= 0x80 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf && c3 >= 0x80 && c3 <= 0xbf) - || (c0 == 0xF4 && c1 >= 0x80 && c1 <= 0x8f && c2 >= 0x80 && c2 <= 0xbf && c3 >= 0x80 && c3 <= 0xbf) ){ //4 byte UTF-8 sequence found - c = ((c0&0x07)<<2) | ((c1&0x3f)<<4) | ((c2&0x3f)<<6) | (c3&0x3f); - i+=3; - } + cFont::Utf8CodeAdjustCounter(text, c, i); if (xt > xmax) { @@ -832,3 +807,4 @@ void cBitmap::SavePBM(const std::string & fileName) } } // end of namespace + diff --git a/glcdgraphics/font.c b/glcdgraphics/font.c index 2730b91..3b71247 100644 --- a/glcdgraphics/font.c +++ b/glcdgraphics/font.c @@ -313,13 +313,10 @@ int cFont::Width(int ch) const return 0; } -int cFont::Width(const std::string & str) const +void cFont::Utf8CodeAdjustCounter(const std::string & str, int & c, unsigned int & i) { - unsigned int i; - int sum = 0; - int c,c0,c1,c2,c3,symcount=0; - - for (i = 0; i < str.length(); i++) + int c0,c1,c2,c3; + if (i < str.length()) { c = str[i]; c0 = str[i]; @@ -343,6 +340,19 @@ int cFont::Width(const std::string & str) const c = ((c0&0x07)<<2) | ((c1&0x3f)<<4) | ((c2&0x3f)<<6) | (c3&0x3f); i+=3; } + } +} + +int cFont::Width(const std::string & str) const +{ + unsigned int i; + int sum = 0; + int symcount=0; + int c; + + for (i = 0; i < str.length(); i++) + { + Utf8CodeAdjustCounter(str, c, i); symcount++; sum += Width(c); } @@ -354,33 +364,12 @@ int cFont::Width(const std::string & str, unsigned int len) const { unsigned int i; int sum = 0; - - int c,c0,c1,c2,c3,symcount=0; + int symcount=0; + int c; for (i = 0; i < str.length() && symcount < len; i++) { - c = str[i]; - c0 = str[i]; - c1 = (i+1 < str.length()) ? str[i+1] : 0; - c2 = (i+2 < str.length()) ? str[i+2] : 0; - c3 = (i+3 < str.length()) ? str[i+3] : 0; - c0 &=0xff; c1 &=0xff; c2 &=0xff; c3 &=0xff; - - if( c0 >= 0xc2 && c0 <= 0xdf && c1 >= 0x80 && c1 <= 0xbf ){ //2 byte UTF-8 sequence found - i+=1; - c = ((c0&0x1f)<<6) | (c1&0x3f); - }else if( (c0 == 0xE0 && c1 >= 0xA0 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf) - || (c0 >= 0xE1 && c1 <= 0xEC && c1 >= 0x80 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf) - || (c0 == 0xED && c1 >= 0x80 && c1 <= 0x9f && c2 >= 0x80 && c2 <= 0xbf) - || (c0 >= 0xEE && c0 <= 0xEF && c1 >= 0x80 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf) ){ //3 byte UTF-8 sequence found - c = ((c0&0x0f)<<4) | ((c1&0x3f)<<6) | (c2&0x3f); - i+=2; - }else if( (c0 == 0xF0 && c1 >= 0x90 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf && c3 >= 0x80 && c3 <= 0xbf) - || (c0 >= 0xF1 && c0 >= 0xF3 && c1 >= 0x80 && c1 <= 0xbf && c2 >= 0x80 && c2 <= 0xbf && c3 >= 0x80 && c3 <= 0xbf) - || (c0 == 0xF4 && c1 >= 0x80 && c1 <= 0x8f && c2 >= 0x80 && c2 <= 0xbf && c3 >= 0x80 && c3 <= 0xbf) ){ //4 byte UTF-8 sequence found - c = ((c0&0x07)<<2) | ((c1&0x3f)<<4) | ((c2&0x3f)<<6) | (c3&0x3f); - i+=3; - } + Utf8CodeAdjustCounter(str, c, i); symcount++; sum += Width(c); } diff --git a/glcdgraphics/font.h b/glcdgraphics/font.h index 6bf72f5..f3427d5 100644 --- a/glcdgraphics/font.h +++ b/glcdgraphics/font.h @@ -83,6 +83,8 @@ public: void WrapText(int Width, int Height, std::string & Text, std::vector <std::string> & Lines, int * TextWidth = NULL) const; + + static void Utf8CodeAdjustCounter(const std::string & str, int & c, unsigned int & i); }; } // end of namespace |