241 #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
244 unsigned char ttf_buffer[1<<20];
245 unsigned char temp_bitmap[512*512];
250 void my_stbtt_initfont(
void)
252 fread(ttf_buffer, 1, 1<<20, fopen(
"c:/windows/fonts/times.ttf",
"rb"));
255 glGenTextures(1, &ftex);
256 glBindTexture(GL_TEXTURE_2D, ftex);
257 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
262 void my_stbtt_print(
float x,
float y,
char *text)
265 glEnable(GL_TEXTURE_2D);
266 glBindTexture(GL_TEXTURE_2D, ftex);
269 if (*text >= 32 && *text < 128) {
272 glTexCoord2f(q.
s0,q.
t1); glVertex2f(q.
x0,q.
y0);
273 glTexCoord2f(q.
s1,q.
t1); glVertex2f(q.
x1,q.
y0);
274 glTexCoord2f(q.
s1,q.
t0); glVertex2f(q.
x1,q.
y1);
275 glTexCoord2f(q.
s0,q.
t0); glVertex2f(q.
x0,q.
y1);
290 #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
293 char ttf_buffer[1<<25];
295 int main(
int argc,
char **argv)
298 unsigned char *bitmap;
299 int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) :
'a'), s = (argc > 2 ? atoi(argv[2]) : 20);
301 fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] :
"c:/windows/fonts/arialbd.ttf",
"rb"));
306 for (j=0; j < h; ++j) {
307 for (i=0; i < w; ++i)
308 putchar(
" .:ioVM@"[bitmap[j*w+i]>>5]);
334 unsigned char screen[20][79];
336 int main(
int arg,
char **argv)
339 int i,j,ascent,baseline,ch=0;
341 char *text =
"Heljo World!";
343 fread(buffer, 1, 1000000, fopen(
"c:/windows/fonts/arialbd.ttf",
"rb"));
348 baseline = (
int) (ascent*scale);
351 int advance,lsb,x0,y0,x1,y1;
352 float x_shift = xpos - (float) floor(xpos);
360 xpos += (advance * scale);
366 for (j=0; j < 20; ++j) {
367 for (i=0; i < 78; ++i)
368 putchar(
" .:ioVM@"[screen[j][i]>>5]);
385 #ifdef STB_TRUETYPE_IMPLEMENTATION
388 typedef unsigned char stbtt_uint8;
389 typedef signed char stbtt_int8;
390 typedef unsigned short stbtt_uint16;
391 typedef signed short stbtt_int16;
392 typedef unsigned int stbtt_uint32;
393 typedef signed int stbtt_int32;
396 typedef char stbtt__check_size32[
sizeof(stbtt_int32)==4 ? 1 : -1];
397 typedef char stbtt__check_size16[
sizeof(stbtt_int16)==2 ? 1 : -1];
402 #define STBTT_ifloor(x) ((int) floor(x))
403 #define STBTT_iceil(x) ((int) ceil(x))
408 #define STBTT_sqrt(x) sqrt(x)
413 #define STBTT_fabs(x) fabs(x)
419 #define STBTT_malloc(x,u) ((void)(u),malloc(x))
420 #define STBTT_free(x,u) ((void)(u),free(x))
425 #define STBTT_assert(x) assert(x)
430 #define STBTT_strlen(x) strlen(x)
435 #define STBTT_memcpy memcpy
436 #define STBTT_memset memset
447 #ifndef __STB_INCLUDE_STB_TRUETYPE_H__
448 #define __STB_INCLUDE_STB_TRUETYPE_H__
451 #define STBTT_DEF static
453 #define STBTT_DEF extern
477 unsigned short x0,y0,x1,
y1;
483 unsigned char *pixels,
int pw,
int ph,
484 int first_char,
int num_chars,
499 float *xpos,
float *ypos,
501 int opengl_fillrule);
523 unsigned short x0,y0,x1,
y1;
530 #ifndef STB_RECT_PACK_VERSION
548 #define STBTT_POINT_SIZE(x) (-(x))
551 int first_unicode_char_in_range,
int num_chars_in_range,
stbtt_packedchar *chardata_for_range);
599 float *xpos,
float *ypos,
601 int align_to_integer);
743 #ifndef STBTT_vmove // you can predefine these to use different values (but why?)
752 #ifndef stbtt_vertex // you can predefine this to use different values
754 #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file
841 float flatness_in_pixels,
844 float scale_x,
float scale_y,
845 float shift_x,
float shift_y,
846 int x_off,
int y_off,
877 #define STBTT_MACSTYLE_DONTCARE 0
878 #define STBTT_MACSTYLE_BOLD 1
879 #define STBTT_MACSTYLE_ITALIC 2
880 #define STBTT_MACSTYLE_UNDERSCORE 4
881 #define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0
948 #endif // __STB_INCLUDE_STB_TRUETYPE_H__
957 #ifdef STB_TRUETYPE_IMPLEMENTATION
959 #ifndef STBTT_MAX_OVERSAMPLE
960 #define STBTT_MAX_OVERSAMPLE 8
963 #if STBTT_MAX_OVERSAMPLE > 255
964 #error "STBTT_MAX_OVERSAMPLE cannot be > 255"
967 typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1];
969 #ifndef STBTT_RASTERIZER_VERSION
970 #define STBTT_RASTERIZER_VERSION 2
974 #define STBTT__NOTUSED(v) (void)(v)
976 #define STBTT__NOTUSED(v) (void)sizeof(v)
984 static stbtt_uint8 stbtt__buf_get8(
stbtt__buf *b)
991 static stbtt_uint8 stbtt__buf_peek8(
stbtt__buf *b)
998 static void stbtt__buf_seek(
stbtt__buf *b,
int o)
1000 STBTT_assert(!(o > b->
size || o < 0));
1004 static void stbtt__buf_skip(
stbtt__buf *b,
int o)
1006 stbtt__buf_seek(b, b->
cursor + o);
1009 static stbtt_uint32 stbtt__buf_get(
stbtt__buf *b,
int n)
1013 STBTT_assert(n >= 1 && n <= 4);
1014 for (i = 0; i < n; i++)
1015 v = (v << 8) | stbtt__buf_get8(b);
1022 STBTT_assert(size < 0x40000000);
1023 r.
data = (stbtt_uint8*) p;
1029 #define stbtt__buf_get16(b) stbtt__buf_get((b), 2)
1030 #define stbtt__buf_get32(b) stbtt__buf_get((b), 4)
1035 if (o < 0 || s < 0 || o > b->
size || s > b->
size - o)
return r;
1043 int count, start, offsize;
1045 count = stbtt__buf_get16(b);
1047 offsize = stbtt__buf_get8(b);
1048 STBTT_assert(offsize >= 1 && offsize <= 4);
1049 stbtt__buf_skip(b, offsize * count);
1050 stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);
1052 return stbtt__buf_range(b, start, b->
cursor - start);
1055 static stbtt_uint32 stbtt__cff_int(
stbtt__buf *b)
1057 int b0 = stbtt__buf_get8(b);
1058 if (b0 >= 32 && b0 <= 246)
return b0 - 139;
1059 else if (b0 >= 247 && b0 <= 250)
return (b0 - 247)*256 + stbtt__buf_get8(b) + 108;
1060 else if (b0 >= 251 && b0 <= 254)
return -(b0 - 251)*256 - stbtt__buf_get8(b) - 108;
1061 else if (b0 == 28)
return stbtt__buf_get16(b);
1062 else if (b0 == 29)
return stbtt__buf_get32(b);
1067 static void stbtt__cff_skip_operand(
stbtt__buf *b) {
1068 int v, b0 = stbtt__buf_peek8(b);
1069 STBTT_assert(b0 >= 28);
1071 stbtt__buf_skip(b, 1);
1073 v = stbtt__buf_get8(b);
1074 if ((v & 0xF) == 0xF || (v >> 4) == 0xF)
1084 stbtt__buf_seek(b, 0);
1086 int start = b->
cursor, end, op;
1087 while (stbtt__buf_peek8(b) >= 28)
1088 stbtt__cff_skip_operand(b);
1090 op = stbtt__buf_get8(b);
1091 if (op == 12) op = stbtt__buf_get8(b) | 0x100;
1092 if (op == key)
return stbtt__buf_range(b, start, end-start);
1094 return stbtt__buf_range(b, 0, 0);
1097 static void stbtt__dict_get_ints(
stbtt__buf *b,
int key,
int outcount, stbtt_uint32 *out)
1100 stbtt__buf operands = stbtt__dict_get(b, key);
1101 for (i = 0; i < outcount && operands.
cursor < operands.
size; i++)
1102 out[i] = stbtt__cff_int(&operands);
1105 static int stbtt__cff_index_count(
stbtt__buf *b)
1107 stbtt__buf_seek(b, 0);
1108 return stbtt__buf_get16(b);
1113 int count, offsize, start, end;
1114 stbtt__buf_seek(&b, 0);
1115 count = stbtt__buf_get16(&b);
1116 offsize = stbtt__buf_get8(&b);
1117 STBTT_assert(i >= 0 && i < count);
1118 STBTT_assert(offsize >= 1 && offsize <= 4);
1119 stbtt__buf_skip(&b, i*offsize);
1120 start = stbtt__buf_get(&b, offsize);
1121 end = stbtt__buf_get(&b, offsize);
1122 return stbtt__buf_range(&b, 2+(count+1)*offsize+start, end - start);
1133 #define ttBYTE(p) (* (stbtt_uint8 *) (p))
1134 #define ttCHAR(p) (* (stbtt_int8 *) (p))
1135 #define ttFixed(p) ttLONG(p)
1137 static stbtt_uint16 ttUSHORT(stbtt_uint8 *p) {
return p[0]*256 + p[1]; }
1138 static stbtt_int16 ttSHORT(stbtt_uint8 *p) {
return p[0]*256 + p[1]; }
1139 static stbtt_uint32 ttULONG(stbtt_uint8 *p) {
return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
1140 static stbtt_int32 ttLONG(stbtt_uint8 *p) {
return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
1142 #define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
1143 #define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3])
1145 static int stbtt__isfont(stbtt_uint8 *font)
1148 if (stbtt_tag4(font,
'1',0,0,0))
return 1;
1149 if (stbtt_tag(font,
"typ1"))
return 1;
1150 if (stbtt_tag(font,
"OTTO"))
return 1;
1151 if (stbtt_tag4(font, 0,1,0,0))
return 1;
1152 if (stbtt_tag(font,
"true"))
return 1;
1157 static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart,
const char *tag)
1159 stbtt_int32 num_tables = ttUSHORT(data+fontstart+4);
1160 stbtt_uint32 tabledir = fontstart + 12;
1162 for (i=0; i < num_tables; ++i) {
1163 stbtt_uint32 loc = tabledir + 16*i;
1164 if (stbtt_tag(data+loc+0, tag))
1165 return ttULONG(data+loc+8);
1170 static int stbtt_GetFontOffsetForIndex_internal(
unsigned char *font_collection,
int index)
1173 if (stbtt__isfont(font_collection))
1174 return index == 0 ? 0 : -1;
1177 if (stbtt_tag(font_collection,
"ttcf")) {
1179 if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
1180 stbtt_int32 n = ttLONG(font_collection+8);
1183 return ttULONG(font_collection+12+index*4);
1189 static int stbtt_GetNumberOfFonts_internal(
unsigned char *font_collection)
1192 if (stbtt__isfont(font_collection))
1196 if (stbtt_tag(font_collection,
"ttcf")) {
1198 if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
1199 return ttLONG(font_collection+8);
1207 stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 };
1209 stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);
1210 if (!private_loc[1] || !private_loc[0])
return stbtt__new_buf(NULL, 0);
1211 pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);
1212 stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);
1213 if (!subrsoff)
return stbtt__new_buf(NULL, 0);
1214 stbtt__buf_seek(&cff, private_loc[1]+subrsoff);
1215 return stbtt__cff_get_index(&cff);
1218 static int stbtt_InitFont_internal(
stbtt_fontinfo *info,
unsigned char *data,
int fontstart)
1220 stbtt_uint32 cmap, t;
1221 stbtt_int32 i,numTables;
1225 info->
cff = stbtt__new_buf(NULL, 0);
1227 cmap = stbtt__find_table(data, fontstart,
"cmap");
1228 info->
loca = stbtt__find_table(data, fontstart,
"loca");
1229 info->
head = stbtt__find_table(data, fontstart,
"head");
1230 info->
glyf = stbtt__find_table(data, fontstart,
"glyf");
1231 info->
hhea = stbtt__find_table(data, fontstart,
"hhea");
1232 info->
hmtx = stbtt__find_table(data, fontstart,
"hmtx");
1233 info->
kern = stbtt__find_table(data, fontstart,
"kern");
1235 if (!cmap || !info->
head || !info->
hhea || !info->
hmtx)
1239 if (!info->
loca)
return 0;
1243 stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;
1246 cff = stbtt__find_table(data, fontstart,
"CFF ");
1249 info->
fontdicts = stbtt__new_buf(NULL, 0);
1250 info->
fdselect = stbtt__new_buf(NULL, 0);
1253 info->
cff = stbtt__new_buf(data+cff, 512*1024*1024);
1257 stbtt__buf_skip(&b, 2);
1258 stbtt__buf_seek(&b, stbtt__buf_get8(&b));
1262 stbtt__cff_get_index(&b);
1263 topdictidx = stbtt__cff_get_index(&b);
1264 topdict = stbtt__cff_index_get(topdictidx, 0);
1265 stbtt__cff_get_index(&b);
1266 info->
gsubrs = stbtt__cff_get_index(&b);
1268 stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);
1269 stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
1270 stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);
1271 stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);
1272 info->
subrs = stbtt__get_subrs(b, topdict);
1275 if (cstype != 2)
return 0;
1276 if (charstrings == 0)
return 0;
1280 if (!fdselectoff)
return 0;
1281 stbtt__buf_seek(&b, fdarrayoff);
1282 info->
fontdicts = stbtt__cff_get_index(&b);
1283 info->
fdselect = stbtt__buf_range(&b, fdselectoff, b.
size-fdselectoff);
1286 stbtt__buf_seek(&b, charstrings);
1290 t = stbtt__find_table(data, fontstart,
"maxp");
1299 numTables = ttUSHORT(data + cmap + 2);
1301 for (i=0; i < numTables; ++i) {
1302 stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
1304 switch(ttUSHORT(data+encoding_record)) {
1306 switch (ttUSHORT(data+encoding_record+2)) {
1310 info->
index_map = cmap + ttULONG(data+encoding_record+4);
1317 info->
index_map = cmap + ttULONG(data+encoding_record+4);
1330 stbtt_uint8 *data = info->
data;
1331 stbtt_uint32 index_map = info->
index_map;
1333 stbtt_uint16 format = ttUSHORT(data + index_map + 0);
1335 stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
1336 if (unicode_codepoint < bytes-6)
1337 return ttBYTE(data + index_map + 6 + unicode_codepoint);
1339 }
else if (format == 6) {
1340 stbtt_uint32 first = ttUSHORT(data + index_map + 6);
1341 stbtt_uint32 count = ttUSHORT(data + index_map + 8);
1342 if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)
1343 return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
1345 }
else if (format == 2) {
1348 }
else if (format == 4) {
1349 stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1;
1350 stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1;
1351 stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10);
1352 stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1;
1355 stbtt_uint32 endCount = index_map + 14;
1356 stbtt_uint32 search = endCount;
1358 if (unicode_codepoint > 0xffff)
1363 if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))
1364 search += rangeShift*2;
1368 while (entrySelector) {
1371 end = ttUSHORT(data + search + searchRange*2);
1372 if (unicode_codepoint > end)
1373 search += searchRange*2;
1379 stbtt_uint16
offset, start;
1380 stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1);
1382 STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));
1383 start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
1384 if (unicode_codepoint < start)
1387 offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
1389 return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
1391 return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
1393 }
else if (format == 12 || format == 13) {
1394 stbtt_uint32 ngroups = ttULONG(data+index_map+12);
1395 stbtt_int32 low,high;
1396 low = 0; high = (stbtt_int32)ngroups;
1398 while (low < high) {
1399 stbtt_int32 mid = low + ((high-low) >> 1);
1400 stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12);
1401 stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4);
1402 if ((stbtt_uint32) unicode_codepoint < start_char)
1404 else if ((stbtt_uint32) unicode_codepoint > end_char)
1407 stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);
1409 return start_glyph + unicode_codepoint-start_char;
1426 static void stbtt_setvertex(
stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy)
1429 v->
x = (stbtt_int16) x;
1430 v->
y = (stbtt_int16) y;
1431 v->
cx = (stbtt_int16) cx;
1432 v->
cy = (stbtt_int16) cy;
1435 static int stbtt__GetGlyfOffset(
const stbtt_fontinfo *info,
int glyph_index)
1439 STBTT_assert(!info->
cff.
size);
1441 if (glyph_index >= info->
numGlyphs)
return -1;
1445 g1 = info->
glyf + ttUSHORT(info->
data + info->
loca + glyph_index * 2) * 2;
1446 g2 = info->
glyf + ttUSHORT(info->
data + info->
loca + glyph_index * 2 + 2) * 2;
1448 g1 = info->
glyf + ttULONG (info->
data + info->
loca + glyph_index * 4);
1449 g2 = info->
glyf + ttULONG (info->
data + info->
loca + glyph_index * 4 + 4);
1452 return g1==g2 ? -1 : g1;
1455 static int stbtt__GetGlyphInfoT2(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1);
1460 stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);
1462 int g = stbtt__GetGlyfOffset(info, glyph_index);
1463 if (g < 0)
return 0;
1465 if (x0) *x0 = ttSHORT(info->
data + g + 2);
1466 if (y0) *y0 = ttSHORT(info->
data + g + 4);
1467 if (x1) *x1 = ttSHORT(info->
data + g + 6);
1468 if (y1) *y1 = ttSHORT(info->
data + g + 8);
1480 stbtt_int16 numberOfContours;
1483 return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0;
1484 g = stbtt__GetGlyfOffset(info, glyph_index);
1485 if (g < 0)
return 1;
1486 numberOfContours = ttSHORT(info->
data + g);
1487 return numberOfContours == 0;
1490 static int stbtt__close_shape(
stbtt_vertex *vertices,
int num_vertices,
int was_off,
int start_off,
1491 stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
1495 stbtt_setvertex(&vertices[num_vertices++],
STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
1496 stbtt_setvertex(&vertices[num_vertices++],
STBTT_vcurve, sx,sy,scx,scy);
1499 stbtt_setvertex(&vertices[num_vertices++],
STBTT_vcurve,sx,sy,cx,cy);
1501 stbtt_setvertex(&vertices[num_vertices++],
STBTT_vline,sx,sy,0,0);
1503 return num_vertices;
1508 stbtt_int16 numberOfContours;
1509 stbtt_uint8 *endPtsOfContours;
1510 stbtt_uint8 *data = info->
data;
1513 int g = stbtt__GetGlyfOffset(info, glyph_index);
1517 if (g < 0)
return 0;
1519 numberOfContours = ttSHORT(data + g);
1521 if (numberOfContours > 0) {
1522 stbtt_uint8 flags=0,flagcount;
1523 stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
1524 stbtt_int32 x,y,cx,cy,sx,sy, scx,scy;
1525 stbtt_uint8 *points;
1526 endPtsOfContours = (data + g + 10);
1527 ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
1528 points = data + g + 10 + numberOfContours * 2 + 2 + ins;
1530 n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2);
1532 m = n + 2*numberOfContours;
1548 for (i=0; i < n; ++i) {
1549 if (flagcount == 0) {
1552 flagcount = *points++;
1555 vertices[off+i].
type = flags;
1560 for (i=0; i < n; ++i) {
1561 flags = vertices[off+i].
type;
1563 stbtt_int16 dx = *points++;
1564 x += (flags & 16) ? dx : -dx;
1566 if (!(flags & 16)) {
1567 x = x + (stbtt_int16) (points[0]*256 + points[1]);
1571 vertices[off+i].
x = (stbtt_int16) x;
1576 for (i=0; i < n; ++i) {
1577 flags = vertices[off+i].
type;
1579 stbtt_int16 dy = *points++;
1580 y += (flags & 32) ? dy : -dy;
1582 if (!(flags & 32)) {
1583 y = y + (stbtt_int16) (points[0]*256 + points[1]);
1587 vertices[off+i].
y = (stbtt_int16) y;
1592 sx = sy = cx = cy = scx = scy = 0;
1593 for (i=0; i < n; ++i) {
1594 flags = vertices[off+i].
type;
1595 x = (stbtt_int16) vertices[off+i].x;
1596 y = (stbtt_int16) vertices[off+i].y;
1598 if (next_move == i) {
1600 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
1603 start_off = !(flags & 1);
1609 if (!(vertices[off+i+1].type & 1)) {
1611 sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1;
1612 sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1;
1615 sx = (stbtt_int32) vertices[off+i+1].x;
1616 sy = (stbtt_int32) vertices[off+i+1].y;
1623 stbtt_setvertex(&vertices[num_vertices++],
STBTT_vmove,sx,sy,0,0);
1625 next_move = 1 + ttUSHORT(endPtsOfContours+j*2);
1630 stbtt_setvertex(&vertices[num_vertices++],
STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
1636 stbtt_setvertex(&vertices[num_vertices++],
STBTT_vcurve, x,y, cx, cy);
1638 stbtt_setvertex(&vertices[num_vertices++],
STBTT_vline, x,y,0,0);
1643 num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
1644 }
else if (numberOfContours == -1) {
1647 stbtt_uint8 *comp = data + g + 10;
1651 stbtt_uint16 flags, gidx;
1652 int comp_num_verts = 0, i;
1654 float mtx[6] = {1,0,0,1,0,0}, m, n;
1656 flags = ttSHORT(comp); comp+=2;
1657 gidx = ttSHORT(comp); comp+=2;
1661 mtx[4] = ttSHORT(comp); comp+=2;
1662 mtx[5] = ttSHORT(comp); comp+=2;
1664 mtx[4] = ttCHAR(comp); comp+=1;
1665 mtx[5] = ttCHAR(comp); comp+=1;
1672 if (flags & (1<<3)) {
1673 mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1674 mtx[1] = mtx[2] = 0;
1675 }
else if (flags & (1<<6)) {
1676 mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
1677 mtx[1] = mtx[2] = 0;
1678 mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1679 }
else if (flags & (1<<7)) {
1680 mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
1681 mtx[1] = ttSHORT(comp)/16384.0f; comp+=2;
1682 mtx[2] = ttSHORT(comp)/16384.0f; comp+=2;
1683 mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
1687 m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
1688 n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
1692 if (comp_num_verts > 0) {
1694 for (i = 0; i < comp_num_verts; ++i) {
1707 if (vertices) STBTT_free(vertices, info->
userdata);
1708 if (comp_verts) STBTT_free(comp_verts, info->
userdata);
1711 if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*
sizeof(
stbtt_vertex));
1712 STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*
sizeof(
stbtt_vertex));
1713 if (vertices) STBTT_free(vertices, info->
userdata);
1715 STBTT_free(comp_verts, info->
userdata);
1716 num_vertices += comp_num_verts;
1719 more = flags & (1<<5);
1721 }
else if (numberOfContours < 0) {
1728 *pvertices = vertices;
1729 return num_vertices;
1736 float first_x, first_y;
1738 stbtt_int32 min_x, max_x, min_y, max_y;
1744 #define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0}
1746 static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y)
1748 if (x > c->max_x || !c->started) c->max_x = x;
1749 if (y > c->max_y || !c->started) c->max_y = y;
1750 if (x < c->min_x || !c->started) c->min_x = x;
1751 if (y < c->min_y || !c->started) c->min_y = y;
1755 static void stbtt__csctx_v(stbtt__csctx *c, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy, stbtt_int32 cx1, stbtt_int32 cy1)
1758 stbtt__track_vertex(c, x, y);
1760 stbtt__track_vertex(c, cx, cy);
1761 stbtt__track_vertex(c, cx1, cy1);
1764 stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);
1765 c->pvertices[c->num_vertices].cx1 = (stbtt_int16) cx1;
1766 c->pvertices[c->num_vertices].cy1 = (stbtt_int16) cy1;
1771 static void stbtt__csctx_close_shape(stbtt__csctx *ctx)
1773 if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)
1774 stbtt__csctx_v(ctx,
STBTT_vline, (
int)ctx->first_x, (
int)ctx->first_y, 0, 0, 0, 0);
1777 static void stbtt__csctx_rmove_to(stbtt__csctx *ctx,
float dx,
float dy)
1779 stbtt__csctx_close_shape(ctx);
1780 ctx->first_x = ctx->x = ctx->x + dx;
1781 ctx->first_y = ctx->y = ctx->y + dy;
1782 stbtt__csctx_v(ctx,
STBTT_vmove, (
int)ctx->x, (
int)ctx->y, 0, 0, 0, 0);
1785 static void stbtt__csctx_rline_to(stbtt__csctx *ctx,
float dx,
float dy)
1789 stbtt__csctx_v(ctx,
STBTT_vline, (
int)ctx->x, (
int)ctx->y, 0, 0, 0, 0);
1792 static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx,
float dx1,
float dy1,
float dx2,
float dy2,
float dx3,
float dy3)
1794 float cx1 = ctx->x + dx1;
1795 float cy1 = ctx->y + dy1;
1796 float cx2 = cx1 + dx2;
1797 float cy2 = cy1 + dy2;
1800 stbtt__csctx_v(ctx,
STBTT_vcubic, (
int)ctx->x, (
int)ctx->y, (
int)cx1, (
int)cy1, (
int)cx2, (
int)cy2);
1805 int count = stbtt__cff_index_count(&idx);
1809 else if (count >= 1240)
1812 if (n < 0 || n >= count)
1813 return stbtt__new_buf(NULL, 0);
1814 return stbtt__cff_index_get(idx, n);
1820 int nranges, start, end, v, fmt, fdselector = -1, i;
1822 stbtt__buf_seek(&fdselect, 0);
1823 fmt = stbtt__buf_get8(&fdselect);
1826 stbtt__buf_skip(&fdselect, glyph_index);
1827 fdselector = stbtt__buf_get8(&fdselect);
1828 }
else if (fmt == 3) {
1829 nranges = stbtt__buf_get16(&fdselect);
1830 start = stbtt__buf_get16(&fdselect);
1831 for (i = 0; i < nranges; i++) {
1832 v = stbtt__buf_get8(&fdselect);
1833 end = stbtt__buf_get16(&fdselect);
1834 if (glyph_index >= start && glyph_index < end) {
1841 if (fdselector == -1) stbtt__new_buf(NULL, 0);
1842 return stbtt__get_subrs(info->
cff, stbtt__cff_index_get(info->
fontdicts, fdselector));
1845 static int stbtt__run_charstring(
const stbtt_fontinfo *info,
int glyph_index, stbtt__csctx *c)
1847 int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;
1848 int has_subrs = 0, clear_stack;
1853 #define STBTT__CSERR(s) (0)
1856 b = stbtt__cff_index_get(info->
charstrings, glyph_index);
1860 b0 = stbtt__buf_get8(&b);
1866 maskbits += (sp / 2);
1868 stbtt__buf_skip(&b, (maskbits + 7) / 8);
1875 maskbits += (sp / 2);
1880 if (sp < 2)
return STBTT__CSERR(
"rmoveto stack");
1881 stbtt__csctx_rmove_to(c, s[sp-2], s[sp-1]);
1885 if (sp < 1)
return STBTT__CSERR(
"vmoveto stack");
1886 stbtt__csctx_rmove_to(c, 0, s[sp-1]);
1890 if (sp < 1)
return STBTT__CSERR(
"hmoveto stack");
1891 stbtt__csctx_rmove_to(c, s[sp-1], 0);
1895 if (sp < 2)
return STBTT__CSERR(
"rlineto stack");
1896 for (; i + 1 < sp; i += 2)
1897 stbtt__csctx_rline_to(c, s[i], s[i+1]);
1904 if (sp < 1)
return STBTT__CSERR(
"vlineto stack");
1907 if (sp < 1)
return STBTT__CSERR(
"hlineto stack");
1910 stbtt__csctx_rline_to(c, s[i], 0);
1914 stbtt__csctx_rline_to(c, 0, s[i]);
1920 if (sp < 4)
return STBTT__CSERR(
"hvcurveto stack");
1923 if (sp < 4)
return STBTT__CSERR(
"vhcurveto stack");
1925 if (i + 3 >= sp)
break;
1926 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);
1929 if (i + 3 >= sp)
break;
1930 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]);
1936 if (sp < 6)
return STBTT__CSERR(
"rcurveline stack");
1937 for (; i + 5 < sp; i += 6)
1938 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
1942 if (sp < 8)
return STBTT__CSERR(
"rcurveline stack");
1943 for (; i + 5 < sp - 2; i += 6)
1944 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
1945 if (i + 1 >= sp)
return STBTT__CSERR(
"rcurveline stack");
1946 stbtt__csctx_rline_to(c, s[i], s[i+1]);
1950 if (sp < 8)
return STBTT__CSERR(
"rlinecurve stack");
1951 for (; i + 1 < sp - 6; i += 2)
1952 stbtt__csctx_rline_to(c, s[i], s[i+1]);
1953 if (i + 5 >= sp)
return STBTT__CSERR(
"rlinecurve stack");
1954 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
1959 if (sp < 4)
return STBTT__CSERR(
"(vv|hh)curveto stack");
1961 if (sp & 1) { f = s[i]; i++; }
1962 for (; i + 3 < sp; i += 4) {
1964 stbtt__csctx_rccurve_to(c, s[i], f, s[i+1], s[i+2], s[i+3], 0.0);
1966 stbtt__csctx_rccurve_to(c, f, s[i], s[i+1], s[i+2], 0.0, s[i+3]);
1974 subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);
1979 if (sp < 1)
return STBTT__CSERR(
"call(g|)subr stack");
1981 if (subr_stack_height >= 10)
return STBTT__CSERR(
"recursion limit");
1982 subr_stack[subr_stack_height++] = b;
1983 b = stbtt__get_subr(b0 == 0x0A ? subrs : info->
gsubrs, v);
1984 if (b.
size == 0)
return STBTT__CSERR(
"subr not found");
1990 if (subr_stack_height <= 0)
return STBTT__CSERR(
"return outside subr");
1991 b = subr_stack[--subr_stack_height];
1996 stbtt__csctx_close_shape(c);
2000 float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;
2002 int b1 = stbtt__buf_get8(&b);
2007 if (sp < 7)
return STBTT__CSERR(
"hflex stack");
2015 stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);
2016 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);
2020 if (sp < 13)
return STBTT__CSERR(
"flex stack");
2034 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
2035 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
2039 if (sp < 9)
return STBTT__CSERR(
"hflex1 stack");
2049 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);
2050 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1+dy2+dy5));
2054 if (sp < 11)
return STBTT__CSERR(
"flex1 stack");
2066 dx = dx1+dx2+dx3+dx4+dx5;
2067 dy = dy1+dy2+dy3+dy4+dy5;
2068 if (STBTT_fabs(dx) > STBTT_fabs(dy))
2072 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
2073 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
2077 return STBTT__CSERR(
"unimplemented");
2082 if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254))
2083 return STBTT__CSERR(
"reserved operator");
2087 f = (float)stbtt__buf_get32(&b) / 0x10000;
2089 stbtt__buf_skip(&b, -1);
2090 f = (float)(stbtt_int16)stbtt__cff_int(&b);
2092 if (sp >= 48)
return STBTT__CSERR(
"push stack overflow");
2097 if (clear_stack) sp = 0;
2099 return STBTT__CSERR(
"no endchar");
2107 stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);
2108 stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);
2109 if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {
2111 output_ctx.pvertices = *pvertices;
2112 if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {
2113 STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);
2114 return output_ctx.num_vertices;
2121 static int stbtt__GetGlyphInfoT2(
const stbtt_fontinfo *info,
int glyph_index,
int *x0,
int *y0,
int *x1,
int *y1)
2123 stbtt__csctx c = STBTT__CSCTX_INIT(1);
2124 int r = stbtt__run_charstring(info, glyph_index, &c);
2126 *x0 = r ? c.min_x : 0;
2127 *y0 = r ? c.min_y : 0;
2128 *x1 = r ? c.max_x : 0;
2129 *y1 = r ? c.max_y : 0;
2131 return r ? c.num_vertices : 0;
2137 return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);
2139 return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);
2144 stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->
data+info->
hhea + 34);
2145 if (glyph_index < numOfLongHorMetrics) {
2146 if (advanceWidth) *advanceWidth = ttSHORT(info->
data + info->
hmtx + 4*glyph_index);
2147 if (leftSideBearing) *leftSideBearing = ttSHORT(info->
data + info->
hmtx + 4*glyph_index + 2);
2149 if (advanceWidth) *advanceWidth = ttSHORT(info->
data + info->
hmtx + 4*(numOfLongHorMetrics-1));
2150 if (leftSideBearing) *leftSideBearing = ttSHORT(info->
data + info->
hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
2156 stbtt_uint8 *data = info->
data + info->
kern;
2157 stbtt_uint32 needle, straw;
2163 if (ttUSHORT(data+2) < 1)
2165 if (ttUSHORT(data+8) != 1)
2169 r = ttUSHORT(data+10) - 1;
2170 needle = glyph1 << 16 | glyph2;
2173 straw = ttULONG(data+18+(m*6));
2176 else if (needle > straw)
2179 return ttSHORT(data+22+(m*6));
2198 if (ascent ) *ascent = ttSHORT(info->
data+info->
hhea + 4);
2199 if (descent) *descent = ttSHORT(info->
data+info->
hhea + 6);
2200 if (lineGap) *lineGap = ttSHORT(info->
data+info->
hhea + 8);
2205 *x0 = ttSHORT(info->
data + info->
head + 36);
2206 *y0 = ttSHORT(info->
data + info->
head + 38);
2207 *x1 = ttSHORT(info->
data + info->
head + 40);
2208 *y1 = ttSHORT(info->
data + info->
head + 42);
2213 int fheight = ttSHORT(info->
data + info->
hhea + 4) - ttSHORT(info->
data + info->
hhea + 6);
2214 return (
float) height / fheight;
2219 int unitsPerEm = ttUSHORT(info->
data + info->
head + 18);
2220 return pixels / unitsPerEm;
2235 int x0=0,y0=0,x1,y1;
2244 if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x);
2245 if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
2246 if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x);
2247 if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y);
2258 stbtt_GetGlyphBitmapBoxSubpixel(font,
stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1);
2270 typedef struct stbtt__hheap_chunk
2272 struct stbtt__hheap_chunk *next;
2273 } stbtt__hheap_chunk;
2275 typedef struct stbtt__hheap
2277 struct stbtt__hheap_chunk *head;
2279 int num_remaining_in_head_chunk;
2282 static void *stbtt__hheap_alloc(stbtt__hheap *hh,
size_t size,
void *userdata)
2284 if (hh->first_free) {
2285 void *p = hh->first_free;
2286 hh->first_free = * (
void **) p;
2289 if (hh->num_remaining_in_head_chunk == 0) {
2290 int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
2291 stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(
sizeof(stbtt__hheap_chunk) + size * count, userdata);
2296 hh->num_remaining_in_head_chunk = count;
2298 --hh->num_remaining_in_head_chunk;
2299 return (
char *) (hh->head) + size * hh->num_remaining_in_head_chunk;
2303 static void stbtt__hheap_free(stbtt__hheap *hh,
void *p)
2305 *(
void **) p = hh->first_free;
2309 static void stbtt__hheap_cleanup(stbtt__hheap *hh,
void *userdata)
2311 stbtt__hheap_chunk *c = hh->head;
2313 stbtt__hheap_chunk *n = c->next;
2314 STBTT_free(c, userdata);
2319 typedef struct stbtt__edge {
2325 typedef struct stbtt__active_edge
2327 struct stbtt__active_edge *next;
2328 #if STBTT_RASTERIZER_VERSION==1
2332 #elif STBTT_RASTERIZER_VERSION==2
2338 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2340 } stbtt__active_edge;
2342 #if STBTT_RASTERIZER_VERSION == 1
2343 #define STBTT_FIXSHIFT 10
2344 #define STBTT_FIX (1 << STBTT_FIXSHIFT)
2345 #define STBTT_FIXMASK (STBTT_FIX-1)
2347 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
void *userdata)
2349 stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
2350 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
2351 STBTT_assert(z != NULL);
2356 z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);
2358 z->dx = STBTT_ifloor(STBTT_FIX * dxdy);
2360 z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - e->y0));
2361 z->x -= off_x * STBTT_FIX;
2365 z->direction = e->invert ? 1 : -1;
2368 #elif STBTT_RASTERIZER_VERSION == 2
2369 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e,
int off_x,
float start_point,
void *userdata)
2371 stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh,
sizeof(*z), userdata);
2372 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
2373 STBTT_assert(z != NULL);
2377 z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f;
2378 z->fx = e->x0 + dxdy * (start_point - e->y0);
2380 z->direction = e->invert ? 1.0f : -1.0f;
2387 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2390 #if STBTT_RASTERIZER_VERSION == 1
2394 static void stbtt__fill_active_edges(
unsigned char *scanline,
int len, stbtt__active_edge *e,
int max_weight)
2402 x0 = e->x; w += e->direction;
2404 int x1 = e->x; w += e->direction;
2407 int i = x0 >> STBTT_FIXSHIFT;
2408 int j = x1 >> STBTT_FIXSHIFT;
2410 if (i < len && j >= 0) {
2413 scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT);
2416 scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);
2421 scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);
2425 for (++i; i < j; ++i)
2426 scanline[i] = scanline[i] + (stbtt_uint8) max_weight;
2436 static void stbtt__rasterize_sorted_edges(
stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
int off_y,
void *userdata)
2438 stbtt__hheap hh = { 0, 0, 0 };
2439 stbtt__active_edge *active = NULL;
2441 int max_weight = (255 / vsubsample);
2443 unsigned char scanline_data[512], *scanline;
2445 if (result->
w > 512)
2446 scanline = (
unsigned char *) STBTT_malloc(result->
w, userdata);
2448 scanline = scanline_data;
2450 y = off_y * vsubsample;
2451 e[n].y0 = (off_y + result->
h) * (
float) vsubsample + 1;
2453 while (j < result->h) {
2454 STBTT_memset(scanline, 0, result->
w);
2455 for (s=0; s < vsubsample; ++s) {
2457 float scan_y = y + 0.5f;
2458 stbtt__active_edge **step = &active;
2463 stbtt__active_edge * z = *step;
2464 if (z->ey <= scan_y) {
2466 STBTT_assert(z->direction);
2468 stbtt__hheap_free(&hh, z);
2471 step = &((*step)->next);
2479 while (*step && (*step)->next) {
2480 if ((*step)->x > (*step)->next->x) {
2481 stbtt__active_edge *t = *step;
2482 stbtt__active_edge *q = t->next;
2489 step = &(*step)->next;
2491 if (!changed)
break;
2495 while (e->y0 <= scan_y) {
2496 if (e->y1 > scan_y) {
2497 stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);
2502 else if (z->x < active->x) {
2508 stbtt__active_edge *p = active;
2509 while (p->next && p->next->x < z->x)
2522 stbtt__fill_active_edges(scanline, result->
w, active, max_weight);
2526 STBTT_memcpy(result->
pixels + j * result->
stride, scanline, result->
w);
2530 stbtt__hheap_cleanup(&hh, userdata);
2532 if (scanline != scanline_data)
2533 STBTT_free(scanline, userdata);
2536 #elif STBTT_RASTERIZER_VERSION == 2
2540 static void stbtt__handle_clipped_edge(
float *scanline,
int x, stbtt__active_edge *e,
float x0,
float y0,
float x1,
float y1)
2542 if (y0 == y1)
return;
2543 STBTT_assert(y0 < y1);
2544 STBTT_assert(e->sy <= e->ey);
2545 if (y0 > e->ey)
return;
2546 if (y1 < e->sy)
return;
2548 x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
2552 x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
2557 STBTT_assert(x1 <= x+1);
2559 STBTT_assert(x1 >= x);
2561 STBTT_assert(x1 <= x);
2563 STBTT_assert(x1 >= x+1);
2565 STBTT_assert(x1 >= x && x1 <= x+1);
2567 if (x0 <= x && x1 <= x)
2568 scanline[x] += e->direction * (y1-y0);
2569 else if (x0 >= x+1 && x1 >= x+1)
2572 STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
2573 scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2);
2577 static void stbtt__fill_active_edges_new(
float *scanline,
float *scanline_fill,
int len, stbtt__active_edge *e,
float y_top)
2579 float y_bottom = y_top+1;
2585 STBTT_assert(e->ey >= y_top);
2591 stbtt__handle_clipped_edge(scanline,(
int) x0,e, x0,y_top, x0,y_bottom);
2592 stbtt__handle_clipped_edge(scanline_fill-1,(
int) x0+1,e, x0,y_top, x0,y_bottom);
2594 stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
2601 float x_top, x_bottom;
2604 STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);
2609 if (e->sy > y_top) {
2610 x_top = x0 + dx * (e->sy - y_top);
2616 if (e->ey < y_bottom) {
2617 x_bottom = x0 + dx * (e->ey - y_top);
2624 if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {
2627 if ((
int) x_top == (
int) x_bottom) {
2630 int x = (
int) x_top;
2632 STBTT_assert(x >= 0 && x < len);
2633 scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height;
2634 scanline_fill[x] += e->direction * height;
2637 float y_crossing, step, sign, area;
2639 if (x_top > x_bottom) {
2642 sy0 = y_bottom - (sy0 - y_top);
2643 sy1 = y_bottom - (sy1 - y_top);
2644 t = sy0, sy0 = sy1, sy1 = t;
2645 t = x_bottom, x_bottom = x_top, x_top = t;
2648 t = x0, x0 = xb, xb = t;
2652 x2 = (
int) x_bottom;
2654 y_crossing = (x1+1 - x0) * dy + y_top;
2656 sign = e->direction;
2658 area = sign * (y_crossing-sy0);
2660 scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2);
2663 for (x = x1+1; x < x2; ++x) {
2664 scanline[x] += area + step/2;
2667 y_crossing += dy * (x2 - (x1+1));
2669 STBTT_assert(STBTT_fabs(area) <= 1.01f);
2671 scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing);
2673 scanline_fill[x2] += sign * (sy1-sy0);
2681 for (x=0; x < len; ++x) {
2697 float x1 = (float) (x);
2698 float x2 = (float) (x+1);
2700 float y3 = y_bottom;
2706 y1 = (x - x0) / dx + y_top;
2707 y2 = (x+1 - x0) / dx + y_top;
2709 if (x0 < x1 && x3 > x2) {
2710 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2711 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2);
2712 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2713 }
else if (x3 < x1 && x0 > x2) {
2714 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2715 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1);
2716 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2717 }
else if (x0 < x1 && x3 > x1) {
2718 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2719 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2720 }
else if (x3 < x1 && x0 > x1) {
2721 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
2722 stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
2723 }
else if (x0 < x2 && x3 > x2) {
2724 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2725 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2726 }
else if (x3 < x2 && x0 > x2) {
2727 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
2728 stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
2730 stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3);
2740 static void stbtt__rasterize_sorted_edges(
stbtt__bitmap *result, stbtt__edge *e,
int n,
int vsubsample,
int off_x,
int off_y,
void *userdata)
2742 stbtt__hheap hh = { 0, 0, 0 };
2743 stbtt__active_edge *active = NULL;
2745 float scanline_data[129], *scanline, *scanline2;
2747 STBTT__NOTUSED(vsubsample);
2750 scanline = (
float *) STBTT_malloc((result->
w*2+1) *
sizeof(float), userdata);
2752 scanline = scanline_data;
2754 scanline2 = scanline + result->
w;
2757 e[n].y0 = (float) (off_y + result->
h) + 1;
2759 while (j < result->h) {
2761 float scan_y_top = y + 0.0f;
2762 float scan_y_bottom = y + 1.0f;
2763 stbtt__active_edge **step = &active;
2765 STBTT_memset(scanline , 0, result->
w*
sizeof(scanline[0]));
2766 STBTT_memset(scanline2, 0, (result->
w+1)*
sizeof(scanline[0]));
2771 stbtt__active_edge * z = *step;
2772 if (z->ey <= scan_y_top) {
2774 STBTT_assert(z->direction);
2776 stbtt__hheap_free(&hh, z);
2778 step = &((*step)->next);
2783 while (e->y0 <= scan_y_bottom) {
2784 if (e->y0 != e->y1) {
2785 stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
2787 STBTT_assert(z->ey >= scan_y_top);
2798 stbtt__fill_active_edges_new(scanline, scanline2+1, result->
w, active, scan_y_top);
2802 for (i=0; i < result->
w; ++i) {
2805 sum += scanline2[i];
2806 k = scanline[i] + sum;
2807 k = (float) STBTT_fabs(k)*255 + 0.5f;
2809 if (m > 255) m = 255;
2816 stbtt__active_edge *z = *step;
2818 step = &((*step)->next);
2825 stbtt__hheap_cleanup(&hh, userdata);
2827 if (scanline != scanline_data)
2828 STBTT_free(scanline, userdata);
2831 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2834 #define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0)
2836 static void stbtt__sort_edges_ins_sort(stbtt__edge *p,
int n)
2839 for (i=1; i < n; ++i) {
2840 stbtt__edge t = p[i], *a = &t;
2843 stbtt__edge *b = &p[j-1];
2844 int c = STBTT__COMPARE(a,b);
2854 static void stbtt__sort_edges_quicksort(stbtt__edge *p,
int n)
2859 int c01,c12,c,m,i,j;
2863 c01 = STBTT__COMPARE(&p[0],&p[m]);
2864 c12 = STBTT__COMPARE(&p[m],&p[n-1]);
2869 c = STBTT__COMPARE(&p[0],&p[n-1]);
2872 z = (c == c12) ? 0 : n-1;
2890 if (!STBTT__COMPARE(&p[i], &p[0]))
break;
2893 if (!STBTT__COMPARE(&p[0], &p[j]))
break;
2906 stbtt__sort_edges_quicksort(p,j);
2910 stbtt__sort_edges_quicksort(p+i, n-i);
2916 static void stbtt__sort_edges(stbtt__edge *p,
int n)
2918 stbtt__sort_edges_quicksort(p, n);
2919 stbtt__sort_edges_ins_sort(p, n);
2927 static void stbtt__rasterize(
stbtt__bitmap *result, stbtt__point *pts,
int *wcount,
int windings,
float scale_x,
float scale_y,
float shift_x,
float shift_y,
int off_x,
int off_y,
int invert,
void *userdata)
2929 float y_scale_inv = invert ? -scale_y : scale_y;
2932 #if STBTT_RASTERIZER_VERSION == 1
2933 int vsubsample = result->
h < 8 ? 15 : 5;
2934 #elif STBTT_RASTERIZER_VERSION == 2
2937 #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
2943 for (i=0; i < windings; ++i)
2946 e = (stbtt__edge *) STBTT_malloc(
sizeof(*e) * (n+1), userdata);
2951 for (i=0; i < windings; ++i) {
2952 stbtt__point *p = pts + m;
2955 for (k=0; k < wcount[i]; j=k++) {
2958 if (p[j].y == p[k].y)
2962 if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
2966 e[n].x0 = p[a].x * scale_x + shift_x;
2967 e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
2968 e[n].x1 = p[b].x * scale_x + shift_x;
2969 e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
2976 stbtt__sort_edges(e, n);
2979 stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
2981 STBTT_free(e, userdata);
2984 static void stbtt__add_point(stbtt__point *points,
int n,
float x,
float y)
2986 if (!points)
return;
2992 static int stbtt__tesselate_curve(stbtt__point *points,
int *num_points,
float x0,
float y0,
float x1,
float y1,
float x2,
float y2,
float objspace_flatness_squared,
int n)
2995 float mx = (x0 + 2*x1 + x2)/4;
2996 float my = (y0 + 2*y1 + y2)/4;
2998 float dx = (x0+x2)/2 - mx;
2999 float dy = (y0+y2)/2 - my;
3002 if (dx*dx+dy*dy > objspace_flatness_squared) {
3003 stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
3004 stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
3006 stbtt__add_point(points, *num_points,x2,y2);
3007 *num_points = *num_points+1;
3012 static void stbtt__tesselate_cubic(stbtt__point *points,
int *num_points,
float x0,
float y0,
float x1,
float y1,
float x2,
float y2,
float x3,
float y3,
float objspace_flatness_squared,
int n)
3023 float longlen = (float) (STBTT_sqrt(dx0*dx0+dy0*dy0)+STBTT_sqrt(dx1*dx1+dy1*dy1)+STBTT_sqrt(dx2*dx2+dy2*dy2));
3024 float shortlen = (float) STBTT_sqrt(dx*dx+dy*dy);
3025 float flatness_squared = longlen*longlen-shortlen*shortlen;
3030 if (flatness_squared > objspace_flatness_squared) {
3031 float x01 = (x0+x1)/2;
3032 float y01 = (y0+y1)/2;
3033 float x12 = (x1+x2)/2;
3034 float y12 = (y1+y2)/2;
3035 float x23 = (x2+x3)/2;
3036 float y23 = (y2+y3)/2;
3038 float xa = (x01+x12)/2;
3039 float ya = (y01+y12)/2;
3040 float xb = (x12+x23)/2;
3041 float yb = (y12+y23)/2;
3043 float mx = (xa+xb)/2;
3044 float my = (ya+yb)/2;
3046 stbtt__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my, objspace_flatness_squared,n+1);
3047 stbtt__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3, objspace_flatness_squared,n+1);
3049 stbtt__add_point(points, *num_points,x3,y3);
3050 *num_points = *num_points+1;
3055 static stbtt__point *stbtt_FlattenCurves(
stbtt_vertex *vertices,
int num_verts,
float objspace_flatness,
int **contour_lengths,
int *num_contours,
void *userdata)
3057 stbtt__point *points=0;
3060 float objspace_flatness_squared = objspace_flatness * objspace_flatness;
3061 int i,n=0,start=0, pass;
3064 for (i=0; i < num_verts; ++i)
3069 if (n == 0)
return 0;
3071 *contour_lengths = (
int *) STBTT_malloc(
sizeof(**contour_lengths) * n, userdata);
3073 if (*contour_lengths == 0) {
3079 for (pass=0; pass < 2; ++pass) {
3082 points = (stbtt__point *) STBTT_malloc(num_points *
sizeof(points[0]), userdata);
3083 if (points == NULL)
goto error;
3087 for (i=0; i < num_verts; ++i) {
3088 switch (vertices[i].type) {
3092 (*contour_lengths)[n] = num_points - start;
3096 x = vertices[i].
x, y = vertices[i].
y;
3097 stbtt__add_point(points, num_points++, x,y);
3100 x = vertices[i].
x, y = vertices[i].
y;
3101 stbtt__add_point(points, num_points++, x, y);
3104 stbtt__tesselate_curve(points, &num_points, x,y,
3105 vertices[i].cx, vertices[i].cy,
3106 vertices[i].x, vertices[i].y,
3107 objspace_flatness_squared, 0);
3108 x = vertices[i].
x, y = vertices[i].
y;
3111 stbtt__tesselate_cubic(points, &num_points, x,y,
3112 vertices[i].cx, vertices[i].cy,
3113 vertices[i].cx1, vertices[i].cy1,
3114 vertices[i].x, vertices[i].y,
3115 objspace_flatness_squared, 0);
3116 x = vertices[i].
x, y = vertices[i].
y;
3120 (*contour_lengths)[n] = num_points - start;
3125 STBTT_free(points, userdata);
3126 STBTT_free(*contour_lengths, userdata);
3127 *contour_lengths = 0;
3132 STBTT_DEF void stbtt_Rasterize(
stbtt__bitmap *result,
float flatness_in_pixels,
stbtt_vertex *vertices,
int num_verts,
float scale_x,
float scale_y,
float shift_x,
float shift_y,
int x_off,
int y_off,
int invert,
void *userdata)
3134 float scale = scale_x > scale_y ? scale_y : scale_x;
3135 int winding_count, *winding_lengths;
3136 stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
3138 stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata);
3139 STBTT_free(winding_lengths, userdata);
3140 STBTT_free(windings, userdata);
3146 STBTT_free(bitmap, userdata);
3151 int ix0,iy0,ix1,iy1;
3156 if (scale_x == 0) scale_x = scale_y;
3159 STBTT_free(vertices, info->
userdata);
3168 gbm.
w = (ix1 - ix0);
3169 gbm.
h = (iy1 - iy0);
3172 if (width ) *width = gbm.
w;
3173 if (height) *height = gbm.
h;
3174 if (xoff ) *xoff = ix0;
3175 if (yoff ) *yoff = iy0;
3177 if (gbm.
w && gbm.
h) {
3178 gbm.
pixels = (
unsigned char *) STBTT_malloc(gbm.
w * gbm.
h, info->
userdata);
3182 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->
userdata);
3185 STBTT_free(vertices, info->
userdata);
3208 stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->
userdata);
3210 STBTT_free(vertices, info->
userdata);
3220 return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y,
stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff);
3225 stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y,
stbtt_FindGlyphIndex(info,codepoint));
3244 static int stbtt_BakeFontBitmap_internal(
unsigned char *data,
int offset,
3246 unsigned char *pixels,
int pw,
int ph,
3247 int first_char,
int num_chars,
3251 int x,y,bottom_y, i;
3256 STBTT_memset(pixels, 0, pw*ph);
3262 for (i=0; i < num_chars; ++i) {
3263 int advance, lsb, x0,y0,x1,y1,gw,gh;
3269 if (x + gw + 1 >= pw)
3270 y = bottom_y, x = 1;
3271 if (y + gh + 1 >= ph)
3273 STBTT_assert(x+gw < pw);
3274 STBTT_assert(y+gh < ph);
3276 chardata[i].
x0 = (stbtt_int16) x;
3277 chardata[i].
y0 = (stbtt_int16) y;
3278 chardata[i].
x1 = (stbtt_int16) (x + gw);
3279 chardata[i].
y1 = (stbtt_int16) (y + gh);
3280 chardata[i].
xadvance = scale * advance;
3281 chardata[i].
xoff = (float) x0;
3282 chardata[i].
yoff = (float) y0;
3284 if (y+gh+1 > bottom_y)
3292 float d3d_bias = opengl_fillrule ? 0 : -0.5f;
3293 float ipw = 1.0f / pw, iph = 1.0f / ph;
3295 int round_x = STBTT_ifloor((*xpos + b->
xoff) + 0.5f);
3296 int round_y = STBTT_ifloor((*ypos + b->
yoff) + 0.5f);
3298 q->
x0 = round_x + d3d_bias;
3299 q->
y0 = round_y + d3d_bias;
3300 q->
x1 = round_x + b->
x1 - b->
x0 + d3d_bias;
3301 q->
y1 = round_y + b->
y1 - b->
y0 + d3d_bias;
3303 q->
s0 = b->
x0 * ipw;
3304 q->
t0 = b->
y0 * iph;
3305 q->
s1 = b->
x1 * ipw;
3306 q->
t1 = b->
y1 * iph;
3316 #ifndef STB_RECT_PACK_VERSION
3355 STBTT__NOTUSED(nodes);
3356 STBTT__NOTUSED(num_nodes);
3362 for (i=0; i < num_rects; ++i) {
3363 if (con->x + rects[i].
w > con->
width) {
3365 con->y = con->bottom_y;
3367 if (con->y + rects[i].
h > con->
height)
3369 rects[i].
x = con->x;
3370 rects[i].
y = con->y;
3372 con->x += rects[i].
w;
3373 if (con->y + rects[i].
h > con->bottom_y)
3374 con->bottom_y = con->y + rects[i].
h;
3376 for ( ; i < num_rects; ++i)
3377 rects[i].was_packed = 0;
3391 int num_nodes = pw - padding;
3394 if (context == NULL || nodes == NULL) {
3395 if (context != NULL) STBTT_free(context, alloc_context);
3396 if (nodes != NULL) STBTT_free(nodes , alloc_context);
3414 STBTT_memset(pixels, 0, pw*ph);
3427 STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
3428 STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
3429 if (h_oversample <= STBTT_MAX_OVERSAMPLE)
3431 if (v_oversample <= STBTT_MAX_OVERSAMPLE)
3435 #define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1)
3437 static void stbtt__h_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
3439 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
3440 int safe_w = w - kernel_width;
3442 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
3443 for (j=0; j < h; ++j) {
3446 STBTT_memset(buffer, 0, kernel_width);
3451 switch (kernel_width) {
3453 for (i=0; i <= safe_w; ++i) {
3454 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3455 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3456 pixels[i] = (
unsigned char) (total / 2);
3460 for (i=0; i <= safe_w; ++i) {
3461 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3462 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3463 pixels[i] = (
unsigned char) (total / 3);
3467 for (i=0; i <= safe_w; ++i) {
3468 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3469 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3470 pixels[i] = (
unsigned char) (total / 4);
3474 for (i=0; i <= safe_w; ++i) {
3475 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3476 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3477 pixels[i] = (
unsigned char) (total / 5);
3481 for (i=0; i <= safe_w; ++i) {
3482 total += pixels[i] - buffer[i & STBTT__OVER_MASK];
3483 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
3484 pixels[i] = (
unsigned char) (total / kernel_width);
3489 for (; i < w; ++i) {
3490 STBTT_assert(pixels[i] == 0);
3491 total -= buffer[i & STBTT__OVER_MASK];
3492 pixels[i] = (
unsigned char) (total / kernel_width);
3495 pixels += stride_in_bytes;
3499 static void stbtt__v_prefilter(
unsigned char *pixels,
int w,
int h,
int stride_in_bytes,
unsigned int kernel_width)
3501 unsigned char buffer[STBTT_MAX_OVERSAMPLE];
3502 int safe_h = h - kernel_width;
3504 STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE);
3505 for (j=0; j < w; ++j) {
3508 STBTT_memset(buffer, 0, kernel_width);
3513 switch (kernel_width) {
3515 for (i=0; i <= safe_h; ++i) {
3516 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3517 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3518 pixels[i*stride_in_bytes] = (
unsigned char) (total / 2);
3522 for (i=0; i <= safe_h; ++i) {
3523 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3524 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3525 pixels[i*stride_in_bytes] = (
unsigned char) (total / 3);
3529 for (i=0; i <= safe_h; ++i) {
3530 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3531 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3532 pixels[i*stride_in_bytes] = (
unsigned char) (total / 4);
3536 for (i=0; i <= safe_h; ++i) {
3537 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3538 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3539 pixels[i*stride_in_bytes] = (
unsigned char) (total / 5);
3543 for (i=0; i <= safe_h; ++i) {
3544 total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
3545 buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
3546 pixels[i*stride_in_bytes] = (
unsigned char) (total / kernel_width);
3551 for (; i < h; ++i) {
3552 STBTT_assert(pixels[i*stride_in_bytes] == 0);
3553 total -= buffer[i & STBTT__OVER_MASK];
3554 pixels[i*stride_in_bytes] = (
unsigned char) (total / kernel_width);
3561 static float stbtt__oversample_shift(
int oversample)
3570 return (
float)-(oversample - 1) / (2.0f * (
float)oversample);
3579 for (i=0; i < num_ranges; ++i) {
3584 for (j=0; j < ranges[i].
num_chars; ++j) {
3605 int i,j,k, return_value = 1;
3612 for (i=0; i < num_ranges; ++i) {
3615 float recip_h,recip_v,sub_x,sub_y;
3622 for (j=0; j < ranges[i].
num_chars; ++j) {
3626 int advance, lsb, x0,y0,x1,y1;
3661 bc->
x0 = (stbtt_int16) r->
x;
3662 bc->
y0 = (stbtt_int16) r->
y;
3663 bc->
x1 = (stbtt_int16) (r->
x + r->
w);
3664 bc->
y1 = (stbtt_int16) (r->
y + r->
h);
3666 bc->
xoff = (float) x0 * recip_h + sub_x;
3667 bc->
yoff = (float) y0 * recip_v + sub_y;
3668 bc->
xoff2 = (x0 + r->
w) * recip_h + sub_x;
3669 bc->
yoff2 = (y0 + r->
h) * recip_v + sub_y;
3682 return return_value;
3693 int i,j,n, return_value = 1;
3698 for (i=0; i < num_ranges; ++i)
3700 ranges[i].chardata_for_range[j].x0 =
3701 ranges[i].chardata_for_range[j].y0 =
3702 ranges[i].chardata_for_range[j].x1 =
3703 ranges[i].chardata_for_range[j].y1 = 0;
3706 for (i=0; i < num_ranges; ++i)
3707 n += ranges[i].num_chars;
3723 return return_value;
3727 int first_unicode_codepoint_in_range,
int num_chars_in_range,
stbtt_packedchar *chardata_for_range)
3740 float ipw = 1.0f / pw, iph = 1.0f / ph;
3743 if (align_to_integer) {
3744 float x = (float) STBTT_ifloor((*xpos + b->
xoff) + 0.5f);
3745 float y = (float) STBTT_ifloor((*ypos + b->
yoff) + 0.5f);
3757 q->
s0 = b->
x0 * ipw;
3758 q->
t0 = b->
y0 * iph;
3759 q->
s1 = b->
x1 * ipw;
3760 q->
t1 = b->
y1 * iph;
3772 static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2, stbtt_int32 len2)
3778 stbtt_uint16 ch = s2[0]*256 + s2[1];
3780 if (i >= len1)
return -1;
3781 if (s1[i++] != ch)
return -1;
3782 }
else if (ch < 0x800) {
3783 if (i+1 >= len1)
return -1;
3784 if (s1[i++] != 0xc0 + (ch >> 6))
return -1;
3785 if (s1[i++] != 0x80 + (ch & 0x3f))
return -1;
3786 }
else if (ch >= 0xd800 && ch < 0xdc00) {
3788 stbtt_uint16 ch2 = s2[2]*256 + s2[3];
3789 if (i+3 >= len1)
return -1;
3790 c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
3791 if (s1[i++] != 0xf0 + (c >> 18))
return -1;
3792 if (s1[i++] != 0x80 + ((c >> 12) & 0x3f))
return -1;
3793 if (s1[i++] != 0x80 + ((c >> 6) & 0x3f))
return -1;
3794 if (s1[i++] != 0x80 + ((c ) & 0x3f))
return -1;
3797 }
else if (ch >= 0xdc00 && ch < 0xe000) {
3800 if (i+2 >= len1)
return -1;
3801 if (s1[i++] != 0xe0 + (ch >> 12))
return -1;
3802 if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f))
return -1;
3803 if (s1[i++] != 0x80 + ((ch ) & 0x3f))
return -1;
3811 static int stbtt_CompareUTF8toUTF16_bigendian_internal(
char *s1,
int len1,
char *s2,
int len2)
3813 return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8*) s1, len1, (stbtt_uint8*) s2, len2);
3820 stbtt_int32 i,count,stringOffset;
3821 stbtt_uint8 *fc = font->
data;
3823 stbtt_uint32 nm = stbtt__find_table(fc, offset,
"name");
3824 if (!nm)
return NULL;
3826 count = ttUSHORT(fc+nm+2);
3827 stringOffset = nm + ttUSHORT(fc+nm+4);
3828 for (i=0; i < count; ++i) {
3829 stbtt_uint32 loc = nm + 6 + 12 * i;
3830 if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2)
3831 && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) {
3832 *length = ttUSHORT(fc+loc+8);
3833 return (
const char *) (fc+stringOffset+ttUSHORT(fc+loc+10));
3839 static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)
3842 stbtt_int32 count = ttUSHORT(fc+nm+2);
3843 stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4);
3845 for (i=0; i < count; ++i) {
3846 stbtt_uint32 loc = nm + 6 + 12 * i;
3847 stbtt_int32
id = ttUSHORT(fc+loc+6);
3848 if (
id == target_id) {
3850 stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4);
3853 if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
3854 stbtt_int32 slen = ttUSHORT(fc+loc+8);
3855 stbtt_int32 off = ttUSHORT(fc+loc+10);
3858 stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);
3859 if (matchlen >= 0) {
3861 if (i+1 < count && ttUSHORT(fc+loc+12+6) == next_id && ttUSHORT(fc+loc+12) == platform && ttUSHORT(fc+loc+12+2) == encoding && ttUSHORT(fc+loc+12+4) == language) {
3862 slen = ttUSHORT(fc+loc+12+8);
3863 off = ttUSHORT(fc+loc+12+10);
3865 if (matchlen == nlen)
3867 }
else if (matchlen < nlen && name[matchlen] ==
' ') {
3869 if (stbtt_CompareUTF8toUTF16_bigendian_internal((
char*) (name+matchlen), nlen-matchlen, (
char*)(fc+stringOffset+off),slen))
3874 if (matchlen == nlen)
3886 static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)
3888 stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((
char *) name);
3890 if (!stbtt__isfont(fc+offset))
return 0;
3894 hd = stbtt__find_table(fc, offset,
"head");
3895 if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7))
return 0;
3898 nm = stbtt__find_table(fc, offset,
"name");
3903 if (stbtt__matchpair(fc, nm, name, nlen, 16, -1))
return 1;
3904 if (stbtt__matchpair(fc, nm, name, nlen, 1, -1))
return 1;
3905 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
return 1;
3907 if (stbtt__matchpair(fc, nm, name, nlen, 16, 17))
return 1;
3908 if (stbtt__matchpair(fc, nm, name, nlen, 1, 2))
return 1;
3909 if (stbtt__matchpair(fc, nm, name, nlen, 3, -1))
return 1;
3915 static int stbtt_FindMatchingFont_internal(
unsigned char *font_collection,
char *name_utf8, stbtt_int32 flags)
3920 if (off < 0)
return off;
3921 if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags))
3926 #if defined(__GNUC__) || defined(__clang__)
3927 #pragma GCC diagnostic push
3928 #pragma GCC diagnostic ignored "-Wcast-qual"
3932 float pixel_height,
unsigned char *pixels,
int pw,
int ph,
3935 return stbtt_BakeFontBitmap_internal((
unsigned char *) data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, chardata);
3940 return stbtt_GetFontOffsetForIndex_internal((
unsigned char *) data, index);
3945 return stbtt_GetNumberOfFonts_internal((
unsigned char *) data);
3950 return stbtt_InitFont_internal(info, (
unsigned char *) data, offset);
3955 return stbtt_FindMatchingFont_internal((
unsigned char *) fontdata, (
char *) name, flags);
3960 return stbtt_CompareUTF8toUTF16_bigendian_internal((
char *) s1, len1, (
char *) s2, len2);
3963 #if defined(__GNUC__) || defined(__clang__)
3964 #pragma GCC diagnostic pop
3967 #endif // STB_TRUETYPE_IMPLEMENTATION