+ least_one = true;
+ maxwidth = -maxwidth;
+ }
+
+ //if (snap)
+ // x = snap_to_pixel_x(x, 0.4); // haha, it's 0 anyway
+
+ if (fontmap)
+ width_of = fontmap->width_of;
+ else
+ width_of = fnt->width_of;
+
+ for (i = 0;((bytes_left = *maxlen - (text - text_start)) > 0) && *text;)
+ {
+ size_t i0 = i;
+ nextch = ch = u8_getnchar(text, &text, bytes_left);
+ i = text - text_start;
+ if (!ch)
+ break;
+ if (ch == ' ' && !fontmap)
+ {
+ if(!least_one || i0) // never skip the first character
+ if(x + width_of[(int) ' '] * dw > maxwidth)
+ {
+ i = i0;
+ break; // oops, can't draw this
+ }
+ x += width_of[(int) ' '] * dw;
+ continue;
+ }
+ // i points to the char after ^
+ if (ch == STRING_COLOR_TAG && !ignorecolorcodes && i < *maxlen)
+ {
+ ch = *text; // colors are ascii, so no u8_ needed
+ if (ch <= '9' && ch >= '0') // ^[0-9] found
+ {
+ colorindex = ch - '0';
+ ++text;
+ ++i;
+ continue;
+ }
+ // i points to the char after ^...
+ // i+3 points to 3 in ^x123
+ // i+3 == *maxlen would mean that char is missing
+ else if (ch == STRING_COLOR_RGB_TAG_CHAR && i + 3 < *maxlen ) // ^x found
+ {
+ // building colorindex...
+ ch = tolower(text[1]);
+ tempcolorindex = 0x10000; // binary: 1,0000,0000,0000,0000
+ if (ch <= '9' && ch >= '0') tempcolorindex |= (ch - '0') << 12;
+ else if (ch >= 'a' && ch <= 'f') tempcolorindex |= (ch - 87) << 12;
+ else tempcolorindex = 0;
+ if (tempcolorindex)
+ {
+ ch = tolower(text[2]);
+ if (ch <= '9' && ch >= '0') tempcolorindex |= (ch - '0') << 8;
+ else if (ch >= 'a' && ch <= 'f') tempcolorindex |= (ch - 87) << 8;
+ else tempcolorindex = 0;
+ if (tempcolorindex)
+ {
+ ch = tolower(text[3]);
+ if (ch <= '9' && ch >= '0') tempcolorindex |= (ch - '0') << 4;
+ else if (ch >= 'a' && ch <= 'f') tempcolorindex |= (ch - 87) << 4;
+ else tempcolorindex = 0;
+ if (tempcolorindex)
+ {
+ colorindex = tempcolorindex | 0xf;
+ // ...done! now colorindex has rgba codes (1,rrrr,gggg,bbbb,aaaa)
+ i+=4;
+ text += 4;
+ continue;
+ }
+ }
+ }
+ }
+ else if (ch == STRING_COLOR_TAG) // ^^ found, ignore the first ^ and go to print the second
+ {
+ i++;
+ text++;
+ }
+ i--;
+ }
+ ch = nextch;
+
+ if (!fontmap || (ch <= 0xFF && fontmap->glyphs[ch].image) || (ch >= 0xE000 && ch <= 0xE0FF))
+ {
+ if (ch > 0xE000)
+ ch -= 0xE000;
+ if (ch > 0xFF)
+ continue;
+ if (fontmap)
+ map = ft2_oldstyle_map;
+ prevch = 0;
+ if(!least_one || i0) // never skip the first character
+ if(x + width_of[ch] * dw > maxwidth)
+ {
+ i = i0;
+ break; // oops, can't draw this
+ }
+ x += width_of[ch] * dw;
+ } else {
+ if (!map || map == ft2_oldstyle_map || ch < map->start || ch >= map->start + FONT_CHARS_PER_MAP)
+ {
+ map = FontMap_FindForChar(fontmap, ch);
+ if (!map)
+ {
+ if (!Font_LoadMapForIndex(ft2, map_index, ch, &map))
+ break;
+ if (!map)
+ break;
+ }
+ }
+ mapch = ch - map->start;
+ if (prevch && Font_GetKerningForMap(ft2, map_index, w, h, prevch, ch, &kx, NULL))
+ x += kx * dw;
+ x += map->glyphs[mapch].advance_x * dw;
+ //prevmap = map;
+ prevch = ch;
+ }
+ }
+
+ *maxlen = i;
+
+ if (outcolor)
+ *outcolor = colorindex;
+
+ return x;
+}