GetUtf16StringWidth
Short summary
This function returns the width of a given Utf-16 string.
A width of -1 means the string contains control characters, e.g. line feed or carriage return. If a string has for example a line break, the width cannot be calculated. A width 0 means the string is either empty or consists only of combining marks.
Attention: The actual width a character needs for displaying is always dependent on the used font, the fontsize and the rendering! The width this function returns is actually an estimation.
Example:
myString :WString(10) := "Test";
myWidth, charCount :UDINT;
------------------
GetUtf16StringLength(
stringAddress := ADR(myString),
width => myWidth,
visibleCharacters => charCount
);
- Return type:
DINT
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| utf16StringBuffer | PVOID | adrress of the utf-8 encoded string or byte array (must be also null terminated!) | input |
| width | DINT | width of the given string | output |
| visibleCharacters | UDINT | count of visible characters | output |
Code
Declaration
FUNCTION GetUtf16StringWidth :DINT;
VAR_INPUT
(* adrress of the utf-8 encoded string or byte array (must be also null terminated!) *)
utf16StringBuffer :PVOID;
END_VAR
VAR_OUTPUT
(* width of the given string *)
width :DINT := 0;
(* count of visible characters *)
visibleCharacters :UDINT;
END_VAR
VAR
codepoints :POINTER TO UnicodeCodePoint;
codepointCount :UDINT;
stringLength :UDINT;
END_VAR
Implementation
GetUtf16StringWidth := 0;
stringLength := GetUtf16StringLength(utf16StringBuffer);
RETURN((utf16StringBuffer = 0) OR_ELSE (stringLength = 0));
codepoints := __NEW(UnicodeCodepoint, (stringLength+1));
RETURN(codepoints = 0);
GetCodepointsFromUtf16String(
utf16StringAddress := utf16StringBuffer,
utf16StringWordCount := stringLength,
codepointBuffer := codepoints,
bufferSize := (stringLength+1)*SIZEOF(UnicodeCodePoint),
codePointCount => codepointCount);
width := GetWidthFromCodepointSequence(
codepoints := codepoints,
codepointCount := codepointCount,
visibleCharacters => visibleCharacters
);
__DELETE(codepoints);
GetUtf16StringWidth := width;