changeset 323:7066c4836d65

Fix unicode handling
author Frank Benoit <benoit@tionex.de>
date Sun, 30 Nov 2008 09:25:27 +0100
parents 2b8f40a1c515
children ba58cbca64a1
files dwt/dwthelper/utils.d dwt/graphics/TextLayout.d
diffstat 2 files changed, 30 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/dwt/dwthelper/utils.d	Thu Nov 20 20:16:19 2008 +0100
+++ b/dwt/dwthelper/utils.d	Sun Nov 30 09:25:27 2008 +0100
@@ -479,17 +479,40 @@
 alias tango.text.convert.Utf.toString16 toString16;
 alias tango.text.convert.Utf.toString toString;
 
+int toAbsoluteCodePointStartOffset( String str, int index ){
+    //Trace.formatln( "str={}, str.length={}, index={}", str, str.length, index );
+    //Trace.memory( str );
+    if( ( str[index] & 0x80 ) is 0x00 )
+    {
+        return index;
+    }
+    else{
+        while(( str[index] & 0xC0 ) is 0x80 ){
+            index--;
+        }
+        return index;
+    }
+}
 int getRelativeCodePointOffset( String str, int startIndex, int searchRelCp ){
     return getAbsoluteCodePointOffset( str, startIndex, searchRelCp ) - startIndex;
 }
 int getAbsoluteCodePointOffset( String str, int startIndex, int searchRelCp ){
+
+    //Trace.formatln( "str={}, str.length={}, startIndex={}, searchRelCp={}", str, str.length, startIndex, searchRelCp );
+    //Trace.memory( str );
+
     int ignore;
     int i = startIndex;
     if( searchRelCp > 0 ){
         while( searchRelCp !is 0 ){
 
+            //Trace.formatln( "( i+2 < str.length )          = {}", ( i+2 < str.length ));
+            //Trace.formatln( "( str[i+2] & 0xC0 ) is 0x80 ) = {}", (( str[i+2] & 0xC0 ) is 0x80 ));
+            //Trace.formatln( "( str[i+1] & 0xC0 ) is 0x80 ) = {}", (( str[i+1] & 0xC0 ) is 0x80 ));
+            //Trace.formatln( "( str[i  ] & 0xF0 ) is 0xE0 ) = {}", (( str[i  ] & 0xF0 ) is 0xE0 ));
+
             if( ( i < str.length )
-                && ( str[i] & 0x80 ) is 0x00 )
+            && (( str[i] & 0x80 ) is 0x00 ))
             {
                 i+=1;
             }
@@ -515,10 +538,11 @@
                 i+=4;
             }
             else{
-                Trace.formatln( "invalid utf8 characters:  {:X2}", cast(ubyte[]) str );
+                Trace.formatln( "getAbsoluteCodePointOffset invalid utf8 characters:  {:X2}", cast(ubyte[]) str );
                 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
             }
             searchRelCp--;
+            //Trace.formatln(" . ");
         }
     }
     else if( searchRelCp < 0 ){
@@ -554,8 +578,8 @@
                 i+=2;
             }
             else{
-                Trace.formatln( "invalid utf8 characters:  {:X2}", cast(ubyte[]) str );
-                tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
+                Trace.formatln( "invalid utf16 characters:  {:X2}", cast(ubyte[]) str );
+                tango.text.convert.Utf.onUnicodeError( "invalid utf16 input", i );
             }
             searchRelCp--;
         }
@@ -567,7 +591,7 @@
                 if( i < 0 ){
                     return startIndex-1;
                     //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp );
-                    //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
+                    //tango.text.convert.Utf.onUnicodeError( "invalid utf16 input", i );
                 }
             } while(( str[i] & 0xDC00 ) is 0xDC00 );
             searchRelCp++;
--- a/dwt/graphics/TextLayout.d	Thu Nov 20 20:16:19 2008 +0100
+++ b/dwt/graphics/TextLayout.d	Sun Nov 30 09:25:27 2008 +0100
@@ -3116,6 +3116,7 @@
 }
 
 int validadeOffset(int offset, int step) {
+    offset = segmentsText.toAbsoluteCodePointStartOffset( offset );
     offset += segmentsText.getRelativeCodePointOffset( offset, step );
     if (segments !is null && segments.length > 2) {
         for (int i = 0; i < segments.length; i++) {