]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Fix several long-lasting font rendering problems:
authorNaitLee <naitli@foxmail.com>
Sun, 29 Jan 2023 08:51:37 +0000 (16:51 +0800)
committerbones_was_here <bones_was_here@xonotic.au>
Thu, 4 Jan 2024 01:31:58 +0000 (11:31 +1000)
Fix resources waste of fontmap usage worst-cases;
Fix memory waste of improperly designed `struct ft2_font_map_s`;
Deprecate `r_font_diskcache`

Signed-off-by: NaitLee <naitli@foxmail.com>
draw.h
ft2.c
ft2.h
ft2_fontdefs.h
gl_draw.c

diff --git a/draw.h b/draw.h
index 562b8d1ba21fe9494678d691c76e59d07dde50ec..c703c80ce9ce08767af73f8814a6a04831c22ee3 100644 (file)
--- a/draw.h
+++ b/draw.h
@@ -97,6 +97,7 @@ typedef struct dp_font_s
 {
        cachepic_t *pic;
        float width_of[256]; // width_of[0] == max width of any char; 1.0f is base width (1/16 of texture width); therefore, all widths have to be <= 1 (does not include scale)
+       float width_of_ft2[MAX_FONT_SIZES][256];
        float maxwidth; // precalculated max width of the font (includes scale)
        char texpath[MAX_QPATH];
        char title[MAX_QPATH];
diff --git a/ft2.c b/ft2.c
index b7421b218c3f79abe5ebd11039c51510ea868ade..db3021c0e3a327c9a78a2c620d60e83f2a8525ad 100644 (file)
--- a/ft2.c
+++ b/ft2.c
@@ -27,6 +27,25 @@ static int img_fontmap[256] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
+/*
+ * Some big blocks of Unicode characters, according to
+ *     http://www.unicode.org/Public/UNIDATA/Blocks.txt
+ *
+ * Let's call these "bigblocks".
+ * These appears in a text in a "spreaded" way, ordinary maps will 
+ *     waste huge amount of resources rendering/caching unused glyphs.
+ *
+ * So, another matter is invented to counter this: incremental maps,
+ *     in which only used glyphs may present.
+ */
+static const Uchar unicode_bigblocks[] = {
+       0x3400, 0x4DBF,         //   6592  CJK Unified Ideographs Extension A
+       0x4E00, 0x9FFF,         //  20992  CJK Unified Ideographs
+       0xAC00, 0xD7AF,         //  11184  Hangul Syllables
+       0xE000, 0xF8FF,         //   6400  Private Use Area
+       0x10000, 0x10FFFF   //         Everything above
+};
+
 /*
 ================================================================================
 CVars introduced with the freetype extension
@@ -37,11 +56,13 @@ cvar_t r_font_disable_freetype = {CF_CLIENT | CF_ARCHIVE, "r_font_disable_freety
 cvar_t r_font_use_alpha_textures = {CF_CLIENT | CF_ARCHIVE, "r_font_use_alpha_textures", "0", "use alpha-textures for font rendering, this should safe memory"};
 cvar_t r_font_size_snapping = {CF_CLIENT | CF_ARCHIVE, "r_font_size_snapping", "1", "stick to good looking font sizes whenever possible - bad when the mod doesn't support it!"};
 cvar_t r_font_kerning = {CF_CLIENT | CF_ARCHIVE, "r_font_kerning", "1", "Use kerning if available"};
-cvar_t r_font_diskcache = {CF_CLIENT | CF_ARCHIVE, "r_font_diskcache", "0", "save font textures to disk for future loading rather than generating them every time"};
+cvar_t r_font_diskcache = {CF_CLIENT | CF_ARCHIVE, "r_font_diskcache", "0", "(deprecated and non-functional) save font textures to disk for future loading rather than generating them every time"};
 cvar_t r_font_compress = {CF_CLIENT | CF_ARCHIVE, "r_font_compress", "0", "use texture compression on font textures to save video memory"};
 cvar_t r_font_nonpoweroftwo = {CF_CLIENT | CF_ARCHIVE, "r_font_nonpoweroftwo", "1", "use nonpoweroftwo textures for font (saves memory, potentially slower)"};
 cvar_t developer_font = {CF_CLIENT | CF_ARCHIVE, "developer_font", "0", "prints debug messages about fonts"};
 
+cvar_t r_font_disable_incmaps = {CF_CLIENT | CF_ARCHIVE, "r_font_disable_incmaps", "0", "always to load a full glyph map for individual unmapped character, even when it will mean extreme resources waste"};
+
 #ifndef DP_FREETYPE_STATIC
 
 /*
@@ -431,6 +452,8 @@ void Font_Init(void)
        Cvar_RegisterVariable(&r_font_compress);
        Cvar_RegisterVariable(&developer_font);
 
+       Cvar_RegisterVariable(&r_font_disable_incmaps);
+
        // let's open it at startup already
        Font_OpenLibrary();
 }
@@ -886,7 +909,7 @@ static void Font_Postprocess(ft2_font_t *fnt, unsigned char *imagedata, int pitc
 }
 
 static float Font_SearchSize(ft2_font_t *font, FT_Face fontface, float size);
-static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch, ft2_font_map_t **outmap);
+static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch, ft2_font_map_t **outmap, int *outmapch, qbool incmap_ok);
 static qbool Font_LoadSize(ft2_font_t *font, float size, qbool check_only)
 {
        int map_index;
@@ -933,7 +956,7 @@ static qbool Font_LoadSize(ft2_font_t *font, float size, qbool check_only)
        temp.sfx = (1.0/64.0)/(double)size;
        temp.sfy = (1.0/64.0)/(double)size;
        temp.intSize = -1; // negative value: LoadMap must search now :)
-       if (!Font_LoadMap(font, &temp, 0, &fmap))
+       if (!Font_LoadMap(font, &temp, 0, &fmap, NULL, false))
        {
                Con_Printf(CON_ERROR "ERROR: can't load the first character map for %s\n"
                           "This is fatal\n",
@@ -951,6 +974,7 @@ static qbool Font_LoadSize(ft2_font_t *font, float size, qbool check_only)
        {
                Uchar l, r;
                FT_Vector kernvec;
+               fmap->kerning = (ft2_kerning_t *)Mem_Alloc(font_mempool, sizeof(ft2_kerning_t));
                for (l = 0; l < 256; ++l)
                {
                        for (r = 0; r < 256; ++r)
@@ -960,13 +984,13 @@ static qbool Font_LoadSize(ft2_font_t *font, float size, qbool check_only)
                                ur = qFT_Get_Char_Index((FT_Face)font->face, r);
                                if (qFT_Get_Kerning((FT_Face)font->face, ul, ur, FT_KERNING_DEFAULT, &kernvec))
                                {
-                                       fmap->kerning.kerning[l][r][0] = 0;
-                                       fmap->kerning.kerning[l][r][1] = 0;
+                                       fmap->kerning->kerning[l][r][0] = 0;
+                                       fmap->kerning->kerning[l][r][1] = 0;
                                }
                                else
                                {
-                                       fmap->kerning.kerning[l][r][0] = Font_SnapTo((kernvec.x / 64.0) / fmap->size, 1 / fmap->size);
-                                       fmap->kerning.kerning[l][r][1] = Font_SnapTo((kernvec.y / 64.0) / fmap->size, 1 / fmap->size);
+                                       fmap->kerning->kerning[l][r][0] = Font_SnapTo((kernvec.x / 64.0) / fmap->size, 1 / fmap->size);
+                                       fmap->kerning->kerning[l][r][1] = Font_SnapTo((kernvec.y / 64.0) / fmap->size, 1 / fmap->size);
                                }
                        }
                }
@@ -1074,8 +1098,8 @@ qbool Font_GetKerningForMap(ft2_font_t *font, int map_index, float w, float h, U
        {
                //Con_Printf("%g : %f, %f, %f :: %f\n", (w / (float)fmap->size), w, fmap->size, fmap->intSize, Font_VirtualToRealSize(w));
                // quick-kerning, be aware of the size: scale it
-               if (outx) *outx = fmap->kerning.kerning[left][right][0];// * (w / (float)fmap->size);
-               if (outy) *outy = fmap->kerning.kerning[left][right][1];// * (h / (float)fmap->size);
+               if (outx) *outx = fmap->kerning->kerning[left][right][0];// * (w / (float)fmap->size);
+               if (outy) *outy = fmap->kerning->kerning[left][right][1];// * (h / (float)fmap->size);
                return true;
        }
        else
@@ -1123,16 +1147,40 @@ qbool Font_GetKerningForSize(ft2_font_t *font, float w, float h, Uchar left, Uch
        return Font_GetKerningForMap(font, Font_IndexForSize(font, h, NULL, NULL), w, h, left, right, outx, outy);
 }
 
-static void UnloadMapRec(ft2_font_map_t *map)
+// this is used to gracefully unload a map chain; the passed map
+// needs not necessarily be a startmap, so maps ahead of it can be kept
+static void UnloadMapChain(ft2_font_map_t *map)
 {
-       if (map->pic)
+       int i;
+       ft2_font_map_t *nextmap;
+       // these may only be in a startmap
+       if (map->kerning != NULL)
+               Mem_Free(map->kerning);
+       if (map->incmap != NULL)
        {
-               //Draw_FreePic(map->pic); // FIXME: refcounting needed...
-               map->pic = NULL;
+               for (i = 0; i < FONT_CHARS_PER_LINE; ++i)
+                       if (map->incmap->data_tier1[i] != NULL)
+                               Mem_Free(map->incmap->data_tier1[i]);
+                       else
+                               break;
+               for (i = 0; i < FONT_CHAR_LINES; ++i)
+                       if (map->incmap->data_tier2[i] != NULL)
+                               Mem_Free(map->incmap->data_tier2[i]);
+                       else
+                               break;
+               Mem_Free(map->incmap);
+       }
+       while (map != NULL)
+       {
+               if (map->pic)
+               {
+                       //Draw_FreePic(map->pic); // FIXME: refcounting needed...
+                       map->pic = NULL;
+               }
+               nextmap = map->next;
+               Mem_Free(map);
+               map = nextmap;
        }
-       if (map->next)
-               UnloadMapRec(map->next);
-       Mem_Free(map);
 }
 
 void Font_UnloadFont(ft2_font_t *font)
@@ -1157,7 +1205,7 @@ void Font_UnloadFont(ft2_font_t *font)
        {
                if (font->font_maps[i])
                {
-                       UnloadMapRec(font->font_maps[i]);
+                       UnloadMapChain(font->font_maps[i]);
                        font->font_maps[i] = NULL;
                }
        }
@@ -1200,33 +1248,254 @@ static float Font_SearchSize(ft2_font_t *font, FT_Face fontface, float size)
        }
 }
 
-static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch, ft2_font_map_t **outmap)
+// some color transforming functions I made in attempt to fix font_diskcache
+// until the point I realize it's not worthwhile anymore
+
+static inline void rgba_to_bgra(unsigned char *source, unsigned char *target, int source_size)
+{
+       int x;
+       for (x = 0; x < source_size; x += 4)
+       {
+               target[0] = source[2];
+               target[1] = source[1];
+               target[2] = source[0];
+               target[3] = source[3];
+       }
+}
+static inline void rgba_to_alpha(unsigned char *source, unsigned char *target, int source_size)
+{
+       int x, i;
+       for (x = 0, i = 0; x < source_size; x += 4)
+       {
+               target[i++] = (
+                       source[x+0] * 0.2125 +      // r
+                       source[x+1] * 0.7154 +          // g
+                       source[x+2] * 0.0721            // b
+               ) / 3 * (source[x+3] / 256);    // a
+       }
+}
+static inline void alpha_to_rgba(unsigned char *source, unsigned char *target, int source_size)
+{
+       int x, i;
+       for (x = 0, i = 0; x < source_size; x += 4)
+       {
+               target[x+0] = target[x+1] = target[x+2] = 0xff - source[i];
+               target[x+3] = source[i++];
+       }
+}
+#define alpha_to_bgra alpha_to_rgba
+
+// helper inline functions for incmap_post_process
+
+static inline void update_pic_for_fontmap(ft2_font_map_t *fontmap, const char *identifier,
+               int width, int height, unsigned char *data)
+{
+       fontmap->pic = Draw_NewPic(identifier, width, height, data,
+               r_font_use_alpha_textures.integer ? TEXTYPE_ALPHA : TEXTYPE_RGBA,
+               TEXF_ALPHA | TEXF_CLAMP | (r_font_compress.integer > 0 ? TEXF_COMPRESS : 0));
+}
+
+// glyphs' texture coords needs to be fixed when merging to bigger texture
+static inline void transform_glyph_coords(glyph_slot_t *glyph, float shiftx, float shifty, float scalex, float scaley)
+{
+       glyph->txmin = glyph->txmin * scalex + shiftx;
+       glyph->txmax = glyph->txmax * scalex + shiftx;
+       glyph->tymin = glyph->tymin * scaley + shifty;
+       glyph->tymax = glyph->tymax * scaley + shifty;
+}
+#define fix_glyph_coords_tier1(glyph, order) transform_glyph_coords(glyph, order / (float)FONT_CHARS_PER_LINE, 0.0f, 1.0f / (float)FONT_CHARS_PER_LINE, 1.0f)
+#define fix_glyph_coords_tier2(glyph, order) transform_glyph_coords(glyph, 0.0f, order / (float)FONT_CHARS_PER_LINE, 1.0f, 1.0f / (float)FONT_CHARS_PER_LINE)
+
+// pull glyph things from sourcemap to targetmap
+static inline void merge_single_map(ft2_font_map_t *targetmap, int targetindex, ft2_font_map_t *sourcemap, int sourceindex)
+{
+       targetmap->glyphs[targetindex] = sourcemap->glyphs[sourceindex];
+       targetmap->glyphchars[targetindex] = sourcemap->glyphchars[sourceindex];
+}
+
+#define calc_data_arguments(w, h)                      \
+               width = startmap->glyphSize * w;        \
+               height = startmap->glyphSize * h;       \
+               pitch = width * bytes_per_pixel;        \
+               datasize = height * pitch;
+
+// do incremental map process
+static inline void incmap_post_process(font_incmap_t *incmap, Uchar ch,
+               unsigned char *data, ft2_font_map_t **outmap, int *outmapch)
+{
+       int index, targetmap_at;
+       // where will the next `data` be placed
+       int tier1_data_index, tier2_data_index;
+       // metrics of data to manipulate
+       int width, height, bytes_per_pixel, pitch, datasize;
+       int i, j, x, y;
+       unsigned char *newdata, *chunk;
+       ft2_font_map_t *startmap, *targetmap, *currentmap;
+       #define M FONT_CHARS_PER_LINE
+       #define N FONT_CHAR_LINES
+
+       bytes_per_pixel = r_font_use_alpha_textures.integer ? 1 : 4;
+
+       startmap = incmap->fontmap;
+       index = incmap->charcount;
+       tier1_data_index = index % M;
+       tier2_data_index = incmap->tier1_merged;
+
+       if (bytes_per_pixel != incmap->bytes_per_pixel)
+       {
+               // should it really happen...
+               int olddata_size;
+               calc_data_arguments(1, 1);
+               olddata_size = datasize / bytes_per_pixel * incmap->bytes_per_pixel;
+               for (i = 0; i < M; ++i)
+               {
+                       chunk = incmap->data_tier1[i];
+                       if (chunk == NULL) break;
+                       newdata = (unsigned char *)Mem_Alloc(font_mempool, datasize);
+                       if (bytes_per_pixel == 1)
+                               rgba_to_alpha(chunk, newdata, olddata_size);
+                       else if (bytes_per_pixel == 4)
+                               alpha_to_rgba(chunk, newdata, olddata_size);
+                       Mem_Free(chunk);
+                       incmap->data_tier1[i] = newdata;
+               }
+               chunk = NULL;
+               olddata_size *= M;
+               for (i = 0; i < N; ++i)
+               {
+                       chunk = incmap->data_tier2[i];
+                       if (chunk == NULL) break;
+                       newdata = (unsigned char *)Mem_Alloc(font_mempool, datasize);
+                       if (bytes_per_pixel == 1)
+                               rgba_to_alpha(chunk, newdata, olddata_size);
+                       else if (bytes_per_pixel == 4)
+                               alpha_to_rgba(chunk, newdata, olddata_size);
+                       Mem_Free(chunk);
+                       incmap->data_tier2[i] = newdata;
+               }
+               chunk = NULL;
+               incmap->bytes_per_pixel = bytes_per_pixel;
+       }
+       
+       incmap->data_tier1[tier1_data_index] = data;
+
+       if (index % M == M - 1)
+       {
+               // tier 1 reduction, pieces to line
+               calc_data_arguments(1, 1);
+               targetmap_at = incmap->tier2_merged + incmap->tier1_merged;
+               targetmap = startmap;
+               for (i = 0; i < targetmap_at; ++i)
+                       targetmap = targetmap->next;
+               currentmap = targetmap;
+               newdata = (unsigned char *)Mem_Alloc(font_mempool, datasize * M);
+               for (i = 0; i < M; ++i)
+               {
+                       chunk = incmap->data_tier1[i];
+                       if (chunk == NULL)
+                               continue;
+                       for (y = 0; y < datasize; y += pitch)
+                               for (x = 0; x < pitch; ++x)
+                                       newdata[y * M + i * pitch + x] = chunk[y + x];
+                       Mem_Free(chunk);
+                       incmap->data_tier1[i] = NULL;
+                       merge_single_map(targetmap, i, currentmap, 0);
+                       fix_glyph_coords_tier1(&targetmap->glyphs[i], (float)i);
+                       currentmap = currentmap->next;
+               }
+               update_pic_for_fontmap(targetmap, Draw_GetPicName(targetmap->pic), width * M, height, newdata);
+               UnloadMapChain(targetmap->next);
+               targetmap->next = NULL;
+               incmap->data_tier2[tier2_data_index] = newdata;
+               ++incmap->tier1_merged;
+               incmap->tier1_merged %= M;
+               incmap->newmap_start = INCMAP_START + targetmap_at + 1;
+               // then give this merged map
+               *outmap = targetmap;
+               *outmapch = i;
+       }
+       if (index % (M * N) == M * N - 1)
+       {
+               // tier 2 reduction, lines to full map
+               calc_data_arguments(M, 1);
+               targetmap_at = incmap->tier2_merged;
+               targetmap = startmap;
+               for (i = 0; i < targetmap_at; ++i)
+                       targetmap = targetmap->next;
+               currentmap = targetmap;
+               newdata = (unsigned char *)Mem_Alloc(font_mempool, datasize * N);
+               for (i = 0; i < N; ++i)
+               {
+                       chunk = incmap->data_tier2[i];
+                       if (chunk == NULL)
+                               continue;
+                       for (x = 0; x < datasize; ++x)
+                               newdata[i * datasize + x] = chunk[x];
+                       Mem_Free(chunk);
+                       incmap->data_tier2[i] = NULL;
+                       for (j = 0; j < M; ++j)
+                       {
+                               merge_single_map(targetmap, i * M + j, currentmap, j);
+                               fix_glyph_coords_tier2(&targetmap->glyphs[i * M + j], (float)i);
+                       }
+                       currentmap = currentmap->next;
+               }
+               update_pic_for_fontmap(targetmap, Draw_GetPicName(targetmap->pic), width, height * N, newdata);
+               UnloadMapChain(targetmap->next);
+               targetmap->next = NULL;
+               Mem_Free(newdata);
+               ++incmap->tier2_merged;
+               incmap->newmap_start = INCMAP_START + targetmap_at + 1;
+               // then give this merged map
+               *outmap = targetmap;
+               *outmapch = i * M + j;
+       }
+
+       ++incmap->charcount;
+       ++incmap->newmap_start;
+
+       #undef M
+       #undef N
+}
+
+static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
+               ft2_font_map_t **outmap, int *outmapch, qbool use_incmap)
 {
        char map_identifier[MAX_QPATH];
-       unsigned long mapidx = _ch / FONT_CHARS_PER_MAP;
+       unsigned long map_startglyph = _ch / FONT_CHARS_PER_MAP * FONT_CHARS_PER_MAP;
        unsigned char *data = NULL;
-       FT_ULong ch, mapch;
+       FT_ULong ch = 0, mapch = 0;
        int status;
        int tp;
        FT_Int32 load_flags;
        int gpad_l, gpad_r, gpad_t, gpad_b;
-       char vabuf[1024];
 
        int pitch;
-       int gR, gC; // glyph position: row and column
+       int bytes_per_pixel;
+       int width, height, datasize;
+       int glyph_row, glyph_column;
+
+       int chars_per_line = FONT_CHARS_PER_LINE;
+       int char_lines = FONT_CHAR_LINES;
+       int chars_per_map = FONT_CHARS_PER_MAP;
 
-       ft2_font_map_t *map, *next;
        ft2_font_t *usefont;
+       ft2_font_map_t *map, *next;
+       font_incmap_t *incmap;
 
        FT_Face fontface;
 
-       int bytesPerPixel = 4; // change the conversion loop too if you change this!
-
-       if (outmap)
-               *outmap = NULL;
+       bytes_per_pixel = r_font_use_alpha_textures.integer ? 1 : 4;
 
-       if (r_font_use_alpha_textures.integer)
-               bytesPerPixel = 1;
+       incmap = mapstart->incmap;
+       if (use_incmap)
+       {
+               // only render one character in this map;
+               // such small maps will be merged together later in `incmap_post_process`
+               chars_per_line = char_lines = chars_per_map = 1;
+               // and the index is incremental
+               map_startglyph = incmap ? incmap->newmap_start : INCMAP_START;
+       }
 
        if (font->image_font)
                fontface = (FT_Face)font->next->face;
@@ -1311,13 +1580,16 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
        map = (ft2_font_map_t *)Mem_Alloc(font_mempool, sizeof(ft2_font_map_t));
        if (!map)
        {
-               Con_Printf(CON_ERROR "ERROR: Out of memory when loading fontmap for %s\n", font->name);
+               Con_Printf(CON_ERROR "ERROR: Out of memory when allowcating fontmap for %s\n", font->name);
                return false;
        }
 
-       // create a totally unique name for this map, then we will use it to make a unique cachepic_t to avoid redundant textures
+       map->start = map_startglyph;
+
+       // create a unique name for this map, then we will use it to make a unique cachepic_t to avoid redundant textures
+       /*
        dpsnprintf(map_identifier, sizeof(map_identifier),
-               "%s_cache_%g_%d_%g_%g_%g_%g_%g_%u",
+               "%s_cache_%g_%d_%g_%g_%g_%g_%g_%u_%lx",
                font->name,
                (double) mapstart->intSize,
                (int) load_flags,
@@ -1326,13 +1598,18 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                (double) font->settings->shadowx,
                (double) font->settings->shadowy,
                (double) font->settings->shadowz,
-               (unsigned) mapidx);
+               (unsigned) map_startglyph,
+               // add pointer as a unique part to avoid earlier incmaps' state being trashed
+               use_incmap ? (unsigned long)mapstart : 0x0);
+       */
+       dpsnprintf(map_identifier, sizeof(map_identifier), "%s_%g_%lx_%u",
+                       font->name, mapstart->intSize, (unsigned long) mapstart, (unsigned) map_startglyph);
 
        // create a cachepic_t from the data now, or reuse an existing one
        if (developer_font.integer)
                Con_Printf("Generating font map %s (size: %.1f MB)\n", map_identifier, mapstart->glyphSize * (256 * 4 / 1048576.0) * mapstart->glyphSize);
 
-       Font_Postprocess(font, NULL, 0, bytesPerPixel, mapstart->size*2, mapstart->size*2, &gpad_l, &gpad_r, &gpad_t, &gpad_b);
+       Font_Postprocess(font, NULL, 0, bytes_per_pixel, mapstart->size*2, mapstart->size*2, &gpad_l, &gpad_r, &gpad_t, &gpad_b);
 
        // copy over the information
        map->size = mapstart->size;
@@ -1341,19 +1618,58 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
        map->sfx = mapstart->sfx;
        map->sfy = mapstart->sfy;
 
-       pitch = map->glyphSize * FONT_CHARS_PER_LINE * bytesPerPixel;
-       data = (unsigned char *)Mem_Alloc(font_mempool, (FONT_CHAR_LINES * map->glyphSize) * pitch);
+       width = map->glyphSize * chars_per_line;
+       height = map->glyphSize * char_lines;
+       pitch = width * bytes_per_pixel;
+       datasize = height * pitch;
+       data = (unsigned char *)Mem_Alloc(font_mempool, datasize);
        if (!data)
        {
                Con_Printf(CON_ERROR "ERROR: Failed to allocate memory for font %s size %g\n", font->name, map->size);
                Mem_Free(map);
                return false;
        }
+
+       if (use_incmap)
+       {
+               if (mapstart->incmap == NULL)
+               {
+                       // initial incmap
+                       incmap = mapstart->incmap = (font_incmap_t *)Mem_Alloc(font_mempool, sizeof(font_incmap_t));
+                       if (!incmap)
+                       {
+                               Con_Printf(CON_ERROR "ERROR: Out of memory when allowcating incremental fontmap for %s\n", font->name);
+                               return false;
+                       }
+                       // this will be the startmap of incmap
+                       incmap->fontmap = map;
+                       incmap->newmap_start = INCMAP_START;
+                       incmap->bytes_per_pixel = bytes_per_pixel;
+               }
+               else
+               {
+                       // new maps for incmap shall always be the last one
+                       next = incmap->fontmap;
+                       while (next->next != NULL)
+                               next = next->next;
+                       next->next = map;
+               }
+       }
+       else
+       {
+               // insert this normal map
+               next = use_incmap ? incmap->fontmap : mapstart;
+               while(next->next && next->next->start < map->start)
+                       next = next->next;
+               map->next = next->next;
+               next->next = map;
+       }
+
        // initialize as white texture with zero alpha
        tp = 0;
-       while (tp < (FONT_CHAR_LINES * map->glyphSize) * pitch)
+       while (tp < datasize)
        {
-               if (bytesPerPixel == 4)
+               if (bytes_per_pixel == 4)
                {
                        data[tp++] = 0xFF;
                        data[tp++] = 0xFF;
@@ -1362,21 +1678,11 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                data[tp++] = 0x00;
        }
 
-       memset(map->width_of, 0, sizeof(map->width_of));
-
-       // insert the map
-       map->start = mapidx * FONT_CHARS_PER_MAP;
-       next = mapstart;
-       while(next->next && next->next->start < map->start)
-               next = next->next;
-       map->next = next->next;
-       next->next = map;
-
-       gR = 0;
-       gC = -1;
-       for (ch = map->start;
-            ch < (FT_ULong)map->start + FONT_CHARS_PER_MAP;
-            ++ch)
+       glyph_row = 0;
+       glyph_column = 0;
+       ch = (FT_ULong)(use_incmap ? _ch : map->start);
+       mapch = 0;
+       while (true)
        {
                FT_ULong glyphIndex;
                int w, h, x, y;
@@ -1387,22 +1693,15 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                FT_Face face;
                int pad_l, pad_r, pad_t, pad_b;
 
-               mapch = ch - map->start;
-
                if (developer_font.integer)
                        Con_DPrint("glyphinfo: ------------- GLYPH INFO -----------------\n");
 
-               ++gC;
-               if (gC >= FONT_CHARS_PER_LINE)
-               {
-                       gC -= FONT_CHARS_PER_LINE;
-                       ++gR;
-               }
+               map->glyphchars[mapch] = (Uchar)ch;
 
                if (data)
                {
-                       imagedata = data + gR * pitch * map->glyphSize + gC * map->glyphSize * bytesPerPixel;
-                       imagedata += gpad_t * pitch + gpad_l * bytesPerPixel;
+                       imagedata = data + glyph_row * pitch * map->glyphSize + glyph_column * map->glyphSize * bytes_per_pixel;
+                       imagedata += gpad_t * pitch + gpad_l * bytes_per_pixel;
                }
                //status = qFT_Load_Char(face, ch, FT_LOAD_RENDER);
                // we need the glyphIndex
@@ -1503,44 +1802,44 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                                switch (bmp->pixel_mode)
                                {
                                case FT_PIXEL_MODE_MONO:
-                                       dst += bytesPerPixel - 1; // shift to alpha byte
+                                       dst += bytes_per_pixel - 1; // shift to alpha byte
                                        for (x = 0; x < bmp->width; x += 8)
                                        {
                                                unsigned char c = *src++;
-                                               *dst = 255 * !!((c & 0x80) >> 7); dst += bytesPerPixel;
-                                               *dst = 255 * !!((c & 0x40) >> 6); dst += bytesPerPixel;
-                                               *dst = 255 * !!((c & 0x20) >> 5); dst += bytesPerPixel;
-                                               *dst = 255 * !!((c & 0x10) >> 4); dst += bytesPerPixel;
-                                               *dst = 255 * !!((c & 0x08) >> 3); dst += bytesPerPixel;
-                                               *dst = 255 * !!((c & 0x04) >> 2); dst += bytesPerPixel;
-                                               *dst = 255 * !!((c & 0x02) >> 1); dst += bytesPerPixel;
-                                               *dst = 255 * !!((c & 0x01) >> 0); dst += bytesPerPixel;
+                                               *dst = 255 * !!((c & 0x80) >> 7); dst += bytes_per_pixel;
+                                               *dst = 255 * !!((c & 0x40) >> 6); dst += bytes_per_pixel;
+                                               *dst = 255 * !!((c & 0x20) >> 5); dst += bytes_per_pixel;
+                                               *dst = 255 * !!((c & 0x10) >> 4); dst += bytes_per_pixel;
+                                               *dst = 255 * !!((c & 0x08) >> 3); dst += bytes_per_pixel;
+                                               *dst = 255 * !!((c & 0x04) >> 2); dst += bytes_per_pixel;
+                                               *dst = 255 * !!((c & 0x02) >> 1); dst += bytes_per_pixel;
+                                               *dst = 255 * !!((c & 0x01) >> 0); dst += bytes_per_pixel;
                                        }
                                        break;
                                case FT_PIXEL_MODE_GRAY2:
-                                       dst += bytesPerPixel - 1; // shift to alpha byte
+                                       dst += bytes_per_pixel - 1; // shift to alpha byte
                                        for (x = 0; x < bmp->width; x += 4)
                                        {
                                                unsigned char c = *src++;
-                                               *dst = ( ((c & 0xA0) >> 6) * 0x55 ); c <<= 2; dst += bytesPerPixel;
-                                               *dst = ( ((c & 0xA0) >> 6) * 0x55 ); c <<= 2; dst += bytesPerPixel;
-                                               *dst = ( ((c & 0xA0) >> 6) * 0x55 ); c <<= 2; dst += bytesPerPixel;
-                                               *dst = ( ((c & 0xA0) >> 6) * 0x55 ); c <<= 2; dst += bytesPerPixel;
+                                               *dst = ( ((c & 0xA0) >> 6) * 0x55 ); c <<= 2; dst += bytes_per_pixel;
+                                               *dst = ( ((c & 0xA0) >> 6) * 0x55 ); c <<= 2; dst += bytes_per_pixel;
+                                               *dst = ( ((c & 0xA0) >> 6) * 0x55 ); c <<= 2; dst += bytes_per_pixel;
+                                               *dst = ( ((c & 0xA0) >> 6) * 0x55 ); c <<= 2; dst += bytes_per_pixel;
                                        }
                                        break;
                                case FT_PIXEL_MODE_GRAY4:
-                                       dst += bytesPerPixel - 1; // shift to alpha byte
+                                       dst += bytes_per_pixel - 1; // shift to alpha byte
                                        for (x = 0; x < bmp->width; x += 2)
                                        {
                                                unsigned char c = *src++;
-                                               *dst = ( ((c & 0xF0) >> 4) * 0x11); dst += bytesPerPixel;
-                                               *dst = ( ((c & 0x0F) ) * 0x11); dst += bytesPerPixel;
+                                               *dst = ( ((c & 0xF0) >> 4) * 0x11); dst += bytes_per_pixel;
+                                               *dst = ( ((c & 0x0F) ) * 0x11); dst += bytes_per_pixel;
                                        }
                                        break;
                                case FT_PIXEL_MODE_GRAY:
                                        // in this case pitch should equal width
                                        for (tp = 0; tp < bmp->pitch; ++tp)
-                                               dst[(bytesPerPixel - 1) + tp*bytesPerPixel] = src[tp]; // copy the grey value into the alpha bytes
+                                               dst[(bytes_per_pixel - 1) + tp*bytes_per_pixel] = src[tp]; // copy the grey value into the alpha bytes
 
                                        //memcpy((void*)dst, (void*)src, bmp->pitch);
                                        //dst += bmp->pitch;
@@ -1554,7 +1853,7 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                        pad_r = gpad_r;
                        pad_t = gpad_t;
                        pad_b = gpad_b;
-                       Font_Postprocess(font, imagedata, pitch, bytesPerPixel, w, h, &pad_l, &pad_r, &pad_t, &pad_b);
+                       Font_Postprocess(font, imagedata, pitch, bytes_per_pixel, w, h, &pad_l, &pad_r, &pad_t, &pad_b);
                }
                else
                {
@@ -1562,7 +1861,7 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                        pad_r = gpad_r;
                        pad_t = gpad_t;
                        pad_b = gpad_b;
-                       Font_Postprocess(font, NULL, pitch, bytesPerPixel, w, h, &pad_l, &pad_r, &pad_t, &pad_b);
+                       Font_Postprocess(font, NULL, pitch, bytes_per_pixel, w, h, &pad_l, &pad_r, &pad_t, &pad_b);
                }
 
 
@@ -1579,10 +1878,10 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                        //double mWidth = (glyph->metrics.width >> 6) / map->size;
                        //double mHeight = (glyph->metrics.height >> 6) / map->size;
 
-                       mapglyph->txmin = ( (double)(gC * map->glyphSize) + (double)(gpad_l - pad_l) ) / ( (double)(map->glyphSize * FONT_CHARS_PER_LINE) );
-                       mapglyph->txmax = mapglyph->txmin + (double)(bmp->width + pad_l + pad_r) / ( (double)(map->glyphSize * FONT_CHARS_PER_LINE) );
-                       mapglyph->tymin = ( (double)(gR * map->glyphSize) + (double)(gpad_r - pad_r) ) / ( (double)(map->glyphSize * FONT_CHAR_LINES) );
-                       mapglyph->tymax = mapglyph->tymin + (double)(bmp->rows + pad_t + pad_b) / ( (double)(map->glyphSize * FONT_CHAR_LINES) );
+                       mapglyph->txmin = ( (double)(glyph_column * map->glyphSize) + (double)(gpad_l - pad_l) ) / ( (double)(map->glyphSize * chars_per_line) );
+                       mapglyph->txmax = mapglyph->txmin + (double)(bmp->width + pad_l + pad_r) / ( (double)(map->glyphSize * chars_per_line) );
+                       mapglyph->tymin = ( (double)(glyph_row * map->glyphSize) + (double)(gpad_r - pad_r) ) / ( (double)(map->glyphSize * char_lines) );
+                       mapglyph->tymax = mapglyph->tymin + (double)(bmp->rows + pad_t + pad_b) / ( (double)(map->glyphSize * char_lines) );
                        //mapglyph->vxmin = bearingX;
                        //mapglyph->vxmax = bearingX + mWidth;
                        mapglyph->vxmin = (glyph->bitmap_left - pad_l) / map->size;
@@ -1599,7 +1898,7 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
 
                        if (developer_font.integer)
                        {
-                               Con_DPrintf("glyphinfo:   Glyph: %lu   at (%i, %i)\n", (unsigned long)ch, gC, gR);
+                               Con_DPrintf("glyphinfo:   Glyph: %lu   at (%i, %i)\n", (unsigned long)ch, glyph_column, glyph_row);
                                Con_DPrintf("glyphinfo:   %f, %f, %lu\n", bearingX, map->sfx, (unsigned long)glyph->metrics.horiBearingX);
                                if (ch >= 32 && ch <= 128)
                                        Con_DPrintf("glyphinfo:   Character: %c\n", (int)ch);
@@ -1613,36 +1912,40 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                        }
                }
                map->glyphs[mapch].image = false;
+
+               ++mapch; ++ch;
+               if ((int)mapch == chars_per_map)
+                       break;
+               if (++glyph_column % chars_per_line == 0)
+               {
+                       glyph_column = 0;
+                       ++glyph_row;
+               }
        }
 
-       {
-               int w = map->glyphSize * FONT_CHARS_PER_LINE;
-               int h = map->glyphSize * FONT_CHAR_LINES;
-               // update the pic returned by Draw_CachePic_Flags earlier to contain our texture
-               map->pic = Draw_NewPic(map_identifier, w, h, data, r_font_use_alpha_textures.integer ? TEXTYPE_ALPHA : TEXTYPE_RGBA, TEXF_ALPHA | TEXF_CLAMP | (r_font_compress.integer > 0 ? TEXF_COMPRESS : 0));
+       // update the pic returned by Draw_CachePic_Flags earlier to contain our texture
+       update_pic_for_fontmap(map, map_identifier, width, height, data);
 
-               if (r_font_diskcache.integer >= 1)
-               {
+       // diskcache deprecated for being complicated to fix and not worthwhile anymore
+#if 0
+       if (r_font_diskcache.integer >= 1 && bytes_per_pixel == 4 && !use_incmap)
+       {
                        // swap to BGRA for tga writing...
-                       int s = w * h;
                        int x;
                        int b;
-                       for (x = 0;x < s;x++)
+                       for (x = 0;x < datasize;x++)
                        {
                                b = data[x*4+0];
                                data[x*4+0] = data[x*4+2];
                                data[x*4+2] = b;
                        }
-                       Image_WriteTGABGRA(va(vabuf, sizeof(vabuf), "%s.tga", map_identifier), w, h, data);
+                       Image_WriteTGABGRA(va(vabuf, sizeof(vabuf), "%s.tga", map_identifier), width, height, data);
 #ifndef USE_GLES2
-                       if (r_font_compress.integer && Draw_IsPicLoaded(map->pic))
-                               R_SaveTextureDDSFile(Draw_GetPicTexture(map->pic), va(vabuf, sizeof(vabuf), "dds/%s.dds", map_identifier), r_texture_dds_save.integer < 2, true);
+               if (r_font_compress.integer && Draw_IsPicLoaded(map->pic))
+                       R_SaveTextureDDSFile(Draw_GetPicTexture(map->pic), va(vabuf, sizeof(vabuf), "dds/%s.dds", map_identifier), r_texture_dds_save.integer < 2, true);
 #endif
-               }
        }
-
-       if(data)
-               Mem_Free(data);
+#endif
 
        if (!Draw_IsPicLoaded(map->pic))
        {
@@ -1651,29 +1954,150 @@ static qbool Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch,
                // this would be bad...
                // only `data' must be freed
                Con_Printf(CON_ERROR "ERROR: Failed to generate texture for font %s size %f map %lu\n",
-                          font->name, mapstart->size, mapidx);
+                          font->name, mapstart->size, map_startglyph);
                return false;
        }
-       if (outmap)
+
+       if (use_incmap)
+       {
+               *outmap = map;
+               *outmapch = 0;
+               // data will be kept in incmap for being merged later, freed afterward
+               incmap_post_process(incmap, _ch, data, outmap, outmapch);
+       }
+       else if (data)
+       {
+               Mem_Free(data);
                *outmap = map;
+               if (outmapch != NULL)
+                       *outmapch = _ch - map->start;
+       }
+
        return true;
 }
 
