How to make integer alignment for texts?

Example object for this post can be downloaded here.

To provide integer alignment of texts the values which needed to be displayed should be converted into string types. After that, the location of the decimal point within each string is needed to be known, to define the exact horizontal starting positions for the strings.

In the following example the actual Area, Volume, Width, Depth and Height values are displayed of a block. The value column is aligned by the integer parts of the values, the decimal points and the fractions after it are positioned in the middle of the width of the block.

TipsAndTricks_IntegerAlign_Ex1

First the conversion to string types should be done. For the Area and the Volume values variables are used to store the calculated values, for the side lengths of the block the parameter values are directly used in the STR function. Various formats of the values can be displayed, for further information see the STR function in GDL Reference Guide.

! ------------------------------------------------------------------------------
! Input parameters controlled by the user:
! ------------------------------------------------------------------------------
! ZZYZX 		- (Length) height of the block whose data are displayed
! blockWidth 		- (Length) width of the block whose data are displayed
! blockDepth 		- (Length) depth of the block whose data are displayed
! sampleSize 		- (RealNum) indicates the height of the font from the
! 		 	  baseline to the cap height, given in Paper size - mm
! fontType 		- (String) font family of the displayed text
! stSepChar  		- (String) the actual decimal point character, 
! 		 	  depends on the settings of the operation system
! stAreaName 		- (String) the name of the block area value
! stVolumeName 		- (String) the name of the block volume value
! stWidthName 		- (String) the name of the block width value
! stDepthName 		- (String) the name of the block depth value
! stHeightName 		- (String) the name of the block height value

! ------------------------------------------------------------------------------
! Draw the block in 2D
! ------------------------------------------------------------------------------
rect2 0, 0, blockWidth, blockDepth

! ------------------------------------------------------------------------------
! Define styles
! ------------------------------------------------------------------------------
! Not all of the following variables are used in this example, but the kind of
! definition showed below is the GRAPHISOFT standard constant definition

! Text styles
NORMAL		= 0
BOLD 		= 1
ITALIC 		= 2
UNDERLINE 	= 4

! Anchor positions
LEFT_UPPER	= 1
MIDDLE_UPPER	= 2
RIGHT_UPPER	= 3
LEFT_MIDDLE	= 4
MIDDLE		= 5
RIGHT_MIDDLE	= 6
LEFT_LOWER	= 7
MIDDLE_LOWER	= 8
RIGHT_LOWER	= 9
 
! Define the different text styles
define style "ItalicStyle" 	fontType, sampleSize, LEFT_UPPER, ITALIC
define style "NormalStyle" 	fontType, sampleSize, LEFT_UPPER, NORMAL

! ------------------------------------------------------------------------------
! Set input data for the text drawing
! ------------------------------------------------------------------------------

! Set the multiplier of the paper scale
PAPER_TO_MODEL = GLOB_SCALE / 1000

! Set text coordinate variable:
! samplesize is given in Paper Size - mm, convert it into Model size  - m
! calculate the row height from it, 
! therefore the row height will not depend on the font family
dy = - sampleSize * 1.5 * PAPER_TO_MODEL 

! ------------------------------------------------------------------------------
! Display the texts in the first column
! ------------------------------------------------------------------------------
style "ItalicStyle"			! Set the previously defined style

text2 0, 0, 	stAreaName
text2 0, dy, 	stVolumeName

style "NormalStyle"			! Set the previously defined style

text2 0, dy * 2, stWidthName
text2 0, dy * 3, stDepthName
text2 0, dy * 4, stHeightName

! ------------------------------------------------------------------------------
! Use str command to get the values to display as a string
! ------------------------------------------------------------------------------
_format = "%~.2m"	! Unit: meter, Decimals: 2, Hide zero decimals
_stAreaValue = ""
_stAreaValue = str(_format, _areaValue)

_stVolumeValue = ""
_stVolumeValue = str(_format, _volumeValue)

_stWidthValue = ""
_stWidthValue = str(_format, blockWidth)

_stDepthValue = ""
_stDepthValue = str(_format, blockDepth)

_stHeightValue = ""
_stHeightValue = str(_format, ZZYZX)

