//+------------------------------------------------------------------+
//|                                               TextLimitProbe.mq5 |
//|                                       Copyright 2026, Ali Rajput |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Ali Rajput"
#property version   "1.10"
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <Canvas\Canvas.mqh>
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
string      g_type_names[]={"OBJ_LABEL","OBJ_TEXT","OBJ_EDIT","OBJ_BUTTON","OBJ_RECTANGLE_LABEL"};
ENUM_OBJECT g_types[]={OBJ_LABEL,OBJ_TEXT,OBJ_EDIT,OBJ_BUTTON,OBJ_RECTANGLE_LABEL};
int         g_lengths[]={10,62,63,64,100,255,256,1000,5000};
string      g_emoji;     // one character outside the BMP, two code units
//+------------------------------------------------------------------+
//| Digits                                                           |
//| The digits 0 to 9 repeated, so the cut point can be read.        |
//+------------------------------------------------------------------+
string Digits(const int len)
  {
   string s="";
   for(int i=0; i<len; i++)
      s+=StringFormat("%d",i%10);
   return(s);
  }
//+------------------------------------------------------------------+
//| Repeat                                                           |
//+------------------------------------------------------------------+
string Repeat(const string unit,const int n)
  {
   string s="";
   for(int i=0; i<n; i++)
      s+=unit;
   return(s);
  }
//+------------------------------------------------------------------+
//| Fresh                                                            |
//| Deletes and creates the label that every unit probe uses.        |
//+------------------------------------------------------------------+
void Fresh(const string name)
  {
   ObjectDelete(0,name);
   ObjectCreate(0,name,OBJ_LABEL,0,0,0);
  }
//+------------------------------------------------------------------+
//| ProbeTypes                                                       |
//| Sets a text of every length on every text object type and reads  |
//| the stored text back.                                            |
//+------------------------------------------------------------------+
void ProbeTypes(void)
  {
   for(int t=0; t<ArraySize(g_types); t++)
      for(int k=0; k<ArraySize(g_lengths); k++)
        {
         string name="CTL_type";
         ObjectDelete(0,name);
         if(!ObjectCreate(0,name,g_types[t],0,TimeCurrent(),SymbolInfoDouble(_Symbol,SYMBOL_BID)))
           {
            PrintFormat("LIMIT type=%s len=%d create_failed err=%d",g_type_names[t],g_lengths[k],GetLastError());
            continue;
           }
         string want=Digits(g_lengths[k]);
         ResetLastError();
         bool ok=ObjectSetString(0,name,OBJPROP_TEXT,want);
         int err=GetLastError();
         string got=ObjectGetString(0,name,OBJPROP_TEXT);
         PrintFormat("LIMIT type=%s sent=%d set=%s err=%d stored=%d same=%s",g_type_names[t],g_lengths[k],ok ? "true" : "false",err,
                     StringLen(got),got==want ? "yes" : "no");
         ObjectDelete(0,name);
        }
  }
//+------------------------------------------------------------------+
//| ProbeUnits                                                       |
//| What counts as one of the 63: a UTF-16 code unit.                |
//+------------------------------------------------------------------+
void ProbeUnits(void)
  {
   string tags[]={"ascii","cyrillic","cjk","emoji_pairs"};
   string units[4];
   units[0]="a";
   units[1]=ShortToString(0x0416);
   units[2]=ShortToString(0x4E2D);
   units[3]=g_emoji;
   int repeats[]={70,70,70,35};
   for(int i=0; i<4; i++)
     {
      Fresh("CTL_unit");
      string want=Repeat(units[i],repeats[i]);
      ResetLastError();
      bool ok=ObjectSetString(0,"CTL_unit",OBJPROP_TEXT,want);
      int err=GetLastError();
      PrintFormat("LIMIT units tag=%s sent=%d set=%s err=%d stored=%d",tags[i],StringLen(want),ok ? "true" : "false",err,
                  StringLen(ObjectGetString(0,"CTL_unit",OBJPROP_TEXT)));
     }
   Fresh("CTL_unit");
   ResetLastError();
   bool ok=ObjectSetString(0,"CTL_unit",OBJPROP_TEXT,Repeat("a",62)+g_emoji);
   int err=GetLastError();
   string cut=ObjectGetString(0,"CTL_unit",OBJPROP_TEXT);
   PrintFormat("LIMIT pairsplit set=%s err=%d stored=%d last_unit=0x%04X",ok ? "true" : "false",err,StringLen(cut),
               StringGetCharacter(cut,StringLen(cut)-1));
   Fresh("CTL_unit");
   ResetLastError();
   ok=ObjectSetString(0,"CTL_unit",OBJPROP_TEXT,"line one\nline two");
   err=GetLastError();
   string lf=ObjectGetString(0,"CTL_unit",OBJPROP_TEXT);
   PrintFormat("LIMIT linebreak set=%s err=%d stored=%d has_lf=%s",ok ? "true" : "false",err,StringLen(lf),StringFind(lf,"\n")>=0 ? "yes" : "no");
   ObjectDelete(0,"CTL_unit");
  }
