Actual source code: dtext.c

  1: /*
  2:        Provides the calling sequences for all the basic PetscDraw routines.
  3: */
  4: #include <petsc/private/drawimpl.h>

  6: /*@C
  7:    PetscDrawString - PetscDraws text onto a drawable.

  9:    Not Collective

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

 17:    Level: beginner

 19: .seealso: PetscDrawStringVertical(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
 20:           PetscDrawStringGetSize(), PetscDrawLine(), PetscDrawRectangle(), PetscDrawTriangle(), PetscDrawEllipse(),
 21:           PetscDrawMarker(), PetscDrawPoint()

 23: @*/
 24: PetscErrorCode  PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
 25: {

 31:   if (!draw->ops->string) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support drawing strings",((PetscObject)draw)->type_name);
 32:   (*draw->ops->string)(draw,xl,yl,cl,text);
 33:   return(0);
 34: }

 36: /*@C
 37:    PetscDrawStringVertical - PetscDraws text onto a drawable.

 39:    Not Collective

 41:    Input Parameters:
 42: +  draw - the drawing context
 43: .  xl,yl - the coordinates of upper left corner of text
 44: .  cl - the color of the text
 45: -  text - the text to draw

 47:    Level: beginner

 49: .seealso: PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
 50:           PetscDrawStringGetSize()

 52: @*/
 53: PetscErrorCode  PetscDrawStringVertical(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
 54: {
 55:   int            i;
 56:   char           chr[2] = {0, 0};
 57:   PetscReal      tw,th;


 64:   if (draw->ops->stringvertical) {
 65:     (*draw->ops->stringvertical)(draw,xl,yl,cl,text);
 66:     return(0);
 67:   }
 68:   PetscDrawStringGetSize(draw,&tw,&th);
 69:   for (i = 0; (chr[0] = text[i]); i++) {
 70:     PetscDrawString(draw,xl,yl-th*(i+1),cl,chr);
 71:   }
 72:   return(0);
 73: }

 75: /*@C
 76:    PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point

 78:    Not Collective

 80:    Input Parameters:
 81: +  draw - the drawing context
 82: .  xc - the coordinates of right-left center of text
 83: .  yl - the coordinates of lower edge of text
 84: .  cl - the color of the text
 85: -  text - the text to draw

 87:    Level: beginner

 89: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
 90:           PetscDrawStringGetSize()

 92: @*/
 93: PetscErrorCode  PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[])
 94: {
 96:   size_t         len;
 97:   PetscReal      tw,th;


103:   PetscDrawStringGetSize(draw,&tw,&th);
104:   PetscStrlen(text,&len);
105:   xc   = xc - len*tw/2;
106:   PetscDrawString(draw,xc,yl,cl,text);
107:   return(0);
108: }

110: /*@C
111:    PetscDrawStringBoxed - Draws a string with a box around it

113:    Not Collective

115:    Input Parameters:
116: +  draw - the drawing context
117: .  sxl - the coordinates of center of the box
118: .  syl - the coordinates of top line of box
119: .  sc - the color of the text
120: .  bc - the color of the bounding box
121: -  text - the text to draw

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

126:    Level: beginner

128: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringSetSize(),
129:           PetscDrawStringGetSize()

131: @*/
132: PetscErrorCode  PetscDrawStringBoxed(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
133: {
135:   PetscReal      top,left,right,bottom,tw,th;
136:   size_t         len,mlen = 0;
137:   char           **array;
138:   int            cnt,i;


144:   if (draw->ops->boxedstring) {
145:     (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);
146:     return(0);
147:   }

149:   PetscStrToArray(text,'\n',&cnt,&array);
150:   for (i=0; i<cnt; i++) {
151:     PetscStrlen(array[i],&len);
152:     mlen = PetscMax(mlen,len);
153:   }

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

157:   top    = syl;
158:   left   = sxl - .5*(mlen + 2)*tw;
159:   right  = sxl + .5*(mlen + 2)*tw;
160:   bottom = syl - (1.0 + cnt)*th;
161:   if (w) *w = right - left;
162:   if (h) *h = top - bottom;

164:   /* compute new bounding box */
165:   draw->boundbox_xl = PetscMin(draw->boundbox_xl,left);
166:   draw->boundbox_xr = PetscMax(draw->boundbox_xr,right);
167:   draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom);
168:   draw->boundbox_yr = PetscMax(draw->boundbox_yr,top);

170:   /* top, left, bottom, right lines */
171:   PetscDrawLine(draw,left,top,right,top,bc);
172:   PetscDrawLine(draw,left,bottom,left,top,bc);
173:   PetscDrawLine(draw,right,bottom,right,top,bc);
174:   PetscDrawLine(draw,left,bottom,right,bottom,bc);

176:   for  (i=0; i<cnt; i++) {
177:     PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);
178:   }
179:   PetscStrToArrayDestroy(cnt,array);
180:   return(0);
181: }

183: /*@
184:    PetscDrawStringSetSize - Sets the size for character text.

186:    Not Collective

188:    Input Parameters:
189: +  draw - the drawing context
190: .  width - the width in user coordinates
191: -  height - the character height in user coordinates

193:    Level: advanced

195:    Note:
196:    Only a limited range of sizes are available.

198: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(),
199:           PetscDrawStringGetSize()

201: @*/
202: PetscErrorCode  PetscDrawStringSetSize(PetscDraw draw,PetscReal width,PetscReal height)
203: {

208:   if (draw->ops->stringsetsize) {
209:     (*draw->ops->stringsetsize)(draw,width,height);
210:   }
211:   return(0);
212: }

214: /*@
215:    PetscDrawStringGetSize - Gets the size for character text.  The width is
216:    relative to the user coordinates of the window.

218:    Not Collective

220:    Input Parameters:
221: +  draw - the drawing context
222: .  width - the width in user coordinates
223: -  height - the character height

225:    Level: advanced

227: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(),
228:           PetscDrawStringSetSize()

230: @*/
231: PetscErrorCode  PetscDrawStringGetSize(PetscDraw draw,PetscReal *width,PetscReal *height)
232: {

237:   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);
238:   (*draw->ops->stringgetsize)(draw,width,height);
239:   return(0);
240: }