KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility to get a string's dimensions using SVG
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: December 4, 2014

It is often necessary to know the height and width of a string given a font, size and style so that and object, form or SVG, can be properly sized before the string is displayed.

The utility below will return the font, height, and width of the passed string. It is also an example of how to using a 4D Language object to pass and return multiple parameters.

Example use of the utility is shown below.

C_OBJECT($Config_O)
OB SET($Config_O;"text";"HOME")
OB SET($Config_O;"font";"Arial")
OB SET($Config_O;"size";32)
OB SET($Config_O;"style";Bold)

$Config_O:=UTIL_SVG_TextDimensions ($Config_O)

$Font_T:=OB Get($Config_O;"font")
$Ht_L:=OB Get($Config_O;"height")
$Wd_L:=OB Get($Config_O;"width")


Project Method UTIL_SVG_TextDimensions

If (True)
    If (False)
       Begin SQL
         /*
         Name: UTIL_SVG_TextDimensions
         Path: UTIL_SVG_TextDimensions

         Purpose: Return the width and height of an SVG text string

         $0 - C_OBJECT - Text dimensions
         $1 - C_OBJECT - Text configuration settings
         */
       End SQL
    End if
    C_TEXT($MethodName_T)
    $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
    C_OBJECT($0;$Dems_O)
    C_OBJECT($Config_O;$1)
    //--------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------
    //local_variable_declarations
    C_LONGINT($Ndx;$SOA;$RIS;$Params_L;$Size_L;$Style_L;$Width_L;$Height_L;;$Ignore_L)
    C_TEXT($SVG;$textID;$Text_T;$Font_T;$Ignore_T)
    C_PICTURE($Pic_G)
End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters
If ($Params_L=1)
    $Config_O:=$1
    If (OB Is defined($Config_O))
       If(OB Is defined($Config_O;"text"))
          $Text_T:=OB Get($Config_O;"text")
       End if

       If (OB Is defined($Config_O;"font"))
           $Font_T:=OB Get($Config_O;"font")
       Else
           GET STYLE SHEET INFO(Automatic style sheet;$Font_T;$Ignore_L;$Ignore_L)
       End if

       If (OB Is defined($Config_O;"size"))
          $Size_L:=OB Get($Config_O;"size")
       Else
          GET STYLE SHEET INFO(Automatic style sheet;$Ignore_T;$Size_L;$Ignore_L)
       End if

       If (OB Is defined($Config_O;"style"))
           $Style_L:=OB Get($Config_O;"style")
       Else
           GET STYLE SHEET INFO(Automatic style sheet;$Ignore_T;$Ignore_L;$Style_L)
       End if

       //======================== Method Actions ==================================

       If($Text_T#"")
          $SVG:=SVG_New
          $textID:=SVG_New_text ($SVG;$Text_T;0;0;$Font_T;$Size_L;$Style_L)
          $Pic_G:=SVG_Export_to_picture ($SVG)
          SVG_CLEAR ($SVG)

          PICTURE PROPERTIES($Pic_G;$Width_L;$Height_L)

          OB SET($Dems_O;"font";$Font_T)
          OB SET($Dems_O;"width";$Width_L)
          OB SET($Dems_O;"height";$Height_L)
       End if
    End if
End if

//======================== Clean up and Exit =================================

$0:=$Dems_O