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