History

read (sub)family, underline position and thickness

Sigrid Haflínudóttir committed 6 years ago

Commit 700a47c1 parent 189db4a2 files · +106 −1Browse files

Unified · Split

2 files
  1. stb_truetype.h +44 −1
  2. test.c +62 −0
stb_truetype.h+44−1
@@ -717,6 +717,10 @@ struct stbtt_fontinfo · 716 unmodified lines
717717 int loca,head,glyf,hhea,hmtx,kern,gpos,svg; // table locations as offset from start of .ttf
718718 int index_map; // a cmap mapping for our chosen character encoding
719719 int indexToLocFormat; // format needed to map from glyph index to glyph
720+ int underlinePosition;
721+ int underlineThickness;
722+ char family[128];
723+ char subfamily[128];
720724
721725 stbtt__buf cff; // cff font data
722726 stbtt__buf charstrings; // the charstring index
@@ -1378,7 +1382,9 @@ static int stbtt__get_svg(stbtt_fontinfo *info) · 655 unmodified lines
13781382 static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart)
13791383 {
13801384 stbtt_uint32 cmap, t;
1381− stbtt_int32 i,numTables;
1385+ stbtt_int32 i, numTables;
1386+
1387+ memset(info, 0, sizeof(*info));
13821388
13831389 info->data = data;
13841390 info->fontstart = fontstart;
@@ -1456,6 +1462,43 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in · 71 unmodified lines
14561462
14571463 info->svg = -1;
14581464
1465+ t = stbtt__find_table(data, fontstart, "post");
1466+ if (t) {
1467+ info->underlinePosition = ttSHORT(data+t+8);
1468+ info->underlineThickness = ttUSHORT(data+t+10);
1469+ }
1470+
1471+ t = stbtt__find_table(data, fontstart, "name");
1472+ if (t) {
1473+ stbtt_int32 i, platform, name, count, stringOffset, length;
1474+ stbtt_uint8 *fc = data;
1475+ stbtt_uint32 offset = fontstart;
1476+ stbtt_uint32 nm = stbtt__find_table(fc, offset, "name");
1477+ char *o, tmp[256];
1478+
1479+ count = ttUSHORT(fc+nm+2);
1480+ stringOffset = nm + ttUSHORT(fc+nm+4);
1481+ for (i=0; i < count; ++i) {
1482+ stbtt_uint32 loc = nm + 6 + 12 * i;
1483+ platform = ttUSHORT(fc+loc+0);
1484+ if (platform != 0 && platform != 1)
1485+ continue;
1486+ name = ttUSHORT(fc+loc+6);
1487+ if (name != 1 && name != 2) /* FIXME use 16 and 17? */
1488+ continue;
1489+ length = ttUSHORT(fc+loc+8);
1490+ o = (char*)fc+stringOffset+ttUSHORT(fc+loc+10);
1491+ if (platform == 0) {
1492+ length = utf16to8((uchar*)tmp, sizeof(tmp), (uchar*)o, length);
1493+ o = tmp;
1494+ }
1495+ if (name == 1)
1496+ snprint(info->family, sizeof(info->family), "%.*s", length, o);
1497+ else if (name == 2)
1498+ snprint(info->subfamily, sizeof(info->subfamily), "%.*s", length, o);
1499+ }
1500+ }
1501+
14591502 // find a cmap encoding table we understand *now* to avoid searching
14601503 // later. (todo: could make this installable)
14611504 // the same regardless of glyph.
test.c+62−0
@@ -23,6 +23,7 @@ · 22 unmodified lines
2323 typedef usize size_t;
2424 #define NULL nil
2525 #define STBTT_RASTERIZER_VERSION 1 /* that one is definitely FASTER on amd64 */
26+static int utf16to8(uchar *o, int osz, const uchar *s, int sz);
2627 #include "stb_truetype.h"
2728
2829 #define MAX(a,b) ((a)>=(b)?(a):(b))
@@ -50,6 +51,63 @@ static int invert; · 21 unmodified lines
5051 static Point offset = {8, 8};
5152 int mainstacksize = 32768;
5253
54+#define rchr(s) (be ? ((s)[0]<<8 | (s)[1]) : ((s)[1]<<8 | (s)[0]))
55+
56+static const uchar mark[] = {0x00, 0x00, 0xc0, 0xe0, 0xf0};
57+
58+static int
59+utf16to8(uchar *o, int osz, const uchar *s, int sz)
60+{
61+ int i, be, c, c2, wr, j;
62+
63+ i = 0;
64+ be = 1;
65+ if(s[0] == 0xfe && s[1] == 0xff)
66+ i += 2;
67+ else if(s[0] == 0xff && s[1] == 0xfe){
68+ be = 0;
69+ i += 2;
70+ }
71+
72+ for(; i < sz-1 && osz > 1;){
73+ c = rchr(&s[i]);
74+ i += 2;
75+ if(c >= 0xd800 && c <= 0xdbff && i < sz-1){
76+ c2 = rchr(&s[i]);
77+ if(c2 >= 0xdc00 && c2 <= 0xdfff){
78+ c = 0x10000 | (c - 0xd800)<<10 | (c2 - 0xdc00);
79+ i += 2;
80+ }else
81+ return -1;
82+ }else if(c >= 0xdc00 && c <= 0xdfff)
83+ return -1;
84+
85+ if(c < 0x80)
86+ wr = 1;
87+ else if(c < 0x800)
88+ wr = 2;
89+ else if(c < 0x10000)
90+ wr = 3;
91+ else
92+ wr = 4;
93+
94+ osz -= wr;
95+ if(osz < 1)
96+ break;
97+
98+ o += wr;
99+ for(j = wr; j > 1; j--){
100+ *(--o) = (c & 0xbf) | 0x80;
101+ c >>= 6;
102+ }
103+ *(--o) = c | mark[wr];
104+ o += wr;
105+ }
106+
107+ *o = 0;
108+ return i;
109+}
110+
53111 static void
54112 redraw(void)
55113 {
@@ -289,6 +347,10 @@ threadmain(int argc, char **argv) · 233 unmodified lines
289347 if(initfont(&fo, data, height) != 0)
290348 sysfatal("initfont");
291349 prepare(&fo, s);
350+ print("%s %s\n", fo.fi.family, fo.fi.subfamily);
351+ print("max glyphs: %d\n", fo.fi.numGlyphs);
352+ print("underlinePosition: %d\n", fo.fi.underlinePosition);
353+ print("underlineThickness: %d\n", fo.fi.underlineThickness);
292354
293355 for(;;){
294356 oldb = m.buttons;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t