drawTextEntity.c

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------*/
00002 /* file: drawTextEntity.c                                                 */
00003 /* Copyright INRIA 2006                                                   */
00004 /* Authors : Jean-Baptiste Silvy                                          */
00005 /* desc : Contains functions needed to draw a text handle                 */
00006 /*------------------------------------------------------------------------*/
00007 
00008 #include "drawTextEntity.h"
00009 #include "Vect2Matrix.h"
00010 #include "GetProperty.h"
00011 #include "periScreen.h" /* should be removed */
00012 #include "DrawObjects.h" /* should be removed */
00013 #include "Xcall1.h"
00014 #include "Axes.h"
00015 #include "StringBox.h"
00016 #include "math_graphics.h"
00017 #include "sciprint.h"
00018 #include "clipping.h"
00019 
00020 /*----------------------------------------------------------------------------------------*/
00021 int drawTextEntity( sciPointObj * pObj )
00022 {
00023   int    v  = 0  ;
00024   double dv = 0. ;
00025   double anglestr = 0. ;
00026   int textProperties[6] ;
00027   int verb=0;
00028   int font_[2], cur_font_[2] ;
00029   int position[2] ;
00030   int textDim[2] ;
00031   double userSize[2] ;
00032   int    textSize[2] ;
00033   double textPos[3] ;
00034   Vect2iMatrix * bboxes ;
00035   int globalBbox[4][2] ;
00036   sciText * ppText =  pTEXT_FEATURE( pObj ) ;
00037   sciPointObj * parentSW = sciGetParentSubwin(pObj) ;
00038 
00039   if (!sciGetVisibility(pObj))
00040   {
00041     return 0 ;
00042   }
00043 
00044 
00045 
00046 #ifdef _MSC_VER
00047   flag_DO = MaybeSetWinhdc ();
00048 #endif
00049   if ( ppText->isclip )
00050   {
00051     sciClip(pObj);
00052   }
00053 
00054   /* transform the position in pixels */
00055   /* we don't take the axes reverse into account. This has obviously no meaning for text.*/
00056   textPos[0] = ppText->x ;
00057   textPos[1] = ppText->y ;
00058   textPos[2] = ppText->z ;
00059 
00060   if ( sciGetIs3d( pObj ) )
00061   {
00062     /* normal case */
00063     getPixelCoordinates( parentSW, textPos, position ) ;
00064   }
00065   else
00066   {
00067     /* for labels, the text is displayed using 2d scale. */
00068     position[0] = XDouble2Pixel( textPos[0] ) ;
00069     position[1] = YDouble2Pixel( textPos[1] ) ;
00070   }
00071 
00072 
00073   sciGetUserSize( pObj, &(userSize[0]), &(userSize[1]) ) ;
00074 
00075 
00076   /* We take the size in 2d. */
00077   textSize[0] = PixelWidth2d(  parentSW, ppText->x, userSize[0] ) ;
00078   textSize[1] = PixelHeight2d( parentSW, ppText->y, userSize[1] ) ;
00079 
00080   /* *10 parce que l'angle est conserve en 1/10eme de degre*/
00081   /* si c'est pas nimp tout ca.... */
00082   anglestr = (sciGetFontOrientation (pObj)/10);         
00083 
00084   /* set the font */
00085   C2F(dr)("xget","font",&verb,cur_font_,&v,&v,&v,&v,&dv,&dv,&dv,&dv,5L,5L);
00086 
00087   font_[0] = sciGetFontStyle (pObj);
00088   font_[1] = sciGetFontDeciWidth (pObj)/100;
00089 
00090   C2F(dr)("xset","font",&font_[0],&font_[1],PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
00091 
00092   /* get the bounding box of the text matrix */
00093   /* the matrix with the bounding box of each strings in the text object */
00094   sciGetTextSize( pObj, &(textDim[0]), &(textDim[1]) ) ;
00095   bboxes = newMatrix( textDim[0] + 1, textDim[1] + 1 ) ;
00096 
00097 
00098   /* we get the array not turned because the display will turn everything by itself */
00099   /* However, the bounding box needs to be turned after */
00100   getStringsPositions( sciGetText( pObj )     ,
00101     &font_[0]              ,
00102     &font_[1]              ,
00103     position               ,
00104     sciGetAutoSize( pObj ) ,
00105     textSize               ,
00106     sciGetCenterPos( pObj ),
00107     0.0                    ,
00108     bboxes                 ,
00109     globalBbox              ) ;
00110 
00111   /* font might have changed */
00112   C2F(dr)("xset","font",&font_[0],&font_[1],PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
00113 
00114   /* wether or not we draw and/or fill the box */
00115   /* no need to compute anything if both line_mode */
00116   /* and fill mode are false */
00117   /* Of course, the background should be drawn first */
00118   if(    sciGetIsBoxed (pObj)
00119     && ( sciGetIsFilled( pObj ) || sciGetIsLine( pObj ) ) )
00120   {
00121     int xm[4], ym[4],n=4;
00122     int close=1;
00123 
00124     /* we need to rotate the bounding box */
00125     rotateBoundingBox( globalBbox, position,  DEG2RAD(anglestr) ) ;
00126 
00127     xm[0] = globalBbox[0][0] ;
00128     xm[1] = globalBbox[1][0] ;
00129     xm[2] = globalBbox[2][0] ;
00130     xm[3] = globalBbox[3][0] ;
00131 
00132     ym[0] = globalBbox[0][1] ;
00133     ym[1] = globalBbox[1][1] ;
00134     ym[2] = globalBbox[2][1] ;
00135     ym[3] = globalBbox[3][1] ;
00136 
00137 
00138 
00139     /* draw the background */
00140     if ( sciGetIsFilled( pObj ) )
00141     {
00142       textProperties[0] = sciGetBackground( pObj ) ;
00143       C2F (dr) ("xset", "dashes", textProperties, textProperties, textProperties+3, textProperties+3, textProperties+3, &v, &dv,&dv, &dv, &dv, 5L, 6L);
00144       C2F (dr) ("xset", "foreground", textProperties, textProperties, textProperties+3, textProperties+3, textProperties+3, &v,&dv, &dv, &dv, &dv, 5L, 10L);
00145 
00146       C2F (dr) ("xarea", "xv", &n, xm, ym, &close, PI0, PI0, PD0, PD0, PD0, PD0, 5L,strlen("xv"));
00147     }
00148 
00149     /* draw the line around the box */
00150     if ( sciGetIsLine( pObj ) )
00151     {
00152       textProperties[0] = sciGetForeground( pObj ) ;
00153       /* draw a rectangle around the text */
00154       C2F (dr) ("xset", "dashes", textProperties, textProperties, textProperties+3, textProperties+3, textProperties+3, &v, &dv,&dv, &dv, &dv, 5L, 6L);
00155       C2F (dr) ("xset", "foreground", textProperties, textProperties, textProperties+3, textProperties+3, textProperties+3, &v,&dv, &dv, &dv, &dv, 5L, 10L);
00156 
00157       C2F (dr) ("xlines", "xv", &n, xm, ym, &close, PI0, PI0, PD0, PD0, PD0, PD0,6L,2L);
00158     }
00159   }
00160 
00161 #ifdef _MSC_VER
00162   flag_DO = MaybeSetWinhdc ();
00163 #endif
00164   /* load the object foreground and dashes color */
00165   textProperties[0] = sciGetFontForeground (pObj);/*la dash est de la meme couleur que le foreground*/
00166   textProperties[2] = sciGetFontDeciWidth (pObj)/100;
00167   textProperties[3] = 0 ;
00168   textProperties[4] = sciGetFontStyle(pObj);
00169   textProperties[5] = 0 ;
00170 
00171   C2F (dr) ("xset", "dashes", textProperties, textProperties, textProperties+3, textProperties+3, textProperties+3, &v, &dv,&dv, &dv, &dv, 5L, 6L);
00172   C2F (dr) ("xset", "foreground", textProperties, textProperties, textProperties+3, textProperties+3, textProperties+3, &v,&dv, &dv, &dv, &dv, 5L, 10L);
00173   /* C2F(dr)("xset","font",x+4,x+2,&v, &v, &v, &v,&dv, &dv, &dv, &dv, 5L, 4L); */
00174 
00175 #ifdef _MSC_VER
00176   if ( flag_DO == 1) ReleaseWinHdc ();
00177 #endif
00178 
00179   drawStringsInPosition( sciGetText( pObj ), bboxes,position, DEG2RAD(anglestr), sciGetAlignment( pObj ) ) ;
00180   /* C2F(dr)("xstring",getStrMatElement(sciGetText(pObj),0,0),&x1,&yy1,PI0,&flagx,PI0,PI0,&anglestr, PD0,PD0,PD0,0L,0L); */
00181 
00182 
00183   C2F(dr)("xset","font",&cur_font_[0],&cur_font_[1],PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
00184 
00185   deleteMatrix( bboxes ) ;
00186 
00187   if ( ppText->isclip )
00188   {
00189     sciUnClip(pObj);
00190   }
00191 
00192 #ifdef _MSC_VER
00193   if ( flag_DO == 1) ReleaseWinHdc ();
00194 #endif
00195 
00196   return 0 ;
00197 
00198 }
00199 /*----------------------------------------------------------------------------------------*/
00200 void drawStringsInPosition( StringMatrix     * strings    ,
00201                            Vect2iMatrix     * cellsArray ,
00202                            int                center[2]  ,
00203                            double             angle      ,
00204                            sciTextAlignment   align       )
00205 {
00206   int nbRow = getMatNbRow( strings ) ;
00207   int nbCol = getMatNbCol( strings ) ;
00208   int i ;
00209   int j ;
00210 
00211   for ( i = 0 ; i < nbRow ; i++ )
00212   {
00213     for ( j = 0 ; j < nbCol ; j++ )
00214     {
00215       /* get the two extreme corners of the cell in which the text will be drawn */
00216       int  * blCorner = getVect2iMatElement( cellsArray, i    , j     ) ;
00217       int  * urCorner = getVect2iMatElement( cellsArray, i + 1, j + 1 ) ;
00218       char * string   = getStrMatElement( strings, i, j ) ;
00219       drawStringInCell( string, blCorner, urCorner, center, angle, align ) ;
00220     }
00221   }
00222 }
00223 /*----------------------------------------------------------------------------------------*/
00224 void drawStringInCell( char             * string      ,
00225                       int                blCorner[2] ,
00226                       int                urCorner[2] ,
00227                       int                rotCenter[2],
00228                       double             angle       ,
00229                       sciTextAlignment   align        )
00230 {
00231   int rect[4] ;
00232   int leftMargin ;
00233   int bottomMargin ;
00234   int pos[2] ;
00235   int flag = 0 ;
00236   double angleStr = RAD2DEG( angle ) ;
00237   /* get the size of the string */
00238   getStringBbox( string, blCorner, rect ) ;
00239 
00240   getStringMargins( &(rect[2]), blCorner, urCorner, align, &leftMargin, &bottomMargin ) ;
00241 
00242   /* get the position unturned */
00243   pos[0] = blCorner[0] + leftMargin   ;
00244   pos[1] = blCorner[1] + bottomMargin + rect[3] ;
00245 
00246   /* then turn it around the center */
00247   if ( Abs( angle ) > EPSILON )
00248   {
00249     iRotate2D( pos, rotCenter, angle, pos ) ;
00250   }
00251   /* then display it */
00252   C2F(dr)("xstring",string,&(pos[0]),&(pos[1]),PI0,&flag,PI0,PI0,&angleStr, PD0,PD0,PD0,0L,0L);
00253 }                       
00254 /*----------------------------------------------------------------------------------------*/
00255 void getStringMargins( int                stringSize[2],
00256                       int                blCorner[2]  ,
00257                       int                urCorner[2]  ,
00258                       sciTextAlignment   align        ,
00259                       int              * lMargin      ,
00260                       int              * bMargin       )
00261 {
00262   int cellWidth  = urCorner[0] - blCorner[0] ;
00263   int cellHeight = urCorner[1] - blCorner[1] ;
00264 
00265   switch( align )
00266   {
00267   case ALIGN_LEFT:
00268     *lMargin = 0 ;
00269     break ;
00270   case ALIGN_RIGHT:
00271     *lMargin = cellWidth - stringSize[0] ;
00272     break ;
00273   case ALIGN_CENTER:
00274     /* same margin for each side */
00275     *lMargin = ( cellWidth - stringSize[0] ) / 2 ;
00276     break ;
00277   default:
00278     sciprint("error unhandled alignment\n");
00279     *lMargin = -1 ;
00280     *bMargin = -1 ;
00281     break ;
00282   }
00283   *bMargin = ( cellHeight - stringSize[1] ) / 2 ;
00284 
00285 }
00286 /*----------------------------------------------------------------------------------------*/

Generated on Sun Mar 4 15:03:52 2007 for Scilab [trunk] by  doxygen 1.5.1