Browse Source

+ introduced Video.ExtendedGraphemeClusterDisplayWidth

git-svn-id: branches/unicodekvm@48767 -
nickysn 4 years ago
parent
commit
cc643608a2
2 changed files with 27 additions and 17 deletions
  1. 25 17
      packages/rtl-console/src/inc/video.inc
  2. 2 0
      packages/rtl-console/src/inc/videoh.inc

+ 25 - 17
packages/rtl-console/src/inc/video.inc

@@ -449,29 +449,37 @@ begin
     GetCapabilities:=0;
 end;
 
+function ExtendedGraphemeClusterDisplayWidth(const EGC: UnicodeString): Integer;
+var
+  FirstCodePoint: UCS4Char;
+begin
+  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:
+          Result := 2;
+        else
+          Result := 1;
+      end;
+    end
+  else
+    Result := 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;
+    Inc(Result, ExtendedGraphemeClusterDisplayWidth(EGC));
 end;
 
 { ---------------------------------------------------------------------

+ 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 ExtendedGraphemeClusterDisplayWidth(const EGC: UnicodeString): Integer;
+{ Returns the number of display columns needed for the given extended grapheme cluster }
 function StringDisplayWidth(const S: UnicodeString): Integer;
 { Returns the number of display columns needed for the given string }