Actual source code: dtext.c

petsc-3.14.6 2021-03-30
Report Typos and Errors
  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


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

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

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

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

 40:    Not Collective

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

 48:    Level: beginner

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

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


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

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

 79:    Not Collective

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

 88:    Level: beginner


 91: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
 92:           PetscDrawStringGetSize()

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


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

112: /*@C
113:    PetscDrawStringBoxed - Draws a string with a box around it

115:    Not Collective

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

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

128:    Level: beginner


131: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
132:           PetscDrawStringGetSize()

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


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

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

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

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

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

173:   /* top, left, bottom, right lines */
174:   PetscDrawLine(draw,left,top,right,top,bc);
175:   PetscDrawLine(draw,left,bottom,left,top,bc);
176:   PetscDrawLine(draw,right,bottom,right,top,bc);
177:   PetscDrawLine(draw,left,bottom,right,bottom,bc);

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

186: /*@
187:    PetscDrawStringSetSize - Sets the size for character text.

189:    Not Collective

191:    Input Parameters:
192: +  draw - the drawing context
193: .  width - the width in user coordinates
194: -  height - the character height in user coordinates

196:    Level: advanced

198:    Note:
199:    Only a limited range of sizes are available.

201: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(),
202:           PetscDrawStringGetSize()

204: @*/
205: PetscErrorCode  PetscDrawStringSetSize(PetscDraw draw,PetscReal width,PetscReal height)
206: {

211:   if (draw->ops->stringsetsize) {
212:     (*draw->ops->stringsetsize)(draw,width,height);
213:   }
214:   return(0);
215: }

217: /*@
218:    PetscDrawStringGetSize - Gets the size for character text.  The width is
219:    relative to the user coordinates of the window.

221:    Not Collective

223:    Input Parameters:
224: +  draw - the drawing context
225: .  width - the width in user coordinates
226: -  height - the character height

228:    Level: advanced

230: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(),
231:           PetscDrawStringSetSize()

233: @*/
234: PetscErrorCode  PetscDrawStringGetSize(PetscDraw draw,PetscReal *width,PetscReal *height)
235: {

240:   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);
241:   (*draw->ops->stringgetsize)(draw,width,height);
242:   return(0);
243: }