Actual source code: dtext.c
1: #include <petsc/private/drawimpl.h>
3: /*@C
4: PetscDrawString - draws text onto a drawable.
6: Not Collective
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
15: Level: beginner
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;
25: PetscAssertPointer(text, 5);
26: PetscUseTypeMethod(draw, string, xl, yl, cl, text);
27: PetscFunctionReturn(PETSC_SUCCESS);
28: }
30: /*@C
31: PetscDrawStringVertical - draws text onto a drawable.
33: Not Collective
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
42: Level: beginner
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;
53: PetscFunctionBegin;
55: PetscAssertPointer(text, 5);
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: }
65: /*@C
66: PetscDrawStringCentered - draws text onto a drawable centered at a point
68: Not Collective
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
77: Level: beginner
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;
87: PetscFunctionBegin;
89: PetscAssertPointer(text, 5);
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: }
98: /*@C
99: PetscDrawStringBoxed - Draws a string with a box around it
101: Not Collective
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
111: Output Parameters:
112: + w - the width of the resulting box (optional)
113: - h - the height of resulting box (optional)
115: Level: beginner
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;
127: PetscFunctionBegin;
129: PetscAssertPointer(text, 6);
131: if (draw->ops->boxedstring) {
132: PetscUseTypeMethod(draw, boxedstring, sxl, syl, sc, bc, text, w, h);
133: PetscFunctionReturn(PETSC_SUCCESS);
134: }
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: }
142: PetscCall(PetscDrawStringGetSize(draw, &tw, &th));
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;
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);
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));
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: }
168: /*@
169: PetscDrawStringSetSize - Sets the size for character text.
171: Not Collective
173: Input Parameters:
174: + draw - the drawing context
175: . width - the width in user coordinates
176: - height - the character height in user coordinates
178: Level: advanced
180: Note:
181: Only a limited range of sizes are available.
183: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`,
184: `PetscDrawStringGetSize()`
185: @*/
186: PetscErrorCode PetscDrawStringSetSize(PetscDraw draw, PetscReal width, PetscReal height)
187: {
188: PetscFunctionBegin;
190: PetscTryTypeMethod(draw, stringsetsize, width, height);
191: PetscFunctionReturn(PETSC_SUCCESS);
192: }
194: /*@
195: PetscDrawStringGetSize - Gets the size for character text. The width is
196: relative to the user coordinates of the window.
198: Not Collective
200: Input Parameters:
201: + draw - the drawing context
202: . width - the width in user coordinates
203: - height - the character height
205: Level: advanced
207: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`,
208: `PetscDrawStringSetSize()`
209: @*/
210: PetscErrorCode PetscDrawStringGetSize(PetscDraw draw, PetscReal *width, PetscReal *height)
211: {
212: PetscFunctionBegin;
214: PetscUseTypeMethod(draw, stringgetsize, width, height);
215: PetscFunctionReturn(PETSC_SUCCESS);
216: }