From 1b53162d56fabd2faa3eb7649d5484810ce514d2 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 2 Jun 2020 16:49:17 -0700 Subject: removed annoying macro --- sys/libfont/font.c | 245 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 169 insertions(+), 76 deletions(-) diff --git a/sys/libfont/font.c b/sys/libfont/font.c index efd3e21..1492b61 100644 --- a/sys/libfont/font.c +++ b/sys/libfont/font.c @@ -552,7 +552,6 @@ font·glyph_index(font·Info *info, int unicode_codepoint) uint32 end; uint32 search; }; - struct { uint32 begg, begc, endc, ngroups; int32 low, mid, high; @@ -669,10 +668,10 @@ void setvertex(font·Vertex *v, uchar type, int32 x, int32 y, int32 cx, int32 cy) { v->type = type; - v->x = (short) x; - v->y = (short) y; - v->cx = (short) cx; - v->cy = (short) cy; + v->x = (short) x; + v->y = (short) y; + v->cx = (short) cx; + v->cy = (short) cy; } static @@ -694,7 +693,7 @@ glyph_offset(font·Info *info, int glyph_index) g2 = info->glyf + ttulong (info->data + info->loca + glyph_index * 4 + 4); } - return g1==g2 ? -1 : g1; // if length is 0, return -1 + return (g1==g2) ? -1 : g1; // if length is 0, return -1 } static int glyph_info_t2(font·Info *info, int glyph_index, int *x0, int *y0, int *x1, int *y1); @@ -725,14 +724,18 @@ font·code_box(font·Info *info, int codepoint, int *x0, int *y0, int *x1, int * int font·glyph_empty(font·Info *info, int glyph_index) { - short numberOfContours; int g; + short numc; + if (info->cff.size) 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); - return numberOfContours == 0; + if (g < 0) + return 1; + + numc = ttshort(info->data + g); + return numc == 0; } static @@ -757,8 +760,8 @@ static int glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) { - short numberOfContours; - uchar *endPtsOfContours; + short numc; + uchar *contourend; uchar *data = info->data; font·Vertex *verts = nil; int num_verts = 0; @@ -769,20 +772,21 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) if (g < 0) return 0; - numberOfContours = ttshort(data + g); + numc = ttshort(data + g); - if (numberOfContours > 0) { - uchar flags=0,flagcount; + if (numc > 0) { + uchar flags=0, flagcount; int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0; - int32 x,y,cx,cy,sx,sy, scx,scy; + int32 x,y,cx,cy,sx,sy,scx,scy; + uchar *points; - endPtsOfContours = (data + g + 10); - ins = ttushort(data + g + 10 + numberOfContours * 2); - points = data + g + 10 + numberOfContours * 2 + 2 + ins; + contourend = (data + g + 10); + ins = ttushort(data + g + 10 + numc * 2); + points = data + g + 10 + numc * 2 + 2 + ins; - n = 1+ttushort(endPtsOfContours + numberOfContours*2-2); + n = 1+ttushort(contourend + numc*2-2); - m = n + 2*numberOfContours; // a loose bound on how many verts we might need + m = n + 2*numc; // a loose bound on how many verts we might need verts = info->alloc(info->heap, m, sizeof(verts[0])); if (verts == 0) return 0; @@ -817,11 +821,11 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) x += (flags & 16) ? dx : -dx; // ??? } else { if (!(flags & 16)) { - x = x + (short) (points[0]*256 + points[1]); + x = x + (short)(points[0]*256 + points[1]); points += 2; } } - verts[off+i].x = (short) x; + verts[off+i].x = (short)x; } // now load y coordinates @@ -837,7 +841,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) points += 2; } } - verts[off+i].y = (short) y; + verts[off+i].y = (short)y; } // now convert them to our format @@ -845,8 +849,8 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) sx = sy = cx = cy = scx = scy = 0; for (i=0; i < n; ++i) { flags = verts[off+i].type; - x = (short) verts[off+i].x; - y = (short) verts[off+i].y; + x = (short)verts[off+i].x; + y = (short)verts[off+i].y; if (next_move == i) { if (i != 0) @@ -874,8 +878,8 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) sy = y; } setvertex(&verts[num_verts++], font·Vmove,sx,sy,0,0); - was_off = 0; - next_move = 1 + ttushort(endPtsOfContours+j*2); + was_off = 0; + next_move = 1 + ttushort(contourend+j*2); ++j; } else { if (!(flags & 1)) { // if it's a curve @@ -893,8 +897,8 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) } } } - num_verts = close_shape(verts, num_verts, was_off, start_off, sx,sy,scx,scy,cx,cy); - } else if (numberOfContours < 0) { + num_verts = close_shape(verts, num_verts, was_off, start_off, sx, sy, scx, scy, cx, cy); + } else if (numc < 0) { // Compound shapes. int more = 1; uchar *comp = data + g + 10; @@ -976,9 +980,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) // More components ? more = flags & (1<<5); } - } else { - // numberOfCounters == 0, do nothing - } + } *pverts = verts; return num_verts; @@ -998,7 +1000,9 @@ typedef struct #define CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, nil, 0} -static void track_vertex(csctx *c, int32 x, int32 y) +static +void +track_vertex(csctx *c, int32 x, int32 y) { if (x > c->max_x || !c->started) c->max_x = x; if (y > c->max_y || !c->started) c->max_y = y; @@ -1007,7 +1011,9 @@ static void track_vertex(csctx *c, int32 x, int32 y) c->started = 1; } -static void csctx_v(csctx *c, uchar type, int32 x, int32 y, int32 cx, int32 cy, int32 cx1, int32 cy1) +static +void +csctx_v(csctx *c, uchar type, int32 x, int32 y, int32 cx, int32 cy, int32 cx1, int32 cy1) { if (c->bounds) { track_vertex(c, x, y); @@ -1023,13 +1029,17 @@ static void csctx_v(csctx *c, uchar type, int32 x, int32 y, int32 cx, int32 cy, c->num_verts++; } -static void csctx_close_shape(csctx *ctx) +static +void +csctx_close_shape(csctx *ctx) { if (ctx->first_x != ctx->x || ctx->first_y != ctx->y) csctx_v(ctx, font·Vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0); } -static void csctx_rmove_to(csctx *ctx, float dx, float dy) +static +void +csctx_rmove_to(csctx *ctx, float dx, float dy) { csctx_close_shape(ctx); ctx->first_x = ctx->x = ctx->x + dx; @@ -1037,14 +1047,18 @@ static void csctx_rmove_to(csctx *ctx, float dx, float dy) csctx_v(ctx, font·Vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0); } -static void csctx_rline_to(csctx *ctx, float dx, float dy) +static +void +csctx_rline_to(csctx *ctx, float dx, float dy) { ctx->x += dx; ctx->y += dy; csctx_v(ctx, font·Vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0); } -static void csctx_rccurve_to(csctx *ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3) +static +void +csctx_rccurve_to(csctx *ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3) { float cx1 = ctx->x + dx1; float cy1 = ctx->y + dy1; @@ -1055,7 +1069,9 @@ static void csctx_rccurve_to(csctx *ctx, float dx1, float dy1, float dx2, float csctx_v(ctx, font·Vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2); } -static Buffer get_subr(Buffer idx, int n) +static +Buffer +get_subr(Buffer idx, int n) { int count = cff_index_count(&idx); int bias = 107; @@ -1069,7 +1085,9 @@ static Buffer get_subr(Buffer idx, int n) return cff_index_get(idx, n); } -static Buffer cid_get_glyph_subrs(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; @@ -1107,8 +1125,6 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) Buffer subr_stack[10], subrs = info->subrs, b; float f; -#define STBTT__CSERR(s) (0) - // this currently ignores the initial width value, which isn't needed if we have hmtx b = cff_index_get(info->charstrings, glyph_index); while (b.cursor < b.size) { @@ -1134,22 +1150,34 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) case 0x15: // rmoveto in_header = 0; - if (sp < 2) return STBTT__CSERR("rmoveto stack"); + if (sp < 2) { + errorf("rmoveto stack"); + return 0; + } csctx_rmove_to(c, s[sp-2], s[sp-1]); break; case 0x04: // vmoveto in_header = 0; - if (sp < 1) return STBTT__CSERR("vmoveto stack"); + if (sp < 1) { + errorf("vmoveto stack"); + return 0; + } csctx_rmove_to(c, 0, s[sp-1]); break; case 0x16: // hmoveto in_header = 0; - if (sp < 1) return STBTT__CSERR("hmoveto stack"); + if (sp < 1) { + errorf("hmoveto stack"); + return 0; + } csctx_rmove_to(c, s[sp-1], 0); break; case 0x05: // rlineto - if (sp < 2) return STBTT__CSERR("rlineto stack"); + if (sp < 2) { + errorf("rlineto stack"); + return 0; + } for (; i + 1 < sp; i += 2) csctx_rline_to(c, s[i], s[i+1]); break; @@ -1158,10 +1186,16 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) // starting from a different place. case 0x07: // vlineto - if (sp < 1) return STBTT__CSERR("vlineto stack"); + if (sp < 1) { + errorf("vlineto stack"); + return 0; + } goto vlineto; case 0x06: // hlineto - if (sp < 1) return STBTT__CSERR("hlineto stack"); + if (sp < 1) { + errorf("hlineto stack"); + return 0; + } for (;;) { if (i >= sp) break; csctx_rline_to(c, s[i], 0); @@ -1174,10 +1208,16 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) break; case 0x1F: // hvcurveto - if (sp < 4) return STBTT__CSERR("hvcurveto stack"); + if (sp < 4) { + errorf("hvcurveto stack"); + return 0; + } goto hvcurveto; case 0x1E: // vhcurveto - if (sp < 4) return STBTT__CSERR("vhcurveto stack"); + if (sp < 4) { + errorf("vhcurveto stack"); + return 0; + } for (;;) { if (i + 3 >= sp) break; csctx_rccurve_to(c, 0, s[i], s[i+1], s[i+2], s[i+3], (sp - i == 5) ? s[i + 4] : 0.0f); @@ -1190,30 +1230,48 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) break; case 0x08: // rrcurveto - if (sp < 6) return STBTT__CSERR("rcurveline stack"); + if (sp < 6) { + errorf("rcurveline stack"); + return 0; + } for (; i + 5 < sp; i += 6) csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); break; case 0x18: // rcurveline - if (sp < 8) return STBTT__CSERR("rcurveline stack"); + if (sp < 8) { + errorf("rcurveline stack"); + return 0; + } for (; i + 5 < sp - 2; i += 6) csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); - if (i + 1 >= sp) return STBTT__CSERR("rcurveline stack"); + if (i + 1 >= sp) { + errorf("rcurveline stack"); + return 0; + } csctx_rline_to(c, s[i], s[i+1]); break; case 0x19: // rlinecurve - if (sp < 8) return STBTT__CSERR("rlinecurve stack"); + if (sp < 8) { + errorf("rlinecurve stack"); + return 0; + } for (; i + 1 < sp - 6; i += 2) csctx_rline_to(c, s[i], s[i+1]); - if (i + 5 >= sp) return STBTT__CSERR("rlinecurve stack"); + if (i + 5 >= sp) { + errorf("rlinecurve stack"); + return 0; + } csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); break; case 0x1A: // vvcurveto case 0x1B: // hhcurveto - if (sp < 4) return STBTT__CSERR("(vv|hh)curveto stack"); + if (sp < 4) { + errorf("(vv|hh)curveto stack"); + return 0; + } f = 0.0; if (sp & 1) { f = s[i]; i++; } for (; i + 3 < sp; i += 4) { @@ -1233,18 +1291,30 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) } // fallthrough case 0x1D: // callgsubr - if (sp < 1) return STBTT__CSERR("call(g|)subr stack"); + if (sp < 1) { + errorf("call(g|)subr stack"); + return 0; + } v = (int) s[--sp]; - if (subr_stack_height >= 10) return STBTT__CSERR("recursion limit"); + if (subr_stack_height >= 10) { + errorf("recursion limit"); + return 0; + } subr_stack[subr_stack_height++] = b; b = get_subr(b0 == 0x0A ? subrs : info->gsubrs, v); - if (b.size == 0) return STBTT__CSERR("subr not found"); + if (b.size == 0) { + errorf("subr not found"); + return 0; + } b.cursor = 0; clear_stack = 0; break; case 0x0B: // return - if (subr_stack_height <= 0) return STBTT__CSERR("return outside subr"); + if (subr_stack_height <= 0) { + errorf("return outside subr"); + return 0; + } b = subr_stack[--subr_stack_height]; clear_stack = 0; break; @@ -1261,7 +1331,10 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) // @TODO These "flex" implementations ignore the flex-depth and resolution, // and always draw beziers. case 0x22: // hflex - if (sp < 7) return STBTT__CSERR("hflex stack"); + if (sp < 7) { + errorf("hflex stack"); + return 0; + } dx1 = s[0]; dx2 = s[1]; dy2 = s[2]; @@ -1274,7 +1347,10 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) break; case 0x23: // flex - if (sp < 13) return STBTT__CSERR("flex stack"); + if (sp < 13) { + errorf("flex stack"); + return 0; + } dx1 = s[0]; dy1 = s[1]; dx2 = s[2]; @@ -1293,7 +1369,10 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) break; case 0x24: // hflex1 - if (sp < 9) return STBTT__CSERR("hflex1 stack"); + if (sp < 9) { + errorf("hflex1 stack"); + return 0; + } dx1 = s[0]; dy1 = s[1]; dx2 = s[2]; @@ -1308,7 +1387,10 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) break; case 0x25: // flex1 - if (sp < 11) return STBTT__CSERR("flex1 stack"); + if (sp < 11) { + errorf("flex1 stack"); + return 0; + } dx1 = s[0]; dy1 = s[1]; dx2 = s[2]; @@ -1331,13 +1413,16 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) break; default: - return STBTT__CSERR("unimplemented"); + panicf("unimplemented"); + return 0; } } break; default: - if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254)) - return STBTT__CSERR("reserved operator"); + if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254)) { + errorf("reserved operator"); + return 0; + } // push immediate if (b0 == 255) { @@ -1346,23 +1431,30 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) skip(&b, -1); f = (float)(short)cff_int(&b); } - if (sp >= 48) return STBTT__CSERR("push stack overflow"); + if (sp >= 48) { + errorf("push stack overflow"); + return 0; + } s[sp++] = f; clear_stack = 0; break; } if (clear_stack) sp = 0; } - return STBTT__CSERR("no endchar"); -#undef STBTT__CSERR + errorf("no endchar"); + return 0; + } -static int glyph_shape_t2(font·Info *info, int glyph_index, font·Vertex **pverts) +static +int +glyph_shape_t2(font·Info *info, int glyph_index, font·Vertex **pverts) { // runs the charstring twice, once to count and once to output (to avoid realloc) - csctx count_ctx = CSCTX_INIT(1); + csctx count_ctx = CSCTX_INIT(1); csctx output_ctx = CSCTX_INIT(0); + if (run_charstring(info, glyph_index, &count_ctx)) { *pverts = info->alloc(info->heap, count_ctx.num_verts, sizeof(font·Vertex)); output_ctx.pverts = *pverts; @@ -1371,6 +1463,7 @@ static int glyph_shape_t2(font·Info *info, int glyph_index, font·Vertex **pver return output_ctx.num_verts; } } + *pverts = nil; return 0; } @@ -1840,7 +1933,7 @@ font·code_svg(font·Info *info, int unicode_codepoint, char **svg) void font·glyph_bitmapbox_subpixel(font·Info *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1) { - int x0=0,y0=0,x1,y1; // =0 suppresses compiler warning + int x0=0,y0=0,x1,y1; if (!font·glyph_box(font, glyph, &x0,&y0,&x1,&y1)) { // e.g. space character if (ix0) *ix0 = 0; @@ -1865,13 +1958,13 @@ font·glyph_bitmapbox(font·Info *font, int glyph, float scale_x, float scale_y, void font·code_bitmapbox_subpixel(font·Info *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1) { - font·glyph_bitmapbox_subpixel(font, font·glyph_index(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1); + font·glyph_bitmapbox_subpixel(font, font·glyph_index(font,codepoint), scale_x, scale_y, shift_x, shift_y, ix0, iy0, ix1, iy1); } void font·code_bitmapbox(font·Info *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1) { - font·code_bitmapbox_subpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1); + font·code_bitmapbox_subpixel(font, codepoint, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1); } // ------------------------------------------------------------------------ -- cgit v1.2.1