-qbool Font_LoadMapForIndex(ft2_font_t *font, int map_index, Uchar _ch, ft2_font_map_t **outmap)
+static qbool legacy_font_loading_api_alerted = false;
+static inline void alert_legacy_font_api(const char *name)
+{
+       if (!legacy_font_loading_api_alerted)
+       {
+               Con_DPrintf(CON_WARN "Warning: You are using an legacy API '%s', which have certain limitations; please use 'Font_GetMapForChar' instead\n", name);
+               legacy_font_loading_api_alerted = true;
+       }
+}
+
+// legacy font API, please use `Font_GetMapForChar` instead
+qbool Font_LoadMapForIndex(ft2_font_t *font, int map_index, Uchar ch, ft2_font_map_t **outmap)
 {
        if (map_index < 0 || map_index >= MAX_FONT_SIZES)
                return false;
        // the first map must have been loaded already
        if (!font->font_maps[map_index])
                return false;
-       return Font_LoadMap(font, font->font_maps[map_index], _ch, outmap);
+       alert_legacy_font_api("Font_LoadMapForIndex");
+       return Font_LoadMap(font, font->font_maps[map_index], ch, outmap, NULL, false);
 }
 
+// legacy font API, please use `Font_GetMapForChar` instead
 ft2_font_map_t *FontMap_FindForChar(ft2_font_map_t *start, Uchar ch)
 {
-       while (start && start->start + FONT_CHARS_PER_MAP <= ch)
-               start = start->next;
-       if (start && start->start > ch)
+       ft2_font_map_t *map = start;
+       while (map && map->start + FONT_CHARS_PER_MAP <= ch)
+               map = map->next;
+       if (map && map->start > ch)
                return NULL;
-       return start;
+       alert_legacy_font_api("FontMap_FindForChar");
+       return map;
+}
+
+static inline qbool should_use_incmap(Uchar ch)
+{
+       int i;
+       // optimize: a simple check logic for usual conditions
+       if (ch < unicode_bigblocks[0])
+               return false;
+       if (r_font_disable_incmaps.integer == 1)
+               return false;
+       for (i = 0; i < (int)(sizeof(unicode_bigblocks) / sizeof(Uchar)); i += 2)
+               if (unicode_bigblocks[i] <= ch && ch <= unicode_bigblocks[i + 1])
+                       return true;
+       return false;
+}
+
+static inline qbool get_char_from_incmap(ft2_font_map_t *map, Uchar ch, ft2_font_map_t **outmap, int *outmapch)
+{
+       int i;
+       font_incmap_t *incmap;
+
+       incmap = map->incmap;
+       *outmapch = 0;
+
+       if (incmap != NULL)
+       {
+               map = incmap->fontmap;
+               while (map != NULL)
+               {
+                       for (i = 0; i < FONT_CHARS_PER_MAP; ++i)
+                       {
+                               if (map->glyphchars[i] == ch)
+                               {
+                                       *outmap = map;
+                                       *outmapch = i;
+                                       return true;
+                               }
+                               else if (map->glyphchars[i] == 0)
+                                       // this tier0/tier1 map ends here
+                                       break;
+                       }
+                       map = map->next;
+               }
+       }
+       return false;
+}
+
+/**
+ * Query for or load a font map for a character, with the character's place on it.
+ * Supports the incremental map mechanism; returning if the operation is done successfully
+ */
+qbool Font_GetMapForChar(ft2_font_t *font, int map_index, Uchar ch, ft2_font_map_t **outmap, int *outmapch)
+{
+       qbool use_incmap;
+       ft2_font_map_t *map;
+
+       // startmap
+       map = Font_MapForIndex(font, map_index);
+
+       // optimize: the first map must have been loaded already
+       if (ch < FONT_CHARS_PER_MAP)
+       {
+               *outmapch = ch;
+               *outmap = map;
+               return true;
+       }
+
+       // search for the character
+
+       use_incmap = should_use_incmap(ch);
+
+       if (!use_incmap)
+       {
+               // normal way
+               *outmapch = ch % FONT_CHARS_PER_MAP;
+               while (map && map->start + FONT_CHARS_PER_MAP <= ch)
+                       map = map->next;
+               if (map && map->start <= ch)
+               {
+                       *outmap = map;
+                       return true;
+               }
+       }
+       else if (get_char_from_incmap(map, ch, outmap, outmapch))
+               // got it
+               return true;
+
+       // so no appropriate map was found, load one
+
+       if (map_index < 0 || map_index >= MAX_FONT_SIZES)
+               return false;
+       return Font_LoadMap(font, font->font_maps[map_index], ch, outmap, outmapch, use_incmap);
 }
