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