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

xymrtext.cpp

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /* Módulo: xymrtext.cpp                                                      */
00003 /* Autores: Carlos Henrique Levy, Jaudênia Cavalcante e Camilo Freire        */
00004 /* Data: 5 mai 2000                                                          */
00005 /* Comentário:                                                               */
00006 /*    Implementação de métodos da classe que desenha textos raster em        */
00007 /*    multiplas linhas.                                                      */
00008 /*****************************************************************************/
00009 
00010 #include "xymrtext.h"
00011 #include "cdimage.h"
00012 #include "xymath.h"
00013 
00014 const char* xy_id_xymrtext_cpp="$Id: xymrtext.cpp,v 1.3 2003/03/10 19:25:30 clinio Exp $";                                                              
00015 
00016 XYMultiRasterText::XYMultiRasterText (const char* text,         
00017                                       XYCoordinate x,   
00018                                       XYCoordinate y,   
00019                                       long color,               
00020                                       int size,                 
00021                                       Typeface font,            
00022                                       Style style,              
00023                                       Alignment alignment,      
00024                                       Orientation orientation,
00025                                       xybool underline,
00026                                       xybool visible)   
00027                                       : XYRasterText(text, x, y, color, size,
00028                                                      font, style, alignment,
00029                                                      orientation, underline,
00030                                                      visible)
00031 {
00032    splitText();
00033 }
00034 
00035 XYMultiRasterText::XYMultiRasterText (const char* text,         
00036                                       XYCoordinate x,
00037                                       XYCoordinate y,
00038                                       xybool visible)   
00039                                       : XYRasterText(text, x, y, visible)
00040 {
00041    splitText();
00042 }
00043 
00044 XYMultiRasterText::XYMultiRasterText (const char* text,
00045                                       long color,
00046                                       int size,
00047                                       Typeface font,                    
00048                                       Style style,                      
00049                                       Alignment alignment,      
00050                                       Orientation orientation,
00051                                       xybool underline,
00052                                       xybool visible)
00053                                       : XYRasterText(text, color, size, font,
00054                                                      style, alignment,
00055                                                      orientation, underline,
00056                                                      visible)
00057 {
00058    splitText();
00059 }
00060 
00061 XYMultiRasterText::XYMultiRasterText (long color,
00062                                       int size,
00063                                       Typeface font,                    
00064                                       Style style,                      
00065                                       Alignment alignment,      
00066                                       Orientation orientation,
00067                                       xybool underline,
00068                                       xybool visible)
00069                                       : XYRasterText(color, size, font, style,
00070                                                      alignment, orientation,
00071                                                      underline, visible)
00072 {
00073 }
00074 
00075 void XYMultiRasterText::splitText( void )
00076 {
00077  int block = 50;
00078  _mtexts = (char **) malloc ( block*sizeof(char *) );
00079  char *c = strdup(_text);
00080 
00081  _ntexts = 0;
00082 
00083  _mtexts[0]=strtok( c, "\n" );
00084  if ( _mtexts[0]==NULL ) _mtexts[0] = strdup( "" );
00085 
00086  for ( _ntexts=1; (_mtexts[_ntexts]=strtok(NULL,"\n"))!=NULL; _ntexts++ )
00087   {
00088    if ( _ntexts==block )
00089     {
00090      block += 50;
00091      _mtexts = (char **) realloc ( _mtexts, block*sizeof(char *) );
00092     }
00093   }
00094 }
00095 
00096 void XYMultiRasterText::text (const char* s)
00097 {
00098    XYText::text(s);
00099 
00100    splitText();
00101 }
00102 
00103 char* XYMultiRasterText::text (void) const
00104 {
00105    return XYText::text();
00106 }
00107 
00108 void XYMultiRasterText::draw (void)
00109 {
00110    draw (_xmin, _ymin, _xmax, _ymax);
00111 }
00112 
00113 void XYMultiRasterText::draw (int xmin, int ymin, int xmax, int ymax) const
00114 {
00115    // teste de inconsistência
00116    if (_ntexts == 0) return;
00117 
00118    if (visible() == xyfalse) return;  // invisível!!!
00119 
00120    double oldOrientation =  cdTextOrientation(CD_QUERY);
00121 
00122    int mode = cdClip(CD_CLIPON);
00123 
00124    // guarda a região de clip corrente
00125    int oldclip_xmin, oldclip_xmax, oldclip_ymin, oldclip_ymax;
00126    cdGetClipArea (&oldclip_xmin, &oldclip_xmax, &oldclip_ymin, &oldclip_ymax);
00127    // define nova região de clip
00128    cdClipArea(xmin, xmax, ymin, ymax);
00129 
00130    cdFont(font(), style(), size());
00131    cdTextAlignment(align());
00132    cdForeground(color());
00133    cdBackOpacity(CD_TRANSPARENT);
00134 
00135    int width=0, height=0, xd=0, yd=0;
00136    position (&xd, &yd);
00137    if ( _mtexts[0][0]!='\0') 
00138     cdTextSize(_mtexts[0], &width, &height);
00139 
00140    /*
00141    switch ( align() )
00142     {
00143      case west: case center: case east:
00144      case northWest: case north: case northEast:
00145       if ( orientation()==horizontal ) yd += (2*height-1)/2;
00146       if ( orientation()==vertBotTop || orientation()==vertTopBot ) xd -= (height/2)*(_ntexts-1);
00147       break;
00148      case southWest: case south: case southEast:
00149      case leftBase: case centerBase: case rightBase:
00150       if ( orientation()==horizontal ) yd += (_ntexts-1)*height;
00151       if ( orientation()==vertBotTop || orientation()==vertTopBot ) xd -= (_ntexts/2-1)*height;
00152       break;
00153     }
00154     */
00155 
00156    if (orientation() == horizontal)  // orientação default
00157    {
00158       cdTextOrientation(0.0);
00159 
00160       for( int i=0; i<_ntexts; i++ )
00161        {
00162         cdText(xd, yd, _mtexts[i]);
00163         yd -= height;
00164        }
00165 
00166       cdTextOrientation(oldOrientation);
00167    }
00168    else if (orientation() == vertBotTop)
00169    {
00170       cdTextOrientation(90.0);
00171 
00172       for( int i=0; i<_ntexts; i++ )
00173        {
00174         int width, height;
00175         cdText(xd, yd, _mtexts[i]);
00176         cdTextSize(_mtexts[i], &width, &height);
00177         xd += height;
00178        }
00179 
00180       cdTextOrientation(oldOrientation);
00181    }
00182    else if (orientation() == vertTopBot)
00183    {
00184       cdTextOrientation(-90.0);
00185 
00186       for( int i=0; i<_ntexts; i++ )
00187        {
00188         int width, height;
00189         cdText(xd, yd, _mtexts[i]);
00190         cdTextSize(_mtexts[i], &width, &height);
00191         xd += height;
00192        }
00193 
00194       cdTextOrientation(oldOrientation);
00195    }
00196 
00197    // acrescenta o sublinhado, se houver
00198    if (underline())
00199    {
00200       int descent;
00201       cdFontDim(0, 0, 0, &descent);
00202       cdLineStyle(CD_SOLID);
00203       cdLineWidth((style() == bold || style() == boldItalic) ? 2 : 1);
00204 
00205       int bxmin, bxmax, bymin, bymax;
00206       boundingBox(bxmin, bymin, bxmax, bymax);
00207 
00208       if (orientation() == horizontal)
00209          cdLine(bxmin, bymin + descent, bxmax, bymin + descent);
00210       else if (orientation() == vertBotTop)
00211          cdLine(bxmax - descent, bymin, bxmax - descent, bymax);
00212       else
00213          cdLine(bxmin + descent, bymin, bxmin + descent, bymax);
00214    }
00215 
00216    cdClip(mode);
00217    // restaura a região de clip anterior
00218    cdClipArea(oldclip_xmin, oldclip_xmax, oldclip_ymin, oldclip_ymax);
00219 }
00220 
00221 void XYMultiRasterText::boundingBox (int& xmin, int& ymin, int& xmax, int& ymax)
00222                                 const
00223 {
00224    // teste de inconsistência
00225    if (text() == NULL) return;
00226 
00227    if (visible() == xyfalse) return;  // invisível!!!
00228 
00229    int xi, yi;
00230    position (&xi, &yi);
00231 
00232    cdFont(font(), style(), size());
00233    cdTextAlignment(align());
00234 
00235    if (orientation() == horizontal)  // orientação default
00236     cdTextOrientation(0.0);
00237    else if (orientation() == vertBotTop)
00238     cdTextOrientation(90.0);
00239    else if (orientation() == vertTopBot)
00240     cdTextOrientation(-90.0);
00241 
00242    int width=0, height=0;
00243    if ( _mtexts[0][0]!='\0' )
00244     cdTextSize(_mtexts[0], &width, &height);
00245 
00246    int xmn, xmx, ymn, ymx;
00247    for( int i=0; i<_ntexts; i++ )
00248     {
00249      if (orientation() == horizontal && i>0)  // orientação default
00250       yi -= height; 
00251      else if (orientation() == vertBotTop && i>0)
00252       xi += height; 
00253      else if (orientation() == vertTopBot && i>0)
00254       xi += height; 
00255 
00256      cdTextBox(xi, yi, _mtexts[i], &xmn, &xmx, &ymn, &ymx);
00257      if (xmn>xmx)
00258       {
00259        int tmp=xmn;
00260        xmn=xmx; xmx=tmp;
00261       }
00262      if (ymn>ymx)
00263       {
00264        int tmp=ymn;
00265        ymn=ymx; ymx=tmp;
00266       }
00267      if ( i==0 )
00268       {
00269        xmin = xmn; ymin = ymn;
00270        xmax = xmx; ymax = ymx;
00271       }
00272      else
00273       {
00274        if ( xmn<xmin ) xmin = xmn;
00275        if ( ymn<ymin ) ymin = ymn;
00276        if ( xmx>xmax ) xmax = xmx;
00277        if ( ymx>ymax ) ymax = ymx;
00278       }
00279    }
00280 
00281    switch ( align() )
00282     {
00283      case west: case center: case east:
00284      if ( orientation()==horizontal )
00285       {
00286        ymin += (2*height-1)/2;
00287        ymax += (2*height-1)/2;
00288       }
00289      else if ( orientation()==vertBotTop || orientation()==vertTopBot )
00290       {
00291        xmin -= (height/2)*(_ntexts-1);
00292        xmax -= (height/2)*(_ntexts-1);
00293       }
00294      break;
00295      case southWest: case south: case southEast:
00296      case leftBase: case centerBase: case rightBase:
00297       if ( orientation()==horizontal )
00298        {
00299         ymin += (_ntexts-1)*height;
00300         ymax += (_ntexts-1)*height;
00301        }
00302       else if ( orientation()==vertBotTop || orientation()==vertTopBot )
00303        {
00304         xmin -= (_ntexts/2-1)*height;
00305         xmax -= (_ntexts/2-1)*height;
00306        }
00307       break;
00308     }
00309 }
00310 

XY
Tecgraf / PUC-Rio - Computer Graphics Technology Group