Actual source code: dtext.c
petsc-3.5.4 2015-05-23
2: /*
3: Provides the calling sequences for all the basic PetscDraw routines.
4: */
5: #include <petsc-private/drawimpl.h> /*I "petscdraw.h" I*/
9: /*@C
10: PetscDrawString - PetscDraws text onto a drawable.
12: Not Collective
14: Input Parameters:
15: + draw - the drawing context
16: . xl - the coordinates of lower left corner of text
17: . yl - the coordinates of lower left corner of text
18: . cl - the color of the text
19: - text - the text to draw
21: Level: beginner
23: Concepts: drawing^string
24: Concepts: string^drawing
26: .seealso: PetscDrawStringVertical()
28: @*/
29: PetscErrorCode PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
30: {
32: PetscBool isnull;
37: PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
38: if (isnull) return(0);
39: (*draw->ops->string)(draw,xl,yl,cl,text);
40: return(0);
41: }
45: /*@C
46: PetscDrawBoxedString - Draws a string with a box around it
48: Not Collective
50: Input Parameters:
51: + draw - the drawing context
52: . sxl - the coordinates of center of the box
53: . syl - the coordinates of top line of box
54: . sc - the color of the text
55: . bc - the color of the bounding box
56: - text - the text to draw
58: Output Parameter:
59: . w,h - width and height of resulting box (optional)
61: Level: beginner
63: Concepts: drawing^string
64: Concepts: string^drawing
66: .seealso: PetscDrawStringVertical(), PetscDrawBoxedStringSize()
68: @*/
69: PetscErrorCode PetscDrawBoxedString(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
70: {
72: PetscBool isnull;
73: PetscReal top,left,right,bottom,tw,th;
74: size_t len,mlen = 0;
75: char **array;
76: int cnt,i;
81: PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
82: if (isnull) return(0);
84: if (draw->ops->boxedstring) {
85: (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);
86: return(0);
87: }
89: PetscStrToArray(text,'\n',&cnt,&array);
90: for (i=0; i<cnt; i++) {
91: PetscStrlen(array[i],&len);
92: mlen = PetscMax(mlen,len);
93: }
95: PetscDrawStringGetSize(draw,&tw,&th);
97: top = syl;
98: left = sxl - .5*(mlen + 2)*tw;
99: right = sxl + .5*(mlen + 2)*tw;
100: bottom = syl - (1.0 + cnt)*th;
101: if (w) *w = right - left;
102: if (h) *h = top - bottom;
104: /* compute new bounding box */
105: draw->boundbox_xl = PetscMin(draw->boundbox_xl,left);
106: draw->boundbox_xr = PetscMax(draw->boundbox_xr,right);
107: draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom);
108: draw->boundbox_yr = PetscMax(draw->boundbox_yr,top);
110: /* top, left, bottom, right lines */
111: PetscDrawLine(draw,left,top,right,top,bc);
112: PetscDrawLine(draw,left,bottom,left,top,bc);
113: PetscDrawLine(draw,right,bottom,right,top,bc);
114: PetscDrawLine(draw,left,bottom,right,bottom,bc);
116: for (i=0; i<cnt; i++) {
117: PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);
118: }
119: PetscStrToArrayDestroy(cnt,array);
120: return(0);
121: }