diff --git a/ft2.h b/ft2.h
index 2104a8c4fd9f6620afeb7fb231d77fad5a075663..6495d3eb57b8f603c2c02995a950592272ac92d4 100644 (file)
--- a/ft2.h
+++ b/ft2.h
@@ -21,6 +21,8 @@
  */
 
 typedef struct ft2_font_map_s ft2_font_map_t;
+typedef struct font_incmap_s font_incmap_t;
+typedef struct incmap_lookup_cache_s incmap_lookup_cache_t;
 typedef struct ft2_attachment_s ft2_attachment_t;
 #ifdef WIN64
 #define ft2_oldstyle_map ((ft2_font_map_t*)-1LL)
@@ -36,38 +38,38 @@ typedef struct ft2_kerning_s
 
 typedef struct ft2_font_s
 {
-       char            name[64];
-       qbool        has_kerning;
+       char   name[64];
+       qbool  has_kerning;
        // last requested size loaded using Font_SetSize
-       float           currentw;
-       float           currenth;
-       float           ascend;
-       float           descend;
-       qbool        image_font; // only fallbacks are freetype fonts
+       float  currentw;
+       float  currenth;
+       float  ascend;
+       float  descend;
+       qbool  image_font; // only fallbacks are freetype fonts
 
        // TODO: clean this up and do not expose everything.
-       
+
        const unsigned char  *data; // FT2 needs it to stay
        //fs_offset_t     datasize;
-       void           *face;
+       void                 *face;
 
        // an unordered array of ordered linked lists of glyph maps for a specific size
-       ft2_font_map_t *font_maps[MAX_FONT_SIZES];
-       int             num_sizes;
+       ft2_font_map_t       *font_maps[MAX_FONT_SIZES];
+       int                   num_sizes;
 
        // attachments
-       size_t            attachmentcount;
-       ft2_attachment_t *attachments;
+       size_t                attachmentcount;
+       ft2_attachment_t     *attachments;
 
-       ft2_settings_t *settings;
+       ft2_settings_t       *settings;
 
        // fallback mechanism
-       struct ft2_font_s *next;
+       struct ft2_font_s    *next;
 } ft2_font_t;
 
 void            Font_CloseLibrary(void);
 void            Font_Init(void);
