From sfischme at uwaterloo.ca  Mon Jan 19 06:54:40 2009
From: sfischme at uwaterloo.ca (Sebastian Fischmeister)
Date: Mon, 19 Jan 2009 00:54:40 -0500
Subject: [Rd] patch for textspecial and defaultfont in xfig
Message-ID: <6B180BB8-BB08-45CE-A911-0A67CFDBCBA6@uwaterloo.ca>

Hello,

   The current xfig device lacks the functionality to set the  
textspecial flag and use the defaultfont in xfig. This is necessary  
when you want to export to xfig and use interpreted text (e.g., $ 
\frac{1}{e}$ gets interpreted by latex). The attached patch adds this  
functionality.

Why would you like to do this?

- Use math in labels (e.g., name your variables $r_{xy}$, do a  
barplot, run figtex2pdf.sh [fetch if off the web], and look at the  
nice result]).

- Uses native document font -> uses same font as all the other text in  
your latex document.

- Does not cause troubles with color allocation limit (see pictex  
loading problem).

- Can edit R output figure with a vector drawing program (xfig).

... and of course it now perfectly fits my workflow.

I hope it's useful.

   Sebastian

Index: src/library/grDevices/R/postscript.R
===================================================================
--- src/library/grDevices/R/postscript.R	(revision 47637)
+++ src/library/grDevices/R/postscript.R	(working copy)
@@ -273,7 +273,7 @@
                    paper = "default", horizontal = TRUE,
                    width = 0, height = 0, family = "Helvetica",
                    pointsize = 12, bg = "transparent", fg = "black",
-                  pagecentre = TRUE)
+                  pagecentre = TRUE, defaultfont = FALSE, textspecial  
= FALSE)
  {
      ## do initialization if needed
      initPSandPDFfonts()
@@ -281,7 +281,7 @@
      if(!checkIntFormat(file)) stop("invalid 'file'")
      .External(XFig, file, paper, family, bg, fg,
                width, height, horizontal, pointsize,
-              onefile, pagecentre, encoding)
+              onefile, pagecentre, defaultfont, textspecial, encoding)
      invisible()
  }

Index: src/library/grDevices/src/devPS.c
===================================================================
--- src/library/grDevices/src/devPS.c	(revision 47637)
+++ src/library/grDevices/src/devPS.c	(working copy)
@@ -4354,6 +4354,9 @@
      int ymax;            /* used to invert coord system */
      char encoding[50];   /* for writing text */

+    Rboolean textspecial; /* use textspecial flag in xfig for latex  
integration */
+    Rboolean defaultfont; /* use the default font in xfig */
+
      /*
       * Fonts and encodings used on the device
       *
@@ -4553,6 +4556,7 @@
  		 double width, double height,
  		 Rboolean horizontal, double ps,
  		 Rboolean onefile, Rboolean pagecentre,
+		 Rboolean defaultfont, Rboolean textspecial,
  		 const char *encoding)
  {
      /* If we need to bail out with some sort of "error" */
@@ -4591,6 +4595,8 @@
      pd->width = width;
      pd->height = height;
      pd->landscape = horizontal;
+    pd->textspecial = textspecial;
+    pd->defaultfont = defaultfont;
      pointsize = floor(ps);
      if(R_TRANSPARENT(pd->bg) && R_TRANSPARENT(pd->col)) {
  	free(dd);
@@ -5108,7 +5114,7 @@
  	fprintf(fp, "4 %d ", (int)floor(2*hadj)); /* Text, how justified */
  	fprintf(fp, "%d 100 0 ", XF_SetColor(gc->col, pd));
  	/* color, depth, pen_style */
-	fprintf(fp, "%d %d %.4f 4 ", fontnum, (int)size, rot * DEG2RAD);
+	fprintf(fp, "%d %d %.4f %d ", pd->defaultfont?-1:fontnum, (int)size,  
rot * DEG2RAD,pd->textspecial?6:4);
  	/* font pointsize angle flags (Postscript font) */
  	fprintf(fp, "%d %d ", (int)(size*12),
  		(int)(16.667*XFig_StrWidth(str, gc, dd) +0.5));
@@ -7441,6 +7447,9 @@
   *  ps		= pointsize
   *  onefile     = {TRUE: normal; FALSE: single EPSF page}
   *  pagecentre  = centre plot region on paper?
+ *  defaultfont = {TRUE: use xfig default font; FALSE: use R font}
+ *  textspecial = {TRUE: use textspecial; FALSE: use standard font}
+ *
   *  encoding
   */

@@ -7449,7 +7458,7 @@
      pGEDevDesc gdd;
      char *vmax;
      const char *file, *paper, *family, *bg, *fg, *encoding;
-    int horizontal, onefile, pagecentre;
+    int horizontal, onefile, pagecentre, defaultfont, textspecial;
      double height, width, ps;

      vmax = vmaxget();
@@ -7467,6 +7476,8 @@
      ps = asReal(CAR(args));	      args = CDR(args);
      onefile = asLogical(CAR(args));   args = CDR(args);
      pagecentre = asLogical(CAR(args));args = CDR(args);
+    defaultfont = asLogical(CAR(args)); args = CDR(args);
+    textspecial = asLogical(CAR(args)); args = CDR(args);
      encoding = CHAR(asChar(CAR(args)));

      R_GE_checkVersionOrDie(R_GE_version);
@@ -7476,7 +7487,7 @@
  	if (!(dev = (pDevDesc) calloc(1, sizeof(DevDesc))))
  	    return 0;
  	if(!XFigDeviceDriver(dev, file, paper, family, bg, fg, width, height,
-			     (double) horizontal, ps, onefile, pagecentre,
+			     (double) horizontal, ps, onefile, pagecentre, defaultfont,  
textspecial,
  			     encoding)) {
  	    /* free(dev); No, freed inside XFigDeviceDriver */
  	    error(_("unable to start device xfig"));
Index: src/library/grDevices/man/xfig.Rd
===================================================================
--- src/library/grDevices/man/xfig.Rd	(revision 47637)
+++ src/library/grDevices/man/xfig.Rd	(working copy)
@@ -20,7 +20,7 @@
       paper = "default", horizontal = TRUE,
       width = 0, height = 0, family = "Helvetica",
       pointsize = 12, bg = "transparent", fg = "black",
-     pagecentre = TRUE)
+     pagecentre = TRUE, defaultfont = FALSE, textspecial = FALSE)
  }
  \arguments{
    \item{file}{a character string giving the name of the file.   For  
use
@@ -60,6 +60,10 @@
    \item{fg}{the initial foreground color to be used.}
    \item{pagecentre}{logical: should the device region be centred on  
the
      page?}
+  \item{defaultfont}{logical: should the device use xfig's default
+    font?}
+  \item{textspecial}{logical: should the device set the textspecial  
flag
+  for all text elements. This is useful when generating pstex from  
xfig figures.}
  }
  %%>>> REAL DOC in SOURCE:    ../../../main/devices.c  &   ../../../ 
unix/devPS.c
  \details{



[Rd] patch for textspecial and defaultfont in xfig
Sebastian Fischmeister