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