public static void stbtt_setvertex(stbtt_vertex *v, byte type, int x, int y, int cx, int cy)
 {
     v->type = type;
     v->x    = (short)x;
     v->y    = (short)y;
     v->cx   = (short)cx;
     v->cy   = (short)cy;
 }
Esempio n. 2
0
 public static void stbtt_setvertex(stbtt_vertex *v, byte type, int x, int y, int cx, int cy)
 {
     v->type = type;
     v->x    = ((short)(x));
     v->y    = ((short)(y));
     v->cx   = ((short)(cx));
     v->cy   = ((short)(cy));
 }
Esempio n. 3
0
        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 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 int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex *verts)
        {
            var   i       = 0;
            var   orig    = stackalloc float[2];
            var   ray     = stackalloc float[] { 1, 0 };
            float y_frac  = 0;
            var   winding = 0;

            y_frac = (float)CRuntime.fmod(y, 1.0f);
            if (y_frac < 0.01f)
            {
                y += 0.01f;
            }
            else if (y_frac > 0.99f)
            {
                y -= 0.01f;
            }
            orig[0] = x;
            orig[1] = y;
            for (i = 0; i < nverts; ++i)
            {
                if (verts[i].type == STBTT_vline)
                {
                    int x0 = verts[i - 1].x;
                    int y0 = verts[i - 1].y;
                    int x1 = verts[i].x;
                    int y1 = verts[i].y;
                    if (y > (y0 < y1 ? y0 : y1) && y < (y0 < y1 ? y1 : y0) && x > (x0 < x1 ? x0 : x1))
                    {
                        var x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0;
                        if (x_inter < x)
                        {
                            winding += y0 < y1 ? 1 : -1;
                        }
                    }
                }

                if (verts[i].type == STBTT_vcurve)
                {
                    int x0 = verts[i - 1].x;
                    int y0 = verts[i - 1].y;
                    int x1 = verts[i].cx;
                    int y1 = verts[i].cy;
                    int x2 = verts[i].x;
                    int y2 = verts[i].y;
                    var ax = x0 < (x1 < x2 ? x1 : x2) ? x0 : x1 < x2 ? x1 : x2;
                    var ay = y0 < (y1 < y2 ? y1 : y2) ? y0 : y1 < y2 ? y1 : y2;
                    var by = y0 < (y1 < y2 ? y2 : y1) ? y1 < y2 ? y2 : y1 : y0;
                    if (y > ay && y < by && x > ax)
                    {
                        var q0        = stackalloc float[2];
                        var q1        = stackalloc float[2];
                        var q2        = stackalloc float[2];
                        var hitsArray = new UnsafeArray2D <float>(2, 2);
                        var hits      = (float **)hitsArray;
                        q0[0] = x0;
                        q0[1] = y0;
                        q1[0] = x1;
                        q1[1] = y1;
                        q2[0] = x2;
                        q2[1] = y2;
                        if (equal(q0, q1) != 0 || equal(q1, q2) != 0)
                        {
                            x0 = verts[i - 1].x;
                            y0 = verts[i - 1].y;
                            x1 = verts[i].x;
                            y1 = verts[i].y;
                            if (y > (y0 < y1 ? y0 : y1) && y < (y0 < y1 ? y1 : y0) && x > (x0 < x1 ? x0 : x1))
                            {
                                var x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0;
                                if (x_inter < x)
                                {
                                    winding += y0 < y1 ? 1 : -1;
                                }
                            }
                        }
                        else
                        {
                            var num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits);
                            if (num_hits >= 1)
                            {
                                if (hits[0][0] < 0)
                                {
                                    winding += hits[0][1] < 0 ? -1 : 1;
                                }
                            }
                            if (num_hits >= 2)
                            {
                                if (hits[1][0] < 0)
                                {
                                    winding += hits[1][1] < 0 ? -1 : 1;
                                }
                            }
                        }
                    }
                }
            }

            return(winding);
        }
Esempio n. 6
0
        public static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex *verts)
        {
            int    i    = 0;
            float *orig = stackalloc float[2];
            float *ray  = stackalloc float[2];

            ray[0] = 1;
            ray[1] = 0;

            float y_frac  = 0;
            int   winding = 0;

            orig[0] = x;
            orig[1] = y;
            y_frac  = ((float)(CRuntime.Fmod(y, 1.0f)));
            if ((y_frac) < (0.01f))
            {
                y += 0.01f;
            }
            else if ((y_frac) > (0.99f))
            {
                y -= 0.01f;
            }
            orig[1] = y;
            for (i = 0; (i) < (nverts); ++i)
            {
                if ((verts[i].type) == (STBTT_vline))
                {
                    int x0 = verts[i - 1].x;
                    int y0 = verts[i - 1].y;
                    int x1 = verts[i].x;
                    int y1 = verts[i].y;
                    if ((((y) > ((y0) < (y1) ? (y0) : (y1))) && ((y) < ((y0) < (y1) ? (y1) : (y0)))) && ((x) > ((x0) < (x1) ? (x0) : (x1))))
                    {
                        float x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0;
                        if ((x_inter) < (x))
                        {
                            winding += ((y0) < (y1)) ? 1 : -1;
                        }
                    }
                }
                if ((verts[i].type) == (STBTT_vcurve))
                {
                    int x0 = verts[i - 1].x;
                    int y0 = verts[i - 1].y;
                    int x1 = verts[i].cx;
                    int y1 = verts[i].cy;
                    int x2 = verts[i].x;
                    int y2 = verts[i].y;
                    int ax = (x0) < ((x1) < (x2) ? (x1) : (x2)) ? (x0) : ((x1) < (x2) ? (x1) : (x2));
                    int ay = (y0) < ((y1) < (y2) ? (y1) : (y2)) ? (y0) : ((y1) < (y2) ? (y1) : (y2));
                    int by = (y0) < ((y1) < (y2) ? (y2) : (y1)) ? ((y1) < (y2) ? (y2) : (y1)) : (y0);
                    if ((((y) > (ay)) && ((y) < (by))) && ((x) > (ax)))
                    {
                        float *q0   = stackalloc float[2];
                        float *q1   = stackalloc float[2];
                        float *q2   = stackalloc float[2];
                        float *hits = stackalloc float[4];
                        q0[0] = x0;
                        q0[1] = y0;
                        q1[0] = x1;
                        q1[1] = y1;
                        q2[0] = x2;
                        q2[1] = y2;
                        if (((equal(q0, q1)) != 0) || ((equal(q1, q2)) != 0))
                        {
                            x0 = verts[i - 1].x;
                            y0 = verts[i - 1].y;
                            x1 = verts[i].x;
                            y1 = verts[i].y;
                            if ((((y) > ((y0) < (y1) ? (y0) : (y1))) && ((y) < ((y0) < (y1) ? (y1) : (y0)))) && ((x) > ((x0) < (x1) ? (x0) : (x1))))
                            {
                                float x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0;
                                if ((x_inter) < (x))
                                {
                                    winding += ((y0) < (y1)) ? 1 : -1;
                                }
                            }
                        }
                        else
                        {
                            int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits);
                            if ((num_hits) >= (1))
                            {
                                if ((hits[0]) < (0))
                                {
                                    winding += (hits[1]) < (0) ? -1 : 1;
                                }
                            }
                            if ((num_hits) >= (2))
                            {
                                if ((hits[2]) < (0))
                                {
                                    winding += (hits[3]) < (0) ? -1 : 1;
                                }
                            }
                        }
                    }
                }
            }
            return(winding);
        }
Esempio n. 7
0
        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);
        }