xref: /petsc/src/sys/classes/draw/interface/dtext.c (revision af0996ce37bc06907c37d8d91773840993d61e62) !
1 
2 /*
3        Provides the calling sequences for all the basic PetscDraw routines.
4 */
5 #include <petsc/private/drawimpl.h>  /*I "petscdraw.h" I*/
6 
7 #undef __FUNCT__
8 #define __FUNCT__ "PetscDrawStringCentered"
9 /*@C
10    PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point
11 
12    Not Collective
13 
14    Input Parameters:
15 +  draw - the drawing context
16 .  xc - the coordinates of right-left center of text
17 .  yl - the coordinates of lower edge of text
18 .  cl - the color of the text
19 -  text - the text to draw
20 
21    Level: beginner
22 
23    Concepts: drawing^string
24    Concepts: string^drawing
25 
26 .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed()
27 
28 @*/
29 PetscErrorCode  PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[])
30 {
31   PetscErrorCode ierr;
32   PetscBool      isnull;
33   size_t         len;
34   PetscReal      tw,th;
35 
36   PetscFunctionBegin;
37   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
38   PetscValidCharPointer(text,5);
39   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
40   if (isnull) PetscFunctionReturn(0);
41 
42   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
43   ierr =  PetscStrlen(text,&len);CHKERRQ(ierr);
44   xc   = xc - .5*len*tw;
45   ierr = PetscDrawString(draw,xc,yl,cl,text);CHKERRQ(ierr);
46   PetscFunctionReturn(0);
47 }
48 
49 
50 #undef __FUNCT__
51 #define __FUNCT__ "PetscDrawString"
52 /*@C
53    PetscDrawString - PetscDraws text onto a drawable.
54 
55    Not Collective
56 
57    Input Parameters:
58 +  draw - the drawing context
59 .  xl - the coordinates of lower left corner of text
60 .  yl - the coordinates of lower left corner of text
61 .  cl - the color of the text
62 -  text - the text to draw
63 
64    Level: beginner
65 
66    Concepts: drawing^string
67    Concepts: string^drawing
68 
69 .seealso: PetscDrawStringVertical(), PetscDrawStringCentered(), PetscDrawStringBoxed()
70 
71 @*/
72 PetscErrorCode  PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
73 {
74   PetscErrorCode ierr;
75   PetscBool      isnull;
76 
77   PetscFunctionBegin;
78   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
79   PetscValidCharPointer(text,5);
80   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
81   if (isnull) PetscFunctionReturn(0);
82   ierr = (*draw->ops->string)(draw,xl,yl,cl,text);CHKERRQ(ierr);
83   PetscFunctionReturn(0);
84 }
85 
86 #undef __FUNCT__
87 #define __FUNCT__ "PetscDrawStringBoxed"
88 /*@C
89    PetscDrawStringBoxed - Draws a string with a box around it
90 
91    Not Collective
92 
93    Input Parameters:
94 +  draw - the drawing context
95 .  sxl - the coordinates of center of the box
96 .  syl - the coordinates of top line of box
97 .  sc - the color of the text
98 .  bc - the color of the bounding box
99 -  text - the text to draw
100 
101    Output Parameter:
102 .   w,h - width and height of resulting box (optional)
103 
104    Level: beginner
105 
106    Concepts: drawing^string
107    Concepts: string^drawing
108 
109 .seealso: PetscDrawStringVertical(), PetscDrawStringBoxedSize(), PetscDrawString(), PetscDrawStringCentered()
110 
111 @*/
112 PetscErrorCode  PetscDrawStringBoxed(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
113 {
114   PetscErrorCode ierr;
115   PetscBool      isnull;
116   PetscReal      top,left,right,bottom,tw,th;
117   size_t         len,mlen = 0;
118   char           **array;
119   int            cnt,i;
120 
121   PetscFunctionBegin;
122   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
123   PetscValidCharPointer(text,5);
124   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
125   if (isnull) PetscFunctionReturn(0);
126 
127   if (draw->ops->boxedstring) {
128     ierr = (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);CHKERRQ(ierr);
129     PetscFunctionReturn(0);
130   }
131 
132   ierr = PetscStrToArray(text,'\n',&cnt,&array);CHKERRQ(ierr);
133   for (i=0; i<cnt; i++) {
134     ierr = PetscStrlen(array[i],&len);CHKERRQ(ierr);
135     mlen = PetscMax(mlen,len);
136   }
137 
138   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
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   ierr = PetscDrawLine(draw,left,top,right,top,bc);CHKERRQ(ierr);
155   ierr = PetscDrawLine(draw,left,bottom,left,top,bc);CHKERRQ(ierr);
156   ierr = PetscDrawLine(draw,right,bottom,right,top,bc);CHKERRQ(ierr);
157   ierr = PetscDrawLine(draw,left,bottom,right,bottom,bc);CHKERRQ(ierr);
158 
159   for  (i=0; i<cnt; i++) {
160     ierr = PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);CHKERRQ(ierr);
161   }
162   ierr = PetscStrToArrayDestroy(cnt,array);CHKERRQ(ierr);
163   PetscFunctionReturn(0);
164 }
165