xref: /petsc/src/sys/classes/draw/interface/dtext.c (revision b0b385f45f76f1e108f857efe1d02ffe3b58ed6c)
1 #include <petsc/private/drawimpl.h> /*I "petscdraw.h" I*/
2 
3 /*@C
4   PetscDrawString - draws text onto a drawable.
5 
6   Not Collective
7 
8   Input Parameters:
9 + draw - the drawing context
10 . xl   - coordinate of lower left corner of text
11 . yl   - coordinate of lower left corner of text
12 . cl   - the color of the text
13 - text - the text to draw
14 
15   Level: beginner
16 
17 .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`, `PetscDrawStringSetSize()`,
18           `PetscDrawStringGetSize()`, `PetscDrawLine()`, `PetscDrawRectangle()`, `PetscDrawTriangle()`, `PetscDrawEllipse()`,
19           `PetscDrawMarker()`, `PetscDrawPoint()`
20 @*/
21 PetscErrorCode PetscDrawString(PetscDraw draw, PetscReal xl, PetscReal yl, int cl, const char text[])
22 {
23   PetscFunctionBegin;
24   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
25   PetscAssertPointer(text, 5);
26   PetscUseTypeMethod(draw, string, xl, yl, cl, text);
27   PetscFunctionReturn(PETSC_SUCCESS);
28 }
29 
30 /*@C
31   PetscDrawStringVertical - draws text onto a drawable.
32 
33   Not Collective
34 
35   Input Parameters:
36 + draw - the drawing context
37 . xl   - coordinate of upper left corner of text
38 . yl   - coordinate of upper left corner of text
39 . cl   - the color of the text
40 - text - the text to draw
41 
42   Level: beginner
43 
44 .seealso: `PetscDraw`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`, `PetscDrawStringSetSize()`,
45           `PetscDrawStringGetSize()`
46 @*/
47 PetscErrorCode PetscDrawStringVertical(PetscDraw draw, PetscReal xl, PetscReal yl, int cl, const char text[])
48 {
49   int       i;
50   char      chr[2] = {0, 0};
51   PetscReal tw, th;
52 
53   PetscFunctionBegin;
54   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
55   PetscAssertPointer(text, 5);
56 
57   if (draw->ops->stringvertical) PetscUseTypeMethod(draw, stringvertical, xl, yl, cl, text);
58   else {
59     PetscCall(PetscDrawStringGetSize(draw, &tw, &th));
60     for (i = 0; (chr[0] = text[i]); i++) PetscCall(PetscDrawString(draw, xl, yl - th * (i + 1), cl, chr));
61   }
62   PetscFunctionReturn(PETSC_SUCCESS);
63 }
64 
65 /*@C
66   PetscDrawStringCentered - draws text onto a drawable centered at a point
67 
68   Not Collective
69 
70   Input Parameters:
71 + draw - the drawing context
72 . xc   - the coordinates of right-left center of text
73 . yl   - the coordinates of lower edge of text
74 . cl   - the color of the text
75 - text - the text to draw
76 
77   Level: beginner
78 
79 .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringBoxed()`, `PetscDrawStringSetSize()`,
80           `PetscDrawStringGetSize()`
81 @*/
82 PetscErrorCode PetscDrawStringCentered(PetscDraw draw, PetscReal xc, PetscReal yl, int cl, const char text[])
83 {
84   size_t    len;
85   PetscReal tw, th;
86 
87   PetscFunctionBegin;
88   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
89   PetscAssertPointer(text, 5);
90 
91   PetscCall(PetscDrawStringGetSize(draw, &tw, &th));
92   PetscCall(PetscStrlen(text, &len));
93   xc = xc - len * tw / 2;
94   PetscCall(PetscDrawString(draw, xc, yl, cl, text));
95   PetscFunctionReturn(PETSC_SUCCESS);
96 }
97 
98 /*@C
99   PetscDrawStringBoxed - Draws a string with a box around it
100 
101   Not Collective
102 
103   Input Parameters:
104 + draw - the drawing context
105 . sxl  - the coordinates of center of the box
106 . syl  - the coordinates of top line of box
107 . sc   - the color of the text
108 . bc   - the color of the bounding box
109 - text - the text to draw
110 
111   Output Parameters:
112 + w - the width of the resulting box (optional)
113 - h - the 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   PetscAssertPointer(text, 6);
130 
131   if (draw->ops->boxedstring) {
132     PetscUseTypeMethod(draw, boxedstring, sxl, syl, sc, bc, text, w, h);
133     PetscFunctionReturn(PETSC_SUCCESS);
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(PETSC_SUCCESS);
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(PETSC_SUCCESS);
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(PETSC_SUCCESS);
216 }
217