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