protected override bool OnExposeEvent(Gdk.EventExpose e)
            {
                if (widget.LeftDiff == null)
                {
                    return(true);
                }
                var adj = widget.vAdjustment;

                var diff = useLeftDiff ? widget.LeftDiff : widget.RightDiff;

                using (Cairo.Context cr = Gdk.CairoHelper.Create(e.Window)) {
                    cr.LineWidth = 1;
                    double curY = 0;

                    foreach (var hunk in diff)
                    {
                        double y, count;
                        if (paintInsert)
                        {
                            y     = hunk.InsertStart / (double)editor.LineCount;
                            count = hunk.Inserted / (double)editor.LineCount;
                        }
                        else
                        {
                            y     = hunk.RemoveStart / (double)editor.LineCount;
                            count = hunk.Removed / (double)editor.LineCount;
                        }

                        double start = y * Allocation.Height;
                        cr.Rectangle(0.5, 0.5 + curY, Allocation.Width, (start - curY));
                        cr.Color = new Cairo.Color(1, 1, 1);
                        cr.Fill();

                        curY = start;
                        double height = Math.Max(cr.LineWidth, count * Allocation.Height);
                        cr.Rectangle(0.5, 0.5 + curY, Allocation.Width, height);
                        cr.Color = GetColor(hunk, !paintInsert, false, 1.0);
                        cr.Fill();
                        curY += height;
                    }

                    cr.Rectangle(0.5, 0.5 + curY, Allocation.Width, Allocation.Height - curY);
                    cr.Color = new Cairo.Color(1, 1, 1);
                    cr.Fill();

                    cr.Rectangle(1,
                                 Allocation.Height * adj.Value / adj.Upper + cr.LineWidth + 0.5,
                                 Allocation.Width - 2,
                                 Allocation.Height * (adj.PageSize / adj.Upper));
                    cr.Color = new Cairo.Color(0, 0, 0, 0.5);
                    cr.StrokePreserve();

                    cr.Color = new Cairo.Color(0, 0, 0, 0.03);
                    cr.Fill();
                    cr.Rectangle(0.5, 0.5, Allocation.Width - 1, Allocation.Height - 1);
                    cr.Color = (Mono.TextEditor.HslColor)Style.Dark(StateType.Normal);
                    cr.Stroke();
                }
                return(true);
            }
Ejemplo n.º 2
0
        protected override void onDraw(Cairo.Context gr)
        {
            gr.Save();

            int spacing = (Parent as TabView).Spacing;

            gr.MoveTo(0.5, TabTitle.Slot.Bottom - 0.5);
            gr.LineTo(TabTitle.Slot.Left - spacing, TabTitle.Slot.Bottom - 0.5);
            gr.CurveTo(
                TabTitle.Slot.Left - spacing / 2, TabTitle.Slot.Bottom - 0.5,
                TabTitle.Slot.Left - spacing / 2, 0.5,
                TabTitle.Slot.Left, 0.5);
            gr.LineTo(TabTitle.Slot.Right, 0.5);
            gr.CurveTo(
                TabTitle.Slot.Right + spacing / 2, 0.5,
                TabTitle.Slot.Right + spacing / 2, TabTitle.Slot.Bottom - 0.5,
                TabTitle.Slot.Right + spacing, TabTitle.Slot.Bottom - 0.5);
            gr.LineTo(Slot.Width - 0.5, TabTitle.Slot.Bottom - 0.5);


            gr.LineTo(Slot.Width - 0.5, Slot.Height - 0.5);
            gr.LineTo(0.5, Slot.Height - 0.5);
            gr.ClosePath();
            gr.LineWidth = 2;
            Foreground.SetAsSource(gr);
            gr.StrokePreserve();

            gr.Clip();
            base.onDraw(gr);
            gr.Restore();
        }
Ejemplo n.º 3
0
        public static void DrawStringAt(this Cairo.Context cr, double x, double y, string text, Cairo.Color foreground, Cairo.Color background)
        {
            string[] lines = text.Split("\n".ToCharArray());
            double   leftX = x;

            cr.LineWidth = 3.5;
            cr.LineJoin  = Cairo.LineJoin.Round;

            foreach (var line in lines)
            {
                string[] tabs = line.Split("\t".ToCharArray());
                x = leftX;

                foreach (var tab in tabs)
                {
                    cr.MoveTo(x, y);
                    cr.TextPath(tab);

                    if (tabs.Length > 1)
                    {
                        var extents = cr.TextExtents(tab);
                        x += extents.XAdvance;

                        x = (((int)x / TAB_WIDTH) + 1) * TAB_WIDTH;
                    }
                }
                y += cr.FontExtents.Height;
            }

            cr.SetSourceColor(background);
            cr.StrokePreserve();
            cr.SetSourceColor(foreground);
            cr.Fill();
        }
Ejemplo n.º 4
0
        protected virtual void DrawInner(Cairo.Context graphics, double uw, double x, double y, double width, double height)
        {
            if (d_radius > 0)
            {
                DrawRoundedRectangle(graphics, x, y, width, height, d_radius);
            }
            else
            {
                graphics.Rectangle(x, y, width, height);
            }

            graphics.StrokePreserve();

            graphics.Source = d_inner;
            graphics.Fill();
        }
