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