xref: /petsc/src/sys/classes/draw/impls/tikz/tikz.c (revision 8cc058d9cd56c1ccb3be12a47760ddfc446aaffc)
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__
11542963b84SBarry Smith #define __FUNCT__ "PetscDrawBoxedString_TikZ"
11642963b84SBarry Smith /*
11742963b84SBarry Smith     Does not handle multiline strings correctly
11842963b84SBarry Smith */
11942963b84SBarry Smith PetscErrorCode PetscDrawBoxedString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,int ct,const char text[],PetscReal *w,PetscReal *h)
12042963b84SBarry Smith {
12142963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
12242963b84SBarry Smith   PetscErrorCode ierr;
12342963b84SBarry Smith   size_t         len;
12442963b84SBarry Smith 
12542963b84SBarry Smith   PetscFunctionBegin;
12642963b84SBarry Smith   win->written = PETSC_TRUE;
127ce94432eSBarry 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);
12842963b84SBarry Smith 
12942963b84SBarry Smith   /* make up totally bogus height and width of box */
13042963b84SBarry Smith   ierr = PetscStrlen(text,&len);CHKERRQ(ierr);
13142963b84SBarry Smith   if (w) *w = .07*len;
13242963b84SBarry Smith   if (h) *h = .07;
13342963b84SBarry Smith   PetscFunctionReturn(0);
13442963b84SBarry Smith }
13542963b84SBarry Smith 
13642963b84SBarry Smith static struct _PetscDrawOps DvOps = { 0,
13742963b84SBarry Smith                                       0,
13842963b84SBarry Smith                                       PetscDrawLine_TikZ,
13942963b84SBarry Smith                                       0,
14042963b84SBarry Smith                                       0,
14142963b84SBarry Smith                                       0,
14242963b84SBarry Smith                                       0,
14342963b84SBarry Smith                                       PetscDrawString_TikZ,
14442963b84SBarry Smith                                       0,
14542963b84SBarry Smith                                       0,
14642963b84SBarry Smith                                       0,
14742963b84SBarry Smith                                       0,
14842963b84SBarry Smith                                       PetscDrawClear_TikZ,
14942963b84SBarry Smith                                       0,
15042963b84SBarry Smith                                       0,
15142963b84SBarry Smith                                       0,
15242963b84SBarry Smith                                       0,
15342963b84SBarry Smith                                       0,
15442963b84SBarry Smith                                       0,
15542963b84SBarry Smith                                       0,
15642963b84SBarry Smith                                       0,
15742963b84SBarry Smith                                       0,
15842963b84SBarry Smith                                       0,
15942963b84SBarry Smith                                       0,
16042963b84SBarry Smith                                       0,
16142963b84SBarry Smith                                       0,
16242963b84SBarry Smith                                       PetscDrawDestroy_TikZ,
16342963b84SBarry Smith                                       0,
16442963b84SBarry Smith                                       0,
16542963b84SBarry Smith                                       0,
16642963b84SBarry Smith                                       0,
16742963b84SBarry Smith                                       0,
16842963b84SBarry Smith                                       0,
16942963b84SBarry Smith                                       0,
17042963b84SBarry Smith                                       0,
17142963b84SBarry Smith                                       0,
17242963b84SBarry Smith                                       0,
17342963b84SBarry Smith                                       PetscDrawBoxedString_TikZ};
17442963b84SBarry Smith 
17542963b84SBarry Smith #undef __FUNCT__
17642963b84SBarry Smith #define __FUNCT__ "PetscDrawCreate_TikZ"
177*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscDrawCreate_TikZ(PetscDraw draw)
17842963b84SBarry Smith {
17942963b84SBarry Smith   PetscDraw_TikZ *win;
18042963b84SBarry Smith   PetscErrorCode ierr;
18142963b84SBarry Smith 
18242963b84SBarry Smith   PetscFunctionBegin;
18342963b84SBarry Smith   ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
18442963b84SBarry Smith   ierr = PetscNew(PetscDraw_TikZ,&win);CHKERRQ(ierr);
18542963b84SBarry Smith   ierr = PetscLogObjectMemory(draw,sizeof(PetscDraw_TikZ));CHKERRQ(ierr);
186a297a907SKarl Rupp 
18742963b84SBarry Smith   draw->data = (void*) win;
18842963b84SBarry Smith 
18942963b84SBarry Smith   if (draw->title) {
19042963b84SBarry Smith     ierr = PetscStrallocpy(draw->title,&win->filename);CHKERRQ(ierr);
19142963b84SBarry Smith   } else {
19242963b84SBarry Smith     const char *fname;
19342963b84SBarry Smith     ierr = PetscObjectGetName((PetscObject)draw,&fname);CHKERRQ(ierr);
19442963b84SBarry Smith     ierr = PetscStrallocpy(fname,&win->filename);CHKERRQ(ierr);
19542963b84SBarry Smith   }
196ce94432eSBarry Smith   ierr = PetscFOpen(PetscObjectComm((PetscObject)draw),win->filename,"w",&win->fd);CHKERRQ(ierr);
197ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_DOCUMENT);CHKERRQ(ierr);
198ce94432eSBarry Smith   ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr);
199a297a907SKarl Rupp 
20042963b84SBarry Smith   win->written = PETSC_FALSE;
20142963b84SBarry Smith   PetscFunctionReturn(0);
20242963b84SBarry Smith }
20342963b84SBarry Smith 
20442963b84SBarry Smith 
20542963b84SBarry Smith 
20642963b84SBarry Smith 
20742963b84SBarry Smith 
20842963b84SBarry Smith 
20942963b84SBarry Smith 
21042963b84SBarry Smith 
21142963b84SBarry Smith 
212