xref: /petsc/src/sys/classes/draw/interface/dtext.c (revision 8f69470a7058bcc0cf21d9492705f60bbdfc5aa4)
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 - the coordinates of lower left corner of text
16 .  yl - the coordinates of lower left corner of text
17 .  cl - the color of the text
18 -  text - the text to draw
19 
20    Level: beginner
21 
22    Concepts: drawing^string
23    Concepts: string^drawing
24 
25 .seealso: PetscDrawStringVertical(), PetscDrawStringCentered(), PetscDrawStringBoxed()
26 
27 @*/
28 PetscErrorCode  PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
29 {
30   PetscErrorCode ierr;
31   PetscBool      isnull;
32 
33   PetscFunctionBegin;
34   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
35   PetscValidCharPointer(text,5);
36   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
37   if (isnull) PetscFunctionReturn(0);
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   PetscErrorCode ierr;
66   PetscBool      isnull;
67 
68   PetscFunctionBegin;
69   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
70   PetscValidCharPointer(text,5);
71   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
72   if (isnull) PetscFunctionReturn(0);
73   if (!draw->ops->stringvertical) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support drawing vertical strings",((PetscObject)draw)->type_name);
74   ierr = (*draw->ops->stringvertical)(draw,xl,yl,cl,text);CHKERRQ(ierr);
75   PetscFunctionReturn(0);
76 }
77 
78 #undef __FUNCT__
79 #define __FUNCT__ "PetscDrawStringCentered"
80 /*@C
81    PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point
82 
83    Not Collective
84 
85    Input Parameters:
86 +  draw - the drawing context
87 .  xc - the coordinates of right-left center of text
88 .  yl - the coordinates of lower edge of text
89 .  cl - the color of the text
90 -  text - the text to draw
91 
92    Level: beginner
93 
94    Concepts: drawing^string
95    Concepts: string^drawing
96 
97 .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed()
98 
99 @*/
100 PetscErrorCode  PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[])
101 {
102   PetscErrorCode ierr;
103   PetscBool      isnull;
104   size_t         len;
105   PetscReal      tw,th;
106 
107   PetscFunctionBegin;
108   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
109   PetscValidCharPointer(text,5);
110   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
111   if (isnull) PetscFunctionReturn(0);
112 
113   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
114   ierr =  PetscStrlen(text,&len);CHKERRQ(ierr);
115   xc   = xc - .5*len*tw;
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   PetscBool      isnull;
150   PetscReal      top,left,right,bottom,tw,th;
151   size_t         len,mlen = 0;
152   char           **array;
153   int            cnt,i;
154 
155   PetscFunctionBegin;
156   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
157   PetscValidCharPointer(text,5);
158   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
159   if (isnull) PetscFunctionReturn(0);
160 
161   if (draw->ops->boxedstring) {
162     ierr = (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);CHKERRQ(ierr);
163     PetscFunctionReturn(0);
164   }
165 
166   ierr = PetscStrToArray(text,'\n',&cnt,&array);CHKERRQ(ierr);
167   for (i=0; i<cnt; i++) {
168     ierr = PetscStrlen(array[i],&len);CHKERRQ(ierr);
169     mlen = PetscMax(mlen,len);
170   }
171 
172   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
173 
174   top    = syl;
175   left   = sxl - .5*(mlen + 2)*tw;
176   right  = sxl + .5*(mlen + 2)*tw;
177   bottom = syl - (1.0 + cnt)*th;
178   if (w) *w = right - left;
179   if (h) *h = top - bottom;
180 
181   /* compute new bounding box */
182   draw->boundbox_xl = PetscMin(draw->boundbox_xl,left);
183   draw->boundbox_xr = PetscMax(draw->boundbox_xr,right);
184   draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom);
185   draw->boundbox_yr = PetscMax(draw->boundbox_yr,top);
186 
187   /* top, left, bottom, right lines */
188   ierr = PetscDrawLine(draw,left,top,right,top,bc);CHKERRQ(ierr);
189   ierr = PetscDrawLine(draw,left,bottom,left,top,bc);CHKERRQ(ierr);
190   ierr = PetscDrawLine(draw,right,bottom,right,top,bc);CHKERRQ(ierr);
191   ierr = PetscDrawLine(draw,left,bottom,right,bottom,bc);CHKERRQ(ierr);
192 
193   for  (i=0; i<cnt; i++) {
194     ierr = PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);CHKERRQ(ierr);
195   }
196   ierr = PetscStrToArrayDestroy(cnt,array);CHKERRQ(ierr);
197   PetscFunctionReturn(0);
198 }
199 
200 #undef __FUNCT__
201 #define __FUNCT__ "PetscDrawStringSetSize"
202 /*@
203    PetscDrawStringSetSize - Sets the size for character text.
204 
205    Not Collective
206 
207    Input Parameters:
208 +  draw - the drawing context
209 .  width - the width in user coordinates
210 -  height - the character height in user coordinates
211 
212    Level: advanced
213 
214    Note:
215    Only a limited range of sizes are available.
216 
217    Concepts: string^drawing size
218 
219 .seealso: PetscDrawString(), PetscDrawStringVertical(), PetscDrawStringGetSize()
220 
221 @*/
222 PetscErrorCode  PetscDrawStringSetSize(PetscDraw draw,PetscReal width,PetscReal height)
223 {
224   PetscErrorCode ierr;
225 
226   PetscFunctionBegin;
227   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
228   if (draw->ops->stringsetsize) {
229     ierr = (*draw->ops->stringsetsize)(draw,width,height);CHKERRQ(ierr);
230   }
231   PetscFunctionReturn(0);
232 }
233 
234 #undef __FUNCT__
235 #define __FUNCT__ "PetscDrawStringGetSize"
236 /*@
237    PetscDrawStringGetSize - Gets the size for character text.  The width is
238    relative to the user coordinates of the window.
239 
240    Not Collective
241 
242    Input Parameters:
243 +  draw - the drawing context
244 .  width - the width in user coordinates
245 -  height - the character height
246 
247    Level: advanced
248 
249    Concepts: string^drawing size
250 
251 .seealso: PetscDrawString(), PetscDrawStringVertical(), PetscDrawStringSetSize()
252 
253 @*/
254 PetscErrorCode  PetscDrawStringGetSize(PetscDraw draw,PetscReal *width,PetscReal *height)
255 {
256   PetscErrorCode ierr;
257   PetscBool      isnull;
258 
259   PetscFunctionBegin;
260   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
261   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
262   if (isnull) {*width = *height = 0.0; PetscFunctionReturn(0);}
263   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);
264   ierr = (*draw->ops->stringgetsize)(draw,width,height);CHKERRQ(ierr);
265   PetscFunctionReturn(0);
266 }
267 
268