Browse code

Improve handling of multi-line node/edge labels.

Graphviz stores each line separately in u.txt.para, therefore RGraphviz only
returned the first line. When using the common workaround of \\\n in the
label, it did not get layouted properly and the codepath for labelsize > nodesize
kicked in, decreasing overall font size, which is very annoying when using
fixedsize=FALSE.

Ilia Kats authored on 28/04/2014 17:23:04
Showing2 changed files

... ...
@@ -4,7 +4,12 @@ setMethod("getPoints", "xyPoint", function(object) c(object@x, object@y))
4 4
 setMethod("show", "xyPoint", function(object)
5 5
           cat(paste("x: ", object@x, ", y: ", object@y, "\n", sep="")))
6 6
 
7
-setMethod("labelText", "AgTextLabel", function(object) object@labelText)
7
+setMethod("labelText", "AgTextLabel", function(object) {
8
+    text <- object@labelText
9
+    text <- gsub('\\n', '\n', text, fixed=T)
10
+    text <- gsub('\\r', '\r', text, fixed=T) # only these two as per Graphviz lib/common/labels.c:make_simple_label
11
+    return(text)
12
+})
8 13
 setMethod("labelColor", "AgTextLabel", function(object) object@labelColor)
9 14
 setMethod("labelLoc", "AgTextLabel", function(object) object@labelLoc)
10 15
 setMethod("labelJust", "AgTextLabel", function(object) object@labelJust)
... ...
@@ -116,7 +116,7 @@ SEXP getEdgeLocs(Agraph_t *g) {
116 116
             if (edge->u.label != NULL) {
117 117
                 PROTECT(curLab = NEW_OBJECT(labClass));
118 118
                 SET_SLOT(curLab, Rf_install("labelText"),
119
-                         Rgraphviz_ScalarStringOrNull(ED_label(edge)->u.txt.para->str));
119
+                         Rgraphviz_ScalarStringOrNull(ED_label(edge)->text));
120 120
                 /* Get the X/Y location of the label */
121 121
                 PROTECT(curXY = NEW_OBJECT(xyClass));
122 122
 #if GRAPHVIZ_MAJOR == 2 && GRAPHVIZ_MINOR > 20
... ...
@@ -196,10 +196,10 @@ SEXP getNodeLayouts(Agraph_t *g) {
196 196
 
197 197
         PROTECT(curLab = NEW_OBJECT(labClass));
198 198
 
199
-	if (ND_label(node)  == NULL) {
200
-	} else if (ND_label(node)->u.txt.para != NULL) {
199
+        if (ND_label(node)  == NULL) {
200
+        } else if (ND_label(node)->u.txt.para != NULL) {
201 201
             SET_SLOT(curLab, Rf_install("labelText"),
202
-                     Rgraphviz_ScalarStringOrNull(ND_label(node)->u.txt.para->str));
202
+                     Rgraphviz_ScalarStringOrNull(ND_label(node)->text));
203 203
             snprintf(tmpString, 2, "%c",ND_label(node)->u.txt.para->just);
204 204
             SET_SLOT(curLab, Rf_install("labelJust"), Rgraphviz_ScalarStringOrNull(tmpString));
205 205