//+------------------------------------------------------------------+
//| StoredName                                                       |
//| The name of the object on the chart whose name is a prefix of    |
//| full, or "" when there is none. This shows what the terminal     |
//| stored as the name.                                              |
//+------------------------------------------------------------------+
string StoredName(const string full)
  {
   for(int i=ObjectsTotal(0)-1; i>=0; i--)
     {
      string name=ObjectName(0,i);
      if(StringLen(name)>=10 && StringFind(full,name)==0)
         return(name);
     }
   return("");
  }
//+------------------------------------------------------------------+
//| ProbeName                                                        |
//| Creates one label, prints the length of the name the terminal    |
//| stored, and whether the full name and its first 63 code units    |
//| find the object. For a name of 63 units or less the first 63     |
//| units are the full name, so that column prints n/a.              |
//+------------------------------------------------------------------+
void ProbeName(const string kind,const string name)
  {
   int len=StringLen(name);
   int before=ObjectsTotal(0);
   ResetLastError();
   bool made=ObjectCreate(0,name,OBJ_LABEL,0,0,0);
   int err=GetLastError();
   string stored=StoredName(name);
   int n=StringLen(stored);
   string by_63=(len<=63) ? "n/a" : (ObjectFind(0,StringSubstr(name,0,63))>=0 ? "yes" : "no");
   PrintFormat("LIMIT name kind=%s len=%d created=%s err=%d objects_added=%d stored_len=%d stored_last_unit=0x%04X found_by_full=%s found_by_first_63=%s",
               kind,len,made ? "true" : "false",err,ObjectsTotal(0)-before,n,n>0 ? StringGetCharacter(stored,n-1) : 0,
               ObjectFind(0,name)>=0 ? "yes" : "no",by_63);
   ObjectDelete(0,name);
   if(stored!="")
      ObjectDelete(0,stored);
  }
//+------------------------------------------------------------------+
//| ProbeNames                                                       |
//| Object names longer than 63 code units, and what happens when    |
//| two long names differ only after the 63rd unit.                  |
//+------------------------------------------------------------------+
void ProbeNames(void)
  {
   int lengths[]={62,63,64,70};
   for(int i=0; i<ArraySize(lengths); i++)
      ProbeName("ascii",Repeat("n",lengths[i]));
   ProbeName("emoji",Repeat(g_emoji,35));
   string a=Repeat("m",65)+"A";
   string b=Repeat("m",65)+"B";
   int before=ObjectsTotal(0);
   ResetLastError();
   bool made_a=ObjectCreate(0,a,OBJ_LABEL,0,0,0);
   int err_a=GetLastError();
   ResetLastError();
   bool made_b=ObjectCreate(0,b,OBJ_LABEL,0,0,0);
   int err_b=GetLastError();
   PrintFormat("LIMIT collision first=%s err=%d second=%s err=%d objects_added=%d",made_a ? "true" : "false",err_a,made_b ? "true" : "false",err_b,
               ObjectsTotal(0)-before);
   ObjectSetString(0,a,OBJPROP_TEXT,"text of A");
   ObjectSetString(0,b,OBJPROP_TEXT,"text of B");
   PrintFormat("LIMIT collision read_a=%s read_b=%s",ObjectGetString(0,a,OBJPROP_TEXT),ObjectGetString(0,b,OBJPROP_TEXT));
   ObjectsDeleteAll(0,"mmmm");
  }