Ejemplo n.º 5
0
        private void OnExpose(object obj, Gtk.ExposeEventArgs args)
        {
            Gtk.DrawingArea area = (Gtk.DrawingArea)obj;

            Cairo.Context cr = Gdk.CairoHelper.Create(area.GdkWindow);

            int width, height;

            width  = Allocation.Width;
            height = Allocation.Height;
            double radius = Math.Min(width, height) / 4;

            //draw face
            cr.Arc(width / 2, height / 2, radius, 0, 2 * Math.PI);
            cr.StrokePreserve();
            cr.SetSourceRGB(0, 0, 0);
            cr.Fill();

            //draw hands
            double hoursHandSize   = 0.35 * radius;
            double minutesHandSize = 0.5 * radius;
            double secondsHandSize = 0.75 * radius;
            double x = width / 2;
            double y = height / 2;

            cr.LineWidth = 2 * cr.LineWidth;
            cr.SetSourceRGB(0, 255, 0);

            cr.MoveTo(x, y);
            cr.LineTo(x + hoursHandSize * Math.Cos(ToRadian(hoursHand)), y + hoursHandSize * Math.Sin(ToRadian(hoursHand)));
            cr.Stroke();

            cr.MoveTo(x, y);
            cr.LineTo(x + minutesHandSize * Math.Cos(ToRadian(minutesHand)), y + minutesHandSize * Math.Sin(ToRadian(minutesHand)));
            cr.Stroke();

            cr.LineWidth = 0.25 * cr.LineWidth;
            cr.SetSourceRGB(255, 0, 0);
            cr.MoveTo(x, y);
            cr.LineTo(x + secondsHandSize * Math.Cos(ToRadian(secondsHand)), y + secondsHandSize * Math.Sin(ToRadian(secondsHand)));
            cr.Stroke();

            ((IDisposable)cr.Target).Dispose();
            ((IDisposable)cr).Dispose();
        }
        protected virtual void SetupBackground()
        {
            background.Clear();
            Cairo.Context context = background.Create();

            double lwidth  = 1;
            double hlwidth = lwidth * 0.5;

            double margin = Margin;

            //left curvature:
            context.MoveTo(-hlwidth, -hlwidth);
            context.CurveTo(margin * 0.33, -hlwidth,
                            margin * 0.5, Height * 0.4,
                            margin * 0.5, Height * 0.5);
            context.CurveTo(margin * 0.5, Height * 0.6,
                            margin * 0.66, Height - hlwidth,
                            margin - hlwidth, Height - hlwidth);


            //straight bottom:
            context.LineTo(Width - margin - hlwidth, Height - hlwidth);

            //right curvature:
            context.CurveTo(Width - margin * 0.66, Height - hlwidth,
                            Width - margin * 0.5, Height * 0.6,
                            Width - margin * 0.5, Height * 0.5);
            context.CurveTo(Width - margin * 0.5, Height * 0.4,
                            Width - margin * 0.33, -hlwidth,
                            Width - hlwidth, -hlwidth);

            //straight top:
            context.LineTo(-hlwidth, -hlwidth);
            context.ClosePath();

            context.LineWidth = lwidth;
            context.SetSourceRGBA(1.0, 1.0, 1.0, 1.0);
            context.StrokePreserve();
            context.SetSourceRGBA(1.0, 1.0, 1.0, 0.10);
            context.Fill();

            ((IDisposable)context.Target).Dispose();
            ((IDisposable)context).Dispose();
        }
            void DrawArrow(Cairo.Context g, double x, double y)
            {
                var    editor      = mode.editor;
                double phi         = 1.618;
                double arrowLength = editor.LineHeight * phi;
                double arrowHeight = editor.LineHeight / phi;

                g.MoveTo(x - arrowLength, y - arrowHeight);
                g.LineTo(x, y);
                g.LineTo(x - arrowLength, y + arrowHeight);

                g.LineTo(x - arrowLength / phi, y);
                g.ClosePath();
                g.SetSourceRGB(1.0, 0, 0);
                g.StrokePreserve();

                g.SetSourceRGBA(1.0, 0, 0, 0.1);
                g.Fill();
            }
            void DrawArrow(Cairo.Context g, int x, int y)
            {
                TextEditor editor      = mode.editor;
                double     phi         = 1.618;
                double     arrowLength = editor.LineHeight * phi;
                double     arrowHeight = editor.LineHeight / phi;

                g.MoveTo(x - arrowLength, y - arrowHeight);
                g.LineTo(x, y);
                g.LineTo(x - arrowLength, y + arrowHeight);

                g.LineTo(x - arrowLength / phi, y);
                g.ClosePath();
                g.Color = new Cairo.Color(1.0, 0, 0);
                g.StrokePreserve();

                g.Color = new Cairo.Color(1.0, 0, 0, 0.1);
                g.Fill();
            }
Ejemplo n.º 9
0
 public static void DrawCircle(DrawingArea da,
                               int width,
                               int height,
                               double lineWidth,
                               double[] lineColor,
                               double[] fillColor,
                               Position position,
                               double size)
 {
     Cairo.Context cr = Gdk.CairoHelper.Create(da.GdkWindow);
     cr.SetSourceRGB(lineColor[0], lineColor[1], lineColor[2]);
     cr.LineWidth = lineWidth;
     cr.Translate(position.x * width, position.y * height);
     cr.Arc(0, 0, size * width / 100f, 0, 2 * Math.PI);
     cr.StrokePreserve();
     cr.SetSourceRGB(fillColor[0], fillColor[1], fillColor[2]);
     cr.Fill();
     ((IDisposable)cr.GetTarget()).Dispose();
     ((IDisposable)cr).Dispose();
 }
Ejemplo n.º 10
0
        void DrawBar(Cairo.Context cr)
        {
            if (adj == null || adj.Upper <= adj.PageSize)
            {
                return;
            }
            int h = Allocation.Height - Allocation.Width - 6;

            cr.Rectangle(1.5,
                         h * adj.Value / adj.Upper + cr.LineWidth + 0.5,
                         Allocation.Width - 2,
                         h * (adj.PageSize / adj.Upper));
            Cairo.Color color = (TextEditor.ColorStyle != null) ? TextEditor.ColorStyle.Default.CairoColor : new Cairo.Color(0, 0, 0);
            color.A  = 0.5;
            cr.Color = color;
            cr.StrokePreserve();

            color.A  = 0.05;
            cr.Color = color;
            cr.Fill();
        }
