Actual source code: dtext.c


  2: #include <petsc/private/drawimpl.h>

  4: /*@C
  5:    PetscDrawString - draws text onto a drawable.

  7:    Not Collective

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

 16:    Level: beginner

 18: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`, `PetscDrawStringSetSize()`,
 19:           `PetscDrawStringGetSize()`, `PetscDrawLine()`, `PetscDrawRectangle()`, `PetscDrawTriangle()`, `PetscDrawEllipse()`,
 20:           `PetscDrawMarker()`, `PetscDrawPoint()`
 21: @*/
 22: PetscErrorCode PetscDrawString(PetscDraw draw, PetscReal xl, PetscReal yl, int cl, const char text[])
 23: {
 24:   PetscFunctionBegin;
 27:   PetscUseTypeMethod(draw, string, xl, yl, cl, text);
 28:   PetscFunctionReturn(PETSC_SUCCESS);
 29: }

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

 34:    Not Collective

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

 43:    Level: beginner

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

 54:   PetscFunctionBegin;

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

 66: /*@C
 67:    PetscDrawStringCentered - draws text onto a drawable centered at a point

 69:    Not Collective

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

 78:    Level: beginner

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

 88:   PetscFunctionBegin;

 92:   PetscCall(PetscDrawStringGetSize(draw, &tw, &th));
 93:   PetscCall(PetscStrlen(text, &len));
 94:   xc = xc - len * tw / 2;
 95:   PetscCall(PetscDrawString(draw, xc, yl, cl, text));
 96:   PetscFunctionReturn(PETSC_SUCCESS);
 97: }

 99: /*@C
100:    PetscDrawStringBoxed - Draws a string with a box around it

102:    Not Collective

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

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

115:    Level: beginner

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

127:   PetscFunctionBegin;

131:   if (draw->ops->boxedstring) {
132:     PetscUseTypeMethod(draw, boxedstring, sxl, syl, sc, bc, text, w, h);
133:     PetscFunctionReturn(PETSC_SUCCESS);
134:   }

136:   PetscCall(PetscStrToArray(text, '\n', &cnt, &array));
137:   for (i = 0; i < cnt; i++) {
138:     PetscCall(PetscStrlen(array[i], &len));
139:     mlen = PetscMax(mlen, len);
140:   }

142:   PetscCall(PetscDrawStringGetSize(draw, &tw, &th));

144:   top    = syl;
145:   left   = sxl - .5 * (mlen + 2) * tw;
146:   right  = sxl + .5 * (mlen + 2) * tw;
147:   bottom = syl - (1.0 + cnt) * th;
148:   if (w) *w = right - left;
149:   if (h) *h = top - bottom;

151:   /* compute new bounding box */
152:   draw->boundbox_xl = PetscMin(draw->boundbox_xl, left);
153:   draw->boundbox_xr = PetscMax(draw->boundbox_xr, right);
154:   draw->boundbox_yl = PetscMin(draw->boundbox_yl, bottom);
155:   draw->boundbox_yr = PetscMax(draw->boundbox_yr, top);

157:   /* top, left, bottom, right lines */
158:   PetscCall(PetscDrawLine(draw, left, top, right, top, bc));
159:   PetscCall(PetscDrawLine(draw, left, bottom, left, top, bc));
160:   PetscCall(PetscDrawLine(draw, right, bottom, right, top, bc));
161:   PetscCall(PetscDrawLine(draw, left, bottom, right, bottom, bc));

163:   for (i = 0; i < cnt; i++) PetscCall(PetscDrawString(draw, left + tw, top - (1.5 + i) * th, sc, array[i]));
164:   PetscCall(PetscStrToArrayDestroy(cnt, array));
165:   PetscFunctionReturn(PETSC_SUCCESS);
166: }

168: /*@
169:    PetscDrawStringSetSize - Sets the size for character text.

171:    Not Collective

173:    Input Parameters:
174: +  draw - the drawing context
175: .  width - the width in user coordinates
176: -  height - the character height in user coordinates

178:    Level: advanced

180:    Note:
181:    Only a limited range of sizes are available.

183: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`,
184:           `PetscDrawStringGetSize()`
185: @*/
186: PetscErrorCode PetscDrawStringSetSize(PetscDraw draw, PetscReal width, PetscReal height)
187: {
188:   PetscFunctionBegin;
190:   PetscTryTypeMethod(draw, stringsetsize, width, height);
191:   PetscFunctionReturn(PETSC_SUCCESS);
192: }

194: /*@
195:    PetscDrawStringGetSize - Gets the size for character text.  The width is
196:    relative to the user coordinates of the window.

198:    Not Collective

200:    Input Parameters:
201: +  draw - the drawing context
202: .  width - the width in user coordinates
203: -  height - the character height

205:    Level: advanced

207: .seealso: `PetscDraw`, `PetscDrawStringVertical()`, `PetscDrawString()`, `PetscDrawStringCentered()`, `PetscDrawStringBoxed()`,
208:           `PetscDrawStringSetSize()`
209: @*/
210: PetscErrorCode PetscDrawStringGetSize(PetscDraw draw, PetscReal *width, PetscReal *height)
211: {
212:   PetscFunctionBegin;
214:   PetscUseTypeMethod(draw, stringgetsize, width, height);
215:   PetscFunctionReturn(PETSC_SUCCESS);
216: }