-qbool        Font_OpenLibrary(void);
+qbool           Font_OpenLibrary(void);
 ft2_font_t*     Font_Alloc(void);
 void            Font_UnloadFont(ft2_font_t *font);
 // IndexForSize suggests to change the width and height if a font size is in a reasonable range
@@ -75,11 +77,12 @@ void            Font_UnloadFont(ft2_font_t *font);
 // in such a case, *outw and *outh are set to 12, which is often a good alternative size
 int             Font_IndexForSize(ft2_font_t *font, float size, float *outw, float *outh);
 ft2_font_map_t *Font_MapForIndex(ft2_font_t *font, int index);
-qbool        Font_LoadFont(const char *name, dp_font_t *dpfnt);
-qbool        Font_GetKerningForSize(ft2_font_t *font, float w, float h, Uchar left, Uchar right, float *outx, float *outy);
-qbool        Font_GetKerningForMap(ft2_font_t *font, int map_index, float w, float h, Uchar left, Uchar right, float *outx, float *outy);
+qbool           Font_LoadFont(const char *name, dp_font_t *dpfnt);
+qbool           Font_GetKerningForSize(ft2_font_t *font, float w, float h, Uchar left, Uchar right, float *outx, float *outy);
+qbool           Font_GetKerningForMap(ft2_font_t *font, int map_index, float w, float h, Uchar left, Uchar right, float *outx, float *outy);
 float           Font_VirtualToRealSize(float sz);
 float           Font_SnapTo(float val, float snapwidth);
 // since this is used on a font_map_t, let's name it FontMap_*
 ft2_font_map_t *FontMap_FindForChar(ft2_font_map_t *start, Uchar ch);
