new font bitmap baking API
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent 37c95d8 · Browse files · 1 file·+186 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+186 added−1 removed
- other:+186 added−1 removed
Provenance
- Commit
0a3dd5a- Tree
01c8253- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
37c95d8
1 file changed+186 added−1 removed
modified:stb_truetype.h+186 added-1 removed
461 unmodified lines462 //463 // It's inefficient; you might want to c&p it and optimize it.464 465 466+467 //////////////////////////////////////////////////////////////////////////////468 //469+// NEW TEXTURE BAKING API470+//471+// This provides options for packing multiple fonts into one atlas, not472+// perfectly but better than nothing.473+474+typedef struct stbtt_pack_context stbtt_pack_context;475+typedef struct476+{477+ float font_size; // if positive, pixel height; if negative, points478+ int first_unicode_char_in_range;479+ int num_chars_in_range;480+ stbtt_bakedchar *chardata_for_range; // output481+} stbtt_pack_range;482+483+extern int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, void *alloc_context);484+// returns 0 if the allocations fail485+486+extern void stbtt_PackEnd (stbtt_pack_context *spc);487+extern int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,488+ int first_unicode_char_in_range, int num_chars_in_range, stbtt_bakedchar *chardata_for_range);489+extern int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);490+491+492+// this is an opaque structure that you shouldn't mess with which holds493+// all the context needed from PackBegin to PackEnd.494+struct stbtt_pack_context {495+ void *user_allocator_context;496+ void *pack_info;497+ int width;498+ int height;499+ int stride_in_bytes;500+ unsigned char *pixels;501+ void *nodes;502+};503+504+//////////////////////////////////////////////////////////////////////////////505+//506 // FONT LOADING507 //508 //509 1407 unmodified lines1917 {1918 float scale;1919 int x,y,bottom_y, i;1920 stbtt_fontinfo f;1883- stbtt_InitFont(&f, data, offset);1921+ if (!stbtt_InitFont(&f, data, offset))1922+ return -1;1923 STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels1924 x=y=1;1925 bottom_y = 1;1926 49 unmodified lines1976 }1977 1978 //////////////////////////////////////////////////////////////////////////////1979 //1980+// bitmap baking1981+//1982+// This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If1983+// stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy.1984+1985+#ifndef STB_RECT_PACK_VERSION1986+// @TODO: simulate STB_RECT_PACK API with trivial logic from BakeFontBitmap,1987+// try to share code?!?1988+#error "no stb_rect_pack"1989+#endif1990+1991+#if 01992+struct stbtt_pack_context {1993+ void *user_allocator_context;1994+ void *pack_info;1995+ int width;1996+ int height;1997+ unsigned char *pixels;1998+};1999+#endif2000+2001+int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, void *alloc_context)2002+{2003+ stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context) ,alloc_context);2004+ int num_nodes = pw-1;2005+ stbrp_node *nodes = (stbrp_node *) STBTT_malloc(sizeof(*nodes ) * num_nodes,alloc_context);2006+2007+ if (context == NULL || nodes == NULL) {2008+ if (context != NULL) STBTT_free(context, alloc_context);2009+ if (nodes != NULL) STBTT_free(nodes , alloc_context);2010+ return 0;2011+ }2012+2013+ spc->user_allocator_context = alloc_context;2014+ spc->width = pw;2015+ spc->height = ph;2016+ spc->pixels = pixels;2017+ spc->pack_info = context;2018+ spc->nodes = nodes;2019+ spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;2020+2021+ stbrp_init_target(context, pw-1, ph-1, nodes, num_nodes);2022+2023+ STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels2024+2025+ return 1;2026+}2027+2028+void stbtt_PackEnd (stbtt_pack_context *spc)2029+{2030+ STBTT_free(spc->nodes , spc->user_allocator_context);2031+ STBTT_free(spc->pack_info, spc->user_allocator_context);2032+}2033+2034+int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)2035+{2036+ stbtt_fontinfo info;2037+ int i,j,k,n, return_value = 1;2038+ stbrp_context *context = (stbrp_context *) spc->pack_info;2039+ stbrp_rect *rects;2040+2041+ // flag all characters as NOT packed2042+ for (i=0; i < num_ranges; ++i)2043+ for (j=0; j < ranges[i].num_chars_in_range; ++j)2044+ ranges[i].chardata_for_range[j].x0 =2045+ ranges[i].chardata_for_range[j].y0 =2046+ ranges[i].chardata_for_range[j].x1 =2047+ ranges[i].chardata_for_range[j].y1 = 0;2048+2049+ n = 0;2050+ for (i=0; i < num_ranges; ++i)2051+ n += ranges[i].num_chars_in_range;2052+ 2053+ rects = STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context);2054+ if (rects == NULL)2055+ return 0;2056+2057+ stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index));2058+ k=0;2059+ for (i=0; i < num_ranges; ++i) {2060+ float fh = ranges[i].font_size;2061+ //float scale = fh > 0 ? stbtt_ScaleForPixelHeight(&info, fh) : stbtt_ScaleForPointSize(&info, fh);2062+ float scale = stbtt_ScaleForPixelHeight(&info, fh);2063+ for (j=0; j < ranges[i].num_chars_in_range; ++j) {2064+ int x0,y0,x1,y1;2065+ stbtt_GetCodepointBitmapBox(&info, ranges[i].first_unicode_char_in_range + j, scale,scale, &x0,&y0,&x1,&y1);2066+ rects[k].w = x1-x0+1;2067+ rects[k].h = y1-y0+1;2068+ ++k;2069+ }2070+ }2071+2072+ stbrp_pack_rects(context, rects, k);2073+2074+ k = 0;2075+ for (i=0; i < num_ranges; ++i) {2076+ float fh = ranges[i].font_size;2077+ //float scale = fh > 0 ? stbtt_ScaleForPixelHeight(&info, fh) : stbtt_ScaleForPointSize(&info, fh);2078+ float scale = stbtt_ScaleForPixelHeight(&info, fh);2079+ for (j=0; j < ranges[i].num_chars_in_range; ++j) {2080+ stbrp_rect *r = &rects[k];2081+ if (r->was_packed) {2082+ stbtt_bakedchar *bc = &ranges[i].chardata_for_range[j];2083+ int advance, lsb, x0,y0,x1,y1;2084+ int glyph = stbtt_FindGlyphIndex(&info, ranges[i].first_unicode_char_in_range + j);2085+2086+ stbtt_GetGlyphHMetrics(&info, glyph, &advance, &lsb);2087+ stbtt_GetGlyphBitmapBox(&info, glyph, scale,scale, &x0,&y0,&x1,&y1);2088+ stbtt_MakeGlyphBitmapSubpixel(&info,2089+ spc->pixels + r->x + r->y*spc->stride_in_bytes,2090+ r->w-1, r->h-1,2091+ spc->stride_in_bytes,2092+ scale,scale,2093+ 0.0f, 0.0f,2094+ glyph);2095+ bc->x0 = (stbtt_int16) r->x;2096+ bc->y0 = (stbtt_int16) r->y;2097+ bc->x1 = (stbtt_int16) (r->x + r->w-1);2098+ bc->y1 = (stbtt_int16) (r->y + r->h-1);2099+ bc->xadvance = scale * advance;2100+ bc->xoff = (float) x0;2101+ bc->yoff = (float) y0;2102+ } else {2103+ return_value = 0; // if any fail, report failure2104+ }2105+2106+ ++k;2107+ }2108+ }2109+2110+ return return_value;2111+}2112+2113+int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,2114+ int first_unicode_char_in_range, int num_chars_in_range, stbtt_bakedchar *chardata_for_range)2115+{2116+ stbtt_pack_range range;2117+ range.first_unicode_char_in_range = first_unicode_char_in_range;2118+ range.num_chars_in_range = num_chars_in_range;2119+ range.chardata_for_range = chardata_for_range;2120+ range.font_size = font_size;2121+ return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);2122+}2123+2124+//////////////////////////////////////////////////////////////////////////////2125+//2126 // font name matching -- recommended not to use this2127 //2128 2129 // check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string155 unmodified lines