xref: /petsc/src/sys/classes/draw/impls/tikz/tikz.c (revision 42963b84bc3d3eb508a08ddbd058b7419ac22fe8)
1*42963b84SBarry Smith 
2*42963b84SBarry Smith /*
3*42963b84SBarry Smith     Defines the operations for the X PetscDraw implementation.
4*42963b84SBarry Smith */
5*42963b84SBarry Smith 
6*42963b84SBarry Smith #include <petsc-private/drawimpl.h>         /*I  "petscsys.h" I*/
7*42963b84SBarry Smith 
8*42963b84SBarry Smith typedef struct {
9*42963b84SBarry Smith   char      *filename;
10*42963b84SBarry Smith   FILE      *fd;
11*42963b84SBarry Smith   PetscBool written;  /* something has been written to the current frame */
12*42963b84SBarry Smith } PetscDraw_TikZ;
13*42963b84SBarry Smith 
14*42963b84SBarry Smith #define TikZ_BEGIN_DOCUMENT  "\\documentclass{beamer}\n\n\
15*42963b84SBarry Smith \\usepackage{tikz}\n\
16*42963b84SBarry Smith \\usepackage{pgflibraryshapes}\n\
17*42963b84SBarry Smith \\usetikzlibrary{backgrounds}\n\
18*42963b84SBarry Smith \\usetikzlibrary{arrows}\n\
19*42963b84SBarry Smith \\newenvironment{changemargin}[2]{%%\n\
20*42963b84SBarry Smith   \\begin{list}{}{%%\n\
21*42963b84SBarry Smith     \\setlength{\\topsep}{0pt}%%\n\
22*42963b84SBarry Smith     \\setlength{\\leftmargin}{#1}%%\n\
23*42963b84SBarry Smith     \\setlength{\\rightmargin}{#2}%%\n\
24*42963b84SBarry Smith     \\setlength{\\listparindent}{\\parindent}%%\n\
25*42963b84SBarry Smith     \\setlength{\\itemindent}{\\parindent}%%\n\
26*42963b84SBarry Smith     \\setlength{\\parsep}{\\parskip}%%\n\
27*42963b84SBarry Smith   }%%\n\
28*42963b84SBarry Smith   \\item[]}{\\end{list}}\n\n\
29*42963b84SBarry Smith \\begin{document}\n"
30*42963b84SBarry Smith 
31*42963b84SBarry Smith #define TikZ_BEGIN_FRAME "\\begin{frame}{}\n\
32*42963b84SBarry Smith \\begin{changemargin}{-1cm}{0cm}\n\
33*42963b84SBarry Smith \\begin{center}\n\
34*42963b84SBarry Smith \\begin{tikzpicture}[scale = 10.00,font=\\fontsize{8}{8}\\selectfont]\n"
35*42963b84SBarry Smith 
36*42963b84SBarry Smith #define TikZ_END_FRAME "\\end{tikzpicture}\n\
37*42963b84SBarry Smith \\end{center}\n\
38*42963b84SBarry Smith \\end{changemargin}\n\
39*42963b84SBarry Smith \\end{frame}\n"
40*42963b84SBarry Smith 
41*42963b84SBarry Smith #define TikZ_END_DOCUMENT  "\\end{document}\n"
42*42963b84SBarry Smith 
43*42963b84SBarry Smith #undef __FUNCT__
44*42963b84SBarry Smith #define __FUNCT__ "PetscDrawDestroy_TikZ"
45*42963b84SBarry Smith PetscErrorCode  PetscDrawDestroy_TikZ(PetscDraw draw)
46*42963b84SBarry Smith {
47*42963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
48*42963b84SBarry Smith   PetscErrorCode ierr;
49*42963b84SBarry Smith 
50*42963b84SBarry Smith   PetscFunctionBegin;
51*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,win->fd,TikZ_END_FRAME);CHKERRQ(ierr);
52*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,win->fd,TikZ_END_DOCUMENT);CHKERRQ(ierr);
53*42963b84SBarry Smith   ierr = PetscFClose(((PetscObject)draw)->comm,win->fd);CHKERRQ(ierr);
54*42963b84SBarry Smith   ierr = PetscFree(win->filename);CHKERRQ(ierr);
55*42963b84SBarry Smith   ierr = PetscFree(win);CHKERRQ(ierr);
56*42963b84SBarry Smith   PetscFunctionReturn(0);
57*42963b84SBarry Smith }
58*42963b84SBarry Smith 
59*42963b84SBarry Smith static const char *TikZColors[] = { "white",  "black", "red",  "green", "cyan",   "blue", "magenta", 0, 0, "orange",
60*42963b84SBarry Smith                                     "violet", "brown", "pink", 0,       "yellow", 0};
61*42963b84SBarry Smith 
62*42963b84SBarry Smith PETSC_STATIC_INLINE const char * TikZColorMap(int cl)
63*42963b84SBarry Smith {
64*42963b84SBarry Smith   return((cl < 16) ? ( TikZColors[cl] ? TikZColors[cl] : "black") : "black");
65*42963b84SBarry Smith }
66*42963b84SBarry Smith 
67*42963b84SBarry Smith /*
68*42963b84SBarry Smith      These macros transform from the users coordinates to the (0,0) -> (1,1) coordinate system
69*42963b84SBarry Smith */
70*42963b84SBarry 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))))
71*42963b84SBarry 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))))
72*42963b84SBarry Smith 
73*42963b84SBarry Smith #undef __FUNCT__
74*42963b84SBarry Smith #define __FUNCT__ "PetscDrawClear_TikZ"
75*42963b84SBarry Smith PetscErrorCode PetscDrawClear_TikZ(PetscDraw draw)
76*42963b84SBarry Smith {
77*42963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
78*42963b84SBarry Smith   PetscErrorCode ierr;
79*42963b84SBarry Smith 
80*42963b84SBarry Smith   PetscFunctionBegin;
81*42963b84SBarry Smith   /* often PETSc generates unneeded clears, we want avoid creating empy pictures for them */
82*42963b84SBarry Smith   if (!win->written) PetscFunctionReturn(0);
83*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,win->fd,TikZ_END_FRAME);CHKERRQ(ierr);
84*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr);
85*42963b84SBarry Smith   PetscFunctionReturn(0);
86*42963b84SBarry Smith }
87*42963b84SBarry Smith 
88*42963b84SBarry Smith #undef __FUNCT__
89*42963b84SBarry Smith #define __FUNCT__ "PetscDrawLine_TikZ"
90*42963b84SBarry Smith PetscErrorCode PetscDrawLine_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
91*42963b84SBarry Smith {
92*42963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
93*42963b84SBarry Smith   PetscErrorCode ierr;
94*42963b84SBarry Smith 
95*42963b84SBarry Smith   PetscFunctionBegin;
96*42963b84SBarry Smith   win->written = PETSC_TRUE;
97*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,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);
98*42963b84SBarry Smith   PetscFunctionReturn(0);
99*42963b84SBarry Smith }
100*42963b84SBarry Smith 
101*42963b84SBarry Smith #undef __FUNCT__
102*42963b84SBarry Smith #define __FUNCT__ "PetscDrawString_TikZ"
103*42963b84SBarry Smith PetscErrorCode PetscDrawString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
104*42963b84SBarry Smith {
105*42963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
106*42963b84SBarry Smith   PetscErrorCode ierr;
107*42963b84SBarry Smith 
108*42963b84SBarry Smith   PetscFunctionBegin;
109*42963b84SBarry Smith   win->written = PETSC_TRUE;
110*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,win->fd,"\\node [above right, %s] at (%g,%g) {%s};\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),text);CHKERRQ(ierr);
111*42963b84SBarry Smith   PetscFunctionReturn(0);
112*42963b84SBarry Smith }
113*42963b84SBarry Smith 
114*42963b84SBarry Smith #undef __FUNCT__
115*42963b84SBarry Smith #define __FUNCT__ "PetscDrawBoxedString_TikZ"
116*42963b84SBarry Smith /*
117*42963b84SBarry Smith     Does not handle multiline strings correctly
118*42963b84SBarry Smith */
119*42963b84SBarry Smith PetscErrorCode PetscDrawBoxedString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,int ct,const char text[],PetscReal *w,PetscReal *h)
120*42963b84SBarry Smith {
121*42963b84SBarry Smith   PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
122*42963b84SBarry Smith   PetscErrorCode ierr;
123*42963b84SBarry Smith   size_t         len;
124*42963b84SBarry Smith 
125*42963b84SBarry Smith   PetscFunctionBegin;
126*42963b84SBarry Smith   win->written = PETSC_TRUE;
127*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,win->fd,"\\draw (%g,%g) node [rectangle, draw, align=center, inner sep=1ex] {%s};\n",XTRANS(draw,xl),YTRANS(draw,yl),text);CHKERRQ(ierr);
128*42963b84SBarry Smith 
129*42963b84SBarry Smith   /* make up totally bogus height and width of box */
130*42963b84SBarry Smith   ierr = PetscStrlen(text,&len);CHKERRQ(ierr);
131*42963b84SBarry Smith   if (w) *w = .07*len;
132*42963b84SBarry Smith   if (h) *h = .07;
133*42963b84SBarry Smith   PetscFunctionReturn(0);
134*42963b84SBarry Smith }
135*42963b84SBarry Smith 
136*42963b84SBarry Smith static struct _PetscDrawOps DvOps = { 0,
137*42963b84SBarry Smith                                       0,
138*42963b84SBarry Smith                                       PetscDrawLine_TikZ,
139*42963b84SBarry Smith                                       0,
140*42963b84SBarry Smith                                       0,
141*42963b84SBarry Smith                                       0,
142*42963b84SBarry Smith                                       0,
143*42963b84SBarry Smith                                       PetscDrawString_TikZ,
144*42963b84SBarry Smith                                       0,
145*42963b84SBarry Smith                                       0,
146*42963b84SBarry Smith                                       0,
147*42963b84SBarry Smith                                       0,
148*42963b84SBarry Smith                                       PetscDrawClear_TikZ,
149*42963b84SBarry Smith                                       0,
150*42963b84SBarry Smith                                       0,
151*42963b84SBarry Smith                                       0,
152*42963b84SBarry Smith                                       0,
153*42963b84SBarry Smith                                       0,
154*42963b84SBarry Smith                                       0,
155*42963b84SBarry Smith                                       0,
156*42963b84SBarry Smith                                       0,
157*42963b84SBarry Smith                                       0,
158*42963b84SBarry Smith                                       0,
159*42963b84SBarry Smith                                       0,
160*42963b84SBarry Smith                                       0,
161*42963b84SBarry Smith                                       0,
162*42963b84SBarry Smith                                       PetscDrawDestroy_TikZ,
163*42963b84SBarry Smith                                       0,
164*42963b84SBarry Smith                                       0,
165*42963b84SBarry Smith                                       0,
166*42963b84SBarry Smith                                       0,
167*42963b84SBarry Smith                                       0,
168*42963b84SBarry Smith                                       0,
169*42963b84SBarry Smith                                       0,
170*42963b84SBarry Smith                                       0,
171*42963b84SBarry Smith                                       0,
172*42963b84SBarry Smith                                       0,
173*42963b84SBarry Smith                                       PetscDrawBoxedString_TikZ};
174*42963b84SBarry Smith 
175*42963b84SBarry Smith 
176*42963b84SBarry Smith 
177*42963b84SBarry Smith 
178*42963b84SBarry Smith EXTERN_C_BEGIN
179*42963b84SBarry Smith #undef __FUNCT__
180*42963b84SBarry Smith #define __FUNCT__ "PetscDrawCreate_TikZ"
181*42963b84SBarry Smith PetscErrorCode  PetscDrawCreate_TikZ(PetscDraw draw)
182*42963b84SBarry Smith {
183*42963b84SBarry Smith   PetscDraw_TikZ *win;
184*42963b84SBarry Smith   PetscErrorCode ierr;
185*42963b84SBarry Smith 
186*42963b84SBarry Smith   PetscFunctionBegin;
187*42963b84SBarry Smith   ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
188*42963b84SBarry Smith   ierr = PetscNew(PetscDraw_TikZ,&win);CHKERRQ(ierr);
189*42963b84SBarry Smith   ierr = PetscLogObjectMemory(draw,sizeof(PetscDraw_TikZ));CHKERRQ(ierr);
190*42963b84SBarry Smith   draw->data = (void*) win;
191*42963b84SBarry Smith 
192*42963b84SBarry Smith   if (draw->title) {
193*42963b84SBarry Smith     ierr = PetscStrallocpy(draw->title,&win->filename);CHKERRQ(ierr);
194*42963b84SBarry Smith   } else {
195*42963b84SBarry Smith     const char *fname;
196*42963b84SBarry Smith     ierr = PetscObjectGetName((PetscObject)draw,&fname);CHKERRQ(ierr);
197*42963b84SBarry Smith     ierr = PetscStrallocpy(fname,&win->filename);CHKERRQ(ierr);
198*42963b84SBarry Smith   }
199*42963b84SBarry Smith   ierr = PetscFOpen(((PetscObject)draw)->comm,win->filename,"w",&win->fd);CHKERRQ(ierr);
200*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,win->fd,TikZ_BEGIN_DOCUMENT);CHKERRQ(ierr);
201*42963b84SBarry Smith   ierr = PetscFPrintf(((PetscObject)draw)->comm,win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr);
202*42963b84SBarry Smith   win->written = PETSC_FALSE;
203*42963b84SBarry Smith   PetscFunctionReturn(0);
204*42963b84SBarry Smith }
205*42963b84SBarry Smith EXTERN_C_END
206*42963b84SBarry Smith 
207*42963b84SBarry Smith 
208*42963b84SBarry Smith 
209*42963b84SBarry Smith 
210*42963b84SBarry Smith 
211*42963b84SBarry Smith 
212*42963b84SBarry Smith 
213*42963b84SBarry Smith 
214*42963b84SBarry Smith 
215