1 /* FreeType 2 and UTF-8 encoding support for
11 ================================================================================
12 Function definitions. Taken from the freetype2 headers.
13 ================================================================================
18 (*qFT_Init_FreeType)( FT_Library *alibrary );
20 (*qFT_Done_FreeType)( FT_Library library );
22 (*qFT_New_Face)( FT_Library library,
23 const char* filepathname,
27 (*qFT_New_Memory_Face)( FT_Library library,
28 const FT_Byte* file_base,
33 (*qFT_Done_Face)( FT_Face face );
35 (*qFT_Select_Size)( FT_Face face,
36 FT_Int strike_index );
38 (*qFT_Request_Size)( FT_Face face,
39 FT_Size_Request req );
41 (*qFT_Set_Char_Size)( FT_Face face,
42 FT_F26Dot6 char_width,
43 FT_F26Dot6 char_height,
44 FT_UInt horz_resolution,
45 FT_UInt vert_resolution );
47 (*qFT_Set_Pixel_Sizes)( FT_Face face,
49 FT_UInt pixel_height );
51 (*qFT_Load_Glyph)( FT_Face face,
53 FT_Int32 load_flags );
55 (*qFT_Load_Char)( FT_Face face,
57 FT_Int32 load_flags );
59 (*qFT_Get_Char_Index)( FT_Face face,
62 (*qFT_Render_Glyph)( FT_GlyphSlot slot,
63 FT_Render_Mode render_mode );
65 (*qFT_Get_Kerning)( FT_Face face,
69 FT_Vector *akerning );
72 ================================================================================
73 Support for dynamically loading the FreeType2 library
74 ================================================================================
77 static dllfunction_t ft2funcs[] =
79 {"FT_Init_FreeType", (void **) &qFT_Init_FreeType},
80 {"FT_Done_FreeType", (void **) &qFT_Done_FreeType},
81 {"FT_New_Face", (void **) &qFT_New_Face},
82 {"FT_New_Memory_Face", (void **) &qFT_New_Memory_Face},
83 {"FT_Done_Face", (void **) &qFT_Done_Face},
84 {"FT_Select_Size", (void **) &qFT_Select_Size},
85 {"FT_Request_Size", (void **) &qFT_Request_Size},
86 {"FT_Set_Char_Size", (void **) &qFT_Set_Char_Size},
87 {"FT_Set_Pixel_Sizes", (void **) &qFT_Set_Pixel_Sizes},
88 {"FT_Load_Glyph", (void **) &qFT_Load_Glyph},
89 {"FT_Load_Char", (void **) &qFT_Load_Char},
90 {"FT_Get_Char_Index", (void **) &qFT_Get_Char_Index},
91 {"FT_Render_Glyph", (void **) &qFT_Render_Glyph},
92 {"FT_Get_Kerning", (void **) &qFT_Get_Kerning},
96 /// Handle for FreeType2 DLL
97 static dllhandle_t ft2_dll = NULL;
99 /// Memory pool for fonts
100 static mempool_t *font_mempool= NULL;
101 static rtexturepool_t *font_texturepool = NULL;
103 /// FreeType library handle
104 static FT_Library font_ft2lib = NULL;
110 Unload the FreeType2 DLL
113 void Font_CloseLibrary (void)
116 Mem_FreePool(&font_mempool);
117 if (font_texturepool)
118 R_FreeTexturePool(&font_texturepool);
119 if (font_ft2lib && qFT_Done_FreeType)
121 qFT_Done_FreeType(font_ft2lib);
124 Sys_UnloadLibrary (&ft2_dll);
131 Try to load the FreeType2 DLL
134 qboolean Font_OpenLibrary (void)
136 const char* dllnames [] =
139 #error path for freetype 2 dll
141 #error path for freetype 2 dll
142 #elif defined(MACOSX)
156 if (!Sys_LoadLibrary (dllnames, &ft2_dll, ft2funcs))
165 Initialize the freetype2 font subsystem
169 static font_t test_font;
171 static void font_start(void)
173 if (!Font_OpenLibrary())
176 if (qFT_Init_FreeType(&font_ft2lib))
178 Con_Print("ERROR: Failed to initialize the FreeType2 library!\n");
183 font_mempool = Mem_AllocPool("FONT", 0, NULL);
186 Con_Print("ERROR: Failed to allocate FONT memory pool!\n");
191 font_texturepool = R_AllocTexturePool();
192 if (!font_texturepool)
194 Con_Print("ERROR: Failed to allocate FONT texture pool!\n");
199 if (!Font_LoadFont("gfx/test", 16, &test_font))
201 Con_Print("ERROR: Failed to load test font!\n");
207 static void font_shutdown(void)
212 static void font_newmap(void)
218 R_RegisterModule("Font_FreeType2", font_start, font_shutdown, font_newmap);
222 ================================================================================
223 UTF-8 encoding and decoding functions follow.
224 ================================================================================
227 /** Get the number of characters in in an UTF-8 string.
228 * @param _s An utf-8 encoded null-terminated string.
229 * @return The number of unicode characters in the string.
231 size_t u8_strlen(const char *_s)
234 unsigned char *s = (unsigned char*)_s;
245 // start of a wide character
249 for (++s; *s >= 0x80 && *s <= 0xC0; ++s);
253 // part of a wide character, we ignore that one
254 if (*s <= 0xBF) // 10111111
263 /** Fetch a character from an utf-8 encoded string.
264 * @param _s The start of an utf-8 encoded multi-byte character.
265 * @param _end Will point to after the first multi-byte character.
266 * @return The 32-bit integer representation of the first multi-byte character.
268 Uchar u8_getchar(const char *_s, const char **_end)
270 const unsigned char *s = (unsigned char*)_s;
284 // starting within a wide character - skip it and retrieve the one after it
285 for (++s; *s >= 0x80 && *s < 0xC0; ++s);
286 // or we could return '?' here?
288 // for a little speedup:
289 if ( (*s & 0xE0) == 0xC0 )
292 u = ( (s[0] & 0x1F) << 6 ) | (s[1] & 0x3F);
297 if ( (*s & 0xF0) == 0xE0 )
300 u = ( (s[0] & 0x0F) << 12 ) | ( (s[1] & 0x3F) << 6 ) | (s[2] & 0x3F);
309 for (mask >>= 1; v > (*s & mask); mask >>= 1)
311 u = (Uchar)(*s & mask);
312 for (++s; *s >= 0x80 && *s < 0xC0; ++s)
313 u = (u << 6) | (*s & 0x3F);
316 *_end = (const char*)s;
321 /** Encode a wide-character into utf-8.
322 * @param w The wide character to encode.
323 * @param to The target buffer the utf-8 encoded string is stored to.
324 * @param maxlen The maximum number of bytes that fit into the target buffer.
325 * @return Number of bytes written to the buffer, or less or equal to 0 if the buffer is too small.
327 int u8_fromchar(Uchar w, char *to, size_t maxlen)
344 // for a little speedup
353 to[1] = 0x80 | (w & 0x3F); w >>= 6;
365 to[2] = 0x80 | (w & 0x3F); w >>= 6;
366 to[1] = 0x80 | (w & 0x3F); w >>= 6;
371 // "more general" version:
373 // check how much space we need and store data into a
374 // temp buffer - this is faster than recalculating again
379 tmp[i++] = 0x80 | (w & 0x3F);
380 bt = (bt >> 1) | 0x80;
382 // see if we still fit into the target buffer
383 if (i+1 >= maxlen) // +1 for the \0
386 // there are no characters which take up that much space yet
387 // and there won't be for the next many many years, still... let's be safe
388 if (i >= sizeof(tmp))
392 for (j = 0; j < i; ++j)
401 /** Convert a utf-8 multibyte string to a wide character string.
402 * @param wcs The target wide-character buffer.
403 * @param mb The utf-8 encoded multibyte string to convert.
404 * @param maxlen The maximum number of wide-characters that fit into the target buffer.
405 * @return The number of characters written to the target buffer.
407 size_t u8_mbstowcs(Uchar *wcs, const char *mb, size_t maxlen)
410 for (i = 0; *mb && i < maxlen; ++i)
411 *wcs++ = u8_getchar(mb, &mb);
417 /** Convert a wide-character string to a utf-8 multibyte string.
418 * @param mb The target buffer the utf-8 string is written to.
419 * @param wcs The wide-character string to convert.
420 * @param maxlen The number bytes that fit into the multibyte target buffer.
421 * @return The number of bytes written, not including the terminating \0
423 size_t u8_wcstombs(char *mb, const Uchar *wcs, size_t maxlen)
426 const char *start = mb;
427 for (i = 0; *wcs && i < maxlen; ++i)
430 if ( (len = u8_fromchar(*wcs++, mb, maxlen - i)) < 0)
440 ================================================================================
441 Implementation of a more or less lazy font loading and rendering code.
442 ================================================================================
445 // anything should work, but I recommend multiples of 8
446 // since the texture size should be a power of 2
447 #define FONT_CHARS_PER_LINE 16
448 #define FONT_CHAR_LINES 16
449 #define FONT_CHARS_PER_MAP (FONT_CHARS_PER_LINE * FONT_CHAR_LINES)
451 typedef struct glyph_slot_s
453 // we keep the quad coords here only currently
454 // if you need other info, make Font_LoadMapForIndex fill it into this slot
455 double txmin; // texture coordinate in [0,1]
470 struct font_map_s *next;
473 glyph_slot_t glyphs[FONT_CHARS_PER_MAP];
476 static qboolean Font_LoadMapForIndex(font_t *font, Uchar _ch, font_map_t **outmap);
477 qboolean Font_LoadFont(const char *name, int size, font_t *font)
480 char filename[PATH_MAX];
483 memset(font, 0, sizeof(*font));
485 if (!Font_OpenLibrary())
487 Con_Printf("WARNING: can't open load font %s\n"
488 "You need the FreeType2 DLL to load font files\n",
493 namelen = strlen(name);
495 memcpy(filename, name, namelen);
496 memcpy(filename + namelen, ".ttf", 5);
498 font->data = FS_LoadFile(filename, font_mempool, false, &font->datasize);
501 // FS_LoadFile being not-quiet should print an error :)
506 status = qFT_New_Memory_Face(font_ft2lib, (FT_Bytes)font->data, font->datasize, 0, (FT_Face*)&font->face);
509 Con_Printf("ERROR: can't create face for %s\n"
510 "Error %i\n", // TODO: error strings
512 Mem_Free(font->data);
516 memcpy(font->name, name, namelen+1);
518 font->glyphSize = font->size * 2;
519 font->has_kerning = !!(((FT_Face)(font->face))->face_flags & FT_FACE_FLAG_KERNING);
521 status = qFT_Set_Pixel_Sizes((FT_Face)font->face, size, size);
524 Con_Printf("ERROR: can't size pixel sizes for face of font %s\n"
525 "Error %i\n", // TODO: error strings
527 Mem_Free(font->data);
531 if (!Font_LoadMapForIndex(font, 0, NULL))
533 Con_Printf("ERROR: can't load the first character map for %s\n"
536 Mem_Free(font->data);
543 void Font_UnloadFont(font_t *font)
546 Mem_Free(font->data);
551 qFT_Done_Face((FT_Face)font->face);
557 static qboolean Font_LoadMapForIndex(font_t *font, Uchar _ch, font_map_t **outmap)
559 char map_identifier[PATH_MAX];
560 unsigned long mapidx = _ch / FONT_CHARS_PER_MAP;
566 FT_Face face = font->face;
569 int gR, gC; // glyph position: row and column
573 int bytesPerPixel = 4; // change the conversion loop too if you change this!
578 map = Mem_Alloc(font_mempool, sizeof(font_map_t));
581 Con_Printf("ERROR: Out of memory when loading fontmap for %s\n", font->name);
585 pitch = font->glyphSize * FONT_CHARS_PER_LINE * bytesPerPixel;
586 data = Mem_Alloc(font_mempool, (FONT_CHAR_LINES * font->glyphSize) * pitch);
590 Con_Printf("ERROR: Failed to allocate memory for glyph data for font %s\n", font->name);
594 // initialize as white texture with zero alpha
596 while (tp < (FONT_CHAR_LINES * font->glyphSize) * pitch)
604 map->start = mapidx * FONT_CHARS_PER_MAP;
606 font->font_map = map;
609 // insert the map at the right place
610 font_map_t *next = font->font_map;
611 while(next->next && next->next->start < map->start)
613 map->next = next->next;
619 for (ch = map->start;
620 ch < (FT_ULong)map->start + FONT_CHARS_PER_MAP;
627 unsigned char *imagedata, *dst, *src;
628 glyph_slot_t *mapglyph;
630 fprintf(stderr, "------------- GLYPH INFO -----------------\n");
633 if (gC >= FONT_CHARS_PER_LINE)
635 gC -= FONT_CHARS_PER_LINE;
639 imagedata = data + gR * pitch * font->glyphSize + gC * font->glyphSize * bytesPerPixel;
640 glyphIndex = qFT_Get_Char_Index(face, ch);
642 status = qFT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT);
645 Con_Printf("failed to load glyph %lu for %s\n", glyphIndex, font->name);
649 status = qFT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
652 Con_Printf("failed to render glyph %lu for %s\n", glyphIndex, font->name);
657 bmp = &glyph->bitmap;
662 if (w > font->glyphSize || h > font->glyphSize)
663 Con_Printf("WARNING: Glyph %lu is too big in font %s\n", ch, font->name);
665 switch (bmp->pixel_mode)
667 case FT_PIXEL_MODE_MONO:
668 fprintf(stderr, " Pixel Mode: MONO\n");
670 case FT_PIXEL_MODE_GRAY2:
671 fprintf(stderr, " Pixel Mode: GRAY2\n");
673 case FT_PIXEL_MODE_GRAY4:
674 fprintf(stderr, " Pixel Mode: GRAY4\n");
676 case FT_PIXEL_MODE_GRAY:
677 fprintf(stderr, " Pixel Mode: GRAY\n");
680 fprintf(stderr, " Pixel Mode: Unknown: %i\n", bmp->pixel_mode);
684 for (y = 0; y < h; ++y)
686 dst = imagedata + y * pitch;
687 src = bmp->buffer + y * bmp->pitch;
689 switch (bmp->pixel_mode)
691 case FT_PIXEL_MODE_MONO:
692 for (x = 0; x < bmp->width; x += 8)
694 unsigned char ch = *src++;
695 dst += 3; // shift to alpha byte
696 *dst = 255 * ((ch & 0x80) >> 7); dst += 4;
697 *dst = 255 * ((ch & 0x40) >> 6); dst += 4;
698 *dst = 255 * ((ch & 0x20) >> 5); dst += 4;
699 *dst = 255 * ((ch & 0x10) >> 4); dst += 4;
700 *dst = 255 * ((ch & 0x08) >> 3); dst += 4;
701 *dst = 255 * ((ch & 0x04) >> 2); dst += 4;
702 *dst = 255 * ((ch & 0x02) >> 1); dst += 4;
703 *dst = 255 * ((ch & 0x01) >> 0); dst++; // compensate the first += 3
706 case FT_PIXEL_MODE_GRAY2:
707 for (x = 0; x < bmp->width; x += 4)
709 unsigned char ch = *src++;
710 dst += 3; // shift to alpha byte
711 *dst = ( ((ch & 0xA0) >> 6) * 0x55 ); ch <<= 2; dst += 4;
712 *dst = ( ((ch & 0xA0) >> 6) * 0x55 ); ch <<= 2; dst += 4;
713 *dst = ( ((ch & 0xA0) >> 6) * 0x55 ); ch <<= 2; dst += 4;
714 *dst = ( ((ch & 0xA0) >> 6) * 0x55 ); ch <<= 2; dst++; // compensate the +=3
717 case FT_PIXEL_MODE_GRAY4:
718 for (x = 0; x < bmp->width; x += 2)
720 unsigned char ch = *src++;
721 dst += 3; // shift to alpha byte
722 *dst = ( ((ch & 0xF0) >> 4) * 0x24); dst += 4;
723 *dst = ( ((ch & 0x0F) ) * 0x24); dst++; // compensate the += 3
726 case FT_PIXEL_MODE_GRAY:
727 // in this case pitch should equal width
728 for (tp = 0; tp < bmp->pitch; ++tp)
729 dst[3 + tp*4] = src[tp]; // copy the grey value into the alpha bytes
731 //memcpy((void*)dst, (void*)src, bmp->pitch);
739 // now fill map->glyphs[ch - map->start]
740 mapch = ch - map->start;
741 mapglyph = &map->glyphs[mapch];
744 double sfx = (1.0/64.0) / (double)font->size;
745 double sfy = (1.0/64.0) / (double)font->size;
746 double bearingX = (double)glyph->metrics.horiBearingX * sfx;
747 double bearingY = (double)glyph->metrics.horiBearingY * sfy;
748 double advance = (double)glyph->metrics.horiAdvance * sfx;
749 double mWidth = (double)glyph->metrics.width * sfx;
750 double mHeight = (double)glyph->metrics.height * sfy;
751 //double tWidth = bmp->width / (double)font->size;
752 //double tHeight = bmp->rows / (double)font->size;
754 mapglyph->vxmin = bearingX;
755 mapglyph->vxmax = bearingX + mWidth;
756 mapglyph->vymin = -bearingY;
757 mapglyph->vymax = mHeight - bearingY;
758 mapglyph->txmin = ( (double)(gC * font->glyphSize) ) / ( (double)(font->glyphSize * FONT_CHARS_PER_LINE) );
759 mapglyph->txmax = mapglyph->txmin + (double)bmp->width / ( (double)(font->glyphSize * FONT_CHARS_PER_LINE) );
760 mapglyph->tymin = ( (double)(gR * font->glyphSize) ) / ( (double)(font->glyphSize * FONT_CHAR_LINES) );
761 mapglyph->tymax = mapglyph->tymin + (double)bmp->rows / ( (double)(font->glyphSize * FONT_CHAR_LINES) );
762 //mapglyph->advance_x = advance * font->size;
763 mapglyph->advance_x = advance;
764 mapglyph->advance_y = 0;
768 fprintf(stderr, " Glyph: %lu at (%i, %i)\n", (unsigned long)ch, gC, gR);
769 if (ch >= 32 && ch <= 128)
770 fprintf(stderr, " Character: %c\n", (int)ch);
771 fprintf(stderr, " Vertex info:\n");
772 fprintf(stderr, " X: ( %f -- %f )\n", mapglyph->vxmin, mapglyph->vxmax);
773 fprintf(stderr, " Y: ( %f -- %f )\n", mapglyph->vymin, mapglyph->vymax);
774 fprintf(stderr, " Texture info:\n");
775 fprintf(stderr, " S: ( %f -- %f )\n", mapglyph->txmin, mapglyph->txmax);
776 fprintf(stderr, " T: ( %f -- %f )\n", mapglyph->tymin, mapglyph->tymax);
777 fprintf(stderr, " Advance: %f, %f\n", mapglyph->advance_x, mapglyph->advance_y);
782 // create a texture from the data now
783 dpsnprintf(map_identifier, sizeof(map_identifier), "%s_%u_%x.rgba", font->name, (unsigned)map->start/FONT_CHARS_PER_MAP, (unsigned)map->start);
785 FILE *f = fopen(map_identifier, "wb");
786 fwrite(data, pitch * FONT_CHAR_LINES * font->glyphSize, 1, f);
789 map->texture = R_LoadTexture2D(font_texturepool, map_identifier,
790 font->glyphSize * FONT_CHARS_PER_LINE,
791 font->glyphSize * FONT_CHAR_LINES,
792 data, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_ALWAYSPRECACHE | TEXF_MIPMAP, NULL);
796 // if the first try isn't successful, keep it with a broken texture
797 // otherwise we retry to load it every single frame where ft2 rendering is used
798 // this would be bad...
799 // only `data' must be freed
807 extern void _DrawQ_Setup(void);
809 // TODO: If no additional stuff ends up in the following static functions
810 // use the DrawQ ones!
811 static void _Font_ProcessDrawFlag(int flags)
815 if(flags == DRAWFLAG_ADDITIVE)
816 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
817 else if(flags == DRAWFLAG_MODULATE)
818 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
819 else if(flags == DRAWFLAG_2XMODULATE)
820 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
821 else if(flags == DRAWFLAG_SCREEN)
822 GL_BlendFunc(GL_ONE_MINUS_DST_COLOR,GL_ONE);
824 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
827 extern cvar_t r_textcontrast, r_textbrightness, r_textshadow;
828 static const vec4_t string_colors[] =
831 // LordHavoc: why on earth is cyan before magenta in Quake3?
832 // LordHavoc: note: Doom3 uses white for [0] and [7]
833 {0.0, 0.0, 0.0, 1.0}, // black
834 {1.0, 0.0, 0.0, 1.0}, // red
835 {0.0, 1.0, 0.0, 1.0}, // green
836 {1.0, 1.0, 0.0, 1.0}, // yellow
837 {0.0, 0.0, 1.0, 1.0}, // blue
838 {0.0, 1.0, 1.0, 1.0}, // cyan
839 {1.0, 0.0, 1.0, 1.0}, // magenta
840 {1.0, 1.0, 1.0, 1.0}, // white
841 // [515]'s BX_COLOREDTEXT extension
842 {1.0, 1.0, 1.0, 0.5}, // half transparent
843 {0.5, 0.5, 0.5, 1.0} // half brightness
844 // Black's color table
845 //{1.0, 1.0, 1.0, 1.0},
846 //{1.0, 0.0, 0.0, 1.0},
847 //{0.0, 1.0, 0.0, 1.0},
848 //{0.0, 0.0, 1.0, 1.0},
849 //{1.0, 1.0, 0.0, 1.0},
850 //{0.0, 1.0, 1.0, 1.0},
851 //{1.0, 0.0, 1.0, 1.0},
852 //{0.1, 0.1, 0.1, 1.0}
855 static void Font_GetTextColor(float color[4], int colorindex, float r, float g, float b, float a, qboolean shadow)
857 float C = r_textcontrast.value;
858 float B = r_textbrightness.value;
859 if (colorindex & 0x10000) // that bit means RGB color
861 color[0] = ((colorindex >> 12) & 0xf) / 15.0;
862 color[1] = ((colorindex >> 8) & 0xf) / 15.0;
863 color[2] = ((colorindex >> 4) & 0xf) / 15.0;
864 color[3] = (colorindex & 0xf) / 15.0;
867 Vector4Copy(string_colors[colorindex], color);
868 Vector4Set(color, color[0] * r * C + B, color[1] * g * C + B, color[2] * b * C + B, color[3] * a);
871 float shadowalpha = (color[0]+color[1]+color[2]) * 0.8;
872 Vector4Set(color, 0, 0, 0, color[3] * bound(0, shadowalpha, 1));
876 float Font_DrawString_Font(float startx, float starty,
877 const char *text, size_t maxlen,
879 float basered, float basegreen, float baseblue, float basealpha,
880 int flags, int *outcolor, qboolean ignorecolorcodes,
883 int shadow, colorindex = STRING_COLOR_DEFAULT;
884 float x = startx, y, thisw;
888 float vertex3f[QUADELEMENTS_MAXQUADS*4*3];
889 float texcoord2f[QUADELEMENTS_MAXQUADS*4*2];
890 float color4f[QUADELEMENTS_MAXQUADS*4*4];
893 const char *text_start = text;
895 font_map_t *prevmap = NULL;
901 _Font_ProcessDrawFlag(flags);
903 R_Mesh_ColorPointer(color4f, 0, 0);
904 R_Mesh_ResetTextureState();
905 R_Mesh_TexCoordPointer(0, 2, texcoord2f, 0, 0);
906 R_Mesh_VertexPointer(vertex3f, 0, 0);
907 R_SetupGenericShader(true);
914 // We render onto the baseline, so move down by the intended height.
915 // Otherwise the text appears too high since the top edge would be the base line.
916 starty += (double)h * (5.0/6.0); // don't use the complete height
917 // with sane fonts it should be possible to use the font's `ascent` value
918 // but then again, is it safe?
920 for (shadow = r_textshadow.value != 0 && basealpha > 0;shadow >= 0;shadow--)
923 if (!outcolor || *outcolor == -1)
924 colorindex = STRING_COLOR_DEFAULT;
926 colorindex = *outcolor;
928 Font_GetTextColor(color, colorindex, basered, basegreen, baseblue, basealpha, shadow);
934 x += r_textshadow.value;
935 y += r_textshadow.value;
937 for (i = 0;i < maxlen && *text;i++)
939 if (*text == STRING_COLOR_TAG && !ignorecolorcodes && i + 1 < maxlen)
944 ch = *text; // the color tag is an ASCII character!
945 if (ch == STRING_COLOR_RGB_TAG_CHAR)
947 // we need some preparation here
950 if (*text) chx[0] = u8_getchar(text, &text);
951 if (*text) chx[1] = u8_getchar(text, &text);
952 if (*text) chx[2] = u8_getchar(text, &text);
953 if ( ( (chx[0] >= 'A' && chx[0] <= 'F') || (chx[0] >= 'a' && chx[0] <= 'f') || (chx[0] >= '0' && chx[0] <= '9') ) &&
954 ( (chx[1] >= 'A' && chx[1] <= 'F') || (chx[1] >= 'a' && chx[1] <= 'f') || (chx[1] >= '0' && chx[1] <= '9') ) &&
955 ( (chx[2] >= 'A' && chx[2] <= 'F') || (chx[2] >= 'a' && chx[2] <= 'f') || (chx[2] >= '0' && chx[2] <= '9') ) )
960 text = before; // start from the first hex character
962 if (ch <= '9' && ch >= '0') // ^[0-9] found
964 colorindex = ch - '0';
965 Font_GetTextColor(color, colorindex, basered, basegreen, baseblue, basealpha, shadow);
968 else if (ch == STRING_COLOR_RGB_TAG_CHAR && i + 3 < maxlen && chx[2]) // ^x found
970 // building colorindex...
971 ch = tolower(text[i+1]);
972 tempcolorindex = 0x10000; // binary: 1,0000,0000,0000,0000
973 if (ch <= '9' && ch >= '0') tempcolorindex |= (ch - '0') << 12;
974 else if (ch >= 'a' && ch <= 'f') tempcolorindex |= (ch - 87) << 12;
975 else tempcolorindex = 0;
978 ch = tolower(text[i+2]);
979 if (ch <= '9' && ch >= '0') tempcolorindex |= (ch - '0') << 8;
980 else if (ch >= 'a' && ch <= 'f') tempcolorindex |= (ch - 87) << 8;
981 else tempcolorindex = 0;
984 ch = tolower(text[i+3]);
985 if (ch <= '9' && ch >= '0') tempcolorindex |= (ch - '0') << 4;
986 else if (ch >= 'a' && ch <= 'f') tempcolorindex |= (ch - 87) << 4;
987 else tempcolorindex = 0;
990 colorindex = tempcolorindex | 0xf;
991 // ...done! now colorindex has rgba codes (1,rrrr,gggg,bbbb,aaaa)
992 //Con_Printf("^1colorindex:^7 %x\n", colorindex);
993 Font_GetTextColor(color, colorindex, basered, basegreen, baseblue, basealpha, shadow);
1000 else if (ch == STRING_COLOR_TAG)
1004 ch = u8_getchar(text, &text);
1006 map = fnt->font_map;
1007 while(map && map->start + FONT_CHARS_PER_MAP < ch)
1011 if (!Font_LoadMapForIndex(fnt, ch, &map))
1018 // this shouldn't happen
1024 if (map != prevmap && batchcount)
1026 // we need a different character map, render what we currently have:
1027 GL_LockArrays(0, batchcount * 4);
1028 R_Mesh_Draw(0, batchcount * 4, 0, batchcount * 2, NULL, quadelements, 0, 0);
1029 GL_LockArrays(0, 0);
1036 // TODO: don't call Mesh_Draw all the time
1037 // call it when the texture changes or the batchcount hits the limit
1039 R_Mesh_TexBind(0, R_GetTexture(map->texture));
1040 R_SetupGenericShader(true);
1042 mapch = ch - map->start;
1043 thisw = map->glyphs[mapch].advance_x;
1045 ac[ 0] = color[0]; ac[ 1] = color[1]; ac[ 2] = color[2]; ac[ 3] = color[3];
1046 ac[ 4] = color[0]; ac[ 5] = color[1]; ac[ 6] = color[2]; ac[ 7] = color[3];
1047 ac[ 8] = color[0]; ac[ 9] = color[1]; ac[10] = color[2]; ac[11] = color[3];
1048 ac[12] = color[0]; ac[13] = color[1]; ac[14] = color[2]; ac[15] = color[3];
1049 at[0] = map->glyphs[mapch].txmin; at[1] = map->glyphs[mapch].tymin;
1050 at[2] = map->glyphs[mapch].txmax; at[3] = map->glyphs[mapch].tymin;
1051 at[4] = map->glyphs[mapch].txmax; at[5] = map->glyphs[mapch].tymax;
1052 at[6] = map->glyphs[mapch].txmin; at[7] = map->glyphs[mapch].tymax;
1053 #define PIXEL_X(x) ( (x)/vid_conwidth.value * vid.width )
1054 #define PIXEL_Y(x) ( (x)/vid_conheight.value * vid.height )
1055 av[ 0] = x + w * PIXEL_X(map->glyphs[mapch].vxmin); av[ 1] = y + h * PIXEL_Y(map->glyphs[mapch].vymin); av[ 2] = 10;
1056 av[ 3] = x + w * PIXEL_X(map->glyphs[mapch].vxmax); av[ 4] = y + h * PIXEL_Y(map->glyphs[mapch].vymin); av[ 5] = 10;
1057 av[ 6] = x + w * PIXEL_X(map->glyphs[mapch].vxmax); av[ 7] = y + h * PIXEL_Y(map->glyphs[mapch].vymax); av[ 8] = 10;
1058 av[ 9] = x + w * PIXEL_X(map->glyphs[mapch].vxmin); av[10] = y + h * PIXEL_Y(map->glyphs[mapch].vymax); av[11] = 10;
1060 x += PIXEL_X(thisw * w);
1065 if (batchcount >= QUADELEMENTS_MAXQUADS)
1067 GL_LockArrays(0, batchcount * 4);
1068 R_Mesh_Draw(0, batchcount * 4, 0, batchcount * 2, NULL, quadelements, 0, 0);
1069 GL_LockArrays(0, 0);
1076 GL_LockArrays(0, 4);
1077 R_Mesh_Draw(0, 4, 0, 2, NULL, quadelements, 0, 0);
1078 GL_LockArrays(0, 0);
1086 GL_LockArrays(0, batchcount * 4);
1087 R_Mesh_Draw(0, batchcount * 4, 0, batchcount * 2, NULL, quadelements, 0, 0);
1088 GL_LockArrays(0, 0);
1092 *outcolor = colorindex;
1094 // note: this relies on the proper text (not shadow) being drawn last
1098 float Font_DrawString(float startx, float starty,
1099 const char *text, size_t maxlen,
1100 float width, float height,
1101 float basered, float basegreen, float baseblue, float basealpha,
1102 int flags, int *outcolor, qboolean ignorecolorcodes)
1104 return Font_DrawString_Font(startx, starty, text, maxlen,
1106 basered, basegreen, baseblue, basealpha,
1107 flags, outcolor, ignorecolorcodes,