xref: /petsc/src/sys/classes/draw/interface/dtext.c (revision 9895aa37ac365bac650f6bd8bf977519f7222510)
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__ "PetscDrawString"
9 /*@C
10    PetscDrawString - PetscDraws text onto a drawable.
11 
12    Not Collective
13 
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
20 
21    Level: beginner
22 
23    Concepts: drawing^string
24    Concepts: string^drawing
25 
26 .seealso: PetscDrawStringVertical()
27 
28 @*/
29 PetscErrorCode  PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
30 {
31   PetscErrorCode ierr;
32   PetscBool      isnull;
33 
34   PetscFunctionBegin;
35   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
36   PetscValidCharPointer(text,5);
37   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
38   if (isnull) PetscFunctionReturn(0);
39   ierr = (*draw->ops->string)(draw,xl,yl,cl,text);CHKERRQ(ierr);
40   PetscFunctionReturn(0);
41 }
42 
43 #undef __FUNCT__
44 #define __FUNCT__ "PetscDrawBoxedString"
45 /*@C
46    PetscDrawBoxedString - Draws a string with a box around it
47 
48    Not Collective
49 
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
57 
58    Output Parameter:
59 .   w,h - width and height of resulting box (optional)
60 
61    Level: beginner
62 
63    Concepts: drawing^string
64    Concepts: string^drawing
65 
66 .seealso: PetscDrawStringVertical(), PetscDrawBoxedStringSize()
67 
68 @*/
69 PetscErrorCode  PetscDrawBoxedString(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
70 {
71   PetscErrorCode ierr;
72   PetscBool      isnull;
73   PetscReal      top,left,right,bottom,tw,th;
74   size_t         len,mlen = 0;
75   char           **array;
76   int            cnt,i;
77 
78   PetscFunctionBegin;
79   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
80   PetscValidCharPointer(text,5);
81   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
82   if (isnull) PetscFunctionReturn(0);
83 
84   if (draw->ops->boxedstring) {
85     ierr = (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);CHKERRQ(ierr);
86     PetscFunctionReturn(0);
87   }
88 
89   ierr = PetscStrToArray(text,'\n',&cnt,&array);CHKERRQ(ierr);
90   for (i=0; i<cnt; i++) {
91     ierr = PetscStrlen(array[i],&len);CHKERRQ(ierr);
92     mlen = PetscMax(mlen,len);
93   }
94 
95   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
96 
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;
103 
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);
109 
110   /* top, left, bottom, right lines */
111   ierr = PetscDrawLine(draw,left,top,right,top,bc);CHKERRQ(ierr);
112   ierr = PetscDrawLine(draw,left,bottom,left,top,bc);CHKERRQ(ierr);
113   ierr = PetscDrawLine(draw,right,bottom,right,top,bc);CHKERRQ(ierr);
114   ierr = PetscDrawLine(draw,left,bottom,right,bottom,bc);CHKERRQ(ierr);
115 
116   for  (i=0; i<cnt; i++) {
117     ierr = PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);CHKERRQ(ierr);
118   }
119   ierr = PetscStrToArrayDestroy(cnt,array);CHKERRQ(ierr);
120   PetscFunctionReturn(0);
121 }
122