Ejemplo n.º 11
0
        public override void Render(Cairo.Context context, Point scale)
        {
            if ((d_borderColor == null || d_borderWidth <= 0) && d_backgroundColor == null)
            {
                return;
            }

            double px = UnitToPixel(XUnits, d_origin.X, scale.X);
            double py = UnitToPixel(YUnits, d_origin.Y, scale.Y);

            double dx = UnitToPixel(XUnits, XRange.Max - XRange.Min, scale.X);
            double dy = UnitToPixel(YUnits, YRange.Max - YRange.Min, scale.Y);

            if (d_borderRadius > 0)
            {
                RoundedRectangle(context, px, py, dx, dy, d_borderRadius);
            }
            else
            {
                context.Rectangle(px, py, dx, dy);
            }

            if (d_borderColor != null && d_borderWidth > 0)
            {
                context.LineWidth = d_borderWidth;
                d_borderColor.Set(context);

                context.StrokePreserve();
            }

            if (d_backgroundColor != null)
            {
                d_backgroundColor.Set(context);
                context.Fill();
            }

            context.NewPath();
        }
Ejemplo n.º 12
0
            public void Read(string fontFileName, char character, string expectedImageFilePath)
            {
                Typeface typeFace;

                using (var fs = Utility.ReadFile(Utility.FontDir + fontFileName))
                {
                    var reader = new OpenFontReader();
                    typeFace = reader.Read(fs);
                }

                Typography.OpenFont.Glyph glyph = typeFace.Lookup(character);

                // read polygons and bezier segments
                GlyphLoader.Read(glyph, out var polygons, out var bezierSegments);

                Rect aabb = new Rect(polygons[0][0], polygons[0][1]);

                //print to test output
                //calcualte the aabb
                for (int i = 0; i < polygons.Count; i++)
                {
                    o.WriteLine("Polygon " + i);
                    var polygon = polygons[i];
                    foreach (var p in polygon)
                    {
                        aabb.Union(p);
                        o.WriteLine("{0}, {1}", (int)p.X, (int)p.Y);
                    }
                    o.WriteLine("");
                }

                foreach (var segment in bezierSegments)
                {
                    aabb.Union(segment.Item1);
                    aabb.Union(segment.Item2);
                    aabb.Union(segment.Item3);
                    o.WriteLine("<{0}, {1}> <{2}, {3}> <{4}, {5}>",
                                (int)segment.Item1.X, (int)segment.Item1.Y,
                                (int)segment.Item2.X, (int)segment.Item2.Y,
                                (int)segment.Item3.X, (int)segment.Item3.Y);
                }

                o.WriteLine("");

                // draw to an image
                using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.Argb32, MathEx.RoundToInt(aabb.Width), MathEx.RoundToInt(aabb.Height)))
                    using (Cairo.Context g = new Cairo.Context(surface))
                    {
                        g.Translate(-aabb.Min.X, -aabb.Min.Y);
                        //essential: set surface back ground to white (1,1,1,1)
                        g.SetSourceColor(CairoEx.ColorWhite);
                        g.Paint();

                        for (var i = 0; i < polygons.Count; i++)
                        {
                            var polygon = polygons[i];
                            g.MoveTo(polygon[0].X, polygon[0].Y);
                            foreach (var point in polygon)
                            {
                                g.LineTo(point.X, point.Y);
                            }
                            g.ClosePath();
                        }
                        g.SetSourceColor(new Cairo.Color(0, 0, 0));
                        g.LineWidth = 4;
                        g.StrokePreserve();
                        g.SetSourceColor(new Cairo.Color(0.8, 0, 0, 0.6));
                        g.Fill();

                        foreach (var segment in bezierSegments)
                        {
                            var p0 = segment.Item1;
                            var c  = segment.Item2;
                            var p1 = segment.Item3;
                            g.MoveTo(p0.X, p0.Y);
                            g.QuadraticTo(c.X, c.Y, p1.X, p1.Y);
                        }

                        g.LineWidth = 4;
                        g.SetSourceColor(CairoEx.ColorRgb(0, 122, 204));
                        g.Stroke();

                        //used to generate expected image
                        //surface.WriteToPng($"{Util.OutputPath}\\GlyphReaderFacts.TheReadMethod.Read_{fontFileName}_{character}.png");

                        var image         = Image.LoadPixelData <Bgra32>(Configuration.Default, surface.Data, surface.Width, surface.Height);
                        var expectedImage = Image.Load(expectedImageFilePath);
                        Assert.True(Util.CompareImage(expectedImage, image));
                    }
            }
