xref: /petsc/src/sys/classes/draw/impls/tikz/tikz.c (revision d6ed00de919ff7f8a2b466946ae4ac73c6db4d9a)
142963b84SBarry Smith 
242963b84SBarry Smith /*
342963b84SBarry Smith     Defines the operations for the X PetscDraw implementation.
442963b84SBarry Smith */
542963b84SBarry Smith 
642963b84SBarry Smith #include <petsc-private/drawimpl.h>         /*I  "petscsys.h" I*/
742963b84SBarry Smith 
842963b84SBarry Smith typedef struct {
942963b84SBarry Smith   char      *filename;
1042963b84SBarry Smith   FILE      *fd;
1142963b84SBarry Smith   PetscBool written;  /* something has been written to the current frame */
1242963b84SBarry Smith } PetscDraw_TikZ;
1342963b84SBarry Smith 
1442963b84SBarry Smith #define TikZ_BEGIN_DOCUMENT  "\\documentclass{beamer}\n\n\
1542963b84SBarry Smith \\usepackage{tikz}\n\
1642963b84SBarry Smith \\usepackage{pgflibraryshapes}\n\
1742963b84SBarry Smith \\usetikzlibrary{backgrounds}\n\
1842963b84SBarry Smith \\usetikzlibrary{arrows}\n\
1942963b84SBarry Smith \\newenvironment{changemargin}[2]{%%\n\
2042963b84SBarry Smith   \\begin{list}{}{%%\n\
2142963b84SBarry Smith     \\setlength{\\topsep}{0pt}%%\n\
2242963b84SBarry Smith     \\setlength{\\leftmargin}{#1}%%\n\
2342963b84SBarry Smith     \\setlength{\\rightmargin}{#2}%%\n\
2442963b84SBarry Smith     \\setlength{\\listparindent}{\\parindent}%%\n\
2542963b84SBarry Smith     \\setlength{\\itemindent}{\\parindent}%%\n\
2642963b84SBarry Smith     \\setlength{\\parsep}{\\parskip}%%\n\
2742963b84SBarry Smith   }%%\n\
2842963b84SBarry Smith   \\item[]}{\\end{list}}\n\n\
2942963b84SBarry Smith \\begin{document}\n"
3042963b84SBarry Smith 
3142963b84SBarry Smith #define TikZ_BEGIN_FRAME "\\begin{frame}{}\n\
3242963b84SBarry Smith \\begin{changemargin}{-1cm}{0cm}\n\
3342963b84SBarry Smith \\begin{center}\n\
3442963b84SBarry Smith \\begin{tikzpicture}[scale = 10.00,font=\\fontsize{8}{8}\\selectfont]\n"
3542963b84SBarry Smith 
3642963b84SBarry Smith #define TikZ_END_FRAME "\\end{tikzpicture}\n\
3742963b84SBarry Smith \\end{center}\n\
3842963b84SBarry Smith \\end{changemargin}\n\
3942963b84SBarry Smith \\end{frame}\n"
4042963b84SBarry Smith 
4142963b84SBarry Smith #define TikZ_END_DOCUMENT  "\\end{document}\n"
4242963b84SBarry Smith 
4342963b84SBarry Smith #undef __FUNCT__
4442963b84SBarry Smith #define __FUNCT__ "PetscDrawDestroy_TikZ"
4542963b84SBarry Smith PetscErrorCode  PetscDrawDestroy_TikZ(PetscDraw draw)
4642963b84SBarry Smith {
4742963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
4842963b84SBarry Smith   PetscErrorCode ierr;
4942963b84SBarry Smith 
5042963b84SBarry Smith   PetscFunctionBegin;
51ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_FRAME);CHKERRQ(ierr);
52ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_DOCUMENT);CHKERRQ(ierr);
53ce94432eSBarry Smith   ierr = PetscFClose(PetscObjectComm((PetscObject)draw),win->fd);CHKERRQ(ierr);
5442963b84SBarry Smith   ierr = PetscFree(win->filename);CHKERRQ(ierr);
5542963b84SBarry Smith   ierr = PetscFree(win);CHKERRQ(ierr);
5642963b84SBarry Smith   PetscFunctionReturn(0);
5742963b84SBarry Smith }
5842963b84SBarry Smith 
5942963b84SBarry Smith static const char *TikZColors[] = { "white",  "black", "red",  "green", "cyan",   "blue", "magenta", 0, 0, "orange",
6042963b84SBarry Smith                                     "violet", "brown", "pink", 0,       "yellow", 0};
6142963b84SBarry Smith 
6242963b84SBarry Smith PETSC_STATIC_INLINE const char *TikZColorMap(int cl)
6342963b84SBarry Smith {
6442963b84SBarry Smith   return((cl < 16) ? (TikZColors[cl] ? TikZColors[cl] : "black") : "black");
6542963b84SBarry Smith }
6642963b84SBarry Smith 
6742963b84SBarry Smith /*
6842963b84SBarry Smith      These macros transform from the users coordinates to the (0,0) -> (1,1) coordinate system
6942963b84SBarry Smith */
7042963b84SBarry Smith #define XTRANS(draw,x)  (double)(((draw)->port_xl + (((x - (draw)->coor_xl)*((draw)->port_xr - (draw)->port_xl))/((draw)->coor_xr - (draw)->coor_xl))))
7142963b84SBarry Smith #define YTRANS(draw,y)  (double)(((draw)->port_yl + (((y - (draw)->coor_yl)*((draw)->port_yr - (draw)->port_yl))/((draw)->coor_yr - (draw)->coor_yl))))
7242963b84SBarry Smith 
7342963b84SBarry Smith #undef __FUNCT__
7442963b84SBarry Smith #define __FUNCT__ "PetscDrawClear_TikZ"
7542963b84SBarry Smith PetscErrorCode PetscDrawClear_TikZ(PetscDraw draw)
7642963b84SBarry Smith {
7742963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
7842963b84SBarry Smith   PetscErrorCode ierr;
7942963b84SBarry Smith 
8042963b84SBarry Smith   PetscFunctionBegin;
8142963b84SBarry Smith   /* often PETSc generates unneeded clears, we want avoid creating empy pictures for them */
8242963b84SBarry Smith   if (!win->written) PetscFunctionReturn(0);
83ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_FRAME);CHKERRQ(ierr);
84ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr);
8542963b84SBarry Smith   PetscFunctionReturn(0);
8642963b84SBarry Smith }
8742963b84SBarry Smith 
8842963b84SBarry Smith #undef __FUNCT__
8942963b84SBarry Smith #define __FUNCT__ "PetscDrawLine_TikZ"
9042963b84SBarry Smith PetscErrorCode PetscDrawLine_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
9142963b84SBarry Smith {
9242963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
9342963b84SBarry Smith   PetscErrorCode ierr;
9442963b84SBarry Smith 
9542963b84SBarry Smith   PetscFunctionBegin;
9642963b84SBarry Smith   win->written = PETSC_TRUE;
97ce94432eSBarry Smith   ierr = 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));CHKERRQ(ierr);
9842963b84SBarry Smith   PetscFunctionReturn(0);
9942963b84SBarry Smith }
10042963b84SBarry Smith 
10142963b84SBarry Smith #undef __FUNCT__
10242963b84SBarry Smith #define __FUNCT__ "PetscDrawString_TikZ"
10342963b84SBarry Smith PetscErrorCode PetscDrawString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
10442963b84SBarry Smith {
10542963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
10642963b84SBarry Smith   PetscErrorCode ierr;
10742963b84SBarry Smith 
10842963b84SBarry Smith   PetscFunctionBegin;
10942963b84SBarry Smith   win->written = PETSC_TRUE;
110ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\node [above right, %s] at (%g,%g) {%s};\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),text);CHKERRQ(ierr);
11142963b84SBarry Smith   PetscFunctionReturn(0);
11242963b84SBarry Smith }
11342963b84SBarry Smith 
11442963b84SBarry Smith #undef __FUNCT__
115*d6ed00deSBarry Smith #define __FUNCT__ "PetscDrawStringVertical_TikZ"
116*d6ed00deSBarry Smith PetscErrorCode PetscDrawStringVertical_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
117*d6ed00deSBarry Smith {
118*d6ed00deSBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
119*d6ed00deSBarry Smith   PetscErrorCode ierr;
120*d6ed00deSBarry Smith   size_t         len;
121*d6ed00deSBarry Smith   PetscReal      width;
122*d6ed00deSBarry Smith 
123*d6ed00deSBarry Smith   PetscFunctionBegin;
124*d6ed00deSBarry Smith   win->written = PETSC_TRUE;
125*d6ed00deSBarry Smith   ierr = PetscStrlen(text,&len);CHKERRQ(ierr);
126*d6ed00deSBarry Smith   ierr = PetscDrawStringGetSize(draw,&width,NULL);CHKERRQ(ierr);
127*d6ed00deSBarry Smith   yl   = yl - len*width*(draw->coor_yr - draw->coor_yl)/(draw->coor_xr - draw->coor_xl);
128*d6ed00deSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\node [rotate=90, %s] at (%g,%g) {%s};\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),text);CHKERRQ(ierr);
129*d6ed00deSBarry Smith   PetscFunctionReturn(0);
130*d6ed00deSBarry Smith }
131*d6ed00deSBarry Smith 
132*d6ed00deSBarry Smith #undef __FUNCT__
13342963b84SBarry Smith #define __FUNCT__ "PetscDrawBoxedString_TikZ"
13442963b84SBarry Smith /*
13542963b84SBarry Smith     Does not handle multiline strings correctly
13642963b84SBarry Smith */
13742963b84SBarry Smith PetscErrorCode PetscDrawBoxedString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,int ct,const char text[],PetscReal *w,PetscReal *h)
13842963b84SBarry Smith {
13942963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
14042963b84SBarry Smith   PetscErrorCode ierr;
14142963b84SBarry Smith   size_t         len;
14242963b84SBarry Smith 
14342963b84SBarry Smith   PetscFunctionBegin;
14442963b84SBarry Smith   win->written = PETSC_TRUE;
145ce94432eSBarry Smith   ierr = 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);CHKERRQ(ierr);
14642963b84SBarry Smith 
14742963b84SBarry Smith   /* make up totally bogus height and width of box */
14842963b84SBarry Smith   ierr = PetscStrlen(text,&len);CHKERRQ(ierr);
14942963b84SBarry Smith   if (w) *w = .07*len;
15042963b84SBarry Smith   if (h) *h = .07;
15142963b84SBarry Smith   PetscFunctionReturn(0);
15242963b84SBarry Smith }
15342963b84SBarry Smith 
154*d6ed00deSBarry Smith #undef __FUNCT__
155*d6ed00deSBarry Smith #define __FUNCT__ "PetscDrawStringGetSize_TikZ"
156*d6ed00deSBarry Smith PetscErrorCode PetscDrawStringGetSize_TikZ(PetscDraw draw,PetscReal *x,PetscReal  *y)
157*d6ed00deSBarry Smith {
158*d6ed00deSBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
159*d6ed00deSBarry Smith 
160*d6ed00deSBarry Smith   PetscFunctionBegin;
161*d6ed00deSBarry Smith   if (x) *x = .014*(draw->coor_xr - draw->coor_xl)/((draw->port_xr - draw->port_xl));
162*d6ed00deSBarry Smith   if (y) *y = .05*(draw->coor_yr - draw->coor_yl)/((draw->port_yr - draw->port_yl));
163*d6ed00deSBarry Smith   PetscFunctionReturn(0);
164*d6ed00deSBarry Smith }
165*d6ed00deSBarry Smith 
16642963b84SBarry Smith static struct _PetscDrawOps DvOps = { 0,
16742963b84SBarry Smith                                       0,
16842963b84SBarry Smith                                       PetscDrawLine_TikZ,
16942963b84SBarry Smith                                       0,
17042963b84SBarry Smith                                       0,
17142963b84SBarry Smith                                       0,
17242963b84SBarry Smith                                       0,
17342963b84SBarry Smith                                       PetscDrawString_TikZ,
174*d6ed00deSBarry Smith                                       PetscDrawStringVertical_TikZ,
17542963b84SBarry Smith                                       0,
176*d6ed00deSBarry Smith                                       PetscDrawStringGetSize_TikZ,
17742963b84SBarry Smith                                       0,
17842963b84SBarry Smith                                       PetscDrawClear_TikZ,
17942963b84SBarry Smith                                       0,
18042963b84SBarry Smith                                       0,
18142963b84SBarry Smith                                       0,
18242963b84SBarry Smith                                       0,
18342963b84SBarry Smith                                       0,
18442963b84SBarry Smith                                       0,
18542963b84SBarry Smith                                       0,
18642963b84SBarry Smith                                       0,
18742963b84SBarry Smith                                       0,
18842963b84SBarry Smith                                       0,
18942963b84SBarry Smith                                       0,
19042963b84SBarry Smith                                       0,
19142963b84SBarry Smith                                       0,
19242963b84SBarry Smith                                       PetscDrawDestroy_TikZ,
19342963b84SBarry Smith                                       0,
19442963b84SBarry Smith                                       0,
19542963b84SBarry Smith                                       0,
19642963b84SBarry Smith                                       0,
19742963b84SBarry Smith                                       0,
19842963b84SBarry Smith                                       0,
19942963b84SBarry Smith                                       0,
20042963b84SBarry Smith                                       0,
20142963b84SBarry Smith                                       0,
20242963b84SBarry Smith                                       0,
20342963b84SBarry Smith                                       PetscDrawBoxedString_TikZ};
20442963b84SBarry Smith 
20542963b84SBarry Smith #undef __FUNCT__
20642963b84SBarry Smith #define __FUNCT__ "PetscDrawCreate_TikZ"
2078cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscDrawCreate_TikZ(PetscDraw draw)
20842963b84SBarry Smith {
20942963b84SBarry Smith   PetscDraw_TikZ *win;
21042963b84SBarry Smith   PetscErrorCode ierr;
21142963b84SBarry Smith 
21242963b84SBarry Smith   PetscFunctionBegin;
21342963b84SBarry Smith   ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
214b00a9115SJed Brown   ierr = PetscNew(&win);CHKERRQ(ierr);
2153bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)draw,sizeof(PetscDraw_TikZ));CHKERRQ(ierr);
216a297a907SKarl Rupp 
21742963b84SBarry Smith   draw->data = (void*) win;
21842963b84SBarry Smith 
21942963b84SBarry Smith   if (draw->title) {
22042963b84SBarry Smith     ierr = PetscStrallocpy(draw->title,&win->filename);CHKERRQ(ierr);
22142963b84SBarry Smith   } else {
22242963b84SBarry Smith     const char *fname;
22342963b84SBarry Smith     ierr = PetscObjectGetName((PetscObject)draw,&fname);CHKERRQ(ierr);
22442963b84SBarry Smith     ierr = PetscStrallocpy(fname,&win->filename);CHKERRQ(ierr);
22542963b84SBarry Smith   }
226ce94432eSBarry Smith   ierr = PetscFOpen(PetscObjectComm((PetscObject)draw),win->filename,"w",&win->fd);CHKERRQ(ierr);
227ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_DOCUMENT);CHKERRQ(ierr);
228ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr);
229a297a907SKarl Rupp 
23042963b84SBarry Smith   win->written = PETSC_FALSE;
23142963b84SBarry Smith   PetscFunctionReturn(0);
23242963b84SBarry Smith }
23342963b84SBarry Smith 
23442963b84SBarry Smith 
23542963b84SBarry Smith 
23642963b84SBarry Smith 
23742963b84SBarry Smith 
23842963b84SBarry Smith 
23942963b84SBarry Smith 
24042963b84SBarry Smith 
24142963b84SBarry Smith 
242