Changeset 96

Show
Ignore:
Timestamp:
02/07/06 11:38:33 (3 years ago)
Author:
Shawn Liu
Message:

fix shortenText() Chinese character issue

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/current/win32/import/dwt/custom/clabel.d

    r95 r96  
    705705 * Override if you need a different strategy. 
    706706 */ 
    707 protected char[] shortenText(GC gc, char[] ts, int width) { 
    708      
    709     if (ts is null) return null; 
     707protected char[] shortenText(GC gc, char[] text, int width) { 
     708     
     709    if (text.length == 0) return text; 
     710    if (gc.textExtent(text).x <= width) return text; 
    710711 
    711712    // <Shawn> 
    712     // must make a copy of the the original text, otherwise the orginal text will be changed !!! 
    713     char[] t = ts.dup; 
     713    // 20060207 : when strip character by char in UTF8 will cut MBCS such as Chinese character   
     714    // to half and may cause UTF convert error when call OS API to draw, the fix is convert to  
     715    // wchar[] during shorten the Text 
     716    wchar[] t = std.utf.toUTF16(text); 
     717    wchar[] wELLIPSIS = std.utf.toUTF16(ELLIPSIS); 
    714718    int w = gc.textExtent(ELLIPSIS).x; 
    715719    int l = t.length; 
     
    718722    int e = pivot+1; 
    719723    while (s >= 0 && e < l) { 
    720         char[] s1 = t[0..s]; 
    721         char[] s2 = Converter.substring(t, e,l)
    722         int l1 = gc.textExtent(s1).x; 
    723         int l2 = gc.textExtent(s2).x; 
     724        wchar[] s1 = t[0..s]; 
     725        wchar[] s2 = t[e..l]
     726        int l1 = gc.textExtent(std.utf.toUTF8(s1)).x; 
     727        int l2 = gc.textExtent(std.utf.toUTF8(s2)).x; 
    724728        if (l1+w+l2 < width) { 
    725             t = s1 ~ ELLIPSIS ~ s2; 
     729            t = s1 ~ wELLIPSIS ~ s2; 
    726730            break; 
    727731        } 
     
    729733        e++; 
    730734    } 
    731     return t
    732 } 
    733 } 
     735    return std.utf.toUTF8(t)
     736} 
     737} 
  • trunk/current/win32/import/dwt/custom/ctabitem.d

    r95 r96  
    141141} 
    142142static char[] shortenText(GC gc, char[] string, int width) { 
    143     if( string is null) return null
     143    if( string.length == 0) return string
    144144    if (gc.textExtent(string, FLAGS).x <= width) return string; 
    145145 
    146146    // <Shawn> 
    147     // must make a copy of the the original text, otherwise the orginal text will be cut short !!! 
    148     char[] t = string.dup; 
     147    // 20060207 : when strip character by char in UTF8 will cut MBCS such as Chinese character   
     148    // to half and may cause UTF convert error when call OS API to draw, the fix is convert to  
     149    // wchar[] during shorten the Text 
     150     
    149151    int ellipseWidth = gc.textExtent(ELLIPSIS, FLAGS).x; 
     152    wchar[] t = std.utf.toUTF16(string); 
    150153    int length = t.length; 
    151154    int end = length - 1; 
    152155    while (end > 0) { 
    153         t = Converter.substring(t, 0, end)
    154         int l = gc.textExtent(t, FLAGS).x; 
     156        t = t[0..end]
     157        int l = gc.textExtent(std.utf.toUTF8(t), FLAGS).x; 
    155158        if (l + ellipseWidth <= width) { 
    156             return t ~ ELLIPSIS; 
     159            return std.utf.toUTF8(t) ~ ELLIPSIS; 
    157160        } 
    158161        end--; 
    159162    } 
    160     return Converter.substring(t, 0, 1); 
    161 
     163    return std.utf.toUTF8(t[0..1]); 
     164
     165 
    162166public void dispose() { 
    163167    if (isDisposed ()) return;