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