Logo Search packages:      
Sourcecode: magics++ version File versions  Download package

MAGICS_NO_EXPORT void GDDriver::renderText ( const Text &  text  )  const [private, virtual]

renders text strings

This method renders given text strings.

GD offers two ways of plotting text: using own line fonts or using TTF fonts.

By default line fonts are used if MAGICS_TTF is not set (see configure & config.h).

Note:
Be careful: TTF text ploting seems to fail in multithread context (AIX Jan2006)
See also:
Text
Parameters:
text object containing the strings and their description

Reimplemented from magics::BaseDriver.

Definition at line 571 of file GDDriver.cc.

References magics::getEnvVariable(), magics::lowerCase(), and scaleFactor_.

{
#ifdef MAGICS_TTF
      if(text.empty()) return;

      const vector<NiceText>& niceT = text.getNiceText();
      if(niceT.empty() && (text.empty()) ) return;

      const float PNGASCENT = 0.12;
      const float PGNDESENT = 0.2;

      float H = 0.;
      float W = 0.;

      const float LastAngle_ = -text.getAngle();
      const float cs = cos(LastAngle_);
      const float ss = sin(LastAngle_);

      const enum Justification horizontal = text.getJustification();
      const enum VerticalAlign vertical   = text.getVerticalAlign();

      float HA = 0.; // MLEFT default
      if (horizontal==MCENTRE)     HA = -0.5;
      else if (horizontal==MRIGHT) HA = -1.;

      float VA = 0.;
      if (vertical==MBASE) VA = - PGNDESENT;
      else if (vertical==MTOP)    VA = 1 - PNGASCENT - PGNDESENT;
      else if (vertical==MHALF)   VA = 0.5 - PGNDESENT;
      else if (vertical==MBOTTOM) VA = 0.;

      string textString;

      vector<NiceText>::const_iterator niceText = text.textBegin();
      vector<NiceText>::const_iterator niceTextEnd = text.textEnd();

      int text_length = 0;

      for(;niceText<niceTextEnd;niceText++)
      {
            const MagFont magfont   = (*niceText).font();
            const string style      = (magfont.style()=="") ? "normal": magfont.style();
//          const bool underlined   = (magfont.style()=="underline") ? true : false; 
            const string font = magfont.name()+"_"+style;
            const string lowFont    = lowerCase(font);
            fontMapIter iter  = FontMap_.find(lowFont);

            string ttf = getEnvVariable("MAGPLUS_HOME") + "/" + MAGICS_TTF_PATH;
            if(iter!=FontMap_.end())
                  ttf += iter->second.ttf_filename;
            else
            {
                  ttf += FontMap_["sansserif_normal"].ttf_filename; // if not found get default
                  Log::warning() << "GDDriver: Font "<< font << " is not registered! Default font is used."<< endl;
            }

            // test if font files exists
            FILE* fontfile = fopen(ttf.c_str(),"r");
            if(!fontfile)
            {
                  const string new_font = lowerCase( "sansserif_"+style);
                  Log::warning() << "GDDriver (pretty): TTF font file "<< ttf <<" could not be found! Font "<< new_font<< " is used instead!"<< endl;
                  fontMapIter iter = FontMap_.find(new_font);

                  ttf = getEnvVariable("MAGPLUS_HOME") + "/" + MAGICS_TTF_PATH;
                  if(iter!=FontMap_.end())
                        ttf += iter->second.ttf_filename;
            }
            else fclose(fontfile);

            const float fontSize = scaleFactor_ * convertCM(magfont.size());

            int bbx[8];
            char *err = gdImageStringFT(NULL,&bbx[0],0,(char *)ttf.c_str(),fontSize,0.,10,10,(char*)(*niceText).text().c_str());
            if(err) Log::error() << "GDDriver::renderText() -> Could not determine text bounding box for >"<<(*niceText).text()<<"< !\n";

            text_length += (bbx[2] - bbx[0]);
       }

       text_length *= HA;
       offsetsX_.push(offsetX_);
       offsetX_ = offsetX_+text_length;

       int offset = 0;
       niceText = text.textBegin();
       for(;niceText<niceTextEnd;niceText++)
       {
            textString = (*niceText).text();
//          const int length =textString.length()+1;

            const MagFont magfont = (*niceText).font();
            const string style      = (magfont.style()=="") ? "normal": magfont.style();
//          const bool underlined = (magfont.style()=="underline") ? true : false; 
            const string font = magfont.name()+"_"+style;
            const string lowFont    = lowerCase(font);
            fontMapIter iter  = FontMap_.find(lowFont);
            const float fontSize = scaleFactor_ * convertCM(magfont.size());

            string ttf = getEnvVariable("MAGPLUS_HOME") + "/" + MAGICS_TTF_PATH;
            if(iter!=FontMap_.end())
                  ttf += iter->second.ttf_filename;
            else
            {
                  ttf += FontMap_["sansserif_normal"].ttf_filename; // if not found get default
                  Log::warning() << "GDDriver: Font "<< font << " is not registered! Default font is used."<< endl;
            }
      
            // test if font files exists
            if(currentFont_!=ttf)
            {
              FILE* fontfile = fopen(ttf.c_str(),"r");
              if(!fontfile)
              {
                  const string new_font = lowerCase( "sansserif_"+style);
                  Log::warning() << "GDDriver (pretty): TTF font file "<< ttf <<" could not be found! Font "<< new_font<< " is used instead!"<< endl;
                  fontMapIter iter = FontMap_.find(new_font);

                  ttf = getEnvVariable("MAGPLUS_HOME") + "/" + MAGICS_TTF_PATH;
                  if(iter!=FontMap_.end())
                        ttf += iter->second.ttf_filename;
              }
              else
              {
                  fclose(fontfile);
                  currentFont_=ttf;
              }
            }

            // get bbx to size text
            int bbx[8];
            char *err = gdImageStringFT(NULL,&bbx[0],0,(char *)ttf.c_str(),fontSize,0.,10,10,(char*)(*niceText).text().c_str());
      
            if (err)
            {
                  Log::error() << "GDDriver: gdImageStringFT (prettyText) -> "<< err <<" in "<<ttf<< endl;
                  return;
            }
            else
            {
                  //W = text_length;
                  W = (bbx[2] - bbx[0]);
                  H = (bbx[1] - bbx[7]);
            }

            const float E1x = 0;
            const float E1y = H*VA;
            const float E1xcsE1yss =  E1x*cs + E1y*ss + offset; 
            const float E1xssE1ycs = -E1x*ss + E1y*cs;
            offset += W;

//          const unsigned int tsize = (*niceText).text().size();
            setNewColour(magfont.colour());

        unsigned int noTexts = text.size();
        for(unsigned int nT=0;nT<noTexts;nT++)  // for all sting COORDINATES
        {
            const int x0 = static_cast<int>( E1xcsE1yss + projectX(text[nT].x()));//lower left
            const int y0 = static_cast<int>( E1xssE1ycs + projectY(text[nT].y()));

            if(text.getBlanking())
            {
                  err = gdImageStringFT(NULL,&bbx[0],0,(char *)ttf.c_str(),fontSize,LastAngle_,x0,y0,(char *)(*niceText).text().c_str());
                  if(!err)
                  {
                        const int white = gdImageColorAllocate(currentImage_, 255, 255, 255);
                        gdPoint points[4];
                        points[0].x = bbx[0];  points[0].y = bbx[1];
                        points[1].x = bbx[2];  points[1].y = bbx[3];
                        points[2].x = bbx[4];  points[2].y = bbx[5];
                        points[3].x = bbx[6];  points[3].y = bbx[7];
                        gdImageFilledPolygon(currentImage_, points, 4, white);
                  }
                  else Log::error() << "GDDriver: gdImageStringFT -> "<< err <<" -> NO blanking!!!"<< endl;
            }
            err = gdImageStringFT(currentImage_,&bbx[0],currentColourIndex_,(char *)ttf.c_str(),fontSize,LastAngle_,x0,y0,(char *)(*niceText).text().c_str());
            if (err)
            {
                  Log::error() << "GDDriver: gdImageStringFT -> "<< err << endl;
                  gdImageString(currentImage_,gdFontGetLarge(),x0,y0,(unsigned char*)(*niceText).text().c_str(),currentColourIndex_);
            }
            delete [] err;
        }
      }// endfor all text segments
      offsetX_ = offsetsX_.top();offsetsX_.pop();
#else
      Log::warning() << "GDDriver: No TTF fonts support in Magics++!" << endl;
      return;
#endif
}


Generated by  Doxygen 1.6.0   Back to index