xref: /petsc/src/sys/classes/draw/interface/dtext.c (revision e611a964e9853b74d61a56642fe9d06a6e51780f)
1 /*
2        Provides the calling sequences for all the basic PetscDraw routines.
3 */
4 #include <petsc/private/drawimpl.h>  /*I "petscdraw.h" I*/
5 
6 #undef __FUNCT__
7 #define __FUNCT__ "PetscDrawString"
8 /*@C
9    PetscDrawString - PetscDraws text onto a drawable.
10 
11    Not Collective
12 
13    Input Parameters:
14 +  draw - the drawing context
15 .  xl,yl - the coordinates of lower left corner of text
16 .  cl - the color of the text
17 -  text - the text to draw
18 
19    Level: beginner
20 
21    Concepts: drawing^string
22    Concepts: string^drawing
23 
24 .seealso: PetscDrawStringVertical(), PetscDrawStringCentered(), PetscDrawStringBoxed()
25 
26 @*/
27 PetscErrorCode  PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
28 {
29   PetscErrorCode ierr;
30 
31   PetscFunctionBegin;
32   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
33   PetscValidCharPointer(text,5);
34   if (!draw->ops->string) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support drawing strings",((PetscObject)draw)->type_name);
35   ierr = (*draw->ops->string)(draw,xl,yl,cl,text);CHKERRQ(ierr);
36   PetscFunctionReturn(0);
37 }
38 
39 #undef __FUNCT__
40 #define __FUNCT__ "PetscDrawStringVertical"
41 /*@C
42    PetscDrawStringVertical - PetscDraws text onto a drawable.
43 
44    Not Collective
45 
46    Input Parameters:
47 +  draw - the drawing context
48 .  xl,yl - the coordinates of upper left corner of text
49 .  cl - the color of the text
50 -  text - the text to draw
51 
52    Level: beginner
53 
54    Concepts: string^drawing vertical
55 
56 .seealso: PetscDrawString()
57 
58 @*/
59 PetscErrorCode  PetscDrawStringVertical(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
60 {
61   int            i;
62   char           chr[2] = {0, 0};
63   PetscReal      tw,th;
64   PetscErrorCode ierr;
65 
66   PetscFunctionBegin;
67   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
68   PetscValidCharPointer(text,5);
69 
70   if (draw->ops->stringvertical) {
71     ierr = (*draw->ops->stringvertical)(draw,xl,yl,cl,text);CHKERRQ(ierr);
72     PetscFunctionReturn(0);
73   }
74   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
75   for (i = 0; (chr[0] = text[i]); i++) {
76     ierr = PetscDrawString(draw,xl,yl-th*(i+1),cl,chr);CHKERRQ(ierr);
77   }
78   PetscFunctionReturn(0);
79 }
80 
81 #undef __FUNCT__
82 #define __FUNCT__ "PetscDrawStringCentered"
83 /*@C
84    PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point
85 
86    Not Collective
87 
88    Input Parameters:
89 +  draw - the drawing context
90 .  xc - the coordinates of right-left center of text
91 .  yl - the coordinates of lower edge of text
92 .  cl - the color of the text
93 -  text - the text to draw
94 
95    Level: beginner
96 
97    Concepts: drawing^string
98    Concepts: string^drawing
99 
100 .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed()
101 
102 @*/
103 PetscErrorCode  PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[])
104 {
105   PetscErrorCode ierr;
106   size_t         len;
107   PetscReal      tw,th;
108 
109   PetscFunctionBegin;
110   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
111   PetscValidCharPointer(text,5);
112 
113   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
114   ierr = PetscStrlen(text,&len);CHKERRQ(ierr);
115   xc   = xc - len*tw/2;
116   ierr = PetscDrawString(draw,xc,yl,cl,text);CHKERRQ(ierr);
117   PetscFunctionReturn(0);
118 }
119 
120 #undef __FUNCT__
121 #define __FUNCT__ "PetscDrawStringBoxed"
122 /*@C
123    PetscDrawStringBoxed - Draws a string with a box around it
124 
125    Not Collective
126 
127    Input Parameters:
128 +  draw - the drawing context
129 .  sxl - the coordinates of center of the box
130 .  syl - the coordinates of top line of box
131 .  sc - the color of the text
132 .  bc - the color of the bounding box
133 -  text - the text to draw
134 
135    Output Parameter:
136 .   w,h - width and height of resulting box (optional)
137 
138    Level: beginner
139 
140    Concepts: drawing^string
141    Concepts: string^drawing
142 
143 .seealso: PetscDrawStringVertical(), PetscDrawStringBoxedSize(), PetscDrawString(), PetscDrawStringCentered()
144 
145 @*/
146 PetscErrorCode  PetscDrawStringBoxed(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
147 {
148   PetscErrorCode ierr;
149   PetscReal      top,left,right,bottom,tw,th;
150   size_t         len,mlen = 0;
151   char           **array;
152   int            cnt,i;
153 
154   PetscFunctionBegin;
155   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
156   PetscValidCharPointer(text,5);
157 
158   if (draw->ops->boxedstring) {
159     ierr = (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);CHKERRQ(ierr);
160     PetscFunctionReturn(0);
161   }
162 
163   ierr = PetscStrToArray(text,'\n',&cnt,&array);CHKERRQ(ierr);
164   for (i=0; i<cnt; i++) {
165     ierr = PetscStrlen(array[i],&len);CHKERRQ(ierr);
166     mlen = PetscMax(mlen,len);
167   }
168 
169   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
170 
171   top    = syl;
172   left   = sxl - .5*(mlen + 2)*tw;
173   right  = sxl + .5*(mlen + 2)*tw;
174   bottom = syl - (1.0 + cnt)*th;
175   if (w) *w = right - left;
176   if (h) *h = top - bottom;
177 
178   /* compute new bounding box */
179   draw->boundbox_xl = PetscMin(draw->boundbox_xl,left);
180   draw->boundbox_xr = PetscMax(draw->boundbox_xr,right);
181   draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom);
182   draw->boundbox_yr = PetscMax(draw->boundbox_yr,top);
183 
184   /* top, left, bottom, right lines */
185   ierr = PetscDrawLine(draw,left,top,right,top,bc);CHKERRQ(ierr);
186   ierr = PetscDrawLine(draw,left,bottom,left,top,bc);CHKERRQ(ierr);
187   ierr = PetscDrawLine(draw,right,bottom,right,top,bc);CHKERRQ(ierr);
188   ierr = PetscDrawLine(draw,left,bottom,right,bottom,bc);CHKERRQ(ierr);
189 
190   for  (i=0; i<cnt; i++) {
191     ierr = PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);CHKERRQ(ierr);
192   }
193   ierr = PetscStrToArrayDestroy(cnt,array);CHKERRQ(ierr);
194   PetscFunctionReturn(0);
195 }
196 
197 #undef __FUNCT__
198 #define __FUNCT__ "PetscDrawStringSetSize"
199 /*@
200    PetscDrawStringSetSize - Sets the size for character text.
201 
202    Not Collective
203 
204    Input Parameters:
205 +  draw - the drawing context
206 .  width - the width in user coordinates
207 -  height - the character height in user coordinates
208 
209    Level: advanced
210 
211    Note:
212    Only a limited range of sizes are available.
213 
214    Concepts: string^drawing size
215 
216 .seealso: PetscDrawString(), PetscDrawStringVertical(), PetscDrawStringGetSize()
217 
218 @*/
219 PetscErrorCode  PetscDrawStringSetSize(PetscDraw draw,PetscReal width,PetscReal height)
220 {
221   PetscErrorCode ierr;
222 
223   PetscFunctionBegin;
224   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
225   if (draw->ops->stringsetsize) {
226     ierr = (*draw->ops->stringsetsize)(draw,width,height);CHKERRQ(ierr);
227   }
228   PetscFunctionReturn(0);
229 }
230 
231 #undef __FUNCT__
232 #define __FUNCT__ "PetscDrawStringGetSize"
233 /*@
234    PetscDrawStringGetSize - Gets the size for character text.  The width is
235    relative to the user coordinates of the window.
236 
237    Not Collective
238 
239    Input Parameters:
240 +  draw - the drawing context
241 .  width - the width in user coordinates
242 -  height - the character height
243 
244    Level: advanced
245 
246    Concepts: string^drawing size
247 
248 .seealso: PetscDrawString(), PetscDrawStringVertical(), PetscDrawStringSetSize()
249 
250 @*/
251 PetscErrorCode  PetscDrawStringGetSize(PetscDraw draw,PetscReal *width,PetscReal *height)
252 {
253   PetscErrorCode ierr;
254 
255   PetscFunctionBegin;
256   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
257   if (!draw->ops->stringgetsize) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support getting string size",((PetscObject)draw)->type_name);
258   ierr = (*draw->ops->stringgetsize)(draw,width,height);CHKERRQ(ierr);
259   PetscFunctionReturn(0);
260 }
261 
262