Actual source code: tikz.c
petsc-3.5.4 2015-05-23
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: PetscErrorCode PetscDrawStringVertical_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
117: {
118: PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
120: size_t len;
121: PetscReal width;
124: win->written = PETSC_TRUE;
125: PetscStrlen(text,&len);
126: PetscDrawStringGetSize(draw,&width,NULL);
127: yl = yl - len*width*(draw->coor_yr - draw->coor_yl)/(draw->coor_xr - draw->coor_xl);
128: PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\node [rotate=90, %s] at (%g,%g) {%s};\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),text);
129: return(0);
130: }
134: /*
135: Does not handle multiline strings correctly
136: */
137: PetscErrorCode PetscDrawBoxedString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,int ct,const char text[],PetscReal *w,PetscReal *h)
138: {
139: PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data;
141: size_t len;
144: win->written = PETSC_TRUE;
145: 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);
147: /* make up totally bogus height and width of box */
148: PetscStrlen(text,&len);
149: if (w) *w = .07*len;
150: if (h) *h = .07;
151: return(0);
152: }
156: PetscErrorCode PetscDrawStringGetSize_TikZ(PetscDraw draw,PetscReal *x,PetscReal *y)
157: {
159: if (x) *x = .014*(draw->coor_xr - draw->coor_xl)/((draw->port_xr - draw->port_xl));
160: if (y) *y = .05*(draw->coor_yr - draw->coor_yl)/((draw->port_yr - draw->port_yl));
161: return(0);
162: }
164: static struct _PetscDrawOps DvOps = { 0,
165: 0,
166: PetscDrawLine_TikZ,
167: 0,
168: 0,
169: 0,
170: 0,
171: PetscDrawString_TikZ,
172: PetscDrawStringVertical_TikZ,
173: 0,
174: PetscDrawStringGetSize_TikZ,
175: 0,
176: PetscDrawClear_TikZ,
177: 0,
178: 0,
179: 0,
180: 0,
181: 0,
182: 0,
183: 0,
184: 0,
185: 0,
186: 0,
187: 0,
188: 0,
189: 0,
190: PetscDrawDestroy_TikZ,
191: 0,
192: 0,
193: 0,
194: 0,
195: 0,
196: 0,
197: 0,
198: 0,
199: 0,
200: 0,
201: PetscDrawBoxedString_TikZ};
205: PETSC_EXTERN PetscErrorCode PetscDrawCreate_TikZ(PetscDraw draw)
206: {
207: PetscDraw_TikZ *win;
211: PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));
212: PetscNew(&win);
213: PetscLogObjectMemory((PetscObject)draw,sizeof(PetscDraw_TikZ));
215: draw->data = (void*) win;
217: if (draw->title) {
218: PetscStrallocpy(draw->title,&win->filename);
219: } else {
220: const char *fname;
221: PetscObjectGetName((PetscObject)draw,&fname);
222: PetscStrallocpy(fname,&win->filename);
223: }
224: PetscFOpen(PetscObjectComm((PetscObject)draw),win->filename,"w",&win->fd);
225: PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_DOCUMENT);
226: PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);
228: win->written = PETSC_FALSE;
229: return(0);
230: }