+ // if no any 'free' fonts - expand buffer
+ oldsize = dp_fonts.maxsize;
+ dp_fonts.maxsize = dp_fonts.maxsize + FONTS_EXPAND;
+ if (developer_font.integer)
+ Con_Printf("FindFont: enlarging fonts buffer (%i -> %i)\n", oldsize, dp_fonts.maxsize);
+ dp_fonts.f = (dp_font_t *)Mem_Realloc(fonts_mempool, dp_fonts.f, sizeof(dp_font_t) * dp_fonts.maxsize);
+ // relink ft2 structures
+ for(i = 0; i < oldsize; ++i)
+ if (dp_fonts.f[i].ft2)
+ dp_fonts.f[i].ft2->settings = &dp_fonts.f[i].settings;
+ // register a font in first expanded slot
+ strlcpy(dp_fonts.f[oldsize].title, title, sizeof(dp_fonts.f[oldsize].title));
+ return &dp_fonts.f[oldsize];
+ }
+ return NULL;
+}
+
+static float snap_to_pixel_x(float x, float roundUpAt)
+{
+ float pixelpos = x * vid.width / vid_conwidth.value;
+ int snap = (int) pixelpos;
+ if (pixelpos - snap >= roundUpAt) ++snap;
+ return ((float)snap * vid_conwidth.value / vid.width);
+ /*
+ x = (int)(x * vid.width / vid_conwidth.value);
+ x = (x * vid_conwidth.value / vid.width);
+ return x;
+ */
+}
+
+static float snap_to_pixel_y(float y, float roundUpAt)
+{
+ float pixelpos = y * vid.height / vid_conheight.value;
+ int snap = (int) pixelpos;
+ if (pixelpos - snap > roundUpAt) ++snap;
+ return ((float)snap * vid_conheight.value / vid.height);
+ /*
+ y = (int)(y * vid.height / vid_conheight.value);
+ y = (y * vid_conheight.value / vid.height);
+ return y;
+ */
+}
+
+static void LoadFont_f(void)
+{
+ dp_font_t *f;
+ int i, sizes;
+ const char *filelist, *c, *cm;
+ float sz, scale, voffset;
+ char mainfont[MAX_QPATH];
+
+ if(Cmd_Argc() < 2)
+ {
+ Con_Printf("Available font commands:\n");
+ for(i = 0; i < dp_fonts.maxsize; ++i)
+ if (dp_fonts.f[i].title[0])
+ Con_Printf(" loadfont %s gfx/tgafile[...] [custom switches] [sizes...]\n", dp_fonts.f[i].title);
+ Con_Printf("A font can simply be gfx/tgafile, or alternatively you\n"
+ "can specify multiple fonts and faces\n"
+ "Like this: gfx/vera-sans:2,gfx/fallback:1\n"
+ "to load face 2 of the font gfx/vera-sans and use face 1\n"
+ "of gfx/fallback as fallback font.\n"
+ "You can also specify a list of font sizes to load, like this:\n"
+ "loadfont console gfx/conchars,gfx/fallback 8 12 16 24 32\n"
+ "In many cases, 8 12 16 24 32 should be a good choice.\n"
+ "custom switches:\n"
+ " scale x : scale all characters by this amount when rendering (doesnt change line height)\n"
+ " voffset x : offset all chars vertical when rendering, this is multiplied to character height\n"
+ );
+ return;
+ }
+ f = FindFont(Cmd_Argv(1), true);
+ if(f == NULL)
+ {
+ Con_Printf("font function not found\n");
+ return;
+ }
+
+ if(Cmd_Argc() < 3)
+ filelist = "gfx/conchars";
+ else
+ filelist = Cmd_Argv(2);
+
+ memset(f->fallbacks, 0, sizeof(f->fallbacks));
+ memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
+
+ // first font is handled "normally"
+ c = strchr(filelist, ':');
+ cm = strchr(filelist, ',');
+ if(c && (!cm || c < cm))
+ f->req_face = atoi(c+1);
+ else
+ {
+ f->req_face = 0;
+ c = cm;
+ }
+
+ if(!c || (c - filelist) > MAX_QPATH)
+ strlcpy(mainfont, filelist, sizeof(mainfont));
+ else
+ {
+ memcpy(mainfont, filelist, c - filelist);
+ mainfont[c - filelist] = 0;
+ }
+
+ for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
+ {
+ c = strchr(filelist, ',');
+ if(!c)
+ break;
+ filelist = c + 1;
+ if(!*filelist)
+ break;
+ c = strchr(filelist, ':');
+ cm = strchr(filelist, ',');
+ if(c && (!cm || c < cm))
+ f->fallback_faces[i] = atoi(c+1);
+ else
+ {
+ f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
+ c = cm;
+ }
+ if(!c || (c-filelist) > MAX_QPATH)
+ {
+ strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
+ }
+ else
+ {
+ memcpy(f->fallbacks[i], filelist, c - filelist);
+ f->fallbacks[i][c - filelist] = 0;
+ }
+ }
+
+ // for now: by default load only one size: the default size
+ f->req_sizes[0] = 0;
+ for(i = 1; i < MAX_FONT_SIZES; ++i)
+ f->req_sizes[i] = -1;
+
+ scale = 1;
+ voffset = 0;
+ if(Cmd_Argc() >= 4)
+ {
+ for(sizes = 0, i = 3; i < Cmd_Argc(); ++i)
+ {
+ // special switches
+ if (!strcmp(Cmd_Argv(i), "scale"))
+ {
+ i++;
+ if (i < Cmd_Argc())
+ scale = atof(Cmd_Argv(i));
+ continue;
+ }
+ if (!strcmp(Cmd_Argv(i), "voffset"))
+ {
+ i++;
+ if (i < Cmd_Argc())
+ voffset = atof(Cmd_Argv(i));
+ continue;
+ }
+
+ if (sizes == -1)
+ continue; // no slot for other sizes
+
+ // parse one of sizes
+ sz = atof(Cmd_Argv(i));
+ if (sz > 0.001f && sz < 1000.0f) // do not use crap sizes
+ {
+ // search for duplicated sizes
+ int j;
+ for (j=0; j<sizes; j++)
+ if (f->req_sizes[j] == sz)
+ break;
+ if (j != sizes)
+ continue; // sz already in req_sizes, don't add it again
+
+ if (sizes == MAX_FONT_SIZES)
+ {
+ Con_Printf("Warning: specified more than %i different font sizes, exceding ones are ignored\n", MAX_FONT_SIZES);
+ sizes = -1;
+ continue;
+ }
+ f->req_sizes[sizes] = sz;
+ sizes++;
+ }
+ }
+ }
+
+ LoadFont(true, mainfont, f, scale, voffset);
+}
+
+/*
+===============
+Draw_Init
+===============
+*/
+static void gl_draw_start(void)
+{
+ int i;
+ char vabuf[1024];
+ drawtexturepool = R_AllocTexturePool();
+
+ numcachepics = 0;
+ memset(cachepichash, 0, sizeof(cachepichash));
+
+ font_start();
+
+ // load default font textures
+ for(i = 0; i < dp_fonts.maxsize; ++i)
+ if (dp_fonts.f[i].title[0])
+ LoadFont(false, va(vabuf, sizeof(vabuf), "gfx/font_%s", dp_fonts.f[i].title), &dp_fonts.f[i], 1, 0);
+
+ // draw the loading screen so people have something to see in the newly opened window
+ SCR_UpdateLoadingScreen(true, true);
+}
+
+static void gl_draw_shutdown(void)
+{
+ font_shutdown();
+
+ R_FreeTexturePool(&drawtexturepool);
+
+ numcachepics = 0;
+ memset(cachepichash, 0, sizeof(cachepichash));
+}
+
+static void gl_draw_newmap(void)
+{
+ font_newmap();
+}
+
+void GL_Draw_Init (void)
+{
+ int i, j;
+
+ Cvar_RegisterVariable(&r_font_postprocess_blur);
+ Cvar_RegisterVariable(&r_font_postprocess_outline);
+ Cvar_RegisterVariable(&r_font_postprocess_shadow_x);
+ Cvar_RegisterVariable(&r_font_postprocess_shadow_y);
+ Cvar_RegisterVariable(&r_font_postprocess_shadow_z);
+ Cvar_RegisterVariable(&r_font_hinting);
+ Cvar_RegisterVariable(&r_font_antialias);
+ Cvar_RegisterVariable(&r_textshadow);
+ Cvar_RegisterVariable(&r_textbrightness);
+ Cvar_RegisterVariable(&r_textcontrast);
+ Cvar_RegisterVariable(&r_nearest_2d);
+ Cvar_RegisterVariable(&r_nearest_conchars);
+
+ // allocate fonts storage
+ fonts_mempool = Mem_AllocPool("FONTS", 0, NULL);
+ dp_fonts.maxsize = MAX_FONTS;
+ dp_fonts.f = (dp_font_t *)Mem_Alloc(fonts_mempool, sizeof(dp_font_t) * dp_fonts.maxsize);
+ memset(dp_fonts.f, 0, sizeof(dp_font_t) * dp_fonts.maxsize);
+
+ // assign starting font names
+ strlcpy(FONT_DEFAULT->title, "default", sizeof(FONT_DEFAULT->title));
+ strlcpy(FONT_DEFAULT->texpath, "gfx/conchars", sizeof(FONT_DEFAULT->texpath));
+ strlcpy(FONT_CONSOLE->title, "console", sizeof(FONT_CONSOLE->title));
+ strlcpy(FONT_SBAR->title, "sbar", sizeof(FONT_SBAR->title));
+ strlcpy(FONT_NOTIFY->title, "notify", sizeof(FONT_NOTIFY->title));
+ strlcpy(FONT_CHAT->title, "chat", sizeof(FONT_CHAT->title));
+ strlcpy(FONT_CENTERPRINT->title, "centerprint", sizeof(FONT_CENTERPRINT->title));
+ strlcpy(FONT_INFOBAR->title, "infobar", sizeof(FONT_INFOBAR->title));
+ strlcpy(FONT_MENU->title, "menu", sizeof(FONT_MENU->title));
+ for(i = 0, j = 0; i < MAX_USERFONTS; ++i)
+ if(!FONT_USER(i)->title[0])
+ dpsnprintf(FONT_USER(i)->title, sizeof(FONT_USER(i)->title), "user%d", j++);
+
+ Cmd_AddCommand ("loadfont",LoadFont_f, "loadfont function tganame loads a font; example: loadfont console gfx/veramono; loadfont without arguments lists the available functions");
+ R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap, NULL, NULL);
+}
+
+static void _DrawQ_Setup(void) // see R_ResetViewRendering2D
+{
+ if (r_refdef.draw2dstage == 1)
+ return;
+ r_refdef.draw2dstage = 1;
+
+ R_ResetViewRendering2D_Common(0, NULL, NULL, vid_conwidth.integer, vid_conheight.integer);
+}
+
+qboolean r_draw2d_force = false;
+static void _DrawQ_SetupAndProcessDrawFlag(int flags, cachepic_t *pic, float alpha)
+{
+ _DrawQ_Setup();
+ if(!r_draw2d.integer && !r_draw2d_force)
+ return;
+ DrawQ_ProcessDrawFlag(flags, (alpha < 1) || (pic && pic->hasalpha));
+}
+void DrawQ_ProcessDrawFlag(int flags, qboolean alpha)
+{
+ if(flags == DRAWFLAG_ADDITIVE)
+ {
+ GL_DepthMask(false);
+ GL_BlendFunc(alpha ? GL_SRC_ALPHA : GL_ONE, GL_ONE);
+ }
+ else if(flags == DRAWFLAG_MODULATE)
+ {
+ GL_DepthMask(false);
+ GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
+ }
+ else if(flags == DRAWFLAG_2XMODULATE)
+ {
+ GL_DepthMask(false);
+ GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
+ }
+ else if(flags == DRAWFLAG_SCREEN)
+ {
+ GL_DepthMask(false);
+ GL_BlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE);
+ }
+ else if(alpha)
+ {
+ GL_DepthMask(false);
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ }
+ else
+ {
+ GL_DepthMask(true);
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ }
+}
+
+void DrawQ_Pic(float x, float y, cachepic_t *pic, float width, float height, float red, float green, float blue, float alpha, int flags)
+{
+ float floats[36];
+
+ _DrawQ_SetupAndProcessDrawFlag(flags, pic, alpha);
+ if(!r_draw2d.integer && !r_draw2d_force)
+ return;
+
+// R_Mesh_ResetTextureState();
+ floats[12] = 0.0f;floats[13] = 0.0f;
+ floats[14] = 1.0f;floats[15] = 0.0f;
+ floats[16] = 1.0f;floats[17] = 1.0f;
+ floats[18] = 0.0f;floats[19] = 1.0f;
+ floats[20] = floats[24] = floats[28] = floats[32] = red;
+ floats[21] = floats[25] = floats[29] = floats[33] = green;
+ floats[22] = floats[26] = floats[30] = floats[34] = blue;
+ floats[23] = floats[27] = floats[31] = floats[35] = alpha;
+ if (pic)
+ {
+ if (width == 0)
+ width = pic->width;
+ if (height == 0)
+ height = pic->height;
+ R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
+
+#if 0
+ // AK07: lets be texel correct on the corners
+ {
+ float horz_offset = 0.5f / pic->width;
+ float vert_offset = 0.5f / pic->height;
+
+ floats[12] = 0.0f + horz_offset;floats[13] = 0.0f + vert_offset;
+ floats[14] = 1.0f - horz_offset;floats[15] = 0.0f + vert_offset;
+ floats[16] = 1.0f - horz_offset;floats[17] = 1.0f - vert_offset;
+ floats[18] = 0.0f + horz_offset;floats[19] = 1.0f - vert_offset;
+ }
+#endif
+ }
+ else
+ R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
+
+ floats[2] = floats[5] = floats[8] = floats[11] = 0;
+ floats[0] = floats[9] = x;
+ floats[1] = floats[4] = y;
+ floats[3] = floats[6] = x + width;
+ floats[7] = floats[10] = y + height;
+
+ R_Mesh_PrepareVertices_Generic_Arrays(4, floats, floats + 20, floats + 12);
+ R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
+}
+
+void DrawQ_RotPic(float x, float y, cachepic_t *pic, float width, float height, float org_x, float org_y, float angle, float red, float green, float blue, float alpha, int flags)
+{
+ float floats[36];
+ float af = DEG2RAD(-angle); // forward
+ float ar = DEG2RAD(-angle + 90); // right
+ float sinaf = sin(af);
+ float cosaf = cos(af);
+ float sinar = sin(ar);
+ float cosar = cos(ar);
+
+ _DrawQ_SetupAndProcessDrawFlag(flags, pic, alpha);
+ if(!r_draw2d.integer && !r_draw2d_force)
+ return;
+
+// R_Mesh_ResetTextureState();
+ if (pic)
+ {
+ if (width == 0)
+ width = pic->width;
+ if (height == 0)
+ height = pic->height;
+ R_SetupShader_Generic(Draw_GetPicTexture(pic), NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
+ }
+ else
+ R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
+
+ floats[2] = floats[5] = floats[8] = floats[11] = 0;
+
+// top left
+ floats[0] = x - cosaf*org_x - cosar*org_y;
+ floats[1] = y - sinaf*org_x - sinar*org_y;
+
+// top right
+ floats[3] = x + cosaf*(width-org_x) - cosar*org_y;
+ floats[4] = y + sinaf*(width-org_x) - sinar*org_y;
+
+// bottom right
+ floats[6] = x + cosaf*(width-org_x) + cosar*(height-org_y);
+ floats[7] = y + sinaf*(width-org_x) + sinar*(height-org_y);
+
+// bottom left
+ floats[9] = x - cosaf*org_x + cosar*(height-org_y);
+ floats[10] = y - sinaf*org_x + sinar*(height-org_y);
+
+ floats[12] = 0.0f;floats[13] = 0.0f;
+ floats[14] = 1.0f;floats[15] = 0.0f;
+ floats[16] = 1.0f;floats[17] = 1.0f;
+ floats[18] = 0.0f;floats[19] = 1.0f;
+ floats[20] = floats[24] = floats[28] = floats[32] = red;
+ floats[21] = floats[25] = floats[29] = floats[33] = green;
+ floats[22] = floats[26] = floats[30] = floats[34] = blue;
+ floats[23] = floats[27] = floats[31] = floats[35] = alpha;
+
+ R_Mesh_PrepareVertices_Generic_Arrays(4, floats, floats + 20, floats + 12);
+ R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
+}
+
+void DrawQ_Fill(float x, float y, float width, float height, float red, float green, float blue, float alpha, int flags)
+{
+ float floats[36];
+
+ _DrawQ_SetupAndProcessDrawFlag(flags, NULL, alpha);
+ if(!r_draw2d.integer && !r_draw2d_force)
+ return;
+
+// R_Mesh_ResetTextureState();
+ R_SetupShader_Generic_NoTexture((flags & DRAWFLAGS_BLEND) ? false : true, true);
+
+ floats[2] = floats[5] = floats[8] = floats[11] = 0;
+ floats[0] = floats[9] = x;
+ floats[1] = floats[4] = y;
+ floats[3] = floats[6] = x + width;
+ floats[7] = floats[10] = y + height;
+ floats[12] = 0.0f;floats[13] = 0.0f;
+ floats[14] = 1.0f;floats[15] = 0.0f;
+ floats[16] = 1.0f;floats[17] = 1.0f;
+ floats[18] = 0.0f;floats[19] = 1.0f;
+ floats[20] = floats[24] = floats[28] = floats[32] = red;
+ floats[21] = floats[25] = floats[29] = floats[33] = green;
+ floats[22] = floats[26] = floats[30] = floats[34] = blue;
+ floats[23] = floats[27] = floats[31] = floats[35] = alpha;
+
+ R_Mesh_PrepareVertices_Generic_Arrays(4, floats, floats + 20, floats + 12);
+ R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
+}
+
+/// color tag printing
+static const vec4_t string_colors[] =
+{
+ // Quake3 colors
+ // LordHavoc: why on earth is cyan before magenta in Quake3?
+ // LordHavoc: note: Doom3 uses white for [0] and [7]
+ {0.0, 0.0, 0.0, 1.0}, // black
+ {1.0, 0.0, 0.0, 1.0}, // red
+ {0.0, 1.0, 0.0, 1.0}, // green
+ {1.0, 1.0, 0.0, 1.0}, // yellow
+ {0.0, 0.0, 1.0, 1.0}, // blue
+ {0.0, 1.0, 1.0, 1.0}, // cyan
+ {1.0, 0.0, 1.0, 1.0}, // magenta
+ {1.0, 1.0, 1.0, 1.0}, // white
+ // [515]'s BX_COLOREDTEXT extension
+ {1.0, 1.0, 1.0, 0.5}, // half transparent
+ {0.5, 0.5, 0.5, 1.0} // half brightness
+ // Black's color table
+ //{1.0, 1.0, 1.0, 1.0},
+ //{1.0, 0.0, 0.0, 1.0},
+ //{0.0, 1.0, 0.0, 1.0},
+ //{0.0, 0.0, 1.0, 1.0},
+ //{1.0, 1.0, 0.0, 1.0},
+ //{0.0, 1.0, 1.0, 1.0},
+ //{1.0, 0.0, 1.0, 1.0},
+ //{0.1, 0.1, 0.1, 1.0}
+};
+
+#define STRING_COLORS_COUNT (sizeof(string_colors) / sizeof(vec4_t))
+
+static void DrawQ_GetTextColor(float color[4], int colorindex, float r, float g, float b, float a, qboolean shadow)
+{
+ float C = r_textcontrast.value;
+ float B = r_textbrightness.value;
+ if (colorindex & 0x10000) // that bit means RGB color
+ {
+ color[0] = ((colorindex >> 12) & 0xf) / 15.0;
+ color[1] = ((colorindex >> 8) & 0xf) / 15.0;
+ color[2] = ((colorindex >> 4) & 0xf) / 15.0;
+ color[3] = (colorindex & 0xf) / 15.0;
+ }
+ else
+ Vector4Copy(string_colors[colorindex], color);
+ Vector4Set(color, color[0] * r * C + B, color[1] * g * C + B, color[2] * b * C + B, color[3] * a);
+ if (shadow)
+ {
+ float shadowalpha = (color[0]+color[1]+color[2]) * 0.8;
+ Vector4Set(color, 0, 0, 0, color[3] * bound(0, shadowalpha, 1));
+ }
+}
+
+// NOTE: this function always draws exactly one character if maxwidth <= 0
+float DrawQ_TextWidth_UntilWidth_TrackColors_Scale(const char *text, size_t *maxlen, float w, float h, float sw, float sh, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt, float maxwidth)
+{
+ const char *text_start = text;
+ int colorindex = STRING_COLOR_DEFAULT;
+ size_t i;
+ float x = 0;
+ Uchar ch, mapch, nextch;
+ Uchar prevch = 0; // used for kerning
+ int tempcolorindex;
+ float kx;
+ int map_index = 0;
+ size_t bytes_left;
+ ft2_font_map_t *fontmap = NULL;
+ ft2_font_map_t *map = NULL;
+ //ft2_font_map_t *prevmap = NULL;
+ ft2_font_t *ft2 = fnt->ft2;
+ // float ftbase_x;
+ qboolean snap = true;
+ qboolean least_one = false;
+ float dw; // display w
+ //float dh; // display h
+ const float *width_of;
+
+ if (!h) h = w;
+ if (!h) {
+ w = h = 1;
+ snap = false;
+ }
+ // do this in the end
+ w *= fnt->settings.scale;
+ h *= fnt->settings.scale;
+
+ // find the most fitting size:
+ if (ft2 != NULL)
+ {
+ if (snap)
+ map_index = Font_IndexForSize(ft2, h, &w, &h);
+ else
+ map_index = Font_IndexForSize(ft2, h, NULL, NULL);
+ fontmap = Font_MapForIndex(ft2, map_index);
+ }
+
+ dw = w * sw;
+ //dh = h * sh;
+
+ if (*maxlen < 1)
+ *maxlen = 1<<30;
+
+ if (!outcolor || *outcolor == -1)
+ colorindex = STRING_COLOR_DEFAULT;
+ else
+ colorindex = *outcolor;
+
+ // maxwidth /= fnt->scale; // w and h are multiplied by it already
+ // ftbase_x = snap_to_pixel_x(0);
+
+ if(maxwidth <= 0)
+ {
+ 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;
+
+ i = 0;
+ while (((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;
+}
+
+float DrawQ_Color[4];
+float DrawQ_String_Scale(float startx, float starty, const char *text, size_t maxlen, float w, float h, float sw, float sh, float basered, float basegreen, float baseblue, float basealpha, int flags, int *outcolor, qboolean ignorecolorcodes, const dp_font_t *fnt)
+{
+ int shadow, colorindex = STRING_COLOR_DEFAULT;
+ size_t i;
+ float x = startx, y, s, t, u, v, thisw;
+ float *av, *at, *ac;
+ int batchcount;
+ static float vertex3f[QUADELEMENTS_MAXQUADS*4*3];
+ static float texcoord2f[QUADELEMENTS_MAXQUADS*4*2];
+ static float color4f[QUADELEMENTS_MAXQUADS*4*4];
+ Uchar ch, mapch, nextch;
+ Uchar prevch = 0; // used for kerning
+ int tempcolorindex;
+ int map_index = 0;
+ //ft2_font_map_t *prevmap = NULL; // the previous map
+ ft2_font_map_t *map = NULL; // the currently used map
+ ft2_font_map_t *fontmap = NULL; // the font map for the size
+ float ftbase_y;
+ const char *text_start = text;
+ float kx, ky;
+ ft2_font_t *ft2 = fnt->ft2;
+ qboolean snap = true;
+ float pix_x, pix_y;
+ size_t bytes_left;
+ float dw, dh;
+ const float *width_of;
+
+ int tw, th;
+ tw = R_TextureWidth(fnt->tex);
+ th = R_TextureHeight(fnt->tex);
+
+ if (!h) h = w;
+ if (!h) {
+ h = w = 1;
+ snap = false;
+ }
+
+ starty -= (fnt->settings.scale - 1) * h * 0.5 - fnt->settings.voffset*h; // center & offset
+ w *= fnt->settings.scale;
+ h *= fnt->settings.scale;
+
+ if (ft2 != NULL)
+ {
+ if (snap)
+ map_index = Font_IndexForSize(ft2, h, &w, &h);
+ else
+ map_index = Font_IndexForSize(ft2, h, NULL, NULL);
+ fontmap = Font_MapForIndex(ft2, map_index);
+ }
+
+ dw = w * sw;
+ dh = h * sh;
+
+ // draw the font at its baseline when using freetype
+ //ftbase_x = 0;
+ ftbase_y = dh * (4.5/6.0);
+
+ if (maxlen < 1)
+ maxlen = 1<<30;
+
+ _DrawQ_SetupAndProcessDrawFlag(flags, NULL, 0);
+ if(!r_draw2d.integer && !r_draw2d_force)
+ return startx + DrawQ_TextWidth_UntilWidth_TrackColors_Scale(text, &maxlen, w, h, sw, sh, NULL, ignorecolorcodes, fnt, 1000000000);
+
+// R_Mesh_ResetTextureState();
+ if (!fontmap)
+ R_Mesh_TexBind(0, fnt->tex);
+ R_SetupShader_Generic(fnt->tex, NULL, GL_MODULATE, 1, (flags & DRAWFLAGS_BLEND) ? false : true, true, false);
+
+ ac = color4f;
+ at = texcoord2f;
+ av = vertex3f;
+ batchcount = 0;
+
+ //ftbase_x = snap_to_pixel_x(ftbase_x);
+ if(snap)
+ {
+ startx = snap_to_pixel_x(startx, 0.4);
+ starty = snap_to_pixel_y(starty, 0.4);
+ ftbase_y = snap_to_pixel_y(ftbase_y, 0.3);
+ }
+
+ pix_x = vid.width / vid_conwidth.value;
+ pix_y = vid.height / vid_conheight.value;
+
+ if (fontmap)
+ width_of = fontmap->width_of;
+ else
+ width_of = fnt->width_of;
+
+ for (shadow = r_textshadow.value != 0 && basealpha > 0;shadow >= 0;shadow--)
+ {
+ prevch = 0;
+ text = text_start;
+
+ if (!outcolor || *outcolor == -1)
+ colorindex = STRING_COLOR_DEFAULT;
+ else
+ colorindex = *outcolor;
+
+ DrawQ_GetTextColor(DrawQ_Color, colorindex, basered, basegreen, baseblue, basealpha, shadow != 0);
+
+ x = startx;
+ y = starty;
+ /*
+ if (shadow)