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

  1. /* 
  2.    drvJAVA.cpp : This file is part of pstoedit
  3.    backend to generate a Java(TM) applet
  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 <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "drvjava.h"
  27.  
  28. drvJAVA::drvJAVA(const char * driveroptions_p,ostream & theoutStream,ostream & theerrStream): // Constructor
  29.     drvbase(driveroptions_p,theoutStream,theerrStream,
  30.         0, // if backend supports subpathes, else 0
  31.            // if subpathes are supported, the backend must deal with
  32.            // sequences of the following form
  33.            // moveto (start of subpath)
  34.            // lineto (a line segment)
  35.            // lineto 
  36.            // moveto (start of a new subpath)
  37.            // lineto (a line segment)
  38.            // lineto 
  39.            //
  40.            // If this argument is set to 0 each subpath is drawn 
  41.            // individually which might not necessarily represent
  42.            // the original drawing.
  43.  
  44.         0,  // if backend supports curves, else 0
  45.         0  // if backend supports elements with fill and edges
  46.     )
  47. {
  48. // driver specific initializations
  49. // and writing of header to output file
  50.     outf << "import java.applet.*;" << endl;
  51.     outf << "import java.awt.*;" << endl;
  52.     outf << "public class PsDrawing extends Applet" << endl;
  53.     outf << "{" << endl;
  54.     outf << "    public void init()" << endl;
  55.     outf << "    {" << endl;
  56.     outf << "        setBackground( Color.white );" << endl;
  57.     outf << "    }" << endl;
  58.     outf << "    public void paint( Graphics g )" << endl;
  59.     outf << "    {" << endl;
  60. }
  61.  
  62. drvJAVA::~drvJAVA() {
  63. // driver specific deallocations
  64. // and writing of trailer to output file
  65.     outf << "    }" << endl;
  66.     outf << "}" << endl;
  67. }
  68.  
  69. void drvJAVA::print_coords()
  70. {
  71.     outf << "\tPolygon p = new Polygon();" << endl;;
  72.     for (unsigned int n = 0; n < numberOfElementsInPath(); n++) {
  73.     const basedrawingelement & elem = pathElement(n);
  74.     switch (elem.getType()) {
  75.         case moveto: {
  76.             const Point & p = elem.getPoint(0);
  77.             outf << "\tp.addPoint(";
  78.                 outf  << (int) (p.x_ + x_offset) << "," 
  79.                      <<     (int) (currentDeviceHeight -     p.y_ + y_offset) << ");" ;
  80.             }
  81.             break;
  82.         case lineto: {
  83.             const Point & p = elem.getPoint(0);
  84.             outf << "\tp.addPoint(";
  85.                 outf  << (int) (p.x_ + x_offset) << "," 
  86.                      <<     (int) (currentDeviceHeight -     p.y_ + y_offset) << ");" ;
  87.             }
  88.             break;
  89.         case closepath: 
  90.             // outf << "\t\tclosepath ";
  91.             break;
  92.         case curveto:{
  93.             errf << "\t\tFatal: unexpected case in drvpdf " << endl;
  94.             abort();
  95.             }
  96.             break;
  97.         default:
  98.             errf << "\t\tFatal: unexpected case in drvpdf " << endl;
  99.             abort();
  100.             break;
  101.     }
  102.     outf << endl;
  103.     }
  104. }
  105.  
  106.  
  107. void drvJAVA::open_page()
  108. {
  109.     outf << "//Opening page: " << currentPageNumber << endl;
  110. }
  111.  
  112. void drvJAVA::close_page()
  113. {
  114.     outf << "//Closing page: " << (currentPageNumber) << endl;
  115. }
  116.  
  117. void drvJAVA::show_text(const TextInfo & textinfo)
  118. {
  119.     outf << "\t{" << endl;
  120.     outf << "\tColor c = new Color(" << currentR()<< "F," << currentG() << "F," << currentB()<< "F);" << endl;
  121.     outf << "\tg.setColor(c);" << endl;
  122.     outf << "\tg.setFont(new Font(new String(\"" <<    textinfo.currentFontName.value() << "\"),Font.PLAIN,"<< (int) (textinfo.currentFontSize +0.5) <<  ")); " << endl;
  123.     outf << "\tg.drawString(new String(\"" ;
  124.     // << textinfo.thetext 
  125.     for (const char * p = textinfo.thetext; (*p) != 0; p++ ){
  126.         if ((*p) == '"') {
  127.             outf << '\\' << *p;
  128.         } else if ((*p) == '\\') {
  129.             outf << '\\' << *p;
  130.         } else if ((*p) == (char) 13 ) { // ^M
  131.             outf << ' ';
  132.         } else {
  133.             outf << *p;
  134.         }
  135.     }
  136.     outf << "\")," << (int) (textinfo.x + x_offset) << "," << (int) (currentDeviceHeight - textinfo.y + y_offset) << ");" << endl;
  137.     outf << "\t}" << endl;
  138. }
  139.  
  140. void drvJAVA::show_path()
  141. {
  142.     outf << "\t{" << endl;
  143.     outf << "\t// Path # " << pathnumber << endl;;
  144.     outf << "\tColor c = new Color(" << currentR()<< "F," << currentG() << "F," << currentB()<< "F);" << endl;
  145.     outf << "\tg.setColor(c);" << endl;
  146.  
  147. // if fill then use a polygon
  148. // else use line-segments.
  149.         switch (currentShowType() ) {
  150.         case drvbase::stroke : {
  151.         for(unsigned int t=1;t<numberOfElementsInPath();t++) {
  152.             const Point & p = pathElement(t-1).getPoint(0);
  153.             const Point & q = pathElement(t).getPoint(0);
  154.             outf << "\tg.drawLine(" ;
  155.                     outf  << (int) (p.x_ + x_offset) << "," 
  156.                        << (int) (currentDeviceHeight -     p.y_ + y_offset) << "," ;
  157.                     outf  << (int) (q.x_ + x_offset) << "," 
  158.                        << (int) (currentDeviceHeight -  q.y_ + y_offset) << ");\n" ;
  159.         }
  160.         }
  161.               break;
  162.         case drvbase::fill :
  163.         case drvbase::eofill :
  164.         print_coords();
  165.             if (!isPolygon())  {
  166.             // make closed polygon anyway
  167.             const basedrawingelement & elem = pathElement(0);
  168.             const Point & p = elem.getPoint(0);
  169.             outf << "\tp.addPoint(";
  170.                     outf  << (int) (p.x_ + x_offset) << "," 
  171.                        << (int) (currentDeviceHeight -     p.y_ + y_offset) << ");\n " ;
  172.         }
  173.         outf << "\tg.fillPolygon(p);" << endl;
  174.               break;
  175.         default: 
  176.     // cannot happen
  177.               outf << "unexpected ShowType " << (int) currentShowType() ;
  178.               break;
  179.         }
  180.     outf << "\t}" << endl;
  181.     // outf << "\tcurrentLineWidth: " <<  currentLineWidth() << endl;
  182. };
  183.  
  184. void drvJAVA::show_rectangle(const float llx, const float lly, const float urx, const float ury)
  185. {
  186. //    outf << "Rectangle ( " << llx << "," << lly << ") (" << urx << "," << ury << ")" << endl;
  187. // just do show_path for a first guess
  188.       unused(&llx); unused(&lly); unused(&urx); unused(&ury);
  189.     show_path();
  190. }
  191.