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
717 int loca,head,glyf,hhea,hmtx,kern,gpos,svg; // table locations as offset from start of .ttf717 int loca,head,glyf,hhea,hmtx,kern,gpos,svg; // table locations as offset from start of .ttf
718 int index_map; // a cmap mapping for our chosen character encoding718 int index_map; // a cmap mapping for our chosen character encoding
719 int indexToLocFormat; // format needed to map from glyph index to glyph719 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];
720 724
721 stbtt__buf cff; // cff font data725 stbtt__buf cff; // cff font data
722 stbtt__buf charstrings; // the charstring index726 stbtt__buf charstrings; // the charstring index
@@ -1378,7 +1382,9 @@ static int stbtt__get_svg(stbtt_fontinfo *info) · 655 unmodified lines
1378static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart)1382static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart)
1379{1383{
1380 stbtt_uint32 cmap, t;1384 stbtt_uint32 cmap, t;
1381 stbtt_int32 i,numTables;1385 stbtt_int32 i, numTables;
1386
1387 memset(info, 0, sizeof(*info));
1382 1388
1383 info->data = data;1389 info->data = data;
1384 info->fontstart = fontstart;1390 info->fontstart = fontstart;
@@ -1456,6 +1462,43 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in · 71 unmodified lines
1456 1462
1457 info->svg = -1;1463 info->svg = -1;
1458 1464
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
1459 // find a cmap encoding table we understand *now* to avoid searching1502 // find a cmap encoding table we understand *now* to avoid searching
1460 // later. (todo: could make this installable)1503 // later. (todo: could make this installable)
1461 // the same regardless of glyph.1504 // the same regardless of glyph.
test.c+62−0
@@ -23,6 +23,7 @@ · 22 unmodified lines
23typedef usize size_t;23typedef usize size_t;
24#define NULL nil24#define NULL nil
25#define STBTT_RASTERIZER_VERSION 1 /* that one is definitely FASTER on amd64 */25#define STBTT_RASTERIZER_VERSION 1 /* that one is definitely FASTER on amd64 */
26static int utf16to8(uchar *o, int osz, const uchar *s, int sz);
26#include "stb_truetype.h"27#include "stb_truetype.h"
27 28
28#define MAX(a,b) ((a)>=(b)?(a):(b))29#define MAX(a,b) ((a)>=(b)?(a):(b))
@@ -50,6 +51,63 @@ static int invert; · 21 unmodified lines
50static Point offset = {8, 8};51static Point offset = {8, 8};
51int mainstacksize = 32768;52int mainstacksize = 32768;
52 53
54#define rchr(s) (be ? ((s)[0]<<8 | (s)[1]) : ((s)[1]<<8 | (s)[0]))
55
56static const uchar mark[] = {0x00, 0x00, 0xc0, 0xe0, 0xf0};
57
58static int
59utf16to8(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
53static void111static void
54redraw(void)112redraw(void)
55{113{
@@ -289,6 +347,10 @@ threadmain(int argc, char **argv) · 233 unmodified lines
289 if(initfont(&fo, data, height) != 0)347 if(initfont(&fo, data, height) != 0)
290 sysfatal("initfont");348 sysfatal("initfont");
291 prepare(&fo, s);349 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);
292 354
293 for(;;){355 for(;;){
294 oldb = m.buttons;356 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