Merge branch 'cff-type2' of https://github.com/dougallj/stb into type2
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Conflicts: stb_truetype.h
authored by Sean Barrett · committed by Sean Barrett · parents d5ec778, 0181b37 · Browse files · 1 file·+711 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+711 added−11 removed
- other:+711 added−11 removed
Provenance
- Commit
ad6614a- Tree
6a52d03- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent 1
d5ec778- Parent 2
0181b37
1 file changed+711 added−11 removed
modified:stb_truetype.h+711 added-11 removed
451 unmodified lines452 #ifdef __cplusplus453 extern "C" {454 #endif455 456+// private structure457+typedef struct458+{459+ stbtt_uint8 *data;460+ int cursor;461+ int size;462+} stbtt__buf;463+464 //////////////////////////////////////////////////////////////////////////////465 //466 // TEXTURE BAKING API467 //182 unmodified lines650 651 int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf652 int index_map; // a cmap mapping for our chosen character encoding653 int indexToLocFormat; // format needed to map from glyph index to glyph654+655+ stbtt__buf cff; // cff font data656+ stbtt__buf charstrings; // the charstring index657+ stbtt__buf gsubrs; // global charstring subroutines index658+ stbtt__buf subrs; // private charstring subroutines index659+ stbtt__buf fontdicts; // array of font dicts660+ stbtt__buf fdselect; // map from glyph to fontdict661 };662 663 STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);664 // Given an offset into the file that defines a font, this function builds69 unmodified lines734 #ifndef STBTT_vmove // you can predefine these to use different values (but why?)735 enum {736 STBTT_vmove=1,737 STBTT_vline,723- STBTT_vcurve738+ STBTT_vcurve,739+ STBTT_vcubic740 };741 #endif742 743 #ifndef stbtt_vertex // you can predefine this to use different values744 // (we share this with other code at RAD)745 #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file746 typedef struct747 {732- stbtt_vertex_type x,y,cx,cy;748+ stbtt_vertex_type x,y,cx,cy,cx1,cy1;749 unsigned char type,padding;750 } stbtt_vertex;751 #endif752 215 unmodified lines968 #endif969 970 //////////////////////////////////////////////////////////////////////////971 //972+// stbtt__buf helpers to parse data from file973+//974+975+static stbtt_uint8 stbtt__buf_get8(stbtt__buf *b)976+{977+ if (b->cursor >= b->size)978+ return 0;979+ return b->data[b->cursor++];980+}981+982+static stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b)983+{984+ if (b->cursor >= b->size)985+ return 0;986+ return b->data[b->cursor];987+}988+989+static void stbtt__buf_seek(stbtt__buf *b, int o)990+{991+ STBTT_assert(!(o > b->size || o < 0));992+ b->cursor = (o > b->size || o < 0) ? b->size : o;993+}994+995+static void stbtt__buf_skip(stbtt__buf *b, int o)996+{997+ stbtt__buf_seek(b, b->cursor + o);998+}999+1000+static stbtt_uint32 stbtt__buf_get(stbtt__buf *b, int n)1001+{1002+ stbtt_uint32 v = 0;1003+ int i;1004+ STBTT_assert(n >= 1 && n <= 4);1005+ for (i = 0; i < n; i++)1006+ v = (v << 8) | stbtt__buf_get8(b);1007+ return v;1008+}1009+1010+static stbtt__buf stbtt__new_buf(const void *p, size_t size)1011+{1012+ stbtt__buf r;1013+ STBTT_assert(size < 0x40000000);1014+ r.data = (stbtt_uint8*) p;1015+ r.size = (int) size;1016+ r.cursor = 0;1017+ return r;1018+}1019+1020+#define stbtt__buf_get16(b) stbtt__buf_get((b), 2)1021+#define stbtt__buf_get32(b) stbtt__buf_get((b), 4)1022+1023+static stbtt__buf stbtt__buf_range(const stbtt__buf *b, int o, int s)1024+{1025+ stbtt__buf r = stbtt__new_buf(NULL, 0);1026+ if (o < 0 || s < 0 || o > b->size || s > b->size - o) return r;1027+ r.data = b->data + o;1028+ r.size = s;1029+ return r;1030+}1031+1032+static stbtt__buf stbtt__cff_get_index(stbtt__buf *b)1033+{1034+ int count, start, offsize;1035+ start = b->cursor;1036+ count = stbtt__buf_get16(b);1037+ if (count) {1038+ offsize = stbtt__buf_get8(b);1039+ STBTT_assert(offsize >= 1 && offsize <= 4);1040+ stbtt__buf_skip(b, offsize * count);1041+ stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);1042+ }1043+ return stbtt__buf_range(b, start, b->cursor - start);1044+}1045+1046+static stbtt_uint32 stbtt__cff_int(stbtt__buf *b)1047+{1048+ int b0 = stbtt__buf_get8(b);1049+ if (b0 >= 32 && b0 <= 246) return b0 - 139;1050+ else if (b0 >= 247 && b0 <= 250) return (b0 - 247)*256 + stbtt__buf_get8(b) + 108;1051+ else if (b0 >= 251 && b0 <= 254) return -(b0 - 251)*256 - stbtt__buf_get8(b) - 108;1052+ else if (b0 == 28) return stbtt__buf_get16(b);1053+ else if (b0 == 29) return stbtt__buf_get32(b);1054+ STBTT_assert(0);1055+ return 0;1056+}1057+1058+static void stbtt__cff_skip_operand(stbtt__buf *b) {1059+ int v, b0 = stbtt__buf_peek8(b);1060+ STBTT_assert(b0 >= 28);1061+ if (b0 == 30) {1062+ stbtt__buf_skip(b, 1);1063+ while (b->cursor < b->size) {1064+ v = stbtt__buf_get8(b);1065+ if ((v & 0xF) == 0xF || (v >> 4) == 0xF)1066+ break;1067+ }1068+ } else {1069+ stbtt__cff_int(b);1070+ }1071+}1072+1073+static stbtt__buf stbtt__dict_get(stbtt__buf *b, int key)1074+{1075+ stbtt__buf_seek(b, 0);1076+ while (b->cursor < b->size) {1077+ int start = b->cursor, end, op;1078+ while (stbtt__buf_peek8(b) >= 28)1079+ stbtt__cff_skip_operand(b);1080+ end = b->cursor;1081+ op = stbtt__buf_get8(b);1082+ if (op == 12) op = stbtt__buf_get8(b) | 0x100;1083+ if (op == key) return stbtt__buf_range(b, start, end-start);1084+ }1085+ return stbtt__buf_range(b, 0, 0);1086+}1087+1088+static void stbtt__dict_get_ints(stbtt__buf *b, int key, int outcount, stbtt_uint32 *out)1089+{1090+ int i;1091+ stbtt__buf operands = stbtt__dict_get(b, key);1092+ for (i = 0; i < outcount && operands.cursor < operands.size; i++)1093+ out[i] = stbtt__cff_int(&operands);1094+}1095+1096+static int stbtt__cff_index_count(stbtt__buf *b)1097+{1098+ stbtt__buf_seek(b, 0);1099+ return stbtt__buf_get16(b);1100+}1101+1102+static stbtt__buf stbtt__cff_index_get(stbtt__buf b, int i)1103+{1104+ int count, offsize, start, end;1105+ stbtt__buf_seek(&b, 0);1106+ count = stbtt__buf_get16(&b);1107+ offsize = stbtt__buf_get8(&b);1108+ STBTT_assert(i >= 0 && i < count);1109+ STBTT_assert(offsize >= 1 && offsize <= 4);1110+ stbtt__buf_skip(&b, i*offsize);1111+ start = stbtt__buf_get(&b, offsize);1112+ end = stbtt__buf_get(&b, offsize);1113+ return stbtt__buf_range(&b, 2+(count+1)*offsize+start, end - start);1114+}1115+1116+//////////////////////////////////////////////////////////////////////////1117+//1118 // accessors to parse data from file1119 //1120 1121 // on platforms that don't allow misaligned reads, if we want to allow53 unmodified lines1175 }1176 return -1;1177 }1178 1179+static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict)1180+{1181+ stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 };1182+ stbtt__buf pdict;1183+ stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);1184+ if (!private_loc[1] || !private_loc[0]) return stbtt__new_buf(NULL, 0);1185+ pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);1186+ stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);1187+ if (!subrsoff) return stbtt__new_buf(NULL, 0);1188+ stbtt__buf_seek(&cff, private_loc[1]+subrsoff);1189+ return stbtt__cff_get_index(&cff);1190+}1191+1192 static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart)1193 {1194 stbtt_uint32 cmap, t;1195 stbtt_int32 i,numTables;1196 1197 info->data = data;1198 info->fontstart = fontstart;1199+ info->cff = stbtt__new_buf(NULL, 0);1200 1201 cmap = stbtt__find_table(data, fontstart, "cmap"); // required1202 info->loca = stbtt__find_table(data, fontstart, "loca"); // required1203 info->head = stbtt__find_table(data, fontstart, "head"); // required1204 info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required1205 info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required1206 info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required1207 info->kern = stbtt__find_table(data, fontstart, "kern"); // not required1208+1032- if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)1209+ if (!cmap || !info->head || !info->hhea || !info->hmtx)1210 return 0;1211+ if (info->glyf) {1212+ // required for truetype1213+ if (!info->loca) return 0;1214+ } else {1215+ // initialization for CFF / Type2 fonts (OTF)1216+ stbtt__buf b, topdict, topdictidx;1217+ stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;1218+ stbtt_uint32 cff;1219 1220+ cff = stbtt__find_table(data, fontstart, "CFF ");1221+ if (!cff) return 0;1222+1223+ info->fontdicts = stbtt__new_buf(NULL, 0);1224+ info->fdselect = stbtt__new_buf(NULL, 0);1225+1226+ // @TODO this should use size from table (not 512MB)1227+ info->cff = stbtt__new_buf(data+cff, 512*1024*1024);1228+ b = info->cff;1229+1230+ // read the header1231+ stbtt__buf_skip(&b, 2);1232+ stbtt__buf_seek(&b, stbtt__buf_get8(&b)); // hdrsize1233+1234+ // @TODO the name INDEX could list multiple fonts,1235+ // but we just use the first one.1236+ stbtt__cff_get_index(&b); // name INDEX1237+ topdictidx = stbtt__cff_get_index(&b);1238+ topdict = stbtt__cff_index_get(topdictidx, 0);1239+ stbtt__cff_get_index(&b); // string INDEX1240+ info->gsubrs = stbtt__cff_get_index(&b);1241+1242+ stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);1243+ stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);1244+ stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);1245+ stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);1246+ info->subrs = stbtt__get_subrs(b, topdict);1247+1248+ // we only support Type 2 charstrings1249+ if (cstype != 2) return 0;1250+ if (charstrings == 0) return 0;1251+1252+ if (fdarrayoff) {1253+ // looks like a CID font1254+ if (!fdselectoff) return 0;1255+ stbtt__buf_seek(&b, fdarrayoff);1256+ info->fontdicts = stbtt__cff_get_index(&b);1257+ info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size-fdselectoff);1258+ }1259+1260+ stbtt__buf_seek(&b, charstrings);1261+ info->charstrings = stbtt__cff_get_index(&b);1262+ }1263+1264 t = stbtt__find_table(data, fontstart, "maxp");1265 if (t)1266 info->numGlyphs = ttUSHORT(data+t+4);1267 else141 unmodified lines1409 static int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index)1410 {1411 int g1,g2;1412 1413+ STBTT_assert(!info->cff.size);1414+1415 if (glyph_index >= info->numGlyphs) return -1; // glyph index out of range1416 if (info->indexToLocFormat >= 2) return -1; // unknown index->glyph map format1417 1418 if (info->indexToLocFormat == 0) {6 unmodified lines1425 1426 return g1==g2 ? -1 : g1; // if length is 0, return -11427 }1428 1429+static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);1430+1431 STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)1432 {1433+ if (info->cff.size) {1434+ stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);1435+ } else {1200- int g = stbtt__GetGlyfOffset(info, glyph_index);1201- if (g < 0) return 0;1436+ int g = stbtt__GetGlyfOffset(info, glyph_index);1437+ if (g < 0) return 0;1438 1203- if (x0) *x0 = ttSHORT(info->data + g + 2);1204- if (y0) *y0 = ttSHORT(info->data + g + 4);1205- if (x1) *x1 = ttSHORT(info->data + g + 6);1206- if (y1) *y1 = ttSHORT(info->data + g + 8);1439+ if (x0) *x0 = ttSHORT(info->data + g + 2);1440+ if (y0) *y0 = ttSHORT(info->data + g + 4);1441+ if (x1) *x1 = ttSHORT(info->data + g + 6);1442+ if (y1) *y1 = ttSHORT(info->data + g + 8);1443+ }1444 return 1;1445 }1446 1447 STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)3 unmodified lines1451 1452 STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)1453 {1454 stbtt_int16 numberOfContours;1455+ int g;1456+ if (info->cff.size)1457+ return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0;1218- int g = stbtt__GetGlyfOffset(info, glyph_index);1458+ g = stbtt__GetGlyfOffset(info, glyph_index);1459 if (g < 0) return 1;1460 numberOfContours = ttSHORT(info->data + g);1461 return numberOfContours == 0;1462 }13 unmodified lines1476 }1477 return num_vertices;1478 }1479 1240-STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)1480+static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)1481 {1482 stbtt_int16 numberOfContours;1483 stbtt_uint8 *endPtsOfContours;1484 stbtt_uint8 *data = info->data;217 unmodified lines1702 *pvertices = vertices;1703 return num_vertices;1704 }1705 1706+typedef struct1707+{1708+ int bounds;1709+ int started;1710+ float first_x, first_y;1711+ float x, y;1712+ stbtt_int32 min_x, max_x, min_y, max_y;1713+1714+ stbtt_vertex *pvertices;1715+ int num_vertices;1716+} stbtt__csctx;1717+1718+#define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0}1719+1720+static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y)1721+{1722+ if (x > c->max_x || !c->started) c->max_x = x;1723+ if (y > c->max_y || !c->started) c->max_y = y;1724+ if (x < c->min_x || !c->started) c->min_x = x;1725+ if (y < c->min_y || !c->started) c->min_y = y;1726+ c->started = 1;1727+}1728+1729+static void stbtt__cstx_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)1730+{1731+ if (c->bounds) {1732+ stbtt__track_vertex(c, x, y);1733+ if (type == STBTT_vcubic) {1734+ stbtt__track_vertex(c, cx, cy);1735+ stbtt__track_vertex(c, cx1, cy1);1736+ }1737+ } else {1738+ stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);1739+ c->pvertices[c->num_vertices].cx1 = (stbtt_int16) cx1;1740+ c->pvertices[c->num_vertices].cy1 = (stbtt_int16) cy1;1741+ }1742+ c->num_vertices++;1743+}1744+1745+static void stbtt__csctx_close_shape(stbtt__csctx *ctx)1746+{1747+ if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)1748+ stbtt__cstx_v(ctx, STBTT_vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0);1749+}1750+1751+static void stbtt__csctx_rmove_to(stbtt__csctx *ctx, float dx, float dy)1752+{1753+ stbtt__csctx_close_shape(ctx);1754+ ctx->first_x = ctx->x = ctx->x + dx;1755+ ctx->first_y = ctx->y = ctx->y + dy;1756+ stbtt__cstx_v(ctx, STBTT_vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);1757+}1758+1759+static void stbtt__csctx_rline_to(stbtt__csctx *ctx, float dx, float dy)1760+{1761+ ctx->x += dx;1762+ ctx->y += dy;1763+ stbtt__cstx_v(ctx, STBTT_vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);1764+}1765+1766+static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3)1767+{1768+ float cx1 = ctx->x + dx1;1769+ float cy1 = ctx->y + dy1;1770+ float cx2 = cx1 + dx2;1771+ float cy2 = cy1 + dy2;1772+ ctx->x = cx2 + dx3;1773+ ctx->y = cy2 + dy3;1774+ stbtt__cstx_v(ctx, STBTT_vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2);1775+}1776+1777+static stbtt__buf stbtt__get_subr(stbtt__buf idx, int n)1778+{1779+ int count = stbtt__cff_index_count(&idx);1780+ int bias = 107;1781+ if (count >= 33900)1782+ bias = 32768;1783+ else if (count >= 1240)1784+ bias = 1131;1785+ n += bias;1786+ if (n < 0 || n >= count)1787+ return stbtt__new_buf(NULL, 0);1788+ return stbtt__cff_index_get(idx, n);1789+}1790+1791+static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo *info, int glyph_index)1792+{1793+ stbtt__buf fdselect = info->fdselect;1794+ int nranges, start, end, v, fmt, fdselector = -1, i;1795+1796+ stbtt__buf_seek(&fdselect, 0);1797+ fmt = stbtt__buf_get8(&fdselect);1798+ if (fmt == 0) {1799+ // untested1800+ stbtt__buf_skip(&fdselect, glyph_index);1801+ fdselector = stbtt__buf_get8(&fdselect);1802+ } else if (fmt == 3) {1803+ nranges = stbtt__buf_get16(&fdselect);1804+ start = stbtt__buf_get16(&fdselect);1805+ for (i = 0; i < nranges; i++) {1806+ v = stbtt__buf_get8(&fdselect);1807+ end = stbtt__buf_get16(&fdselect);1808+ if (glyph_index >= start && glyph_index < end) {1809+ fdselector = v;1810+ break;1811+ }1812+ start = end;1813+ }1814+ }1815+ if (fdselector == -1) stbtt__new_buf(NULL, 0);1816+ return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));1817+}1818+1819+static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, stbtt__csctx *c)1820+{1821+ int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;1822+ int has_subrs = 0, clear_stack;1823+ float s[48];1824+ stbtt__buf subr_stack[10], subrs = info->subrs, b;1825+ float f;1826+1827+#define STBTT__CSERR(s) (0)1828+1829+ // this currently ignores the initial width value, which isn't needed if we have hmtx1830+ b = stbtt__cff_index_get(info->charstrings, glyph_index);1831+ while (b.cursor < b.size) {1832+ i = 0;1833+ clear_stack = 1;1834+ b0 = stbtt__buf_get8(&b);1835+ switch (b0) {1836+ // @TODO implement hinting1837+ case 0x13: // hintmask1838+ case 0x14: // cntrmask1839+ if (in_header)1840+ maskbits += (sp / 2); // implicit "vstem"1841+ in_header = 0;1842+ stbtt__buf_skip(&b, (maskbits + 7) / 8);1843+ break;1844+1845+ case 0x01: // hstem1846+ case 0x03: // vstem1847+ case 0x12: // hstemhm1848+ case 0x17: // vstemhm1849+ maskbits += (sp / 2);1850+ break;1851+1852+ case 0x15: // rmoveto1853+ in_header = 0;1854+ if (sp < 2) return STBTT__CSERR("rmoveto stack");1855+ stbtt__csctx_rmove_to(c, s[sp-2], s[sp-1]);1856+ break;1857+ case 0x04: // vmoveto1858+ in_header = 0;1859+ if (sp < 1) return STBTT__CSERR("vmoveto stack");1860+ stbtt__csctx_rmove_to(c, 0, s[sp-1]);1861+ break;1862+ case 0x16: // hmoveto1863+ in_header = 0;1864+ if (sp < 1) return STBTT__CSERR("hmoveto stack");1865+ stbtt__csctx_rmove_to(c, s[sp-1], 0);1866+ break;1867+1868+ case 0x05: // rlineto1869+ if (sp < 2) return STBTT__CSERR("rlineto stack");1870+ for (; i + 1 < sp; i += 2)1871+ stbtt__csctx_rline_to(c, s[i], s[i+1]);1872+ break;1873+1874+ // hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical1875+ // starting from a different place.1876+1877+ case 0x07: // vlineto1878+ if (sp < 1) return STBTT__CSERR("vlineto stack");1879+ goto vlineto;1880+ case 0x06: // hlineto1881+ if (sp < 1) return STBTT__CSERR("hlineto stack");1882+ for (;;) {1883+ if (i >= sp) break;1884+ stbtt__csctx_rline_to(c, s[i], 0);1885+ i++;1886+ vlineto:1887+ if (i >= sp) break;1888+ stbtt__csctx_rline_to(c, 0, s[i]);1889+ i++;1890+ }1891+ break;1892+1893+ case 0x1F: // hvcurveto1894+ if (sp < 4) return STBTT__CSERR("hvcurveto stack");1895+ goto hvcurveto;1896+ case 0x1E: // vhcurveto1897+ if (sp < 4) return STBTT__CSERR("vhcurveto stack");1898+ for (;;) {1899+ if (i + 3 >= sp) break;1900+ 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);1901+ i += 4;1902+ hvcurveto:1903+ if (i + 3 >= sp) break;1904+ 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]);1905+ i += 4;1906+ }1907+ break;1908+1909+ case 0x08: // rrcurveto1910+ if (sp < 6) return STBTT__CSERR("rcurveline stack");1911+ for (; i + 5 < sp; i += 6)1912+ stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);1913+ break;1914+1915+ case 0x18: // rcurveline1916+ if (sp < 8) return STBTT__CSERR("rcurveline stack");1917+ for (; i + 5 < sp - 2; i += 6)1918+ stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);1919+ if (i + 1 >= sp) return STBTT__CSERR("rcurveline stack");1920+ stbtt__csctx_rline_to(c, s[i], s[i+1]);1921+ break;1922+1923+ case 0x19: // rlinecurve1924+ if (sp < 8) return STBTT__CSERR("rlinecurve stack");1925+ for (; i + 1 < sp - 6; i += 2)1926+ stbtt__csctx_rline_to(c, s[i], s[i+1]);1927+ if (i + 5 >= sp) return STBTT__CSERR("rlinecurve stack");1928+ stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);1929+ break;1930+1931+ case 0x1A: // vvcurveto1932+ case 0x1B: // hhcurveto1933+ if (sp < 4) return STBTT__CSERR("(vv|hh)curveto stack");1934+ f = 0.0;1935+ if (sp & 1) { f = s[i]; i++; }1936+ for (; i + 3 < sp; i += 4) {1937+ if (b0 == 0x1B)1938+ stbtt__csctx_rccurve_to(c, s[i], f, s[i+1], s[i+2], s[i+3], 0.0);1939+ else1940+ stbtt__csctx_rccurve_to(c, f, s[i], s[i+1], s[i+2], 0.0, s[i+3]);1941+ f = 0.0;1942+ }1943+ break;1944+1945+ case 0x0A: // callsubr1946+ if (!has_subrs) {1947+ if (info->fdselect.size)1948+ subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);1949+ has_subrs = 1;1950+ }1951+ // fallthrough1952+ case 0x1D: // callgsubr1953+ if (sp < 1) return STBTT__CSERR("call(g|)subr stack");1954+ v = (int) s[--sp];1955+ if (subr_stack_height >= 10) return STBTT__CSERR("recursion limit");1956+ subr_stack[subr_stack_height++] = b;1957+ b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v);1958+ if (b.size == 0) return STBTT__CSERR("subr not found");1959+ b.cursor = 0;1960+ clear_stack = 0;1961+ break;1962+1963+ case 0x0B: // return1964+ if (subr_stack_height <= 0) return STBTT__CSERR("return outside subr");1965+ b = subr_stack[--subr_stack_height];1966+ clear_stack = 0;1967+ break;1968+1969+ case 0x0E: // endchar1970+ stbtt__csctx_close_shape(c);1971+ return 1;1972+1973+ case 0x0C: { // two-byte escape1974+ float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;1975+ float dx, dy;1976+ int b1 = stbtt__buf_get8(&b);1977+ switch (b1) {1978+ // @TODO These "flex" implementations ignore the flex-depth and resolution,1979+ // and always draw beziers.1980+ case 0x22: // hflex1981+ if (sp < 7) return STBTT__CSERR("hflex stack");1982+ dx1 = s[0];1983+ dx2 = s[1];1984+ dy2 = s[2];1985+ dx3 = s[3];1986+ dx4 = s[4];1987+ dx5 = s[5];1988+ dx6 = s[6];1989+ stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);1990+ stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);1991+ break;1992+1993+ case 0x23: // flex1994+ if (sp < 13) return STBTT__CSERR("flex stack");1995+ dx1 = s[0];1996+ dy1 = s[1];1997+ dx2 = s[2];1998+ dy2 = s[3];1999+ dx3 = s[4];2000+ dy3 = s[5];2001+ dx4 = s[6];2002+ dy4 = s[7];2003+ dx5 = s[8];2004+ dy5 = s[9];2005+ dx6 = s[10];2006+ dy6 = s[11];2007+ //fd is s[12]2008+ stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);2009+ stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);2010+ break;2011+2012+ case 0x24: // hflex12013+ if (sp < 9) return STBTT__CSERR("hflex1 stack");2014+ dx1 = s[0];2015+ dy1 = s[1];2016+ dx2 = s[2];2017+ dy2 = s[3];2018+ dx3 = s[4];2019+ dx4 = s[5];2020+ dx5 = s[6];2021+ dy5 = s[7];2022+ dx6 = s[8];2023+ stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);2024+ stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1+dy2+dy5));2025+ break;2026+2027+ case 0x25: // flex12028+ if (sp < 11) return STBTT__CSERR("flex1 stack");2029+ dx1 = s[0];2030+ dy1 = s[1];2031+ dx2 = s[2];2032+ dy2 = s[3];2033+ dx3 = s[4];2034+ dy3 = s[5];2035+ dx4 = s[6];2036+ dy4 = s[7];2037+ dx5 = s[8];2038+ dy5 = s[9];2039+ dx6 = dy6 = s[10];2040+ dx = dx1+dx2+dx3+dx4+dx5;2041+ dy = dy1+dy2+dy3+dy4+dy5;2042+ if (STBTT_fabs(dx) > STBTT_fabs(dy))2043+ dy6 = -dy;2044+ else2045+ dx6 = -dx;2046+ stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);2047+ stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);2048+ break;2049+2050+ default:2051+ return STBTT__CSERR("unimplemented");2052+ }2053+ } break;2054+2055+ default:2056+ if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254))2057+ return STBTT__CSERR("reserved operator");2058+2059+ // push immediate2060+ if (b0 == 255) {2061+ f = (float)stbtt__buf_get32(&b) / 0x10000;2062+ } else {2063+ stbtt__buf_skip(&b, -1);2064+ f = (float)(stbtt_int16)stbtt__cff_int(&b);2065+ }2066+ if (sp >= 48) return STBTT__CSERR("push stack overflow");2067+ s[sp++] = f;2068+ clear_stack = 0;2069+ break;2070+ }2071+ if (clear_stack) sp = 0;2072+ }2073+ return STBTT__CSERR("no endchar");2074+2075+#undef STBTT__CSERR2076+}2077+2078+static int stbtt__GetGlyphShapeT2(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)2079+{2080+ // runs the charstring twice, once to count and once to output (to avoid realloc)2081+ stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);2082+ stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);2083+ if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {2084+ *pvertices = (stbtt_vertex*)STBTT_malloc(count_ctx.num_vertices*sizeof(stbtt_vertex), info->userdata);2085+ output_ctx.pvertices = *pvertices;2086+ if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {2087+ STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);2088+ return output_ctx.num_vertices;2089+ }2090+ }2091+ *pvertices = NULL;2092+ return 0;2093+}2094+2095+static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)2096+{2097+ stbtt__csctx c = STBTT__CSCTX_INIT(1);2098+ int r = stbtt__run_charstring(info, glyph_index, &c);2099+ if (x0) {2100+ *x0 = r ? c.min_x : 0;2101+ *y0 = r ? c.min_y : 0;2102+ *x1 = r ? c.max_x : 0;2103+ *y1 = r ? c.max_y : 0;2104+ }2105+ return r ? c.num_vertices : 0;2106+}2107+2108+STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)2109+{2110+ if (!info->cff.size)2111+ return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);2112+ else2113+ return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);2114+}2115+2116 STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)2117 {2118 stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);2119 if (glyph_index < numOfLongHorMetrics) {862 unmodified lines2982 }2983 return 1;2984 }2985 2986+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)2987+{2988+ // @TODO this "flatness" calculation is just made-up nonsense that seems to work well enough2989+ float dx0 = x1-x0;2990+ float dy0 = y1-y0;2991+ float dx1 = x2-x1;2992+ float dy1 = y2-y1;2993+ float dx2 = x3-x2;2994+ float dy2 = y3-y2;2995+ float dx = x3-x0;2996+ float dy = y3-y0;2997+ float longlen = (float) (STBTT_sqrt(dx0*dx0+dy0*dy0)+STBTT_sqrt(dx1*dx1+dy1*dy1)+STBTT_sqrt(dx2*dx2+dy2*dy2));2998+ float shortlen = (float) STBTT_sqrt(dx*dx+dy*dy);2999+ float flatness_squared = longlen*longlen-shortlen*shortlen;3000+3001+ if (n > 16) // 65536 segments on one curve better be enough!3002+ return;3003+3004+ if (flatness_squared > objspace_flatness_squared) {3005+ float x01 = (x0+x1)/2;3006+ float y01 = (y0+y1)/2;3007+ float x12 = (x1+x2)/2;3008+ float y12 = (y1+y2)/2;3009+ float x23 = (x2+x3)/2;3010+ float y23 = (y2+y3)/2;3011+3012+ float xa = (x01+x12)/2;3013+ float ya = (y01+y12)/2;3014+ float xb = (x12+x23)/2;3015+ float yb = (y12+y23)/2;3016+3017+ float mx = (xa+xb)/2;3018+ float my = (ya+yb)/2;3019+3020+ stbtt__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my, objspace_flatness_squared,n+1);3021+ stbtt__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3, objspace_flatness_squared,n+1);3022+ } else {3023+ stbtt__add_point(points, *num_points,x3,y3);3024+ *num_points = *num_points+1;3025+ }3026+}3027+3028 // returns number of contours3029 static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)3030 {3031 stbtt__point *points=0;48 unmodified lines3080 vertices[i].x, vertices[i].y,3081 objspace_flatness_squared, 0);3082 x = vertices[i].x, y = vertices[i].y;3083 break;3084+ case STBTT_vcubic:3085+ stbtt__tesselate_cubic(points, &num_points, x,y,3086+ vertices[i].cx, vertices[i].cy,3087+ vertices[i].cx1, vertices[i].cy1,3088+ vertices[i].x, vertices[i].y,3089+ objspace_flatness_squared, 0);3090+ x = vertices[i].x, y = vertices[i].y;3091+ break;3092 }3093 }3094 (*contour_lengths)[n] = num_points - start;3095 }892 unmodified lines