The visualization of the decimal point depends on the settings of the operation system, so the certain way to know the actual decimal point character is to create a parameter for it which is available for the user. In the following example this parameter’s name is “stSepChar”.

For getting the location of the decimal point, the STRSTR function is used. It returns an integer number, which indicates the position of the decimal point among the characters within the string value. If there is no decimal point in the string value, the STRSTR function returns 0. In this case the text should be indented by the whole string. The STRSUB function is used for getting the part of the string from the beginning to the decimal point. In this example the decimal point is not included in the horizontal positioning, therefore the integer position of it should be subtracted by 1. The STW function returns the length of the substring in millimeters (paper size). The horizontal position is calculated from the middle of the block (blockWidth / 2), by a subtraction of the length of the integer part in model size.

TipsAndTricks_IntegerAlign_Ex2

! ------------------------------------------------------------------------------
! Set the x coordinates for the texts in the second column
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
! Area Value
! ------------------------------------------------------------------------------
! Get the integer position of the decimal point
_posSepChar = strstr(_stAreaValue, stSepChar)	
! If the decimal point is not found, get the width of the whole string
if _posSepChar = 0 then														
    _lengthOfIntegerPart = stw (_stAreaValue)	
! If the decimal point is found, get the width of the integer part substring						
else
    _lengthOfIntegerPart = stw (strsub (_stAreaValue, 0, _posSepChar - 1)) 
endif
! Convert the length of the integer part substring into Model size - m,
! indent the string position with it
_xPosArea	= blockWidth / 2 - _lengthOfIntegerPart * PAPER_TO_MODEL 

! ------------------------------------------------------------------------------
! Volume Value
! ------------------------------------------------------------------------------
_posSepChar = strstr(_stVolumeValue, stSepChar)
if _posSepChar = 0 then
    _lengthOfIntegerPart = stw (_stVolumeValue)
else
    _lengthOfIntegerPart = stw (strsub (_stVolumeValue, 0, _posSepChar - 1))
endif
_xPosVolume	= blockWidth / 2 - _lengthOfIntegerPart * PAPER_TO_MODEL

! ------------------------------------------------------------------------------
! Width Value
! ------------------------------------------------------------------------------
_posSepChar = strstr(_stWidthValue, stSepChar)
if _posSepChar = 0 then
    _lengthOfIntegerPart = stw (_stWidthValue)
else
    _lengthOfIntegerPart = stw (strsub (_stWidthValue, 0, _posSepChar - 1))
endif
_xPosWidth	= blockWidth / 2 - _lengthOfIntegerPart * PAPER_TO_MODEL

! ------------------------------------------------------------------------------
! Depth Value
! ------------------------------------------------------------------------------
_posSepChar = strstr(_stDepthValue, stSepChar)
if _posSepChar = 0 then
    _lengthOfIntegerPart = stw (_stDepthValue)
else
    _lengthOfIntegerPart = stw (strsub (_stDepthValue, 0, _posSepChar - 1))
endif
_xPosDepth	= blockWidth / 2 - _lengthOfIntegerPart * PAPER_TO_MODEL

! ------------------------------------------------------------------------------
! Height Value
! ------------------------------------------------------------------------------
_posSepChar = strstr(_stHeightValue, stSepChar)
if _posSepChar = 0 then
    _lengthOfIntegerPart = stw (_stHeightValue)
else
    _lengthOfIntegerPart = stw (strsub (_stHeightValue, 0, _posSepChar - 1))
endif
_xPosHeight	= blockWidth / 2 - _lengthOfIntegerPart * PAPER_TO_MODEL

After calculating the positions of each text lines and setting the previously defined text style, the value column can be displayed:

! ------------------------------------------------------------------------------
! Display the texts in the second column
! ------------------------------------------------------------------------------
style "NormalStyle"			! Set the previously defined style

text2 _xPosArea, 	0, 	_stAreaValue
text2 _xPosVolume, 	dy, 	_stVolumeValue
text2 _xPosWidth, 	dy * 2, _stWidthValue
text2 _xPosDepth, 	dy * 3, _stDepthValue
text2 _xPosHeight, 	dy * 4, _stHeightValue

Command and function pool:
RECT2
DEFINE STYLE
GLOB_SCALE
[SET] STYLE
TEXT2
STR
STRSTR
STW
STRSUB