TextSetFont

The function sets the font for displaying the text using drawing methods and returns the result of that operation. Arial font with the size -120 (12 pt) is used by default.

bool  TextSetFont(
   const string  name,            // font name or path to font file on the disk
   int           size,            // font size
   uint          flags,           // combination of flags
   int           orientation=0    // text slope angle
   );

Parameters

name

[in]  Font name in the system or the name of the resource containing the font or the path to font file on the disk.

size

[in]  The font size that can be set using positive and negative values. In case of positive values, the size of a displayed text does not depend on the operating system's font size settings. In case of negative values, the value is set in tenths of a point and the text size depends on the operating system settings ("standard scale" or "large scale"). See the Note below for more information about the differences between the modes.

flags

[in]  Combination of flags describing font style.

orientation

[in]  Text's horizontal inclination to X axis, the unit of measurement is 0.1 degrees. It means that orientation=450 stands for inclination equal to 45 degrees.

Return Value

Returns true if the current font is successfully installed, otherwise false. Possible code errors:

  • ERR_INVALID_PARAMETER(4003) - name presents NULL or "" (empty string),
  • ERR_INTERNAL_ERROR(4001) - operating system error (for example, an attempt to create a non-existent font).

Note

If "::" is used in font name, the font is downloaded from EX5 resource. If name font name is specified with an extension, the font is downloaded from the file, if the path starts from "\" or "/", the file is searched relative to MQL5 directory. Otherwise, it is searched relative to the path of EX5 file which called TextSetFont() function.

The font size is set using positive or negative values. This fact defines the dependence of the text size from the operating system settings (size scale).

  • If the size is specified using a positive number, this size is transformed into physical measurement units of a device (pixels) when changing a logical font into a physical one, and this size corresponds to the height of the symbol glyphs picked from the available fonts. This case is not recommended when the texts displayed by TextOut() function and the ones displayed by OBJ_LABEL ("Label") graphical object are to be used together on the chart.
  • If the size is specified using a negative number, this number is supposed to be set in tenths of a logical point and is divided by 10 (for example, -350 is equal to 35 logical points). An obtained value is then transformed into physical measurement units of a device (pixels) and corresponds to the absolute value of the height of a symbol picked from the available fonts. Multiply the font size specified in the object properties by -10 to make the size of a text on the screen similar to the one in OBJ_LABEL object.

The flags can be used as the combination of style flags with one of the flags specifying the font width. Flag names are shown below.

Flags for specifying font style

Flag

Description

FONT_ITALIC

Italic

FONT_UNDERLINE

Underline

FONT_STRIKEOUT

Strikeout

 

Flags for specifying font width

Flag

FW_DONTCARE

FW_THIN

FW_EXTRALIGHT

FW_ULTRALIGHT

FW_LIGHT

FW_NORMAL

FW_REGULAR

FW_MEDIUM

FW_SEMIBOLD

FW_DEMIBOLD

FW_BOLD

FW_EXTRABOLD

FW_ULTRABOLD

FW_HEAVY

FW_BLACK

Example:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   COORD_X    200
#define   COORD_Y    100
#define   OBJ_NAME   "TestTextGetSizeBitmapLabel"
#define   RES_NAME   "TestTextGetSizeResource"
#define   COLOR_NULL 0x00FFFFFF
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- current chart ID
   long   chart_idChartID();
   
   string font_names[] ={"Arial""Tahoma""Calibri"};
   uint   flags_array[]={0FONT_ITALICFONT_UNDERLINEFONT_STRIKEOUT};
   uint   fw_array[]={FW_DONTCAREFW_THINFW_EXTRALIGHTFW_ULTRALIGHTFW_LIGHT,
                      FW_NORMALFW_REGULARFW_MEDIUMFW_SEMIBOLDFW_DEMIBOLD,
                      FW_BOLDFW_EXTRABOLDFW_ULTRABOLDFW_HEAVYFW_BLACK};
   