//+------------------------------------------------------------------+
//| ProbeOther                                                       |
//| The tooltip and the chart comment, for comparison.               |
//+------------------------------------------------------------------+
void ProbeOther(void)
  {
   int tips[]={63,64,200,1000};
   for(int i=0; i<ArraySize(tips); i++)
     {
      Fresh("CTL_tip");
      ObjectSetString(0,"CTL_tip",OBJPROP_TOOLTIP,Repeat("t",tips[i]));
      PrintFormat("LIMIT tooltip sent=%d stored=%d",tips[i],StringLen(ObjectGetString(0,"CTL_tip",OBJPROP_TOOLTIP)));
     }
   ObjectDelete(0,"CTL_tip");
   int sizes[]={63,500,2045,2046,3000};
   for(int i=0; i<ArraySize(sizes); i++)
     {
      Comment(Repeat("c",sizes[i]));
      PrintFormat("LIMIT comment sent=%d read_back=%d",sizes[i],StringLen(ChartGetString(0,CHART_COMMENT)));
     }
   Comment("");
  }
//+------------------------------------------------------------------+
//| ProbeCanvas                                                      |
//| Draws digit strings of several lengths with CCanvas::TextOut()   |
//| into a bitmap in memory and finds the columns that hold ink.     |
//| Then it tries to create bitmaps of a few sizes.                  |
//+------------------------------------------------------------------+
void ProbeCanvas(void)
  {
   int lengths[]={63,64,255,256,1000,2000};
   for(int k=0; k<ArraySize(lengths); k++)
     {
      CCanvas canvas;
      string text=Digits(lengths[k]);
      canvas.FontSet("Arial",-100);
      int text_w=canvas.TextWidth(text);
      ResetLastError();
      if(!canvas.Create("CTL_canvas_probe",text_w+40,40,COLOR_FORMAT_ARGB_NORMALIZE))
        {
         PrintFormat("LIMIT canvas units=%d create_failed err=%d",lengths[k],GetLastError());
         continue;
        }
      canvas.FontSet("Arial",-100);
      canvas.Erase(0xFFFFFFFF);
      canvas.TextOut(10,8,text,0xFF000000);
      int left=-1;
      int right=-1;
      for(int x=0; x<canvas.Width(); x++)
         for(int y=0; y<canvas.Height(); y++)
            if(canvas.PixelGet(x,y)!=0xFFFFFFFF)
              {
               if(left<0)
                  left=x;
               right=x;
               break;
              }
      PrintFormat("LIMIT canvas units=%d text_width=%d bitmap_width=%d ink_from=%d ink_to=%d ink_width=%d",lengths[k],text_w,canvas.Width(),
                  left,right,right-left+1);
      canvas.Destroy();
     }
   int widths[]={1000,16384,32768,65536};
   int heights[]={25000,64,64,64};
   for(int k=0; k<ArraySize(widths); k++)
     {
      CCanvas canvas;
      ResetLastError();
      bool made=canvas.Create("CTL_canvas_size",widths[k],heights[k],COLOR_FORMAT_ARGB_NORMALIZE);
      int err=GetLastError();
      PrintFormat("LIMIT bitmap width=%d height=%d created=%s err=%d megabytes=%.1f",widths[k],heights[k],made ? "true" : "false",err,
                  4.0*widths[k]*heights[k]/1048576.0);
      canvas.Destroy();
     }
  }
//+------------------------------------------------------------------+
//| ProbeSize                                                        |
//| Whether the size of a label can be read back after it is drawn.  |
//+------------------------------------------------------------------+
void ProbeSize(void)
  {
   Fresh("CTL_size");
   ObjectSetString(0,"CTL_size",OBJPROP_TEXT,"WWWWWWWWWW");
   ChartRedraw(0);
   Sleep(300);
   PrintFormat("LIMIT size label_xsize=%d label_ysize=%d",(int)ObjectGetInteger(0,"CTL_size",OBJPROP_XSIZE),
               (int)ObjectGetInteger(0,"CTL_size",OBJPROP_YSIZE));
   ObjectDelete(0,"CTL_size");
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   g_emoji=ShortToString(0xD83D)+ShortToString(0xDE00);
   PrintFormat("LIMIT build=%d",TerminalInfoInteger(TERMINAL_BUILD));
   ProbeTypes();
   ProbeUnits();
   ProbeNames();
   ProbeOther();
   ProbeCanvas();
   ProbeSize();
  }
//+------------------------------------------------------------------+
