From d1f071bf9aa394525745f0b4f582f97112ece26e Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Wed, 3 Jun 2020 12:32:29 -0700 Subject: checkin: before attempting hinting engine --- include/libfont.h | 21 +- sys/libfont/font.c | 793 ++++++++++++++++++++++++++++------------------------- sys/libfont/test.c | 40 ++- 3 files changed, 455 insertions(+), 399 deletions(-) diff --git a/include/libfont.h b/include/libfont.h index 860d8d4..42d160a 100644 --- a/include/libfont.h +++ b/include/libfont.h @@ -174,28 +174,27 @@ void font·freebitmap(font·Info *info, uchar *bitmap); uchar *font·code_makebitmap(font·Info *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff); /* - * the same as font·GetCodepoitnBitmap, but you can specify a subpixel - * shift for the character + * the same as font·code_makebitmap, but you can specify a subpixel shift for the character */ uchar *font·code_makebitmap_subpixel(font·Info *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff); /* * the same as font·codepointbitmap, but you pass in storage for the bitmap * in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap - * is clipped to out_w/out_h bytes. Call font·codepointbitmapbox to get the + * is clipped to out_w/out_h bytes. Call font·codepointbbox to get the * width and height and positioning info for it first. */ void font·code_fillbitmap(font·Info *info, uchar *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint); /* - * same as font·MakeCodepointBitmap, but you can specify a subpixel - * shift for the character + * same as font·code_fillbitmap, but you can specify a subpixel shift for the character */ void font·code_fillbitmap_subpixel(font·Info *info, uchar *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint); /* - * same as font·MakeCodepointBitmapSubpixel, but prefiltering - * is performed (see font·PackSetOversampling) + * same as font·fillbitmap_subpixel, but prefiltering is performed + * oversampling a font increases the quality by allowing higher-quality subpixel + * positioning, and is especially valuable at smaller text sizes. */ void font·code_fillbitmap_subpixel_prefilter(font·Info *info, uchar *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int codepoint); @@ -206,13 +205,13 @@ void font·code_fillbitmap_subpixel_prefilter(font·Info *info, uchar *output, i * (Note that the bitmap uses y-increases-down, but the shape uses * y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.) */ -void font·code_bitmapbox(font·Info *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); +void font·code_bbox(font·Info *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); /* * same as font·GetCodepointBitmapBox, but you can specify a subpixel * shift for the character */ -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); +void font·code_bbox_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); /* * the following functions are equivalent to the above functions, but operate @@ -223,8 +222,8 @@ uchar *font·glyph_makebitmap_subpixel(font·Info *info, float scale_x, float sc void font·glyph_fillbitmap(font·Info *info, uchar *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph); void font·glyph_fillbitmap_subpixel(font·Info *info, uchar *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph); void font·glyph_fillbitmap_subpixel_prefilter(font·Info *info, uchar *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int glyph); -void font·glyph_bitmapbox(font·Info *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); -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); +void font·glyph_bbox(font·Info *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); +void font·glyph_bbox_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); /* signed distance function (or field) rendering */ diff --git a/sys/libfont/font.c b/sys/libfont/font.c index 9424946..7de5d19 100644 --- a/sys/libfont/font.c +++ b/sys/libfont/font.c @@ -23,7 +23,7 @@ struct Buffer }; struct Edge { - float x0, y0, x1,y1; + float x0, y0, x1, y1; int invert; }; @@ -58,10 +58,21 @@ struct font·Info int fontstart; // offset of start of font int numglyphs; // number of glyphs, needed for range checking - - int loca,head,glyf,hhea,hmtx,kern,gpos,svg; // table locations as offset from start of .ttf + /* table locations as offset from start of ttf file */ + struct { + int loca; + int head; + int glyf; + int hhea; + int hmtx; + int kern; + int gpos; + int fpgm; + int svg; + int cvt; + }; int index_map; // a cmap mapping for our chosen character encoding - int indexToLocFormat; // format needed to map from glyph index to glyph + int index_fmt; // format needed to map from glyph index to glyph Buffer cff; // cff font data Buffer charstrings; // the charstring index @@ -269,9 +280,9 @@ cff_index_get(Buffer b, int i) * truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE */ -#define ttBYTE(p) (* (uchar *) (p)) -#define ttCHAR(p) (* (char *) (p)) -#define ttFixed(p) ttlong(p) +#define ttbyte(p) (* (uchar *) (p)) +#define ttchar(p) (* (char *) (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]; } @@ -315,37 +326,37 @@ find_table(uchar *data, uint32 offset, char *tag) } int -font·offsetfor(uchar *font_collection, int index) +font·offsetfor(uchar *collection, int index) { // if it's just a font, there's only one valid index - if (isfont(font_collection)) + if (isfont(collection)) return index == 0 ? 0 : -1; // check if it's a TTC - if (ttf·tag(font_collection, "ttcf")) { + if (ttf·tag(collection, "ttcf")) { // version 1? - if (ttulong(font_collection+4) == 0x00010000 || ttulong(font_collection+4) == 0x00020000) { - int32 n = ttlong(font_collection+8); + if (ttulong(collection+4) == 0x00010000 || ttulong(collection+4) == 0x00020000) { + int32 n = ttlong(collection+8); if (index >= n) return -1; - return ttulong(font_collection+12+index*4); + return ttulong(collection+12+index*4); } } return -1; } int -font·number(uchar *font_collection) +font·number(uchar *collection) { // if it's just a font, there's only one valid font - if (isfont(font_collection)) + if (isfont(collection)) return 1; // check if it's a TTC - if (ttf·tag(font_collection, "ttcf")) { + if (ttf·tag(collection, "ttcf")) { // version 1? - if (ttulong(font_collection+4) == 0x00010000 || ttulong(font_collection+4) == 0x00020000) { - return ttlong(font_collection+8); + if (ttulong(collection+4) == 0x00010000 || ttulong(collection+4) == 0x00020000) { + return ttlong(collection+8); } } return 0; @@ -405,6 +416,11 @@ init(font·Info *info, uchar *data, int offset) 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 + info->fpgm = find_table(data, offset, "fpgm"); // not required (execute once per load) + info->cvt = find_table(data, offset, "cvt"); // not required (execute once per size) + + printf("cvt found at %d\n", info->cvt); + printf("fpgm found at %d\n", info->cvt); if (!cmap || !info->head || !info->hhea || !info->hmtx) return 1; @@ -496,7 +512,7 @@ init(font·Info *info, uchar *data, int offset) return 1; } - info->indexToLocFormat = ttushort(data+info->head + 50); + info->index_fmt = ttushort(data+info->head + 50); return 0; } @@ -561,12 +577,16 @@ font·glyph_index(font·Info *info, int unicode_codepoint) data = info->data; index_map = info->index_map; + /* + * useful discussion of table formats: + * https://docs.microsoft.com/en-us/typography/opentype/spec/cmap + */ fmt = ttushort(data + index_map + 0); switch (fmt) { case 0: /* apple byte encoding */ v.bytes = ttushort(data + index_map + 2); if (unicode_codepoint < v.bytes-6) - return ttBYTE(data + index_map + 6 + unicode_codepoint); + return ttbyte(data + index_map + 6 + unicode_codepoint); return 0; case 6: @@ -678,22 +698,22 @@ static int glyph_offset(font·Info *info, int glyph_index) { - int g1,g2; + int gb, ge; assert(!info->cff.size); if (glyph_index >= info->numglyphs) return -1; // glyph index out of range - if (info->indexToLocFormat >= 2) return -1; // unknown index->glyph map format + if (info->index_fmt >= 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; + if (info->index_fmt == 0) { + gb = info->glyf + 2*ttushort(info->data + info->loca + 2*glyph_index); + ge = info->glyf + 2*ttushort(info->data + info->loca + 2*glyph_index + 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); + gb = info->glyf + 1*ttulong (info->data + info->loca + 4*glyph_index); + ge = info->glyf + 1*ttulong (info->data + info->loca + 4*glyph_index + 4); } - return (g1==g2) ? -1 : g1; // if length is 0, return -1 + return (gb==ge) ? -1 : gb; // 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); @@ -701,18 +721,21 @@ static int glyph_info_t2(font·Info *info, int glyph_index, int *x0, int *y0, in int font·glyph_box(font·Info *info, int glyph_index, int *x0, int *y0, int *x1, int *y1) { - if (info->cff.size) { - glyph_info_t2(info, glyph_index, x0, y0, x1, y1); - } else { - int g = glyph_offset(info, glyph_index); - if (g < 0) return 0; + int g; + if (info->cff.size) { + glyph_info_t2(info, glyph_index, x0, y0, x1, y1); + } else { + 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; + return 1; } int @@ -740,20 +763,20 @@ font·glyph_empty(font·Info *info, int glyph_index) static int -close_shape(font·Vertex *verts, int num_verts, int was_off, int start_off, +close_shape(font·Vertex *verts, int numv, int was_off, int start_off, int32 sx, int32 sy, int32 scx, int32 scy, int32 cx, int32 cy) { if (start_off) { if (was_off) - setvertex(&verts[num_verts++], font·Vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy); - setvertex(&verts[num_verts++], font·Vcurve, sx,sy,scx,scy); + setvertex(&verts[numv++], font·Vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy); + setvertex(&verts[numv++], font·Vcurve, sx,sy,scx,scy); } else { if (was_off) - setvertex(&verts[num_verts++], font·Vcurve,sx,sy,cx,cy); + setvertex(&verts[numv++], font·Vcurve,sx,sy,cx,cy); else - setvertex(&verts[num_verts++], font·Vline,sx,sy,0,0); + setvertex(&verts[numv++], font·Vline,sx,sy,0,0); } - return num_verts; + return numv; } static @@ -764,11 +787,12 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) uchar *contourend; uchar *data = info->data; font·Vertex *verts = nil; - int num_verts = 0; - int g = glyph_offset(info, glyph_index); + int numv = 0; + int g = glyph_offset(info, glyph_index); *pverts = nil; + /* glyph not found */ if (g < 0) return 0; @@ -776,13 +800,14 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) if (numc > 0) { uchar flags=0, flagcount; - int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0; + int32 nins, i,j=0,m,n, next_move, was_off=0, off, start_off=0; int32 x,y,cx,cy,sx,sy,scx,scy; uchar *points; - contourend = (data + g + 10); - ins = ttushort(data + g + 10 + numc * 2); - points = data + g + 10 + numc * 2 + 2 + ins; + contourend = data + g + 10; + nins = ttushort(data + g + 10 + numc * 2); + points = data + g + 10 + 2*numc + 2 + nins; + printf("number of instructions %d\n", nins); n = 1+ttushort(contourend + numc*2-2); @@ -802,7 +827,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) // first load flags - for (i=0; i < n; ++i) { + for (i = 0; i < n; ++i) { if (flagcount == 0) { flags = *points++; if (flags & 8) @@ -814,7 +839,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) // now load x coordinates x=0; - for (i=0; i < n; ++i) { + for (i = 0; i < n; ++i) { flags = verts[off+i].type; if (flags & 2) { short dx = *points++; @@ -829,8 +854,8 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) } // now load y coordinates - y=0; - for (i=0; i < n; ++i) { + y = 0; + for (i = 0; i < n; ++i) { flags = verts[off+i].type; if (flags & 4) { short dy = *points++; @@ -845,7 +870,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) } // now convert them to our format - num_verts=0; + numv = 0; sx = sy = cx = cy = scx = scy = 0; for (i=0; i < n; ++i) { flags = verts[off+i].type; @@ -854,7 +879,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) if (next_move == i) { if (i != 0) - num_verts = close_shape(verts, num_verts, was_off, start_off, sx,sy,scx,scy,cx,cy); + numv = close_shape(verts, numv, was_off, start_off, sx,sy,scx,scy,cx,cy); // now start the new one start_off = !(flags & 1); @@ -877,32 +902,32 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) sx = x; sy = y; } - setvertex(&verts[num_verts++], font·Vmove,sx,sy,0,0); + setvertex(&verts[numv++], font·Vmove,sx,sy,0,0); was_off = 0; next_move = 1 + ttushort(contourend+j*2); ++j; } else { if (!(flags & 1)) { // if it's a curve if (was_off) // two off-curve control points in a row means interpolate an on-curve midpoint - setvertex(&verts[num_verts++], font·Vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy); + setvertex(&verts[numv++], font·Vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy); cx = x; cy = y; was_off = 1; } else { if (was_off) - setvertex(&verts[num_verts++], font·Vcurve, x,y, cx, cy); + setvertex(&verts[numv++], font·Vcurve, x,y, cx, cy); else - setvertex(&verts[num_verts++], font·Vline, x,y,0,0); + setvertex(&verts[numv++], font·Vline, x,y,0,0); was_off = 0; } } } - num_verts = close_shape(verts, num_verts, was_off, start_off, sx, sy, scx, scy, cx, cy); + numv = close_shape(verts, numv, was_off, start_off, sx, sy, scx, scy, cx, cy); } else if (numc < 0) { // Compound shapes. int more = 1; uchar *comp = data + g + 10; - num_verts = 0; + numv = 0; verts = 0; while (more) { ushort flags, gidx; @@ -918,8 +943,8 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) 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; + mtx[4] = ttchar(comp); comp+=1; + mtx[5] = ttchar(comp); comp+=1; } } else { @@ -959,23 +984,23 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) v->cy = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5])); } // Append verts. - tmp = info->alloc(info->heap, num_verts+comp_num_verts, sizeof(font·Vertex)); + tmp = info->alloc(info->heap, numv+comp_num_verts, sizeof(font·Vertex)); if (!tmp) { if (verts) info->free(info->heap, verts); if (comp_verts) info->free(info->heap, comp_verts); return 0; } - if (num_verts > 0) - memcpy(tmp, verts, num_verts*sizeof(font·Vertex)); + if (numv > 0) + memcpy(tmp, verts, numv*sizeof(font·Vertex)); - memcpy(tmp+num_verts, comp_verts, comp_num_verts*sizeof(font·Vertex)); + memcpy(tmp+numv, comp_verts, comp_num_verts*sizeof(font·Vertex)); if (verts) info->free(info->heap, verts); verts = tmp; info->free(info->heap, comp_verts); - num_verts += comp_num_verts; + numv += comp_num_verts; } // More components ? more = flags & (1<<5); @@ -983,7 +1008,7 @@ glyph_shape_tt(font·Info *info, int glyph_index, font·Vertex **pverts) } *pverts = verts; - return num_verts; + return numv; } typedef struct @@ -995,7 +1020,7 @@ typedef struct int32 min_x, max_x, min_y, max_y; font·Vertex *pverts; - int num_verts; + int numv; } csctx; #define CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, nil, 0} @@ -1011,6 +1036,8 @@ track_vertex(csctx *c, int32 x, int32 y) c->started = 1; } +/* decode curve sequences */ + static void csctx_v(csctx *c, uchar type, int32 x, int32 y, int32 cx, int32 cy, int32 cx1, int32 cy1) @@ -1022,11 +1049,11 @@ csctx_v(csctx *c, uchar type, int32 x, int32 y, int32 cx, int32 cy, int32 cx1, i track_vertex(c, cx1, cy1); } } else { - setvertex(&c->pverts[c->num_verts], type, x, y, cx, cy); - c->pverts[c->num_verts].cx1 = (short) cx1; - c->pverts[c->num_verts].cy1 = (short) cy1; + setvertex(&c->pverts[c->numv], type, x, y, cx, cy); + c->pverts[c->numv].cx1 = (short) cx1; + c->pverts[c->numv].cy1 = (short) cy1; } - c->num_verts++; + c->numv++; } static @@ -1064,6 +1091,7 @@ csctx_rccurve_to(csctx *ctx, float dx1, float dy1, float dx2, float dy2, float d float cy1 = ctx->y + dy1; float cx2 = cx1 + dx2; float cy2 = cy1 + dy2; + ctx->x = cx2 + dx3; ctx->y = cy2 + dy3; csctx_v(ctx, font·Vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2); @@ -1075,13 +1103,16 @@ get_subr(Buffer idx, int n) { int count = cff_index_count(&idx); int bias = 107; + if (count >= 33900) bias = 32768; else if (count >= 1240) bias = 1131; n += bias; + if (n < 0 || n >= count) return makebuffer(nil, 0); + return cff_index_get(idx, n); } @@ -1156,6 +1187,7 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) } csctx_rmove_to(c, s[sp-2], s[sp-1]); break; + case 0x04: // vmoveto in_header = 0; if (sp < 1) { @@ -1164,6 +1196,7 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) } csctx_rmove_to(c, 0, s[sp-1]); break; + case 0x16: // hmoveto in_header = 0; if (sp < 1) { @@ -1289,13 +1322,13 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) subrs = cid_get_glyph_subrs(info, glyph_index); has_subrs = 1; } - // fallthrough + /* fallthrough */ case 0x1D: // callgsubr if (sp < 1) { errorf("call(g|)subr stack"); return 0; } - v = (int) s[--sp]; + v = (int)s[--sp]; if (subr_stack_height >= 10) { errorf("recursion limit"); return 0; @@ -1342,7 +1375,7 @@ run_charstring(font·Info *info, int glyph_index, csctx *c) dx4 = s[4]; dx5 = s[5]; dx6 = s[6]; - csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0); + csctx_rccurve_to(c, dx1, 0, dx2, +dy2, dx3, 0); csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0); break; @@ -1456,11 +1489,11 @@ glyph_shape_t2(font·Info *info, int glyph_index, font·Vertex **pverts) 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)); + *pverts = info->alloc(info->heap, count_ctx.numv, sizeof(font·Vertex)); output_ctx.pverts = *pverts; if (run_charstring(info, glyph_index, &output_ctx)) { - assert(output_ctx.num_verts == count_ctx.num_verts); - return output_ctx.num_verts; + assert(output_ctx.numv == count_ctx.numv); + return output_ctx.numv; } } @@ -1480,7 +1513,7 @@ glyph_info_t2(font·Info *info, int glyph_index, int *x0, int *y0, int *x1, int if (x1) *x1 = r ? c.max_x : 0; if (y1) *y1 = r ? c.max_y : 0; - return r ? c.num_verts : 0; + return r ? c.numv : 0; } int @@ -1836,23 +1869,25 @@ font·glyph_kernadvance(font·Info *info, int g1, int g2) int font·code_kernadvance(font·Info *info, int ch1, int ch2) { - if (!info->kern && !info->gpos) // if no kerning table, don't waste time looking up both codepoint->glyphs - return 0; - return font·glyph_kernadvance(info, font·glyph_index(info,ch1), font·glyph_index(info,ch2)); + // if no kerning table, don't waste time looking up both codepoint->glyphs + if (!info->kern && !info->gpos) + return 0; + + return font·glyph_kernadvance(info, font·glyph_index(info,ch1), font·glyph_index(info,ch2)); } void font·code_hmetrics(font·Info *info, int codepoint, int *advanceWidth, int *lsb) { - font·glyph_hmetrics(info, font·glyph_index(info,codepoint), advanceWidth, lsb); + font·glyph_hmetrics(info, font·glyph_index(info, codepoint), advanceWidth, lsb); } void -font·vmetrics(font·Info *info, int *ascent, int *descent, int *lineGap) +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 @@ -1931,7 +1966,7 @@ font·code_svg(font·Info *info, int unicode_codepoint, char **svg) // antialiasing software rasterizer 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) +font·glyph_bbox_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; if (!font·glyph_box(font, glyph, &x0,&y0,&x1,&y1)) { @@ -1950,21 +1985,21 @@ font·glyph_bitmapbox_subpixel(font·Info *font, int glyph, float scale_x, float } void -font·glyph_bitmapbox(font·Info *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1) +font·glyph_bbox(font·Info *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1) { - font·glyph_bitmapbox_subpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1); + font·glyph_bbox_subpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1); } 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·code_bbox_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_bbox_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_bbox(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_bbox_subpixel(font, codepoint, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1); } // ------------------------------------------------------------------------ @@ -2049,24 +2084,31 @@ new_active(hheap *hh, Edge *e, int off_x, float start_point, mem·Allocator mem, return z; } -// the edge passed in here does not cross the vertical line at x or the vertical line at x+1 +// assumes: the edge passed in here does not cross the vertical line at x or the vertical line at x+1 // (i.e. it has already been clipped to those) static void handle_clipped_edge(float *scanline, int x, ActiveEdge *e, float x0, float y0, float x1, float y1) { - if (y0 == y1) return; + if (y0 == y1) + return; + assert(y0 < y1); assert(e->sy <= e->ey); - if (y0 > e->ey) return; - if (y1 < e->sy) return; + + if (y0 > e->ey) + return; + if (y1 < e->sy) + return; + if (y0 < e->sy) { x0 += (x1-x0) * (e->sy - y0) / (y1-y0); - y0 = e->sy; + y0 = e->sy; } + if (y1 > e->ey) { x1 += (x1-x0) * (e->ey - y1) / (y1-y0); - y1 = e->ey; + y1 = e->ey; } if (x0 == x) @@ -2094,163 +2136,158 @@ static void fill_active_edges_new(float *scanline, float *scanline_fill, int len, ActiveEdge *e, float y_top) { - float y_bottom = y_top+1; - - while (e) { - // brute force every pixel - - // compute intersection points with top & bottom - assert(e->ey >= y_top); - - if (e->fdx == 0) { - float x0 = e->fx; - if (x0 < len) { - if (x0 >= 0) { - handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom); - handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom); - } else { - handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom); - } - } - } else { - float x0 = e->fx; - float dx = e->fdx; - float xb = x0 + dx; - float x_top, x_bottom; - float sy0,sy1; - float dy = e->fdy; - assert(e->sy <= y_bottom && e->ey >= y_top); - - // compute endpoints of line segment clipped to this scanline (if the - // line segment starts on this scanline. x0 is the intersection of the - // line with y_top, but that may be off the line segment. - if (e->sy > y_top) { - x_top = x0 + dx * (e->sy - y_top); - sy0 = e->sy; - } else { - x_top = x0; - sy0 = y_top; - } - if (e->ey < y_bottom) { - x_bottom = x0 + dx * (e->ey - y_top); - sy1 = e->ey; - } else { - x_bottom = xb; - sy1 = y_bottom; - } - - if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) { - // from here on, we don't have to range check x values - - if ((int) x_top == (int) x_bottom) { - float height; - // simple case, only spans one pixel - int x = (int) x_top; - height = sy1 - sy0; - assert(x >= 0 && x < len); - scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height; - scanline_fill[x] += e->direction * height; // everything right of this pixel is filled - } else { - int x,x1,x2; - float y_crossing, step, sign, area; - // covers 2+ pixels - if (x_top > x_bottom) { - // flip scanline vertically; signed area is the same - float t; - sy0 = y_bottom - (sy0 - y_top); - sy1 = y_bottom - (sy1 - y_top); - t = sy0, sy0 = sy1, sy1 = t; - t = x_bottom, x_bottom = x_top, x_top = t; - dx = -dx; - dy = -dy; - t = x0, x0 = xb, xb = t; - } - - x1 = (int) x_top; - x2 = (int) x_bottom; - // compute intersection with y axis at x1+1 - y_crossing = (x1+1 - x0) * dy + y_top; - - sign = e->direction; - // area of the rectangle covered from y0..y_crossing - area = sign * (y_crossing-sy0); - // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) - scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2); - - step = sign * dy; - for (x = x1+1; x < x2; ++x) { - scanline[x] += area + step/2; - area += step; - } - y_crossing += dy * (x2 - (x1+1)); - - assert(fabs(area) <= 1.01f); - - scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing); - - scanline_fill[x2] += sign * (sy1-sy0); - } - } else { - // if edge goes outside of box we're drawing, we require - // clipping logic. since this does not match the intended use - // of this library, we use a different, very slow brute - // force implementation - int x; - for (x=0; x < len; ++x) { - // cases: - // - // there can be up to two intersections with the pixel. any intersection - // with left or right edges can be handled by splitting into two (or three) - // regions. intersections with top & bottom do not necessitate case-wise logic. - // - // the old way of doing this found the intersections with the left & right edges, - // then used some simple logic to produce up to three segments in sorted order - // from top-to-bottom. however, this had a problem: if an x edge was epsilon - // across the x border, then the corresponding y position might not be distinct - // from the other y segment, and it might ignored as an empty segment. to avoid - // that, we need to explicitly produce segments based on x positions. - - // rename variables to clearly-defined pairs - float y0 = y_top; - float x1 = (float) (x); - float x2 = (float) (x+1); - float x3 = xb; - float y3 = y_bottom; - - // x = e->x + e->dx * (y-y_top) - // (y-y_top) = (x - e->x) / e->dx - // y = (x - e->x) / e->dx + y_top - float y1 = (x - x0) / dx + y_top; - float y2 = (x+1 - x0) / dx + y_top; - - if (x0 < x1 && x3 > x2) { // three segments descending down-right - handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); - handle_clipped_edge(scanline,x,e, x1,y1, x2,y2); - handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); - } else if (x3 < x1 && x0 > x2) { // three segments descending down-left - handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); - handle_clipped_edge(scanline,x,e, x2,y2, x1,y1); - handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); - } else if (x0 < x1 && x3 > x1) { // two segments across x, down-right - handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); - handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); - } else if (x3 < x1 && x0 > x1) { // two segments across x, down-left - handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); - handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); - } else if (x0 < x2 && x3 > x2) { // two segments across x+1, down-right - handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); - handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); - } else if (x3 < x2 && x0 > x2) { // two segments across x+1, down-left - handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); - handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); - } else { // one segment - handle_clipped_edge(scanline,x,e, x0,y0, x3,y3); - } - } - } - } - e = e->next; - } + float y_bot = y_top+1; + + while (e) { + // brute force every pixel + // compute intersection points with top & bottom + assert(e->ey >= y_top); + + if (e->fdx == 0) { + float x0 = e->fx; + if (x0 < len) { + if (x0 >= 0) { + handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bot); + handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bot); + } else { + handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bot); + } + } + } else { + float x0 = e->fx; + float dx = e->fdx; + float xb = x0 + dx; + float x_top, x_bot; + float sy0,sy1; + float dy = e->fdy; + assert(e->sy <= y_bot && e->ey >= y_top); + + // compute endpoints of line segment clipped to this scanline (if the + // line segment starts on this scanline. x0 is the intersection of the + // line with y_top, but that may be off the line segment. + if (e->sy > y_top) { + x_top = x0 + dx * (e->sy - y_top); + sy0 = e->sy; + } else { + x_top = x0; + sy0 = y_top; + } + if (e->ey < y_bot) { + x_bot = x0 + dx * (e->ey - y_top); + sy1 = e->ey; + } else { + x_bot = xb; + sy1 = y_bot; + } + + if (x_top >= 0 && x_bot >= 0 && x_top < len && x_bot < len) { + // from here on, we don't have to range check x values + + if ((int)x_top == (int)x_bot) { + float height; + // simple case, only spans one pixel + int x = (int) x_top; + height = sy1 - sy0; + assert(x >= 0 && x < len); + scanline[x] += e->direction * (1-((x_top - x) + (x_bot-x))/2) * height; + scanline_fill[x] += e->direction * height; // everything right of this pixel is filled + } else { + int x,x1,x2; + float y_crossing, step, sign, area; + // covers 2+ pixels + if (x_top > x_bot) { + // flip scanline vertically; signed area is the same + float t; + sy0 = y_bot - (sy0 - y_top); + sy1 = y_bot - (sy1 - y_top); + t = sy0, sy0 = sy1, sy1 = t; + t = x_bot, x_bot = x_top, x_top = t; + dx = -dx; + dy = -dy; + t = x0, x0 = xb, xb = t; + } + + x1 = (int)x_top; + x2 = (int)x_bot; + // compute intersection with y axis at x1+1 + y_crossing = (x1+1 - x0) * dy + y_top; + + sign = e->direction; + // area of the rectangle covered from y0..y_crossing + area = sign * (y_crossing-sy0); + // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) + scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2); + + step = sign * dy; + for (x = x1+1; x < x2; ++x) { + scanline[x] += area + step/2; + area += step; + } + y_crossing += dy * (x2 - (x1+1)); + + assert(fabs(area) <= 1.01f); + + scanline[x2] += area + sign * (1-((x2-x2)+(x_bot-x2))/2) * (sy1-y_crossing); + scanline_fill[x2] += sign * (sy1-sy0); + } + } else { + // if edge goes outside of box we're drawing, we require + // clipping logic. since this does not match the intended use + // of this library, we use a different, very slow brute + // force implementation + int x; + for (x=0; x < len; ++x) { + // cases: + // + // there can be up to two intersections with the pixel. any intersection + // with left or right edges can be handled by splitting into two (or three) + // regions. intersections with top & bottom do not necessitate case-wise logic. + // + // the old way of doing this found the intersections with the left & right edges, + // then used some simple logic to produce up to three segments in sorted order + // from top-to-bottom. however, this had a problem: if an x edge was epsilon + // across the x border, then the corresponding y position might not be distinct + // from the other y segment, and it might ignored as an empty segment. to avoid + // that, we need to explicitly produce segments based on x positions. + + // rename variables to clearly-defined pairs + float y0 = y_top; + float x1 = (float)(x); + float x2 = (float)(x+1); + float x3 = xb; + float y3 = y_bot; + + float y1 = (x+0 - x0) / dx + y_top; + float y2 = (x+1 - x0) / dx + y_top; + + if (x0 < x1 && x3 > x2) { // three segments descending down-right + handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); + handle_clipped_edge(scanline,x,e, x1,y1, x2,y2); + handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); + } else if (x3 < x1 && x0 > x2) { // three segments descending down-left + handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); + handle_clipped_edge(scanline,x,e, x2,y2, x1,y1); + handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); + } else if (x0 < x1 && x3 > x1) { // two segments across x, down-right + handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); + handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); + } else if (x3 < x1 && x0 > x1) { // two segments across x, down-left + handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); + handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); + } else if (x0 < x2 && x3 > x2) { // two segments across x+1, down-right + handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); + handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); + } else if (x3 < x2 && x0 > x2) { // two segments across x+1, down-left + handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); + handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); + } else { // one segment + handle_clipped_edge(scanline,x,e, x0,y0, x3,y3); + } + } + } + } + e = e->next; + } } // directly AA rasterize edges w/o supersampling @@ -2271,15 +2308,15 @@ rasterize_sorted_edges(font·Bitmap *result, Edge *e, int n, int vsubsample, int scanline2 = scanline + result->w; y = off_y; - e[n].y0 = (float) (off_y + result->h) + 1; + e[n].y0 = (float)(off_y + result->h) + 1; while (j < result->h) { // find center of pixel for this scanline - float scan_y_top = y + 0.0f; - float scan_y_bottom = y + 1.0f; + float scan_y_top = y - 0.0f; + float scan_y_bot = y + 1.0f; ActiveEdge **step = &active; - memset(scanline , 0, result->w*sizeof(scanline[0])); + memset(scanline , 0, (result->w+0)*sizeof(scanline[0])); memset(scanline2, 0, (result->w+1)*sizeof(scanline[0])); // update all active edges; @@ -2291,13 +2328,12 @@ rasterize_sorted_edges(font·Bitmap *result, Edge *e, int n, int vsubsample, int assert(z->direction); z->direction = 0; hheap_free(&hh, z); - } else { + } else step = &((*step)->next); // advance through list - } } // insert all edges that start before the bottom of this scanline - while (e->y0 <= scan_y_bottom) { + while (e->y0 <= scan_y_bot) { if (e->y0 != e->y1) { ActiveEdge *z = new_active(&hh, e, off_x, scan_y_top, mem, heap); if (z != nil) { @@ -2327,10 +2363,11 @@ rasterize_sorted_edges(font·Bitmap *result, Edge *e, int n, int vsubsample, int int m; sum += scanline2[i]; k = scanline[i] + sum; - k = (float) fabs(k)*255 + 0.5f; - m = (int) k; - if (m > 255) m = 255; - result->pixels[j*result->stride + i] = (uchar) m; + k = (float)fabs(k)*255 + 0.5f; + m = (int)k; + if (m > 255) + m = 255; + result->pixels[j*result->stride + i] = (uchar)m; } } // advance all the edges @@ -2357,20 +2394,22 @@ static void sort_edges_ins_sort(Edge *p, int n) { - int i,j; - for (i=1; i < n; ++i) { - Edge t = p[i], *a = &t; - j = i; - while (j > 0) { - Edge *b = &p[j-1]; - int c = CMP_Y0(a,b); - if (!c) break; - p[j] = p[j-1]; - --j; - } - if (i != j) - p[j] = t; - } + int i,j; + for (i=1; i < n; ++i) { + Edge t = p[i], *a = &t; + j = i; + while (j > 0) { + Edge *b = &p[j-1]; + int c = CMP_Y0(a,b); + if (!c) + break; + + p[j] = p[j-1]; + --j; + } + if (i != j) + p[j] = t; + } } static @@ -2450,7 +2489,7 @@ sort_edges(Edge *p, int n) static void -rasterize_points(font·Bitmap *result, Point *pts, int *wcount, int windings, float scale_x, float scale_y, float shift_x, float shift_y, int off_x, int off_y, int invert, mem·Allocator mem, void *heap) +rasterize_points(font·Bitmap *result, Point *pts, int *wcount, int nwinding, float scale_x, float scale_y, float shift_x, float shift_y, int off_x, int off_y, int invert, mem·Allocator mem, void *heap) { float y_scale_inv = invert ? -scale_y : scale_y; Edge *e; @@ -2458,9 +2497,9 @@ rasterize_points(font·Bitmap *result, Point *pts, int *wcount, int windings, fl int vsubsample = 1; // vsubsample should divide 255 evenly; otherwise we won't reach full opacity - // now we have to blow out the windings into explicit edge lists + // now we have to blow out the nwinding into explicit edge lists n = 0; - for (i=0; i < windings; ++i) + for (i = 0; i < nwinding; ++i) n += wcount[i]; e = mem.alloc(heap, n+1, sizeof(*e)); // add an extra one as a sentinel @@ -2468,13 +2507,13 @@ rasterize_points(font·Bitmap *result, Point *pts, int *wcount, int windings, fl return; n = 0; - m=0; - for (i=0; i < windings; ++i) { + m = 0; + for (i = 0; i < nwinding; ++i) { Point *p = pts + m; m += wcount[i]; - j = wcount[i]-1; + j = wcount[i]-1; for (k=0; k < wcount[i]; j=k++) { - int a=k,b=j; + int a = k, b = j; // skip the Edge if horizontal if (p[j].y == p[k].y) continue; @@ -2515,7 +2554,7 @@ add_point(Point *points, int n, float x, float y) // tessellate until threshold p is happy... @TODO warped to compensate for non-linear stretching static int -tesselate_curve(Point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float objspace_flatness_squared, int n) +tesselate_curve(Point *points, int *npts, float x0, float y0, float x1, float y1, float x2, float y2, float objspace_flatness_squared, int n) { // midpoint float mx = (x0 + 2*x1 + x2)/4; @@ -2523,14 +2562,15 @@ tesselate_curve(Point *points, int *num_points, float x0, float y0, float x1, fl // versus directly drawn line float dx = (x0+x2)/2 - mx; float dy = (y0+y2)/2 - my; + if (n > 16) // 65536 segments on one curve better be enough! return 1; if (dx*dx+dy*dy > objspace_flatness_squared) { // half-pixel error allowed... need to be smaller if AA - tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1); - tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1); + tesselate_curve(points, npts, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1); + tesselate_curve(points, npts, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1); } else { - add_point(points, *num_points,x2,y2); - *num_points = *num_points+1; + add_point(points, *npts,x2,y2); + *npts = *npts+1; } return 1; } @@ -2548,7 +2588,7 @@ tesselate_cubic(Point *points, int *num_points, float x0, float y0, float x1, fl float dy2 = y3-y2; float dx = x3-x0; float dy = y3-y0; - float longlen = (float) (sqrt(dx0*dx0+dy0*dy0)+sqrt(dx1*dx1+dy1*dy1)+sqrt(dx2*dx2+dy2*dy2)); + float longlen = (float)(sqrt(dx0*dx0+dy0*dy0)+sqrt(dx1*dx1+dy1*dy1)+sqrt(dx2*dx2+dy2*dy2)); float shortlen = (float) sqrt(dx*dx+dy*dy); float flatness_squared = longlen*longlen-shortlen*shortlen; @@ -2582,7 +2622,7 @@ tesselate_cubic(Point *points, int *num_points, float x0, float y0, float x1, fl // returns number of contours static Point * -flatten(font·Vertex *verts, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, mem·Allocator mem, void *heap) +flatten(font·Vertex *verts, int numv, float objspace_flatness, int **contour_lengths, int *num_contours, mem·Allocator mem, void *heap) { Point *points=0; int num_points=0; @@ -2591,7 +2631,7 @@ flatten(font·Vertex *verts, int num_verts, float objspace_flatness, int **conto int i,n=0,start=0, pass; // count how many "moves" there are to get the contour count - for (i=0; i < num_verts; ++i) + for (i=0; i < numv; ++i) if (verts[i].type == font·Vmove) ++n; @@ -2612,11 +2652,11 @@ flatten(font·Vertex *verts, int num_verts, float objspace_flatness, int **conto if (pass == 1) { points = mem.alloc(heap, num_points, sizeof(points[0])); if (!points) - goto error; + goto nomemory; } num_points = 0; - n= -1; - for (i=0; i < num_verts; ++i) { + n = -1; + for (i=0; i < numv; ++i) { switch (verts[i].type) { case font·Vmove: // start the next contour @@ -2626,7 +2666,7 @@ flatten(font·Vertex *verts, int num_verts, float objspace_flatness, int **conto start = num_points; x = verts[i].x, y = verts[i].y; - add_point(points, num_points++, x,y); + add_point(points, num_points++, x, y); break; case font·Vline: x = verts[i].x, y = verts[i].y; @@ -2653,7 +2693,7 @@ flatten(font·Vertex *verts, int num_verts, float objspace_flatness, int **conto } return points; -error: +nomemory: mem.free(heap, points); mem.free(heap, *contour_lengths); *contour_lengths = nil; @@ -2663,16 +2703,17 @@ error: static void -rasterize(font·Bitmap *result, float flatness_in_pixels, font·Vertex *verts, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, mem·Allocator mal, void *heap) +rasterize(font·Bitmap *result, float flatness_in_pixels, font·Vertex *verts, int numv, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, mem·Allocator mal, void *heap) { - float scale = (scale_x > scale_y) ? scale_y : scale_x; - int winding_count = 0; - int *winding_lengths = nil; - Point *windings = flatten(verts, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, mal, heap); + float scale = MIN(scale_x, scale_y); + int winding_count = 0; + int *winding_lens = nil; + + Point *windings = flatten(verts, numv, flatness_in_pixels / scale, &winding_lens, &winding_count, mal, heap); if (windings) { - rasterize_points(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, mal, heap); + rasterize_points(result, windings, winding_lens, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, mal, heap); mal.free(heap, windings); - mal.free(heap, winding_lengths); + mal.free(heap, winding_lens); } } @@ -2688,7 +2729,7 @@ font·glyph_makebitmap_subpixel(font·Info *info, float scale_x, float scale_y, int ix0,iy0,ix1,iy1; font·Bitmap gbm; font·Vertex *verts; - int num_verts = font·glyph_shape(info, glyph, &verts); + int numv = font·glyph_shape(info, glyph, &verts); if (scale_x == 0) scale_x = scale_y; if (scale_y == 0) { @@ -2699,7 +2740,7 @@ font·glyph_makebitmap_subpixel(font·Info *info, float scale_x, float scale_y, scale_y = scale_x; } - font·glyph_bitmapbox_subpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1); + font·glyph_bbox_subpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1); // now we get the size gbm.w = (ix1 - ix0); @@ -2716,7 +2757,7 @@ font·glyph_makebitmap_subpixel(font·Info *info, float scale_x, float scale_y, if (gbm.pixels) { gbm.stride = gbm.w; - rasterize(&gbm, 0.35f, verts, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->mal, info->heap); + rasterize(&gbm, 0.35f, verts, numv, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->mal, info->heap); } } info->free(info->heap, verts); @@ -2732,21 +2773,22 @@ font·glyph_makebitmap(font·Info *info, float scale_x, float scale_y, int glyph void font·glyph_fillbitmap_subpixel(font·Info *info, uchar *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph) { - int ix0,iy0; - font·Vertex *verts; - int num_verts = font·glyph_shape(info, glyph, &verts); - font·Bitmap gbm; - - font·glyph_bitmapbox_subpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0); - gbm.pixels = output; - gbm.w = out_w; - gbm.h = out_h; - gbm.stride = out_stride; - - if (gbm.w && gbm.h) - rasterize(&gbm, 0.35f, verts, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->mal, info->heap); - - info->free(info->heap, verts); + int ix0, iy0, nv; + font·Vertex *verts; + font·Bitmap gbm; + + nv = font·glyph_shape(info, glyph, &verts); + + font·glyph_bbox_subpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0); + gbm.pixels = output; + gbm.w = out_w; + gbm.h = out_h; + gbm.stride = out_stride; + + if (gbm.w && gbm.h) + rasterize(&gbm, 0.35f, verts, nv, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->mal, info->heap); + + info->free(info->heap, verts); } void @@ -2789,62 +2831,62 @@ font·code_fillbitmap(font·Info *info, unsigned char *output, int out_w, int ou static void -h_prefilter(uchar *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) +h_prefilter(uchar *pixels, int w, int h, int stride_in_bytes, uint kern) { uchar buffer[SAMPLE]; - int safe_w = w - kernel_width; + int safe_w = w - kern; int j; memset(buffer, 0, SAMPLE); for (j=0; j < h; ++j) { int i; - unsigned int total; - memset(buffer, 0, kernel_width); + uint total; + memset(buffer, 0, kern); total = 0; - // make kernel_width a constant in common cases so compiler can optimize out the divide - switch (kernel_width) { + // make kern a constant in common cases so compiler can optimize out the divide + switch (kern) { case 2: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i]; + buffer[(i+kern) & OVERMASK] = pixels[i]; pixels[i] = (uchar) (total / 2); } break; case 3: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i]; + buffer[(i+kern) & OVERMASK] = pixels[i]; pixels[i] = (uchar) (total / 3); } break; case 4: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i]; + buffer[(i+kern) & OVERMASK] = pixels[i]; pixels[i] = (uchar) (total / 4); } break; case 5: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i]; + buffer[(i+kern) & OVERMASK] = pixels[i]; pixels[i] = (uchar) (total / 5); } break; default: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i]; - pixels[i] = (uchar) (total / kernel_width); + buffer[(i+kern) & OVERMASK] = pixels[i]; + pixels[i] = (uchar) (total / kern); } break; } for (; i < w; ++i) { assert(pixels[i] == 0); - total -= buffer[i & OVERMASK]; - pixels[i] = (uchar) (total / kernel_width); + total -= buffer[i & OVERMASK]; + pixels[i] = (uchar) (total / kern); } pixels += stride_in_bytes; @@ -2853,54 +2895,55 @@ h_prefilter(uchar *pixels, int w, int h, int stride_in_bytes, unsigned int kerne static void -v_prefilter(uchar *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) +v_prefilter(uchar *pixels, int w, int h, int stride_in_bytes, uint kern) { uchar buffer[SAMPLE]; - int safe_h = h - kernel_width; + int safe_h = h - kern; int j; memset(buffer, 0, SAMPLE); + for (j=0; j < w; ++j) { - int i; - unsigned int total; - memset(buffer, 0, kernel_width); + int i; + uint total; + memset(buffer, 0, kern); total = 0; - // make kernel_width a constant in common cases so compiler can optimize out the divide - switch (kernel_width) { + // make kern a constant in common cases so compiler can optimize out the divide + switch (kern) { case 2: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i*stride_in_bytes]; + buffer[(i+kern) & OVERMASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (uchar) (total / 2); } break; case 3: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i*stride_in_bytes]; + buffer[(i+kern) & OVERMASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (uchar) (total / 3); } break; case 4: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i*stride_in_bytes]; + buffer[(i+kern) & OVERMASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (uchar) (total / 4); } break; case 5: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i*stride_in_bytes]; + buffer[(i+kern) & OVERMASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (uchar) (total / 5); } break; default: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & OVERMASK]; - buffer[(i+kernel_width) & OVERMASK] = pixels[i*stride_in_bytes]; - pixels[i*stride_in_bytes] = (uchar) (total / kernel_width); + buffer[(i+kern) & OVERMASK] = pixels[i*stride_in_bytes]; + pixels[i*stride_in_bytes] = (uchar) (total / kern); } break; } @@ -2908,7 +2951,7 @@ v_prefilter(uchar *pixels, int w, int h, int stride_in_bytes, unsigned int kerne for (; i < h; ++i) { assert(pixels[i*stride_in_bytes] == 0); total -= buffer[i & OVERMASK]; - pixels[i*stride_in_bytes] = (uchar) (total / kernel_width); + pixels[i*stride_in_bytes] = (uchar)(total / kern); } pixels += 1; @@ -2917,16 +2960,16 @@ v_prefilter(uchar *pixels, int w, int h, int stride_in_bytes, unsigned int kerne static float -oversample_shift(int oversample) +oversample_shift(int width) { - if (!oversample) + if (!width) return 0.0f; // The prefilter is a box filter of width "oversample", // which shifts phase by (oversample - 1)/2 pixels in // oversampled space. We want to shift in the opposite // direction to counter this. - return (float)-(oversample - 1) / (2.0f * (float)oversample); + return -((float)width - 1.0f) / (2.0f * (float)width); } // rects array must be big enough to accommodate all characters in the given ranges @@ -2955,17 +2998,17 @@ font·glyph_fillbitmap_subpixel_prefilter(font·Info *info, uchar *output, int o } void -font·scaledvmetrics(uchar *fontdata, int index, float size, float *ascent, float *descent, float *lineGap) +font·scaledvmetrics(uchar *fontdata, int index, float size, float *ascent, float *descent, float *linegap) { - int i_ascent, i_descent, i_lineGap; + int i_ascent, i_descent, i_linegap; float scale; font·Info info; init(&info, fontdata, font·offsetfor(fontdata, index)); scale = size > 0 ? font·scaleheightto(&info, size) : font·scaleheighttoem(&info, -size); - font·vmetrics(&info, &i_ascent, &i_descent, &i_lineGap); + font·vmetrics(&info, &i_ascent, &i_descent, &i_linegap); *ascent = (float) i_ascent * scale; *descent = (float) i_descent * scale; - *lineGap = (float) i_lineGap * scale; + *linegap = (float) i_linegap * scale; } // ----------------------------------------------------------------------- @@ -3166,7 +3209,7 @@ font·glyph_sdf(font·Info *info, float scale, int glyph, int padding, uchar one if (scale == 0) return nil; - font·glyph_bitmapbox_subpixel(info, glyph, scale, scale, 0.0f,0.0f, &ix0,&iy0,&ix1,&iy1); + font·glyph_bbox_subpixel(info, glyph, scale, scale, 0.0f,0.0f, &ix0,&iy0,&ix1,&iy1); // if empty, return nil if (ix0 == ix1 || iy0 == iy1) @@ -3192,11 +3235,11 @@ font·glyph_sdf(font·Info *info, float scale, int glyph, int padding, uchar one int x,y,i,j; float *precompute; font·Vertex *verts; - int num_verts = font·glyph_shape(info, glyph, &verts); + int numv = font·glyph_shape(info, glyph, &verts); data = info->alloc(info->heap, 1, w * h); - precompute = info->alloc(info->heap, num_verts, sizeof(float)); + precompute = info->alloc(info->heap, numv, sizeof(float)); - for (i=0,j=num_verts-1; i < num_verts; j=i++) { + for (i=0,j=numv-1; i < numv; j=i++) { if (verts[i].type == font·Vline) { float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y; float x1 = verts[j].x*scale_x, y1 = verts[j].y*scale_y; @@ -3225,9 +3268,9 @@ font·glyph_sdf(font·Info *info, float scale, int glyph, int padding, uchar one float x_gspace = (sx / scale_x); float y_gspace = (sy / scale_y); - int winding = compute_crossings_x(x_gspace, y_gspace, num_verts, verts); // @OPTIMIZE: this could just be a rasterization, but needs to be line vs. non-tesselated curves so a new path + int winding = compute_crossings_x(x_gspace, y_gspace, numv, verts); // @OPTIMIZE: this could just be a rasterization, but needs to be line vs. non-tesselated curves so a new path - for (i=0; i < num_verts; ++i) { + for (i=0; i < numv; ++i) { float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y; // check against every point here rather than inside line/curve primitives -- @TODO: wrong if multiple 'moves' in a row produce a garbage point, and given culling, probably more efficient to do within line/curve @@ -3455,13 +3498,13 @@ matches(uchar *fc, uint32 offset, uchar *name, int32 flags) } int -font·findmatch(uchar *font_collection, char *name_utf8, int32 flags) +font·findmatch(uchar *collection, char *name_utf8, int32 flags) { int32 i; for (i=0;;++i) { - int32 off = font·offsetfor(font_collection, i); + int32 off = font·offsetfor(collection, i); if (off < 0) return off; - if (matches((uchar *) font_collection, off, (uchar*) name_utf8, flags)) + if (matches((uchar *) collection, off, (uchar*) name_utf8, flags)) return off; } } diff --git a/sys/libfont/test.c b/sys/libfont/test.c index bd55f33..7ce9dc0 100644 --- a/sys/libfont/test.c +++ b/sys/libfont/test.c @@ -5,25 +5,26 @@ #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" -#define W 512 -#define H 128 -#define L 64 +#define DIV 2 +#define W 1920/DIV +#define H 1080 +#define L H/80 -static char *phrase = "the quick brown"; +static char *phrase = "strlen(info)=000"; int main() { - int i, err; - float x, dx, scale; - uchar *bitmap; + int i, j, k, err; + float x, dx, dy, scale, sx, sy; + uchar *bitmap, *work; font·Info *info; mmap·Reader fontfile; - int off, y, ascent, descent, baseln; + int w, h, off, y, ascent, descent, baseln; int adv, lsb, r0[2], r1[2]; err = 0; - fontfile = mmap·open("/home/nolln/root/data/DejaVuSans.ttf"); + fontfile = mmap·open("/home/nolln/root/data/Inconsolata-Regular.ttf"); if (!fontfile.len) { err = 1; goto end; @@ -39,16 +40,29 @@ main() font·vmetrics(info, &ascent, &descent, &baseln); ascent *= scale; - x = 0.; + x = 0.; + dy = 0.; for (i = 0; i < strlen(phrase); i++) { dx = x - (float)floor(x); font·code_hmetrics(info, phrase[i], &adv, &lsb); - font·code_bitmapbox_subpixel(info, phrase[i], scale, scale, dx, 0, r0, r0+1, r1, r1+1); + font·code_bbox_subpixel(info, phrase[i], scale, scale, dx, 0, r0, r0+1, r1, r1+1); y = ascent + r0[1]; - off = (int)floor(x) + (lsb * scale) + y * W; + dy = y - (float)floor(y); - font·code_fillbitmap_subpixel(info, bitmap+off, r1[0]-r0[0], r1[1]-r0[1], W, scale, scale, dx, 0, phrase[i]); + w = r1[0]-r0[0]; + h = r1[1]-r0[1]; + + work = calloc(w*h, sizeof(*bitmap)); + font·code_fillbitmap_subpixel_prefilter(info, work, w, h, w, scale, scale, dx, 0, 1, 1, &sx, &sy, phrase[i]); + + off = (int)floor(x+sx) + r0[0] + ((int)floor(sy)+y) * W; + for (j = 0; j < h; j++) { + for (k = 0; k < w; k++) { + bitmap[off + W*j + k] += work[k + w*j]; + } + } + free(work); x += scale * adv; if (phrase[i+1]) -- cgit v1.2.1