242 #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
243 #include "stb_truetype.h"
245 unsigned char ttf_buffer[1<<20];
246 unsigned char temp_bitmap[512*512];
251 void my_stbtt_initfont(
void)
253 fread(ttf_buffer, 1, 1<<20, fopen(
"c:/windows/fonts/times.ttf",
"rb"));
254 stbtt_BakeFontBitmap(ttf_buffer,0, 32.0, temp_bitmap,512,512, 32,96, cdata);
256 glGenTextures(1, &ftex);
257 glBindTexture(GL_TEXTURE_2D, ftex);
258 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
263 void my_stbtt_print(
float x,
float y,
char *text)
266 glEnable(GL_TEXTURE_2D);
267 glBindTexture(GL_TEXTURE_2D, ftex);
270 if (*text >= 32 && *text < 128) {
272 stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);
273 glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);
274 glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0);
275 glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1);
276 glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1);
291 #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
292 #include "stb_truetype.h"
294 char ttf_buffer[1<<25];
296 int main(
int argc,
char **argv)
299 unsigned char *bitmap;
300 int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) :
'a'), s = (argc > 2 ? atoi(argv[2]) : 20);
302 fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] :
"c:/windows/fonts/arialbd.ttf",
"rb"));
304 stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));
305 bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0);
307 for (j=0; j < h; ++j) {
308 for (i=0; i < w; ++i)
309 putchar(
" .:ioVM@"[bitmap[j*w+i]>>5]);
335 unsigned char screen[20][79];
337 int main(
int arg,
char **argv)
340 int i,j,ascent,baseline,ch=0;
342 char *text =
"Heljo World!";
344 fread(buffer, 1, 1000000, fopen(
"c:/windows/fonts/arialbd.ttf",
"rb"));
345 stbtt_InitFont(&font, buffer, 0);
347 scale = stbtt_ScaleForPixelHeight(&font, 15);
348 stbtt_GetFontVMetrics(&font, &ascent,0,0);
349 baseline = (int) (ascent*scale);
352 int advance,lsb,x0,y0,x1,y1;
353 float x_shift = xpos - (float) floor(xpos);
354 stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb);
355 stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1);
356 stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(
int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]);
361 xpos += (advance * scale);
363 xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]);
367 for (j=0; j < 20; ++j) {
368 for (i=0; i < 78; ++i)
369 putchar(
" .:ioVM@"[screen[j][i]>>5]);
386 #ifdef STB_TRUETYPE_IMPLEMENTATION
389 typedef unsigned char stbtt_uint8;
390 typedef signed char stbtt_int8;
391 typedef unsigned short stbtt_uint16;
392 typedef signed short stbtt_int16;
393 typedef unsigned int stbtt_uint32;
394 typedef signed int stbtt_int32;
397 typedef char stbtt__check_size32[
sizeof(stbtt_int32)==4 ? 1 : -1];
398 typedef char stbtt__check_size16[
sizeof(stbtt_int16)==2 ? 1 : -1];
403 #define STBTT_ifloor(x) ((int) floor(x))
404 #define STBTT_iceil(x) ((int) ceil(x))
409 #define STBTT_sqrt(x) sqrt(x)
414 #define STBTT_fabs(x) fabs(x)
420 #define STBTT_malloc(x,u) ((void)(u),malloc(x))
421 #define STBTT_free(x,u) ((void)(u),free(x))
426 #define STBTT_assert(x) assert(x)
431 #define STBTT_strlen(x) strlen(x)
436 #define STBTT_memcpy memcpy
437 #define STBTT_memset memset
448 #ifndef __STB_INCLUDE_STB_TRUETYPE_H__
449 #define __STB_INCLUDE_STB_TRUETYPE_H__
452 #define STBTT_DEF static
454 #define STBTT_DEF extern
470 unsigned short x0,y0,x1,y1;
471 float xoff,yoff,xadvance;
474 STBTT_DEF
int stbtt_BakeFontBitmap(
const unsigned char *data,
int offset,
476 unsigned char *pixels,
int pw,
int ph,
477 int first_char,
int num_chars,
490 STBTT_DEF
void stbtt_GetBakedQuad(
stbtt_bakedchar *chardata,
int pw,
int ph,
492 float *xpos,
float *ypos,
494 int opengl_fillrule);
516 unsigned short x0,y0,x1,y1;
517 float xoff,yoff,xadvance;
523 #ifndef STB_RECT_PACK_VERSION
524 typedef struct stbrp_rect stbrp_rect;
527 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);
541 #define STBTT_POINT_SIZE(x) (-(x))
543 STBTT_DEF
int stbtt_PackFontRange(
stbtt_pack_context *spc,
unsigned char *fontdata,
int font_index,
float font_size,
544 int first_unicode_char_in_range,
int num_chars_in_range,
stbtt_packedchar *chardata_for_range);
561 int first_unicode_codepoint_in_range;
562 int *array_of_unicode_codepoints;
565 unsigned char h_oversample, v_oversample;
574 STBTT_DEF
void stbtt_PackSetOversampling(
stbtt_pack_context *spc,
unsigned int h_oversample,
unsigned int v_oversample);
590 STBTT_DEF
void stbtt_GetPackedQuad(
stbtt_packedchar *chardata,
int pw,
int ph,
592 float *xpos,
float *ypos,
594 int align_to_integer);
597 STBTT_DEF
void stbtt_PackFontRangesPackRects(
stbtt_pack_context *spc, stbrp_rect *rects,
int num_rects);
612 void *user_allocator_context;
618 unsigned int h_oversample, v_oversample;
619 unsigned char *pixels;
629 STBTT_DEF
int stbtt_GetFontOffsetForIndex(
const unsigned char *data,
int index);
643 unsigned char * data;
648 int loca,head,glyf,hhea,hmtx,kern;
650 int indexToLocFormat;
653 STBTT_DEF
int stbtt_InitFont(
stbtt_fontinfo *info,
const unsigned char *data,
int offset);
665 STBTT_DEF
int stbtt_FindGlyphIndex(
const stbtt_fontinfo *info,
int unicode_codepoint);
677 STBTT_DEF
float stbtt_ScaleForPixelHeight(
const stbtt_fontinfo *info,
float pixels);
685 STBTT_DEF
float stbtt_ScaleForMappingEmToPixels(
const stbtt_fontinfo *info,
float pixels);
690 STBTT_DEF
void stbtt_GetFontVMetrics(
const stbtt_fontinfo *info,
int *ascent,
int *descent,
int *lineGap);
698 STBTT_DEF
void stbtt_GetFontBoundingBox(
const stbtt_fontinfo *info,
int *x0,
int *y0,
int *x1,
int *y1);
701 STBTT_DEF
void stbtt_GetCodepointHMetrics(
const stbtt_fontinfo *info,
int codepoint,
int *advanceWidth,
int *leftSideBearing);
706 STBTT_DEF
int stbtt_GetCodepointKernAdvance(
const stbtt_fontinfo *info,
int ch1,
int ch2);
709 STBTT_DEF
int stbtt_GetCodepointBox(
const stbtt_fontinfo *info,
int codepoint,
int *x0,
int *y0,
int *x1,
int *y1);
712 STBTT_DEF
void stbtt_GetGlyphHMetrics(
const stbtt_fontinfo *info,
int glyph_index,
int *advanceWidth,
int *leftSideBearing);
713 STBTT_DEF
int stbtt_GetGlyphKernAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2);
714 STBTT_DEF
int stbtt_GetGlyphBox(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1);
724 #ifndef STBTT_vmove // you can predefine these to use different values (but why?)
732 #ifndef stbtt_vertex // you can predefine this to use different values
734 #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file
737 stbtt_vertex_type x,y,cx,cy;
738 unsigned char type,padding;
742 STBTT_DEF
int stbtt_IsGlyphEmpty(
const stbtt_fontinfo *info,
int glyph_index);
765 STBTT_DEF
void stbtt_FreeBitmap(
unsigned char *bitmap,
void *userdata);
768 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);
777 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);
781 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);
787 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);
791 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);
798 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);
804 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);
805 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);
806 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);
807 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);
808 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);
809 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);
816 unsigned char *pixels;
821 float flatness_in_pixels,
824 float scale_x,
float scale_y,
825 float shift_x,
float shift_y,
826 int x_off,
int y_off,
852 STBTT_DEF
int stbtt_FindMatchingFont(
const unsigned char *fontdata,
const char *name,
int flags);
857 #define STBTT_MACSTYLE_DONTCARE 0
858 #define STBTT_MACSTYLE_BOLD 1
859 #define STBTT_MACSTYLE_ITALIC 2
860 #define STBTT_MACSTYLE_UNDERSCORE 4
861 #define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0
863 STBTT_DEF
int stbtt_CompareUTF8toUTF16_bigendian(
const char *s1,
int len1,
const char *s2,
int len2);
867 STBTT_DEF
const char *stbtt_GetFontNameString(
const stbtt_fontinfo *font,
int *length,
int platformID,
int encodingID,
int languageID,
int nameID);
876 STBTT_PLATFORM_ID_UNICODE =0,
877 STBTT_PLATFORM_ID_MAC =1,
878 STBTT_PLATFORM_ID_ISO =2,
879 STBTT_PLATFORM_ID_MICROSOFT =3
883 STBTT_UNICODE_EID_UNICODE_1_0 =0,
884 STBTT_UNICODE_EID_UNICODE_1_1 =1,
885 STBTT_UNICODE_EID_ISO_10646 =2,
886 STBTT_UNICODE_EID_UNICODE_2_0_BMP=3,
887 STBTT_UNICODE_EID_UNICODE_2_0_FULL=4
891 STBTT_MS_EID_SYMBOL =0,
892 STBTT_MS_EID_UNICODE_BMP =1,
893 STBTT_MS_EID_SHIFTJIS =2,
894 STBTT_MS_EID_UNICODE_FULL =10
898 STBTT_MAC_EID_ROMAN =0, STBTT_MAC_EID_ARABIC =4,
899 STBTT_MAC_EID_JAPANESE =1, STBTT_MAC_EID_HEBREW =5,
900 STBTT_MAC_EID_CHINESE_TRAD =2, STBTT_MAC_EID_GREEK =6,
901 STBTT_MAC_EID_KOREAN =3, STBTT_MAC_EID_RUSSIAN =7
906 STBTT_MS_LANG_ENGLISH =0x0409, STBTT_MS_LANG_ITALIAN =0x0410,
907 STBTT_MS_LANG_CHINESE =0x0804, STBTT_MS_LANG_JAPANESE =0x0411,
908 STBTT_MS_LANG_DUTCH =0x0413, STBTT_MS_LANG_KOREAN =0x0412,
909 STBTT_MS_LANG_FRENCH =0x040c, STBTT_MS_LANG_RUSSIAN =0x0419,
910 STBTT_MS_LANG_GERMAN =0x0407, STBTT_MS_LANG_SPANISH =0x0409,
911 STBTT_MS_LANG_HEBREW =0x040d, STBTT_MS_LANG_SWEDISH =0x041D
915 STBTT_MAC_LANG_ENGLISH =0 , STBTT_MAC_LANG_JAPANESE =11,
916 STBTT_MAC_LANG_ARABIC =12, STBTT_MAC_LANG_KOREAN =23,
917 STBTT_MAC_LANG_DUTCH =4 , STBTT_MAC_LANG_RUSSIAN =32,
918 STBTT_MAC_LANG_FRENCH =1 , STBTT_MAC_LANG_SPANISH =6 ,
919 STBTT_MAC_LANG_GERMAN =2 , STBTT_MAC_LANG_SWEDISH =5 ,
920 STBTT_MAC_LANG_HEBREW =10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33,
921 STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19
928 #endif // __STB_INCLUDE_STB_TRUETYPE_H__
937 #ifdef STB_TRUETYPE_IMPLEMENTATION
939 #ifndef STBTT_MAX_OVERSAMPLE
940 #define STBTT_MAX_OVERSAMPLE 8
943 #if STBTT_MAX_OVERSAMPLE > 255
944 #error "STBTT_MAX_OVERSAMPLE cannot be > 255"
947 typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1];
949 #ifndef STBTT_RASTERIZER_VERSION
950 #define STBTT_RASTERIZER_VERSION 2
954 #define STBTT__NOTUSED(v) (void)(v)
956 #define STBTT__NOTUSED(v) (void)sizeof(v)
967 #define ttBYTE(p) (* (stbtt_uint8 *) (p))
968 #define ttCHAR(p) (* (stbtt_int8 *) (p))
969 #define ttFixed(p) ttLONG(p)
971 #if defined(STB_TRUETYPE_BIGENDIAN) && !defined(ALLOW_UNALIGNED_TRUETYPE)
973 #define ttUSHORT(p) (* (stbtt_uint16 *) (p))
974 #define ttSHORT(p) (* (stbtt_int16 *) (p))
975 #define ttULONG(p) (* (stbtt_uint32 *) (p))
976 #define ttLONG(p) (* (stbtt_int32 *) (p))
980 static stbtt_uint16 ttUSHORT(
const stbtt_uint8 *p) {
return p[0]*256 + p[1]; }
981 static stbtt_int16 ttSHORT(
const stbtt_uint8 *p) {
return p[0]*256 + p[1]; }
982 static stbtt_uint32 ttULONG(
const stbtt_uint8 *p) {
return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
983 static stbtt_int32 ttLONG(
const stbtt_uint8 *p) {
return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
987 #define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
988 #define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3])
990 static int stbtt__isfont(
const stbtt_uint8 *font)
993 if (stbtt_tag4(font,
'1',0,0,0))
return 1;
994 if (stbtt_tag(font,
"typ1"))
return 1;
995 if (stbtt_tag(font,
"OTTO"))
return 1;
996 if (stbtt_tag4(font, 0,1,0,0))
return 1;
1001 static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart,
const char *tag)
1003 stbtt_int32 num_tables = ttUSHORT(data+fontstart+4);
1004 stbtt_uint32 tabledir = fontstart + 12;
1006 for (i=0; i < num_tables; ++i) {
1007 stbtt_uint32 loc = tabledir + 16*i;
1008 if (stbtt_tag(data+loc+0, tag))
1009 return ttULONG(data+loc+8);
1014 STBTT_DEF
int stbtt_GetFontOffsetForIndex(
const unsigned char *font_collection,
int index)
1017 if (stbtt__isfont(font_collection))
1018 return index == 0 ? 0 : -1;
1021 if (stbtt_tag(font_collection,
"ttcf")) {
1023 if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
1024 stbtt_int32 n = ttLONG(font_collection+8);
1027 return ttULONG(font_collection+12+index*4);
1033 STBTT_DEF
int stbtt_InitFont(
stbtt_fontinfo *info,
const unsigned char *data2,
int fontstart)
1035 stbtt_uint8 *data = (stbtt_uint8 *) data2;
1036 stbtt_uint32 cmap, t;
1037 stbtt_int32 i,numTables;
1040 info->fontstart = fontstart;
1042 cmap = stbtt__find_table(data, fontstart,
"cmap");
1043 info->loca = stbtt__find_table(data, fontstart,
"loca");
1044 info->head = stbtt__find_table(data, fontstart,
"head");
1045 info->glyf = stbtt__find_table(data, fontstart,
"glyf");
1046 info->hhea = stbtt__find_table(data, fontstart,
"hhea");
1047 info->hmtx = stbtt__find_table(data, fontstart,
"hmtx");
1048 info->kern = stbtt__find_table(data, fontstart,
"kern");
1049 if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
1052 t = stbtt__find_table(data, fontstart,
"maxp");
1054 info->numGlyphs = ttUSHORT(data+t+4);
1056 info->numGlyphs = 0xffff;
1061 numTables = ttUSHORT(data + cmap + 2);
1062 info->index_map = 0;
1063 for (i=0; i < numTables; ++i) {
1064 stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
1066 switch(ttUSHORT(data+encoding_record)) {
1067 case STBTT_PLATFORM_ID_MICROSOFT:
1068 switch (ttUSHORT(data+encoding_record+2)) {
1069 case STBTT_MS_EID_UNICODE_BMP:
1070 case STBTT_MS_EID_UNICODE_FULL:
1072 info->index_map = cmap + ttULONG(data+encoding_record+4);
1076 case STBTT_PLATFORM_ID_UNICODE:
1079 info->index_map = cmap + ttULONG(data+encoding_record+4);
1083 if (info->index_map == 0)
1086 info->indexToLocFormat = ttUSHORT(data+info->head + 50);
1090 STBTT_DEF
int stbtt_FindGlyphIndex(
const stbtt_fontinfo *info,
int unicode_codepoint)
1092 stbtt_uint8 *data = info->data;
1093 stbtt_uint32 index_map = info->index_map;
1095 stbtt_uint16 format = ttUSHORT(data + index_map + 0);
1097 stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
1098 if (unicode_codepoint < bytes-6)
1099 return ttBYTE(data + index_map + 6 + unicode_codepoint);
1101 }
else if (format == 6) {
1102 stbtt_uint32 first = ttUSHORT(data + index_map + 6);
1103 stbtt_uint32 count = ttUSHORT(data + index_map + 8);
1104 if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)
1105 return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
1107 }
else if (format == 2) {
1110 }
else if (format == 4) {
1111 stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1;
1112 stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1;
1113 stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10);
1114 stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1;
1117 stbtt_uint32 endCount = index_map + 14;
1118 stbtt_uint32 search = endCount;
1120 if (unicode_codepoint > 0xffff)
1125 if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))
1126 search += rangeShift*2;
1130 while (entrySelector) {
1133 end = ttUSHORT(data + search + searchRange*2);
1134 if (unicode_codepoint > end)
1135 search += searchRange*2;
1141 stbtt_uint16 offset, start;
1142 stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1);
1144 STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));
1145 start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
1146 if (unicode_codepoint < start)
1149 offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
1151 return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
1153 return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
1155 }
else if (format == 12 || format == 13) {
1156 stbtt_uint32 ngroups = ttULONG(data+index_map+12);
1157 stbtt_int32 low,high;
1158 low = 0; high = (stbtt_int32)ngroups;
1160 while (low < high) {
1161 stbtt_int32 mid = low + ((high-low) >> 1);
1162 stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12);
1163 stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4);
1164 if ((stbtt_uint32) unicode_codepoint < start_char)
1166 else if ((stbtt_uint32) unicode_codepoint > end_char)
1169 stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);
1171 return start_glyph + unicode_codepoint-start_char;
1185 return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);
1188 static void stbtt_setvertex(
stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy)
1191 v->x = (stbtt_int16) x;
1192 v->y = (stbtt_int16) y;
1193 v->cx = (stbtt_int16) cx;
1194 v->cy = (stbtt_int16) cy;
1197 static int stbtt__GetGlyfOffset(
const stbtt_fontinfo *info,
int glyph_index)
1201 if (glyph_index >= info->numGlyphs)
return -1;
1202 if (info->indexToLocFormat >= 2)
return -1;
1204 if (info->indexToLocFormat == 0) {
1205 g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
1206 g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
1208 g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4);
1209 g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4);
1212 return g1==g2 ? -1 : g1;
1215 STBTT_DEF
int stbtt_GetGlyphBox(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1)
1217 int g = stbtt__GetGlyfOffset(info, glyph_index);
1218 if (g < 0)
return 0;
1220 if (x0) *x0 = ttSHORT(info->data + g + 2);
1221 if (y0) *y0 = ttSHORT(info->data + g + 4);
1222 if (x1) *x1 = ttSHORT(info->data + g + 6);
1223 if (y1) *y1 = ttSHORT(info->data + g + 8);
1227 STBTT_DEF
int stbtt_GetCodepointBox(
const stbtt_fontinfo *info,
int codepoint,
int *x0,
int *y0,
int *x1,
int *y1)
1229 return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1);
1232 STBTT_DEF
int stbtt_IsGlyphEmpty(
const stbtt_fontinfo *info,
int glyph_index)
1234 stbtt_int16 numberOfContours;
1235 int g = stbtt__GetGlyfOffset(info, glyph_index);
1236 if (g < 0)
return 1;
1237 numberOfContours = ttSHORT(info->data + g);
1238 return numberOfContours == 0;
1241 static int stbtt__close_shape(
stbtt_vertex *vertices,
int num_vertices,
int was_off,
int start_off,
1242 stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
1246 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
1247 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy);
1250 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy);
1252 stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0);
1254 return num_vertices;
1259 stbtt_int16 numberOfContours;
1260 stbtt_uint8 *endPtsOfContours;
1261 stbtt_uint8 *data = info->data;
1264 int g = stbtt__GetGlyfOffset(info, glyph_index);
1268 if (g < 0)
return 0;
1270 numberOfContours = ttSHORT(data + g);
1272 if (numberOfContours > 0) {
1273 stbtt_uint8 flags=0,flagcount;
1274 stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
1275 stbtt_int32 x,y,cx,cy,sx,sy, scx,scy;
1276 stbtt_uint8 *points;
1277 endPtsOfContours = (data + g + 10);
1278 ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
1279 points = data + g + 10 + numberOfContours * 2 + 2 + ins;
1281 n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2);
1283 m = n + 2*numberOfContours;
1284 vertices = (
stbtt_vertex *) STBTT_malloc(m *
sizeof(vertices[0]), info->userdata);
1299 for (i=0; i < n; ++i) {
1300 if (flagcount == 0) {
1303 flagcount = *points++;
1306 vertices[off+i].type = flags;
1311 for (i=0; i < n; ++i) {
1312 flags = vertices[off+i].type;
1314 stbtt_int16 dx = *points++;
1315 x += (flags & 16) ? dx : -dx;
1317 if (!(flags & 16)) {
1318 x = x + (stbtt_int16) (points[0]*256 + points[1]);
1322 vertices[off+i].x = (stbtt_int16) x;
1327 for (i=0; i < n; ++i) {
1328 flags = vertices[off+i].type;
1330 stbtt_int16 dy = *points++;
1331 y += (flags & 32) ? dy : -dy;
1333 if (!(flags & 32)) {
1334 y = y + (stbtt_int16) (points[0]*256 + points[1]);
1338 vertices[off+i].y = (stbtt_int16) y;
1343 sx = sy = cx = cy = scx = scy = 0;
1344 for (i=0; i < n; ++i) {
1345 flags = vertices[off+i].type;
1346 x = (stbtt_int16) vertices[off+i].x;
1347 y = (stbtt_int16) vertices[off+i].y;
1349 if (next_move == i) {
1351 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
1354 start_off = !(flags & 1);
1360 if (!(vertices[off+i+1].type & 1)) {
1362 sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1;
1363 sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1;
1366 sx = (stbtt_int32) vertices[off+i+1].x;
1367 sy = (stbtt_int32) vertices[off+i+1].y;
1374 stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0);
1376 next_move = 1 + ttUSHORT(endPtsOfContours+j*2);
1381 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
1387 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy);
1389 stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0);
1394 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
1395 }
else if (numberOfContours == -1) {
1398 stbtt_uint8 *comp = data + g + 10;
1402 stbtt_uint16 flags, gidx;
1403 int comp_num_verts = 0, i;
1405 float mtx[6] = {1,0,0,1,0,0}, m, n;
1407 flags = ttSHORT(comp); comp+=2;
1408 gidx = ttSHORT(comp); comp+=2;
1412 mtx[4] = ttSHORT(comp); comp+=2;
1413 mtx[5] = ttSHORT(comp); comp+=2;
1415 mtx[4] = ttCHAR(comp); comp+=1;
1416 mtx[5] = ttCHAR(comp); comp+=1;
1423 if (flags & (1<<3)) {
1424 mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1425 mtx[1] = mtx[2] = 0;
1426 }
else if (flags & (1<<6)) {
1427 mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
1428 mtx[1] = mtx[2] = 0;
1429 mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1430 }
else if (flags & (1<<7)) {
1431 mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
1432 mtx[1] = ttSHORT(comp)/16384.0f; comp+=2;
1433 mtx[2] = ttSHORT(comp)/16384.0f; comp+=2;
1434 mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1438 m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
1439 n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
1442 comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
1443 if (comp_num_verts > 0) {
1445 for (i = 0; i < comp_num_verts; ++i) {
1447 stbtt_vertex_type x,y;
1449 v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
1450 v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
1452 v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
1453 v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
1458 if (vertices) STBTT_free(vertices, info->userdata);
1459 if (comp_verts) STBTT_free(comp_verts, info->userdata);
1462 if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*
sizeof(
stbtt_vertex));
1463 STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*
sizeof(
stbtt_vertex));
1464 if (vertices) STBTT_free(vertices, info->userdata);
1466 STBTT_free(comp_verts, info->userdata);
1467 num_vertices += comp_num_verts;
1470 more = flags & (1<<5);
1472 }
else if (numberOfContours < 0) {
1479 *pvertices = vertices;
1480 return num_vertices;
1483 STBTT_DEF
void stbtt_GetGlyphHMetrics(
const stbtt_fontinfo *info,
int glyph_index,
int *advanceWidth,
int *leftSideBearing)
1485 stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);
1486 if (glyph_index < numOfLongHorMetrics) {
1487 if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index);
1488 if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
1490 if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
1491 if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
1495 STBTT_DEF
int stbtt_GetGlyphKernAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2)
1497 stbtt_uint8 *data = info->data + info->kern;
1498 stbtt_uint32 needle, straw;
1504 if (ttUSHORT(data+2) < 1)
1506 if (ttUSHORT(data+8) != 1)
1510 r = ttUSHORT(data+10) - 1;
1511 needle = glyph1 << 16 | glyph2;
1514 straw = ttULONG(data+18+(m*6));
1517 else if (needle > straw)
1520 return ttSHORT(data+22+(m*6));
1525 STBTT_DEF
int stbtt_GetCodepointKernAdvance(
const stbtt_fontinfo *info,
int ch1,
int ch2)
1529 return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2));
1532 STBTT_DEF
void stbtt_GetCodepointHMetrics(
const stbtt_fontinfo *info,
int codepoint,
int *advanceWidth,
int *leftSideBearing)
1534 stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing);
1537 STBTT_DEF
void stbtt_GetFontVMetrics(
const stbtt_fontinfo *info,
int *ascent,
int *descent,
int *lineGap)
1539 if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4);
1540 if (descent) *descent = ttSHORT(info->data+info->hhea + 6);
1541 if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);
1544 STBTT_DEF
void stbtt_GetFontBoundingBox(
const stbtt_fontinfo *info,
int *x0,
int *y0,
int *x1,
int *y1)
1546 *x0 = ttSHORT(info->data + info->head + 36);
1547 *y0 = ttSHORT(info->data + info->head + 38);
1548 *x1 = ttSHORT(info->data + info->head + 40);
1549 *y1 = ttSHORT(info->data + info->head + 42);
1552 STBTT_DEF
float stbtt_ScaleForPixelHeight(
const stbtt_fontinfo *info,
float height)
1554 int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
1555 return (
float) height / fheight;
1558 STBTT_DEF
float stbtt_ScaleForMappingEmToPixels(
const stbtt_fontinfo *info,
float pixels)
1560 int unitsPerEm = ttUSHORT(info->data + info->head + 18);
1561 return pixels / unitsPerEm;
1566 STBTT_free(v, info->userdata);
1574 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)
1576 int x0=0,y0=0,x1,y1;
1577 if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
1585 if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x);
1586 if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
1587 if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x);
1588 if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y);
1592 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)
1594 stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
1597 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)
1599 stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1);
1602 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)
1604 stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1);
1611 typedef struct stbtt__hheap_chunk
1613 struct stbtt__hheap_chunk *next;
1614 } stbtt__hheap_chunk;
1616 typedef struct stbtt__hheap
1618 struct stbtt__hheap_chunk *head;
1620 int num_remaining_in_head_chunk;
1623 static void *stbtt__hheap_alloc(stbtt__hheap *hh,
size_t size,
void *userdata)
1625 if (hh->first_free) {
1626 void *p = hh->first_free;
1627 hh->first_free = * (
void **) p;
1630 if (hh->num_remaining_in_head_chunk == 0) {
1631 int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
1632 stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(
sizeof(stbtt__hheap_chunk) + size * count, userdata);
1637 hh->num_remaining_in_head_chunk = count;
1639 --hh->num_remaining_in_head_chunk;
1640 return (
char *) (hh->head) + size * hh->num_remaining_in_head_chunk;
1644 static void stbtt__hheap_free(stbtt__hheap *hh,
void *p)
1646 *(
void **) p = hh->first_free;
1650 static void stbtt__hheap_cleanup(stbtt__hheap *hh,
void *userdata)
1652 stbtt__hheap_chunk *c = hh->head;
1654 stbtt__hheap_chunk *n = c->next;
1655 STBTT_free(c, userdata);
1660 typedef struct stbtt__edge {
1666 typedef struct stbtt__active_edge
1668 struct stbtt__active_edge *next;
1669 #if STBTT_RASTERIZER_VERSION==1
1673 #elif STBTT_RASTERIZER_VERSION==2
1679 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
1681 } stbtt__active_edge;
1683 #if STBTT_RASTERIZER_VERSION == 1
1684 #define STBTT_FIXSHIFT 10
1685 #define STBTT_FIX (1 << STBTT_FIXSHIFT)
1686 #define STBTT_FIXMASK (STBTT_FIX-1)
1688 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
void *userdata)
1690 stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
1691 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
1692 STBTT_assert(z != NULL);
1697 z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);
1699 z->dx = STBTT_ifloor(STBTT_FIX * dxdy);
1701 z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - e->y0));
1702 z->x -= off_x * STBTT_FIX;
1706 z->direction = e->invert ? 1 : -1;
1709 #elif STBTT_RASTERIZER_VERSION == 2
1710 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
void *userdata)
1712 stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
1713 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
1714 STBTT_assert(z != NULL);
1718 z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f;
1719 z->fx = e->x0 + dxdy * (start_point - e->y0);
1721 z->direction = e->invert ? 1.0f : -1.0f;
1728 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
1731 #if STBTT_RASTERIZER_VERSION == 1
1735 static void stbtt__fill_active_edges(
unsigned char *scanline,
int len, stbtt__active_edge *e,
int max_weight)
1743 x0 = e->x; w += e->direction;
1745 int x1 = e->x; w += e->direction;
1748 int i = x0 >> STBTT_FIXSHIFT;
1749 int j = x1 >> STBTT_FIXSHIFT;
1751 if (i < len && j >= 0) {
1754 scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT);
1757 scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);
1762 scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);
1766 for (++i; i < j; ++i)
1767 scanline[i] = scanline[i] + (stbtt_uint8) max_weight;
1777 static void stbtt__rasterize_sorted_edges(
stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
int off_y,
void *userdata)
1779 stbtt__hheap hh = { 0, 0, 0 };
1780 stbtt__active_edge *active = NULL;
1782 int max_weight = (255 / vsubsample);
1784 unsigned char scanline_data[512], *scanline;
1786 if (result->w > 512)
1787 scanline = (
unsigned char *) STBTT_malloc(result->w, userdata);
1789 scanline = scanline_data;
1791 y = off_y * vsubsample;
1792 e[n].y0 = (off_y + result->h) * (
float) vsubsample + 1;
1794 while (j < result->h) {
1795 STBTT_memset(scanline, 0, result->w);
1796 for (s=0; s < vsubsample; ++s) {
1798 float scan_y = y + 0.5f;
1799 stbtt__active_edge **step = &active;
1804 stbtt__active_edge * z = *step;
1805 if (z->ey <= scan_y) {
1807 STBTT_assert(z->direction);
1809 stbtt__hheap_free(&hh, z);
1812 step = &((*step)->next);
1820 while (*step && (*step)->next) {
1821 if ((*step)->x > (*step)->next->x) {
1822 stbtt__active_edge *t = *step;
1823 stbtt__active_edge *q = t->next;
1830 step = &(*step)->next;
1832 if (!changed)
break;
1836 while (e->y0 <= scan_y) {
1837 if (e->y1 > scan_y) {
1838 stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);
1843 else if (z->x < active->x) {
1849 stbtt__active_edge *p = active;
1850 while (p->next && p->next->x < z->x)
1863 stbtt__fill_active_edges(scanline, result->w, active, max_weight);
1867 STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);
1871 stbtt__hheap_cleanup(&hh, userdata);
1873 if (scanline != scanline_data)
1874 STBTT_free(scanline, userdata);
1877 #elif STBTT_RASTERIZER_VERSION == 2
1881 static void stbtt__handle_clipped_edge(
float *scanline,
int x, stbtt__active_edge *e,
float x0,
float y0,
float x1,
float y1)
1883 if (y0 == y1)
return;
1884 STBTT_assert(y0 < y1);
1885 STBTT_assert(e->sy <= e->ey);
1886 if (y0 > e->ey)
return;
1887 if (y1 < e->sy)
return;
1889 x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
1893 x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
1898 STBTT_assert(x1 <= x+1);
1900 STBTT_assert(x1 >= x);
1902 STBTT_assert(x1 <= x);
1904 STBTT_assert(x1 >= x+1);
1906 STBTT_assert(x1 >= x && x1 <= x+1);
1908 if (x0 <= x && x1 <= x)
1909 scanline[x] += e->direction * (y1-y0);
1910 else if (x0 >= x+1 && x1 >= x+1)
1913 STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
1914 scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2);
1918 static void stbtt__fill_active_edges_new(
float *scanline,
float *scanline_fill,
int len, stbtt__active_edge *e,
float y_top)
1920 float y_bottom = y_top+1;
1926 STBTT_assert(e->ey >= y_top);
1932 stbtt__handle_clipped_edge(scanline,(
int) x0,e, x0,y_top, x0,y_bottom);
1933 stbtt__handle_clipped_edge(scanline_fill-1,(
int) x0+1,e, x0,y_top, x0,y_bottom);
1935 stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
1942 float x_top, x_bottom;
1945 STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);
1950 if (e->sy > y_top) {
1951 x_top = x0 + dx * (e->sy - y_top);
1957 if (e->ey < y_bottom) {
1958 x_bottom = x0 + dx * (e->ey - y_top);
1965 if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {
1968 if ((
int) x_top == (int) x_bottom) {
1971 int x = (int) x_top;
1973 STBTT_assert(x >= 0 && x < len);
1974 scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height;
1975 scanline_fill[x] += e->direction * height;
1978 float y_crossing, step, sign, area;
1980 if (x_top > x_bottom) {
1983 sy0 = y_bottom - (sy0 - y_top);
1984 sy1 = y_bottom - (sy1 - y_top);
1985 t = sy0, sy0 = sy1, sy1 = t;
1986 t = x_bottom, x_bottom = x_top, x_top = t;
1989 t = x0, x0 = xb, xb = t;
1993 x2 = (int) x_bottom;
1995 y_crossing = (x1+1 - x0) * dy + y_top;
1997 sign = e->direction;
1999 area = sign * (y_crossing-sy0);
2001 scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2);
2004 for (x = x1+1; x < x2; ++x) {
2005 scanline[x] += area + step/2;
2008 y_crossing += dy * (x2 - (x1+1));
2010 STBTT_assert(STBTT_fabs(area) <= 1.01f);
2012 scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing);
2014 scanline_fill[x2] += sign * (sy1-sy0);
2022 for (x=0; x < len; ++x) {
2038 float x1 = (float) (x);
2039 float x2 = (float) (x+1);
2041 float y3 = y_bottom;
2047 y1 = (x - x0) / dx + y_top;
2048 y2 = (x+1 - x0) / dx + y_top;
2050 if (x0 < x1 && x3 > x2) {
2051 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2052 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2);
2053 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2054 }
else if (x3 < x1 && x0 > x2) {
2055 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2056 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1);
2057 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2058 }
else if (x0 < x1 && x3 > x1) {
2059 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2060 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2061 }
else if (x3 < x1 && x0 > x1) {
2062 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2063 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2064 }
else if (x0 < x2 && x3 > x2) {
2065 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2066 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2067 }
else if (x3 < x2 && x0 > x2) {
2068 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2069 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2071 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3);
2081 static void stbtt__rasterize_sorted_edges(
stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
int off_y,
void *userdata)
2083 stbtt__hheap hh = { 0, 0, 0 };
2084 stbtt__active_edge *active = NULL;
2086 float scanline_data[129], *scanline, *scanline2;
2088 STBTT__NOTUSED(vsubsample);
2091 scanline = (
float *) STBTT_malloc((result->w*2+1) *
sizeof(float), userdata);
2093 scanline = scanline_data;
2095 scanline2 = scanline + result->w;
2098 e[n].y0 = (float) (off_y + result->h) + 1;
2100 while (j < result->h) {
2102 float scan_y_top = y + 0.0f;
2103 float scan_y_bottom = y + 1.0f;
2104 stbtt__active_edge **step = &active;
2106 STBTT_memset(scanline , 0, result->w*
sizeof(scanline[0]));
2107 STBTT_memset(scanline2, 0, (result->w+1)*
sizeof(scanline[0]));
2112 stbtt__active_edge * z = *step;
2113 if (z->ey <= scan_y_top) {
2115 STBTT_assert(z->direction);
2117 stbtt__hheap_free(&hh, z);
2119 step = &((*step)->next);
2124 while (e->y0 <= scan_y_bottom) {
2125 if (e->y0 != e->y1) {
2126 stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
2128 STBTT_assert(z->ey >= scan_y_top);
2139 stbtt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
2143 for (i=0; i < result->w; ++i) {
2146 sum += scanline2[i];
2147 k = scanline[i] + sum;
2148 k = (float) STBTT_fabs(k)*255 + 0.5f;
2150 if (m > 255) m = 255;
2151 result->pixels[j*result->stride + i] = (
unsigned char) m;
2157 stbtt__active_edge *z = *step;
2159 step = &((*step)->next);
2166 stbtt__hheap_cleanup(&hh, userdata);
2168 if (scanline != scanline_data)
2169 STBTT_free(scanline, userdata);
2172 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2175 #define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0)
2177 static void stbtt__sort_edges_ins_sort(stbtt__edge *p,
int n)
2180 for (i=1; i < n; ++i) {
2181 stbtt__edge t = p[i], *a = &t;
2184 stbtt__edge *b = &p[j-1];
2185 int c = STBTT__COMPARE(a,b);
2195 static void stbtt__sort_edges_quicksort(stbtt__edge *p,
int n)
2200 int c01,c12,c,m,i,j;
2204 c01 = STBTT__COMPARE(&p[0],&p[m]);
2205 c12 = STBTT__COMPARE(&p[m],&p[n-1]);
2210 c = STBTT__COMPARE(&p[0],&p[n-1]);
2213 z = (c == c12) ? 0 : n-1;
2231 if (!STBTT__COMPARE(&p[i], &p[0]))
break;
2234 if (!STBTT__COMPARE(&p[0], &p[j]))
break;
2247 stbtt__sort_edges_quicksort(p,j);
2251 stbtt__sort_edges_quicksort(p+i, n-i);
2257 static void stbtt__sort_edges(stbtt__edge *p,
int n)
2259 stbtt__sort_edges_quicksort(p, n);
2260 stbtt__sort_edges_ins_sort(p, n);
2268 static void stbtt__rasterize(
stbtt__bitmap *result, stbtt__point *pts,
int *wcount,
int windings,
float scale_x,
float scale_y,
float shift_x,
float shift_y,
int off_x,
int off_y,
int invert,
void *userdata)
2270 float y_scale_inv = invert ? -scale_y : scale_y;
2273 #if STBTT_RASTERIZER_VERSION == 1
2274 int vsubsample = result->h < 8 ? 15 : 5;
2275 #elif STBTT_RASTERIZER_VERSION == 2
2278 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2284 for (i=0; i < windings; ++i)
2287 e = (stbtt__edge *) STBTT_malloc(
sizeof(*e) * (n+1), userdata);
2292 for (i=0; i < windings; ++i) {
2293 stbtt__point *p = pts + m;
2296 for (k=0; k < wcount[i]; j=k++) {
2299 if (p[j].y == p[k].y)
2303 if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
2307 e[n].x0 = p[a].x * scale_x + shift_x;
2308 e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
2309 e[n].x1 = p[b].x * scale_x + shift_x;
2310 e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
2317 stbtt__sort_edges(e, n);
2320 stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
2322 STBTT_free(e, userdata);
2325 static void stbtt__add_point(stbtt__point *points,
int n,
float x,
float y)
2327 if (!points)
return;
2333 static int stbtt__tesselate_curve(stbtt__point *points,
int *num_points,
float x0,
float y0,
float x1,
float y1,
float x2,
float y2,
float objspace_flatness_squared,
int n)
2336 float mx = (x0 + 2*x1 + x2)/4;
2337 float my = (y0 + 2*y1 + y2)/4;
2339 float dx = (x0+x2)/2 - mx;
2340 float dy = (y0+y2)/2 - my;
2343 if (dx*dx+dy*dy > objspace_flatness_squared) {
2344 stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
2345 stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
2347 stbtt__add_point(points, *num_points,x2,y2);
2348 *num_points = *num_points+1;
2354 static stbtt__point *stbtt_FlattenCurves(
stbtt_vertex *vertices,
int num_verts,
float objspace_flatness,
int **contour_lengths,
int *num_contours,
void *userdata)
2356 stbtt__point *points=0;
2359 float objspace_flatness_squared = objspace_flatness * objspace_flatness;
2360 int i,n=0,start=0, pass;
2363 for (i=0; i < num_verts; ++i)
2364 if (vertices[i].type == STBTT_vmove)
2368 if (n == 0)
return 0;
2370 *contour_lengths = (
int *) STBTT_malloc(
sizeof(**contour_lengths) * n, userdata);
2372 if (*contour_lengths == 0) {
2378 for (pass=0; pass < 2; ++pass) {
2381 points = (stbtt__point *) STBTT_malloc(num_points *
sizeof(points[0]), userdata);
2382 if (points == NULL)
goto error;
2386 for (i=0; i < num_verts; ++i) {
2387 switch (vertices[i].type) {
2391 (*contour_lengths)[n] = num_points - start;
2395 x = vertices[i].x, y = vertices[i].y;
2396 stbtt__add_point(points, num_points++, x,y);
2399 x = vertices[i].x, y = vertices[i].y;
2400 stbtt__add_point(points, num_points++, x, y);
2403 stbtt__tesselate_curve(points, &num_points, x,y,
2404 vertices[i].cx, vertices[i].cy,
2405 vertices[i].x, vertices[i].y,
2406 objspace_flatness_squared, 0);
2407 x = vertices[i].x, y = vertices[i].y;
2411 (*contour_lengths)[n] = num_points - start;
2416 STBTT_free(points, userdata);
2417 STBTT_free(*contour_lengths, userdata);
2418 *contour_lengths = 0;
2423 STBTT_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)
2425 float scale = scale_x > scale_y ? scale_y : scale_x;
2426 int winding_count, *winding_lengths;
2427 stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
2429 stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata);
2430 STBTT_free(winding_lengths, userdata);
2431 STBTT_free(windings, userdata);
2435 STBTT_DEF
void stbtt_FreeBitmap(
unsigned char *bitmap,
void *userdata)
2437 STBTT_free(bitmap, userdata);
2440 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)
2442 int ix0,iy0,ix1,iy1;
2445 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
2447 if (scale_x == 0) scale_x = scale_y;
2450 STBTT_free(vertices, info->userdata);
2456 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1);
2459 gbm.w = (ix1 - ix0);
2460 gbm.h = (iy1 - iy0);
2463 if (width ) *width = gbm.w;
2464 if (height) *height = gbm.h;
2465 if (xoff ) *xoff = ix0;
2466 if (yoff ) *yoff = iy0;
2468 if (gbm.w && gbm.h) {
2469 gbm.pixels = (
unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata);
2473 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
2476 STBTT_free(vertices, info->userdata);
2480 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)
2482 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);
2485 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)
2489 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
2492 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0);
2493 gbm.pixels = output;
2496 gbm.stride = out_stride;
2499 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata);
2501 STBTT_free(vertices, info->userdata);
2504 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)
2506 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph);
2509 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)
2511 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff);
2514 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)
2516 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint));
2519 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)
2521 return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff);
2524 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)
2526 stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint);
2535 STBTT_DEF
int stbtt_BakeFontBitmap(
const unsigned char *data,
int offset,
2537 unsigned char *pixels,
int pw,
int ph,
2538 int first_char,
int num_chars,
2542 int x,y,bottom_y, i;
2545 if (!stbtt_InitFont(&f, data, offset))
2547 STBTT_memset(pixels, 0, pw*ph);
2551 scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
2553 for (i=0; i < num_chars; ++i) {
2554 int advance, lsb, x0,y0,x1,y1,gw,gh;
2555 int g = stbtt_FindGlyphIndex(&f, first_char + i);
2556 stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
2557 stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);
2560 if (x + gw + 1 >= pw)
2561 y = bottom_y, x = 1;
2562 if (y + gh + 1 >= ph)
2564 STBTT_assert(x+gw < pw);
2565 STBTT_assert(y+gh < ph);
2566 stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g);
2567 chardata[i].x0 = (stbtt_int16) x;
2568 chardata[i].y0 = (stbtt_int16) y;
2569 chardata[i].x1 = (stbtt_int16) (x + gw);
2570 chardata[i].y1 = (stbtt_int16) (y + gh);
2571 chardata[i].xadvance = scale * advance;
2572 chardata[i].xoff = (float) x0;
2573 chardata[i].yoff = (float) y0;
2575 if (y+gh+1 > bottom_y)
2581 STBTT_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)
2583 float d3d_bias = opengl_fillrule ? 0 : -0.5f;
2584 float ipw = 1.0f / pw, iph = 1.0f / ph;
2586 int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f);
2587 int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f);
2589 q->x0 = round_x + d3d_bias;
2590 q->y0 = round_y + d3d_bias;
2591 q->x1 = round_x + b->x1 - b->x0 + d3d_bias;
2592 q->y1 = round_y + b->y1 - b->y0 + d3d_bias;
2594 q->s0 = b->x0 * ipw;
2595 q->t0 = b->y0 * iph;
2596 q->s1 = b->x1 * ipw;
2597 q->t1 = b->y1 * iph;
2599 *xpos += b->xadvance;
2607 #ifndef STB_RECT_PACK_VERSION
2609 typedef int stbrp_coord;
2636 int id,w,h,was_packed;
2639 static void stbrp_init_target(stbrp_context *con,
int pw,
int ph, stbrp_node *nodes,
int num_nodes)
2646 STBTT__NOTUSED(nodes);
2647 STBTT__NOTUSED(num_nodes);
2650 static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects,
int num_rects)
2653 for (i=0; i < num_rects; ++i) {
2654 if (con->x + rects[i].w > con->width) {
2656 con->y = con->bottom_y;
2658 if (con->y + rects[i].h > con->height)
2660 rects[i].x = con->x;
2661 rects[i].y = con->y;
2662 rects[i].was_packed = 1;
2663 con->x += rects[i].w;
2664 if (con->y + rects[i].h > con->bottom_y)
2665 con->bottom_y = con->y + rects[i].h;
2667 for ( ; i < num_rects; ++i)
2668 rects[i].was_packed = 0;
2679 STBTT_DEF
int stbtt_PackBegin(
stbtt_pack_context *spc,
unsigned char *pixels,
int pw,
int ph,
int stride_in_bytes,
int padding,
void *alloc_context)
2681 stbrp_context *context = (stbrp_context *) STBTT_malloc(
sizeof(*context) ,alloc_context);
2682 int num_nodes = pw - padding;
2683 stbrp_node *nodes = (stbrp_node *) STBTT_malloc(
sizeof(*nodes ) * num_nodes,alloc_context);
2685 if (context == NULL || nodes == NULL) {
2686 if (context != NULL) STBTT_free(context, alloc_context);
2687 if (nodes != NULL) STBTT_free(nodes , alloc_context);
2691 spc->user_allocator_context = alloc_context;
2694 spc->pixels = pixels;
2695 spc->pack_info = context;
2697 spc->padding = padding;
2698 spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;
2699 spc->h_oversample = 1;
2700 spc->v_oversample = 1;
2702 stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
2705 STBTT_memset(pixels, 0, pw*ph);
2712 STBTT_free(spc->nodes , spc->user_allocator_context);
2713 STBTT_free(spc->pack_info, spc->user_allocator_context);
2716 STBTT_DEF
void stbtt_PackSetOversampling(
stbtt_pack_context *spc,
unsigned int h_oversample,
unsigned int v_oversample)
2718 STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
2719 STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
2720 if (h_oversample <= STBTT_MAX_OVERSAMPLE)
2721 spc->h_oversample = h_oversample;
2722 if (v_oversample <= STBTT_MAX_OVERSAMPLE)
2723 spc->v_oversample = v_oversample;
2726 #define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1)
2728 static void stbtt__h_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
2730 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
2731 int safe_w = w - kernel_width;
2733 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
2734 for (j=0; j < h; ++j) {
2737 STBTT_memset(buffer, 0, kernel_width);
2742 switch (kernel_width) {
2744 for (i=0; i <= safe_w; ++i) {
2745 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2746 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2747 pixels[i] = (
unsigned char) (total / 2);
2751 for (i=0; i <= safe_w; ++i) {
2752 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2753 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2754 pixels[i] = (
unsigned char) (total / 3);
2758 for (i=0; i <= safe_w; ++i) {
2759 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2760 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2761 pixels[i] = (
unsigned char) (total / 4);
2765 for (i=0; i <= safe_w; ++i) {
2766 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2767 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2768 pixels[i] = (
unsigned char) (total / 5);
2772 for (i=0; i <= safe_w; ++i) {
2773 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
2774 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
2775 pixels[i] = (
unsigned char) (total / kernel_width);
2780 for (; i < w; ++i) {
2781 STBTT_assert(pixels[i] == 0);
2782 total -= buffer[i & STBTT__OVER_MASK];
2783 pixels[i] = (
unsigned char) (total / kernel_width);
2786 pixels += stride_in_bytes;
2790 static void stbtt__v_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
2792 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
2793 int safe_h = h - kernel_width;
2795 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
2796 for (j=0; j < w; ++j) {
2799 STBTT_memset(buffer, 0, kernel_width);
2804 switch (kernel_width) {
2806 for (i=0; i <= safe_h; ++i) {
2807 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2808 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2809 pixels[i*stride_in_bytes] = (
unsigned char) (total / 2);
2813 for (i=0; i <= safe_h; ++i) {
2814 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2815 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2816 pixels[i*stride_in_bytes] = (
unsigned char) (total / 3);
2820 for (i=0; i <= safe_h; ++i) {
2821 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2822 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2823 pixels[i*stride_in_bytes] = (
unsigned char) (total / 4);
2827 for (i=0; i <= safe_h; ++i) {
2828 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2829 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2830 pixels[i*stride_in_bytes] = (
unsigned char) (total / 5);
2834 for (i=0; i <= safe_h; ++i) {
2835 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
2836 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
2837 pixels[i*stride_in_bytes] = (
unsigned char) (total / kernel_width);
2842 for (; i < h; ++i) {
2843 STBTT_assert(pixels[i*stride_in_bytes] == 0);
2844 total -= buffer[i & STBTT__OVER_MASK];
2845 pixels[i*stride_in_bytes] = (
unsigned char) (total / kernel_width);
2852 static float stbtt__oversample_shift(
int oversample)
2861 return (
float)-(oversample - 1) / (2.0f * (
float)oversample);
2870 for (i=0; i < num_ranges; ++i) {
2871 float fh = ranges[i].font_size;
2872 float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
2873 ranges[i].h_oversample = (
unsigned char) spc->h_oversample;
2874 ranges[i].v_oversample = (
unsigned char) spc->v_oversample;
2875 for (j=0; j < ranges[i].num_chars; ++j) {
2877 int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
2878 int glyph = stbtt_FindGlyphIndex(info, codepoint);
2879 stbtt_GetGlyphBitmapBoxSubpixel(info,glyph,
2880 scale * spc->h_oversample,
2881 scale * spc->v_oversample,
2884 rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1);
2885 rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1);
2896 int i,j,k, return_value = 1;
2899 int old_h_over = spc->h_oversample;
2900 int old_v_over = spc->v_oversample;
2903 for (i=0; i < num_ranges; ++i) {
2904 float fh = ranges[i].font_size;
2905 float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
2906 float recip_h,recip_v,sub_x,sub_y;
2907 spc->h_oversample = ranges[i].h_oversample;
2908 spc->v_oversample = ranges[i].v_oversample;
2909 recip_h = 1.0f / spc->h_oversample;
2910 recip_v = 1.0f / spc->v_oversample;
2911 sub_x = stbtt__oversample_shift(spc->h_oversample);
2912 sub_y = stbtt__oversample_shift(spc->v_oversample);
2913 for (j=0; j < ranges[i].num_chars; ++j) {
2914 stbrp_rect *r = &rects[k];
2915 if (r->was_packed) {
2917 int advance, lsb, x0,y0,x1,y1;
2918 int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
2919 int glyph = stbtt_FindGlyphIndex(info, codepoint);
2920 stbrp_coord pad = (stbrp_coord) spc->padding;
2927 stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
2928 stbtt_GetGlyphBitmapBox(info, glyph,
2929 scale * spc->h_oversample,
2930 scale * spc->v_oversample,
2932 stbtt_MakeGlyphBitmapSubpixel(info,
2933 spc->pixels + r->x + r->y*spc->stride_in_bytes,
2934 r->w - spc->h_oversample+1,
2935 r->h - spc->v_oversample+1,
2936 spc->stride_in_bytes,
2937 scale * spc->h_oversample,
2938 scale * spc->v_oversample,
2942 if (spc->h_oversample > 1)
2943 stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
2944 r->w, r->h, spc->stride_in_bytes,
2947 if (spc->v_oversample > 1)
2948 stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
2949 r->w, r->h, spc->stride_in_bytes,
2952 bc->x0 = (stbtt_int16) r->x;
2953 bc->y0 = (stbtt_int16) r->y;
2954 bc->x1 = (stbtt_int16) (r->x + r->w);
2955 bc->y1 = (stbtt_int16) (r->y + r->h);
2956 bc->xadvance = scale * advance;
2957 bc->xoff = (float) x0 * recip_h + sub_x;
2958 bc->yoff = (float) y0 * recip_v + sub_y;
2959 bc->xoff2 = (x0 + r->w) * recip_h + sub_x;
2960 bc->yoff2 = (y0 + r->h) * recip_v + sub_y;
2970 spc->h_oversample = old_h_over;
2971 spc->v_oversample = old_v_over;
2973 return return_value;
2976 STBTT_DEF
void stbtt_PackFontRangesPackRects(
stbtt_pack_context *spc, stbrp_rect *rects,
int num_rects)
2978 stbrp_pack_rects((stbrp_context *) spc->pack_info, rects, num_rects);
2984 int i,j,n, return_value = 1;
2989 for (i=0; i < num_ranges; ++i)
2990 for (j=0; j < ranges[i].num_chars; ++j)
2991 ranges[i].chardata_for_range[j].x0 =
2992 ranges[i].chardata_for_range[j].y0 =
2993 ranges[i].chardata_for_range[j].x1 =
2994 ranges[i].chardata_for_range[j].y1 = 0;
2997 for (i=0; i < num_ranges; ++i)
2998 n += ranges[i].num_chars;
3000 rects = (stbrp_rect *) STBTT_malloc(
sizeof(*rects) * n, spc->user_allocator_context);
3004 info.userdata = spc->user_allocator_context;
3005 stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index));
3007 n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);
3009 stbtt_PackFontRangesPackRects(spc, rects, n);
3011 return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);
3013 STBTT_free(rects, spc->user_allocator_context);
3014 return return_value;
3017 STBTT_DEF
int stbtt_PackFontRange(
stbtt_pack_context *spc,
unsigned char *fontdata,
int font_index,
float font_size,
3018 int first_unicode_codepoint_in_range,
int num_chars_in_range,
stbtt_packedchar *chardata_for_range)
3021 range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range;
3022 range.array_of_unicode_codepoints = NULL;
3023 range.num_chars = num_chars_in_range;
3024 range.chardata_for_range = chardata_for_range;
3025 range.font_size = font_size;
3026 return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);
3029 STBTT_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)
3031 float ipw = 1.0f / pw, iph = 1.0f / ph;
3034 if (align_to_integer) {
3035 float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5f);
3036 float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5f);
3039 q->x1 = x + b->xoff2 - b->xoff;
3040 q->y1 = y + b->yoff2 - b->yoff;
3042 q->x0 = *xpos + b->xoff;
3043 q->y0 = *ypos + b->yoff;
3044 q->x1 = *xpos + b->xoff2;
3045 q->y1 = *ypos + b->yoff2;
3048 q->s0 = b->x0 * ipw;
3049 q->t0 = b->y0 * iph;
3050 q->s1 = b->x1 * ipw;
3051 q->t1 = b->y1 * iph;
3053 *xpos += b->xadvance;
3063 static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(
const stbtt_uint8 *s1, stbtt_int32 len1,
const stbtt_uint8 *s2, stbtt_int32 len2)
3069 stbtt_uint16 ch = s2[0]*256 + s2[1];
3071 if (i >= len1)
return -1;
3072 if (s1[i++] != ch)
return -1;
3073 }
else if (ch < 0x800) {
3074 if (i+1 >= len1)
return -1;
3075 if (s1[i++] != 0xc0 + (ch >> 6))
return -1;
3076 if (s1[i++] != 0x80 + (ch & 0x3f))
return -1;
3077 }
else if (ch >= 0xd800 && ch < 0xdc00) {
3079 stbtt_uint16 ch2 = s2[2]*256 + s2[3];
3080 if (i+3 >= len1)
return -1;
3081 c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
3082 if (s1[i++] != 0xf0 + (c >> 18))
return -1;
3083 if (s1[i++] != 0x80 + ((c >> 12) & 0x3f))
return -1;
3084 if (s1[i++] != 0x80 + ((c >> 6) & 0x3f))
return -1;
3085 if (s1[i++] != 0x80 + ((c ) & 0x3f))
return -1;
3088 }
else if (ch >= 0xdc00 && ch < 0xe000) {
3091 if (i+2 >= len1)
return -1;
3092 if (s1[i++] != 0xe0 + (ch >> 12))
return -1;
3093 if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f))
return -1;
3094 if (s1[i++] != 0x80 + ((ch ) & 0x3f))
return -1;
3102 STBTT_DEF
int stbtt_CompareUTF8toUTF16_bigendian(
const char *s1,
int len1,
const char *s2,
int len2)
3104 return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((
const stbtt_uint8*) s1, len1, (
const stbtt_uint8*) s2, len2);
3109 STBTT_DEF
const char *stbtt_GetFontNameString(
const stbtt_fontinfo *font,
int *length,
int platformID,
int encodingID,
int languageID,
int nameID)
3111 stbtt_int32 i,count,stringOffset;
3112 stbtt_uint8 *fc = font->data;
3113 stbtt_uint32 offset = font->fontstart;
3114 stbtt_uint32 nm = stbtt__find_table(fc, offset,
"name");
3115 if (!nm)
return NULL;
3117 count = ttUSHORT(fc+nm+2);
3118 stringOffset = nm + ttUSHORT(fc+nm+4);
3119 for (i=0; i < count; ++i) {
3120 stbtt_uint32 loc = nm + 6 + 12 * i;
3121 if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2)
3122 && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) {
3123 *length = ttUSHORT(fc+loc+8);
3124 return (
const char *) (fc+stringOffset+ttUSHORT(fc+loc+10));
3130 static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)
3133 stbtt_int32 count = ttUSHORT(fc+nm+2);
3134 stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4);
3136 for (i=0; i < count; ++i) {
3137 stbtt_uint32 loc = nm + 6 + 12 * i;
3138 stbtt_int32
id = ttUSHORT(fc+loc+6);
3139 if (
id == target_id) {
3141 stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4);
3144 if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
3145 stbtt_int32 slen = ttUSHORT(fc+loc+8);
3146 stbtt_int32 off = ttUSHORT(fc+loc+10);
3149 stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);
3150 if (matchlen >= 0) {
3152 if (i+1 < count && ttUSHORT(fc+loc+12+6) == next_id && ttUSHORT(fc+loc+12) == platform && ttUSHORT(fc+loc+12+2) == encoding && ttUSHORT(fc+loc+12+4) == language) {
3153 slen = ttUSHORT(fc+loc+12+8);
3154 off = ttUSHORT(fc+loc+12+10);
3156 if (matchlen == nlen)
3158 }
else if (matchlen < nlen && name[matchlen] ==
' ') {
3160 if (stbtt_CompareUTF8toUTF16_bigendian((
char*) (name+matchlen), nlen-matchlen, (
char*)(fc+stringOffset+off),slen))
3165 if (matchlen == nlen)
3177 static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)
3179 stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((
char *) name);
3181 if (!stbtt__isfont(fc+offset))
return 0;
3185 hd = stbtt__find_table(fc, offset,
"head");
3186 if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7))
return 0;
3189 nm = stbtt__find_table(fc, offset,
"name");
3194 if (stbtt__matchpair(fc, nm, name, nlen, 16, -1))
return 1;
3195 if (stbtt__matchpair(fc, nm, name, nlen, 1, -1))
return 1;
3196 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
return 1;
3198 if (stbtt__matchpair(fc, nm, name, nlen, 16, 17))
return 1;
3199 if (stbtt__matchpair(fc, nm, name, nlen, 1, 2))
return 1;
3200 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
return 1;
3206 STBTT_DEF
int stbtt_FindMatchingFont(
const unsigned char *font_collection,
const char *name_utf8, stbtt_int32 flags)
3210 stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
3211 if (off < 0)
return off;
3212 if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags))
3217 #endif // STB_TRUETYPE_IMPLEMENTATION
Definition: stb_truetype.h:468
Definition: stb_truetype.h:484
Definition: stb_truetype.h:558
Definition: stb_truetype.h:611
Definition: stb_truetype.h:514
Definition: stb_truetype.h:640
Definition: stb_truetype.h:735
Definition: stb_truetype.h:813