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