Example #1
0
        public static void stbtt__fill_active_edges_new(float[] scanline, int scanline_fill, int len,
                                                        stbtt__active_edge e, float y_top)
        {
            var y_bottom = y_top + 1;

            while (e != null)
            {
                if (e.fdx == 0)
                {
                    var x0 = e.fx;
                    if (x0 < len)
                    {
                        if (x0 >= 0)
                        {
                            stbtt__handle_clipped_edge(scanline, 0, (int)x0, e, x0, y_top, x0, y_bottom);
                            stbtt__handle_clipped_edge(scanline, scanline_fill - 1, (int)x0 + 1, e, x0, y_top, x0, y_bottom);
                        }
                        else
                        {
                            stbtt__handle_clipped_edge(scanline, scanline_fill - 1, 0, e, x0, y_top, x0, y_bottom);
                        }
                    }
                }
                else
                {
                    var   x0       = e.fx;
                    var   dx       = e.fdx;
                    var   xb       = x0 + dx;
                    float x_top    = 0;
                    float x_bottom = 0;
                    float sy0      = 0;
                    float sy1      = 0;
                    var   dy       = e.fdy;
                    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)
                    {
                        if ((int)x_top == (int)x_bottom)
                        {
                            float height = 0;
                            var   x      = (int)x_top;
                            height       = sy1 - sy0;
                            scanline[x] += e.direction * (1 - (x_top - x + (x_bottom - x)) / 2) * height;
                            scanline[x + scanline_fill] += e.direction * height;
                        }
                        else
                        {
                            var   x          = 0;
                            var   x1         = 0;
                            var   x2         = 0;
                            float y_crossing = 0;
                            float step       = 0;
                            float sign       = 0;
                            float area       = 0;
                            if (x_top > x_bottom)
                            {
                                float t = 0;
                                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;
                            y_crossing    = (x1 + 1 - x0) * dy + y_top;
                            sign          = e.direction;
                            area          = sign * (y_crossing - sy0);
                            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));
                            scanline[x2] += area + sign * (1 - (x2 - x2 + (x_bottom - x2)) / 2) * (sy1 - y_crossing);
                            scanline[x2 + scanline_fill] += sign * (sy1 - sy0);
                        }
                    }
                    else
                    {
                        var x = 0;
                        for (x = 0; x < len; ++x)
                        {
                            var y0 = y_top;
                            var x1 = (float)x;
                            var x2 = (float)(x + 1);
                            var x3 = xb;
                            var y3 = y_bottom;
                            var y1 = (x - x0) / dx + y_top;
                            var y2 = (x + 1 - x0) / dx + y_top;
                            if (x0 < x1 && x3 > x2)
                            {
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x0, y0, x1, y1);
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x1, y1, x2, y2);
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x2, y2, x3, y3);
                            }
                            else if (x3 < x1 && x0 > x2)
                            {
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x0, y0, x2, y2);
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x2, y2, x1, y1);
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x1, y1, x3, y3);
                            }
                            else if (x0 < x1 && x3 > x1)
                            {
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x0, y0, x1, y1);
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x1, y1, x3, y3);
                            }
                            else if (x3 < x1 && x0 > x1)
                            {
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x0, y0, x1, y1);
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x1, y1, x3, y3);
                            }
                            else if (x0 < x2 && x3 > x2)
                            {
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x0, y0, x2, y2);
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x2, y2, x3, y3);
                            }
                            else if (x3 < x2 && x0 > x2)
                            {
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x0, y0, x2, y2);
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x2, y2, x3, y3);
                            }
                            else
                            {
                                stbtt__handle_clipped_edge(scanline, 0, x, e, x0, y0, x3, y3);
                            }
                        }
                    }
                }

                e = e.next;
            }
        }
Example #2
0
        public static void stbtt__handle_clipped_edge(float[] scanline, int offset, int x, stbtt__active_edge e, float x0,
                                                      float y0, float x1, float y1)
        {
            if (y0 == y1)
            {
                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;
            }

            if (y1 > e.ey)
            {
                x1 += (x1 - x0) * (e.ey - y1) / (y1 - y0);
                y1  = e.ey;
            }

            if (x0 <= x && x1 <= x)
            {
                scanline[x + offset] += e.direction * (y1 - y0);
            }
            else if (x0 >= x + 1 && x1 >= x + 1)
            {
            }
            else
            {
                scanline[x + offset] += e.direction * (y1 - y0) * (1 - (x0 - x + (x1 - x)) / 2);
            }
        }