Ejemplo n.º 13
0
        protected override bool OnExposeEvent(EventExpose e)
        {
            base.OnExposeEvent(e);

            if (!PintaCore.Workspace.HasOpenDocuments)
            {
                return(true);
            }

            double scale = PintaCore.Workspace.Scale;

            int x = (int)PintaCore.Workspace.Offset.X;
            int y = (int)PintaCore.Workspace.Offset.Y;

            // Translate our expose area for the whole drawingarea to just our canvas
            Rectangle canvas_bounds = new Rectangle(x, y, PintaCore.Workspace.CanvasSize.Width, PintaCore.Workspace.CanvasSize.Height);

            canvas_bounds.Intersect(e.Area);

            if (canvas_bounds.IsEmpty)
            {
                return(true);
            }

            canvas_bounds.X -= x;
            canvas_bounds.Y -= y;

            // Resize our offscreen surface to a surface the size of our drawing area
            if (canvas == null || canvas.Width != canvas_bounds.Width || canvas.Height != canvas_bounds.Height)
            {
                if (canvas != null)
                {
                    (canvas as IDisposable).Dispose();
                }

                canvas = new Cairo.ImageSurface(Cairo.Format.Argb32, canvas_bounds.Width, canvas_bounds.Height);
            }

            cr.Initialize(PintaCore.Workspace.ImageSize, PintaCore.Workspace.CanvasSize);

            using (Cairo.Context g = CairoHelper.Create(GdkWindow)) {
                // Draw our 1 px black border
                g.DrawRectangle(new Cairo.Rectangle(x, y, PintaCore.Workspace.CanvasSize.Width + 1, PintaCore.Workspace.CanvasSize.Height + 1), new Cairo.Color(0, 0, 0), 1);

                // Set up our clip rectangle
                g.Rectangle(new Cairo.Rectangle(x, y, PintaCore.Workspace.CanvasSize.Width, PintaCore.Workspace.CanvasSize.Height));
                g.Clip();

                g.Translate(x, y);

                bool checker = true;

                // Resize each layer and paint it to the screen
                foreach (Layer layer in PintaCore.Layers.GetLayersToPaint())
                {
                    cr.Render(layer.Surface, canvas, canvas_bounds.Location, checker);
                    g.SetSourceSurface(canvas, canvas_bounds.X + (int)(layer.Offset.X * scale), canvas_bounds.Y + (int)(layer.Offset.Y * scale));
                    g.PaintWithAlpha(layer.Opacity);

                    if (layer == PintaCore.Layers.CurrentLayer && PintaCore.LivePreview.IsEnabled)
                    {
                        cr.Render(PintaCore.LivePreview.LivePreviewSurface, canvas, canvas_bounds.Location, checker);

                        g.Save();
                        g.Scale(scale, scale);
                        g.AppendPath(PintaCore.Layers.SelectionPath);
                        g.Clip();

                        g.Scale(1 / scale, 1 / scale);
                        g.SetSourceSurface(canvas, canvas_bounds.X, canvas_bounds.Y);
                        g.PaintWithAlpha(layer.Opacity);

                        g.Restore();
                    }

                    checker = false;
                }

                // If we are at least 200% and grid is requested, draw it
                if (PintaCore.Actions.View.PixelGrid.Active && cr.ScaleFactor.Ratio <= 0.5d)
                {
                    gr.Render(canvas, canvas_bounds.Location);
                    g.SetSourceSurface(canvas, canvas_bounds.X, canvas_bounds.Y);
                    g.Paint();
                }

                // Selection outline
                if (PintaCore.Layers.ShowSelection)
                {
                    g.Save();
                    g.Translate(0.5, 0.5);
                    g.Scale(scale, scale);

                    g.AppendPath(PintaCore.Layers.SelectionPath);

                    if (PintaCore.Tools.CurrentTool.Name.Contains("Select") && !PintaCore.Tools.CurrentTool.Name.Contains("Selected"))
                    {
                        g.Color    = new Cairo.Color(0.7, 0.8, 0.9, 0.2);
                        g.FillRule = Cairo.FillRule.EvenOdd;
                        g.FillPreserve();
                    }

                    g.LineWidth = 1 / scale;

                    // Draw a white line first so it shows up on dark backgrounds
                    g.Color = new Cairo.Color(1, 1, 1);
                    g.StrokePreserve();

                    // Draw a black dashed line over the white line
                    g.SetDash(new double[] { 2 / scale, 4 / scale }, 0);
                    g.Color = new Cairo.Color(0, 0, 0);

                    g.Stroke();
                    g.Restore();
                }
            }

            return(true);
        }
Ejemplo n.º 14
0
            public void Read(string fontFileName, char character, int fontSize)
            {
                Typeface typeFace;

                using (var fs = Utility.ReadFile(Utility.FontDir + fontFileName))
                {
                    var reader = new OpenFontReader();
                    typeFace = reader.Read(fs);
                }

                Glyph glyph = typeFace.Lookup(character);

                // read polygons and bezier segments
                GlyphLoader.Read(glyph, out var polygons, out var bezierSegments);

                Rect aabb = new Rect(polygons[0][0], polygons[0][1]);

                //print to test output
                //calcualte the aabb
                for (int i = 0; i < polygons.Count; i++)
                {
                    o.WriteLine("Polygon " + i);
                    var polygon = polygons[i];
                    foreach (var p in polygon)
                    {
                        aabb.Union(p);
                        o.WriteLine("{0}, {1}", (int)p.X, (int)p.Y);
                    }
                    o.WriteLine("");
                }

                foreach (var segment in bezierSegments)
                {
                    aabb.Union(segment.Item1);
                    aabb.Union(segment.Item2);
                    aabb.Union(segment.Item3);
                    o.WriteLine("<{0}, {1}> <{2}, {3}> <{4}, {5}>",
                                (int)segment.Item1.X, (int)segment.Item1.Y,
                                (int)segment.Item2.X, (int)segment.Item2.Y,
                                (int)segment.Item3.X, (int)segment.Item3.Y);
                }

                o.WriteLine("");

                // draw to an image
                using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.Argb32, 2000, 2000))
                    using (Cairo.Context g = new Cairo.Context(surface))
                    {
                        g.Translate(-aabb.Min.X, -aabb.Min.Y);

                        for (var i = 0; i < polygons.Count; i++)
                        {
                            var polygon = polygons[i];
                            g.MoveTo(polygon[0].X, polygon[0].Y);
                            foreach (var point in polygon)
                            {
                                g.LineTo(point.X, point.Y);
                            }
                            g.ClosePath();
                        }
                        g.SetSourceColor(new Cairo.Color(0, 0, 0));
                        g.LineWidth = 2;
                        g.StrokePreserve();
                        g.SetSourceColor(new Cairo.Color(0.8, 0, 0, 0.6));
                        g.Fill();

                        foreach (var segment in bezierSegments)
                        {
                            var p0 = segment.Item1;
                            var c  = segment.Item2;
                            var p1 = segment.Item3;
                            g.MoveTo(p0.X, p0.Y);
                            g.QuadraticTo(c.X, c.Y, p1.X, p1.Y);
                        }

                        g.LineWidth = 4;
                        g.SetSourceColor(CairoEx.ColorRgb(0, 122, 204));
                        g.Stroke();

                        var path =
                            $"{OutputPath}\\GlyphReaderFacts.TheReadMethod.Read_{fontFileName}_{character}_{fontSize}.png";
                        surface.WriteToPng(path);

                        Util.OpenImage(path);

                        // Now inspect the image to check whether the glyph is correct
                        // TODO Are there a better way to do such kind of unit-test?
                    }
            }
Ejemplo n.º 15
0
 public override void StrokePreserve(object backend)
 {
     Cairo.Context ctx = ((CairoContextBackend)backend).Context;
     ctx.StrokePreserve();
 }
