Skip to main content

GetAnyStringWidth

Short summary

This functions returns the width of a given AnyString.

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.

  • Return type: DINT

Parameters

NameTypeCommentKind
stringToCheckANY_STRINGany string that should be processedinput
widthDINTwidth of the string when displayedoutput

Code

Declaration

FUNCTION GetAnyStringWidth :DINT
VAR_INPUT
(* any string that should be processed *)
stringToCheck :ANY_STRING;
END_VAR
VAR_OUTPUT
(* width of the string when displayed *)
width :DINT;
END_VAR
VAR
END_VAR

Implementation

GetAnyStringWidth := -1;
RETURN((stringToCheck.pValue = 0) OR stringToCheck.diSize <= 0);

CASE stringToCheck.TypeClass OF
__SYSTEM.TYPE_CLASS.TYPE_STRING:
width := GetUtf8StringWidth(stringToCheck.pValue);
__SYSTEM.TYPE_CLASS.TYPE_WSTRING:
width := GetUtf16StringWidth(stringToCheck.pValue);
ELSE
; // do nothing
END_CASE
GetAnyStringWidth := width;