GetUtf8StringWidth
Short summary
This function returns the width of a given Utf-8 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 :String(10) := 'Test';
myWidth, charCount :UDINT;
------------------
GetUtf8StringLength(
stringAddress := ADR(myString),
width => myWidth,
visibleCharacters => charCount
);
- Return type: DINT
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| utf8StringBuffer | 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 GetUtf8StringWidth
VAR_INPUT
(* adrress of the utf-8 encoded string or byte array (must be also null terminated!) *)
utf8StringBuffer :PVOID;
END_VAR
VAR_OUTPUT
(* width of the given string *)
width :DINT := 0;
(* count of visible characters *)
visibleCharacters :UDINT := 0;
END_VAR
VAR
stringBufferLength :UDINT := 0;
codepoints :POINTER TO UnicodeCodePoint;
codepointCount :UDINT;
END_VAR
Implementation
stringBufferLength := GetUtf8StringLength(utf8StringBuffer);
RETURN (stringBufferLength = 0);
codepoints := __NEW(UnicodeCodePoint, stringBufferLength);
RETURN(codepoints = 0);
GetCodepointsFromUtf8String(
utf8StringAddress := utf8StringBuffer,
utf8StringByteCount := GetUtf8StringLength(utf8StringBuffer),
codePointBuffer := codepoints,
bufferSize := stringBufferLength * 4,
codePointsCount => codepointCount );
width := GetWidthFromCodepointSequence(
codepoints := codepoints,
codepointCount := codepointCount,
visibleCharacters => visibleCharacters
);
__DELETE(codepoints);