Ejemplo n.º 16
0
        protected override bool OnExposeEvent(Gdk.EventExpose ev)
        {
            base.OnExposeEvent(ev);

            using (Cairo.Context CairoContext = Gdk.CairoHelper.Create(this.GdkWindow)) {
                //Drawing code here.

                //Graph Background
                CairoContext.LineWidth = 1;
                CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
                CairoContext.Rectangle(this.graphRect);
                CairoContext.StrokePreserve();
                CairoContext.SetSourceRGB(1, 1, 1);
                CairoContext.Fill();

                if (this.data.Length <= 0)
                {
                    return(true);
                }

                double Min = double.MaxValue;
                double Max = double.MinValue;

                foreach (float FloatItem in this.data)
                {
                    if (FloatItem > Max)
                    {
                        Max = FloatItem;
                    }
                    if (FloatItem < Min)
                    {
                        Min = FloatItem;
                    }
                }

                //XScale
                int XStep = 100 - ((int)(Min - Max) / 100);
                XStep = Math.Max(XStep, 1);
                for (int X = 0; X < this.scaleRectX.Width; X += XStep)
                {
                    CairoContext.MoveTo(this.scaleRectX.X + X, this.scaleRectX.Y);
                    CairoContext.LineTo(this.scaleRectX.X + X, this.scaleRectX.Y + this.scaleRectX.Height);
                }

                //YScale
                int YStep = 100 - (this.data.Length / 100);
                YStep = Math.Max(YStep, 1);
                for (int Y = 0; Y < this.scaleRectY.Height; Y += YStep)
                {
                    CairoContext.MoveTo(this.scaleRectY.X, this.scaleRectY.Y + Y);
                    CairoContext.LineTo(this.scaleRectY.X + this.scaleRectY.Width, this.scaleRectY.Y + Y);
                }

                //Graph Line
                CairoContext.LineWidth = 2;
                Cairo.Point LastValue = new Cairo.Point(0, 0);
                CairoContext.SetSourceRGB(0.5, 0.5, 0.5);
                for (int i = 0; i < this.data.Length; i++)
                {
                    double X = this.graphRect.X + Scale(i, 0, (double)this.data.Length, 4d, (double)this.graphRect.Width - 8d);
                    double Y = this.graphRect.Y + Scale(this.data[i], Min, Max, (double)this.graphRect.Height - 8d, 4d);

                    if (LastValue.X != 0 && LastValue.Y != 0)
                    {
                        CairoContext.MoveTo(X, Y);
                        CairoContext.LineTo(LastValue.X, LastValue.Y);
                    }
                    LastValue.X = (int)X;
                    LastValue.Y = (int)Y;
                }
                CairoContext.Stroke();
            }

            return(true);
        }
Ejemplo n.º 17
0
        protected override bool OnExposeEvent(EventExpose e)
        {
            base.OnExposeEvent(e);

            if (!PintaCore.Workspace.HasOpenDocuments)
            {
                return(true);
            }

            double scale = PintaCore.Workspace.Scale;

            int x = (int)PintaCore.Workspace.Offset.X;
            int y = (int)PintaCore.Workspace.Offset.Y;

            // Translate our expose area for the whole drawingarea to just our canvas
            Rectangle canvas_bounds = new Rectangle(x, y, PintaCore.Workspace.CanvasSize.Width, PintaCore.Workspace.CanvasSize.Height);

            canvas_bounds.Intersect(e.Area);

            if (canvas_bounds.IsEmpty)
            {
                return(true);
            }

            canvas_bounds.X -= x;
            canvas_bounds.Y -= y;

            // Resize our offscreen surface to a surface the size of our drawing area
            if (canvas == null || canvas.Width != canvas_bounds.Width || canvas.Height != canvas_bounds.Height)
            {
                if (canvas != null)
                {
                    (canvas as IDisposable).Dispose();
                }

                canvas = new Cairo.ImageSurface(Cairo.Format.Argb32, canvas_bounds.Width, canvas_bounds.Height);
            }

            cr.Initialize(PintaCore.Workspace.ImageSize, PintaCore.Workspace.CanvasSize);

            using (Cairo.Context g = CairoHelper.Create(GdkWindow)) {
                // Draw our canvas drop shadow
                g.DrawRectangle(new Cairo.Rectangle(x, y, PintaCore.Workspace.CanvasSize.Width + 1, PintaCore.Workspace.CanvasSize.Height + 1), new Cairo.Color(.5, .5, .5), 1);
                g.DrawRectangle(new Cairo.Rectangle(x - 1, y - 1, PintaCore.Workspace.CanvasSize.Width + 3, PintaCore.Workspace.CanvasSize.Height + 3), new Cairo.Color(.8, .8, .8), 1);
                g.DrawRectangle(new Cairo.Rectangle(x - 2, y - 2, PintaCore.Workspace.CanvasSize.Width + 5, PintaCore.Workspace.CanvasSize.Height + 5), new Cairo.Color(.9, .9, .9), 1);

                // Set up our clip rectangle
                g.Rectangle(new Cairo.Rectangle(x, y, PintaCore.Workspace.CanvasSize.Width, PintaCore.Workspace.CanvasSize.Height));
                g.Clip();

                g.Translate(x, y);

                // Render all the layers to a surface
                var layers = PintaCore.Layers.GetLayersToPaint();
                if (layers.Count == 0)
                {
                    canvas.Clear();
                }
                cr.Render(layers, canvas, canvas_bounds.Location);

                // Paint the surface to our canvas
                g.SetSourceSurface(canvas, canvas_bounds.X + (int)(0 * scale), canvas_bounds.Y + (int)(0 * scale));
                g.Paint();

                // Selection outline
                if (PintaCore.Layers.ShowSelection)
                {
                    g.Save();
                    g.Translate(0.5, 0.5);
                    g.Scale(scale, scale);

                    g.AppendPath(PintaCore.Workspace.ActiveDocument.Selection.SelectionPath);

                    if (PintaCore.Tools.CurrentTool.Name.Contains("Select") && !PintaCore.Tools.CurrentTool.Name.Contains("Selected"))
                    {
                        g.Color    = new Cairo.Color(0.7, 0.8, 0.9, 0.2);
                        g.FillRule = Cairo.FillRule.EvenOdd;
                        g.FillPreserve();
                    }

                    g.LineWidth = 1 / scale;

                    // Draw a white line first so it shows up on dark backgrounds
                    g.Color = new Cairo.Color(1, 1, 1);
                    g.StrokePreserve();

                    // Draw a black dashed line over the white line
                    g.SetDash(new double[] { 2 / scale, 4 / scale }, 0);
                    g.Color = new Cairo.Color(0, 0, 0);

                    g.Stroke();
                    g.Restore();
                }
            }

            return(true);
        }
