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