aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-06-02 16:07:10 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-06-02 16:07:10 -0700
commit269b52b8c87e692517ea2dc1da14fd19c3c236c2 (patch)
tree9e29fd5fa38ed8014f5ef5c6be0f60777cdfae16 /sys
parent87c93d0193a6d61efe436fa2d8ac1cbc8cd18002 (diff)
reorganized find tables
Diffstat (limited to 'sys')
-rw-r--r--sys/libfont/font.c424
1 files changed, 218 insertions, 206 deletions
diff --git a/sys/libfont/font.c b/sys/libfont/font.c
index 3c615fd..c7944d0 100644
--- a/sys/libfont/font.c
+++ b/sys/libfont/font.c
@@ -82,7 +82,7 @@ struct font·Bitmap
typedef int test_oversample_pow2[(SAMPLE & (SAMPLE-1)) == 0 ? 1 : -1];
// -----------------------------------------------------------------------
-// buf helpers to parse data from file
+// buffer helpers to parse data from file
static
uchar
@@ -90,6 +90,7 @@ getbyte(Buffer *b)
{
if (b->cursor >= b->size)
return 0;
+
return b->data[b->cursor++];
}
@@ -99,6 +100,7 @@ peek(Buffer *b)
{
if (b->cursor >= b->size)
return 0;
+
return b->data[b->cursor];
}
@@ -121,9 +123,11 @@ static
uint32
getbytes(Buffer *b, int n)
{
- uint32 v = 0;
+ uint32 v;
int i;
assert(n >= 1 && n <= 4);
+
+ v = 0;
for (i = 0; i < n; i++)
v = (v << 8) | getbyte(b);
return v;
@@ -131,24 +135,29 @@ getbytes(Buffer *b, int n)
static
Buffer
-makebuf(const void *p, size_t size)
+makebuffer(void *p, uintptr size)
{
Buffer r;
assert(size < 0x40000000);
- r.data = (uchar*) p;
- r.size = (int) size;
+
+ r.data = (uchar*) p;
+ r.size = (int)size;
r.cursor = 0;
+
return r;
}
static
Buffer
-slice(const Buffer *b, int o, int s)
+slice(Buffer *b, int o, int s)
{
- Buffer r = makebuf(nil, 0);
- if (o < 0 || s < 0 || o > b->size || s > b->size - o) return r;
+ Buffer r = makebuffer(nil, 0);
+ if (o < 0 || s < 0 || o > b->size || s > b->size - o)
+ return r;
+
r.data = b->data + o;
r.size = s;
+
return r;
}
@@ -174,12 +183,13 @@ uint32
cff_int(Buffer *b)
{
int b0 = getbyte(b);
- if (b0 >= 32 && b0 <= 246) return b0 - 139;
- else if (b0 >= 247 && b0 <= 250) return (b0 - 247)*256 + getbyte(b) + 108;
+ if (b0 >= 32 && b0 <= 246) return +b0 - 139;
+ else if (b0 >= 247 && b0 <= 250) return +(b0 - 247)*256 + getbyte(b) + 108;
else if (b0 >= 251 && b0 <= 254) return -(b0 - 251)*256 - getbyte(b) - 108;
- else if (b0 == 28) return getshort(b);
- else if (b0 == 29) return getint(b);
- assert(0);
+ else if (b0 == 28) return +getshort(b);
+ else if (b0 == 29) return +getint(b);
+
+ panicf("unreachable");
return 0;
}
@@ -261,26 +271,26 @@ cff_index_get(Buffer b, int i)
#define ttBYTE(p) (* (uchar *) (p))
#define ttCHAR(p) (* (char *) (p))
-#define ttFixed(p) ttLONG(p)
+#define ttFixed(p) ttlong(p)
-static ushort ttUSHORT(uchar *p) { return p[0]*256 + p[1]; }
-static short ttSHORT(uchar *p) { return p[0]*256 + p[1]; }
-static uint32 ttULONG(uchar *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
-static int32 ttLONG(uchar *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
+static ushort ttushort(uchar *p) { return p[0]*256 + p[1]; }
+static short ttshort(uchar *p) { return p[0]*256 + p[1]; }
+static uint32 ttulong(uchar *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
+static int32 ttlong(uchar *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
-#define font·tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
-#define font·tag(p,str) font·tag4(p,str[0],str[1],str[2],str[3])
+#define ttf·tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
+#define ttf·tag(p,str) ttf·tag4(p,str[0],str[1],str[2],str[3])
static
int
isfont(uchar *font)
{
// check the version number
- if (font·tag4(font, '1',0,0,0)) return 1; // TrueType 1
- if (font·tag(font, "typ1")) return 1; // TrueType with type 1 font -- we don't support this!
- if (font·tag(font, "OTTO")) return 1; // OpenType with CFF
- if (font·tag4(font, 0,1,0,0)) return 1; // OpenType 1.0
- if (font·tag(font, "true")) return 1; // Apple specification for TrueType fonts
+ if (ttf·tag4(font, '1',0,0,0)) return 1; // TrueType 1
+ if (ttf·tag(font, "typ1")) return 1; // TrueType with type 1 font -- we don't support this!
+ if (ttf·tag(font, "OTTO")) return 1; // OpenType with CFF
+ if (ttf·tag4(font, 0,1,0,0)) return 1; // OpenType 1.0
+ if (ttf·tag(font, "true")) return 1; // Apple specification for TrueType fonts
return 0;
}
@@ -288,18 +298,18 @@ isfont(uchar *font)
// @OPTIMIZE: binary search
static
uint32
-find_table(uchar *data, uint32 fontstart, const char *tag)
+find_table(uchar *data, uint32 offset, char *tag)
{
int i;
int32 ntab;
- uint32 tabdir;
+ uint32 tabdir, loc;
- ntab = ttUSHORT(data+fontstart+4);
- tabdir = fontstart + 12;
+ ntab = ttushort(data+offset+4);
+ tabdir = offset + 12;
for (i=0; i < ntab; ++i) {
- uint32 loc = tabdir + 16*i;
- if (font·tag(data+loc+0, tag))
- return ttULONG(data+loc+8);
+ loc = tabdir + 16*i;
+ if (ttf·tag(data+loc+0, tag))
+ return ttulong(data+loc+8);
}
return 0;
}
@@ -312,13 +322,13 @@ font·offsetfor(uchar *font_collection, int index)
return index == 0 ? 0 : -1;
// check if it's a TTC
- if (font·tag(font_collection, "ttcf")) {
+ if (ttf·tag(font_collection, "ttcf")) {
// version 1?
- if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
- int32 n = ttLONG(font_collection+8);
+ if (ttulong(font_collection+4) == 0x00010000 || ttulong(font_collection+4) == 0x00020000) {
+ int32 n = ttlong(font_collection+8);
if (index >= n)
return -1;
- return ttULONG(font_collection+12+index*4);
+ return ttulong(font_collection+12+index*4);
}
}
return -1;
@@ -332,10 +342,10 @@ font·number(uchar *font_collection)
return 1;
// check if it's a TTC
- if (font·tag(font_collection, "ttcf")) {
+ if (ttf·tag(font_collection, "ttcf")) {
// version 1?
- if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
- return ttLONG(font_collection+8);
+ if (ttulong(font_collection+4) == 0x00010000 || ttulong(font_collection+4) == 0x00020000) {
+ return ttlong(font_collection+8);
}
}
return 0;
@@ -348,10 +358,12 @@ get_subrs(Buffer cff, Buffer fontdict)
uint32 subrsoff = 0, private_loc[2] = { 0, 0 };
Buffer pdict;
dict_get_ints(&fontdict, 18, 2, private_loc);
- if (!private_loc[1] || !private_loc[0]) return makebuf(nil, 0);
+ if (!private_loc[1] || !private_loc[0])
+ return makebuffer(nil, 0);
pdict = slice(&cff, private_loc[1], private_loc[0]);
dict_get_ints(&pdict, 19, 1, &subrsoff);
- if (!subrsoff) return makebuf(nil, 0);
+ if (!subrsoff)
+ return makebuffer(nil, 0);
seek(&cff, private_loc[1]+subrsoff);
return cff_index(&cff);
}
@@ -365,7 +377,7 @@ get_svg(font·Info *info)
if (info->svg < 0) {
t = find_table(info->data, info->fontstart, "SVG ");
if (t) {
- uint32 offset = ttULONG(info->data + t + 2);
+ uint32 offset = ttulong(info->data + t + 2);
info->svg = t + offset;
} else {
info->svg = 0;
@@ -376,26 +388,26 @@ get_svg(font·Info *info)
static
int
-init(font·Info *info, uchar *data, int fontstart)
+init(font·Info *info, uchar *data, int offset)
{
uint32 cmap, t;
int32 i,numTables;
- info->data = data;
- info->fontstart = fontstart;
- info->cff = makebuf(nil, 0);
+ info->data = data;
+ info->fontstart = offset;
+ info->cff = makebuffer(nil, 0);
- cmap = find_table(data, fontstart, "cmap"); // required
- info->loca = find_table(data, fontstart, "loca"); // required
- info->head = find_table(data, fontstart, "head"); // required
- info->glyf = find_table(data, fontstart, "glyf"); // required
- info->hhea = find_table(data, fontstart, "hhea"); // required
- info->hmtx = find_table(data, fontstart, "hmtx"); // required
- info->kern = find_table(data, fontstart, "kern"); // not required
- info->gpos = find_table(data, fontstart, "GPOS"); // not required
+ cmap = find_table(data, offset, "cmap"); // required
+ info->loca = find_table(data, offset, "loca"); // required
+ info->head = find_table(data, offset, "head"); // required
+ info->glyf = find_table(data, offset, "glyf"); // required
+ info->hhea = find_table(data, offset, "hhea"); // required
+ info->hmtx = find_table(data, offset, "hmtx"); // required
+ info->kern = find_table(data, offset, "kern"); // not required
+ info->gpos = find_table(data, offset, "GPOS"); // not required
if (!cmap || !info->head || !info->hhea || !info->hmtx)
- return 0;
+ return 1;
if (info->glyf) {
// required for truetype
if (!info->loca)
@@ -406,15 +418,15 @@ init(font·Info *info, uchar *data, int fontstart)
uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;
uint32 cff;
- cff = find_table(data, fontstart, "CFF ");
+ cff = find_table(data, offset, "CFF ");
if (!cff)
return 1;
- info->fontdicts = makebuf(nil, 0);
- info->fdselect = makebuf(nil, 0);
+ info->fontdicts = makebuffer(nil, 0);
+ info->fdselect = makebuffer(nil, 0);
// @TODO this should use size from table (not 512MB)
- info->cff = makebuf(data+cff, 512*1024*1024);
+ info->cff = makebuffer(data+cff, 512*1024*1024);
b = info->cff;
// read the header
@@ -429,8 +441,8 @@ init(font·Info *info, uchar *data, int fontstart)
cff_index(&b); // string INDEX
info->gsubrs = cff_index(&b);
- dict_get_ints(&topdict, 17, 1, &charstrings);
- dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
+ dict_get_ints(&topdict, 17, 1, &charstrings);
+ dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);
dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);
info->subrs = get_subrs(b, topdict);
@@ -454,9 +466,9 @@ init(font·Info *info, uchar *data, int fontstart)
info->charstrings = cff_index(&b);
}
- t = find_table(data, fontstart, "maxp");
+ t = find_table(data, offset, "maxp");
if (t)
- info->numglyphs = ttUSHORT(data+t+4);
+ info->numglyphs = ttushort(data+t+4);
else
info->numglyphs = 0xffff;
@@ -465,16 +477,16 @@ init(font·Info *info, uchar *data, int fontstart)
// find a cmap encoding table we understand *now* to avoid searching
// later. (todo: could make this installable)
// the same regardless of glyph.
- numTables = ttUSHORT(data + cmap + 2);
+ numTables = ttushort(data + cmap + 2);
info->index_map = 0;
for (i=0; i < numTables; ++i) {
uint32 encoding_record = cmap + 4 + 8 * i;
// find an encoding we understand:
- switch(ttUSHORT(data+encoding_record)) {
+ switch(ttushort(data+encoding_record)) {
case font·platform_unicode:
// Mac/iOS has these
// all the encodingIDs are unicode, so we don't bother to check it
- info->index_map = cmap + ttULONG(data+encoding_record+4);
+ info->index_map = cmap + ttulong(data+encoding_record+4);
break;
default:
;
@@ -484,7 +496,7 @@ init(font·Info *info, uchar *data, int fontstart)
return 1;
}
- info->indexToLocFormat = ttUSHORT(data+info->head + 50);
+ info->indexToLocFormat = ttushort(data+info->head + 50);
return 0;
}
@@ -525,26 +537,26 @@ font·glyph_index(font·Info *info, int unicode_codepoint)
uchar *data = info->data;
uint32 index_map = info->index_map;
- ushort format = ttUSHORT(data + index_map + 0);
+ ushort format = ttushort(data + index_map + 0);
if (format == 0) { // apple byte encoding
- int32 bytes = ttUSHORT(data + index_map + 2);
+ int32 bytes = ttushort(data + index_map + 2);
if (unicode_codepoint < bytes-6)
return ttBYTE(data + index_map + 6 + unicode_codepoint);
return 0;
} else if (format == 6) {
- uint32 first = ttUSHORT(data + index_map + 6);
- uint32 count = ttUSHORT(data + index_map + 8);
+ uint32 first = ttushort(data + index_map + 6);
+ uint32 count = ttushort(data + index_map + 8);
if ((uint32) unicode_codepoint >= first && (uint32) unicode_codepoint < first+count)
- return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
+ return ttushort(data + index_map + 10 + (unicode_codepoint - first)*2);
return 0;
} else if (format == 2) {
assert(0); // @TODO: high-byte mapping for japanese/chinese/korean
return 0;
} else if (format == 4) { // standard mapping for windows fonts: binary search collection of ranges
- ushort segcount = ttUSHORT(data+index_map+6) >> 1;
- ushort searchRange = ttUSHORT(data+index_map+8) >> 1;
- ushort entrySelector = ttUSHORT(data+index_map+10);
- ushort rangeShift = ttUSHORT(data+index_map+12) >> 1;
+ ushort segcount = ttushort(data+index_map+6) >> 1;
+ ushort searchRange = ttushort(data+index_map+8) >> 1;
+ ushort entrySelector = ttushort(data+index_map+10);
+ ushort rangeShift = ttushort(data+index_map+12) >> 1;
// do a binary search of the segments
uint32 endCount = index_map + 14;
@@ -555,7 +567,7 @@ font·glyph_index(font·Info *info, int unicode_codepoint)
// they lie from endCount .. endCount + segCount
// but searchRange is the nearest power of two, so...
- if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))
+ if (unicode_codepoint >= ttushort(data + search + rangeShift*2))
search += rangeShift*2;
// now decrement to bias correctly to find smallest
@@ -563,7 +575,7 @@ font·glyph_index(font·Info *info, int unicode_codepoint)
while (entrySelector) {
ushort end;
searchRange >>= 1;
- end = ttUSHORT(data + search + searchRange*2);
+ end = ttushort(data + search + searchRange*2);
if (unicode_codepoint > end)
search += searchRange*2;
--entrySelector;
@@ -574,32 +586,32 @@ font·glyph_index(font·Info *info, int unicode_codepoint)
ushort offset, start;
ushort item = (ushort) ((search - endCount) >> 1);
- assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));
- start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
+ assert(unicode_codepoint <= ttushort(data + endCount + 2*item));
+ start = ttushort(data + index_map + 14 + segcount*2 + 2 + 2*item);
if (unicode_codepoint < start)
return 0;
- offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
+ offset = ttushort(data + index_map + 14 + segcount*6 + 2 + 2*item);
if (offset == 0)
- return (ushort) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
+ return (ushort) (unicode_codepoint + ttshort(data + index_map + 14 + segcount*4 + 2 + 2*item));
- return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
+ return ttushort(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
}
} else if (format == 12 || format == 13) {
- uint32 ngroups = ttULONG(data+index_map+12);
+ uint32 ngroups = ttulong(data+index_map+12);
int32 low,high;
low = 0; high = (int32)ngroups;
// Binary search the right group.
while (low < high) {
int32 mid = low + ((high-low) >> 1); // rounds down, so low <= mid < high
- uint32 start_char = ttULONG(data+index_map+16+mid*12);
- uint32 end_char = ttULONG(data+index_map+16+mid*12+4);
+ uint32 start_char = ttulong(data+index_map+16+mid*12);
+ uint32 end_char = ttulong(data+index_map+16+mid*12+4);
if ((uint32) unicode_codepoint < start_char)
high = mid;
else if ((uint32) unicode_codepoint > end_char)
low = mid+1;
else {
- uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);
+ uint32 start_glyph = ttulong(data+index_map+16+mid*12+8);
if (format == 12)
return start_glyph + unicode_codepoint-start_char;
else // format == 13
@@ -632,7 +644,7 @@ setvertex(font·Vertex *v, uchar type, int32 x, int32 y, int32 cx, int32 cy)
static
int
-glyph_offset(const font·Info *info, int glyph_index)
+glyph_offset(font·Info *info, int glyph_index)
{
int g1,g2;
@@ -642,11 +654,11 @@ glyph_offset(const font·Info *info, int glyph_index)
if (info->indexToLocFormat >= 2) return -1; // unknown index->glyph map format
if (info->indexToLocFormat == 0) {
- g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
- g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
+ g1 = info->glyf + ttushort(info->data + info->loca + glyph_index * 2) * 2;
+ g2 = info->glyf + ttushort(info->data + info->loca + glyph_index * 2 + 2) * 2;
} else {
- g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4);
- g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4);
+ g1 = info->glyf + ttulong (info->data + info->loca + glyph_index * 4);
+ g2 = info->glyf + ttulong (info->data + info->loca + glyph_index * 4 + 4);
}
return g1==g2 ? -1 : g1; // if length is 0, return -1
@@ -663,10 +675,10 @@ font·glyph_box(font·Info *info, int glyph_index, int *x0, int *y0, int *x1, in
int g = glyph_offset(info, glyph_index);
if (g < 0) return 0;
- if (x0) *x0 = ttSHORT(info->data + g + 2);
- if (y0) *y0 = ttSHORT(info->data + g + 4);
- if (x1) *x1 = ttSHORT(info->data + g + 6);
- if (y1) *y1 = ttSHORT(info->data + g + 8);
+ if (x0) *x0 = ttshort(info->data + g + 2);
+ if (y0) *y0 = ttshort(info->data + g + 4);
+ if (x1) *x1 = ttshort(info->data + g + 6);
+ if (y1) *y1 = ttshort(info->data + g + 8);
}
return 1;
}
@@ -686,7 +698,7 @@ font·glyph_empty(font·Info *info, int glyph_index)
return glyph_info_t2(info, glyph_index, nil, nil, nil, nil) == 0;
g = glyph_offset(info, glyph_index);
if (g < 0) return 1;
- numberOfContours = ttSHORT(info->data + g);
+ numberOfContours = ttshort(info->data + g);
return numberOfContours == 0;
}
@@ -723,7 +735,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pvertices)
if (g < 0) return 0;
- numberOfContours = ttSHORT(data + g);
+ numberOfContours = ttshort(data + g);
if (numberOfContours > 0) {
uchar flags=0,flagcount;
@@ -731,10 +743,10 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pvertices)
int32 x,y,cx,cy,sx,sy, scx,scy;
uchar *points;
endPtsOfContours = (data + g + 10);
- ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
+ ins = ttushort(data + g + 10 + numberOfContours * 2);
points = data + g + 10 + numberOfContours * 2 + 2 + ins;
- n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2);
+ n = 1+ttushort(endPtsOfContours + numberOfContours*2-2);
m = n + 2*numberOfContours; // a loose bound on how many vertices we might need
vertices = info->alloc(info->heap, m, sizeof(vertices[0]));
@@ -829,7 +841,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pvertices)
}
setvertex(&vertices[num_vertices++], font·Vmove,sx,sy,0,0);
was_off = 0;
- next_move = 1 + ttUSHORT(endPtsOfContours+j*2);
+ next_move = 1 + ttushort(endPtsOfContours+j*2);
++j;
} else {
if (!(flags & 1)) { // if it's a curve
@@ -860,13 +872,13 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pvertices)
font·Vertex *comp_verts = 0, *tmp = 0;
float mtx[6] = {1,0,0,1,0,0}, m, n;
- flags = ttSHORT(comp); comp+=2;
- gidx = ttSHORT(comp); comp+=2;
+ flags = ttshort(comp); comp+=2;
+ gidx = ttshort(comp); comp+=2;
if (flags & 2) { // XY values
if (flags & 1) { // shorts
- mtx[4] = ttSHORT(comp); comp+=2;
- mtx[5] = ttSHORT(comp); comp+=2;
+ mtx[4] = ttshort(comp); comp+=2;
+ mtx[5] = ttshort(comp); comp+=2;
} else {
mtx[4] = ttCHAR(comp); comp+=1;
mtx[5] = ttCHAR(comp); comp+=1;
@@ -877,17 +889,17 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pvertices)
assert(0);
}
if (flags & (1<<3)) { // WE_HAVE_A_SCALE
- mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
+ mtx[0] = mtx[3] = ttshort(comp)/16384.0f; comp+=2;
mtx[1] = mtx[2] = 0;
} else if (flags & (1<<6)) { // WE_HAVE_AN_X_AND_YSCALE
- mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
+ mtx[0] = ttshort(comp)/16384.0f; comp+=2;
mtx[1] = mtx[2] = 0;
- mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
+ mtx[3] = ttshort(comp)/16384.0f; comp+=2;
} else if (flags & (1<<7)) { // WE_HAVE_A_TWO_BY_TWO
- mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
- mtx[1] = ttSHORT(comp)/16384.0f; comp+=2;
- mtx[2] = ttSHORT(comp)/16384.0f; comp+=2;
- mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
+ mtx[0] = ttshort(comp)/16384.0f; comp+=2;
+ mtx[1] = ttshort(comp)/16384.0f; comp+=2;
+ mtx[2] = ttshort(comp)/16384.0f; comp+=2;
+ mtx[3] = ttshort(comp)/16384.0f; comp+=2;
}
// Find transformation scales.
@@ -1019,11 +1031,11 @@ static Buffer get_subr(Buffer idx, int n)
bias = 1131;
n += bias;
if (n < 0 || n >= count)
- return makebuf(nil, 0);
+ return makebuffer(nil, 0);
return cff_index_get(idx, n);
}
-static Buffer cid_get_glyph_subrs(const font·Info *info, int glyph_index)
+static Buffer cid_get_glyph_subrs(font·Info *info, int glyph_index)
{
Buffer fdselect = info->fdselect;
int nranges, start, end, v, fmt, fdselector = -1, i;
@@ -1047,7 +1059,7 @@ static Buffer cid_get_glyph_subrs(const font·Info *info, int glyph_index)
start = end;
}
}
- if (fdselector == -1) makebuf(nil, 0);
+ if (fdselector == -1) makebuffer(nil, 0);
return get_subrs(info->cff, cff_index_get(info->fontdicts, fdselector));
}
@@ -1354,13 +1366,13 @@ font·glyph_shape(font·Info *info, int glyph_index, font·Vertex **pvertices)
void
font·glyph_hmetrics(font·Info *info, int glyph_index, int *advanceWidth, int *leftSideBearing)
{
- ushort numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);
+ ushort numOfLongHorMetrics = ttushort(info->data+info->hhea + 34);
if (glyph_index < numOfLongHorMetrics) {
- if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index);
- if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
+ if (advanceWidth) *advanceWidth = ttshort(info->data + info->hmtx + 4*glyph_index);
+ if (leftSideBearing) *leftSideBearing = ttshort(info->data + info->hmtx + 4*glyph_index + 2);
} else {
- if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
- if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
+ if (advanceWidth) *advanceWidth = ttshort(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
+ if (leftSideBearing) *leftSideBearing = ttshort(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
}
}
@@ -1372,12 +1384,12 @@ font·kerntablen(font·Info *info)
// we only look at the first table. it must be 'horizontal' and format 0.
if (!info->kern)
return 0;
- if (ttUSHORT(data+2) < 1) // number of tables, need at least 1
+ if (ttushort(data+2) < 1) // number of tables, need at least 1
return 0;
- if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format
+ if (ttushort(data+8) != 1) // horizontal flag must be set in format
return 0;
- return ttUSHORT(data+10);
+ return ttushort(data+10);
}
int
@@ -1389,20 +1401,20 @@ font·kerntab(font·Info *info, font·TabElt* table, int table_length)
// we only look at the first table. it must be 'horizontal' and format 0.
if (!info->kern)
return 0;
- if (ttUSHORT(data+2) < 1) // number of tables, need at least 1
+ if (ttushort(data+2) < 1) // number of tables, need at least 1
return 0;
- if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format
+ if (ttushort(data+8) != 1) // horizontal flag must be set in format
return 0;
- length = ttUSHORT(data+10);
+ length = ttushort(data+10);
if (table_length < length)
length = table_length;
for (k = 0; k < length; k++)
{
- table[k].glyph1 = ttUSHORT(data+18+(k*6));
- table[k].glyph2 = ttUSHORT(data+20+(k*6));
- table[k].advance = ttSHORT(data+22+(k*6));
+ table[k].glyph1 = ttushort(data+18+(k*6));
+ table[k].glyph2 = ttushort(data+20+(k*6));
+ table[k].advance = ttshort(data+22+(k*6));
}
return length;
@@ -1410,7 +1422,7 @@ font·kerntab(font·Info *info, font·TabElt* table, int table_length)
static
int
-glyph_kernadvance(const font·Info *info, int glyph1, int glyph2)
+glyph_kernadvance(font·Info *info, int glyph1, int glyph2)
{
uchar *data = info->data + info->kern;
uint32 needle, straw;
@@ -1419,23 +1431,23 @@ glyph_kernadvance(const font·Info *info, int glyph1, int glyph2)
// we only look at the first table. it must be 'horizontal' and format 0.
if (!info->kern)
return 0;
- if (ttUSHORT(data+2) < 1) // number of tables, need at least 1
+ if (ttushort(data+2) < 1) // number of tables, need at least 1
return 0;
- if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format
+ if (ttushort(data+8) != 1) // horizontal flag must be set in format
return 0;
l = 0;
- r = ttUSHORT(data+10) - 1;
+ r = ttushort(data+10) - 1;
needle = glyph1 << 16 | glyph2;
while (l <= r) {
m = (l + r) >> 1;
- straw = ttULONG(data+18+(m*6)); // note: unaligned read
+ straw = ttulong(data+18+(m*6)); // note: unaligned read
if (needle < straw)
r = m - 1;
else if (needle > straw)
l = m + 1;
else
- return ttSHORT(data+22+(m*6));
+ return ttshort(data+22+(m*6));
}
return 0;
}
@@ -1444,10 +1456,10 @@ static
int32
coverage_index(uchar *coverageTable, int glyph)
{
- ushort coverageFormat = ttUSHORT(coverageTable);
+ ushort coverageFormat = ttushort(coverageTable);
switch(coverageFormat) {
case 1: {
- ushort glyphCount = ttUSHORT(coverageTable + 2);
+ ushort glyphCount = ttushort(coverageTable + 2);
// Binary search.
int32 l=0, r=glyphCount-1, m;
@@ -1456,7 +1468,7 @@ coverage_index(uchar *coverageTable, int glyph)
uchar *glyphArray = coverageTable + 4;
ushort glyphID;
m = (l + r) >> 1;
- glyphID = ttUSHORT(glyphArray + 2 * m);
+ glyphID = ttushort(glyphArray + 2 * m);
straw = glyphID;
if (needle < straw)
r = m - 1;
@@ -1469,7 +1481,7 @@ coverage_index(uchar *coverageTable, int glyph)
} break;
case 2: {
- ushort rangeCount = ttUSHORT(coverageTable + 2);
+ ushort rangeCount = ttushort(coverageTable + 2);
uchar *rangeArray = coverageTable + 4;
// Binary search.
@@ -1479,14 +1491,14 @@ coverage_index(uchar *coverageTable, int glyph)
uchar *rangeRecord;
m = (l + r) >> 1;
rangeRecord = rangeArray + 6 * m;
- strawStart = ttUSHORT(rangeRecord);
- strawEnd = ttUSHORT(rangeRecord + 2);
+ strawStart = ttushort(rangeRecord);
+ strawEnd = ttushort(rangeRecord + 2);
if (needle < strawStart)
r = m - 1;
else if (needle > strawEnd)
l = m + 1;
else {
- ushort startCoverageIndex = ttUSHORT(rangeRecord + 4);
+ ushort startCoverageIndex = ttushort(rangeRecord + 4);
return startCoverageIndex + glyph - strawStart;
}
}
@@ -1505,22 +1517,22 @@ static
int32
glyph_class(uchar *classDefTable, int glyph)
{
- ushort classDefFormat = ttUSHORT(classDefTable);
+ ushort classDefFormat = ttushort(classDefTable);
switch(classDefFormat)
{
case 1: {
- ushort startGlyphID = ttUSHORT(classDefTable + 2);
- ushort glyphCount = ttUSHORT(classDefTable + 4);
+ ushort startGlyphID = ttushort(classDefTable + 2);
+ ushort glyphCount = ttushort(classDefTable + 4);
uchar *classDef1ValueArray = classDefTable + 6;
if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount)
- return (int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID));
+ return (int32)ttushort(classDef1ValueArray + 2 * (glyph - startGlyphID));
classDefTable = classDef1ValueArray + 2 * glyphCount;
} break;
case 2: {
- ushort classRangeCount = ttUSHORT(classDefTable + 2);
+ ushort classRangeCount = ttushort(classDefTable + 2);
uchar *classRangeRecords = classDefTable + 4;
// Binary search.
@@ -1530,14 +1542,14 @@ glyph_class(uchar *classDefTable, int glyph)
uchar *classRangeRecord;
m = (l + r) >> 1;
classRangeRecord = classRangeRecords + 6 * m;
- strawStart = ttUSHORT(classRangeRecord);
- strawEnd = ttUSHORT(classRangeRecord + 2);
+ strawStart = ttushort(classRangeRecord);
+ strawEnd = ttushort(classRangeRecord + 2);
if (needle < strawStart)
r = m - 1;
else if (needle > strawEnd)
l = m + 1;
else
- return (int32)ttUSHORT(classRangeRecord + 4);
+ return (int32)ttushort(classRangeRecord + 4);
}
classDefTable = classRangeRecords + 6 * classRangeCount;
@@ -1554,7 +1566,7 @@ glyph_class(uchar *classDefTable, int glyph)
static
int32
-glyph_gposadvance(const font·Info *info, int glyph1, int glyph2)
+glyph_gposadvance(font·Info *info, int glyph1, int glyph2)
{
ushort lookupListOffset;
uchar *lookupList;
@@ -1566,28 +1578,28 @@ glyph_gposadvance(const font·Info *info, int glyph1, int glyph2)
data = info->data + info->gpos;
- if (ttUSHORT(data+0) != 1) return 0; // Major version 1
- if (ttUSHORT(data+2) != 0) return 0; // Minor version 0
+ if (ttushort(data+0) != 1) return 0; // Major version 1
+ if (ttushort(data+2) != 0) return 0; // Minor version 0
- lookupListOffset = ttUSHORT(data+8);
+ lookupListOffset = ttushort(data+8);
lookupList = data + lookupListOffset;
- lookupCount = ttUSHORT(lookupList);
+ lookupCount = ttushort(lookupList);
for (i=0; i<lookupCount; ++i) {
- ushort lookupOffset = ttUSHORT(lookupList + 2 + 2 * i);
+ ushort lookupOffset = ttushort(lookupList + 2 + 2 * i);
uchar *lookupTable = lookupList + lookupOffset;
- ushort lookupType = ttUSHORT(lookupTable);
- ushort subTableCount = ttUSHORT(lookupTable + 4);
+ ushort lookupType = ttushort(lookupTable);
+ ushort subTableCount = ttushort(lookupTable + 4);
uchar *subTableOffsets = lookupTable + 6;
switch(lookupType) {
case 2: { // Pair Adjustment Positioning Subtable
int32 sti;
for (sti=0; sti<subTableCount; sti++) {
- ushort subtableOffset = ttUSHORT(subTableOffsets + 2 * sti);
+ ushort subtableOffset = ttushort(subTableOffsets + 2 * sti);
uchar *table = lookupTable + subtableOffset;
- ushort posFormat = ttUSHORT(table);
- ushort coverageOffset = ttUSHORT(table + 2);
+ ushort posFormat = ttushort(table);
+ ushort coverageOffset = ttushort(table + 2);
int32 coverageIndex = coverage_index(table + coverageOffset, glyph1);
if (coverageIndex == -1) continue;
@@ -1595,13 +1607,13 @@ glyph_gposadvance(const font·Info *info, int glyph1, int glyph2)
case 1: {
int32 l, r, m;
int straw, needle;
- ushort valueFormat1 = ttUSHORT(table + 4);
- ushort valueFormat2 = ttUSHORT(table + 6);
+ ushort valueFormat1 = ttushort(table + 4);
+ ushort valueFormat2 = ttushort(table + 6);
int32 valueRecordPairSizeInBytes = 2;
- ushort pairSetCount = ttUSHORT(table + 8);
- ushort pairPosOffset = ttUSHORT(table + 10 + 2 * coverageIndex);
+ ushort pairSetCount = ttushort(table + 8);
+ ushort pairPosOffset = ttushort(table + 10 + 2 * coverageIndex);
uchar *pairValueTable = table + pairPosOffset;
- ushort pairValueCount = ttUSHORT(pairValueTable);
+ ushort pairValueCount = ttushort(pairValueTable);
uchar *pairValueArray = pairValueTable + 2;
// TODO: Support more formats.
if (valueFormat1 != 4) return 0;
@@ -1619,30 +1631,30 @@ glyph_gposadvance(const font·Info *info, int glyph1, int glyph2)
uchar *pairValue;
m = (l + r) >> 1;
pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m;
- secondGlyph = ttUSHORT(pairValue);
+ secondGlyph = ttushort(pairValue);
straw = secondGlyph;
if (needle < straw)
r = m - 1;
else if (needle > straw)
l = m + 1;
else {
- short xAdvance = ttSHORT(pairValue + 2);
+ short xAdvance = ttshort(pairValue + 2);
return xAdvance;
}
}
} break;
case 2: {
- ushort valueFormat1 = ttUSHORT(table + 4);
- ushort valueFormat2 = ttUSHORT(table + 6);
+ ushort valueFormat1 = ttushort(table + 4);
+ ushort valueFormat2 = ttushort(table + 6);
- ushort classDef1Offset = ttUSHORT(table + 8);
- ushort classDef2Offset = ttUSHORT(table + 10);
+ ushort classDef1Offset = ttushort(table + 8);
+ ushort classDef2Offset = ttushort(table + 10);
int glyph1class = glyph_class(table + classDef1Offset, glyph1);
int glyph2class = glyph_class(table + classDef2Offset, glyph2);
- ushort class1Count = ttUSHORT(table + 12);
- ushort class2Count = ttUSHORT(table + 14);
+ ushort class1Count = ttushort(table + 12);
+ ushort class2Count = ttushort(table + 14);
assert(glyph1class < class1Count);
assert(glyph2class < class2Count);
@@ -1653,7 +1665,7 @@ glyph_gposadvance(const font·Info *info, int glyph1, int glyph2)
if (glyph1class >= 0 && glyph1class < class1Count && glyph2class >= 0 && glyph2class < class2Count) {
uchar *class1Records = table + 16;
uchar *class2Records = class1Records + 2 * (glyph1class * class2Count);
- short xAdvance = ttSHORT(class2Records + 2 * glyph2class);
+ short xAdvance = ttshort(class2Records + 2 * glyph2class);
return xAdvance;
}
} break;
@@ -1707,31 +1719,31 @@ font·code_hmetrics(font·Info *info, int codepoint, int *advanceWidth, int *lsb
void
font·vmetrics(font·Info *info, int *ascent, int *descent, int *lineGap)
{
- if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4);
- if (descent) *descent = ttSHORT(info->data+info->hhea + 6);
- if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);
+ if (ascent ) *ascent = ttshort(info->data+info->hhea + 4);
+ if (descent) *descent = ttshort(info->data+info->hhea + 6);
+ if (lineGap) *lineGap = ttshort(info->data+info->hhea + 8);
}
void
font·bbox(font·Info *info, int *x0, int *y0, int *x1, int *y1)
{
- *x0 = ttSHORT(info->data + info->head + 36);
- *y0 = ttSHORT(info->data + info->head + 38);
- *x1 = ttSHORT(info->data + info->head + 40);
- *y1 = ttSHORT(info->data + info->head + 42);
+ *x0 = ttshort(info->data + info->head + 36);
+ *y0 = ttshort(info->data + info->head + 38);
+ *x1 = ttshort(info->data + info->head + 40);
+ *y1 = ttshort(info->data + info->head + 42);
}
float
font·scaleheightto(font·Info *info, float height)
{
- int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
+ int fheight = ttshort(info->data + info->hhea + 4) - ttshort(info->data + info->hhea + 6);
return (float) height / fheight;
}
float
font·scaleheighttoem(font·Info *info, float pixels)
{
- int unitsPerEm = ttUSHORT(info->data + info->head + 18);
+ int unitsPerEm = ttushort(info->data + info->head + 18);
return pixels / unitsPerEm;
}
@@ -1743,18 +1755,18 @@ font·freeshape(font·Info *info, font·Vertex *v)
static
uchar *
-find_svg(const font·Info *info, int gl)
+find_svg(font·Info *info, int gl)
{
int i;
uchar *data = info->data;
uchar *svg_doc_list = data + get_svg((font·Info *) info);
- int numEntries = ttUSHORT(svg_doc_list);
+ int numEntries = ttushort(svg_doc_list);
uchar *svg_docs = svg_doc_list + 2;
for(i=0; i<numEntries; i++) {
uchar *svg_doc = svg_docs + (12 * i);
- if ((gl >= ttUSHORT(svg_doc)) && (gl <= ttUSHORT(svg_doc + 2)))
+ if ((gl >= ttushort(svg_doc)) && (gl <= ttushort(svg_doc + 2)))
return svg_doc;
}
return 0;
@@ -1771,8 +1783,8 @@ font·glyph_svg(font·Info *info, int gl, char **svg)
svg_doc = find_svg(info, gl);
if (svg_doc != nil) {
- *svg = (char *) data + info->svg + ttULONG(svg_doc + 4);
- return ttULONG(svg_doc + 8);
+ *svg = (char *) data + info->svg + ttulong(svg_doc + 4);
+ return ttulong(svg_doc + 8);
} else {
return 0;
}
@@ -1841,7 +1853,7 @@ typedef struct hheap
static
void *
-hheap_alloc(hheap *hh, size_t size, mem·Allocator mem, void *heap)
+hheap_alloc(hheap *hh, uintptr size, mem·Allocator mem, void *heap)
{
if (hh->first_free) {
void *p = hh->first_free;
@@ -3212,14 +3224,14 @@ font·name(font·Info *font, int *length, int platformID, int encodingID, int la
uint32 nm = find_table(fc, offset, "name");
if (!nm) return nil;
- count = ttUSHORT(fc+nm+2);
- stringOffset = nm + ttUSHORT(fc+nm+4);
+ count = ttushort(fc+nm+2);
+ stringOffset = nm + ttushort(fc+nm+4);
for (i=0; i < count; ++i) {
uint32 loc = nm + 6 + 12 * i;
- if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2)
- && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) {
- *length = ttUSHORT(fc+loc+8);
- return (char *) (fc+stringOffset+ttUSHORT(fc+loc+10));
+ if (platformID == ttushort(fc+loc+0) && encodingID == ttushort(fc+loc+2)
+ && languageID == ttushort(fc+loc+4) && nameID == ttushort(fc+loc+6)) {
+ *length = ttushort(fc+loc+8);
+ return (char *) (fc+stringOffset+ttushort(fc+loc+10));
}
}
return nil;
@@ -3231,28 +3243,28 @@ int
matchpair(uchar *fc, uint32 nm, uchar *name, int32 nlen, int32 target_id, int32 next_id)
{
int32 i;
- int32 count = ttUSHORT(fc+nm+2);
- int32 stringOffset = nm + ttUSHORT(fc+nm+4);
+ int32 count = ttushort(fc+nm+2);
+ int32 stringOffset = nm + ttushort(fc+nm+4);
for (i=0; i < count; ++i) {
uint32 loc = nm + 6 + 12 * i;
- int32 id = ttUSHORT(fc+loc+6);
+ int32 id = ttushort(fc+loc+6);
if (id == target_id) {
// find the encoding
- int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4);
+ int32 platform = ttushort(fc+loc+0), encoding = ttushort(fc+loc+2), language = ttushort(fc+loc+4);
// is this a Unicode encoding?
if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
- int32 slen = ttUSHORT(fc+loc+8);
- int32 off = ttUSHORT(fc+loc+10);
+ int32 slen = ttushort(fc+loc+8);
+ int32 off = ttushort(fc+loc+10);
// check if there's a prefix match
int32 matchlen = CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);
if (matchlen >= 0) {
// check for target_id+1 immediately following, with same encoding & language
- 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) {
- slen = ttUSHORT(fc+loc+12+8);
- off = ttUSHORT(fc+loc+12+10);
+ 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) {
+ slen = ttushort(fc+loc+12+8);
+ off = ttushort(fc+loc+12+10);
if (slen == 0) {
if (matchlen == nlen)
return 1;
@@ -3287,7 +3299,7 @@ matches(uchar *fc, uint32 offset, uchar *name, int32 flags)
// check italics/bold/underline flags in macStyle...
if (flags) {
hd = find_table(fc, offset, "head");
- if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7)) return 0;
+ if ((ttushort(fc+hd+44) & 7) != (flags & 7)) return 0;
}
nm = find_table(fc, offset, "name");