Date: 2013-07-02T14:38:20+00:00

Actual source code: tikz.c
petsc-3.4.2 2013-07-02

      2: /*
      3:     Defines the operations for the X PetscDraw implementation.
      4: */

      6: #include <petsc-private/drawimpl.h>         /*I  "petscsys.h" I*/

      8: typedef struct {
      9:   char      *filename;
     10:   FILE      *fd;
     11:   PetscBool written;  /* something has been written to the current frame */
     12: } PetscDraw_TikZ;

     14: #define TikZ_BEGIN_DOCUMENT  "\\documentclass{beamer}\n\n\
     15: \\usepackage{tikz}\n\
     16: \\usepackage{pgflibraryshapes}\n\
     17: \\usetikzlibrary{backgrounds}\n\
     18: \\usetikzlibrary{arrows}\n\
     19: \\newenvironment{changemargin}[2]{%%\n\
     20:   \\begin{list}{}{%%\n\
     21:     \\setlength{\\topsep}{0pt}%%\n\
     22:     \\setlength{\\leftmargin}{#1}%%\n\
     23:     \\setlength{\\rightmargin}{#2}%%\n\
     24:     \\setlength{\\listparindent}{\\parindent}%%\n\
     25:     \\setlength{\\itemindent}{\\parindent}%%\n\
     26:     \\setlength{\\parsep}{\\parskip}%%\n\
     27:   }%%\n\
     28:   \\item[]}{\\end{list}}\n\n\
     29: \\begin{document}\n"

     31: #define TikZ_BEGIN_FRAME "\\begin{frame}{}\n\
     32: \\begin{changemargin}{-1cm}{0cm}\n\
     33: \\begin{center}\n\
     34: \\begin{tikzpicture}[scale = 10.00,font=\\fontsize{8}{8}\\selectfont]\n"

     36: #define TikZ_END_FRAME "\\end{tikzpicture}\n\
     37: \\end{center}\n\
     38: \\end{changemargin}\n\
     39: \\end{frame}\n"

     41: #define TikZ_END_DOCUMENT  "\\end{document}\n"

     45: PetscErrorCode  PetscDrawDestroy_TikZ(PetscDraw draw)
     46: {
     47:   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;

     51:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_FRAME);
     52:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_DOCUMENT);
     53:   PetscFClose(PetscObjectComm((PetscObject)draw),win->fd);
     54:   PetscFree(win->filename);
     55:   PetscFree(win);
     56:   return(0);
     57: }

     59: static const char *TikZColors[] = { "white",  "black", "red",  "green", "cyan",   "blue", "magenta", 0, 0, "orange",
     60:                                     "violet", "brown", "pink", 0,       "yellow", 0};

     62: PETSC_STATIC_INLINE const char *TikZColorMap(int cl)
     63: {
     64:   return((cl < 16) ? (TikZColors[cl] ? TikZColors[cl] : "black") : "black");
     65: }

     67: /*
     68:      These macros transform from the users coordinates to the (0,0) -> (1,1) coordinate system
     69: */
     70: #define XTRANS(draw,x)  (double)(((draw)->port_xl + (((x - (draw)->coor_xl)*((draw)->port_xr - (draw)->port_xl))/((draw)->coor_xr - (draw)->coor_xl))))
     71: #define YTRANS(draw,y)  (double)(((draw)->port_yl + (((y - (draw)->coor_yl)*((draw)->port_yr - (draw)->port_yl))/((draw)->coor_yr - (draw)->coor_yl))))

     75: PetscErrorCode PetscDrawClear_TikZ(PetscDraw draw)
     76: {
     77:   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;

     81:   /* often PETSc generates unneeded clears, we want avoid creating empy pictures for them */
     82:   if (!win->written) return(0);
     83:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_FRAME);
     84:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);
     85:   return(0);
     86: }

     90: PetscErrorCode PetscDrawLine_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
     91: {
     92:   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;

     96:   win->written = PETSC_TRUE;
     97:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\draw [%s] (%g,%g) --(%g,%g);\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),XTRANS(draw,xr),YTRANS(draw,yr));
     98:   return(0);
     99: }

    103: PetscErrorCode PetscDrawString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
    104: {
    105:   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;

    109:   win->written = PETSC_TRUE;
    110:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\node [above right, %s] at (%g,%g) {%s};\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),text);
    111:   return(0);
    112: }

    116: /*
    117:     Does not handle multiline strings correctly
    118: */
    119: PetscErrorCode PetscDrawBoxedString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,int ct,const char text[],PetscReal *w,PetscReal *h)
    120: {
    121:   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
    123:   size_t         len;

    126:   win->written = PETSC_TRUE;
    127:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\draw (%g,%g) node [rectangle, draw, align=center, inner sep=1ex] {%s};\n",XTRANS(draw,xl),YTRANS(draw,yl),text);

    129:   /* make up totally bogus height and width of box */
    130:   PetscStrlen(text,&len);
    131:   if (w) *w = .07*len;
    132:   if (h) *h = .07;
    133:   return(0);
    134: }

    136: static struct _PetscDrawOps DvOps = { 0,
    137:                                       0,
    138:                                       PetscDrawLine_TikZ,
    139:                                       0,
    140:                                       0,
    141:                                       0,
    142:                                       0,
    143:                                       PetscDrawString_TikZ,
    144:                                       0,
    145:                                       0,
    146:                                       0,
    147:                                       0,
    148:                                       PetscDrawClear_TikZ,
    149:                                       0,
    150:                                       0,
    151:                                       0,
    152:                                       0,
    153:                                       0,
    154:                                       0,
    155:                                       0,
    156:                                       0,
    157:                                       0,
    158:                                       0,
    159:                                       0,
    160:                                       0,
    161:                                       0,
    162:                                       PetscDrawDestroy_TikZ,
    163:                                       0,
    164:                                       0,
    165:                                       0,
    166:                                       0,
    167:                                       0,
    168:                                       0,
    169:                                       0,
    170:                                       0,
    171:                                       0,
    172:                                       0,
    173:                                       PetscDrawBoxedString_TikZ};

    177: PETSC_EXTERN PetscErrorCode PetscDrawCreate_TikZ(PetscDraw draw)
    178: {
    179:   PetscDraw_TikZ *win;

    183:   PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));
    184:   PetscNew(PetscDraw_TikZ,&win);
    185:   PetscLogObjectMemory(draw,sizeof(PetscDraw_TikZ));

    187:   draw->data = (void*) win;

    189:   if (draw->title) {
    190:     PetscStrallocpy(draw->title,&win->filename);
    191:   } else {
    192:     const char *fname;
    193:     PetscObjectGetName((PetscObject)draw,&fname);
    194:     PetscStrallocpy(fname,&win->filename);
    195:   }
    196:   PetscFOpen(PetscObjectComm((PetscObject)draw),win->filename,"w",&win->fd);
    197:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_DOCUMENT);
    198:   PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);

    200:   win->written = PETSC_FALSE;
    201:   return(0);
    202: }