Skip to content
Myr

CFF and Type 2 charstream parsing in stb_truetype

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This is a partial implementation of the CFF and Type 2 charstring specifications. It allows stb_truetype to read most OTF files.

authored by Dougall Johnson · committed by Dougall Johnson · parent e713a69 · 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

1 file+711 added−11 removed

modified:stb_truetype.h+711 added-11 removed
456 unmodified lines
457#ifdef __cplusplus458extern "C" {459#endif460461// private structure462typedef struct463{464 stbtt_uint8 *data;465 int cursor;466 int size;467} stbtt__buf;468469//////////////////////////////////////////////////////////////////////////////470//471// TEXTURE BAKING API472//
182 unmodified lines
655656 int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf657 int index_map; // a cmap mapping for our chosen character encoding658 int indexToLocFormat; // format needed to map from glyph index to glyph659660 stbtt__buf cff; // cff font data661 stbtt__buf charstrings; // the charstring index662 stbtt__buf gsubrs; // global charstring subroutines index663 stbtt__buf subrs; // private charstring subroutines index664 stbtt__buf fontdicts; // array of font dicts665 stbtt__buf fdselect; // map from glyph to fontdict666};667668STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);669// Given an offset into the file that defines a font, this function builds
69 unmodified lines
739#ifndef STBTT_vmove // you can predefine these to use different values (but why?)740 enum {741 STBTT_vmove=1,742 STBTT_vline,728 STBTT_vcurve743 STBTT_vcurve,744 STBTT_vcubic745 };746#endif747748#ifndef stbtt_vertex // you can predefine this to use different values749 // (we share this with other code at RAD)750 #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file751 typedef struct752 {737 stbtt_vertex_type x,y,cx,cy;753 stbtt_vertex_type x,y,cx,cy,cx1,cy1;754 unsigned char type,padding;755 } stbtt_vertex;756#endif757
215 unmodified lines
973#endif974975//////////////////////////////////////////////////////////////////////////976//977// stbtt__buf helpers to parse data from file978//979980static stbtt_uint8 stbtt__buf_get8(stbtt__buf *b)981{982 if (b->cursor >= b->size)983 return 0;984 return b->data[b->cursor++];985}986987static stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b)988{989 if (b->cursor >= b->size)990 return 0;991 return b->data[b->cursor];992}993994static void stbtt__buf_seek(stbtt__buf *b, int o)995{996 STBTT_assert(!(o > b->size || o < 0));997 b->cursor = (o > b->size || o < 0) ? b->size : o;998}9991000static void stbtt__buf_skip(stbtt__buf *b, int o)1001{1002 stbtt__buf_seek(b, b->cursor + o);1003}10041005static stbtt_uint32 stbtt__buf_get(stbtt__buf *b, int n)1006{1007 stbtt_uint32 v = 0;1008 int i;1009 STBTT_assert(n >= 1 && n <= 4);1010 for (i = 0; i < n; i++)1011 v = (v << 8) | stbtt__buf_get8(b);1012 return v;1013}10141015static stbtt__buf stbtt__new_buf(const void *p, size_t size)1016{1017 stbtt__buf r;1018 STBTT_assert(size < 0x40000000);1019 r.data = (stbtt_uint8*) p;1020 r.size = (int) size;1021 r.cursor = 0;1022 return r;1023}10241025#define stbtt__buf_get16(b) stbtt__buf_get((b), 2)1026#define stbtt__buf_get32(b) stbtt__buf_get((b), 4)10271028static stbtt__buf stbtt__buf_range(const stbtt__buf *b, int o, int s)1029{1030 stbtt__buf r = stbtt__new_buf(NULL, 0);1031 if (o < 0 || s < 0 || o > b->size || s > b->size - o) return r;1032 r.data = b->data + o;1033 r.size = s;1034 return r;1035}10361037static stbtt__buf stbtt__cff_get_index(stbtt__buf *b)1038{1039 int count, start, offsize;1040 start = b->cursor;1041 count = stbtt__buf_get16(b);1042 if (count) {1043 offsize = stbtt__buf_get8(b);1044 STBTT_assert(offsize >= 1 && offsize <= 4);1045 stbtt__buf_skip(b, offsize * count);1046 stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);1047 }1048 return stbtt__buf_range(b, start, b->cursor - start);1049}10501051static stbtt_uint32 stbtt__cff_int(stbtt__buf *b)1052{1053 int b0 = stbtt__buf_get8(b);1054 if (b0 >= 32 && b0 <= 246) return b0 - 139;1055 else if (b0 >= 247 && b0 <= 250) return (b0 - 247)*256 + stbtt__buf_get8(b) + 108;1056 else if (b0 >= 251 && b0 <= 254) return -(b0 - 251)*256 - stbtt__buf_get8(b) - 108;1057 else if (b0 == 28) return stbtt__buf_get16(b);1058 else if (b0 == 29) return stbtt__buf_get32(b);1059 STBTT_assert(0);1060 return 0;1061}10621063static void stbtt__cff_skip_operand(stbtt__buf *b) {1064 int v, b0 = stbtt__buf_peek8(b);1065 STBTT_assert(b0 >= 28);1066 if (b0 == 30) {1067 stbtt__buf_skip(b, 1);1068 while (b->cursor < b->size) {1069 v = stbtt__buf_get8(b);1070 if ((v & 0xF) == 0xF || (v >> 4) == 0xF)1071 break;1072 }1073 } else {1074 stbtt__cff_int(b);1075 }1076}10771078static stbtt__buf stbtt__dict_get(stbtt__buf *b, int key)1079{1080 stbtt__buf_seek(b, 0);1081 while (b->cursor < b->size) {1082 int start = b->cursor, end, op;1083 while (stbtt__buf_peek8(b) >= 28)1084 stbtt__cff_skip_operand(b);1085 end = b->cursor;1086 op = stbtt__buf_get8(b);1087 if (op == 12) op = stbtt__buf_get8(b) | 0x100;1088 if (op == key) return stbtt__buf_range(b, start, end-start);1089 }1090 return stbtt__buf_range(b, 0, 0);1091}10921093static void stbtt__dict_get_ints(stbtt__buf *b, int key, int outcount, stbtt_uint32 *out)1094{1095 int i;1096 stbtt__buf operands = stbtt__dict_get(b, key);1097 for (i = 0; i < outcount && operands.cursor < operands.size; i++)1098 out[i] = stbtt__cff_int(&operands);1099}11001101static int stbtt__cff_index_count(stbtt__buf *b)1102{1103 stbtt__buf_seek(b, 0);1104 return stbtt__buf_get16(b);1105}11061107static stbtt__buf stbtt__cff_index_get(stbtt__buf b, int i)1108{1109 int count, offsize, start, end;1110 stbtt__buf_seek(&b, 0);1111 count = stbtt__buf_get16(&b);1112 offsize = stbtt__buf_get8(&b);1113 STBTT_assert(i >= 0 && i < count);1114 STBTT_assert(offsize >= 1 && offsize <= 4);1115 stbtt__buf_skip(&b, i*offsize);1116 start = stbtt__buf_get(&b, offsize);1117 end = stbtt__buf_get(&b, offsize);1118 return stbtt__buf_range(&b, 2+(count+1)*offsize+start, end - start);1119}11201121//////////////////////////////////////////////////////////////////////////1122//1123// accessors to parse data from file1124//11251126// on platforms that don't allow misaligned reads, if we want to allow
64 unmodified lines
1191 }1192 return -1;1193}11941195static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict)1196{1197 stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 };1198 stbtt__buf pdict;1199 stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);1200 if (!private_loc[1] || !private_loc[0]) return stbtt__new_buf(NULL, 0);1201 pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);1202 stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);1203 if (!subrsoff) return stbtt__new_buf(NULL, 0);1204 stbtt__buf_seek(&cff, private_loc[1]+subrsoff);1205 return stbtt__cff_get_index(&cff);1206}12071208STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data2, int fontstart)1209{1210 stbtt_uint8 *data = (stbtt_uint8 *) data2;1211 stbtt_uint32 cmap, t;1212 stbtt_int32 i,numTables;12131214 info->data = data;1215 info->fontstart = fontstart;1216 info->cff = stbtt__new_buf(NULL, 0);12171218 cmap = stbtt__find_table(data, fontstart, "cmap"); // required1219 info->loca = stbtt__find_table(data, fontstart, "loca"); // required1220 info->head = stbtt__find_table(data, fontstart, "head"); // required1221 info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required1222 info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required1223 info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required1224 info->kern = stbtt__find_table(data, fontstart, "kern"); // not required12251049 if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)1226 if (!cmap || !info->head || !info->hhea || !info->hmtx)1227 return 0;1228 if (info->glyf) {1229 // required for truetype1230 if (!info->loca) return 0;1231 } else {1232 // initialization for CFF / Type2 fonts (OTF)1233 stbtt__buf b, topdict, topdictidx, pdict;1234 stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;1235 stbtt_uint32 cff;12361237 cff = stbtt__find_table(data, fontstart, "CFF ");1238 if (!cff) return 0;12391240 info->fontdicts = stbtt__new_buf(NULL, 0);1241 info->fdselect = stbtt__new_buf(NULL, 0);12421243 // @TODO this should use size from table (not 512MB)1244 info->cff = stbtt__new_buf(data+cff, 512*1024*1024);1245 b = info->cff;12461247 // read the header1248 stbtt__buf_skip(&b, 2);1249 stbtt__buf_seek(&b, stbtt__buf_get8(&b)); // hdrsize12501251 // @TODO the name INDEX could list multiple fonts,1252 // but we just use the first one.1253 stbtt__cff_get_index(&b); // name INDEX1254 topdictidx = stbtt__cff_get_index(&b);1255 topdict = stbtt__cff_index_get(topdictidx, 0);1256 stbtt__cff_get_index(&b); // string INDEX1257 info->gsubrs = stbtt__cff_get_index(&b);12581259 stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);1260 stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);1261 stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);1262 stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);1263 info->subrs = stbtt__get_subrs(b, topdict);12641265 // we only support Type 2 charstrings1266 if (cstype != 2) return 0;1267 if (charstrings == 0) return 0;12681269 if (fdarrayoff) {1270 // looks like a CID font1271 if (!fdselectoff) return 0;1272 stbtt__buf_seek(&b, fdarrayoff);1273 info->fontdicts = stbtt__cff_get_index(&b);1274 info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size-fdselectoff);1275 }12761277 stbtt__buf_seek(&b, charstrings);1278 info->charstrings = stbtt__cff_get_index(&b);1279 }12801281 t = stbtt__find_table(data, fontstart, "maxp");1282 if (t)1283 info->numGlyphs = ttUSHORT(data+t+4);1284 else
141 unmodified lines
1426static int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index)1427{1428 int g1,g2;14291430 STBTT_assert(!info->cff.size);14311432 if (glyph_index >= info->numGlyphs) return -1; // glyph index out of range1433 if (info->indexToLocFormat >= 2) return -1; // unknown index->glyph map format14341435 if (info->indexToLocFormat == 0) {
6 unmodified lines
14421443 return g1==g2 ? -1 : g1; // if length is 0, return -11444}14451446static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);14471448STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)1449{1450 if (info->cff.size) {1451 stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);1452 } else {1217 int g = stbtt__GetGlyfOffset(info, glyph_index);1218 if (g < 0) return 0;1453 int g = stbtt__GetGlyfOffset(info, glyph_index);1454 if (g < 0) return 0;14551220 if (x0) *x0 = ttSHORT(info->data + g + 2);1221 if (y0) *y0 = ttSHORT(info->data + g + 4);1222 if (x1) *x1 = ttSHORT(info->data + g + 6);1223 if (y1) *y1 = ttSHORT(info->data + g + 8);1456 if (x0) *x0 = ttSHORT(info->data + g + 2);1457 if (y0) *y0 = ttSHORT(info->data + g + 4);1458 if (x1) *x1 = ttSHORT(info->data + g + 6);1459 if (y1) *y1 = ttSHORT(info->data + g + 8);1460 }1461 return 1;1462}14631464STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)
3 unmodified lines
14681469STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)1470{1471 stbtt_int16 numberOfContours;1472 int g;1473 if (info->cff.size)1474 return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0;1235 int g = stbtt__GetGlyfOffset(info, glyph_index);1475 g = stbtt__GetGlyfOffset(info, glyph_index);1476 if (g < 0) return 1;1477 numberOfContours = ttSHORT(info->data + g);1478 return numberOfContours == 0;1479}
13 unmodified lines
1493 }1494 return num_vertices;1495}14961257STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)1497static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)1498{1499 stbtt_int16 numberOfContours;1500 stbtt_uint8 *endPtsOfContours;1501 stbtt_uint8 *data = info->data;
217 unmodified lines
1719 *pvertices = vertices;1720 return num_vertices;1721}17221723typedef struct1724{1725 int bounds;1726 int started;1727 float first_x, first_y;1728 float x, y;1729 stbtt_int32 min_x, max_x, min_y, max_y;17301731 stbtt_vertex *pvertices;1732 int num_vertices;1733} stbtt__csctx;17341735#define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0}17361737static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y)1738{1739 if (x > c->max_x || !c->started) c->max_x = x;1740 if (y > c->max_y || !c->started) c->max_y = y;1741 if (x < c->min_x || !c->started) c->min_x = x;1742 if (y < c->min_y || !c->started) c->min_y = y;1743 c->started = 1;1744}17451746static 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)1747{1748 if (c->bounds) {1749 stbtt__track_vertex(c, x, y);1750 if (type == STBTT_vcubic) {1751 stbtt__track_vertex(c, cx, cy);1752 stbtt__track_vertex(c, cx1, cy1);1753 }1754 } else {1755 stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);1756 c->pvertices[c->num_vertices].cx1 = (stbtt_int16) cx1;1757 c->pvertices[c->num_vertices].cy1 = (stbtt_int16) cy1;1758 }1759 c->num_vertices++;1760}17611762static void stbtt__csctx_close_shape(stbtt__csctx *ctx)1763{1764 if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)1765 stbtt__cstx_v(ctx, STBTT_vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0);1766}17671768static void stbtt__csctx_rmove_to(stbtt__csctx *ctx, float dx, float dy)1769{1770 stbtt__csctx_close_shape(ctx);1771 ctx->first_x = ctx->x = ctx->x + dx;1772 ctx->first_y = ctx->y = ctx->y + dy;1773 stbtt__cstx_v(ctx, STBTT_vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);1774}17751776static void stbtt__csctx_rline_to(stbtt__csctx *ctx, float dx, float dy)1777{1778 ctx->x += dx;1779 ctx->y += dy;1780 stbtt__cstx_v(ctx, STBTT_vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);1781}17821783static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3)1784{1785 float cx1 = ctx->x + dx1;1786 float cy1 = ctx->y + dy1;1787 float cx2 = cx1 + dx2;1788 float cy2 = cy1 + dy2;1789 ctx->x = cx2 + dx3;1790 ctx->y = cy2 + dy3;1791 stbtt__cstx_v(ctx, STBTT_vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2);1792}17931794static stbtt__buf stbtt__get_subr(stbtt__buf idx, int n)1795{1796 int count = stbtt__cff_index_count(&idx);1797 int bias = 107;1798 if (count >= 33900)1799 bias = 32768;1800 else if (count >= 1240)1801 bias = 1131;1802 n += bias;1803 if (n < 0 || n >= count)1804 return stbtt__new_buf(NULL, 0);1805 return stbtt__cff_index_get(idx, n);1806}18071808static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo *info, int glyph_index)1809{1810 stbtt__buf fdselect = info->fdselect;1811 int nranges, start, end, v, fmt, fdselector = -1, i;18121813 stbtt__buf_seek(&fdselect, 0);1814 fmt = stbtt__buf_get8(&fdselect);1815 if (fmt == 0) {1816 // untested1817 stbtt__buf_skip(&fdselect, glyph_index);1818 fdselector = stbtt__buf_get8(&fdselect);1819 } else if (fmt == 3) {1820 nranges = stbtt__buf_get16(&fdselect);1821 start = stbtt__buf_get16(&fdselect);1822 for (i = 0; i < nranges; i++) {1823 v = stbtt__buf_get8(&fdselect);1824 end = stbtt__buf_get16(&fdselect);1825 if (glyph_index >= start && glyph_index < end) {1826 fdselector = v;1827 break;1828 }1829 start = end;1830 }1831 }1832 if (fdselector == -1) stbtt__new_buf(NULL, 0);1833 return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));1834}18351836static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, stbtt__csctx *c)1837{1838 int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;1839 int has_subrs = 0, clear_stack;1840 float s[48];1841 stbtt__buf subr_stack[10], subrs = info->subrs, b;1842 float f;18431844#define STBTT__CSERR(s) (0)18451846 // this currently ignores the initial width value, which isn't needed if we have hmtx1847 b = stbtt__cff_index_get(info->charstrings, glyph_index);1848 while (b.cursor < b.size) {1849 i = 0;1850 clear_stack = 1;1851 b0 = stbtt__buf_get8(&b);1852 switch (b0) {1853 // @TODO implement hinting1854 case 0x13: // hintmask1855 case 0x14: // cntrmask1856 if (in_header)1857 maskbits += (sp / 2); // implicit "vstem"1858 in_header = 0;1859 stbtt__buf_skip(&b, (maskbits + 7) / 8);1860 break;18611862 case 0x01: // hstem1863 case 0x03: // vstem1864 case 0x12: // hstemhm1865 case 0x17: // vstemhm1866 maskbits += (sp / 2);1867 break;18681869 case 0x15: // rmoveto1870 in_header = 0;1871 if (sp < 2) return STBTT__CSERR("rmoveto stack");1872 stbtt__csctx_rmove_to(c, s[sp-2], s[sp-1]);1873 break;1874 case 0x04: // vmoveto1875 in_header = 0;1876 if (sp < 1) return STBTT__CSERR("vmoveto stack");1877 stbtt__csctx_rmove_to(c, 0, s[sp-1]);1878 break;1879 case 0x16: // hmoveto1880 in_header = 0;1881 if (sp < 1) return STBTT__CSERR("hmoveto stack");1882 stbtt__csctx_rmove_to(c, s[sp-1], 0);1883 break;18841885 case 0x05: // rlineto1886 if (sp < 2) return STBTT__CSERR("rlineto stack");1887 for (; i + 1 < sp; i += 2)1888 stbtt__csctx_rline_to(c, s[i], s[i+1]);1889 break;18901891 // hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical1892 // starting from a different place.18931894 case 0x07: // vlineto1895 if (sp < 1) return STBTT__CSERR("vlineto stack");1896 goto vlineto;1897 case 0x06: // hlineto1898 if (sp < 1) return STBTT__CSERR("hlineto stack");1899 for (;;) {1900 if (i >= sp) break;1901 stbtt__csctx_rline_to(c, s[i], 0);1902 i++;1903 vlineto:1904 if (i >= sp) break;1905 stbtt__csctx_rline_to(c, 0, s[i]);1906 i++;1907 }1908 break;19091910 case 0x1F: // hvcurveto1911 if (sp < 4) return STBTT__CSERR("hvcurveto stack");1912 goto hvcurveto;1913 case 0x1E: // vhcurveto1914 if (sp < 4) return STBTT__CSERR("vhcurveto stack");1915 for (;;) {1916 if (i + 3 >= sp) break;1917 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);1918 i += 4;1919 hvcurveto:1920 if (i + 3 >= sp) break;1921 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]);1922 i += 4;1923 }1924 break;19251926 case 0x08: // rrcurveto1927 if (sp < 6) return STBTT__CSERR("rcurveline stack");1928 for (; i + 5 < sp; i += 6)1929 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);1930 break;19311932 case 0x18: // rcurveline1933 if (sp < 8) return STBTT__CSERR("rcurveline stack");1934 for (; i + 5 < sp - 2; i += 6)1935 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);1936 if (i + 1 >= sp) return STBTT__CSERR("rcurveline stack");1937 stbtt__csctx_rline_to(c, s[i], s[i+1]);1938 break;19391940 case 0x19: // rlinecurve1941 if (sp < 8) return STBTT__CSERR("rlinecurve stack");1942 for (; i + 1 < sp - 6; i += 2)1943 stbtt__csctx_rline_to(c, s[i], s[i+1]);1944 if (i + 5 >= sp) return STBTT__CSERR("rlinecurve stack");1945 stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);1946 break;19471948 case 0x1A: // vvcurveto1949 case 0x1B: // hhcurveto1950 if (sp < 4) return STBTT__CSERR("(vv|hh)curveto stack");1951 f = 0.0;1952 if (sp & 1) { f = s[i]; i++; }1953 for (; i + 3 < sp; i += 4) {1954 if (b0 == 0x1B)1955 stbtt__csctx_rccurve_to(c, s[i], f, s[i+1], s[i+2], s[i+3], 0.0);1956 else1957 stbtt__csctx_rccurve_to(c, f, s[i], s[i+1], s[i+2], 0.0, s[i+3]);1958 f = 0.0;1959 }1960 break;19611962 case 0x0A: // callsubr1963 if (!has_subrs) {1964 if (info->fdselect.size)1965 subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);1966 has_subrs = 1;1967 }1968 // fallthrough1969 case 0x1D: // callgsubr1970 if (sp < 1) return STBTT__CSERR("call(g|)subr stack");1971 v = (int) s[--sp];1972 if (subr_stack_height >= 10) return STBTT__CSERR("recursion limit");1973 subr_stack[subr_stack_height++] = b;1974 b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v);1975 if (b.size == 0) return STBTT__CSERR("subr not found");1976 b.cursor = 0;1977 clear_stack = 0;1978 break;19791980 case 0x0B: // return1981 if (subr_stack_height <= 0) return STBTT__CSERR("return outside subr");1982 b = subr_stack[--subr_stack_height];1983 clear_stack = 0;1984 break;19851986 case 0x0E: // endchar1987 stbtt__csctx_close_shape(c);1988 return 1;19891990 case 0x0C: { // two-byte escape1991 float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;1992 float dx, dy;1993 int b1 = stbtt__buf_get8(&b);1994 switch (b1) {1995 // @TODO These "flex" implementations ignore the flex-depth and resolution,1996 // and always draw beziers.1997 case 0x22: // hflex1998 if (sp < 7) return STBTT__CSERR("hflex stack");1999 dx1 = s[0];2000 dx2 = s[1];2001 dy2 = s[2];2002 dx3 = s[3];2003 dx4 = s[4];2004 dx5 = s[5];2005 dx6 = s[6];2006 stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);2007 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);2008 break;20092010 case 0x23: // flex2011 if (sp < 13) return STBTT__CSERR("flex stack");2012 dx1 = s[0];2013 dy1 = s[1];2014 dx2 = s[2];2015 dy2 = s[3];2016 dx3 = s[4];2017 dy3 = s[5];2018 dx4 = s[6];2019 dy4 = s[7];2020 dx5 = s[8];2021 dy5 = s[9];2022 dx6 = s[10];2023 dy6 = s[11];2024 //fd is s[12]2025 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);2026 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);2027 break;20282029 case 0x24: // hflex12030 if (sp < 9) return STBTT__CSERR("hflex1 stack");2031 dx1 = s[0];2032 dy1 = s[1];2033 dx2 = s[2];2034 dy2 = s[3];2035 dx3 = s[4];2036 dx4 = s[5];2037 dx5 = s[6];2038 dy5 = s[7];2039 dx6 = s[8];2040 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);2041 stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1+dy2+dy5));2042 break;20432044 case 0x25: // flex12045 if (sp < 11) return STBTT__CSERR("flex1 stack");2046 dx1 = s[0];2047 dy1 = s[1];2048 dx2 = s[2];2049 dy2 = s[3];2050 dx3 = s[4];2051 dy3 = s[5];2052 dx4 = s[6];2053 dy4 = s[7];2054 dx5 = s[8];2055 dy5 = s[9];2056 dx6 = dy6 = s[10];2057 dx = dx1+dx2+dx3+dx4+dx5;2058 dy = dy1+dy2+dy3+dy4+dy5;2059 if (STBTT_fabs(dx) > STBTT_fabs(dy))2060 dy6 = -dy;2061 else2062 dx6 = -dx;2063 stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);2064 stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);2065 break;20662067 default:2068 return STBTT__CSERR("unimplemented");2069 }2070 } break;20712072 default:2073 if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254))2074 return STBTT__CSERR("reserved operator");20752076 // push immediate2077 if (b0 == 255) {2078 f = (float)stbtt__buf_get32(&b) / 0x10000;2079 } else {2080 stbtt__buf_skip(&b, -1);2081 f = (float)(stbtt_int16)stbtt__cff_int(&b);2082 }2083 if (sp >= 48) return STBTT__CSERR("push stack overflow");2084 s[sp++] = f;2085 clear_stack = 0;2086 break;2087 }2088 if (clear_stack) sp = 0;2089 }2090 return STBTT__CSERR("no endchar");20912092#undef STBTT__CSERR2093}20942095static int stbtt__GetGlyphShapeT2(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)2096{2097 // runs the charstring twice, once to count and once to output (to avoid realloc)2098 stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);2099 stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);2100 if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {2101 *pvertices = (stbtt_vertex*)STBTT_malloc(count_ctx.num_vertices*sizeof(stbtt_vertex), info->userdata);2102 output_ctx.pvertices = *pvertices;2103 if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {2104 STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);2105 return output_ctx.num_vertices;2106 }2107 }2108 *pvertices = NULL;2109 return 0;2110}21112112static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)2113{2114 stbtt__csctx c = STBTT__CSCTX_INIT(1);2115 int r = stbtt__run_charstring(info, glyph_index, &c);2116 if (x0) {2117 *x0 = r ? c.min_x : 0;2118 *y0 = r ? c.min_y : 0;2119 *x1 = r ? c.max_x : 0;2120 *y1 = r ? c.max_y : 0;2121 }2122 return r ? c.num_vertices : 0;2123}21242125STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)2126{2127 if (!info->cff.size)2128 return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);2129 else2130 return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);2131}21322133STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)2134{2135 stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);2136 if (glyph_index < numOfLongHorMetrics) {
862 unmodified lines
2999 }3000 return 1;3001}30023003static 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)3004{3005 // @TODO this "flatness" calculation is just made-up nonsense that seems to work well enough3006 float dx0 = x1-x0;3007 float dy0 = y1-y0;3008 float dx1 = x2-x1;3009 float dy1 = y2-y1;3010 float dx2 = x3-x2;3011 float dy2 = y3-y2;3012 float dx = x3-x0;3013 float dy = y3-y0;3014 float longlen = STBTT_sqrt(dx0*dx0+dy0*dy0)+STBTT_sqrt(dx1*dx1+dy1*dy1)+STBTT_sqrt(dx2*dx2+dy2*dy2);3015 float shortlen = STBTT_sqrt(dx*dx+dy*dy);3016 float flatness_squared = longlen*longlen-shortlen*shortlen;30173018 if (n > 16) // 65536 segments on one curve better be enough!3019 return;30203021 if (flatness_squared > objspace_flatness_squared) {3022 float x01 = (x0+x1)/2;3023 float y01 = (y0+y1)/2;3024 float x12 = (x1+x2)/2;3025 float y12 = (y1+y2)/2;3026 float x23 = (x2+x3)/2;3027 float y23 = (y2+y3)/2;30283029 float xa = (x01+x12)/2;3030 float ya = (y01+y12)/2;3031 float xb = (x12+x23)/2;3032 float yb = (y12+y23)/2;30333034 float mx = (xa+xb)/2;3035 float my = (ya+yb)/2;30363037 stbtt__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my, objspace_flatness_squared,n+1);3038 stbtt__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3, objspace_flatness_squared,n+1);3039 } else {3040 stbtt__add_point(points, *num_points,x3,y3);3041 *num_points = *num_points+1;3042 }3043}30443045// returns number of contours3046static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)3047{3048 stbtt__point *points=0;
48 unmodified lines
3097 vertices[i].x, vertices[i].y,3098 objspace_flatness_squared, 0);3099 x = vertices[i].x, y = vertices[i].y;3100 break;3101 case STBTT_vcubic:3102 stbtt__tesselate_cubic(points, &num_points, x,y,3103 vertices[i].cx, vertices[i].cy,3104 vertices[i].cx1, vertices[i].cy1,3105 vertices[i].x, vertices[i].y,3106 objspace_flatness_squared, 0);3107 x = vertices[i].x, y = vertices[i].y;3108 break;3109 }3110 }3111 (*contour_lengths)[n] = num_points - start;3112 }
855 unmodified lines