public override void Render(Gdk.Drawable window, Gdk.Rectangle bounds, Gtk.StateType state) { int yd = (bounds.Height - ColorBoxSize) * 4 / 5; int width = ColorBoxSize * 2 - 1; int heigth = ColorBoxSize - 1; Cairo.Context cr = Gdk.CairoHelper.Create(window); // black cross to show alpha cr.LineWidth = 2; cr.SetSourceColor(new Cairo.Color(0, 0, 0)); cr.MoveTo(bounds.X, bounds.Y + yd); cr.LineTo(bounds.X + width, bounds.Y + heigth); cr.MoveTo(bounds.X, bounds.Y + yd + heigth); cr.LineTo(bounds.X + width, bounds.Y); cr.Stroke(); // rect around, also only visible with alpha cr.Rectangle(bounds.X, bounds.Y + yd, width, heigth); cr.Stroke(); // fill with color cr.SetSourceColor(GetCairoColor()); cr.Rectangle(bounds.X, bounds.Y + yd, width, heigth); cr.Fill(); cr.Dispose(); bounds.X += width + ColorBoxSpacing; bounds.Width -= heigth + ColorBoxSpacing; base.Render(window, bounds, state); }
void OnExpose(object sender, ExposeEventArgs args) { DrawingArea area = (DrawingArea)sender; Cairo.Context cr = Gdk.CairoHelper.Create(area.GdkWindow); IcnRecode icn = new IcnRecode(project, 1920, 1080, (x, y, w, h, s) => { x *= scale; y *= scale; w *= scale; h *= scale; x += X; y += Y; w += X; h += Y; cr.Save(); cr.SetSourceRGB(0, 0, 0); cr.LineWidth = s * 0.7; cr.MoveTo(x, y); cr.LineTo(w, h); cr.ClosePath(); cr.Stroke(); cr.Restore(); }); icn.Exec(); cr.Dispose(); }
void OnExpose(object sender, ExposeEventArgs args) { DrawingArea area = (DrawingArea)sender; Cairo.Context cairoContext = Gdk.CairoHelper.Create(area.GdkWindow); int width = area.Allocation.Width; int height = area.Allocation.Height; PixelDimensions = new Vector2i(width, height); AxisAlignedBox2d bounds = DrawingBounds; double sx = (double)width / bounds.Width; double sy = (double)height / bounds.Height; float scale = (float)Math.Min(sx, sy); // we apply this translate after scaling to pixel coords Vector2f pixC = Zoom * scale * (Vector2f)bounds.Center; Vector2f translate = new Vector2f(width / 2, height / 2) - pixC; using (var bitmap = new SKBitmap(width, height, SkiaUtil.ColorType(), SKAlphaType.Premul)) { IntPtr len; using (var skSurface = SKSurface.Create(bitmap.Info.Width, bitmap.Info.Height, SkiaUtil.ColorType(), SKAlphaType.Premul, bitmap.GetPixels(out len), bitmap.Info.RowBytes)) { var canvas = skSurface.Canvas; // update scene xform Func <Vector2d, Vector2f> ViewTransformF = (pOrig) => { Vector2f pNew = (Vector2f)pOrig - (Vector2f)bounds.Center; pNew = Zoom * scale * pNew; pNew += (Vector2f)pixC; pNew += translate + Zoom * PixelTranslate; pNew.y = canvas.ClipBounds.Height - pNew.y; return(pNew); }; DrawScene(canvas, ViewTransformF); Cairo.Surface cairoSurf = new Cairo.ImageSurface( bitmap.GetPixels(out len), Cairo.Format.Argb32, bitmap.Width, bitmap.Height, bitmap.Width * 4); cairoSurf.MarkDirty(); cairoContext.SetSourceSurface(cairoSurf, 0, 0); cairoContext.Paint(); cairoSurf.Dispose(); } } cairoContext.Dispose(); //return true; }
/// <summary> /// Creates a bitmap for each character in the font. /// Characters that the font does not include will not be assigned a bitmap. /// </summary> private void CreateBitmaps() { characters = new Dictionary <char, PixelSet>(); ImageSurface surface = new ImageSurface(Format.RGB24, 1, 1); Context context = new Context(surface); context.SelectFontFace(FontName, FontSlant.Normal, FontWeight.Normal); context.SetFontSize(Font.Size); TextExtents ext = context.TextExtents("@"); int charW = (int)Math.Ceiling(ext.Width); int charH = (int)Math.Ceiling(ext.Height); context.Dispose(); surface.Dispose(); surface = new ImageSurface(Format.RGB24, charW, charH); context = new Context(surface); PixelSet missingChar = null; for (int i = 0; i < asciiString.Length; i++) { // Render one character into a PixelSet string asciiChar = string.Empty + asciiString[i]; context.SelectFontFace(FontName, FontSlant.Normal, FontWeight.Normal); context.SetFontSize(Font.Size); context.SetSourceRGB(0.067, 0.067, 0.067); context.Paint(); context.SetSourceRGB(1, 1, 1); ext = context.TextExtents(asciiChar); context.MoveTo(ext.XBearing, ext.YBearing * -1); context.ShowText(asciiChar); PixelSet ch = new PixelSet(surface); // Filter out characters the font doesn't include // The first character is always unprintable, and serves as // a reference for what unprintable characters look like in this font if (i == 0) { missingChar = ch; continue; } else if (ch == missingChar) { continue; } characters.Add(asciiString[i], ch); } context.Dispose(); surface.Dispose(); // Add the space manually if it wasn't included if (!characters.ContainsKey(' ')) { var en = characters.Values.GetEnumerator(); en.MoveNext(); characters.Add(' ', new PixelSet(en.Current.Width, en.Current.Height)); } }
public void Dispose() { if (this.Path == null) { Path = _context.CopyPath(); Bounds = _context.FillExtents().ToAvalonia(); } _context.Dispose(); _surf.Dispose(); }
/// <summary> /// Build a ImageSurface /// </summary> /// <param name="Width">width</param> /// <param name="Height">height</param> /// <param name="Color">color</param> /// <param name="format">surface format</param> /// <returns>the created ImageSurface</returns> public static Cairo.ImageSurface BuildSurface(int Width, int Height, Color Color, Cairo.Format format) { var surface = new Cairo.ImageSurface(format, Width, Height); var c = new Cairo.Context(surface); c.Rectangle(0, 0, Width, Height); c.SetSourceColor(Color); c.Fill(); c.Dispose(); return(surface); }
public void Dispose() { if (g != null) { g.Dispose(); } if (layout != null) { layout.Dispose(); } }
protected override void OnRealized() { base.OnRealized(); using (Cairo.Context context = Gdk.CairoHelper.Create(GdkWindow)) { primarySurface = new SurfaceWrapper(context, primary); secondarySurface = new SurfaceWrapper(context, secondary); context.Dispose(); } primary.Dispose(); primary = null; secondary.Dispose(); secondary = null; }
protected override bool OnExposeEvent(Gdk.EventExpose evnt) { using (Cairo.Context context = Gdk.CairoHelper.Create(evnt.Window)) { if (secondaryOpacity < 1.0f) { RenderIcon(context, primarySurface, 1.0f - (float)Math.Pow(secondaryOpacity, 3.0f)); } if (secondaryOpacity > 0.0f) { RenderIcon(context, secondarySurface, secondaryOpacity); } context.Dispose(); } return(false); }
public void ShouldGetCorrectOutline() { ITextContext textContext = new TypographyTextContext( "0123456", FontFile, 36, FontStretch.Normal, FontStyle.Normal, FontWeight.Normal, 1000, 100, TextAlignment.Leading); // prepare debug contexts { surface = new Cairo.ImageSurface(Cairo.Format.Argb32, 2000, 2000); g = new Cairo.Context(surface); g.SetSourceRGBA(1, 1, 1, 1); g.Paint(); g.SetSourceRGBA(0, 0, 0, 1); g.LineWidth = 1; } // build path CairoPathBuilder cairoPathBuilder; { cairoPathBuilder = new CairoPathBuilder(g, 0, 0, 1); textContext.Build(Point.Zero, cairoPathBuilder); } // show debug results // image { surface.WriteToPng(PathImagePath); g.Dispose(); Assert.False(PathImagePath.Contains(" ")); // open the image in Windows Photo Viewer Process.Start(@"C:\WINDOWS\System32\rundll32.exe", @"""C:\Program Files\Windows Photo Viewer\PhotoViewer.dll"", ImageView_Fullscreen " + PathImagePath); } //text { var text = cairoPathBuilder.Result; File.WriteAllText(PathTextPath, text); Process.Start(@"notepad", PathTextPath); } // Now, check if the output text and image shows a right outline of the text. }
public static void DrawCaption(DrawingArea da, int width, int height, string text, double posX, double posY, double size = 3.75) { if (posX <= 1 && posY <= 1) { posX *= width; posY *= height; } Cairo.Context texTcr = Gdk.CairoHelper.Create(da.GdkWindow); texTcr.SelectFontFace("", Cairo.FontSlant.Normal, Cairo.FontWeight.Bold); texTcr.SetFontSize(size * width / 100f); texTcr.MoveTo(posX, posY); texTcr.TextPath(text); texTcr.SetSourceRGB(1, 1, 1); texTcr.FillPreserve(); texTcr.LineWidth = width / 100f / 3; texTcr.SetSourceRGB(0, 0, 0); texTcr.Stroke(); texTcr.Dispose(); return; }
public void ShowResult() { var surface = CairoEx.BuildSurface((int)rect.Width, (int)rect.Height, CairoEx.ColorMetal, Cairo.Format.Rgb24); var context = new Cairo.Context(surface); Draw(context, isClipped); string outputPath = "D:\\LayoutTest"; if (!System.IO.Directory.Exists(outputPath)) { System.IO.Directory.CreateDirectory(outputPath); } string filePath = outputPath + "\\" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss-fff_") + surface.GetHashCode() + ".png"; surface.WriteToPng(filePath); surface.Dispose(); context.Dispose(); Process.Start("rundll32.exe", @"""C:\Program Files\Windows Photo Viewer\PhotoViewer.dll"",ImageView_Fullscreen " + filePath); }
bool RunHandler() { tokenExit: if (token.IsCancellationRequested || mode.TextEditor.GetTextEditorData() == null) { cr.Dispose(); // if the surface was newly created dispose it otherwise it'll leak. if (surface != mode.swapIndicatorSurface) { surface.Dispose(); } return(false); } bool nextStep = false; switch (drawingStep) { case 0: for (int i = 0; i < 10 && !nextStep; i++) { if (token.IsCancellationRequested) { goto tokenExit; } if (mode.TextEditor.HighlightSearchPattern) { mode.GetSearchResultIndicator(state, searchResults, ref nextStep); } else { if (!Debugger.DebuggingService.IsDebugging) { mode.GetQuickTasks(state, allUsages, allTasks, ref nextStep); } else { nextStep = true; } } } if (nextStep) { drawingStep++; nextStep = false; if (!ForceUpdate && state.Equals(mode.currentDrawingState)) { cr.Dispose(); // if the surface was newly created dispose it otherwise it'll leak. if (surface != mode.swapIndicatorSurface) { surface.Dispose(); } IndicatorDrawingState.Dispose(state); return(false); } } return(true); case 1: var displayScale = Core.Platform.IsMac ? GtkWorkarounds.GetScaleFactor(mode) : 1.0; mode.DrawBackground(cr, allocation); drawingStep++; state.taskIterator = 0; state.usageIterator = 0; curIndex = 0; return(true); case 2: for (int i = 0; i < 10 && !nextStep; i++) { if (token.IsCancellationRequested) { goto tokenExit; } if (mode.TextEditor.HighlightSearchPattern) { if (curIndex < state.SearchResultIndicators.Count) { mode.DrawSearchResults(cr, state, curIndex++); } else { nextStep = true; } } else { if (!Debugger.DebuggingService.IsDebugging) { mode.DrawQuickTasks(cr, state, curIndex++, ref nextStep); } else { nextStep = true; } } } if (nextStep) { drawingStep++; } return(true); case 3: if (mode.TextEditor.HighlightSearchPattern) { mode.DrawSearchIndicator(cr); } else { if (!Debugger.DebuggingService.IsDebugging) { mode.DrawIndicator(cr, state.Severity); } } drawingStep++; return(true); default: mode.DrawBreakpoints(cr); cr.Dispose(); var tmp = mode.indicatorSurface; mode.indicatorSurface = surface; mode.swapIndicatorSurface = tmp; IndicatorDrawingState.Dispose(mode.currentDrawingState); mode.currentDrawingState = state; mode.QueueDraw(); return(false); } }
public void Dispose() { _context.Dispose(); _surf.Dispose(); }
bool RunHandler() { tokenExit: if (token.IsCancellationRequested || mode.TextEditor.GetTextEditorData() == null) { cr.Dispose(); // if the surface was newly created dispose it otherwise it'll leak. if (surface != mode.swapIndicatorSurface) { surface.Dispose(); } return(false); } var lineCache = new List <HashSet <int> > (); lineCache.Add(new HashSet <int> ()); lineCache.Add(new HashSet <int> ()); bool nextStep = false; switch (drawingStep) { case 0: CachedDraw(cr, ref mode.backgroundSurface, allocation, draw: (c, o) => mode.DrawBackground(c, allocation)); drawingStep++; return(true); case 1: for (int i = 0; i < 10 && !nextStep; i++) { if (token.IsCancellationRequested) { goto tokenExit; } if (mode.TextEditor.HighlightSearchPattern) { mode.DrawSearchResults(cr, searchResults, ref nextStep); } else { if (!Debugger.DebuggingService.IsDebugging) { mode.DrawQuickTasks(cr, allUsages, allTasks, ref nextStep, ref severity, lineCache); } else { nextStep = true; } } } if (nextStep) { drawingStep++; } return(true); case 2: if (mode.TextEditor.HighlightSearchPattern) { mode.DrawSearchIndicator(cr); } else { if (!Debugger.DebuggingService.IsDebugging) { mode.DrawIndicator(cr, severity); } } drawingStep++; return(true); default: cr.Dispose(); var tmp = mode.indicatorSurface; mode.indicatorSurface = surface; mode.swapIndicatorSurface = tmp; mode.QueueDraw(); return(false); } }
protected override bool OnExposeEvent(Gdk.EventExpose evnt) { Gdk.Rectangle rect = Allocation; //Gdk.Rectangle.Right and Bottom are inconsistent int right = rect.X + rect.Width, bottom = rect.Y + rect.Height; var bcolor = backgroundColorSet ? BackgroundColor : Style.Background(Gtk.StateType.Normal); using (Cairo.Context cr = Gdk.CairoHelper.Create(evnt.Window)) { if (GradientBackround) { cr.NewPath(); cr.MoveTo(rect.X, rect.Y); cr.RelLineTo(rect.Width, 0); cr.RelLineTo(0, rect.Height); cr.RelLineTo(-rect.Width, 0); cr.RelLineTo(0, -rect.Height); cr.ClosePath(); using (Cairo.Gradient pat = new Cairo.LinearGradient(rect.X, rect.Y, rect.X, bottom)) { pat.AddColorStop(0, bcolor.ToCairoColor()); HslColor gcol = bcolor; gcol.L -= 0.1; if (gcol.L < 0) { gcol.L = 0; } pat.AddColorStop(1, gcol); cr.SetSource(pat); cr.Fill(); } } else { if (backgroundColorSet) { Gdk.GC gc = new Gdk.GC(GdkWindow); gc.RgbFgColor = bcolor; evnt.Window.DrawRectangle(gc, true, rect.X, rect.Y, rect.Width, rect.Height); gc.Dispose(); } } cr.Dispose(); } // using base.OnExposeEvent(evnt); using (Cairo.Context cr = Gdk.CairoHelper.Create(evnt.Window)) { cr.SetSourceColor((HslColor)Style.Dark(Gtk.StateType.Normal)); double y = rect.Y + topMargin / 2d; cr.LineWidth = topMargin; cr.Line(rect.X, y, right, y); cr.Stroke(); y = bottom - bottomMargin / 2d; cr.LineWidth = bottomMargin; cr.Line(rect.X, y, right, y); cr.Stroke(); double x = rect.X + leftMargin / 2d; cr.LineWidth = leftMargin; cr.Line(x, rect.Y, x, bottom); cr.Stroke(); x = right - rightMargin / 2d; cr.LineWidth = rightMargin; cr.Line(x, rect.Y, x, bottom); cr.Stroke(); cr.Dispose(); return(false); } }
protected override void Render(Drawable window, Widget widget, Gdk.Rectangle backgroundArea, Gdk.Rectangle cellArea, Gdk.Rectangle exposeArea, CellRendererState flags) { Gdk.GC gc = widget.Style.TextGC(StateType.Normal); // hey, what's this? another way to set color. /*Gdk.PangoRenderer renderer = Gdk.PangoRenderer.GetDefault(gc.Screen); * renderer.Drawable = drawingArea.GdkWindow; * renderer.Gc = drawingArea.Style.BlackGC; * renderer.SetOverrideColor(RenderPart.Foreground, new Gdk.Color(200, 30, 30)); * layout.Alignment = Pango.Alignment.Center; * renderer.DrawLayout(layout, 0, 0); * * renderer.SetOverrideColor(RenderPart.Foreground, Gdk.Color.Zero); * renderer.Drawable = null; * renderer.Gc = null; */ //gc.RgbFgColor = Constants.Colors.Text.Gdk; //gc.RgbBgColor = new Gdk.Color (0, 0, 0); RendererHelper rendererHelper = new RendererHelper(gc, window, widget, exposeArea); rendererHelper.Image(icons[TransactionItem.Direction == DirectionEnum.Recieved ? 0 : 1], 10, 10); TextRenderer textRenderer = new TextRenderer(widget); Cairo.Context context = Gdk.CairoHelper.Create(window); int TEXT_PADDING = 12; int TEXT_PADDING_LEFT = 70; textRenderer.RenderLayoutText(context, TransactionItem.Date.TimeAgo(), 0, TEXT_PADDING + exposeArea.Y, exposeArea.Width, 20, Constants.Colors.Text, Pango.Alignment.Right, Pango.EllipsizeMode.End, -20); textRenderer.RenderLayoutText(context, GetDescrption(), TEXT_PADDING_LEFT, TEXT_PADDING + exposeArea.Y, exposeArea.Width, 20, Constants.Colors.Text, Pango.Alignment.Left, Pango.EllipsizeMode.End); int EXPANTION_SPACE = 50; int ROW_SPACE = 30; int ROW_SPACE2 = 50; int TEXT_PADDING_RIGHT = 20; int HEADER_SIZE = 16; int TEXT_SIZE = 14; if (Expanded) { textRenderer.RenderLayoutText(context, "DATE", TEXT_PADDING_LEFT, EXPANTION_SPACE + TEXT_PADDING + exposeArea.Y, exposeArea.Width, HEADER_SIZE, Constants.Colors.SubText, Pango.Alignment.Left, Pango.EllipsizeMode.End); textRenderer.RenderLayoutText(context, "TO", exposeArea.Width / 2, EXPANTION_SPACE + TEXT_PADDING + exposeArea.Y, exposeArea.Width, HEADER_SIZE, Constants.Colors.SubText, Pango.Alignment.Left, Pango.EllipsizeMode.End); //http://www.csharp-examples.net/string-format-datetime/ textRenderer.RenderLayoutText(context, TransactionItem.Date.ToLongDateString(), TEXT_PADDING_LEFT, EXPANTION_SPACE + ROW_SPACE + TEXT_PADDING + exposeArea.Y, exposeArea.Width, TEXT_SIZE, Constants.Colors.Text, Pango.Alignment.Left, Pango.EllipsizeMode.End); textRenderer.RenderLayoutText(context, TransactionItem.To, exposeArea.Width / 2, EXPANTION_SPACE + ROW_SPACE + TEXT_PADDING + exposeArea.Y, exposeArea.Width, TEXT_SIZE, Constants.Colors.Text, Pango.Alignment.Left, Pango.EllipsizeMode.End); textRenderer.RenderLayoutText(context, "TRANSACTION ID", TEXT_PADDING_LEFT, EXPANTION_SPACE + ROW_SPACE + ROW_SPACE2 + TEXT_PADDING + exposeArea.Y, exposeArea.Width, HEADER_SIZE, Constants.Colors.SubText, Pango.Alignment.Left, Pango.EllipsizeMode.End); textRenderer.RenderLayoutText(context, "FEE", exposeArea.Width / 2, EXPANTION_SPACE + ROW_SPACE + ROW_SPACE2 + TEXT_PADDING + exposeArea.Y, exposeArea.Width, HEADER_SIZE, Constants.Colors.SubText, Pango.Alignment.Left, Pango.EllipsizeMode.End); textRenderer.RenderLayoutText(context, TransactionItem.Id, TEXT_PADDING_LEFT, EXPANTION_SPACE + ROW_SPACE * 2 + ROW_SPACE2 + TEXT_PADDING + exposeArea.Y, exposeArea.Width / 2 - TEXT_PADDING_LEFT - TEXT_PADDING_RIGHT, TEXT_SIZE, Constants.Colors.Text, Pango.Alignment.Left, Pango.EllipsizeMode.End); textRenderer.RenderLayoutText(context, TransactionItem.Fee.ToString() + " " + TransactionItem.Asset, exposeArea.Width / 2, EXPANTION_SPACE + ROW_SPACE * 2 + ROW_SPACE2 + TEXT_PADDING + exposeArea.Y, exposeArea.Width, TEXT_SIZE, Constants.Colors.Text, Pango.Alignment.Left, Pango.EllipsizeMode.End); } context.Dispose(); }