Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

xyvtext.cpp

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /* Módulo: xyvtext.cpp                                                       */
00003 /* Autores: Carlos Henrique Levy e Jaudênia Cavalcante                       */
00004 /* Data: 20 jun 97                                                           */
00005 /* Comentário:                                                               */
00006 /*    Implementação de métodos da classe que desenha textos vetoriais.       */
00007 /*****************************************************************************/
00008 
00009 #include "xyvtext.h"
00010 #include "cdimage.h"
00011 #include "xymath.h"
00012 
00013 const char* xy_id_xyvtext_cpp="$Id: xyvtext.cpp,v 1.9 2000/01/17 13:46:58 camilo Exp $";                                                                
00014 
00015 XYVectorText::XYVectorText (const char* text,   
00016                             XYCoordinate x,     
00017                             XYCoordinate y,     
00018                             long color,                 
00019                             double width,
00020                             double height,
00021                             Typeface font,              
00022                             Style style,                
00023                             Alignment alignment,        
00024                             Orientation orientation,
00025                             xybool underline,
00026                             xybool visible)     
00027                                   : XYText(text, x, y, color, font, style, alignment,
00028                                            orientation, underline, visible),
00029                                     _height(height),
00030                                     _width(width)
00031 {
00032  _tmatrix = NULL;
00033 }
00034 
00035 XYVectorText::XYVectorText (const char* text,   
00036                             XYCoordinate x,
00037                             XYCoordinate y,
00038                             xybool visible)     
00039                             : XYText(text, x, y, visible),
00040                               _height(0.05),
00041                               _width(0.05)
00042 {
00043  _tmatrix = NULL;
00044 }
00045 
00046 XYVectorText::XYVectorText (const char* text,
00047                                   long color,
00048                             double width,
00049                             double height,
00050                                     Typeface font,                      
00051                                     Style style,                        
00052                                     Alignment alignment,        
00053                                     Orientation orientation,
00054                                   xybool underline,
00055                                   xybool visible)
00056                                   : XYText(text, color, font, style, alignment,
00057                                            orientation, underline, visible),
00058                                     _height(height),
00059                                     _width(width)
00060 {
00061  _tmatrix = NULL;
00062 }
00063 
00064 XYVectorText::XYVectorText (long color,
00065                             double width,
00066                             double height,
00067                                     Typeface font,                      
00068                                     Style style,                        
00069                                     Alignment alignment,        
00070                                     Orientation orientation,
00071                                   xybool underline,
00072                                   xybool visible)
00073                                   : XYText(color, font, style, alignment,
00074                                      orientation, underline, visible),
00075                                     _height(height),
00076                                     _width(width)
00077 {
00078  _tmatrix = NULL;
00079 }
00080 
00081 XYVectorText::~XYVectorText (void)
00082 {
00083 }
00084 
00085 extern "C"
00086 {
00087    extern cdCanvas* cdActiveCanvas(void);
00088 }
00089 
00090 void XYVectorText::height (double h)
00091 {
00092    _height = h;
00093 }
00094 
00095 double XYVectorText::height (void) const
00096 {
00097    return _height;
00098 }
00099 
00100 void XYVectorText::width (double w)
00101 {
00102    _width = w;
00103 }
00104 
00105 double XYVectorText::width (void) const
00106 {
00107    return _width;
00108 }
00109 
00110 void XYVectorText::matrix (double *m)
00111 {
00112    _tmatrix = m;
00113 }
00114 
00115 void XYVectorText::accmatrix (double *m)
00116 {
00117    double tmp[6];
00118    memcpy( tmp, _tmatrix, sizeof(double)*6 );
00119    _tmatrix[0] = m[0]*tmp[3] + m[1]*tmp[0];
00120    _tmatrix[1] = m[0]*tmp[4] + m[1]*tmp[1];
00121    _tmatrix[2] = m[0]*tmp[5] + m[1]*tmp[2] + m[2];
00122    _tmatrix[3] = m[3]*tmp[3] + m[4]*tmp[0];
00123    _tmatrix[4] = m[3]*tmp[4] + m[4]*tmp[1];
00124    _tmatrix[5] = m[3]*tmp[5] + m[4]*tmp[2] + m[5];
00125 }
00126 
00127 double *XYVectorText::matrix (void) const
00128 {
00129    return _tmatrix;
00130 }
00131 
00132 void XYVectorText::draw (void)
00133 {
00134    draw (_xmin, _ymin, _xmax, _ymax);
00135 }
00136 
00137 static void setAlignment (int a)
00138 {
00139    // consulta alinhamento dado pelo usuário e o converte para o alinhamento
00140    // de texto vetorial que resultará no alinhamento esperado
00141    switch (a)
00142    {
00143       case XYText::north:      cdTextAlignment(XYText::east);       break;
00144       case XYText::south:      cdTextAlignment(XYText::west);       break;
00145       case XYText::east:       cdTextAlignment(XYText::south);      break;
00146       case XYText::west:       cdTextAlignment(XYText::north);      break;
00147       case XYText::northEast:  cdTextAlignment(XYText::southEast);  break;
00148       case XYText::northWest:  cdTextAlignment(XYText::northEast);  break;
00149       case XYText::southEast:  cdTextAlignment(XYText::southWest);  break;
00150       case XYText::southWest:  cdTextAlignment(XYText::northWest);  break;
00151       case XYText::center:     cdTextAlignment(XYText::center);     break;
00152       case XYText::baseLeft:   cdTextAlignment(XYText::baseLeft);   break;
00153       case XYText::baseCenter: cdTextAlignment(XYText::baseCenter); break;
00154       case XYText::baseRight:  cdTextAlignment(XYText::baseRight);  break;
00155    }
00156 }
00157 
00158 static void xyVectorStyle (int style)
00159 {
00160    // matriz de cisalhamento para a direita de 30 graus
00161 //   double matrix[] = {0., 1., 0., 1., tan(XY_PI/6), 0.};
00162 
00163    switch (style)
00164    {
00165       case XYText::plain:
00166          cdLineWidth(1);                                  // espessura para texto sem negrito
00167                //wdVectorTextTransform(NULL);     // não usa transformação
00168 
00169          break;
00170 
00171       case XYText::bold:
00172          cdLineWidth(3);                                  // espessura para texto com negrito
00173                //wdVectorTextTransform(NULL);     // não usa transformação
00174 
00175          break;
00176 
00177       case XYText::italic:
00178          cdLineWidth(1);                                  // espessura para texto sem negrito
00179                //wdVectorTextTransform(matrix); // usa transformação de cisalhamento
00180 
00181          break;
00182 
00183       case XYText::boldItalic:
00184          cdLineWidth(3);                                  // espessura para texto com negrito
00185                //wdVectorTextTransform(matrix); // usa transformação de cisalhamento
00186 
00187          break;
00188    }
00189 }
00190 
00191 void XYVectorText::draw (int xmin, int ymin, int xmax, int ymax) const
00192 {
00193    // teste de inconsistência
00194    if (text() == NULL) return;
00195 
00196    if (visible() == xyfalse) return;  // invisível!!!
00197 
00198    int mode = cdClip(CD_CLIPON);
00199 
00200    // guarda a região de clip corrente
00201    int oldclip_xmin, oldclip_xmax, oldclip_ymin, oldclip_ymax;
00202    cdGetClipArea (&oldclip_xmin, &oldclip_xmax, &oldclip_ymin, &oldclip_ymax);
00203    
00204    // define nova região de clip
00205    cdClipArea(xmin, xmax, ymin, ymax);
00206 
00207    // atribuição de fonte e estilo para o texto
00208    wdVectorFont(NULL);
00209    xyVectorStyle(style());
00210 
00211    // atribuição de alinhamento e cor a serem usados
00212    cdTextAlignment(align());
00213    cdForeground(color());
00214 
00215    // largura e altura do canvas em milímetros
00216    double wmm, hmm;
00217    int wpx,hpx;
00218    cdGetCanvasSize(&wpx, &hpx, &wmm, &hmm);
00219 
00220    if (orientation() == horizontal)  // orientação default
00221    {
00222       wdVectorTextSize(width(), height(), "_");
00223           wdVectorTextDirection(0., 0., 1., 0.);
00224    }
00225    else                            // com orientação
00226    {
00227       wdVectorTextSize(width(),height(), "_");
00228 
00229       // rotaciona texto com orientação: vertical de baixo para cima (90 graus)
00230       if (orientation() == vertBotTop)          
00231           {
00232              wdVectorTextDirection(0., 0., 0., 1.);
00233          setAlignment(align());
00234       }
00235       else      // orientação: vertical de cima para baixo (-90 graus)
00236           {
00237              wdVectorTextDirection(0., 1., 0., 0.);
00238          setAlignment(align());
00239       }
00240    }
00241 
00242    double xd, yd;
00243    position (&xd, &yd);
00244 
00245    double *mt=NULL, x0, y0;
00246    if ( _tmatrix!=NULL )
00247     {
00248      mt = new double[6];
00249      memcpy( mt, _tmatrix, sizeof(double)*6 );
00250      wdCanvas2World(0, 0, &x0, &y0);
00251      wdCanvas2World((int)_tmatrix[5], (int)_tmatrix[2], &(mt[5]), &(mt[2]));
00252      mt[5] -= x0;
00253      mt[2] -= y0;
00254     }
00255    wdVectorTextTransform(mt);
00256 
00257    // desenha um texto vetorial
00258    wdVectorText(xd, yd, text());
00259 
00260    // acrescenta o sublinhado, se houver
00261    if (underline())
00262    {
00263       // define estilo e largura da linha a ser usada para o sublinhado
00264       cdLineStyle(CD_SOLID);
00265       wdLineWidth((style() == bold || style() == boldItalic) ? 1. : .7);
00266 
00267             // obtém boundingBox do texto vetorial para determinar posição de
00268             // desenho para o sublinhado
00269       int bxmin, bxmax, bymin, bymax;
00270       boundingBox(bxmin, bymin, bxmax, bymax);
00271 
00272       int descent;
00273       cdFontDim(0, 0, 0, &descent);
00274         
00275       if (orientation() == horizontal)
00276             {
00277                cdLine(bxmin, bymin, bxmin, bymax);
00278                      cdLine(bxmin, bymin, bxmax, bymin);
00279                      cdLine(bxmax, bymin, bxmax, bymax);
00280                      cdLine(bxmin, bymax, bxmax, bymax);
00281             }
00282       // cdLine(bxmin, bymin - descent, bxmax, bymin - descent);
00283       else if (orientation() == vertBotTop)
00284             {
00285                cdLine(bxmin, bymin, bxmin, bymax);
00286                      cdLine(bxmin, bymin, bxmax, bymin);
00287                      cdLine(bxmax, bymin, bxmax, bymax);
00288                      cdLine(bxmin, bymax, bxmax, bymax);
00289             }
00290       //   cdLine(bxmax + descent, bymin, bxmax + descent, bymax);
00291       else
00292             {
00293                cdLine(bxmin, bymin, bxmin, bymax);
00294                      cdLine(bxmin, bymin, bxmax, bymin);
00295                      cdLine(bxmax, bymin, bxmax, bymax);
00296                      cdLine(bxmin, bymax, bxmax, bymax);
00297             }
00298       //   cdLine(bxmin - descent, bymin, bxmin - descent, bymax);
00299    }
00300 
00301    cdClip(mode);
00302    // restaura a região de clip anterior
00303    cdClipArea(oldclip_xmin, oldclip_xmax, oldclip_ymin, oldclip_ymax);
00304 
00305    wdVectorTextTransform(NULL);
00306 }
00307 
00308 void XYVectorText::boundingBox (int& xmin, int& ymin, int& xmax, int& ymax)
00309                                 const
00310 {
00311    double xmn, xmx, ymn, ymx;
00312 
00313    // atribuição de fonte e estilo para o texto
00314    wdVectorFont(NULL);
00315    xyVectorStyle(style());
00316 
00317    // atribuição de alinhamento e cor a serem usados
00318    cdTextAlignment(align());
00319    cdForeground(color());
00320 
00321    // largura e altura do canvas em milímetros
00322    double wmm, hmm;
00323    int wpx,hpx;
00324    cdGetCanvasSize(&wpx, &hpx, &wmm, &hmm);
00325 
00326    if (orientation() == horizontal)  // orientação default
00327    {
00328       wdVectorTextSize(width(), height(), "_");
00329           wdVectorTextDirection(0., 0., 1., 0.);
00330    }
00331    else                            // com orientação
00332    {
00333       wdVectorTextSize(width(),height(), "_");
00334 
00335       // rotaciona texto com orientação: vertical de baixo para cima (90 graus)
00336       if (orientation() == vertBotTop)          
00337           {
00338              wdVectorTextDirection(0., 0., 0., 1.);
00339          setAlignment(align());
00340       }
00341       else      // orientação: vertical de cima para baixo (-90 graus)
00342           {
00343              wdVectorTextDirection(0., 1., 0., 0.);
00344          setAlignment(align());
00345       }
00346    }
00347 
00348    double xd, yd;
00349    position (&xd, &yd);
00350 
00351    double *mt=NULL, x0, y0;
00352    if ( _tmatrix!=NULL )
00353     {
00354      mt = new double[6];
00355      memcpy( mt, _tmatrix, sizeof(double)*6 );
00356      wdCanvas2World(0, 0, &x0, &y0);
00357      wdCanvas2World((int)_tmatrix[5], (int)_tmatrix[2], &(mt[5]), &(mt[2]));
00358      mt[5] -= x0;
00359      mt[2] -= y0;
00360     }
00361    wdVectorTextTransform(mt);
00362 
00363    double rect[8];
00364    wdGetVectorTextBounds(text(), xd, yd, rect);
00365 
00366    xmn = xmx = rect[0];
00367    ymn = ymx = rect[1];
00368    for ( int i=0; i<8; i=i+2 )
00369     {
00370      xmn = (xmn > rect[i]) ? rect[i] : xmn;
00371      ymn = (ymn > rect[i+1]) ? rect[i+1] : ymn;
00372      xmx = (xmx < rect[i]) ? rect[i] : xmx;
00373      ymx = (ymx < rect[i+1]) ? rect[i+1] : ymx;
00374     }
00375 
00376   wdWorld2Canvas( xmn, ymn, &xmin, &ymin );
00377   wdWorld2Canvas( xmx, ymx, &xmax, &ymax );
00378 
00379   wdVectorTextTransform(NULL);
00380 }

XY
Tecgraf / PUC-Rio - Computer Graphics Technology Group