Skip to content

Commit

Permalink
Merge pull request #22 from ljubobratovicrelja/ggplotd-gtk-drawToSurface
Browse files Browse the repository at this point in the history
Improved interfacing with GtkD for GGPlotD.drawToSurface
  • Loading branch information
BlackEdder authored Jun 21, 2016
2 parents 6607012 + e0a2b96 commit 3ac24b6
Showing 1 changed file with 81 additions and 1 deletion.
82 changes: 81 additions & 1 deletion source/ggplotd/ggplotd.d
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,17 @@ struct Margins
/// GGPlotD contains the needed information to create a plot
struct GGPlotD
{
/// Draw the plot to a cairo surface
/**
Draw the plot to a cairoD cairo surface.
Params:
surface = Surface object of type cairo.Surface from cairoD library, on top of which this plot is drawn.
width = Width of the given surface.
height = Height of the given surface.
Returns:
Resulting surface of the same type as input surface, with this plot drawn on top of it.
*/
auto drawToSurface( ref cairo.Surface surface, int width, int height ) const
{
import std.range : empty, front;
Expand Down Expand Up @@ -233,6 +243,35 @@ struct GGPlotD
return surface;
}

version(ggplotdGTK)
{
import gtkdSurface = cairo.Surface; // cairo surface module in GtkD package.

/**
Draw the plot to a GtkD cairo surface.
Params:
surface = Surface object of type cairo.Surface from GtkD library, on top of which this plot is drawn.
width = Width of the given surface.
height = Height of the given surface.
Returns:
Resulting surface of the same type as input surface, with this plot drawn on top of it.
*/
auto drawToSurface( ref gtkdSurface.Surface surface, int width, int height ) const
{
import gtkc = gtkc.cairotypes;
import cairod = cairo.c.cairo;

alias gtkd_surface_t = gtkc.cairo_surface_t;
alias cairod_surface_t = cairod.cairo_surface_t;

cairo.Surface cairodSurface = new cairo.Surface(cast(cairod_surface_t*)surface.getSurfaceStruct());
drawToSurface(cairodSurface, width, height);

return surface;
}
}

/// save the plot to a file
void save( string fname, int width = 470, int height = 470 ) const
Expand Down Expand Up @@ -349,6 +388,47 @@ unittest
assertEqual( dim, gg.geomRange.data.length );
}

version(ggplotdGTK)
{
unittest
{
// Draw same plot on cairod.ImageSurface, and on gtkd.cairo.ImageSurface,
// and prove resulting images are the same.

import ggplotd.geom;
import ggplotd.aes;

import gtkSurface = cairo.Surface;
import gtkImageSurface = cairo.ImageSurface;
import gtkCairoTypes = gtkc.cairotypes;

const win_width = 1024;
const win_height = 1024;

const radius = 400.;

auto line_aes11 = Aes!(double[], "x", double[], "y")( [ 0, radius*0.45 ], [ 0, radius*0.45]);
auto line_aes22 = Aes!(double[], "x", double[], "y")( [ 300, radius*0.45 ], [ 210, radius*0.45]);

auto gg = GGPlotD();
gg.put( geomLine(line_aes11) );
gg.put( geomLine(line_aes22) );

cairo.Surface cairodSurface = new cairo.ImageSurface(cairo.Format.CAIRO_FORMAT_RGB24, win_width, win_height);
gtkSurface.Surface gtkdSurface = gtkImageSurface.ImageSurface.create(gtkCairoTypes.cairo_format_t.RGB24, win_width, win_height);

auto cairodImageSurface = cast(cairo.ImageSurface)cairodSurface;
auto gtkdImageSurface = cast(gtkImageSurface.ImageSurface)gtkdSurface;

gg.drawToSurface(cairodSurface, win_width, win_height);
gg.drawToSurface(gtkdSurface, win_width, win_height);

auto byteSize = win_width*win_height*4;

assertEqual(cairodImageSurface.getData()[0..byteSize], gtkdImageSurface.getData()[0..byteSize]);
}
}

unittest
{
auto gg = GGPlotD()
Expand Down

0 comments on commit 3ac24b6

Please sign in to comment.