home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / pstoedit.zip / source.zip / pstoedit.2.50 / src / drvtgif.cpp < prev    next >
C/C++ Source or Header  |  1996-10-29  |  8KB  |  267 lines

  1. /* 
  2.    drvTGIF.cpp : This file is part of pstoedit
  3.    Backend for TGIF
  4.  
  5.    Copyright (C) 1993,1994,1995,1996 Wolfgang Glunz, Wolfgang.Glunz@zfe.siemens.de
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. */
  22.  
  23. #include "drvtgif.h"
  24. #include <iostream.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. // for sin and cos
  29. #include <math.h>
  30.  
  31.  
  32. static const char * colorstring(float r, float g, float b)
  33. {
  34. static char buffer[10];
  35.     sprintf(buffer,"%s%.2x%.2x%.2x","#", (int) (r * 255), (int) ( g * 255) ,  (int) (b * 255));
  36.     return buffer;
  37. }
  38.  
  39. drvTGIF::drvTGIF(const char * driveroptions_p,ostream & theoutStream,ostream & theerrStream, float theMagnification):
  40.     drvbase(driveroptions_p,theoutStream,theerrStream,0,0,0),
  41.         buffer(tempFile.asOutput()),
  42.     objectId            (1),
  43.     magnification(theMagnification)
  44. {
  45.     // set tgif specific values
  46.     scale = 128.0 / (72.0 *magnification );        /* tgif internal scale factor */
  47.     currentDeviceHeight = 792.0 * scale;
  48.  
  49.     x_offset = 0.0;        /* set to fit to tgif page    */
  50.     y_offset = 89.61;        /*          "                 */
  51.  
  52.     // cannot write any header part, since we need the total number of pages
  53.     // in the header
  54. }
  55.  
  56. drvTGIF::~drvTGIF() 
  57. {
  58.         outf << "state(0,33," << (int) (magnification * 100) 
  59.      << ",0,0,1,16,1,9,1,1,0,0,1,0,1,0,'Courier',0,17,0,0,1,5,0,0,1,1,0,16,1,0,1," << currentPageNumber << ",1,0,1056,1497,0,0,2880)." << endl;
  60.     outf << "unit(\"1 pixel/pixel\")." << endl;
  61.     // now we can copy the buffer the output
  62.     ifstream & inbuffer = tempFile.asInput();
  63.     copy_file(inbuffer,outf);
  64. }
  65.  
  66. void drvTGIF::print_coords()
  67. {
  68.     for (unsigned int n = 0; n < numberOfElementsInPath(); n++) {
  69.     const Point & p = pathElement(n).getPoint(0);
  70.         buffer << p.x_ + x_offset ;
  71.     buffer << ',' << currentDeviceHeight - p.y_ + y_offset;
  72.     if (n != numberOfElementsInPath() - 1 ) {
  73.         // it is not the last point, so add a ,
  74.         buffer << ','; 
  75.     }
  76.     if ((!((n + 1) % 8)) && ((n + 1) != numberOfElementsInPath())) {
  77.         buffer << "\n\t";
  78.     }
  79.     }
  80. }
  81. void drvTGIF::close_page()
  82. {
  83. }
  84.  
  85. void drvTGIF::open_page()
  86. {
  87.     buffer << "page(" << currentPageNumber << ",\"\",1).\n";
  88. }
  89.  
  90. void drvTGIF::show_text(const TextInfo & textinfo)
  91. {
  92.     buffer << "text('" <<  colorstring(textinfo.currentR,textinfo.currentG,textinfo.currentB) << "'";
  93.     buffer << "," << textinfo.x + x_offset;
  94.     buffer << "," << currentDeviceHeight - textinfo.y + y_offset - textinfo.currentFontSize;
  95.     // TGIF's origin of text is at the top line, pstoedit's at the bottom
  96.     buffer << ",'" << textinfo.currentFontName.value() << "'";
  97.     int boldfont   =  (strstr(textinfo.currentFontName.value(),"Bold") != NULL);
  98.     int italicfont = ((strstr(textinfo.currentFontName.value(),"Italic") != NULL) ||
  99.               (strstr(textinfo.currentFontName.value(),"Oblique") != NULL));
  100.     int fonttype = 0;
  101.     if (boldfont) {
  102.     if (italicfont) fonttype = 3;
  103.     else            fonttype = 1;
  104.     } else {
  105.     if (italicfont) fonttype = 2;
  106.     else            fonttype = 0;
  107.     }
  108.     buffer << "," << fonttype <<  "," << (int) (textinfo.currentFontSize + 0.5) << ",1,0,0,1,70," << textinfo.currentFontSize << "," <<  objectId++ << ",0,14,3,0,0,0,0,0,0,0,0,0,0,\"\",0,";
  109.  
  110.     const bool rotated = textinfo.currentFontAngle != 0.0;
  111.     if (rotated) {
  112.         buffer << "1,0,[" << endl;
  113.     } else {
  114.         buffer << "0,0,[" << endl;
  115.     }
  116.     if (rotated) {
  117.     const float toRadians = 3.14159265359 / 180.0;
  118.     const float angleInRadians = textinfo.currentFontAngle * toRadians;
  119.     const float cosphi = cos(angleInRadians);
  120.     const float sinphi = sin(angleInRadians);
  121. //    const float Sx = textinfo.currentFontSize;
  122. //    const float Sy = textinfo.currentFontSize;
  123. //    const int len = strlen(textinfo.thetext);
  124.     buffer << '\t'<< textinfo.x + x_offset;
  125.     buffer << "," << currentDeviceHeight - textinfo.y + y_offset;
  126.  
  127.     // the obbox stuff
  128. #ifdef OLDTGIF
  129.     buffer << "," << textinfo.x + x_offset  ;
  130.     buffer << "," << currentDeviceHeight - textinfo.y + y_offset;
  131.     buffer << "," << textinfo.x + x_offset + len * textinfo.currentFontSize ;
  132.     buffer << "," << currentDeviceHeight - textinfo.y + textinfo.currentFontSize + y_offset;
  133. #else
  134.     // starting with tgif 3.0 pl 7. an all 0 bounding box causes tgif to recalculate it
  135.     buffer << "," << 0;
  136.     buffer << "," << 0;
  137.     buffer << "," << 0;
  138.     buffer << "," << 0;
  139. #endif
  140.  
  141.     // the CTM stuff
  142.     buffer << "," << cosphi * 1000 ;
  143.     buffer << "," << -sinphi * 1000 ;
  144.     buffer << "," << sinphi * 1000 ;
  145.     buffer << "," << cosphi * 1000 ;
  146.     buffer << "," << 0 ; // no translate
  147.     buffer << "," << 0 ; // no translate
  148.  
  149.     // the bbox stuff
  150. #ifdef OLDTGIF
  151.     buffer << "," << textinfo.x + x_offset  ;
  152.     buffer << "," << currentDeviceHeight - textinfo.y + y_offset;
  153.     buffer << "," << textinfo.x + x_offset + len * textinfo.currentFontSize ;
  154.     buffer << "," << currentDeviceHeight - textinfo.y + y_offset  + textinfo.currentFontSize + y_offset ;
  155. #else
  156.     // starting with tgif 3.0 pl 7. an all 0 bounding box causes tgif to recalculate it
  157.     buffer << "," << 0;
  158.     buffer << "," << 0;
  159.     buffer << "," << 0;
  160.     buffer << "," << 0;
  161. #endif
  162.  
  163.     buffer << "],[" << endl;
  164.     } 
  165.     buffer <<  "\t\"" ;
  166.     const char * cp = textinfo.thetext;
  167.     while (*cp) {
  168.     if ( (*cp  == '"') || (*cp == '\\') ) {
  169.         buffer << '\\';
  170.     }
  171.     buffer << *cp ;
  172.     cp++;
  173.     }
  174.     buffer << "\"]\n";
  175. }
  176.  
  177. static const int Fill   = 1;
  178. static const int noFill = 0;
  179. void drvTGIF::show_path()
  180. {
  181. //     buffer << "# Path " << pathnumber << " " << numberOfElementsInPath() << endl;
  182.     const int fillpat = (currentShowType() == drvbase::stroke) ? noFill : Fill;
  183.     if (isPolygon()) {
  184.     buffer << "polygon('"
  185.      << colorstring(currentR(),currentG(),currentB())
  186.      << "',"
  187.      << numberOfElementsInPath()
  188.      << ",["
  189.      << endl
  190.      << "\t";
  191.     print_coords();
  192.     buffer << "],"
  193.      << fillpat
  194.      << ","
  195.      << currentLineWidth()
  196.      << ","
  197.      << Fill
  198.      << ",0,"
  199.      << objectId++
  200.      << ",0,0,0,0,0,'1";
  201.     buffer << (int) (currentLineWidth() +0.5);
  202.     buffer << "',\n    \"";
  203.      for (unsigned int i = 0 ; i < numberOfElementsInPath() ; i = i + 4) {
  204.         if ( (i > 0) && ((i) % 256) == 0) {
  205.             buffer << "\n     " ;
  206.         }
  207.         buffer << '0';
  208.      }
  209.     buffer << "\",["
  210.      << endl
  211.      << "])."
  212.      << endl;
  213.     } else {
  214.     buffer << "poly('"
  215.      << colorstring(currentR(),currentG(),currentB())
  216.      << "'," 
  217.      << numberOfElementsInPath()
  218.      << ",[" 
  219.      << endl
  220.      << "\t";
  221.     print_coords();
  222.     buffer << "],0," 
  223.      << currentLineWidth()
  224.      << ","
  225.          << Fill 
  226.      << ","
  227.          << objectId++
  228.          << ",0,"
  229.          << fillpat
  230.          << ",0,0,0,3,0,0,0,'";
  231.     buffer << (int) (currentLineWidth() +0.5);
  232.     buffer <<    "','8','3',\n    \"";
  233.      for (unsigned int i = 0 ; i < numberOfElementsInPath() ; i = i + 4) {
  234.         if ( (i > 0) && ((i) % 256) == 0) {
  235.             buffer << "\n     " ;
  236.         }
  237.         buffer << '0';
  238.      }
  239.     buffer << "\",[" 
  240.      << endl
  241.          << "])." 
  242.      << endl;
  243.     }
  244. };
  245.  
  246. void drvTGIF::show_rectangle(const float llx, const float lly, const float urx, const float ury)
  247. {
  248.     buffer << "box('" <<  colorstring(currentR(),currentG(),currentB()) << "'";
  249.     buffer << "," << llx + x_offset;
  250.     buffer << "," <<  currentDeviceHeight - lly + y_offset;
  251.     buffer << "," <<  urx + x_offset;
  252.     buffer << "," <<  currentDeviceHeight - ury + y_offset;
  253.     const int fillpat = (currentShowType() == drvbase::stroke) ? noFill : Fill;
  254.     buffer << ","
  255.      << fillpat
  256.      << ","
  257.      << currentLineWidth() 
  258.      << ","
  259.      << Fill
  260.      << ","
  261.      << objectId++
  262.      << ",0,0,0,0,0,'1',["
  263.      << endl
  264.      << "])."
  265.      << endl;
  266. }
  267.