//--- disable drawing any attributes of the price chart
   ChartSetInteger(chart_idCHART_SHOWfalse);
   
//--- declare the parameters of the graphical resource
   uint rc_width =(int)ChartGetInteger(chart_idCHART_WIDTH_IN_PIXELS); 
   uint rc_height=(int)ChartGetInteger(chart_idCHART_HEIGHT_IN_PIXELS); 
   uint rc_data[]; 
   uint rc_size=rc_width*rc_height;
  
//--- create a graphical resource for text output
   if(!CreateResource(chart_idrc_datarc_widthrc_height))
      return;
   
//--- loop through font names
   for(int i=0i<(int)font_names.Size(); i++)
     {
      //--- loop through font flags
      for(int j=0j<(int)flags_array.Size(); j++)
        {
         //--- draw text with the font and style flag obtained from the array
         DrawText(font_names[i], flags_array[j], rc_datarc_widthrc_height);
         Sleep(800);
         ArrayInitialize(rc_dataCOLOR_NULL);
        }
      Sleep(800);
      ArrayInitialize(rc_dataCOLOR_NULL);
     }
   
//--- after outputting all texts with different font sizes and styles,
//--- show text with different font width flags
   for(int i=0i<(int)fw_array.Size(); i++)
     {
      //--- set the font size and name for displaying text with width flags
      string font_name="Tahoma";
      int    size=-140;
      TextSetFont(font_namesizefw_array[i]);
      
      //--- create a drawn text string, output it to the resource pixel array, and update the resource
      string text=StringFormat("Text%d: Font name: \"%s%s\", size: %d (%d)"i+1font_nameFlagWidthDescription(fw_array[i]), sizesize/-10);
      TextOut(textCOORD_XCOORD_YANCHOR_LEFT_UPPERrc_datarc_widthrc_heightColorToARGB(clrDodgerBlue), COLOR_FORMAT_ARGB_NORMALIZE);
      Update(RES_NAMErc_datarc_widthrc_heighttrue);
      //--- wait a second
      Sleep(1000);
      ArrayInitialize(rc_dataCOLOR_NULL);
     }
   
//--- wait five seconds, then free the resource and delete the graphical object
   Sleep(5000);
   ResourceFree(RES_NAME);
   ObjectDelete(chart_idOBJ_NAME);
   
//--- allow drawing any attributes of the price chart
   ChartSetInteger(chart_idCHART_SHOWtrue);
   ChartRedraw(chart_id);
   /*
   Text messages are displayed on the chart as a result of the script execution.
   The texts have different fonts, as well as style and width flags
   */ 
  }
//+------------------------------------------------------------------+
//| Display five text strings of different                           |
//| sizes with the specified font and flags                          |
//+------------------------------------------------------------------+
void DrawText(const string font_nameuint flagsuint &pixels_array[], uint res_widthuint res_height)
  {
//--- output five strings of text with different font sizes
   for(int i=0i<5i++)
     {
      //--- calculate the font size and set the font for text output using drawing methods
      int size=-140+10*i;
      TextSetFont(font_namesizeflags);
      
      //--- create a drawn text string, output it to the resource pixel array, and update the resource
      string text=StringFormat("Text%d: Font name: \"%s%s\", size: %d (%d)"i+1font_nameFlagDescription(flags), sizesize/-10);
      TextOut(textCOORD_XCOORD_Y+22*iANCHOR_LEFT_UPPERpixels_arrayres_widthres_heightColorToARGB(clrDodgerBlue), COLOR_FORMAT_ARGB_NORMALIZE);
      Update(RES_NAMEpixels_arrayres_widthres_heighttrue);
      //--- wait a bit
      Sleep(800);
     }
  }
