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