+qbool           Font_GetMapForChar(ft2_font_t *font, int map_index, Uchar ch, ft2_font_map_t **outmap, int *outmapch);
 #endif // DP_FREETYPE2_H__
index 8cdc9e1d0ceb3ac6240e1a8ef9e16e51fa0a0e36..654ab898c3695d639535e651697952a41dcef110 100644 (file)
@@ -7,6 +7,9 @@
 #define FONT_CHAR_LINES 16
 #define FONT_CHARS_PER_MAP (FONT_CHARS_PER_LINE * FONT_CHAR_LINES)
 
+// map.start value for incremental maps to hold a place
+#define INCMAP_START 0x110000
+
 typedef struct glyph_slot_s
 {
        qbool image;
@@ -26,32 +29,55 @@ typedef struct glyph_slot_s
 
 struct ft2_font_map_s
 {
-       Uchar                  start;
-       struct ft2_font_map_s *next;
-       float                  size;
+       Uchar  start;
+       float  size;
+
        // the actual size used in the freetype code
        // by convention, the requested size is the height of the font's bounding box.
-       float                  intSize;
-       int                    glyphSize;
+       float  intSize;
+       int    glyphSize;
+
+       ft2_font_map_t *next;
+       cachepic_t     *pic;
+       qbool           static_tex;
+       glyph_slot_t    glyphs[FONT_CHARS_PER_MAP];
+       Uchar           glyphchars[FONT_CHARS_PER_MAP];
 
-       cachepic_t            *pic;
-       qbool               static_tex;
-       glyph_slot_t           glyphs[FONT_CHARS_PER_MAP];
+       // safes us the trouble of calculating these over and over again
+       double          sfx, sfy;
+
+       // note: float width_of[256] was moved to `struct dp_font_s` as width_of_ft2
 
+       // these may only present in a startmap
        // contains the kerning information for the first 256 characters
        // for the other characters, we will lookup the kerning information
-       ft2_kerning_t          kerning;
-       // safes us the trouble of calculating these over and over again
-       double                 sfx, sfy;
+       ft2_kerning_t  *kerning;
+       // for accessing incremental maps for bigblock glyphs
+       font_incmap_t  *incmap;
+};
+
+struct font_incmap_s
+{
+       // associated fontmap; startmap of incmaps
+       struct ft2_font_map_s *fontmap;
+       int charcount;
+       int newmap_start;
+
+       // two rounds of merge will take place, keep those data until then
+       unsigned char *data_tier1[FONT_CHARS_PER_LINE];
+       unsigned char *data_tier2[FONT_CHAR_LINES];
+
+       // count of merged maps
+       int tier1_merged, tier2_merged;
 
-       // the width_of for the image-font, pixel-snapped for this size
-       float           width_of[256];
+       // in case it changed middleway, convert previous data
+       int bytes_per_pixel;
 };
 
 struct ft2_attachment_s
 {
        const unsigned char *data;
-       fs_offset_t    size;
+       fs_offset_t          size;
 };
 
 //qbool Font_LoadMapForIndex(ft2_font_t *font, Uchar _ch, ft2_font_map_t **outmap);
index fd9adfa152b65fd80f59b0cc35c30325009fa23d..2706bc022a0e230067dfbf43585465d25bdb71fb 100644 (file)
--- a/gl_draw.c
+++ b/gl_draw.c
@@ -354,6 +354,9 @@ void LoadFont(qbool override, const char *name, dp_font_t *fnt, float scale, flo
        if(drawtexturepool == NULL)
                return; // before gl_draw_start, so will be loaded later
 
+       /*
+       // this "reset" seems interrupts fontmap loading and wastes previous works
+       // no side-effects so far without this
        if(fnt->ft2)
        {
                // clear freetype font
@@ -361,6 +364,7 @@ void LoadFont(qbool override, const char *name, dp_font_t *fnt, float scale, flo
                Mem_Free(fnt->ft2);
                fnt->ft2 = NULL;
        }
+       */
 
        if(fnt->req_face != -1)
        {
@@ -458,7 +462,7 @@ void LoadFont(qbool override, const char *name, dp_font_t *fnt, float scale, flo
                        if (!map)
                                break;
                        for(ch = 0; ch < 256; ++ch)
-                               map->width_of[ch] = Font_SnapTo(fnt->width_of[ch], 1/map->size);
+                               fnt->width_of_ft2[i][ch] = Font_SnapTo(fnt->width_of[ch], 1/map->size);
                }
        }
 
@@ -975,7 +979,7 @@ float DrawQ_TextWidth_UntilWidth_TrackColors_Scale(const char *text, size_t *max
        //      x = snap_to_pixel_x(x, 0.4); // haha, it's 0 anyway
 
        if (fontmap)
-               width_of = fontmap->width_of;
+               width_of = fnt->width_of_ft2[map_index];
        else
                width_of = fnt->width_of;
 
@@ -1046,18 +1050,12 @@ float DrawQ_TextWidth_UntilWidth_TrackColors_Scale(const char *text, size_t *max
                        }
                        x += width_of[ch] * dw;
                } else {
-                       if (!map || map == ft2_oldstyle_map || ch < map->start || ch >= map->start + FONT_CHARS_PER_MAP)
+                       if (!map || map == ft2_oldstyle_map || ch != prevch)
                        {
-                               map = FontMap_FindForChar(fontmap, ch);
+                               Font_GetMapForChar(ft2, map_index, ch, &map, &mapch);
                                if (!map)
-                               {
-                                       if (!Font_LoadMapForIndex(ft2, map_index, ch, &map))
-                                               break;
-                                       if (!map)
-                                               break;
-                               }
+                                       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;
@@ -1146,7 +1144,7 @@ float DrawQ_String_Scale(float startx, float starty, const char *text, size_t ma
        pix_y = vid.height / vid_conheight.value;
 
        if (fontmap)
-               width_of = fontmap->width_of;
+               width_of = fnt->width_of_ft2[map_index];
        else
                width_of = fnt->width_of;
 
@@ -1259,27 +1257,16 @@ float DrawQ_String_Scale(float startx, float starty, const char *text, size_t ma
                                Mod_Mesh_AddTriangle(mod, surf, e0, e2, e3);
                                x += width_of[ch] * dw;
                        } else {
-                               if (!map || map == ft2_oldstyle_map || ch < map->start || ch >= map->start + FONT_CHARS_PER_MAP)
+                               if (!map || map == ft2_oldstyle_map || ch != prevch)
                                {
-                                       // find the new map
-                                       map = FontMap_FindForChar(fontmap, ch);
+                                       Font_GetMapForChar(ft2, map_index, ch, &map, &mapch);
                                        if (!map)
                                        {
-                                               if (!Font_LoadMapForIndex(ft2, map_index, ch, &map))
-                                               {
-                                                       shadow = -1;
-                                                       break;
-                                               }
-                                               if (!map)
-                                               {
-                                                       // this shouldn't happen
-                                                       shadow = -1;
-                                                       break;
-                                               }
+                                               shadow = -1;
+                                               break;
                                        }
                                }
 
-                               mapch = ch - map->start;
                                thisw = map->glyphs[mapch].advance_x;
 
                                //x += ftbase_x;