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 - draws 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: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`, `PetscDrawStringSetSize()`,
 20:           `PetscDrawStringGetSize()`, `PetscDrawLine()`, `PetscDrawRectangle()`, `PetscDrawTriangle()`, `PetscDrawEllipse()`,
 21:           `PetscDrawMarker()`, `PetscDrawPoint()`
 22: @*/
 23: PetscErrorCode PetscDrawString(PetscDraw draw, PetscReal xl, PetscReal yl, int cl, const char text[])
 24: {
 27:   PetscUseTypeMethod(draw, string, xl, yl, cl, text);
 28:   return 0;
 29: }

 31: /*@C
 32:    PetscDrawStringVertical - draws text onto a drawable.

 34:    Not Collective

 36:    Input Parameters:
 37: +  draw - the drawing context
 38: .  xl,yl - the coordinates of upper left corner of text
 39: .  cl - the color of the text
 40: -  text - the text to draw

 42:    Level: beginner

 44: .seealso: `PetscDraw`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`, `PetscDrawStringSetSize()`,
 45:           `PetscDrawStringGetSize()`
 46: @*/
 47: PetscErrorCode PetscDrawStringVertical(PetscDraw draw, PetscReal xl, PetscReal yl, int cl, const char text[])
 48: {
 49:   int       i;
 50:   char      chr[2] = {0, 0};
 51:   PetscReal tw, th;


 56:   if (draw->ops->stringvertical) PetscUseTypeMethod(draw, stringvertical, xl, yl, cl, text);
 57:   else {
 58:     PetscDrawStringGetSize(draw, &tw, &th);
 59:     for (i = 0; (chr[0] = text[i]); i++) PetscDrawString(draw, xl, yl - th * (i + 1), cl, chr);
 60:   }
 61:   return 0;
 62: }

 64: /*@C
 65:    PetscDrawStringCentered - draws text onto a drawable centered at a point

 67:    Not Collective

 69:    Input Parameters:
 70: +  draw - the drawing context
 71: .  xc - the coordinates of right-left center of text
 72: .  yl - the coordinates of lower edge of text
 73: .  cl - the color of the text
 74: -  text - the text to draw

 76:    Level: beginner

 78: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringBoxed()`, `PetscDrawStringSetSize()`,
 79:           `PetscDrawStringGetSize()`
 80: @*/
 81: PetscErrorCode PetscDrawStringCentered(PetscDraw draw, PetscReal xc, PetscReal yl, int cl, const char text[])
 82: {
 83:   size_t    len;
 84:   PetscReal tw, th;


 89:   PetscDrawStringGetSize(draw, &tw, &th);
 90:   PetscStrlen(text, &len);
 91:   xc = xc - len * tw / 2;
 92:   PetscDrawString(draw, xc, yl, cl, text);
 93:   return 0;
 94: }

 96: /*@C
 97:    PetscDrawStringBoxed - Draws a string with a box around it

 99:    Not Collective

101:    Input Parameters:
102: +  draw - the drawing context
103: .  sxl - the coordinates of center of the box
104: .  syl - the coordinates of top line of box
105: .  sc - the color of the text
106: .  bc - the color of the bounding box
107: -  text - the text to draw

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

112:    Level: beginner

114: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringSetSize()`,
115:           `PetscDrawStringGetSize()`
116: @*/
117: PetscErrorCode PetscDrawStringBoxed(PetscDraw draw, PetscReal sxl, PetscReal syl, int sc, int bc, const char text[], PetscReal *w, PetscReal *h)
118: {
119:   PetscReal top, left, right, bottom, tw, th;
120:   size_t    len, mlen = 0;
121:   char    **array;
122:   int       cnt, i;


127:   if (draw->ops->boxedstring) {
128:     PetscUseTypeMethod(draw, boxedstring, 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++) PetscDrawString(draw, left + tw, top - (1.5 + i) * th, sc, array[i]);
160:   PetscStrToArrayDestroy(cnt, array);
161:   return 0;
162: }

164: /*@
165:    PetscDrawStringSetSize - Sets the size for character text.

167:    Not Collective

169:    Input Parameters:
170: +  draw - the drawing context
171: .  width - the width in user coordinates
172: -  height - the character height in user coordinates

174:    Level: advanced

176:    Note:
177:    Only a limited range of sizes are available.

179: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`,
180:           `PetscDrawStringGetSize()`
181: @*/
182: PetscErrorCode PetscDrawStringSetSize(PetscDraw draw, PetscReal width, PetscReal height)
183: {
185:   PetscTryTypeMethod(draw, stringsetsize, width, height);
186:   return 0;
187: }

189: /*@
190:    PetscDrawStringGetSize - Gets the size for character text.  The width is
191:    relative to the user coordinates of the window.

193:    Not Collective

195:    Input Parameters:
196: +  draw - the drawing context
197: .  width - the width in user coordinates
198: -  height - the character height

200:    Level: advanced

202: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`,
203:           `PetscDrawStringSetSize()`
204: @*/
205: PetscErrorCode PetscDrawStringGetSize(PetscDraw draw, PetscReal *width, PetscReal *height)
206: {
208:   PetscUseTypeMethod(draw, stringgetsize, width, height);
209:   return 0;
210: }