return j;
}
-int skipIncompleteTag(string theText, float pos, int len)
-{
- int tag_start = -1;
-
- if(substring(theText, pos - 1, 1) == "^")
- {
- if(isCaretEscaped(theText, pos - 1) || pos >= len)
- return 0;
-
- int ch = str2chr(theText, pos);
- if(ch >= '0' && ch <= '9')
- return 1; // ^[0-9] color code found
- else if (ch == 'x')
- tag_start = pos - 1; // ^x tag found
- else
- return 0;
- }
- else
- {
- for(int i = 2; pos - i >= 0 && i <= 4; ++i)
- {
- if(substring(theText, pos - i, 2) == "^x")
- {
- tag_start = pos - i; // ^x tag found
- break;
- }
- }
- }
-
- if(tag_start >= 0)
- {
- if(tag_start + 5 < len)
- if(IS_HEXDIGIT(substring(theText, tag_start + 2, 1)))
- if(IS_HEXDIGIT(substring(theText, tag_start + 3, 1)))
- if(IS_HEXDIGIT(substring(theText, tag_start + 4, 1)))
- {
- if(!isCaretEscaped(theText, tag_start))
- return 5 - (pos - tag_start); // ^xRGB color code found
- }
- }
- return 0;
-}
-
float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
{
// STOP.
{
middle = floor((left + right) / 2);
if(colors)
- ofs = skipIncompleteTag(theText, middle, len);
+ {
+ vector res = checkColorCode(theText, len, middle, false);
+ ofs = (res.x) ? res.x - res.y : 0;
+ }
+
if(w(substring(theText, 0, middle + ofs), theSize) <= maxWidth)
left = middle + ofs;
else
{
middle = floor((left + right) / 2);
if(colors)
- ofs = skipIncompleteTag(theText, middle, len);
+ {
+ vector res = checkColorCode(theText, len, middle, true);
+ ofs = (!res.x) ? 0 : res.x - res.y;
+ }
+
if(w(substring(theText, 0, middle + ofs)) <= maxWidth)
left = middle + ofs;
else
}
// it returns 0 if pos is NOT in the middle or at the end of a color code
-// otherwise it returns a 2-digit number with cc_len as the first digit
-// and the offset from '^' position to pos as the second digit
+// otherwise it returns a vector with color code length as the first component
+// and the offset from '^' position to pos as the second component
// e.g.:
-// "a^2xy" | returns 0 if pos == 0 or 1 or 4
-// ^^ | returns 21 or 22 if pos == 2 or 3
+// "j^2kl" | returns 0 if pos == 0 or 1 or 4
+// ^^ | returns '2 1' or '2 2' if pos == 2 or 3
ERASEABLE
-int checkColorCode(string theText, int pos)
+vector checkColorCode(string theText, int text_len, int pos, bool check_at_the_end)
{
- int text_len = strlen(theText);
+ if (text_len == 0)
+ text_len = strlen(theText);
string tag_type = "^";
int cc_len = 2;
int tag_len = 1;
LABEL(check_color_tag)
- for (int ofs = cc_len; ofs >= 1; ofs--)
+ int ofs = cc_len;
+ if (!check_at_the_end)
+ ofs--;
+ for (; ofs >= 1; ofs--)
{
if (!(pos >= ofs && text_len >= pos + (cc_len - ofs)))
continue;
if(substring(theText, pos - ofs, tag_len) == tag_type)
{
if (!isCaretEscaped(theText, pos - ofs) && isValidColorCodeValue(theText, cc_len, pos - ofs))
- return cc_len * 10 + ofs;
+ return eX * cc_len + eY * ofs;
}
}
if (cc_len == 2)
tag_len = 2;
goto check_color_tag;
}
- return 0;
+ return '0 0 0';
}
for (;;)
{
i = me.controlledTextbox.cursorPos;
-
- int res = checkColorCode(me.controlledTextbox.text, i);
- if (res)
- {
- int tag_length = floor(res / 10);
- int ofs = res % 10;
- for (int j = tag_length - ofs; j > 0; j--)
- me.controlledTextbox.keyDown(me.controlledTextbox, K_RIGHTARROW, 8, 0);
- for (int j = tag_length; j > 0; j--)
- me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
- continue;
- }
-
- break;
+ string theText = me.controlledTextbox.text;
+ vector res = checkColorCode(theText, strlen(theText), i, true);
+ if (!res.x)
+ break;
+
+ int cc_len = res.x;
+ int ofs = res.y;
+ for (int j = cc_len - ofs; j > 0; j--)
+ me.controlledTextbox.keyDown(me.controlledTextbox, K_RIGHTARROW, 8, 0);
+ for (int j = cc_len; j > 0; j--)
+ me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
}
if(substring(me.controlledTextbox.text, i-1, 1) == "^")