Commit 75418ae1 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

graph_draw(): implement text outline color and width

parent 1e3b2e38
......@@ -66,6 +66,8 @@ enum vertex_attr_t {
VERTEX_TEXT_POSITION,
VERTEX_TEXT_ROTATION,
VERTEX_TEXT_OFFSET,
VERTEX_TEXT_OUT_WIDTH,
VERTEX_TEXT_OUT_COLOR,
VERTEX_FONT_FAMILY,
VERTEX_FONT_SLANT,
VERTEX_FONT_WEIGHT,
......@@ -90,6 +92,8 @@ enum edge_attr_t {
EDGE_TEXT_COLOR,
EDGE_TEXT_DISTANCE,
EDGE_TEXT_PARALLEL,
EDGE_TEXT_OUT_WIDTH,
EDGE_TEXT_OUT_COLOR,
EDGE_FONT_FAMILY,
EDGE_FONT_SLANT,
EDGE_FONT_WEIGHT,
......@@ -130,7 +134,7 @@ typedef pair<double, double> pos_t;
typedef std::tuple<double, double, double, double> color_t;
typedef gt_hash_map<int, boost::any> attrs_t;
typedef boost::mpl::map43<
typedef boost::mpl::map47<
boost::mpl::pair<boost::mpl::int_<VERTEX_SHAPE>, vertex_shape_t>,
boost::mpl::pair<boost::mpl::int_<VERTEX_COLOR>, color_t>,
boost::mpl::pair<boost::mpl::int_<VERTEX_FILL_COLOR>, color_t>,
......@@ -147,6 +151,8 @@ typedef boost::mpl::map43<
boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_POSITION>, double>,
boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_ROTATION>, double>,
boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_OFFSET>, vector<double> >,
boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_OUT_WIDTH>, double>,
boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_OUT_COLOR>, color_t>,
boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_FAMILY>, string>,
boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_SLANT>, int32_t>,
boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_WEIGHT>, int32_t>,
......@@ -168,6 +174,8 @@ typedef boost::mpl::map43<
boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_COLOR>, color_t>,
boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_DISTANCE>, double>,
boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_PARALLEL>, uint8_t>,
boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_OUT_WIDTH>, double>,
boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_OUT_COLOR>, color_t>,
boost::mpl::pair<boost::mpl::int_<EDGE_FONT_FAMILY>, string>,
boost::mpl::pair<boost::mpl::int_<EDGE_FONT_SLANT>, int32_t>,
boost::mpl::pair<boost::mpl::int_<EDGE_FONT_WEIGHT>, int32_t>,
......@@ -1011,10 +1019,20 @@ public:
cr.translate(anchor.first, anchor.second);
}
cr.text_path(text);
color = _attrs.template get<color_t>(VERTEX_TEXT_OUT_COLOR);
cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
get<3>(color));
cr.set_line_width(_attrs.template get<double>(VERTEX_TEXT_OUT_WIDTH));
cr.stroke();
cr.begin_new_path();
color = _attrs.template get<color_t>(VERTEX_TEXT_COLOR);
cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
get<3>(color));
cr.show_text(text);
cr.begin_new_path();
cr.restore();
}
......@@ -1366,10 +1384,18 @@ public:
cr.translate(text_dist, 0);
}
color = _attrs.template get<color_t>(EDGE_TEXT_OUT_COLOR);
cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
get<3>(color));
cr.set_line_width(_attrs.template get<double>(EDGE_TEXT_OUT_WIDTH));
cr.stroke();
cr.begin_new_path();
color = _attrs.template get<color_t>(EDGE_TEXT_COLOR);
cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
get<3>(color));
cr.show_text(text);
cr.begin_new_path();
cr.restore();
}
......@@ -2188,6 +2214,8 @@ BOOST_PYTHON_MODULE(libgraph_tool_draw)
.value("text_position", VERTEX_TEXT_POSITION)
.value("text_rotation", VERTEX_TEXT_ROTATION)
.value("text_offset", VERTEX_TEXT_OFFSET)
.value("text_out_color", VERTEX_TEXT_OUT_COLOR)
.value("text_out_width", VERTEX_TEXT_OUT_WIDTH)
.value("font_family", VERTEX_FONT_FAMILY)
.value("font_slant", VERTEX_FONT_SLANT)
.value("font_weight", VERTEX_FONT_WEIGHT)
......@@ -2211,6 +2239,8 @@ BOOST_PYTHON_MODULE(libgraph_tool_draw)
.value("text_color", EDGE_TEXT_COLOR)
.value("text_distance", EDGE_TEXT_DISTANCE)
.value("text_parallel", EDGE_TEXT_PARALLEL)
.value("text_out_color", EDGE_TEXT_OUT_COLOR)
.value("text_out_width", EDGE_TEXT_OUT_WIDTH)
.value("font_family", EDGE_FONT_FAMILY)
.value("font_slant", EDGE_FONT_SLANT)
.value("font_weight", EDGE_FONT_WEIGHT)
......
......@@ -118,6 +118,8 @@ _vdefaults = {
"text_position": -1.,
"text_rotation": 0.,
"text_offset": [0., 0.],
"text_out_width": .1,
"text_out_color": [0., 0., 0., 0.],
"font_family": "serif",
"font_slant": cairo.FONT_SLANT_NORMAL,
"font_weight": cairo.FONT_WEIGHT_NORMAL,
......@@ -142,6 +144,8 @@ _edefaults = {
"text_color": (0., 0., 0., 1.),
"text_distance": 5,
"text_parallel": True,
"text_out_width": .1,
"text_out_color": [0., 0., 0., 0.],
"font_family": "serif",
"font_slant": cairo.FONT_SLANT_NORMAL,
"font_weight": cairo.FONT_WEIGHT_NORMAL,
......@@ -167,10 +171,12 @@ _vtypes = {
"text_position": "double",
"text_rotation": "double",
"text_offset": "vector<double>",
"text_out_width": "double",
"text_out_color": "vector<double>",
"font_family": "string",
"font_slant": "int",
"font_weight": "int",
"font_size": "float",
"font_size": "double",
"surface": "object",
"pie_fractions": "vector<double>",
"pie_colors": "vector<double>"
......@@ -191,6 +197,8 @@ _etypes = {
"text_color": "vector<double>",
"text_distance": "double",
"text_parallel": "bool",
"text_out_width": "double",
"text_out_color": "vector<double>",
"font_family": "string",
"font_slant": "int",
"font_weight": "int",
......@@ -394,8 +402,9 @@ def _convert(attr, val, cmap, pmap_default=False, g=None, k=None):
else:
return new_val
elif attr in [vertex_attrs.color, vertex_attrs.fill_color,
vertex_attrs.text_color, vertex_attrs.halo_color,
edge_attrs.color, edge_attrs.text_color]:
vertex_attrs.text_color, vertex_attrs.text_out_color,
vertex_attrs.halo_color, edge_attrs.color,
edge_attrs.text_color, edge_attrs.text_out_color]:
if isinstance(val, list):
new_val = val
elif isinstance(val, (tuple, np.ndarray)):
......@@ -828,12 +837,11 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
Notes
-----
.. table:: **List of vertex properties**
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| Name | Description | Accepted types | Default Value |
+===============+===================================================+========================+==================================+
+================+===================================================+========================+==================================+
| shape | The vertex shape. Can be one of the following | ``str`` or ``int`` | ``"circle"`` |
| | strings: "circle", "triangle", "square", | | |
| | "pentagon", "hexagon", "heptagon", "octagon" | | |
......@@ -843,44 +851,44 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
| | "double_octagon", "pie", "none". | | |
| | Optionally, this might take a numeric value | | |
| | corresponding to position in the list above. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| color | Color used to stroke the lines of the vertex. | ``str`` or list of | ``[0., 0., 0., 1]`` |
| | | ``floats`` | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| fill_color | Color used to fill the interior of the vertex. | ``str`` or list of | ``[0.640625, 0, 0, 0.9]`` |
| | | ``floats`` | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| size | The size of the vertex, in the default units of | ``float`` or ``int`` | ``5`` |
| | the output format (normally either pixels or | | |
| | points). | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| aspect | The aspect ratio of the vertex. | ``float`` or ``int`` | ``1.0`` |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| rotation | Angle (in radians) to rotate the vertex. | ``float`` | ``0.`` |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| anchor | Specifies how the edges anchor to the vertices. | ``int`` | ``1`` |
| | If `0`, the anchor is at the center of the vertex,| | |
| | otherwise it is at the border. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| pen_width | Width of the lines used to draw the vertex, in | ``float`` or ``int`` | ``0.8`` |
| | the default units of the output format (normally | | |
| | either pixels or points). | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| halo | Whether to draw a circular halo around the | ``bool`` | ``False`` |
| | vertex. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| halo_color | Color used to draw the halo. | ``str`` or list of | ``[0., 0., 1., 0.5]`` |
| | | ``floats`` | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| halo_size | Relative size of the halo. | ``float`` | ``1.5`` |
| | | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| text | Text to draw together with the vertex. | ``str`` | ``""`` |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| text_color | Color used to draw the text. If the value is | ``str`` or list of | ``"auto"`` |
| | ``"auto"``, it will be computed based on | ``floats`` | |
| | fill_color to maximize contrast. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| text_position | Position of the text relative to the vertex. | ``float`` or ``int`` | ``-1`` |
| | If the passed value is positive, it will | or ``"centered"`` | |
| | correspond to an angle in radians, which will | | |
......@@ -891,31 +899,36 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
| | increased to accommodate the text. The special | | |
| | value ``"centered"`` positions the texts rotated | | |
| | radially around the center of mass. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| text_offset | Text position offset. | list of ``float`` | ``[0.0, 0.0]`` |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| text_rotation | Angle of rotation (in radians) for the text. | ``float`` | ``0.0`` |
| | The center of rotation is the position of the | | |
| | vertex. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| text_out_color | Color used to draw the text outline. | ``str`` or list of | ``[0,0,0,0]`` |
| | | ``floats`` | |
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| text_out_width | Width of the text outline. | ``float`` | ``1.`` |
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| font_family | Font family used to draw the text. | ``str`` | ``"serif"`` |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| font_slant | Font slant used to draw the text. | ``cairo.FONT_SLANT_*`` | :data:`cairo.FONT_SLANT_NORMAL` |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| font_weight | Font weight used to draw the text. | ``cairo.FONT_WEIGHT_*``| :data:`cairo.FONT_WEIGHT_NORMAL` |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| font_size | Font size used to draw the text. | ``float`` or ``int`` | ``12`` |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| 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 an image file name to be loaded. | | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| pie_fractions | Fractions of the pie sections for the vertices if | list of ``int`` or | ``[0.75, 0.25]`` |
| | ``shape=="pie"``. | ``float`` | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| pie_colors | Colors used in the pie sections if | list of strings or | ``('b','g','r','c','m','y','k')``|
| | ``shape=="pie"``. | ``float``. | |
+---------------+---------------------------------------------------+------------------------+----------------------------------+
+----------------+---------------------------------------------------+------------------------+----------------------------------+
.. table:: **List of edge properties**
......@@ -967,6 +980,11 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
| text_parallel | If ``True`` the text will be drawn parallel to | ``bool`` | ``True`` |
| | the edges. | | |
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| text_out_color | Color used to draw the text outline. | ``str`` or list of | ``[0,0,0,0]`` |
| | | ``floats`` | |
+--------------- +---------------------------------------------------+------------------------+----------------------------------+
| text_out_width | Width of the text outline. | ``float`` | ``1.`` |
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| font_family | Font family used to draw the text. | ``str`` | ``"serif"`` |
+----------------+---------------------------------------------------+------------------------+----------------------------------+
| font_slant | Font slant used to draw the text. | ``cairo.FONT_SLANT_*`` | :data:`cairo.FONT_SLANT_NORMAL` |
......
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