xref: /petsc/src/sys/classes/draw/interface/dtext.c (revision 697336901c45ac77e1fd620fe1fca906cf3f95c8)
1 
2 #include <petsc/private/drawimpl.h> /*I "petscdraw.h" I*/
3 
4 /*@C
5   PetscDrawString - draws text onto a drawable.
6 
7   Not Collective
8 
9   Input Parameters:
10 + draw - the drawing context
11 . xl   - coordinate of lower left corner of text
12 . yl   - coordinate of lower left corner of text
13 . cl   - the color of the text
14 - text - the text to draw
15 
16   Level: beginner
17 
18 .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`, `PetscDrawStringSetSize()`,
19           `PetscDrawStringGetSize()`, `PetscDrawLine()`, `PetscDrawRectangle()`, `PetscDrawTriangle()`, `PetscDrawEllipse()`,
20           `PetscDrawMarker()`, `PetscDrawPoint()`
21 @*/
22 PetscErrorCode PetscDrawString(PetscDraw draw, PetscReal xl, PetscReal yl, int cl, const char text[])
23 {
24   PetscFunctionBegin;
25   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
26   PetscValidCharPointer(text, 5);
27   PetscUseTypeMethod(draw, string, xl, yl, cl, text);
28   PetscFunctionReturn(PETSC_SUCCESS);
29 }
30 
31 /*@C
32   PetscDrawStringVertical - draws text onto a drawable.
33 
34   Not Collective
35 
36   Input Parameters:
37 + draw - the drawing context
38 . xl   - coordinate of upper left corner of text
39 . yl   - coordinate 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(PETSC_SUCCESS);
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(PETSC_SUCCESS);
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 Parameters:
113 + w - the width of the resulting box (optional)
114 - h - the height of resulting box (optional)
115 
116   Level: beginner
117 
118 .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringSetSize()`,
119           `PetscDrawStringGetSize()`
120 @*/
121 PetscErrorCode PetscDrawStringBoxed(PetscDraw draw, PetscReal sxl, PetscReal syl, int sc, int bc, const char text[], PetscReal *w, PetscReal *h)
122 {
123   PetscReal top, left, right, bottom, tw, th;
124   size_t    len, mlen = 0;
125   char    **array;
126   int       cnt, i;
127 
128   PetscFunctionBegin;
129   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
130   PetscValidCharPointer(text, 6);
131 
132   if (draw->ops->boxedstring) {
133     PetscUseTypeMethod(draw, boxedstring, sxl, syl, sc, bc, text, w, h);
134     PetscFunctionReturn(PETSC_SUCCESS);
135   }
136 
137   PetscCall(PetscStrToArray(text, '\n', &cnt, &array));
138   for (i = 0; i < cnt; i++) {
139     PetscCall(PetscStrlen(array[i], &len));
140     mlen = PetscMax(mlen, len);
141   }
142 
143   PetscCall(PetscDrawStringGetSize(draw, &tw, &th));
144 
145   top    = syl;
146   left   = sxl - .5 * (mlen + 2) * tw;
147   right  = sxl + .5 * (mlen + 2) * tw;
148   bottom = syl - (1.0 + cnt) * th;
149   if (w) *w = right - left;
150   if (h) *h = top - bottom;
151 
152   /* compute new bounding box */
153   draw->boundbox_xl = PetscMin(draw->boundbox_xl, left);
154   draw->boundbox_xr = PetscMax(draw->boundbox_xr, right);
155   draw->boundbox_yl = PetscMin(draw->boundbox_yl, bottom);
156   draw->boundbox_yr = PetscMax(draw->boundbox_yr, top);
157 
158   /* top, left, bottom, right lines */
159   PetscCall(PetscDrawLine(draw, left, top, right, top, bc));
160   PetscCall(PetscDrawLine(draw, left, bottom, left, top, bc));
161   PetscCall(PetscDrawLine(draw, right, bottom, right, top, bc));
162   PetscCall(PetscDrawLine(draw, left, bottom, right, bottom, bc));
163 
164   for (i = 0; i < cnt; i++) PetscCall(PetscDrawString(draw, left + tw, top - (1.5 + i) * th, sc, array[i]));
165   PetscCall(PetscStrToArrayDestroy(cnt, array));
166   PetscFunctionReturn(PETSC_SUCCESS);
167 }
168 
169 /*@
170   PetscDrawStringSetSize - Sets the size for character text.
171 
172   Not Collective
173 
174   Input Parameters:
175 + draw   - the drawing context
176 . width  - the width in user coordinates
177 - height - the character height in user coordinates
178 
179   Level: advanced
180 
181   Note:
182   Only a limited range of sizes are available.
183 
184 .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`,
185           `PetscDrawStringGetSize()`
186 @*/
187 PetscErrorCode PetscDrawStringSetSize(PetscDraw draw, PetscReal width, PetscReal height)
188 {
189   PetscFunctionBegin;
190   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
191   PetscTryTypeMethod(draw, stringsetsize, width, height);
192   PetscFunctionReturn(PETSC_SUCCESS);
193 }
194 
195 /*@
196   PetscDrawStringGetSize - Gets the size for character text.  The width is
197   relative to the user coordinates of the window.
198 
199   Not Collective
200 
201   Input Parameters:
202 + draw   - the drawing context
203 . width  - the width in user coordinates
204 - height - the character height
205 
206   Level: advanced
207 
208 .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`,
209           `PetscDrawStringSetSize()`
210 @*/
211 PetscErrorCode PetscDrawStringGetSize(PetscDraw draw, PetscReal *width, PetscReal *height)
212 {
213   PetscFunctionBegin;
214   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
215   PetscUseTypeMethod(draw, stringgetsize, width, height);
216   PetscFunctionReturn(PETSC_SUCCESS);
217 }
218