Commit a0f67bbd authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Simplify vertex surface drawing and support arbitrary image types

parent 502c383c
......@@ -91,8 +91,7 @@ enum vertex_shape_t {
SHAPE_DOUBLE_PENTAGON,
SHAPE_DOUBLE_HEXAGON,
SHAPE_DOUBLE_HEPTAGON,
SHAPE_DOUBLE_OCTAGON,
SHAPE_SURFACE
SHAPE_DOUBLE_OCTAGON
};
enum edge_marker_t {
......@@ -448,7 +447,7 @@ public:
cr.get_text_extents(text, extents);
double s = max(extents.width, extents.height) * 1.4;
vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
if (shape >= SHAPE_DOUBLE_CIRCLE && shape != SHAPE_SURFACE)
if (shape >= SHAPE_DOUBLE_CIRCLE)
{
s /= 0.7;
double pw = _attrs.template get<double>(VERTEX_PENWIDTH);
......@@ -500,9 +499,6 @@ public:
case SHAPE_DOUBLE_CIRCLE:
dr = r;
break;
case SHAPE_SURFACE:
dr = get_polygon_anchor(4, r, angle);
break;
default:
throw ValueException("Invalid vertex shape: " +
lexical_cast<string>(int(_attrs.template get<vertex_shape_t>(VERTEX_SHAPE))));
......@@ -567,32 +563,6 @@ public:
vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
switch (shape)
{
case SHAPE_SURFACE:
{
python::object osrc = _attrs.template get<python::object>(VERTEX_SURFACE);
if (osrc != python::object())
{
double swidth, sheight;
PycairoSurface* src = (PycairoSurface*) osrc.ptr();
Cairo::RefPtr<Cairo::Surface> surface(new Cairo::Surface(src->surface));
get_surface_size(surface, swidth, sheight);
Cairo::RefPtr<Cairo::SurfacePattern> pat(Cairo::SurfacePattern::create(surface));
//pat->set_extend(Cairo::EXTEND_REPEAT);
double r = size / sqrt(2);
double scale = r / max(swidth / aspect, sheight);
Cairo::Matrix m = Cairo::identity_matrix();
m.translate(swidth / 2, sheight / 2);
m.scale(1. / scale, 1. / scale);
pat->set_matrix(m);
cr.set_source(pat);
cr.rectangle(-r * aspect / 2, -r / 2, r * aspect, r);
cr.fill();
}
}
break;
case SHAPE_CIRCLE:
case SHAPE_DOUBLE_CIRCLE:
cr.save();
......@@ -645,7 +615,8 @@ public:
lexical_cast<string>(int(_attrs.template get<vertex_shape_t>(VERTEX_SHAPE))));
}
if (shape != SHAPE_SURFACE)
python::object osrc = _attrs.template get<python::object>(VERTEX_SURFACE);
if (osrc == python::object())
{
fillcolor = _attrs.template get<color_t>(VERTEX_FILL_COLOR);
cr.set_source_rgba(get<0>(fillcolor), get<1>(fillcolor),
......@@ -656,6 +627,27 @@ public:
get<3>(color));
cr.stroke();
}
else
{
double swidth, sheight;
PycairoSurface* src = (PycairoSurface*) osrc.ptr();
Cairo::RefPtr<Cairo::Surface> surface(new Cairo::Surface(src->surface));
get_surface_size(surface, swidth, sheight);
Cairo::RefPtr<Cairo::SurfacePattern> pat(Cairo::SurfacePattern::create(surface));
//pat->set_extend(Cairo::EXTEND_REPEAT);
double r = size / sqrt(2);
double scale = r / max(swidth / aspect, sheight);
Cairo::Matrix m = Cairo::identity_matrix();
m.translate(swidth / 2, sheight / 2);
m.scale(1. / scale, 1. / scale);
pat->set_matrix(m);
cr.set_source(pat);
cr.rectangle(-r * aspect / 2, -r / 2, r * aspect, r);
cr.fill();
}
if (text != "")
{
......@@ -1457,8 +1449,7 @@ BOOST_PYTHON_MODULE(libgraph_tool_draw)
.value("double_pentagon", SHAPE_DOUBLE_PENTAGON)
.value("double_hexagon", SHAPE_DOUBLE_HEXAGON)
.value("double_heptagon", SHAPE_DOUBLE_HEPTAGON)
.value("double_octagon", SHAPE_DOUBLE_OCTAGON)
.value("surface", SHAPE_SURFACE);
.value("double_octagon", SHAPE_DOUBLE_OCTAGON);
enum_<edge_marker_t>("edge_marker")
.value("none", MARKER_SHAPE_NONE)
......
......@@ -141,15 +141,6 @@ def open_file(name, mode="r"):
return name, fmt
def gen_surface(name):
fobj, fmt = open_file(name)
if fmt == "png":
sfc = cairo.ImageSurface.create_from_png(fobj)
return sfc
else:
raise ValueError("Cannot guess format of file: " + name)
def surface_from_prop(surface):
if isinstance(surface, PropertyMap):
if surface.key_type() == "v":
......@@ -468,7 +459,7 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
| | "double_circle", "double_triangle", | | |
| | "double_square", "double_pentagon", | | |
| | "double_hexagon", "double_heptagon", | | |
| | "double_octagon" or "surface". | | |
| | "double_octagon". | | |
| | Optionally, this might take a numeric value | | |
| | corresponding to position in the list above. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
......@@ -516,9 +507,7 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
+---------------+---------------------------------------------------+------------------------+----------------------------------+
| surface | The cairo surface used to draw the vertex. If | :class:`cairo.Surface` | ``None`` |
| | the value passed is a string, it is interpreted | or ``str`` | |
| | as a PNG image file name to be loaded. This | | |
| | property is only used if `shape` is set to | | |
| | "surface". | | |
| | as a PNG image file name to be loaded. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
......@@ -800,3 +789,17 @@ except (ImportError, RuntimeError) as e:
msg = "Error importing Gtk module: %s; GTK+ drawing will not work." % str(e)
warnings.filterwarnings("always", msg, ImportWarning)
warnings.warn(msg, ImportWarning)
def gen_surface(name):
fobj, fmt = open_file(name)
if fmt in ["png", "PNG"]:
sfc = cairo.ImageSurface.create_from_png(fobj)
return sfc
else:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(name)
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, pixbuf.get_width(),
pixbuf.get_height())
cr = cairo.Context(surface)
Gdk.cairo_set_source_pixbuf(cr, pixbuf, 0, 0)
cr.paint()
return surface
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment