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];
248 stbtt_bakedchar cdata[96];
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) {
271 stbtt_aligned_quad q;
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
478 unsigned short x0,y0,x1,y1;
479 float xoff,yoff,xadvance;
482 STBTT_DEF
int stbtt_BakeFontBitmap(
const unsigned char *data,
int offset,
484 unsigned char *pixels,
int pw,
int ph,
485 int first_char,
int num_chars,
486 stbtt_bakedchar *chardata);
496 } stbtt_aligned_quad;
498 STBTT_DEF
void stbtt_GetBakedQuad(
const stbtt_bakedchar *chardata,
int pw,
int ph,
500 float *xpos,
float *ypos,
501 stbtt_aligned_quad *q,
502 int opengl_fillrule);
513 STBTT_DEF
void stbtt_GetBakedQuadScaled(
const stbtt_bakedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
float *ypos, stbtt_aligned_quad *q,
float scale,
int opengl_fillrule);
524 unsigned short x0,y0,x1,y1;
525 float xoff,yoff,xadvance;
529 typedef struct stbtt_pack_context stbtt_pack_context;
530 typedef struct stbtt_fontinfo stbtt_fontinfo;
531 #ifndef STB_RECT_PACK_VERSION
532 typedef struct stbrp_rect stbrp_rect;
535 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);
546 STBTT_DEF
void stbtt_PackEnd (stbtt_pack_context *spc);
549 #define STBTT_POINT_SIZE(x) (-(x))
551 STBTT_DEF
int stbtt_PackFontRange(stbtt_pack_context *spc,
unsigned char *fontdata,
int font_index,
float font_size,
552 int first_unicode_char_in_range,
int num_chars_in_range, stbtt_packedchar *chardata_for_range);
569 int first_unicode_codepoint_in_range;
570 int *array_of_unicode_codepoints;
572 stbtt_packedchar *chardata_for_range;
573 unsigned char h_oversample, v_oversample;
576 STBTT_DEF
int stbtt_PackFontRanges(stbtt_pack_context *spc,
unsigned char *fontdata,
int font_index, stbtt_pack_range *ranges,
int num_ranges);
582 STBTT_DEF
void stbtt_PackSetOversampling(stbtt_pack_context *spc,
unsigned int h_oversample,
unsigned int v_oversample);
598 STBTT_DEF
void stbtt_GetPackedQuad(
const stbtt_packedchar *chardata,
int pw,
int ph,
600 float *xpos,
float *ypos,
601 stbtt_aligned_quad *q,
602 int align_to_integer);
604 STBTT_DEF
void stbtt_GetPackedQuadScaled(
const stbtt_packedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
float *ypos, stbtt_aligned_quad *q,
float scale);
607 STBTT_DEF
int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info, stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects);
608 STBTT_DEF
void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects,
int num_rects);
609 STBTT_DEF
int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info, stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects);
622 struct stbtt_pack_context {
623 void *user_allocator_context;
629 unsigned int h_oversample, v_oversample;
630 unsigned char *pixels;
640 STBTT_DEF
int stbtt_GetNumberOfFonts(
const unsigned char *data);
647 STBTT_DEF
int stbtt_GetFontOffsetForIndex(
const unsigned char *data,
int index);
656 struct stbtt_fontinfo
659 unsigned char * data;
664 int loca,head,glyf,hhea,hmtx,kern;
666 int indexToLocFormat;
669 stbtt__buf charstrings;
672 stbtt__buf fontdicts;
676 STBTT_DEF
int stbtt_InitFont(stbtt_fontinfo *info,
const unsigned char *data,
int offset);
688 STBTT_DEF
int stbtt_FindGlyphIndex(
const stbtt_fontinfo *info,
int unicode_codepoint);
700 STBTT_DEF
float stbtt_ScaleForPixelHeight(
const stbtt_fontinfo *info,
float pixels);
708 STBTT_DEF
float stbtt_ScaleForMappingEmToPixels(
const stbtt_fontinfo *info,
float pixels);
713 STBTT_DEF
void stbtt_GetFontVMetrics(
const stbtt_fontinfo *info,
int *ascent,
int *descent,
int *lineGap);
721 STBTT_DEF
void stbtt_GetFontBoundingBox(
const stbtt_fontinfo *info,
int *x0,
int *y0,
int *x1,
int *y1);
724 STBTT_DEF
void stbtt_GetCodepointHMetrics(
const stbtt_fontinfo *info,
int codepoint,
int *advanceWidth,
int *leftSideBearing);
729 STBTT_DEF
int stbtt_GetCodepointKernAdvance(
const stbtt_fontinfo *info,
int ch1,
int ch2);
732 STBTT_DEF
int stbtt_GetCodepointBox(
const stbtt_fontinfo *info,
int codepoint,
int *x0,
int *y0,
int *x1,
int *y1);
735 STBTT_DEF
void stbtt_GetGlyphHMetrics(
const stbtt_fontinfo *info,
int glyph_index,
int *advanceWidth,
int *leftSideBearing);
736 STBTT_DEF
int stbtt_GetGlyphKernAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2);
737 STBTT_DEF
int stbtt_GetGlyphBox(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1);
747 #ifndef STBTT_vmove // you can predefine these to use different values (but why?)
756 #ifndef stbtt_vertex // you can predefine this to use different values
758 #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file
761 stbtt_vertex_type x,y,cx,cy,cx1,cy1;
762 unsigned char type,padding;
766 STBTT_DEF
int stbtt_IsGlyphEmpty(
const stbtt_fontinfo *info,
int glyph_index);
769 STBTT_DEF
int stbtt_GetCodepointShape(
const stbtt_fontinfo *info,
int unicode_codepoint, stbtt_vertex **vertices);
770 STBTT_DEF
int stbtt_GetGlyphShape(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **vertices);
781 STBTT_DEF
void stbtt_FreeShape(
const stbtt_fontinfo *info, stbtt_vertex *vertices);
789 STBTT_DEF
void stbtt_FreeBitmap(
unsigned char *bitmap,
void *userdata);
792 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);
801 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);
805 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);
811 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);
815 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);
822 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);
828 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);
829 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);
830 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);
831 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);
832 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);
833 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);
840 unsigned char *pixels;
844 STBTT_DEF
void stbtt_Rasterize(stbtt__bitmap *result,
845 float flatness_in_pixels,
846 stbtt_vertex *vertices,
848 float scale_x,
float scale_y,
849 float shift_x,
float shift_y,
850 int x_off,
int y_off,
876 STBTT_DEF
int stbtt_FindMatchingFont(
const unsigned char *fontdata,
const char *name,
int flags);
881 #define STBTT_MACSTYLE_DONTCARE 0
882 #define STBTT_MACSTYLE_BOLD 1
883 #define STBTT_MACSTYLE_ITALIC 2
884 #define STBTT_MACSTYLE_UNDERSCORE 4
885 #define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0
887 STBTT_DEF
int stbtt_CompareUTF8toUTF16_bigendian(
const char *s1,
int len1,
const char *s2,
int len2);
891 STBTT_DEF
const char *stbtt_GetFontNameString(
const stbtt_fontinfo *font,
int *length,
int platformID,
int encodingID,
int languageID,
int nameID);
900 STBTT_PLATFORM_ID_UNICODE =0,
901 STBTT_PLATFORM_ID_MAC =1,
902 STBTT_PLATFORM_ID_ISO =2,
903 STBTT_PLATFORM_ID_MICROSOFT =3
907 STBTT_UNICODE_EID_UNICODE_1_0 =0,
908 STBTT_UNICODE_EID_UNICODE_1_1 =1,
909 STBTT_UNICODE_EID_ISO_10646 =2,
910 STBTT_UNICODE_EID_UNICODE_2_0_BMP=3,
911 STBTT_UNICODE_EID_UNICODE_2_0_FULL=4
915 STBTT_MS_EID_SYMBOL =0,
916 STBTT_MS_EID_UNICODE_BMP =1,
917 STBTT_MS_EID_SHIFTJIS =2,
918 STBTT_MS_EID_UNICODE_FULL =10
922 STBTT_MAC_EID_ROMAN =0, STBTT_MAC_EID_ARABIC =4,
923 STBTT_MAC_EID_JAPANESE =1, STBTT_MAC_EID_HEBREW =5,
924 STBTT_MAC_EID_CHINESE_TRAD =2, STBTT_MAC_EID_GREEK =6,
925 STBTT_MAC_EID_KOREAN =3, STBTT_MAC_EID_RUSSIAN =7
930 STBTT_MS_LANG_ENGLISH =0x0409, STBTT_MS_LANG_ITALIAN =0x0410,
931 STBTT_MS_LANG_CHINESE =0x0804, STBTT_MS_LANG_JAPANESE =0x0411,
932 STBTT_MS_LANG_DUTCH =0x0413, STBTT_MS_LANG_KOREAN =0x0412,
933 STBTT_MS_LANG_FRENCH =0x040c, STBTT_MS_LANG_RUSSIAN =0x0419,
934 STBTT_MS_LANG_GERMAN =0x0407, STBTT_MS_LANG_SPANISH =0x0409,
935 STBTT_MS_LANG_HEBREW =0x040d, STBTT_MS_LANG_SWEDISH =0x041D
939 STBTT_MAC_LANG_ENGLISH =0 , STBTT_MAC_LANG_JAPANESE =11,
940 STBTT_MAC_LANG_ARABIC =12, STBTT_MAC_LANG_KOREAN =23,
941 STBTT_MAC_LANG_DUTCH =4 , STBTT_MAC_LANG_RUSSIAN =32,
942 STBTT_MAC_LANG_FRENCH =1 , STBTT_MAC_LANG_SPANISH =6 ,
943 STBTT_MAC_LANG_GERMAN =2 , STBTT_MAC_LANG_SWEDISH =5 ,
944 STBTT_MAC_LANG_HEBREW =10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33,
945 STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19
952 #endif // __STB_INCLUDE_STB_TRUETYPE_H__
961 #ifdef STB_TRUETYPE_IMPLEMENTATION
963 #ifndef STBTT_MAX_OVERSAMPLE
964 #define STBTT_MAX_OVERSAMPLE 8
967 #if STBTT_MAX_OVERSAMPLE > 255
968 #error "STBTT_MAX_OVERSAMPLE cannot be > 255"
971 typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1];
973 #ifndef STBTT_RASTERIZER_VERSION
974 #define STBTT_RASTERIZER_VERSION 2
978 #define STBTT__NOTUSED(v) (void)(v)
980 #define STBTT__NOTUSED(v) (void)sizeof(v)
988 static stbtt_uint8 stbtt__buf_get8(stbtt__buf *b)
990 if (b->cursor >= b->size)
992 return b->data[b->cursor++];
995 static stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b)
997 if (b->cursor >= b->size)
999 return b->data[b->cursor];
1002 static void stbtt__buf_seek(stbtt__buf *b,
int o)
1004 STBTT_assert(!(o > b->size || o < 0));
1005 b->cursor = (o > b->size || o < 0) ? b->size : o;
1008 static void stbtt__buf_skip(stbtt__buf *b,
int o)
1010 stbtt__buf_seek(b, b->cursor + o);
1013 static stbtt_uint32 stbtt__buf_get(stbtt__buf *b,
int n)
1017 STBTT_assert(n >= 1 && n <= 4);
1018 for (i = 0; i < n; i++)
1019 v = (v << 8) | stbtt__buf_get8(b);
1023 static stbtt__buf stbtt__new_buf(
const void *p,
size_t size)
1026 STBTT_assert(size < 0x40000000);
1027 r.data = (stbtt_uint8*) p;
1028 r.size = (int) size;
1033 #define stbtt__buf_get16(b) stbtt__buf_get((b), 2)
1034 #define stbtt__buf_get32(b) stbtt__buf_get((b), 4)
1036 static stbtt__buf stbtt__buf_range(
const stbtt__buf *b,
int o,
int s)
1038 stbtt__buf r = stbtt__new_buf(NULL, 0);
1039 if (o < 0 || s < 0 || o > b->size || s > b->size - o)
return r;
1040 r.data = b->data + o;
1045 static stbtt__buf stbtt__cff_get_index(stbtt__buf *b)
1047 int count, start, offsize;
1049 count = stbtt__buf_get16(b);
1051 offsize = stbtt__buf_get8(b);
1052 STBTT_assert(offsize >= 1 && offsize <= 4);
1053 stbtt__buf_skip(b, offsize * count);
1054 stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);
1056 return stbtt__buf_range(b, start, b->cursor - start);
1059 static stbtt_uint32 stbtt__cff_int(stbtt__buf *b)
1061 int b0 = stbtt__buf_get8(b);
1062 if (b0 >= 32 && b0 <= 246)
return b0 - 139;
1063 else if (b0 >= 247 && b0 <= 250)
return (b0 - 247)*256 + stbtt__buf_get8(b) + 108;
1064 else if (b0 >= 251 && b0 <= 254)
return -(b0 - 251)*256 - stbtt__buf_get8(b) - 108;
1065 else if (b0 == 28)
return stbtt__buf_get16(b);
1066 else if (b0 == 29)
return stbtt__buf_get32(b);
1071 static void stbtt__cff_skip_operand(stbtt__buf *b) {
1072 int v, b0 = stbtt__buf_peek8(b);
1073 STBTT_assert(b0 >= 28);
1075 stbtt__buf_skip(b, 1);
1076 while (b->cursor < b->size) {
1077 v = stbtt__buf_get8(b);
1078 if ((v & 0xF) == 0xF || (v >> 4) == 0xF)
1086 static stbtt__buf stbtt__dict_get(stbtt__buf *b,
int key)
1088 stbtt__buf_seek(b, 0);
1089 while (b->cursor < b->size) {
1090 int start = b->cursor, end, op;
1091 while (stbtt__buf_peek8(b) >= 28)
1092 stbtt__cff_skip_operand(b);
1094 op = stbtt__buf_get8(b);
1095 if (op == 12) op = stbtt__buf_get8(b) | 0x100;
1096 if (op == key)
return stbtt__buf_range(b, start, end-start);
1098 return stbtt__buf_range(b, 0, 0);
1101 static void stbtt__dict_get_ints(stbtt__buf *b,
int key,
int outcount, stbtt_uint32 *out)
1104 stbtt__buf operands = stbtt__dict_get(b, key);
1105 for (i = 0; i < outcount && operands.cursor < operands.size; i++)
1106 out[i] = stbtt__cff_int(&operands);
1109 static int stbtt__cff_index_count(stbtt__buf *b)
1111 stbtt__buf_seek(b, 0);
1112 return stbtt__buf_get16(b);
1115 static stbtt__buf stbtt__cff_index_get(stbtt__buf b,
int i)
1117 int count, offsize, start, end;
1118 stbtt__buf_seek(&b, 0);
1119 count = stbtt__buf_get16(&b);
1120 offsize = stbtt__buf_get8(&b);
1121 STBTT_assert(i >= 0 && i < count);
1122 STBTT_assert(offsize >= 1 && offsize <= 4);
1123 stbtt__buf_skip(&b, i*offsize);
1124 start = stbtt__buf_get(&b, offsize);
1125 end = stbtt__buf_get(&b, offsize);
1126 return stbtt__buf_range(&b, 2+(count+1)*offsize+start, end - start);
1137 #define ttBYTE(p) (* (stbtt_uint8 *) (p))
1138 #define ttCHAR(p) (* (stbtt_int8 *) (p))
1139 #define ttFixed(p) ttLONG(p)
1141 static stbtt_uint16 ttUSHORT(stbtt_uint8 *p) {
return p[0]*256 + p[1]; }
1142 static stbtt_int16 ttSHORT(stbtt_uint8 *p) {
return p[0]*256 + p[1]; }
1143 static stbtt_uint32 ttULONG(stbtt_uint8 *p) {
return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
1144 static stbtt_int32 ttLONG(stbtt_uint8 *p) {
return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
1146 #define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
1147 #define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3])
1149 static int stbtt__isfont(stbtt_uint8 *font)
1152 if (stbtt_tag4(font,
'1',0,0,0))
return 1;
1153 if (stbtt_tag(font,
"typ1"))
return 1;
1154 if (stbtt_tag(font,
"OTTO"))
return 1;
1155 if (stbtt_tag4(font, 0,1,0,0))
return 1;
1156 if (stbtt_tag(font,
"true"))
return 1;
1161 static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart,
const char *tag)
1163 stbtt_int32 num_tables = ttUSHORT(data+fontstart+4);
1164 stbtt_uint32 tabledir = fontstart + 12;
1166 for (i=0; i < num_tables; ++i) {
1167 stbtt_uint32 loc = tabledir + 16*i;
1168 if (stbtt_tag(data+loc+0, tag))
1169 return ttULONG(data+loc+8);
1174 static int stbtt_GetFontOffsetForIndex_internal(
unsigned char *font_collection,
int index)
1177 if (stbtt__isfont(font_collection))
1178 return index == 0 ? 0 : -1;
1181 if (stbtt_tag(font_collection,
"ttcf")) {
1183 if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
1184 stbtt_int32 n = ttLONG(font_collection+8);
1187 return ttULONG(font_collection+12+index*4);
1193 static int stbtt_GetNumberOfFonts_internal(
unsigned char *font_collection)
1196 if (stbtt__isfont(font_collection))
1200 if (stbtt_tag(font_collection,
"ttcf")) {
1202 if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
1203 return ttLONG(font_collection+8);
1209 static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict)
1211 stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 };
1213 stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);
1214 if (!private_loc[1] || !private_loc[0])
return stbtt__new_buf(NULL, 0);
1215 pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);
1216 stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);
1217 if (!subrsoff)
return stbtt__new_buf(NULL, 0);
1218 stbtt__buf_seek(&cff, private_loc[1]+subrsoff);
1219 return stbtt__cff_get_index(&cff);
1222 static int stbtt_InitFont_internal(stbtt_fontinfo *info,
unsigned char *data,
int fontstart)
1224 stbtt_uint32 cmap, t;
1225 stbtt_int32 i,numTables;
1228 info->fontstart = fontstart;
1229 info->cff = stbtt__new_buf(NULL, 0);
1231 cmap = stbtt__find_table(data, fontstart,
"cmap");
1232 info->loca = stbtt__find_table(data, fontstart,
"loca");
1233 info->head = stbtt__find_table(data, fontstart,
"head");
1234 info->glyf = stbtt__find_table(data, fontstart,
"glyf");
1235 info->hhea = stbtt__find_table(data, fontstart,
"hhea");
1236 info->hmtx = stbtt__find_table(data, fontstart,
"hmtx");
1237 info->kern = stbtt__find_table(data, fontstart,
"kern");
1239 if (!cmap || !info->head || !info->hhea || !info->hmtx)
1243 if (!info->loca)
return 0;
1246 stbtt__buf b, topdict, topdictidx;
1247 stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;
1250 cff = stbtt__find_table(data, fontstart,
"CFF ");
1253 info->fontdicts = stbtt__new_buf(NULL, 0);
1254 info->fdselect = stbtt__new_buf(NULL, 0);
1257 info->cff = stbtt__new_buf(data+cff, 512*1024*1024);
1261 stbtt__buf_skip(&b, 2);
1262 stbtt__buf_seek(&b, stbtt__buf_get8(&b));
1266 stbtt__cff_get_index(&b);
1267 topdictidx = stbtt__cff_get_index(&b);
1268 topdict = stbtt__cff_index_get(topdictidx, 0);
1269 stbtt__cff_get_index(&b);
1270 info->gsubrs = stbtt__cff_get_index(&b);
1272 stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);
1273 stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
1274 stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);
1275 stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);
1276 info->subrs = stbtt__get_subrs(b, topdict);
1279 if (cstype != 2)
return 0;
1280 if (charstrings == 0)
return 0;
1284 if (!fdselectoff)
return 0;
1285 stbtt__buf_seek(&b, fdarrayoff);
1286 info->fontdicts = stbtt__cff_get_index(&b);
1287 info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size-fdselectoff);
1290 stbtt__buf_seek(&b, charstrings);
1291 info->charstrings = stbtt__cff_get_index(&b);
1294 t = stbtt__find_table(data, fontstart,
"maxp");
1296 info->numGlyphs = ttUSHORT(data+t+4);
1298 info->numGlyphs = 0xffff;
1303 numTables = ttUSHORT(data + cmap + 2);
1304 info->index_map = 0;
1305 for (i=0; i < numTables; ++i) {
1306 stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
1308 switch(ttUSHORT(data+encoding_record)) {
1309 case STBTT_PLATFORM_ID_MICROSOFT:
1310 switch (ttUSHORT(data+encoding_record+2)) {
1311 case STBTT_MS_EID_UNICODE_BMP:
1312 case STBTT_MS_EID_UNICODE_FULL:
1314 info->index_map = cmap + ttULONG(data+encoding_record+4);
1318 case STBTT_PLATFORM_ID_UNICODE:
1321 info->index_map = cmap + ttULONG(data+encoding_record+4);
1325 if (info->index_map == 0)
1328 info->indexToLocFormat = ttUSHORT(data+info->head + 50);
1332 STBTT_DEF
int stbtt_FindGlyphIndex(
const stbtt_fontinfo *info,
int unicode_codepoint)
1334 stbtt_uint8 *data = info->data;
1335 stbtt_uint32 index_map = info->index_map;
1337 stbtt_uint16 format = ttUSHORT(data + index_map + 0);
1339 stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
1340 if (unicode_codepoint < bytes-6)
1341 return ttBYTE(data + index_map + 6 + unicode_codepoint);
1343 }
else if (format == 6) {
1344 stbtt_uint32 first = ttUSHORT(data + index_map + 6);
1345 stbtt_uint32 count = ttUSHORT(data + index_map + 8);
1346 if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)
1347 return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
1349 }
else if (format == 2) {
1352 }
else if (format == 4) {
1353 stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1;
1354 stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1;
1355 stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10);
1356 stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1;
1359 stbtt_uint32 endCount = index_map + 14;
1360 stbtt_uint32 search = endCount;
1362 if (unicode_codepoint > 0xffff)
1367 if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))
1368 search += rangeShift*2;
1372 while (entrySelector) {
1375 end = ttUSHORT(data + search + searchRange*2);
1376 if (unicode_codepoint > end)
1377 search += searchRange*2;
1383 stbtt_uint16 offset, start;
1384 stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1);
1386 STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));
1387 start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
1388 if (unicode_codepoint < start)
1391 offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
1393 return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
1395 return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
1397 }
else if (format == 12 || format == 13) {
1398 stbtt_uint32 ngroups = ttULONG(data+index_map+12);
1399 stbtt_int32 low,high;
1400 low = 0; high = (stbtt_int32)ngroups;
1402 while (low < high) {
1403 stbtt_int32 mid = low + ((high-low) >> 1);
1404 stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12);
1405 stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4);
1406 if ((stbtt_uint32) unicode_codepoint < start_char)
1408 else if ((stbtt_uint32) unicode_codepoint > end_char)
1411 stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);
1413 return start_glyph + unicode_codepoint-start_char;
1425 STBTT_DEF
int stbtt_GetCodepointShape(
const stbtt_fontinfo *info,
int unicode_codepoint, stbtt_vertex **vertices)
1427 return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);
1430 static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy)
1433 v->x = (stbtt_int16) x;
1434 v->y = (stbtt_int16) y;
1435 v->cx = (stbtt_int16) cx;
1436 v->cy = (stbtt_int16) cy;
1439 static int stbtt__GetGlyfOffset(
const stbtt_fontinfo *info,
int glyph_index)
1443 STBTT_assert(!info->cff.size);
1445 if (glyph_index >= info->numGlyphs)
return -1;
1446 if (info->indexToLocFormat >= 2)
return -1;
1448 if (info->indexToLocFormat == 0) {
1449 g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
1450 g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
1452 g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4);
1453 g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4);
1456 return g1==g2 ? -1 : g1;
1459 static int stbtt__GetGlyphInfoT2(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1);
1461 STBTT_DEF
int stbtt_GetGlyphBox(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1)
1463 if (info->cff.size) {
1464 stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);
1466 int g = stbtt__GetGlyfOffset(info, glyph_index);
1467 if (g < 0)
return 0;
1469 if (x0) *x0 = ttSHORT(info->data + g + 2);
1470 if (y0) *y0 = ttSHORT(info->data + g + 4);
1471 if (x1) *x1 = ttSHORT(info->data + g + 6);
1472 if (y1) *y1 = ttSHORT(info->data + g + 8);
1477 STBTT_DEF
int stbtt_GetCodepointBox(
const stbtt_fontinfo *info,
int codepoint,
int *x0,
int *y0,
int *x1,
int *y1)
1479 return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1);
1482 STBTT_DEF
int stbtt_IsGlyphEmpty(
const stbtt_fontinfo *info,
int glyph_index)
1484 stbtt_int16 numberOfContours;
1487 return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0;
1488 g = stbtt__GetGlyfOffset(info, glyph_index);
1489 if (g < 0)
return 1;
1490 numberOfContours = ttSHORT(info->data + g);
1491 return numberOfContours == 0;
1494 static int stbtt__close_shape(stbtt_vertex *vertices,
int num_vertices,
int was_off,
int start_off,
1495 stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
1499 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
1500 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy);
1503 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy);
1505 stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0);
1507 return num_vertices;
1510 static int stbtt__GetGlyphShapeTT(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
1512 stbtt_int16 numberOfContours;
1513 stbtt_uint8 *endPtsOfContours;
1514 stbtt_uint8 *data = info->data;
1515 stbtt_vertex *vertices=0;
1517 int g = stbtt__GetGlyfOffset(info, glyph_index);
1521 if (g < 0)
return 0;
1523 numberOfContours = ttSHORT(data + g);
1525 if (numberOfContours > 0) {
1526 stbtt_uint8 flags=0,flagcount;
1527 stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
1528 stbtt_int32 x,y,cx,cy,sx,sy, scx,scy;
1529 stbtt_uint8 *points;
1530 endPtsOfContours = (data + g + 10);
1531 ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
1532 points = data + g + 10 + numberOfContours * 2 + 2 + ins;
1534 n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2);
1536 m = n + 2*numberOfContours;
1537 vertices = (stbtt_vertex *) STBTT_malloc(m *
sizeof(vertices[0]), info->userdata);
1552 for (i=0; i < n; ++i) {
1553 if (flagcount == 0) {
1556 flagcount = *points++;
1559 vertices[off+i].type = flags;
1564 for (i=0; i < n; ++i) {
1565 flags = vertices[off+i].type;
1567 stbtt_int16 dx = *points++;
1568 x += (flags & 16) ? dx : -dx;
1570 if (!(flags & 16)) {
1571 x = x + (stbtt_int16) (points[0]*256 + points[1]);
1575 vertices[off+i].x = (stbtt_int16) x;
1580 for (i=0; i < n; ++i) {
1581 flags = vertices[off+i].type;
1583 stbtt_int16 dy = *points++;
1584 y += (flags & 32) ? dy : -dy;
1586 if (!(flags & 32)) {
1587 y = y + (stbtt_int16) (points[0]*256 + points[1]);
1591 vertices[off+i].y = (stbtt_int16) y;
1596 sx = sy = cx = cy = scx = scy = 0;
1597 for (i=0; i < n; ++i) {
1598 flags = vertices[off+i].type;
1599 x = (stbtt_int16) vertices[off+i].x;
1600 y = (stbtt_int16) vertices[off+i].y;
1602 if (next_move == i) {
1604 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
1607 start_off = !(flags & 1);
1613 if (!(vertices[off+i+1].type & 1)) {
1615 sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1;
1616 sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1;
1619 sx = (stbtt_int32) vertices[off+i+1].x;
1620 sy = (stbtt_int32) vertices[off+i+1].y;
1627 stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0);
1629 next_move = 1 + ttUSHORT(endPtsOfContours+j*2);
1634 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
1640 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy);
1642 stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0);
1647 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
1648 }
else if (numberOfContours == -1) {
1651 stbtt_uint8 *comp = data + g + 10;
1655 stbtt_uint16 flags, gidx;
1656 int comp_num_verts = 0, i;
1657 stbtt_vertex *comp_verts = 0, *tmp = 0;
1658 float mtx[6] = {1,0,0,1,0,0}, m, n;
1660 flags = ttSHORT(comp); comp+=2;
1661 gidx = ttSHORT(comp); comp+=2;
1665 mtx[4] = ttSHORT(comp); comp+=2;
1666 mtx[5] = ttSHORT(comp); comp+=2;
1668 mtx[4] = ttCHAR(comp); comp+=1;
1669 mtx[5] = ttCHAR(comp); comp+=1;
1676 if (flags & (1<<3)) {
1677 mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1678 mtx[1] = mtx[2] = 0;
1679 }
else if (flags & (1<<6)) {
1680 mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
1681 mtx[1] = mtx[2] = 0;
1682 mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1683 }
else if (flags & (1<<7)) {
1684 mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
1685 mtx[1] = ttSHORT(comp)/16384.0f; comp+=2;
1686 mtx[2] = ttSHORT(comp)/16384.0f; comp+=2;
1687 mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1691 m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
1692 n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
1695 comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
1696 if (comp_num_verts > 0) {
1698 for (i = 0; i < comp_num_verts; ++i) {
1699 stbtt_vertex* v = &comp_verts[i];
1700 stbtt_vertex_type x,y;
1702 v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
1703 v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
1705 v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
1706 v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
1709 tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*
sizeof(stbtt_vertex), info->userdata);
1711 if (vertices) STBTT_free(vertices, info->userdata);
1712 if (comp_verts) STBTT_free(comp_verts, info->userdata);
1715 if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*
sizeof(stbtt_vertex));
1716 STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*
sizeof(stbtt_vertex));
1717 if (vertices) STBTT_free(vertices, info->userdata);
1719 STBTT_free(comp_verts, info->userdata);
1720 num_vertices += comp_num_verts;
1723 more = flags & (1<<5);
1725 }
else if (numberOfContours < 0) {
1732 *pvertices = vertices;
1733 return num_vertices;
1740 float first_x, first_y;
1742 stbtt_int32 min_x, max_x, min_y, max_y;
1744 stbtt_vertex *pvertices;
1748 #define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0}
1750 static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y)
1752 if (x > c->max_x || !c->started) c->max_x = x;
1753 if (y > c->max_y || !c->started) c->max_y = y;
1754 if (x < c->min_x || !c->started) c->min_x = x;
1755 if (y < c->min_y || !c->started) c->min_y = y;
1759 static void stbtt__csctx_v(stbtt__csctx *c, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy, stbtt_int32 cx1, stbtt_int32 cy1)
1762 stbtt__track_vertex(c, x, y);
1763 if (type == STBTT_vcubic) {
1764 stbtt__track_vertex(c, cx, cy);
1765 stbtt__track_vertex(c, cx1, cy1);
1768 stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);
1769 c->pvertices[c->num_vertices].cx1 = (stbtt_int16) cx1;
1770 c->pvertices[c->num_vertices].cy1 = (stbtt_int16) cy1;
1775 static void stbtt__csctx_close_shape(stbtt__csctx *ctx)
1777 if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)
1778 stbtt__csctx_v(ctx, STBTT_vline, (
int)ctx->first_x, (
int)ctx->first_y, 0, 0, 0, 0);
1781 static void stbtt__csctx_rmove_to(stbtt__csctx *ctx,
float dx,
float dy)
1783 stbtt__csctx_close_shape(ctx);
1784 ctx->first_x = ctx->x = ctx->x + dx;
1785 ctx->first_y = ctx->y = ctx->y + dy;
1786 stbtt__csctx_v(ctx, STBTT_vmove, (
int)ctx->x, (
int)ctx->y, 0, 0, 0, 0);
1789 static void stbtt__csctx_rline_to(stbtt__csctx *ctx,
float dx,
float dy)
1793 stbtt__csctx_v(ctx, STBTT_vline, (
int)ctx->x, (
int)ctx->y, 0, 0, 0, 0);
1796 static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx,
float dx1,
float dy1,
float dx2,
float dy2,
float dx3,
float dy3)
1798 float cx1 = ctx->x + dx1;
1799 float cy1 = ctx->y + dy1;
1800 float cx2 = cx1 + dx2;
1801 float cy2 = cy1 + dy2;
1804 stbtt__csctx_v(ctx, STBTT_vcubic, (
int)ctx->x, (
int)ctx->y, (
int)cx1, (
int)cy1, (
int)cx2, (
int)cy2);
1807 static stbtt__buf stbtt__get_subr(stbtt__buf idx,
int n)
1809 int count = stbtt__cff_index_count(&idx);
1813 else if (count >= 1240)
1816 if (n < 0 || n >= count)
1817 return stbtt__new_buf(NULL, 0);
1818 return stbtt__cff_index_get(idx, n);
1821 static stbtt__buf stbtt__cid_get_glyph_subrs(
const stbtt_fontinfo *info,
int glyph_index)
1823 stbtt__buf fdselect = info->fdselect;
1824 int nranges, start, end, v, fmt, fdselector = -1, i;
1826 stbtt__buf_seek(&fdselect, 0);
1827 fmt = stbtt__buf_get8(&fdselect);
1830 stbtt__buf_skip(&fdselect, glyph_index);
1831 fdselector = stbtt__buf_get8(&fdselect);
1832 }
else if (fmt == 3) {
1833 nranges = stbtt__buf_get16(&fdselect);
1834 start = stbtt__buf_get16(&fdselect);
1835 for (i = 0; i < nranges; i++) {
1836 v = stbtt__buf_get8(&fdselect);
1837 end = stbtt__buf_get16(&fdselect);
1838 if (glyph_index >= start && glyph_index < end) {
1845 if (fdselector == -1) stbtt__new_buf(NULL, 0);
1846 return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));
1849 static int stbtt__run_charstring(
const stbtt_fontinfo *info,
int glyph_index, stbtt__csctx *c)
1851 int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;
1852 int has_subrs = 0, clear_stack;
1854 stbtt__buf subr_stack[10], subrs = info->subrs, b;
1857 #define STBTT__CSERR(s) (0)
1860 b = stbtt__cff_index_get(info->charstrings, glyph_index);
1861 while (b.cursor < b.size) {
1864 b0 = stbtt__buf_get8(&b);
1870 maskbits += (sp / 2);
1872 stbtt__buf_skip(&b, (maskbits + 7) / 8);
1879 maskbits += (sp / 2);
1884 if (sp < 2)
return STBTT__CSERR(
"rmoveto stack");
1885 stbtt__csctx_rmove_to(c, s[sp-2], s[sp-1]);
1889 if (sp < 1)
return STBTT__CSERR(
"vmoveto stack");
1890 stbtt__csctx_rmove_to(c, 0, s[sp-1]);
1894 if (sp < 1)
return STBTT__CSERR(
"hmoveto stack");
1895 stbtt__csctx_rmove_to(c, s[sp-1], 0);
1899 if (sp < 2)
return STBTT__CSERR(
"rlineto stack");
1900 for (; i + 1 < sp; i += 2)
1901 stbtt__csctx_rline_to(c, s[i], s[i+1]);
1908 if (sp < 1)
return STBTT__CSERR(
"vlineto stack");
1911 if (sp < 1)
return STBTT__CSERR(
"hlineto stack");
1914 stbtt__csctx_rline_to(c, s[i], 0);
1918 stbtt__csctx_rline_to(c, 0, s[i]);
1924 if (sp < 4)
return STBTT__CSERR(
"hvcurveto stack");
1927 if (sp < 4)
return STBTT__CSERR(
"vhcurveto stack");
1929 if (i + 3 >= sp)
break;
1930 stbtt__csctx_rccurve_to(c, 0, s[i], s[i+1], s[i+2], s[i+3], (sp - i == 5) ? s[i + 4] : 0.0f);
1933 if (i + 3 >= sp)
break;
1934 stbtt__csctx_rccurve_to(c, s[i], 0, s[i+1], s[i+2], (sp - i == 5) ? s[i+4] : 0.0f, s[i+3]);
1940 if (sp < 6)
return STBTT__CSERR(
"rcurveline stack");
1941 for (; i + 5 < sp; i += 6)
1942 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
1946 if (sp < 8)
return STBTT__CSERR(
"rcurveline stack");
1947 for (; i + 5 < sp - 2; i += 6)
1948 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
1949 if (i + 1 >= sp)
return STBTT__CSERR(
"rcurveline stack");
1950 stbtt__csctx_rline_to(c, s[i], s[i+1]);
1954 if (sp < 8)
return STBTT__CSERR(
"rlinecurve stack");
1955 for (; i + 1 < sp - 6; i += 2)
1956 stbtt__csctx_rline_to(c, s[i], s[i+1]);
1957 if (i + 5 >= sp)
return STBTT__CSERR(
"rlinecurve stack");
1958 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
1963 if (sp < 4)
return STBTT__CSERR(
"(vv|hh)curveto stack");
1965 if (sp & 1) { f = s[i]; i++; }
1966 for (; i + 3 < sp; i += 4) {
1968 stbtt__csctx_rccurve_to(c, s[i], f, s[i+1], s[i+2], s[i+3], 0.0);
1970 stbtt__csctx_rccurve_to(c, f, s[i], s[i+1], s[i+2], 0.0, s[i+3]);
1977 if (info->fdselect.size)
1978 subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);
1983 if (sp < 1)
return STBTT__CSERR(
"call(g|)subr stack");
1985 if (subr_stack_height >= 10)
return STBTT__CSERR(
"recursion limit");
1986 subr_stack[subr_stack_height++] = b;
1987 b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v);
1988 if (b.size == 0)
return STBTT__CSERR(
"subr not found");
1994 if (subr_stack_height <= 0)
return STBTT__CSERR(
"return outside subr");
1995 b = subr_stack[--subr_stack_height];
2000 stbtt__csctx_close_shape(c);
2004 float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;
2006 int b1 = stbtt__buf_get8(&b);
2011 if (sp < 7)
return STBTT__CSERR(
"hflex stack");
2019 stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);
2020 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);
2024 if (sp < 13)
return STBTT__CSERR(
"flex stack");
2038 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
2039 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
2043 if (sp < 9)
return STBTT__CSERR(
"hflex1 stack");
2053 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);
2054 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1+dy2+dy5));
2058 if (sp < 11)
return STBTT__CSERR(
"flex1 stack");
2070 dx = dx1+dx2+dx3+dx4+dx5;
2071 dy = dy1+dy2+dy3+dy4+dy5;
2072 if (STBTT_fabs(dx) > STBTT_fabs(dy))
2076 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
2077 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
2081 return STBTT__CSERR(
"unimplemented");
2086 if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254))
2087 return STBTT__CSERR(
"reserved operator");
2091 f = (float)stbtt__buf_get32(&b) / 0x10000;
2093 stbtt__buf_skip(&b, -1);
2094 f = (float)(stbtt_int16)stbtt__cff_int(&b);
2096 if (sp >= 48)
return STBTT__CSERR(
"push stack overflow");
2101 if (clear_stack) sp = 0;
2103 return STBTT__CSERR(
"no endchar");
2108 static int stbtt__GetGlyphShapeT2(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
2111 stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);
2112 stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);
2113 if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {
2114 *pvertices = (stbtt_vertex*)STBTT_malloc(count_ctx.num_vertices*
sizeof(stbtt_vertex), info->userdata);
2115 output_ctx.pvertices = *pvertices;
2116 if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {
2117 STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);
2118 return output_ctx.num_vertices;
2125 static int stbtt__GetGlyphInfoT2(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1)
2127 stbtt__csctx c = STBTT__CSCTX_INIT(1);
2128 int r = stbtt__run_charstring(info, glyph_index, &c);
2130 *x0 = r ? c.min_x : 0;
2131 *y0 = r ? c.min_y : 0;
2132 *x1 = r ? c.max_x : 0;
2133 *y1 = r ? c.max_y : 0;
2135 return r ? c.num_vertices : 0;
2138 STBTT_DEF
int stbtt_GetGlyphShape(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
2140 if (!info->cff.size)
2141 return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);
2143 return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);
2146 STBTT_DEF
void stbtt_GetGlyphHMetrics(
const stbtt_fontinfo *info,
int glyph_index,
int *advanceWidth,
int *leftSideBearing)
2148 stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);
2149 if (glyph_index < numOfLongHorMetrics) {
2150 if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index);
2151 if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
2153 if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
2154 if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
2158 STBTT_DEF
int stbtt_GetGlyphKernAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2)
2160 stbtt_uint8 *data = info->data + info->kern;
2161 stbtt_uint32 needle, straw;
2167 if (ttUSHORT(data+2) < 1)
2169 if (ttUSHORT(data+8) != 1)
2173 r = ttUSHORT(data+10) - 1;
2174 needle = glyph1 << 16 | glyph2;
2177 straw = ttULONG(data+18+(m*6));
2180 else if (needle > straw)
2183 return ttSHORT(data+22+(m*6));
2188 STBTT_DEF
int stbtt_GetCodepointKernAdvance(
const stbtt_fontinfo *info,
int ch1,
int ch2)
2192 return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2));
2195 STBTT_DEF
void stbtt_GetCodepointHMetrics(
const stbtt_fontinfo *info,
int codepoint,
int *advanceWidth,
int *leftSideBearing)
2197 stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing);
2200 STBTT_DEF
void stbtt_GetFontVMetrics(
const stbtt_fontinfo *info,
int *ascent,
int *descent,
int *lineGap)
2202 if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4);
2203 if (descent) *descent = ttSHORT(info->data+info->hhea + 6);
2204 if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);
2207 STBTT_DEF
void stbtt_GetFontBoundingBox(
const stbtt_fontinfo *info,
int *x0,
int *y0,
int *x1,
int *y1)
2209 *x0 = ttSHORT(info->data + info->head + 36);
2210 *y0 = ttSHORT(info->data + info->head + 38);
2211 *x1 = ttSHORT(info->data + info->head + 40);
2212 *y1 = ttSHORT(info->data + info->head + 42);
2215 STBTT_DEF
float stbtt_ScaleForPixelHeight(
const stbtt_fontinfo *info,
float height)
2217 int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
2218 return (
float) height / fheight;
2221 STBTT_DEF
float stbtt_ScaleForMappingEmToPixels(
const stbtt_fontinfo *info,
float pixels)
2223 int unitsPerEm = ttUSHORT(info->data + info->head + 18);
2224 return pixels / unitsPerEm;
2227 STBTT_DEF
void stbtt_FreeShape(
const stbtt_fontinfo *info, stbtt_vertex *v)
2229 STBTT_free(v, info->userdata);
2237 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)
2239 int x0=0,y0=0,x1,y1;
2240 if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
2248 if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x);
2249 if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
2250 if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x);
2251 if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y);
2255 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)
2257 stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
2260 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)
2262 stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1);
2265 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)
2267 stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1);
2274 typedef struct stbtt__hheap_chunk
2276 struct stbtt__hheap_chunk *next;
2277 } stbtt__hheap_chunk;
2279 typedef struct stbtt__hheap
2281 struct stbtt__hheap_chunk *head;
2283 int num_remaining_in_head_chunk;
2286 static void *stbtt__hheap_alloc(stbtt__hheap *hh,
size_t size,
void *userdata)
2288 if (hh->first_free) {
2289 void *p = hh->first_free;
2290 hh->first_free = * (
void **) p;
2293 if (hh->num_remaining_in_head_chunk == 0) {
2294 int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
2295 stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(
sizeof(stbtt__hheap_chunk) + size * count, userdata);
2300 hh->num_remaining_in_head_chunk = count;
2302 --hh->num_remaining_in_head_chunk;
2303 return (
char *) (hh->head) + size * hh->num_remaining_in_head_chunk;
2307 static void stbtt__hheap_free(stbtt__hheap *hh,
void *p)
2309 *(
void **) p = hh->first_free;
2313 static void stbtt__hheap_cleanup(stbtt__hheap *hh,
void *userdata)
2315 stbtt__hheap_chunk *c = hh->head;
2317 stbtt__hheap_chunk *n = c->next;
2318 STBTT_free(c, userdata);
2323 typedef struct stbtt__edge {
2329 typedef struct stbtt__active_edge
2331 struct stbtt__active_edge *next;
2332 #if STBTT_RASTERIZER_VERSION==1
2336 #elif STBTT_RASTERIZER_VERSION==2
2342 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2344 } stbtt__active_edge;
2346 #if STBTT_RASTERIZER_VERSION == 1
2347 #define STBTT_FIXSHIFT 10
2348 #define STBTT_FIX (1 << STBTT_FIXSHIFT)
2349 #define STBTT_FIXMASK (STBTT_FIX-1)
2351 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
void *userdata)
2353 stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
2354 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
2355 STBTT_assert(z != NULL);
2360 z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);
2362 z->dx = STBTT_ifloor(STBTT_FIX * dxdy);
2364 z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - e->y0));
2365 z->x -= off_x * STBTT_FIX;
2369 z->direction = e->invert ? 1 : -1;
2372 #elif STBTT_RASTERIZER_VERSION == 2
2373 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
void *userdata)
2375 stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
2376 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
2377 STBTT_assert(z != NULL);
2381 z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f;
2382 z->fx = e->x0 + dxdy * (start_point - e->y0);
2384 z->direction = e->invert ? 1.0f : -1.0f;
2391 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2394 #if STBTT_RASTERIZER_VERSION == 1
2398 static void stbtt__fill_active_edges(
unsigned char *scanline,
int len, stbtt__active_edge *e,
int max_weight)
2406 x0 = e->x; w += e->direction;
2408 int x1 = e->x; w += e->direction;
2411 int i = x0 >> STBTT_FIXSHIFT;
2412 int j = x1 >> STBTT_FIXSHIFT;
2414 if (i < len && j >= 0) {
2417 scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT);
2420 scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);
2425 scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);
2429 for (++i; i < j; ++i)
2430 scanline[i] = scanline[i] + (stbtt_uint8) max_weight;
2440 static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
int off_y,
void *userdata)
2442 stbtt__hheap hh = { 0, 0, 0 };
2443 stbtt__active_edge *active = NULL;
2445 int max_weight = (255 / vsubsample);
2447 unsigned char scanline_data[512], *scanline;
2449 if (result->w > 512)
2450 scanline = (
unsigned char *) STBTT_malloc(result->w, userdata);
2452 scanline = scanline_data;
2454 y = off_y * vsubsample;
2455 e[n].y0 = (off_y + result->h) * (
float) vsubsample + 1;
2457 while (j < result->h) {
2458 STBTT_memset(scanline, 0, result->w);
2459 for (s=0; s < vsubsample; ++s) {
2461 float scan_y = y + 0.5f;
2462 stbtt__active_edge **step = &active;
2467 stbtt__active_edge * z = *step;
2468 if (z->ey <= scan_y) {
2470 STBTT_assert(z->direction);
2472 stbtt__hheap_free(&hh, z);
2475 step = &((*step)->next);
2483 while (*step && (*step)->next) {
2484 if ((*step)->x > (*step)->next->x) {
2485 stbtt__active_edge *t = *step;
2486 stbtt__active_edge *q = t->next;
2493 step = &(*step)->next;
2495 if (!changed)
break;
2499 while (e->y0 <= scan_y) {
2500 if (e->y1 > scan_y) {
2501 stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);
2506 else if (z->x < active->x) {
2512 stbtt__active_edge *p = active;
2513 while (p->next && p->next->x < z->x)
2526 stbtt__fill_active_edges(scanline, result->w, active, max_weight);
2530 STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);
2534 stbtt__hheap_cleanup(&hh, userdata);
2536 if (scanline != scanline_data)
2537 STBTT_free(scanline, userdata);
2540 #elif STBTT_RASTERIZER_VERSION == 2
2544 static void stbtt__handle_clipped_edge(
float *scanline,
int x, stbtt__active_edge *e,
float x0,
float y0,
float x1,
float y1)
2546 if (y0 == y1)
return;
2547 STBTT_assert(y0 < y1);
2548 STBTT_assert(e->sy <= e->ey);
2549 if (y0 > e->ey)
return;
2550 if (y1 < e->sy)
return;
2552 x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
2556 x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
2561 STBTT_assert(x1 <= x+1);
2563 STBTT_assert(x1 >= x);
2565 STBTT_assert(x1 <= x);
2567 STBTT_assert(x1 >= x+1);
2569 STBTT_assert(x1 >= x && x1 <= x+1);
2571 if (x0 <= x && x1 <= x)
2572 scanline[x] += e->direction * (y1-y0);
2573 else if (x0 >= x+1 && x1 >= x+1)
2576 STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
2577 scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2);
2581 static void stbtt__fill_active_edges_new(
float *scanline,
float *scanline_fill,
int len, stbtt__active_edge *e,
float y_top)
2583 float y_bottom = y_top+1;
2589 STBTT_assert(e->ey >= y_top);
2595 stbtt__handle_clipped_edge(scanline,(
int) x0,e, x0,y_top, x0,y_bottom);
2596 stbtt__handle_clipped_edge(scanline_fill-1,(
int) x0+1,e, x0,y_top, x0,y_bottom);
2598 stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
2605 float x_top, x_bottom;
2608 STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);
2613 if (e->sy > y_top) {
2614 x_top = x0 + dx * (e->sy - y_top);
2620 if (e->ey < y_bottom) {
2621 x_bottom = x0 + dx * (e->ey - y_top);
2628 if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {
2631 if ((
int) x_top == (int) x_bottom) {
2634 int x = (int) x_top;
2636 STBTT_assert(x >= 0 && x < len);
2637 scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height;
2638 scanline_fill[x] += e->direction * height;
2641 float y_crossing, step, sign, area;
2643 if (x_top > x_bottom) {
2646 sy0 = y_bottom - (sy0 - y_top);
2647 sy1 = y_bottom - (sy1 - y_top);
2648 t = sy0, sy0 = sy1, sy1 = t;
2649 t = x_bottom, x_bottom = x_top, x_top = t;
2652 t = x0, x0 = xb, xb = t;
2656 x2 = (int) x_bottom;
2658 y_crossing = (x1+1 - x0) * dy + y_top;
2660 sign = e->direction;
2662 area = sign * (y_crossing-sy0);
2664 scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2);
2667 for (x = x1+1; x < x2; ++x) {
2668 scanline[x] += area + step/2;
2671 y_crossing += dy * (x2 - (x1+1));
2673 STBTT_assert(STBTT_fabs(area) <= 1.01f);
2675 scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing);
2677 scanline_fill[x2] += sign * (sy1-sy0);
2685 for (x=0; x < len; ++x) {
2701 float x1 = (float) (x);
2702 float x2 = (float) (x+1);
2704 float y3 = y_bottom;
2710 y1 = (x - x0) / dx + y_top;
2711 y2 = (x+1 - x0) / dx + y_top;
2713 if (x0 < x1 && x3 > x2) {
2714 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2715 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2);
2716 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2717 }
else if (x3 < x1 && x0 > x2) {
2718 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2719 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1);
2720 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2721 }
else if (x0 < x1 && x3 > x1) {
2722 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2723 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2724 }
else if (x3 < x1 && x0 > x1) {
2725 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2726 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2727 }
else if (x0 < x2 && x3 > x2) {
2728 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2729 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2730 }
else if (x3 < x2 && x0 > x2) {
2731 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2732 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2734 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3);
2744 static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
int off_y,
void *userdata)
2746 stbtt__hheap hh = { 0, 0, 0 };
2747 stbtt__active_edge *active = NULL;
2749 float scanline_data[129], *scanline, *scanline2;
2751 STBTT__NOTUSED(vsubsample);
2754 scanline = (
float *) STBTT_malloc((result->w*2+1) *
sizeof(float), userdata);
2756 scanline = scanline_data;
2758 scanline2 = scanline + result->w;
2761 e[n].y0 = (float) (off_y + result->h) + 1;
2763 while (j < result->h) {
2765 float scan_y_top = y + 0.0f;
2766 float scan_y_bottom = y + 1.0f;
2767 stbtt__active_edge **step = &active;
2769 STBTT_memset(scanline , 0, result->w*
sizeof(scanline[0]));
2770 STBTT_memset(scanline2, 0, (result->w+1)*
sizeof(scanline[0]));
2775 stbtt__active_edge * z = *step;
2776 if (z->ey <= scan_y_top) {
2778 STBTT_assert(z->direction);
2780 stbtt__hheap_free(&hh, z);
2782 step = &((*step)->next);
2787 while (e->y0 <= scan_y_bottom) {
2788 if (e->y0 != e->y1) {
2789 stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
2791 STBTT_assert(z->ey >= scan_y_top);
2802 stbtt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
2806 for (i=0; i < result->w; ++i) {
2809 sum += scanline2[i];
2810 k = scanline[i] + sum;
2811 k = (float) STBTT_fabs(k)*255 + 0.5f;
2813 if (m > 255) m = 255;
2814 result->pixels[j*result->stride + i] = (
unsigned char) m;
2820 stbtt__active_edge *z = *step;
2822 step = &((*step)->next);
2829 stbtt__hheap_cleanup(&hh, userdata);
2831 if (scanline != scanline_data)
2832 STBTT_free(scanline, userdata);
2835 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2838 #define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0)
2840 static void stbtt__sort_edges_ins_sort(stbtt__edge *p,
int n)
2843 for (i=1; i < n; ++i) {
2844 stbtt__edge t = p[i], *a = &t;
2847 stbtt__edge *b = &p[j-1];
2848 int c = STBTT__COMPARE(a,b);
2858 static void stbtt__sort_edges_quicksort(stbtt__edge *p,
int n)
2863 int c01,c12,c,m,i,j;
2867 c01 = STBTT__COMPARE(&p[0],&p[m]);
2868 c12 = STBTT__COMPARE(&p[m],&p[n-1]);
2873 c = STBTT__COMPARE(&p[0],&p[n-1]);
2876 z = (c == c12) ? 0 : n-1;
2894 if (!STBTT__COMPARE(&p[i], &p[0]))
break;
2897 if (!STBTT__COMPARE(&p[0], &p[j]))
break;
2910 stbtt__sort_edges_quicksort(p,j);
2914 stbtt__sort_edges_quicksort(p+i, n-i);
2920 static void stbtt__sort_edges(stbtt__edge *p,
int n)
2922 stbtt__sort_edges_quicksort(p, n);
2923 stbtt__sort_edges_ins_sort(p, n);
2931 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)
2933 float y_scale_inv = invert ? -scale_y : scale_y;
2936 #if STBTT_RASTERIZER_VERSION == 1
2937 int vsubsample = result->h < 8 ? 15 : 5;
2938 #elif STBTT_RASTERIZER_VERSION == 2
2941 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2947 for (i=0; i < windings; ++i)
2950 e = (stbtt__edge *) STBTT_malloc(
sizeof(*e) * (n+1), userdata);
2955 for (i=0; i < windings; ++i) {
2956 stbtt__point *p = pts + m;
2959 for (k=0; k < wcount[i]; j=k++) {
2962 if (p[j].y == p[k].y)
2966 if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
2970 e[n].x0 = p[a].x * scale_x + shift_x;
2971 e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
2972 e[n].x1 = p[b].x * scale_x + shift_x;
2973 e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
2980 stbtt__sort_edges(e, n);
2983 stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
2985 STBTT_free(e, userdata);
2988 static void stbtt__add_point(stbtt__point *points,
int n,
float x,
float y)
2990 if (!points)
return;
2996 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)
2999 float mx = (x0 + 2*x1 + x2)/4;
3000 float my = (y0 + 2*y1 + y2)/4;
3002 float dx = (x0+x2)/2 - mx;
3003 float dy = (y0+y2)/2 - my;
3006 if (dx*dx+dy*dy > objspace_flatness_squared) {
3007 stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
3008 stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
3010 stbtt__add_point(points, *num_points,x2,y2);
3011 *num_points = *num_points+1;
3016 static void stbtt__tesselate_cubic(stbtt__point *points,
int *num_points,
float x0,
float y0,
float x1,
float y1,
float x2,
float y2,
float x3,
float y3,
float objspace_flatness_squared,
int n)
3027 float longlen = (float) (STBTT_sqrt(dx0*dx0+dy0*dy0)+STBTT_sqrt(dx1*dx1+dy1*dy1)+STBTT_sqrt(dx2*dx2+dy2*dy2));
3028 float shortlen = (float) STBTT_sqrt(dx*dx+dy*dy);
3029 float flatness_squared = longlen*longlen-shortlen*shortlen;
3034 if (flatness_squared > objspace_flatness_squared) {
3035 float x01 = (x0+x1)/2;
3036 float y01 = (y0+y1)/2;
3037 float x12 = (x1+x2)/2;
3038 float y12 = (y1+y2)/2;
3039 float x23 = (x2+x3)/2;
3040 float y23 = (y2+y3)/2;
3042 float xa = (x01+x12)/2;
3043 float ya = (y01+y12)/2;
3044 float xb = (x12+x23)/2;
3045 float yb = (y12+y23)/2;
3047 float mx = (xa+xb)/2;
3048 float my = (ya+yb)/2;
3050 stbtt__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my, objspace_flatness_squared,n+1);
3051 stbtt__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3, objspace_flatness_squared,n+1);
3053 stbtt__add_point(points, *num_points,x3,y3);
3054 *num_points = *num_points+1;
3059 static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices,
int num_verts,
float objspace_flatness,
int **contour_lengths,
int *num_contours,
void *userdata)
3061 stbtt__point *points=0;
3064 float objspace_flatness_squared = objspace_flatness * objspace_flatness;
3065 int i,n=0,start=0, pass;
3068 for (i=0; i < num_verts; ++i)
3069 if (vertices[i].type == STBTT_vmove)
3073 if (n == 0)
return 0;
3075 *contour_lengths = (
int *) STBTT_malloc(
sizeof(**contour_lengths) * n, userdata);
3077 if (*contour_lengths == 0) {
3083 for (pass=0; pass < 2; ++pass) {
3086 points = (stbtt__point *) STBTT_malloc(num_points *
sizeof(points[0]), userdata);
3087 if (points == NULL)
goto error;
3091 for (i=0; i < num_verts; ++i) {
3092 switch (vertices[i].type) {
3096 (*contour_lengths)[n] = num_points - start;
3100 x = vertices[i].x, y = vertices[i].y;
3101 stbtt__add_point(points, num_points++, x,y);
3104 x = vertices[i].x, y = vertices[i].y;
3105 stbtt__add_point(points, num_points++, x, y);
3108 stbtt__tesselate_curve(points, &num_points, x,y,
3109 vertices[i].cx, vertices[i].cy,
3110 vertices[i].x, vertices[i].y,
3111 objspace_flatness_squared, 0);
3112 x = vertices[i].x, y = vertices[i].y;
3115 stbtt__tesselate_cubic(points, &num_points, x,y,
3116 vertices[i].cx, vertices[i].cy,
3117 vertices[i].cx1, vertices[i].cy1,
3118 vertices[i].x, vertices[i].y,
3119 objspace_flatness_squared, 0);
3120 x = vertices[i].x, y = vertices[i].y;
3124 (*contour_lengths)[n] = num_points - start;
3129 STBTT_free(points, userdata);
3130 STBTT_free(*contour_lengths, userdata);
3131 *contour_lengths = 0;
3136 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)
3138 float scale = scale_x > scale_y ? scale_y : scale_x;
3139 int winding_count, *winding_lengths;
3140 stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
3142 stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata);
3143 STBTT_free(winding_lengths, userdata);
3144 STBTT_free(windings, userdata);
3148 STBTT_DEF
void stbtt_FreeBitmap(
unsigned char *bitmap,
void *userdata)
3150 STBTT_free(bitmap, userdata);
3153 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)
3155 int ix0,iy0,ix1,iy1;
3157 stbtt_vertex *vertices;
3158 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
3160 if (scale_x == 0) scale_x = scale_y;
3163 STBTT_free(vertices, info->userdata);
3169 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1);
3172 gbm.w = (ix1 - ix0);
3173 gbm.h = (iy1 - iy0);
3176 if (width ) *width = gbm.w;
3177 if (height) *height = gbm.h;
3178 if (xoff ) *xoff = ix0;
3179 if (yoff ) *yoff = iy0;
3181 if (gbm.w && gbm.h) {
3182 gbm.pixels = (
unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata);
3186 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
3189 STBTT_free(vertices, info->userdata);
3193 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)
3195 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);
3198 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)
3201 stbtt_vertex *vertices;
3202 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
3205 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0);
3206 gbm.pixels = output;
3209 gbm.stride = out_stride;
3212 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata);
3214 STBTT_free(vertices, info->userdata);
3217 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)
3219 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph);
3222 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)
3224 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff);
3227 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)
3229 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint));
3232 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)
3234 return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff);
3237 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)
3239 stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint);
3248 static int stbtt_BakeFontBitmap_internal(
unsigned char *data,
int offset,
3250 unsigned char *pixels,
int pw,
int ph,
3251 int first_char,
int num_chars,
3252 stbtt_bakedchar *chardata)
3255 int x,y,bottom_y, i;
3258 if (!stbtt_InitFont(&f, data, offset))
3260 STBTT_memset(pixels, 0, pw*ph);
3264 scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
3266 for (i=0; i < num_chars; ++i) {
3267 int advance, lsb, x0,y0,x1,y1,gw,gh;
3268 int g = stbtt_FindGlyphIndex(&f, first_char + i);
3269 stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
3270 stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);
3273 if (x + gw + 1 >= pw)
3274 y = bottom_y, x = 1;
3275 if (y + gh + 1 >= ph)
3277 STBTT_assert(x+gw < pw);
3278 STBTT_assert(y+gh < ph);
3279 stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g);
3280 chardata[i].x0 = (stbtt_int16) x;
3281 chardata[i].y0 = (stbtt_int16) y;
3282 chardata[i].x1 = (stbtt_int16) (x + gw);
3283 chardata[i].y1 = (stbtt_int16) (y + gh);
3284 chardata[i].xadvance = scale * advance;
3285 chardata[i].xoff = (float) x0;
3286 chardata[i].yoff = (float) y0;
3288 if (y+gh+1 > bottom_y)
3294 STBTT_DEF
void stbtt_GetBakedQuad(
const stbtt_bakedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
float *ypos, stbtt_aligned_quad *q,
int opengl_fillrule)
3296 float d3d_bias = opengl_fillrule ? 0 : -0.5f;
3297 float ipw = 1.0f / pw, iph = 1.0f / ph;
3298 const stbtt_bakedchar *b = chardata + char_index;
3299 int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f);
3300 int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f);
3302 q->x0 = round_x + d3d_bias;
3303 q->y0 = round_y + d3d_bias;
3304 q->x1 = round_x + b->x1 - b->x0 + d3d_bias;
3305 q->y1 = round_y + b->y1 - b->y0 + d3d_bias;
3307 q->s0 = b->x0 * ipw;
3308 q->t0 = b->y0 * iph;
3309 q->s1 = b->x1 * ipw;
3310 q->t1 = b->y1 * iph;
3312 *xpos += b->xadvance;
3315 STBTT_DEF
void stbtt_GetBakedQuadScaled(
const stbtt_bakedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
float *ypos, stbtt_aligned_quad *q,
float scale,
int opengl_fillrule)
3317 float d3d_bias = opengl_fillrule ? 0 : -0.5f;
3318 float ipw = 1.0f / pw, iph = 1.0f / ph;
3319 const stbtt_bakedchar *b = chardata + char_index;
3320 float round_x = ((*xpos + b->xoff*scale) );
3321 float round_y = ((*ypos + b->yoff*scale) );
3323 q->x0 = round_x + d3d_bias;
3324 q->y0 = round_y + d3d_bias;
3325 q->x1 = round_x + (b->x1 - b->x0)*scale + d3d_bias;
3326 q->y1 = round_y + (b->y1 - b->y0)*scale + d3d_bias;
3328 q->s0 = b->x0 * ipw;
3329 q->t0 = b->y0 * iph;
3330 q->s1 = b->x1 * ipw;
3331 q->t1 = b->y1 * iph;
3333 *xpos += b->xadvance*scale;
3341 #ifndef STB_RECT_PACK_VERSION
3343 typedef int stbrp_coord;
3370 int id,w,h,was_packed;
3373 static void stbrp_init_target(stbrp_context *con,
int pw,
int ph, stbrp_node *nodes,
int num_nodes)
3380 STBTT__NOTUSED(nodes);
3381 STBTT__NOTUSED(num_nodes);
3384 static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects,
int num_rects)
3387 for (i=0; i < num_rects; ++i) {
3388 if (con->x + rects[i].w > con->width) {
3390 con->y = con->bottom_y;
3392 if (con->y + rects[i].h > con->height)
3394 rects[i].x = con->x;
3395 rects[i].y = con->y;
3396 rects[i].was_packed = 1;
3397 con->x += rects[i].w;
3398 if (con->y + rects[i].h > con->bottom_y)
3399 con->bottom_y = con->y + rects[i].h;
3401 for ( ; i < num_rects; ++i)
3402 rects[i].was_packed = 0;
3413 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)
3415 stbrp_context *context = (stbrp_context *) STBTT_malloc(
sizeof(*context) ,alloc_context);
3416 int num_nodes = pw - padding;
3417 stbrp_node *nodes = (stbrp_node *) STBTT_malloc(
sizeof(*nodes ) * num_nodes,alloc_context);
3419 if (context == NULL || nodes == NULL) {
3420 if (context != NULL) STBTT_free(context, alloc_context);
3421 if (nodes != NULL) STBTT_free(nodes , alloc_context);
3425 spc->user_allocator_context = alloc_context;
3428 spc->pixels = pixels;
3429 spc->pack_info = context;
3431 spc->padding = padding;
3432 spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;
3433 spc->h_oversample = 1;
3434 spc->v_oversample = 1;
3436 stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
3439 STBTT_memset(pixels, 0, pw*ph);
3444 STBTT_DEF
void stbtt_PackEnd (stbtt_pack_context *spc)
3446 STBTT_free(spc->nodes , spc->user_allocator_context);
3447 STBTT_free(spc->pack_info, spc->user_allocator_context);
3450 STBTT_DEF
void stbtt_PackSetOversampling(stbtt_pack_context *spc,
unsigned int h_oversample,
unsigned int v_oversample)
3452 STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
3453 STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
3454 if (h_oversample <= STBTT_MAX_OVERSAMPLE)
3455 spc->h_oversample = h_oversample;
3456 if (v_oversample <= STBTT_MAX_OVERSAMPLE)
3457 spc->v_oversample = v_oversample;
3460 #define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1)
3462 static void stbtt__h_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
3464 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
3465 int safe_w = w - kernel_width;
3467 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
3468 for (j=0; j < h; ++j) {
3471 STBTT_memset(buffer, 0, kernel_width);
3476 switch (kernel_width) {
3478 for (i=0; i <= safe_w; ++i) {
3479 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3480 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3481 pixels[i] = (
unsigned char) (total / 2);
3485 for (i=0; i <= safe_w; ++i) {
3486 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3487 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3488 pixels[i] = (
unsigned char) (total / 3);
3492 for (i=0; i <= safe_w; ++i) {
3493 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3494 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3495 pixels[i] = (
unsigned char) (total / 4);
3499 for (i=0; i <= safe_w; ++i) {
3500 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3501 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3502 pixels[i] = (
unsigned char) (total / 5);
3506 for (i=0; i <= safe_w; ++i) {
3507 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3508 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3509 pixels[i] = (
unsigned char) (total / kernel_width);
3514 for (; i < w; ++i) {
3515 STBTT_assert(pixels[i] == 0);
3516 total -= buffer[i & STBTT__OVER_MASK];
3517 pixels[i] = (
unsigned char) (total / kernel_width);
3520 pixels += stride_in_bytes;
3524 static void stbtt__v_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
3526 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
3527 int safe_h = h - kernel_width;
3529 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
3530 for (j=0; j < w; ++j) {
3533 STBTT_memset(buffer, 0, kernel_width);
3538 switch (kernel_width) {
3540 for (i=0; i <= safe_h; ++i) {
3541 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3542 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3543 pixels[i*stride_in_bytes] = (
unsigned char) (total / 2);
3547 for (i=0; i <= safe_h; ++i) {
3548 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3549 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3550 pixels[i*stride_in_bytes] = (
unsigned char) (total / 3);
3554 for (i=0; i <= safe_h; ++i) {
3555 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3556 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3557 pixels[i*stride_in_bytes] = (
unsigned char) (total / 4);
3561 for (i=0; i <= safe_h; ++i) {
3562 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3563 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3564 pixels[i*stride_in_bytes] = (
unsigned char) (total / 5);
3568 for (i=0; i <= safe_h; ++i) {
3569 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3570 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3571 pixels[i*stride_in_bytes] = (
unsigned char) (total / kernel_width);
3576 for (; i < h; ++i) {
3577 STBTT_assert(pixels[i*stride_in_bytes] == 0);
3578 total -= buffer[i & STBTT__OVER_MASK];
3579 pixels[i*stride_in_bytes] = (
unsigned char) (total / kernel_width);
3586 static float stbtt__oversample_shift(
int oversample)
3595 return (
float)-(oversample - 1) / (2.0f * (
float)oversample);
3599 STBTT_DEF
int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info, stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects)
3604 for (i=0; i < num_ranges; ++i) {
3605 float fh = ranges[i].font_size;
3606 float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
3607 ranges[i].h_oversample = (
unsigned char) spc->h_oversample;
3608 ranges[i].v_oversample = (
unsigned char) spc->v_oversample;
3609 for (j=0; j < ranges[i].num_chars; ++j) {
3611 int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
3612 int glyph = stbtt_FindGlyphIndex(info, codepoint);
3613 stbtt_GetGlyphBitmapBoxSubpixel(info,glyph,
3614 scale * spc->h_oversample,
3615 scale * spc->v_oversample,
3618 rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1);
3619 rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1);
3628 STBTT_DEF
int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info, stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects)
3630 int i,j,k, return_value = 1;
3633 int old_h_over = spc->h_oversample;
3634 int old_v_over = spc->v_oversample;
3637 for (i=0; i < num_ranges; ++i) {
3638 float fh = ranges[i].font_size;
3639 float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
3640 float recip_h,recip_v,sub_x,sub_y;
3641 spc->h_oversample = ranges[i].h_oversample;
3642 spc->v_oversample = ranges[i].v_oversample;
3643 recip_h = 1.0f / spc->h_oversample;
3644 recip_v = 1.0f / spc->v_oversample;
3645 sub_x = stbtt__oversample_shift(spc->h_oversample);
3646 sub_y = stbtt__oversample_shift(spc->v_oversample);
3647 for (j=0; j < ranges[i].num_chars; ++j) {
3648 stbrp_rect *r = &rects[k];
3649 if (r->was_packed) {
3650 stbtt_packedchar *bc = &ranges[i].chardata_for_range[j];
3651 int advance, lsb, x0,y0,x1,y1;
3652 int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
3653 int glyph = stbtt_FindGlyphIndex(info, codepoint);
3654 stbrp_coord pad = (stbrp_coord) spc->padding;
3661 stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
3662 stbtt_GetGlyphBitmapBox(info, glyph,
3663 scale * spc->h_oversample,
3664 scale * spc->v_oversample,
3666 stbtt_MakeGlyphBitmapSubpixel(info,
3667 spc->pixels + r->x + r->y*spc->stride_in_bytes,
3668 r->w - spc->h_oversample+1,
3669 r->h - spc->v_oversample+1,
3670 spc->stride_in_bytes,
3671 scale * spc->h_oversample,
3672 scale * spc->v_oversample,
3676 if (spc->h_oversample > 1)
3677 stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
3678 r->w, r->h, spc->stride_in_bytes,
3681 if (spc->v_oversample > 1)
3682 stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
3683 r->w, r->h, spc->stride_in_bytes,
3686 bc->x0 = (stbtt_int16) r->x;
3687 bc->y0 = (stbtt_int16) r->y;
3688 bc->x1 = (stbtt_int16) (r->x + r->w);
3689 bc->y1 = (stbtt_int16) (r->y + r->h);
3690 bc->xadvance = scale * advance;
3691 bc->xoff = (float) x0 * recip_h + sub_x;
3692 bc->yoff = (float) y0 * recip_v + sub_y;
3693 bc->xoff2 = (x0 + r->w) * recip_h + sub_x;
3694 bc->yoff2 = (y0 + r->h) * recip_v + sub_y;
3704 spc->h_oversample = old_h_over;
3705 spc->v_oversample = old_v_over;
3707 return return_value;
3710 STBTT_DEF
void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects,
int num_rects)
3712 stbrp_pack_rects((stbrp_context *) spc->pack_info, rects, num_rects);
3715 STBTT_DEF
int stbtt_PackFontRanges(stbtt_pack_context *spc,
unsigned char *fontdata,
int font_index, stbtt_pack_range *ranges,
int num_ranges)
3717 stbtt_fontinfo info;
3718 int i,j,n, return_value = 1;
3723 for (i=0; i < num_ranges; ++i)
3724 for (j=0; j < ranges[i].num_chars; ++j)
3725 ranges[i].chardata_for_range[j].x0 =
3726 ranges[i].chardata_for_range[j].y0 =
3727 ranges[i].chardata_for_range[j].x1 =
3728 ranges[i].chardata_for_range[j].y1 = 0;
3731 for (i=0; i < num_ranges; ++i)
3732 n += ranges[i].num_chars;
3734 rects = (stbrp_rect *) STBTT_malloc(
sizeof(*rects) * n, spc->user_allocator_context);
3738 info.userdata = spc->user_allocator_context;
3739 stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index));
3741 n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);
3743 stbtt_PackFontRangesPackRects(spc, rects, n);
3745 return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);
3747 STBTT_free(rects, spc->user_allocator_context);
3748 return return_value;
3751 STBTT_DEF
int stbtt_PackFontRange(stbtt_pack_context *spc,
unsigned char *fontdata,
int font_index,
float font_size,
3752 int first_unicode_codepoint_in_range,
int num_chars_in_range, stbtt_packedchar *chardata_for_range)
3754 stbtt_pack_range range;
3755 range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range;
3756 range.array_of_unicode_codepoints = NULL;
3757 range.num_chars = num_chars_in_range;
3758 range.chardata_for_range = chardata_for_range;
3759 range.font_size = font_size;
3760 return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);
3763 STBTT_DEF
void stbtt_GetPackedQuad(
const stbtt_packedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
float *ypos, stbtt_aligned_quad *q,
int align_to_integer)
3765 float ipw = 1.0f / pw, iph = 1.0f / ph;
3766 const stbtt_packedchar *b = chardata + char_index;
3768 if (align_to_integer) {
3769 float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5f);
3770 float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5f);
3773 q->x1 = x + b->xoff2 - b->xoff;
3774 q->y1 = y + b->yoff2 - b->yoff;
3776 q->x0 = *xpos + b->xoff;
3777 q->y0 = *ypos + b->yoff;
3778 q->x1 = *xpos + b->xoff2;
3779 q->y1 = *ypos + b->yoff2;
3782 q->s0 = b->x0 * ipw;
3783 q->t0 = b->y0 * iph;
3784 q->s1 = b->x1 * ipw;
3785 q->t1 = b->y1 * iph;
3787 *xpos += b->xadvance;
3791 STBTT_DEF
void stbtt_GetPackedQuadScaled(
const stbtt_packedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
float *ypos, stbtt_aligned_quad *q,
float scale)
3793 float ipw = 1.0f / pw, iph = 1.0f / ph;
3794 const stbtt_packedchar *b = chardata + char_index;
3796 q->x0 = *xpos + b->xoff*scale;
3797 q->y0 = *ypos + b->yoff*scale;
3798 q->x1 = *xpos + b->xoff2*scale;
3799 q->y1 = *ypos + b->yoff2*scale;
3801 q->s0 = b->x0 * ipw;
3802 q->t0 = b->y0 * iph;
3803 q->s1 = b->x1 * ipw;
3804 q->t1 = b->y1 * iph;
3806 *xpos += b->xadvance*scale;
3816 static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2, stbtt_int32 len2)
3822 stbtt_uint16 ch = s2[0]*256 + s2[1];
3824 if (i >= len1)
return -1;
3825 if (s1[i++] != ch)
return -1;
3826 }
else if (ch < 0x800) {
3827 if (i+1 >= len1)
return -1;
3828 if (s1[i++] != 0xc0 + (ch >> 6))
return -1;
3829 if (s1[i++] != 0x80 + (ch & 0x3f))
return -1;
3830 }
else if (ch >= 0xd800 && ch < 0xdc00) {
3832 stbtt_uint16 ch2 = s2[2]*256 + s2[3];
3833 if (i+3 >= len1)
return -1;
3834 c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
3835 if (s1[i++] != 0xf0 + (c >> 18))
return -1;
3836 if (s1[i++] != 0x80 + ((c >> 12) & 0x3f))
return -1;
3837 if (s1[i++] != 0x80 + ((c >> 6) & 0x3f))
return -1;
3838 if (s1[i++] != 0x80 + ((c ) & 0x3f))
return -1;
3841 }
else if (ch >= 0xdc00 && ch < 0xe000) {
3844 if (i+2 >= len1)
return -1;
3845 if (s1[i++] != 0xe0 + (ch >> 12))
return -1;
3846 if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f))
return -1;
3847 if (s1[i++] != 0x80 + ((ch ) & 0x3f))
return -1;
3855 static int stbtt_CompareUTF8toUTF16_bigendian_internal(
char *s1,
int len1,
char *s2,
int len2)
3857 return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8*) s1, len1, (stbtt_uint8*) s2, len2);
3862 STBTT_DEF
const char *stbtt_GetFontNameString(
const stbtt_fontinfo *font,
int *length,
int platformID,
int encodingID,
int languageID,
int nameID)
3864 stbtt_int32 i,count,stringOffset;
3865 stbtt_uint8 *fc = font->data;
3866 stbtt_uint32 offset = font->fontstart;
3867 stbtt_uint32 nm = stbtt__find_table(fc, offset,
"name");
3868 if (!nm)
return NULL;
3870 count = ttUSHORT(fc+nm+2);
3871 stringOffset = nm + ttUSHORT(fc+nm+4);
3872 for (i=0; i < count; ++i) {
3873 stbtt_uint32 loc = nm + 6 + 12 * i;
3874 if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2)
3875 && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) {
3876 *length = ttUSHORT(fc+loc+8);
3877 return (
const char *) (fc+stringOffset+ttUSHORT(fc+loc+10));
3883 static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)
3886 stbtt_int32 count = ttUSHORT(fc+nm+2);
3887 stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4);
3889 for (i=0; i < count; ++i) {
3890 stbtt_uint32 loc = nm + 6 + 12 * i;
3891 stbtt_int32
id = ttUSHORT(fc+loc+6);
3892 if (
id == target_id) {
3894 stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4);
3897 if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
3898 stbtt_int32 slen = ttUSHORT(fc+loc+8);
3899 stbtt_int32 off = ttUSHORT(fc+loc+10);
3902 stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);
3903 if (matchlen >= 0) {
3905 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) {
3906 slen = ttUSHORT(fc+loc+12+8);
3907 off = ttUSHORT(fc+loc+12+10);
3909 if (matchlen == nlen)
3911 }
else if (matchlen < nlen && name[matchlen] ==
' ') {
3913 if (stbtt_CompareUTF8toUTF16_bigendian_internal((
char*) (name+matchlen), nlen-matchlen, (
char*)(fc+stringOffset+off),slen))
3918 if (matchlen == nlen)
3930 static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)
3932 stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((
char *) name);
3934 if (!stbtt__isfont(fc+offset))
return 0;
3938 hd = stbtt__find_table(fc, offset,
"head");
3939 if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7))
return 0;
3942 nm = stbtt__find_table(fc, offset,
"name");
3947 if (stbtt__matchpair(fc, nm, name, nlen, 16, -1))
return 1;
3948 if (stbtt__matchpair(fc, nm, name, nlen, 1, -1))
return 1;
3949 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
return 1;
3951 if (stbtt__matchpair(fc, nm, name, nlen, 16, 17))
return 1;
3952 if (stbtt__matchpair(fc, nm, name, nlen, 1, 2))
return 1;
3953 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
return 1;
3959 static int stbtt_FindMatchingFont_internal(
unsigned char *font_collection,
char *name_utf8, stbtt_int32 flags)
3963 stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
3964 if (off < 0)
return off;
3965 if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags))
3970 #if defined(__GNUC__) || defined(__clang__)
3971 #pragma GCC diagnostic push
3972 #pragma GCC diagnostic ignored "-Wcast-qual"
3975 STBTT_DEF
int stbtt_BakeFontBitmap(
const unsigned char *data,
int offset,
3976 float pixel_height,
unsigned char *pixels,
int pw,
int ph,
3977 int first_char,
int num_chars, stbtt_bakedchar *chardata)
3979 return stbtt_BakeFontBitmap_internal((
unsigned char *) data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, chardata);
3982 STBTT_DEF
int stbtt_GetFontOffsetForIndex(
const unsigned char *data,
int index)
3984 return stbtt_GetFontOffsetForIndex_internal((
unsigned char *) data, index);
3987 STBTT_DEF
int stbtt_GetNumberOfFonts(
const unsigned char *data)
3989 return stbtt_GetNumberOfFonts_internal((
unsigned char *) data);
3992 STBTT_DEF
int stbtt_InitFont(stbtt_fontinfo *info,
const unsigned char *data,
int offset)
3994 return stbtt_InitFont_internal(info, (
unsigned char *) data, offset);
3997 STBTT_DEF
int stbtt_FindMatchingFont(
const unsigned char *fontdata,
const char *name,
int flags)
3999 return stbtt_FindMatchingFont_internal((
unsigned char *) fontdata, (
char *) name, flags);
4002 STBTT_DEF
int stbtt_CompareUTF8toUTF16_bigendian(
const char *s1,
int len1,
const char *s2,
int len2)
4004 return stbtt_CompareUTF8toUTF16_bigendian_internal((
char *) s1, len1, (
char *) s2, len2);
4007 #if defined(__GNUC__) || defined(__clang__)
4008 #pragma GCC diagnostic pop
4011 #endif // STB_TRUETYPE_IMPLEMENTATION