278 #define STB_TRUETYPE_IMPLEMENTATION
279 #include "stb_truetype.h"
281 unsigned char ttf_buffer[1<<20];
282 unsigned char temp_bitmap[512*512];
284 stbtt_bakedchar cdata[96];
287 void my_stbtt_initfont(
void)
289 fread(ttf_buffer, 1, 1<<20, fopen(
"c:/windows/fonts/times.ttf",
"rb"));
290 stbtt_BakeFontBitmap(ttf_buffer,0, 32.0, temp_bitmap,512,512, 32,96, cdata);
292 glGenTextures(1, &ftex);
293 glBindTexture(GL_TEXTURE_2D, ftex);
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
299 void my_stbtt_print(
float x,
float y,
char *text)
303 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
304 glEnable(GL_TEXTURE_2D);
305 glBindTexture(GL_TEXTURE_2D, ftex);
308 if (*text >= 32 && *text < 128) {
309 stbtt_aligned_quad q;
310 stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);
311 glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y0);
312 glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y0);
313 glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y1);
314 glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y1);
329 #define STB_TRUETYPE_IMPLEMENTATION
330 #include "stb_truetype.h"
332 char ttf_buffer[1<<25];
334 int main(
int argc,
char **argv)
337 unsigned char *bitmap;
338 int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) :
'a'), s = (argc > 2 ? atoi(argv[2]) : 20);
340 fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] :
"c:/windows/fonts/arialbd.ttf",
"rb"));
342 stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));
343 bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0);
345 for (j=0; j < h; ++j) {
346 for (i=0; i < w; ++i)
347 putchar(
" .:ioVM@"[bitmap[j*w+i]>>5]);
373 unsigned char screen[20][79];
375 int main(
int arg,
char **argv)
378 int i,j,ascent,baseline,ch=0;
380 char *text =
"Heljo World!";
382 fread(buffer, 1, 1000000, fopen(
"c:/windows/fonts/arialbd.ttf",
"rb"));
383 stbtt_InitFont(&font, buffer, 0);
385 scale = stbtt_ScaleForPixelHeight(&font, 15);
386 stbtt_GetFontVMetrics(&font, &ascent,0,0);
387 baseline = (int) (ascent*scale);
390 int advance,lsb,x0,y0,x1,y1;
391 float x_shift = xpos - (float) floor(xpos);
392 stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb);
393 stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1);
394 stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(
int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]);
399 xpos += (advance * scale);
401 xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]);
405 for (j=0; j < 20; ++j) {
406 for (i=0; i < 78; ++i)
407 putchar(
" .:ioVM@"[screen[j][i]>>5]);
423 #ifndef DOXYGEN_SHOULD_SKIP_THIS
425 #ifdef STB_TRUETYPE_IMPLEMENTATION
428 typedef unsigned char stbtt_uint8;
429 typedef signed char stbtt_int8;
430 typedef unsigned short stbtt_uint16;
431 typedef signed short stbtt_int16;
432 typedef unsigned int stbtt_uint32;
433 typedef signed int stbtt_int32;
436 typedef char stbtt__check_size32[
sizeof(stbtt_int32) == 4 ? 1 : -1];
437 typedef char stbtt__check_size16[
sizeof(stbtt_int16) == 2 ? 1 : -1];
442 #define STBTT_ifloor(x) ((int)floor(x))
443 #define STBTT_iceil(x) ((int)ceil(x))
448 #define STBTT_sqrt(x) sqrt(x)
449 #define STBTT_pow(x, y) pow(x, y)
454 #define STBTT_fmod(x, y) fmod(x, y)
459 #define STBTT_cos(x) cos(x)
460 #define STBTT_acos(x) acos(x)
465 #define STBTT_fabs(x) fabs(x)
471 #define STBTT_malloc(x, u) ((void)(u), malloc(x))
472 #define STBTT_free(x, u) ((void)(u), free(x))
477 #define STBTT_assert(x) assert(x)
482 #define STBTT_strlen(x) strlen(x)
487 #define STBTT_memcpy memcpy
488 #define STBTT_memset memset
499 #ifndef __STB_INCLUDE_STB_TRUETYPE_H__
500 #define __STB_INCLUDE_STB_TRUETYPE_H__
503 #define STBTT_DEF static
505 #define STBTT_DEF extern
527 unsigned short x0, y0, x1, y1;
528 float xoff, yoff, xadvance;
531 STBTT_DEF
int stbtt_BakeFontBitmap(
const unsigned char *data,
int offset,
533 unsigned char *pixels,
int pw,
int ph,
534 int first_char,
int num_chars,
535 stbtt_bakedchar *chardata);
542 float x0, y0, s0, t0;
543 float x1, y1, s1, t1;
544 } stbtt_aligned_quad;
546 STBTT_DEF
void stbtt_GetBakedQuad(
const stbtt_bakedchar *chardata,
int pw,
int ph,
548 float *xpos,
float *ypos,
549 stbtt_aligned_quad *q,
550 int opengl_fillrule);
561 STBTT_DEF
void stbtt_GetScaledFontVMetrics(
const unsigned char *fontdata,
int index,
float size,
float *ascent,
562 float *descent,
float *lineGap);
573 unsigned short x0, y0, x1, y1;
574 float xoff, yoff, xadvance;
578 typedef struct stbtt_pack_context stbtt_pack_context;
579 typedef struct stbtt_fontinfo stbtt_fontinfo;
580 #ifndef STB_RECT_PACK_VERSION
581 typedef struct stbrp_rect stbrp_rect;
584 STBTT_DEF
int stbtt_PackBegin(stbtt_pack_context *spc,
unsigned char *pixels,
int width,
int height,
585 int stride_in_bytes,
int padding,
void *alloc_context);
596 STBTT_DEF
void stbtt_PackEnd(stbtt_pack_context *spc);
599 #define STBTT_POINT_SIZE(x) (-(x))
601 STBTT_DEF
int stbtt_PackFontRange(stbtt_pack_context *spc,
const unsigned char *fontdata,
int font_index,
602 float font_size,
int first_unicode_char_in_range,
int num_chars_in_range,
603 stbtt_packedchar *chardata_for_range);
619 int first_unicode_codepoint_in_range;
620 int *array_of_unicode_codepoints;
622 stbtt_packedchar *chardata_for_range;
623 unsigned char h_oversample, v_oversample;
626 STBTT_DEF
int stbtt_PackFontRanges(stbtt_pack_context *spc,
const unsigned char *fontdata,
int font_index,
627 stbtt_pack_range *ranges,
int num_ranges);
633 STBTT_DEF
void stbtt_PackSetOversampling(stbtt_pack_context *spc,
unsigned int h_oversample,
unsigned int v_oversample);
649 STBTT_DEF
void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc,
int skip);
655 STBTT_DEF
void stbtt_GetPackedQuad(
const stbtt_packedchar *chardata,
int pw,
int ph,
657 float *xpos,
float *ypos,
658 stbtt_aligned_quad *q,
659 int align_to_integer);
661 STBTT_DEF
int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info,
662 stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects);
663 STBTT_DEF
void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects,
int num_rects);
664 STBTT_DEF
int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info,
665 stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects);
678 struct stbtt_pack_context {
679 void *user_allocator_context;
686 unsigned int h_oversample, v_oversample;
687 unsigned char *pixels;
697 STBTT_DEF
int stbtt_GetNumberOfFonts(
const unsigned char *data);
704 STBTT_DEF
int stbtt_GetFontOffsetForIndex(
const unsigned char *data,
int index);
713 struct stbtt_fontinfo {
720 int loca, head, glyf, hhea, hmtx, kern, gpos, svg;
722 int indexToLocFormat;
725 stbtt__buf charstrings;
728 stbtt__buf fontdicts;
732 STBTT_DEF
int stbtt_InitFont(stbtt_fontinfo *info,
const unsigned char *data,
int offset);
743 STBTT_DEF
int stbtt_FindGlyphIndex(
const stbtt_fontinfo *info,
int unicode_codepoint);
755 STBTT_DEF
float stbtt_ScaleForPixelHeight(
const stbtt_fontinfo *info,
float pixels);
763 STBTT_DEF
float stbtt_ScaleForMappingEmToPixels(
const stbtt_fontinfo *info,
float pixels);
768 STBTT_DEF
void stbtt_GetFontVMetrics(
const stbtt_fontinfo *info,
int *ascent,
int *descent,
int *lineGap);
776 STBTT_DEF
int stbtt_GetFontVMetricsOS2(
const stbtt_fontinfo *info,
int *typoAscent,
int *typoDescent,
int *typoLineGap);
782 STBTT_DEF
void stbtt_GetFontBoundingBox(
const stbtt_fontinfo *info,
int *x0,
int *y0,
int *x1,
int *y1);
785 STBTT_DEF
void stbtt_GetCodepointHMetrics(
const stbtt_fontinfo *info,
int codepoint,
int *advanceWidth,
786 int *leftSideBearing);
791 STBTT_DEF
int stbtt_GetCodepointKernAdvance(
const stbtt_fontinfo *info,
int ch1,
int ch2);
794 STBTT_DEF
int stbtt_GetCodepointBox(
const stbtt_fontinfo *info,
int codepoint,
int *x0,
int *y0,
int *x1,
int *y1);
797 STBTT_DEF
void stbtt_GetGlyphHMetrics(
const stbtt_fontinfo *info,
int glyph_index,
int *advanceWidth,
798 int *leftSideBearing);
799 STBTT_DEF
int stbtt_GetGlyphKernAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2);
800 STBTT_DEF
int stbtt_GetGlyphBox(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1);
803 typedef struct stbtt_kerningentry {
807 } stbtt_kerningentry;
809 STBTT_DEF
int stbtt_GetKerningTableLength(
const stbtt_fontinfo *info);
810 STBTT_DEF
int stbtt_GetKerningTable(
const stbtt_fontinfo *info, stbtt_kerningentry *table,
int table_length);
822 enum { STBTT_vmove = 1, STBTT_vline, STBTT_vcurve, STBTT_vcubic };
827 #define stbtt_vertex_type short
829 stbtt_vertex_type x, y, cx, cy, cx1, cy1;
830 unsigned char type, padding;
834 STBTT_DEF
int stbtt_IsGlyphEmpty(
const stbtt_fontinfo *info,
int glyph_index);
837 STBTT_DEF
int stbtt_GetCodepointShape(
const stbtt_fontinfo *info,
int unicode_codepoint, stbtt_vertex **vertices);
838 STBTT_DEF
int stbtt_GetGlyphShape(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **vertices);
849 STBTT_DEF
void stbtt_FreeShape(
const stbtt_fontinfo *info, stbtt_vertex *vertices);
852 STBTT_DEF
unsigned char *stbtt_FindSVGDoc(
const stbtt_fontinfo *info,
int gl);
853 STBTT_DEF
int stbtt_GetCodepointSVG(
const stbtt_fontinfo *info,
int unicode_codepoint,
const char **svg);
854 STBTT_DEF
int stbtt_GetGlyphSVG(
const stbtt_fontinfo *info,
int gl,
const char **svg);
863 STBTT_DEF
void stbtt_FreeBitmap(
unsigned char *bitmap,
void *userdata);
866 STBTT_DEF
unsigned char *stbtt_GetCodepointBitmap(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
867 int codepoint,
int *width,
int *height,
int *xoff,
int *yoff);
876 STBTT_DEF
unsigned char *stbtt_GetCodepointBitmapSubpixel(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
877 float shift_x,
float shift_y,
int codepoint,
int *width,
878 int *height,
int *xoff,
int *yoff);
882 STBTT_DEF
void stbtt_MakeCodepointBitmap(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
883 int out_stride,
float scale_x,
float scale_y,
int codepoint);
889 STBTT_DEF
void stbtt_MakeCodepointBitmapSubpixel(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
890 int out_h,
int out_stride,
float scale_x,
float scale_y,
float shift_x,
891 float shift_y,
int codepoint);
895 STBTT_DEF
void stbtt_MakeCodepointBitmapSubpixelPrefilter(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
896 int out_h,
int out_stride,
float scale_x,
float scale_y,
897 float shift_x,
float shift_y,
int oversample_x,
898 int oversample_y,
float *sub_x,
float *sub_y,
int codepoint);
902 STBTT_DEF
void stbtt_GetCodepointBitmapBox(
const stbtt_fontinfo *font,
int codepoint,
float scale_x,
float scale_y,
903 int *ix0,
int *iy0,
int *ix1,
int *iy1);
910 STBTT_DEF
void stbtt_GetCodepointBitmapBoxSubpixel(
const stbtt_fontinfo *font,
int codepoint,
float scale_x,
911 float scale_y,
float shift_x,
float shift_y,
int *ix0,
int *iy0,
918 STBTT_DEF
unsigned char *stbtt_GetGlyphBitmap(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
int glyph,
919 int *width,
int *height,
int *xoff,
int *yoff);
920 STBTT_DEF
unsigned char *stbtt_GetGlyphBitmapSubpixel(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
921 float shift_x,
float shift_y,
int glyph,
int *width,
int *height,
922 int *xoff,
int *yoff);
923 STBTT_DEF
void stbtt_MakeGlyphBitmap(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
924 int out_stride,
float scale_x,
float scale_y,
int glyph);
925 STBTT_DEF
void stbtt_MakeGlyphBitmapSubpixel(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
926 int out_stride,
float scale_x,
float scale_y,
float shift_x,
float shift_y,
928 STBTT_DEF
void stbtt_MakeGlyphBitmapSubpixelPrefilter(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
929 int out_h,
int out_stride,
float scale_x,
float scale_y,
930 float shift_x,
float shift_y,
int oversample_x,
int oversample_y,
931 float *sub_x,
float *sub_y,
int glyph);
932 STBTT_DEF
void stbtt_GetGlyphBitmapBox(
const stbtt_fontinfo *font,
int glyph,
float scale_x,
float scale_y,
int *ix0,
933 int *iy0,
int *ix1,
int *iy1);
934 STBTT_DEF
void stbtt_GetGlyphBitmapBoxSubpixel(
const stbtt_fontinfo *font,
int glyph,
float scale_x,
float scale_y,
935 float shift_x,
float shift_y,
int *ix0,
int *iy0,
int *ix1,
int *iy1);
940 unsigned char *pixels;
944 STBTT_DEF
void stbtt_Rasterize(stbtt__bitmap *result,
945 float flatness_in_pixels,
946 stbtt_vertex *vertices,
948 float scale_x,
float scale_y,
949 float shift_x,
float shift_y,
950 int x_off,
int y_off,
958 STBTT_DEF
void stbtt_FreeSDF(
unsigned char *bitmap,
void *userdata);
961 STBTT_DEF
unsigned char *stbtt_GetGlyphSDF(
const stbtt_fontinfo *info,
float scale,
int glyph,
int padding,
962 unsigned char onedge_value,
float pixel_dist_scale,
int *width,
int *height,
963 int *xoff,
int *yoff);
964 STBTT_DEF
unsigned char *stbtt_GetCodepointSDF(
const stbtt_fontinfo *info,
float scale,
int codepoint,
int padding,
965 unsigned char onedge_value,
float pixel_dist_scale,
int *width,
966 int *height,
int *xoff,
int *yoff);
1036 STBTT_DEF
int stbtt_FindMatchingFont(
const unsigned char *fontdata,
const char *name,
int flags);
1041 #define STBTT_MACSTYLE_DONTCARE 0
1042 #define STBTT_MACSTYLE_BOLD 1
1043 #define STBTT_MACSTYLE_ITALIC 2
1044 #define STBTT_MACSTYLE_UNDERSCORE 4
1045 #define STBTT_MACSTYLE_NONE 8
1047 STBTT_DEF
int stbtt_CompareUTF8toUTF16_bigendian(
const char *s1,
int len1,
const char *s2,
int len2);
1051 STBTT_DEF
const char *stbtt_GetFontNameString(
const stbtt_fontinfo *font,
int *length,
int platformID,
int encodingID,
1052 int languageID,
int nameID);
1061 STBTT_PLATFORM_ID_UNICODE = 0,
1062 STBTT_PLATFORM_ID_MAC = 1,
1063 STBTT_PLATFORM_ID_ISO = 2,
1064 STBTT_PLATFORM_ID_MICROSOFT = 3
1068 STBTT_UNICODE_EID_UNICODE_1_0 = 0,
1069 STBTT_UNICODE_EID_UNICODE_1_1 = 1,
1070 STBTT_UNICODE_EID_ISO_10646 = 2,
1071 STBTT_UNICODE_EID_UNICODE_2_0_BMP = 3,
1072 STBTT_UNICODE_EID_UNICODE_2_0_FULL = 4
1076 STBTT_MS_EID_SYMBOL = 0,
1077 STBTT_MS_EID_UNICODE_BMP = 1,
1078 STBTT_MS_EID_SHIFTJIS = 2,
1079 STBTT_MS_EID_UNICODE_FULL = 10
1083 STBTT_MAC_EID_ROMAN = 0,
1084 STBTT_MAC_EID_ARABIC = 4,
1085 STBTT_MAC_EID_JAPANESE = 1,
1086 STBTT_MAC_EID_HEBREW = 5,
1087 STBTT_MAC_EID_CHINESE_TRAD = 2,
1088 STBTT_MAC_EID_GREEK = 6,
1089 STBTT_MAC_EID_KOREAN = 3,
1090 STBTT_MAC_EID_RUSSIAN = 7
1095 STBTT_MS_LANG_ENGLISH = 0x0409,
1096 STBTT_MS_LANG_ITALIAN = 0x0410,
1097 STBTT_MS_LANG_CHINESE = 0x0804,
1098 STBTT_MS_LANG_JAPANESE = 0x0411,
1099 STBTT_MS_LANG_DUTCH = 0x0413,
1100 STBTT_MS_LANG_KOREAN = 0x0412,
1101 STBTT_MS_LANG_FRENCH = 0x040c,
1102 STBTT_MS_LANG_RUSSIAN = 0x0419,
1103 STBTT_MS_LANG_GERMAN = 0x0407,
1104 STBTT_MS_LANG_SPANISH = 0x0409,
1105 STBTT_MS_LANG_HEBREW = 0x040d,
1106 STBTT_MS_LANG_SWEDISH = 0x041D
1110 STBTT_MAC_LANG_ENGLISH = 0,
1111 STBTT_MAC_LANG_JAPANESE = 11,
1112 STBTT_MAC_LANG_ARABIC = 12,
1113 STBTT_MAC_LANG_KOREAN = 23,
1114 STBTT_MAC_LANG_DUTCH = 4,
1115 STBTT_MAC_LANG_RUSSIAN = 32,
1116 STBTT_MAC_LANG_FRENCH = 1,
1117 STBTT_MAC_LANG_SPANISH = 6,
1118 STBTT_MAC_LANG_GERMAN = 2,
1119 STBTT_MAC_LANG_SWEDISH = 5,
1120 STBTT_MAC_LANG_HEBREW = 10,
1121 STBTT_MAC_LANG_CHINESE_SIMPLIFIED = 33,
1122 STBTT_MAC_LANG_ITALIAN = 3,
1123 STBTT_MAC_LANG_CHINESE_TRAD = 19
1139 #ifdef STB_TRUETYPE_IMPLEMENTATION
1141 #ifndef STBTT_MAX_OVERSAMPLE
1142 #define STBTT_MAX_OVERSAMPLE 8
1145 #if STBTT_MAX_OVERSAMPLE > 255
1146 #error "STBTT_MAX_OVERSAMPLE cannot be > 255"
1149 typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE - 1)) == 0 ? 1 : -1];
1151 #ifndef STBTT_RASTERIZER_VERSION
1152 #define STBTT_RASTERIZER_VERSION 2
1156 #define STBTT__NOTUSED(v) (void)(v)
1158 #define STBTT__NOTUSED(v) (void)sizeof(v)
1166 static stbtt_uint8 stbtt__buf_get8(stbtt__buf *b)
1168 if (b->cursor >= b->size)
1170 return b->data[b->cursor++];
1173 static stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b)
1175 if (b->cursor >= b->size)
1177 return b->data[b->cursor];
1180 static void stbtt__buf_seek(stbtt__buf *b,
int o)
1182 STBTT_assert(!(o > b->size || o < 0));
1183 b->cursor = (o > b->size || o < 0) ? b->size : o;
1186 static void stbtt__buf_skip(stbtt__buf *b,
int o) { stbtt__buf_seek(b, b->cursor + o); }
1188 static stbtt_uint32 stbtt__buf_get(stbtt__buf *b,
int n)
1192 STBTT_assert(n >= 1 && n <= 4);
1193 for (i = 0; i < n; i++)
1194 v = (v << 8) | stbtt__buf_get8(b);
1198 static stbtt__buf stbtt__new_buf(
const void *p,
size_t size)
1201 STBTT_assert(size < 0x40000000);
1202 r.data = (stbtt_uint8 *)p;
1208 #define stbtt__buf_get16(b) stbtt__buf_get((b), 2)
1209 #define stbtt__buf_get32(b) stbtt__buf_get((b), 4)
1211 static stbtt__buf stbtt__buf_range(
const stbtt__buf *b,
int o,
int s)
1213 stbtt__buf r = stbtt__new_buf(
nullptr, 0);
1214 if (o < 0 || s < 0 || o > b->size || s > b->size - o)
1216 r.data = b->data + o;
1221 static stbtt__buf stbtt__cff_get_index(stbtt__buf *b)
1223 int count, start, offsize;
1225 count = stbtt__buf_get16(b);
1227 offsize = stbtt__buf_get8(b);
1228 STBTT_assert(offsize >= 1 && offsize <= 4);
1229 stbtt__buf_skip(b, offsize * count);
1230 stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);
1232 return stbtt__buf_range(b, start, b->cursor - start);
1235 static stbtt_uint32 stbtt__cff_int(stbtt__buf *b)
1237 int b0 = stbtt__buf_get8(b);
1238 if (b0 >= 32 && b0 <= 246)
1240 else if (b0 >= 247 && b0 <= 250)
1241 return (b0 - 247) * 256 + stbtt__buf_get8(b) + 108;
1242 else if (b0 >= 251 && b0 <= 254)
1243 return -(b0 - 251) * 256 - stbtt__buf_get8(b) - 108;
1245 return stbtt__buf_get16(b);
1247 return stbtt__buf_get32(b);
1252 static void stbtt__cff_skip_operand(stbtt__buf *b)
1254 int v, b0 = stbtt__buf_peek8(b);
1255 STBTT_assert(b0 >= 28);
1257 stbtt__buf_skip(b, 1);
1258 while (b->cursor < b->size) {
1259 v = stbtt__buf_get8(b);
1260 if ((v & 0xF) == 0xF || (v >> 4) == 0xF)
1268 static stbtt__buf stbtt__dict_get(stbtt__buf *b,
int key)
1270 stbtt__buf_seek(b, 0);
1271 while (b->cursor < b->size) {
1272 int start = b->cursor, end, op;
1273 while (stbtt__buf_peek8(b) >= 28)
1274 stbtt__cff_skip_operand(b);
1276 op = stbtt__buf_get8(b);
1278 op = stbtt__buf_get8(b) | 0x100;
1280 return stbtt__buf_range(b, start, end - start);
1282 return stbtt__buf_range(b, 0, 0);
1285 static void stbtt__dict_get_ints(stbtt__buf *b,
int key,
int outcount, stbtt_uint32 *out)
1288 stbtt__buf operands = stbtt__dict_get(b, key);
1289 for (i = 0; i < outcount && operands.cursor < operands.size; i++)
1290 out[i] = stbtt__cff_int(&operands);
1293 static int stbtt__cff_index_count(stbtt__buf *b)
1295 stbtt__buf_seek(b, 0);
1296 return stbtt__buf_get16(b);
1299 static stbtt__buf stbtt__cff_index_get(stbtt__buf b,
int i)
1301 int count, offsize, start, end;
1302 stbtt__buf_seek(&b, 0);
1303 count = stbtt__buf_get16(&b);
1304 offsize = stbtt__buf_get8(&b);
1305 STBTT_assert(i >= 0 && i < count);
1306 STBTT_assert(offsize >= 1 && offsize <= 4);
1307 stbtt__buf_skip(&b, i * offsize);
1308 start = stbtt__buf_get(&b, offsize);
1309 end = stbtt__buf_get(&b, offsize);
1310 return stbtt__buf_range(&b, 2 + (count + 1) * offsize + start, end - start);
1321 #define ttBYTE(p) (*(stbtt_uint8 *)(p))
1322 #define ttCHAR(p) (*(stbtt_int8 *)(p))
1323 #define ttFixed(p) ttLONG(p)
1325 static stbtt_uint16 ttUSHORT(stbtt_uint8 *p) {
return p[0] * 256 + p[1]; }
1326 static stbtt_int16 ttSHORT(stbtt_uint8 *p) {
return p[0] * 256 + p[1]; }
1327 static stbtt_uint32 ttULONG(stbtt_uint8 *p) {
return (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3]; }
1328 static stbtt_int32 ttLONG(stbtt_uint8 *p) {
return (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3]; }
1330 #define stbtt_tag4(p, c0, c1, c2, c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
1331 #define stbtt_tag(p, str) stbtt_tag4(p, str[0], str[1], str[2], str[3])
1333 static int stbtt__isfont(stbtt_uint8 *font)
1336 if (stbtt_tag4(font,
'1', 0, 0, 0))
1338 if (stbtt_tag(font,
"typ1"))
1340 if (stbtt_tag(font,
"OTTO"))
1342 if (stbtt_tag4(font, 0, 1, 0, 0))
1344 if (stbtt_tag(font,
"true"))
1350 static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart,
const char *tag)
1352 stbtt_int32 num_tables = ttUSHORT(data + fontstart + 4);
1353 stbtt_uint32 tabledir = fontstart + 12;
1355 for (i = 0; i < num_tables; ++i) {
1356 stbtt_uint32 loc = tabledir + 16 * i;
1357 if (stbtt_tag(data + loc + 0, tag))
1358 return ttULONG(data + loc + 8);
1363 static int stbtt_GetFontOffsetForIndex_internal(
unsigned char *font_collection,
int index)
1366 if (stbtt__isfont(font_collection))
1367 return index == 0 ? 0 : -1;
1370 if (stbtt_tag(font_collection,
"ttcf")) {
1372 if (ttULONG(font_collection + 4) == 0x00010000 || ttULONG(font_collection + 4) == 0x00020000) {
1373 stbtt_int32 n = ttLONG(font_collection + 8);
1376 return ttULONG(font_collection + 12 + index * 4);
1382 static int stbtt_GetNumberOfFonts_internal(
unsigned char *font_collection)
1385 if (stbtt__isfont(font_collection))
1389 if (stbtt_tag(font_collection,
"ttcf")) {
1391 if (ttULONG(font_collection + 4) == 0x00010000 || ttULONG(font_collection + 4) == 0x00020000) {
1392 return ttLONG(font_collection + 8);
1398 static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict)
1400 stbtt_uint32 subrsoff = 0, private_loc[2] = {0, 0};
1402 stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);
1403 if (!private_loc[1] || !private_loc[0])
1404 return stbtt__new_buf(
nullptr, 0);
1405 pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);
1406 stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);
1408 return stbtt__new_buf(
nullptr, 0);
1409 stbtt__buf_seek(&cff, private_loc[1] + subrsoff);
1410 return stbtt__cff_get_index(&cff);
1414 static int stbtt__get_svg(stbtt_fontinfo *info)
1417 if (info->svg < 0) {
1418 t = stbtt__find_table(info->data, info->fontstart,
"SVG ");
1420 stbtt_uint32 offset = ttULONG(info->data + t + 2);
1421 info->svg = t + offset;
1429 static int stbtt_InitFont_internal(stbtt_fontinfo *info,
unsigned char *data,
int fontstart)
1431 stbtt_uint32 cmap, t;
1432 stbtt_int32 i, numTables;
1435 info->fontstart = fontstart;
1436 info->cff = stbtt__new_buf(
nullptr, 0);
1438 cmap = stbtt__find_table(data, fontstart,
"cmap");
1439 info->loca = stbtt__find_table(data, fontstart,
"loca");
1440 info->head = stbtt__find_table(data, fontstart,
"head");
1441 info->glyf = stbtt__find_table(data, fontstart,
"glyf");
1442 info->hhea = stbtt__find_table(data, fontstart,
"hhea");
1443 info->hmtx = stbtt__find_table(data, fontstart,
"hmtx");
1444 info->kern = stbtt__find_table(data, fontstart,
"kern");
1445 info->gpos = stbtt__find_table(data, fontstart,
"GPOS");
1447 if (!cmap || !info->head || !info->hhea || !info->hmtx)
1455 stbtt__buf b, topdict, topdictidx;
1456 stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;
1459 cff = stbtt__find_table(data, fontstart,
"CFF ");
1463 info->fontdicts = stbtt__new_buf(
nullptr, 0);
1464 info->fdselect = stbtt__new_buf(
nullptr, 0);
1467 info->cff = stbtt__new_buf(data + cff, 512 * 1024 * 1024);
1471 stbtt__buf_skip(&b, 2);
1472 stbtt__buf_seek(&b, stbtt__buf_get8(&b));
1476 stbtt__cff_get_index(&b);
1477 topdictidx = stbtt__cff_get_index(&b);
1478 topdict = stbtt__cff_index_get(topdictidx, 0);
1479 stbtt__cff_get_index(&b);
1480 info->gsubrs = stbtt__cff_get_index(&b);
1482 stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);
1483 stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
1484 stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);
1485 stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);
1486 info->subrs = stbtt__get_subrs(b, topdict);
1491 if (charstrings == 0)
1498 stbtt__buf_seek(&b, fdarrayoff);
1499 info->fontdicts = stbtt__cff_get_index(&b);
1500 info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size - fdselectoff);
1503 stbtt__buf_seek(&b, charstrings);
1504 info->charstrings = stbtt__cff_get_index(&b);
1507 t = stbtt__find_table(data, fontstart,
"maxp");
1509 info->numGlyphs = ttUSHORT(data + t + 4);
1511 info->numGlyphs = 0xffff;
1518 numTables = ttUSHORT(data + cmap + 2);
1519 info->index_map = 0;
1520 for (i = 0; i < numTables; ++i) {
1521 stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
1523 switch (ttUSHORT(data + encoding_record)) {
1524 case STBTT_PLATFORM_ID_MICROSOFT:
1525 switch (ttUSHORT(data + encoding_record + 2)) {
1526 case STBTT_MS_EID_UNICODE_BMP:
1527 case STBTT_MS_EID_UNICODE_FULL:
1529 info->index_map = cmap + ttULONG(data + encoding_record + 4);
1533 case STBTT_PLATFORM_ID_UNICODE:
1536 info->index_map = cmap + ttULONG(data + encoding_record + 4);
1540 if (info->index_map == 0)
1543 info->indexToLocFormat = ttUSHORT(data + info->head + 50);
1547 STBTT_DEF
int stbtt_FindGlyphIndex(
const stbtt_fontinfo *info,
int unicode_codepoint)
1549 stbtt_uint8 *data = info->data;
1550 stbtt_uint32 index_map = info->index_map;
1552 stbtt_uint16 format = ttUSHORT(data + index_map + 0);
1554 stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
1555 if (unicode_codepoint < bytes - 6)
1556 return ttBYTE(data + index_map + 6 + unicode_codepoint);
1558 }
else if (format == 6) {
1559 stbtt_uint32 first = ttUSHORT(data + index_map + 6);
1560 stbtt_uint32 count = ttUSHORT(data + index_map + 8);
1561 if ((stbtt_uint32)unicode_codepoint >= first && (stbtt_uint32)unicode_codepoint < first + count)
1562 return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first) * 2);
1564 }
else if (format == 2) {
1567 }
else if (format == 4) {
1568 stbtt_uint16 segcount = ttUSHORT(data + index_map + 6) >> 1;
1569 stbtt_uint16 searchRange = ttUSHORT(data + index_map + 8) >> 1;
1570 stbtt_uint16 entrySelector = ttUSHORT(data + index_map + 10);
1571 stbtt_uint16 rangeShift = ttUSHORT(data + index_map + 12) >> 1;
1574 stbtt_uint32 endCount = index_map + 14;
1575 stbtt_uint32 search = endCount;
1577 if (unicode_codepoint > 0xffff)
1582 if (unicode_codepoint >= ttUSHORT(data + search + rangeShift * 2))
1583 search += rangeShift * 2;
1587 while (entrySelector) {
1590 end = ttUSHORT(data + search + searchRange * 2);
1591 if (unicode_codepoint > end)
1592 search += searchRange * 2;
1598 stbtt_uint16 offset, start, last;
1599 stbtt_uint16 item = (stbtt_uint16)((search - endCount) >> 1);
1601 start = ttUSHORT(data + index_map + 14 + segcount * 2 + 2 + 2 * item);
1602 last = ttUSHORT(data + endCount + 2 * item);
1603 if (unicode_codepoint < start || unicode_codepoint > last)
1606 offset = ttUSHORT(data + index_map + 14 + segcount * 6 + 2 + 2 * item);
1608 return (stbtt_uint16)(unicode_codepoint + ttSHORT(data + index_map + 14 + segcount * 4 + 2 + 2 * item));
1610 return ttUSHORT(data + offset + (unicode_codepoint - start) * 2 + index_map + 14 + segcount * 6 + 2 + 2 * item);
1612 }
else if (format == 12 || format == 13) {
1613 stbtt_uint32 ngroups = ttULONG(data + index_map + 12);
1614 stbtt_int32 low, high;
1616 high = (stbtt_int32)ngroups;
1618 while (low < high) {
1619 stbtt_int32 mid = low + ((high - low) >> 1);
1620 stbtt_uint32 start_char = ttULONG(data + index_map + 16 + mid * 12);
1621 stbtt_uint32 end_char = ttULONG(data + index_map + 16 + mid * 12 + 4);
1622 if ((stbtt_uint32)unicode_codepoint < start_char)
1624 else if ((stbtt_uint32)unicode_codepoint > end_char)
1627 stbtt_uint32 start_glyph = ttULONG(data + index_map + 16 + mid * 12 + 8);
1629 return start_glyph + unicode_codepoint - start_char;
1641 STBTT_DEF
int stbtt_GetCodepointShape(
const stbtt_fontinfo *info,
int unicode_codepoint, stbtt_vertex **vertices)
1643 return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);
1646 static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx,
1650 v->x = (stbtt_int16)x;
1651 v->y = (stbtt_int16)y;
1652 v->cx = (stbtt_int16)cx;
1653 v->cy = (stbtt_int16)cy;
1656 static int stbtt__GetGlyfOffset(
const stbtt_fontinfo *info,
int glyph_index)
1660 STBTT_assert(!info->cff.size);
1662 if (glyph_index >= info->numGlyphs)
1664 if (info->indexToLocFormat >= 2)
1667 if (info->indexToLocFormat == 0) {
1668 g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
1669 g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
1671 g1 = info->glyf + ttULONG(info->data + info->loca + glyph_index * 4);
1672 g2 = info->glyf + ttULONG(info->data + info->loca + glyph_index * 4 + 4);
1675 return g1 == g2 ? -1 : g1;
1678 static int stbtt__GetGlyphInfoT2(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1);
1680 STBTT_DEF
int stbtt_GetGlyphBox(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1)
1682 if (info->cff.size) {
1683 stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);
1685 int g = stbtt__GetGlyfOffset(info, glyph_index);
1690 *x0 = ttSHORT(info->data + g + 2);
1692 *y0 = ttSHORT(info->data + g + 4);
1694 *x1 = ttSHORT(info->data + g + 6);
1696 *y1 = ttSHORT(info->data + g + 8);
1701 STBTT_DEF
int stbtt_GetCodepointBox(
const stbtt_fontinfo *info,
int codepoint,
int *x0,
int *y0,
int *x1,
int *y1)
1703 return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info, codepoint), x0, y0, x1, y1);
1706 STBTT_DEF
int stbtt_IsGlyphEmpty(
const stbtt_fontinfo *info,
int glyph_index)
1708 stbtt_int16 numberOfContours;
1711 return stbtt__GetGlyphInfoT2(info, glyph_index,
nullptr,
nullptr,
nullptr,
nullptr) == 0;
1712 g = stbtt__GetGlyfOffset(info, glyph_index);
1715 numberOfContours = ttSHORT(info->data + g);
1716 return numberOfContours == 0;
1719 static int stbtt__close_shape(stbtt_vertex *vertices,
int num_vertices,
int was_off,
int start_off, stbtt_int32 sx,
1720 stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
1724 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx + scx) >> 1, (cy + scy) >> 1, cx, cy);
1725 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx, sy, scx, scy);
1728 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx, sy, cx, cy);
1730 stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, sx, sy, 0, 0);
1732 return num_vertices;
1735 static int stbtt__GetGlyphShapeTT(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
1737 stbtt_int16 numberOfContours;
1738 stbtt_uint8 *endPtsOfContours;
1739 stbtt_uint8 *data = info->data;
1740 stbtt_vertex *vertices = 0;
1741 int num_vertices = 0;
1742 int g = stbtt__GetGlyfOffset(info, glyph_index);
1744 *pvertices =
nullptr;
1749 numberOfContours = ttSHORT(data + g);
1751 if (numberOfContours > 0) {
1752 stbtt_uint8 flags = 0, flagcount;
1753 stbtt_int32 ins, i, j = 0, m, n, next_move, was_off = 0, off, start_off = 0;
1754 stbtt_int32 x, y, cx, cy, sx, sy, scx, scy;
1755 stbtt_uint8 *points;
1756 endPtsOfContours = (data + g + 10);
1757 ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
1758 points = data + g + 10 + numberOfContours * 2 + 2 + ins;
1760 n = 1 + ttUSHORT(endPtsOfContours + numberOfContours * 2 - 2);
1762 m = n + 2 * numberOfContours;
1763 vertices = (stbtt_vertex *)STBTT_malloc(m *
sizeof(vertices[0]), info->userdata);
1778 for (i = 0; i < n; ++i) {
1779 if (flagcount == 0) {
1782 flagcount = *points++;
1785 vertices[off + i].type = flags;
1790 for (i = 0; i < n; ++i) {
1791 flags = vertices[off + i].type;
1793 stbtt_int16 dx = *points++;
1794 x += (flags & 16) ? dx : -dx;
1796 if (!(flags & 16)) {
1797 x = x + (stbtt_int16)(points[0] * 256 + points[1]);
1801 vertices[off + i].x = (stbtt_int16)x;
1806 for (i = 0; i < n; ++i) {
1807 flags = vertices[off + i].type;
1809 stbtt_int16 dy = *points++;
1810 y += (flags & 32) ? dy : -dy;
1812 if (!(flags & 32)) {
1813 y = y + (stbtt_int16)(points[0] * 256 + points[1]);
1817 vertices[off + i].y = (stbtt_int16)y;
1822 sx = sy = cx = cy = scx = scy = 0;
1823 for (i = 0; i < n; ++i) {
1824 flags = vertices[off + i].type;
1825 x = (stbtt_int16)vertices[off + i].x;
1826 y = (stbtt_int16)vertices[off + i].y;
1828 if (next_move == i) {
1830 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy);
1833 start_off = !(flags & 1);
1839 if (!(vertices[off + i + 1].type & 1)) {
1841 sx = (x + (stbtt_int32)vertices[off + i + 1].x) >> 1;
1842 sy = (y + (stbtt_int32)vertices[off + i + 1].y) >> 1;
1845 sx = (stbtt_int32)vertices[off + i + 1].x;
1846 sy = (stbtt_int32)vertices[off + i + 1].y;
1853 stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove, sx, sy, 0, 0);
1855 next_move = 1 + ttUSHORT(endPtsOfContours + j * 2);
1860 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx + x) >> 1, (cy + y) >> 1, cx, cy);
1866 stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x, y, cx, cy);
1868 stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x, y, 0, 0);
1873 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy);
1874 }
else if (numberOfContours < 0) {
1877 stbtt_uint8 *comp = data + g + 10;
1881 stbtt_uint16 flags, gidx;
1882 int comp_num_verts = 0, i;
1883 stbtt_vertex *comp_verts = 0, *tmp = 0;
1884 float mtx[6] = {1, 0, 0, 1, 0, 0}, m, n;
1886 flags = ttSHORT(comp);
1888 gidx = ttSHORT(comp);
1893 mtx[4] = ttSHORT(comp);
1895 mtx[5] = ttSHORT(comp);
1898 mtx[4] = ttCHAR(comp);
1900 mtx[5] = ttCHAR(comp);
1907 if (flags & (1 << 3)) {
1908 mtx[0] = mtx[3] = ttSHORT(comp) / 16384.0f;
1910 mtx[1] = mtx[2] = 0;
1911 }
else if (flags & (1 << 6)) {
1912 mtx[0] = ttSHORT(comp) / 16384.0f;
1914 mtx[1] = mtx[2] = 0;
1915 mtx[3] = ttSHORT(comp) / 16384.0f;
1917 }
else if (flags & (1 << 7)) {
1918 mtx[0] = ttSHORT(comp) / 16384.0f;
1920 mtx[1] = ttSHORT(comp) / 16384.0f;
1922 mtx[2] = ttSHORT(comp) / 16384.0f;
1924 mtx[3] = ttSHORT(comp) / 16384.0f;
1929 m = (float)STBTT_sqrt(mtx[0] * mtx[0] + mtx[1] * mtx[1]);
1930 n = (float)STBTT_sqrt(mtx[2] * mtx[2] + mtx[3] * mtx[3]);
1933 comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
1934 if (comp_num_verts > 0) {
1936 for (i = 0; i < comp_num_verts; ++i) {
1937 stbtt_vertex *v = &comp_verts[i];
1938 stbtt_vertex_type x, y;
1941 v->x = (stbtt_vertex_type)(m * (mtx[0] * x + mtx[2] * y + mtx[4]));
1942 v->y = (stbtt_vertex_type)(n * (mtx[1] * x + mtx[3] * y + mtx[5]));
1945 v->cx = (stbtt_vertex_type)(m * (mtx[0] * x + mtx[2] * y + mtx[4]));
1946 v->cy = (stbtt_vertex_type)(n * (mtx[1] * x + mtx[3] * y + mtx[5]));
1949 tmp = (stbtt_vertex *)STBTT_malloc((num_vertices + comp_num_verts) *
sizeof(stbtt_vertex), info->userdata);
1952 STBTT_free(vertices, info->userdata);
1954 STBTT_free(comp_verts, info->userdata);
1957 if (num_vertices > 0 && vertices)
1958 STBTT_memcpy(tmp, vertices, num_vertices *
sizeof(stbtt_vertex));
1959 STBTT_memcpy(tmp + num_vertices, comp_verts, comp_num_verts *
sizeof(stbtt_vertex));
1961 STBTT_free(vertices, info->userdata);
1963 STBTT_free(comp_verts, info->userdata);
1964 num_vertices += comp_num_verts;
1967 more = flags & (1 << 5);
1973 *pvertices = vertices;
1974 return num_vertices;
1980 float first_x, first_y;
1982 stbtt_int32 min_x, max_x, min_y, max_y;
1984 stbtt_vertex *pvertices;
1988 #define STBTT__CSCTX_INIT(bounds) \
1990 bounds, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr, 0 \
1993 static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y)
1995 if (x > c->max_x || !c->started)
1997 if (y > c->max_y || !c->started)
1999 if (x < c->min_x || !c->started)
2001 if (y < c->min_y || !c->started)
2006 static void stbtt__csctx_v(stbtt__csctx *c, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx,
2007 stbtt_int32 cy, stbtt_int32 cx1, stbtt_int32 cy1)
2010 stbtt__track_vertex(c, x, y);
2011 if (type == STBTT_vcubic) {
2012 stbtt__track_vertex(c, cx, cy);
2013 stbtt__track_vertex(c, cx1, cy1);
2016 stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);
2017 c->pvertices[c->num_vertices].cx1 = (stbtt_int16)cx1;
2018 c->pvertices[c->num_vertices].cy1 = (stbtt_int16)cy1;
2023 static void stbtt__csctx_close_shape(stbtt__csctx *ctx)
2025 if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)
2026 stbtt__csctx_v(ctx, STBTT_vline, (
int)ctx->first_x, (
int)ctx->first_y, 0, 0, 0, 0);
2029 static void stbtt__csctx_rmove_to(stbtt__csctx *ctx,
float dx,
float dy)
2031 stbtt__csctx_close_shape(ctx);
2032 ctx->first_x = ctx->x = ctx->x + dx;
2033 ctx->first_y = ctx->y = ctx->y + dy;
2034 stbtt__csctx_v(ctx, STBTT_vmove, (
int)ctx->x, (
int)ctx->y, 0, 0, 0, 0);
2037 static void stbtt__csctx_rline_to(stbtt__csctx *ctx,
float dx,
float dy)
2041 stbtt__csctx_v(ctx, STBTT_vline, (
int)ctx->x, (
int)ctx->y, 0, 0, 0, 0);
2044 static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx,
float dx1,
float dy1,
float dx2,
float dy2,
float dx3,
float dy3)
2046 float cx1 = ctx->x + dx1;
2047 float cy1 = ctx->y + dy1;
2048 float cx2 = cx1 + dx2;
2049 float cy2 = cy1 + dy2;
2052 stbtt__csctx_v(ctx, STBTT_vcubic, (
int)ctx->x, (
int)ctx->y, (
int)cx1, (
int)cy1, (
int)cx2, (
int)cy2);
2055 static stbtt__buf stbtt__get_subr(stbtt__buf idx,
int n)
2057 int count = stbtt__cff_index_count(&idx);
2061 else if (count >= 1240)
2064 if (n < 0 || n >= count)
2065 return stbtt__new_buf(
nullptr, 0);
2066 return stbtt__cff_index_get(idx, n);
2069 static stbtt__buf stbtt__cid_get_glyph_subrs(
const stbtt_fontinfo *info,
int glyph_index)
2071 stbtt__buf fdselect = info->fdselect;
2072 int nranges, start, end, v, fmt, fdselector = -1, i;
2074 stbtt__buf_seek(&fdselect, 0);
2075 fmt = stbtt__buf_get8(&fdselect);
2078 stbtt__buf_skip(&fdselect, glyph_index);
2079 fdselector = stbtt__buf_get8(&fdselect);
2080 }
else if (fmt == 3) {
2081 nranges = stbtt__buf_get16(&fdselect);
2082 start = stbtt__buf_get16(&fdselect);
2083 for (i = 0; i < nranges; i++) {
2084 v = stbtt__buf_get8(&fdselect);
2085 end = stbtt__buf_get16(&fdselect);
2086 if (glyph_index >= start && glyph_index < end) {
2093 if (fdselector == -1)
2094 stbtt__new_buf(
nullptr, 0);
2095 return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));
2098 static int stbtt__run_charstring(
const stbtt_fontinfo *info,
int glyph_index, stbtt__csctx *c)
2100 int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;
2101 int has_subrs = 0, clear_stack;
2103 stbtt__buf subr_stack[10], subrs = info->subrs, b;
2106 #define STBTT__CSERR(s) (0)
2109 b = stbtt__cff_index_get(info->charstrings, glyph_index);
2110 while (b.cursor < b.size) {
2113 b0 = stbtt__buf_get8(&b);
2119 maskbits += (sp / 2);
2121 stbtt__buf_skip(&b, (maskbits + 7) / 8);
2128 maskbits += (sp / 2);
2134 return STBTT__CSERR(
"rmoveto stack");
2135 stbtt__csctx_rmove_to(c, s[sp - 2], s[sp - 1]);
2140 return STBTT__CSERR(
"vmoveto stack");
2141 stbtt__csctx_rmove_to(c, 0, s[sp - 1]);
2146 return STBTT__CSERR(
"hmoveto stack");
2147 stbtt__csctx_rmove_to(c, s[sp - 1], 0);
2152 return STBTT__CSERR(
"rlineto stack");
2153 for (; i + 1 < sp; i += 2)
2154 stbtt__csctx_rline_to(c, s[i], s[i + 1]);
2162 return STBTT__CSERR(
"vlineto stack");
2166 return STBTT__CSERR(
"hlineto stack");
2170 stbtt__csctx_rline_to(c, s[i], 0);
2175 stbtt__csctx_rline_to(c, 0, s[i]);
2182 return STBTT__CSERR(
"hvcurveto stack");
2186 return STBTT__CSERR(
"vhcurveto stack");
2190 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);
2195 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]);
2202 return STBTT__CSERR(
"rcurveline stack");
2203 for (; i + 5 < sp; i += 6)
2204 stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
2209 return STBTT__CSERR(
"rcurveline stack");
2210 for (; i + 5 < sp - 2; i += 6)
2211 stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
2213 return STBTT__CSERR(
"rcurveline stack");
2214 stbtt__csctx_rline_to(c, s[i], s[i + 1]);
2219 return STBTT__CSERR(
"rlinecurve stack");
2220 for (; i + 1 < sp - 6; i += 2)
2221 stbtt__csctx_rline_to(c, s[i], s[i + 1]);
2223 return STBTT__CSERR(
"rlinecurve stack");
2224 stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
2230 return STBTT__CSERR(
"(vv|hh)curveto stack");
2236 for (; i + 3 < sp; i += 4) {
2238 stbtt__csctx_rccurve_to(c, s[i], f, s[i + 1], s[i + 2], s[i + 3], 0.0);
2240 stbtt__csctx_rccurve_to(c, f, s[i], s[i + 1], s[i + 2], 0.0, s[i + 3]);
2247 if (info->fdselect.size)
2248 subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);
2254 return STBTT__CSERR(
"call(g|)subr stack");
2256 if (subr_stack_height >= 10)
2257 return STBTT__CSERR(
"recursion limit");
2258 subr_stack[subr_stack_height++] = b;
2259 b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v);
2261 return STBTT__CSERR(
"subr not found");
2267 if (subr_stack_height <= 0)
2268 return STBTT__CSERR(
"return outside subr");
2269 b = subr_stack[--subr_stack_height];
2274 stbtt__csctx_close_shape(c);
2278 float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;
2280 int b1 = stbtt__buf_get8(&b);
2286 return STBTT__CSERR(
"hflex stack");
2294 stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);
2295 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);
2300 return STBTT__CSERR(
"flex stack");
2314 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
2315 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
2320 return STBTT__CSERR(
"hflex1 stack");
2330 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);
2331 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1 + dy2 + dy5));
2336 return STBTT__CSERR(
"flex1 stack");
2348 dx = dx1 + dx2 + dx3 + dx4 + dx5;
2349 dy = dy1 + dy2 + dy3 + dy4 + dy5;
2350 if (STBTT_fabs(dx) > STBTT_fabs(dy))
2354 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
2355 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
2359 return STBTT__CSERR(
"unimplemented");
2364 if (b0 != 255 && b0 != 28 && b0 < 32)
2365 return STBTT__CSERR(
"reserved operator");
2369 f = (float)(stbtt_int32)stbtt__buf_get32(&b) / 0x10000;
2371 stbtt__buf_skip(&b, -1);
2372 f = (float)(stbtt_int16)stbtt__cff_int(&b);
2375 return STBTT__CSERR(
"push stack overflow");
2383 return STBTT__CSERR(
"no endchar");
2388 static int stbtt__GetGlyphShapeT2(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
2391 stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);
2392 stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);
2393 if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {
2394 *pvertices = (stbtt_vertex *)STBTT_malloc(count_ctx.num_vertices *
sizeof(stbtt_vertex), info->userdata);
2395 output_ctx.pvertices = *pvertices;
2396 if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {
2397 STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);
2398 return output_ctx.num_vertices;
2401 *pvertices =
nullptr;
2405 static int stbtt__GetGlyphInfoT2(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1)
2407 stbtt__csctx c = STBTT__CSCTX_INIT(1);
2408 int r = stbtt__run_charstring(info, glyph_index, &c);
2410 *x0 = r ? c.min_x : 0;
2412 *y0 = r ? c.min_y : 0;
2414 *x1 = r ? c.max_x : 0;
2416 *y1 = r ? c.max_y : 0;
2417 return r ? c.num_vertices : 0;
2420 STBTT_DEF
int stbtt_GetGlyphShape(
const stbtt_fontinfo *info,
int glyph_index, stbtt_vertex **pvertices)
2422 if (!info->cff.size)
2423 return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);
2425 return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);
2428 STBTT_DEF
void stbtt_GetGlyphHMetrics(
const stbtt_fontinfo *info,
int glyph_index,
int *advanceWidth,
2429 int *leftSideBearing)
2431 stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data + info->hhea + 34);
2432 if (glyph_index < numOfLongHorMetrics) {
2434 *advanceWidth = ttSHORT(info->data + info->hmtx + 4 * glyph_index);
2435 if (leftSideBearing)
2436 *leftSideBearing = ttSHORT(info->data + info->hmtx + 4 * glyph_index + 2);
2439 *advanceWidth = ttSHORT(info->data + info->hmtx + 4 * (numOfLongHorMetrics - 1));
2440 if (leftSideBearing)
2442 ttSHORT(info->data + info->hmtx + 4 * numOfLongHorMetrics + 2 * (glyph_index - numOfLongHorMetrics));
2446 STBTT_DEF
int stbtt_GetKerningTableLength(
const stbtt_fontinfo *info)
2448 stbtt_uint8 *data = info->data + info->kern;
2453 if (ttUSHORT(data + 2) < 1)
2455 if (ttUSHORT(data + 8) != 1)
2458 return ttUSHORT(data + 10);
2461 STBTT_DEF
int stbtt_GetKerningTable(
const stbtt_fontinfo *info, stbtt_kerningentry *table,
int table_length)
2463 stbtt_uint8 *data = info->data + info->kern;
2469 if (ttUSHORT(data + 2) < 1)
2471 if (ttUSHORT(data + 8) != 1)
2474 length = ttUSHORT(data + 10);
2475 if (table_length < length)
2476 length = table_length;
2478 for (k = 0; k < length; k++) {
2479 table[k].glyph1 = ttUSHORT(data + 18 + (k * 6));
2480 table[k].glyph2 = ttUSHORT(data + 20 + (k * 6));
2481 table[k].advance = ttSHORT(data + 22 + (k * 6));
2487 static int stbtt__GetGlyphKernInfoAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2)
2489 stbtt_uint8 *data = info->data + info->kern;
2490 stbtt_uint32 needle, straw;
2496 if (ttUSHORT(data + 2) < 1)
2498 if (ttUSHORT(data + 8) != 1)
2502 r = ttUSHORT(data + 10) - 1;
2503 needle = glyph1 << 16 | glyph2;
2506 straw = ttULONG(data + 18 + (m * 6));
2509 else if (needle > straw)
2512 return ttSHORT(data + 22 + (m * 6));
2517 static stbtt_int32 stbtt__GetCoverageIndex(stbtt_uint8 *coverageTable,
int glyph)
2519 stbtt_uint16 coverageFormat = ttUSHORT(coverageTable);
2520 switch (coverageFormat) {
2522 stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2);
2525 stbtt_int32 l = 0, r = glyphCount - 1, m;
2526 int straw, needle = glyph;
2528 stbtt_uint8 *glyphArray = coverageTable + 4;
2529 stbtt_uint16 glyphID;
2531 glyphID = ttUSHORT(glyphArray + 2 * m);
2535 else if (needle > straw)
2545 stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2);
2546 stbtt_uint8 *rangeArray = coverageTable + 4;
2549 stbtt_int32 l = 0, r = rangeCount - 1, m;
2550 int strawStart, strawEnd, needle = glyph;
2552 stbtt_uint8 *rangeRecord;
2554 rangeRecord = rangeArray + 6 * m;
2555 strawStart = ttUSHORT(rangeRecord);
2556 strawEnd = ttUSHORT(rangeRecord + 2);
2557 if (needle < strawStart)
2559 else if (needle > strawEnd)
2562 stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4);
2563 return startCoverageIndex + glyph - strawStart;
2576 static stbtt_int32 stbtt__GetGlyphClass(stbtt_uint8 *classDefTable,
int glyph)
2578 stbtt_uint16 classDefFormat = ttUSHORT(classDefTable);
2579 switch (classDefFormat) {
2581 stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2);
2582 stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4);
2583 stbtt_uint8 *classDef1ValueArray = classDefTable + 6;
2585 if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount)
2586 return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID));
2591 stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2);
2592 stbtt_uint8 *classRangeRecords = classDefTable + 4;
2595 stbtt_int32 l = 0, r = classRangeCount - 1, m;
2596 int strawStart, strawEnd, needle = glyph;
2598 stbtt_uint8 *classRangeRecord;
2600 classRangeRecord = classRangeRecords + 6 * m;
2601 strawStart = ttUSHORT(classRangeRecord);
2602 strawEnd = ttUSHORT(classRangeRecord + 2);
2603 if (needle < strawStart)
2605 else if (needle > strawEnd)
2608 return (stbtt_int32)ttUSHORT(classRangeRecord + 4);
2622 #define STBTT_GPOS_TODO_assert(x)
2624 static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(
const stbtt_fontinfo *info,
int glyph1,
int glyph2)
2626 stbtt_uint16 lookupListOffset;
2627 stbtt_uint8 *lookupList;
2628 stbtt_uint16 lookupCount;
2635 data = info->data + info->gpos;
2637 if (ttUSHORT(data + 0) != 1)
2639 if (ttUSHORT(data + 2) != 0)
2642 lookupListOffset = ttUSHORT(data + 8);
2643 lookupList = data + lookupListOffset;
2644 lookupCount = ttUSHORT(lookupList);
2646 for (i = 0; i < lookupCount; ++i) {
2647 stbtt_uint16 lookupOffset = ttUSHORT(lookupList + 2 + 2 * i);
2648 stbtt_uint8 *lookupTable = lookupList + lookupOffset;
2650 stbtt_uint16 lookupType = ttUSHORT(lookupTable);
2651 stbtt_uint16 subTableCount = ttUSHORT(lookupTable + 4);
2652 stbtt_uint8 *subTableOffsets = lookupTable + 6;
2653 if (lookupType != 2)
2656 for (sti = 0; sti < subTableCount; sti++) {
2657 stbtt_uint16 subtableOffset = ttUSHORT(subTableOffsets + 2 * sti);
2658 stbtt_uint8 *table = lookupTable + subtableOffset;
2659 stbtt_uint16 posFormat = ttUSHORT(table);
2660 stbtt_uint16 coverageOffset = ttUSHORT(table + 2);
2661 stbtt_int32 coverageIndex = stbtt__GetCoverageIndex(table + coverageOffset, glyph1);
2662 if (coverageIndex == -1)
2665 switch (posFormat) {
2667 stbtt_int32 l, r, m;
2669 stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
2670 stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
2671 if (valueFormat1 == 4 && valueFormat2 == 0) {
2672 stbtt_int32 valueRecordPairSizeInBytes = 2;
2673 stbtt_uint16 pairSetCount = ttUSHORT(table + 8);
2674 stbtt_uint16 pairPosOffset = ttUSHORT(table + 10 + 2 * coverageIndex);
2675 stbtt_uint8 *pairValueTable = table + pairPosOffset;
2676 stbtt_uint16 pairValueCount = ttUSHORT(pairValueTable);
2677 stbtt_uint8 *pairValueArray = pairValueTable + 2;
2679 if (coverageIndex >= pairSetCount)
2683 r = pairValueCount - 1;
2688 stbtt_uint16 secondGlyph;
2689 stbtt_uint8 *pairValue;
2691 pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m;
2692 secondGlyph = ttUSHORT(pairValue);
2693 straw = secondGlyph;
2696 else if (needle > straw)
2699 stbtt_int16 xAdvance = ttSHORT(pairValue + 2);
2709 stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
2710 stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
2711 if (valueFormat1 == 4 && valueFormat2 == 0) {
2712 stbtt_uint16 classDef1Offset = ttUSHORT(table + 8);
2713 stbtt_uint16 classDef2Offset = ttUSHORT(table + 10);
2714 int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1);
2715 int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2);
2717 stbtt_uint16 class1Count = ttUSHORT(table + 12);
2718 stbtt_uint16 class2Count = ttUSHORT(table + 14);
2719 stbtt_uint8 *class1Records, *class2Records;
2720 stbtt_int16 xAdvance;
2722 if (glyph1class < 0 || glyph1class >= class1Count)
2724 if (glyph2class < 0 || glyph2class >= class2Count)
2727 class1Records = table + 16;
2728 class2Records = class1Records + 2 * (glyph1class * class2Count);
2729 xAdvance = ttSHORT(class2Records + 2 * glyph2class);
2745 STBTT_DEF
int stbtt_GetGlyphKernAdvance(
const stbtt_fontinfo *info,
int g1,
int g2)
2750 xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2);
2751 else if (info->kern)
2752 xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2);
2757 STBTT_DEF
int stbtt_GetCodepointKernAdvance(
const stbtt_fontinfo *info,
int ch1,
int ch2)
2759 if (!info->kern && !info->gpos)
2761 return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info, ch1), stbtt_FindGlyphIndex(info, ch2));
2764 STBTT_DEF
void stbtt_GetCodepointHMetrics(
const stbtt_fontinfo *info,
int codepoint,
int *advanceWidth,
2765 int *leftSideBearing)
2767 stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info, codepoint), advanceWidth, leftSideBearing);
2770 STBTT_DEF
void stbtt_GetFontVMetrics(
const stbtt_fontinfo *info,
int *ascent,
int *descent,
int *lineGap)
2773 *ascent = ttSHORT(info->data + info->hhea + 4);
2775 *descent = ttSHORT(info->data + info->hhea + 6);
2777 *lineGap = ttSHORT(info->data + info->hhea + 8);
2780 STBTT_DEF
int stbtt_GetFontVMetricsOS2(
const stbtt_fontinfo *info,
int *typoAscent,
int *typoDescent,
int *typoLineGap)
2782 int tab = stbtt__find_table(info->data, info->fontstart,
"OS/2");
2786 *typoAscent = ttSHORT(info->data + tab + 68);
2788 *typoDescent = ttSHORT(info->data + tab + 70);
2790 *typoLineGap = ttSHORT(info->data + tab + 72);
2794 STBTT_DEF
void stbtt_GetFontBoundingBox(
const stbtt_fontinfo *info,
int *x0,
int *y0,
int *x1,
int *y1)
2796 *x0 = ttSHORT(info->data + info->head + 36);
2797 *y0 = ttSHORT(info->data + info->head + 38);
2798 *x1 = ttSHORT(info->data + info->head + 40);
2799 *y1 = ttSHORT(info->data + info->head + 42);
2802 STBTT_DEF
float stbtt_ScaleForPixelHeight(
const stbtt_fontinfo *info,
float height)
2804 int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
2805 return (
float)height / fheight;
2808 STBTT_DEF
float stbtt_ScaleForMappingEmToPixels(
const stbtt_fontinfo *info,
float pixels)
2810 int unitsPerEm = ttUSHORT(info->data + info->head + 18);
2811 return pixels / unitsPerEm;
2814 STBTT_DEF
void stbtt_FreeShape(
const stbtt_fontinfo *info, stbtt_vertex *v) { STBTT_free(v, info->userdata); }
2816 STBTT_DEF stbtt_uint8 *stbtt_FindSVGDoc(
const stbtt_fontinfo *info,
int gl)
2819 stbtt_uint8 *data = info->data;
2820 stbtt_uint8 *svg_doc_list = data + stbtt__get_svg((stbtt_fontinfo *)info);
2822 int numEntries = ttUSHORT(svg_doc_list);
2823 stbtt_uint8 *svg_docs = svg_doc_list + 2;
2825 for (i = 0; i < numEntries; i++) {
2826 stbtt_uint8 *svg_doc = svg_docs + (12 * i);
2827 if ((gl >= ttUSHORT(svg_doc)) && (gl <= ttUSHORT(svg_doc + 2)))
2833 STBTT_DEF
int stbtt_GetGlyphSVG(
const stbtt_fontinfo *info,
int gl,
const char **svg)
2835 stbtt_uint8 *data = info->data;
2836 stbtt_uint8 *svg_doc;
2841 svg_doc = stbtt_FindSVGDoc(info, gl);
2842 if (svg_doc !=
nullptr) {
2843 *svg = (
char *)data + info->svg + ttULONG(svg_doc + 4);
2844 return ttULONG(svg_doc + 8);
2850 STBTT_DEF
int stbtt_GetCodepointSVG(
const stbtt_fontinfo *info,
int unicode_codepoint,
const char **svg)
2852 return stbtt_GetGlyphSVG(info, stbtt_FindGlyphIndex(info, unicode_codepoint), svg);
2860 STBTT_DEF
void stbtt_GetGlyphBitmapBoxSubpixel(
const stbtt_fontinfo *font,
int glyph,
float scale_x,
float scale_y,
2861 float shift_x,
float shift_y,
int *ix0,
int *iy0,
int *ix1,
int *iy1)
2863 int x0 = 0, y0 = 0, x1, y1;
2864 if (!stbtt_GetGlyphBox(font, glyph, &x0, &y0, &x1, &y1)) {
2877 *ix0 = STBTT_ifloor(x0 * scale_x + shift_x);
2879 *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
2881 *ix1 = STBTT_iceil(x1 * scale_x + shift_x);
2883 *iy1 = STBTT_iceil(-y0 * scale_y + shift_y);
2887 STBTT_DEF
void stbtt_GetGlyphBitmapBox(
const stbtt_fontinfo *font,
int glyph,
float scale_x,
float scale_y,
int *ix0,
2888 int *iy0,
int *ix1,
int *iy1)
2890 stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1);
2893 STBTT_DEF
void stbtt_GetCodepointBitmapBoxSubpixel(
const stbtt_fontinfo *font,
int codepoint,
float scale_x,
2894 float scale_y,
float shift_x,
float shift_y,
int *ix0,
int *iy0,
2897 stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font, codepoint), scale_x, scale_y, shift_x, shift_y, ix0,
2901 STBTT_DEF
void stbtt_GetCodepointBitmapBox(
const stbtt_fontinfo *font,
int codepoint,
float scale_x,
float scale_y,
2902 int *ix0,
int *iy0,
int *ix1,
int *iy1)
2904 stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1);
2911 typedef struct stbtt__hheap_chunk {
2912 struct stbtt__hheap_chunk *next;
2913 } stbtt__hheap_chunk;
2915 typedef struct stbtt__hheap {
2916 struct stbtt__hheap_chunk *head;
2918 int num_remaining_in_head_chunk;
2921 static void *stbtt__hheap_alloc(stbtt__hheap *hh,
size_t size,
void *userdata)
2923 if (hh->first_free) {
2924 void *p = hh->first_free;
2925 hh->first_free = *(
void **)p;
2928 if (hh->num_remaining_in_head_chunk == 0) {
2929 int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
2930 stbtt__hheap_chunk *c = (stbtt__hheap_chunk *)STBTT_malloc(
sizeof(stbtt__hheap_chunk) + size * count, userdata);
2935 hh->num_remaining_in_head_chunk = count;
2937 --hh->num_remaining_in_head_chunk;
2938 return (
char *)(hh->head) +
sizeof(stbtt__hheap_chunk) + size * hh->num_remaining_in_head_chunk;
2942 static void stbtt__hheap_free(stbtt__hheap *hh,
void *p)
2944 *(
void **)p = hh->first_free;
2948 static void stbtt__hheap_cleanup(stbtt__hheap *hh,
void *userdata)
2950 stbtt__hheap_chunk *c = hh->head;
2952 stbtt__hheap_chunk *n = c->next;
2953 STBTT_free(c, userdata);
2958 typedef struct stbtt__edge {
2959 float x0, y0, x1, y1;
2963 typedef struct stbtt__active_edge {
2964 struct stbtt__active_edge *next;
2965 #if STBTT_RASTERIZER_VERSION == 1
2969 #elif STBTT_RASTERIZER_VERSION == 2
2975 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2977 } stbtt__active_edge;
2979 #if STBTT_RASTERIZER_VERSION == 1
2980 #define STBTT_FIXSHIFT 10
2981 #define STBTT_FIX (1 << STBTT_FIXSHIFT)
2982 #define STBTT_FIXMASK (STBTT_FIX - 1)
2984 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
2987 stbtt__active_edge *z = (stbtt__active_edge *)stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
2988 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
2989 STBTT_assert(z !=
nullptr);
2995 z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);
2997 z->dx = STBTT_ifloor(STBTT_FIX * dxdy);
2999 z->x = STBTT_ifloor(STBTT_FIX * e->x0 +
3000 z->dx * (start_point - e->y0));
3001 z->x -= off_x * STBTT_FIX;
3005 z->direction = e->invert ? 1 : -1;
3008 #elif STBTT_RASTERIZER_VERSION == 2
3009 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
3012 stbtt__active_edge *z = (stbtt__active_edge *)stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
3013 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
3014 STBTT_assert(z !=
nullptr);
3019 z->fdy = dxdy != 0.0f ? (1.0f / dxdy) : 0.0f;
3020 z->fx = e->x0 + dxdy * (start_point - e->y0);
3022 z->direction = e->invert ? 1.0f : -1.0f;
3029 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
3032 #if STBTT_RASTERIZER_VERSION == 1
3036 static void stbtt__fill_active_edges(
unsigned char *scanline,
int len, stbtt__active_edge *e,
int max_weight)
3051 int i = x0 >> STBTT_FIXSHIFT;
3052 int j = x1 >> STBTT_FIXSHIFT;
3054 if (i < len && j >= 0) {
3057 scanline[i] = scanline[i] + (stbtt_uint8)((x1 - x0) * max_weight >> STBTT_FIXSHIFT);
3061 scanline[i] + (stbtt_uint8)(((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);
3066 scanline[j] = scanline[j] + (stbtt_uint8)(((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);
3070 for (++i; i < j; ++i)
3071 scanline[i] = scanline[i] + (stbtt_uint8)max_weight;
3081 static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
3082 int off_y,
void *userdata)
3084 stbtt__hheap hh = {0, 0, 0};
3085 stbtt__active_edge *active =
nullptr;
3087 int max_weight = (255 / vsubsample);
3089 unsigned char scanline_data[512], *scanline;
3091 if (result->w > 512)
3092 scanline = (
unsigned char *)STBTT_malloc(result->w, userdata);
3094 scanline = scanline_data;
3096 y = off_y * vsubsample;
3097 e[n].y0 = (off_y + result->h) * (
float)vsubsample + 1;
3099 while (j < result->h) {
3100 STBTT_memset(scanline, 0, result->w);
3101 for (s = 0; s < vsubsample; ++s) {
3103 float scan_y = y + 0.5f;
3104 stbtt__active_edge **step = &active;
3109 stbtt__active_edge *z = *step;
3110 if (z->ey <= scan_y) {
3112 STBTT_assert(z->direction);
3114 stbtt__hheap_free(&hh, z);
3117 step = &((*step)->next);
3125 while (*step && (*step)->next) {
3126 if ((*step)->x > (*step)->next->x) {
3127 stbtt__active_edge *t = *step;
3128 stbtt__active_edge *q = t->next;
3135 step = &(*step)->next;
3142 while (e->y0 <= scan_y) {
3143 if (e->y1 > scan_y) {
3144 stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);
3147 if (active ==
nullptr)
3149 else if (z->x < active->x) {
3155 stbtt__active_edge *p = active;
3156 while (p->next && p->next->x < z->x)
3169 stbtt__fill_active_edges(scanline, result->w, active, max_weight);
3173 STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);
3177 stbtt__hheap_cleanup(&hh, userdata);
3179 if (scanline != scanline_data)
3180 STBTT_free(scanline, userdata);
3183 #elif STBTT_RASTERIZER_VERSION == 2
3187 static void stbtt__handle_clipped_edge(
float *scanline,
int x, stbtt__active_edge *e,
float x0,
float y0,
float x1,
3192 STBTT_assert(y0 < y1);
3193 STBTT_assert(e->sy <= e->ey);
3199 x0 += (x1 - x0) * (e->sy - y0) / (y1 - y0);
3203 x1 += (x1 - x0) * (e->ey - y1) / (y1 - y0);
3208 STBTT_assert(x1 <= x + 1);
3209 else if (x0 == x + 1)
3210 STBTT_assert(x1 >= x);
3212 STBTT_assert(x1 <= x);
3213 else if (x0 >= x + 1)
3214 STBTT_assert(x1 >= x + 1);
3216 STBTT_assert(x1 >= x && x1 <= x + 1);
3218 if (x0 <= x && x1 <= x)
3219 scanline[x] += e->direction * (y1 - y0);
3220 else if (x0 >= x + 1 && x1 >= x + 1)
3223 STBTT_assert(x0 >= x && x0 <= x + 1 && x1 >= x && x1 <= x + 1);
3224 scanline[x] += e->direction * (y1 - y0) * (1 - ((x0 - x) + (x1 - x)) / 2);
3228 static float stbtt__sized_trapezoid_area(
float height,
float top_width,
float bottom_width)
3230 STBTT_assert(top_width >= 0);
3231 STBTT_assert(bottom_width >= 0);
3232 return (top_width + bottom_width) / 2.0f * height;
3235 static float stbtt__position_trapezoid_area(
float height,
float tx0,
float tx1,
float bx0,
float bx1)
3237 return stbtt__sized_trapezoid_area(height, tx1 - tx0, bx1 - bx0);
3240 static float stbtt__sized_triangle_area(
float height,
float width) {
return height * width / 2; }
3242 static void stbtt__fill_active_edges_new(
float *scanline,
float *scanline_fill,
int len, stbtt__active_edge *e,
3245 float y_bottom = y_top + 1;
3251 STBTT_assert(e->ey >= y_top);
3257 stbtt__handle_clipped_edge(scanline, (
int)x0, e, x0, y_top, x0, y_bottom);
3258 stbtt__handle_clipped_edge(scanline_fill - 1, (
int)x0 + 1, e, x0, y_top, x0, y_bottom);
3260 stbtt__handle_clipped_edge(scanline_fill - 1, 0, e, x0, y_top, x0, y_bottom);
3267 float x_top, x_bottom;
3270 STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);
3275 if (e->sy > y_top) {
3276 x_top = x0 + dx * (e->sy - y_top);
3282 if (e->ey < y_bottom) {
3283 x_bottom = x0 + dx * (e->ey - y_top);
3290 if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {
3293 if ((
int)x_top == (int)x_bottom) {
3297 height = (sy1 - sy0) * e->direction;
3298 STBTT_assert(x >= 0 && x < len);
3299 scanline[x] += stbtt__position_trapezoid_area(height, x_top, x + 1.0f, x_bottom, x + 1.0f);
3300 scanline_fill[x] += height;
3303 float y_crossing, y_final, step, sign, area;
3305 if (x_top > x_bottom) {
3308 sy0 = y_bottom - (sy0 - y_top);
3309 sy1 = y_bottom - (sy1 - y_top);
3310 t = sy0, sy0 = sy1, sy1 = t;
3311 t = x_bottom, x_bottom = x_top, x_top = t;
3314 t = x0, x0 = xb, xb = t;
3316 STBTT_assert(dy >= 0);
3317 STBTT_assert(dx >= 0);
3322 y_crossing = y_top + dy * (x1 + 1 - x0);
3325 y_final = y_top + dy * (x2 - x0);
3346 if (y_crossing > y_bottom)
3347 y_crossing = y_bottom;
3349 sign = e->direction;
3352 area = sign * (y_crossing - sy0);
3355 scanline[x1] += stbtt__sized_triangle_area(area, x1 + 1 - x_top);
3358 if (y_final > y_bottom) {
3360 dy = (y_final - y_crossing) / (x2 - (x1 + 1));
3373 step = sign * dy * 1;
3377 for (x = x1 + 1; x < x2; ++x) {
3378 scanline[x] += area + step / 2;
3381 STBTT_assert(STBTT_fabs(area) <= 1.01f);
3382 STBTT_assert(sy1 > y_final - 0.01f);
3387 area + sign * stbtt__position_trapezoid_area(sy1 - y_final, (
float)x2, x2 + 1.0f, x_bottom, x2 + 1.0f);
3390 scanline_fill[x2] += sign * (sy1 - sy0);
3401 for (x = 0; x < len; ++x) {
3417 float x1 = (float)(x);
3418 float x2 = (float)(x + 1);
3420 float y3 = y_bottom;
3425 float y1 = (x - x0) / dx + y_top;
3426 float y2 = (x + 1 - x0) / dx + y_top;
3428 if (x0 < x1 && x3 > x2) {
3429 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
3430 stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x2, y2);
3431 stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
3432 }
else if (x3 < x1 && x0 > x2) {
3433 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
3434 stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x1, y1);
3435 stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
3436 }
else if (x0 < x1 && x3 > x1) {
3437 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
3438 stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
3439 }
else if (x3 < x1 && x0 > x1) {
3440 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
3441 stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
3442 }
else if (x0 < x2 && x3 > x2) {
3443 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
3444 stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
3445 }
else if (x3 < x2 && x0 > x2) {
3446 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
3447 stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
3449 stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x3, y3);
3459 static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
3460 int off_y,
void *userdata)
3462 stbtt__hheap hh = {0, 0, 0};
3463 stbtt__active_edge *active =
nullptr;
3465 float scanline_data[129], *scanline, *scanline2;
3467 STBTT__NOTUSED(vsubsample);
3470 scanline = (
float *)STBTT_malloc((result->w * 2 + 1) *
sizeof(float), userdata);
3472 scanline = scanline_data;
3474 scanline2 = scanline + result->w;
3477 e[n].y0 = (float)(off_y + result->h) + 1;
3479 while (j < result->h) {
3481 float scan_y_top = y + 0.0f;
3482 float scan_y_bottom = y + 1.0f;
3483 stbtt__active_edge **step = &active;
3485 STBTT_memset(scanline, 0, result->w *
sizeof(scanline[0]));
3486 STBTT_memset(scanline2, 0, (result->w + 1) *
sizeof(scanline[0]));
3491 stbtt__active_edge *z = *step;
3492 if (z->ey <= scan_y_top) {
3494 STBTT_assert(z->direction);
3496 stbtt__hheap_free(&hh, z);
3498 step = &((*step)->next);
3503 while (e->y0 <= scan_y_bottom) {
3504 if (e->y0 != e->y1) {
3505 stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
3507 if (j == 0 && off_y != 0) {
3508 if (z->ey < scan_y_top) {
3513 STBTT_assert(z->ey >= scan_y_top);
3524 stbtt__fill_active_edges_new(scanline, scanline2 + 1, result->w, active, scan_y_top);
3528 for (i = 0; i < result->w; ++i) {
3531 sum += scanline2[i];
3532 k = scanline[i] + sum;
3533 k = (float)STBTT_fabs(k) * 255 + 0.5f;
3537 result->pixels[j * result->stride + i] = (
unsigned char)m;
3543 stbtt__active_edge *z = *step;
3545 step = &((*step)->next);
3552 stbtt__hheap_cleanup(&hh, userdata);
3554 if (scanline != scanline_data)
3555 STBTT_free(scanline, userdata);
3558 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
3561 #define STBTT__COMPARE(a, b) ((a)->y0 < (b)->y0)
3563 static void stbtt__sort_edges_ins_sort(stbtt__edge *p,
int n)
3566 for (i = 1; i < n; ++i) {
3567 stbtt__edge t = p[i], *a = &t;
3570 stbtt__edge *b = &p[j - 1];
3571 int c = STBTT__COMPARE(a, b);
3582 static void stbtt__sort_edges_quicksort(stbtt__edge *p,
int n)
3587 int c01, c12, c, m, i, j;
3591 c01 = STBTT__COMPARE(&p[0], &p[m]);
3592 c12 = STBTT__COMPARE(&p[m], &p[n - 1]);
3597 c = STBTT__COMPARE(&p[0], &p[n - 1]);
3600 z = (c == c12) ? 0 : n - 1;
3618 if (!STBTT__COMPARE(&p[i], &p[0]))
3622 if (!STBTT__COMPARE(&p[0], &p[j]))
3637 stbtt__sort_edges_quicksort(p, j);
3641 stbtt__sort_edges_quicksort(p + i, n - i);
3647 static void stbtt__sort_edges(stbtt__edge *p,
int n)
3649 stbtt__sort_edges_quicksort(p, n);
3650 stbtt__sort_edges_ins_sort(p, n);
3657 static void stbtt__rasterize(stbtt__bitmap *result, stbtt__point *pts,
int *wcount,
int windings,
float scale_x,
3658 float scale_y,
float shift_x,
float shift_y,
int off_x,
int off_y,
int invert,
3661 float y_scale_inv = invert ? -scale_y : scale_y;
3664 #if STBTT_RASTERIZER_VERSION == 1
3665 int vsubsample = result->h < 8 ? 15 : 5;
3666 #elif STBTT_RASTERIZER_VERSION == 2
3669 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
3675 for (i = 0; i < windings; ++i)
3678 e = (stbtt__edge *)STBTT_malloc(
sizeof(*e) * (n + 1), userdata);
3684 for (i = 0; i < windings; ++i) {
3685 stbtt__point *p = pts + m;
3688 for (k = 0; k < wcount[i]; j = k++) {
3691 if (p[j].y == p[k].y)
3695 if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
3699 e[n].x0 = p[a].x * scale_x + shift_x;
3700 e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
3701 e[n].x1 = p[b].x * scale_x + shift_x;
3702 e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
3709 stbtt__sort_edges(e, n);
3712 stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
3714 STBTT_free(e, userdata);
3717 static void stbtt__add_point(stbtt__point *points,
int n,
float x,
float y)
3726 static int stbtt__tesselate_curve(stbtt__point *points,
int *num_points,
float x0,
float y0,
float x1,
float y1,
3727 float x2,
float y2,
float objspace_flatness_squared,
int n)
3730 float mx = (x0 + 2 * x1 + x2) / 4;
3731 float my = (y0 + 2 * y1 + y2) / 4;
3733 float dx = (x0 + x2) / 2 - mx;
3734 float dy = (y0 + y2) / 2 - my;
3737 if (dx * dx + dy * dy > objspace_flatness_squared) {
3738 stbtt__tesselate_curve(points, num_points, x0, y0, (x0 + x1) / 2.0f, (y0 + y1) / 2.0f, mx, my,
3739 objspace_flatness_squared, n + 1);
3740 stbtt__tesselate_curve(points, num_points, mx, my, (x1 + x2) / 2.0f, (y1 + y2) / 2.0f, x2, y2,
3741 objspace_flatness_squared, n + 1);
3743 stbtt__add_point(points, *num_points, x2, y2);
3744 *num_points = *num_points + 1;
3749 static void stbtt__tesselate_cubic(stbtt__point *points,
int *num_points,
float x0,
float y0,
float x1,
float y1,
3750 float x2,
float y2,
float x3,
float y3,
float objspace_flatness_squared,
int n)
3753 float dx0 = x1 - x0;
3754 float dy0 = y1 - y0;
3755 float dx1 = x2 - x1;
3756 float dy1 = y2 - y1;
3757 float dx2 = x3 - x2;
3758 float dy2 = y3 - y2;
3761 float longlen = (float)(STBTT_sqrt(dx0 * dx0 + dy0 * dy0) + STBTT_sqrt(dx1 * dx1 + dy1 * dy1) +
3762 STBTT_sqrt(dx2 * dx2 + dy2 * dy2));
3763 float shortlen = (float)STBTT_sqrt(dx * dx + dy * dy);
3764 float flatness_squared = longlen * longlen - shortlen * shortlen;
3769 if (flatness_squared > objspace_flatness_squared) {
3770 float x01 = (x0 + x1) / 2;
3771 float y01 = (y0 + y1) / 2;
3772 float x12 = (x1 + x2) / 2;
3773 float y12 = (y1 + y2) / 2;
3774 float x23 = (x2 + x3) / 2;
3775 float y23 = (y2 + y3) / 2;
3777 float xa = (x01 + x12) / 2;
3778 float ya = (y01 + y12) / 2;
3779 float xb = (x12 + x23) / 2;
3780 float yb = (y12 + y23) / 2;
3782 float mx = (xa + xb) / 2;
3783 float my = (ya + yb) / 2;
3785 stbtt__tesselate_cubic(points, num_points, x0, y0, x01, y01, xa, ya, mx, my, objspace_flatness_squared, n + 1);
3786 stbtt__tesselate_cubic(points, num_points, mx, my, xb, yb, x23, y23, x3, y3, objspace_flatness_squared, n + 1);
3788 stbtt__add_point(points, *num_points, x3, y3);
3789 *num_points = *num_points + 1;
3794 static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices,
int num_verts,
float objspace_flatness,
3795 int **contour_lengths,
int *num_contours,
void *userdata)
3797 stbtt__point *points = 0;
3800 float objspace_flatness_squared = objspace_flatness * objspace_flatness;
3801 int i, n = 0, start = 0, pass;
3804 for (i = 0; i < num_verts; ++i)
3805 if (vertices[i].type == STBTT_vmove)
3812 *contour_lengths = (
int *)STBTT_malloc(
sizeof(**contour_lengths) * n, userdata);
3814 if (*contour_lengths == 0) {
3820 for (pass = 0; pass < 2; ++pass) {
3823 points = (stbtt__point *)STBTT_malloc(num_points *
sizeof(points[0]), userdata);
3824 if (points ==
nullptr)
3829 for (i = 0; i < num_verts; ++i) {
3830 switch (vertices[i].type) {
3834 (*contour_lengths)[n] = num_points - start;
3838 x = vertices[i].x, y = vertices[i].y;
3839 stbtt__add_point(points, num_points++, x, y);
3842 x = vertices[i].x, y = vertices[i].y;
3843 stbtt__add_point(points, num_points++, x, y);
3846 stbtt__tesselate_curve(points, &num_points, x, y, vertices[i].cx, vertices[i].cy, vertices[i].x, vertices[i].y,
3847 objspace_flatness_squared, 0);
3848 x = vertices[i].x, y = vertices[i].y;
3851 stbtt__tesselate_cubic(points, &num_points, x, y, vertices[i].cx, vertices[i].cy, vertices[i].cx1,
3852 vertices[i].cy1, vertices[i].x, vertices[i].y, objspace_flatness_squared, 0);
3853 x = vertices[i].x, y = vertices[i].y;
3857 (*contour_lengths)[n] = num_points - start;
3862 STBTT_free(points, userdata);
3863 STBTT_free(*contour_lengths, userdata);
3864 *contour_lengths = 0;
3869 STBTT_DEF
void stbtt_Rasterize(stbtt__bitmap *result,
float flatness_in_pixels, stbtt_vertex *vertices,
int num_verts,
3870 float scale_x,
float scale_y,
float shift_x,
float shift_y,
int x_off,
int y_off,
3871 int invert,
void *userdata)
3873 float scale = scale_x > scale_y ? scale_y : scale_x;
3874 int winding_count = 0;
3875 int *winding_lengths =
nullptr;
3876 stbtt__point *windings =
3877 stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
3879 stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off,
3881 STBTT_free(winding_lengths, userdata);
3882 STBTT_free(windings, userdata);
3886 STBTT_DEF
void stbtt_FreeBitmap(
unsigned char *bitmap,
void *userdata) { STBTT_free(bitmap, userdata); }
3888 STBTT_DEF
unsigned char *stbtt_GetGlyphBitmapSubpixel(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
3889 float shift_x,
float shift_y,
int glyph,
int *width,
int *height,
3890 int *xoff,
int *yoff)
3892 int ix0, iy0, ix1, iy1;
3894 stbtt_vertex *vertices;
3895 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
3901 STBTT_free(vertices, info->userdata);
3907 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0, &iy0, &ix1, &iy1);
3910 gbm.w = (ix1 - ix0);
3911 gbm.h = (iy1 - iy0);
3912 gbm.pixels =
nullptr;
3923 if (gbm.w && gbm.h) {
3924 gbm.pixels = (
unsigned char *)STBTT_malloc(gbm.w * gbm.h, info->userdata);
3928 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1,
3932 STBTT_free(vertices, info->userdata);
3936 STBTT_DEF
unsigned char *stbtt_GetGlyphBitmap(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
int glyph,
3937 int *width,
int *height,
int *xoff,
int *yoff)
3939 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);
3942 STBTT_DEF
void stbtt_MakeGlyphBitmapSubpixel(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
3943 int out_stride,
float scale_x,
float scale_y,
float shift_x,
float shift_y,
3947 stbtt_vertex *vertices;
3948 int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
3951 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0, &iy0, 0, 0);
3952 gbm.pixels = output;
3955 gbm.stride = out_stride;
3958 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
3960 STBTT_free(vertices, info->userdata);
3963 STBTT_DEF
void stbtt_MakeGlyphBitmap(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
3964 int out_stride,
float scale_x,
float scale_y,
int glyph)
3966 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f, 0.0f, glyph);
3969 STBTT_DEF
unsigned char *stbtt_GetCodepointBitmapSubpixel(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
3970 float shift_x,
float shift_y,
int codepoint,
int *width,
3971 int *height,
int *xoff,
int *yoff)
3973 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info, codepoint),
3974 width, height, xoff, yoff);
3977 STBTT_DEF
void stbtt_MakeCodepointBitmapSubpixelPrefilter(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
3978 int out_h,
int out_stride,
float scale_x,
float scale_y,
3979 float shift_x,
float shift_y,
int oversample_x,
3980 int oversample_y,
float *sub_x,
float *sub_y,
int codepoint)
3982 stbtt_MakeGlyphBitmapSubpixelPrefilter(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y,
3983 oversample_x, oversample_y, sub_x, sub_y,
3984 stbtt_FindGlyphIndex(info, codepoint));
3987 STBTT_DEF
void stbtt_MakeCodepointBitmapSubpixel(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
3988 int out_h,
int out_stride,
float scale_x,
float scale_y,
float shift_x,
3989 float shift_y,
int codepoint)
3991 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y,
3992 stbtt_FindGlyphIndex(info, codepoint));
3995 STBTT_DEF
unsigned char *stbtt_GetCodepointBitmap(
const stbtt_fontinfo *info,
float scale_x,
float scale_y,
3996 int codepoint,
int *width,
int *height,
int *xoff,
int *yoff)
3998 return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, codepoint, width, height, xoff, yoff);
4001 STBTT_DEF
void stbtt_MakeCodepointBitmap(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
int out_h,
4002 int out_stride,
float scale_x,
float scale_y,
int codepoint)
4004 stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f, 0.0f, codepoint);
4013 static int stbtt_BakeFontBitmap_internal(
unsigned char *data,
int offset,
4015 unsigned char *pixels,
int pw,
int ph,
4016 int first_char,
int num_chars,
4017 stbtt_bakedchar *chardata)
4020 int x, y, bottom_y, i;
4022 f.userdata =
nullptr;
4023 if (!stbtt_InitFont(&f, data, offset))
4025 STBTT_memset(pixels, 0, pw * ph);
4029 scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
4031 for (i = 0; i < num_chars; ++i) {
4032 int advance, lsb, x0, y0, x1, y1, gw, gh;
4033 int g = stbtt_FindGlyphIndex(&f, first_char + i);
4034 stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
4035 stbtt_GetGlyphBitmapBox(&f, g, scale, scale, &x0, &y0, &x1, &y1);
4038 if (x + gw + 1 >= pw)
4039 y = bottom_y, x = 1;
4040 if (y + gh + 1 >= ph)
4042 STBTT_assert(x + gw < pw);
4043 STBTT_assert(y + gh < ph);
4044 stbtt_MakeGlyphBitmap(&f, pixels + x + y * pw, gw, gh, pw, scale, scale, g);
4045 chardata[i].x0 = (stbtt_int16)x;
4046 chardata[i].y0 = (stbtt_int16)y;
4047 chardata[i].x1 = (stbtt_int16)(x + gw);
4048 chardata[i].y1 = (stbtt_int16)(y + gh);
4049 chardata[i].xadvance = scale * advance;
4050 chardata[i].xoff = (float)x0;
4051 chardata[i].yoff = (float)y0;
4053 if (y + gh + 1 > bottom_y)
4054 bottom_y = y + gh + 1;
4059 STBTT_DEF
void stbtt_GetBakedQuad(
const stbtt_bakedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
4060 float *ypos, stbtt_aligned_quad *q,
int opengl_fillrule)
4062 float d3d_bias = opengl_fillrule ? 0 : -0.5f;
4063 float ipw = 1.0f / pw, iph = 1.0f / ph;
4064 const stbtt_bakedchar *b = chardata + char_index;
4065 int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f);
4066 int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f);
4068 q->x0 = round_x + d3d_bias;
4069 q->y0 = round_y + d3d_bias;
4070 q->x1 = round_x + b->x1 - b->x0 + d3d_bias;
4071 q->y1 = round_y + b->y1 - b->y0 + d3d_bias;
4073 q->s0 = b->x0 * ipw;
4074 q->t0 = b->y0 * iph;
4075 q->s1 = b->x1 * ipw;
4076 q->t1 = b->y1 * iph;
4078 *xpos += b->xadvance;
4086 #ifndef STB_RECT_PACK_VERSION
4088 typedef int stbrp_coord;
4112 int id, w, h, was_packed;
4115 static void stbrp_init_target(stbrp_context *con,
int pw,
int ph, stbrp_node *nodes,
int num_nodes)
4122 STBTT__NOTUSED(nodes);
4123 STBTT__NOTUSED(num_nodes);
4126 static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects,
int num_rects)
4129 for (i = 0; i < num_rects; ++i) {
4130 if (con->x + rects[i].w > con->width) {
4132 con->y = con->bottom_y;
4134 if (con->y + rects[i].h > con->height)
4136 rects[i].x = con->x;
4137 rects[i].y = con->y;
4138 rects[i].was_packed = 1;
4139 con->x += rects[i].w;
4140 if (con->y + rects[i].h > con->bottom_y)
4141 con->bottom_y = con->y + rects[i].h;
4143 for (; i < num_rects; ++i)
4144 rects[i].was_packed = 0;
4155 STBTT_DEF
int stbtt_PackBegin(stbtt_pack_context *spc,
unsigned char *pixels,
int pw,
int ph,
int stride_in_bytes,
4156 int padding,
void *alloc_context)
4158 stbrp_context *context = (stbrp_context *)STBTT_malloc(
sizeof(*context), alloc_context);
4159 int num_nodes = pw - padding;
4160 stbrp_node *nodes = (stbrp_node *)STBTT_malloc(
sizeof(*nodes) * num_nodes, alloc_context);
4162 if (context ==
nullptr || nodes ==
nullptr) {
4163 if (context !=
nullptr)
4164 STBTT_free(context, alloc_context);
4165 if (nodes !=
nullptr)
4166 STBTT_free(nodes, alloc_context);
4170 spc->user_allocator_context = alloc_context;
4173 spc->pixels = pixels;
4174 spc->pack_info = context;
4176 spc->padding = padding;
4177 spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;
4178 spc->h_oversample = 1;
4179 spc->v_oversample = 1;
4180 spc->skip_missing = 0;
4182 stbrp_init_target(context, pw - padding, ph - padding, nodes, num_nodes);
4185 STBTT_memset(pixels, 0, pw * ph);
4190 STBTT_DEF
void stbtt_PackEnd(stbtt_pack_context *spc)
4192 STBTT_free(spc->nodes, spc->user_allocator_context);
4193 STBTT_free(spc->pack_info, spc->user_allocator_context);
4196 STBTT_DEF
void stbtt_PackSetOversampling(stbtt_pack_context *spc,
unsigned int h_oversample,
unsigned int v_oversample)
4198 STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
4199 STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
4200 if (h_oversample <= STBTT_MAX_OVERSAMPLE)
4201 spc->h_oversample = h_oversample;
4202 if (v_oversample <= STBTT_MAX_OVERSAMPLE)
4203 spc->v_oversample = v_oversample;
4206 STBTT_DEF
void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc,
int skip) { spc->skip_missing = skip; }
4208 #define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE - 1)
4210 static void stbtt__h_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
4212 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
4213 int safe_w = w - kernel_width;
4215 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
4216 for (j = 0; j < h; ++j) {
4219 STBTT_memset(buffer, 0, kernel_width);
4224 switch (kernel_width) {
4226 for (i = 0; i <= safe_w; ++i) {
4227 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
4228 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4229 pixels[i] = (
unsigned char)(total / 2);
4233 for (i = 0; i <= safe_w; ++i) {
4234 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
4235 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4236 pixels[i] = (
unsigned char)(total / 3);
4240 for (i = 0; i <= safe_w; ++i) {
4241 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
4242 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4243 pixels[i] = (
unsigned char)(total / 4);
4247 for (i = 0; i <= safe_w; ++i) {
4248 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
4249 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4250 pixels[i] = (
unsigned char)(total / 5);
4254 for (i = 0; i <= safe_w; ++i) {
4255 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
4256 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
4257 pixels[i] = (
unsigned char)(total / kernel_width);
4262 for (; i < w; ++i) {
4263 STBTT_assert(pixels[i] == 0);
4264 total -= buffer[i & STBTT__OVER_MASK];
4265 pixels[i] = (
unsigned char)(total / kernel_width);
4268 pixels += stride_in_bytes;
4272 static void stbtt__v_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
4274 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
4275 int safe_h = h - kernel_width;
4277 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
4278 for (j = 0; j < w; ++j) {
4281 STBTT_memset(buffer, 0, kernel_width);
4286 switch (kernel_width) {
4288 for (i = 0; i <= safe_h; ++i) {
4289 total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
4290 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4291 pixels[i * stride_in_bytes] = (
unsigned char)(total / 2);
4295 for (i = 0; i <= safe_h; ++i) {
4296 total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
4297 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4298 pixels[i * stride_in_bytes] = (
unsigned char)(total / 3);
4302 for (i = 0; i <= safe_h; ++i) {
4303 total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
4304 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4305 pixels[i * stride_in_bytes] = (
unsigned char)(total / 4);
4309 for (i = 0; i <= safe_h; ++i) {
4310 total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
4311 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4312 pixels[i * stride_in_bytes] = (
unsigned char)(total / 5);
4316 for (i = 0; i <= safe_h; ++i) {
4317 total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
4318 buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
4319 pixels[i * stride_in_bytes] = (
unsigned char)(total / kernel_width);
4324 for (; i < h; ++i) {
4325 STBTT_assert(pixels[i * stride_in_bytes] == 0);
4326 total -= buffer[i & STBTT__OVER_MASK];
4327 pixels[i * stride_in_bytes] = (
unsigned char)(total / kernel_width);
4334 static float stbtt__oversample_shift(
int oversample)
4343 return (
float)-(oversample - 1) / (2.0f * (
float)oversample);
4347 STBTT_DEF
int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info,
4348 stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects)
4351 int missing_glyph_added = 0;
4354 for (i = 0; i < num_ranges; ++i) {
4355 float fh = ranges[i].font_size;
4356 float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
4357 ranges[i].h_oversample = (
unsigned char)spc->h_oversample;
4358 ranges[i].v_oversample = (
unsigned char)spc->v_oversample;
4359 for (j = 0; j < ranges[i].num_chars; ++j) {
4361 int codepoint = ranges[i].array_of_unicode_codepoints ==
nullptr ? ranges[i].first_unicode_codepoint_in_range + j
4362 : ranges[i].array_of_unicode_codepoints[j];
4363 int glyph = stbtt_FindGlyphIndex(info, codepoint);
4364 if (glyph == 0 && (spc->skip_missing || missing_glyph_added)) {
4365 rects[k].w = rects[k].h = 0;
4367 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale * spc->h_oversample, scale * spc->v_oversample, 0, 0, &x0,
4369 rects[k].w = (stbrp_coord)(x1 - x0 + spc->padding + spc->h_oversample - 1);
4370 rects[k].h = (stbrp_coord)(y1 - y0 + spc->padding + spc->v_oversample - 1);
4372 missing_glyph_added = 1;
4381 STBTT_DEF
void stbtt_MakeGlyphBitmapSubpixelPrefilter(
const stbtt_fontinfo *info,
unsigned char *output,
int out_w,
4382 int out_h,
int out_stride,
float scale_x,
float scale_y,
4383 float shift_x,
float shift_y,
int prefilter_x,
int prefilter_y,
4384 float *sub_x,
float *sub_y,
int glyph)
4386 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w - (prefilter_x - 1), out_h - (prefilter_y - 1), out_stride, scale_x,
4387 scale_y, shift_x, shift_y, glyph);
4389 if (prefilter_x > 1)
4390 stbtt__h_prefilter(output, out_w, out_h, out_stride, prefilter_x);
4392 if (prefilter_y > 1)
4393 stbtt__v_prefilter(output, out_w, out_h, out_stride, prefilter_y);
4395 *sub_x = stbtt__oversample_shift(prefilter_x);
4396 *sub_y = stbtt__oversample_shift(prefilter_y);
4400 STBTT_DEF
int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc,
const stbtt_fontinfo *info,
4401 stbtt_pack_range *ranges,
int num_ranges, stbrp_rect *rects)
4403 int i, j, k, missing_glyph = -1, return_value = 1;
4406 int old_h_over = spc->h_oversample;
4407 int old_v_over = spc->v_oversample;
4410 for (i = 0; i < num_ranges; ++i) {
4411 float fh = ranges[i].font_size;
4412 float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
4413 float recip_h, recip_v, sub_x, sub_y;
4414 spc->h_oversample = ranges[i].h_oversample;
4415 spc->v_oversample = ranges[i].v_oversample;
4416 recip_h = 1.0f / spc->h_oversample;
4417 recip_v = 1.0f / spc->v_oversample;
4418 sub_x = stbtt__oversample_shift(spc->h_oversample);
4419 sub_y = stbtt__oversample_shift(spc->v_oversample);
4420 for (j = 0; j < ranges[i].num_chars; ++j) {
4421 stbrp_rect *r = &rects[k];
4422 if (r->was_packed && r->w != 0 && r->h != 0) {
4423 stbtt_packedchar *bc = &ranges[i].chardata_for_range[j];
4424 int advance, lsb, x0, y0, x1, y1;
4425 int codepoint = ranges[i].array_of_unicode_codepoints ==
nullptr ? ranges[i].first_unicode_codepoint_in_range + j
4426 : ranges[i].array_of_unicode_codepoints[j];
4427 int glyph = stbtt_FindGlyphIndex(info, codepoint);
4428 stbrp_coord pad = (stbrp_coord)spc->padding;
4435 stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
4436 stbtt_GetGlyphBitmapBox(info, glyph, scale * spc->h_oversample, scale * spc->v_oversample, &x0, &y0, &x1, &y1);
4437 stbtt_MakeGlyphBitmapSubpixel(info, spc->pixels + r->x + r->y * spc->stride_in_bytes,
4438 r->w - spc->h_oversample + 1, r->h - spc->v_oversample + 1, spc->stride_in_bytes,
4439 scale * spc->h_oversample, scale * spc->v_oversample, 0, 0, glyph);
4441 if (spc->h_oversample > 1)
4442 stbtt__h_prefilter(spc->pixels + r->x + r->y * spc->stride_in_bytes, r->w, r->h, spc->stride_in_bytes,
4445 if (spc->v_oversample > 1)
4446 stbtt__v_prefilter(spc->pixels + r->x + r->y * spc->stride_in_bytes, r->w, r->h, spc->stride_in_bytes,
4449 bc->x0 = (stbtt_int16)r->x;
4450 bc->y0 = (stbtt_int16)r->y;
4451 bc->x1 = (stbtt_int16)(r->x + r->w);
4452 bc->y1 = (stbtt_int16)(r->y + r->h);
4453 bc->xadvance = scale * advance;
4454 bc->xoff = (float)x0 * recip_h + sub_x;
4455 bc->yoff = (float)y0 * recip_v + sub_y;
4456 bc->xoff2 = (x0 + r->w) * recip_h + sub_x;
4457 bc->yoff2 = (y0 + r->h) * recip_v + sub_y;
4461 }
else if (spc->skip_missing) {
4463 }
else if (r->was_packed && r->w == 0 && r->h == 0 && missing_glyph >= 0) {
4464 ranges[i].chardata_for_range[j] = ranges[i].chardata_for_range[missing_glyph];
4474 spc->h_oversample = old_h_over;
4475 spc->v_oversample = old_v_over;
4477 return return_value;
4480 STBTT_DEF
void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects,
int num_rects)
4482 stbrp_pack_rects((stbrp_context *)spc->pack_info, rects, num_rects);
4485 STBTT_DEF
int stbtt_PackFontRanges(stbtt_pack_context *spc,
const unsigned char *fontdata,
int font_index,
4486 stbtt_pack_range *ranges,
int num_ranges)
4488 stbtt_fontinfo info;
4489 int i, j, n, return_value = 1;
4494 for (i = 0; i < num_ranges; ++i)
4495 for (j = 0; j < ranges[i].num_chars; ++j)
4496 ranges[i].chardata_for_range[j].x0 = ranges[i].chardata_for_range[j].y0 = ranges[i].chardata_for_range[j].x1 =
4497 ranges[i].chardata_for_range[j].y1 = 0;
4500 for (i = 0; i < num_ranges; ++i)
4501 n += ranges[i].num_chars;
4503 rects = (stbrp_rect *)STBTT_malloc(
sizeof(*rects) * n, spc->user_allocator_context);
4504 if (rects ==
nullptr)
4507 info.userdata = spc->user_allocator_context;
4508 stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, font_index));
4510 n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);
4512 stbtt_PackFontRangesPackRects(spc, rects, n);
4514 return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);
4516 STBTT_free(rects, spc->user_allocator_context);
4517 return return_value;
4520 STBTT_DEF
int stbtt_PackFontRange(stbtt_pack_context *spc,
const unsigned char *fontdata,
int font_index,
4521 float font_size,
int first_unicode_codepoint_in_range,
int num_chars_in_range,
4522 stbtt_packedchar *chardata_for_range)
4524 stbtt_pack_range range;
4525 range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range;
4526 range.array_of_unicode_codepoints =
nullptr;
4527 range.num_chars = num_chars_in_range;
4528 range.chardata_for_range = chardata_for_range;
4529 range.font_size = font_size;
4530 return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);
4533 STBTT_DEF
void stbtt_GetScaledFontVMetrics(
const unsigned char *fontdata,
int index,
float size,
float *ascent,
4534 float *descent,
float *lineGap)
4536 int i_ascent, i_descent, i_lineGap;
4538 stbtt_fontinfo info;
4539 stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, index));
4540 scale = size > 0 ? stbtt_ScaleForPixelHeight(&info, size) : stbtt_ScaleForMappingEmToPixels(&info, -size);
4541 stbtt_GetFontVMetrics(&info, &i_ascent, &i_descent, &i_lineGap);
4542 *ascent = (float)i_ascent * scale;
4543 *descent = (float)i_descent * scale;
4544 *lineGap = (float)i_lineGap * scale;
4547 STBTT_DEF
void stbtt_GetPackedQuad(
const stbtt_packedchar *chardata,
int pw,
int ph,
int char_index,
float *xpos,
4548 float *ypos, stbtt_aligned_quad *q,
int align_to_integer)
4550 float ipw = 1.0f / pw, iph = 1.0f / ph;
4551 const stbtt_packedchar *b = chardata + char_index;
4553 if (align_to_integer) {
4554 float x = (float)STBTT_ifloor((*xpos + b->xoff) + 0.5f);
4555 float y = (float)STBTT_ifloor((*ypos + b->yoff) + 0.5f);
4558 q->x1 = x + b->xoff2 - b->xoff;
4559 q->y1 = y + b->yoff2 - b->yoff;
4561 q->x0 = *xpos + b->xoff;
4562 q->y0 = *ypos + b->yoff;
4563 q->x1 = *xpos + b->xoff2;
4564 q->y1 = *ypos + b->yoff2;
4567 q->s0 = b->x0 * ipw;
4568 q->t0 = b->y0 * iph;
4569 q->s1 = b->x1 * ipw;
4570 q->t1 = b->y1 * iph;
4572 *xpos += b->xadvance;
4580 #define STBTT_min(a, b) ((a) < (b) ? (a) : (b))
4581 #define STBTT_max(a, b) ((a) < (b) ? (b) : (a))
4583 static int stbtt__ray_intersect_bezier(
float orig[2],
float ray[2],
float q0[2],
float q1[2],
float q2[2],
4586 float q0perp = q0[1] * ray[0] - q0[0] * ray[1];
4587 float q1perp = q1[1] * ray[0] - q1[0] * ray[1];
4588 float q2perp = q2[1] * ray[0] - q2[0] * ray[1];
4589 float roperp = orig[1] * ray[0] - orig[0] * ray[1];
4591 float a = q0perp - 2 * q1perp + q2perp;
4592 float b = q1perp - q0perp;
4593 float c = q0perp - roperp;
4595 float s0 = 0., s1 = 0.;
4599 float discr = b * b - a * c;
4601 float rcpna = -1 / a;
4602 float d = (float)STBTT_sqrt(discr);
4603 s0 = (b + d) * rcpna;
4604 s1 = (b - d) * rcpna;
4605 if (s0 >= 0.0 && s0 <= 1.0)
4607 if (d > 0.0 && s1 >= 0.0 && s1 <= 1.0) {
4617 if (s0 >= 0.0 && s0 <= 1.0)
4624 float rcp_len2 = 1 / (ray[0] * ray[0] + ray[1] * ray[1]);
4625 float rayn_x = ray[0] * rcp_len2, rayn_y = ray[1] * rcp_len2;
4627 float q0d = q0[0] * rayn_x + q0[1] * rayn_y;
4628 float q1d = q1[0] * rayn_x + q1[1] * rayn_y;
4629 float q2d = q2[0] * rayn_x + q2[1] * rayn_y;
4630 float rod = orig[0] * rayn_x + orig[1] * rayn_y;
4632 float q10d = q1d - q0d;
4633 float q20d = q2d - q0d;
4634 float q0rd = q0d - rod;
4636 hits[0][0] = q0rd + s0 * (2.0f - 2.0f * s0) * q10d + s0 * s0 * q20d;
4637 hits[0][1] = a * s0 + b;
4640 hits[1][0] = q0rd + s1 * (2.0f - 2.0f * s1) * q10d + s1 * s1 * q20d;
4641 hits[1][1] = a * s1 + b;
4649 static int equal(
float *a,
float *b) {
return (a[0] == b[0] && a[1] == b[1]); }
4651 static int stbtt__compute_crossings_x(
float x,
float y,
int nverts, stbtt_vertex *verts)
4654 float orig[2], ray[2] = {1, 0};
4659 y_frac = (float)STBTT_fmod(y, 1.0f);
4662 else if (y_frac > 0.99f)
4669 for (i = 0; i < nverts; ++i) {
4670 if (verts[i].type == STBTT_vline) {
4671 int x0 = (int)verts[i - 1].x, y0 = (
int)verts[i - 1].y;
4672 int x1 = (int)verts[i].x, y1 = (
int)verts[i].y;
4673 if (y > STBTT_min(y0, y1) && y < STBTT_max(y0, y1) && x > STBTT_min(x0, x1)) {
4674 float x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0;
4676 winding += (y0 < y1) ? 1 : -1;
4679 if (verts[i].type == STBTT_vcurve) {
4680 int x0 = (int)verts[i - 1].x, y0 = (
int)verts[i - 1].y;
4681 int x1 = (int)verts[i].cx, y1 = (
int)verts[i].cy;
4682 int x2 = (int)verts[i].x, y2 = (
int)verts[i].y;
4683 int ax = STBTT_min(x0, STBTT_min(x1, x2)), ay = STBTT_min(y0, STBTT_min(y1, y2));
4684 int by = STBTT_max(y0, STBTT_max(y1, y2));
4685 if (y > ay && y < by && x > ax) {
4686 float q0[2], q1[2], q2[2];
4694 if (equal(q0, q1) || equal(q1, q2)) {
4695 x0 = (int)verts[i - 1].x;
4696 y0 = (int)verts[i - 1].y;
4697 x1 = (int)verts[i].x;
4698 y1 = (int)verts[i].y;
4699 if (y > STBTT_min(y0, y1) && y < STBTT_max(y0, y1) && x > STBTT_min(x0, x1)) {
4700 float x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0;
4702 winding += (y0 < y1) ? 1 : -1;
4705 int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits);
4708 winding += (hits[0][1] < 0 ? -1 : 1);
4711 winding += (hits[1][1] < 0 ? -1 : 1);
4719 static float stbtt__cuberoot(
float x)
4722 return -(float)STBTT_pow(-x, 1.0f / 3.0f);
4724 return (
float)STBTT_pow(x, 1.0f / 3.0f);
4728 static int stbtt__solve_cubic(
float a,
float b,
float c,
float *r)
4731 float p = b - a * a / 3;
4732 float q = a * (2 * a * a - 9 * b) / 27 + c;
4733 float p3 = p * p * p;
4734 float d = q * q + 4 * p3 / 27;
4736 float z = (float)STBTT_sqrt(d);
4737 float u = (-q + z) / 2;
4738 float v = (-q - z) / 2;
4739 u = stbtt__cuberoot(u);
4740 v = stbtt__cuberoot(v);
4744 float u = (float)STBTT_sqrt(-p / 3);
4745 float v = (float)STBTT_acos(-STBTT_sqrt(-27 / p3) * q / 2) / 3;
4746 float m = (float)STBTT_cos(v);
4747 float n = (float)STBTT_cos(v - 3.141592 / 2) * 1.732050808f;
4748 r[0] = s + u * 2 * m;
4749 r[1] = s - u * (m + n);
4750 r[2] = s - u * (m - n);
4759 STBTT_DEF
unsigned char *stbtt_GetGlyphSDF(
const stbtt_fontinfo *info,
float scale,
int glyph,
int padding,
4760 unsigned char onedge_value,
float pixel_dist_scale,
int *width,
int *height,
4761 int *xoff,
int *yoff)
4763 float scale_x = scale, scale_y = scale;
4764 int ix0, iy0, ix1, iy1;
4766 unsigned char *data;
4771 stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f, 0.0f, &ix0, &iy0, &ix1, &iy1);
4774 if (ix0 == ix1 || iy0 == iy1)
4800 stbtt_vertex *verts;
4801 int num_verts = stbtt_GetGlyphShape(info, glyph, &verts);
4802 data = (
unsigned char *)STBTT_malloc(w * h, info->userdata);
4803 precompute = (
float *)STBTT_malloc(num_verts *
sizeof(
float), info->userdata);
4805 for (i = 0, j = num_verts - 1; i < num_verts; j = i++) {
4806 if (verts[i].type == STBTT_vline) {
4807 float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y;
4808 float x1 = verts[j].x * scale_x, y1 = verts[j].y * scale_y;
4809 float dist = (float)STBTT_sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
4810 precompute[i] = (dist == 0) ? 0.0f : 1.0f / dist;
4811 }
else if (verts[i].type == STBTT_vcurve) {
4812 float x2 = verts[j].x * scale_x, y2 = verts[j].y * scale_y;
4813 float x1 = verts[i].cx * scale_x, y1 = verts[i].cy * scale_y;
4814 float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y;
4815 float bx = x0 - 2 * x1 + x2, by = y0 - 2 * y1 + y2;
4816 float len2 = bx * bx + by * by;
4818 precompute[i] = 1.0f / (bx * bx + by * by);
4820 precompute[i] = 0.0f;
4822 precompute[i] = 0.0f;
4825 for (y = iy0; y < iy1; ++y) {
4826 for (x = ix0; x < ix1; ++x) {
4828 float min_dist = 999999.0f;
4829 float sx = (float)x + 0.5f;
4830 float sy = (float)y + 0.5f;
4831 float x_gspace = (sx / scale_x);
4832 float y_gspace = (sy / scale_y);
4834 int winding = stbtt__compute_crossings_x(x_gspace, y_gspace, num_verts,
4838 for (i = 0; i < num_verts; ++i) {
4839 float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y;
4841 if (verts[i].type == STBTT_vline && precompute[i] != 0.0f) {
4842 float x1 = verts[i - 1].x * scale_x, y1 = verts[i - 1].y * scale_y;
4844 float dist, dist2 = (x0 - sx) * (x0 - sx) + (y0 - sy) * (y0 - sy);
4845 if (dist2 < min_dist * min_dist)
4846 min_dist = (float)STBTT_sqrt(dist2);
4851 dist = (float)STBTT_fabs((x1 - x0) * (y0 - sy) - (y1 - y0) * (x0 - sx)) * precompute[i];
4852 STBTT_assert(i != 0);
4853 if (dist < min_dist) {
4857 float dx = x1 - x0, dy = y1 - y0;
4858 float px = x0 - sx, py = y0 - sy;
4861 float t = -(px * dx + py * dy) / (dx * dx + dy * dy);
4862 if (t >= 0.0f && t <= 1.0f)
4865 }
else if (verts[i].type == STBTT_vcurve) {
4866 float x2 = verts[i - 1].x * scale_x, y2 = verts[i - 1].y * scale_y;
4867 float x1 = verts[i].cx * scale_x, y1 = verts[i].cy * scale_y;
4868 float box_x0 = STBTT_min(STBTT_min(x0, x1), x2);
4869 float box_y0 = STBTT_min(STBTT_min(y0, y1), y2);
4870 float box_x1 = STBTT_max(STBTT_max(x0, x1), x2);
4871 float box_y1 = STBTT_max(STBTT_max(y0, y1), y2);
4873 if (sx > box_x0 - min_dist && sx < box_x1 + min_dist && sy > box_y0 - min_dist && sy < box_y1 + min_dist) {
4875 float ax = x1 - x0, ay = y1 - y0;
4876 float bx = x0 - 2 * x1 + x2, by = y0 - 2 * y1 + y2;
4877 float mx = x0 - sx, my = y0 - sy;
4878 float res[3] = {0.f, 0.f, 0.f};
4879 float px, py, t, it, dist2;
4880 float a_inv = precompute[i];
4882 float a = 3 * (ax * bx + ay * by);
4883 float b = 2 * (ax * ax + ay * ay) + (mx * bx + my * by);
4884 float c = mx * ax + my * ay;
4887 res[num++] = -c / b;
4890 float discriminant = b * b - 4 * a * c;
4891 if (discriminant < 0)
4894 float root = (float)STBTT_sqrt(discriminant);
4895 res[0] = (-b - root) / (2 * a);
4896 res[1] = (-b + root) / (2 * a);
4901 float b = 3 * (ax * bx + ay * by) * a_inv;
4902 float c = (2 * (ax * ax + ay * ay) + (mx * bx + my * by)) * a_inv;
4903 float d = (mx * ax + my * ay) * a_inv;
4904 num = stbtt__solve_cubic(b, c, d, res);
4906 dist2 = (x0 - sx) * (x0 - sx) + (y0 - sy) * (y0 - sy);
4907 if (dist2 < min_dist * min_dist)
4908 min_dist = (float)STBTT_sqrt(dist2);
4910 if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) {
4911 t = res[0], it = 1.0f - t;
4912 px = it * it * x0 + 2 * t * it * x1 + t * t * x2;
4913 py = it * it * y0 + 2 * t * it * y1 + t * t * y2;
4914 dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
4915 if (dist2 < min_dist * min_dist)
4916 min_dist = (float)STBTT_sqrt(dist2);
4918 if (num >= 2 && res[1] >= 0.0f && res[1] <= 1.0f) {
4919 t = res[1], it = 1.0f - t;
4920 px = it * it * x0 + 2 * t * it * x1 + t * t * x2;
4921 py = it * it * y0 + 2 * t * it * y1 + t * t * y2;
4922 dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
4923 if (dist2 < min_dist * min_dist)
4924 min_dist = (float)STBTT_sqrt(dist2);
4926 if (num >= 3 && res[2] >= 0.0f && res[2] <= 1.0f) {
4927 t = res[2], it = 1.0f - t;
4928 px = it * it * x0 + 2 * t * it * x1 + t * t * x2;
4929 py = it * it * y0 + 2 * t * it * y1 + t * t * y2;
4930 dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
4931 if (dist2 < min_dist * min_dist)
4932 min_dist = (float)STBTT_sqrt(dist2);
4938 min_dist = -min_dist;
4939 val = onedge_value + pixel_dist_scale * min_dist;
4944 data[(y - iy0) * w + (x - ix0)] = (
unsigned char)val;
4947 STBTT_free(precompute, info->userdata);
4948 STBTT_free(verts, info->userdata);
4953 STBTT_DEF
unsigned char *stbtt_GetCodepointSDF(
const stbtt_fontinfo *info,
float scale,
int codepoint,
int padding,
4954 unsigned char onedge_value,
float pixel_dist_scale,
int *width,
4955 int *height,
int *xoff,
int *yoff)
4957 return stbtt_GetGlyphSDF(info, scale, stbtt_FindGlyphIndex(info, codepoint), padding, onedge_value, pixel_dist_scale,
4958 width, height, xoff, yoff);
4961 STBTT_DEF
void stbtt_FreeSDF(
unsigned char *bitmap,
void *userdata) { STBTT_free(bitmap, userdata); }
4969 static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2,
4976 stbtt_uint16 ch = s2[0] * 256 + s2[1];
4982 }
else if (ch < 0x800) {
4985 if (s1[i++] != 0xc0 + (ch >> 6))
4987 if (s1[i++] != 0x80 + (ch & 0x3f))
4989 }
else if (ch >= 0xd800 && ch < 0xdc00) {
4991 stbtt_uint16 ch2 = s2[2] * 256 + s2[3];
4994 c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
4995 if (s1[i++] != 0xf0 + (c >> 18))
4997 if (s1[i++] != 0x80 + ((c >> 12) & 0x3f))
4999 if (s1[i++] != 0x80 + ((c >> 6) & 0x3f))
5001 if (s1[i++] != 0x80 + ((c)&0x3f))
5005 }
else if (ch >= 0xdc00 && ch < 0xe000) {
5010 if (s1[i++] != 0xe0 + (ch >> 12))
5012 if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f))
5014 if (s1[i++] != 0x80 + ((ch)&0x3f))
5023 static int stbtt_CompareUTF8toUTF16_bigendian_internal(
char *s1,
int len1,
char *s2,
int len2)
5025 return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8 *)s1, len1, (stbtt_uint8 *)s2, len2);
5030 STBTT_DEF
const char *stbtt_GetFontNameString(
const stbtt_fontinfo *font,
int *length,
int platformID,
int encodingID,
5031 int languageID,
int nameID)
5033 stbtt_int32 i, count, stringOffset;
5034 stbtt_uint8 *fc = font->data;
5035 stbtt_uint32 offset = font->fontstart;
5036 stbtt_uint32 nm = stbtt__find_table(fc, offset,
"name");
5040 count = ttUSHORT(fc + nm + 2);
5041 stringOffset = nm + ttUSHORT(fc + nm + 4);
5042 for (i = 0; i < count; ++i) {
5043 stbtt_uint32 loc = nm + 6 + 12 * i;
5044 if (platformID == ttUSHORT(fc + loc + 0) && encodingID == ttUSHORT(fc + loc + 2) &&
5045 languageID == ttUSHORT(fc + loc + 4) && nameID == ttUSHORT(fc + loc + 6)) {
5046 *length = ttUSHORT(fc + loc + 8);
5047 return (
const char *)(fc + stringOffset + ttUSHORT(fc + loc + 10));
5053 static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen,
5054 stbtt_int32 target_id, stbtt_int32 next_id)
5057 stbtt_int32 count = ttUSHORT(fc + nm + 2);
5058 stbtt_int32 stringOffset = nm + ttUSHORT(fc + nm + 4);
5060 for (i = 0; i < count; ++i) {
5061 stbtt_uint32 loc = nm + 6 + 12 * i;
5062 stbtt_int32
id = ttUSHORT(fc + loc + 6);
5063 if (
id == target_id) {
5065 stbtt_int32 platform = ttUSHORT(fc + loc + 0), encoding = ttUSHORT(fc + loc + 2),
5066 language = ttUSHORT(fc + loc + 4);
5069 if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
5070 stbtt_int32 slen = ttUSHORT(fc + loc + 8);
5071 stbtt_int32 off = ttUSHORT(fc + loc + 10);
5074 stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc + stringOffset + off, slen);
5075 if (matchlen >= 0) {
5077 if (i + 1 < count && ttUSHORT(fc + loc + 12 + 6) == next_id && ttUSHORT(fc + loc + 12) == platform &&
5078 ttUSHORT(fc + loc + 12 + 2) == encoding && ttUSHORT(fc + loc + 12 + 4) == language) {
5079 slen = ttUSHORT(fc + loc + 12 + 8);
5080 off = ttUSHORT(fc + loc + 12 + 10);
5082 if (matchlen == nlen)
5084 }
else if (matchlen < nlen && name[matchlen] ==
' ') {
5086 if (stbtt_CompareUTF8toUTF16_bigendian_internal((
char *)(name + matchlen), nlen - matchlen,
5087 (
char *)(fc + stringOffset + off), slen))
5092 if (matchlen == nlen)
5104 static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)
5106 stbtt_int32 nlen = (stbtt_int32)STBTT_strlen((
char *)name);
5107 stbtt_uint32 nm, hd;
5108 if (!stbtt__isfont(fc + offset))
5113 hd = stbtt__find_table(fc, offset,
"head");
5114 if ((ttUSHORT(fc + hd + 44) & 7) != (flags & 7))
5118 nm = stbtt__find_table(fc, offset,
"name");
5124 if (stbtt__matchpair(fc, nm, name, nlen, 16, -1))
5126 if (stbtt__matchpair(fc, nm, name, nlen, 1, -1))
5128 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
5131 if (stbtt__matchpair(fc, nm, name, nlen, 16, 17))
5133 if (stbtt__matchpair(fc, nm, name, nlen, 1, 2))
5135 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
5142 static int stbtt_FindMatchingFont_internal(
unsigned char *font_collection,
char *name_utf8, stbtt_int32 flags)
5146 stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
5149 if (stbtt__matches((stbtt_uint8 *)font_collection, off, (stbtt_uint8 *)name_utf8, flags))
5154 #if defined(__GNUC__) || defined(__clang__)
5155 #pragma GCC diagnostic push
5156 #pragma GCC diagnostic ignored "-Wcast-qual"
5159 STBTT_DEF
int stbtt_BakeFontBitmap(
const unsigned char *data,
int offset,
float pixel_height,
unsigned char *pixels,
5160 int pw,
int ph,
int first_char,
int num_chars, stbtt_bakedchar *chardata)
5162 return stbtt_BakeFontBitmap_internal((
unsigned char *)data, offset, pixel_height, pixels, pw, ph, first_char,
5163 num_chars, chardata);
5166 STBTT_DEF
int stbtt_GetFontOffsetForIndex(
const unsigned char *data,
int index)
5168 return stbtt_GetFontOffsetForIndex_internal((
unsigned char *)data, index);
5171 STBTT_DEF
int stbtt_GetNumberOfFonts(
const unsigned char *data)
5173 return stbtt_GetNumberOfFonts_internal((
unsigned char *)data);
5176 STBTT_DEF
int stbtt_InitFont(stbtt_fontinfo *info,
const unsigned char *data,
int offset)
5178 return stbtt_InitFont_internal(info, (
unsigned char *)data, offset);
5181 STBTT_DEF
int stbtt_FindMatchingFont(
const unsigned char *fontdata,
const char *name,
int flags)
5183 return stbtt_FindMatchingFont_internal((
unsigned char *)fontdata, (
char *)name, flags);
5186 STBTT_DEF
int stbtt_CompareUTF8toUTF16_bigendian(
const char *s1,
int len1,
const char *s2,
int len2)
5188 return stbtt_CompareUTF8toUTF16_bigendian_internal((
char *)s1, len1, (
char *)s2, len2);
5191 #if defined(__GNUC__) || defined(__clang__)
5192 #pragma GCC diagnostic pop