Date: 2022-08-02T03:13:17+00:00

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: {
     29:   (*draw->ops->string)(draw,xl,yl,cl,text);
     30:   return 0;
     31: }

     33: /*@C
     34:    PetscDrawStringVertical - PetscDraws text onto a drawable.

     36:    Not Collective

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

     44:    Level: beginner

     46: .seealso: PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
     47:           PetscDrawStringGetSize()

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

     59:   if (draw->ops->stringvertical) {
     60:     (*draw->ops->stringvertical)(draw,xl,yl,cl,text);
     61:     return 0;
     62:   }
     63:   PetscDrawStringGetSize(draw,&tw,&th);
     64:   for (i = 0; (chr[0] = text[i]); i++) {
     65:     PetscDrawString(draw,xl,yl-th*(i+1),cl,chr);
     66:   }
     67:   return 0;
     68: }

     70: /*@C
     71:    PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point

     73:    Not Collective

     75:    Input Parameters:
     76: +  draw - the drawing context
     77: .  xc - the coordinates of right-left center of text
     78: .  yl - the coordinates of lower edge of text
     79: .  cl - the color of the text
     80: -  text - the text to draw

     82:    Level: beginner

     84: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed(), PetscDrawStringSetSize(),
     85:           PetscDrawStringGetSize()

     87: @*/
     88: PetscErrorCode  PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[])
     89: {
     90:   size_t         len;
     91:   PetscReal      tw,th;

     96:   PetscDrawStringGetSize(draw,&tw,&th);
     97:   PetscStrlen(text,&len);
     98:   xc   = xc - len*tw/2;
     99:   PetscDrawString(draw,xc,yl,cl,text);
    100:   return 0;
    101: }

    103: /*@C
    104:    PetscDrawStringBoxed - Draws a string with a box around it

    106:    Not Collective

    108:    Input Parameters:
    109: +  draw - the drawing context
    110: .  sxl - the coordinates of center of the box
    111: .  syl - the coordinates of top line of box
    112: .  sc - the color of the text
    113: .  bc - the color of the bounding box
    114: -  text - the text to draw

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

    119:    Level: beginner

    121: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringSetSize(),
    122:           PetscDrawStringGetSize()

    124: @*/
    125: PetscErrorCode  PetscDrawStringBoxed(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
    126: {
    127:   PetscReal      top,left,right,bottom,tw,th;
    128:   size_t         len,mlen = 0;
    129:   char           **array;
    130:   int            cnt,i;

    135:   if (draw->ops->boxedstring) {
    136:     (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);
    137:     return 0;
    138:   }

    140:   PetscStrToArray(text,'\n',&cnt,&array);
    141:   for (i=0; i<cnt; i++) {
    142:     PetscStrlen(array[i],&len);
    143:     mlen = PetscMax(mlen,len);
    144:   }

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

    148:   top    = syl;
    149:   left   = sxl - .5*(mlen + 2)*tw;
    150:   right  = sxl + .5*(mlen + 2)*tw;
    151:   bottom = syl - (1.0 + cnt)*th;
    152:   if (w) *w = right - left;
    153:   if (h) *h = top - bottom;

    155:   /* compute new bounding box */
    156:   draw->boundbox_xl = PetscMin(draw->boundbox_xl,left);
    157:   draw->boundbox_xr = PetscMax(draw->boundbox_xr,right);
    158:   draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom);
    159:   draw->boundbox_yr = PetscMax(draw->boundbox_yr,top);

    161:   /* top, left, bottom, right lines */
    162:   PetscDrawLine(draw,left,top,right,top,bc);
    163:   PetscDrawLine(draw,left,bottom,left,top,bc);
    164:   PetscDrawLine(draw,right,bottom,right,top,bc);
    165:   PetscDrawLine(draw,left,bottom,right,bottom,bc);

    167:   for  (i=0; i<cnt; i++) {
    168:     PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);
    169:   }
    170:   PetscStrToArrayDestroy(cnt,array);
    171:   return 0;
    172: }

    174: /*@
    175:    PetscDrawStringSetSize - Sets the size for character text.

    177:    Not Collective

    179:    Input Parameters:
    180: +  draw - the drawing context
    181: .  width - the width in user coordinates
    182: -  height - the character height in user coordinates

    184:    Level: advanced

    186:    Note:
    187:    Only a limited range of sizes are available.

    189: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(),
    190:           PetscDrawStringGetSize()

    192: @*/
    193: PetscErrorCode  PetscDrawStringSetSize(PetscDraw draw,PetscReal width,PetscReal height)
    194: {
    196:   if (draw->ops->stringsetsize) {
    197:     (*draw->ops->stringsetsize)(draw,width,height);
    198:   }
    199:   return 0;
    200: }

    202: /*@
    203:    PetscDrawStringGetSize - Gets the size for character text.  The width is
    204:    relative to the user coordinates of the window.

    206:    Not Collective

    208:    Input Parameters:
    209: +  draw - the drawing context
    210: .  width - the width in user coordinates
    211: -  height - the character height

    213:    Level: advanced

    215: .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(),
    216:           PetscDrawStringSetSize()

    218: @*/
    219: PetscErrorCode  PetscDrawStringGetSize(PetscDraw draw,PetscReal *width,PetscReal *height)
    220: {
    223:   (*draw->ops->stringgetsize)(draw,width,height);
    224:   return 0;
    225: }