Actual source code: dtext.c

petsc-3.7.7 2017-09-25
Report Typos and Errors
  1: /*
  2:        Provides the calling sequences for all the basic PetscDraw routines.
  3: */
  4: #include <petsc/private/drawimpl.h>  /*I "petscdraw.h" I*/

  8: /*@C
  9:    PetscDrawString - PetscDraws text onto a drawable.

 11:    Not Collective

 13:    Input Parameters:
 14: +  draw - the drawing context
 15: .  xl,yl - the coordinates of lower left corner of text
 16: .  cl - the color of the text
 17: -  text - the text to draw

 19:    Level: beginner

 21:    Concepts: drawing^string
 22:    Concepts: string^drawing

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

 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: }

 41: /*@C
 42:    PetscDrawStringVertical - PetscDraws text onto a drawable.

 44:    Not Collective

 46:    Input Parameters:
 47: +  draw - the drawing context
 48: .  xl,yl - the coordinates of upper left corner of text
 49: .  cl - the color of the text
 50: -  text - the text to draw

 52:    Level: beginner

 54:    Concepts: string^drawing vertical

 56: .seealso: PetscDrawString()

 58: @*/
 59: PetscErrorCode  PetscDrawStringVertical(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
 60: {
 61:   int            i;
 62:   char           chr[2] = {0, 0};
 63:   PetscReal      tw,th;


 70:   if (draw->ops->stringvertical) {
 71:     (*draw->ops->stringvertical)(draw,xl,yl,cl,text);
 72:     return(0);
 73:   }
 74:   PetscDrawStringGetSize(draw,&tw,&th);
 75:   for (i = 0; (chr[0] = text[i]); i++) {
 76:     PetscDrawString(draw,xl,yl-th*(i+1),cl,chr);
 77:   }
 78:   return(0);
 79: }

 83: /*@C
 84:    PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point

 86:    Not Collective

 88:    Input Parameters:
 89: +  draw - the drawing context
 90: .  xc - the coordinates of right-left center of text
 91: .  yl - the coordinates of lower edge of text
 92: .  cl - the color of the text
 93: -  text - the text to draw

 95:    Level: beginner

 97:    Concepts: drawing^string
 98:    Concepts: string^drawing

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

102: @*/
103: PetscErrorCode  PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[])
104: {
106:   size_t         len;
107:   PetscReal      tw,th;


113:   PetscDrawStringGetSize(draw,&tw,&th);
114:   PetscStrlen(text,&len);
115:   xc   = xc - len*tw/2;
116:   PetscDrawString(draw,xc,yl,cl,text);
117:   return(0);
118: }

122: /*@C
123:    PetscDrawStringBoxed - Draws a string with a box around it

125:    Not Collective

127:    Input Parameters:
128: +  draw - the drawing context
129: .  sxl - the coordinates of center of the box
130: .  syl - the coordinates of top line of box
131: .  sc - the color of the text
132: .  bc - the color of the bounding box
133: -  text - the text to draw

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

138:    Level: beginner

140:    Concepts: drawing^string
141:    Concepts: string^drawing

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

145: @*/
146: PetscErrorCode  PetscDrawStringBoxed(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
147: {
149:   PetscReal      top,left,right,bottom,tw,th;
150:   size_t         len,mlen = 0;
151:   char           **array;
152:   int            cnt,i;


158:   if (draw->ops->boxedstring) {
159:     (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);
160:     return(0);
161:   }

163:   PetscStrToArray(text,'\n',&cnt,&array);
164:   for (i=0; i<cnt; i++) {
165:     PetscStrlen(array[i],&len);
166:     mlen = PetscMax(mlen,len);
167:   }

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

171:   top    = syl;
172:   left   = sxl - .5*(mlen + 2)*tw;
173:   right  = sxl + .5*(mlen + 2)*tw;
174:   bottom = syl - (1.0 + cnt)*th;
175:   if (w) *w = right - left;
176:   if (h) *h = top - bottom;

178:   /* compute new bounding box */
179:   draw->boundbox_xl = PetscMin(draw->boundbox_xl,left);
180:   draw->boundbox_xr = PetscMax(draw->boundbox_xr,right);
181:   draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom);
182:   draw->boundbox_yr = PetscMax(draw->boundbox_yr,top);

184:   /* top, left, bottom, right lines */
185:   PetscDrawLine(draw,left,top,right,top,bc);
186:   PetscDrawLine(draw,left,bottom,left,top,bc);
187:   PetscDrawLine(draw,right,bottom,right,top,bc);
188:   PetscDrawLine(draw,left,bottom,right,bottom,bc);

190:   for  (i=0; i<cnt; i++) {
191:     PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);
192:   }
193:   PetscStrToArrayDestroy(cnt,array);
194:   return(0);
195: }

199: /*@
200:    PetscDrawStringSetSize - Sets the size for character text.

202:    Not Collective

204:    Input Parameters:
205: +  draw - the drawing context
206: .  width - the width in user coordinates
207: -  height - the character height in user coordinates

209:    Level: advanced

211:    Note:
212:    Only a limited range of sizes are available.

214:    Concepts: string^drawing size

216: .seealso: PetscDrawString(), PetscDrawStringVertical(), PetscDrawStringGetSize()

218: @*/
219: PetscErrorCode  PetscDrawStringSetSize(PetscDraw draw,PetscReal width,PetscReal height)
220: {

225:   if (draw->ops->stringsetsize) {
226:     (*draw->ops->stringsetsize)(draw,width,height);
227:   }
228:   return(0);
229: }

233: /*@
234:    PetscDrawStringGetSize - Gets the size for character text.  The width is
235:    relative to the user coordinates of the window.

237:    Not Collective

239:    Input Parameters:
240: +  draw - the drawing context
241: .  width - the width in user coordinates
242: -  height - the character height

244:    Level: advanced

246:    Concepts: string^drawing size

248: .seealso: PetscDrawString(), PetscDrawStringVertical(), PetscDrawStringSetSize()

250: @*/
251: PetscErrorCode  PetscDrawStringGetSize(PetscDraw draw,PetscReal *width,PetscReal *height)
252: {

257:   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);
258:   (*draw->ops->stringgetsize)(draw,width,height);
259:   return(0);
260: }