Skip to content
Myr

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

1 file+186 added−1 removed

modified:stb_truetype.h+186 added-1 removed
461 unmodified lines
462//463// It's inefficient; you might want to c&p it and optimize it.464465466467//////////////////////////////////////////////////////////////////////////////468//469// NEW TEXTURE BAKING API470//471// This provides options for packing multiple fonts into one atlas, not472// perfectly but better than nothing.473474typedef struct stbtt_pack_context stbtt_pack_context;475typedef 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;482483extern 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 fail485486extern void stbtt_PackEnd (stbtt_pack_context *spc);487extern 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);489extern int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);490491492// this is an opaque structure that you shouldn't mess with which holds493// all the context needed from PackBegin to PackEnd.494struct 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};503504//////////////////////////////////////////////////////////////////////////////505//506// FONT LOADING507//508//509
1407 unmodified lines
1917{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 lines
1976}19771978//////////////////////////////////////////////////////////////////////////////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.19841985#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#endif19901991#if 01992struct stbtt_pack_context {1993 void *user_allocator_context;1994 void *pack_info;1995 int width;1996 int height;1997 unsigned char *pixels;1998};1999#endif20002001int 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);20062007 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 }20122013 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;20202021 stbrp_init_target(context, pw-1, ph-1, nodes, num_nodes);20222023 STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels20242025 return 1;2026}20272028void 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}20332034int 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;20402041 // 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;20482049 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;20562057 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 }20712072 stbrp_pack_rects(context, rects, k);20732074 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);20852086 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 }21052106 ++k;2107 }2108 }21092110 return return_value;2111}21122113int 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}21232124//////////////////////////////////////////////////////////////////////////////2125//2126// font name matching -- recommended not to use this2127//21282129// check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string
155 unmodified lines