Actual source code: dtext.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  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:    PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point

 12:    Not Collective

 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

 21:    Level: beginner

 23:    Concepts: drawing^string
 24:    Concepts: string^drawing

 26: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed()

 28: @*/
 29: PetscErrorCode  PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[])
 30: {
 32:   PetscBool      isnull;
 33:   size_t         len;
 34:   PetscReal      tw,th;

 39:   PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
 40:   if (isnull) return(0);

 42:   PetscDrawStringGetSize(draw,&tw,&th);
 43:    PetscStrlen(text,&len);
 44:   xc   = xc - .5*len*tw;
 45:   PetscDrawString(draw,xc,yl,cl,text);
 46:   return(0);
 47: }


 52: /*@C
 53:    PetscDrawString - PetscDraws text onto a drawable.

 55:    Not Collective

 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

 64:    Level: beginner

 66:    Concepts: drawing^string
 67:    Concepts: string^drawing

 69: .seealso: PetscDrawStringVertical(), PetscDrawStringCentered(), PetscDrawStringBoxed()

 71: @*/
 72: PetscErrorCode  PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
 73: {
 75:   PetscBool      isnull;

 80:   PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
 81:   if (isnull) return(0);
 82:   (*draw->ops->string)(draw,xl,yl,cl,text);
 83:   return(0);
 84: }

 88: /*@C
 89:    PetscDrawStringBoxed - Draws a string with a box around it

 91:    Not Collective

 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

101:    Output Parameter:
102: .   w,h - width and height of resulting box (optional)

104:    Level: beginner

106:    Concepts: drawing^string
107:    Concepts: string^drawing

109: .seealso: PetscDrawStringVertical(), PetscDrawStringBoxedSize(), PetscDrawString(), PetscDrawStringCentered()

111: @*/
112: PetscErrorCode  PetscDrawStringBoxed(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
113: {
115:   PetscBool      isnull;
116:   PetscReal      top,left,right,bottom,tw,th;
117:   size_t         len,mlen = 0;
118:   char           **array;
119:   int            cnt,i;

124:   PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
125:   if (isnull) return(0);

127:   if (draw->ops->boxedstring) {
128:     (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);
129:     return(0);
130:   }

132:   PetscStrToArray(text,'\n',&cnt,&array);
133:   for (i=0; i<cnt; i++) {
134:     PetscStrlen(array[i],&len);
135:     mlen = PetscMax(mlen,len);
136:   }

138:   PetscDrawStringGetSize(draw,&tw,&th);

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;

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);

153:   /* top, left, bottom, right lines */
154:   PetscDrawLine(draw,left,top,right,top,bc);
155:   PetscDrawLine(draw,left,bottom,left,top,bc);
156:   PetscDrawLine(draw,right,bottom,right,top,bc);
157:   PetscDrawLine(draw,left,bottom,right,bottom,bc);

159:   for  (i=0; i<cnt; i++) {
160:     PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);
161:   }
162:   PetscStrToArrayDestroy(cnt,array);
163:   return(0);
164: }