public static int stbtt__tesselate_curve(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float objspace_flatness_squared, int n) { var mx = (x0 + 2 * x1 + x2) / 4; var my = (y0 + 2 * y1 + y2) / 4; var dx = (x0 + x2) / 2 - mx; var dy = (y0 + y2) / 2 - my; if (n > 16) { return(1); } if (dx * dx + dy * dy > objspace_flatness_squared) { stbtt__tesselate_curve(points, num_points, x0, y0, (x0 + x1) / 2.0f, (y0 + y1) / 2.0f, mx, my, objspace_flatness_squared, n + 1); stbtt__tesselate_curve(points, num_points, mx, my, (x1 + x2) / 2.0f, (y1 + y2) / 2.0f, x2, y2, objspace_flatness_squared, n + 1); } else { stbtt__add_point(points, *num_points, x2, y2); *num_points = *num_points + 1; } return(1); }
public static void stbtt__rasterize(stbtt__bitmap *result, stbtt__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, void *userdata) { var y_scale_inv = invert != 0 ? -scale_y : scale_y; stbtt__edge *e; var n = 0; var i = 0; var j = 0; var k = 0; var m = 0; var vsubsample = 1; n = 0; for (i = 0; i < windings; ++i) { n += wcount[i]; } e = (stbtt__edge *)CRuntime.malloc((ulong)(sizeof(stbtt__edge) * (n + 1))); if (e == null) { return; } n = 0; m = 0; for (i = 0; i < windings; ++i) { var p = pts + m; m += wcount[i]; j = wcount[i] - 1; for (k = 0; k < wcount[i]; j = k++) { var a = k; var b = j; if (p[j].y == p[k].y) { continue; } e[n].invert = 0; if (invert != 0 && p[j].y > p[k].y || invert == 0 && p[j].y < p[k].y) { e[n].invert = 1; a = j; b = k; } e[n].x0 = p[a].x * scale_x + shift_x; e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample; e[n].x1 = p[b].x * scale_x + shift_x; e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample; ++n; } } stbtt__sort_edges(e, n); stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata); CRuntime.free(e); }
public static void stbtt__add_point(stbtt__point *points, int n, float x, float y) { if (points == null) { return; } points[n].x = x; points[n].y = y; }
public static void stbtt__rasterize(stbtt__bitmap *result, stbtt__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, void *userdata) { float y_scale_inv = (invert) != 0 ? -scale_y : scale_y; stbtt__edge *e; int n = 0; int i = 0; int j = 0; int k = 0; int m = 0; int vsubsample = 1; n = 0; for (i = 0; (i) < (windings); ++i) { n += wcount[i]; } e = (stbtt__edge *)(CRuntime.Malloc((ulong)(sizeof(stbtt__edge) * (n + 1)))); if ((e) == (null)) { return; } n = 0; m = 0; for (i = 0; (i) < (windings); ++i) { stbtt__point *p = pts + m; m += wcount[i]; j = wcount[i] - 1; for (k = 0; (k) < (wcount[i]); j = k++) { int a = k; int b = j; if ((p[j].y) == (p[k].y)) { continue; } e[n].invert = 0; if ((((invert) != 0) && ((p[j].y) > (p[k].y))) || ((invert == 0) && ((p[j].y) < (p[k].y)))) { e[n].invert = 1; a = j; b = k; } e[n].x0 = p[a].x * scale_x + shift_x; e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample; e[n].x1 = p[b].x * scale_x + shift_x; e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample; ++n; } } stbtt__sort_edges(e, n); stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata); CRuntime.Free(e); }
public static void stbtt__rasterize(stbtt__bitmap *result, stbtt__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, void *userdata) { float y_scale_inv = (float)((invert) != 0 ? -scale_y : scale_y); stbtt__edge *e; int n = 0; int i = 0; int j = 0; int k = 0; int m = 0; int vsubsample = (int)(1); n = (int)(0); for (i = (int)(0); (i) < (windings); ++i) { n += (int)(wcount[i]); } e = (stbtt__edge *)(CRuntime.malloc((ulong)(sizeof(stbtt__edge) * (n + 1)))); if ((e) == (null)) { return; } n = (int)(0); m = (int)(0); for (i = (int)(0); (i) < (windings); ++i) { stbtt__point *p = pts + m; m += (int)(wcount[i]); j = (int)(wcount[i] - 1); for (k = (int)(0); (k) < (wcount[i]); j = (int)(k++)) { int a = (int)(k); int b = (int)(j); if ((p[j].y) == (p[k].y)) { continue; } e[n].invert = (int)(0); if ((((invert) != 0) && ((p[j].y) > (p[k].y))) || ((invert == 0) && ((p[j].y) < (p[k].y)))) { e[n].invert = (int)(1); a = (int)(j); b = (int)(k); } e[n].x0 = (float)(p[a].x * scale_x + shift_x); e[n].y0 = (float)((p[a].y * y_scale_inv + shift_y) * vsubsample); e[n].x1 = (float)(p[b].x * scale_x + shift_x); e[n].y1 = (float)((p[b].y * y_scale_inv + shift_y) * vsubsample); ++n; } } stbtt__sort_edges(e, (int)(n)); stbtt__rasterize_sorted_edges(result, e, (int)(n), (int)(vsubsample), (int)(off_x), (int)(off_y), userdata); CRuntime.free(e); }
public static void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata) { float scale = (scale_x) > (scale_y) ? scale_y : scale_x; int winding_count = 0; int * winding_lengths = (null); stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata); if ((windings) != null) { stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata); CRuntime.Free(winding_lengths); CRuntime.Free(windings); } }
public static void stbtt__tesselate_cubic(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, float objspace_flatness_squared, int n) { var dx0 = x1 - x0; var dy0 = y1 - y0; var dx1 = x2 - x1; var dy1 = y2 - y1; var dx2 = x3 - x2; var dy2 = y3 - y2; var dx = x3 - x0; var dy = y3 - y0; var longlen = (float)(CRuntime.sqrt(dx0 * dx0 + dy0 * dy0) + CRuntime.sqrt(dx1 * dx1 + dy1 * dy1) + CRuntime.sqrt(dx2 * dx2 + dy2 * dy2)); var shortlen = (float)CRuntime.sqrt(dx * dx + dy * dy); var flatness_squared = longlen * longlen - shortlen * shortlen; if (n > 16) { return; } if (flatness_squared > objspace_flatness_squared) { var x01 = (x0 + x1) / 2; var y01 = (y0 + y1) / 2; var x12 = (x1 + x2) / 2; var y12 = (y1 + y2) / 2; var x23 = (x2 + x3) / 2; var y23 = (y2 + y3) / 2; var xa = (x01 + x12) / 2; var ya = (y01 + y12) / 2; var xb = (x12 + x23) / 2; var yb = (y12 + y23) / 2; var mx = (xa + xb) / 2; var my = (ya + yb) / 2; stbtt__tesselate_cubic(points, num_points, x0, y0, x01, y01, xa, ya, mx, my, objspace_flatness_squared, n + 1); stbtt__tesselate_cubic(points, num_points, mx, my, xb, yb, x23, y23, x3, y3, objspace_flatness_squared, n + 1); } else { stbtt__add_point(points, *num_points, x3, y3); *num_points = *num_points + 1; } }
public static void stbtt__tesselate_cubic(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, float objspace_flatness_squared, int n) { float dx0 = (float)(x1 - x0); float dy0 = (float)(y1 - y0); float dx1 = (float)(x2 - x1); float dy1 = (float)(y2 - y1); float dx2 = (float)(x3 - x2); float dy2 = (float)(y3 - y2); float dx = (float)(x3 - x0); float dy = (float)(y3 - y0); float longlen = (float)(CRuntime.sqrt((double)(dx0 * dx0 + dy0 * dy0)) + CRuntime.sqrt((double)(dx1 * dx1 + dy1 * dy1)) + CRuntime.sqrt((double)(dx2 * dx2 + dy2 * dy2))); float shortlen = (float)(CRuntime.sqrt((double)(dx * dx + dy * dy))); float flatness_squared = (float)(longlen * longlen - shortlen * shortlen); if ((n) > (16)) { return; } if ((flatness_squared) > (objspace_flatness_squared)) { float x01 = (float)((x0 + x1) / 2); float y01 = (float)((y0 + y1) / 2); float x12 = (float)((x1 + x2) / 2); float y12 = (float)((y1 + y2) / 2); float x23 = (float)((x2 + x3) / 2); float y23 = (float)((y2 + y3) / 2); float xa = (float)((x01 + x12) / 2); float ya = (float)((y01 + y12) / 2); float xb = (float)((x12 + x23) / 2); float yb = (float)((y12 + y23) / 2); float mx = (float)((xa + xb) / 2); float my = (float)((ya + yb) / 2); stbtt__tesselate_cubic(points, num_points, (float)(x0), (float)(y0), (float)(x01), (float)(y01), (float)(xa), (float)(ya), (float)(mx), (float)(my), (float)(objspace_flatness_squared), (int)(n + 1)); stbtt__tesselate_cubic(points, num_points, (float)(mx), (float)(my), (float)(xb), (float)(yb), (float)(x23), (float)(y23), (float)(x3), (float)(y3), (float)(objspace_flatness_squared), (int)(n + 1)); } else { stbtt__add_point(points, (int)(*num_points), (float)(x3), (float)(y3)); *num_points = (int)(*num_points + 1); } }
public static int stbtt__tesselate_curve(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float objspace_flatness_squared, int n) { float mx = (float)((x0 + 2 * x1 + x2) / 4); float my = (float)((y0 + 2 * y1 + y2) / 4); float dx = (float)((x0 + x2) / 2 - mx); float dy = (float)((y0 + y2) / 2 - my); if ((n) > (16)) { return((int)(1)); } if ((dx * dx + dy * dy) > (objspace_flatness_squared)) { stbtt__tesselate_curve(points, num_points, (float)(x0), (float)(y0), (float)((x0 + x1) / 2.0f), (float)((y0 + y1) / 2.0f), (float)(mx), (float)(my), (float)(objspace_flatness_squared), (int)(n + 1)); stbtt__tesselate_curve(points, num_points, (float)(mx), (float)(my), (float)((x1 + x2) / 2.0f), (float)((y1 + y2) / 2.0f), (float)(x2), (float)(y2), (float)(objspace_flatness_squared), (int)(n + 1)); } else { stbtt__add_point(points, (int)(*num_points), (float)(x2), (float)(y2)); *num_points = (int)(*num_points + 1); } return((int)(1)); }
public static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata) { stbtt__point *points = null; var num_points = 0; var objspace_flatness_squared = objspace_flatness * objspace_flatness; var i = 0; var n = 0; var start = 0; var pass = 0; for (i = 0; i < num_verts; ++i) { if (vertices[i].type == STBTT_vmove) { ++n; } } *num_contours = n; if (n == 0) { return(null); } *contour_lengths = (int *)CRuntime.malloc((ulong)(sizeof(int) * n)); if (*contour_lengths == null) { *num_contours = 0; return(null); } for (pass = 0; pass < 2; ++pass) { float x = 0; float y = 0; if (pass == 1) { points = (stbtt__point *)CRuntime.malloc((ulong)(num_points * sizeof(stbtt__point))); if (points == null) { goto error; } } num_points = 0; n = -1; for (i = 0; i < num_verts; ++i) { switch (vertices[i].type) { case STBTT_vmove: if (n >= 0) { (*contour_lengths)[n] = num_points - start; } ++n; start = num_points; x = vertices[i].x; y = vertices[i].y; stbtt__add_point(points, num_points++, x, y); break; case STBTT_vline: x = vertices[i].x; y = vertices[i].y; stbtt__add_point(points, num_points++, x, y); break; case STBTT_vcurve: stbtt__tesselate_curve(points, &num_points, x, y, vertices[i].cx, vertices[i].cy, vertices[i].x, vertices[i].y, objspace_flatness_squared, 0); x = vertices[i].x; y = vertices[i].y; break; case STBTT_vcubic: stbtt__tesselate_cubic(points, &num_points, x, y, vertices[i].cx, vertices[i].cy, vertices[i].cx1, vertices[i].cy1, vertices[i].x, vertices[i].y, objspace_flatness_squared, 0); x = vertices[i].x; y = vertices[i].y; break; } } (*contour_lengths)[n] = num_points - start; } return(points); error :; CRuntime.free(points); CRuntime.free(*contour_lengths); *contour_lengths = null; *num_contours = 0; return(null); }
public static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata) { stbtt__point *points = null; int num_points = (int)(0); float objspace_flatness_squared = (float)(objspace_flatness * objspace_flatness); int i = 0; int n = (int)(0); int start = (int)(0); int pass = 0; for (i = (int)(0); (i) < (num_verts); ++i) { if ((vertices[i].type) == (STBTT_vmove)) { ++n; } } *num_contours = (int)(n); if ((n) == (0)) { return(null); } *contour_lengths = (int *)(CRuntime.malloc((ulong)(sizeof(int) * n))); if ((*contour_lengths) == (null)) { *num_contours = (int)(0); return(null); } for (pass = (int)(0); (pass) < (2); ++pass) { float x = (float)(0); float y = (float)(0); if ((pass) == (1)) { points = (stbtt__point *)(CRuntime.malloc((ulong)(num_points * sizeof(stbtt__point)))); if ((points) == (null)) { goto error; } } num_points = (int)(0); n = (int)(-1); for (i = (int)(0); (i) < (num_verts); ++i) { switch (vertices[i].type) { case STBTT_vmove: if ((n) >= (0)) { (*contour_lengths)[n] = (int)(num_points - start); } ++n; start = (int)(num_points); x = (float)(vertices[i].x); y = (float)(vertices[i].y); stbtt__add_point(points, (int)(num_points++), (float)(x), (float)(y)); break; case STBTT_vline: x = (float)(vertices[i].x); y = (float)(vertices[i].y); stbtt__add_point(points, (int)(num_points++), (float)(x), (float)(y)); break; case STBTT_vcurve: stbtt__tesselate_curve(points, &num_points, (float)(x), (float)(y), (float)(vertices[i].cx), (float)(vertices[i].cy), (float)(vertices[i].x), (float)(vertices[i].y), (float)(objspace_flatness_squared), (int)(0)); x = (float)(vertices[i].x); y = (float)(vertices[i].y); break; case STBTT_vcubic: stbtt__tesselate_cubic(points, &num_points, (float)(x), (float)(y), (float)(vertices[i].cx), (float)(vertices[i].cy), (float)(vertices[i].cx1), (float)(vertices[i].cy1), (float)(vertices[i].x), (float)(vertices[i].y), (float)(objspace_flatness_squared), (int)(0)); x = (float)(vertices[i].x); y = (float)(vertices[i].y); break; } } (*contour_lengths)[n] = (int)(num_points - start); } return(points); error: ; CRuntime.free(points); CRuntime.free(*contour_lengths); *contour_lengths = null; *num_contours = (int)(0); return(null); }