Ejemplo n.º 18
0
            protected override bool OnExposeEvent(Gdk.EventExpose e)
            {
                var adj = widget.Vadjustment;

                using (Cairo.Context cr = Gdk.CairoHelper.Create(e.Window)) {
                    cr.LineWidth = 1;

                    int count = 0;
                    foreach (Hunk h in widget.Diff)
                    {
                        IncPos(h, ref count);
                    }

                    int start = 0;
                    foreach (Hunk h in widget.Diff)
                    {
                        int size = 0;
                        IncPos(h, ref size);

                        cr.Rectangle(0.5, 0.5 + Allocation.Height * start / count, Allocation.Width, Math.Max(1, Allocation.Height * size / count));
                        if (h.Same)
                        {
                            cr.Color = new Cairo.Color(1, 1, 1);
                        }
                        else if (h.Original().Count == 0)
                        {
                            cr.Color = new Cairo.Color(0.4, 0.8, 0.4);
                        }
                        else if (h.Changes(0).Count == 0)
                        {
                            cr.Color = new Cairo.Color(0.8, 0.4, 0.4);
                        }
                        else
                        {
                            cr.Color = new Cairo.Color(0.4, 0.8, 0.8);
                        }
                        cr.Fill();
                        start += size;
                    }

                    cr.Rectangle(1,
                                 (int)(Allocation.Height * adj.Value / adj.Upper),
                                 Allocation.Width - 2,
                                 (int)(Allocation.Height * ((double)adj.PageSize / adj.Upper)));
                    cr.Color = new Cairo.Color(0, 0, 0, 0.5);
                    cr.StrokePreserve();

                    cr.Color = new Cairo.Color(0, 0, 0, 0.03);
                    cr.Fill();
                    cr.Rectangle(0.5, 0.5, Allocation.Width - 1, Allocation.Height - 1);
                    cr.Color = (HslColor)Style.Dark(StateType.Normal);
                    cr.Stroke();
                }

/*				gc.RgbFgColor = ColorGrey;
 *                              e.Window.DrawRectangle(gc, false,
 *                                      1,
 *                                      (int)(Allocation.Height*scroller.Adjustment.Value/scroller.Adjustment.Upper) + 1,
 *                                      Allocation.Width-3,
 *                                      (int)(Allocation.Height*((double)scroller.Allocation.Height/scroller.Adjustment.Upper))-2);
 */
                return(true);
            }
