Browse Source

+ introduced video.StringDisplayWidth

git-svn-id: branches/unicodekvm@48761 -
nickysn 4 years ago
parent
commit
fdfc40e86b

+ 1 - 0
packages/rtl-console/fpmake.pp

@@ -46,6 +46,7 @@ begin
     P.Description := 'Rtl-console, console abstraction';
     P.NeedLibC:= false;
     P.Dependencies.Add('rtl-extra'); // linux,android gpm.
+    P.Dependencies.Add('rtl-unicode');
     P.Dependencies.Add('morphunits',[morphos]);
     P.Dependencies.Add('arosunits',[aros]);
     if Defaults.CPU=m68k then

+ 24 - 0
packages/rtl-console/src/inc/video.inc

@@ -449,6 +449,30 @@ begin
     GetCapabilities:=0;
 end;
 
+function StringDisplayWidth(const S: UnicodeString): Integer;
+var
+  EGC: UnicodeString;
+  FirstCodePoint: UCS4Char;
+begin
+  Result:=0;
+  for EGC in TUnicodeStringExtendedGraphemeClustersEnumerator.Create(S) do
+    if Length(EGC) > 0 then
+      begin
+        FirstCodePoint:=UCS4Char(EGC[1]);
+        if (FirstCodePoint>=$D800) and (FirstCodePoint<=$DBFF) and (Length(EGC)>=2) and
+           (Ord(EGC[2])>=$DC00) and (Ord(EGC[2])<=$DFFF) then
+          begin
+            FirstCodePoint := ((FirstCodePoint-$D800) shl 10) or (Ord(EGC[2])-$DC00);
+          end;
+        { todo: handle emoji + modifiers }
+        case GetEastAsianWidth(FirstCodePoint) of
+          eawW, eawF:
+            Inc(Result, 2);
+          else
+            Inc(Result);
+        end;
+      end;
+end;
 
 { ---------------------------------------------------------------------
     General functions

+ 2 - 0
packages/rtl-console/src/inc/videoh.inc

@@ -176,6 +176,8 @@ function GetCursorType: Word;
 { Return the cursor type: Hidden, UnderLine or Block }
 procedure SetCursorType(NewType: Word);
 { Set the cursor to the given type }
+function StringDisplayWidth(const S: UnicodeString): Integer;
+{ Returns the number of display columns needed for the given string }
 
 procedure GetVideoMode(var Mode: TVideoMode);
 { Return dimensions of the current video mode }

+ 1 - 1
packages/rtl-console/src/unix/video.pp

@@ -58,7 +58,7 @@ var internal_codepage,external_codepage:Tencoding;
                                 implementation
 {*****************************************************************************}
 
-uses  baseunix,termio,strings,unixkvmbase
+uses  baseunix,termio,strings,unixkvmbase,graphemebreakproperty,eastasianwidth
      {$ifdef linux},linuxvcs{$endif};
 
 {$i video.inc}