stb_truetype.h (35204B)
1 // stb_truetype.h - v1.24 - public domain 2 // authored from 2009-2020 by Sean Barrett / RAD Game Tools 3 // 4 // ======================================================================= 5 // 6 // NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES 7 // 8 // This library does no range checking of the offsets found in the file, 9 // meaning an attacker can use it to read arbitrary memory. 10 // 11 // ======================================================================= 12 13 /////////////////////////////////////////////////////////////////////////////// 14 /////////////////////////////////////////////////////////////////////////////// 15 //// 16 //// INTERFACE 17 //// 18 //// 19 20 #ifndef __STB_INCLUDE_STB_TRUETYPE_H__ 21 #define __STB_INCLUDE_STB_TRUETYPE_H__ 22 23 #ifdef STBTT_STATIC 24 #define STBTT_DEF static 25 #else 26 #define STBTT_DEF extern 27 #endif 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 // private structure 34 typedef struct 35 { 36 unsigned char *data; 37 int cursor; 38 int size; 39 } stbtt__buf; 40 41 ////////////////////////////////////////////////////////////////////////////// 42 // 43 // TEXTURE BAKING API 44 // 45 // If you use this API, you only have to call two functions ever. 46 // 47 48 typedef struct 49 { 50 unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap 51 float xoff,yoff,xadvance; 52 } stbtt_bakedchar; 53 54 STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf) 55 float pixel_height, // height of font in pixels 56 unsigned char *pixels, int pw, int ph, // bitmap to be filled in 57 int first_char, int num_chars, // characters to bake 58 stbtt_bakedchar *chardata); // you allocate this, it's num_chars long 59 // if return is positive, the first unused row of the bitmap 60 // if return is negative, returns the negative of the number of characters that fit 61 // if return is 0, no characters fit and no rows were used 62 // This uses a very crappy packing. 63 64 typedef struct 65 { 66 float x0,y0,s0,t0; // top-left 67 float x1,y1,s1,t1; // bottom-right 68 } stbtt_aligned_quad; 69 70 STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar *chardata, int pw, int ph, // same data as above 71 int char_index, // character to display 72 float *xpos, float *ypos, // pointers to current position in screen pixel space 73 stbtt_aligned_quad *q, // output: quad to draw 74 int opengl_fillrule); // true if opengl fill rule; false if DX9 or earlier 75 // Call GetBakedQuad with char_index = 'character - first_char', and it 76 // creates the quad you need to draw and advances the current position. 77 // 78 // The coordinate system used assumes y increases downwards. 79 // 80 // Characters will extend both above and below the current position; 81 // see discussion of "BASELINE" above. 82 // 83 // It's inefficient; you might want to c&p it and optimize it. 84 85 STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap); 86 // Query the font vertical metrics without having to create a font first. 87 88 89 ////////////////////////////////////////////////////////////////////////////// 90 // 91 // NEW TEXTURE BAKING API 92 // 93 // This provides options for packing multiple fonts into one atlas, not 94 // perfectly but better than nothing. 95 96 typedef struct 97 { 98 unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap 99 float xoff,yoff,xadvance; 100 float xoff2,yoff2; 101 } stbtt_packedchar; 102 103 typedef struct stbtt_pack_context stbtt_pack_context; 104 typedef struct stbtt_fontinfo stbtt_fontinfo; 105 #ifndef STB_RECT_PACK_VERSION 106 typedef struct stbrp_rect stbrp_rect; 107 #endif 108 109 STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context); 110 // Initializes a packing context stored in the passed-in stbtt_pack_context. 111 // Future calls using this context will pack characters into the bitmap passed 112 // in here: a 1-channel bitmap that is width * height. stride_in_bytes is 113 // the distance from one row to the next (or 0 to mean they are packed tightly 114 // together). "padding" is the amount of padding to leave between each 115 // character (normally you want '1' for bitmaps you'll use as textures with 116 // bilinear filtering). 117 // 118 // Returns 0 on failure, 1 on success. 119 120 STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc); 121 // Cleans up the packing context and frees all memory. 122 123 #define STBTT_POINT_SIZE(x) (-(x)) 124 125 STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size, 126 int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range); 127 // Creates character bitmaps from the font_index'th font found in fontdata (use 128 // font_index=0 if you don't know what that is). It creates num_chars_in_range 129 // bitmaps for characters with unicode values starting at first_unicode_char_in_range 130 // and increasing. Data for how to render them is stored in chardata_for_range; 131 // pass these to stbtt_GetPackedQuad to get back renderable quads. 132 // 133 // font_size is the full height of the character from ascender to descender, 134 // as computed by stbtt_ScaleForPixelHeight. To use a point size as computed 135 // by stbtt_ScaleForMappingEmToPixels, wrap the point size in STBTT_POINT_SIZE() 136 // and pass that result as 'font_size': 137 // ..., 20 , ... // font max minus min y is 20 pixels tall 138 // ..., STBTT_POINT_SIZE(20), ... // 'M' is 20 pixels tall 139 140 typedef struct 141 { 142 float font_size; 143 int first_unicode_codepoint_in_range; // if non-zero, then the chars are continuous, and this is the first codepoint 144 int *array_of_unicode_codepoints; // if non-zero, then this is an array of unicode codepoints 145 int num_chars; 146 stbtt_packedchar *chardata_for_range; // output 147 unsigned char h_oversample, v_oversample; // don't set these, they're used internally 148 } stbtt_pack_range; 149 150 STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges); 151 // Creates character bitmaps from multiple ranges of characters stored in 152 // ranges. This will usually create a better-packed bitmap than multiple 153 // calls to stbtt_PackFontRange. Note that you can call this multiple 154 // times within a single PackBegin/PackEnd. 155 156 STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample); 157 // Oversampling a font increases the quality by allowing higher-quality subpixel 158 // positioning, and is especially valuable at smaller text sizes. 159 // 160 // This function sets the amount of oversampling for all following calls to 161 // stbtt_PackFontRange(s) or stbtt_PackFontRangesGatherRects for a given 162 // pack context. The default (no oversampling) is achieved by h_oversample=1 163 // and v_oversample=1. The total number of pixels required is 164 // h_oversample*v_oversample larger than the default; for example, 2x2 165 // oversampling requires 4x the storage of 1x1. For best results, render 166 // oversampled textures with bilinear filtering. Look at the readme in 167 // stb/tests/oversample for information about oversampled fonts 168 // 169 // To use with PackFontRangesGather etc., you must set it before calls 170 // call to PackFontRangesGatherRects. 171 172 STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip); 173 // If skip != 0, this tells stb_truetype to skip any codepoints for which 174 // there is no corresponding glyph. If skip=0, which is the default, then 175 // codepoints without a glyph recived the font's "missing character" glyph, 176 // typically an empty box by convention. 177 178 STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph, // same data as above 179 int char_index, // character to display 180 float *xpos, float *ypos, // pointers to current position in screen pixel space 181 stbtt_aligned_quad *q, // output: quad to draw 182 int align_to_integer); 183 184 STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects); 185 STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects); 186 STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects); 187 // Calling these functions in sequence is roughly equivalent to calling 188 // stbtt_PackFontRanges(). If you more control over the packing of multiple 189 // fonts, or if you want to pack custom data into a font texture, take a look 190 // at the source to of stbtt_PackFontRanges() and create a custom version 191 // using these functions, e.g. call GatherRects multiple times, 192 // building up a single array of rects, then call PackRects once, 193 // then call RenderIntoRects repeatedly. This may result in a 194 // better packing than calling PackFontRanges multiple times 195 // (or it may not). 196 197 // this is an opaque structure that you shouldn't mess with which holds 198 // all the context needed from PackBegin to PackEnd. 199 struct stbtt_pack_context { 200 void *user_allocator_context; 201 void *pack_info; 202 int width; 203 int height; 204 int stride_in_bytes; 205 int padding; 206 int skip_missing; 207 unsigned int h_oversample, v_oversample; 208 unsigned char *pixels; 209 void *nodes; 210 }; 211 212 ////////////////////////////////////////////////////////////////////////////// 213 // 214 // FONT LOADING 215 // 216 // 217 218 STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data); 219 // This function will determine the number of fonts in a font file. TrueType 220 // collection (.ttc) files may contain multiple fonts, while TrueType font 221 // (.ttf) files only contain one font. The number of fonts can be used for 222 // indexing with the previous function where the index is between zero and one 223 // less than the total fonts. If an error occurs, -1 is returned. 224 225 STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index); 226 // Each .ttf/.ttc file may have more than one font. Each font has a sequential 227 // index number starting from 0. Call this function to get the font offset for 228 // a given index; it returns -1 if the index is out of range. A regular .ttf 229 // file will only define one font and it always be at offset 0, so it will 230 // return '0' for index 0, and -1 for all other indices. 231 232 // The following structure is defined publicly so you can declare one on 233 // the stack or as a global or etc, but you should treat it as opaque. 234 struct stbtt_fontinfo 235 { 236 void * userdata; 237 unsigned char * data; // pointer to .ttf file 238 int fontstart; // offset of start of font 239 240 int numGlyphs; // number of glyphs, needed for range checking 241 242 int loca,head,glyf,hhea,hmtx,kern,gpos,svg; // table locations as offset from start of .ttf 243 int index_map; // a cmap mapping for our chosen character encoding 244 int indexToLocFormat; // format needed to map from glyph index to glyph 245 246 stbtt__buf cff; // cff font data 247 stbtt__buf charstrings; // the charstring index 248 stbtt__buf gsubrs; // global charstring subroutines index 249 stbtt__buf subrs; // private charstring subroutines index 250 stbtt__buf fontdicts; // array of font dicts 251 stbtt__buf fdselect; // map from glyph to fontdict 252 }; 253 254 STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset); 255 // Given an offset into the file that defines a font, this function builds 256 // the necessary cached info for the rest of the system. You must allocate 257 // the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't 258 // need to do anything special to free it, because the contents are pure 259 // value data with no additional data structures. Returns 0 on failure. 260 261 262 ////////////////////////////////////////////////////////////////////////////// 263 // 264 // CHARACTER TO GLYPH-INDEX CONVERSIOn 265 266 STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint); 267 // If you're going to perform multiple operations on the same character 268 // and you want a speed-up, call this function with the character you're 269 // going to process, then use glyph-based functions instead of the 270 // codepoint-based functions. 271 // Returns 0 if the character codepoint is not defined in the font. 272 273 274 ////////////////////////////////////////////////////////////////////////////// 275 // 276 // CHARACTER PROPERTIES 277 // 278 279 STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels); 280 // computes a scale factor to produce a font whose "height" is 'pixels' tall. 281 // Height is measured as the distance from the highest ascender to the lowest 282 // descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics 283 // and computing: 284 // scale = pixels / (ascent - descent) 285 // so if you prefer to measure height by the ascent only, use a similar calculation. 286 287 STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels); 288 // computes a scale factor to produce a font whose EM size is mapped to 289 // 'pixels' tall. This is probably what traditional APIs compute, but 290 // I'm not positive. 291 292 STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap); 293 // ascent is the coordinate above the baseline the font extends; descent 294 // is the coordinate below the baseline the font extends (i.e. it is typically negative) 295 // lineGap is the spacing between one row's descent and the next row's ascent... 296 // so you should advance the vertical position by "*ascent - *descent + *lineGap" 297 // these are expressed in unscaled coordinates, so you must multiply by 298 // the scale factor for a given size 299 300 STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap); 301 // analogous to GetFontVMetrics, but returns the "typographic" values from the OS/2 302 // table (specific to MS/Windows TTF files). 303 // 304 // Returns 1 on success (table present), 0 on failure. 305 306 STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1); 307 // the bounding box around all possible characters 308 309 STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing); 310 // leftSideBearing is the offset from the current horizontal position to the left edge of the character 311 // advanceWidth is the offset from the current horizontal position to the next horizontal position 312 // these are expressed in unscaled coordinates 313 314 STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2); 315 // an additional amount to add to the 'advance' value between ch1 and ch2 316 317 STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1); 318 // Gets the bounding box of the visible part of the glyph, in unscaled coordinates 319 320 STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing); 321 STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2); 322 STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1); 323 // as above, but takes one or more glyph indices for greater efficiency 324 325 typedef struct stbtt_kerningentry 326 { 327 int glyph1; // use stbtt_FindGlyphIndex 328 int glyph2; 329 int advance; 330 } stbtt_kerningentry; 331 332 STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo *info); 333 STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo *info, stbtt_kerningentry* table, int table_length); 334 // Retrieves a complete list of all of the kerning pairs provided by the font 335 // stbtt_GetKerningTable never writes more than table_length entries and returns how many entries it did write. 336 // The table will be sorted by (a.glyph1 == b.glyph1)?(a.glyph2 < b.glyph2):(a.glyph1 < b.glyph1) 337 338 ////////////////////////////////////////////////////////////////////////////// 339 // 340 // GLYPH SHAPES (you probably don't need these, but they have to go before 341 // the bitmaps for C declaration-order reasons) 342 // 343 344 #ifndef STBTT_vmove // you can predefine these to use different values (but why?) 345 enum { 346 STBTT_vmove=1, 347 STBTT_vline, 348 STBTT_vcurve, 349 STBTT_vcubic 350 }; 351 #endif 352 353 #ifndef stbtt_vertex // you can predefine this to use different values 354 // (we share this with other code at RAD) 355 #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file 356 typedef struct 357 { 358 stbtt_vertex_type x,y,cx,cy,cx1,cy1; 359 unsigned char type,padding; 360 } stbtt_vertex; 361 #endif 362 363 STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index); 364 // returns non-zero if nothing is drawn for this glyph 365 366 STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices); 367 STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices); 368 // returns # of vertices and fills *vertices with the pointer to them 369 // these are expressed in "unscaled" coordinates 370 // 371 // The shape is a series of contours. Each one starts with 372 // a STBTT_moveto, then consists of a series of mixed 373 // STBTT_lineto and STBTT_curveto segments. A lineto 374 // draws a line from previous endpoint to its x,y; a curveto 375 // draws a quadratic bezier from previous endpoint to 376 // its x,y, using cx,cy as the bezier control point. 377 378 STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices); 379 // frees the data allocated above 380 381 STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo *info, int unicode_codepoint, const char **svg); 382 STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo *info, int gl, const char **svg); 383 // fills svg with the character's SVG data. 384 // returns data size or 0 if SVG not found. 385 386 ////////////////////////////////////////////////////////////////////////////// 387 // 388 // BITMAP RENDERING 389 // 390 391 STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata); 392 // frees the bitmap allocated below 393 394 STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff); 395 // allocates a large-enough single-channel 8bpp bitmap and renders the 396 // specified character/glyph at the specified scale into it, with 397 // antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque). 398 // *width & *height are filled out with the width & height of the bitmap, 399 // which is stored left-to-right, top-to-bottom. 400 // 401 // xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap 402 403 STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff); 404 // the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel 405 // shift for the character 406 407 STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint); 408 // the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap 409 // in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap 410 // is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the 411 // width and height and positioning info for it first. 412 413 STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint); 414 // same as stbtt_MakeCodepointBitmap, but you can specify a subpixel 415 // shift for the character 416 417 STBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int codepoint); 418 // same as stbtt_MakeCodepointBitmapSubpixel, but prefiltering 419 // is performed (see stbtt_PackSetOversampling) 420 421 STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); 422 // get the bbox of the bitmap centered around the glyph origin; so the 423 // bitmap width is ix1-ix0, height is iy1-iy0, and location to place 424 // the bitmap top left is (leftSideBearing*scale,iy0). 425 // (Note that the bitmap uses y-increases-down, but the shape uses 426 // y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.) 427 428 STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1); 429 // same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel 430 // shift for the character 431 432 // the following functions are equivalent to the above functions, but operate 433 // on glyph indices instead of Unicode codepoints (for efficiency) 434 STBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff); 435 STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff); 436 STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph); 437 STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph); 438 STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int glyph); 439 STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); 440 STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1); 441 442 443 // @TODO: don't expose this structure 444 typedef struct 445 { 446 int w,h,stride; 447 unsigned char *pixels; 448 } stbtt__bitmap; 449 450 // rasterize a shape with quadratic beziers into a bitmap 451 STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, // 1-channel bitmap to draw into 452 float flatness_in_pixels, // allowable error of curve in pixels 453 stbtt_vertex *vertices, // array of vertices defining shape 454 int num_verts, // number of vertices in above array 455 float scale_x, float scale_y, // scale applied to input vertices 456 float shift_x, float shift_y, // translation applied to input vertices 457 int x_off, int y_off, // another translation applied to input 458 int invert, // if non-zero, vertically flip shape 459 void *userdata); // context for to STBTT_MALLOC 460 461 ////////////////////////////////////////////////////////////////////////////// 462 // 463 // Signed Distance Function (or Field) rendering 464 465 STBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata); 466 // frees the SDF bitmap allocated below 467 468 STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float scale, int glyph, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff); 469 STBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo *info, float scale, int codepoint, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff); 470 // These functions compute a discretized SDF field for a single character, suitable for storing 471 // in a single-channel texture, sampling with bilinear filtering, and testing against 472 // larger than some threshold to produce scalable fonts. 473 // info -- the font 474 // scale -- controls the size of the resulting SDF bitmap, same as it would be creating a regular bitmap 475 // glyph/codepoint -- the character to generate the SDF for 476 // padding -- extra "pixels" around the character which are filled with the distance to the character (not 0), 477 // which allows effects like bit outlines 478 // onedge_value -- value 0-255 to test the SDF against to reconstruct the character (i.e. the isocontour of the character) 479 // pixel_dist_scale -- what value the SDF should increase by when moving one SDF "pixel" away from the edge (on the 0..255 scale) 480 // if positive, > onedge_value is inside; if negative, < onedge_value is inside 481 // width,height -- output height & width of the SDF bitmap (including padding) 482 // xoff,yoff -- output origin of the character 483 // return value -- a 2D array of bytes 0..255, width*height in size 484 // 485 // pixel_dist_scale & onedge_value are a scale & bias that allows you to make 486 // optimal use of the limited 0..255 for your application, trading off precision 487 // and special effects. SDF values outside the range 0..255 are clamped to 0..255. 488 // 489 // Example: 490 // scale = stbtt_ScaleForPixelHeight(22) 491 // padding = 5 492 // onedge_value = 180 493 // pixel_dist_scale = 180/5.0 = 36.0 494 // 495 // This will create an SDF bitmap in which the character is about 22 pixels 496 // high but the whole bitmap is about 22+5+5=32 pixels high. To produce a filled 497 // shape, sample the SDF at each pixel and fill the pixel if the SDF value 498 // is greater than or equal to 180/255. (You'll actually want to antialias, 499 // which is beyond the scope of this example.) Additionally, you can compute 500 // offset outlines (e.g. to stroke the character border inside & outside, 501 // or only outside). For example, to fill outside the character up to 3 SDF 502 // pixels, you would compare against (180-36.0*3)/255 = 72/255. The above 503 // choice of variables maps a range from 5 pixels outside the shape to 504 // 2 pixels inside the shape to 0..255; this is intended primarily for apply 505 // outside effects only (the interior range is needed to allow proper 506 // antialiasing of the font at *smaller* sizes) 507 // 508 // The function computes the SDF analytically at each SDF pixel, not by e.g. 509 // building a higher-res bitmap and approximating it. In theory the quality 510 // should be as high as possible for an SDF of this size & representation, but 511 // unclear if this is true in practice (perhaps building a higher-res bitmap 512 // and computing from that can allow drop-out prevention). 513 // 514 // The algorithm has not been optimized at all, so expect it to be slow 515 // if computing lots of characters or very large sizes. 516 517 518 519 ////////////////////////////////////////////////////////////////////////////// 520 // 521 // Finding the right font... 522 // 523 // You should really just solve this offline, keep your own tables 524 // of what font is what, and don't try to get it out of the .ttf file. 525 // That's because getting it out of the .ttf file is really hard, because 526 // the names in the file can appear in many possible encodings, in many 527 // possible languages, and e.g. if you need a case-insensitive comparison, 528 // the details of that depend on the encoding & language in a complex way 529 // (actually underspecified in truetype, but also gigantic). 530 // 531 // But you can use the provided functions in two possible ways: 532 // stbtt_FindMatchingFont() will use *case-sensitive* comparisons on 533 // unicode-encoded names to try to find the font you want; 534 // you can run this before calling stbtt_InitFont() 535 // 536 // stbtt_GetFontNameString() lets you get any of the various strings 537 // from the file yourself and do your own comparisons on them. 538 // You have to have called stbtt_InitFont() first. 539 540 541 STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags); 542 // returns the offset (not index) of the font that matches, or -1 if none 543 // if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold". 544 // if you use any other flag, use a font name like "Arial"; this checks 545 // the 'macStyle' header field; i don't know if fonts set this consistently 546 #define STBTT_MACSTYLE_DONTCARE 0 547 #define STBTT_MACSTYLE_BOLD 1 548 #define STBTT_MACSTYLE_ITALIC 2 549 #define STBTT_MACSTYLE_UNDERSCORE 4 550 #define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0 551 552 STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2); 553 // returns 1/0 whether the first string interpreted as utf8 is identical to 554 // the second string interpreted as big-endian utf16... useful for strings from next func 555 556 STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID); 557 // returns the string (which may be big-endian double byte, e.g. for unicode) 558 // and puts the length in bytes in *length. 559 // 560 // some of the values for the IDs are below; for more see the truetype spec: 561 // http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html 562 // http://www.microsoft.com/typography/otspec/name.htm 563 564 enum { // platformID 565 STBTT_PLATFORM_ID_UNICODE =0, 566 STBTT_PLATFORM_ID_MAC =1, 567 STBTT_PLATFORM_ID_ISO =2, 568 STBTT_PLATFORM_ID_MICROSOFT =3 569 }; 570 571 enum { // encodingID for STBTT_PLATFORM_ID_UNICODE 572 STBTT_UNICODE_EID_UNICODE_1_0 =0, 573 STBTT_UNICODE_EID_UNICODE_1_1 =1, 574 STBTT_UNICODE_EID_ISO_10646 =2, 575 STBTT_UNICODE_EID_UNICODE_2_0_BMP=3, 576 STBTT_UNICODE_EID_UNICODE_2_0_FULL=4 577 }; 578 579 enum { // encodingID for STBTT_PLATFORM_ID_MICROSOFT 580 STBTT_MS_EID_SYMBOL =0, 581 STBTT_MS_EID_UNICODE_BMP =1, 582 STBTT_MS_EID_SHIFTJIS =2, 583 STBTT_MS_EID_UNICODE_FULL =10 584 }; 585 586 enum { // encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes 587 STBTT_MAC_EID_ROMAN =0, STBTT_MAC_EID_ARABIC =4, 588 STBTT_MAC_EID_JAPANESE =1, STBTT_MAC_EID_HEBREW =5, 589 STBTT_MAC_EID_CHINESE_TRAD =2, STBTT_MAC_EID_GREEK =6, 590 STBTT_MAC_EID_KOREAN =3, STBTT_MAC_EID_RUSSIAN =7 591 }; 592 593 enum { // languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID... 594 // problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs 595 STBTT_MS_LANG_ENGLISH =0x0409, STBTT_MS_LANG_ITALIAN =0x0410, 596 STBTT_MS_LANG_CHINESE =0x0804, STBTT_MS_LANG_JAPANESE =0x0411, 597 STBTT_MS_LANG_DUTCH =0x0413, STBTT_MS_LANG_KOREAN =0x0412, 598 STBTT_MS_LANG_FRENCH =0x040c, STBTT_MS_LANG_RUSSIAN =0x0419, 599 STBTT_MS_LANG_GERMAN =0x0407, STBTT_MS_LANG_SPANISH =0x0409, 600 STBTT_MS_LANG_HEBREW =0x040d, STBTT_MS_LANG_SWEDISH =0x041D 601 }; 602 603 enum { // languageID for STBTT_PLATFORM_ID_MAC 604 STBTT_MAC_LANG_ENGLISH =0 , STBTT_MAC_LANG_JAPANESE =11, 605 STBTT_MAC_LANG_ARABIC =12, STBTT_MAC_LANG_KOREAN =23, 606 STBTT_MAC_LANG_DUTCH =4 , STBTT_MAC_LANG_RUSSIAN =32, 607 STBTT_MAC_LANG_FRENCH =1 , STBTT_MAC_LANG_SPANISH =6 , 608 STBTT_MAC_LANG_GERMAN =2 , STBTT_MAC_LANG_SWEDISH =5 , 609 STBTT_MAC_LANG_HEBREW =10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33, 610 STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19 611 }; 612 613 #ifdef __cplusplus 614 } 615 #endif 616 617 #endif // __STB_INCLUDE_STB_TRUETYPE_H__ 618 619 /* 620 ------------------------------------------------------------------------------ 621 This software is available under 2 licenses -- choose whichever you prefer. 622 ------------------------------------------------------------------------------ 623 ALTERNATIVE A - MIT License 624 Copyright (c) 2017 Sean Barrett 625 Permission is hereby granted, free of charge, to any person obtaining a copy of 626 this software and associated documentation files (the "Software"), to deal in 627 the Software without restriction, including without limitation the rights to 628 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 629 of the Software, and to permit persons to whom the Software is furnished to do 630 so, subject to the following conditions: 631 The above copyright notice and this permission notice shall be included in all 632 copies or substantial portions of the Software. 633 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 634 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 635 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 636 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 637 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 638 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 639 SOFTWARE. 640 ------------------------------------------------------------------------------ 641 ALTERNATIVE B - Public Domain (www.unlicense.org) 642 This is free and unencumbered software released into the public domain. 643 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 644 software, either in source code form or as a compiled binary, for any purpose, 645 commercial or non-commercial, and by any means. 646 In jurisdictions that recognize copyright laws, the author or authors of this 647 software dedicate any and all copyright interest in the software to the public 648 domain. We make this dedication for the benefit of the public at large and to 649 the detriment of our heirs and successors. We intend this dedication to be an 650 overt act of relinquishment in perpetuity of all present and future rights to 651 this software under copyright law. 652 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 653 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 654 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 655 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 656 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 657 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 658 ------------------------------------------------------------------------------ 659 */