Ejemplo n.º 19
0
 public void StrokePreserve(object backend)
 {
     Cairo.Context ctx = ((GtkContext)backend).Context;
     ctx.StrokePreserve();
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="showCursor">Whether or not to show the mouse cursor in the drawing.</param>
        /// <param name="useTextLayer">Whether or not to use the TextLayer (as opposed to the Userlayer).</param>
        private void RedrawText(bool showCursor, bool useTextLayer)
        {
            Rectangle r = CurrentTextEngine.GetLayoutBounds();
            r.Inflate(10 + OutlineWidth, 10 + OutlineWidth);
            CurrentTextBounds = r;

            Rectangle cursorBounds = Rectangle.Zero;

            Cairo.ImageSurface surf;

            if (!useTextLayer)
            {
                //Draw text on the current UserLayer's surface as finalized text.
                surf = PintaCore.Workspace.ActiveDocument.CurrentUserLayer.Surface;
            }
            else
            {
                //Draw text on the current UserLayer's TextLayer's surface as re-editable text.
                surf = PintaCore.Workspace.ActiveDocument.CurrentUserLayer.TextLayer.Surface;

                ClearTextLayer();
            }

            using (var g = new Cairo.Context (surf)) {
                g.Save ();

                // Show selection if on text layer
                if (useTextLayer) {
                    // Selected Text
                    Cairo.Color c = new Cairo.Color (0.7, 0.8, 0.9, 0.5);
                    foreach (Rectangle rect in CurrentTextEngine.SelectionRectangles)
                        g.FillRectangle (rect.ToCairoRectangle (), c);
                }
                g.AppendPath (PintaCore.Workspace.ActiveDocument.Selection.SelectionPath);
                g.FillRule = Cairo.FillRule.EvenOdd;
                g.Clip ();

                g.MoveTo (new Cairo.PointD (CurrentTextEngine.Origin.X, CurrentTextEngine.Origin.Y));

                g.Color = PintaCore.Palette.PrimaryColor;

                //Fill in background
                if (BackgroundFill) {
                    using (var g2 = new Cairo.Context (surf)) {
                        g2.FillRectangle(CurrentTextEngine.GetLayoutBounds().ToCairoRectangle(), PintaCore.Palette.SecondaryColor);
                    }
                }

                // Draw the text
                if (FillText)
                    Pango.CairoHelper.ShowLayout (g, CurrentTextEngine.Layout);

                if (FillText && StrokeText) {
                    g.Color = PintaCore.Palette.SecondaryColor;
                    g.LineWidth = OutlineWidth;

                    Pango.CairoHelper.LayoutPath (g, CurrentTextEngine.Layout);
                    g.Stroke ();
                } else if (StrokeText) {
                    g.Color = PintaCore.Palette.PrimaryColor;
                    g.LineWidth = OutlineWidth;

                    Pango.CairoHelper.LayoutPath (g, CurrentTextEngine.Layout);
                    g.Stroke ();
                }

                if (showCursor) {
                    var loc = CurrentTextEngine.GetCursorLocation ();

                    g.Antialias = Cairo.Antialias.None;
                    g.DrawLine (new Cairo.PointD (loc.X, loc.Y), new Cairo.PointD (loc.X, loc.Y + loc.Height), new Cairo.Color (0, 0, 0, 1), 1);

                    cursorBounds = Rectangle.Inflate (loc, 2, 10);
                }

                g.Restore ();

                if (useTextLayer && (is_editing || ctrlKey) && !CurrentTextEngine.IsEmpty())
                {
                    //Draw the text edit rectangle.

                    g.Save();

                    g.Translate(.5, .5);

                    using (Cairo.Path p = g.CreateRectanglePath(new Cairo.Rectangle(CurrentTextBounds.Left, CurrentTextBounds.Top,
                        CurrentTextBounds.Width, CurrentTextBounds.Height - FontSize)))
                    {
                        g.AppendPath(p);
                    }

                    g.LineWidth = 1;

                    g.Color = new Cairo.Color(1, 1, 1);
                    g.StrokePreserve();

                    g.SetDash(new double[] { 2, 4 }, 0);
                    g.Color = new Cairo.Color(1, .1, .2);

                    g.Stroke();

                    g.Restore();
                }
            }

            InflateAndInvalidate(PintaCore.Workspace.ActiveDocument.CurrentUserLayer.previousTextBounds);
            PintaCore.Workspace.Invalidate(old_cursor_bounds);
            PintaCore.Workspace.Invalidate(r);
            PintaCore.Workspace.Invalidate(cursorBounds);

            old_cursor_bounds = cursorBounds;
        }
Ejemplo n.º 21
0
        private void Redraw(Cairo.Context cr)
        {
            // Clear the background
            cr.Rectangle(0, 0, Allocation.Width, Allocation.Height);
            Gdk.CairoHelper.SetSourceColor(cr, Style.Base(State));
            cr.Fill();

            if (model == null)
            {
                if (hadj != null)
                {
                    hadj.Upper = hadj.Lower = 0;
                    hadj.Change();
                }

                if (vadj != null)
                {
                    vadj.Upper = 0;
                    vadj.Change();
                }
                return;
            }

            if (rows == 0 || cols == 0)
            {
                return;
            }

            Gdk.Rectangle background_area = cell_size;
            background_area.Width  += padding;
            background_area.Height += padding;

            TreeIter iter;

            if (model.GetIterFirst(out iter))
            {
                do
                {
                    TreePath path = model.GetPath(iter);

                    int x, y;
                    GetCellPosition(path.Indices[0], out x, out y);

                    if (hadj != null &&
                        (x + cell_size.Width < hadj.Value ||
                         x > hadj.Value + hadj.PageSize))
                    {
                        continue;
                    }

                    if (vadj != null &&
                        (y + cell_size.Height < vadj.Value ||
                         y > vadj.Value + vadj.PageSize))
                    {
                        continue;
                    }

                    if (data_func != null)
                    {
                        data_func(this, renderer, model, iter);
                    }

                    cell_size.X = x;
                    cell_size.Y = y;

                    if (hadj != null)
                    {
                        cell_size.X -= (int)hadj.Value;
                    }

                    if (vadj != null)
                    {
                        cell_size.Y -= (int)vadj.Value;
                    }

                    background_area.X = cell_size.X - (padding / 2);
                    background_area.Y = cell_size.Y - (padding / 2);

                    cr.Rectangle(background_area.X, background_area.Y,
                                 background_area.Width, background_area.Height);
                    cr.Clip();

                    renderer.Render(cr, this, background_area,
                                    cell_size, GetCellState(path));

                    cr.ResetClip();
                } while (model.IterNext(ref iter));
            }

            if (have_rubberband_selection)
            {
                int hadj_val = (hadj != null) ? (int)hadj.Value : 0;
                int vadj_val = (vadj != null) ? (int)vadj.Value : 0;

                cr.Rectangle(sel_rect.X - hadj_val + 0.5f, sel_rect.Y - vadj_val + 0.5f,
                             sel_rect.Width, sel_rect.Height);

                Cairo.Color sel_cairo_color = CairoHelper.GetCairoColor(Style.Background(StateType.Selected));



                //cr.Color = sel_cairo_color;
                cr.SetSourceRGBA(sel_cairo_color.R, sel_cairo_color.G, sel_cairo_color.B, sel_cairo_color.A);

                cr.LineWidth = 1.0f;
                cr.StrokePreserve();

                sel_cairo_color.A = 0.3f;
                //cr.Color = sel_cairo_color;
                cr.SetSourceRGBA(sel_cairo_color.R, sel_cairo_color.G, sel_cairo_color.B, sel_cairo_color.A);

                cr.Fill();
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="showCursor">Whether or not to show the mouse cursor in the drawing.</param>
        /// <param name="useTextLayer">Whether or not to use the TextLayer (as opposed to the Userlayer).</param>
        private void RedrawText(bool showCursor, bool useTextLayer)
        {
            Rectangle r = CurrentTextLayout.GetLayoutBounds();

            r.Inflate(10 + OutlineWidth, 10 + OutlineWidth);
            InflateAndInvalidate(r);
            CurrentTextBounds = r;

            Rectangle cursorBounds = Rectangle.Zero;

            Cairo.ImageSurface surf;

            if (!useTextLayer)
            {
                //Draw text on the current UserLayer's surface as finalized text.
                surf = PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayer.Surface;
            }
            else
            {
                //Draw text on the current UserLayer's TextLayer's surface as re-editable text.
                surf = PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayer.TextLayer.Layer.Surface;

                ClearTextLayer();
            }

            using (var g = new Cairo.Context(surf)) {
                g.Save();

                // Show selection if on text layer
                if (useTextLayer)
                {
                    // Selected Text
                    Cairo.Color c = new Cairo.Color(0.7, 0.8, 0.9, 0.5);
                    foreach (Rectangle rect in CurrentTextLayout.SelectionRectangles)
                    {
                        g.FillRectangle(rect.ToCairoRectangle(), c);
                    }
                }

                if (selection != null)
                {
                    selection.Clip(g);
                }

                g.MoveTo(new Cairo.PointD(CurrentTextEngine.Origin.X, CurrentTextEngine.Origin.Y));

                g.SetSourceColor(PintaCore.Palette.PrimaryColor);

                //Fill in background
                if (BackgroundFill)
                {
                    using (var g2 = new Cairo.Context(surf)) {
                        if (selection != null)
                        {
                            selection.Clip(g2);
                        }

                        g2.FillRectangle(CurrentTextLayout.GetLayoutBounds().ToCairoRectangle(), PintaCore.Palette.SecondaryColor);
                    }
                }

                // Draw the text
                if (FillText)
                {
                    Pango.CairoHelper.ShowLayout(g, CurrentTextLayout.Layout);
                }

                if (FillText && StrokeText)
                {
                    g.SetSourceColor(PintaCore.Palette.SecondaryColor);
                    g.LineWidth = OutlineWidth;

                    Pango.CairoHelper.LayoutPath(g, CurrentTextLayout.Layout);
                    g.Stroke();
                }
                else if (StrokeText)
                {
                    g.SetSourceColor(PintaCore.Palette.PrimaryColor);
                    g.LineWidth = OutlineWidth;

                    Pango.CairoHelper.LayoutPath(g, CurrentTextLayout.Layout);
                    g.Stroke();
                }

                if (showCursor)
                {
                    var loc   = CurrentTextLayout.GetCursorLocation();
                    var color = PintaCore.Palette.PrimaryColor;

                    g.Antialias = Cairo.Antialias.None;
                    g.DrawLine(new Cairo.PointD(loc.X, loc.Y),
                               new Cairo.PointD(loc.X, loc.Y + loc.Height),
                               color, 1);

                    cursorBounds = Rectangle.Inflate(loc, 2, 10);
                }

                g.Restore();


                if (useTextLayer && (is_editing || ctrlKey) && !CurrentTextEngine.IsEmpty())
                {
                    //Draw the text edit rectangle.

                    g.Save();

                    g.Translate(.5, .5);

                    using (Cairo.Path p = g.CreateRectanglePath(CurrentTextBounds.ToCairoRectangle())) {
                        g.AppendPath(p);
                    }

                    g.LineWidth = 1;

                    g.SetSourceColor(new Cairo.Color(1, 1, 1));
                    g.StrokePreserve();

                    g.SetDash(new double[] { 2, 4 }, 0);
                    g.SetSourceColor(new Cairo.Color(1, .1, .2));

                    g.Stroke();

                    g.Restore();
                }
            }

            InflateAndInvalidate(PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayer.previousTextBounds);
            PintaCore.Workspace.Invalidate(old_cursor_bounds);
            InflateAndInvalidate(r);
            PintaCore.Workspace.Invalidate(cursorBounds);

            old_cursor_bounds = cursorBounds;
        }
Ejemplo n.º 23
0
            public void Read(string fontFileName, char character, int fontSize)
            {
                Typeface typeFace;

                using (var fs = Utility.ReadFile(Utility.FontDir + fontFileName))
                {
                    var reader = new OpenFontReader();
                    typeFace = reader.Read(fs);
                }

                Glyph glyph = typeFace.Lookup(character);

                // read polygons and bezier segments
                var polygons       = new List <List <Point> >();
                var bezierSegments = new List <(Point, Point, Point)>();

                GlyphLoader.Read(glyph, out polygons, out bezierSegments);

                //print to test output
                for (int i = 0; i < polygons.Count; i++)
                {
                    o.WriteLine("Polygon " + i);
                    var polygon = polygons[i];
                    foreach (var p in polygon)
                    {
                        o.WriteLine("{0}, {1}", (int)p.X, (int)p.Y);
                    }
                    o.WriteLine("");
                }

                foreach (var segment in bezierSegments)
                {
                    o.WriteLine("<{0}, {1}> <{2}, {3}> <{4}, {5}>",
                                (int)segment.Item1.X, (int)segment.Item1.Y,
                                (int)segment.Item2.X, (int)segment.Item2.Y,
                                (int)segment.Item3.X, (int)segment.Item3.Y);
                }

                o.WriteLine("");

                //FIXME move/scale the rendered glyph to visible region of the cairo surface.

                // draw to an image
                using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.Argb32, 2000, 2000))
                    using (Cairo.Context g = new Cairo.Context(surface))
                    {
                        for (int i = 0; i < polygons.Count; i++)
                        {
                            var polygon = polygons[i];
                            g.MoveTo(polygon[0].X, polygon[0].Y);
                            foreach (var point in polygon)
                            {
                                g.LineTo(point.X, point.Y);
                            }
                            g.ClosePath();
                        }
                        g.SetSourceColor(new Cairo.Color(0, 0, 0));
                        g.LineWidth = 2;
                        g.StrokePreserve();
                        g.SetSourceColor(new Cairo.Color(0.8, 0, 0));
                        g.Fill();

                        foreach (var segment in bezierSegments)
                        {
                            var p0 = segment.Item1;
                            var c  = segment.Item2;
                            var p1 = segment.Item3;
                            g.MoveTo(p0.X, p0.Y);
                            g.QuadraticTo(c.X, c.Y, p1.X, p1.Y);
                        }
                        g.SetSourceColor(new Cairo.Color(0.5, 0.5, 0));
                        g.Stroke();

                        var path = string.Format("D:\\ImGui.UnitTest\\GlyphReaderFacts.TheReadMethod.Read_{0}_{1}_{2}.png",
                                                 fontFileName, character, fontSize);
                        surface.WriteToPng(path);

                        Util.OpenImage(path);

                        // Now inspect the image to check whether the glyph is correct
                        // TODO Are there a better way to do such kind of unit-test?
                    }
            }