Actual source code: dtext.c
petsc-3.9.4 2018-09-11
1: /*
2: Provides the calling sequences for all the basic PetscDraw routines.
3: */
4: #include <petsc/private/drawimpl.h>
6: /*@C
7: PetscDrawString - PetscDraws text onto a drawable.
9: Not Collective
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
17: Level: beginner
19: Concepts: drawing^string
20: Concepts: string^drawing
22: .seealso: PetscDrawStringVertical(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
23: PetscDrawStringGetSize(), PetscDrawLine(), PetscDrawRectangle(), PetscDrawTriangle(), PetscDrawEllipse(),
24: PetscDrawMarker(), PetscDrawPoint()
26: @*/
27: PetscErrorCode PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
28: {
34: if (!draw->ops->string) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support drawing strings",((PetscObject)draw)->type_name);
35: (*draw->ops->string)(draw,xl,yl,cl,text);
36: return(0);
37: }
39: /*@C
40: PetscDrawStringVertical - PetscDraws text onto a drawable.
42: Not Collective
44: Input Parameters:
45: + draw - the drawing context
46: . xl,yl - the coordinates of upper left corner of text
47: . cl - the color of the text
48: - text - the text to draw
50: Level: beginner
52: Concepts: string^drawing vertical
54: .seealso: PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
55: PetscDrawStringGetSize()
57: @*/
58: PetscErrorCode PetscDrawStringVertical(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
59: {
60: int i;
61: char chr[2] = {0, 0};
62: PetscReal tw,th;
69: if (draw->ops->stringvertical) {
70: (*draw->ops->stringvertical)(draw,xl,yl,cl,text);
71: return(0);
72: }
73: PetscDrawStringGetSize(draw,&tw,&th);
74: for (i = 0; (chr[0] = text[i]); i++) {
75: PetscDrawString(draw,xl,yl-th*(i+1),cl,chr);
76: }
77: return(0);
78: }
80: /*@C
81: PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point
83: Not Collective
85: Input Parameters:
86: + draw - the drawing context
87: . xc - the coordinates of right-left center of text
88: . yl - the coordinates of lower edge of text
89: . cl - the color of the text
90: - text - the text to draw
92: Level: beginner
94: Concepts: drawing^string
95: Concepts: string^drawing
97: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
98: PetscDrawStringGetSize()
100: @*/
101: PetscErrorCode PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[])
102: {
104: size_t len;
105: PetscReal tw,th;
111: PetscDrawStringGetSize(draw,&tw,&th);
112: PetscStrlen(text,&len);
113: xc = xc - len*tw/2;
114: PetscDrawString(draw,xc,yl,cl,text);
115: return(0);
116: }
118: /*@C
119: PetscDrawStringBoxed - Draws a string with a box around it
121: Not Collective
123: Input Parameters:
124: + draw - the drawing context
125: . sxl - the coordinates of center of the box
126: . syl - the coordinates of top line of box
127: . sc - the color of the text
128: . bc - the color of the bounding box
129: - text - the text to draw
131: Output Parameter:
132: . w,h - width and height of resulting box (optional)
134: Level: beginner
136: Concepts: drawing^string
137: Concepts: string^drawing
139: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
140: PetscDrawStringGetSize()
142: @*/
143: PetscErrorCode PetscDrawStringBoxed(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
144: {
146: PetscReal top,left,right,bottom,tw,th;
147: size_t len,mlen = 0;
148: char **array;
149: int cnt,i;
155: if (draw->ops->boxedstring) {
156: (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);
157: return(0);
158: }
160: PetscStrToArray(text,'\n',&cnt,&array);
161: for (i=0; i<cnt; i++) {
162: PetscStrlen(array[i],&len);
163: mlen = PetscMax(mlen,len);
164: }
166: PetscDrawStringGetSize(draw,&tw,&th);
168: top = syl;
169: left = sxl - .5*(mlen + 2)*tw;
170: right = sxl + .5*(mlen + 2)*tw;
171: bottom = syl - (1.0 + cnt)*th;
172: if (w) *w = right - left;
173: if (h) *h = top - bottom;
175: /* compute new bounding box */
176: draw->boundbox_xl = PetscMin(draw->boundbox_xl,left);
177: draw->boundbox_xr = PetscMax(draw->boundbox_xr,right);
178: draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom);
179: draw->boundbox_yr = PetscMax(draw->boundbox_yr,top);
181: /* top, left, bottom, right lines */
182: PetscDrawLine(draw,left,top,right,top,bc);
183: PetscDrawLine(draw,left,bottom,left,top,bc);
184: PetscDrawLine(draw,right,bottom,right,top,bc);
185: PetscDrawLine(draw,left,bottom,right,bottom,bc);
187: for (i=0; i<cnt; i++) {
188: PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);
189: }
190: PetscStrToArrayDestroy(cnt,array);
191: return(0);
192: }
194: /*@
195: PetscDrawStringSetSize - Sets the size for character text.
197: Not Collective
199: Input Parameters:
200: + draw - the drawing context
201: . width - the width in user coordinates
202: - height - the character height in user coordinates
204: Level: advanced
206: Note:
207: Only a limited range of sizes are available.
209: Concepts: string^drawing size
211: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(),
212: PetscDrawStringGetSize()
214: @*/
215: PetscErrorCode PetscDrawStringSetSize(PetscDraw draw,PetscReal width,PetscReal height)
216: {
221: if (draw->ops->stringsetsize) {
222: (*draw->ops->stringsetsize)(draw,width,height);
223: }
224: return(0);
225: }
227: /*@
228: PetscDrawStringGetSize - Gets the size for character text. The width is
229: relative to the user coordinates of the window.
231: Not Collective
233: Input Parameters:
234: + draw - the drawing context
235: . width - the width in user coordinates
236: - height - the character height
238: Level: advanced
240: Concepts: string^drawing size
242: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(),
243: PetscDrawStringSetSize()
245: @*/
246: PetscErrorCode PetscDrawStringGetSize(PetscDraw draw,PetscReal *width,PetscReal *height)
247: {
252: if (!draw->ops->stringgetsize) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support getting string size",((PetscObject)draw)->type_name);
253: (*draw->ops->stringgetsize)(draw,width,height);
254: return(0);
255: }