1 /* 2 Provides the calling sequences for all the basic PetscDraw routines. 3 */ 4 #include <petsc/private/drawimpl.h> /*I "petscdraw.h" I*/ 5 6 /*@C 7 PetscDrawString - PetscDraws text onto a drawable. 8 9 Not Collective 10 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 16 17 Level: beginner 18 19 20 .seealso: PetscDrawStringVertical(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), PetscDrawStringSetSize(), 21 PetscDrawStringGetSize(), PetscDrawLine(), PetscDrawRectangle(), PetscDrawTriangle(), PetscDrawEllipse(), 22 PetscDrawMarker(), PetscDrawPoint() 23 24 @*/ 25 PetscErrorCode PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[]) 26 { 27 PetscErrorCode ierr; 28 29 PetscFunctionBegin; 30 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 31 PetscValidCharPointer(text,5); 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 ierr = (*draw->ops->string)(draw,xl,yl,cl,text);CHKERRQ(ierr); 34 PetscFunctionReturn(0); 35 } 36 37 /*@C 38 PetscDrawStringVertical - PetscDraws text onto a drawable. 39 40 Not Collective 41 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 47 48 Level: beginner 49 50 .seealso: PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), PetscDrawStringSetSize(), 51 PetscDrawStringGetSize() 52 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; 59 PetscErrorCode ierr; 60 61 PetscFunctionBegin; 62 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 63 PetscValidCharPointer(text,5); 64 65 if (draw->ops->stringvertical) { 66 ierr = (*draw->ops->stringvertical)(draw,xl,yl,cl,text);CHKERRQ(ierr); 67 PetscFunctionReturn(0); 68 } 69 ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr); 70 for (i = 0; (chr[0] = text[i]); i++) { 71 ierr = PetscDrawString(draw,xl,yl-th*(i+1),cl,chr);CHKERRQ(ierr); 72 } 73 PetscFunctionReturn(0); 74 } 75 76 /*@C 77 PetscDrawStringCentered - PetscDraws text onto a drawable centered at a point 78 79 Not Collective 80 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 87 88 Level: beginner 89 90 91 .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), PetscDrawStringSetSize(), 92 PetscDrawStringGetSize() 93 94 @*/ 95 PetscErrorCode PetscDrawStringCentered(PetscDraw draw,PetscReal xc,PetscReal yl,int cl,const char text[]) 96 { 97 PetscErrorCode ierr; 98 size_t len; 99 PetscReal tw,th; 100 101 PetscFunctionBegin; 102 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 103 PetscValidCharPointer(text,5); 104 105 ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr); 106 ierr = PetscStrlen(text,&len);CHKERRQ(ierr); 107 xc = xc - len*tw/2; 108 ierr = PetscDrawString(draw,xc,yl,cl,text);CHKERRQ(ierr); 109 PetscFunctionReturn(0); 110 } 111 112 /*@C 113 PetscDrawStringBoxed - Draws a string with a box around it 114 115 Not Collective 116 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 124 125 Output Parameter: 126 . w,h - width and height of resulting box (optional) 127 128 Level: beginner 129 130 131 .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringSetSize(), 132 PetscDrawStringGetSize() 133 134 @*/ 135 PetscErrorCode PetscDrawStringBoxed(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h) 136 { 137 PetscErrorCode ierr; 138 PetscReal top,left,right,bottom,tw,th; 139 size_t len,mlen = 0; 140 char **array; 141 int cnt,i; 142 143 PetscFunctionBegin; 144 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 145 PetscValidCharPointer(text,5); 146 147 if (draw->ops->boxedstring) { 148 ierr = (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);CHKERRQ(ierr); 149 PetscFunctionReturn(0); 150 } 151 152 ierr = PetscStrToArray(text,'\n',&cnt,&array);CHKERRQ(ierr); 153 for (i=0; i<cnt; i++) { 154 ierr = PetscStrlen(array[i],&len);CHKERRQ(ierr); 155 mlen = PetscMax(mlen,len); 156 } 157 158 ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr); 159 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; 166 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); 172 173 /* top, left, bottom, right lines */ 174 ierr = PetscDrawLine(draw,left,top,right,top,bc);CHKERRQ(ierr); 175 ierr = PetscDrawLine(draw,left,bottom,left,top,bc);CHKERRQ(ierr); 176 ierr = PetscDrawLine(draw,right,bottom,right,top,bc);CHKERRQ(ierr); 177 ierr = PetscDrawLine(draw,left,bottom,right,bottom,bc);CHKERRQ(ierr); 178 179 for (i=0; i<cnt; i++) { 180 ierr = PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);CHKERRQ(ierr); 181 } 182 ierr = PetscStrToArrayDestroy(cnt,array);CHKERRQ(ierr); 183 PetscFunctionReturn(0); 184 } 185 186 /*@ 187 PetscDrawStringSetSize - Sets the size for character text. 188 189 Not Collective 190 191 Input Parameters: 192 + draw - the drawing context 193 . width - the width in user coordinates 194 - height - the character height in user coordinates 195 196 Level: advanced 197 198 Note: 199 Only a limited range of sizes are available. 200 201 .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), 202 PetscDrawStringGetSize() 203 204 @*/ 205 PetscErrorCode PetscDrawStringSetSize(PetscDraw draw,PetscReal width,PetscReal height) 206 { 207 PetscErrorCode ierr; 208 209 PetscFunctionBegin; 210 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 211 if (draw->ops->stringsetsize) { 212 ierr = (*draw->ops->stringsetsize)(draw,width,height);CHKERRQ(ierr); 213 } 214 PetscFunctionReturn(0); 215 } 216 217 /*@ 218 PetscDrawStringGetSize - Gets the size for character text. The width is 219 relative to the user coordinates of the window. 220 221 Not Collective 222 223 Input Parameters: 224 + draw - the drawing context 225 . width - the width in user coordinates 226 - height - the character height 227 228 Level: advanced 229 230 .seealso: PetscDrawStringVertical(), PetscDrawString(), PetscDrawStringCentered(), PetscDrawStringBoxed(), PetscDrawStringBoxed(), 231 PetscDrawStringSetSize() 232 233 @*/ 234 PetscErrorCode PetscDrawStringGetSize(PetscDraw draw,PetscReal *width,PetscReal *height) 235 { 236 PetscErrorCode ierr; 237 238 PetscFunctionBegin; 239 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 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 ierr = (*draw->ops->stringgetsize)(draw,width,height);CHKERRQ(ierr); 242 PetscFunctionReturn(0); 243 } 244 245