Skip to content
Myr

Merge branch 'stb-truetype-static' of https://github.com/ocornut/stb into work2

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 · parents 1e57bcd, 45fec17 · Browse files · 1 file+95 added−89 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+95 added−89 removed

  • other:+95 added−89 removed

1 file+95 added−89 removed

modified:stb_truetype.h+95 added-89 removed
375 unmodified lines
376377 typedef char stbtt__check_size32[sizeof(stbtt_int32)==4 ? 1 : -1];378 typedef char stbtt__check_size16[sizeof(stbtt_int16)==2 ? 1 : -1];379380 #ifdef STBTT_STATIC381 #define STBTT_DEF static382 #else383 #define STBTT_DEF extern384 #endif385386 // #define your own STBTT_sort() to override this to avoid qsort387 #ifndef STBTT_sort388 #include <stdlib.h>389 #define STBTT_sort(data,num_items,item_size,compare_func) qsort(data,num_items,item_size,compare_func)
61 unmodified lines
451 unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap452 float xoff,yoff,xadvance;453} stbtt_bakedchar;454449extern int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf)455STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf)456 float pixel_height, // height of font in pixels457 unsigned char *pixels, int pw, int ph, // bitmap to be filled in458 int first_char, int num_chars, // characters to bake459 stbtt_bakedchar *chardata); // you allocate this, it's num_chars long
7 unmodified lines
467 float x0,y0,s0,t0; // top-left468 float x1,y1,s1,t1; // bottom-right469} stbtt_aligned_quad;470465extern void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, // same data as above471STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, // same data as above472 int char_index, // character to display473 float *xpos, float *ypos, // pointers to current position in screen pixel space474 stbtt_aligned_quad *q, // output: quad to draw475 int opengl_fillrule); // true if opengl fill rule; false if DX9 or earlier
24 unmodified lines
500} stbtt_packedchar;501502typedef struct stbtt_pack_context stbtt_pack_context;503498extern int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context);504STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context);505// Initializes a packing context stored in the passed-in stbtt_pack_context.506// Future calls using this context will pack characters into the bitmap passed507// in here: a 1-channel bitmap that is weight x height. stride_in_bytes is508// the distance from one row to the next (or 0 to mean they are packed tightly
2 unmodified lines
511// bilinear filtering).512//513// Returns 0 on failure, 1 on success.514509extern void stbtt_PackEnd (stbtt_pack_context *spc);515STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc);516// Cleans up the packing context and frees all memory.517518#define STBTT_POINT_SIZE(x) (-(x))519514extern int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,520STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,521 int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range);522// Creates character bitmaps from the font_index'th font found in fontdata (use523// font_index=0 if you don't know what that is). It creates num_chars_in_range524// bitmaps for characters with unicode values starting at first_unicode_char_in_range
14 unmodified lines
539 int num_chars_in_range;540 stbtt_packedchar *chardata_for_range; // output541} stbtt_pack_range;542537extern int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);543STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);544// Creates character bitmaps from multiple ranges of characters stored in545// ranges. This will usually create a better-packed bitmap than multiple546// calls to stbtt_PackFontRange.547548543extern void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample);549STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample);550// Oversampling a font increases the quality by allowing higher-quality subpixel551// positioning, and is especially valuable at smaller text sizes.552//553// This function sets the amount of oversampling for all following calls to
3 unmodified lines
557// oversampling requires 4x the storage of 1x1. For best results, render558// oversampled textures with bilinear filtering. Look at the readme in559// stb/tests/oversample for information about oversampled fonts560555extern void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, // same data as above561STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, // same data as above562 int char_index, // character to display563 float *xpos, float *ypos, // pointers to current position in screen pixel space564 stbtt_aligned_quad *q, // output: quad to draw565 int align_to_integer);
17 unmodified lines
583// FONT LOADING584//585//586581extern int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index);587STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index);588// Each .ttf/.ttc file may have more than one font. Each font has a sequential589// index number starting from 0. Call this function to get the font offset for590// a given index; it returns -1 if the index is out of range. A regular .ttf591// file will only define one font and it always be at offset 0, so it will
15 unmodified lines
607 int index_map; // a cmap mapping for our chosen character encoding608 int indexToLocFormat; // format needed to map from glyph index to glyph609} stbtt_fontinfo;610605extern int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);611STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);612// Given an offset into the file that defines a font, this function builds613// the necessary cached info for the rest of the system. You must allocate614// the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't615// need to do anything special to free it, because the contents are pure
3 unmodified lines
619//////////////////////////////////////////////////////////////////////////////620//621// CHARACTER TO GLYPH-INDEX CONVERSIOn622617int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint);623STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint);624// If you're going to perform multiple operations on the same character625// and you want a speed-up, call this function with the character you're626// going to process, then use glyph-based functions instead of the627// codepoint-based functions.
3 unmodified lines
631//632// CHARACTER PROPERTIES633//634629extern float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels);635STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels);636// computes a scale factor to produce a font whose "height" is 'pixels' tall.637// Height is measured as the distance from the highest ascender to the lowest638// descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics639// and computing:640// scale = pixels / (ascent - descent)641// so if you prefer to measure height by the ascent only, use a similar calculation.642637extern float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels);643STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels);644// computes a scale factor to produce a font whose EM size is mapped to645// 'pixels' tall. This is probably what traditional APIs compute, but646// I'm not positive.647642extern void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap);648STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap);649// ascent is the coordinate above the baseline the font extends; descent650// is the coordinate below the baseline the font extends (i.e. it is typically negative)651// lineGap is the spacing between one row's descent and the next row's ascent...652// so you should advance the vertical position by "*ascent - *descent + *lineGap"653// these are expressed in unscaled coordinates, so you must multiply by654// the scale factor for a given size655650extern void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1);656STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1);657// the bounding box around all possible characters658653extern void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing);659STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing);660// leftSideBearing is the offset from the current horizontal position to the left edge of the character661// advanceWidth is the offset from the current horizontal position to the next horizontal position662// these are expressed in unscaled coordinates663658extern int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2);664STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2);665// an additional amount to add to the 'advance' value between ch1 and ch2666661extern int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1);667STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1);668// Gets the bounding box of the visible part of the glyph, in unscaled coordinates669664extern void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing);665extern int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);666extern int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);670STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing);671STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);672STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);673// as above, but takes one or more glyph indices for greater efficiency674675676//////////////////////////////////////////////////////////////////////////////
19 unmodified lines
696 unsigned char type,padding;697 } stbtt_vertex;698#endif699694extern int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index);700STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index);701// returns non-zero if nothing is drawn for this glyph702697extern int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices);698extern int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices);703STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices);704STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices);705// returns # of vertices and fills *vertices with the pointer to them706// these are expressed in "unscaled" coordinates707//708// The shape is a series of countours. Each one starts with
2 unmodified lines
711// draws a line from previous endpoint to its x,y; a curveto712// draws a quadratic bezier from previous endpoint to713// its x,y, using cx,cy as the bezier control point.714709extern void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices);715STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices);716// frees the data allocated above717718//////////////////////////////////////////////////////////////////////////////719//720// BITMAP RENDERING721//722717extern void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata);723STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata);724// frees the bitmap allocated below725720extern unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff);726STBTT_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);727// allocates a large-enough single-channel 8bpp bitmap and renders the728// specified character/glyph at the specified scale into it, with729// antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque).730// *width & *height are filled out with the width & height of the bitmap,731// which is stored left-to-right, top-to-bottom.732//733// xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap734729extern 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);735STBTT_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);736// the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel737// shift for the character738733extern 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);739STBTT_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);740// the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap741// in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap742// is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the743// width and height and positioning info for it first.744739extern 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);745STBTT_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);746// same as stbtt_MakeCodepointBitmap, but you can specify a subpixel747// shift for the character748743extern void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);749STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);750// get the bbox of the bitmap centered around the glyph origin; so the751// bitmap width is ix1-ix0, height is iy1-iy0, and location to place752// the bitmap top left is (leftSideBearing*scale,iy0).753// (Note that the bitmap uses y-increases-down, but the shape uses754// y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.)755750extern 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);756STBTT_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);757// same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel758// shift for the character759760// the following functions are equivalent to the above functions, but operate761// on glyph indices instead of Unicode codepoints (for efficiency)756extern unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff);757extern 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);758extern 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);759extern 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);760extern void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);761extern 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);762STBTT_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);763STBTT_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);764STBTT_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);765STBTT_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);766STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);767STBTT_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);768769770// @TODO: don't expose this structure771typedef struct
1 unmodified line
773 int w,h,stride;774 unsigned char *pixels;775} stbtt__bitmap;776771extern void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata);777STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata);778779//////////////////////////////////////////////////////////////////////////////780//781// Finding the right font...
15 unmodified lines
797// from the file yourself and do your own comparisons on them.798// You have to have called stbtt_InitFont() first.799800795extern int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags);801STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags);802// returns the offset (not index) of the font that matches, or -1 if none803// if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold".804// if you use any other flag, use a font name like "Arial"; this checks805// the 'macStyle' header field; i don't know if fonts set this consistently
2 unmodified lines
808#define STBTT_MACSTYLE_ITALIC 2809#define STBTT_MACSTYLE_UNDERSCORE 4810#define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0811806extern int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2);812STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2);813// returns 1/0 whether the first string interpreted as utf8 is identical to814// the second string interpreted as big-endian utf16... useful for strings from next func815810extern const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID);816STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID);817// returns the string (which may be big-endian double byte, e.g. for unicode)818// and puts the length in bytes in *length.819//820// some of the values for the IDs are below; for more see the truetype spec:
90 unmodified lines
911 #define ttLONG(p) (* (stbtt_int32 *) (p))912913#else914909 stbtt_uint16 ttUSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; }910 stbtt_int16 ttSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; }911 stbtt_uint32 ttULONG(const stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }912 stbtt_int32 ttLONG(const stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }915 static stbtt_uint16 ttUSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; }916 static stbtt_int16 ttSHORT(const stbtt_uint8 *p) { return p[0]*256 + p[1]; }917 static stbtt_uint32 ttULONG(const stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }918 static stbtt_int32 ttLONG(const stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }919920#endif921922#define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
22 unmodified lines
945 }946 return 0;947}948943int stbtt_GetFontOffsetForIndex(const unsigned char *font_collection, int index)949STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *font_collection, int index)950{951 // if it's just a font, there's only one valid index952 if (stbtt__isfont(font_collection))953 return index == 0 ? 0 : -1;
10 unmodified lines
964 }965 return -1;966}967962int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data2, int fontstart)968STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data2, int fontstart)969{970 stbtt_uint8 *data = (stbtt_uint8 *) data2;971 stbtt_uint32 cmap, t;972 stbtt_int32 i,numTables;
48 unmodified lines
1021 info->indexToLocFormat = ttUSHORT(data+info->head + 50);1022 return 1;1023}10241019int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)1025STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)1026{1027 stbtt_uint8 *data = info->data;1028 stbtt_uint32 index_map = info->index_map;1029
83 unmodified lines
1113 STBTT_assert(0);1114 return 0;1115}11161111int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices)1117STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices)1118{1119 return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);1120}1121
23 unmodified lines
11451146 return g1==g2 ? -1 : g1; // if length is 0, return -11147}11481143int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)1149STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)1150{1151 int g = stbtt__GetGlyfOffset(info, glyph_index);1152 if (g < 0) return 0;1153
3 unmodified lines
1157 if (y1) *y1 = ttSHORT(info->data + g + 8);1158 return 1;1159}11601155int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)1161STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)1162{1163 return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1);1164}11651160int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)1166STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)1167{1168 stbtt_int16 numberOfContours;1169 int g = stbtt__GetGlyfOffset(info, glyph_index);1170 if (g < 0) return 1;
16 unmodified lines
1187 }1188 return num_vertices;1189}11901185int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)1191STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)1192{1193 stbtt_int16 numberOfContours;1194 stbtt_uint8 *endPtsOfContours;1195 stbtt_uint8 *data = info->data;
217 unmodified lines
1413 *pvertices = vertices;1414 return num_vertices;1415}14161411void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)1417STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)1418{1419 stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);1420 if (glyph_index < numOfLongHorMetrics) {1421 if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index);
3 unmodified lines
1425 if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));1426 }1427}14281423int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)1429STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)1430{1431 stbtt_uint8 *data = info->data + info->kern;1432 stbtt_uint32 needle, straw;1433 int l, r, m;
21 unmodified lines
1455 }1456 return 0;1457}14581453int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2)1459STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2)1460{1461 if (!info->kern) // if no kerning table, don't waste time looking up both codepoint->glyphs1462 return 0;1463 return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2));1464}14651460void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing)1466STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing)1467{1468 stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing);1469}14701465void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)1471STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)1472{1473 if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4);1474 if (descent) *descent = ttSHORT(info->data+info->hhea + 6);1475 if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);1476}14771472void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1)1478STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1)1479{1480 *x0 = ttSHORT(info->data + info->head + 36);1481 *y0 = ttSHORT(info->data + info->head + 38);1482 *x1 = ttSHORT(info->data + info->head + 40);1483 *y1 = ttSHORT(info->data + info->head + 42);1484}14851480float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height)1486STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height)1487{1488 int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);1489 return (float) height / fheight;1490}14911486float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels)1492STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels)1493{1494 int unitsPerEm = ttUSHORT(info->data + info->head + 18);1495 return pixels / unitsPerEm;1496}14971492void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v)1498STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v)1499{1500 STBTT_free(v, info->userdata);1501}1502
1 unmodified line
1504//1505// antialiasing software rasterizer1506//15071502void 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)1508STBTT_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)1509{1510 int x0,y0,x1,y1;1511 if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {1512 // e.g. space character
9 unmodified lines
1522 if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y);1523 }1524}15251520void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)1526STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)1527{1528 stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);1529}15301525void 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)1531STBTT_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)1532{1533 stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1);1534}15351530void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)1536STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)1537{1538 stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1);1539}1540
272 unmodified lines
1813 return 1;1814}18151816// returns number of contours1811stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)1817static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)1818{1819 stbtt__point *points=0;1820 int num_points=0;1821
60 unmodified lines
1882 *num_contours = 0;1883 return NULL;1884}18851880void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata)1886STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata)1887{1888 float scale = scale_x > scale_y ? scale_y : scale_x;1889 int winding_count, *winding_lengths;1890 stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
3 unmodified lines
1894 STBTT_free(windings, userdata);1895 }1896}18971892void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata)1898STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata)1899{1900 STBTT_free(bitmap, userdata);1901}19021897unsigned 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)1903STBTT_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)1904{1905 int ix0,iy0,ix1,iy1;1906 stbtt__bitmap gbm;1907 stbtt_vertex *vertices;
28 unmodified lines
1936 STBTT_free(vertices, info->userdata);1937 return gbm.pixels;1938} 19391934unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff)1940STBTT_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)1941{1942 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);1943}19441939void 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)1945STBTT_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)1946{1947 int ix0,iy0;1948 stbtt_vertex *vertices;1949 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
10 unmodified lines
19601961 STBTT_free(vertices, info->userdata);1962}19631958void 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)1964STBTT_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)1965{1966 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph);1967}19681963unsigned 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)1969STBTT_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)1970{1971 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff);1972} 19731968void 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)1974STBTT_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)1975{1976 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint));1977}19781973unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff)1979STBTT_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)1980{1981 return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff);1982} 19831978void 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)1984STBTT_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)1985{1986 stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint);1987}1988
2 unmodified lines
1991// bitmap baking1992//1993// This is SUPER-CRAPPY packing to keep source code small19941989extern int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf)1995STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf)1996 float pixel_height, // height of font in pixels1997 unsigned char *pixels, int pw, int ph, // bitmap to be filled in1998 int first_char, int num_chars, // characters to bake1999 stbtt_bakedchar *chardata)
36 unmodified lines
2036 }2037 return bottom_y;2038}20392034void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int opengl_fillrule)2040STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int opengl_fillrule)2041{2042 float d3d_bias = opengl_fillrule ? 0 : -0.5f;2043 float ipw = 1.0f / pw, iph = 1.0f / ph;2044 stbtt_bakedchar *b = chardata + char_index;
94 unmodified lines
2139//2140// This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If2141// stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy.21422137int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context)2143STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context)2144{2145 stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context) ,alloc_context);2146 int num_nodes = pw - padding;2147 stbrp_node *nodes = (stbrp_node *) STBTT_malloc(sizeof(*nodes ) * num_nodes,alloc_context);
21 unmodified lines
21692170 return 1;2171}21722167void stbtt_PackEnd (stbtt_pack_context *spc)2173STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc)2174{2175 STBTT_free(spc->nodes , spc->user_allocator_context);2176 STBTT_free(spc->pack_info, spc->user_allocator_context);2177}21782173void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)2179STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)2180{2181 STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);2182 STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);2183 if (h_oversample <= STBTT_MAX_OVERSAMPLE)
123 unmodified lines
2307 // direction to counter this.2308 return (float)-(oversample - 1) / (2.0f * (float)oversample);2309}23102305int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)2311STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)2312{2313 stbtt_fontinfo info;2314 float recip_h = 1.0f / spc->h_oversample;2315 float recip_v = 1.0f / spc->v_oversample;
100 unmodified lines
24162417 return return_value;2418}24192414int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,2420STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,2421 int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range)2422{2423 stbtt_pack_range range;2424 range.first_unicode_char_in_range = first_unicode_char_in_range;
2 unmodified lines
2427 range.font_size = font_size;2428 return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);2429}24302425void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer)2431STBTT_DEF void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer)2432{2433 float ipw = 1.0f / pw, iph = 1.0f / ph;2434 stbtt_packedchar *b = chardata + char_index;2435
64 unmodified lines
2500 }2501 return i;2502}25032498int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2) 2504STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2) 2505{2506 return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((const stbtt_uint8*) s1, len1, (const stbtt_uint8*) s2, len2);2507}25082509// returns results in whatever encoding you request... but note that 2-byte encodings2510// will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare2505const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID)2511STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID)2512{2513 stbtt_int32 i,count,stringOffset;2514 stbtt_uint8 *fc = font->data;2515 stbtt_uint32 offset = font->fontstart;
88 unmodified lines
26042605 return 0;2606}26072602int stbtt_FindMatchingFont(const unsigned char *font_collection, const char *name_utf8, stbtt_int32 flags)2608STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *font_collection, const char *name_utf8, stbtt_int32 flags)2609{2610 stbtt_int32 i;2611 for (i=0;;++i) {2612 stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
7 unmodified lines