//+------------------------------------------------------------------+
//| Return a description of the font style flags                     |
//+------------------------------------------------------------------+
string FlagDescription(const uint flag)
  {
   switch(flag)
     {
      case FONT_ITALIC     :  return(" Italic");
      case FONT_UNDERLINE  :  return(" Underline");
      case FONT_STRIKEOUT  :  return(" Strikeout");
     }
   return("");
  }
//+------------------------------------------------------------------+
//| Return the description of the font width flags                   |
//+------------------------------------------------------------------+
string FlagWidthDescription(const uint flag)
  {
   switch(flag)
     {
      case FW_DONTCARE  :  return(" Dontcare");
      case FW_THIN      :  return(" Thin");
      case FW_EXTRALIGHT:  return(" Extralight");
      case FW_ULTRALIGHT:  return(" Ultralight");
      case FW_LIGHT     :  return(" Light");
      case FW_NORMAL    :  return(" Normal");
      case FW_REGULAR   :  return(" Regular");
      case FW_MEDIUM    :  return(" Medium");
      case FW_SEMIBOLD  :  return(" Semibold");
      case FW_DEMIBOLD  :  return(" Demibold");
      case FW_BOLD      :  return(" Bold");
      case FW_EXTRABOLD :  return(" Extrabold");
      case FW_ULTRABOLD :  return(" Ultrabold");
      case FW_HEAVY     :  return(" Heavy");
      case FW_BLACK     :  return(" Black");
     }
   return("");
  }
//+------------------------------------------------------------------+
//| Create a graphical resource for the entire chart                 |
//+------------------------------------------------------------------+
bool CreateResource(const long chart_iduint &pixel_data[], const uint widthconst uint height)
  {
//--- set the size of the pixel array
   ResetLastError(); 
   uint size=width*height;
   if(ArrayResize(pixel_datasize)!=size
     { 
      PrintFormat("%s: ArrayResize() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     } 
//--- fill the pixel array with a transparent color and create a graphical resource based on it
   ArrayInitialize(pixel_dataCOLOR_NULL); 
   if(!ResourceCreate(RES_NAMEpixel_datawidthheight000COLOR_FORMAT_ARGB_NORMALIZE)) 
     { 
      PrintFormat("%s: ResourceCreate() failed. Error code ",__FUNCTION__GetLastError()); 
      return(false); 
     } 
  
//--- create the Graphic Label object at the coordinates of the upper left corner of the chart
   if(!ObjectCreate(0OBJ_NAMEOBJ_BITMAP_LABEL000)) 
     { 
      PrintFormat("%s: ObjectCreate() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     } 
//--- set the width and height of the created bitmap object equal to the width and height of the graphical resource.
//--- set the object anchor point to its center.
   if(!ObjectSetInteger(chart_idOBJ_NAMEOBJPROP_XSIZEwidth))
     {
      PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
   if(!ObjectSetInteger(chart_idOBJ_NAMEOBJPROP_YSIZEheight))
     {
      PrintFormat("%s: ObjectSetInteger() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
//--- specify the previously created graphical resource for the bitmap object as an image file
//--- in this case, in order to indicate the name of the graphical resource used, we need to add "::" before its name
   if(!ObjectSetString(chart_idOBJ_NAMEOBJPROP_BMPFILE"::"+RES_NAME))
     {
      PrintFormat("%s: ObjectSetString() failed. Error code %d",__FUNCTION__GetLastError()); 
      return(false); 
     }
    
//--- all is fine
   return(true);
  }
//+------------------------------------------------------------------+ 
//| Update graphical resource data                                   |
//+------------------------------------------------------------------+ 
void Update(const string res_nameconst uint &pixel_data[], const uint widthconst uint heightconst bool redraw
  { 
//--- leave if zero dimensions are passed
   if(width==0 || height==0
      return
//--- update resource data and redraw the chart
   if(ResourceCreate(res_namepixel_datawidthheight000COLOR_FORMAT_ARGB_NORMALIZE) && redraw
      ChartRedraw(); 
  } 

See also

Resources, ResourceCreate(), ResourceSave(), TextOut()